]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915/skl: Buffer translation improvements
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
e5510fac 39#include "i915_trace.h"
319c1d42 40#include <drm/drm_atomic.h>
c196e1d6 41#include <drm/drm_atomic_helper.h>
760285e7
DH
42#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
465c120c
MR
44#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
c0f372b3 46#include <linux/dma_remapping.h>
79e53945 47
465c120c 48/* Primary plane formats for gen <= 3 */
568db4f2 49static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
50 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
465c120c 52 DRM_FORMAT_XRGB1555,
67fe7dc5 53 DRM_FORMAT_XRGB8888,
465c120c
MR
54};
55
56/* Primary plane formats for gen >= 4 */
568db4f2 57static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
58 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
60 DRM_FORMAT_XRGB8888,
61 DRM_FORMAT_XBGR8888,
62 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
64};
65
66static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
465c120c 70 DRM_FORMAT_XBGR8888,
67fe7dc5 71 DRM_FORMAT_ARGB8888,
465c120c
MR
72 DRM_FORMAT_ABGR8888,
73 DRM_FORMAT_XRGB2101010,
465c120c 74 DRM_FORMAT_XBGR2101010,
465c120c
MR
75};
76
3d7d6510
MR
77/* Cursor formats */
78static const uint32_t intel_cursor_formats[] = {
79 DRM_FORMAT_ARGB8888,
80};
81
6b383a7f 82static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 83
f1f644dc 84static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 85 struct intel_crtc_state *pipe_config);
18442d08 86static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 87 struct intel_crtc_state *pipe_config);
f1f644dc 88
568c634a 89static int intel_set_mode(struct drm_atomic_state *state);
eb1bfe80
JB
90static int intel_framebuffer_init(struct drm_device *dev,
91 struct intel_framebuffer *ifb,
92 struct drm_mode_fb_cmd2 *mode_cmd,
93 struct drm_i915_gem_object *obj);
5b18e57c
DV
94static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
95static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 96static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
97 struct intel_link_m_n *m_n,
98 struct intel_link_m_n *m2_n2);
29407aab 99static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
100static void haswell_set_pipeconf(struct drm_crtc *crtc);
101static void intel_set_pipe_csc(struct drm_crtc *crtc);
d288f65f 102static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 103 const struct intel_crtc_state *pipe_config);
d288f65f 104static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 105 const struct intel_crtc_state *pipe_config);
ea2c67bb
MR
106static void intel_begin_crtc_commit(struct drm_crtc *crtc);
107static void intel_finish_crtc_commit(struct drm_crtc *crtc);
549e2bfb
CK
108static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
109 struct intel_crtc_state *crtc_state);
5ab7b0b7
ID
110static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
111 int num_connectors);
e7457a9a 112
0e32b39c
DA
113static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
114{
115 if (!connector->mst_port)
116 return connector->encoder;
117 else
118 return &connector->mst_port->mst_encoders[pipe]->base;
119}
120
79e53945 121typedef struct {
0206e353 122 int min, max;
79e53945
JB
123} intel_range_t;
124
125typedef struct {
0206e353
AJ
126 int dot_limit;
127 int p2_slow, p2_fast;
79e53945
JB
128} intel_p2_t;
129
d4906093
ML
130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
0206e353
AJ
132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
d4906093 134};
79e53945 135
d2acd215
DV
136int
137intel_pch_rawclk(struct drm_device *dev)
138{
139 struct drm_i915_private *dev_priv = dev->dev_private;
140
141 WARN_ON(!HAS_PCH_SPLIT(dev));
142
143 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
144}
145
021357ac
CW
146static inline u32 /* units of 100MHz */
147intel_fdi_link_freq(struct drm_device *dev)
148{
8b99e68c
CW
149 if (IS_GEN5(dev)) {
150 struct drm_i915_private *dev_priv = dev->dev_private;
151 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
152 } else
153 return 27;
021357ac
CW
154}
155
5d536e28 156static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 157 .dot = { .min = 25000, .max = 350000 },
9c333719 158 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 159 .n = { .min = 2, .max = 16 },
0206e353
AJ
160 .m = { .min = 96, .max = 140 },
161 .m1 = { .min = 18, .max = 26 },
162 .m2 = { .min = 6, .max = 16 },
163 .p = { .min = 4, .max = 128 },
164 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
165 .p2 = { .dot_limit = 165000,
166 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
167};
168
5d536e28
DV
169static const intel_limit_t intel_limits_i8xx_dvo = {
170 .dot = { .min = 25000, .max = 350000 },
9c333719 171 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 172 .n = { .min = 2, .max = 16 },
5d536e28
DV
173 .m = { .min = 96, .max = 140 },
174 .m1 = { .min = 18, .max = 26 },
175 .m2 = { .min = 6, .max = 16 },
176 .p = { .min = 4, .max = 128 },
177 .p1 = { .min = 2, .max = 33 },
178 .p2 = { .dot_limit = 165000,
179 .p2_slow = 4, .p2_fast = 4 },
180};
181
e4b36699 182static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 183 .dot = { .min = 25000, .max = 350000 },
9c333719 184 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 185 .n = { .min = 2, .max = 16 },
0206e353
AJ
186 .m = { .min = 96, .max = 140 },
187 .m1 = { .min = 18, .max = 26 },
188 .m2 = { .min = 6, .max = 16 },
189 .p = { .min = 4, .max = 128 },
190 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
191 .p2 = { .dot_limit = 165000,
192 .p2_slow = 14, .p2_fast = 7 },
e4b36699 193};
273e27ca 194
e4b36699 195static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
196 .dot = { .min = 20000, .max = 400000 },
197 .vco = { .min = 1400000, .max = 2800000 },
198 .n = { .min = 1, .max = 6 },
199 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
200 .m1 = { .min = 8, .max = 18 },
201 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
202 .p = { .min = 5, .max = 80 },
203 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
204 .p2 = { .dot_limit = 200000,
205 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
206};
207
208static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
209 .dot = { .min = 20000, .max = 400000 },
210 .vco = { .min = 1400000, .max = 2800000 },
211 .n = { .min = 1, .max = 6 },
212 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
213 .m1 = { .min = 8, .max = 18 },
214 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
215 .p = { .min = 7, .max = 98 },
216 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
217 .p2 = { .dot_limit = 112000,
218 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
219};
220
273e27ca 221
e4b36699 222static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
223 .dot = { .min = 25000, .max = 270000 },
224 .vco = { .min = 1750000, .max = 3500000},
225 .n = { .min = 1, .max = 4 },
226 .m = { .min = 104, .max = 138 },
227 .m1 = { .min = 17, .max = 23 },
228 .m2 = { .min = 5, .max = 11 },
229 .p = { .min = 10, .max = 30 },
230 .p1 = { .min = 1, .max = 3},
231 .p2 = { .dot_limit = 270000,
232 .p2_slow = 10,
233 .p2_fast = 10
044c7c41 234 },
e4b36699
KP
235};
236
237static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
238 .dot = { .min = 22000, .max = 400000 },
239 .vco = { .min = 1750000, .max = 3500000},
240 .n = { .min = 1, .max = 4 },
241 .m = { .min = 104, .max = 138 },
242 .m1 = { .min = 16, .max = 23 },
243 .m2 = { .min = 5, .max = 11 },
244 .p = { .min = 5, .max = 80 },
245 .p1 = { .min = 1, .max = 8},
246 .p2 = { .dot_limit = 165000,
247 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
248};
249
250static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
251 .dot = { .min = 20000, .max = 115000 },
252 .vco = { .min = 1750000, .max = 3500000 },
253 .n = { .min = 1, .max = 3 },
254 .m = { .min = 104, .max = 138 },
255 .m1 = { .min = 17, .max = 23 },
256 .m2 = { .min = 5, .max = 11 },
257 .p = { .min = 28, .max = 112 },
258 .p1 = { .min = 2, .max = 8 },
259 .p2 = { .dot_limit = 0,
260 .p2_slow = 14, .p2_fast = 14
044c7c41 261 },
e4b36699
KP
262};
263
264static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
265 .dot = { .min = 80000, .max = 224000 },
266 .vco = { .min = 1750000, .max = 3500000 },
267 .n = { .min = 1, .max = 3 },
268 .m = { .min = 104, .max = 138 },
269 .m1 = { .min = 17, .max = 23 },
270 .m2 = { .min = 5, .max = 11 },
271 .p = { .min = 14, .max = 42 },
272 .p1 = { .min = 2, .max = 6 },
273 .p2 = { .dot_limit = 0,
274 .p2_slow = 7, .p2_fast = 7
044c7c41 275 },
e4b36699
KP
276};
277
f2b115e6 278static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
279 .dot = { .min = 20000, .max = 400000},
280 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 281 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
282 .n = { .min = 3, .max = 6 },
283 .m = { .min = 2, .max = 256 },
273e27ca 284 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
285 .m1 = { .min = 0, .max = 0 },
286 .m2 = { .min = 0, .max = 254 },
287 .p = { .min = 5, .max = 80 },
288 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
289 .p2 = { .dot_limit = 200000,
290 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
291};
292
f2b115e6 293static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
294 .dot = { .min = 20000, .max = 400000 },
295 .vco = { .min = 1700000, .max = 3500000 },
296 .n = { .min = 3, .max = 6 },
297 .m = { .min = 2, .max = 256 },
298 .m1 = { .min = 0, .max = 0 },
299 .m2 = { .min = 0, .max = 254 },
300 .p = { .min = 7, .max = 112 },
301 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
302 .p2 = { .dot_limit = 112000,
303 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
304};
305
273e27ca
EA
306/* Ironlake / Sandybridge
307 *
308 * We calculate clock using (register_value + 2) for N/M1/M2, so here
309 * the range value for them is (actual_value - 2).
310 */
b91ad0ec 311static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
312 .dot = { .min = 25000, .max = 350000 },
313 .vco = { .min = 1760000, .max = 3510000 },
314 .n = { .min = 1, .max = 5 },
315 .m = { .min = 79, .max = 127 },
316 .m1 = { .min = 12, .max = 22 },
317 .m2 = { .min = 5, .max = 9 },
318 .p = { .min = 5, .max = 80 },
319 .p1 = { .min = 1, .max = 8 },
320 .p2 = { .dot_limit = 225000,
321 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
322};
323
b91ad0ec 324static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
325 .dot = { .min = 25000, .max = 350000 },
326 .vco = { .min = 1760000, .max = 3510000 },
327 .n = { .min = 1, .max = 3 },
328 .m = { .min = 79, .max = 118 },
329 .m1 = { .min = 12, .max = 22 },
330 .m2 = { .min = 5, .max = 9 },
331 .p = { .min = 28, .max = 112 },
332 .p1 = { .min = 2, .max = 8 },
333 .p2 = { .dot_limit = 225000,
334 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
335};
336
337static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
338 .dot = { .min = 25000, .max = 350000 },
339 .vco = { .min = 1760000, .max = 3510000 },
340 .n = { .min = 1, .max = 3 },
341 .m = { .min = 79, .max = 127 },
342 .m1 = { .min = 12, .max = 22 },
343 .m2 = { .min = 5, .max = 9 },
344 .p = { .min = 14, .max = 56 },
345 .p1 = { .min = 2, .max = 8 },
346 .p2 = { .dot_limit = 225000,
347 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
348};
349
273e27ca 350/* LVDS 100mhz refclk limits. */
b91ad0ec 351static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
352 .dot = { .min = 25000, .max = 350000 },
353 .vco = { .min = 1760000, .max = 3510000 },
354 .n = { .min = 1, .max = 2 },
355 .m = { .min = 79, .max = 126 },
356 .m1 = { .min = 12, .max = 22 },
357 .m2 = { .min = 5, .max = 9 },
358 .p = { .min = 28, .max = 112 },
0206e353 359 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
360 .p2 = { .dot_limit = 225000,
361 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
362};
363
364static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
365 .dot = { .min = 25000, .max = 350000 },
366 .vco = { .min = 1760000, .max = 3510000 },
367 .n = { .min = 1, .max = 3 },
368 .m = { .min = 79, .max = 126 },
369 .m1 = { .min = 12, .max = 22 },
370 .m2 = { .min = 5, .max = 9 },
371 .p = { .min = 14, .max = 42 },
0206e353 372 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
373 .p2 = { .dot_limit = 225000,
374 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
375};
376
dc730512 377static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
378 /*
379 * These are the data rate limits (measured in fast clocks)
380 * since those are the strictest limits we have. The fast
381 * clock and actual rate limits are more relaxed, so checking
382 * them would make no difference.
383 */
384 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 385 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 386 .n = { .min = 1, .max = 7 },
a0c4da24
JB
387 .m1 = { .min = 2, .max = 3 },
388 .m2 = { .min = 11, .max = 156 },
b99ab663 389 .p1 = { .min = 2, .max = 3 },
5fdc9c49 390 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
391};
392
ef9348c8
CML
393static const intel_limit_t intel_limits_chv = {
394 /*
395 * These are the data rate limits (measured in fast clocks)
396 * since those are the strictest limits we have. The fast
397 * clock and actual rate limits are more relaxed, so checking
398 * them would make no difference.
399 */
400 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 401 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
402 .n = { .min = 1, .max = 1 },
403 .m1 = { .min = 2, .max = 2 },
404 .m2 = { .min = 24 << 22, .max = 175 << 22 },
405 .p1 = { .min = 2, .max = 4 },
406 .p2 = { .p2_slow = 1, .p2_fast = 14 },
407};
408
5ab7b0b7
ID
409static const intel_limit_t intel_limits_bxt = {
410 /* FIXME: find real dot limits */
411 .dot = { .min = 0, .max = INT_MAX },
412 .vco = { .min = 4800000, .max = 6480000 },
413 .n = { .min = 1, .max = 1 },
414 .m1 = { .min = 2, .max = 2 },
415 /* FIXME: find real m2 limits */
416 .m2 = { .min = 2 << 22, .max = 255 << 22 },
417 .p1 = { .min = 2, .max = 4 },
418 .p2 = { .p2_slow = 1, .p2_fast = 20 },
419};
420
6b4bf1c4
VS
421static void vlv_clock(int refclk, intel_clock_t *clock)
422{
423 clock->m = clock->m1 * clock->m2;
424 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
425 if (WARN_ON(clock->n == 0 || clock->p == 0))
426 return;
fb03ac01
VS
427 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
428 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
429}
430
cdba954e
ACO
431static bool
432needs_modeset(struct drm_crtc_state *state)
433{
434 return state->mode_changed || state->active_changed;
435}
436
e0638cdf
PZ
437/**
438 * Returns whether any output on the specified pipe is of the specified type
439 */
4093561b 440bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 441{
409ee761 442 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
443 struct intel_encoder *encoder;
444
409ee761 445 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
446 if (encoder->type == type)
447 return true;
448
449 return false;
450}
451
d0737e1d
ACO
452/**
453 * Returns whether any output on the specified pipe will have the specified
454 * type after a staged modeset is complete, i.e., the same as
455 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
456 * encoder->crtc.
457 */
a93e255f
ACO
458static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
459 int type)
d0737e1d 460{
a93e255f 461 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 462 struct drm_connector *connector;
a93e255f 463 struct drm_connector_state *connector_state;
d0737e1d 464 struct intel_encoder *encoder;
a93e255f
ACO
465 int i, num_connectors = 0;
466
da3ced29 467 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
468 if (connector_state->crtc != crtc_state->base.crtc)
469 continue;
470
471 num_connectors++;
d0737e1d 472
a93e255f
ACO
473 encoder = to_intel_encoder(connector_state->best_encoder);
474 if (encoder->type == type)
d0737e1d 475 return true;
a93e255f
ACO
476 }
477
478 WARN_ON(num_connectors == 0);
d0737e1d
ACO
479
480 return false;
481}
482
a93e255f
ACO
483static const intel_limit_t *
484intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
2c07245f 485{
a93e255f 486 struct drm_device *dev = crtc_state->base.crtc->dev;
2c07245f 487 const intel_limit_t *limit;
b91ad0ec 488
a93e255f 489 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 490 if (intel_is_dual_link_lvds(dev)) {
1b894b59 491 if (refclk == 100000)
b91ad0ec
ZW
492 limit = &intel_limits_ironlake_dual_lvds_100m;
493 else
494 limit = &intel_limits_ironlake_dual_lvds;
495 } else {
1b894b59 496 if (refclk == 100000)
b91ad0ec
ZW
497 limit = &intel_limits_ironlake_single_lvds_100m;
498 else
499 limit = &intel_limits_ironlake_single_lvds;
500 }
c6bb3538 501 } else
b91ad0ec 502 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
503
504 return limit;
505}
506
a93e255f
ACO
507static const intel_limit_t *
508intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 509{
a93e255f 510 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
511 const intel_limit_t *limit;
512
a93e255f 513 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 514 if (intel_is_dual_link_lvds(dev))
e4b36699 515 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 516 else
e4b36699 517 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
518 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
519 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 520 limit = &intel_limits_g4x_hdmi;
a93e255f 521 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 522 limit = &intel_limits_g4x_sdvo;
044c7c41 523 } else /* The option is for other outputs */
e4b36699 524 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
525
526 return limit;
527}
528
a93e255f
ACO
529static const intel_limit_t *
530intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 531{
a93e255f 532 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
533 const intel_limit_t *limit;
534
5ab7b0b7
ID
535 if (IS_BROXTON(dev))
536 limit = &intel_limits_bxt;
537 else if (HAS_PCH_SPLIT(dev))
a93e255f 538 limit = intel_ironlake_limit(crtc_state, refclk);
2c07245f 539 else if (IS_G4X(dev)) {
a93e255f 540 limit = intel_g4x_limit(crtc_state);
f2b115e6 541 } else if (IS_PINEVIEW(dev)) {
a93e255f 542 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 543 limit = &intel_limits_pineview_lvds;
2177832f 544 else
f2b115e6 545 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
546 } else if (IS_CHERRYVIEW(dev)) {
547 limit = &intel_limits_chv;
a0c4da24 548 } else if (IS_VALLEYVIEW(dev)) {
dc730512 549 limit = &intel_limits_vlv;
a6c45cf0 550 } else if (!IS_GEN2(dev)) {
a93e255f 551 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
552 limit = &intel_limits_i9xx_lvds;
553 else
554 limit = &intel_limits_i9xx_sdvo;
79e53945 555 } else {
a93e255f 556 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 557 limit = &intel_limits_i8xx_lvds;
a93e255f 558 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 559 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
560 else
561 limit = &intel_limits_i8xx_dac;
79e53945
JB
562 }
563 return limit;
564}
565
f2b115e6
AJ
566/* m1 is reserved as 0 in Pineview, n is a ring counter */
567static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 568{
2177832f
SL
569 clock->m = clock->m2 + 2;
570 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
571 if (WARN_ON(clock->n == 0 || clock->p == 0))
572 return;
fb03ac01
VS
573 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
574 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
575}
576
7429e9d4
DV
577static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
578{
579 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
580}
581
ac58c3f0 582static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 583{
7429e9d4 584 clock->m = i9xx_dpll_compute_m(clock);
79e53945 585 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
586 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
587 return;
fb03ac01
VS
588 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
589 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
590}
591
ef9348c8
CML
592static void chv_clock(int refclk, intel_clock_t *clock)
593{
594 clock->m = clock->m1 * clock->m2;
595 clock->p = clock->p1 * clock->p2;
596 if (WARN_ON(clock->n == 0 || clock->p == 0))
597 return;
598 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
599 clock->n << 22);
600 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
601}
602
7c04d1d9 603#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
604/**
605 * Returns whether the given set of divisors are valid for a given refclk with
606 * the given connectors.
607 */
608
1b894b59
CW
609static bool intel_PLL_is_valid(struct drm_device *dev,
610 const intel_limit_t *limit,
611 const intel_clock_t *clock)
79e53945 612{
f01b7962
VS
613 if (clock->n < limit->n.min || limit->n.max < clock->n)
614 INTELPllInvalid("n out of range\n");
79e53945 615 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 616 INTELPllInvalid("p1 out of range\n");
79e53945 617 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 618 INTELPllInvalid("m2 out of range\n");
79e53945 619 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 620 INTELPllInvalid("m1 out of range\n");
f01b7962 621
5ab7b0b7 622 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
623 if (clock->m1 <= clock->m2)
624 INTELPllInvalid("m1 <= m2\n");
625
5ab7b0b7 626 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
627 if (clock->p < limit->p.min || limit->p.max < clock->p)
628 INTELPllInvalid("p out of range\n");
629 if (clock->m < limit->m.min || limit->m.max < clock->m)
630 INTELPllInvalid("m out of range\n");
631 }
632
79e53945 633 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 634 INTELPllInvalid("vco out of range\n");
79e53945
JB
635 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
636 * connector, etc., rather than just a single range.
637 */
638 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 639 INTELPllInvalid("dot out of range\n");
79e53945
JB
640
641 return true;
642}
643
3b1429d9
VS
644static int
645i9xx_select_p2_div(const intel_limit_t *limit,
646 const struct intel_crtc_state *crtc_state,
647 int target)
79e53945 648{
3b1429d9 649 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 650
a93e255f 651 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 652 /*
a210b028
DV
653 * For LVDS just rely on its current settings for dual-channel.
654 * We haven't figured out how to reliably set up different
655 * single/dual channel state, if we even can.
79e53945 656 */
1974cad0 657 if (intel_is_dual_link_lvds(dev))
3b1429d9 658 return limit->p2.p2_fast;
79e53945 659 else
3b1429d9 660 return limit->p2.p2_slow;
79e53945
JB
661 } else {
662 if (target < limit->p2.dot_limit)
3b1429d9 663 return limit->p2.p2_slow;
79e53945 664 else
3b1429d9 665 return limit->p2.p2_fast;
79e53945 666 }
3b1429d9
VS
667}
668
669static bool
670i9xx_find_best_dpll(const intel_limit_t *limit,
671 struct intel_crtc_state *crtc_state,
672 int target, int refclk, intel_clock_t *match_clock,
673 intel_clock_t *best_clock)
674{
675 struct drm_device *dev = crtc_state->base.crtc->dev;
676 intel_clock_t clock;
677 int err = target;
79e53945 678
0206e353 679 memset(best_clock, 0, sizeof(*best_clock));
79e53945 680
3b1429d9
VS
681 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
682
42158660
ZY
683 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
684 clock.m1++) {
685 for (clock.m2 = limit->m2.min;
686 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 687 if (clock.m2 >= clock.m1)
42158660
ZY
688 break;
689 for (clock.n = limit->n.min;
690 clock.n <= limit->n.max; clock.n++) {
691 for (clock.p1 = limit->p1.min;
692 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
693 int this_err;
694
ac58c3f0
DV
695 i9xx_clock(refclk, &clock);
696 if (!intel_PLL_is_valid(dev, limit,
697 &clock))
698 continue;
699 if (match_clock &&
700 clock.p != match_clock->p)
701 continue;
702
703 this_err = abs(clock.dot - target);
704 if (this_err < err) {
705 *best_clock = clock;
706 err = this_err;
707 }
708 }
709 }
710 }
711 }
712
713 return (err != target);
714}
715
716static bool
a93e255f
ACO
717pnv_find_best_dpll(const intel_limit_t *limit,
718 struct intel_crtc_state *crtc_state,
ee9300bb
DV
719 int target, int refclk, intel_clock_t *match_clock,
720 intel_clock_t *best_clock)
79e53945 721{
3b1429d9 722 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 723 intel_clock_t clock;
79e53945
JB
724 int err = target;
725
0206e353 726 memset(best_clock, 0, sizeof(*best_clock));
79e53945 727
3b1429d9
VS
728 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
729
42158660
ZY
730 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
731 clock.m1++) {
732 for (clock.m2 = limit->m2.min;
733 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
734 for (clock.n = limit->n.min;
735 clock.n <= limit->n.max; clock.n++) {
736 for (clock.p1 = limit->p1.min;
737 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
738 int this_err;
739
ac58c3f0 740 pineview_clock(refclk, &clock);
1b894b59
CW
741 if (!intel_PLL_is_valid(dev, limit,
742 &clock))
79e53945 743 continue;
cec2f356
SP
744 if (match_clock &&
745 clock.p != match_clock->p)
746 continue;
79e53945
JB
747
748 this_err = abs(clock.dot - target);
749 if (this_err < err) {
750 *best_clock = clock;
751 err = this_err;
752 }
753 }
754 }
755 }
756 }
757
758 return (err != target);
759}
760
d4906093 761static bool
a93e255f
ACO
762g4x_find_best_dpll(const intel_limit_t *limit,
763 struct intel_crtc_state *crtc_state,
ee9300bb
DV
764 int target, int refclk, intel_clock_t *match_clock,
765 intel_clock_t *best_clock)
d4906093 766{
3b1429d9 767 struct drm_device *dev = crtc_state->base.crtc->dev;
d4906093
ML
768 intel_clock_t clock;
769 int max_n;
3b1429d9 770 bool found = false;
6ba770dc
AJ
771 /* approximately equals target * 0.00585 */
772 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
773
774 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
775
776 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
777
d4906093 778 max_n = limit->n.max;
f77f13e2 779 /* based on hardware requirement, prefer smaller n to precision */
d4906093 780 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 781 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
782 for (clock.m1 = limit->m1.max;
783 clock.m1 >= limit->m1.min; clock.m1--) {
784 for (clock.m2 = limit->m2.max;
785 clock.m2 >= limit->m2.min; clock.m2--) {
786 for (clock.p1 = limit->p1.max;
787 clock.p1 >= limit->p1.min; clock.p1--) {
788 int this_err;
789
ac58c3f0 790 i9xx_clock(refclk, &clock);
1b894b59
CW
791 if (!intel_PLL_is_valid(dev, limit,
792 &clock))
d4906093 793 continue;
1b894b59
CW
794
795 this_err = abs(clock.dot - target);
d4906093
ML
796 if (this_err < err_most) {
797 *best_clock = clock;
798 err_most = this_err;
799 max_n = clock.n;
800 found = true;
801 }
802 }
803 }
804 }
805 }
2c07245f
ZW
806 return found;
807}
808
d5dd62bd
ID
809/*
810 * Check if the calculated PLL configuration is more optimal compared to the
811 * best configuration and error found so far. Return the calculated error.
812 */
813static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
814 const intel_clock_t *calculated_clock,
815 const intel_clock_t *best_clock,
816 unsigned int best_error_ppm,
817 unsigned int *error_ppm)
818{
9ca3ba01
ID
819 /*
820 * For CHV ignore the error and consider only the P value.
821 * Prefer a bigger P value based on HW requirements.
822 */
823 if (IS_CHERRYVIEW(dev)) {
824 *error_ppm = 0;
825
826 return calculated_clock->p > best_clock->p;
827 }
828
24be4e46
ID
829 if (WARN_ON_ONCE(!target_freq))
830 return false;
831
d5dd62bd
ID
832 *error_ppm = div_u64(1000000ULL *
833 abs(target_freq - calculated_clock->dot),
834 target_freq);
835 /*
836 * Prefer a better P value over a better (smaller) error if the error
837 * is small. Ensure this preference for future configurations too by
838 * setting the error to 0.
839 */
840 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
841 *error_ppm = 0;
842
843 return true;
844 }
845
846 return *error_ppm + 10 < best_error_ppm;
847}
848
a0c4da24 849static bool
a93e255f
ACO
850vlv_find_best_dpll(const intel_limit_t *limit,
851 struct intel_crtc_state *crtc_state,
ee9300bb
DV
852 int target, int refclk, intel_clock_t *match_clock,
853 intel_clock_t *best_clock)
a0c4da24 854{
a93e255f 855 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 856 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 857 intel_clock_t clock;
69e4f900 858 unsigned int bestppm = 1000000;
27e639bf
VS
859 /* min update 19.2 MHz */
860 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 861 bool found = false;
a0c4da24 862
6b4bf1c4
VS
863 target *= 5; /* fast clock */
864
865 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
866
867 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 868 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 869 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 870 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 871 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 872 clock.p = clock.p1 * clock.p2;
a0c4da24 873 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 874 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 875 unsigned int ppm;
69e4f900 876
6b4bf1c4
VS
877 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
878 refclk * clock.m1);
879
880 vlv_clock(refclk, &clock);
43b0ac53 881
f01b7962
VS
882 if (!intel_PLL_is_valid(dev, limit,
883 &clock))
43b0ac53
VS
884 continue;
885
d5dd62bd
ID
886 if (!vlv_PLL_is_optimal(dev, target,
887 &clock,
888 best_clock,
889 bestppm, &ppm))
890 continue;
6b4bf1c4 891
d5dd62bd
ID
892 *best_clock = clock;
893 bestppm = ppm;
894 found = true;
a0c4da24
JB
895 }
896 }
897 }
898 }
a0c4da24 899
49e497ef 900 return found;
a0c4da24 901}
a4fc5ed6 902
ef9348c8 903static bool
a93e255f
ACO
904chv_find_best_dpll(const intel_limit_t *limit,
905 struct intel_crtc_state *crtc_state,
ef9348c8
CML
906 int target, int refclk, intel_clock_t *match_clock,
907 intel_clock_t *best_clock)
908{
a93e255f 909 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 910 struct drm_device *dev = crtc->base.dev;
9ca3ba01 911 unsigned int best_error_ppm;
ef9348c8
CML
912 intel_clock_t clock;
913 uint64_t m2;
914 int found = false;
915
916 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 917 best_error_ppm = 1000000;
ef9348c8
CML
918
919 /*
920 * Based on hardware doc, the n always set to 1, and m1 always
921 * set to 2. If requires to support 200Mhz refclk, we need to
922 * revisit this because n may not 1 anymore.
923 */
924 clock.n = 1, clock.m1 = 2;
925 target *= 5; /* fast clock */
926
927 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
928 for (clock.p2 = limit->p2.p2_fast;
929 clock.p2 >= limit->p2.p2_slow;
930 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 931 unsigned int error_ppm;
ef9348c8
CML
932
933 clock.p = clock.p1 * clock.p2;
934
935 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
936 clock.n) << 22, refclk * clock.m1);
937
938 if (m2 > INT_MAX/clock.m1)
939 continue;
940
941 clock.m2 = m2;
942
943 chv_clock(refclk, &clock);
944
945 if (!intel_PLL_is_valid(dev, limit, &clock))
946 continue;
947
9ca3ba01
ID
948 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
949 best_error_ppm, &error_ppm))
950 continue;
951
952 *best_clock = clock;
953 best_error_ppm = error_ppm;
954 found = true;
ef9348c8
CML
955 }
956 }
957
958 return found;
959}
960
5ab7b0b7
ID
961bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
962 intel_clock_t *best_clock)
963{
964 int refclk = i9xx_get_refclk(crtc_state, 0);
965
966 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
967 target_clock, refclk, NULL, best_clock);
968}
969
20ddf665
VS
970bool intel_crtc_active(struct drm_crtc *crtc)
971{
972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
973
974 /* Be paranoid as we can arrive here with only partial
975 * state retrieved from the hardware during setup.
976 *
241bfc38 977 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
978 * as Haswell has gained clock readout/fastboot support.
979 *
66e514c1 980 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 981 * properly reconstruct framebuffers.
c3d1f436
MR
982 *
983 * FIXME: The intel_crtc->active here should be switched to
984 * crtc->state->active once we have proper CRTC states wired up
985 * for atomic.
20ddf665 986 */
c3d1f436 987 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 988 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
989}
990
a5c961d1
PZ
991enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
992 enum pipe pipe)
993{
994 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
996
6e3c9717 997 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
998}
999
fbf49ea2
VS
1000static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1001{
1002 struct drm_i915_private *dev_priv = dev->dev_private;
1003 u32 reg = PIPEDSL(pipe);
1004 u32 line1, line2;
1005 u32 line_mask;
1006
1007 if (IS_GEN2(dev))
1008 line_mask = DSL_LINEMASK_GEN2;
1009 else
1010 line_mask = DSL_LINEMASK_GEN3;
1011
1012 line1 = I915_READ(reg) & line_mask;
1013 mdelay(5);
1014 line2 = I915_READ(reg) & line_mask;
1015
1016 return line1 == line2;
1017}
1018
ab7ad7f6
KP
1019/*
1020 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1021 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1022 *
1023 * After disabling a pipe, we can't wait for vblank in the usual way,
1024 * spinning on the vblank interrupt status bit, since we won't actually
1025 * see an interrupt when the pipe is disabled.
1026 *
ab7ad7f6
KP
1027 * On Gen4 and above:
1028 * wait for the pipe register state bit to turn off
1029 *
1030 * Otherwise:
1031 * wait for the display line value to settle (it usually
1032 * ends up stopping at the start of the next frame).
58e10eb9 1033 *
9d0498a2 1034 */
575f7ab7 1035static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1036{
575f7ab7 1037 struct drm_device *dev = crtc->base.dev;
9d0498a2 1038 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1039 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1040 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1041
1042 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 1043 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1044
1045 /* Wait for the Pipe State to go off */
58e10eb9
CW
1046 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1047 100))
284637d9 1048 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1049 } else {
ab7ad7f6 1050 /* Wait for the display line to settle */
fbf49ea2 1051 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1052 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1053 }
79e53945
JB
1054}
1055
b0ea7d37
DL
1056/*
1057 * ibx_digital_port_connected - is the specified port connected?
1058 * @dev_priv: i915 private structure
1059 * @port: the port to test
1060 *
1061 * Returns true if @port is connected, false otherwise.
1062 */
1063bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1064 struct intel_digital_port *port)
1065{
1066 u32 bit;
1067
c36346e3 1068 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1069 switch (port->port) {
c36346e3
DL
1070 case PORT_B:
1071 bit = SDE_PORTB_HOTPLUG;
1072 break;
1073 case PORT_C:
1074 bit = SDE_PORTC_HOTPLUG;
1075 break;
1076 case PORT_D:
1077 bit = SDE_PORTD_HOTPLUG;
1078 break;
1079 default:
1080 return true;
1081 }
1082 } else {
eba905b2 1083 switch (port->port) {
c36346e3
DL
1084 case PORT_B:
1085 bit = SDE_PORTB_HOTPLUG_CPT;
1086 break;
1087 case PORT_C:
1088 bit = SDE_PORTC_HOTPLUG_CPT;
1089 break;
1090 case PORT_D:
1091 bit = SDE_PORTD_HOTPLUG_CPT;
1092 break;
1093 default:
1094 return true;
1095 }
b0ea7d37
DL
1096 }
1097
1098 return I915_READ(SDEISR) & bit;
1099}
1100
b24e7179
JB
1101static const char *state_string(bool enabled)
1102{
1103 return enabled ? "on" : "off";
1104}
1105
1106/* Only for pre-ILK configs */
55607e8a
DV
1107void assert_pll(struct drm_i915_private *dev_priv,
1108 enum pipe pipe, bool state)
b24e7179
JB
1109{
1110 int reg;
1111 u32 val;
1112 bool cur_state;
1113
1114 reg = DPLL(pipe);
1115 val = I915_READ(reg);
1116 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1117 I915_STATE_WARN(cur_state != state,
b24e7179
JB
1118 "PLL state assertion failure (expected %s, current %s)\n",
1119 state_string(state), state_string(cur_state));
1120}
b24e7179 1121
23538ef1
JN
1122/* XXX: the dsi pll is shared between MIPI DSI ports */
1123static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1124{
1125 u32 val;
1126 bool cur_state;
1127
a580516d 1128 mutex_lock(&dev_priv->sb_lock);
23538ef1 1129 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1130 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1131
1132 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1133 I915_STATE_WARN(cur_state != state,
23538ef1
JN
1134 "DSI PLL state assertion failure (expected %s, current %s)\n",
1135 state_string(state), state_string(cur_state));
1136}
1137#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1138#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1139
55607e8a 1140struct intel_shared_dpll *
e2b78267
DV
1141intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1142{
1143 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1144
6e3c9717 1145 if (crtc->config->shared_dpll < 0)
e2b78267
DV
1146 return NULL;
1147
6e3c9717 1148 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
e2b78267
DV
1149}
1150
040484af 1151/* For ILK+ */
55607e8a
DV
1152void assert_shared_dpll(struct drm_i915_private *dev_priv,
1153 struct intel_shared_dpll *pll,
1154 bool state)
040484af 1155{
040484af 1156 bool cur_state;
5358901f 1157 struct intel_dpll_hw_state hw_state;
040484af 1158
92b27b08 1159 if (WARN (!pll,
46edb027 1160 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1161 return;
ee7b9f93 1162
5358901f 1163 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
e2c719b7 1164 I915_STATE_WARN(cur_state != state,
5358901f
DV
1165 "%s assertion failure (expected %s, current %s)\n",
1166 pll->name, state_string(state), state_string(cur_state));
040484af 1167}
040484af
JB
1168
1169static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1170 enum pipe pipe, bool state)
1171{
1172 int reg;
1173 u32 val;
1174 bool cur_state;
ad80a810
PZ
1175 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1176 pipe);
040484af 1177
affa9354
PZ
1178 if (HAS_DDI(dev_priv->dev)) {
1179 /* DDI does not have a specific FDI_TX register */
ad80a810 1180 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1181 val = I915_READ(reg);
ad80a810 1182 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1183 } else {
1184 reg = FDI_TX_CTL(pipe);
1185 val = I915_READ(reg);
1186 cur_state = !!(val & FDI_TX_ENABLE);
1187 }
e2c719b7 1188 I915_STATE_WARN(cur_state != state,
040484af
JB
1189 "FDI TX state assertion failure (expected %s, current %s)\n",
1190 state_string(state), state_string(cur_state));
1191}
1192#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1193#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1194
1195static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1196 enum pipe pipe, bool state)
1197{
1198 int reg;
1199 u32 val;
1200 bool cur_state;
1201
d63fa0dc
PZ
1202 reg = FDI_RX_CTL(pipe);
1203 val = I915_READ(reg);
1204 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1205 I915_STATE_WARN(cur_state != state,
040484af
JB
1206 "FDI RX state assertion failure (expected %s, current %s)\n",
1207 state_string(state), state_string(cur_state));
1208}
1209#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1210#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1211
1212static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1213 enum pipe pipe)
1214{
1215 int reg;
1216 u32 val;
1217
1218 /* ILK FDI PLL is always enabled */
3d13ef2e 1219 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1220 return;
1221
bf507ef7 1222 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1223 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1224 return;
1225
040484af
JB
1226 reg = FDI_TX_CTL(pipe);
1227 val = I915_READ(reg);
e2c719b7 1228 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1229}
1230
55607e8a
DV
1231void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1232 enum pipe pipe, bool state)
040484af
JB
1233{
1234 int reg;
1235 u32 val;
55607e8a 1236 bool cur_state;
040484af
JB
1237
1238 reg = FDI_RX_CTL(pipe);
1239 val = I915_READ(reg);
55607e8a 1240 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1241 I915_STATE_WARN(cur_state != state,
55607e8a
DV
1242 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1243 state_string(state), state_string(cur_state));
040484af
JB
1244}
1245
b680c37a
DV
1246void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1247 enum pipe pipe)
ea0760cf 1248{
bedd4dba
JN
1249 struct drm_device *dev = dev_priv->dev;
1250 int pp_reg;
ea0760cf
JB
1251 u32 val;
1252 enum pipe panel_pipe = PIPE_A;
0de3b485 1253 bool locked = true;
ea0760cf 1254
bedd4dba
JN
1255 if (WARN_ON(HAS_DDI(dev)))
1256 return;
1257
1258 if (HAS_PCH_SPLIT(dev)) {
1259 u32 port_sel;
1260
ea0760cf 1261 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1262 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1263
1264 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1265 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1266 panel_pipe = PIPE_B;
1267 /* XXX: else fix for eDP */
1268 } else if (IS_VALLEYVIEW(dev)) {
1269 /* presumably write lock depends on pipe, not port select */
1270 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1271 panel_pipe = pipe;
ea0760cf
JB
1272 } else {
1273 pp_reg = PP_CONTROL;
bedd4dba
JN
1274 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1275 panel_pipe = PIPE_B;
ea0760cf
JB
1276 }
1277
1278 val = I915_READ(pp_reg);
1279 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1280 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1281 locked = false;
1282
e2c719b7 1283 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1284 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1285 pipe_name(pipe));
ea0760cf
JB
1286}
1287
93ce0ba6
JN
1288static void assert_cursor(struct drm_i915_private *dev_priv,
1289 enum pipe pipe, bool state)
1290{
1291 struct drm_device *dev = dev_priv->dev;
1292 bool cur_state;
1293
d9d82081 1294 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1295 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1296 else
5efb3e28 1297 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1298
e2c719b7 1299 I915_STATE_WARN(cur_state != state,
93ce0ba6
JN
1300 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1301 pipe_name(pipe), state_string(state), state_string(cur_state));
1302}
1303#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1304#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1305
b840d907
JB
1306void assert_pipe(struct drm_i915_private *dev_priv,
1307 enum pipe pipe, bool state)
b24e7179
JB
1308{
1309 int reg;
1310 u32 val;
63d7bbe9 1311 bool cur_state;
702e7a56
PZ
1312 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1313 pipe);
b24e7179 1314
b6b5d049
VS
1315 /* if we need the pipe quirk it must be always on */
1316 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1317 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1318 state = true;
1319
f458ebbc 1320 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1321 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1322 cur_state = false;
1323 } else {
1324 reg = PIPECONF(cpu_transcoder);
1325 val = I915_READ(reg);
1326 cur_state = !!(val & PIPECONF_ENABLE);
1327 }
1328
e2c719b7 1329 I915_STATE_WARN(cur_state != state,
63d7bbe9 1330 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1331 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1332}
1333
931872fc
CW
1334static void assert_plane(struct drm_i915_private *dev_priv,
1335 enum plane plane, bool state)
b24e7179
JB
1336{
1337 int reg;
1338 u32 val;
931872fc 1339 bool cur_state;
b24e7179
JB
1340
1341 reg = DSPCNTR(plane);
1342 val = I915_READ(reg);
931872fc 1343 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1344 I915_STATE_WARN(cur_state != state,
931872fc
CW
1345 "plane %c assertion failure (expected %s, current %s)\n",
1346 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1347}
1348
931872fc
CW
1349#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1350#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1351
b24e7179
JB
1352static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1353 enum pipe pipe)
1354{
653e1026 1355 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1356 int reg, i;
1357 u32 val;
1358 int cur_pipe;
1359
653e1026
VS
1360 /* Primary planes are fixed to pipes on gen4+ */
1361 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1362 reg = DSPCNTR(pipe);
1363 val = I915_READ(reg);
e2c719b7 1364 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1365 "plane %c assertion failure, should be disabled but not\n",
1366 plane_name(pipe));
19ec1358 1367 return;
28c05794 1368 }
19ec1358 1369
b24e7179 1370 /* Need to check both planes against the pipe */
055e393f 1371 for_each_pipe(dev_priv, i) {
b24e7179
JB
1372 reg = DSPCNTR(i);
1373 val = I915_READ(reg);
1374 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1375 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1376 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1377 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1378 plane_name(i), pipe_name(pipe));
b24e7179
JB
1379 }
1380}
1381
19332d7a
JB
1382static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1383 enum pipe pipe)
1384{
20674eef 1385 struct drm_device *dev = dev_priv->dev;
1fe47785 1386 int reg, sprite;
19332d7a
JB
1387 u32 val;
1388
7feb8b88 1389 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1390 for_each_sprite(dev_priv, pipe, sprite) {
7feb8b88 1391 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1392 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1393 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1394 sprite, pipe_name(pipe));
1395 }
1396 } else if (IS_VALLEYVIEW(dev)) {
3bdcfc0c 1397 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 1398 reg = SPCNTR(pipe, sprite);
20674eef 1399 val = I915_READ(reg);
e2c719b7 1400 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1401 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1402 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1403 }
1404 } else if (INTEL_INFO(dev)->gen >= 7) {
1405 reg = SPRCTL(pipe);
19332d7a 1406 val = I915_READ(reg);
e2c719b7 1407 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1408 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1409 plane_name(pipe), pipe_name(pipe));
1410 } else if (INTEL_INFO(dev)->gen >= 5) {
1411 reg = DVSCNTR(pipe);
19332d7a 1412 val = I915_READ(reg);
e2c719b7 1413 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1414 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1415 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1416 }
1417}
1418
08c71e5e
VS
1419static void assert_vblank_disabled(struct drm_crtc *crtc)
1420{
e2c719b7 1421 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1422 drm_crtc_vblank_put(crtc);
1423}
1424
89eff4be 1425static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1426{
1427 u32 val;
1428 bool enabled;
1429
e2c719b7 1430 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1431
92f2584a
JB
1432 val = I915_READ(PCH_DREF_CONTROL);
1433 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1434 DREF_SUPERSPREAD_SOURCE_MASK));
e2c719b7 1435 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
92f2584a
JB
1436}
1437
ab9412ba
DV
1438static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1439 enum pipe pipe)
92f2584a
JB
1440{
1441 int reg;
1442 u32 val;
1443 bool enabled;
1444
ab9412ba 1445 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1446 val = I915_READ(reg);
1447 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1448 I915_STATE_WARN(enabled,
9db4a9c7
JB
1449 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1450 pipe_name(pipe));
92f2584a
JB
1451}
1452
4e634389
KP
1453static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1454 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1455{
1456 if ((val & DP_PORT_EN) == 0)
1457 return false;
1458
1459 if (HAS_PCH_CPT(dev_priv->dev)) {
1460 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1461 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1462 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1463 return false;
44f37d1f
CML
1464 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1465 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1466 return false;
f0575e92
KP
1467 } else {
1468 if ((val & DP_PIPE_MASK) != (pipe << 30))
1469 return false;
1470 }
1471 return true;
1472}
1473
1519b995
KP
1474static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1475 enum pipe pipe, u32 val)
1476{
dc0fa718 1477 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1478 return false;
1479
1480 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1481 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1482 return false;
44f37d1f
CML
1483 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1484 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1485 return false;
1519b995 1486 } else {
dc0fa718 1487 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1488 return false;
1489 }
1490 return true;
1491}
1492
1493static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1494 enum pipe pipe, u32 val)
1495{
1496 if ((val & LVDS_PORT_EN) == 0)
1497 return false;
1498
1499 if (HAS_PCH_CPT(dev_priv->dev)) {
1500 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1501 return false;
1502 } else {
1503 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1504 return false;
1505 }
1506 return true;
1507}
1508
1509static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1510 enum pipe pipe, u32 val)
1511{
1512 if ((val & ADPA_DAC_ENABLE) == 0)
1513 return false;
1514 if (HAS_PCH_CPT(dev_priv->dev)) {
1515 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1516 return false;
1517 } else {
1518 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1519 return false;
1520 }
1521 return true;
1522}
1523
291906f1 1524static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1525 enum pipe pipe, int reg, u32 port_sel)
291906f1 1526{
47a05eca 1527 u32 val = I915_READ(reg);
e2c719b7 1528 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1529 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1530 reg, pipe_name(pipe));
de9a35ab 1531
e2c719b7 1532 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1533 && (val & DP_PIPEB_SELECT),
de9a35ab 1534 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1535}
1536
1537static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1538 enum pipe pipe, int reg)
1539{
47a05eca 1540 u32 val = I915_READ(reg);
e2c719b7 1541 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1542 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1543 reg, pipe_name(pipe));
de9a35ab 1544
e2c719b7 1545 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1546 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1547 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1548}
1549
1550static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1551 enum pipe pipe)
1552{
1553 int reg;
1554 u32 val;
291906f1 1555
f0575e92
KP
1556 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1557 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1558 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1559
1560 reg = PCH_ADPA;
1561 val = I915_READ(reg);
e2c719b7 1562 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1563 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1564 pipe_name(pipe));
291906f1
JB
1565
1566 reg = PCH_LVDS;
1567 val = I915_READ(reg);
e2c719b7 1568 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1569 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1570 pipe_name(pipe));
291906f1 1571
e2debe91
PZ
1572 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1573 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1574 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1575}
1576
40e9cf64
JB
1577static void intel_init_dpio(struct drm_device *dev)
1578{
1579 struct drm_i915_private *dev_priv = dev->dev_private;
1580
1581 if (!IS_VALLEYVIEW(dev))
1582 return;
1583
a09caddd
CML
1584 /*
1585 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1586 * CHV x1 PHY (DP/HDMI D)
1587 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1588 */
1589 if (IS_CHERRYVIEW(dev)) {
1590 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1591 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1592 } else {
1593 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1594 }
5382f5f3
JB
1595}
1596
d288f65f 1597static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1598 const struct intel_crtc_state *pipe_config)
87442f73 1599{
426115cf
DV
1600 struct drm_device *dev = crtc->base.dev;
1601 struct drm_i915_private *dev_priv = dev->dev_private;
1602 int reg = DPLL(crtc->pipe);
d288f65f 1603 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1604
426115cf 1605 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1606
1607 /* No really, not for ILK+ */
1608 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1609
1610 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1611 if (IS_MOBILE(dev_priv->dev))
426115cf 1612 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1613
426115cf
DV
1614 I915_WRITE(reg, dpll);
1615 POSTING_READ(reg);
1616 udelay(150);
1617
1618 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1619 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1620
d288f65f 1621 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1622 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1623
1624 /* We do this three times for luck */
426115cf 1625 I915_WRITE(reg, dpll);
87442f73
DV
1626 POSTING_READ(reg);
1627 udelay(150); /* wait for warmup */
426115cf 1628 I915_WRITE(reg, dpll);
87442f73
DV
1629 POSTING_READ(reg);
1630 udelay(150); /* wait for warmup */
426115cf 1631 I915_WRITE(reg, dpll);
87442f73
DV
1632 POSTING_READ(reg);
1633 udelay(150); /* wait for warmup */
1634}
1635
d288f65f 1636static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1637 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1638{
1639 struct drm_device *dev = crtc->base.dev;
1640 struct drm_i915_private *dev_priv = dev->dev_private;
1641 int pipe = crtc->pipe;
1642 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1643 u32 tmp;
1644
1645 assert_pipe_disabled(dev_priv, crtc->pipe);
1646
1647 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1648
a580516d 1649 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1650
1651 /* Enable back the 10bit clock to display controller */
1652 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1653 tmp |= DPIO_DCLKP_EN;
1654 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1655
54433e91
VS
1656 mutex_unlock(&dev_priv->sb_lock);
1657
9d556c99
CML
1658 /*
1659 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1660 */
1661 udelay(1);
1662
1663 /* Enable PLL */
d288f65f 1664 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1665
1666 /* Check PLL is locked */
a11b0703 1667 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1668 DRM_ERROR("PLL %d failed to lock\n", pipe);
1669
a11b0703 1670 /* not sure when this should be written */
d288f65f 1671 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703 1672 POSTING_READ(DPLL_MD(pipe));
9d556c99
CML
1673}
1674
1c4e0274
VS
1675static int intel_num_dvo_pipes(struct drm_device *dev)
1676{
1677 struct intel_crtc *crtc;
1678 int count = 0;
1679
1680 for_each_intel_crtc(dev, crtc)
3538b9df 1681 count += crtc->base.state->active &&
409ee761 1682 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1683
1684 return count;
1685}
1686
66e3d5c0 1687static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1688{
66e3d5c0
DV
1689 struct drm_device *dev = crtc->base.dev;
1690 struct drm_i915_private *dev_priv = dev->dev_private;
1691 int reg = DPLL(crtc->pipe);
6e3c9717 1692 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1693
66e3d5c0 1694 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1695
63d7bbe9 1696 /* No really, not for ILK+ */
3d13ef2e 1697 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1698
1699 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1700 if (IS_MOBILE(dev) && !IS_I830(dev))
1701 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1702
1c4e0274
VS
1703 /* Enable DVO 2x clock on both PLLs if necessary */
1704 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1705 /*
1706 * It appears to be important that we don't enable this
1707 * for the current pipe before otherwise configuring the
1708 * PLL. No idea how this should be handled if multiple
1709 * DVO outputs are enabled simultaneosly.
1710 */
1711 dpll |= DPLL_DVO_2X_MODE;
1712 I915_WRITE(DPLL(!crtc->pipe),
1713 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1714 }
66e3d5c0
DV
1715
1716 /* Wait for the clocks to stabilize. */
1717 POSTING_READ(reg);
1718 udelay(150);
1719
1720 if (INTEL_INFO(dev)->gen >= 4) {
1721 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1722 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1723 } else {
1724 /* The pixel multiplier can only be updated once the
1725 * DPLL is enabled and the clocks are stable.
1726 *
1727 * So write it again.
1728 */
1729 I915_WRITE(reg, dpll);
1730 }
63d7bbe9
JB
1731
1732 /* We do this three times for luck */
66e3d5c0 1733 I915_WRITE(reg, dpll);
63d7bbe9
JB
1734 POSTING_READ(reg);
1735 udelay(150); /* wait for warmup */
66e3d5c0 1736 I915_WRITE(reg, dpll);
63d7bbe9
JB
1737 POSTING_READ(reg);
1738 udelay(150); /* wait for warmup */
66e3d5c0 1739 I915_WRITE(reg, dpll);
63d7bbe9
JB
1740 POSTING_READ(reg);
1741 udelay(150); /* wait for warmup */
1742}
1743
1744/**
50b44a44 1745 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1746 * @dev_priv: i915 private structure
1747 * @pipe: pipe PLL to disable
1748 *
1749 * Disable the PLL for @pipe, making sure the pipe is off first.
1750 *
1751 * Note! This is for pre-ILK only.
1752 */
1c4e0274 1753static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1754{
1c4e0274
VS
1755 struct drm_device *dev = crtc->base.dev;
1756 struct drm_i915_private *dev_priv = dev->dev_private;
1757 enum pipe pipe = crtc->pipe;
1758
1759 /* Disable DVO 2x clock on both PLLs if necessary */
1760 if (IS_I830(dev) &&
409ee761 1761 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1762 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1763 I915_WRITE(DPLL(PIPE_B),
1764 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1765 I915_WRITE(DPLL(PIPE_A),
1766 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1767 }
1768
b6b5d049
VS
1769 /* Don't disable pipe or pipe PLLs if needed */
1770 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1771 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1772 return;
1773
1774 /* Make sure the pipe isn't still relying on us */
1775 assert_pipe_disabled(dev_priv, pipe);
1776
50b44a44
DV
1777 I915_WRITE(DPLL(pipe), 0);
1778 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1779}
1780
f6071166
JB
1781static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1782{
1783 u32 val = 0;
1784
1785 /* Make sure the pipe isn't still relying on us */
1786 assert_pipe_disabled(dev_priv, pipe);
1787
e5cbfbfb
ID
1788 /*
1789 * Leave integrated clock source and reference clock enabled for pipe B.
1790 * The latter is needed for VGA hotplug / manual detection.
1791 */
f6071166 1792 if (pipe == PIPE_B)
e5cbfbfb 1793 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1794 I915_WRITE(DPLL(pipe), val);
1795 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1796
1797}
1798
1799static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1800{
d752048d 1801 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1802 u32 val;
1803
a11b0703
VS
1804 /* Make sure the pipe isn't still relying on us */
1805 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1806
a11b0703 1807 /* Set PLL en = 0 */
d17ec4ce 1808 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1809 if (pipe != PIPE_A)
1810 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1811 I915_WRITE(DPLL(pipe), val);
1812 POSTING_READ(DPLL(pipe));
d752048d 1813
a580516d 1814 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1815
1816 /* Disable 10bit clock to display controller */
1817 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1818 val &= ~DPIO_DCLKP_EN;
1819 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1820
61407f6d
VS
1821 /* disable left/right clock distribution */
1822 if (pipe != PIPE_B) {
1823 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1824 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1825 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1826 } else {
1827 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1828 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1829 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1830 }
1831
a580516d 1832 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1833}
1834
e4607fcf 1835void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1836 struct intel_digital_port *dport,
1837 unsigned int expected_mask)
89b667f8
JB
1838{
1839 u32 port_mask;
00fc31b7 1840 int dpll_reg;
89b667f8 1841
e4607fcf
CML
1842 switch (dport->port) {
1843 case PORT_B:
89b667f8 1844 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1845 dpll_reg = DPLL(0);
e4607fcf
CML
1846 break;
1847 case PORT_C:
89b667f8 1848 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1849 dpll_reg = DPLL(0);
9b6de0a1 1850 expected_mask <<= 4;
00fc31b7
CML
1851 break;
1852 case PORT_D:
1853 port_mask = DPLL_PORTD_READY_MASK;
1854 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1855 break;
1856 default:
1857 BUG();
1858 }
89b667f8 1859
9b6de0a1
VS
1860 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1861 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1862 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1863}
1864
b14b1055
DV
1865static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1866{
1867 struct drm_device *dev = crtc->base.dev;
1868 struct drm_i915_private *dev_priv = dev->dev_private;
1869 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1870
be19f0ff
CW
1871 if (WARN_ON(pll == NULL))
1872 return;
1873
3e369b76 1874 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1875 if (pll->active == 0) {
1876 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1877 WARN_ON(pll->on);
1878 assert_shared_dpll_disabled(dev_priv, pll);
1879
1880 pll->mode_set(dev_priv, pll);
1881 }
1882}
1883
92f2584a 1884/**
85b3894f 1885 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1886 * @dev_priv: i915 private structure
1887 * @pipe: pipe PLL to enable
1888 *
1889 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1890 * drives the transcoder clock.
1891 */
85b3894f 1892static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1893{
3d13ef2e
DL
1894 struct drm_device *dev = crtc->base.dev;
1895 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1896 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1897
87a875bb 1898 if (WARN_ON(pll == NULL))
48da64a8
CW
1899 return;
1900
3e369b76 1901 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1902 return;
ee7b9f93 1903
74dd6928 1904 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1905 pll->name, pll->active, pll->on,
e2b78267 1906 crtc->base.base.id);
92f2584a 1907
cdbd2316
DV
1908 if (pll->active++) {
1909 WARN_ON(!pll->on);
e9d6944e 1910 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1911 return;
1912 }
f4a091c7 1913 WARN_ON(pll->on);
ee7b9f93 1914
bd2bb1b9
PZ
1915 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1916
46edb027 1917 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1918 pll->enable(dev_priv, pll);
ee7b9f93 1919 pll->on = true;
92f2584a
JB
1920}
1921
f6daaec2 1922static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1923{
3d13ef2e
DL
1924 struct drm_device *dev = crtc->base.dev;
1925 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1926 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1927
92f2584a 1928 /* PCH only available on ILK+ */
3d13ef2e 1929 BUG_ON(INTEL_INFO(dev)->gen < 5);
eddfcbcd
ML
1930 if (pll == NULL)
1931 return;
92f2584a 1932
eddfcbcd 1933 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
48da64a8 1934 return;
7a419866 1935
46edb027
DV
1936 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1937 pll->name, pll->active, pll->on,
e2b78267 1938 crtc->base.base.id);
7a419866 1939
48da64a8 1940 if (WARN_ON(pll->active == 0)) {
e9d6944e 1941 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1942 return;
1943 }
1944
e9d6944e 1945 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1946 WARN_ON(!pll->on);
cdbd2316 1947 if (--pll->active)
7a419866 1948 return;
ee7b9f93 1949
46edb027 1950 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1951 pll->disable(dev_priv, pll);
ee7b9f93 1952 pll->on = false;
bd2bb1b9
PZ
1953
1954 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1955}
1956
b8a4f404
PZ
1957static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1958 enum pipe pipe)
040484af 1959{
23670b32 1960 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1961 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1963 uint32_t reg, val, pipeconf_val;
040484af
JB
1964
1965 /* PCH only available on ILK+ */
55522f37 1966 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1967
1968 /* Make sure PCH DPLL is enabled */
e72f9fbf 1969 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1970 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1971
1972 /* FDI must be feeding us bits for PCH ports */
1973 assert_fdi_tx_enabled(dev_priv, pipe);
1974 assert_fdi_rx_enabled(dev_priv, pipe);
1975
23670b32
DV
1976 if (HAS_PCH_CPT(dev)) {
1977 /* Workaround: Set the timing override bit before enabling the
1978 * pch transcoder. */
1979 reg = TRANS_CHICKEN2(pipe);
1980 val = I915_READ(reg);
1981 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1982 I915_WRITE(reg, val);
59c859d6 1983 }
23670b32 1984
ab9412ba 1985 reg = PCH_TRANSCONF(pipe);
040484af 1986 val = I915_READ(reg);
5f7f726d 1987 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1988
1989 if (HAS_PCH_IBX(dev_priv->dev)) {
1990 /*
c5de7c6f
VS
1991 * Make the BPC in transcoder be consistent with
1992 * that in pipeconf reg. For HDMI we must use 8bpc
1993 * here for both 8bpc and 12bpc.
e9bcff5c 1994 */
dfd07d72 1995 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1996 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1997 val |= PIPECONF_8BPC;
1998 else
1999 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 2000 }
5f7f726d
PZ
2001
2002 val &= ~TRANS_INTERLACE_MASK;
2003 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 2004 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 2005 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
2006 val |= TRANS_LEGACY_INTERLACED_ILK;
2007 else
2008 val |= TRANS_INTERLACED;
5f7f726d
PZ
2009 else
2010 val |= TRANS_PROGRESSIVE;
2011
040484af
JB
2012 I915_WRITE(reg, val | TRANS_ENABLE);
2013 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 2014 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
2015}
2016
8fb033d7 2017static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 2018 enum transcoder cpu_transcoder)
040484af 2019{
8fb033d7 2020 u32 val, pipeconf_val;
8fb033d7
PZ
2021
2022 /* PCH only available on ILK+ */
55522f37 2023 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 2024
8fb033d7 2025 /* FDI must be feeding us bits for PCH ports */
1a240d4d 2026 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 2027 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 2028
223a6fdf
PZ
2029 /* Workaround: set timing override bit. */
2030 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2031 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
2032 I915_WRITE(_TRANSA_CHICKEN2, val);
2033
25f3ef11 2034 val = TRANS_ENABLE;
937bb610 2035 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 2036
9a76b1c6
PZ
2037 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2038 PIPECONF_INTERLACED_ILK)
a35f2679 2039 val |= TRANS_INTERLACED;
8fb033d7
PZ
2040 else
2041 val |= TRANS_PROGRESSIVE;
2042
ab9412ba
DV
2043 I915_WRITE(LPT_TRANSCONF, val);
2044 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 2045 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
2046}
2047
b8a4f404
PZ
2048static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2049 enum pipe pipe)
040484af 2050{
23670b32
DV
2051 struct drm_device *dev = dev_priv->dev;
2052 uint32_t reg, val;
040484af
JB
2053
2054 /* FDI relies on the transcoder */
2055 assert_fdi_tx_disabled(dev_priv, pipe);
2056 assert_fdi_rx_disabled(dev_priv, pipe);
2057
291906f1
JB
2058 /* Ports must be off as well */
2059 assert_pch_ports_disabled(dev_priv, pipe);
2060
ab9412ba 2061 reg = PCH_TRANSCONF(pipe);
040484af
JB
2062 val = I915_READ(reg);
2063 val &= ~TRANS_ENABLE;
2064 I915_WRITE(reg, val);
2065 /* wait for PCH transcoder off, transcoder state */
2066 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 2067 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
2068
2069 if (!HAS_PCH_IBX(dev)) {
2070 /* Workaround: Clear the timing override chicken bit again. */
2071 reg = TRANS_CHICKEN2(pipe);
2072 val = I915_READ(reg);
2073 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2074 I915_WRITE(reg, val);
2075 }
040484af
JB
2076}
2077
ab4d966c 2078static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 2079{
8fb033d7
PZ
2080 u32 val;
2081
ab9412ba 2082 val = I915_READ(LPT_TRANSCONF);
8fb033d7 2083 val &= ~TRANS_ENABLE;
ab9412ba 2084 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2085 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2086 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2087 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2088
2089 /* Workaround: clear timing override bit. */
2090 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2091 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2092 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2093}
2094
b24e7179 2095/**
309cfea8 2096 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2097 * @crtc: crtc responsible for the pipe
b24e7179 2098 *
0372264a 2099 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2100 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2101 */
e1fdc473 2102static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2103{
0372264a
PZ
2104 struct drm_device *dev = crtc->base.dev;
2105 struct drm_i915_private *dev_priv = dev->dev_private;
2106 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2107 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2108 pipe);
1a240d4d 2109 enum pipe pch_transcoder;
b24e7179
JB
2110 int reg;
2111 u32 val;
2112
9e2ee2dd
VS
2113 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2114
58c6eaa2 2115 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2116 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2117 assert_sprites_disabled(dev_priv, pipe);
2118
681e5811 2119 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2120 pch_transcoder = TRANSCODER_A;
2121 else
2122 pch_transcoder = pipe;
2123
b24e7179
JB
2124 /*
2125 * A pipe without a PLL won't actually be able to drive bits from
2126 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2127 * need the check.
2128 */
50360403 2129 if (HAS_GMCH_DISPLAY(dev_priv->dev))
409ee761 2130 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2131 assert_dsi_pll_enabled(dev_priv);
2132 else
2133 assert_pll_enabled(dev_priv, pipe);
040484af 2134 else {
6e3c9717 2135 if (crtc->config->has_pch_encoder) {
040484af 2136 /* if driving the PCH, we need FDI enabled */
cc391bbb 2137 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2138 assert_fdi_tx_pll_enabled(dev_priv,
2139 (enum pipe) cpu_transcoder);
040484af
JB
2140 }
2141 /* FIXME: assert CPU port conditions for SNB+ */
2142 }
b24e7179 2143
702e7a56 2144 reg = PIPECONF(cpu_transcoder);
b24e7179 2145 val = I915_READ(reg);
7ad25d48 2146 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2147 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2148 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2149 return;
7ad25d48 2150 }
00d70b15
CW
2151
2152 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2153 POSTING_READ(reg);
b24e7179
JB
2154}
2155
2156/**
309cfea8 2157 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2158 * @crtc: crtc whose pipes is to be disabled
b24e7179 2159 *
575f7ab7
VS
2160 * Disable the pipe of @crtc, making sure that various hardware
2161 * specific requirements are met, if applicable, e.g. plane
2162 * disabled, panel fitter off, etc.
b24e7179
JB
2163 *
2164 * Will wait until the pipe has shut down before returning.
2165 */
575f7ab7 2166static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2167{
575f7ab7 2168 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2169 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2170 enum pipe pipe = crtc->pipe;
b24e7179
JB
2171 int reg;
2172 u32 val;
2173
9e2ee2dd
VS
2174 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2175
b24e7179
JB
2176 /*
2177 * Make sure planes won't keep trying to pump pixels to us,
2178 * or we might hang the display.
2179 */
2180 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2181 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2182 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2183
702e7a56 2184 reg = PIPECONF(cpu_transcoder);
b24e7179 2185 val = I915_READ(reg);
00d70b15
CW
2186 if ((val & PIPECONF_ENABLE) == 0)
2187 return;
2188
67adc644
VS
2189 /*
2190 * Double wide has implications for planes
2191 * so best keep it disabled when not needed.
2192 */
6e3c9717 2193 if (crtc->config->double_wide)
67adc644
VS
2194 val &= ~PIPECONF_DOUBLE_WIDE;
2195
2196 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2197 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2198 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2199 val &= ~PIPECONF_ENABLE;
2200
2201 I915_WRITE(reg, val);
2202 if ((val & PIPECONF_ENABLE) == 0)
2203 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2204}
2205
693db184
CW
2206static bool need_vtd_wa(struct drm_device *dev)
2207{
2208#ifdef CONFIG_INTEL_IOMMU
2209 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2210 return true;
2211#endif
2212 return false;
2213}
2214
50470bb0 2215unsigned int
6761dd31
TU
2216intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2217 uint64_t fb_format_modifier)
a57ce0b2 2218{
6761dd31
TU
2219 unsigned int tile_height;
2220 uint32_t pixel_bytes;
a57ce0b2 2221
b5d0e9bf
DL
2222 switch (fb_format_modifier) {
2223 case DRM_FORMAT_MOD_NONE:
2224 tile_height = 1;
2225 break;
2226 case I915_FORMAT_MOD_X_TILED:
2227 tile_height = IS_GEN2(dev) ? 16 : 8;
2228 break;
2229 case I915_FORMAT_MOD_Y_TILED:
2230 tile_height = 32;
2231 break;
2232 case I915_FORMAT_MOD_Yf_TILED:
6761dd31
TU
2233 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2234 switch (pixel_bytes) {
b5d0e9bf 2235 default:
6761dd31 2236 case 1:
b5d0e9bf
DL
2237 tile_height = 64;
2238 break;
6761dd31
TU
2239 case 2:
2240 case 4:
b5d0e9bf
DL
2241 tile_height = 32;
2242 break;
6761dd31 2243 case 8:
b5d0e9bf
DL
2244 tile_height = 16;
2245 break;
6761dd31 2246 case 16:
b5d0e9bf
DL
2247 WARN_ONCE(1,
2248 "128-bit pixels are not supported for display!");
2249 tile_height = 16;
2250 break;
2251 }
2252 break;
2253 default:
2254 MISSING_CASE(fb_format_modifier);
2255 tile_height = 1;
2256 break;
2257 }
091df6cb 2258
6761dd31
TU
2259 return tile_height;
2260}
2261
2262unsigned int
2263intel_fb_align_height(struct drm_device *dev, unsigned int height,
2264 uint32_t pixel_format, uint64_t fb_format_modifier)
2265{
2266 return ALIGN(height, intel_tile_height(dev, pixel_format,
2267 fb_format_modifier));
a57ce0b2
JB
2268}
2269
f64b98cd
TU
2270static int
2271intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2272 const struct drm_plane_state *plane_state)
2273{
50470bb0 2274 struct intel_rotation_info *info = &view->rotation_info;
84fe03f7 2275 unsigned int tile_height, tile_pitch;
50470bb0 2276
f64b98cd
TU
2277 *view = i915_ggtt_view_normal;
2278
50470bb0
TU
2279 if (!plane_state)
2280 return 0;
2281
121920fa 2282 if (!intel_rotation_90_or_270(plane_state->rotation))
50470bb0
TU
2283 return 0;
2284
9abc4648 2285 *view = i915_ggtt_view_rotated;
50470bb0
TU
2286
2287 info->height = fb->height;
2288 info->pixel_format = fb->pixel_format;
2289 info->pitch = fb->pitches[0];
2290 info->fb_modifier = fb->modifier[0];
2291
84fe03f7
TU
2292 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
2293 fb->modifier[0]);
2294 tile_pitch = PAGE_SIZE / tile_height;
2295 info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2296 info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
2297 info->size = info->width_pages * info->height_pages * PAGE_SIZE;
2298
f64b98cd
TU
2299 return 0;
2300}
2301
4e9a86b6
VS
2302static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2303{
2304 if (INTEL_INFO(dev_priv)->gen >= 9)
2305 return 256 * 1024;
985b8bb4
VS
2306 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2307 IS_VALLEYVIEW(dev_priv))
4e9a86b6
VS
2308 return 128 * 1024;
2309 else if (INTEL_INFO(dev_priv)->gen >= 4)
2310 return 4 * 1024;
2311 else
44c5905e 2312 return 0;
4e9a86b6
VS
2313}
2314
127bd2ac 2315int
850c4cdc
TU
2316intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2317 struct drm_framebuffer *fb,
82bc3b2d 2318 const struct drm_plane_state *plane_state,
91af127f
JH
2319 struct intel_engine_cs *pipelined,
2320 struct drm_i915_gem_request **pipelined_request)
6b95a207 2321{
850c4cdc 2322 struct drm_device *dev = fb->dev;
ce453d81 2323 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2324 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2325 struct i915_ggtt_view view;
6b95a207
KH
2326 u32 alignment;
2327 int ret;
2328
ebcdd39e
MR
2329 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2330
7b911adc
TU
2331 switch (fb->modifier[0]) {
2332 case DRM_FORMAT_MOD_NONE:
4e9a86b6 2333 alignment = intel_linear_alignment(dev_priv);
6b95a207 2334 break;
7b911adc 2335 case I915_FORMAT_MOD_X_TILED:
1fada4cc
DL
2336 if (INTEL_INFO(dev)->gen >= 9)
2337 alignment = 256 * 1024;
2338 else {
2339 /* pin() will align the object as required by fence */
2340 alignment = 0;
2341 }
6b95a207 2342 break;
7b911adc 2343 case I915_FORMAT_MOD_Y_TILED:
1327b9a1
DL
2344 case I915_FORMAT_MOD_Yf_TILED:
2345 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2346 "Y tiling bo slipped through, driver bug!\n"))
2347 return -EINVAL;
2348 alignment = 1 * 1024 * 1024;
2349 break;
6b95a207 2350 default:
7b911adc
TU
2351 MISSING_CASE(fb->modifier[0]);
2352 return -EINVAL;
6b95a207
KH
2353 }
2354
f64b98cd
TU
2355 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2356 if (ret)
2357 return ret;
2358
693db184
CW
2359 /* Note that the w/a also requires 64 PTE of padding following the
2360 * bo. We currently fill all unused PTE with the shadow page and so
2361 * we should always have valid PTE following the scanout preventing
2362 * the VT-d warning.
2363 */
2364 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2365 alignment = 256 * 1024;
2366
d6dd6843
PZ
2367 /*
2368 * Global gtt pte registers are special registers which actually forward
2369 * writes to a chunk of system memory. Which means that there is no risk
2370 * that the register values disappear as soon as we call
2371 * intel_runtime_pm_put(), so it is correct to wrap only the
2372 * pin/unpin/fence and not more.
2373 */
2374 intel_runtime_pm_get(dev_priv);
2375
ce453d81 2376 dev_priv->mm.interruptible = false;
e6617330 2377 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
91af127f 2378 pipelined_request, &view);
48b956c5 2379 if (ret)
ce453d81 2380 goto err_interruptible;
6b95a207
KH
2381
2382 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2383 * fence, whereas 965+ only requires a fence if using
2384 * framebuffer compression. For simplicity, we always install
2385 * a fence as the cost is not that onerous.
2386 */
06d98131 2387 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2388 if (ret)
2389 goto err_unpin;
1690e1eb 2390
9a5a53b3 2391 i915_gem_object_pin_fence(obj);
6b95a207 2392
ce453d81 2393 dev_priv->mm.interruptible = true;
d6dd6843 2394 intel_runtime_pm_put(dev_priv);
6b95a207 2395 return 0;
48b956c5
CW
2396
2397err_unpin:
f64b98cd 2398 i915_gem_object_unpin_from_display_plane(obj, &view);
ce453d81
CW
2399err_interruptible:
2400 dev_priv->mm.interruptible = true;
d6dd6843 2401 intel_runtime_pm_put(dev_priv);
48b956c5 2402 return ret;
6b95a207
KH
2403}
2404
82bc3b2d
TU
2405static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2406 const struct drm_plane_state *plane_state)
1690e1eb 2407{
82bc3b2d 2408 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd
TU
2409 struct i915_ggtt_view view;
2410 int ret;
82bc3b2d 2411
ebcdd39e
MR
2412 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2413
f64b98cd
TU
2414 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2415 WARN_ONCE(ret, "Couldn't get view from plane state!");
2416
1690e1eb 2417 i915_gem_object_unpin_fence(obj);
f64b98cd 2418 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2419}
2420
c2c75131
DV
2421/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2422 * is assumed to be a power-of-two. */
4e9a86b6
VS
2423unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2424 int *x, int *y,
bc752862
CW
2425 unsigned int tiling_mode,
2426 unsigned int cpp,
2427 unsigned int pitch)
c2c75131 2428{
bc752862
CW
2429 if (tiling_mode != I915_TILING_NONE) {
2430 unsigned int tile_rows, tiles;
c2c75131 2431
bc752862
CW
2432 tile_rows = *y / 8;
2433 *y %= 8;
c2c75131 2434
bc752862
CW
2435 tiles = *x / (512/cpp);
2436 *x %= 512/cpp;
2437
2438 return tile_rows * pitch * 8 + tiles * 4096;
2439 } else {
4e9a86b6 2440 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
bc752862
CW
2441 unsigned int offset;
2442
2443 offset = *y * pitch + *x * cpp;
4e9a86b6
VS
2444 *y = (offset & alignment) / pitch;
2445 *x = ((offset & alignment) - *y * pitch) / cpp;
2446 return offset & ~alignment;
bc752862 2447 }
c2c75131
DV
2448}
2449
b35d63fa 2450static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2451{
2452 switch (format) {
2453 case DISPPLANE_8BPP:
2454 return DRM_FORMAT_C8;
2455 case DISPPLANE_BGRX555:
2456 return DRM_FORMAT_XRGB1555;
2457 case DISPPLANE_BGRX565:
2458 return DRM_FORMAT_RGB565;
2459 default:
2460 case DISPPLANE_BGRX888:
2461 return DRM_FORMAT_XRGB8888;
2462 case DISPPLANE_RGBX888:
2463 return DRM_FORMAT_XBGR8888;
2464 case DISPPLANE_BGRX101010:
2465 return DRM_FORMAT_XRGB2101010;
2466 case DISPPLANE_RGBX101010:
2467 return DRM_FORMAT_XBGR2101010;
2468 }
2469}
2470
bc8d7dff
DL
2471static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2472{
2473 switch (format) {
2474 case PLANE_CTL_FORMAT_RGB_565:
2475 return DRM_FORMAT_RGB565;
2476 default:
2477 case PLANE_CTL_FORMAT_XRGB_8888:
2478 if (rgb_order) {
2479 if (alpha)
2480 return DRM_FORMAT_ABGR8888;
2481 else
2482 return DRM_FORMAT_XBGR8888;
2483 } else {
2484 if (alpha)
2485 return DRM_FORMAT_ARGB8888;
2486 else
2487 return DRM_FORMAT_XRGB8888;
2488 }
2489 case PLANE_CTL_FORMAT_XRGB_2101010:
2490 if (rgb_order)
2491 return DRM_FORMAT_XBGR2101010;
2492 else
2493 return DRM_FORMAT_XRGB2101010;
2494 }
2495}
2496
5724dbd1 2497static bool
f6936e29
DV
2498intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2499 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2500{
2501 struct drm_device *dev = crtc->base.dev;
2502 struct drm_i915_gem_object *obj = NULL;
2503 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2504 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2505 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2506 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2507 PAGE_SIZE);
2508
2509 size_aligned -= base_aligned;
46f297fb 2510
ff2652ea
CW
2511 if (plane_config->size == 0)
2512 return false;
2513
f37b5c2b
DV
2514 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2515 base_aligned,
2516 base_aligned,
2517 size_aligned);
46f297fb 2518 if (!obj)
484b41dd 2519 return false;
46f297fb 2520
49af449b
DL
2521 obj->tiling_mode = plane_config->tiling;
2522 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2523 obj->stride = fb->pitches[0];
46f297fb 2524
6bf129df
DL
2525 mode_cmd.pixel_format = fb->pixel_format;
2526 mode_cmd.width = fb->width;
2527 mode_cmd.height = fb->height;
2528 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2529 mode_cmd.modifier[0] = fb->modifier[0];
2530 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb
JB
2531
2532 mutex_lock(&dev->struct_mutex);
6bf129df 2533 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2534 &mode_cmd, obj)) {
46f297fb
JB
2535 DRM_DEBUG_KMS("intel fb init failed\n");
2536 goto out_unref_obj;
2537 }
46f297fb 2538 mutex_unlock(&dev->struct_mutex);
484b41dd 2539
f6936e29 2540 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2541 return true;
46f297fb
JB
2542
2543out_unref_obj:
2544 drm_gem_object_unreference(&obj->base);
2545 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2546 return false;
2547}
2548
afd65eb4
MR
2549/* Update plane->state->fb to match plane->fb after driver-internal updates */
2550static void
2551update_state_fb(struct drm_plane *plane)
2552{
2553 if (plane->fb == plane->state->fb)
2554 return;
2555
2556 if (plane->state->fb)
2557 drm_framebuffer_unreference(plane->state->fb);
2558 plane->state->fb = plane->fb;
2559 if (plane->state->fb)
2560 drm_framebuffer_reference(plane->state->fb);
2561}
2562
5724dbd1 2563static void
f6936e29
DV
2564intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2565 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2566{
2567 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2568 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2569 struct drm_crtc *c;
2570 struct intel_crtc *i;
2ff8fde1 2571 struct drm_i915_gem_object *obj;
88595ac9
DV
2572 struct drm_plane *primary = intel_crtc->base.primary;
2573 struct drm_framebuffer *fb;
484b41dd 2574
2d14030b 2575 if (!plane_config->fb)
484b41dd
JB
2576 return;
2577
f6936e29 2578 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2579 fb = &plane_config->fb->base;
2580 goto valid_fb;
f55548b5 2581 }
484b41dd 2582
2d14030b 2583 kfree(plane_config->fb);
484b41dd
JB
2584
2585 /*
2586 * Failed to alloc the obj, check to see if we should share
2587 * an fb with another CRTC instead
2588 */
70e1e0ec 2589 for_each_crtc(dev, c) {
484b41dd
JB
2590 i = to_intel_crtc(c);
2591
2592 if (c == &intel_crtc->base)
2593 continue;
2594
2ff8fde1
MR
2595 if (!i->active)
2596 continue;
2597
88595ac9
DV
2598 fb = c->primary->fb;
2599 if (!fb)
484b41dd
JB
2600 continue;
2601
88595ac9 2602 obj = intel_fb_obj(fb);
2ff8fde1 2603 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2604 drm_framebuffer_reference(fb);
2605 goto valid_fb;
484b41dd
JB
2606 }
2607 }
88595ac9
DV
2608
2609 return;
2610
2611valid_fb:
2612 obj = intel_fb_obj(fb);
2613 if (obj->tiling_mode != I915_TILING_NONE)
2614 dev_priv->preserve_bios_swizzle = true;
2615
2616 primary->fb = fb;
36750f28 2617 primary->crtc = primary->state->crtc = &intel_crtc->base;
88595ac9 2618 update_state_fb(primary);
36750f28 2619 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
a9ff8714 2620 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
46f297fb
JB
2621}
2622
29b9bde6
DV
2623static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2624 struct drm_framebuffer *fb,
2625 int x, int y)
81255565
JB
2626{
2627 struct drm_device *dev = crtc->dev;
2628 struct drm_i915_private *dev_priv = dev->dev_private;
2629 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2630 struct drm_plane *primary = crtc->primary;
2631 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2632 struct drm_i915_gem_object *obj;
81255565 2633 int plane = intel_crtc->plane;
e506a0c6 2634 unsigned long linear_offset;
81255565 2635 u32 dspcntr;
f45651ba 2636 u32 reg = DSPCNTR(plane);
48404c1e 2637 int pixel_size;
f45651ba 2638
b70709a6 2639 if (!visible || !fb) {
fdd508a6
VS
2640 I915_WRITE(reg, 0);
2641 if (INTEL_INFO(dev)->gen >= 4)
2642 I915_WRITE(DSPSURF(plane), 0);
2643 else
2644 I915_WRITE(DSPADDR(plane), 0);
2645 POSTING_READ(reg);
2646 return;
2647 }
2648
c9ba6fad
VS
2649 obj = intel_fb_obj(fb);
2650 if (WARN_ON(obj == NULL))
2651 return;
2652
2653 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2654
f45651ba
VS
2655 dspcntr = DISPPLANE_GAMMA_ENABLE;
2656
fdd508a6 2657 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2658
2659 if (INTEL_INFO(dev)->gen < 4) {
2660 if (intel_crtc->pipe == PIPE_B)
2661 dspcntr |= DISPPLANE_SEL_PIPE_B;
2662
2663 /* pipesrc and dspsize control the size that is scaled from,
2664 * which should always be the user's requested size.
2665 */
2666 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2667 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2668 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2669 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2670 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2671 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2672 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2673 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2674 I915_WRITE(PRIMPOS(plane), 0);
2675 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2676 }
81255565 2677
57779d06
VS
2678 switch (fb->pixel_format) {
2679 case DRM_FORMAT_C8:
81255565
JB
2680 dspcntr |= DISPPLANE_8BPP;
2681 break;
57779d06 2682 case DRM_FORMAT_XRGB1555:
57779d06 2683 dspcntr |= DISPPLANE_BGRX555;
81255565 2684 break;
57779d06
VS
2685 case DRM_FORMAT_RGB565:
2686 dspcntr |= DISPPLANE_BGRX565;
2687 break;
2688 case DRM_FORMAT_XRGB8888:
57779d06
VS
2689 dspcntr |= DISPPLANE_BGRX888;
2690 break;
2691 case DRM_FORMAT_XBGR8888:
57779d06
VS
2692 dspcntr |= DISPPLANE_RGBX888;
2693 break;
2694 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2695 dspcntr |= DISPPLANE_BGRX101010;
2696 break;
2697 case DRM_FORMAT_XBGR2101010:
57779d06 2698 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2699 break;
2700 default:
baba133a 2701 BUG();
81255565 2702 }
57779d06 2703
f45651ba
VS
2704 if (INTEL_INFO(dev)->gen >= 4 &&
2705 obj->tiling_mode != I915_TILING_NONE)
2706 dspcntr |= DISPPLANE_TILED;
81255565 2707
de1aa629
VS
2708 if (IS_G4X(dev))
2709 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2710
b9897127 2711 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2712
c2c75131
DV
2713 if (INTEL_INFO(dev)->gen >= 4) {
2714 intel_crtc->dspaddr_offset =
4e9a86b6
VS
2715 intel_gen4_compute_page_offset(dev_priv,
2716 &x, &y, obj->tiling_mode,
b9897127 2717 pixel_size,
bc752862 2718 fb->pitches[0]);
c2c75131
DV
2719 linear_offset -= intel_crtc->dspaddr_offset;
2720 } else {
e506a0c6 2721 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2722 }
e506a0c6 2723
8e7d688b 2724 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2725 dspcntr |= DISPPLANE_ROTATE_180;
2726
6e3c9717
ACO
2727 x += (intel_crtc->config->pipe_src_w - 1);
2728 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2729
2730 /* Finding the last pixel of the last line of the display
2731 data and adding to linear_offset*/
2732 linear_offset +=
6e3c9717
ACO
2733 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2734 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2735 }
2736
2737 I915_WRITE(reg, dspcntr);
2738
01f2c773 2739 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2740 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2741 I915_WRITE(DSPSURF(plane),
2742 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2743 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2744 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2745 } else
f343c5f6 2746 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2747 POSTING_READ(reg);
17638cd6
JB
2748}
2749
29b9bde6
DV
2750static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2751 struct drm_framebuffer *fb,
2752 int x, int y)
17638cd6
JB
2753{
2754 struct drm_device *dev = crtc->dev;
2755 struct drm_i915_private *dev_priv = dev->dev_private;
2756 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2757 struct drm_plane *primary = crtc->primary;
2758 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2759 struct drm_i915_gem_object *obj;
17638cd6 2760 int plane = intel_crtc->plane;
e506a0c6 2761 unsigned long linear_offset;
17638cd6 2762 u32 dspcntr;
f45651ba 2763 u32 reg = DSPCNTR(plane);
48404c1e 2764 int pixel_size;
f45651ba 2765
b70709a6 2766 if (!visible || !fb) {
fdd508a6
VS
2767 I915_WRITE(reg, 0);
2768 I915_WRITE(DSPSURF(plane), 0);
2769 POSTING_READ(reg);
2770 return;
2771 }
2772
c9ba6fad
VS
2773 obj = intel_fb_obj(fb);
2774 if (WARN_ON(obj == NULL))
2775 return;
2776
2777 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2778
f45651ba
VS
2779 dspcntr = DISPPLANE_GAMMA_ENABLE;
2780
fdd508a6 2781 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2782
2783 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2784 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2785
57779d06
VS
2786 switch (fb->pixel_format) {
2787 case DRM_FORMAT_C8:
17638cd6
JB
2788 dspcntr |= DISPPLANE_8BPP;
2789 break;
57779d06
VS
2790 case DRM_FORMAT_RGB565:
2791 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2792 break;
57779d06 2793 case DRM_FORMAT_XRGB8888:
57779d06
VS
2794 dspcntr |= DISPPLANE_BGRX888;
2795 break;
2796 case DRM_FORMAT_XBGR8888:
57779d06
VS
2797 dspcntr |= DISPPLANE_RGBX888;
2798 break;
2799 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2800 dspcntr |= DISPPLANE_BGRX101010;
2801 break;
2802 case DRM_FORMAT_XBGR2101010:
57779d06 2803 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2804 break;
2805 default:
baba133a 2806 BUG();
17638cd6
JB
2807 }
2808
2809 if (obj->tiling_mode != I915_TILING_NONE)
2810 dspcntr |= DISPPLANE_TILED;
17638cd6 2811
f45651ba 2812 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2813 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2814
b9897127 2815 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2816 intel_crtc->dspaddr_offset =
4e9a86b6
VS
2817 intel_gen4_compute_page_offset(dev_priv,
2818 &x, &y, obj->tiling_mode,
b9897127 2819 pixel_size,
bc752862 2820 fb->pitches[0]);
c2c75131 2821 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2822 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2823 dspcntr |= DISPPLANE_ROTATE_180;
2824
2825 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2826 x += (intel_crtc->config->pipe_src_w - 1);
2827 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2828
2829 /* Finding the last pixel of the last line of the display
2830 data and adding to linear_offset*/
2831 linear_offset +=
6e3c9717
ACO
2832 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2833 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2834 }
2835 }
2836
2837 I915_WRITE(reg, dspcntr);
17638cd6 2838
01f2c773 2839 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2840 I915_WRITE(DSPSURF(plane),
2841 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2842 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2843 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2844 } else {
2845 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2846 I915_WRITE(DSPLINOFF(plane), linear_offset);
2847 }
17638cd6 2848 POSTING_READ(reg);
17638cd6
JB
2849}
2850
b321803d
DL
2851u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2852 uint32_t pixel_format)
2853{
2854 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2855
2856 /*
2857 * The stride is either expressed as a multiple of 64 bytes
2858 * chunks for linear buffers or in number of tiles for tiled
2859 * buffers.
2860 */
2861 switch (fb_modifier) {
2862 case DRM_FORMAT_MOD_NONE:
2863 return 64;
2864 case I915_FORMAT_MOD_X_TILED:
2865 if (INTEL_INFO(dev)->gen == 2)
2866 return 128;
2867 return 512;
2868 case I915_FORMAT_MOD_Y_TILED:
2869 /* No need to check for old gens and Y tiling since this is
2870 * about the display engine and those will be blocked before
2871 * we get here.
2872 */
2873 return 128;
2874 case I915_FORMAT_MOD_Yf_TILED:
2875 if (bits_per_pixel == 8)
2876 return 64;
2877 else
2878 return 128;
2879 default:
2880 MISSING_CASE(fb_modifier);
2881 return 64;
2882 }
2883}
2884
121920fa
TU
2885unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2886 struct drm_i915_gem_object *obj)
2887{
9abc4648 2888 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
121920fa
TU
2889
2890 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
9abc4648 2891 view = &i915_ggtt_view_rotated;
121920fa
TU
2892
2893 return i915_gem_obj_ggtt_offset_view(obj, view);
2894}
2895
a1b2278e
CK
2896/*
2897 * This function detaches (aka. unbinds) unused scalers in hardware
2898 */
0583236e 2899static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e
CK
2900{
2901 struct drm_device *dev;
2902 struct drm_i915_private *dev_priv;
2903 struct intel_crtc_scaler_state *scaler_state;
2904 int i;
2905
a1b2278e
CK
2906 dev = intel_crtc->base.dev;
2907 dev_priv = dev->dev_private;
2908 scaler_state = &intel_crtc->config->scaler_state;
2909
2910 /* loop through and disable scalers that aren't in use */
2911 for (i = 0; i < intel_crtc->num_scalers; i++) {
2912 if (!scaler_state->scalers[i].in_use) {
2913 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2914 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2915 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2916 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2917 intel_crtc->base.base.id, intel_crtc->pipe, i);
2918 }
2919 }
2920}
2921
6156a456 2922u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2923{
6156a456 2924 switch (pixel_format) {
d161cf7a 2925 case DRM_FORMAT_C8:
c34ce3d1 2926 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2927 case DRM_FORMAT_RGB565:
c34ce3d1 2928 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2929 case DRM_FORMAT_XBGR8888:
c34ce3d1 2930 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2931 case DRM_FORMAT_XRGB8888:
c34ce3d1 2932 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2933 /*
2934 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2935 * to be already pre-multiplied. We need to add a knob (or a different
2936 * DRM_FORMAT) for user-space to configure that.
2937 */
f75fb42a 2938 case DRM_FORMAT_ABGR8888:
c34ce3d1 2939 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2940 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2941 case DRM_FORMAT_ARGB8888:
c34ce3d1 2942 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2943 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2944 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2945 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2946 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2947 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2948 case DRM_FORMAT_YUYV:
c34ce3d1 2949 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2950 case DRM_FORMAT_YVYU:
c34ce3d1 2951 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2952 case DRM_FORMAT_UYVY:
c34ce3d1 2953 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2954 case DRM_FORMAT_VYUY:
c34ce3d1 2955 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2956 default:
4249eeef 2957 MISSING_CASE(pixel_format);
70d21f0e 2958 }
8cfcba41 2959
c34ce3d1 2960 return 0;
6156a456 2961}
70d21f0e 2962
6156a456
CK
2963u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2964{
6156a456 2965 switch (fb_modifier) {
30af77c4 2966 case DRM_FORMAT_MOD_NONE:
70d21f0e 2967 break;
30af77c4 2968 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2969 return PLANE_CTL_TILED_X;
b321803d 2970 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2971 return PLANE_CTL_TILED_Y;
b321803d 2972 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2973 return PLANE_CTL_TILED_YF;
70d21f0e 2974 default:
6156a456 2975 MISSING_CASE(fb_modifier);
70d21f0e 2976 }
8cfcba41 2977
c34ce3d1 2978 return 0;
6156a456 2979}
70d21f0e 2980
6156a456
CK
2981u32 skl_plane_ctl_rotation(unsigned int rotation)
2982{
3b7a5119 2983 switch (rotation) {
6156a456
CK
2984 case BIT(DRM_ROTATE_0):
2985 break;
1e8df167
SJ
2986 /*
2987 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2988 * while i915 HW rotation is clockwise, thats why this swapping.
2989 */
3b7a5119 2990 case BIT(DRM_ROTATE_90):
1e8df167 2991 return PLANE_CTL_ROTATE_270;
3b7a5119 2992 case BIT(DRM_ROTATE_180):
c34ce3d1 2993 return PLANE_CTL_ROTATE_180;
3b7a5119 2994 case BIT(DRM_ROTATE_270):
1e8df167 2995 return PLANE_CTL_ROTATE_90;
6156a456
CK
2996 default:
2997 MISSING_CASE(rotation);
2998 }
2999
c34ce3d1 3000 return 0;
6156a456
CK
3001}
3002
3003static void skylake_update_primary_plane(struct drm_crtc *crtc,
3004 struct drm_framebuffer *fb,
3005 int x, int y)
3006{
3007 struct drm_device *dev = crtc->dev;
3008 struct drm_i915_private *dev_priv = dev->dev_private;
3009 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
3010 struct drm_plane *plane = crtc->primary;
3011 bool visible = to_intel_plane_state(plane->state)->visible;
6156a456
CK
3012 struct drm_i915_gem_object *obj;
3013 int pipe = intel_crtc->pipe;
3014 u32 plane_ctl, stride_div, stride;
3015 u32 tile_height, plane_offset, plane_size;
3016 unsigned int rotation;
3017 int x_offset, y_offset;
3018 unsigned long surf_addr;
6156a456
CK
3019 struct intel_crtc_state *crtc_state = intel_crtc->config;
3020 struct intel_plane_state *plane_state;
3021 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3022 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3023 int scaler_id = -1;
3024
6156a456
CK
3025 plane_state = to_intel_plane_state(plane->state);
3026
b70709a6 3027 if (!visible || !fb) {
6156a456
CK
3028 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3029 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3030 POSTING_READ(PLANE_CTL(pipe, 0));
3031 return;
3b7a5119 3032 }
70d21f0e 3033
6156a456
CK
3034 plane_ctl = PLANE_CTL_ENABLE |
3035 PLANE_CTL_PIPE_GAMMA_ENABLE |
3036 PLANE_CTL_PIPE_CSC_ENABLE;
3037
3038 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3039 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3040 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3041
3042 rotation = plane->state->rotation;
3043 plane_ctl |= skl_plane_ctl_rotation(rotation);
3044
b321803d
DL
3045 obj = intel_fb_obj(fb);
3046 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3047 fb->pixel_format);
3b7a5119
SJ
3048 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3049
6156a456
CK
3050 /*
3051 * FIXME: intel_plane_state->src, dst aren't set when transitional
3052 * update_plane helpers are called from legacy paths.
3053 * Once full atomic crtc is available, below check can be avoided.
3054 */
3055 if (drm_rect_width(&plane_state->src)) {
3056 scaler_id = plane_state->scaler_id;
3057 src_x = plane_state->src.x1 >> 16;
3058 src_y = plane_state->src.y1 >> 16;
3059 src_w = drm_rect_width(&plane_state->src) >> 16;
3060 src_h = drm_rect_height(&plane_state->src) >> 16;
3061 dst_x = plane_state->dst.x1;
3062 dst_y = plane_state->dst.y1;
3063 dst_w = drm_rect_width(&plane_state->dst);
3064 dst_h = drm_rect_height(&plane_state->dst);
3065
3066 WARN_ON(x != src_x || y != src_y);
3067 } else {
3068 src_w = intel_crtc->config->pipe_src_w;
3069 src_h = intel_crtc->config->pipe_src_h;
3070 }
3071
3b7a5119
SJ
3072 if (intel_rotation_90_or_270(rotation)) {
3073 /* stride = Surface height in tiles */
2614f17d 3074 tile_height = intel_tile_height(dev, fb->pixel_format,
3b7a5119
SJ
3075 fb->modifier[0]);
3076 stride = DIV_ROUND_UP(fb->height, tile_height);
6156a456 3077 x_offset = stride * tile_height - y - src_h;
3b7a5119 3078 y_offset = x;
6156a456 3079 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3080 } else {
3081 stride = fb->pitches[0] / stride_div;
3082 x_offset = x;
3083 y_offset = y;
6156a456 3084 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3085 }
3086 plane_offset = y_offset << 16 | x_offset;
b321803d 3087
70d21f0e 3088 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3089 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3090 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3091 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3092
3093 if (scaler_id >= 0) {
3094 uint32_t ps_ctrl = 0;
3095
3096 WARN_ON(!dst_w || !dst_h);
3097 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3098 crtc_state->scaler_state.scalers[scaler_id].mode;
3099 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3100 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3101 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3102 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3103 I915_WRITE(PLANE_POS(pipe, 0), 0);
3104 } else {
3105 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3106 }
3107
121920fa 3108 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3109
3110 POSTING_READ(PLANE_SURF(pipe, 0));
3111}
3112
17638cd6
JB
3113/* Assume fb object is pinned & idle & fenced and just update base pointers */
3114static int
3115intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3116 int x, int y, enum mode_set_atomic state)
3117{
3118 struct drm_device *dev = crtc->dev;
3119 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 3120
6b8e6ed0
CW
3121 if (dev_priv->display.disable_fbc)
3122 dev_priv->display.disable_fbc(dev);
81255565 3123
29b9bde6
DV
3124 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3125
3126 return 0;
81255565
JB
3127}
3128
7514747d 3129static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3130{
96a02917
VS
3131 struct drm_crtc *crtc;
3132
70e1e0ec 3133 for_each_crtc(dev, crtc) {
96a02917
VS
3134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3135 enum plane plane = intel_crtc->plane;
3136
3137 intel_prepare_page_flip(dev, plane);
3138 intel_finish_page_flip_plane(dev, plane);
3139 }
7514747d
VS
3140}
3141
3142static void intel_update_primary_planes(struct drm_device *dev)
3143{
3144 struct drm_i915_private *dev_priv = dev->dev_private;
3145 struct drm_crtc *crtc;
96a02917 3146
70e1e0ec 3147 for_each_crtc(dev, crtc) {
96a02917
VS
3148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3149
51fd371b 3150 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
3151 /*
3152 * FIXME: Once we have proper support for primary planes (and
3153 * disabling them without disabling the entire crtc) allow again
66e514c1 3154 * a NULL crtc->primary->fb.
947fdaad 3155 */
f4510a27 3156 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 3157 dev_priv->display.update_primary_plane(crtc,
66e514c1 3158 crtc->primary->fb,
262ca2b0
MR
3159 crtc->x,
3160 crtc->y);
51fd371b 3161 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
3162 }
3163}
3164
7514747d
VS
3165void intel_prepare_reset(struct drm_device *dev)
3166{
3167 /* no reset support for gen2 */
3168 if (IS_GEN2(dev))
3169 return;
3170
3171 /* reset doesn't touch the display */
3172 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3173 return;
3174
3175 drm_modeset_lock_all(dev);
f98ce92f
VS
3176 /*
3177 * Disabling the crtcs gracefully seems nicer. Also the
3178 * g33 docs say we should at least disable all the planes.
3179 */
6b72d486 3180 intel_display_suspend(dev);
7514747d
VS
3181}
3182
3183void intel_finish_reset(struct drm_device *dev)
3184{
3185 struct drm_i915_private *dev_priv = to_i915(dev);
3186
3187 /*
3188 * Flips in the rings will be nuked by the reset,
3189 * so complete all pending flips so that user space
3190 * will get its events and not get stuck.
3191 */
3192 intel_complete_page_flips(dev);
3193
3194 /* no reset support for gen2 */
3195 if (IS_GEN2(dev))
3196 return;
3197
3198 /* reset doesn't touch the display */
3199 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3200 /*
3201 * Flips in the rings have been nuked by the reset,
3202 * so update the base address of all primary
3203 * planes to the the last fb to make sure we're
3204 * showing the correct fb after a reset.
3205 */
3206 intel_update_primary_planes(dev);
3207 return;
3208 }
3209
3210 /*
3211 * The display has been reset as well,
3212 * so need a full re-initialization.
3213 */
3214 intel_runtime_pm_disable_interrupts(dev_priv);
3215 intel_runtime_pm_enable_interrupts(dev_priv);
3216
3217 intel_modeset_init_hw(dev);
3218
3219 spin_lock_irq(&dev_priv->irq_lock);
3220 if (dev_priv->display.hpd_irq_setup)
3221 dev_priv->display.hpd_irq_setup(dev);
3222 spin_unlock_irq(&dev_priv->irq_lock);
3223
3224 intel_modeset_setup_hw_state(dev, true);
3225
3226 intel_hpd_init(dev_priv);
3227
3228 drm_modeset_unlock_all(dev);
3229}
3230
2e2f351d 3231static void
14667a4b
CW
3232intel_finish_fb(struct drm_framebuffer *old_fb)
3233{
2ff8fde1 3234 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
2e2f351d 3235 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
14667a4b
CW
3236 bool was_interruptible = dev_priv->mm.interruptible;
3237 int ret;
3238
14667a4b
CW
3239 /* Big Hammer, we also need to ensure that any pending
3240 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3241 * current scanout is retired before unpinning the old
2e2f351d
CW
3242 * framebuffer. Note that we rely on userspace rendering
3243 * into the buffer attached to the pipe they are waiting
3244 * on. If not, userspace generates a GPU hang with IPEHR
3245 * point to the MI_WAIT_FOR_EVENT.
14667a4b
CW
3246 *
3247 * This should only fail upon a hung GPU, in which case we
3248 * can safely continue.
3249 */
3250 dev_priv->mm.interruptible = false;
2e2f351d 3251 ret = i915_gem_object_wait_rendering(obj, true);
14667a4b
CW
3252 dev_priv->mm.interruptible = was_interruptible;
3253
2e2f351d 3254 WARN_ON(ret);
14667a4b
CW
3255}
3256
7d5e3799
CW
3257static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3258{
3259 struct drm_device *dev = crtc->dev;
3260 struct drm_i915_private *dev_priv = dev->dev_private;
3261 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3262 bool pending;
3263
3264 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3265 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3266 return false;
3267
5e2d7afc 3268 spin_lock_irq(&dev->event_lock);
7d5e3799 3269 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3270 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3271
3272 return pending;
3273}
3274
e30e8f75
GP
3275static void intel_update_pipe_size(struct intel_crtc *crtc)
3276{
3277 struct drm_device *dev = crtc->base.dev;
3278 struct drm_i915_private *dev_priv = dev->dev_private;
3279 const struct drm_display_mode *adjusted_mode;
3280
3281 if (!i915.fastboot)
3282 return;
3283
3284 /*
3285 * Update pipe size and adjust fitter if needed: the reason for this is
3286 * that in compute_mode_changes we check the native mode (not the pfit
3287 * mode) to see if we can flip rather than do a full mode set. In the
3288 * fastboot case, we'll flip, but if we don't update the pipesrc and
3289 * pfit state, we'll end up with a big fb scanned out into the wrong
3290 * sized surface.
3291 *
3292 * To fix this properly, we need to hoist the checks up into
3293 * compute_mode_changes (or above), check the actual pfit state and
3294 * whether the platform allows pfit disable with pipe active, and only
3295 * then update the pipesrc and pfit state, even on the flip path.
3296 */
3297
6e3c9717 3298 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
3299
3300 I915_WRITE(PIPESRC(crtc->pipe),
3301 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3302 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 3303 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
3304 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3305 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3306 I915_WRITE(PF_CTL(crtc->pipe), 0);
3307 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3308 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3309 }
6e3c9717
ACO
3310 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3311 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3312}
3313
5e84e1a4
ZW
3314static void intel_fdi_normal_train(struct drm_crtc *crtc)
3315{
3316 struct drm_device *dev = crtc->dev;
3317 struct drm_i915_private *dev_priv = dev->dev_private;
3318 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3319 int pipe = intel_crtc->pipe;
3320 u32 reg, temp;
3321
3322 /* enable normal train */
3323 reg = FDI_TX_CTL(pipe);
3324 temp = I915_READ(reg);
61e499bf 3325 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3326 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3327 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3328 } else {
3329 temp &= ~FDI_LINK_TRAIN_NONE;
3330 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3331 }
5e84e1a4
ZW
3332 I915_WRITE(reg, temp);
3333
3334 reg = FDI_RX_CTL(pipe);
3335 temp = I915_READ(reg);
3336 if (HAS_PCH_CPT(dev)) {
3337 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3338 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3339 } else {
3340 temp &= ~FDI_LINK_TRAIN_NONE;
3341 temp |= FDI_LINK_TRAIN_NONE;
3342 }
3343 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3344
3345 /* wait one idle pattern time */
3346 POSTING_READ(reg);
3347 udelay(1000);
357555c0
JB
3348
3349 /* IVB wants error correction enabled */
3350 if (IS_IVYBRIDGE(dev))
3351 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3352 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3353}
3354
8db9d77b
ZW
3355/* The FDI link training functions for ILK/Ibexpeak. */
3356static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3357{
3358 struct drm_device *dev = crtc->dev;
3359 struct drm_i915_private *dev_priv = dev->dev_private;
3360 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3361 int pipe = intel_crtc->pipe;
5eddb70b 3362 u32 reg, temp, tries;
8db9d77b 3363
1c8562f6 3364 /* FDI needs bits from pipe first */
0fc932b8 3365 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3366
e1a44743
AJ
3367 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3368 for train result */
5eddb70b
CW
3369 reg = FDI_RX_IMR(pipe);
3370 temp = I915_READ(reg);
e1a44743
AJ
3371 temp &= ~FDI_RX_SYMBOL_LOCK;
3372 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3373 I915_WRITE(reg, temp);
3374 I915_READ(reg);
e1a44743
AJ
3375 udelay(150);
3376
8db9d77b 3377 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3378 reg = FDI_TX_CTL(pipe);
3379 temp = I915_READ(reg);
627eb5a3 3380 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3381 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3382 temp &= ~FDI_LINK_TRAIN_NONE;
3383 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3384 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3385
5eddb70b
CW
3386 reg = FDI_RX_CTL(pipe);
3387 temp = I915_READ(reg);
8db9d77b
ZW
3388 temp &= ~FDI_LINK_TRAIN_NONE;
3389 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3390 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3391
3392 POSTING_READ(reg);
8db9d77b
ZW
3393 udelay(150);
3394
5b2adf89 3395 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3396 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3397 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3398 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3399
5eddb70b 3400 reg = FDI_RX_IIR(pipe);
e1a44743 3401 for (tries = 0; tries < 5; tries++) {
5eddb70b 3402 temp = I915_READ(reg);
8db9d77b
ZW
3403 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3404
3405 if ((temp & FDI_RX_BIT_LOCK)) {
3406 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3407 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3408 break;
3409 }
8db9d77b 3410 }
e1a44743 3411 if (tries == 5)
5eddb70b 3412 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3413
3414 /* Train 2 */
5eddb70b
CW
3415 reg = FDI_TX_CTL(pipe);
3416 temp = I915_READ(reg);
8db9d77b
ZW
3417 temp &= ~FDI_LINK_TRAIN_NONE;
3418 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3419 I915_WRITE(reg, temp);
8db9d77b 3420
5eddb70b
CW
3421 reg = FDI_RX_CTL(pipe);
3422 temp = I915_READ(reg);
8db9d77b
ZW
3423 temp &= ~FDI_LINK_TRAIN_NONE;
3424 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3425 I915_WRITE(reg, temp);
8db9d77b 3426
5eddb70b
CW
3427 POSTING_READ(reg);
3428 udelay(150);
8db9d77b 3429
5eddb70b 3430 reg = FDI_RX_IIR(pipe);
e1a44743 3431 for (tries = 0; tries < 5; tries++) {
5eddb70b 3432 temp = I915_READ(reg);
8db9d77b
ZW
3433 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3434
3435 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3436 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3437 DRM_DEBUG_KMS("FDI train 2 done.\n");
3438 break;
3439 }
8db9d77b 3440 }
e1a44743 3441 if (tries == 5)
5eddb70b 3442 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3443
3444 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3445
8db9d77b
ZW
3446}
3447
0206e353 3448static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3449 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3450 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3451 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3452 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3453};
3454
3455/* The FDI link training functions for SNB/Cougarpoint. */
3456static void gen6_fdi_link_train(struct drm_crtc *crtc)
3457{
3458 struct drm_device *dev = crtc->dev;
3459 struct drm_i915_private *dev_priv = dev->dev_private;
3460 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3461 int pipe = intel_crtc->pipe;
fa37d39e 3462 u32 reg, temp, i, retry;
8db9d77b 3463
e1a44743
AJ
3464 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3465 for train result */
5eddb70b
CW
3466 reg = FDI_RX_IMR(pipe);
3467 temp = I915_READ(reg);
e1a44743
AJ
3468 temp &= ~FDI_RX_SYMBOL_LOCK;
3469 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3470 I915_WRITE(reg, temp);
3471
3472 POSTING_READ(reg);
e1a44743
AJ
3473 udelay(150);
3474
8db9d77b 3475 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3476 reg = FDI_TX_CTL(pipe);
3477 temp = I915_READ(reg);
627eb5a3 3478 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3479 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3480 temp &= ~FDI_LINK_TRAIN_NONE;
3481 temp |= FDI_LINK_TRAIN_PATTERN_1;
3482 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3483 /* SNB-B */
3484 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3485 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3486
d74cf324
DV
3487 I915_WRITE(FDI_RX_MISC(pipe),
3488 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3489
5eddb70b
CW
3490 reg = FDI_RX_CTL(pipe);
3491 temp = I915_READ(reg);
8db9d77b
ZW
3492 if (HAS_PCH_CPT(dev)) {
3493 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3494 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3495 } else {
3496 temp &= ~FDI_LINK_TRAIN_NONE;
3497 temp |= FDI_LINK_TRAIN_PATTERN_1;
3498 }
5eddb70b
CW
3499 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3500
3501 POSTING_READ(reg);
8db9d77b
ZW
3502 udelay(150);
3503
0206e353 3504 for (i = 0; i < 4; i++) {
5eddb70b
CW
3505 reg = FDI_TX_CTL(pipe);
3506 temp = I915_READ(reg);
8db9d77b
ZW
3507 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3508 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3509 I915_WRITE(reg, temp);
3510
3511 POSTING_READ(reg);
8db9d77b
ZW
3512 udelay(500);
3513
fa37d39e
SP
3514 for (retry = 0; retry < 5; retry++) {
3515 reg = FDI_RX_IIR(pipe);
3516 temp = I915_READ(reg);
3517 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3518 if (temp & FDI_RX_BIT_LOCK) {
3519 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3520 DRM_DEBUG_KMS("FDI train 1 done.\n");
3521 break;
3522 }
3523 udelay(50);
8db9d77b 3524 }
fa37d39e
SP
3525 if (retry < 5)
3526 break;
8db9d77b
ZW
3527 }
3528 if (i == 4)
5eddb70b 3529 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3530
3531 /* Train 2 */
5eddb70b
CW
3532 reg = FDI_TX_CTL(pipe);
3533 temp = I915_READ(reg);
8db9d77b
ZW
3534 temp &= ~FDI_LINK_TRAIN_NONE;
3535 temp |= FDI_LINK_TRAIN_PATTERN_2;
3536 if (IS_GEN6(dev)) {
3537 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3538 /* SNB-B */
3539 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3540 }
5eddb70b 3541 I915_WRITE(reg, temp);
8db9d77b 3542
5eddb70b
CW
3543 reg = FDI_RX_CTL(pipe);
3544 temp = I915_READ(reg);
8db9d77b
ZW
3545 if (HAS_PCH_CPT(dev)) {
3546 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3547 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3548 } else {
3549 temp &= ~FDI_LINK_TRAIN_NONE;
3550 temp |= FDI_LINK_TRAIN_PATTERN_2;
3551 }
5eddb70b
CW
3552 I915_WRITE(reg, temp);
3553
3554 POSTING_READ(reg);
8db9d77b
ZW
3555 udelay(150);
3556
0206e353 3557 for (i = 0; i < 4; i++) {
5eddb70b
CW
3558 reg = FDI_TX_CTL(pipe);
3559 temp = I915_READ(reg);
8db9d77b
ZW
3560 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3561 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3562 I915_WRITE(reg, temp);
3563
3564 POSTING_READ(reg);
8db9d77b
ZW
3565 udelay(500);
3566
fa37d39e
SP
3567 for (retry = 0; retry < 5; retry++) {
3568 reg = FDI_RX_IIR(pipe);
3569 temp = I915_READ(reg);
3570 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3571 if (temp & FDI_RX_SYMBOL_LOCK) {
3572 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3573 DRM_DEBUG_KMS("FDI train 2 done.\n");
3574 break;
3575 }
3576 udelay(50);
8db9d77b 3577 }
fa37d39e
SP
3578 if (retry < 5)
3579 break;
8db9d77b
ZW
3580 }
3581 if (i == 4)
5eddb70b 3582 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3583
3584 DRM_DEBUG_KMS("FDI train done.\n");
3585}
3586
357555c0
JB
3587/* Manual link training for Ivy Bridge A0 parts */
3588static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3589{
3590 struct drm_device *dev = crtc->dev;
3591 struct drm_i915_private *dev_priv = dev->dev_private;
3592 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3593 int pipe = intel_crtc->pipe;
139ccd3f 3594 u32 reg, temp, i, j;
357555c0
JB
3595
3596 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3597 for train result */
3598 reg = FDI_RX_IMR(pipe);
3599 temp = I915_READ(reg);
3600 temp &= ~FDI_RX_SYMBOL_LOCK;
3601 temp &= ~FDI_RX_BIT_LOCK;
3602 I915_WRITE(reg, temp);
3603
3604 POSTING_READ(reg);
3605 udelay(150);
3606
01a415fd
DV
3607 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3608 I915_READ(FDI_RX_IIR(pipe)));
3609
139ccd3f
JB
3610 /* Try each vswing and preemphasis setting twice before moving on */
3611 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3612 /* disable first in case we need to retry */
3613 reg = FDI_TX_CTL(pipe);
3614 temp = I915_READ(reg);
3615 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3616 temp &= ~FDI_TX_ENABLE;
3617 I915_WRITE(reg, temp);
357555c0 3618
139ccd3f
JB
3619 reg = FDI_RX_CTL(pipe);
3620 temp = I915_READ(reg);
3621 temp &= ~FDI_LINK_TRAIN_AUTO;
3622 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3623 temp &= ~FDI_RX_ENABLE;
3624 I915_WRITE(reg, temp);
357555c0 3625
139ccd3f 3626 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3627 reg = FDI_TX_CTL(pipe);
3628 temp = I915_READ(reg);
139ccd3f 3629 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3630 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3631 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3632 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3633 temp |= snb_b_fdi_train_param[j/2];
3634 temp |= FDI_COMPOSITE_SYNC;
3635 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3636
139ccd3f
JB
3637 I915_WRITE(FDI_RX_MISC(pipe),
3638 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3639
139ccd3f 3640 reg = FDI_RX_CTL(pipe);
357555c0 3641 temp = I915_READ(reg);
139ccd3f
JB
3642 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3643 temp |= FDI_COMPOSITE_SYNC;
3644 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3645
139ccd3f
JB
3646 POSTING_READ(reg);
3647 udelay(1); /* should be 0.5us */
357555c0 3648
139ccd3f
JB
3649 for (i = 0; i < 4; i++) {
3650 reg = FDI_RX_IIR(pipe);
3651 temp = I915_READ(reg);
3652 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3653
139ccd3f
JB
3654 if (temp & FDI_RX_BIT_LOCK ||
3655 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3656 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3657 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3658 i);
3659 break;
3660 }
3661 udelay(1); /* should be 0.5us */
3662 }
3663 if (i == 4) {
3664 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3665 continue;
3666 }
357555c0 3667
139ccd3f 3668 /* Train 2 */
357555c0
JB
3669 reg = FDI_TX_CTL(pipe);
3670 temp = I915_READ(reg);
139ccd3f
JB
3671 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3672 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3673 I915_WRITE(reg, temp);
3674
3675 reg = FDI_RX_CTL(pipe);
3676 temp = I915_READ(reg);
3677 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3678 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3679 I915_WRITE(reg, temp);
3680
3681 POSTING_READ(reg);
139ccd3f 3682 udelay(2); /* should be 1.5us */
357555c0 3683
139ccd3f
JB
3684 for (i = 0; i < 4; i++) {
3685 reg = FDI_RX_IIR(pipe);
3686 temp = I915_READ(reg);
3687 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3688
139ccd3f
JB
3689 if (temp & FDI_RX_SYMBOL_LOCK ||
3690 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3691 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3692 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3693 i);
3694 goto train_done;
3695 }
3696 udelay(2); /* should be 1.5us */
357555c0 3697 }
139ccd3f
JB
3698 if (i == 4)
3699 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3700 }
357555c0 3701
139ccd3f 3702train_done:
357555c0
JB
3703 DRM_DEBUG_KMS("FDI train done.\n");
3704}
3705
88cefb6c 3706static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3707{
88cefb6c 3708 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3709 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3710 int pipe = intel_crtc->pipe;
5eddb70b 3711 u32 reg, temp;
79e53945 3712
c64e311e 3713
c98e9dcf 3714 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3715 reg = FDI_RX_CTL(pipe);
3716 temp = I915_READ(reg);
627eb5a3 3717 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3718 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3719 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3720 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3721
3722 POSTING_READ(reg);
c98e9dcf
JB
3723 udelay(200);
3724
3725 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3726 temp = I915_READ(reg);
3727 I915_WRITE(reg, temp | FDI_PCDCLK);
3728
3729 POSTING_READ(reg);
c98e9dcf
JB
3730 udelay(200);
3731
20749730
PZ
3732 /* Enable CPU FDI TX PLL, always on for Ironlake */
3733 reg = FDI_TX_CTL(pipe);
3734 temp = I915_READ(reg);
3735 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3736 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3737
20749730
PZ
3738 POSTING_READ(reg);
3739 udelay(100);
6be4a607 3740 }
0e23b99d
JB
3741}
3742
88cefb6c
DV
3743static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3744{
3745 struct drm_device *dev = intel_crtc->base.dev;
3746 struct drm_i915_private *dev_priv = dev->dev_private;
3747 int pipe = intel_crtc->pipe;
3748 u32 reg, temp;
3749
3750 /* Switch from PCDclk to Rawclk */
3751 reg = FDI_RX_CTL(pipe);
3752 temp = I915_READ(reg);
3753 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3754
3755 /* Disable CPU FDI TX PLL */
3756 reg = FDI_TX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3759
3760 POSTING_READ(reg);
3761 udelay(100);
3762
3763 reg = FDI_RX_CTL(pipe);
3764 temp = I915_READ(reg);
3765 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3766
3767 /* Wait for the clocks to turn off. */
3768 POSTING_READ(reg);
3769 udelay(100);
3770}
3771
0fc932b8
JB
3772static void ironlake_fdi_disable(struct drm_crtc *crtc)
3773{
3774 struct drm_device *dev = crtc->dev;
3775 struct drm_i915_private *dev_priv = dev->dev_private;
3776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3777 int pipe = intel_crtc->pipe;
3778 u32 reg, temp;
3779
3780 /* disable CPU FDI tx and PCH FDI rx */
3781 reg = FDI_TX_CTL(pipe);
3782 temp = I915_READ(reg);
3783 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3784 POSTING_READ(reg);
3785
3786 reg = FDI_RX_CTL(pipe);
3787 temp = I915_READ(reg);
3788 temp &= ~(0x7 << 16);
dfd07d72 3789 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3790 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3791
3792 POSTING_READ(reg);
3793 udelay(100);
3794
3795 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3796 if (HAS_PCH_IBX(dev))
6f06ce18 3797 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3798
3799 /* still set train pattern 1 */
3800 reg = FDI_TX_CTL(pipe);
3801 temp = I915_READ(reg);
3802 temp &= ~FDI_LINK_TRAIN_NONE;
3803 temp |= FDI_LINK_TRAIN_PATTERN_1;
3804 I915_WRITE(reg, temp);
3805
3806 reg = FDI_RX_CTL(pipe);
3807 temp = I915_READ(reg);
3808 if (HAS_PCH_CPT(dev)) {
3809 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3810 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3811 } else {
3812 temp &= ~FDI_LINK_TRAIN_NONE;
3813 temp |= FDI_LINK_TRAIN_PATTERN_1;
3814 }
3815 /* BPC in FDI rx is consistent with that in PIPECONF */
3816 temp &= ~(0x07 << 16);
dfd07d72 3817 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3818 I915_WRITE(reg, temp);
3819
3820 POSTING_READ(reg);
3821 udelay(100);
3822}
3823
5dce5b93
CW
3824bool intel_has_pending_fb_unpin(struct drm_device *dev)
3825{
3826 struct intel_crtc *crtc;
3827
3828 /* Note that we don't need to be called with mode_config.lock here
3829 * as our list of CRTC objects is static for the lifetime of the
3830 * device and so cannot disappear as we iterate. Similarly, we can
3831 * happily treat the predicates as racy, atomic checks as userspace
3832 * cannot claim and pin a new fb without at least acquring the
3833 * struct_mutex and so serialising with us.
3834 */
d3fcc808 3835 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3836 if (atomic_read(&crtc->unpin_work_count) == 0)
3837 continue;
3838
3839 if (crtc->unpin_work)
3840 intel_wait_for_vblank(dev, crtc->pipe);
3841
3842 return true;
3843 }
3844
3845 return false;
3846}
3847
d6bbafa1
CW
3848static void page_flip_completed(struct intel_crtc *intel_crtc)
3849{
3850 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3851 struct intel_unpin_work *work = intel_crtc->unpin_work;
3852
3853 /* ensure that the unpin work is consistent wrt ->pending. */
3854 smp_rmb();
3855 intel_crtc->unpin_work = NULL;
3856
3857 if (work->event)
3858 drm_send_vblank_event(intel_crtc->base.dev,
3859 intel_crtc->pipe,
3860 work->event);
3861
3862 drm_crtc_vblank_put(&intel_crtc->base);
3863
3864 wake_up_all(&dev_priv->pending_flip_queue);
3865 queue_work(dev_priv->wq, &work->work);
3866
3867 trace_i915_flip_complete(intel_crtc->plane,
3868 work->pending_flip_obj);
3869}
3870
46a55d30 3871void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3872{
0f91128d 3873 struct drm_device *dev = crtc->dev;
5bb61643 3874 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3875
2c10d571 3876 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3877 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3878 !intel_crtc_has_pending_flip(crtc),
3879 60*HZ) == 0)) {
3880 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3881
5e2d7afc 3882 spin_lock_irq(&dev->event_lock);
9c787942
CW
3883 if (intel_crtc->unpin_work) {
3884 WARN_ONCE(1, "Removing stuck page flip\n");
3885 page_flip_completed(intel_crtc);
3886 }
5e2d7afc 3887 spin_unlock_irq(&dev->event_lock);
9c787942 3888 }
5bb61643 3889
975d568a
CW
3890 if (crtc->primary->fb) {
3891 mutex_lock(&dev->struct_mutex);
3892 intel_finish_fb(crtc->primary->fb);
3893 mutex_unlock(&dev->struct_mutex);
3894 }
e6c3a2a6
CW
3895}
3896
e615efe4
ED
3897/* Program iCLKIP clock to the desired frequency */
3898static void lpt_program_iclkip(struct drm_crtc *crtc)
3899{
3900 struct drm_device *dev = crtc->dev;
3901 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3902 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3903 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3904 u32 temp;
3905
a580516d 3906 mutex_lock(&dev_priv->sb_lock);
09153000 3907
e615efe4
ED
3908 /* It is necessary to ungate the pixclk gate prior to programming
3909 * the divisors, and gate it back when it is done.
3910 */
3911 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3912
3913 /* Disable SSCCTL */
3914 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3915 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3916 SBI_SSCCTL_DISABLE,
3917 SBI_ICLK);
e615efe4
ED
3918
3919 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3920 if (clock == 20000) {
e615efe4
ED
3921 auxdiv = 1;
3922 divsel = 0x41;
3923 phaseinc = 0x20;
3924 } else {
3925 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3926 * but the adjusted_mode->crtc_clock in in KHz. To get the
3927 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3928 * convert the virtual clock precision to KHz here for higher
3929 * precision.
3930 */
3931 u32 iclk_virtual_root_freq = 172800 * 1000;
3932 u32 iclk_pi_range = 64;
3933 u32 desired_divisor, msb_divisor_value, pi_value;
3934
12d7ceed 3935 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3936 msb_divisor_value = desired_divisor / iclk_pi_range;
3937 pi_value = desired_divisor % iclk_pi_range;
3938
3939 auxdiv = 0;
3940 divsel = msb_divisor_value - 2;
3941 phaseinc = pi_value;
3942 }
3943
3944 /* This should not happen with any sane values */
3945 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3946 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3947 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3948 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3949
3950 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3951 clock,
e615efe4
ED
3952 auxdiv,
3953 divsel,
3954 phasedir,
3955 phaseinc);
3956
3957 /* Program SSCDIVINTPHASE6 */
988d6ee8 3958 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3959 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3960 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3961 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3962 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3963 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3964 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3965 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3966
3967 /* Program SSCAUXDIV */
988d6ee8 3968 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3969 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3970 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3971 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3972
3973 /* Enable modulator and associated divider */
988d6ee8 3974 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3975 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3976 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3977
3978 /* Wait for initialization time */
3979 udelay(24);
3980
3981 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000 3982
a580516d 3983 mutex_unlock(&dev_priv->sb_lock);
e615efe4
ED
3984}
3985
275f01b2
DV
3986static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3987 enum pipe pch_transcoder)
3988{
3989 struct drm_device *dev = crtc->base.dev;
3990 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3991 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
3992
3993 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3994 I915_READ(HTOTAL(cpu_transcoder)));
3995 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3996 I915_READ(HBLANK(cpu_transcoder)));
3997 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3998 I915_READ(HSYNC(cpu_transcoder)));
3999
4000 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4001 I915_READ(VTOTAL(cpu_transcoder)));
4002 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4003 I915_READ(VBLANK(cpu_transcoder)));
4004 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4005 I915_READ(VSYNC(cpu_transcoder)));
4006 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4007 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4008}
4009
003632d9 4010static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4011{
4012 struct drm_i915_private *dev_priv = dev->dev_private;
4013 uint32_t temp;
4014
4015 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4016 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4017 return;
4018
4019 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4020 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4021
003632d9
ACO
4022 temp &= ~FDI_BC_BIFURCATION_SELECT;
4023 if (enable)
4024 temp |= FDI_BC_BIFURCATION_SELECT;
4025
4026 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4027 I915_WRITE(SOUTH_CHICKEN1, temp);
4028 POSTING_READ(SOUTH_CHICKEN1);
4029}
4030
4031static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4032{
4033 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4034
4035 switch (intel_crtc->pipe) {
4036 case PIPE_A:
4037 break;
4038 case PIPE_B:
6e3c9717 4039 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4040 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4041 else
003632d9 4042 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4043
4044 break;
4045 case PIPE_C:
003632d9 4046 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4047
4048 break;
4049 default:
4050 BUG();
4051 }
4052}
4053
f67a559d
JB
4054/*
4055 * Enable PCH resources required for PCH ports:
4056 * - PCH PLLs
4057 * - FDI training & RX/TX
4058 * - update transcoder timings
4059 * - DP transcoding bits
4060 * - transcoder
4061 */
4062static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4063{
4064 struct drm_device *dev = crtc->dev;
4065 struct drm_i915_private *dev_priv = dev->dev_private;
4066 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4067 int pipe = intel_crtc->pipe;
ee7b9f93 4068 u32 reg, temp;
2c07245f 4069
ab9412ba 4070 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4071
1fbc0d78
DV
4072 if (IS_IVYBRIDGE(dev))
4073 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4074
cd986abb
DV
4075 /* Write the TU size bits before fdi link training, so that error
4076 * detection works. */
4077 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4078 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4079
c98e9dcf 4080 /* For PCH output, training FDI link */
674cf967 4081 dev_priv->display.fdi_link_train(crtc);
2c07245f 4082
3ad8a208
DV
4083 /* We need to program the right clock selection before writing the pixel
4084 * mutliplier into the DPLL. */
303b81e0 4085 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4086 u32 sel;
4b645f14 4087
c98e9dcf 4088 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4089 temp |= TRANS_DPLL_ENABLE(pipe);
4090 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 4091 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
4092 temp |= sel;
4093 else
4094 temp &= ~sel;
c98e9dcf 4095 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4096 }
5eddb70b 4097
3ad8a208
DV
4098 /* XXX: pch pll's can be enabled any time before we enable the PCH
4099 * transcoder, and we actually should do this to not upset any PCH
4100 * transcoder that already use the clock when we share it.
4101 *
4102 * Note that enable_shared_dpll tries to do the right thing, but
4103 * get_shared_dpll unconditionally resets the pll - we need that to have
4104 * the right LVDS enable sequence. */
85b3894f 4105 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4106
d9b6cb56
JB
4107 /* set transcoder timing, panel must allow it */
4108 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4109 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4110
303b81e0 4111 intel_fdi_normal_train(crtc);
5e84e1a4 4112
c98e9dcf 4113 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4114 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 4115 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
4116 reg = TRANS_DP_CTL(pipe);
4117 temp = I915_READ(reg);
4118 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4119 TRANS_DP_SYNC_MASK |
4120 TRANS_DP_BPC_MASK);
e3ef4479 4121 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4122 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
4123
4124 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4125 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 4126 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4127 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4128
4129 switch (intel_trans_dp_port_sel(crtc)) {
4130 case PCH_DP_B:
5eddb70b 4131 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
4132 break;
4133 case PCH_DP_C:
5eddb70b 4134 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
4135 break;
4136 case PCH_DP_D:
5eddb70b 4137 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4138 break;
4139 default:
e95d41e1 4140 BUG();
32f9d658 4141 }
2c07245f 4142
5eddb70b 4143 I915_WRITE(reg, temp);
6be4a607 4144 }
b52eb4dc 4145
b8a4f404 4146 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4147}
4148
1507e5bd
PZ
4149static void lpt_pch_enable(struct drm_crtc *crtc)
4150{
4151 struct drm_device *dev = crtc->dev;
4152 struct drm_i915_private *dev_priv = dev->dev_private;
4153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4154 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4155
ab9412ba 4156 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4157
8c52b5e8 4158 lpt_program_iclkip(crtc);
1507e5bd 4159
0540e488 4160 /* Set transcoder timing. */
275f01b2 4161 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4162
937bb610 4163 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4164}
4165
190f68c5
ACO
4166struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4167 struct intel_crtc_state *crtc_state)
ee7b9f93 4168{
e2b78267 4169 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 4170 struct intel_shared_dpll *pll;
de419ab6 4171 struct intel_shared_dpll_config *shared_dpll;
e2b78267 4172 enum intel_dpll_id i;
ee7b9f93 4173
de419ab6
ML
4174 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4175
98b6bd99
DV
4176 if (HAS_PCH_IBX(dev_priv->dev)) {
4177 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 4178 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 4179 pll = &dev_priv->shared_dplls[i];
98b6bd99 4180
46edb027
DV
4181 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4182 crtc->base.base.id, pll->name);
98b6bd99 4183
de419ab6 4184 WARN_ON(shared_dpll[i].crtc_mask);
f2a69f44 4185
98b6bd99
DV
4186 goto found;
4187 }
4188
bcddf610
S
4189 if (IS_BROXTON(dev_priv->dev)) {
4190 /* PLL is attached to port in bxt */
4191 struct intel_encoder *encoder;
4192 struct intel_digital_port *intel_dig_port;
4193
4194 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4195 if (WARN_ON(!encoder))
4196 return NULL;
4197
4198 intel_dig_port = enc_to_dig_port(&encoder->base);
4199 /* 1:1 mapping between ports and PLLs */
4200 i = (enum intel_dpll_id)intel_dig_port->port;
4201 pll = &dev_priv->shared_dplls[i];
4202 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4203 crtc->base.base.id, pll->name);
de419ab6 4204 WARN_ON(shared_dpll[i].crtc_mask);
bcddf610
S
4205
4206 goto found;
4207 }
4208
e72f9fbf
DV
4209 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4210 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
4211
4212 /* Only want to check enabled timings first */
de419ab6 4213 if (shared_dpll[i].crtc_mask == 0)
ee7b9f93
JB
4214 continue;
4215
190f68c5 4216 if (memcmp(&crtc_state->dpll_hw_state,
de419ab6
ML
4217 &shared_dpll[i].hw_state,
4218 sizeof(crtc_state->dpll_hw_state)) == 0) {
8bd31e67 4219 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 4220 crtc->base.base.id, pll->name,
de419ab6 4221 shared_dpll[i].crtc_mask,
8bd31e67 4222 pll->active);
ee7b9f93
JB
4223 goto found;
4224 }
4225 }
4226
4227 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
4228 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4229 pll = &dev_priv->shared_dplls[i];
de419ab6 4230 if (shared_dpll[i].crtc_mask == 0) {
46edb027
DV
4231 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4232 crtc->base.base.id, pll->name);
ee7b9f93
JB
4233 goto found;
4234 }
4235 }
4236
4237 return NULL;
4238
4239found:
de419ab6
ML
4240 if (shared_dpll[i].crtc_mask == 0)
4241 shared_dpll[i].hw_state =
4242 crtc_state->dpll_hw_state;
f2a69f44 4243
190f68c5 4244 crtc_state->shared_dpll = i;
46edb027
DV
4245 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4246 pipe_name(crtc->pipe));
ee7b9f93 4247
de419ab6 4248 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
e04c7350 4249
ee7b9f93
JB
4250 return pll;
4251}
4252
de419ab6 4253static void intel_shared_dpll_commit(struct drm_atomic_state *state)
8bd31e67 4254{
de419ab6
ML
4255 struct drm_i915_private *dev_priv = to_i915(state->dev);
4256 struct intel_shared_dpll_config *shared_dpll;
8bd31e67
ACO
4257 struct intel_shared_dpll *pll;
4258 enum intel_dpll_id i;
4259
de419ab6
ML
4260 if (!to_intel_atomic_state(state)->dpll_set)
4261 return;
8bd31e67 4262
de419ab6 4263 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
8bd31e67
ACO
4264 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4265 pll = &dev_priv->shared_dplls[i];
de419ab6 4266 pll->config = shared_dpll[i];
8bd31e67
ACO
4267 }
4268}
4269
a1520318 4270static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4271{
4272 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4273 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4274 u32 temp;
4275
4276 temp = I915_READ(dslreg);
4277 udelay(500);
4278 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4279 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4280 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4281 }
4282}
4283
86adf9d7
ML
4284static int
4285skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4286 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4287 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4288{
86adf9d7
ML
4289 struct intel_crtc_scaler_state *scaler_state =
4290 &crtc_state->scaler_state;
4291 struct intel_crtc *intel_crtc =
4292 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4293 int need_scaling;
6156a456
CK
4294
4295 need_scaling = intel_rotation_90_or_270(rotation) ?
4296 (src_h != dst_w || src_w != dst_h):
4297 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4298
4299 /*
4300 * if plane is being disabled or scaler is no more required or force detach
4301 * - free scaler binded to this plane/crtc
4302 * - in order to do this, update crtc->scaler_usage
4303 *
4304 * Here scaler state in crtc_state is set free so that
4305 * scaler can be assigned to other user. Actual register
4306 * update to free the scaler is done in plane/panel-fit programming.
4307 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4308 */
86adf9d7 4309 if (force_detach || !need_scaling) {
a1b2278e 4310 if (*scaler_id >= 0) {
86adf9d7 4311 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4312 scaler_state->scalers[*scaler_id].in_use = 0;
4313
86adf9d7
ML
4314 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4315 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4316 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4317 scaler_state->scaler_users);
4318 *scaler_id = -1;
4319 }
4320 return 0;
4321 }
4322
4323 /* range checks */
4324 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4325 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4326
4327 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4328 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4329 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4330 "size is out of scaler range\n",
86adf9d7 4331 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4332 return -EINVAL;
4333 }
4334
86adf9d7
ML
4335 /* mark this plane as a scaler user in crtc_state */
4336 scaler_state->scaler_users |= (1 << scaler_user);
4337 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4338 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4339 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4340 scaler_state->scaler_users);
4341
4342 return 0;
4343}
4344
4345/**
4346 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4347 *
4348 * @state: crtc's scaler state
4349 * @force_detach: whether to forcibly disable scaler
4350 *
4351 * Return
4352 * 0 - scaler_usage updated successfully
4353 * error - requested scaling cannot be supported or other error condition
4354 */
4355int skl_update_scaler_crtc(struct intel_crtc_state *state, int force_detach)
4356{
4357 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4358 struct drm_display_mode *adjusted_mode =
4359 &state->base.adjusted_mode;
4360
4361 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4362 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4363
4364 return skl_update_scaler(state, force_detach, SKL_CRTC_INDEX,
4365 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4366 state->pipe_src_w, state->pipe_src_h,
8c6cda29 4367 adjusted_mode->hdisplay, adjusted_mode->vdisplay);
86adf9d7
ML
4368}
4369
4370/**
4371 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4372 *
4373 * @state: crtc's scaler state
86adf9d7
ML
4374 * @plane_state: atomic plane state to update
4375 *
4376 * Return
4377 * 0 - scaler_usage updated successfully
4378 * error - requested scaling cannot be supported or other error condition
4379 */
da20eabd
ML
4380static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4381 struct intel_plane_state *plane_state)
86adf9d7
ML
4382{
4383
4384 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4385 struct intel_plane *intel_plane =
4386 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4387 struct drm_framebuffer *fb = plane_state->base.fb;
4388 int ret;
4389
4390 bool force_detach = !fb || !plane_state->visible;
4391
4392 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4393 intel_plane->base.base.id, intel_crtc->pipe,
4394 drm_plane_index(&intel_plane->base));
4395
4396 ret = skl_update_scaler(crtc_state, force_detach,
4397 drm_plane_index(&intel_plane->base),
4398 &plane_state->scaler_id,
4399 plane_state->base.rotation,
4400 drm_rect_width(&plane_state->src) >> 16,
4401 drm_rect_height(&plane_state->src) >> 16,
4402 drm_rect_width(&plane_state->dst),
4403 drm_rect_height(&plane_state->dst));
4404
4405 if (ret || plane_state->scaler_id < 0)
4406 return ret;
4407
a1b2278e 4408 /* check colorkey */
818ed961 4409 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
86adf9d7 4410 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
818ed961 4411 intel_plane->base.base.id);
a1b2278e
CK
4412 return -EINVAL;
4413 }
4414
4415 /* Check src format */
86adf9d7
ML
4416 switch (fb->pixel_format) {
4417 case DRM_FORMAT_RGB565:
4418 case DRM_FORMAT_XBGR8888:
4419 case DRM_FORMAT_XRGB8888:
4420 case DRM_FORMAT_ABGR8888:
4421 case DRM_FORMAT_ARGB8888:
4422 case DRM_FORMAT_XRGB2101010:
4423 case DRM_FORMAT_XBGR2101010:
4424 case DRM_FORMAT_YUYV:
4425 case DRM_FORMAT_YVYU:
4426 case DRM_FORMAT_UYVY:
4427 case DRM_FORMAT_VYUY:
4428 break;
4429 default:
4430 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4431 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4432 return -EINVAL;
a1b2278e
CK
4433 }
4434
a1b2278e
CK
4435 return 0;
4436}
4437
4438static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
bd2e244f
JB
4439{
4440 struct drm_device *dev = crtc->base.dev;
4441 struct drm_i915_private *dev_priv = dev->dev_private;
4442 int pipe = crtc->pipe;
a1b2278e
CK
4443 struct intel_crtc_scaler_state *scaler_state =
4444 &crtc->config->scaler_state;
4445
4446 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4447
4448 /* To update pfit, first update scaler state */
86adf9d7 4449 skl_update_scaler_crtc(crtc->config, !enable);
a1b2278e
CK
4450 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4451 skl_detach_scalers(crtc);
4452 if (!enable)
4453 return;
bd2e244f 4454
6e3c9717 4455 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4456 int id;
4457
4458 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4459 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4460 return;
4461 }
4462
4463 id = scaler_state->scaler_id;
4464 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4465 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4466 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4467 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4468
4469 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4470 }
4471}
4472
b074cec8
JB
4473static void ironlake_pfit_enable(struct intel_crtc *crtc)
4474{
4475 struct drm_device *dev = crtc->base.dev;
4476 struct drm_i915_private *dev_priv = dev->dev_private;
4477 int pipe = crtc->pipe;
4478
6e3c9717 4479 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4480 /* Force use of hard-coded filter coefficients
4481 * as some pre-programmed values are broken,
4482 * e.g. x201.
4483 */
4484 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4485 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4486 PF_PIPE_SEL_IVB(pipe));
4487 else
4488 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4489 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4490 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4491 }
4492}
4493
20bc8673 4494void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4495{
cea165c3
VS
4496 struct drm_device *dev = crtc->base.dev;
4497 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4498
6e3c9717 4499 if (!crtc->config->ips_enabled)
d77e4531
PZ
4500 return;
4501
cea165c3
VS
4502 /* We can only enable IPS after we enable a plane and wait for a vblank */
4503 intel_wait_for_vblank(dev, crtc->pipe);
4504
d77e4531 4505 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4506 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4507 mutex_lock(&dev_priv->rps.hw_lock);
4508 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4509 mutex_unlock(&dev_priv->rps.hw_lock);
4510 /* Quoting Art Runyan: "its not safe to expect any particular
4511 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4512 * mailbox." Moreover, the mailbox may return a bogus state,
4513 * so we need to just enable it and continue on.
2a114cc1
BW
4514 */
4515 } else {
4516 I915_WRITE(IPS_CTL, IPS_ENABLE);
4517 /* The bit only becomes 1 in the next vblank, so this wait here
4518 * is essentially intel_wait_for_vblank. If we don't have this
4519 * and don't wait for vblanks until the end of crtc_enable, then
4520 * the HW state readout code will complain that the expected
4521 * IPS_CTL value is not the one we read. */
4522 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4523 DRM_ERROR("Timed out waiting for IPS enable\n");
4524 }
d77e4531
PZ
4525}
4526
20bc8673 4527void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4528{
4529 struct drm_device *dev = crtc->base.dev;
4530 struct drm_i915_private *dev_priv = dev->dev_private;
4531
6e3c9717 4532 if (!crtc->config->ips_enabled)
d77e4531
PZ
4533 return;
4534
4535 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4536 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4537 mutex_lock(&dev_priv->rps.hw_lock);
4538 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4539 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4540 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4541 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4542 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4543 } else {
2a114cc1 4544 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4545 POSTING_READ(IPS_CTL);
4546 }
d77e4531
PZ
4547
4548 /* We need to wait for a vblank before we can disable the plane. */
4549 intel_wait_for_vblank(dev, crtc->pipe);
4550}
4551
4552/** Loads the palette/gamma unit for the CRTC with the prepared values */
4553static void intel_crtc_load_lut(struct drm_crtc *crtc)
4554{
4555 struct drm_device *dev = crtc->dev;
4556 struct drm_i915_private *dev_priv = dev->dev_private;
4557 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4558 enum pipe pipe = intel_crtc->pipe;
4559 int palreg = PALETTE(pipe);
4560 int i;
4561 bool reenable_ips = false;
4562
4563 /* The clocks have to be on to load the palette. */
53d9f4e9 4564 if (!crtc->state->active)
d77e4531
PZ
4565 return;
4566
50360403 4567 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
409ee761 4568 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4569 assert_dsi_pll_enabled(dev_priv);
4570 else
4571 assert_pll_enabled(dev_priv, pipe);
4572 }
4573
4574 /* use legacy palette for Ironlake */
7a1db49a 4575 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4576 palreg = LGC_PALETTE(pipe);
4577
4578 /* Workaround : Do not read or write the pipe palette/gamma data while
4579 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4580 */
6e3c9717 4581 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4582 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4583 GAMMA_MODE_MODE_SPLIT)) {
4584 hsw_disable_ips(intel_crtc);
4585 reenable_ips = true;
4586 }
4587
4588 for (i = 0; i < 256; i++) {
4589 I915_WRITE(palreg + 4 * i,
4590 (intel_crtc->lut_r[i] << 16) |
4591 (intel_crtc->lut_g[i] << 8) |
4592 intel_crtc->lut_b[i]);
4593 }
4594
4595 if (reenable_ips)
4596 hsw_enable_ips(intel_crtc);
4597}
4598
7cac945f 4599static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4600{
7cac945f 4601 if (intel_crtc->overlay) {
d3eedb1a
VS
4602 struct drm_device *dev = intel_crtc->base.dev;
4603 struct drm_i915_private *dev_priv = dev->dev_private;
4604
4605 mutex_lock(&dev->struct_mutex);
4606 dev_priv->mm.interruptible = false;
4607 (void) intel_overlay_switch_off(intel_crtc->overlay);
4608 dev_priv->mm.interruptible = true;
4609 mutex_unlock(&dev->struct_mutex);
4610 }
4611
4612 /* Let userspace switch the overlay on again. In most cases userspace
4613 * has to recompute where to put it anyway.
4614 */
4615}
4616
87d4300a
ML
4617/**
4618 * intel_post_enable_primary - Perform operations after enabling primary plane
4619 * @crtc: the CRTC whose primary plane was just enabled
4620 *
4621 * Performs potentially sleeping operations that must be done after the primary
4622 * plane is enabled, such as updating FBC and IPS. Note that this may be
4623 * called due to an explicit primary plane update, or due to an implicit
4624 * re-enable that is caused when a sprite plane is updated to no longer
4625 * completely hide the primary plane.
4626 */
4627static void
4628intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4629{
4630 struct drm_device *dev = crtc->dev;
87d4300a 4631 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4632 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4633 int pipe = intel_crtc->pipe;
a5c4d7bc 4634
87d4300a
ML
4635 /*
4636 * BDW signals flip done immediately if the plane
4637 * is disabled, even if the plane enable is already
4638 * armed to occur at the next vblank :(
4639 */
4640 if (IS_BROADWELL(dev))
4641 intel_wait_for_vblank(dev, pipe);
a5c4d7bc 4642
87d4300a
ML
4643 /*
4644 * FIXME IPS should be fine as long as one plane is
4645 * enabled, but in practice it seems to have problems
4646 * when going from primary only to sprite only and vice
4647 * versa.
4648 */
a5c4d7bc
VS
4649 hsw_enable_ips(intel_crtc);
4650
f99d7069 4651 /*
87d4300a
ML
4652 * Gen2 reports pipe underruns whenever all planes are disabled.
4653 * So don't enable underrun reporting before at least some planes
4654 * are enabled.
4655 * FIXME: Need to fix the logic to work when we turn off all planes
4656 * but leave the pipe running.
f99d7069 4657 */
87d4300a
ML
4658 if (IS_GEN2(dev))
4659 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4660
4661 /* Underruns don't raise interrupts, so check manually. */
4662 if (HAS_GMCH_DISPLAY(dev))
4663 i9xx_check_fifo_underruns(dev_priv);
a5c4d7bc
VS
4664}
4665
87d4300a
ML
4666/**
4667 * intel_pre_disable_primary - Perform operations before disabling primary plane
4668 * @crtc: the CRTC whose primary plane is to be disabled
4669 *
4670 * Performs potentially sleeping operations that must be done before the
4671 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4672 * be called due to an explicit primary plane update, or due to an implicit
4673 * disable that is caused when a sprite plane completely hides the primary
4674 * plane.
4675 */
4676static void
4677intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4678{
4679 struct drm_device *dev = crtc->dev;
4680 struct drm_i915_private *dev_priv = dev->dev_private;
4681 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4682 int pipe = intel_crtc->pipe;
a5c4d7bc 4683
87d4300a
ML
4684 /*
4685 * Gen2 reports pipe underruns whenever all planes are disabled.
4686 * So diasble underrun reporting before all the planes get disabled.
4687 * FIXME: Need to fix the logic to work when we turn off all planes
4688 * but leave the pipe running.
4689 */
4690 if (IS_GEN2(dev))
4691 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4692
87d4300a
ML
4693 /*
4694 * Vblank time updates from the shadow to live plane control register
4695 * are blocked if the memory self-refresh mode is active at that
4696 * moment. So to make sure the plane gets truly disabled, disable
4697 * first the self-refresh mode. The self-refresh enable bit in turn
4698 * will be checked/applied by the HW only at the next frame start
4699 * event which is after the vblank start event, so we need to have a
4700 * wait-for-vblank between disabling the plane and the pipe.
4701 */
262cd2e1 4702 if (HAS_GMCH_DISPLAY(dev)) {
87d4300a 4703 intel_set_memory_cxsr(dev_priv, false);
262cd2e1
VS
4704 dev_priv->wm.vlv.cxsr = false;
4705 intel_wait_for_vblank(dev, pipe);
4706 }
87d4300a 4707
87d4300a
ML
4708 /*
4709 * FIXME IPS should be fine as long as one plane is
4710 * enabled, but in practice it seems to have problems
4711 * when going from primary only to sprite only and vice
4712 * versa.
4713 */
a5c4d7bc 4714 hsw_disable_ips(intel_crtc);
87d4300a
ML
4715}
4716
ac21b225
ML
4717static void intel_post_plane_update(struct intel_crtc *crtc)
4718{
4719 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4720 struct drm_device *dev = crtc->base.dev;
4721 struct drm_plane *plane;
4722
4723 if (atomic->wait_vblank)
4724 intel_wait_for_vblank(dev, crtc->pipe);
4725
4726 intel_frontbuffer_flip(dev, atomic->fb_bits);
4727
852eb00d
VS
4728 if (atomic->disable_cxsr)
4729 crtc->wm.cxsr_allowed = true;
4730
f015c551
VS
4731 if (crtc->atomic.update_wm_post)
4732 intel_update_watermarks(&crtc->base);
4733
ac21b225
ML
4734 if (atomic->update_fbc) {
4735 mutex_lock(&dev->struct_mutex);
4736 intel_fbc_update(dev);
4737 mutex_unlock(&dev->struct_mutex);
4738 }
4739
4740 if (atomic->post_enable_primary)
4741 intel_post_enable_primary(&crtc->base);
4742
4743 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4744 intel_update_sprite_watermarks(plane, &crtc->base,
4745 0, 0, 0, false, false);
4746
4747 memset(atomic, 0, sizeof(*atomic));
4748}
4749
4750static void intel_pre_plane_update(struct intel_crtc *crtc)
4751{
4752 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4753 struct drm_i915_private *dev_priv = dev->dev_private;
ac21b225
ML
4754 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4755 struct drm_plane *p;
4756
4757 /* Track fb's for any planes being disabled */
ac21b225
ML
4758 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4759 struct intel_plane *plane = to_intel_plane(p);
ac21b225
ML
4760
4761 mutex_lock(&dev->struct_mutex);
a9ff8714
VS
4762 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
4763 plane->frontbuffer_bit);
ac21b225
ML
4764 mutex_unlock(&dev->struct_mutex);
4765 }
4766
4767 if (atomic->wait_for_flips)
4768 intel_crtc_wait_for_pending_flips(&crtc->base);
4769
eddfcbcd
ML
4770 if (atomic->disable_fbc &&
4771 dev_priv->fbc.crtc == crtc) {
4772 mutex_lock(&dev->struct_mutex);
4773 if (dev_priv->fbc.crtc == crtc)
4774 intel_fbc_disable(dev);
4775 mutex_unlock(&dev->struct_mutex);
4776 }
ac21b225 4777
066cf55b
RV
4778 if (crtc->atomic.disable_ips)
4779 hsw_disable_ips(crtc);
4780
ac21b225
ML
4781 if (atomic->pre_disable_primary)
4782 intel_pre_disable_primary(&crtc->base);
852eb00d
VS
4783
4784 if (atomic->disable_cxsr) {
4785 crtc->wm.cxsr_allowed = false;
4786 intel_set_memory_cxsr(dev_priv, false);
4787 }
ac21b225
ML
4788}
4789
d032ffa0 4790static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4791{
4792 struct drm_device *dev = crtc->dev;
4793 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4794 struct drm_plane *p;
87d4300a
ML
4795 int pipe = intel_crtc->pipe;
4796
7cac945f 4797 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4798
d032ffa0
ML
4799 drm_for_each_plane_mask(p, dev, plane_mask)
4800 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4801
f99d7069
DV
4802 /*
4803 * FIXME: Once we grow proper nuclear flip support out of this we need
4804 * to compute the mask of flip planes precisely. For the time being
4805 * consider this a flip to a NULL plane.
4806 */
4807 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4808}
4809
f67a559d
JB
4810static void ironlake_crtc_enable(struct drm_crtc *crtc)
4811{
4812 struct drm_device *dev = crtc->dev;
4813 struct drm_i915_private *dev_priv = dev->dev_private;
4814 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4815 struct intel_encoder *encoder;
f67a559d 4816 int pipe = intel_crtc->pipe;
f67a559d 4817
53d9f4e9 4818 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4819 return;
4820
6e3c9717 4821 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4822 intel_prepare_shared_dpll(intel_crtc);
4823
6e3c9717 4824 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4825 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4826
4827 intel_set_pipe_timings(intel_crtc);
4828
6e3c9717 4829 if (intel_crtc->config->has_pch_encoder) {
29407aab 4830 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4831 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4832 }
4833
4834 ironlake_set_pipeconf(crtc);
4835
f67a559d 4836 intel_crtc->active = true;
8664281b 4837
a72e4c9f
DV
4838 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4839 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4840
f6736a1a 4841 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4842 if (encoder->pre_enable)
4843 encoder->pre_enable(encoder);
f67a559d 4844
6e3c9717 4845 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4846 /* Note: FDI PLL enabling _must_ be done before we enable the
4847 * cpu pipes, hence this is separate from all the other fdi/pch
4848 * enabling. */
88cefb6c 4849 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4850 } else {
4851 assert_fdi_tx_disabled(dev_priv, pipe);
4852 assert_fdi_rx_disabled(dev_priv, pipe);
4853 }
f67a559d 4854
b074cec8 4855 ironlake_pfit_enable(intel_crtc);
f67a559d 4856
9c54c0dd
JB
4857 /*
4858 * On ILK+ LUT must be loaded before the pipe is running but with
4859 * clocks enabled
4860 */
4861 intel_crtc_load_lut(crtc);
4862
f37fcc2a 4863 intel_update_watermarks(crtc);
e1fdc473 4864 intel_enable_pipe(intel_crtc);
f67a559d 4865
6e3c9717 4866 if (intel_crtc->config->has_pch_encoder)
f67a559d 4867 ironlake_pch_enable(crtc);
c98e9dcf 4868
f9b61ff6
DV
4869 assert_vblank_disabled(crtc);
4870 drm_crtc_vblank_on(crtc);
4871
fa5c73b1
DV
4872 for_each_encoder_on_crtc(dev, crtc, encoder)
4873 encoder->enable(encoder);
61b77ddd
DV
4874
4875 if (HAS_PCH_CPT(dev))
a1520318 4876 cpt_verify_modeset(dev, intel_crtc->pipe);
6be4a607
JB
4877}
4878
42db64ef
PZ
4879/* IPS only exists on ULT machines and is tied to pipe A. */
4880static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4881{
f5adf94e 4882 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4883}
4884
4f771f10
PZ
4885static void haswell_crtc_enable(struct drm_crtc *crtc)
4886{
4887 struct drm_device *dev = crtc->dev;
4888 struct drm_i915_private *dev_priv = dev->dev_private;
4889 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4890 struct intel_encoder *encoder;
99d736a2
ML
4891 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4892 struct intel_crtc_state *pipe_config =
4893 to_intel_crtc_state(crtc->state);
4f771f10 4894
53d9f4e9 4895 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4896 return;
4897
df8ad70c
DV
4898 if (intel_crtc_to_shared_dpll(intel_crtc))
4899 intel_enable_shared_dpll(intel_crtc);
4900
6e3c9717 4901 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4902 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
4903
4904 intel_set_pipe_timings(intel_crtc);
4905
6e3c9717
ACO
4906 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4907 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4908 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4909 }
4910
6e3c9717 4911 if (intel_crtc->config->has_pch_encoder) {
229fca97 4912 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4913 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4914 }
4915
4916 haswell_set_pipeconf(crtc);
4917
4918 intel_set_pipe_csc(crtc);
4919
4f771f10 4920 intel_crtc->active = true;
8664281b 4921
a72e4c9f 4922 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
4923 for_each_encoder_on_crtc(dev, crtc, encoder)
4924 if (encoder->pre_enable)
4925 encoder->pre_enable(encoder);
4926
6e3c9717 4927 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
4928 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4929 true);
4fe9467d
ID
4930 dev_priv->display.fdi_link_train(crtc);
4931 }
4932
1f544388 4933 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4934
ff6d9f55 4935 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 4936 skylake_pfit_update(intel_crtc, 1);
ff6d9f55 4937 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 4938 ironlake_pfit_enable(intel_crtc);
ff6d9f55
JB
4939 else
4940 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10
PZ
4941
4942 /*
4943 * On ILK+ LUT must be loaded before the pipe is running but with
4944 * clocks enabled
4945 */
4946 intel_crtc_load_lut(crtc);
4947
1f544388 4948 intel_ddi_set_pipe_settings(crtc);
8228c251 4949 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4950
f37fcc2a 4951 intel_update_watermarks(crtc);
e1fdc473 4952 intel_enable_pipe(intel_crtc);
42db64ef 4953
6e3c9717 4954 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4955 lpt_pch_enable(crtc);
4f771f10 4956
6e3c9717 4957 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4958 intel_ddi_set_vc_payload_alloc(crtc, true);
4959
f9b61ff6
DV
4960 assert_vblank_disabled(crtc);
4961 drm_crtc_vblank_on(crtc);
4962
8807e55b 4963 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4964 encoder->enable(encoder);
8807e55b
JN
4965 intel_opregion_notify_encoder(encoder, true);
4966 }
4f771f10 4967
e4916946
PZ
4968 /* If we change the relative order between pipe/planes enabling, we need
4969 * to change the workaround. */
99d736a2
ML
4970 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4971 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4972 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4973 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4974 }
4f771f10
PZ
4975}
4976
3f8dce3a
DV
4977static void ironlake_pfit_disable(struct intel_crtc *crtc)
4978{
4979 struct drm_device *dev = crtc->base.dev;
4980 struct drm_i915_private *dev_priv = dev->dev_private;
4981 int pipe = crtc->pipe;
4982
4983 /* To avoid upsetting the power well on haswell only disable the pfit if
4984 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 4985 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4986 I915_WRITE(PF_CTL(pipe), 0);
4987 I915_WRITE(PF_WIN_POS(pipe), 0);
4988 I915_WRITE(PF_WIN_SZ(pipe), 0);
4989 }
4990}
4991
6be4a607
JB
4992static void ironlake_crtc_disable(struct drm_crtc *crtc)
4993{
4994 struct drm_device *dev = crtc->dev;
4995 struct drm_i915_private *dev_priv = dev->dev_private;
4996 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4997 struct intel_encoder *encoder;
6be4a607 4998 int pipe = intel_crtc->pipe;
5eddb70b 4999 u32 reg, temp;
b52eb4dc 5000
ea9d758d
DV
5001 for_each_encoder_on_crtc(dev, crtc, encoder)
5002 encoder->disable(encoder);
5003
f9b61ff6
DV
5004 drm_crtc_vblank_off(crtc);
5005 assert_vblank_disabled(crtc);
5006
6e3c9717 5007 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 5008 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 5009
575f7ab7 5010 intel_disable_pipe(intel_crtc);
32f9d658 5011
3f8dce3a 5012 ironlake_pfit_disable(intel_crtc);
2c07245f 5013
5a74f70a
VS
5014 if (intel_crtc->config->has_pch_encoder)
5015 ironlake_fdi_disable(crtc);
5016
bf49ec8c
DV
5017 for_each_encoder_on_crtc(dev, crtc, encoder)
5018 if (encoder->post_disable)
5019 encoder->post_disable(encoder);
2c07245f 5020
6e3c9717 5021 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5022 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5023
d925c59a
DV
5024 if (HAS_PCH_CPT(dev)) {
5025 /* disable TRANS_DP_CTL */
5026 reg = TRANS_DP_CTL(pipe);
5027 temp = I915_READ(reg);
5028 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5029 TRANS_DP_PORT_SEL_MASK);
5030 temp |= TRANS_DP_PORT_SEL_NONE;
5031 I915_WRITE(reg, temp);
5032
5033 /* disable DPLL_SEL */
5034 temp = I915_READ(PCH_DPLL_SEL);
11887397 5035 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5036 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5037 }
e3421a18 5038
d925c59a
DV
5039 ironlake_fdi_pll_disable(intel_crtc);
5040 }
6be4a607 5041}
1b3c7a47 5042
4f771f10 5043static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5044{
4f771f10
PZ
5045 struct drm_device *dev = crtc->dev;
5046 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5047 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5048 struct intel_encoder *encoder;
6e3c9717 5049 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5050
8807e55b
JN
5051 for_each_encoder_on_crtc(dev, crtc, encoder) {
5052 intel_opregion_notify_encoder(encoder, false);
4f771f10 5053 encoder->disable(encoder);
8807e55b 5054 }
4f771f10 5055
f9b61ff6
DV
5056 drm_crtc_vblank_off(crtc);
5057 assert_vblank_disabled(crtc);
5058
6e3c9717 5059 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
5060 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5061 false);
575f7ab7 5062 intel_disable_pipe(intel_crtc);
4f771f10 5063
6e3c9717 5064 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5065 intel_ddi_set_vc_payload_alloc(crtc, false);
5066
ad80a810 5067 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5068
ff6d9f55 5069 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5070 skylake_pfit_update(intel_crtc, 0);
ff6d9f55 5071 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5072 ironlake_pfit_disable(intel_crtc);
ff6d9f55
JB
5073 else
5074 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10 5075
1f544388 5076 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5077
6e3c9717 5078 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 5079 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 5080 intel_ddi_fdi_disable(crtc);
83616634 5081 }
4f771f10 5082
97b040aa
ID
5083 for_each_encoder_on_crtc(dev, crtc, encoder)
5084 if (encoder->post_disable)
5085 encoder->post_disable(encoder);
4f771f10
PZ
5086}
5087
2dd24552
JB
5088static void i9xx_pfit_enable(struct intel_crtc *crtc)
5089{
5090 struct drm_device *dev = crtc->base.dev;
5091 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5092 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5093
681a8504 5094 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5095 return;
5096
2dd24552 5097 /*
c0b03411
DV
5098 * The panel fitter should only be adjusted whilst the pipe is disabled,
5099 * according to register description and PRM.
2dd24552 5100 */
c0b03411
DV
5101 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5102 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5103
b074cec8
JB
5104 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5105 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5106
5107 /* Border color in case we don't scale up to the full screen. Black by
5108 * default, change to something else for debugging. */
5109 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5110}
5111
d05410f9
DA
5112static enum intel_display_power_domain port_to_power_domain(enum port port)
5113{
5114 switch (port) {
5115 case PORT_A:
5116 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5117 case PORT_B:
5118 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5119 case PORT_C:
5120 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5121 case PORT_D:
5122 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5123 default:
5124 WARN_ON_ONCE(1);
5125 return POWER_DOMAIN_PORT_OTHER;
5126 }
5127}
5128
77d22dca
ID
5129#define for_each_power_domain(domain, mask) \
5130 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5131 if ((1 << (domain)) & (mask))
5132
319be8ae
ID
5133enum intel_display_power_domain
5134intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5135{
5136 struct drm_device *dev = intel_encoder->base.dev;
5137 struct intel_digital_port *intel_dig_port;
5138
5139 switch (intel_encoder->type) {
5140 case INTEL_OUTPUT_UNKNOWN:
5141 /* Only DDI platforms should ever use this output type */
5142 WARN_ON_ONCE(!HAS_DDI(dev));
5143 case INTEL_OUTPUT_DISPLAYPORT:
5144 case INTEL_OUTPUT_HDMI:
5145 case INTEL_OUTPUT_EDP:
5146 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5147 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5148 case INTEL_OUTPUT_DP_MST:
5149 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5150 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5151 case INTEL_OUTPUT_ANALOG:
5152 return POWER_DOMAIN_PORT_CRT;
5153 case INTEL_OUTPUT_DSI:
5154 return POWER_DOMAIN_PORT_DSI;
5155 default:
5156 return POWER_DOMAIN_PORT_OTHER;
5157 }
5158}
5159
5160static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5161{
319be8ae
ID
5162 struct drm_device *dev = crtc->dev;
5163 struct intel_encoder *intel_encoder;
5164 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5165 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
5166 unsigned long mask;
5167 enum transcoder transcoder;
5168
5169 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5170
5171 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5172 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
5173 if (intel_crtc->config->pch_pfit.enabled ||
5174 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
5175 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5176
319be8ae
ID
5177 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5178 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5179
77d22dca
ID
5180 return mask;
5181}
5182
679dacd4 5183static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
77d22dca 5184{
679dacd4 5185 struct drm_device *dev = state->dev;
77d22dca
ID
5186 struct drm_i915_private *dev_priv = dev->dev_private;
5187 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5188 struct intel_crtc *crtc;
5189
5190 /*
5191 * First get all needed power domains, then put all unneeded, to avoid
5192 * any unnecessary toggling of the power wells.
5193 */
d3fcc808 5194 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5195 enum intel_display_power_domain domain;
5196
83d65738 5197 if (!crtc->base.state->enable)
77d22dca
ID
5198 continue;
5199
319be8ae 5200 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
5201
5202 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5203 intel_display_power_get(dev_priv, domain);
5204 }
5205
27c329ed
ML
5206 if (dev_priv->display.modeset_commit_cdclk) {
5207 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5208
5209 if (cdclk != dev_priv->cdclk_freq &&
5210 !WARN_ON(!state->allow_modeset))
5211 dev_priv->display.modeset_commit_cdclk(state);
5212 }
50f6e502 5213
d3fcc808 5214 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5215 enum intel_display_power_domain domain;
5216
5217 for_each_power_domain(domain, crtc->enabled_power_domains)
5218 intel_display_power_put(dev_priv, domain);
5219
5220 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5221 }
5222
5223 intel_display_set_init_power(dev_priv, false);
5224}
5225
560a7ae4
DL
5226static void intel_update_max_cdclk(struct drm_device *dev)
5227{
5228 struct drm_i915_private *dev_priv = dev->dev_private;
5229
5230 if (IS_SKYLAKE(dev)) {
5231 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5232
5233 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5234 dev_priv->max_cdclk_freq = 675000;
5235 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5236 dev_priv->max_cdclk_freq = 540000;
5237 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5238 dev_priv->max_cdclk_freq = 450000;
5239 else
5240 dev_priv->max_cdclk_freq = 337500;
5241 } else if (IS_BROADWELL(dev)) {
5242 /*
5243 * FIXME with extra cooling we can allow
5244 * 540 MHz for ULX and 675 Mhz for ULT.
5245 * How can we know if extra cooling is
5246 * available? PCI ID, VTB, something else?
5247 */
5248 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5249 dev_priv->max_cdclk_freq = 450000;
5250 else if (IS_BDW_ULX(dev))
5251 dev_priv->max_cdclk_freq = 450000;
5252 else if (IS_BDW_ULT(dev))
5253 dev_priv->max_cdclk_freq = 540000;
5254 else
5255 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5256 } else if (IS_CHERRYVIEW(dev)) {
5257 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5258 } else if (IS_VALLEYVIEW(dev)) {
5259 dev_priv->max_cdclk_freq = 400000;
5260 } else {
5261 /* otherwise assume cdclk is fixed */
5262 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5263 }
5264
5265 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5266 dev_priv->max_cdclk_freq);
5267}
5268
5269static void intel_update_cdclk(struct drm_device *dev)
5270{
5271 struct drm_i915_private *dev_priv = dev->dev_private;
5272
5273 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5274 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5275 dev_priv->cdclk_freq);
5276
5277 /*
5278 * Program the gmbus_freq based on the cdclk frequency.
5279 * BSpec erroneously claims we should aim for 4MHz, but
5280 * in fact 1MHz is the correct frequency.
5281 */
5282 if (IS_VALLEYVIEW(dev)) {
5283 /*
5284 * Program the gmbus_freq based on the cdclk frequency.
5285 * BSpec erroneously claims we should aim for 4MHz, but
5286 * in fact 1MHz is the correct frequency.
5287 */
5288 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5289 }
5290
5291 if (dev_priv->max_cdclk_freq == 0)
5292 intel_update_max_cdclk(dev);
5293}
5294
70d0c574 5295static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5296{
5297 struct drm_i915_private *dev_priv = dev->dev_private;
5298 uint32_t divider;
5299 uint32_t ratio;
5300 uint32_t current_freq;
5301 int ret;
5302
5303 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5304 switch (frequency) {
5305 case 144000:
5306 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5307 ratio = BXT_DE_PLL_RATIO(60);
5308 break;
5309 case 288000:
5310 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5311 ratio = BXT_DE_PLL_RATIO(60);
5312 break;
5313 case 384000:
5314 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5315 ratio = BXT_DE_PLL_RATIO(60);
5316 break;
5317 case 576000:
5318 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5319 ratio = BXT_DE_PLL_RATIO(60);
5320 break;
5321 case 624000:
5322 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5323 ratio = BXT_DE_PLL_RATIO(65);
5324 break;
5325 case 19200:
5326 /*
5327 * Bypass frequency with DE PLL disabled. Init ratio, divider
5328 * to suppress GCC warning.
5329 */
5330 ratio = 0;
5331 divider = 0;
5332 break;
5333 default:
5334 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5335
5336 return;
5337 }
5338
5339 mutex_lock(&dev_priv->rps.hw_lock);
5340 /* Inform power controller of upcoming frequency change */
5341 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5342 0x80000000);
5343 mutex_unlock(&dev_priv->rps.hw_lock);
5344
5345 if (ret) {
5346 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5347 ret, frequency);
5348 return;
5349 }
5350
5351 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5352 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5353 current_freq = current_freq * 500 + 1000;
5354
5355 /*
5356 * DE PLL has to be disabled when
5357 * - setting to 19.2MHz (bypass, PLL isn't used)
5358 * - before setting to 624MHz (PLL needs toggling)
5359 * - before setting to any frequency from 624MHz (PLL needs toggling)
5360 */
5361 if (frequency == 19200 || frequency == 624000 ||
5362 current_freq == 624000) {
5363 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5364 /* Timeout 200us */
5365 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5366 1))
5367 DRM_ERROR("timout waiting for DE PLL unlock\n");
5368 }
5369
5370 if (frequency != 19200) {
5371 uint32_t val;
5372
5373 val = I915_READ(BXT_DE_PLL_CTL);
5374 val &= ~BXT_DE_PLL_RATIO_MASK;
5375 val |= ratio;
5376 I915_WRITE(BXT_DE_PLL_CTL, val);
5377
5378 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5379 /* Timeout 200us */
5380 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5381 DRM_ERROR("timeout waiting for DE PLL lock\n");
5382
5383 val = I915_READ(CDCLK_CTL);
5384 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5385 val |= divider;
5386 /*
5387 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5388 * enable otherwise.
5389 */
5390 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5391 if (frequency >= 500000)
5392 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5393
5394 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5395 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5396 val |= (frequency - 1000) / 500;
5397 I915_WRITE(CDCLK_CTL, val);
5398 }
5399
5400 mutex_lock(&dev_priv->rps.hw_lock);
5401 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5402 DIV_ROUND_UP(frequency, 25000));
5403 mutex_unlock(&dev_priv->rps.hw_lock);
5404
5405 if (ret) {
5406 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5407 ret, frequency);
5408 return;
5409 }
5410
a47871bd 5411 intel_update_cdclk(dev);
f8437dd1
VK
5412}
5413
5414void broxton_init_cdclk(struct drm_device *dev)
5415{
5416 struct drm_i915_private *dev_priv = dev->dev_private;
5417 uint32_t val;
5418
5419 /*
5420 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5421 * or else the reset will hang because there is no PCH to respond.
5422 * Move the handshake programming to initialization sequence.
5423 * Previously was left up to BIOS.
5424 */
5425 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5426 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5427 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5428
5429 /* Enable PG1 for cdclk */
5430 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5431
5432 /* check if cd clock is enabled */
5433 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5434 DRM_DEBUG_KMS("Display already initialized\n");
5435 return;
5436 }
5437
5438 /*
5439 * FIXME:
5440 * - The initial CDCLK needs to be read from VBT.
5441 * Need to make this change after VBT has changes for BXT.
5442 * - check if setting the max (or any) cdclk freq is really necessary
5443 * here, it belongs to modeset time
5444 */
5445 broxton_set_cdclk(dev, 624000);
5446
5447 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5448 POSTING_READ(DBUF_CTL);
5449
f8437dd1
VK
5450 udelay(10);
5451
5452 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5453 DRM_ERROR("DBuf power enable timeout!\n");
5454}
5455
5456void broxton_uninit_cdclk(struct drm_device *dev)
5457{
5458 struct drm_i915_private *dev_priv = dev->dev_private;
5459
5460 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5461 POSTING_READ(DBUF_CTL);
5462
f8437dd1
VK
5463 udelay(10);
5464
5465 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5466 DRM_ERROR("DBuf power disable timeout!\n");
5467
5468 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5469 broxton_set_cdclk(dev, 19200);
5470
5471 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5472}
5473
5d96d8af
DL
5474static const struct skl_cdclk_entry {
5475 unsigned int freq;
5476 unsigned int vco;
5477} skl_cdclk_frequencies[] = {
5478 { .freq = 308570, .vco = 8640 },
5479 { .freq = 337500, .vco = 8100 },
5480 { .freq = 432000, .vco = 8640 },
5481 { .freq = 450000, .vco = 8100 },
5482 { .freq = 540000, .vco = 8100 },
5483 { .freq = 617140, .vco = 8640 },
5484 { .freq = 675000, .vco = 8100 },
5485};
5486
5487static unsigned int skl_cdclk_decimal(unsigned int freq)
5488{
5489 return (freq - 1000) / 500;
5490}
5491
5492static unsigned int skl_cdclk_get_vco(unsigned int freq)
5493{
5494 unsigned int i;
5495
5496 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5497 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5498
5499 if (e->freq == freq)
5500 return e->vco;
5501 }
5502
5503 return 8100;
5504}
5505
5506static void
5507skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5508{
5509 unsigned int min_freq;
5510 u32 val;
5511
5512 /* select the minimum CDCLK before enabling DPLL 0 */
5513 val = I915_READ(CDCLK_CTL);
5514 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5515 val |= CDCLK_FREQ_337_308;
5516
5517 if (required_vco == 8640)
5518 min_freq = 308570;
5519 else
5520 min_freq = 337500;
5521
5522 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5523
5524 I915_WRITE(CDCLK_CTL, val);
5525 POSTING_READ(CDCLK_CTL);
5526
5527 /*
5528 * We always enable DPLL0 with the lowest link rate possible, but still
5529 * taking into account the VCO required to operate the eDP panel at the
5530 * desired frequency. The usual DP link rates operate with a VCO of
5531 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5532 * The modeset code is responsible for the selection of the exact link
5533 * rate later on, with the constraint of choosing a frequency that
5534 * works with required_vco.
5535 */
5536 val = I915_READ(DPLL_CTRL1);
5537
5538 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5539 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5540 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5541 if (required_vco == 8640)
5542 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5543 SKL_DPLL0);
5544 else
5545 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5546 SKL_DPLL0);
5547
5548 I915_WRITE(DPLL_CTRL1, val);
5549 POSTING_READ(DPLL_CTRL1);
5550
5551 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5552
5553 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5554 DRM_ERROR("DPLL0 not locked\n");
5555}
5556
5557static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5558{
5559 int ret;
5560 u32 val;
5561
5562 /* inform PCU we want to change CDCLK */
5563 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5564 mutex_lock(&dev_priv->rps.hw_lock);
5565 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5566 mutex_unlock(&dev_priv->rps.hw_lock);
5567
5568 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5569}
5570
5571static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5572{
5573 unsigned int i;
5574
5575 for (i = 0; i < 15; i++) {
5576 if (skl_cdclk_pcu_ready(dev_priv))
5577 return true;
5578 udelay(10);
5579 }
5580
5581 return false;
5582}
5583
5584static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5585{
560a7ae4 5586 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5587 u32 freq_select, pcu_ack;
5588
5589 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5590
5591 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5592 DRM_ERROR("failed to inform PCU about cdclk change\n");
5593 return;
5594 }
5595
5596 /* set CDCLK_CTL */
5597 switch(freq) {
5598 case 450000:
5599 case 432000:
5600 freq_select = CDCLK_FREQ_450_432;
5601 pcu_ack = 1;
5602 break;
5603 case 540000:
5604 freq_select = CDCLK_FREQ_540;
5605 pcu_ack = 2;
5606 break;
5607 case 308570:
5608 case 337500:
5609 default:
5610 freq_select = CDCLK_FREQ_337_308;
5611 pcu_ack = 0;
5612 break;
5613 case 617140:
5614 case 675000:
5615 freq_select = CDCLK_FREQ_675_617;
5616 pcu_ack = 3;
5617 break;
5618 }
5619
5620 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5621 POSTING_READ(CDCLK_CTL);
5622
5623 /* inform PCU of the change */
5624 mutex_lock(&dev_priv->rps.hw_lock);
5625 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5626 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5627
5628 intel_update_cdclk(dev);
5d96d8af
DL
5629}
5630
5631void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5632{
5633 /* disable DBUF power */
5634 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5635 POSTING_READ(DBUF_CTL);
5636
5637 udelay(10);
5638
5639 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5640 DRM_ERROR("DBuf power disable timeout\n");
5641
5642 /* disable DPLL0 */
5643 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5644 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5645 DRM_ERROR("Couldn't disable DPLL0\n");
5646
5647 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5648}
5649
5650void skl_init_cdclk(struct drm_i915_private *dev_priv)
5651{
5652 u32 val;
5653 unsigned int required_vco;
5654
5655 /* enable PCH reset handshake */
5656 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5657 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5658
5659 /* enable PG1 and Misc I/O */
5660 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5661
5662 /* DPLL0 already enabed !? */
5663 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5664 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5665 return;
5666 }
5667
5668 /* enable DPLL0 */
5669 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5670 skl_dpll0_enable(dev_priv, required_vco);
5671
5672 /* set CDCLK to the frequency the BIOS chose */
5673 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5674
5675 /* enable DBUF power */
5676 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5677 POSTING_READ(DBUF_CTL);
5678
5679 udelay(10);
5680
5681 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5682 DRM_ERROR("DBuf power enable timeout\n");
5683}
5684
dfcab17e 5685/* returns HPLL frequency in kHz */
f8bf63fd 5686static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 5687{
586f49dc 5688 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 5689
586f49dc 5690 /* Obtain SKU information */
a580516d 5691 mutex_lock(&dev_priv->sb_lock);
586f49dc
JB
5692 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5693 CCK_FUSE_HPLL_FREQ_MASK;
a580516d 5694 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5695
dfcab17e 5696 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
5697}
5698
5699/* Adjust CDclk dividers to allow high res or save power if possible */
5700static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5701{
5702 struct drm_i915_private *dev_priv = dev->dev_private;
5703 u32 val, cmd;
5704
164dfd28
VK
5705 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5706 != dev_priv->cdclk_freq);
d60c4473 5707
dfcab17e 5708 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5709 cmd = 2;
dfcab17e 5710 else if (cdclk == 266667)
30a970c6
JB
5711 cmd = 1;
5712 else
5713 cmd = 0;
5714
5715 mutex_lock(&dev_priv->rps.hw_lock);
5716 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5717 val &= ~DSPFREQGUAR_MASK;
5718 val |= (cmd << DSPFREQGUAR_SHIFT);
5719 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5720 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5721 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5722 50)) {
5723 DRM_ERROR("timed out waiting for CDclk change\n");
5724 }
5725 mutex_unlock(&dev_priv->rps.hw_lock);
5726
54433e91
VS
5727 mutex_lock(&dev_priv->sb_lock);
5728
dfcab17e 5729 if (cdclk == 400000) {
6bcda4f0 5730 u32 divider;
30a970c6 5731
6bcda4f0 5732 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5733
30a970c6
JB
5734 /* adjust cdclk divider */
5735 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 5736 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
5737 val |= divider;
5738 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5739
5740 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5741 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5742 50))
5743 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5744 }
5745
30a970c6
JB
5746 /* adjust self-refresh exit latency value */
5747 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5748 val &= ~0x7f;
5749
5750 /*
5751 * For high bandwidth configs, we set a higher latency in the bunit
5752 * so that the core display fetch happens in time to avoid underruns.
5753 */
dfcab17e 5754 if (cdclk == 400000)
30a970c6
JB
5755 val |= 4500 / 250; /* 4.5 usec */
5756 else
5757 val |= 3000 / 250; /* 3.0 usec */
5758 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5759
a580516d 5760 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5761
b6283055 5762 intel_update_cdclk(dev);
30a970c6
JB
5763}
5764
383c5a6a
VS
5765static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5766{
5767 struct drm_i915_private *dev_priv = dev->dev_private;
5768 u32 val, cmd;
5769
164dfd28
VK
5770 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5771 != dev_priv->cdclk_freq);
383c5a6a
VS
5772
5773 switch (cdclk) {
383c5a6a
VS
5774 case 333333:
5775 case 320000:
383c5a6a 5776 case 266667:
383c5a6a 5777 case 200000:
383c5a6a
VS
5778 break;
5779 default:
5f77eeb0 5780 MISSING_CASE(cdclk);
383c5a6a
VS
5781 return;
5782 }
5783
9d0d3fda
VS
5784 /*
5785 * Specs are full of misinformation, but testing on actual
5786 * hardware has shown that we just need to write the desired
5787 * CCK divider into the Punit register.
5788 */
5789 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5790
383c5a6a
VS
5791 mutex_lock(&dev_priv->rps.hw_lock);
5792 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5793 val &= ~DSPFREQGUAR_MASK_CHV;
5794 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5795 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5796 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5797 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5798 50)) {
5799 DRM_ERROR("timed out waiting for CDclk change\n");
5800 }
5801 mutex_unlock(&dev_priv->rps.hw_lock);
5802
b6283055 5803 intel_update_cdclk(dev);
383c5a6a
VS
5804}
5805
30a970c6
JB
5806static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5807 int max_pixclk)
5808{
6bcda4f0 5809 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5810 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5811
30a970c6
JB
5812 /*
5813 * Really only a few cases to deal with, as only 4 CDclks are supported:
5814 * 200MHz
5815 * 267MHz
29dc7ef3 5816 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5817 * 400MHz (VLV only)
5818 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5819 * of the lower bin and adjust if needed.
e37c67a1
VS
5820 *
5821 * We seem to get an unstable or solid color picture at 200MHz.
5822 * Not sure what's wrong. For now use 200MHz only when all pipes
5823 * are off.
30a970c6 5824 */
6cca3195
VS
5825 if (!IS_CHERRYVIEW(dev_priv) &&
5826 max_pixclk > freq_320*limit/100)
dfcab17e 5827 return 400000;
6cca3195 5828 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5829 return freq_320;
e37c67a1 5830 else if (max_pixclk > 0)
dfcab17e 5831 return 266667;
e37c67a1
VS
5832 else
5833 return 200000;
30a970c6
JB
5834}
5835
f8437dd1
VK
5836static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5837 int max_pixclk)
5838{
5839 /*
5840 * FIXME:
5841 * - remove the guardband, it's not needed on BXT
5842 * - set 19.2MHz bypass frequency if there are no active pipes
5843 */
5844 if (max_pixclk > 576000*9/10)
5845 return 624000;
5846 else if (max_pixclk > 384000*9/10)
5847 return 576000;
5848 else if (max_pixclk > 288000*9/10)
5849 return 384000;
5850 else if (max_pixclk > 144000*9/10)
5851 return 288000;
5852 else
5853 return 144000;
5854}
5855
a821fc46
ACO
5856/* Compute the max pixel clock for new configuration. Uses atomic state if
5857 * that's non-NULL, look at current state otherwise. */
5858static int intel_mode_max_pixclk(struct drm_device *dev,
5859 struct drm_atomic_state *state)
30a970c6 5860{
30a970c6 5861 struct intel_crtc *intel_crtc;
304603f4 5862 struct intel_crtc_state *crtc_state;
30a970c6
JB
5863 int max_pixclk = 0;
5864
d3fcc808 5865 for_each_intel_crtc(dev, intel_crtc) {
27c329ed 5866 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
304603f4
ACO
5867 if (IS_ERR(crtc_state))
5868 return PTR_ERR(crtc_state);
5869
5870 if (!crtc_state->base.enable)
5871 continue;
5872
5873 max_pixclk = max(max_pixclk,
5874 crtc_state->base.adjusted_mode.crtc_clock);
30a970c6
JB
5875 }
5876
5877 return max_pixclk;
5878}
5879
27c329ed 5880static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 5881{
27c329ed
ML
5882 struct drm_device *dev = state->dev;
5883 struct drm_i915_private *dev_priv = dev->dev_private;
5884 int max_pixclk = intel_mode_max_pixclk(dev, state);
30a970c6 5885
304603f4
ACO
5886 if (max_pixclk < 0)
5887 return max_pixclk;
30a970c6 5888
27c329ed
ML
5889 to_intel_atomic_state(state)->cdclk =
5890 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 5891
27c329ed
ML
5892 return 0;
5893}
304603f4 5894
27c329ed
ML
5895static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5896{
5897 struct drm_device *dev = state->dev;
5898 struct drm_i915_private *dev_priv = dev->dev_private;
5899 int max_pixclk = intel_mode_max_pixclk(dev, state);
85a96e7a 5900
27c329ed
ML
5901 if (max_pixclk < 0)
5902 return max_pixclk;
85a96e7a 5903
27c329ed
ML
5904 to_intel_atomic_state(state)->cdclk =
5905 broxton_calc_cdclk(dev_priv, max_pixclk);
85a96e7a 5906
27c329ed 5907 return 0;
30a970c6
JB
5908}
5909
1e69cd74
VS
5910static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5911{
5912 unsigned int credits, default_credits;
5913
5914 if (IS_CHERRYVIEW(dev_priv))
5915 default_credits = PFI_CREDIT(12);
5916 else
5917 default_credits = PFI_CREDIT(8);
5918
164dfd28 5919 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
1e69cd74
VS
5920 /* CHV suggested value is 31 or 63 */
5921 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 5922 credits = PFI_CREDIT_63;
1e69cd74
VS
5923 else
5924 credits = PFI_CREDIT(15);
5925 } else {
5926 credits = default_credits;
5927 }
5928
5929 /*
5930 * WA - write default credits before re-programming
5931 * FIXME: should we also set the resend bit here?
5932 */
5933 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5934 default_credits);
5935
5936 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5937 credits | PFI_CREDIT_RESEND);
5938
5939 /*
5940 * FIXME is this guaranteed to clear
5941 * immediately or should we poll for it?
5942 */
5943 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5944}
5945
27c329ed 5946static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 5947{
a821fc46 5948 struct drm_device *dev = old_state->dev;
27c329ed 5949 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
30a970c6 5950 struct drm_i915_private *dev_priv = dev->dev_private;
30a970c6 5951
27c329ed
ML
5952 /*
5953 * FIXME: We can end up here with all power domains off, yet
5954 * with a CDCLK frequency other than the minimum. To account
5955 * for this take the PIPE-A power domain, which covers the HW
5956 * blocks needed for the following programming. This can be
5957 * removed once it's guaranteed that we get here either with
5958 * the minimum CDCLK set, or the required power domains
5959 * enabled.
5960 */
5961 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 5962
27c329ed
ML
5963 if (IS_CHERRYVIEW(dev))
5964 cherryview_set_cdclk(dev, req_cdclk);
5965 else
5966 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 5967
27c329ed 5968 vlv_program_pfi_credits(dev_priv);
1e69cd74 5969
27c329ed 5970 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
5971}
5972
89b667f8
JB
5973static void valleyview_crtc_enable(struct drm_crtc *crtc)
5974{
5975 struct drm_device *dev = crtc->dev;
a72e4c9f 5976 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
5977 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5978 struct intel_encoder *encoder;
5979 int pipe = intel_crtc->pipe;
23538ef1 5980 bool is_dsi;
89b667f8 5981
53d9f4e9 5982 if (WARN_ON(intel_crtc->active))
89b667f8
JB
5983 return;
5984
409ee761 5985 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 5986
1ae0d137
VS
5987 if (!is_dsi) {
5988 if (IS_CHERRYVIEW(dev))
6e3c9717 5989 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5990 else
6e3c9717 5991 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5992 }
5b18e57c 5993
6e3c9717 5994 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5995 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5996
5997 intel_set_pipe_timings(intel_crtc);
5998
c14b0485
VS
5999 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6000 struct drm_i915_private *dev_priv = dev->dev_private;
6001
6002 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6003 I915_WRITE(CHV_CANVAS(pipe), 0);
6004 }
6005
5b18e57c
DV
6006 i9xx_set_pipeconf(intel_crtc);
6007
89b667f8 6008 intel_crtc->active = true;
89b667f8 6009
a72e4c9f 6010 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6011
89b667f8
JB
6012 for_each_encoder_on_crtc(dev, crtc, encoder)
6013 if (encoder->pre_pll_enable)
6014 encoder->pre_pll_enable(encoder);
6015
9d556c99
CML
6016 if (!is_dsi) {
6017 if (IS_CHERRYVIEW(dev))
6e3c9717 6018 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6019 else
6e3c9717 6020 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6021 }
89b667f8
JB
6022
6023 for_each_encoder_on_crtc(dev, crtc, encoder)
6024 if (encoder->pre_enable)
6025 encoder->pre_enable(encoder);
6026
2dd24552
JB
6027 i9xx_pfit_enable(intel_crtc);
6028
63cbb074
VS
6029 intel_crtc_load_lut(crtc);
6030
e1fdc473 6031 intel_enable_pipe(intel_crtc);
be6a6f8e 6032
4b3a9526
VS
6033 assert_vblank_disabled(crtc);
6034 drm_crtc_vblank_on(crtc);
6035
f9b61ff6
DV
6036 for_each_encoder_on_crtc(dev, crtc, encoder)
6037 encoder->enable(encoder);
89b667f8
JB
6038}
6039
f13c2ef3
DV
6040static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6041{
6042 struct drm_device *dev = crtc->base.dev;
6043 struct drm_i915_private *dev_priv = dev->dev_private;
6044
6e3c9717
ACO
6045 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6046 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6047}
6048
0b8765c6 6049static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6050{
6051 struct drm_device *dev = crtc->dev;
a72e4c9f 6052 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6054 struct intel_encoder *encoder;
79e53945 6055 int pipe = intel_crtc->pipe;
79e53945 6056
53d9f4e9 6057 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6058 return;
6059
f13c2ef3
DV
6060 i9xx_set_pll_dividers(intel_crtc);
6061
6e3c9717 6062 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6063 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6064
6065 intel_set_pipe_timings(intel_crtc);
6066
5b18e57c
DV
6067 i9xx_set_pipeconf(intel_crtc);
6068
f7abfe8b 6069 intel_crtc->active = true;
6b383a7f 6070
4a3436e8 6071 if (!IS_GEN2(dev))
a72e4c9f 6072 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6073
9d6d9f19
MK
6074 for_each_encoder_on_crtc(dev, crtc, encoder)
6075 if (encoder->pre_enable)
6076 encoder->pre_enable(encoder);
6077
f6736a1a
DV
6078 i9xx_enable_pll(intel_crtc);
6079
2dd24552
JB
6080 i9xx_pfit_enable(intel_crtc);
6081
63cbb074
VS
6082 intel_crtc_load_lut(crtc);
6083
f37fcc2a 6084 intel_update_watermarks(crtc);
e1fdc473 6085 intel_enable_pipe(intel_crtc);
be6a6f8e 6086
4b3a9526
VS
6087 assert_vblank_disabled(crtc);
6088 drm_crtc_vblank_on(crtc);
6089
f9b61ff6
DV
6090 for_each_encoder_on_crtc(dev, crtc, encoder)
6091 encoder->enable(encoder);
0b8765c6 6092}
79e53945 6093
87476d63
DV
6094static void i9xx_pfit_disable(struct intel_crtc *crtc)
6095{
6096 struct drm_device *dev = crtc->base.dev;
6097 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6098
6e3c9717 6099 if (!crtc->config->gmch_pfit.control)
328d8e82 6100 return;
87476d63 6101
328d8e82 6102 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6103
328d8e82
DV
6104 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6105 I915_READ(PFIT_CONTROL));
6106 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6107}
6108
0b8765c6
JB
6109static void i9xx_crtc_disable(struct drm_crtc *crtc)
6110{
6111 struct drm_device *dev = crtc->dev;
6112 struct drm_i915_private *dev_priv = dev->dev_private;
6113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6114 struct intel_encoder *encoder;
0b8765c6 6115 int pipe = intel_crtc->pipe;
ef9c3aee 6116
6304cd91
VS
6117 /*
6118 * On gen2 planes are double buffered but the pipe isn't, so we must
6119 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6120 * We also need to wait on all gmch platforms because of the
6121 * self-refresh mode constraint explained above.
6304cd91 6122 */
564ed191 6123 intel_wait_for_vblank(dev, pipe);
6304cd91 6124
4b3a9526
VS
6125 for_each_encoder_on_crtc(dev, crtc, encoder)
6126 encoder->disable(encoder);
6127
f9b61ff6
DV
6128 drm_crtc_vblank_off(crtc);
6129 assert_vblank_disabled(crtc);
6130
575f7ab7 6131 intel_disable_pipe(intel_crtc);
24a1f16d 6132
87476d63 6133 i9xx_pfit_disable(intel_crtc);
24a1f16d 6134
89b667f8
JB
6135 for_each_encoder_on_crtc(dev, crtc, encoder)
6136 if (encoder->post_disable)
6137 encoder->post_disable(encoder);
6138
409ee761 6139 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6140 if (IS_CHERRYVIEW(dev))
6141 chv_disable_pll(dev_priv, pipe);
6142 else if (IS_VALLEYVIEW(dev))
6143 vlv_disable_pll(dev_priv, pipe);
6144 else
1c4e0274 6145 i9xx_disable_pll(intel_crtc);
076ed3b2 6146 }
0b8765c6 6147
4a3436e8 6148 if (!IS_GEN2(dev))
a72e4c9f 6149 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6150}
6151
b17d48e2
ML
6152static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6153{
6154 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6155 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6156 enum intel_display_power_domain domain;
6157 unsigned long domains;
6158
6159 if (!intel_crtc->active)
6160 return;
6161
a539205a
ML
6162 if (to_intel_plane_state(crtc->primary->state)->visible) {
6163 intel_crtc_wait_for_pending_flips(crtc);
6164 intel_pre_disable_primary(crtc);
6165 }
6166
d032ffa0 6167 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
b17d48e2
ML
6168 dev_priv->display.crtc_disable(crtc);
6169
6170 domains = intel_crtc->enabled_power_domains;
6171 for_each_power_domain(domain, domains)
6172 intel_display_power_put(dev_priv, domain);
6173 intel_crtc->enabled_power_domains = 0;
6174}
6175
6b72d486
ML
6176/*
6177 * turn all crtc's off, but do not adjust state
6178 * This has to be paired with a call to intel_modeset_setup_hw_state.
6179 */
9716c691 6180void intel_display_suspend(struct drm_device *dev)
ee7b9f93 6181{
6b72d486
ML
6182 struct drm_crtc *crtc;
6183
b17d48e2
ML
6184 for_each_crtc(dev, crtc)
6185 intel_crtc_disable_noatomic(crtc);
ee7b9f93
JB
6186}
6187
b04c5bd6 6188/* Master function to enable/disable CRTC and corresponding power wells */
5da76e94 6189int intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
6190{
6191 struct drm_device *dev = crtc->dev;
5da76e94
ML
6192 struct drm_mode_config *config = &dev->mode_config;
6193 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
0e572fe7 6194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5da76e94
ML
6195 struct intel_crtc_state *pipe_config;
6196 struct drm_atomic_state *state;
6197 int ret;
976f8a20 6198
1b509259 6199 if (enable == intel_crtc->active)
5da76e94 6200 return 0;
0e572fe7 6201
1b509259 6202 if (enable && !crtc->state->enable)
5da76e94 6203 return 0;
1b509259 6204
5da76e94
ML
6205 /* this function should be called with drm_modeset_lock_all for now */
6206 if (WARN_ON(!ctx))
6207 return -EIO;
6208 lockdep_assert_held(&ctx->ww_ctx);
1b509259 6209
5da76e94
ML
6210 state = drm_atomic_state_alloc(dev);
6211 if (WARN_ON(!state))
6212 return -ENOMEM;
1b509259 6213
5da76e94
ML
6214 state->acquire_ctx = ctx;
6215 state->allow_modeset = true;
6216
6217 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6218 if (IS_ERR(pipe_config)) {
6219 ret = PTR_ERR(pipe_config);
6220 goto err;
0e572fe7 6221 }
5da76e94
ML
6222 pipe_config->base.active = enable;
6223
6224 ret = intel_set_mode(state);
6225 if (!ret)
6226 return ret;
6227
6228err:
6229 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6230 drm_atomic_state_free(state);
6231 return ret;
b04c5bd6
BF
6232}
6233
6234/**
6235 * Sets the power management mode of the pipe and plane.
6236 */
6237void intel_crtc_update_dpms(struct drm_crtc *crtc)
6238{
6239 struct drm_device *dev = crtc->dev;
6240 struct intel_encoder *intel_encoder;
6241 bool enable = false;
6242
6243 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6244 enable |= intel_encoder->connectors_active;
6245
6246 intel_crtc_control(crtc, enable);
cdd59983
CW
6247}
6248
ea5b213a 6249void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6250{
4ef69c7a 6251 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6252
ea5b213a
CW
6253 drm_encoder_cleanup(encoder);
6254 kfree(intel_encoder);
7e7d76c3
JB
6255}
6256
9237329d 6257/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
6258 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6259 * state of the entire output pipe. */
9237329d 6260static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 6261{
5ab432ef
DV
6262 if (mode == DRM_MODE_DPMS_ON) {
6263 encoder->connectors_active = true;
6264
b2cabb0e 6265 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
6266 } else {
6267 encoder->connectors_active = false;
6268
b2cabb0e 6269 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 6270 }
79e53945
JB
6271}
6272
0a91ca29
DV
6273/* Cross check the actual hw state with our own modeset state tracking (and it's
6274 * internal consistency). */
b980514c 6275static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6276{
0a91ca29
DV
6277 if (connector->get_hw_state(connector)) {
6278 struct intel_encoder *encoder = connector->encoder;
6279 struct drm_crtc *crtc;
6280 bool encoder_enabled;
6281 enum pipe pipe;
6282
6283 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6284 connector->base.base.id,
c23cc417 6285 connector->base.name);
0a91ca29 6286
0e32b39c
DA
6287 /* there is no real hw state for MST connectors */
6288 if (connector->mst_port)
6289 return;
6290
e2c719b7 6291 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 6292 "wrong connector dpms state\n");
e2c719b7 6293 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 6294 "active connector not linked to encoder\n");
0a91ca29 6295
36cd7444 6296 if (encoder) {
e2c719b7 6297 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
6298 "encoder->connectors_active not set\n");
6299
6300 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
6301 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6302 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 6303 return;
0a91ca29 6304
36cd7444 6305 crtc = encoder->base.crtc;
0a91ca29 6306
83d65738
MR
6307 I915_STATE_WARN(!crtc->state->enable,
6308 "crtc not enabled\n");
e2c719b7
RC
6309 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6310 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
6311 "encoder active on the wrong pipe\n");
6312 }
0a91ca29 6313 }
79e53945
JB
6314}
6315
08d9bc92
ACO
6316int intel_connector_init(struct intel_connector *connector)
6317{
6318 struct drm_connector_state *connector_state;
6319
6320 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6321 if (!connector_state)
6322 return -ENOMEM;
6323
6324 connector->base.state = connector_state;
6325 return 0;
6326}
6327
6328struct intel_connector *intel_connector_alloc(void)
6329{
6330 struct intel_connector *connector;
6331
6332 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6333 if (!connector)
6334 return NULL;
6335
6336 if (intel_connector_init(connector) < 0) {
6337 kfree(connector);
6338 return NULL;
6339 }
6340
6341 return connector;
6342}
6343
5ab432ef
DV
6344/* Even simpler default implementation, if there's really no special case to
6345 * consider. */
6346void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 6347{
5ab432ef
DV
6348 /* All the simple cases only support two dpms states. */
6349 if (mode != DRM_MODE_DPMS_ON)
6350 mode = DRM_MODE_DPMS_OFF;
d4270e57 6351
5ab432ef
DV
6352 if (mode == connector->dpms)
6353 return;
6354
6355 connector->dpms = mode;
6356
6357 /* Only need to change hw state when actually enabled */
c9976dcf
CW
6358 if (connector->encoder)
6359 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 6360
b980514c 6361 intel_modeset_check_state(connector->dev);
79e53945
JB
6362}
6363
f0947c37
DV
6364/* Simple connector->get_hw_state implementation for encoders that support only
6365 * one connector and no cloning and hence the encoder state determines the state
6366 * of the connector. */
6367bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6368{
24929352 6369 enum pipe pipe = 0;
f0947c37 6370 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6371
f0947c37 6372 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6373}
6374
6d293983 6375static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6376{
6d293983
ACO
6377 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6378 return crtc_state->fdi_lanes;
d272ddfa
VS
6379
6380 return 0;
6381}
6382
6d293983 6383static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6384 struct intel_crtc_state *pipe_config)
1857e1da 6385{
6d293983
ACO
6386 struct drm_atomic_state *state = pipe_config->base.state;
6387 struct intel_crtc *other_crtc;
6388 struct intel_crtc_state *other_crtc_state;
6389
1857e1da
DV
6390 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6391 pipe_name(pipe), pipe_config->fdi_lanes);
6392 if (pipe_config->fdi_lanes > 4) {
6393 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6394 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6395 return -EINVAL;
1857e1da
DV
6396 }
6397
bafb6553 6398 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6399 if (pipe_config->fdi_lanes > 2) {
6400 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6401 pipe_config->fdi_lanes);
6d293983 6402 return -EINVAL;
1857e1da 6403 } else {
6d293983 6404 return 0;
1857e1da
DV
6405 }
6406 }
6407
6408 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6409 return 0;
1857e1da
DV
6410
6411 /* Ivybridge 3 pipe is really complicated */
6412 switch (pipe) {
6413 case PIPE_A:
6d293983 6414 return 0;
1857e1da 6415 case PIPE_B:
6d293983
ACO
6416 if (pipe_config->fdi_lanes <= 2)
6417 return 0;
6418
6419 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6420 other_crtc_state =
6421 intel_atomic_get_crtc_state(state, other_crtc);
6422 if (IS_ERR(other_crtc_state))
6423 return PTR_ERR(other_crtc_state);
6424
6425 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6426 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6427 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6428 return -EINVAL;
1857e1da 6429 }
6d293983 6430 return 0;
1857e1da 6431 case PIPE_C:
251cc67c
VS
6432 if (pipe_config->fdi_lanes > 2) {
6433 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6434 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6435 return -EINVAL;
251cc67c 6436 }
6d293983
ACO
6437
6438 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6439 other_crtc_state =
6440 intel_atomic_get_crtc_state(state, other_crtc);
6441 if (IS_ERR(other_crtc_state))
6442 return PTR_ERR(other_crtc_state);
6443
6444 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6445 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6446 return -EINVAL;
1857e1da 6447 }
6d293983 6448 return 0;
1857e1da
DV
6449 default:
6450 BUG();
6451 }
6452}
6453
e29c22c0
DV
6454#define RETRY 1
6455static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6456 struct intel_crtc_state *pipe_config)
877d48d5 6457{
1857e1da 6458 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 6459 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6460 int lane, link_bw, fdi_dotclock, ret;
6461 bool needs_recompute = false;
877d48d5 6462
e29c22c0 6463retry:
877d48d5
DV
6464 /* FDI is a binary signal running at ~2.7GHz, encoding
6465 * each output octet as 10 bits. The actual frequency
6466 * is stored as a divider into a 100MHz clock, and the
6467 * mode pixel clock is stored in units of 1KHz.
6468 * Hence the bw of each lane in terms of the mode signal
6469 * is:
6470 */
6471 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6472
241bfc38 6473 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6474
2bd89a07 6475 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6476 pipe_config->pipe_bpp);
6477
6478 pipe_config->fdi_lanes = lane;
6479
2bd89a07 6480 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6481 link_bw, &pipe_config->fdi_m_n);
1857e1da 6482
6d293983
ACO
6483 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6484 intel_crtc->pipe, pipe_config);
6485 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6486 pipe_config->pipe_bpp -= 2*3;
6487 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6488 pipe_config->pipe_bpp);
6489 needs_recompute = true;
6490 pipe_config->bw_constrained = true;
6491
6492 goto retry;
6493 }
6494
6495 if (needs_recompute)
6496 return RETRY;
6497
6d293983 6498 return ret;
877d48d5
DV
6499}
6500
8cfb3407
VS
6501static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6502 struct intel_crtc_state *pipe_config)
6503{
6504 if (pipe_config->pipe_bpp > 24)
6505 return false;
6506
6507 /* HSW can handle pixel rate up to cdclk? */
6508 if (IS_HASWELL(dev_priv->dev))
6509 return true;
6510
6511 /*
b432e5cf
VS
6512 * We compare against max which means we must take
6513 * the increased cdclk requirement into account when
6514 * calculating the new cdclk.
6515 *
6516 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6517 */
6518 return ilk_pipe_pixel_rate(pipe_config) <=
6519 dev_priv->max_cdclk_freq * 95 / 100;
6520}
6521
42db64ef 6522static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6523 struct intel_crtc_state *pipe_config)
42db64ef 6524{
8cfb3407
VS
6525 struct drm_device *dev = crtc->base.dev;
6526 struct drm_i915_private *dev_priv = dev->dev_private;
6527
d330a953 6528 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6529 hsw_crtc_supports_ips(crtc) &&
6530 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6531}
6532
a43f6e0f 6533static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6534 struct intel_crtc_state *pipe_config)
79e53945 6535{
a43f6e0f 6536 struct drm_device *dev = crtc->base.dev;
8bd31e67 6537 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 6538 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 6539
ad3a4479 6540 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6541 if (INTEL_INFO(dev)->gen < 4) {
44913155 6542 int clock_limit = dev_priv->max_cdclk_freq;
cf532bb2
VS
6543
6544 /*
6545 * Enable pixel doubling when the dot clock
6546 * is > 90% of the (display) core speed.
6547 *
b397c96b
VS
6548 * GDG double wide on either pipe,
6549 * otherwise pipe A only.
cf532bb2 6550 */
b397c96b 6551 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 6552 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 6553 clock_limit *= 2;
cf532bb2 6554 pipe_config->double_wide = true;
ad3a4479
VS
6555 }
6556
241bfc38 6557 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 6558 return -EINVAL;
2c07245f 6559 }
89749350 6560
1d1d0e27
VS
6561 /*
6562 * Pipe horizontal size must be even in:
6563 * - DVO ganged mode
6564 * - LVDS dual channel mode
6565 * - Double wide pipe
6566 */
a93e255f 6567 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6568 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6569 pipe_config->pipe_src_w &= ~1;
6570
8693a824
DL
6571 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6572 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6573 */
6574 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6575 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 6576 return -EINVAL;
44f46b42 6577
f5adf94e 6578 if (HAS_IPS(dev))
a43f6e0f
DV
6579 hsw_compute_ips_config(crtc, pipe_config);
6580
877d48d5 6581 if (pipe_config->has_pch_encoder)
a43f6e0f 6582 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6583
cf5a15be 6584 return 0;
79e53945
JB
6585}
6586
1652d19e
VS
6587static int skylake_get_display_clock_speed(struct drm_device *dev)
6588{
6589 struct drm_i915_private *dev_priv = to_i915(dev);
6590 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6591 uint32_t cdctl = I915_READ(CDCLK_CTL);
6592 uint32_t linkrate;
6593
414355a7 6594 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6595 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6596
6597 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6598 return 540000;
6599
6600 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6601 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6602
71cd8423
DL
6603 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6604 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6605 /* vco 8640 */
6606 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6607 case CDCLK_FREQ_450_432:
6608 return 432000;
6609 case CDCLK_FREQ_337_308:
6610 return 308570;
6611 case CDCLK_FREQ_675_617:
6612 return 617140;
6613 default:
6614 WARN(1, "Unknown cd freq selection\n");
6615 }
6616 } else {
6617 /* vco 8100 */
6618 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6619 case CDCLK_FREQ_450_432:
6620 return 450000;
6621 case CDCLK_FREQ_337_308:
6622 return 337500;
6623 case CDCLK_FREQ_675_617:
6624 return 675000;
6625 default:
6626 WARN(1, "Unknown cd freq selection\n");
6627 }
6628 }
6629
6630 /* error case, do as if DPLL0 isn't enabled */
6631 return 24000;
6632}
6633
acd3f3d3
BP
6634static int broxton_get_display_clock_speed(struct drm_device *dev)
6635{
6636 struct drm_i915_private *dev_priv = to_i915(dev);
6637 uint32_t cdctl = I915_READ(CDCLK_CTL);
6638 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6639 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6640 int cdclk;
6641
6642 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6643 return 19200;
6644
6645 cdclk = 19200 * pll_ratio / 2;
6646
6647 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6648 case BXT_CDCLK_CD2X_DIV_SEL_1:
6649 return cdclk; /* 576MHz or 624MHz */
6650 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6651 return cdclk * 2 / 3; /* 384MHz */
6652 case BXT_CDCLK_CD2X_DIV_SEL_2:
6653 return cdclk / 2; /* 288MHz */
6654 case BXT_CDCLK_CD2X_DIV_SEL_4:
6655 return cdclk / 4; /* 144MHz */
6656 }
6657
6658 /* error case, do as if DE PLL isn't enabled */
6659 return 19200;
6660}
6661
1652d19e
VS
6662static int broadwell_get_display_clock_speed(struct drm_device *dev)
6663{
6664 struct drm_i915_private *dev_priv = dev->dev_private;
6665 uint32_t lcpll = I915_READ(LCPLL_CTL);
6666 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6667
6668 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6669 return 800000;
6670 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6671 return 450000;
6672 else if (freq == LCPLL_CLK_FREQ_450)
6673 return 450000;
6674 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6675 return 540000;
6676 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6677 return 337500;
6678 else
6679 return 675000;
6680}
6681
6682static int haswell_get_display_clock_speed(struct drm_device *dev)
6683{
6684 struct drm_i915_private *dev_priv = dev->dev_private;
6685 uint32_t lcpll = I915_READ(LCPLL_CTL);
6686 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6687
6688 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6689 return 800000;
6690 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6691 return 450000;
6692 else if (freq == LCPLL_CLK_FREQ_450)
6693 return 450000;
6694 else if (IS_HSW_ULT(dev))
6695 return 337500;
6696 else
6697 return 540000;
79e53945
JB
6698}
6699
25eb05fc
JB
6700static int valleyview_get_display_clock_speed(struct drm_device *dev)
6701{
d197b7d3 6702 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
6703 u32 val;
6704 int divider;
6705
6bcda4f0
VS
6706 if (dev_priv->hpll_freq == 0)
6707 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6708
a580516d 6709 mutex_lock(&dev_priv->sb_lock);
d197b7d3 6710 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
a580516d 6711 mutex_unlock(&dev_priv->sb_lock);
d197b7d3
VS
6712
6713 divider = val & DISPLAY_FREQUENCY_VALUES;
6714
7d007f40
VS
6715 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6716 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6717 "cdclk change in progress\n");
6718
6bcda4f0 6719 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
6720}
6721
b37a6434
VS
6722static int ilk_get_display_clock_speed(struct drm_device *dev)
6723{
6724 return 450000;
6725}
6726
e70236a8
JB
6727static int i945_get_display_clock_speed(struct drm_device *dev)
6728{
6729 return 400000;
6730}
79e53945 6731
e70236a8 6732static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6733{
e907f170 6734 return 333333;
e70236a8 6735}
79e53945 6736
e70236a8
JB
6737static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6738{
6739 return 200000;
6740}
79e53945 6741
257a7ffc
DV
6742static int pnv_get_display_clock_speed(struct drm_device *dev)
6743{
6744 u16 gcfgc = 0;
6745
6746 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6747
6748 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6749 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6750 return 266667;
257a7ffc 6751 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6752 return 333333;
257a7ffc 6753 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6754 return 444444;
257a7ffc
DV
6755 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6756 return 200000;
6757 default:
6758 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6759 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6760 return 133333;
257a7ffc 6761 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6762 return 166667;
257a7ffc
DV
6763 }
6764}
6765
e70236a8
JB
6766static int i915gm_get_display_clock_speed(struct drm_device *dev)
6767{
6768 u16 gcfgc = 0;
79e53945 6769
e70236a8
JB
6770 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6771
6772 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6773 return 133333;
e70236a8
JB
6774 else {
6775 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6776 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6777 return 333333;
e70236a8
JB
6778 default:
6779 case GC_DISPLAY_CLOCK_190_200_MHZ:
6780 return 190000;
79e53945 6781 }
e70236a8
JB
6782 }
6783}
6784
6785static int i865_get_display_clock_speed(struct drm_device *dev)
6786{
e907f170 6787 return 266667;
e70236a8
JB
6788}
6789
1b1d2716 6790static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6791{
6792 u16 hpllcc = 0;
1b1d2716 6793
65cd2b3f
VS
6794 /*
6795 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6796 * encoding is different :(
6797 * FIXME is this the right way to detect 852GM/852GMV?
6798 */
6799 if (dev->pdev->revision == 0x1)
6800 return 133333;
6801
1b1d2716
VS
6802 pci_bus_read_config_word(dev->pdev->bus,
6803 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6804
e70236a8
JB
6805 /* Assume that the hardware is in the high speed state. This
6806 * should be the default.
6807 */
6808 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6809 case GC_CLOCK_133_200:
1b1d2716 6810 case GC_CLOCK_133_200_2:
e70236a8
JB
6811 case GC_CLOCK_100_200:
6812 return 200000;
6813 case GC_CLOCK_166_250:
6814 return 250000;
6815 case GC_CLOCK_100_133:
e907f170 6816 return 133333;
1b1d2716
VS
6817 case GC_CLOCK_133_266:
6818 case GC_CLOCK_133_266_2:
6819 case GC_CLOCK_166_266:
6820 return 266667;
e70236a8 6821 }
79e53945 6822
e70236a8
JB
6823 /* Shouldn't happen */
6824 return 0;
6825}
79e53945 6826
e70236a8
JB
6827static int i830_get_display_clock_speed(struct drm_device *dev)
6828{
e907f170 6829 return 133333;
79e53945
JB
6830}
6831
34edce2f
VS
6832static unsigned int intel_hpll_vco(struct drm_device *dev)
6833{
6834 struct drm_i915_private *dev_priv = dev->dev_private;
6835 static const unsigned int blb_vco[8] = {
6836 [0] = 3200000,
6837 [1] = 4000000,
6838 [2] = 5333333,
6839 [3] = 4800000,
6840 [4] = 6400000,
6841 };
6842 static const unsigned int pnv_vco[8] = {
6843 [0] = 3200000,
6844 [1] = 4000000,
6845 [2] = 5333333,
6846 [3] = 4800000,
6847 [4] = 2666667,
6848 };
6849 static const unsigned int cl_vco[8] = {
6850 [0] = 3200000,
6851 [1] = 4000000,
6852 [2] = 5333333,
6853 [3] = 6400000,
6854 [4] = 3333333,
6855 [5] = 3566667,
6856 [6] = 4266667,
6857 };
6858 static const unsigned int elk_vco[8] = {
6859 [0] = 3200000,
6860 [1] = 4000000,
6861 [2] = 5333333,
6862 [3] = 4800000,
6863 };
6864 static const unsigned int ctg_vco[8] = {
6865 [0] = 3200000,
6866 [1] = 4000000,
6867 [2] = 5333333,
6868 [3] = 6400000,
6869 [4] = 2666667,
6870 [5] = 4266667,
6871 };
6872 const unsigned int *vco_table;
6873 unsigned int vco;
6874 uint8_t tmp = 0;
6875
6876 /* FIXME other chipsets? */
6877 if (IS_GM45(dev))
6878 vco_table = ctg_vco;
6879 else if (IS_G4X(dev))
6880 vco_table = elk_vco;
6881 else if (IS_CRESTLINE(dev))
6882 vco_table = cl_vco;
6883 else if (IS_PINEVIEW(dev))
6884 vco_table = pnv_vco;
6885 else if (IS_G33(dev))
6886 vco_table = blb_vco;
6887 else
6888 return 0;
6889
6890 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6891
6892 vco = vco_table[tmp & 0x7];
6893 if (vco == 0)
6894 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6895 else
6896 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6897
6898 return vco;
6899}
6900
6901static int gm45_get_display_clock_speed(struct drm_device *dev)
6902{
6903 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6904 uint16_t tmp = 0;
6905
6906 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6907
6908 cdclk_sel = (tmp >> 12) & 0x1;
6909
6910 switch (vco) {
6911 case 2666667:
6912 case 4000000:
6913 case 5333333:
6914 return cdclk_sel ? 333333 : 222222;
6915 case 3200000:
6916 return cdclk_sel ? 320000 : 228571;
6917 default:
6918 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6919 return 222222;
6920 }
6921}
6922
6923static int i965gm_get_display_clock_speed(struct drm_device *dev)
6924{
6925 static const uint8_t div_3200[] = { 16, 10, 8 };
6926 static const uint8_t div_4000[] = { 20, 12, 10 };
6927 static const uint8_t div_5333[] = { 24, 16, 14 };
6928 const uint8_t *div_table;
6929 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6930 uint16_t tmp = 0;
6931
6932 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6933
6934 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6935
6936 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6937 goto fail;
6938
6939 switch (vco) {
6940 case 3200000:
6941 div_table = div_3200;
6942 break;
6943 case 4000000:
6944 div_table = div_4000;
6945 break;
6946 case 5333333:
6947 div_table = div_5333;
6948 break;
6949 default:
6950 goto fail;
6951 }
6952
6953 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6954
caf4e252 6955fail:
34edce2f
VS
6956 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6957 return 200000;
6958}
6959
6960static int g33_get_display_clock_speed(struct drm_device *dev)
6961{
6962 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6963 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6964 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6965 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6966 const uint8_t *div_table;
6967 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6968 uint16_t tmp = 0;
6969
6970 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6971
6972 cdclk_sel = (tmp >> 4) & 0x7;
6973
6974 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6975 goto fail;
6976
6977 switch (vco) {
6978 case 3200000:
6979 div_table = div_3200;
6980 break;
6981 case 4000000:
6982 div_table = div_4000;
6983 break;
6984 case 4800000:
6985 div_table = div_4800;
6986 break;
6987 case 5333333:
6988 div_table = div_5333;
6989 break;
6990 default:
6991 goto fail;
6992 }
6993
6994 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6995
caf4e252 6996fail:
34edce2f
VS
6997 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6998 return 190476;
6999}
7000
2c07245f 7001static void
a65851af 7002intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7003{
a65851af
VS
7004 while (*num > DATA_LINK_M_N_MASK ||
7005 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7006 *num >>= 1;
7007 *den >>= 1;
7008 }
7009}
7010
a65851af
VS
7011static void compute_m_n(unsigned int m, unsigned int n,
7012 uint32_t *ret_m, uint32_t *ret_n)
7013{
7014 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7015 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7016 intel_reduce_m_n_ratio(ret_m, ret_n);
7017}
7018
e69d0bc1
DV
7019void
7020intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7021 int pixel_clock, int link_clock,
7022 struct intel_link_m_n *m_n)
2c07245f 7023{
e69d0bc1 7024 m_n->tu = 64;
a65851af
VS
7025
7026 compute_m_n(bits_per_pixel * pixel_clock,
7027 link_clock * nlanes * 8,
7028 &m_n->gmch_m, &m_n->gmch_n);
7029
7030 compute_m_n(pixel_clock, link_clock,
7031 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7032}
7033
a7615030
CW
7034static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7035{
d330a953
JN
7036 if (i915.panel_use_ssc >= 0)
7037 return i915.panel_use_ssc != 0;
41aa3448 7038 return dev_priv->vbt.lvds_use_ssc
435793df 7039 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7040}
7041
a93e255f
ACO
7042static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7043 int num_connectors)
c65d77d8 7044{
a93e255f 7045 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
7046 struct drm_i915_private *dev_priv = dev->dev_private;
7047 int refclk;
7048
a93e255f
ACO
7049 WARN_ON(!crtc_state->base.state);
7050
5ab7b0b7 7051 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 7052 refclk = 100000;
a93e255f 7053 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 7054 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
7055 refclk = dev_priv->vbt.lvds_ssc_freq;
7056 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
7057 } else if (!IS_GEN2(dev)) {
7058 refclk = 96000;
7059 } else {
7060 refclk = 48000;
7061 }
7062
7063 return refclk;
7064}
7065
7429e9d4 7066static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7067{
7df00d7a 7068 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7069}
f47709a9 7070
7429e9d4
DV
7071static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7072{
7073 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7074}
7075
f47709a9 7076static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7077 struct intel_crtc_state *crtc_state,
a7516a05
JB
7078 intel_clock_t *reduced_clock)
7079{
f47709a9 7080 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7081 u32 fp, fp2 = 0;
7082
7083 if (IS_PINEVIEW(dev)) {
190f68c5 7084 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7085 if (reduced_clock)
7429e9d4 7086 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7087 } else {
190f68c5 7088 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7089 if (reduced_clock)
7429e9d4 7090 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7091 }
7092
190f68c5 7093 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7094
f47709a9 7095 crtc->lowfreq_avail = false;
a93e255f 7096 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7097 reduced_clock) {
190f68c5 7098 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7099 crtc->lowfreq_avail = true;
a7516a05 7100 } else {
190f68c5 7101 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7102 }
7103}
7104
5e69f97f
CML
7105static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7106 pipe)
89b667f8
JB
7107{
7108 u32 reg_val;
7109
7110 /*
7111 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7112 * and set it to a reasonable value instead.
7113 */
ab3c759a 7114 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7115 reg_val &= 0xffffff00;
7116 reg_val |= 0x00000030;
ab3c759a 7117 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7118
ab3c759a 7119 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7120 reg_val &= 0x8cffffff;
7121 reg_val = 0x8c000000;
ab3c759a 7122 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7123
ab3c759a 7124 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7125 reg_val &= 0xffffff00;
ab3c759a 7126 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7127
ab3c759a 7128 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7129 reg_val &= 0x00ffffff;
7130 reg_val |= 0xb0000000;
ab3c759a 7131 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7132}
7133
b551842d
DV
7134static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7135 struct intel_link_m_n *m_n)
7136{
7137 struct drm_device *dev = crtc->base.dev;
7138 struct drm_i915_private *dev_priv = dev->dev_private;
7139 int pipe = crtc->pipe;
7140
e3b95f1e
DV
7141 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7142 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7143 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7144 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7145}
7146
7147static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7148 struct intel_link_m_n *m_n,
7149 struct intel_link_m_n *m2_n2)
b551842d
DV
7150{
7151 struct drm_device *dev = crtc->base.dev;
7152 struct drm_i915_private *dev_priv = dev->dev_private;
7153 int pipe = crtc->pipe;
6e3c9717 7154 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7155
7156 if (INTEL_INFO(dev)->gen >= 5) {
7157 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7158 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7159 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7160 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7161 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7162 * for gen < 8) and if DRRS is supported (to make sure the
7163 * registers are not unnecessarily accessed).
7164 */
44395bfe 7165 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7166 crtc->config->has_drrs) {
f769cd24
VK
7167 I915_WRITE(PIPE_DATA_M2(transcoder),
7168 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7169 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7170 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7171 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7172 }
b551842d 7173 } else {
e3b95f1e
DV
7174 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7175 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7176 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7177 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7178 }
7179}
7180
fe3cd48d 7181void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7182{
fe3cd48d
R
7183 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7184
7185 if (m_n == M1_N1) {
7186 dp_m_n = &crtc->config->dp_m_n;
7187 dp_m2_n2 = &crtc->config->dp_m2_n2;
7188 } else if (m_n == M2_N2) {
7189
7190 /*
7191 * M2_N2 registers are not supported. Hence m2_n2 divider value
7192 * needs to be programmed into M1_N1.
7193 */
7194 dp_m_n = &crtc->config->dp_m2_n2;
7195 } else {
7196 DRM_ERROR("Unsupported divider value\n");
7197 return;
7198 }
7199
6e3c9717
ACO
7200 if (crtc->config->has_pch_encoder)
7201 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7202 else
fe3cd48d 7203 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7204}
7205
251ac862
DV
7206static void vlv_compute_dpll(struct intel_crtc *crtc,
7207 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7208{
7209 u32 dpll, dpll_md;
7210
7211 /*
7212 * Enable DPIO clock input. We should never disable the reference
7213 * clock for pipe B, since VGA hotplug / manual detection depends
7214 * on it.
7215 */
7216 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7217 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7218 /* We should never disable this, set it here for state tracking */
7219 if (crtc->pipe == PIPE_B)
7220 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7221 dpll |= DPLL_VCO_ENABLE;
d288f65f 7222 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7223
d288f65f 7224 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7225 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7226 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7227}
7228
d288f65f 7229static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7230 const struct intel_crtc_state *pipe_config)
a0c4da24 7231{
f47709a9 7232 struct drm_device *dev = crtc->base.dev;
a0c4da24 7233 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7234 int pipe = crtc->pipe;
bdd4b6a6 7235 u32 mdiv;
a0c4da24 7236 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7237 u32 coreclk, reg_val;
a0c4da24 7238
a580516d 7239 mutex_lock(&dev_priv->sb_lock);
09153000 7240
d288f65f
VS
7241 bestn = pipe_config->dpll.n;
7242 bestm1 = pipe_config->dpll.m1;
7243 bestm2 = pipe_config->dpll.m2;
7244 bestp1 = pipe_config->dpll.p1;
7245 bestp2 = pipe_config->dpll.p2;
a0c4da24 7246
89b667f8
JB
7247 /* See eDP HDMI DPIO driver vbios notes doc */
7248
7249 /* PLL B needs special handling */
bdd4b6a6 7250 if (pipe == PIPE_B)
5e69f97f 7251 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7252
7253 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7254 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7255
7256 /* Disable target IRef on PLL */
ab3c759a 7257 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7258 reg_val &= 0x00ffffff;
ab3c759a 7259 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7260
7261 /* Disable fast lock */
ab3c759a 7262 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7263
7264 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7265 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7266 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7267 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7268 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7269
7270 /*
7271 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7272 * but we don't support that).
7273 * Note: don't use the DAC post divider as it seems unstable.
7274 */
7275 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7276 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7277
a0c4da24 7278 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7279 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7280
89b667f8 7281 /* Set HBR and RBR LPF coefficients */
d288f65f 7282 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7283 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7284 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7285 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7286 0x009f0003);
89b667f8 7287 else
ab3c759a 7288 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7289 0x00d0000f);
7290
681a8504 7291 if (pipe_config->has_dp_encoder) {
89b667f8 7292 /* Use SSC source */
bdd4b6a6 7293 if (pipe == PIPE_A)
ab3c759a 7294 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7295 0x0df40000);
7296 else
ab3c759a 7297 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7298 0x0df70000);
7299 } else { /* HDMI or VGA */
7300 /* Use bend source */
bdd4b6a6 7301 if (pipe == PIPE_A)
ab3c759a 7302 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7303 0x0df70000);
7304 else
ab3c759a 7305 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7306 0x0df40000);
7307 }
a0c4da24 7308
ab3c759a 7309 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7310 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7311 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7312 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7313 coreclk |= 0x01000000;
ab3c759a 7314 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7315
ab3c759a 7316 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7317 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7318}
7319
251ac862
DV
7320static void chv_compute_dpll(struct intel_crtc *crtc,
7321 struct intel_crtc_state *pipe_config)
1ae0d137 7322{
d288f65f 7323 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
7324 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7325 DPLL_VCO_ENABLE;
7326 if (crtc->pipe != PIPE_A)
d288f65f 7327 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7328
d288f65f
VS
7329 pipe_config->dpll_hw_state.dpll_md =
7330 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7331}
7332
d288f65f 7333static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7334 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7335{
7336 struct drm_device *dev = crtc->base.dev;
7337 struct drm_i915_private *dev_priv = dev->dev_private;
7338 int pipe = crtc->pipe;
7339 int dpll_reg = DPLL(crtc->pipe);
7340 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7341 u32 loopfilter, tribuf_calcntr;
9d556c99 7342 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7343 u32 dpio_val;
9cbe40c1 7344 int vco;
9d556c99 7345
d288f65f
VS
7346 bestn = pipe_config->dpll.n;
7347 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7348 bestm1 = pipe_config->dpll.m1;
7349 bestm2 = pipe_config->dpll.m2 >> 22;
7350 bestp1 = pipe_config->dpll.p1;
7351 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7352 vco = pipe_config->dpll.vco;
a945ce7e 7353 dpio_val = 0;
9cbe40c1 7354 loopfilter = 0;
9d556c99
CML
7355
7356 /*
7357 * Enable Refclk and SSC
7358 */
a11b0703 7359 I915_WRITE(dpll_reg,
d288f65f 7360 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7361
a580516d 7362 mutex_lock(&dev_priv->sb_lock);
9d556c99 7363
9d556c99
CML
7364 /* p1 and p2 divider */
7365 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7366 5 << DPIO_CHV_S1_DIV_SHIFT |
7367 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7368 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7369 1 << DPIO_CHV_K_DIV_SHIFT);
7370
7371 /* Feedback post-divider - m2 */
7372 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7373
7374 /* Feedback refclk divider - n and m1 */
7375 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7376 DPIO_CHV_M1_DIV_BY_2 |
7377 1 << DPIO_CHV_N_DIV_SHIFT);
7378
7379 /* M2 fraction division */
a945ce7e
VP
7380 if (bestm2_frac)
7381 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7382
7383 /* M2 fraction division enable */
a945ce7e
VP
7384 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7385 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7386 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7387 if (bestm2_frac)
7388 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7389 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7390
de3a0fde
VP
7391 /* Program digital lock detect threshold */
7392 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7393 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7394 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7395 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7396 if (!bestm2_frac)
7397 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7398 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7399
9d556c99 7400 /* Loop filter */
9cbe40c1
VP
7401 if (vco == 5400000) {
7402 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7403 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7404 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7405 tribuf_calcntr = 0x9;
7406 } else if (vco <= 6200000) {
7407 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7408 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7409 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7410 tribuf_calcntr = 0x9;
7411 } else if (vco <= 6480000) {
7412 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7413 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7414 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7415 tribuf_calcntr = 0x8;
7416 } else {
7417 /* Not supported. Apply the same limits as in the max case */
7418 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7419 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7420 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7421 tribuf_calcntr = 0;
7422 }
9d556c99
CML
7423 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7424
968040b2 7425 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7426 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7427 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7428 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7429
9d556c99
CML
7430 /* AFC Recal */
7431 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7432 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7433 DPIO_AFC_RECAL);
7434
a580516d 7435 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7436}
7437
d288f65f
VS
7438/**
7439 * vlv_force_pll_on - forcibly enable just the PLL
7440 * @dev_priv: i915 private structure
7441 * @pipe: pipe PLL to enable
7442 * @dpll: PLL configuration
7443 *
7444 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7445 * in cases where we need the PLL enabled even when @pipe is not going to
7446 * be enabled.
7447 */
7448void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7449 const struct dpll *dpll)
7450{
7451 struct intel_crtc *crtc =
7452 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 7453 struct intel_crtc_state pipe_config = {
a93e255f 7454 .base.crtc = &crtc->base,
d288f65f
VS
7455 .pixel_multiplier = 1,
7456 .dpll = *dpll,
7457 };
7458
7459 if (IS_CHERRYVIEW(dev)) {
251ac862 7460 chv_compute_dpll(crtc, &pipe_config);
d288f65f
VS
7461 chv_prepare_pll(crtc, &pipe_config);
7462 chv_enable_pll(crtc, &pipe_config);
7463 } else {
251ac862 7464 vlv_compute_dpll(crtc, &pipe_config);
d288f65f
VS
7465 vlv_prepare_pll(crtc, &pipe_config);
7466 vlv_enable_pll(crtc, &pipe_config);
7467 }
7468}
7469
7470/**
7471 * vlv_force_pll_off - forcibly disable just the PLL
7472 * @dev_priv: i915 private structure
7473 * @pipe: pipe PLL to disable
7474 *
7475 * Disable the PLL for @pipe. To be used in cases where we need
7476 * the PLL enabled even when @pipe is not going to be enabled.
7477 */
7478void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7479{
7480 if (IS_CHERRYVIEW(dev))
7481 chv_disable_pll(to_i915(dev), pipe);
7482 else
7483 vlv_disable_pll(to_i915(dev), pipe);
7484}
7485
251ac862
DV
7486static void i9xx_compute_dpll(struct intel_crtc *crtc,
7487 struct intel_crtc_state *crtc_state,
7488 intel_clock_t *reduced_clock,
7489 int num_connectors)
eb1cbe48 7490{
f47709a9 7491 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7492 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7493 u32 dpll;
7494 bool is_sdvo;
190f68c5 7495 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7496
190f68c5 7497 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7498
a93e255f
ACO
7499 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7500 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7501
7502 dpll = DPLL_VGA_MODE_DIS;
7503
a93e255f 7504 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7505 dpll |= DPLLB_MODE_LVDS;
7506 else
7507 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7508
ef1b460d 7509 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7510 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7511 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7512 }
198a037f
DV
7513
7514 if (is_sdvo)
4a33e48d 7515 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7516
190f68c5 7517 if (crtc_state->has_dp_encoder)
4a33e48d 7518 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7519
7520 /* compute bitmask from p1 value */
7521 if (IS_PINEVIEW(dev))
7522 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7523 else {
7524 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7525 if (IS_G4X(dev) && reduced_clock)
7526 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7527 }
7528 switch (clock->p2) {
7529 case 5:
7530 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7531 break;
7532 case 7:
7533 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7534 break;
7535 case 10:
7536 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7537 break;
7538 case 14:
7539 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7540 break;
7541 }
7542 if (INTEL_INFO(dev)->gen >= 4)
7543 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7544
190f68c5 7545 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7546 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7547 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7548 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7549 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7550 else
7551 dpll |= PLL_REF_INPUT_DREFCLK;
7552
7553 dpll |= DPLL_VCO_ENABLE;
190f68c5 7554 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7555
eb1cbe48 7556 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7557 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7558 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7559 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7560 }
7561}
7562
251ac862
DV
7563static void i8xx_compute_dpll(struct intel_crtc *crtc,
7564 struct intel_crtc_state *crtc_state,
7565 intel_clock_t *reduced_clock,
7566 int num_connectors)
eb1cbe48 7567{
f47709a9 7568 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7569 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7570 u32 dpll;
190f68c5 7571 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7572
190f68c5 7573 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7574
eb1cbe48
DV
7575 dpll = DPLL_VGA_MODE_DIS;
7576
a93e255f 7577 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7578 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7579 } else {
7580 if (clock->p1 == 2)
7581 dpll |= PLL_P1_DIVIDE_BY_TWO;
7582 else
7583 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7584 if (clock->p2 == 4)
7585 dpll |= PLL_P2_DIVIDE_BY_4;
7586 }
7587
a93e255f 7588 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7589 dpll |= DPLL_DVO_2X_MODE;
7590
a93e255f 7591 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7592 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7593 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7594 else
7595 dpll |= PLL_REF_INPUT_DREFCLK;
7596
7597 dpll |= DPLL_VCO_ENABLE;
190f68c5 7598 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7599}
7600
8a654f3b 7601static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7602{
7603 struct drm_device *dev = intel_crtc->base.dev;
7604 struct drm_i915_private *dev_priv = dev->dev_private;
7605 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7606 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 7607 struct drm_display_mode *adjusted_mode =
6e3c9717 7608 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7609 uint32_t crtc_vtotal, crtc_vblank_end;
7610 int vsyncshift = 0;
4d8a62ea
DV
7611
7612 /* We need to be careful not to changed the adjusted mode, for otherwise
7613 * the hw state checker will get angry at the mismatch. */
7614 crtc_vtotal = adjusted_mode->crtc_vtotal;
7615 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7616
609aeaca 7617 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7618 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7619 crtc_vtotal -= 1;
7620 crtc_vblank_end -= 1;
609aeaca 7621
409ee761 7622 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7623 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7624 else
7625 vsyncshift = adjusted_mode->crtc_hsync_start -
7626 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7627 if (vsyncshift < 0)
7628 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7629 }
7630
7631 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7632 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7633
fe2b8f9d 7634 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7635 (adjusted_mode->crtc_hdisplay - 1) |
7636 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7637 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7638 (adjusted_mode->crtc_hblank_start - 1) |
7639 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7640 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7641 (adjusted_mode->crtc_hsync_start - 1) |
7642 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7643
fe2b8f9d 7644 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7645 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7646 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7647 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7648 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7649 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7650 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7651 (adjusted_mode->crtc_vsync_start - 1) |
7652 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7653
b5e508d4
PZ
7654 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7655 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7656 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7657 * bits. */
7658 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7659 (pipe == PIPE_B || pipe == PIPE_C))
7660 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7661
b0e77b9c
PZ
7662 /* pipesrc controls the size that is scaled from, which should
7663 * always be the user's requested size.
7664 */
7665 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7666 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7667 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7668}
7669
1bd1bd80 7670static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7671 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7672{
7673 struct drm_device *dev = crtc->base.dev;
7674 struct drm_i915_private *dev_priv = dev->dev_private;
7675 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7676 uint32_t tmp;
7677
7678 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7679 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7680 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7681 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7682 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7683 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7684 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7685 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7686 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7687
7688 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7689 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7690 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7691 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7692 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7693 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7694 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7695 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7696 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7697
7698 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7699 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7700 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7701 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7702 }
7703
7704 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7705 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7706 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7707
2d112de7
ACO
7708 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7709 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7710}
7711
f6a83288 7712void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7713 struct intel_crtc_state *pipe_config)
babea61d 7714{
2d112de7
ACO
7715 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7716 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7717 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7718 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7719
2d112de7
ACO
7720 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7721 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7722 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7723 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7724
2d112de7 7725 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 7726
2d112de7
ACO
7727 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7728 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
7729}
7730
84b046f3
DV
7731static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7732{
7733 struct drm_device *dev = intel_crtc->base.dev;
7734 struct drm_i915_private *dev_priv = dev->dev_private;
7735 uint32_t pipeconf;
7736
9f11a9e4 7737 pipeconf = 0;
84b046f3 7738
b6b5d049
VS
7739 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7740 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7741 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7742
6e3c9717 7743 if (intel_crtc->config->double_wide)
cf532bb2 7744 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7745
ff9ce46e
DV
7746 /* only g4x and later have fancy bpc/dither controls */
7747 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 7748 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7749 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7750 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7751 PIPECONF_DITHER_TYPE_SP;
84b046f3 7752
6e3c9717 7753 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7754 case 18:
7755 pipeconf |= PIPECONF_6BPC;
7756 break;
7757 case 24:
7758 pipeconf |= PIPECONF_8BPC;
7759 break;
7760 case 30:
7761 pipeconf |= PIPECONF_10BPC;
7762 break;
7763 default:
7764 /* Case prevented by intel_choose_pipe_bpp_dither. */
7765 BUG();
84b046f3
DV
7766 }
7767 }
7768
7769 if (HAS_PIPE_CXSR(dev)) {
7770 if (intel_crtc->lowfreq_avail) {
7771 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7772 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7773 } else {
7774 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7775 }
7776 }
7777
6e3c9717 7778 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7779 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7780 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7781 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7782 else
7783 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7784 } else
84b046f3
DV
7785 pipeconf |= PIPECONF_PROGRESSIVE;
7786
6e3c9717 7787 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 7788 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7789
84b046f3
DV
7790 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7791 POSTING_READ(PIPECONF(intel_crtc->pipe));
7792}
7793
190f68c5
ACO
7794static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7795 struct intel_crtc_state *crtc_state)
79e53945 7796{
c7653199 7797 struct drm_device *dev = crtc->base.dev;
79e53945 7798 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7799 int refclk, num_connectors = 0;
c329a4ec
DV
7800 intel_clock_t clock;
7801 bool ok;
7802 bool is_dsi = false;
5eddb70b 7803 struct intel_encoder *encoder;
d4906093 7804 const intel_limit_t *limit;
55bb9992 7805 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 7806 struct drm_connector *connector;
55bb9992
ACO
7807 struct drm_connector_state *connector_state;
7808 int i;
79e53945 7809
dd3cd74a
ACO
7810 memset(&crtc_state->dpll_hw_state, 0,
7811 sizeof(crtc_state->dpll_hw_state));
7812
da3ced29 7813 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
7814 if (connector_state->crtc != &crtc->base)
7815 continue;
7816
7817 encoder = to_intel_encoder(connector_state->best_encoder);
7818
5eddb70b 7819 switch (encoder->type) {
e9fd1c02
JN
7820 case INTEL_OUTPUT_DSI:
7821 is_dsi = true;
7822 break;
6847d71b
PZ
7823 default:
7824 break;
79e53945 7825 }
43565a06 7826
c751ce4f 7827 num_connectors++;
79e53945
JB
7828 }
7829
f2335330 7830 if (is_dsi)
5b18e57c 7831 return 0;
f2335330 7832
190f68c5 7833 if (!crtc_state->clock_set) {
a93e255f 7834 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7835
e9fd1c02
JN
7836 /*
7837 * Returns a set of divisors for the desired target clock with
7838 * the given refclk, or FALSE. The returned values represent
7839 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7840 * 2) / p1 / p2.
7841 */
a93e255f
ACO
7842 limit = intel_limit(crtc_state, refclk);
7843 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7844 crtc_state->port_clock,
e9fd1c02 7845 refclk, NULL, &clock);
f2335330 7846 if (!ok) {
e9fd1c02
JN
7847 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7848 return -EINVAL;
7849 }
79e53945 7850
f2335330 7851 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7852 crtc_state->dpll.n = clock.n;
7853 crtc_state->dpll.m1 = clock.m1;
7854 crtc_state->dpll.m2 = clock.m2;
7855 crtc_state->dpll.p1 = clock.p1;
7856 crtc_state->dpll.p2 = clock.p2;
f47709a9 7857 }
7026d4ac 7858
e9fd1c02 7859 if (IS_GEN2(dev)) {
c329a4ec 7860 i8xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7861 num_connectors);
9d556c99 7862 } else if (IS_CHERRYVIEW(dev)) {
251ac862 7863 chv_compute_dpll(crtc, crtc_state);
e9fd1c02 7864 } else if (IS_VALLEYVIEW(dev)) {
251ac862 7865 vlv_compute_dpll(crtc, crtc_state);
e9fd1c02 7866 } else {
c329a4ec 7867 i9xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7868 num_connectors);
e9fd1c02 7869 }
79e53945 7870
c8f7a0db 7871 return 0;
f564048e
EA
7872}
7873
2fa2fe9a 7874static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7875 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7876{
7877 struct drm_device *dev = crtc->base.dev;
7878 struct drm_i915_private *dev_priv = dev->dev_private;
7879 uint32_t tmp;
7880
dc9e7dec
VS
7881 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7882 return;
7883
2fa2fe9a 7884 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7885 if (!(tmp & PFIT_ENABLE))
7886 return;
2fa2fe9a 7887
06922821 7888 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7889 if (INTEL_INFO(dev)->gen < 4) {
7890 if (crtc->pipe != PIPE_B)
7891 return;
2fa2fe9a
DV
7892 } else {
7893 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7894 return;
7895 }
7896
06922821 7897 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7898 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7899 if (INTEL_INFO(dev)->gen < 5)
7900 pipe_config->gmch_pfit.lvds_border_bits =
7901 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7902}
7903
acbec814 7904static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7905 struct intel_crtc_state *pipe_config)
acbec814
JB
7906{
7907 struct drm_device *dev = crtc->base.dev;
7908 struct drm_i915_private *dev_priv = dev->dev_private;
7909 int pipe = pipe_config->cpu_transcoder;
7910 intel_clock_t clock;
7911 u32 mdiv;
662c6ecb 7912 int refclk = 100000;
acbec814 7913
f573de5a
SK
7914 /* In case of MIPI DPLL will not even be used */
7915 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7916 return;
7917
a580516d 7918 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7919 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7920 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7921
7922 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7923 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7924 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7925 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7926 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7927
f646628b 7928 vlv_clock(refclk, &clock);
acbec814 7929
f646628b
VS
7930 /* clock.dot is the fast clock */
7931 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
7932}
7933
5724dbd1
DL
7934static void
7935i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7936 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7937{
7938 struct drm_device *dev = crtc->base.dev;
7939 struct drm_i915_private *dev_priv = dev->dev_private;
7940 u32 val, base, offset;
7941 int pipe = crtc->pipe, plane = crtc->plane;
7942 int fourcc, pixel_format;
6761dd31 7943 unsigned int aligned_height;
b113d5ee 7944 struct drm_framebuffer *fb;
1b842c89 7945 struct intel_framebuffer *intel_fb;
1ad292b5 7946
42a7b088
DL
7947 val = I915_READ(DSPCNTR(plane));
7948 if (!(val & DISPLAY_PLANE_ENABLE))
7949 return;
7950
d9806c9f 7951 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7952 if (!intel_fb) {
1ad292b5
JB
7953 DRM_DEBUG_KMS("failed to alloc fb\n");
7954 return;
7955 }
7956
1b842c89
DL
7957 fb = &intel_fb->base;
7958
18c5247e
DV
7959 if (INTEL_INFO(dev)->gen >= 4) {
7960 if (val & DISPPLANE_TILED) {
49af449b 7961 plane_config->tiling = I915_TILING_X;
18c5247e
DV
7962 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7963 }
7964 }
1ad292b5
JB
7965
7966 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7967 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
7968 fb->pixel_format = fourcc;
7969 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
7970
7971 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 7972 if (plane_config->tiling)
1ad292b5
JB
7973 offset = I915_READ(DSPTILEOFF(plane));
7974 else
7975 offset = I915_READ(DSPLINOFF(plane));
7976 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7977 } else {
7978 base = I915_READ(DSPADDR(plane));
7979 }
7980 plane_config->base = base;
7981
7982 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7983 fb->width = ((val >> 16) & 0xfff) + 1;
7984 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7985
7986 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7987 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7988
b113d5ee 7989 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
7990 fb->pixel_format,
7991 fb->modifier[0]);
1ad292b5 7992
f37b5c2b 7993 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7994
2844a921
DL
7995 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7996 pipe_name(pipe), plane, fb->width, fb->height,
7997 fb->bits_per_pixel, base, fb->pitches[0],
7998 plane_config->size);
1ad292b5 7999
2d14030b 8000 plane_config->fb = intel_fb;
1ad292b5
JB
8001}
8002
70b23a98 8003static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8004 struct intel_crtc_state *pipe_config)
70b23a98
VS
8005{
8006 struct drm_device *dev = crtc->base.dev;
8007 struct drm_i915_private *dev_priv = dev->dev_private;
8008 int pipe = pipe_config->cpu_transcoder;
8009 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8010 intel_clock_t clock;
8011 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8012 int refclk = 100000;
8013
a580516d 8014 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8015 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8016 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8017 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8018 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
a580516d 8019 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8020
8021 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8022 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8023 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8024 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8025 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8026
8027 chv_clock(refclk, &clock);
8028
8029 /* clock.dot is the fast clock */
8030 pipe_config->port_clock = clock.dot / 5;
8031}
8032
0e8ffe1b 8033static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8034 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8035{
8036 struct drm_device *dev = crtc->base.dev;
8037 struct drm_i915_private *dev_priv = dev->dev_private;
8038 uint32_t tmp;
8039
f458ebbc
DV
8040 if (!intel_display_power_is_enabled(dev_priv,
8041 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
8042 return false;
8043
e143a21c 8044 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8045 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8046
0e8ffe1b
DV
8047 tmp = I915_READ(PIPECONF(crtc->pipe));
8048 if (!(tmp & PIPECONF_ENABLE))
8049 return false;
8050
42571aef
VS
8051 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8052 switch (tmp & PIPECONF_BPC_MASK) {
8053 case PIPECONF_6BPC:
8054 pipe_config->pipe_bpp = 18;
8055 break;
8056 case PIPECONF_8BPC:
8057 pipe_config->pipe_bpp = 24;
8058 break;
8059 case PIPECONF_10BPC:
8060 pipe_config->pipe_bpp = 30;
8061 break;
8062 default:
8063 break;
8064 }
8065 }
8066
b5a9fa09
DV
8067 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8068 pipe_config->limited_color_range = true;
8069
282740f7
VS
8070 if (INTEL_INFO(dev)->gen < 4)
8071 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8072
1bd1bd80
DV
8073 intel_get_pipe_timings(crtc, pipe_config);
8074
2fa2fe9a
DV
8075 i9xx_get_pfit_config(crtc, pipe_config);
8076
6c49f241
DV
8077 if (INTEL_INFO(dev)->gen >= 4) {
8078 tmp = I915_READ(DPLL_MD(crtc->pipe));
8079 pipe_config->pixel_multiplier =
8080 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8081 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8082 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8083 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8084 tmp = I915_READ(DPLL(crtc->pipe));
8085 pipe_config->pixel_multiplier =
8086 ((tmp & SDVO_MULTIPLIER_MASK)
8087 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8088 } else {
8089 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8090 * port and will be fixed up in the encoder->get_config
8091 * function. */
8092 pipe_config->pixel_multiplier = 1;
8093 }
8bcc2795
DV
8094 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8095 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
8096 /*
8097 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8098 * on 830. Filter it out here so that we don't
8099 * report errors due to that.
8100 */
8101 if (IS_I830(dev))
8102 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8103
8bcc2795
DV
8104 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8105 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8106 } else {
8107 /* Mask out read-only status bits. */
8108 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8109 DPLL_PORTC_READY_MASK |
8110 DPLL_PORTB_READY_MASK);
8bcc2795 8111 }
6c49f241 8112
70b23a98
VS
8113 if (IS_CHERRYVIEW(dev))
8114 chv_crtc_clock_get(crtc, pipe_config);
8115 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8116 vlv_crtc_clock_get(crtc, pipe_config);
8117 else
8118 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8119
0e8ffe1b
DV
8120 return true;
8121}
8122
dde86e2d 8123static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8124{
8125 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8126 struct intel_encoder *encoder;
74cfd7ac 8127 u32 val, final;
13d83a67 8128 bool has_lvds = false;
199e5d79 8129 bool has_cpu_edp = false;
199e5d79 8130 bool has_panel = false;
99eb6a01
KP
8131 bool has_ck505 = false;
8132 bool can_ssc = false;
13d83a67
JB
8133
8134 /* We need to take the global config into account */
b2784e15 8135 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8136 switch (encoder->type) {
8137 case INTEL_OUTPUT_LVDS:
8138 has_panel = true;
8139 has_lvds = true;
8140 break;
8141 case INTEL_OUTPUT_EDP:
8142 has_panel = true;
2de6905f 8143 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8144 has_cpu_edp = true;
8145 break;
6847d71b
PZ
8146 default:
8147 break;
13d83a67
JB
8148 }
8149 }
8150
99eb6a01 8151 if (HAS_PCH_IBX(dev)) {
41aa3448 8152 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8153 can_ssc = has_ck505;
8154 } else {
8155 has_ck505 = false;
8156 can_ssc = true;
8157 }
8158
2de6905f
ID
8159 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8160 has_panel, has_lvds, has_ck505);
13d83a67
JB
8161
8162 /* Ironlake: try to setup display ref clock before DPLL
8163 * enabling. This is only under driver's control after
8164 * PCH B stepping, previous chipset stepping should be
8165 * ignoring this setting.
8166 */
74cfd7ac
CW
8167 val = I915_READ(PCH_DREF_CONTROL);
8168
8169 /* As we must carefully and slowly disable/enable each source in turn,
8170 * compute the final state we want first and check if we need to
8171 * make any changes at all.
8172 */
8173 final = val;
8174 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8175 if (has_ck505)
8176 final |= DREF_NONSPREAD_CK505_ENABLE;
8177 else
8178 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8179
8180 final &= ~DREF_SSC_SOURCE_MASK;
8181 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8182 final &= ~DREF_SSC1_ENABLE;
8183
8184 if (has_panel) {
8185 final |= DREF_SSC_SOURCE_ENABLE;
8186
8187 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8188 final |= DREF_SSC1_ENABLE;
8189
8190 if (has_cpu_edp) {
8191 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8192 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8193 else
8194 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8195 } else
8196 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8197 } else {
8198 final |= DREF_SSC_SOURCE_DISABLE;
8199 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8200 }
8201
8202 if (final == val)
8203 return;
8204
13d83a67 8205 /* Always enable nonspread source */
74cfd7ac 8206 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8207
99eb6a01 8208 if (has_ck505)
74cfd7ac 8209 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8210 else
74cfd7ac 8211 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8212
199e5d79 8213 if (has_panel) {
74cfd7ac
CW
8214 val &= ~DREF_SSC_SOURCE_MASK;
8215 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8216
199e5d79 8217 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8218 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8219 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8220 val |= DREF_SSC1_ENABLE;
e77166b5 8221 } else
74cfd7ac 8222 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8223
8224 /* Get SSC going before enabling the outputs */
74cfd7ac 8225 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8226 POSTING_READ(PCH_DREF_CONTROL);
8227 udelay(200);
8228
74cfd7ac 8229 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8230
8231 /* Enable CPU source on CPU attached eDP */
199e5d79 8232 if (has_cpu_edp) {
99eb6a01 8233 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8234 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8235 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8236 } else
74cfd7ac 8237 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8238 } else
74cfd7ac 8239 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8240
74cfd7ac 8241 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8242 POSTING_READ(PCH_DREF_CONTROL);
8243 udelay(200);
8244 } else {
8245 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8246
74cfd7ac 8247 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8248
8249 /* Turn off CPU output */
74cfd7ac 8250 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8251
74cfd7ac 8252 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8253 POSTING_READ(PCH_DREF_CONTROL);
8254 udelay(200);
8255
8256 /* Turn off the SSC source */
74cfd7ac
CW
8257 val &= ~DREF_SSC_SOURCE_MASK;
8258 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8259
8260 /* Turn off SSC1 */
74cfd7ac 8261 val &= ~DREF_SSC1_ENABLE;
199e5d79 8262
74cfd7ac 8263 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8264 POSTING_READ(PCH_DREF_CONTROL);
8265 udelay(200);
8266 }
74cfd7ac
CW
8267
8268 BUG_ON(val != final);
13d83a67
JB
8269}
8270
f31f2d55 8271static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8272{
f31f2d55 8273 uint32_t tmp;
dde86e2d 8274
0ff066a9
PZ
8275 tmp = I915_READ(SOUTH_CHICKEN2);
8276 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8277 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8278
0ff066a9
PZ
8279 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8280 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8281 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8282
0ff066a9
PZ
8283 tmp = I915_READ(SOUTH_CHICKEN2);
8284 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8285 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8286
0ff066a9
PZ
8287 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8288 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8289 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8290}
8291
8292/* WaMPhyProgramming:hsw */
8293static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8294{
8295 uint32_t tmp;
dde86e2d
PZ
8296
8297 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8298 tmp &= ~(0xFF << 24);
8299 tmp |= (0x12 << 24);
8300 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8301
dde86e2d
PZ
8302 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8303 tmp |= (1 << 11);
8304 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8305
8306 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8307 tmp |= (1 << 11);
8308 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8309
dde86e2d
PZ
8310 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8311 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8312 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8313
8314 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8315 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8316 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8317
0ff066a9
PZ
8318 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8319 tmp &= ~(7 << 13);
8320 tmp |= (5 << 13);
8321 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8322
0ff066a9
PZ
8323 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8324 tmp &= ~(7 << 13);
8325 tmp |= (5 << 13);
8326 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8327
8328 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8329 tmp &= ~0xFF;
8330 tmp |= 0x1C;
8331 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8332
8333 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8334 tmp &= ~0xFF;
8335 tmp |= 0x1C;
8336 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8337
8338 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8339 tmp &= ~(0xFF << 16);
8340 tmp |= (0x1C << 16);
8341 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8342
8343 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8344 tmp &= ~(0xFF << 16);
8345 tmp |= (0x1C << 16);
8346 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8347
0ff066a9
PZ
8348 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8349 tmp |= (1 << 27);
8350 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8351
0ff066a9
PZ
8352 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8353 tmp |= (1 << 27);
8354 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8355
0ff066a9
PZ
8356 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8357 tmp &= ~(0xF << 28);
8358 tmp |= (4 << 28);
8359 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8360
0ff066a9
PZ
8361 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8362 tmp &= ~(0xF << 28);
8363 tmp |= (4 << 28);
8364 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8365}
8366
2fa86a1f
PZ
8367/* Implements 3 different sequences from BSpec chapter "Display iCLK
8368 * Programming" based on the parameters passed:
8369 * - Sequence to enable CLKOUT_DP
8370 * - Sequence to enable CLKOUT_DP without spread
8371 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8372 */
8373static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8374 bool with_fdi)
f31f2d55
PZ
8375{
8376 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8377 uint32_t reg, tmp;
8378
8379 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8380 with_spread = true;
8381 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8382 with_fdi, "LP PCH doesn't have FDI\n"))
8383 with_fdi = false;
f31f2d55 8384
a580516d 8385 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8386
8387 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8388 tmp &= ~SBI_SSCCTL_DISABLE;
8389 tmp |= SBI_SSCCTL_PATHALT;
8390 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8391
8392 udelay(24);
8393
2fa86a1f
PZ
8394 if (with_spread) {
8395 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8396 tmp &= ~SBI_SSCCTL_PATHALT;
8397 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8398
2fa86a1f
PZ
8399 if (with_fdi) {
8400 lpt_reset_fdi_mphy(dev_priv);
8401 lpt_program_fdi_mphy(dev_priv);
8402 }
8403 }
dde86e2d 8404
2fa86a1f
PZ
8405 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8406 SBI_GEN0 : SBI_DBUFF0;
8407 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8408 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8409 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8410
a580516d 8411 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8412}
8413
47701c3b
PZ
8414/* Sequence to disable CLKOUT_DP */
8415static void lpt_disable_clkout_dp(struct drm_device *dev)
8416{
8417 struct drm_i915_private *dev_priv = dev->dev_private;
8418 uint32_t reg, tmp;
8419
a580516d 8420 mutex_lock(&dev_priv->sb_lock);
47701c3b
PZ
8421
8422 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8423 SBI_GEN0 : SBI_DBUFF0;
8424 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8425 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8426 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8427
8428 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8429 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8430 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8431 tmp |= SBI_SSCCTL_PATHALT;
8432 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8433 udelay(32);
8434 }
8435 tmp |= SBI_SSCCTL_DISABLE;
8436 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8437 }
8438
a580516d 8439 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8440}
8441
bf8fa3d3
PZ
8442static void lpt_init_pch_refclk(struct drm_device *dev)
8443{
bf8fa3d3
PZ
8444 struct intel_encoder *encoder;
8445 bool has_vga = false;
8446
b2784e15 8447 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8448 switch (encoder->type) {
8449 case INTEL_OUTPUT_ANALOG:
8450 has_vga = true;
8451 break;
6847d71b
PZ
8452 default:
8453 break;
bf8fa3d3
PZ
8454 }
8455 }
8456
47701c3b
PZ
8457 if (has_vga)
8458 lpt_enable_clkout_dp(dev, true, true);
8459 else
8460 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
8461}
8462
dde86e2d
PZ
8463/*
8464 * Initialize reference clocks when the driver loads
8465 */
8466void intel_init_pch_refclk(struct drm_device *dev)
8467{
8468 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8469 ironlake_init_pch_refclk(dev);
8470 else if (HAS_PCH_LPT(dev))
8471 lpt_init_pch_refclk(dev);
8472}
8473
55bb9992 8474static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8475{
55bb9992 8476 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8477 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8478 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8479 struct drm_connector *connector;
55bb9992 8480 struct drm_connector_state *connector_state;
d9d444cb 8481 struct intel_encoder *encoder;
55bb9992 8482 int num_connectors = 0, i;
d9d444cb
JB
8483 bool is_lvds = false;
8484
da3ced29 8485 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8486 if (connector_state->crtc != crtc_state->base.crtc)
8487 continue;
8488
8489 encoder = to_intel_encoder(connector_state->best_encoder);
8490
d9d444cb
JB
8491 switch (encoder->type) {
8492 case INTEL_OUTPUT_LVDS:
8493 is_lvds = true;
8494 break;
6847d71b
PZ
8495 default:
8496 break;
d9d444cb
JB
8497 }
8498 num_connectors++;
8499 }
8500
8501 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8502 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8503 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8504 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8505 }
8506
8507 return 120000;
8508}
8509
6ff93609 8510static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8511{
c8203565 8512 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8514 int pipe = intel_crtc->pipe;
c8203565
PZ
8515 uint32_t val;
8516
78114071 8517 val = 0;
c8203565 8518
6e3c9717 8519 switch (intel_crtc->config->pipe_bpp) {
c8203565 8520 case 18:
dfd07d72 8521 val |= PIPECONF_6BPC;
c8203565
PZ
8522 break;
8523 case 24:
dfd07d72 8524 val |= PIPECONF_8BPC;
c8203565
PZ
8525 break;
8526 case 30:
dfd07d72 8527 val |= PIPECONF_10BPC;
c8203565
PZ
8528 break;
8529 case 36:
dfd07d72 8530 val |= PIPECONF_12BPC;
c8203565
PZ
8531 break;
8532 default:
cc769b62
PZ
8533 /* Case prevented by intel_choose_pipe_bpp_dither. */
8534 BUG();
c8203565
PZ
8535 }
8536
6e3c9717 8537 if (intel_crtc->config->dither)
c8203565
PZ
8538 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8539
6e3c9717 8540 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8541 val |= PIPECONF_INTERLACED_ILK;
8542 else
8543 val |= PIPECONF_PROGRESSIVE;
8544
6e3c9717 8545 if (intel_crtc->config->limited_color_range)
3685a8f3 8546 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8547
c8203565
PZ
8548 I915_WRITE(PIPECONF(pipe), val);
8549 POSTING_READ(PIPECONF(pipe));
8550}
8551
86d3efce
VS
8552/*
8553 * Set up the pipe CSC unit.
8554 *
8555 * Currently only full range RGB to limited range RGB conversion
8556 * is supported, but eventually this should handle various
8557 * RGB<->YCbCr scenarios as well.
8558 */
50f3b016 8559static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8560{
8561 struct drm_device *dev = crtc->dev;
8562 struct drm_i915_private *dev_priv = dev->dev_private;
8563 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8564 int pipe = intel_crtc->pipe;
8565 uint16_t coeff = 0x7800; /* 1.0 */
8566
8567 /*
8568 * TODO: Check what kind of values actually come out of the pipe
8569 * with these coeff/postoff values and adjust to get the best
8570 * accuracy. Perhaps we even need to take the bpc value into
8571 * consideration.
8572 */
8573
6e3c9717 8574 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8575 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8576
8577 /*
8578 * GY/GU and RY/RU should be the other way around according
8579 * to BSpec, but reality doesn't agree. Just set them up in
8580 * a way that results in the correct picture.
8581 */
8582 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8583 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8584
8585 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8586 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8587
8588 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8589 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8590
8591 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8592 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8593 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8594
8595 if (INTEL_INFO(dev)->gen > 6) {
8596 uint16_t postoff = 0;
8597
6e3c9717 8598 if (intel_crtc->config->limited_color_range)
32cf0cb0 8599 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8600
8601 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8602 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8603 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8604
8605 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8606 } else {
8607 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8608
6e3c9717 8609 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8610 mode |= CSC_BLACK_SCREEN_OFFSET;
8611
8612 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8613 }
8614}
8615
6ff93609 8616static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8617{
756f85cf
PZ
8618 struct drm_device *dev = crtc->dev;
8619 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 8620 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8621 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8622 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
8623 uint32_t val;
8624
3eff4faa 8625 val = 0;
ee2b0b38 8626
6e3c9717 8627 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
8628 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8629
6e3c9717 8630 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8631 val |= PIPECONF_INTERLACED_ILK;
8632 else
8633 val |= PIPECONF_PROGRESSIVE;
8634
702e7a56
PZ
8635 I915_WRITE(PIPECONF(cpu_transcoder), val);
8636 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
8637
8638 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8639 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 8640
3cdf122c 8641 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
8642 val = 0;
8643
6e3c9717 8644 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8645 case 18:
8646 val |= PIPEMISC_DITHER_6_BPC;
8647 break;
8648 case 24:
8649 val |= PIPEMISC_DITHER_8_BPC;
8650 break;
8651 case 30:
8652 val |= PIPEMISC_DITHER_10_BPC;
8653 break;
8654 case 36:
8655 val |= PIPEMISC_DITHER_12_BPC;
8656 break;
8657 default:
8658 /* Case prevented by pipe_config_set_bpp. */
8659 BUG();
8660 }
8661
6e3c9717 8662 if (intel_crtc->config->dither)
756f85cf
PZ
8663 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8664
8665 I915_WRITE(PIPEMISC(pipe), val);
8666 }
ee2b0b38
PZ
8667}
8668
6591c6e4 8669static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8670 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8671 intel_clock_t *clock,
8672 bool *has_reduced_clock,
8673 intel_clock_t *reduced_clock)
8674{
8675 struct drm_device *dev = crtc->dev;
8676 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8677 int refclk;
d4906093 8678 const intel_limit_t *limit;
c329a4ec 8679 bool ret;
79e53945 8680
55bb9992 8681 refclk = ironlake_get_refclk(crtc_state);
79e53945 8682
d4906093
ML
8683 /*
8684 * Returns a set of divisors for the desired target clock with the given
8685 * refclk, or FALSE. The returned values represent the clock equation:
8686 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8687 */
a93e255f
ACO
8688 limit = intel_limit(crtc_state, refclk);
8689 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8690 crtc_state->port_clock,
ee9300bb 8691 refclk, NULL, clock);
6591c6e4
PZ
8692 if (!ret)
8693 return false;
cda4b7d3 8694
6591c6e4
PZ
8695 return true;
8696}
8697
d4b1931c
PZ
8698int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8699{
8700 /*
8701 * Account for spread spectrum to avoid
8702 * oversubscribing the link. Max center spread
8703 * is 2.5%; use 5% for safety's sake.
8704 */
8705 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8706 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8707}
8708
7429e9d4 8709static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8710{
7429e9d4 8711 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8712}
8713
de13a2e3 8714static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8715 struct intel_crtc_state *crtc_state,
7429e9d4 8716 u32 *fp,
9a7c7890 8717 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8718{
de13a2e3 8719 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8720 struct drm_device *dev = crtc->dev;
8721 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8722 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8723 struct drm_connector *connector;
55bb9992
ACO
8724 struct drm_connector_state *connector_state;
8725 struct intel_encoder *encoder;
de13a2e3 8726 uint32_t dpll;
55bb9992 8727 int factor, num_connectors = 0, i;
09ede541 8728 bool is_lvds = false, is_sdvo = false;
79e53945 8729
da3ced29 8730 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8731 if (connector_state->crtc != crtc_state->base.crtc)
8732 continue;
8733
8734 encoder = to_intel_encoder(connector_state->best_encoder);
8735
8736 switch (encoder->type) {
79e53945
JB
8737 case INTEL_OUTPUT_LVDS:
8738 is_lvds = true;
8739 break;
8740 case INTEL_OUTPUT_SDVO:
7d57382e 8741 case INTEL_OUTPUT_HDMI:
79e53945 8742 is_sdvo = true;
79e53945 8743 break;
6847d71b
PZ
8744 default:
8745 break;
79e53945 8746 }
43565a06 8747
c751ce4f 8748 num_connectors++;
79e53945 8749 }
79e53945 8750
c1858123 8751 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8752 factor = 21;
8753 if (is_lvds) {
8754 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8755 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8756 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8757 factor = 25;
190f68c5 8758 } else if (crtc_state->sdvo_tv_clock)
8febb297 8759 factor = 20;
c1858123 8760
190f68c5 8761 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8762 *fp |= FP_CB_TUNE;
2c07245f 8763
9a7c7890
DV
8764 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8765 *fp2 |= FP_CB_TUNE;
8766
5eddb70b 8767 dpll = 0;
2c07245f 8768
a07d6787
EA
8769 if (is_lvds)
8770 dpll |= DPLLB_MODE_LVDS;
8771 else
8772 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8773
190f68c5 8774 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8775 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8776
8777 if (is_sdvo)
4a33e48d 8778 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8779 if (crtc_state->has_dp_encoder)
4a33e48d 8780 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8781
a07d6787 8782 /* compute bitmask from p1 value */
190f68c5 8783 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8784 /* also FPA1 */
190f68c5 8785 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8786
190f68c5 8787 switch (crtc_state->dpll.p2) {
a07d6787
EA
8788 case 5:
8789 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8790 break;
8791 case 7:
8792 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8793 break;
8794 case 10:
8795 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8796 break;
8797 case 14:
8798 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8799 break;
79e53945
JB
8800 }
8801
b4c09f3b 8802 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8803 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8804 else
8805 dpll |= PLL_REF_INPUT_DREFCLK;
8806
959e16d6 8807 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8808}
8809
190f68c5
ACO
8810static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8811 struct intel_crtc_state *crtc_state)
de13a2e3 8812{
c7653199 8813 struct drm_device *dev = crtc->base.dev;
de13a2e3 8814 intel_clock_t clock, reduced_clock;
cbbab5bd 8815 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8816 bool ok, has_reduced_clock = false;
8b47047b 8817 bool is_lvds = false;
e2b78267 8818 struct intel_shared_dpll *pll;
de13a2e3 8819
dd3cd74a
ACO
8820 memset(&crtc_state->dpll_hw_state, 0,
8821 sizeof(crtc_state->dpll_hw_state));
8822
409ee761 8823 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 8824
5dc5298b
PZ
8825 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8826 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8827
190f68c5 8828 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8829 &has_reduced_clock, &reduced_clock);
190f68c5 8830 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8831 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8832 return -EINVAL;
79e53945 8833 }
f47709a9 8834 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8835 if (!crtc_state->clock_set) {
8836 crtc_state->dpll.n = clock.n;
8837 crtc_state->dpll.m1 = clock.m1;
8838 crtc_state->dpll.m2 = clock.m2;
8839 crtc_state->dpll.p1 = clock.p1;
8840 crtc_state->dpll.p2 = clock.p2;
f47709a9 8841 }
79e53945 8842
5dc5298b 8843 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8844 if (crtc_state->has_pch_encoder) {
8845 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8846 if (has_reduced_clock)
7429e9d4 8847 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8848
190f68c5 8849 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8850 &fp, &reduced_clock,
8851 has_reduced_clock ? &fp2 : NULL);
8852
190f68c5
ACO
8853 crtc_state->dpll_hw_state.dpll = dpll;
8854 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8855 if (has_reduced_clock)
190f68c5 8856 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8857 else
190f68c5 8858 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8859
190f68c5 8860 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 8861 if (pll == NULL) {
84f44ce7 8862 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8863 pipe_name(crtc->pipe));
4b645f14
JB
8864 return -EINVAL;
8865 }
3fb37703 8866 }
79e53945 8867
ab585dea 8868 if (is_lvds && has_reduced_clock)
c7653199 8869 crtc->lowfreq_avail = true;
bcd644e0 8870 else
c7653199 8871 crtc->lowfreq_avail = false;
e2b78267 8872
c8f7a0db 8873 return 0;
79e53945
JB
8874}
8875
eb14cb74
VS
8876static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8877 struct intel_link_m_n *m_n)
8878{
8879 struct drm_device *dev = crtc->base.dev;
8880 struct drm_i915_private *dev_priv = dev->dev_private;
8881 enum pipe pipe = crtc->pipe;
8882
8883 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8884 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8885 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8886 & ~TU_SIZE_MASK;
8887 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8888 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8889 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8890}
8891
8892static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8893 enum transcoder transcoder,
b95af8be
VK
8894 struct intel_link_m_n *m_n,
8895 struct intel_link_m_n *m2_n2)
72419203
DV
8896{
8897 struct drm_device *dev = crtc->base.dev;
8898 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8899 enum pipe pipe = crtc->pipe;
72419203 8900
eb14cb74
VS
8901 if (INTEL_INFO(dev)->gen >= 5) {
8902 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8903 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8904 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8905 & ~TU_SIZE_MASK;
8906 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8907 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8908 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8909 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8910 * gen < 8) and if DRRS is supported (to make sure the
8911 * registers are not unnecessarily read).
8912 */
8913 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8914 crtc->config->has_drrs) {
b95af8be
VK
8915 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8916 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8917 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8918 & ~TU_SIZE_MASK;
8919 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8920 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8921 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8922 }
eb14cb74
VS
8923 } else {
8924 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8925 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8926 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8927 & ~TU_SIZE_MASK;
8928 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8929 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8930 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8931 }
8932}
8933
8934void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8935 struct intel_crtc_state *pipe_config)
eb14cb74 8936{
681a8504 8937 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8938 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8939 else
8940 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8941 &pipe_config->dp_m_n,
8942 &pipe_config->dp_m2_n2);
eb14cb74 8943}
72419203 8944
eb14cb74 8945static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8946 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8947{
8948 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8949 &pipe_config->fdi_m_n, NULL);
72419203
DV
8950}
8951
bd2e244f 8952static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8953 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8954{
8955 struct drm_device *dev = crtc->base.dev;
8956 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
8957 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8958 uint32_t ps_ctrl = 0;
8959 int id = -1;
8960 int i;
bd2e244f 8961
a1b2278e
CK
8962 /* find scaler attached to this pipe */
8963 for (i = 0; i < crtc->num_scalers; i++) {
8964 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8965 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8966 id = i;
8967 pipe_config->pch_pfit.enabled = true;
8968 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8969 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8970 break;
8971 }
8972 }
bd2e244f 8973
a1b2278e
CK
8974 scaler_state->scaler_id = id;
8975 if (id >= 0) {
8976 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8977 } else {
8978 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8979 }
8980}
8981
5724dbd1
DL
8982static void
8983skylake_get_initial_plane_config(struct intel_crtc *crtc,
8984 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8985{
8986 struct drm_device *dev = crtc->base.dev;
8987 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 8988 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8989 int pipe = crtc->pipe;
8990 int fourcc, pixel_format;
6761dd31 8991 unsigned int aligned_height;
bc8d7dff 8992 struct drm_framebuffer *fb;
1b842c89 8993 struct intel_framebuffer *intel_fb;
bc8d7dff 8994
d9806c9f 8995 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8996 if (!intel_fb) {
bc8d7dff
DL
8997 DRM_DEBUG_KMS("failed to alloc fb\n");
8998 return;
8999 }
9000
1b842c89
DL
9001 fb = &intel_fb->base;
9002
bc8d7dff 9003 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9004 if (!(val & PLANE_CTL_ENABLE))
9005 goto error;
9006
bc8d7dff
DL
9007 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9008 fourcc = skl_format_to_fourcc(pixel_format,
9009 val & PLANE_CTL_ORDER_RGBX,
9010 val & PLANE_CTL_ALPHA_MASK);
9011 fb->pixel_format = fourcc;
9012 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9013
40f46283
DL
9014 tiling = val & PLANE_CTL_TILED_MASK;
9015 switch (tiling) {
9016 case PLANE_CTL_TILED_LINEAR:
9017 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9018 break;
9019 case PLANE_CTL_TILED_X:
9020 plane_config->tiling = I915_TILING_X;
9021 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9022 break;
9023 case PLANE_CTL_TILED_Y:
9024 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9025 break;
9026 case PLANE_CTL_TILED_YF:
9027 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9028 break;
9029 default:
9030 MISSING_CASE(tiling);
9031 goto error;
9032 }
9033
bc8d7dff
DL
9034 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9035 plane_config->base = base;
9036
9037 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9038
9039 val = I915_READ(PLANE_SIZE(pipe, 0));
9040 fb->height = ((val >> 16) & 0xfff) + 1;
9041 fb->width = ((val >> 0) & 0x1fff) + 1;
9042
9043 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
9044 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9045 fb->pixel_format);
bc8d7dff
DL
9046 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9047
9048 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9049 fb->pixel_format,
9050 fb->modifier[0]);
bc8d7dff 9051
f37b5c2b 9052 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9053
9054 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9055 pipe_name(pipe), fb->width, fb->height,
9056 fb->bits_per_pixel, base, fb->pitches[0],
9057 plane_config->size);
9058
2d14030b 9059 plane_config->fb = intel_fb;
bc8d7dff
DL
9060 return;
9061
9062error:
9063 kfree(fb);
9064}
9065
2fa2fe9a 9066static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9067 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9068{
9069 struct drm_device *dev = crtc->base.dev;
9070 struct drm_i915_private *dev_priv = dev->dev_private;
9071 uint32_t tmp;
9072
9073 tmp = I915_READ(PF_CTL(crtc->pipe));
9074
9075 if (tmp & PF_ENABLE) {
fd4daa9c 9076 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9077 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9078 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9079
9080 /* We currently do not free assignements of panel fitters on
9081 * ivb/hsw (since we don't use the higher upscaling modes which
9082 * differentiates them) so just WARN about this case for now. */
9083 if (IS_GEN7(dev)) {
9084 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9085 PF_PIPE_SEL_IVB(crtc->pipe));
9086 }
2fa2fe9a 9087 }
79e53945
JB
9088}
9089
5724dbd1
DL
9090static void
9091ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9092 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9093{
9094 struct drm_device *dev = crtc->base.dev;
9095 struct drm_i915_private *dev_priv = dev->dev_private;
9096 u32 val, base, offset;
aeee5a49 9097 int pipe = crtc->pipe;
4c6baa59 9098 int fourcc, pixel_format;
6761dd31 9099 unsigned int aligned_height;
b113d5ee 9100 struct drm_framebuffer *fb;
1b842c89 9101 struct intel_framebuffer *intel_fb;
4c6baa59 9102
42a7b088
DL
9103 val = I915_READ(DSPCNTR(pipe));
9104 if (!(val & DISPLAY_PLANE_ENABLE))
9105 return;
9106
d9806c9f 9107 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9108 if (!intel_fb) {
4c6baa59
JB
9109 DRM_DEBUG_KMS("failed to alloc fb\n");
9110 return;
9111 }
9112
1b842c89
DL
9113 fb = &intel_fb->base;
9114
18c5247e
DV
9115 if (INTEL_INFO(dev)->gen >= 4) {
9116 if (val & DISPPLANE_TILED) {
49af449b 9117 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9118 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9119 }
9120 }
4c6baa59
JB
9121
9122 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9123 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9124 fb->pixel_format = fourcc;
9125 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9126
aeee5a49 9127 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9128 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9129 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9130 } else {
49af449b 9131 if (plane_config->tiling)
aeee5a49 9132 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9133 else
aeee5a49 9134 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9135 }
9136 plane_config->base = base;
9137
9138 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9139 fb->width = ((val >> 16) & 0xfff) + 1;
9140 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9141
9142 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9143 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9144
b113d5ee 9145 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9146 fb->pixel_format,
9147 fb->modifier[0]);
4c6baa59 9148
f37b5c2b 9149 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9150
2844a921
DL
9151 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9152 pipe_name(pipe), fb->width, fb->height,
9153 fb->bits_per_pixel, base, fb->pitches[0],
9154 plane_config->size);
b113d5ee 9155
2d14030b 9156 plane_config->fb = intel_fb;
4c6baa59
JB
9157}
9158
0e8ffe1b 9159static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9160 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9161{
9162 struct drm_device *dev = crtc->base.dev;
9163 struct drm_i915_private *dev_priv = dev->dev_private;
9164 uint32_t tmp;
9165
f458ebbc
DV
9166 if (!intel_display_power_is_enabled(dev_priv,
9167 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
9168 return false;
9169
e143a21c 9170 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 9171 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 9172
0e8ffe1b
DV
9173 tmp = I915_READ(PIPECONF(crtc->pipe));
9174 if (!(tmp & PIPECONF_ENABLE))
9175 return false;
9176
42571aef
VS
9177 switch (tmp & PIPECONF_BPC_MASK) {
9178 case PIPECONF_6BPC:
9179 pipe_config->pipe_bpp = 18;
9180 break;
9181 case PIPECONF_8BPC:
9182 pipe_config->pipe_bpp = 24;
9183 break;
9184 case PIPECONF_10BPC:
9185 pipe_config->pipe_bpp = 30;
9186 break;
9187 case PIPECONF_12BPC:
9188 pipe_config->pipe_bpp = 36;
9189 break;
9190 default:
9191 break;
9192 }
9193
b5a9fa09
DV
9194 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9195 pipe_config->limited_color_range = true;
9196
ab9412ba 9197 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
9198 struct intel_shared_dpll *pll;
9199
88adfff1
DV
9200 pipe_config->has_pch_encoder = true;
9201
627eb5a3
DV
9202 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9203 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9204 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9205
9206 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9207
c0d43d62 9208 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
9209 pipe_config->shared_dpll =
9210 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9211 } else {
9212 tmp = I915_READ(PCH_DPLL_SEL);
9213 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9214 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9215 else
9216 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9217 }
66e985c0
DV
9218
9219 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9220
9221 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9222 &pipe_config->dpll_hw_state));
c93f54cf
DV
9223
9224 tmp = pipe_config->dpll_hw_state.dpll;
9225 pipe_config->pixel_multiplier =
9226 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9227 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9228
9229 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9230 } else {
9231 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9232 }
9233
1bd1bd80
DV
9234 intel_get_pipe_timings(crtc, pipe_config);
9235
2fa2fe9a
DV
9236 ironlake_get_pfit_config(crtc, pipe_config);
9237
0e8ffe1b
DV
9238 return true;
9239}
9240
be256dc7
PZ
9241static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9242{
9243 struct drm_device *dev = dev_priv->dev;
be256dc7 9244 struct intel_crtc *crtc;
be256dc7 9245
d3fcc808 9246 for_each_intel_crtc(dev, crtc)
e2c719b7 9247 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9248 pipe_name(crtc->pipe));
9249
e2c719b7
RC
9250 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9251 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9252 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9253 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9254 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9255 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9256 "CPU PWM1 enabled\n");
c5107b87 9257 if (IS_HASWELL(dev))
e2c719b7 9258 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9259 "CPU PWM2 enabled\n");
e2c719b7 9260 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9261 "PCH PWM1 enabled\n");
e2c719b7 9262 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9263 "Utility pin enabled\n");
e2c719b7 9264 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9265
9926ada1
PZ
9266 /*
9267 * In theory we can still leave IRQs enabled, as long as only the HPD
9268 * interrupts remain enabled. We used to check for that, but since it's
9269 * gen-specific and since we only disable LCPLL after we fully disable
9270 * the interrupts, the check below should be enough.
9271 */
e2c719b7 9272 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9273}
9274
9ccd5aeb
PZ
9275static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9276{
9277 struct drm_device *dev = dev_priv->dev;
9278
9279 if (IS_HASWELL(dev))
9280 return I915_READ(D_COMP_HSW);
9281 else
9282 return I915_READ(D_COMP_BDW);
9283}
9284
3c4c9b81
PZ
9285static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9286{
9287 struct drm_device *dev = dev_priv->dev;
9288
9289 if (IS_HASWELL(dev)) {
9290 mutex_lock(&dev_priv->rps.hw_lock);
9291 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9292 val))
f475dadf 9293 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9294 mutex_unlock(&dev_priv->rps.hw_lock);
9295 } else {
9ccd5aeb
PZ
9296 I915_WRITE(D_COMP_BDW, val);
9297 POSTING_READ(D_COMP_BDW);
3c4c9b81 9298 }
be256dc7
PZ
9299}
9300
9301/*
9302 * This function implements pieces of two sequences from BSpec:
9303 * - Sequence for display software to disable LCPLL
9304 * - Sequence for display software to allow package C8+
9305 * The steps implemented here are just the steps that actually touch the LCPLL
9306 * register. Callers should take care of disabling all the display engine
9307 * functions, doing the mode unset, fixing interrupts, etc.
9308 */
6ff58d53
PZ
9309static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9310 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9311{
9312 uint32_t val;
9313
9314 assert_can_disable_lcpll(dev_priv);
9315
9316 val = I915_READ(LCPLL_CTL);
9317
9318 if (switch_to_fclk) {
9319 val |= LCPLL_CD_SOURCE_FCLK;
9320 I915_WRITE(LCPLL_CTL, val);
9321
9322 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9323 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9324 DRM_ERROR("Switching to FCLK failed\n");
9325
9326 val = I915_READ(LCPLL_CTL);
9327 }
9328
9329 val |= LCPLL_PLL_DISABLE;
9330 I915_WRITE(LCPLL_CTL, val);
9331 POSTING_READ(LCPLL_CTL);
9332
9333 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9334 DRM_ERROR("LCPLL still locked\n");
9335
9ccd5aeb 9336 val = hsw_read_dcomp(dev_priv);
be256dc7 9337 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9338 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9339 ndelay(100);
9340
9ccd5aeb
PZ
9341 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9342 1))
be256dc7
PZ
9343 DRM_ERROR("D_COMP RCOMP still in progress\n");
9344
9345 if (allow_power_down) {
9346 val = I915_READ(LCPLL_CTL);
9347 val |= LCPLL_POWER_DOWN_ALLOW;
9348 I915_WRITE(LCPLL_CTL, val);
9349 POSTING_READ(LCPLL_CTL);
9350 }
9351}
9352
9353/*
9354 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9355 * source.
9356 */
6ff58d53 9357static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9358{
9359 uint32_t val;
9360
9361 val = I915_READ(LCPLL_CTL);
9362
9363 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9364 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9365 return;
9366
a8a8bd54
PZ
9367 /*
9368 * Make sure we're not on PC8 state before disabling PC8, otherwise
9369 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9370 */
59bad947 9371 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9372
be256dc7
PZ
9373 if (val & LCPLL_POWER_DOWN_ALLOW) {
9374 val &= ~LCPLL_POWER_DOWN_ALLOW;
9375 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9376 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9377 }
9378
9ccd5aeb 9379 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9380 val |= D_COMP_COMP_FORCE;
9381 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9382 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9383
9384 val = I915_READ(LCPLL_CTL);
9385 val &= ~LCPLL_PLL_DISABLE;
9386 I915_WRITE(LCPLL_CTL, val);
9387
9388 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9389 DRM_ERROR("LCPLL not locked yet\n");
9390
9391 if (val & LCPLL_CD_SOURCE_FCLK) {
9392 val = I915_READ(LCPLL_CTL);
9393 val &= ~LCPLL_CD_SOURCE_FCLK;
9394 I915_WRITE(LCPLL_CTL, val);
9395
9396 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9397 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9398 DRM_ERROR("Switching back to LCPLL failed\n");
9399 }
215733fa 9400
59bad947 9401 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9402 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9403}
9404
765dab67
PZ
9405/*
9406 * Package states C8 and deeper are really deep PC states that can only be
9407 * reached when all the devices on the system allow it, so even if the graphics
9408 * device allows PC8+, it doesn't mean the system will actually get to these
9409 * states. Our driver only allows PC8+ when going into runtime PM.
9410 *
9411 * The requirements for PC8+ are that all the outputs are disabled, the power
9412 * well is disabled and most interrupts are disabled, and these are also
9413 * requirements for runtime PM. When these conditions are met, we manually do
9414 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9415 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9416 * hang the machine.
9417 *
9418 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9419 * the state of some registers, so when we come back from PC8+ we need to
9420 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9421 * need to take care of the registers kept by RC6. Notice that this happens even
9422 * if we don't put the device in PCI D3 state (which is what currently happens
9423 * because of the runtime PM support).
9424 *
9425 * For more, read "Display Sequences for Package C8" on the hardware
9426 * documentation.
9427 */
a14cb6fc 9428void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9429{
c67a470b
PZ
9430 struct drm_device *dev = dev_priv->dev;
9431 uint32_t val;
9432
c67a470b
PZ
9433 DRM_DEBUG_KMS("Enabling package C8+\n");
9434
c67a470b
PZ
9435 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9436 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9437 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9438 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9439 }
9440
9441 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9442 hsw_disable_lcpll(dev_priv, true, true);
9443}
9444
a14cb6fc 9445void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9446{
9447 struct drm_device *dev = dev_priv->dev;
9448 uint32_t val;
9449
c67a470b
PZ
9450 DRM_DEBUG_KMS("Disabling package C8+\n");
9451
9452 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9453 lpt_init_pch_refclk(dev);
9454
9455 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9456 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9457 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9458 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9459 }
9460
9461 intel_prepare_ddi(dev);
c67a470b
PZ
9462}
9463
27c329ed 9464static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9465{
a821fc46 9466 struct drm_device *dev = old_state->dev;
27c329ed 9467 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
f8437dd1 9468
27c329ed 9469 broxton_set_cdclk(dev, req_cdclk);
f8437dd1
VK
9470}
9471
b432e5cf 9472/* compute the max rate for new configuration */
27c329ed 9473static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9474{
b432e5cf 9475 struct intel_crtc *intel_crtc;
27c329ed 9476 struct intel_crtc_state *crtc_state;
b432e5cf 9477 int max_pixel_rate = 0;
b432e5cf 9478
27c329ed
ML
9479 for_each_intel_crtc(state->dev, intel_crtc) {
9480 int pixel_rate;
9481
9482 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9483 if (IS_ERR(crtc_state))
9484 return PTR_ERR(crtc_state);
9485
9486 if (!crtc_state->base.enable)
b432e5cf
VS
9487 continue;
9488
27c329ed 9489 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9490
9491 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
27c329ed 9492 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
b432e5cf
VS
9493 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9494
9495 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9496 }
9497
9498 return max_pixel_rate;
9499}
9500
9501static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9502{
9503 struct drm_i915_private *dev_priv = dev->dev_private;
9504 uint32_t val, data;
9505 int ret;
9506
9507 if (WARN((I915_READ(LCPLL_CTL) &
9508 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9509 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9510 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9511 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9512 "trying to change cdclk frequency with cdclk not enabled\n"))
9513 return;
9514
9515 mutex_lock(&dev_priv->rps.hw_lock);
9516 ret = sandybridge_pcode_write(dev_priv,
9517 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9518 mutex_unlock(&dev_priv->rps.hw_lock);
9519 if (ret) {
9520 DRM_ERROR("failed to inform pcode about cdclk change\n");
9521 return;
9522 }
9523
9524 val = I915_READ(LCPLL_CTL);
9525 val |= LCPLL_CD_SOURCE_FCLK;
9526 I915_WRITE(LCPLL_CTL, val);
9527
9528 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9529 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9530 DRM_ERROR("Switching to FCLK failed\n");
9531
9532 val = I915_READ(LCPLL_CTL);
9533 val &= ~LCPLL_CLK_FREQ_MASK;
9534
9535 switch (cdclk) {
9536 case 450000:
9537 val |= LCPLL_CLK_FREQ_450;
9538 data = 0;
9539 break;
9540 case 540000:
9541 val |= LCPLL_CLK_FREQ_54O_BDW;
9542 data = 1;
9543 break;
9544 case 337500:
9545 val |= LCPLL_CLK_FREQ_337_5_BDW;
9546 data = 2;
9547 break;
9548 case 675000:
9549 val |= LCPLL_CLK_FREQ_675_BDW;
9550 data = 3;
9551 break;
9552 default:
9553 WARN(1, "invalid cdclk frequency\n");
9554 return;
9555 }
9556
9557 I915_WRITE(LCPLL_CTL, val);
9558
9559 val = I915_READ(LCPLL_CTL);
9560 val &= ~LCPLL_CD_SOURCE_FCLK;
9561 I915_WRITE(LCPLL_CTL, val);
9562
9563 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9564 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9565 DRM_ERROR("Switching back to LCPLL failed\n");
9566
9567 mutex_lock(&dev_priv->rps.hw_lock);
9568 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9569 mutex_unlock(&dev_priv->rps.hw_lock);
9570
9571 intel_update_cdclk(dev);
9572
9573 WARN(cdclk != dev_priv->cdclk_freq,
9574 "cdclk requested %d kHz but got %d kHz\n",
9575 cdclk, dev_priv->cdclk_freq);
9576}
9577
27c329ed 9578static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9579{
27c329ed
ML
9580 struct drm_i915_private *dev_priv = to_i915(state->dev);
9581 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9582 int cdclk;
9583
9584 /*
9585 * FIXME should also account for plane ratio
9586 * once 64bpp pixel formats are supported.
9587 */
27c329ed 9588 if (max_pixclk > 540000)
b432e5cf 9589 cdclk = 675000;
27c329ed 9590 else if (max_pixclk > 450000)
b432e5cf 9591 cdclk = 540000;
27c329ed 9592 else if (max_pixclk > 337500)
b432e5cf
VS
9593 cdclk = 450000;
9594 else
9595 cdclk = 337500;
9596
9597 /*
9598 * FIXME move the cdclk caclulation to
9599 * compute_config() so we can fail gracegully.
9600 */
9601 if (cdclk > dev_priv->max_cdclk_freq) {
9602 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9603 cdclk, dev_priv->max_cdclk_freq);
9604 cdclk = dev_priv->max_cdclk_freq;
9605 }
9606
27c329ed 9607 to_intel_atomic_state(state)->cdclk = cdclk;
b432e5cf
VS
9608
9609 return 0;
9610}
9611
27c329ed 9612static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9613{
27c329ed
ML
9614 struct drm_device *dev = old_state->dev;
9615 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
b432e5cf 9616
27c329ed 9617 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9618}
9619
190f68c5
ACO
9620static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9621 struct intel_crtc_state *crtc_state)
09b4ddf9 9622{
190f68c5 9623 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 9624 return -EINVAL;
716c2e55 9625
c7653199 9626 crtc->lowfreq_avail = false;
644cef34 9627
c8f7a0db 9628 return 0;
79e53945
JB
9629}
9630
3760b59c
S
9631static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9632 enum port port,
9633 struct intel_crtc_state *pipe_config)
9634{
9635 switch (port) {
9636 case PORT_A:
9637 pipe_config->ddi_pll_sel = SKL_DPLL0;
9638 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9639 break;
9640 case PORT_B:
9641 pipe_config->ddi_pll_sel = SKL_DPLL1;
9642 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9643 break;
9644 case PORT_C:
9645 pipe_config->ddi_pll_sel = SKL_DPLL2;
9646 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9647 break;
9648 default:
9649 DRM_ERROR("Incorrect port type\n");
9650 }
9651}
9652
96b7dfb7
S
9653static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9654 enum port port,
5cec258b 9655 struct intel_crtc_state *pipe_config)
96b7dfb7 9656{
3148ade7 9657 u32 temp, dpll_ctl1;
96b7dfb7
S
9658
9659 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9660 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9661
9662 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
9663 case SKL_DPLL0:
9664 /*
9665 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9666 * of the shared DPLL framework and thus needs to be read out
9667 * separately
9668 */
9669 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9670 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9671 break;
96b7dfb7
S
9672 case SKL_DPLL1:
9673 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9674 break;
9675 case SKL_DPLL2:
9676 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9677 break;
9678 case SKL_DPLL3:
9679 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9680 break;
96b7dfb7
S
9681 }
9682}
9683
7d2c8175
DL
9684static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9685 enum port port,
5cec258b 9686 struct intel_crtc_state *pipe_config)
7d2c8175
DL
9687{
9688 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9689
9690 switch (pipe_config->ddi_pll_sel) {
9691 case PORT_CLK_SEL_WRPLL1:
9692 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9693 break;
9694 case PORT_CLK_SEL_WRPLL2:
9695 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9696 break;
9697 }
9698}
9699
26804afd 9700static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9701 struct intel_crtc_state *pipe_config)
26804afd
DV
9702{
9703 struct drm_device *dev = crtc->base.dev;
9704 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9705 struct intel_shared_dpll *pll;
26804afd
DV
9706 enum port port;
9707 uint32_t tmp;
9708
9709 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9710
9711 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9712
96b7dfb7
S
9713 if (IS_SKYLAKE(dev))
9714 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9715 else if (IS_BROXTON(dev))
9716 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9717 else
9718 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9719
d452c5b6
DV
9720 if (pipe_config->shared_dpll >= 0) {
9721 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9722
9723 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9724 &pipe_config->dpll_hw_state));
9725 }
9726
26804afd
DV
9727 /*
9728 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9729 * DDI E. So just check whether this pipe is wired to DDI E and whether
9730 * the PCH transcoder is on.
9731 */
ca370455
DL
9732 if (INTEL_INFO(dev)->gen < 9 &&
9733 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9734 pipe_config->has_pch_encoder = true;
9735
9736 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9737 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9738 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9739
9740 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9741 }
9742}
9743
0e8ffe1b 9744static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9745 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9746{
9747 struct drm_device *dev = crtc->base.dev;
9748 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 9749 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
9750 uint32_t tmp;
9751
f458ebbc 9752 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
9753 POWER_DOMAIN_PIPE(crtc->pipe)))
9754 return false;
9755
e143a21c 9756 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
9757 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9758
eccb140b
DV
9759 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9760 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9761 enum pipe trans_edp_pipe;
9762 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9763 default:
9764 WARN(1, "unknown pipe linked to edp transcoder\n");
9765 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9766 case TRANS_DDI_EDP_INPUT_A_ON:
9767 trans_edp_pipe = PIPE_A;
9768 break;
9769 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9770 trans_edp_pipe = PIPE_B;
9771 break;
9772 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9773 trans_edp_pipe = PIPE_C;
9774 break;
9775 }
9776
9777 if (trans_edp_pipe == crtc->pipe)
9778 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9779 }
9780
f458ebbc 9781 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 9782 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
9783 return false;
9784
eccb140b 9785 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
9786 if (!(tmp & PIPECONF_ENABLE))
9787 return false;
9788
26804afd 9789 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9790
1bd1bd80
DV
9791 intel_get_pipe_timings(crtc, pipe_config);
9792
a1b2278e
CK
9793 if (INTEL_INFO(dev)->gen >= 9) {
9794 skl_init_scalers(dev, crtc, pipe_config);
9795 }
9796
2fa2fe9a 9797 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
af99ceda
CK
9798
9799 if (INTEL_INFO(dev)->gen >= 9) {
9800 pipe_config->scaler_state.scaler_id = -1;
9801 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9802 }
9803
bd2e244f 9804 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
ff6d9f55 9805 if (INTEL_INFO(dev)->gen == 9)
bd2e244f 9806 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9807 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 9808 ironlake_get_pfit_config(crtc, pipe_config);
ff6d9f55
JB
9809 else
9810 MISSING_CASE(INTEL_INFO(dev)->gen);
bd2e244f 9811 }
88adfff1 9812
e59150dc
JB
9813 if (IS_HASWELL(dev))
9814 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9815 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9816
ebb69c95
CT
9817 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9818 pipe_config->pixel_multiplier =
9819 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9820 } else {
9821 pipe_config->pixel_multiplier = 1;
9822 }
6c49f241 9823
0e8ffe1b
DV
9824 return true;
9825}
9826
560b85bb
CW
9827static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9828{
9829 struct drm_device *dev = crtc->dev;
9830 struct drm_i915_private *dev_priv = dev->dev_private;
9831 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9832 uint32_t cntl = 0, size = 0;
560b85bb 9833
dc41c154 9834 if (base) {
3dd512fb
MR
9835 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9836 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
9837 unsigned int stride = roundup_pow_of_two(width) * 4;
9838
9839 switch (stride) {
9840 default:
9841 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9842 width, stride);
9843 stride = 256;
9844 /* fallthrough */
9845 case 256:
9846 case 512:
9847 case 1024:
9848 case 2048:
9849 break;
4b0e333e
CW
9850 }
9851
dc41c154
VS
9852 cntl |= CURSOR_ENABLE |
9853 CURSOR_GAMMA_ENABLE |
9854 CURSOR_FORMAT_ARGB |
9855 CURSOR_STRIDE(stride);
9856
9857 size = (height << 12) | width;
4b0e333e 9858 }
560b85bb 9859
dc41c154
VS
9860 if (intel_crtc->cursor_cntl != 0 &&
9861 (intel_crtc->cursor_base != base ||
9862 intel_crtc->cursor_size != size ||
9863 intel_crtc->cursor_cntl != cntl)) {
9864 /* On these chipsets we can only modify the base/size/stride
9865 * whilst the cursor is disabled.
9866 */
9867 I915_WRITE(_CURACNTR, 0);
4b0e333e 9868 POSTING_READ(_CURACNTR);
dc41c154 9869 intel_crtc->cursor_cntl = 0;
4b0e333e 9870 }
560b85bb 9871
99d1f387 9872 if (intel_crtc->cursor_base != base) {
9db4a9c7 9873 I915_WRITE(_CURABASE, base);
99d1f387
VS
9874 intel_crtc->cursor_base = base;
9875 }
4726e0b0 9876
dc41c154
VS
9877 if (intel_crtc->cursor_size != size) {
9878 I915_WRITE(CURSIZE, size);
9879 intel_crtc->cursor_size = size;
4b0e333e 9880 }
560b85bb 9881
4b0e333e 9882 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
9883 I915_WRITE(_CURACNTR, cntl);
9884 POSTING_READ(_CURACNTR);
4b0e333e 9885 intel_crtc->cursor_cntl = cntl;
560b85bb 9886 }
560b85bb
CW
9887}
9888
560b85bb 9889static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
9890{
9891 struct drm_device *dev = crtc->dev;
9892 struct drm_i915_private *dev_priv = dev->dev_private;
9893 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9894 int pipe = intel_crtc->pipe;
4b0e333e
CW
9895 uint32_t cntl;
9896
9897 cntl = 0;
9898 if (base) {
9899 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 9900 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
9901 case 64:
9902 cntl |= CURSOR_MODE_64_ARGB_AX;
9903 break;
9904 case 128:
9905 cntl |= CURSOR_MODE_128_ARGB_AX;
9906 break;
9907 case 256:
9908 cntl |= CURSOR_MODE_256_ARGB_AX;
9909 break;
9910 default:
3dd512fb 9911 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 9912 return;
65a21cd6 9913 }
4b0e333e 9914 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
9915
9916 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9917 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 9918 }
65a21cd6 9919
8e7d688b 9920 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
9921 cntl |= CURSOR_ROTATE_180;
9922
4b0e333e
CW
9923 if (intel_crtc->cursor_cntl != cntl) {
9924 I915_WRITE(CURCNTR(pipe), cntl);
9925 POSTING_READ(CURCNTR(pipe));
9926 intel_crtc->cursor_cntl = cntl;
65a21cd6 9927 }
4b0e333e 9928
65a21cd6 9929 /* and commit changes on next vblank */
5efb3e28
VS
9930 I915_WRITE(CURBASE(pipe), base);
9931 POSTING_READ(CURBASE(pipe));
99d1f387
VS
9932
9933 intel_crtc->cursor_base = base;
65a21cd6
JB
9934}
9935
cda4b7d3 9936/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
9937static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9938 bool on)
cda4b7d3
CW
9939{
9940 struct drm_device *dev = crtc->dev;
9941 struct drm_i915_private *dev_priv = dev->dev_private;
9942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9943 int pipe = intel_crtc->pipe;
3d7d6510
MR
9944 int x = crtc->cursor_x;
9945 int y = crtc->cursor_y;
d6e4db15 9946 u32 base = 0, pos = 0;
cda4b7d3 9947
d6e4db15 9948 if (on)
cda4b7d3 9949 base = intel_crtc->cursor_addr;
cda4b7d3 9950
6e3c9717 9951 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
9952 base = 0;
9953
6e3c9717 9954 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
9955 base = 0;
9956
9957 if (x < 0) {
3dd512fb 9958 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
cda4b7d3
CW
9959 base = 0;
9960
9961 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9962 x = -x;
9963 }
9964 pos |= x << CURSOR_X_SHIFT;
9965
9966 if (y < 0) {
3dd512fb 9967 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
cda4b7d3
CW
9968 base = 0;
9969
9970 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9971 y = -y;
9972 }
9973 pos |= y << CURSOR_Y_SHIFT;
9974
4b0e333e 9975 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
9976 return;
9977
5efb3e28
VS
9978 I915_WRITE(CURPOS(pipe), pos);
9979
4398ad45
VS
9980 /* ILK+ do this automagically */
9981 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 9982 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
3dd512fb
MR
9983 base += (intel_crtc->base.cursor->state->crtc_h *
9984 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
4398ad45
VS
9985 }
9986
8ac54669 9987 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
9988 i845_update_cursor(crtc, base);
9989 else
9990 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
9991}
9992
dc41c154
VS
9993static bool cursor_size_ok(struct drm_device *dev,
9994 uint32_t width, uint32_t height)
9995{
9996 if (width == 0 || height == 0)
9997 return false;
9998
9999 /*
10000 * 845g/865g are special in that they are only limited by
10001 * the width of their cursors, the height is arbitrary up to
10002 * the precision of the register. Everything else requires
10003 * square cursors, limited to a few power-of-two sizes.
10004 */
10005 if (IS_845G(dev) || IS_I865G(dev)) {
10006 if ((width & 63) != 0)
10007 return false;
10008
10009 if (width > (IS_845G(dev) ? 64 : 512))
10010 return false;
10011
10012 if (height > 1023)
10013 return false;
10014 } else {
10015 switch (width | height) {
10016 case 256:
10017 case 128:
10018 if (IS_GEN2(dev))
10019 return false;
10020 case 64:
10021 break;
10022 default:
10023 return false;
10024 }
10025 }
10026
10027 return true;
10028}
10029
79e53945 10030static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 10031 u16 *blue, uint32_t start, uint32_t size)
79e53945 10032{
7203425a 10033 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 10034 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 10035
7203425a 10036 for (i = start; i < end; i++) {
79e53945
JB
10037 intel_crtc->lut_r[i] = red[i] >> 8;
10038 intel_crtc->lut_g[i] = green[i] >> 8;
10039 intel_crtc->lut_b[i] = blue[i] >> 8;
10040 }
10041
10042 intel_crtc_load_lut(crtc);
10043}
10044
79e53945
JB
10045/* VESA 640x480x72Hz mode to set on the pipe */
10046static struct drm_display_mode load_detect_mode = {
10047 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10048 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10049};
10050
a8bb6818
DV
10051struct drm_framebuffer *
10052__intel_framebuffer_create(struct drm_device *dev,
10053 struct drm_mode_fb_cmd2 *mode_cmd,
10054 struct drm_i915_gem_object *obj)
d2dff872
CW
10055{
10056 struct intel_framebuffer *intel_fb;
10057 int ret;
10058
10059 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10060 if (!intel_fb) {
6ccb81f2 10061 drm_gem_object_unreference(&obj->base);
d2dff872
CW
10062 return ERR_PTR(-ENOMEM);
10063 }
10064
10065 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10066 if (ret)
10067 goto err;
d2dff872
CW
10068
10069 return &intel_fb->base;
dd4916c5 10070err:
6ccb81f2 10071 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
10072 kfree(intel_fb);
10073
10074 return ERR_PTR(ret);
d2dff872
CW
10075}
10076
b5ea642a 10077static struct drm_framebuffer *
a8bb6818
DV
10078intel_framebuffer_create(struct drm_device *dev,
10079 struct drm_mode_fb_cmd2 *mode_cmd,
10080 struct drm_i915_gem_object *obj)
10081{
10082 struct drm_framebuffer *fb;
10083 int ret;
10084
10085 ret = i915_mutex_lock_interruptible(dev);
10086 if (ret)
10087 return ERR_PTR(ret);
10088 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10089 mutex_unlock(&dev->struct_mutex);
10090
10091 return fb;
10092}
10093
d2dff872
CW
10094static u32
10095intel_framebuffer_pitch_for_width(int width, int bpp)
10096{
10097 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10098 return ALIGN(pitch, 64);
10099}
10100
10101static u32
10102intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10103{
10104 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10105 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10106}
10107
10108static struct drm_framebuffer *
10109intel_framebuffer_create_for_mode(struct drm_device *dev,
10110 struct drm_display_mode *mode,
10111 int depth, int bpp)
10112{
10113 struct drm_i915_gem_object *obj;
0fed39bd 10114 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10115
10116 obj = i915_gem_alloc_object(dev,
10117 intel_framebuffer_size_for_mode(mode, bpp));
10118 if (obj == NULL)
10119 return ERR_PTR(-ENOMEM);
10120
10121 mode_cmd.width = mode->hdisplay;
10122 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10123 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10124 bpp);
5ca0c34a 10125 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
10126
10127 return intel_framebuffer_create(dev, &mode_cmd, obj);
10128}
10129
10130static struct drm_framebuffer *
10131mode_fits_in_fbdev(struct drm_device *dev,
10132 struct drm_display_mode *mode)
10133{
4520f53a 10134#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
10135 struct drm_i915_private *dev_priv = dev->dev_private;
10136 struct drm_i915_gem_object *obj;
10137 struct drm_framebuffer *fb;
10138
4c0e5528 10139 if (!dev_priv->fbdev)
d2dff872
CW
10140 return NULL;
10141
4c0e5528 10142 if (!dev_priv->fbdev->fb)
d2dff872
CW
10143 return NULL;
10144
4c0e5528
DV
10145 obj = dev_priv->fbdev->fb->obj;
10146 BUG_ON(!obj);
10147
8bcd4553 10148 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10149 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10150 fb->bits_per_pixel))
d2dff872
CW
10151 return NULL;
10152
01f2c773 10153 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10154 return NULL;
10155
10156 return fb;
4520f53a
DV
10157#else
10158 return NULL;
10159#endif
d2dff872
CW
10160}
10161
d3a40d1b
ACO
10162static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10163 struct drm_crtc *crtc,
10164 struct drm_display_mode *mode,
10165 struct drm_framebuffer *fb,
10166 int x, int y)
10167{
10168 struct drm_plane_state *plane_state;
10169 int hdisplay, vdisplay;
10170 int ret;
10171
10172 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10173 if (IS_ERR(plane_state))
10174 return PTR_ERR(plane_state);
10175
10176 if (mode)
10177 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10178 else
10179 hdisplay = vdisplay = 0;
10180
10181 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10182 if (ret)
10183 return ret;
10184 drm_atomic_set_fb_for_plane(plane_state, fb);
10185 plane_state->crtc_x = 0;
10186 plane_state->crtc_y = 0;
10187 plane_state->crtc_w = hdisplay;
10188 plane_state->crtc_h = vdisplay;
10189 plane_state->src_x = x << 16;
10190 plane_state->src_y = y << 16;
10191 plane_state->src_w = hdisplay << 16;
10192 plane_state->src_h = vdisplay << 16;
10193
10194 return 0;
10195}
10196
d2434ab7 10197bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10198 struct drm_display_mode *mode,
51fd371b
RC
10199 struct intel_load_detect_pipe *old,
10200 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10201{
10202 struct intel_crtc *intel_crtc;
d2434ab7
DV
10203 struct intel_encoder *intel_encoder =
10204 intel_attached_encoder(connector);
79e53945 10205 struct drm_crtc *possible_crtc;
4ef69c7a 10206 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10207 struct drm_crtc *crtc = NULL;
10208 struct drm_device *dev = encoder->dev;
94352cf9 10209 struct drm_framebuffer *fb;
51fd371b 10210 struct drm_mode_config *config = &dev->mode_config;
83a57153 10211 struct drm_atomic_state *state = NULL;
944b0c76 10212 struct drm_connector_state *connector_state;
4be07317 10213 struct intel_crtc_state *crtc_state;
51fd371b 10214 int ret, i = -1;
79e53945 10215
d2dff872 10216 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10217 connector->base.id, connector->name,
8e329a03 10218 encoder->base.id, encoder->name);
d2dff872 10219
51fd371b
RC
10220retry:
10221 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10222 if (ret)
10223 goto fail_unlock;
6e9f798d 10224
79e53945
JB
10225 /*
10226 * Algorithm gets a little messy:
7a5e4805 10227 *
79e53945
JB
10228 * - if the connector already has an assigned crtc, use it (but make
10229 * sure it's on first)
7a5e4805 10230 *
79e53945
JB
10231 * - try to find the first unused crtc that can drive this connector,
10232 * and use that if we find one
79e53945
JB
10233 */
10234
10235 /* See if we already have a CRTC for this connector */
10236 if (encoder->crtc) {
10237 crtc = encoder->crtc;
8261b191 10238
51fd371b 10239 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de
DV
10240 if (ret)
10241 goto fail_unlock;
10242 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b
RC
10243 if (ret)
10244 goto fail_unlock;
7b24056b 10245
24218aac 10246 old->dpms_mode = connector->dpms;
8261b191
CW
10247 old->load_detect_temp = false;
10248
10249 /* Make sure the crtc and connector are running */
24218aac
DV
10250 if (connector->dpms != DRM_MODE_DPMS_ON)
10251 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 10252
7173188d 10253 return true;
79e53945
JB
10254 }
10255
10256 /* Find an unused one (if possible) */
70e1e0ec 10257 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10258 i++;
10259 if (!(encoder->possible_crtcs & (1 << i)))
10260 continue;
83d65738 10261 if (possible_crtc->state->enable)
a459249c
VS
10262 continue;
10263 /* This can occur when applying the pipe A quirk on resume. */
10264 if (to_intel_crtc(possible_crtc)->new_enabled)
10265 continue;
10266
10267 crtc = possible_crtc;
10268 break;
79e53945
JB
10269 }
10270
10271 /*
10272 * If we didn't find an unused CRTC, don't use any.
10273 */
10274 if (!crtc) {
7173188d 10275 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 10276 goto fail_unlock;
79e53945
JB
10277 }
10278
51fd371b
RC
10279 ret = drm_modeset_lock(&crtc->mutex, ctx);
10280 if (ret)
4d02e2de
DV
10281 goto fail_unlock;
10282 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10283 if (ret)
51fd371b 10284 goto fail_unlock;
fc303101
DV
10285 intel_encoder->new_crtc = to_intel_crtc(crtc);
10286 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
10287
10288 intel_crtc = to_intel_crtc(crtc);
412b61d8 10289 intel_crtc->new_enabled = true;
24218aac 10290 old->dpms_mode = connector->dpms;
8261b191 10291 old->load_detect_temp = true;
d2dff872 10292 old->release_fb = NULL;
79e53945 10293
83a57153
ACO
10294 state = drm_atomic_state_alloc(dev);
10295 if (!state)
10296 return false;
10297
10298 state->acquire_ctx = ctx;
10299
944b0c76
ACO
10300 connector_state = drm_atomic_get_connector_state(state, connector);
10301 if (IS_ERR(connector_state)) {
10302 ret = PTR_ERR(connector_state);
10303 goto fail;
10304 }
10305
10306 connector_state->crtc = crtc;
10307 connector_state->best_encoder = &intel_encoder->base;
10308
4be07317
ACO
10309 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10310 if (IS_ERR(crtc_state)) {
10311 ret = PTR_ERR(crtc_state);
10312 goto fail;
10313 }
10314
49d6fa21 10315 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10316
6492711d
CW
10317 if (!mode)
10318 mode = &load_detect_mode;
79e53945 10319
d2dff872
CW
10320 /* We need a framebuffer large enough to accommodate all accesses
10321 * that the plane may generate whilst we perform load detection.
10322 * We can not rely on the fbcon either being present (we get called
10323 * during its initialisation to detect all boot displays, or it may
10324 * not even exist) or that it is large enough to satisfy the
10325 * requested mode.
10326 */
94352cf9
DV
10327 fb = mode_fits_in_fbdev(dev, mode);
10328 if (fb == NULL) {
d2dff872 10329 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
10330 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10331 old->release_fb = fb;
d2dff872
CW
10332 } else
10333 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10334 if (IS_ERR(fb)) {
d2dff872 10335 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10336 goto fail;
79e53945 10337 }
79e53945 10338
d3a40d1b
ACO
10339 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10340 if (ret)
10341 goto fail;
10342
8c7b5ccb
ACO
10343 drm_mode_copy(&crtc_state->base.mode, mode);
10344
568c634a 10345 if (intel_set_mode(state)) {
6492711d 10346 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
10347 if (old->release_fb)
10348 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 10349 goto fail;
79e53945 10350 }
9128b040 10351 crtc->primary->crtc = crtc;
7173188d 10352
79e53945 10353 /* let the connector get through one full cycle before testing */
9d0498a2 10354 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10355 return true;
412b61d8
VS
10356
10357 fail:
83d65738 10358 intel_crtc->new_enabled = crtc->state->enable;
51fd371b 10359fail_unlock:
e5d958ef
ACO
10360 drm_atomic_state_free(state);
10361 state = NULL;
83a57153 10362
51fd371b
RC
10363 if (ret == -EDEADLK) {
10364 drm_modeset_backoff(ctx);
10365 goto retry;
10366 }
10367
412b61d8 10368 return false;
79e53945
JB
10369}
10370
d2434ab7 10371void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10372 struct intel_load_detect_pipe *old,
10373 struct drm_modeset_acquire_ctx *ctx)
79e53945 10374{
83a57153 10375 struct drm_device *dev = connector->dev;
d2434ab7
DV
10376 struct intel_encoder *intel_encoder =
10377 intel_attached_encoder(connector);
4ef69c7a 10378 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 10379 struct drm_crtc *crtc = encoder->crtc;
412b61d8 10380 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83a57153 10381 struct drm_atomic_state *state;
944b0c76 10382 struct drm_connector_state *connector_state;
4be07317 10383 struct intel_crtc_state *crtc_state;
d3a40d1b 10384 int ret;
79e53945 10385
d2dff872 10386 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10387 connector->base.id, connector->name,
8e329a03 10388 encoder->base.id, encoder->name);
d2dff872 10389
8261b191 10390 if (old->load_detect_temp) {
83a57153 10391 state = drm_atomic_state_alloc(dev);
944b0c76
ACO
10392 if (!state)
10393 goto fail;
83a57153
ACO
10394
10395 state->acquire_ctx = ctx;
10396
944b0c76
ACO
10397 connector_state = drm_atomic_get_connector_state(state, connector);
10398 if (IS_ERR(connector_state))
10399 goto fail;
10400
4be07317
ACO
10401 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10402 if (IS_ERR(crtc_state))
10403 goto fail;
10404
fc303101
DV
10405 to_intel_connector(connector)->new_encoder = NULL;
10406 intel_encoder->new_crtc = NULL;
412b61d8 10407 intel_crtc->new_enabled = false;
944b0c76
ACO
10408
10409 connector_state->best_encoder = NULL;
10410 connector_state->crtc = NULL;
10411
49d6fa21 10412 crtc_state->base.enable = crtc_state->base.active = false;
4be07317 10413
d3a40d1b
ACO
10414 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10415 0, 0);
10416 if (ret)
10417 goto fail;
10418
568c634a 10419 ret = intel_set_mode(state);
2bfb4627
ACO
10420 if (ret)
10421 goto fail;
d2dff872 10422
36206361
DV
10423 if (old->release_fb) {
10424 drm_framebuffer_unregister_private(old->release_fb);
10425 drm_framebuffer_unreference(old->release_fb);
10426 }
d2dff872 10427
0622a53c 10428 return;
79e53945
JB
10429 }
10430
c751ce4f 10431 /* Switch crtc and encoder back off if necessary */
24218aac
DV
10432 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10433 connector->funcs->dpms(connector, old->dpms_mode);
944b0c76
ACO
10434
10435 return;
10436fail:
10437 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10438 drm_atomic_state_free(state);
79e53945
JB
10439}
10440
da4a1efa 10441static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10442 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10443{
10444 struct drm_i915_private *dev_priv = dev->dev_private;
10445 u32 dpll = pipe_config->dpll_hw_state.dpll;
10446
10447 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10448 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10449 else if (HAS_PCH_SPLIT(dev))
10450 return 120000;
10451 else if (!IS_GEN2(dev))
10452 return 96000;
10453 else
10454 return 48000;
10455}
10456
79e53945 10457/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10458static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10459 struct intel_crtc_state *pipe_config)
79e53945 10460{
f1f644dc 10461 struct drm_device *dev = crtc->base.dev;
79e53945 10462 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10463 int pipe = pipe_config->cpu_transcoder;
293623f7 10464 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10465 u32 fp;
10466 intel_clock_t clock;
da4a1efa 10467 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10468
10469 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10470 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10471 else
293623f7 10472 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10473
10474 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10475 if (IS_PINEVIEW(dev)) {
10476 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10477 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10478 } else {
10479 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10480 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10481 }
10482
a6c45cf0 10483 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10484 if (IS_PINEVIEW(dev))
10485 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10486 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10487 else
10488 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10489 DPLL_FPA01_P1_POST_DIV_SHIFT);
10490
10491 switch (dpll & DPLL_MODE_MASK) {
10492 case DPLLB_MODE_DAC_SERIAL:
10493 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10494 5 : 10;
10495 break;
10496 case DPLLB_MODE_LVDS:
10497 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10498 7 : 14;
10499 break;
10500 default:
28c97730 10501 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10502 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10503 return;
79e53945
JB
10504 }
10505
ac58c3f0 10506 if (IS_PINEVIEW(dev))
da4a1efa 10507 pineview_clock(refclk, &clock);
ac58c3f0 10508 else
da4a1efa 10509 i9xx_clock(refclk, &clock);
79e53945 10510 } else {
0fb58223 10511 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10512 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10513
10514 if (is_lvds) {
10515 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10516 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10517
10518 if (lvds & LVDS_CLKB_POWER_UP)
10519 clock.p2 = 7;
10520 else
10521 clock.p2 = 14;
79e53945
JB
10522 } else {
10523 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10524 clock.p1 = 2;
10525 else {
10526 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10527 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10528 }
10529 if (dpll & PLL_P2_DIVIDE_BY_4)
10530 clock.p2 = 4;
10531 else
10532 clock.p2 = 2;
79e53945 10533 }
da4a1efa
VS
10534
10535 i9xx_clock(refclk, &clock);
79e53945
JB
10536 }
10537
18442d08
VS
10538 /*
10539 * This value includes pixel_multiplier. We will use
241bfc38 10540 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10541 * encoder's get_config() function.
10542 */
10543 pipe_config->port_clock = clock.dot;
f1f644dc
JB
10544}
10545
6878da05
VS
10546int intel_dotclock_calculate(int link_freq,
10547 const struct intel_link_m_n *m_n)
f1f644dc 10548{
f1f644dc
JB
10549 /*
10550 * The calculation for the data clock is:
1041a02f 10551 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10552 * But we want to avoid losing precison if possible, so:
1041a02f 10553 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10554 *
10555 * and the link clock is simpler:
1041a02f 10556 * link_clock = (m * link_clock) / n
f1f644dc
JB
10557 */
10558
6878da05
VS
10559 if (!m_n->link_n)
10560 return 0;
f1f644dc 10561
6878da05
VS
10562 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10563}
f1f644dc 10564
18442d08 10565static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10566 struct intel_crtc_state *pipe_config)
6878da05
VS
10567{
10568 struct drm_device *dev = crtc->base.dev;
79e53945 10569
18442d08
VS
10570 /* read out port_clock from the DPLL */
10571 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10572
f1f644dc 10573 /*
18442d08 10574 * This value does not include pixel_multiplier.
241bfc38 10575 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
10576 * agree once we know their relationship in the encoder's
10577 * get_config() function.
79e53945 10578 */
2d112de7 10579 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
10580 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10581 &pipe_config->fdi_m_n);
79e53945
JB
10582}
10583
10584/** Returns the currently programmed mode of the given pipe. */
10585struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10586 struct drm_crtc *crtc)
10587{
548f245b 10588 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10589 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10590 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10591 struct drm_display_mode *mode;
5cec258b 10592 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
10593 int htot = I915_READ(HTOTAL(cpu_transcoder));
10594 int hsync = I915_READ(HSYNC(cpu_transcoder));
10595 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10596 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10597 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10598
10599 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10600 if (!mode)
10601 return NULL;
10602
f1f644dc
JB
10603 /*
10604 * Construct a pipe_config sufficient for getting the clock info
10605 * back out of crtc_clock_get.
10606 *
10607 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10608 * to use a real value here instead.
10609 */
293623f7 10610 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 10611 pipe_config.pixel_multiplier = 1;
293623f7
VS
10612 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10613 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10614 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
10615 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10616
773ae034 10617 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
10618 mode->hdisplay = (htot & 0xffff) + 1;
10619 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10620 mode->hsync_start = (hsync & 0xffff) + 1;
10621 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10622 mode->vdisplay = (vtot & 0xffff) + 1;
10623 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10624 mode->vsync_start = (vsync & 0xffff) + 1;
10625 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10626
10627 drm_mode_set_name(mode);
79e53945
JB
10628
10629 return mode;
10630}
10631
f047e395
CW
10632void intel_mark_busy(struct drm_device *dev)
10633{
c67a470b
PZ
10634 struct drm_i915_private *dev_priv = dev->dev_private;
10635
f62a0076
CW
10636 if (dev_priv->mm.busy)
10637 return;
10638
43694d69 10639 intel_runtime_pm_get(dev_priv);
c67a470b 10640 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10641 if (INTEL_INFO(dev)->gen >= 6)
10642 gen6_rps_busy(dev_priv);
f62a0076 10643 dev_priv->mm.busy = true;
f047e395
CW
10644}
10645
10646void intel_mark_idle(struct drm_device *dev)
652c393a 10647{
c67a470b 10648 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10649
f62a0076
CW
10650 if (!dev_priv->mm.busy)
10651 return;
10652
10653 dev_priv->mm.busy = false;
10654
3d13ef2e 10655 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10656 gen6_rps_idle(dev->dev_private);
bb4cdd53 10657
43694d69 10658 intel_runtime_pm_put(dev_priv);
652c393a
JB
10659}
10660
79e53945
JB
10661static void intel_crtc_destroy(struct drm_crtc *crtc)
10662{
10663 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10664 struct drm_device *dev = crtc->dev;
10665 struct intel_unpin_work *work;
67e77c5a 10666
5e2d7afc 10667 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10668 work = intel_crtc->unpin_work;
10669 intel_crtc->unpin_work = NULL;
5e2d7afc 10670 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10671
10672 if (work) {
10673 cancel_work_sync(&work->work);
10674 kfree(work);
10675 }
79e53945
JB
10676
10677 drm_crtc_cleanup(crtc);
67e77c5a 10678
79e53945
JB
10679 kfree(intel_crtc);
10680}
10681
6b95a207
KH
10682static void intel_unpin_work_fn(struct work_struct *__work)
10683{
10684 struct intel_unpin_work *work =
10685 container_of(__work, struct intel_unpin_work, work);
a9ff8714
VS
10686 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10687 struct drm_device *dev = crtc->base.dev;
10688 struct drm_plane *primary = crtc->base.primary;
6b95a207 10689
b4a98e57 10690 mutex_lock(&dev->struct_mutex);
a9ff8714 10691 intel_unpin_fb_obj(work->old_fb, primary->state);
05394f39 10692 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10693
7ff0ebcc 10694 intel_fbc_update(dev);
f06cc1b9
JH
10695
10696 if (work->flip_queued_req)
146d84f0 10697 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10698 mutex_unlock(&dev->struct_mutex);
10699
a9ff8714 10700 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
89ed88ba 10701 drm_framebuffer_unreference(work->old_fb);
f99d7069 10702
a9ff8714
VS
10703 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10704 atomic_dec(&crtc->unpin_work_count);
b4a98e57 10705
6b95a207
KH
10706 kfree(work);
10707}
10708
1afe3e9d 10709static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10710 struct drm_crtc *crtc)
6b95a207 10711{
6b95a207
KH
10712 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10713 struct intel_unpin_work *work;
6b95a207
KH
10714 unsigned long flags;
10715
10716 /* Ignore early vblank irqs */
10717 if (intel_crtc == NULL)
10718 return;
10719
f326038a
DV
10720 /*
10721 * This is called both by irq handlers and the reset code (to complete
10722 * lost pageflips) so needs the full irqsave spinlocks.
10723 */
6b95a207
KH
10724 spin_lock_irqsave(&dev->event_lock, flags);
10725 work = intel_crtc->unpin_work;
e7d841ca
CW
10726
10727 /* Ensure we don't miss a work->pending update ... */
10728 smp_rmb();
10729
10730 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10731 spin_unlock_irqrestore(&dev->event_lock, flags);
10732 return;
10733 }
10734
d6bbafa1 10735 page_flip_completed(intel_crtc);
0af7e4df 10736
6b95a207 10737 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10738}
10739
1afe3e9d
JB
10740void intel_finish_page_flip(struct drm_device *dev, int pipe)
10741{
fbee40df 10742 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10743 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10744
49b14a5c 10745 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10746}
10747
10748void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10749{
fbee40df 10750 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10751 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10752
49b14a5c 10753 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10754}
10755
75f7f3ec
VS
10756/* Is 'a' after or equal to 'b'? */
10757static bool g4x_flip_count_after_eq(u32 a, u32 b)
10758{
10759 return !((a - b) & 0x80000000);
10760}
10761
10762static bool page_flip_finished(struct intel_crtc *crtc)
10763{
10764 struct drm_device *dev = crtc->base.dev;
10765 struct drm_i915_private *dev_priv = dev->dev_private;
10766
bdfa7542
VS
10767 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10768 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10769 return true;
10770
75f7f3ec
VS
10771 /*
10772 * The relevant registers doen't exist on pre-ctg.
10773 * As the flip done interrupt doesn't trigger for mmio
10774 * flips on gmch platforms, a flip count check isn't
10775 * really needed there. But since ctg has the registers,
10776 * include it in the check anyway.
10777 */
10778 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10779 return true;
10780
10781 /*
10782 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10783 * used the same base address. In that case the mmio flip might
10784 * have completed, but the CS hasn't even executed the flip yet.
10785 *
10786 * A flip count check isn't enough as the CS might have updated
10787 * the base address just after start of vblank, but before we
10788 * managed to process the interrupt. This means we'd complete the
10789 * CS flip too soon.
10790 *
10791 * Combining both checks should get us a good enough result. It may
10792 * still happen that the CS flip has been executed, but has not
10793 * yet actually completed. But in case the base address is the same
10794 * anyway, we don't really care.
10795 */
10796 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10797 crtc->unpin_work->gtt_offset &&
10798 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10799 crtc->unpin_work->flip_count);
10800}
10801
6b95a207
KH
10802void intel_prepare_page_flip(struct drm_device *dev, int plane)
10803{
fbee40df 10804 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10805 struct intel_crtc *intel_crtc =
10806 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10807 unsigned long flags;
10808
f326038a
DV
10809
10810 /*
10811 * This is called both by irq handlers and the reset code (to complete
10812 * lost pageflips) so needs the full irqsave spinlocks.
10813 *
10814 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10815 * generate a page-flip completion irq, i.e. every modeset
10816 * is also accompanied by a spurious intel_prepare_page_flip().
10817 */
6b95a207 10818 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10819 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10820 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10821 spin_unlock_irqrestore(&dev->event_lock, flags);
10822}
10823
eba905b2 10824static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
10825{
10826 /* Ensure that the work item is consistent when activating it ... */
10827 smp_wmb();
10828 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10829 /* and that it is marked active as soon as the irq could fire. */
10830 smp_wmb();
10831}
10832
8c9f3aaf
JB
10833static int intel_gen2_queue_flip(struct drm_device *dev,
10834 struct drm_crtc *crtc,
10835 struct drm_framebuffer *fb,
ed8d1975 10836 struct drm_i915_gem_object *obj,
6258fbe2 10837 struct drm_i915_gem_request *req,
ed8d1975 10838 uint32_t flags)
8c9f3aaf 10839{
6258fbe2 10840 struct intel_engine_cs *ring = req->ring;
8c9f3aaf 10841 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10842 u32 flip_mask;
10843 int ret;
10844
5fb9de1a 10845 ret = intel_ring_begin(req, 6);
8c9f3aaf 10846 if (ret)
4fa62c89 10847 return ret;
8c9f3aaf
JB
10848
10849 /* Can't queue multiple flips, so wait for the previous
10850 * one to finish before executing the next.
10851 */
10852 if (intel_crtc->plane)
10853 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10854 else
10855 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10856 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10857 intel_ring_emit(ring, MI_NOOP);
10858 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10859 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10860 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10861 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 10862 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
10863
10864 intel_mark_page_flip_active(intel_crtc);
83d4092b 10865 return 0;
8c9f3aaf
JB
10866}
10867
10868static int intel_gen3_queue_flip(struct drm_device *dev,
10869 struct drm_crtc *crtc,
10870 struct drm_framebuffer *fb,
ed8d1975 10871 struct drm_i915_gem_object *obj,
6258fbe2 10872 struct drm_i915_gem_request *req,
ed8d1975 10873 uint32_t flags)
8c9f3aaf 10874{
6258fbe2 10875 struct intel_engine_cs *ring = req->ring;
8c9f3aaf 10876 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10877 u32 flip_mask;
10878 int ret;
10879
5fb9de1a 10880 ret = intel_ring_begin(req, 6);
8c9f3aaf 10881 if (ret)
4fa62c89 10882 return ret;
8c9f3aaf
JB
10883
10884 if (intel_crtc->plane)
10885 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10886 else
10887 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10888 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10889 intel_ring_emit(ring, MI_NOOP);
10890 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10891 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10892 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10893 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
10894 intel_ring_emit(ring, MI_NOOP);
10895
e7d841ca 10896 intel_mark_page_flip_active(intel_crtc);
83d4092b 10897 return 0;
8c9f3aaf
JB
10898}
10899
10900static int intel_gen4_queue_flip(struct drm_device *dev,
10901 struct drm_crtc *crtc,
10902 struct drm_framebuffer *fb,
ed8d1975 10903 struct drm_i915_gem_object *obj,
6258fbe2 10904 struct drm_i915_gem_request *req,
ed8d1975 10905 uint32_t flags)
8c9f3aaf 10906{
6258fbe2 10907 struct intel_engine_cs *ring = req->ring;
8c9f3aaf
JB
10908 struct drm_i915_private *dev_priv = dev->dev_private;
10909 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10910 uint32_t pf, pipesrc;
10911 int ret;
10912
5fb9de1a 10913 ret = intel_ring_begin(req, 4);
8c9f3aaf 10914 if (ret)
4fa62c89 10915 return ret;
8c9f3aaf
JB
10916
10917 /* i965+ uses the linear or tiled offsets from the
10918 * Display Registers (which do not change across a page-flip)
10919 * so we need only reprogram the base address.
10920 */
6d90c952
DV
10921 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10922 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10923 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10924 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 10925 obj->tiling_mode);
8c9f3aaf
JB
10926
10927 /* XXX Enabling the panel-fitter across page-flip is so far
10928 * untested on non-native modes, so ignore it for now.
10929 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10930 */
10931 pf = 0;
10932 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10933 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10934
10935 intel_mark_page_flip_active(intel_crtc);
83d4092b 10936 return 0;
8c9f3aaf
JB
10937}
10938
10939static int intel_gen6_queue_flip(struct drm_device *dev,
10940 struct drm_crtc *crtc,
10941 struct drm_framebuffer *fb,
ed8d1975 10942 struct drm_i915_gem_object *obj,
6258fbe2 10943 struct drm_i915_gem_request *req,
ed8d1975 10944 uint32_t flags)
8c9f3aaf 10945{
6258fbe2 10946 struct intel_engine_cs *ring = req->ring;
8c9f3aaf
JB
10947 struct drm_i915_private *dev_priv = dev->dev_private;
10948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10949 uint32_t pf, pipesrc;
10950 int ret;
10951
5fb9de1a 10952 ret = intel_ring_begin(req, 4);
8c9f3aaf 10953 if (ret)
4fa62c89 10954 return ret;
8c9f3aaf 10955
6d90c952
DV
10956 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10957 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10958 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 10959 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 10960
dc257cf1
DV
10961 /* Contrary to the suggestions in the documentation,
10962 * "Enable Panel Fitter" does not seem to be required when page
10963 * flipping with a non-native mode, and worse causes a normal
10964 * modeset to fail.
10965 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10966 */
10967 pf = 0;
8c9f3aaf 10968 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10969 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10970
10971 intel_mark_page_flip_active(intel_crtc);
83d4092b 10972 return 0;
8c9f3aaf
JB
10973}
10974
7c9017e5
JB
10975static int intel_gen7_queue_flip(struct drm_device *dev,
10976 struct drm_crtc *crtc,
10977 struct drm_framebuffer *fb,
ed8d1975 10978 struct drm_i915_gem_object *obj,
6258fbe2 10979 struct drm_i915_gem_request *req,
ed8d1975 10980 uint32_t flags)
7c9017e5 10981{
6258fbe2 10982 struct intel_engine_cs *ring = req->ring;
7c9017e5 10983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 10984 uint32_t plane_bit = 0;
ffe74d75
CW
10985 int len, ret;
10986
eba905b2 10987 switch (intel_crtc->plane) {
cb05d8de
DV
10988 case PLANE_A:
10989 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10990 break;
10991 case PLANE_B:
10992 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10993 break;
10994 case PLANE_C:
10995 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10996 break;
10997 default:
10998 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 10999 return -ENODEV;
cb05d8de
DV
11000 }
11001
ffe74d75 11002 len = 4;
f476828a 11003 if (ring->id == RCS) {
ffe74d75 11004 len += 6;
f476828a
DL
11005 /*
11006 * On Gen 8, SRM is now taking an extra dword to accommodate
11007 * 48bits addresses, and we need a NOOP for the batch size to
11008 * stay even.
11009 */
11010 if (IS_GEN8(dev))
11011 len += 2;
11012 }
ffe74d75 11013
f66fab8e
VS
11014 /*
11015 * BSpec MI_DISPLAY_FLIP for IVB:
11016 * "The full packet must be contained within the same cache line."
11017 *
11018 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11019 * cacheline, if we ever start emitting more commands before
11020 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11021 * then do the cacheline alignment, and finally emit the
11022 * MI_DISPLAY_FLIP.
11023 */
bba09b12 11024 ret = intel_ring_cacheline_align(req);
f66fab8e 11025 if (ret)
4fa62c89 11026 return ret;
f66fab8e 11027
5fb9de1a 11028 ret = intel_ring_begin(req, len);
7c9017e5 11029 if (ret)
4fa62c89 11030 return ret;
7c9017e5 11031
ffe74d75
CW
11032 /* Unmask the flip-done completion message. Note that the bspec says that
11033 * we should do this for both the BCS and RCS, and that we must not unmask
11034 * more than one flip event at any time (or ensure that one flip message
11035 * can be sent by waiting for flip-done prior to queueing new flips).
11036 * Experimentation says that BCS works despite DERRMR masking all
11037 * flip-done completion events and that unmasking all planes at once
11038 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11039 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11040 */
11041 if (ring->id == RCS) {
11042 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11043 intel_ring_emit(ring, DERRMR);
11044 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11045 DERRMR_PIPEB_PRI_FLIP_DONE |
11046 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
11047 if (IS_GEN8(dev))
11048 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11049 MI_SRM_LRM_GLOBAL_GTT);
11050 else
11051 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11052 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
11053 intel_ring_emit(ring, DERRMR);
11054 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
11055 if (IS_GEN8(dev)) {
11056 intel_ring_emit(ring, 0);
11057 intel_ring_emit(ring, MI_NOOP);
11058 }
ffe74d75
CW
11059 }
11060
cb05d8de 11061 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 11062 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 11063 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 11064 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
11065
11066 intel_mark_page_flip_active(intel_crtc);
83d4092b 11067 return 0;
7c9017e5
JB
11068}
11069
84c33a64
SG
11070static bool use_mmio_flip(struct intel_engine_cs *ring,
11071 struct drm_i915_gem_object *obj)
11072{
11073 /*
11074 * This is not being used for older platforms, because
11075 * non-availability of flip done interrupt forces us to use
11076 * CS flips. Older platforms derive flip done using some clever
11077 * tricks involving the flip_pending status bits and vblank irqs.
11078 * So using MMIO flips there would disrupt this mechanism.
11079 */
11080
8e09bf83
CW
11081 if (ring == NULL)
11082 return true;
11083
84c33a64
SG
11084 if (INTEL_INFO(ring->dev)->gen < 5)
11085 return false;
11086
11087 if (i915.use_mmio_flip < 0)
11088 return false;
11089 else if (i915.use_mmio_flip > 0)
11090 return true;
14bf993e
OM
11091 else if (i915.enable_execlists)
11092 return true;
84c33a64 11093 else
b4716185 11094 return ring != i915_gem_request_get_ring(obj->last_write_req);
84c33a64
SG
11095}
11096
ff944564
DL
11097static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11098{
11099 struct drm_device *dev = intel_crtc->base.dev;
11100 struct drm_i915_private *dev_priv = dev->dev_private;
11101 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564
DL
11102 const enum pipe pipe = intel_crtc->pipe;
11103 u32 ctl, stride;
11104
11105 ctl = I915_READ(PLANE_CTL(pipe, 0));
11106 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11107 switch (fb->modifier[0]) {
11108 case DRM_FORMAT_MOD_NONE:
11109 break;
11110 case I915_FORMAT_MOD_X_TILED:
ff944564 11111 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11112 break;
11113 case I915_FORMAT_MOD_Y_TILED:
11114 ctl |= PLANE_CTL_TILED_Y;
11115 break;
11116 case I915_FORMAT_MOD_Yf_TILED:
11117 ctl |= PLANE_CTL_TILED_YF;
11118 break;
11119 default:
11120 MISSING_CASE(fb->modifier[0]);
11121 }
ff944564
DL
11122
11123 /*
11124 * The stride is either expressed as a multiple of 64 bytes chunks for
11125 * linear buffers or in number of tiles for tiled buffers.
11126 */
2ebef630
TU
11127 stride = fb->pitches[0] /
11128 intel_fb_stride_alignment(dev, fb->modifier[0],
11129 fb->pixel_format);
ff944564
DL
11130
11131 /*
11132 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11133 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11134 */
11135 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11136 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11137
11138 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11139 POSTING_READ(PLANE_SURF(pipe, 0));
11140}
11141
11142static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
11143{
11144 struct drm_device *dev = intel_crtc->base.dev;
11145 struct drm_i915_private *dev_priv = dev->dev_private;
11146 struct intel_framebuffer *intel_fb =
11147 to_intel_framebuffer(intel_crtc->base.primary->fb);
11148 struct drm_i915_gem_object *obj = intel_fb->obj;
11149 u32 dspcntr;
11150 u32 reg;
11151
84c33a64
SG
11152 reg = DSPCNTR(intel_crtc->plane);
11153 dspcntr = I915_READ(reg);
11154
c5d97472
DL
11155 if (obj->tiling_mode != I915_TILING_NONE)
11156 dspcntr |= DISPPLANE_TILED;
11157 else
11158 dspcntr &= ~DISPPLANE_TILED;
11159
84c33a64
SG
11160 I915_WRITE(reg, dspcntr);
11161
11162 I915_WRITE(DSPSURF(intel_crtc->plane),
11163 intel_crtc->unpin_work->gtt_offset);
11164 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 11165
ff944564
DL
11166}
11167
11168/*
11169 * XXX: This is the temporary way to update the plane registers until we get
11170 * around to using the usual plane update functions for MMIO flips
11171 */
11172static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11173{
11174 struct drm_device *dev = intel_crtc->base.dev;
11175 bool atomic_update;
11176 u32 start_vbl_count;
11177
11178 intel_mark_page_flip_active(intel_crtc);
11179
11180 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11181
11182 if (INTEL_INFO(dev)->gen >= 9)
11183 skl_do_mmio_flip(intel_crtc);
11184 else
11185 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11186 ilk_do_mmio_flip(intel_crtc);
11187
9362c7c5
ACO
11188 if (atomic_update)
11189 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
11190}
11191
9362c7c5 11192static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11193{
b2cfe0ab
CW
11194 struct intel_mmio_flip *mmio_flip =
11195 container_of(work, struct intel_mmio_flip, work);
84c33a64 11196
eed29a5b
DV
11197 if (mmio_flip->req)
11198 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11199 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11200 false, NULL,
11201 &mmio_flip->i915->rps.mmioflips));
84c33a64 11202
b2cfe0ab
CW
11203 intel_do_mmio_flip(mmio_flip->crtc);
11204
eed29a5b 11205 i915_gem_request_unreference__unlocked(mmio_flip->req);
b2cfe0ab 11206 kfree(mmio_flip);
84c33a64
SG
11207}
11208
11209static int intel_queue_mmio_flip(struct drm_device *dev,
11210 struct drm_crtc *crtc,
11211 struct drm_framebuffer *fb,
11212 struct drm_i915_gem_object *obj,
11213 struct intel_engine_cs *ring,
11214 uint32_t flags)
11215{
b2cfe0ab
CW
11216 struct intel_mmio_flip *mmio_flip;
11217
11218 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11219 if (mmio_flip == NULL)
11220 return -ENOMEM;
84c33a64 11221
bcafc4e3 11222 mmio_flip->i915 = to_i915(dev);
eed29a5b 11223 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11224 mmio_flip->crtc = to_intel_crtc(crtc);
536f5b5e 11225
b2cfe0ab
CW
11226 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11227 schedule_work(&mmio_flip->work);
84c33a64 11228
84c33a64
SG
11229 return 0;
11230}
11231
8c9f3aaf
JB
11232static int intel_default_queue_flip(struct drm_device *dev,
11233 struct drm_crtc *crtc,
11234 struct drm_framebuffer *fb,
ed8d1975 11235 struct drm_i915_gem_object *obj,
6258fbe2 11236 struct drm_i915_gem_request *req,
ed8d1975 11237 uint32_t flags)
8c9f3aaf
JB
11238{
11239 return -ENODEV;
11240}
11241
d6bbafa1
CW
11242static bool __intel_pageflip_stall_check(struct drm_device *dev,
11243 struct drm_crtc *crtc)
11244{
11245 struct drm_i915_private *dev_priv = dev->dev_private;
11246 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11247 struct intel_unpin_work *work = intel_crtc->unpin_work;
11248 u32 addr;
11249
11250 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11251 return true;
11252
11253 if (!work->enable_stall_check)
11254 return false;
11255
11256 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11257 if (work->flip_queued_req &&
11258 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11259 return false;
11260
1e3feefd 11261 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11262 }
11263
1e3feefd 11264 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11265 return false;
11266
11267 /* Potential stall - if we see that the flip has happened,
11268 * assume a missed interrupt. */
11269 if (INTEL_INFO(dev)->gen >= 4)
11270 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11271 else
11272 addr = I915_READ(DSPADDR(intel_crtc->plane));
11273
11274 /* There is a potential issue here with a false positive after a flip
11275 * to the same address. We could address this by checking for a
11276 * non-incrementing frame counter.
11277 */
11278 return addr == work->gtt_offset;
11279}
11280
11281void intel_check_page_flip(struct drm_device *dev, int pipe)
11282{
11283 struct drm_i915_private *dev_priv = dev->dev_private;
11284 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11285 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11286 struct intel_unpin_work *work;
f326038a 11287
6c51d46f 11288 WARN_ON(!in_interrupt());
d6bbafa1
CW
11289
11290 if (crtc == NULL)
11291 return;
11292
f326038a 11293 spin_lock(&dev->event_lock);
6ad790c0
CW
11294 work = intel_crtc->unpin_work;
11295 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11296 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11297 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11298 page_flip_completed(intel_crtc);
6ad790c0 11299 work = NULL;
d6bbafa1 11300 }
6ad790c0
CW
11301 if (work != NULL &&
11302 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11303 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11304 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11305}
11306
6b95a207
KH
11307static int intel_crtc_page_flip(struct drm_crtc *crtc,
11308 struct drm_framebuffer *fb,
ed8d1975
KP
11309 struct drm_pending_vblank_event *event,
11310 uint32_t page_flip_flags)
6b95a207
KH
11311{
11312 struct drm_device *dev = crtc->dev;
11313 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11314 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11315 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11317 struct drm_plane *primary = crtc->primary;
a071fa00 11318 enum pipe pipe = intel_crtc->pipe;
6b95a207 11319 struct intel_unpin_work *work;
a4872ba6 11320 struct intel_engine_cs *ring;
cf5d8a46 11321 bool mmio_flip;
91af127f 11322 struct drm_i915_gem_request *request = NULL;
52e68630 11323 int ret;
6b95a207 11324
2ff8fde1
MR
11325 /*
11326 * drm_mode_page_flip_ioctl() should already catch this, but double
11327 * check to be safe. In the future we may enable pageflipping from
11328 * a disabled primary plane.
11329 */
11330 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11331 return -EBUSY;
11332
e6a595d2 11333 /* Can't change pixel format via MI display flips. */
f4510a27 11334 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11335 return -EINVAL;
11336
11337 /*
11338 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11339 * Note that pitch changes could also affect these register.
11340 */
11341 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11342 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11343 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11344 return -EINVAL;
11345
f900db47
CW
11346 if (i915_terminally_wedged(&dev_priv->gpu_error))
11347 goto out_hang;
11348
b14c5679 11349 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11350 if (work == NULL)
11351 return -ENOMEM;
11352
6b95a207 11353 work->event = event;
b4a98e57 11354 work->crtc = crtc;
ab8d6675 11355 work->old_fb = old_fb;
6b95a207
KH
11356 INIT_WORK(&work->work, intel_unpin_work_fn);
11357
87b6b101 11358 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11359 if (ret)
11360 goto free_work;
11361
6b95a207 11362 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11363 spin_lock_irq(&dev->event_lock);
6b95a207 11364 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11365 /* Before declaring the flip queue wedged, check if
11366 * the hardware completed the operation behind our backs.
11367 */
11368 if (__intel_pageflip_stall_check(dev, crtc)) {
11369 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11370 page_flip_completed(intel_crtc);
11371 } else {
11372 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11373 spin_unlock_irq(&dev->event_lock);
468f0b44 11374
d6bbafa1
CW
11375 drm_crtc_vblank_put(crtc);
11376 kfree(work);
11377 return -EBUSY;
11378 }
6b95a207
KH
11379 }
11380 intel_crtc->unpin_work = work;
5e2d7afc 11381 spin_unlock_irq(&dev->event_lock);
6b95a207 11382
b4a98e57
CW
11383 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11384 flush_workqueue(dev_priv->wq);
11385
75dfca80 11386 /* Reference the objects for the scheduled work. */
ab8d6675 11387 drm_framebuffer_reference(work->old_fb);
05394f39 11388 drm_gem_object_reference(&obj->base);
6b95a207 11389
f4510a27 11390 crtc->primary->fb = fb;
afd65eb4 11391 update_state_fb(crtc->primary);
1ed1f968 11392
e1f99ce6 11393 work->pending_flip_obj = obj;
e1f99ce6 11394
89ed88ba
CW
11395 ret = i915_mutex_lock_interruptible(dev);
11396 if (ret)
11397 goto cleanup;
11398
b4a98e57 11399 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11400 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11401
75f7f3ec 11402 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 11403 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 11404
4fa62c89
VS
11405 if (IS_VALLEYVIEW(dev)) {
11406 ring = &dev_priv->ring[BCS];
ab8d6675 11407 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
11408 /* vlv: DISPLAY_FLIP fails to change tiling */
11409 ring = NULL;
48bf5b2d 11410 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 11411 ring = &dev_priv->ring[BCS];
4fa62c89 11412 } else if (INTEL_INFO(dev)->gen >= 7) {
b4716185 11413 ring = i915_gem_request_get_ring(obj->last_write_req);
4fa62c89
VS
11414 if (ring == NULL || ring->id != RCS)
11415 ring = &dev_priv->ring[BCS];
11416 } else {
11417 ring = &dev_priv->ring[RCS];
11418 }
11419
cf5d8a46
CW
11420 mmio_flip = use_mmio_flip(ring, obj);
11421
11422 /* When using CS flips, we want to emit semaphores between rings.
11423 * However, when using mmio flips we will create a task to do the
11424 * synchronisation, so all we want here is to pin the framebuffer
11425 * into the display plane and skip any waits.
11426 */
82bc3b2d 11427 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
cf5d8a46 11428 crtc->primary->state,
91af127f 11429 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
8c9f3aaf
JB
11430 if (ret)
11431 goto cleanup_pending;
6b95a207 11432
121920fa
TU
11433 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11434 + intel_crtc->dspaddr_offset;
4fa62c89 11435
cf5d8a46 11436 if (mmio_flip) {
84c33a64
SG
11437 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11438 page_flip_flags);
d6bbafa1
CW
11439 if (ret)
11440 goto cleanup_unpin;
11441
f06cc1b9
JH
11442 i915_gem_request_assign(&work->flip_queued_req,
11443 obj->last_write_req);
d6bbafa1 11444 } else {
6258fbe2
JH
11445 if (!request) {
11446 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11447 if (ret)
11448 goto cleanup_unpin;
11449 }
11450
11451 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
d6bbafa1
CW
11452 page_flip_flags);
11453 if (ret)
11454 goto cleanup_unpin;
11455
6258fbe2 11456 i915_gem_request_assign(&work->flip_queued_req, request);
d6bbafa1
CW
11457 }
11458
91af127f 11459 if (request)
75289874 11460 i915_add_request_no_flush(request);
91af127f 11461
1e3feefd 11462 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11463 work->enable_stall_check = true;
4fa62c89 11464
ab8d6675 11465 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a9ff8714 11466 to_intel_plane(primary)->frontbuffer_bit);
a071fa00 11467
7ff0ebcc 11468 intel_fbc_disable(dev);
a9ff8714
VS
11469 intel_frontbuffer_flip_prepare(dev,
11470 to_intel_plane(primary)->frontbuffer_bit);
6b95a207
KH
11471 mutex_unlock(&dev->struct_mutex);
11472
e5510fac
JB
11473 trace_i915_flip_request(intel_crtc->plane, obj);
11474
6b95a207 11475 return 0;
96b099fd 11476
4fa62c89 11477cleanup_unpin:
82bc3b2d 11478 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 11479cleanup_pending:
91af127f
JH
11480 if (request)
11481 i915_gem_request_cancel(request);
b4a98e57 11482 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11483 mutex_unlock(&dev->struct_mutex);
11484cleanup:
f4510a27 11485 crtc->primary->fb = old_fb;
afd65eb4 11486 update_state_fb(crtc->primary);
89ed88ba
CW
11487
11488 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11489 drm_framebuffer_unreference(work->old_fb);
96b099fd 11490
5e2d7afc 11491 spin_lock_irq(&dev->event_lock);
96b099fd 11492 intel_crtc->unpin_work = NULL;
5e2d7afc 11493 spin_unlock_irq(&dev->event_lock);
96b099fd 11494
87b6b101 11495 drm_crtc_vblank_put(crtc);
7317c75e 11496free_work:
96b099fd
CW
11497 kfree(work);
11498
f900db47 11499 if (ret == -EIO) {
02e0efb5
ML
11500 struct drm_atomic_state *state;
11501 struct drm_plane_state *plane_state;
11502
f900db47 11503out_hang:
02e0efb5
ML
11504 state = drm_atomic_state_alloc(dev);
11505 if (!state)
11506 return -ENOMEM;
11507 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11508
11509retry:
11510 plane_state = drm_atomic_get_plane_state(state, primary);
11511 ret = PTR_ERR_OR_ZERO(plane_state);
11512 if (!ret) {
11513 drm_atomic_set_fb_for_plane(plane_state, fb);
11514
11515 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11516 if (!ret)
11517 ret = drm_atomic_commit(state);
11518 }
11519
11520 if (ret == -EDEADLK) {
11521 drm_modeset_backoff(state->acquire_ctx);
11522 drm_atomic_state_clear(state);
11523 goto retry;
11524 }
11525
11526 if (ret)
11527 drm_atomic_state_free(state);
11528
f0d3dad3 11529 if (ret == 0 && event) {
5e2d7afc 11530 spin_lock_irq(&dev->event_lock);
a071fa00 11531 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11532 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11533 }
f900db47 11534 }
96b099fd 11535 return ret;
6b95a207
KH
11536}
11537
da20eabd
ML
11538
11539/**
11540 * intel_wm_need_update - Check whether watermarks need updating
11541 * @plane: drm plane
11542 * @state: new plane state
11543 *
11544 * Check current plane state versus the new one to determine whether
11545 * watermarks need to be recalculated.
11546 *
11547 * Returns true or false.
11548 */
11549static bool intel_wm_need_update(struct drm_plane *plane,
11550 struct drm_plane_state *state)
11551{
11552 /* Update watermarks on tiling changes. */
11553 if (!plane->state->fb || !state->fb ||
11554 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11555 plane->state->rotation != state->rotation)
11556 return true;
11557
11558 if (plane->state->crtc_w != state->crtc_w)
11559 return true;
11560
11561 return false;
11562}
11563
11564int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11565 struct drm_plane_state *plane_state)
11566{
11567 struct drm_crtc *crtc = crtc_state->crtc;
11568 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11569 struct drm_plane *plane = plane_state->plane;
11570 struct drm_device *dev = crtc->dev;
11571 struct drm_i915_private *dev_priv = dev->dev_private;
11572 struct intel_plane_state *old_plane_state =
11573 to_intel_plane_state(plane->state);
11574 int idx = intel_crtc->base.base.id, ret;
11575 int i = drm_plane_index(plane);
11576 bool mode_changed = needs_modeset(crtc_state);
11577 bool was_crtc_enabled = crtc->state->active;
11578 bool is_crtc_enabled = crtc_state->active;
11579
11580 bool turn_off, turn_on, visible, was_visible;
11581 struct drm_framebuffer *fb = plane_state->fb;
11582
11583 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11584 plane->type != DRM_PLANE_TYPE_CURSOR) {
11585 ret = skl_update_scaler_plane(
11586 to_intel_crtc_state(crtc_state),
11587 to_intel_plane_state(plane_state));
11588 if (ret)
11589 return ret;
11590 }
11591
11592 /*
11593 * Disabling a plane is always okay; we just need to update
11594 * fb tracking in a special way since cleanup_fb() won't
11595 * get called by the plane helpers.
11596 */
11597 if (old_plane_state->base.fb && !fb)
11598 intel_crtc->atomic.disabled_planes |= 1 << i;
11599
da20eabd
ML
11600 was_visible = old_plane_state->visible;
11601 visible = to_intel_plane_state(plane_state)->visible;
11602
11603 if (!was_crtc_enabled && WARN_ON(was_visible))
11604 was_visible = false;
11605
11606 if (!is_crtc_enabled && WARN_ON(visible))
11607 visible = false;
11608
11609 if (!was_visible && !visible)
11610 return 0;
11611
11612 turn_off = was_visible && (!visible || mode_changed);
11613 turn_on = visible && (!was_visible || mode_changed);
11614
11615 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11616 plane->base.id, fb ? fb->base.id : -1);
11617
11618 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11619 plane->base.id, was_visible, visible,
11620 turn_off, turn_on, mode_changed);
11621
852eb00d 11622 if (turn_on) {
f015c551 11623 intel_crtc->atomic.update_wm_pre = true;
852eb00d
VS
11624 /* must disable cxsr around plane enable/disable */
11625 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11626 intel_crtc->atomic.disable_cxsr = true;
11627 /* to potentially re-enable cxsr */
11628 intel_crtc->atomic.wait_vblank = true;
11629 intel_crtc->atomic.update_wm_post = true;
11630 }
11631 } else if (turn_off) {
f015c551 11632 intel_crtc->atomic.update_wm_post = true;
852eb00d
VS
11633 /* must disable cxsr around plane enable/disable */
11634 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11635 if (is_crtc_enabled)
11636 intel_crtc->atomic.wait_vblank = true;
11637 intel_crtc->atomic.disable_cxsr = true;
11638 }
11639 } else if (intel_wm_need_update(plane, plane_state)) {
f015c551 11640 intel_crtc->atomic.update_wm_pre = true;
852eb00d 11641 }
da20eabd 11642
a9ff8714
VS
11643 if (visible)
11644 intel_crtc->atomic.fb_bits |=
11645 to_intel_plane(plane)->frontbuffer_bit;
11646
da20eabd
ML
11647 switch (plane->type) {
11648 case DRM_PLANE_TYPE_PRIMARY:
da20eabd
ML
11649 intel_crtc->atomic.wait_for_flips = true;
11650 intel_crtc->atomic.pre_disable_primary = turn_off;
11651 intel_crtc->atomic.post_enable_primary = turn_on;
11652
066cf55b
RV
11653 if (turn_off) {
11654 /*
11655 * FIXME: Actually if we will still have any other
11656 * plane enabled on the pipe we could let IPS enabled
11657 * still, but for now lets consider that when we make
11658 * primary invisible by setting DSPCNTR to 0 on
11659 * update_primary_plane function IPS needs to be
11660 * disable.
11661 */
11662 intel_crtc->atomic.disable_ips = true;
11663
da20eabd 11664 intel_crtc->atomic.disable_fbc = true;
066cf55b 11665 }
da20eabd
ML
11666
11667 /*
11668 * FBC does not work on some platforms for rotated
11669 * planes, so disable it when rotation is not 0 and
11670 * update it when rotation is set back to 0.
11671 *
11672 * FIXME: This is redundant with the fbc update done in
11673 * the primary plane enable function except that that
11674 * one is done too late. We eventually need to unify
11675 * this.
11676 */
11677
11678 if (visible &&
11679 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11680 dev_priv->fbc.crtc == intel_crtc &&
11681 plane_state->rotation != BIT(DRM_ROTATE_0))
11682 intel_crtc->atomic.disable_fbc = true;
11683
11684 /*
11685 * BDW signals flip done immediately if the plane
11686 * is disabled, even if the plane enable is already
11687 * armed to occur at the next vblank :(
11688 */
11689 if (turn_on && IS_BROADWELL(dev))
11690 intel_crtc->atomic.wait_vblank = true;
11691
11692 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11693 break;
11694 case DRM_PLANE_TYPE_CURSOR:
da20eabd
ML
11695 break;
11696 case DRM_PLANE_TYPE_OVERLAY:
d032ffa0 11697 if (turn_off && !mode_changed) {
da20eabd
ML
11698 intel_crtc->atomic.wait_vblank = true;
11699 intel_crtc->atomic.update_sprite_watermarks |=
11700 1 << i;
11701 }
da20eabd
ML
11702 }
11703 return 0;
11704}
11705
6d3a1ce7
ML
11706static bool encoders_cloneable(const struct intel_encoder *a,
11707 const struct intel_encoder *b)
11708{
11709 /* masks could be asymmetric, so check both ways */
11710 return a == b || (a->cloneable & (1 << b->type) &&
11711 b->cloneable & (1 << a->type));
11712}
11713
11714static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11715 struct intel_crtc *crtc,
11716 struct intel_encoder *encoder)
11717{
11718 struct intel_encoder *source_encoder;
11719 struct drm_connector *connector;
11720 struct drm_connector_state *connector_state;
11721 int i;
11722
11723 for_each_connector_in_state(state, connector, connector_state, i) {
11724 if (connector_state->crtc != &crtc->base)
11725 continue;
11726
11727 source_encoder =
11728 to_intel_encoder(connector_state->best_encoder);
11729 if (!encoders_cloneable(encoder, source_encoder))
11730 return false;
11731 }
11732
11733 return true;
11734}
11735
11736static bool check_encoder_cloning(struct drm_atomic_state *state,
11737 struct intel_crtc *crtc)
11738{
11739 struct intel_encoder *encoder;
11740 struct drm_connector *connector;
11741 struct drm_connector_state *connector_state;
11742 int i;
11743
11744 for_each_connector_in_state(state, connector, connector_state, i) {
11745 if (connector_state->crtc != &crtc->base)
11746 continue;
11747
11748 encoder = to_intel_encoder(connector_state->best_encoder);
11749 if (!check_single_encoder_cloning(state, crtc, encoder))
11750 return false;
11751 }
11752
11753 return true;
11754}
11755
d032ffa0
ML
11756static void intel_crtc_check_initial_planes(struct drm_crtc *crtc,
11757 struct drm_crtc_state *crtc_state)
11758{
11759 struct intel_crtc_state *pipe_config =
11760 to_intel_crtc_state(crtc_state);
11761 struct drm_plane *p;
11762 unsigned visible_mask = 0;
11763
11764 drm_for_each_plane_mask(p, crtc->dev, crtc_state->plane_mask) {
11765 struct drm_plane_state *plane_state =
11766 drm_atomic_get_existing_plane_state(crtc_state->state, p);
11767
11768 if (WARN_ON(!plane_state))
11769 continue;
11770
11771 if (!plane_state->fb)
11772 crtc_state->plane_mask &=
11773 ~(1 << drm_plane_index(p));
11774 else if (to_intel_plane_state(plane_state)->visible)
11775 visible_mask |= 1 << drm_plane_index(p);
11776 }
11777
11778 if (!visible_mask)
11779 return;
11780
11781 pipe_config->quirks &= ~PIPE_CONFIG_QUIRK_INITIAL_PLANES;
11782}
11783
6d3a1ce7
ML
11784static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11785 struct drm_crtc_state *crtc_state)
11786{
cf5a15be 11787 struct drm_device *dev = crtc->dev;
ad421372 11788 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 11789 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11790 struct intel_crtc_state *pipe_config =
11791 to_intel_crtc_state(crtc_state);
6d3a1ce7 11792 struct drm_atomic_state *state = crtc_state->state;
ad421372 11793 int ret, idx = crtc->base.id;
6d3a1ce7
ML
11794 bool mode_changed = needs_modeset(crtc_state);
11795
11796 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11797 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11798 return -EINVAL;
11799 }
11800
11801 I915_STATE_WARN(crtc->state->active != intel_crtc->active,
11802 "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
11803 idx, crtc->state->active, intel_crtc->active);
11804
d032ffa0
ML
11805 /* plane mask is fixed up after all initial planes are calculated */
11806 if (pipe_config->quirks & PIPE_CONFIG_QUIRK_INITIAL_PLANES)
11807 intel_crtc_check_initial_planes(crtc, crtc_state);
11808
852eb00d
VS
11809 if (mode_changed && !crtc_state->active)
11810 intel_crtc->atomic.update_wm_post = true;
eddfcbcd 11811
ad421372
ML
11812 if (mode_changed && crtc_state->enable &&
11813 dev_priv->display.crtc_compute_clock &&
11814 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11815 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11816 pipe_config);
11817 if (ret)
11818 return ret;
11819 }
11820
cf5a15be 11821 return intel_atomic_setup_scalers(dev, intel_crtc, pipe_config);
6d3a1ce7
ML
11822}
11823
65b38e0d 11824static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11825 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11826 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11827 .atomic_begin = intel_begin_crtc_commit,
11828 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11829 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11830};
11831
9a935856
DV
11832/**
11833 * intel_modeset_update_staged_output_state
11834 *
11835 * Updates the staged output configuration state, e.g. after we've read out the
11836 * current hw state.
11837 */
11838static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 11839{
7668851f 11840 struct intel_crtc *crtc;
9a935856
DV
11841 struct intel_encoder *encoder;
11842 struct intel_connector *connector;
f6e5b160 11843
3a3371ff 11844 for_each_intel_connector(dev, connector) {
9a935856
DV
11845 connector->new_encoder =
11846 to_intel_encoder(connector->base.encoder);
11847 }
f6e5b160 11848
b2784e15 11849 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11850 encoder->new_crtc =
11851 to_intel_crtc(encoder->base.crtc);
11852 }
7668851f 11853
d3fcc808 11854 for_each_intel_crtc(dev, crtc) {
83d65738 11855 crtc->new_enabled = crtc->base.state->enable;
7668851f 11856 }
f6e5b160
CW
11857}
11858
d29b2f9d
ACO
11859/* Transitional helper to copy current connector/encoder state to
11860 * connector->state. This is needed so that code that is partially
11861 * converted to atomic does the right thing.
11862 */
11863static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11864{
11865 struct intel_connector *connector;
11866
11867 for_each_intel_connector(dev, connector) {
11868 if (connector->base.encoder) {
11869 connector->base.state->best_encoder =
11870 connector->base.encoder;
11871 connector->base.state->crtc =
11872 connector->base.encoder->crtc;
11873 } else {
11874 connector->base.state->best_encoder = NULL;
11875 connector->base.state->crtc = NULL;
11876 }
11877 }
11878}
11879
050f7aeb 11880static void
eba905b2 11881connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11882 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11883{
11884 int bpp = pipe_config->pipe_bpp;
11885
11886 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11887 connector->base.base.id,
c23cc417 11888 connector->base.name);
050f7aeb
DV
11889
11890 /* Don't use an invalid EDID bpc value */
11891 if (connector->base.display_info.bpc &&
11892 connector->base.display_info.bpc * 3 < bpp) {
11893 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11894 bpp, connector->base.display_info.bpc*3);
11895 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11896 }
11897
11898 /* Clamp bpp to 8 on screens without EDID 1.4 */
11899 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11900 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11901 bpp);
11902 pipe_config->pipe_bpp = 24;
11903 }
11904}
11905
4e53c2e0 11906static int
050f7aeb 11907compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11908 struct intel_crtc_state *pipe_config)
4e53c2e0 11909{
050f7aeb 11910 struct drm_device *dev = crtc->base.dev;
1486017f 11911 struct drm_atomic_state *state;
da3ced29
ACO
11912 struct drm_connector *connector;
11913 struct drm_connector_state *connector_state;
1486017f 11914 int bpp, i;
4e53c2e0 11915
d328c9d7 11916 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
4e53c2e0 11917 bpp = 10*3;
d328c9d7
DV
11918 else if (INTEL_INFO(dev)->gen >= 5)
11919 bpp = 12*3;
11920 else
11921 bpp = 8*3;
11922
4e53c2e0 11923
4e53c2e0
DV
11924 pipe_config->pipe_bpp = bpp;
11925
1486017f
ACO
11926 state = pipe_config->base.state;
11927
4e53c2e0 11928 /* Clamp display bpp to EDID value */
da3ced29
ACO
11929 for_each_connector_in_state(state, connector, connector_state, i) {
11930 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11931 continue;
11932
da3ced29
ACO
11933 connected_sink_compute_bpp(to_intel_connector(connector),
11934 pipe_config);
4e53c2e0
DV
11935 }
11936
11937 return bpp;
11938}
11939
644db711
DV
11940static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11941{
11942 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11943 "type: 0x%x flags: 0x%x\n",
1342830c 11944 mode->crtc_clock,
644db711
DV
11945 mode->crtc_hdisplay, mode->crtc_hsync_start,
11946 mode->crtc_hsync_end, mode->crtc_htotal,
11947 mode->crtc_vdisplay, mode->crtc_vsync_start,
11948 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11949}
11950
c0b03411 11951static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11952 struct intel_crtc_state *pipe_config,
c0b03411
DV
11953 const char *context)
11954{
6a60cd87
CK
11955 struct drm_device *dev = crtc->base.dev;
11956 struct drm_plane *plane;
11957 struct intel_plane *intel_plane;
11958 struct intel_plane_state *state;
11959 struct drm_framebuffer *fb;
11960
11961 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11962 context, pipe_config, pipe_name(crtc->pipe));
c0b03411
DV
11963
11964 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11965 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11966 pipe_config->pipe_bpp, pipe_config->dither);
11967 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11968 pipe_config->has_pch_encoder,
11969 pipe_config->fdi_lanes,
11970 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11971 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11972 pipe_config->fdi_m_n.tu);
eb14cb74
VS
11973 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11974 pipe_config->has_dp_encoder,
11975 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11976 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11977 pipe_config->dp_m_n.tu);
b95af8be
VK
11978
11979 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11980 pipe_config->has_dp_encoder,
11981 pipe_config->dp_m2_n2.gmch_m,
11982 pipe_config->dp_m2_n2.gmch_n,
11983 pipe_config->dp_m2_n2.link_m,
11984 pipe_config->dp_m2_n2.link_n,
11985 pipe_config->dp_m2_n2.tu);
11986
55072d19
DV
11987 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11988 pipe_config->has_audio,
11989 pipe_config->has_infoframe);
11990
c0b03411 11991 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11992 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11993 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11994 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11995 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 11996 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
11997 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11998 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
11999 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12000 crtc->num_scalers,
12001 pipe_config->scaler_state.scaler_users,
12002 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12003 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12004 pipe_config->gmch_pfit.control,
12005 pipe_config->gmch_pfit.pgm_ratios,
12006 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12007 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12008 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12009 pipe_config->pch_pfit.size,
12010 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12011 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12012 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12013
415ff0f6
TU
12014 if (IS_BROXTON(dev)) {
12015 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
12016 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
12017 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
12018 pipe_config->ddi_pll_sel,
12019 pipe_config->dpll_hw_state.ebb0,
12020 pipe_config->dpll_hw_state.pll0,
12021 pipe_config->dpll_hw_state.pll1,
12022 pipe_config->dpll_hw_state.pll2,
12023 pipe_config->dpll_hw_state.pll3,
12024 pipe_config->dpll_hw_state.pll6,
12025 pipe_config->dpll_hw_state.pll8,
12026 pipe_config->dpll_hw_state.pcsdw12);
12027 } else if (IS_SKYLAKE(dev)) {
12028 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12029 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12030 pipe_config->ddi_pll_sel,
12031 pipe_config->dpll_hw_state.ctrl1,
12032 pipe_config->dpll_hw_state.cfgcr1,
12033 pipe_config->dpll_hw_state.cfgcr2);
12034 } else if (HAS_DDI(dev)) {
12035 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
12036 pipe_config->ddi_pll_sel,
12037 pipe_config->dpll_hw_state.wrpll);
12038 } else {
12039 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12040 "fp0: 0x%x, fp1: 0x%x\n",
12041 pipe_config->dpll_hw_state.dpll,
12042 pipe_config->dpll_hw_state.dpll_md,
12043 pipe_config->dpll_hw_state.fp0,
12044 pipe_config->dpll_hw_state.fp1);
12045 }
12046
6a60cd87
CK
12047 DRM_DEBUG_KMS("planes on this crtc\n");
12048 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12049 intel_plane = to_intel_plane(plane);
12050 if (intel_plane->pipe != crtc->pipe)
12051 continue;
12052
12053 state = to_intel_plane_state(plane->state);
12054 fb = state->base.fb;
12055 if (!fb) {
12056 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12057 "disabled, scaler_id = %d\n",
12058 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12059 plane->base.id, intel_plane->pipe,
12060 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12061 drm_plane_index(plane), state->scaler_id);
12062 continue;
12063 }
12064
12065 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12066 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12067 plane->base.id, intel_plane->pipe,
12068 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12069 drm_plane_index(plane));
12070 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12071 fb->base.id, fb->width, fb->height, fb->pixel_format);
12072 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12073 state->scaler_id,
12074 state->src.x1 >> 16, state->src.y1 >> 16,
12075 drm_rect_width(&state->src) >> 16,
12076 drm_rect_height(&state->src) >> 16,
12077 state->dst.x1, state->dst.y1,
12078 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12079 }
c0b03411
DV
12080}
12081
5448a00d 12082static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12083{
5448a00d
ACO
12084 struct drm_device *dev = state->dev;
12085 struct intel_encoder *encoder;
da3ced29 12086 struct drm_connector *connector;
5448a00d 12087 struct drm_connector_state *connector_state;
00f0b378 12088 unsigned int used_ports = 0;
5448a00d 12089 int i;
00f0b378
VS
12090
12091 /*
12092 * Walk the connector list instead of the encoder
12093 * list to detect the problem on ddi platforms
12094 * where there's just one encoder per digital port.
12095 */
da3ced29 12096 for_each_connector_in_state(state, connector, connector_state, i) {
5448a00d 12097 if (!connector_state->best_encoder)
00f0b378
VS
12098 continue;
12099
5448a00d
ACO
12100 encoder = to_intel_encoder(connector_state->best_encoder);
12101
12102 WARN_ON(!connector_state->crtc);
00f0b378
VS
12103
12104 switch (encoder->type) {
12105 unsigned int port_mask;
12106 case INTEL_OUTPUT_UNKNOWN:
12107 if (WARN_ON(!HAS_DDI(dev)))
12108 break;
12109 case INTEL_OUTPUT_DISPLAYPORT:
12110 case INTEL_OUTPUT_HDMI:
12111 case INTEL_OUTPUT_EDP:
12112 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12113
12114 /* the same port mustn't appear more than once */
12115 if (used_ports & port_mask)
12116 return false;
12117
12118 used_ports |= port_mask;
12119 default:
12120 break;
12121 }
12122 }
12123
12124 return true;
12125}
12126
83a57153
ACO
12127static void
12128clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12129{
12130 struct drm_crtc_state tmp_state;
663a3640 12131 struct intel_crtc_scaler_state scaler_state;
4978cc93
ACO
12132 struct intel_dpll_hw_state dpll_hw_state;
12133 enum intel_dpll_id shared_dpll;
8504c74c 12134 uint32_t ddi_pll_sel;
83a57153 12135
7546a384
ACO
12136 /* FIXME: before the switch to atomic started, a new pipe_config was
12137 * kzalloc'd. Code that depends on any field being zero should be
12138 * fixed, so that the crtc_state can be safely duplicated. For now,
12139 * only fields that are know to not cause problems are preserved. */
12140
83a57153 12141 tmp_state = crtc_state->base;
663a3640 12142 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12143 shared_dpll = crtc_state->shared_dpll;
12144 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12145 ddi_pll_sel = crtc_state->ddi_pll_sel;
4978cc93 12146
83a57153 12147 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12148
83a57153 12149 crtc_state->base = tmp_state;
663a3640 12150 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12151 crtc_state->shared_dpll = shared_dpll;
12152 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12153 crtc_state->ddi_pll_sel = ddi_pll_sel;
83a57153
ACO
12154}
12155
548ee15b 12156static int
b8cecdf5 12157intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12158 struct intel_crtc_state *pipe_config)
ee7b9f93 12159{
b359283a 12160 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12161 struct intel_encoder *encoder;
da3ced29 12162 struct drm_connector *connector;
0b901879 12163 struct drm_connector_state *connector_state;
d328c9d7 12164 int base_bpp, ret = -EINVAL;
0b901879 12165 int i;
e29c22c0 12166 bool retry = true;
ee7b9f93 12167
83a57153 12168 clear_intel_crtc_state(pipe_config);
7758a113 12169
e143a21c
DV
12170 pipe_config->cpu_transcoder =
12171 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12172
2960bc9c
ID
12173 /*
12174 * Sanitize sync polarity flags based on requested ones. If neither
12175 * positive or negative polarity is requested, treat this as meaning
12176 * negative polarity.
12177 */
2d112de7 12178 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12179 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12180 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12181
2d112de7 12182 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12183 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12184 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12185
050f7aeb
DV
12186 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12187 * plane pixel format and any sink constraints into account. Returns the
12188 * source plane bpp so that dithering can be selected on mismatches
12189 * after encoders and crtc also have had their say. */
d328c9d7
DV
12190 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12191 pipe_config);
12192 if (base_bpp < 0)
4e53c2e0
DV
12193 goto fail;
12194
e41a56be
VS
12195 /*
12196 * Determine the real pipe dimensions. Note that stereo modes can
12197 * increase the actual pipe size due to the frame doubling and
12198 * insertion of additional space for blanks between the frame. This
12199 * is stored in the crtc timings. We use the requested mode to do this
12200 * computation to clearly distinguish it from the adjusted mode, which
12201 * can be changed by the connectors in the below retry loop.
12202 */
2d112de7 12203 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12204 &pipe_config->pipe_src_w,
12205 &pipe_config->pipe_src_h);
e41a56be 12206
e29c22c0 12207encoder_retry:
ef1b460d 12208 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12209 pipe_config->port_clock = 0;
ef1b460d 12210 pipe_config->pixel_multiplier = 1;
ff9a6750 12211
135c81b8 12212 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12213 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12214 CRTC_STEREO_DOUBLE);
135c81b8 12215
7758a113
DV
12216 /* Pass our mode to the connectors and the CRTC to give them a chance to
12217 * adjust it according to limitations or connector properties, and also
12218 * a chance to reject the mode entirely.
47f1c6c9 12219 */
da3ced29 12220 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12221 if (connector_state->crtc != crtc)
7758a113 12222 continue;
7ae89233 12223
0b901879
ACO
12224 encoder = to_intel_encoder(connector_state->best_encoder);
12225
efea6e8e
DV
12226 if (!(encoder->compute_config(encoder, pipe_config))) {
12227 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12228 goto fail;
12229 }
ee7b9f93 12230 }
47f1c6c9 12231
ff9a6750
DV
12232 /* Set default port clock if not overwritten by the encoder. Needs to be
12233 * done afterwards in case the encoder adjusts the mode. */
12234 if (!pipe_config->port_clock)
2d112de7 12235 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12236 * pipe_config->pixel_multiplier;
ff9a6750 12237
a43f6e0f 12238 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12239 if (ret < 0) {
7758a113
DV
12240 DRM_DEBUG_KMS("CRTC fixup failed\n");
12241 goto fail;
ee7b9f93 12242 }
e29c22c0
DV
12243
12244 if (ret == RETRY) {
12245 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12246 ret = -EINVAL;
12247 goto fail;
12248 }
12249
12250 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12251 retry = false;
12252 goto encoder_retry;
12253 }
12254
d328c9d7 12255 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
4e53c2e0 12256 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12257 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12258
cdba954e
ACO
12259 /* Check if we need to force a modeset */
12260 if (pipe_config->has_audio !=
85a96e7a 12261 to_intel_crtc_state(crtc->state)->has_audio) {
cdba954e 12262 pipe_config->base.mode_changed = true;
85a96e7a
ML
12263 ret = drm_atomic_add_affected_planes(state, crtc);
12264 }
cdba954e
ACO
12265
12266 /*
12267 * Note we have an issue here with infoframes: current code
12268 * only updates them on the full mode set path per hw
12269 * requirements. So here we should be checking for any
12270 * required changes and forcing a mode set.
12271 */
7758a113 12272fail:
548ee15b 12273 return ret;
ee7b9f93 12274}
47f1c6c9 12275
ea9d758d 12276static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 12277{
ea9d758d 12278 struct drm_encoder *encoder;
f6e5b160 12279 struct drm_device *dev = crtc->dev;
f6e5b160 12280
ea9d758d
DV
12281 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12282 if (encoder->crtc == crtc)
12283 return true;
12284
12285 return false;
12286}
12287
12288static void
0a9ab303 12289intel_modeset_update_state(struct drm_atomic_state *state)
ea9d758d 12290{
0a9ab303 12291 struct drm_device *dev = state->dev;
ea9d758d 12292 struct intel_encoder *intel_encoder;
0a9ab303
ACO
12293 struct drm_crtc *crtc;
12294 struct drm_crtc_state *crtc_state;
ea9d758d
DV
12295 struct drm_connector *connector;
12296
de419ab6 12297 intel_shared_dpll_commit(state);
ba41c0de 12298
b2784e15 12299 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
12300 if (!intel_encoder->base.crtc)
12301 continue;
12302
69024de8
ML
12303 crtc = intel_encoder->base.crtc;
12304 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12305 if (!crtc_state || !needs_modeset(crtc->state))
12306 continue;
ea9d758d 12307
69024de8 12308 intel_encoder->connectors_active = false;
ea9d758d
DV
12309 }
12310
3cb480bc 12311 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
f7217905 12312 intel_modeset_update_staged_output_state(state->dev);
ea9d758d 12313
7668851f 12314 /* Double check state. */
0a9ab303
ACO
12315 for_each_crtc(dev, crtc) {
12316 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
3cb480bc
ML
12317
12318 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12319
12320 /* Update hwmode for vblank functions */
12321 if (crtc->state->active)
12322 crtc->hwmode = crtc->state->adjusted_mode;
12323 else
12324 crtc->hwmode.crtc_clock = 0;
ea9d758d
DV
12325 }
12326
12327 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12328 if (!connector->encoder || !connector->encoder->crtc)
12329 continue;
12330
69024de8
ML
12331 crtc = connector->encoder->crtc;
12332 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12333 if (!crtc_state || !needs_modeset(crtc->state))
12334 continue;
ea9d758d 12335
53d9f4e9 12336 if (crtc->state->active) {
69024de8
ML
12337 struct drm_property *dpms_property =
12338 dev->mode_config.dpms_property;
68d34720 12339
69024de8
ML
12340 connector->dpms = DRM_MODE_DPMS_ON;
12341 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
ea9d758d 12342
69024de8
ML
12343 intel_encoder = to_intel_encoder(connector->encoder);
12344 intel_encoder->connectors_active = true;
12345 } else
12346 connector->dpms = DRM_MODE_DPMS_OFF;
ea9d758d 12347 }
ea9d758d
DV
12348}
12349
3bd26263 12350static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12351{
3bd26263 12352 int diff;
f1f644dc
JB
12353
12354 if (clock1 == clock2)
12355 return true;
12356
12357 if (!clock1 || !clock2)
12358 return false;
12359
12360 diff = abs(clock1 - clock2);
12361
12362 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12363 return true;
12364
12365 return false;
12366}
12367
25c5b266
DV
12368#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12369 list_for_each_entry((intel_crtc), \
12370 &(dev)->mode_config.crtc_list, \
12371 base.head) \
0973f18f 12372 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12373
0e8ffe1b 12374static bool
2fa2fe9a 12375intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
12376 struct intel_crtc_state *current_config,
12377 struct intel_crtc_state *pipe_config)
0e8ffe1b 12378{
66e985c0
DV
12379#define PIPE_CONF_CHECK_X(name) \
12380 if (current_config->name != pipe_config->name) { \
12381 DRM_ERROR("mismatch in " #name " " \
12382 "(expected 0x%08x, found 0x%08x)\n", \
12383 current_config->name, \
12384 pipe_config->name); \
12385 return false; \
12386 }
12387
08a24034
DV
12388#define PIPE_CONF_CHECK_I(name) \
12389 if (current_config->name != pipe_config->name) { \
12390 DRM_ERROR("mismatch in " #name " " \
12391 "(expected %i, found %i)\n", \
12392 current_config->name, \
12393 pipe_config->name); \
12394 return false; \
88adfff1
DV
12395 }
12396
b95af8be
VK
12397/* This is required for BDW+ where there is only one set of registers for
12398 * switching between high and low RR.
12399 * This macro can be used whenever a comparison has to be made between one
12400 * hw state and multiple sw state variables.
12401 */
12402#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12403 if ((current_config->name != pipe_config->name) && \
12404 (current_config->alt_name != pipe_config->name)) { \
12405 DRM_ERROR("mismatch in " #name " " \
12406 "(expected %i or %i, found %i)\n", \
12407 current_config->name, \
12408 current_config->alt_name, \
12409 pipe_config->name); \
12410 return false; \
12411 }
12412
1bd1bd80
DV
12413#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12414 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 12415 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12416 "(expected %i, found %i)\n", \
12417 current_config->name & (mask), \
12418 pipe_config->name & (mask)); \
12419 return false; \
12420 }
12421
5e550656
VS
12422#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12423 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12424 DRM_ERROR("mismatch in " #name " " \
12425 "(expected %i, found %i)\n", \
12426 current_config->name, \
12427 pipe_config->name); \
12428 return false; \
12429 }
12430
bb760063
DV
12431#define PIPE_CONF_QUIRK(quirk) \
12432 ((current_config->quirks | pipe_config->quirks) & (quirk))
12433
eccb140b
DV
12434 PIPE_CONF_CHECK_I(cpu_transcoder);
12435
08a24034
DV
12436 PIPE_CONF_CHECK_I(has_pch_encoder);
12437 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
12438 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12439 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12440 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12441 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12442 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 12443
eb14cb74 12444 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
12445
12446 if (INTEL_INFO(dev)->gen < 8) {
12447 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12448 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12449 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12450 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12451 PIPE_CONF_CHECK_I(dp_m_n.tu);
12452
12453 if (current_config->has_drrs) {
12454 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12455 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12456 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12457 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12458 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12459 }
12460 } else {
12461 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12462 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12463 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12464 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12465 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12466 }
eb14cb74 12467
2d112de7
ACO
12468 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12469 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12470 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12471 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12472 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12473 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12474
2d112de7
ACO
12475 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12476 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12477 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12478 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12479 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12480 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12481
c93f54cf 12482 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12483 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
12484 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12485 IS_VALLEYVIEW(dev))
12486 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12487 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12488
9ed109a7
DV
12489 PIPE_CONF_CHECK_I(has_audio);
12490
2d112de7 12491 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12492 DRM_MODE_FLAG_INTERLACE);
12493
bb760063 12494 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12495 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12496 DRM_MODE_FLAG_PHSYNC);
2d112de7 12497 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12498 DRM_MODE_FLAG_NHSYNC);
2d112de7 12499 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12500 DRM_MODE_FLAG_PVSYNC);
2d112de7 12501 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12502 DRM_MODE_FLAG_NVSYNC);
12503 }
045ac3b5 12504
37327abd
VS
12505 PIPE_CONF_CHECK_I(pipe_src_w);
12506 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 12507
9953599b
DV
12508 /*
12509 * FIXME: BIOS likes to set up a cloned config with lvds+external
12510 * screen. Since we don't yet re-compute the pipe config when moving
12511 * just the lvds port away to another pipe the sw tracking won't match.
12512 *
12513 * Proper atomic modesets with recomputed global state will fix this.
12514 * Until then just don't check gmch state for inherited modes.
12515 */
12516 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12517 PIPE_CONF_CHECK_I(gmch_pfit.control);
12518 /* pfit ratios are autocomputed by the hw on gen4+ */
12519 if (INTEL_INFO(dev)->gen < 4)
12520 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12521 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12522 }
12523
fd4daa9c
CW
12524 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12525 if (current_config->pch_pfit.enabled) {
12526 PIPE_CONF_CHECK_I(pch_pfit.pos);
12527 PIPE_CONF_CHECK_I(pch_pfit.size);
12528 }
2fa2fe9a 12529
a1b2278e
CK
12530 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12531
e59150dc
JB
12532 /* BDW+ don't expose a synchronous way to read the state */
12533 if (IS_HASWELL(dev))
12534 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12535
282740f7
VS
12536 PIPE_CONF_CHECK_I(double_wide);
12537
26804afd
DV
12538 PIPE_CONF_CHECK_X(ddi_pll_sel);
12539
c0d43d62 12540 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 12541 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12542 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12543 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12544 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12545 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
12546 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12547 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12548 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12549
42571aef
VS
12550 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12551 PIPE_CONF_CHECK_I(pipe_bpp);
12552
2d112de7 12553 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12554 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12555
66e985c0 12556#undef PIPE_CONF_CHECK_X
08a24034 12557#undef PIPE_CONF_CHECK_I
b95af8be 12558#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12559#undef PIPE_CONF_CHECK_FLAGS
5e550656 12560#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12561#undef PIPE_CONF_QUIRK
88adfff1 12562
0e8ffe1b
DV
12563 return true;
12564}
12565
08db6652
DL
12566static void check_wm_state(struct drm_device *dev)
12567{
12568 struct drm_i915_private *dev_priv = dev->dev_private;
12569 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12570 struct intel_crtc *intel_crtc;
12571 int plane;
12572
12573 if (INTEL_INFO(dev)->gen < 9)
12574 return;
12575
12576 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12577 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12578
12579 for_each_intel_crtc(dev, intel_crtc) {
12580 struct skl_ddb_entry *hw_entry, *sw_entry;
12581 const enum pipe pipe = intel_crtc->pipe;
12582
12583 if (!intel_crtc->active)
12584 continue;
12585
12586 /* planes */
dd740780 12587 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12588 hw_entry = &hw_ddb.plane[pipe][plane];
12589 sw_entry = &sw_ddb->plane[pipe][plane];
12590
12591 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12592 continue;
12593
12594 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12595 "(expected (%u,%u), found (%u,%u))\n",
12596 pipe_name(pipe), plane + 1,
12597 sw_entry->start, sw_entry->end,
12598 hw_entry->start, hw_entry->end);
12599 }
12600
12601 /* cursor */
12602 hw_entry = &hw_ddb.cursor[pipe];
12603 sw_entry = &sw_ddb->cursor[pipe];
12604
12605 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12606 continue;
12607
12608 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12609 "(expected (%u,%u), found (%u,%u))\n",
12610 pipe_name(pipe),
12611 sw_entry->start, sw_entry->end,
12612 hw_entry->start, hw_entry->end);
12613 }
12614}
12615
91d1b4bd
DV
12616static void
12617check_connector_state(struct drm_device *dev)
8af6cf88 12618{
8af6cf88
DV
12619 struct intel_connector *connector;
12620
3a3371ff 12621 for_each_intel_connector(dev, connector) {
8af6cf88
DV
12622 /* This also checks the encoder/connector hw state with the
12623 * ->get_hw_state callbacks. */
12624 intel_connector_check_state(connector);
12625
e2c719b7 12626 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
8af6cf88
DV
12627 "connector's staged encoder doesn't match current encoder\n");
12628 }
91d1b4bd
DV
12629}
12630
12631static void
12632check_encoder_state(struct drm_device *dev)
12633{
12634 struct intel_encoder *encoder;
12635 struct intel_connector *connector;
8af6cf88 12636
b2784e15 12637 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12638 bool enabled = false;
12639 bool active = false;
12640 enum pipe pipe, tracked_pipe;
12641
12642 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12643 encoder->base.base.id,
8e329a03 12644 encoder->base.name);
8af6cf88 12645
e2c719b7 12646 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
8af6cf88 12647 "encoder's stage crtc doesn't match current crtc\n");
e2c719b7 12648 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
12649 "encoder's active_connectors set, but no crtc\n");
12650
3a3371ff 12651 for_each_intel_connector(dev, connector) {
8af6cf88
DV
12652 if (connector->base.encoder != &encoder->base)
12653 continue;
12654 enabled = true;
12655 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12656 active = true;
12657 }
0e32b39c
DA
12658 /*
12659 * for MST connectors if we unplug the connector is gone
12660 * away but the encoder is still connected to a crtc
12661 * until a modeset happens in response to the hotplug.
12662 */
12663 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12664 continue;
12665
e2c719b7 12666 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12667 "encoder's enabled state mismatch "
12668 "(expected %i, found %i)\n",
12669 !!encoder->base.crtc, enabled);
e2c719b7 12670 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
12671 "active encoder with no crtc\n");
12672
e2c719b7 12673 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
12674 "encoder's computed active state doesn't match tracked active state "
12675 "(expected %i, found %i)\n", active, encoder->connectors_active);
12676
12677 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 12678 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
12679 "encoder's hw state doesn't match sw tracking "
12680 "(expected %i, found %i)\n",
12681 encoder->connectors_active, active);
12682
12683 if (!encoder->base.crtc)
12684 continue;
12685
12686 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 12687 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
12688 "active encoder's pipe doesn't match"
12689 "(expected %i, found %i)\n",
12690 tracked_pipe, pipe);
12691
12692 }
91d1b4bd
DV
12693}
12694
12695static void
12696check_crtc_state(struct drm_device *dev)
12697{
fbee40df 12698 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12699 struct intel_crtc *crtc;
12700 struct intel_encoder *encoder;
5cec258b 12701 struct intel_crtc_state pipe_config;
8af6cf88 12702
d3fcc808 12703 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
12704 bool enabled = false;
12705 bool active = false;
12706
045ac3b5
JB
12707 memset(&pipe_config, 0, sizeof(pipe_config));
12708
8af6cf88
DV
12709 DRM_DEBUG_KMS("[CRTC:%d]\n",
12710 crtc->base.base.id);
12711
83d65738 12712 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
8af6cf88
DV
12713 "active crtc, but not enabled in sw tracking\n");
12714
b2784e15 12715 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12716 if (encoder->base.crtc != &crtc->base)
12717 continue;
12718 enabled = true;
12719 if (encoder->connectors_active)
12720 active = true;
12721 }
6c49f241 12722
e2c719b7 12723 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
12724 "crtc's computed active state doesn't match tracked active state "
12725 "(expected %i, found %i)\n", active, crtc->active);
83d65738 12726 I915_STATE_WARN(enabled != crtc->base.state->enable,
8af6cf88 12727 "crtc's computed enabled state doesn't match tracked enabled state "
83d65738
MR
12728 "(expected %i, found %i)\n", enabled,
12729 crtc->base.state->enable);
8af6cf88 12730
0e8ffe1b
DV
12731 active = dev_priv->display.get_pipe_config(crtc,
12732 &pipe_config);
d62cf62a 12733
b6b5d049
VS
12734 /* hw state is inconsistent with the pipe quirk */
12735 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12736 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
12737 active = crtc->active;
12738
b2784e15 12739 for_each_intel_encoder(dev, encoder) {
3eaba51c 12740 enum pipe pipe;
6c49f241
DV
12741 if (encoder->base.crtc != &crtc->base)
12742 continue;
1d37b689 12743 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
12744 encoder->get_config(encoder, &pipe_config);
12745 }
12746
e2c719b7 12747 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
12748 "crtc active state doesn't match with hw state "
12749 "(expected %i, found %i)\n", crtc->active, active);
12750
53d9f4e9
ML
12751 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12752 "transitional active state does not match atomic hw state "
12753 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12754
c0b03411 12755 if (active &&
6e3c9717 12756 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 12757 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
12758 intel_dump_pipe_config(crtc, &pipe_config,
12759 "[hw state]");
6e3c9717 12760 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
12761 "[sw state]");
12762 }
8af6cf88
DV
12763 }
12764}
12765
91d1b4bd
DV
12766static void
12767check_shared_dpll_state(struct drm_device *dev)
12768{
fbee40df 12769 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12770 struct intel_crtc *crtc;
12771 struct intel_dpll_hw_state dpll_hw_state;
12772 int i;
5358901f
DV
12773
12774 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12775 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12776 int enabled_crtcs = 0, active_crtcs = 0;
12777 bool active;
12778
12779 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12780
12781 DRM_DEBUG_KMS("%s\n", pll->name);
12782
12783 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12784
e2c719b7 12785 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12786 "more active pll users than references: %i vs %i\n",
3e369b76 12787 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12788 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12789 "pll in active use but not on in sw tracking\n");
e2c719b7 12790 I915_STATE_WARN(pll->on && !pll->active,
35c95375 12791 "pll in on but not on in use in sw tracking\n");
e2c719b7 12792 I915_STATE_WARN(pll->on != active,
5358901f
DV
12793 "pll on state mismatch (expected %i, found %i)\n",
12794 pll->on, active);
12795
d3fcc808 12796 for_each_intel_crtc(dev, crtc) {
83d65738 12797 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
12798 enabled_crtcs++;
12799 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12800 active_crtcs++;
12801 }
e2c719b7 12802 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
12803 "pll active crtcs mismatch (expected %i, found %i)\n",
12804 pll->active, active_crtcs);
e2c719b7 12805 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 12806 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 12807 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 12808
e2c719b7 12809 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12810 sizeof(dpll_hw_state)),
12811 "pll hw state mismatch\n");
5358901f 12812 }
8af6cf88
DV
12813}
12814
91d1b4bd
DV
12815void
12816intel_modeset_check_state(struct drm_device *dev)
12817{
08db6652 12818 check_wm_state(dev);
91d1b4bd
DV
12819 check_connector_state(dev);
12820 check_encoder_state(dev);
12821 check_crtc_state(dev);
12822 check_shared_dpll_state(dev);
12823}
12824
5cec258b 12825void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
12826 int dotclock)
12827{
12828 /*
12829 * FDI already provided one idea for the dotclock.
12830 * Yell if the encoder disagrees.
12831 */
2d112de7 12832 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 12833 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 12834 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
12835}
12836
80715b2f
VS
12837static void update_scanline_offset(struct intel_crtc *crtc)
12838{
12839 struct drm_device *dev = crtc->base.dev;
12840
12841 /*
12842 * The scanline counter increments at the leading edge of hsync.
12843 *
12844 * On most platforms it starts counting from vtotal-1 on the
12845 * first active line. That means the scanline counter value is
12846 * always one less than what we would expect. Ie. just after
12847 * start of vblank, which also occurs at start of hsync (on the
12848 * last active line), the scanline counter will read vblank_start-1.
12849 *
12850 * On gen2 the scanline counter starts counting from 1 instead
12851 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12852 * to keep the value positive), instead of adding one.
12853 *
12854 * On HSW+ the behaviour of the scanline counter depends on the output
12855 * type. For DP ports it behaves like most other platforms, but on HDMI
12856 * there's an extra 1 line difference. So we need to add two instead of
12857 * one to the value.
12858 */
12859 if (IS_GEN2(dev)) {
6e3c9717 12860 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12861 int vtotal;
12862
12863 vtotal = mode->crtc_vtotal;
12864 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12865 vtotal /= 2;
12866
12867 crtc->scanline_offset = vtotal - 1;
12868 } else if (HAS_DDI(dev) &&
409ee761 12869 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12870 crtc->scanline_offset = 2;
12871 } else
12872 crtc->scanline_offset = 1;
12873}
12874
ad421372 12875static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12876{
225da59b 12877 struct drm_device *dev = state->dev;
ed6739ef 12878 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 12879 struct intel_shared_dpll_config *shared_dpll = NULL;
ed6739ef 12880 struct intel_crtc *intel_crtc;
0a9ab303
ACO
12881 struct intel_crtc_state *intel_crtc_state;
12882 struct drm_crtc *crtc;
12883 struct drm_crtc_state *crtc_state;
0a9ab303 12884 int i;
ed6739ef
ACO
12885
12886 if (!dev_priv->display.crtc_compute_clock)
ad421372 12887 return;
ed6739ef 12888
0a9ab303 12889 for_each_crtc_in_state(state, crtc, crtc_state, i) {
ad421372
ML
12890 int dpll;
12891
0a9ab303 12892 intel_crtc = to_intel_crtc(crtc);
4978cc93 12893 intel_crtc_state = to_intel_crtc_state(crtc_state);
ad421372 12894 dpll = intel_crtc_state->shared_dpll;
0a9ab303 12895
ad421372 12896 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
225da59b
ACO
12897 continue;
12898
ad421372 12899 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
0a9ab303 12900
ad421372
ML
12901 if (!shared_dpll)
12902 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 12903
ad421372
ML
12904 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
12905 }
ed6739ef
ACO
12906}
12907
99d736a2
ML
12908/*
12909 * This implements the workaround described in the "notes" section of the mode
12910 * set sequence documentation. When going from no pipes or single pipe to
12911 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12912 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12913 */
12914static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12915{
12916 struct drm_crtc_state *crtc_state;
12917 struct intel_crtc *intel_crtc;
12918 struct drm_crtc *crtc;
12919 struct intel_crtc_state *first_crtc_state = NULL;
12920 struct intel_crtc_state *other_crtc_state = NULL;
12921 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12922 int i;
12923
12924 /* look at all crtc's that are going to be enabled in during modeset */
12925 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12926 intel_crtc = to_intel_crtc(crtc);
12927
12928 if (!crtc_state->active || !needs_modeset(crtc_state))
12929 continue;
12930
12931 if (first_crtc_state) {
12932 other_crtc_state = to_intel_crtc_state(crtc_state);
12933 break;
12934 } else {
12935 first_crtc_state = to_intel_crtc_state(crtc_state);
12936 first_pipe = intel_crtc->pipe;
12937 }
12938 }
12939
12940 /* No workaround needed? */
12941 if (!first_crtc_state)
12942 return 0;
12943
12944 /* w/a possibly needed, check how many crtc's are already enabled. */
12945 for_each_intel_crtc(state->dev, intel_crtc) {
12946 struct intel_crtc_state *pipe_config;
12947
12948 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12949 if (IS_ERR(pipe_config))
12950 return PTR_ERR(pipe_config);
12951
12952 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12953
12954 if (!pipe_config->base.active ||
12955 needs_modeset(&pipe_config->base))
12956 continue;
12957
12958 /* 2 or more enabled crtcs means no need for w/a */
12959 if (enabled_pipe != INVALID_PIPE)
12960 return 0;
12961
12962 enabled_pipe = intel_crtc->pipe;
12963 }
12964
12965 if (enabled_pipe != INVALID_PIPE)
12966 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12967 else if (other_crtc_state)
12968 other_crtc_state->hsw_workaround_pipe = first_pipe;
12969
12970 return 0;
12971}
12972
27c329ed
ML
12973static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12974{
12975 struct drm_crtc *crtc;
12976 struct drm_crtc_state *crtc_state;
12977 int ret = 0;
12978
12979 /* add all active pipes to the state */
12980 for_each_crtc(state->dev, crtc) {
12981 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12982 if (IS_ERR(crtc_state))
12983 return PTR_ERR(crtc_state);
12984
12985 if (!crtc_state->active || needs_modeset(crtc_state))
12986 continue;
12987
12988 crtc_state->mode_changed = true;
12989
12990 ret = drm_atomic_add_affected_connectors(state, crtc);
12991 if (ret)
12992 break;
12993
12994 ret = drm_atomic_add_affected_planes(state, crtc);
12995 if (ret)
12996 break;
12997 }
12998
12999 return ret;
13000}
13001
13002
054518dd 13003/* Code that should eventually be part of atomic_check() */
c347a676 13004static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd
ACO
13005{
13006 struct drm_device *dev = state->dev;
27c329ed 13007 struct drm_i915_private *dev_priv = dev->dev_private;
054518dd
ACO
13008 int ret;
13009
b359283a
ML
13010 if (!check_digital_port_conflicts(state)) {
13011 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13012 return -EINVAL;
13013 }
13014
054518dd
ACO
13015 /*
13016 * See if the config requires any additional preparation, e.g.
13017 * to adjust global state with pipes off. We need to do this
13018 * here so we can get the modeset_pipe updated config for the new
13019 * mode set on this crtc. For other crtcs we need to use the
13020 * adjusted_mode bits in the crtc directly.
13021 */
27c329ed
ML
13022 if (dev_priv->display.modeset_calc_cdclk) {
13023 unsigned int cdclk;
b432e5cf 13024
27c329ed
ML
13025 ret = dev_priv->display.modeset_calc_cdclk(state);
13026
13027 cdclk = to_intel_atomic_state(state)->cdclk;
13028 if (!ret && cdclk != dev_priv->cdclk_freq)
13029 ret = intel_modeset_all_pipes(state);
13030
13031 if (ret < 0)
054518dd 13032 return ret;
27c329ed
ML
13033 } else
13034 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
054518dd 13035
ad421372 13036 intel_modeset_clear_plls(state);
054518dd 13037
99d736a2 13038 if (IS_HASWELL(dev))
ad421372 13039 return haswell_mode_set_planes_workaround(state);
99d736a2 13040
ad421372 13041 return 0;
c347a676
ACO
13042}
13043
13044static int
13045intel_modeset_compute_config(struct drm_atomic_state *state)
13046{
13047 struct drm_crtc *crtc;
13048 struct drm_crtc_state *crtc_state;
13049 int ret, i;
61333b60 13050 bool any_ms = false;
c347a676
ACO
13051
13052 ret = drm_atomic_helper_check_modeset(state->dev, state);
054518dd
ACO
13053 if (ret)
13054 return ret;
13055
c347a676 13056 for_each_crtc_in_state(state, crtc, crtc_state, i) {
61333b60
ML
13057 if (!crtc_state->enable) {
13058 if (needs_modeset(crtc_state))
13059 any_ms = true;
c347a676 13060 continue;
61333b60 13061 }
c347a676 13062
d032ffa0
ML
13063 if (to_intel_crtc_state(crtc_state)->quirks &
13064 PIPE_CONFIG_QUIRK_INITIAL_PLANES) {
13065 ret = drm_atomic_add_affected_planes(state, crtc);
13066 if (ret)
13067 return ret;
13068
13069 /*
13070 * We ought to handle i915.fastboot here.
13071 * If no modeset is required and the primary plane has
13072 * a fb, update the members of crtc_state as needed,
13073 * and run the necessary updates during vblank evasion.
13074 */
13075 }
13076
b359283a
ML
13077 if (!needs_modeset(crtc_state)) {
13078 ret = drm_atomic_add_affected_connectors(state, crtc);
13079 if (ret)
13080 return ret;
13081 }
13082
13083 ret = intel_modeset_pipe_config(crtc,
13084 to_intel_crtc_state(crtc_state));
c347a676
ACO
13085 if (ret)
13086 return ret;
13087
61333b60
ML
13088 if (needs_modeset(crtc_state))
13089 any_ms = true;
13090
c347a676
ACO
13091 intel_dump_pipe_config(to_intel_crtc(crtc),
13092 to_intel_crtc_state(crtc_state),
13093 "[modeset]");
13094 }
13095
61333b60
ML
13096 if (any_ms) {
13097 ret = intel_modeset_checks(state);
13098
13099 if (ret)
13100 return ret;
27c329ed
ML
13101 } else
13102 to_intel_atomic_state(state)->cdclk =
13103 to_i915(state->dev)->cdclk_freq;
c347a676
ACO
13104
13105 return drm_atomic_helper_check_planes(state->dev, state);
054518dd
ACO
13106}
13107
c72d969b 13108static int __intel_set_mode(struct drm_atomic_state *state)
a6778b3c 13109{
c72d969b 13110 struct drm_device *dev = state->dev;
fbee40df 13111 struct drm_i915_private *dev_priv = dev->dev_private;
0a9ab303
ACO
13112 struct drm_crtc *crtc;
13113 struct drm_crtc_state *crtc_state;
c0c36b94 13114 int ret = 0;
0a9ab303 13115 int i;
61333b60 13116 bool any_ms = false;
a6778b3c 13117
d4afb8cc
ACO
13118 ret = drm_atomic_helper_prepare_planes(dev, state);
13119 if (ret)
13120 return ret;
13121
1c5e19f8
ML
13122 drm_atomic_helper_swap_state(dev, state);
13123
0a9ab303 13124 for_each_crtc_in_state(state, crtc, crtc_state, i) {
a539205a
ML
13125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13126
61333b60
ML
13127 if (!needs_modeset(crtc->state))
13128 continue;
13129
852eb00d
VS
13130 intel_pre_plane_update(intel_crtc);
13131
61333b60 13132 any_ms = true;
a539205a 13133 intel_pre_plane_update(intel_crtc);
460da916 13134
a539205a
ML
13135 if (crtc_state->active) {
13136 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13137 dev_priv->display.crtc_disable(crtc);
eddfcbcd
ML
13138 intel_crtc->active = false;
13139 intel_disable_shared_dpll(intel_crtc);
a539205a 13140 }
b8cecdf5 13141 }
7758a113 13142
ea9d758d
DV
13143 /* Only after disabling all output pipelines that will be changed can we
13144 * update the the output configuration. */
0a9ab303 13145 intel_modeset_update_state(state);
f6e5b160 13146
a821fc46
ACO
13147 /* The state has been swaped above, so state actually contains the
13148 * old state now. */
61333b60
ML
13149 if (any_ms)
13150 modeset_update_crtc_power_domains(state);
47fab737 13151
a6778b3c 13152 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
0a9ab303 13153 for_each_crtc_in_state(state, crtc, crtc_state, i) {
a539205a
ML
13154 if (needs_modeset(crtc->state) && crtc->state->active) {
13155 update_scanline_offset(to_intel_crtc(crtc));
13156 dev_priv->display.crtc_enable(crtc);
13157 }
80715b2f 13158
a539205a 13159 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
80715b2f 13160 }
a6778b3c 13161
a6778b3c 13162 /* FIXME: add subpixel order */
83a57153 13163
d4afb8cc
ACO
13164 drm_atomic_helper_cleanup_planes(dev, state);
13165
2bfb4627
ACO
13166 drm_atomic_state_free(state);
13167
9eb45f22 13168 return 0;
f6e5b160
CW
13169}
13170
568c634a 13171static int intel_set_mode_checked(struct drm_atomic_state *state)
f30da187 13172{
568c634a 13173 struct drm_device *dev = state->dev;
f30da187
DV
13174 int ret;
13175
568c634a 13176 ret = __intel_set_mode(state);
f30da187 13177 if (ret == 0)
568c634a 13178 intel_modeset_check_state(dev);
f30da187
DV
13179
13180 return ret;
13181}
13182
568c634a 13183static int intel_set_mode(struct drm_atomic_state *state)
7f27126e 13184{
568c634a 13185 int ret;
83a57153 13186
568c634a 13187 ret = intel_modeset_compute_config(state);
83a57153 13188 if (ret)
568c634a 13189 return ret;
7f27126e 13190
568c634a 13191 return intel_set_mode_checked(state);
7f27126e
JB
13192}
13193
c0c36b94
CW
13194void intel_crtc_restore_mode(struct drm_crtc *crtc)
13195{
83a57153
ACO
13196 struct drm_device *dev = crtc->dev;
13197 struct drm_atomic_state *state;
13198 struct intel_encoder *encoder;
13199 struct intel_connector *connector;
13200 struct drm_connector_state *connector_state;
4be07317 13201 struct intel_crtc_state *crtc_state;
2bfb4627 13202 int ret;
83a57153
ACO
13203
13204 state = drm_atomic_state_alloc(dev);
13205 if (!state) {
13206 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13207 crtc->base.id);
13208 return;
13209 }
13210
13211 state->acquire_ctx = dev->mode_config.acquire_ctx;
13212
13213 /* The force restore path in the HW readout code relies on the staged
13214 * config still keeping the user requested config while the actual
13215 * state has been overwritten by the configuration read from HW. We
13216 * need to copy the staged config to the atomic state, otherwise the
13217 * mode set will just reapply the state the HW is already in. */
13218 for_each_intel_encoder(dev, encoder) {
13219 if (&encoder->new_crtc->base != crtc)
13220 continue;
13221
13222 for_each_intel_connector(dev, connector) {
13223 if (connector->new_encoder != encoder)
13224 continue;
13225
13226 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13227 if (IS_ERR(connector_state)) {
13228 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13229 connector->base.base.id,
13230 connector->base.name,
13231 PTR_ERR(connector_state));
13232 continue;
13233 }
13234
13235 connector_state->crtc = crtc;
13236 connector_state->best_encoder = &encoder->base;
13237 }
13238 }
13239
4ed9fb37
ACO
13240 crtc_state = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
13241 if (IS_ERR(crtc_state)) {
13242 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13243 crtc->base.id, PTR_ERR(crtc_state));
13244 drm_atomic_state_free(state);
13245 return;
13246 }
4be07317 13247
4ed9fb37
ACO
13248 crtc_state->base.active = crtc_state->base.enable =
13249 to_intel_crtc(crtc)->new_enabled;
8c7b5ccb 13250
4ed9fb37 13251 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
4be07317 13252
d3a40d1b
ACO
13253 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13254 crtc->primary->fb, crtc->x, crtc->y);
13255
568c634a 13256 ret = intel_set_mode(state);
2bfb4627
ACO
13257 if (ret)
13258 drm_atomic_state_free(state);
c0c36b94
CW
13259}
13260
25c5b266
DV
13261#undef for_each_intel_crtc_masked
13262
b7885264
ACO
13263static bool intel_connector_in_mode_set(struct intel_connector *connector,
13264 struct drm_mode_set *set)
13265{
13266 int ro;
13267
13268 for (ro = 0; ro < set->num_connectors; ro++)
13269 if (set->connectors[ro] == &connector->base)
13270 return true;
13271
13272 return false;
13273}
13274
2e431051 13275static int
9a935856
DV
13276intel_modeset_stage_output_state(struct drm_device *dev,
13277 struct drm_mode_set *set,
944b0c76 13278 struct drm_atomic_state *state)
50f56119 13279{
9a935856 13280 struct intel_connector *connector;
d5432a9d 13281 struct drm_connector *drm_connector;
944b0c76 13282 struct drm_connector_state *connector_state;
d5432a9d
ACO
13283 struct drm_crtc *crtc;
13284 struct drm_crtc_state *crtc_state;
13285 int i, ret;
50f56119 13286
9abdda74 13287 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
13288 * of connectors. For paranoia, double-check this. */
13289 WARN_ON(!set->fb && (set->num_connectors != 0));
13290 WARN_ON(set->fb && (set->num_connectors == 0));
13291
3a3371ff 13292 for_each_intel_connector(dev, connector) {
b7885264
ACO
13293 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13294
d5432a9d
ACO
13295 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13296 continue;
13297
13298 connector_state =
13299 drm_atomic_get_connector_state(state, &connector->base);
13300 if (IS_ERR(connector_state))
13301 return PTR_ERR(connector_state);
13302
b7885264
ACO
13303 if (in_mode_set) {
13304 int pipe = to_intel_crtc(set->crtc)->pipe;
d5432a9d
ACO
13305 connector_state->best_encoder =
13306 &intel_find_encoder(connector, pipe)->base;
50f56119
DV
13307 }
13308
d5432a9d 13309 if (connector->base.state->crtc != set->crtc)
b7885264
ACO
13310 continue;
13311
9a935856
DV
13312 /* If we disable the crtc, disable all its connectors. Also, if
13313 * the connector is on the changing crtc but not on the new
13314 * connector list, disable it. */
b7885264 13315 if (!set->fb || !in_mode_set) {
d5432a9d 13316 connector_state->best_encoder = NULL;
9a935856
DV
13317
13318 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13319 connector->base.base.id,
c23cc417 13320 connector->base.name);
9a935856 13321 }
50f56119 13322 }
9a935856 13323 /* connector->new_encoder is now updated for all connectors. */
50f56119 13324
d5432a9d
ACO
13325 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13326 connector = to_intel_connector(drm_connector);
13327
13328 if (!connector_state->best_encoder) {
13329 ret = drm_atomic_set_crtc_for_connector(connector_state,
13330 NULL);
13331 if (ret)
13332 return ret;
7668851f 13333
50f56119 13334 continue;
d5432a9d 13335 }
50f56119 13336
d5432a9d
ACO
13337 if (intel_connector_in_mode_set(connector, set)) {
13338 struct drm_crtc *crtc = connector->base.state->crtc;
13339
13340 /* If this connector was in a previous crtc, add it
13341 * to the state. We might need to disable it. */
13342 if (crtc) {
13343 crtc_state =
13344 drm_atomic_get_crtc_state(state, crtc);
13345 if (IS_ERR(crtc_state))
13346 return PTR_ERR(crtc_state);
13347 }
13348
13349 ret = drm_atomic_set_crtc_for_connector(connector_state,
13350 set->crtc);
13351 if (ret)
13352 return ret;
13353 }
50f56119
DV
13354
13355 /* Make sure the new CRTC will work with the encoder */
d5432a9d
ACO
13356 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13357 connector_state->crtc)) {
5e2b584e 13358 return -EINVAL;
50f56119 13359 }
944b0c76 13360
9a935856
DV
13361 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13362 connector->base.base.id,
c23cc417 13363 connector->base.name,
d5432a9d 13364 connector_state->crtc->base.id);
944b0c76 13365
d5432a9d
ACO
13366 if (connector_state->best_encoder != &connector->encoder->base)
13367 connector->encoder =
13368 to_intel_encoder(connector_state->best_encoder);
0e32b39c 13369 }
7668851f 13370
d5432a9d 13371 for_each_crtc_in_state(state, crtc, crtc_state, i) {
49d6fa21
ML
13372 bool has_connectors;
13373
d5432a9d
ACO
13374 ret = drm_atomic_add_affected_connectors(state, crtc);
13375 if (ret)
13376 return ret;
4be07317 13377
49d6fa21
ML
13378 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13379 if (has_connectors != crtc_state->enable)
13380 crtc_state->enable =
13381 crtc_state->active = has_connectors;
7668851f
VS
13382 }
13383
8c7b5ccb
ACO
13384 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13385 set->fb, set->x, set->y);
13386 if (ret)
13387 return ret;
13388
13389 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13390 if (IS_ERR(crtc_state))
13391 return PTR_ERR(crtc_state);
13392
ce52299c
MR
13393 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
13394 if (ret)
13395 return ret;
8c7b5ccb
ACO
13396
13397 if (set->num_connectors)
13398 crtc_state->active = true;
13399
2e431051
DV
13400 return 0;
13401}
13402
13403static int intel_crtc_set_config(struct drm_mode_set *set)
13404{
13405 struct drm_device *dev;
83a57153 13406 struct drm_atomic_state *state = NULL;
2e431051 13407 int ret;
2e431051 13408
8d3e375e
DV
13409 BUG_ON(!set);
13410 BUG_ON(!set->crtc);
13411 BUG_ON(!set->crtc->helper_private);
2e431051 13412
7e53f3a4
DV
13413 /* Enforce sane interface api - has been abused by the fb helper. */
13414 BUG_ON(!set->mode && set->fb);
13415 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 13416
2e431051
DV
13417 if (set->fb) {
13418 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13419 set->crtc->base.id, set->fb->base.id,
13420 (int)set->num_connectors, set->x, set->y);
13421 } else {
13422 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
13423 }
13424
13425 dev = set->crtc->dev;
13426
83a57153 13427 state = drm_atomic_state_alloc(dev);
7cbf41d6
ACO
13428 if (!state)
13429 return -ENOMEM;
83a57153
ACO
13430
13431 state->acquire_ctx = dev->mode_config.acquire_ctx;
13432
462a425a 13433 ret = intel_modeset_stage_output_state(dev, set, state);
2e431051 13434 if (ret)
7cbf41d6 13435 goto out;
2e431051 13436
568c634a
ACO
13437 ret = intel_modeset_compute_config(state);
13438 if (ret)
7cbf41d6 13439 goto out;
50f52756 13440
1f9954d0
JB
13441 intel_update_pipe_size(to_intel_crtc(set->crtc));
13442
568c634a 13443 ret = intel_set_mode_checked(state);
2d05eae1 13444 if (ret) {
bf67dfeb
DV
13445 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13446 set->crtc->base.id, ret);
2d05eae1 13447 }
50f56119 13448
7cbf41d6 13449out:
2bfb4627
ACO
13450 if (ret)
13451 drm_atomic_state_free(state);
50f56119
DV
13452 return ret;
13453}
f6e5b160
CW
13454
13455static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13456 .gamma_set = intel_crtc_gamma_set,
50f56119 13457 .set_config = intel_crtc_set_config,
f6e5b160
CW
13458 .destroy = intel_crtc_destroy,
13459 .page_flip = intel_crtc_page_flip,
1356837e
MR
13460 .atomic_duplicate_state = intel_crtc_duplicate_state,
13461 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13462};
13463
5358901f
DV
13464static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13465 struct intel_shared_dpll *pll,
13466 struct intel_dpll_hw_state *hw_state)
ee7b9f93 13467{
5358901f 13468 uint32_t val;
ee7b9f93 13469
f458ebbc 13470 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
13471 return false;
13472
5358901f 13473 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
13474 hw_state->dpll = val;
13475 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13476 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
13477
13478 return val & DPLL_VCO_ENABLE;
13479}
13480
15bdd4cf
DV
13481static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13482 struct intel_shared_dpll *pll)
13483{
3e369b76
ACO
13484 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13485 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
13486}
13487
e7b903d2
DV
13488static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13489 struct intel_shared_dpll *pll)
13490{
e7b903d2 13491 /* PCH refclock must be enabled first */
89eff4be 13492 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 13493
3e369b76 13494 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
13495
13496 /* Wait for the clocks to stabilize. */
13497 POSTING_READ(PCH_DPLL(pll->id));
13498 udelay(150);
13499
13500 /* The pixel multiplier can only be updated once the
13501 * DPLL is enabled and the clocks are stable.
13502 *
13503 * So write it again.
13504 */
3e369b76 13505 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 13506 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13507 udelay(200);
13508}
13509
13510static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13511 struct intel_shared_dpll *pll)
13512{
13513 struct drm_device *dev = dev_priv->dev;
13514 struct intel_crtc *crtc;
e7b903d2
DV
13515
13516 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 13517 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
13518 if (intel_crtc_to_shared_dpll(crtc) == pll)
13519 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
13520 }
13521
15bdd4cf
DV
13522 I915_WRITE(PCH_DPLL(pll->id), 0);
13523 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13524 udelay(200);
13525}
13526
46edb027
DV
13527static char *ibx_pch_dpll_names[] = {
13528 "PCH DPLL A",
13529 "PCH DPLL B",
13530};
13531
7c74ade1 13532static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 13533{
e7b903d2 13534 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
13535 int i;
13536
7c74ade1 13537 dev_priv->num_shared_dpll = 2;
ee7b9f93 13538
e72f9fbf 13539 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
13540 dev_priv->shared_dplls[i].id = i;
13541 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 13542 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
13543 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13544 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
13545 dev_priv->shared_dplls[i].get_hw_state =
13546 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
13547 }
13548}
13549
7c74ade1
DV
13550static void intel_shared_dpll_init(struct drm_device *dev)
13551{
e7b903d2 13552 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 13553
b6283055
VS
13554 intel_update_cdclk(dev);
13555
9cd86933
DV
13556 if (HAS_DDI(dev))
13557 intel_ddi_pll_init(dev);
13558 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
13559 ibx_pch_dpll_init(dev);
13560 else
13561 dev_priv->num_shared_dpll = 0;
13562
13563 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
13564}
13565
6beb8c23
MR
13566/**
13567 * intel_prepare_plane_fb - Prepare fb for usage on plane
13568 * @plane: drm plane to prepare for
13569 * @fb: framebuffer to prepare for presentation
13570 *
13571 * Prepares a framebuffer for usage on a display plane. Generally this
13572 * involves pinning the underlying object and updating the frontbuffer tracking
13573 * bits. Some older platforms need special physical address handling for
13574 * cursor planes.
13575 *
13576 * Returns 0 on success, negative error code on failure.
13577 */
13578int
13579intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee
TU
13580 struct drm_framebuffer *fb,
13581 const struct drm_plane_state *new_state)
465c120c
MR
13582{
13583 struct drm_device *dev = plane->dev;
6beb8c23 13584 struct intel_plane *intel_plane = to_intel_plane(plane);
6beb8c23
MR
13585 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13586 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
6beb8c23 13587 int ret = 0;
465c120c 13588
ea2c67bb 13589 if (!obj)
465c120c
MR
13590 return 0;
13591
6beb8c23 13592 mutex_lock(&dev->struct_mutex);
465c120c 13593
6beb8c23
MR
13594 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13595 INTEL_INFO(dev)->cursor_needs_physical) {
13596 int align = IS_I830(dev) ? 16 * 1024 : 256;
13597 ret = i915_gem_object_attach_phys(obj, align);
13598 if (ret)
13599 DRM_DEBUG_KMS("failed to attach phys object\n");
13600 } else {
91af127f 13601 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
6beb8c23 13602 }
465c120c 13603
6beb8c23 13604 if (ret == 0)
a9ff8714 13605 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
fdd508a6 13606
4c34574f 13607 mutex_unlock(&dev->struct_mutex);
465c120c 13608
6beb8c23
MR
13609 return ret;
13610}
13611
38f3ce3a
MR
13612/**
13613 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13614 * @plane: drm plane to clean up for
13615 * @fb: old framebuffer that was on plane
13616 *
13617 * Cleans up a framebuffer that has just been removed from a plane.
13618 */
13619void
13620intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee
TU
13621 struct drm_framebuffer *fb,
13622 const struct drm_plane_state *old_state)
38f3ce3a
MR
13623{
13624 struct drm_device *dev = plane->dev;
13625 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13626
13627 if (WARN_ON(!obj))
13628 return;
13629
13630 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13631 !INTEL_INFO(dev)->cursor_needs_physical) {
13632 mutex_lock(&dev->struct_mutex);
82bc3b2d 13633 intel_unpin_fb_obj(fb, old_state);
38f3ce3a
MR
13634 mutex_unlock(&dev->struct_mutex);
13635 }
465c120c
MR
13636}
13637
6156a456
CK
13638int
13639skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13640{
13641 int max_scale;
13642 struct drm_device *dev;
13643 struct drm_i915_private *dev_priv;
13644 int crtc_clock, cdclk;
13645
13646 if (!intel_crtc || !crtc_state)
13647 return DRM_PLANE_HELPER_NO_SCALING;
13648
13649 dev = intel_crtc->base.dev;
13650 dev_priv = dev->dev_private;
13651 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 13652 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456
CK
13653
13654 if (!crtc_clock || !cdclk)
13655 return DRM_PLANE_HELPER_NO_SCALING;
13656
13657 /*
13658 * skl max scale is lower of:
13659 * close to 3 but not 3, -1 is for that purpose
13660 * or
13661 * cdclk/crtc_clock
13662 */
13663 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13664
13665 return max_scale;
13666}
13667
465c120c 13668static int
3c692a41 13669intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13670 struct intel_crtc_state *crtc_state,
3c692a41
GP
13671 struct intel_plane_state *state)
13672{
2b875c22
MR
13673 struct drm_crtc *crtc = state->base.crtc;
13674 struct drm_framebuffer *fb = state->base.fb;
6156a456 13675 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13676 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13677 bool can_position = false;
465c120c 13678
061e4b8d
ML
13679 /* use scaler when colorkey is not required */
13680 if (INTEL_INFO(plane->dev)->gen >= 9 &&
818ed961 13681 state->ckey.flags == I915_SET_COLORKEY_NONE) {
061e4b8d
ML
13682 min_scale = 1;
13683 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
d8106366 13684 can_position = true;
6156a456 13685 }
d8106366 13686
061e4b8d
ML
13687 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13688 &state->dst, &state->clip,
da20eabd
ML
13689 min_scale, max_scale,
13690 can_position, true,
13691 &state->visible);
14af293f
GP
13692}
13693
13694static void
13695intel_commit_primary_plane(struct drm_plane *plane,
13696 struct intel_plane_state *state)
13697{
2b875c22
MR
13698 struct drm_crtc *crtc = state->base.crtc;
13699 struct drm_framebuffer *fb = state->base.fb;
13700 struct drm_device *dev = plane->dev;
14af293f 13701 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 13702 struct intel_crtc *intel_crtc;
14af293f
GP
13703 struct drm_rect *src = &state->src;
13704
ea2c67bb
MR
13705 crtc = crtc ? crtc : plane->crtc;
13706 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
13707
13708 plane->fb = fb;
9dc806fc
MR
13709 crtc->x = src->x1 >> 16;
13710 crtc->y = src->y1 >> 16;
ccc759dc 13711
a539205a 13712 if (!crtc->state->active)
302d19ac 13713 return;
465c120c 13714
302d19ac
ML
13715 if (state->visible)
13716 /* FIXME: kill this fastboot hack */
13717 intel_update_pipe_size(intel_crtc);
13718
13719 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
465c120c
MR
13720}
13721
a8ad0d8e
ML
13722static void
13723intel_disable_primary_plane(struct drm_plane *plane,
7fabf5ef 13724 struct drm_crtc *crtc)
a8ad0d8e
ML
13725{
13726 struct drm_device *dev = plane->dev;
13727 struct drm_i915_private *dev_priv = dev->dev_private;
13728
a8ad0d8e
ML
13729 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13730}
13731
32b7eeec 13732static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 13733{
32b7eeec 13734 struct drm_device *dev = crtc->dev;
140fd38d 13735 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 13736 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3c692a41 13737
a539205a
ML
13738 if (!needs_modeset(crtc->state))
13739 intel_pre_plane_update(intel_crtc);
3c692a41 13740
f015c551 13741 if (intel_crtc->atomic.update_wm_pre)
32b7eeec 13742 intel_update_watermarks(crtc);
3c692a41 13743
32b7eeec 13744 intel_runtime_pm_get(dev_priv);
3c692a41 13745
c34c9ee4 13746 /* Perform vblank evasion around commit operation */
a539205a 13747 if (crtc->state->active)
c34c9ee4
MR
13748 intel_crtc->atomic.evade =
13749 intel_pipe_update_start(intel_crtc,
13750 &intel_crtc->atomic.start_vbl_count);
0583236e
ML
13751
13752 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13753 skl_detach_scalers(intel_crtc);
32b7eeec
MR
13754}
13755
13756static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13757{
13758 struct drm_device *dev = crtc->dev;
13759 struct drm_i915_private *dev_priv = dev->dev_private;
13760 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
32b7eeec 13761
c34c9ee4
MR
13762 if (intel_crtc->atomic.evade)
13763 intel_pipe_update_end(intel_crtc,
13764 intel_crtc->atomic.start_vbl_count);
3c692a41 13765
140fd38d 13766 intel_runtime_pm_put(dev_priv);
3c692a41 13767
ac21b225 13768 intel_post_plane_update(intel_crtc);
3c692a41
GP
13769}
13770
cf4c7c12 13771/**
4a3b8769
MR
13772 * intel_plane_destroy - destroy a plane
13773 * @plane: plane to destroy
cf4c7c12 13774 *
4a3b8769
MR
13775 * Common destruction function for all types of planes (primary, cursor,
13776 * sprite).
cf4c7c12 13777 */
4a3b8769 13778void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13779{
13780 struct intel_plane *intel_plane = to_intel_plane(plane);
13781 drm_plane_cleanup(plane);
13782 kfree(intel_plane);
13783}
13784
65a3fea0 13785const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13786 .update_plane = drm_atomic_helper_update_plane,
13787 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13788 .destroy = intel_plane_destroy,
c196e1d6 13789 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13790 .atomic_get_property = intel_plane_atomic_get_property,
13791 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13792 .atomic_duplicate_state = intel_plane_duplicate_state,
13793 .atomic_destroy_state = intel_plane_destroy_state,
13794
465c120c
MR
13795};
13796
13797static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13798 int pipe)
13799{
13800 struct intel_plane *primary;
8e7d688b 13801 struct intel_plane_state *state;
465c120c
MR
13802 const uint32_t *intel_primary_formats;
13803 int num_formats;
13804
13805 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13806 if (primary == NULL)
13807 return NULL;
13808
8e7d688b
MR
13809 state = intel_create_plane_state(&primary->base);
13810 if (!state) {
ea2c67bb
MR
13811 kfree(primary);
13812 return NULL;
13813 }
8e7d688b 13814 primary->base.state = &state->base;
ea2c67bb 13815
465c120c
MR
13816 primary->can_scale = false;
13817 primary->max_downscale = 1;
6156a456
CK
13818 if (INTEL_INFO(dev)->gen >= 9) {
13819 primary->can_scale = true;
af99ceda 13820 state->scaler_id = -1;
6156a456 13821 }
465c120c
MR
13822 primary->pipe = pipe;
13823 primary->plane = pipe;
a9ff8714 13824 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
c59cb179
MR
13825 primary->check_plane = intel_check_primary_plane;
13826 primary->commit_plane = intel_commit_primary_plane;
a8ad0d8e 13827 primary->disable_plane = intel_disable_primary_plane;
465c120c
MR
13828 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13829 primary->plane = !pipe;
13830
6c0fd451
DL
13831 if (INTEL_INFO(dev)->gen >= 9) {
13832 intel_primary_formats = skl_primary_formats;
13833 num_formats = ARRAY_SIZE(skl_primary_formats);
13834 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
13835 intel_primary_formats = i965_primary_formats;
13836 num_formats = ARRAY_SIZE(i965_primary_formats);
6c0fd451
DL
13837 } else {
13838 intel_primary_formats = i8xx_primary_formats;
13839 num_formats = ARRAY_SIZE(i8xx_primary_formats);
465c120c
MR
13840 }
13841
13842 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13843 &intel_plane_funcs,
465c120c
MR
13844 intel_primary_formats, num_formats,
13845 DRM_PLANE_TYPE_PRIMARY);
48404c1e 13846
3b7a5119
SJ
13847 if (INTEL_INFO(dev)->gen >= 4)
13848 intel_create_rotation_property(dev, primary);
48404c1e 13849
ea2c67bb
MR
13850 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13851
465c120c
MR
13852 return &primary->base;
13853}
13854
3b7a5119
SJ
13855void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13856{
13857 if (!dev->mode_config.rotation_property) {
13858 unsigned long flags = BIT(DRM_ROTATE_0) |
13859 BIT(DRM_ROTATE_180);
13860
13861 if (INTEL_INFO(dev)->gen >= 9)
13862 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13863
13864 dev->mode_config.rotation_property =
13865 drm_mode_create_rotation_property(dev, flags);
13866 }
13867 if (dev->mode_config.rotation_property)
13868 drm_object_attach_property(&plane->base.base,
13869 dev->mode_config.rotation_property,
13870 plane->base.state->rotation);
13871}
13872
3d7d6510 13873static int
852e787c 13874intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 13875 struct intel_crtc_state *crtc_state,
852e787c 13876 struct intel_plane_state *state)
3d7d6510 13877{
061e4b8d 13878 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 13879 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 13880 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
757f9a3e
GP
13881 unsigned stride;
13882 int ret;
3d7d6510 13883
061e4b8d
ML
13884 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13885 &state->dst, &state->clip,
3d7d6510
MR
13886 DRM_PLANE_HELPER_NO_SCALING,
13887 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13888 true, true, &state->visible);
757f9a3e
GP
13889 if (ret)
13890 return ret;
13891
757f9a3e
GP
13892 /* if we want to turn off the cursor ignore width and height */
13893 if (!obj)
da20eabd 13894 return 0;
757f9a3e 13895
757f9a3e 13896 /* Check for which cursor types we support */
061e4b8d 13897 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
13898 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13899 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13900 return -EINVAL;
13901 }
13902
ea2c67bb
MR
13903 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13904 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13905 DRM_DEBUG_KMS("buffer is too small\n");
13906 return -ENOMEM;
13907 }
13908
3a656b54 13909 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 13910 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 13911 return -EINVAL;
32b7eeec
MR
13912 }
13913
da20eabd 13914 return 0;
852e787c 13915}
3d7d6510 13916
a8ad0d8e
ML
13917static void
13918intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 13919 struct drm_crtc *crtc)
a8ad0d8e 13920{
a8ad0d8e
ML
13921 intel_crtc_update_cursor(crtc, false);
13922}
13923
f4a2cf29 13924static void
852e787c
GP
13925intel_commit_cursor_plane(struct drm_plane *plane,
13926 struct intel_plane_state *state)
13927{
2b875c22 13928 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
13929 struct drm_device *dev = plane->dev;
13930 struct intel_crtc *intel_crtc;
2b875c22 13931 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13932 uint32_t addr;
852e787c 13933
ea2c67bb
MR
13934 crtc = crtc ? crtc : plane->crtc;
13935 intel_crtc = to_intel_crtc(crtc);
13936
2b875c22 13937 plane->fb = state->base.fb;
ea2c67bb
MR
13938 crtc->cursor_x = state->base.crtc_x;
13939 crtc->cursor_y = state->base.crtc_y;
13940
a912f12f
GP
13941 if (intel_crtc->cursor_bo == obj)
13942 goto update;
4ed91096 13943
f4a2cf29 13944 if (!obj)
a912f12f 13945 addr = 0;
f4a2cf29 13946 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 13947 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 13948 else
a912f12f 13949 addr = obj->phys_handle->busaddr;
852e787c 13950
a912f12f
GP
13951 intel_crtc->cursor_addr = addr;
13952 intel_crtc->cursor_bo = obj;
852e787c 13953
302d19ac 13954update:
a539205a 13955 if (crtc->state->active)
a912f12f 13956 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
13957}
13958
3d7d6510
MR
13959static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13960 int pipe)
13961{
13962 struct intel_plane *cursor;
8e7d688b 13963 struct intel_plane_state *state;
3d7d6510
MR
13964
13965 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13966 if (cursor == NULL)
13967 return NULL;
13968
8e7d688b
MR
13969 state = intel_create_plane_state(&cursor->base);
13970 if (!state) {
ea2c67bb
MR
13971 kfree(cursor);
13972 return NULL;
13973 }
8e7d688b 13974 cursor->base.state = &state->base;
ea2c67bb 13975
3d7d6510
MR
13976 cursor->can_scale = false;
13977 cursor->max_downscale = 1;
13978 cursor->pipe = pipe;
13979 cursor->plane = pipe;
a9ff8714 13980 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
c59cb179
MR
13981 cursor->check_plane = intel_check_cursor_plane;
13982 cursor->commit_plane = intel_commit_cursor_plane;
a8ad0d8e 13983 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
13984
13985 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 13986 &intel_plane_funcs,
3d7d6510
MR
13987 intel_cursor_formats,
13988 ARRAY_SIZE(intel_cursor_formats),
13989 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
13990
13991 if (INTEL_INFO(dev)->gen >= 4) {
13992 if (!dev->mode_config.rotation_property)
13993 dev->mode_config.rotation_property =
13994 drm_mode_create_rotation_property(dev,
13995 BIT(DRM_ROTATE_0) |
13996 BIT(DRM_ROTATE_180));
13997 if (dev->mode_config.rotation_property)
13998 drm_object_attach_property(&cursor->base.base,
13999 dev->mode_config.rotation_property,
8e7d688b 14000 state->base.rotation);
4398ad45
VS
14001 }
14002
af99ceda
CK
14003 if (INTEL_INFO(dev)->gen >=9)
14004 state->scaler_id = -1;
14005
ea2c67bb
MR
14006 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14007
3d7d6510
MR
14008 return &cursor->base;
14009}
14010
549e2bfb
CK
14011static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14012 struct intel_crtc_state *crtc_state)
14013{
14014 int i;
14015 struct intel_scaler *intel_scaler;
14016 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14017
14018 for (i = 0; i < intel_crtc->num_scalers; i++) {
14019 intel_scaler = &scaler_state->scalers[i];
14020 intel_scaler->in_use = 0;
549e2bfb
CK
14021 intel_scaler->mode = PS_SCALER_MODE_DYN;
14022 }
14023
14024 scaler_state->scaler_id = -1;
14025}
14026
b358d0a6 14027static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14028{
fbee40df 14029 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14030 struct intel_crtc *intel_crtc;
f5de6e07 14031 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14032 struct drm_plane *primary = NULL;
14033 struct drm_plane *cursor = NULL;
465c120c 14034 int i, ret;
79e53945 14035
955382f3 14036 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14037 if (intel_crtc == NULL)
14038 return;
14039
f5de6e07
ACO
14040 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14041 if (!crtc_state)
14042 goto fail;
550acefd
ACO
14043 intel_crtc->config = crtc_state;
14044 intel_crtc->base.state = &crtc_state->base;
07878248 14045 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14046
549e2bfb
CK
14047 /* initialize shared scalers */
14048 if (INTEL_INFO(dev)->gen >= 9) {
14049 if (pipe == PIPE_C)
14050 intel_crtc->num_scalers = 1;
14051 else
14052 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14053
14054 skl_init_scalers(dev, intel_crtc, crtc_state);
14055 }
14056
465c120c 14057 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14058 if (!primary)
14059 goto fail;
14060
14061 cursor = intel_cursor_plane_create(dev, pipe);
14062 if (!cursor)
14063 goto fail;
14064
465c120c 14065 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
14066 cursor, &intel_crtc_funcs);
14067 if (ret)
14068 goto fail;
79e53945
JB
14069
14070 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
14071 for (i = 0; i < 256; i++) {
14072 intel_crtc->lut_r[i] = i;
14073 intel_crtc->lut_g[i] = i;
14074 intel_crtc->lut_b[i] = i;
14075 }
14076
1f1c2e24
VS
14077 /*
14078 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14079 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14080 */
80824003
JB
14081 intel_crtc->pipe = pipe;
14082 intel_crtc->plane = pipe;
3a77c4c4 14083 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14084 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14085 intel_crtc->plane = !pipe;
80824003
JB
14086 }
14087
4b0e333e
CW
14088 intel_crtc->cursor_base = ~0;
14089 intel_crtc->cursor_cntl = ~0;
dc41c154 14090 intel_crtc->cursor_size = ~0;
8d7849db 14091
852eb00d
VS
14092 intel_crtc->wm.cxsr_allowed = true;
14093
22fd0fab
JB
14094 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14095 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14096 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14097 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14098
79e53945 14099 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
14100
14101 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14102 return;
14103
14104fail:
14105 if (primary)
14106 drm_plane_cleanup(primary);
14107 if (cursor)
14108 drm_plane_cleanup(cursor);
f5de6e07 14109 kfree(crtc_state);
3d7d6510 14110 kfree(intel_crtc);
79e53945
JB
14111}
14112
752aa88a
JB
14113enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14114{
14115 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14116 struct drm_device *dev = connector->base.dev;
752aa88a 14117
51fd371b 14118 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14119
d3babd3f 14120 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14121 return INVALID_PIPE;
14122
14123 return to_intel_crtc(encoder->crtc)->pipe;
14124}
14125
08d7b3d1 14126int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14127 struct drm_file *file)
08d7b3d1 14128{
08d7b3d1 14129 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14130 struct drm_crtc *drmmode_crtc;
c05422d5 14131 struct intel_crtc *crtc;
08d7b3d1 14132
7707e653 14133 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14134
7707e653 14135 if (!drmmode_crtc) {
08d7b3d1 14136 DRM_ERROR("no such CRTC id\n");
3f2c2057 14137 return -ENOENT;
08d7b3d1
CW
14138 }
14139
7707e653 14140 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14141 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14142
c05422d5 14143 return 0;
08d7b3d1
CW
14144}
14145
66a9278e 14146static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14147{
66a9278e
DV
14148 struct drm_device *dev = encoder->base.dev;
14149 struct intel_encoder *source_encoder;
79e53945 14150 int index_mask = 0;
79e53945
JB
14151 int entry = 0;
14152
b2784e15 14153 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14154 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14155 index_mask |= (1 << entry);
14156
79e53945
JB
14157 entry++;
14158 }
4ef69c7a 14159
79e53945
JB
14160 return index_mask;
14161}
14162
4d302442
CW
14163static bool has_edp_a(struct drm_device *dev)
14164{
14165 struct drm_i915_private *dev_priv = dev->dev_private;
14166
14167 if (!IS_MOBILE(dev))
14168 return false;
14169
14170 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14171 return false;
14172
e3589908 14173 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14174 return false;
14175
14176 return true;
14177}
14178
84b4e042
JB
14179static bool intel_crt_present(struct drm_device *dev)
14180{
14181 struct drm_i915_private *dev_priv = dev->dev_private;
14182
884497ed
DL
14183 if (INTEL_INFO(dev)->gen >= 9)
14184 return false;
14185
cf404ce4 14186 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14187 return false;
14188
14189 if (IS_CHERRYVIEW(dev))
14190 return false;
14191
14192 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14193 return false;
14194
14195 return true;
14196}
14197
79e53945
JB
14198static void intel_setup_outputs(struct drm_device *dev)
14199{
725e30ad 14200 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14201 struct intel_encoder *encoder;
cb0953d7 14202 bool dpd_is_edp = false;
79e53945 14203
c9093354 14204 intel_lvds_init(dev);
79e53945 14205
84b4e042 14206 if (intel_crt_present(dev))
79935fca 14207 intel_crt_init(dev);
cb0953d7 14208
c776eb2e
VK
14209 if (IS_BROXTON(dev)) {
14210 /*
14211 * FIXME: Broxton doesn't support port detection via the
14212 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14213 * detect the ports.
14214 */
14215 intel_ddi_init(dev, PORT_A);
14216 intel_ddi_init(dev, PORT_B);
14217 intel_ddi_init(dev, PORT_C);
14218 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14219 int found;
14220
de31facd
JB
14221 /*
14222 * Haswell uses DDI functions to detect digital outputs.
14223 * On SKL pre-D0 the strap isn't connected, so we assume
14224 * it's there.
14225 */
0e72a5b5 14226 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
de31facd
JB
14227 /* WaIgnoreDDIAStrap: skl */
14228 if (found ||
14229 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
0e72a5b5
ED
14230 intel_ddi_init(dev, PORT_A);
14231
14232 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14233 * register */
14234 found = I915_READ(SFUSE_STRAP);
14235
14236 if (found & SFUSE_STRAP_DDIB_DETECTED)
14237 intel_ddi_init(dev, PORT_B);
14238 if (found & SFUSE_STRAP_DDIC_DETECTED)
14239 intel_ddi_init(dev, PORT_C);
14240 if (found & SFUSE_STRAP_DDID_DETECTED)
14241 intel_ddi_init(dev, PORT_D);
14242 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14243 int found;
5d8a7752 14244 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14245
14246 if (has_edp_a(dev))
14247 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14248
dc0fa718 14249 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14250 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 14251 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 14252 if (!found)
e2debe91 14253 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14254 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14255 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14256 }
14257
dc0fa718 14258 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14259 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14260
dc0fa718 14261 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14262 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14263
5eb08b69 14264 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14265 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14266
270b3042 14267 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14268 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 14269 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
14270 /*
14271 * The DP_DETECTED bit is the latched state of the DDC
14272 * SDA pin at boot. However since eDP doesn't require DDC
14273 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14274 * eDP ports may have been muxed to an alternate function.
14275 * Thus we can't rely on the DP_DETECTED bit alone to detect
14276 * eDP ports. Consult the VBT as well as DP_DETECTED to
14277 * detect eDP ports.
14278 */
d2182a66
VS
14279 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14280 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
14281 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14282 PORT_B);
e17ac6db
VS
14283 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14284 intel_dp_is_edp(dev, PORT_B))
14285 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 14286
d2182a66
VS
14287 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14288 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
14289 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14290 PORT_C);
e17ac6db
VS
14291 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14292 intel_dp_is_edp(dev, PORT_C))
14293 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 14294
9418c1f1 14295 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14296 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
14297 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14298 PORT_D);
e17ac6db
VS
14299 /* eDP not supported on port D, so don't check VBT */
14300 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14301 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
14302 }
14303
3cfca973 14304 intel_dsi_init(dev);
103a196f 14305 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 14306 bool found = false;
7d57382e 14307
e2debe91 14308 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14309 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 14310 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
14311 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14312 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14313 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14314 }
27185ae1 14315
e7281eab 14316 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14317 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14318 }
13520b05
KH
14319
14320 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14321
e2debe91 14322 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14323 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 14324 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 14325 }
27185ae1 14326
e2debe91 14327 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14328
b01f2c3a
JB
14329 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14330 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14331 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14332 }
e7281eab 14333 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14334 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14335 }
27185ae1 14336
b01f2c3a 14337 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 14338 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14339 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14340 } else if (IS_GEN2(dev))
79e53945
JB
14341 intel_dvo_init(dev);
14342
103a196f 14343 if (SUPPORTS_TV(dev))
79e53945
JB
14344 intel_tv_init(dev);
14345
0bc12bcb 14346 intel_psr_init(dev);
7c8f8a70 14347
b2784e15 14348 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14349 encoder->base.possible_crtcs = encoder->crtc_mask;
14350 encoder->base.possible_clones =
66a9278e 14351 intel_encoder_clones(encoder);
79e53945 14352 }
47356eb6 14353
dde86e2d 14354 intel_init_pch_refclk(dev);
270b3042
DV
14355
14356 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14357}
14358
14359static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14360{
60a5ca01 14361 struct drm_device *dev = fb->dev;
79e53945 14362 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14363
ef2d633e 14364 drm_framebuffer_cleanup(fb);
60a5ca01 14365 mutex_lock(&dev->struct_mutex);
ef2d633e 14366 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14367 drm_gem_object_unreference(&intel_fb->obj->base);
14368 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14369 kfree(intel_fb);
14370}
14371
14372static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14373 struct drm_file *file,
79e53945
JB
14374 unsigned int *handle)
14375{
14376 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14377 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14378
05394f39 14379 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14380}
14381
14382static const struct drm_framebuffer_funcs intel_fb_funcs = {
14383 .destroy = intel_user_framebuffer_destroy,
14384 .create_handle = intel_user_framebuffer_create_handle,
14385};
14386
b321803d
DL
14387static
14388u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14389 uint32_t pixel_format)
14390{
14391 u32 gen = INTEL_INFO(dev)->gen;
14392
14393 if (gen >= 9) {
14394 /* "The stride in bytes must not exceed the of the size of 8K
14395 * pixels and 32K bytes."
14396 */
14397 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14398 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14399 return 32*1024;
14400 } else if (gen >= 4) {
14401 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14402 return 16*1024;
14403 else
14404 return 32*1024;
14405 } else if (gen >= 3) {
14406 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14407 return 8*1024;
14408 else
14409 return 16*1024;
14410 } else {
14411 /* XXX DSPC is limited to 4k tiled */
14412 return 8*1024;
14413 }
14414}
14415
b5ea642a
DV
14416static int intel_framebuffer_init(struct drm_device *dev,
14417 struct intel_framebuffer *intel_fb,
14418 struct drm_mode_fb_cmd2 *mode_cmd,
14419 struct drm_i915_gem_object *obj)
79e53945 14420{
6761dd31 14421 unsigned int aligned_height;
79e53945 14422 int ret;
b321803d 14423 u32 pitch_limit, stride_alignment;
79e53945 14424
dd4916c5
DV
14425 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14426
2a80eada
DV
14427 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14428 /* Enforce that fb modifier and tiling mode match, but only for
14429 * X-tiled. This is needed for FBC. */
14430 if (!!(obj->tiling_mode == I915_TILING_X) !=
14431 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14432 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14433 return -EINVAL;
14434 }
14435 } else {
14436 if (obj->tiling_mode == I915_TILING_X)
14437 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14438 else if (obj->tiling_mode == I915_TILING_Y) {
14439 DRM_DEBUG("No Y tiling for legacy addfb\n");
14440 return -EINVAL;
14441 }
14442 }
14443
9a8f0a12
TU
14444 /* Passed in modifier sanity checking. */
14445 switch (mode_cmd->modifier[0]) {
14446 case I915_FORMAT_MOD_Y_TILED:
14447 case I915_FORMAT_MOD_Yf_TILED:
14448 if (INTEL_INFO(dev)->gen < 9) {
14449 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14450 mode_cmd->modifier[0]);
14451 return -EINVAL;
14452 }
14453 case DRM_FORMAT_MOD_NONE:
14454 case I915_FORMAT_MOD_X_TILED:
14455 break;
14456 default:
c0f40428
JB
14457 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14458 mode_cmd->modifier[0]);
57cd6508 14459 return -EINVAL;
c16ed4be 14460 }
57cd6508 14461
b321803d
DL
14462 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14463 mode_cmd->pixel_format);
14464 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14465 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14466 mode_cmd->pitches[0], stride_alignment);
57cd6508 14467 return -EINVAL;
c16ed4be 14468 }
57cd6508 14469
b321803d
DL
14470 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14471 mode_cmd->pixel_format);
a35cdaa0 14472 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14473 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14474 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14475 "tiled" : "linear",
a35cdaa0 14476 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14477 return -EINVAL;
c16ed4be 14478 }
5d7bd705 14479
2a80eada 14480 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14481 mode_cmd->pitches[0] != obj->stride) {
14482 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14483 mode_cmd->pitches[0], obj->stride);
5d7bd705 14484 return -EINVAL;
c16ed4be 14485 }
5d7bd705 14486
57779d06 14487 /* Reject formats not supported by any plane early. */
308e5bcb 14488 switch (mode_cmd->pixel_format) {
57779d06 14489 case DRM_FORMAT_C8:
04b3924d
VS
14490 case DRM_FORMAT_RGB565:
14491 case DRM_FORMAT_XRGB8888:
14492 case DRM_FORMAT_ARGB8888:
57779d06
VS
14493 break;
14494 case DRM_FORMAT_XRGB1555:
c16ed4be 14495 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14496 DRM_DEBUG("unsupported pixel format: %s\n",
14497 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14498 return -EINVAL;
c16ed4be 14499 }
57779d06 14500 break;
57779d06 14501 case DRM_FORMAT_ABGR8888:
6c0fd451
DL
14502 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14503 DRM_DEBUG("unsupported pixel format: %s\n",
14504 drm_get_format_name(mode_cmd->pixel_format));
14505 return -EINVAL;
14506 }
14507 break;
14508 case DRM_FORMAT_XBGR8888:
04b3924d 14509 case DRM_FORMAT_XRGB2101010:
57779d06 14510 case DRM_FORMAT_XBGR2101010:
c16ed4be 14511 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14512 DRM_DEBUG("unsupported pixel format: %s\n",
14513 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14514 return -EINVAL;
c16ed4be 14515 }
b5626747 14516 break;
7531208b
DL
14517 case DRM_FORMAT_ABGR2101010:
14518 if (!IS_VALLEYVIEW(dev)) {
14519 DRM_DEBUG("unsupported pixel format: %s\n",
14520 drm_get_format_name(mode_cmd->pixel_format));
14521 return -EINVAL;
14522 }
14523 break;
04b3924d
VS
14524 case DRM_FORMAT_YUYV:
14525 case DRM_FORMAT_UYVY:
14526 case DRM_FORMAT_YVYU:
14527 case DRM_FORMAT_VYUY:
c16ed4be 14528 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14529 DRM_DEBUG("unsupported pixel format: %s\n",
14530 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14531 return -EINVAL;
c16ed4be 14532 }
57cd6508
CW
14533 break;
14534 default:
4ee62c76
VS
14535 DRM_DEBUG("unsupported pixel format: %s\n",
14536 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14537 return -EINVAL;
14538 }
14539
90f9a336
VS
14540 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14541 if (mode_cmd->offsets[0] != 0)
14542 return -EINVAL;
14543
ec2c981e 14544 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14545 mode_cmd->pixel_format,
14546 mode_cmd->modifier[0]);
53155c0a
DV
14547 /* FIXME drm helper for size checks (especially planar formats)? */
14548 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14549 return -EINVAL;
14550
c7d73f6a
DV
14551 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14552 intel_fb->obj = obj;
80075d49 14553 intel_fb->obj->framebuffer_references++;
c7d73f6a 14554
79e53945
JB
14555 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14556 if (ret) {
14557 DRM_ERROR("framebuffer init failed %d\n", ret);
14558 return ret;
14559 }
14560
79e53945
JB
14561 return 0;
14562}
14563
79e53945
JB
14564static struct drm_framebuffer *
14565intel_user_framebuffer_create(struct drm_device *dev,
14566 struct drm_file *filp,
308e5bcb 14567 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14568{
05394f39 14569 struct drm_i915_gem_object *obj;
79e53945 14570
308e5bcb
JB
14571 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14572 mode_cmd->handles[0]));
c8725226 14573 if (&obj->base == NULL)
cce13ff7 14574 return ERR_PTR(-ENOENT);
79e53945 14575
d2dff872 14576 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
14577}
14578
4520f53a 14579#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 14580static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14581{
14582}
14583#endif
14584
79e53945 14585static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14586 .fb_create = intel_user_framebuffer_create,
0632fef6 14587 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14588 .atomic_check = intel_atomic_check,
14589 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14590 .atomic_state_alloc = intel_atomic_state_alloc,
14591 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14592};
14593
e70236a8
JB
14594/* Set up chip specific display functions */
14595static void intel_init_display(struct drm_device *dev)
14596{
14597 struct drm_i915_private *dev_priv = dev->dev_private;
14598
ee9300bb
DV
14599 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14600 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14601 else if (IS_CHERRYVIEW(dev))
14602 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14603 else if (IS_VALLEYVIEW(dev))
14604 dev_priv->display.find_dpll = vlv_find_best_dpll;
14605 else if (IS_PINEVIEW(dev))
14606 dev_priv->display.find_dpll = pnv_find_best_dpll;
14607 else
14608 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14609
bc8d7dff
DL
14610 if (INTEL_INFO(dev)->gen >= 9) {
14611 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14612 dev_priv->display.get_initial_plane_config =
14613 skylake_get_initial_plane_config;
bc8d7dff
DL
14614 dev_priv->display.crtc_compute_clock =
14615 haswell_crtc_compute_clock;
14616 dev_priv->display.crtc_enable = haswell_crtc_enable;
14617 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14618 dev_priv->display.update_primary_plane =
14619 skylake_update_primary_plane;
14620 } else if (HAS_DDI(dev)) {
0e8ffe1b 14621 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14622 dev_priv->display.get_initial_plane_config =
14623 ironlake_get_initial_plane_config;
797d0259
ACO
14624 dev_priv->display.crtc_compute_clock =
14625 haswell_crtc_compute_clock;
4f771f10
PZ
14626 dev_priv->display.crtc_enable = haswell_crtc_enable;
14627 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14628 dev_priv->display.update_primary_plane =
14629 ironlake_update_primary_plane;
09b4ddf9 14630 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14631 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14632 dev_priv->display.get_initial_plane_config =
14633 ironlake_get_initial_plane_config;
3fb37703
ACO
14634 dev_priv->display.crtc_compute_clock =
14635 ironlake_crtc_compute_clock;
76e5a89c
DV
14636 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14637 dev_priv->display.crtc_disable = ironlake_crtc_disable;
262ca2b0
MR
14638 dev_priv->display.update_primary_plane =
14639 ironlake_update_primary_plane;
89b667f8
JB
14640 } else if (IS_VALLEYVIEW(dev)) {
14641 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14642 dev_priv->display.get_initial_plane_config =
14643 i9xx_get_initial_plane_config;
d6dfee7a 14644 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14645 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14646 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14647 dev_priv->display.update_primary_plane =
14648 i9xx_update_primary_plane;
f564048e 14649 } else {
0e8ffe1b 14650 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14651 dev_priv->display.get_initial_plane_config =
14652 i9xx_get_initial_plane_config;
d6dfee7a 14653 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14654 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14655 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14656 dev_priv->display.update_primary_plane =
14657 i9xx_update_primary_plane;
f564048e 14658 }
e70236a8 14659
e70236a8 14660 /* Returns the core display clock speed */
1652d19e
VS
14661 if (IS_SKYLAKE(dev))
14662 dev_priv->display.get_display_clock_speed =
14663 skylake_get_display_clock_speed;
acd3f3d3
BP
14664 else if (IS_BROXTON(dev))
14665 dev_priv->display.get_display_clock_speed =
14666 broxton_get_display_clock_speed;
1652d19e
VS
14667 else if (IS_BROADWELL(dev))
14668 dev_priv->display.get_display_clock_speed =
14669 broadwell_get_display_clock_speed;
14670 else if (IS_HASWELL(dev))
14671 dev_priv->display.get_display_clock_speed =
14672 haswell_get_display_clock_speed;
14673 else if (IS_VALLEYVIEW(dev))
25eb05fc
JB
14674 dev_priv->display.get_display_clock_speed =
14675 valleyview_get_display_clock_speed;
b37a6434
VS
14676 else if (IS_GEN5(dev))
14677 dev_priv->display.get_display_clock_speed =
14678 ilk_get_display_clock_speed;
a7c66cd8 14679 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
34edce2f 14680 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
e70236a8
JB
14681 dev_priv->display.get_display_clock_speed =
14682 i945_get_display_clock_speed;
34edce2f
VS
14683 else if (IS_GM45(dev))
14684 dev_priv->display.get_display_clock_speed =
14685 gm45_get_display_clock_speed;
14686 else if (IS_CRESTLINE(dev))
14687 dev_priv->display.get_display_clock_speed =
14688 i965gm_get_display_clock_speed;
14689 else if (IS_PINEVIEW(dev))
14690 dev_priv->display.get_display_clock_speed =
14691 pnv_get_display_clock_speed;
14692 else if (IS_G33(dev) || IS_G4X(dev))
14693 dev_priv->display.get_display_clock_speed =
14694 g33_get_display_clock_speed;
e70236a8
JB
14695 else if (IS_I915G(dev))
14696 dev_priv->display.get_display_clock_speed =
14697 i915_get_display_clock_speed;
257a7ffc 14698 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14699 dev_priv->display.get_display_clock_speed =
14700 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
14701 else if (IS_PINEVIEW(dev))
14702 dev_priv->display.get_display_clock_speed =
14703 pnv_get_display_clock_speed;
e70236a8
JB
14704 else if (IS_I915GM(dev))
14705 dev_priv->display.get_display_clock_speed =
14706 i915gm_get_display_clock_speed;
14707 else if (IS_I865G(dev))
14708 dev_priv->display.get_display_clock_speed =
14709 i865_get_display_clock_speed;
f0f8a9ce 14710 else if (IS_I85X(dev))
e70236a8 14711 dev_priv->display.get_display_clock_speed =
1b1d2716 14712 i85x_get_display_clock_speed;
623e01e5
VS
14713 else { /* 830 */
14714 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14715 dev_priv->display.get_display_clock_speed =
14716 i830_get_display_clock_speed;
623e01e5 14717 }
e70236a8 14718
7c10a2b5 14719 if (IS_GEN5(dev)) {
3bb11b53 14720 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14721 } else if (IS_GEN6(dev)) {
14722 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14723 } else if (IS_IVYBRIDGE(dev)) {
14724 /* FIXME: detect B0+ stepping and use auto training */
14725 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14726 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14727 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
27c329ed
ML
14728 if (IS_BROADWELL(dev)) {
14729 dev_priv->display.modeset_commit_cdclk =
14730 broadwell_modeset_commit_cdclk;
14731 dev_priv->display.modeset_calc_cdclk =
14732 broadwell_modeset_calc_cdclk;
14733 }
30a970c6 14734 } else if (IS_VALLEYVIEW(dev)) {
27c329ed
ML
14735 dev_priv->display.modeset_commit_cdclk =
14736 valleyview_modeset_commit_cdclk;
14737 dev_priv->display.modeset_calc_cdclk =
14738 valleyview_modeset_calc_cdclk;
f8437dd1 14739 } else if (IS_BROXTON(dev)) {
27c329ed
ML
14740 dev_priv->display.modeset_commit_cdclk =
14741 broxton_modeset_commit_cdclk;
14742 dev_priv->display.modeset_calc_cdclk =
14743 broxton_modeset_calc_cdclk;
e70236a8 14744 }
8c9f3aaf 14745
8c9f3aaf
JB
14746 switch (INTEL_INFO(dev)->gen) {
14747 case 2:
14748 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14749 break;
14750
14751 case 3:
14752 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14753 break;
14754
14755 case 4:
14756 case 5:
14757 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14758 break;
14759
14760 case 6:
14761 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14762 break;
7c9017e5 14763 case 7:
4e0bbc31 14764 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14765 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14766 break;
830c81db 14767 case 9:
ba343e02
TU
14768 /* Drop through - unsupported since execlist only. */
14769 default:
14770 /* Default just returns -ENODEV to indicate unsupported */
14771 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14772 }
7bd688cd
JN
14773
14774 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
14775
14776 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
14777}
14778
b690e96c
JB
14779/*
14780 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14781 * resume, or other times. This quirk makes sure that's the case for
14782 * affected systems.
14783 */
0206e353 14784static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14785{
14786 struct drm_i915_private *dev_priv = dev->dev_private;
14787
14788 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14789 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14790}
14791
b6b5d049
VS
14792static void quirk_pipeb_force(struct drm_device *dev)
14793{
14794 struct drm_i915_private *dev_priv = dev->dev_private;
14795
14796 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14797 DRM_INFO("applying pipe b force quirk\n");
14798}
14799
435793df
KP
14800/*
14801 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14802 */
14803static void quirk_ssc_force_disable(struct drm_device *dev)
14804{
14805 struct drm_i915_private *dev_priv = dev->dev_private;
14806 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14807 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14808}
14809
4dca20ef 14810/*
5a15ab5b
CE
14811 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14812 * brightness value
4dca20ef
CE
14813 */
14814static void quirk_invert_brightness(struct drm_device *dev)
14815{
14816 struct drm_i915_private *dev_priv = dev->dev_private;
14817 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14818 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14819}
14820
9c72cc6f
SD
14821/* Some VBT's incorrectly indicate no backlight is present */
14822static void quirk_backlight_present(struct drm_device *dev)
14823{
14824 struct drm_i915_private *dev_priv = dev->dev_private;
14825 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14826 DRM_INFO("applying backlight present quirk\n");
14827}
14828
b690e96c
JB
14829struct intel_quirk {
14830 int device;
14831 int subsystem_vendor;
14832 int subsystem_device;
14833 void (*hook)(struct drm_device *dev);
14834};
14835
5f85f176
EE
14836/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14837struct intel_dmi_quirk {
14838 void (*hook)(struct drm_device *dev);
14839 const struct dmi_system_id (*dmi_id_list)[];
14840};
14841
14842static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14843{
14844 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14845 return 1;
14846}
14847
14848static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14849 {
14850 .dmi_id_list = &(const struct dmi_system_id[]) {
14851 {
14852 .callback = intel_dmi_reverse_brightness,
14853 .ident = "NCR Corporation",
14854 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14855 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14856 },
14857 },
14858 { } /* terminating entry */
14859 },
14860 .hook = quirk_invert_brightness,
14861 },
14862};
14863
c43b5634 14864static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14865 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14866 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14867
b690e96c
JB
14868 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14869 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14870
5f080c0f
VS
14871 /* 830 needs to leave pipe A & dpll A up */
14872 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14873
b6b5d049
VS
14874 /* 830 needs to leave pipe B & dpll B up */
14875 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14876
435793df
KP
14877 /* Lenovo U160 cannot use SSC on LVDS */
14878 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14879
14880 /* Sony Vaio Y cannot use SSC on LVDS */
14881 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14882
be505f64
AH
14883 /* Acer Aspire 5734Z must invert backlight brightness */
14884 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14885
14886 /* Acer/eMachines G725 */
14887 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14888
14889 /* Acer/eMachines e725 */
14890 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14891
14892 /* Acer/Packard Bell NCL20 */
14893 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14894
14895 /* Acer Aspire 4736Z */
14896 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14897
14898 /* Acer Aspire 5336 */
14899 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14900
14901 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14902 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14903
dfb3d47b
SD
14904 /* Acer C720 Chromebook (Core i3 4005U) */
14905 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14906
b2a9601c 14907 /* Apple Macbook 2,1 (Core 2 T7400) */
14908 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14909
d4967d8c
SD
14910 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14911 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14912
14913 /* HP Chromebook 14 (Celeron 2955U) */
14914 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14915
14916 /* Dell Chromebook 11 */
14917 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14918};
14919
14920static void intel_init_quirks(struct drm_device *dev)
14921{
14922 struct pci_dev *d = dev->pdev;
14923 int i;
14924
14925 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14926 struct intel_quirk *q = &intel_quirks[i];
14927
14928 if (d->device == q->device &&
14929 (d->subsystem_vendor == q->subsystem_vendor ||
14930 q->subsystem_vendor == PCI_ANY_ID) &&
14931 (d->subsystem_device == q->subsystem_device ||
14932 q->subsystem_device == PCI_ANY_ID))
14933 q->hook(dev);
14934 }
5f85f176
EE
14935 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14936 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14937 intel_dmi_quirks[i].hook(dev);
14938 }
b690e96c
JB
14939}
14940
9cce37f4
JB
14941/* Disable the VGA plane that we never use */
14942static void i915_disable_vga(struct drm_device *dev)
14943{
14944 struct drm_i915_private *dev_priv = dev->dev_private;
14945 u8 sr1;
766aa1c4 14946 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 14947
2b37c616 14948 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 14949 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14950 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14951 sr1 = inb(VGA_SR_DATA);
14952 outb(sr1 | 1<<5, VGA_SR_DATA);
14953 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14954 udelay(300);
14955
01f5a626 14956 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14957 POSTING_READ(vga_reg);
14958}
14959
f817586c
DV
14960void intel_modeset_init_hw(struct drm_device *dev)
14961{
b6283055 14962 intel_update_cdclk(dev);
a8f78b58 14963 intel_prepare_ddi(dev);
f817586c 14964 intel_init_clock_gating(dev);
8090c6b9 14965 intel_enable_gt_powersave(dev);
f817586c
DV
14966}
14967
79e53945
JB
14968void intel_modeset_init(struct drm_device *dev)
14969{
652c393a 14970 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 14971 int sprite, ret;
8cc87b75 14972 enum pipe pipe;
46f297fb 14973 struct intel_crtc *crtc;
79e53945
JB
14974
14975 drm_mode_config_init(dev);
14976
14977 dev->mode_config.min_width = 0;
14978 dev->mode_config.min_height = 0;
14979
019d96cb
DA
14980 dev->mode_config.preferred_depth = 24;
14981 dev->mode_config.prefer_shadow = 1;
14982
25bab385
TU
14983 dev->mode_config.allow_fb_modifiers = true;
14984
e6ecefaa 14985 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14986
b690e96c
JB
14987 intel_init_quirks(dev);
14988
1fa61106
ED
14989 intel_init_pm(dev);
14990
e3c74757
BW
14991 if (INTEL_INFO(dev)->num_pipes == 0)
14992 return;
14993
e70236a8 14994 intel_init_display(dev);
7c10a2b5 14995 intel_init_audio(dev);
e70236a8 14996
a6c45cf0
CW
14997 if (IS_GEN2(dev)) {
14998 dev->mode_config.max_width = 2048;
14999 dev->mode_config.max_height = 2048;
15000 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15001 dev->mode_config.max_width = 4096;
15002 dev->mode_config.max_height = 4096;
79e53945 15003 } else {
a6c45cf0
CW
15004 dev->mode_config.max_width = 8192;
15005 dev->mode_config.max_height = 8192;
79e53945 15006 }
068be561 15007
dc41c154
VS
15008 if (IS_845G(dev) || IS_I865G(dev)) {
15009 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15010 dev->mode_config.cursor_height = 1023;
15011 } else if (IS_GEN2(dev)) {
068be561
DL
15012 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15013 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15014 } else {
15015 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15016 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15017 }
15018
5d4545ae 15019 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 15020
28c97730 15021 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15022 INTEL_INFO(dev)->num_pipes,
15023 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15024
055e393f 15025 for_each_pipe(dev_priv, pipe) {
8cc87b75 15026 intel_crtc_init(dev, pipe);
3bdcfc0c 15027 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15028 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15029 if (ret)
06da8da2 15030 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15031 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15032 }
79e53945
JB
15033 }
15034
f42bb70d
JB
15035 intel_init_dpio(dev);
15036
e72f9fbf 15037 intel_shared_dpll_init(dev);
ee7b9f93 15038
9cce37f4
JB
15039 /* Just disable it once at startup */
15040 i915_disable_vga(dev);
79e53945 15041 intel_setup_outputs(dev);
11be49eb
CW
15042
15043 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 15044 intel_fbc_disable(dev);
fa9fa083 15045
6e9f798d 15046 drm_modeset_lock_all(dev);
fa9fa083 15047 intel_modeset_setup_hw_state(dev, false);
6e9f798d 15048 drm_modeset_unlock_all(dev);
46f297fb 15049
d3fcc808 15050 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
15051 if (!crtc->active)
15052 continue;
15053
46f297fb 15054 /*
46f297fb
JB
15055 * Note that reserving the BIOS fb up front prevents us
15056 * from stuffing other stolen allocations like the ring
15057 * on top. This prevents some ugliness at boot time, and
15058 * can even allow for smooth boot transitions if the BIOS
15059 * fb is large enough for the active pipe configuration.
15060 */
5724dbd1
DL
15061 if (dev_priv->display.get_initial_plane_config) {
15062 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
15063 &crtc->plane_config);
15064 /*
15065 * If the fb is shared between multiple heads, we'll
15066 * just get the first one.
15067 */
f6936e29 15068 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
46f297fb 15069 }
46f297fb 15070 }
2c7111db
CW
15071}
15072
7fad798e
DV
15073static void intel_enable_pipe_a(struct drm_device *dev)
15074{
15075 struct intel_connector *connector;
15076 struct drm_connector *crt = NULL;
15077 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15078 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15079
15080 /* We can't just switch on the pipe A, we need to set things up with a
15081 * proper mode and output configuration. As a gross hack, enable pipe A
15082 * by enabling the load detect pipe once. */
3a3371ff 15083 for_each_intel_connector(dev, connector) {
7fad798e
DV
15084 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15085 crt = &connector->base;
15086 break;
15087 }
15088 }
15089
15090 if (!crt)
15091 return;
15092
208bf9fd 15093 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15094 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15095}
15096
fa555837
DV
15097static bool
15098intel_check_plane_mapping(struct intel_crtc *crtc)
15099{
7eb552ae
BW
15100 struct drm_device *dev = crtc->base.dev;
15101 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
15102 u32 reg, val;
15103
7eb552ae 15104 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15105 return true;
15106
15107 reg = DSPCNTR(!crtc->plane);
15108 val = I915_READ(reg);
15109
15110 if ((val & DISPLAY_PLANE_ENABLE) &&
15111 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15112 return false;
15113
15114 return true;
15115}
15116
24929352
DV
15117static void intel_sanitize_crtc(struct intel_crtc *crtc)
15118{
15119 struct drm_device *dev = crtc->base.dev;
15120 struct drm_i915_private *dev_priv = dev->dev_private;
b17d48e2 15121 struct intel_encoder *encoder;
fa555837 15122 u32 reg;
b17d48e2 15123 bool enable;
24929352 15124
24929352 15125 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 15126 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
15127 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15128
d3eaf884 15129 /* restore vblank interrupts to correct state */
9625604c 15130 drm_crtc_vblank_reset(&crtc->base);
d297e103
VS
15131 if (crtc->active) {
15132 update_scanline_offset(crtc);
9625604c
DV
15133 drm_crtc_vblank_on(&crtc->base);
15134 }
d3eaf884 15135
24929352 15136 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15137 * disable the crtc (and hence change the state) if it is wrong. Note
15138 * that gen4+ has a fixed plane -> pipe mapping. */
15139 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15140 bool plane;
15141
24929352
DV
15142 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15143 crtc->base.base.id);
15144
15145 /* Pipe has the wrong plane attached and the plane is active.
15146 * Temporarily change the plane mapping and disable everything
15147 * ... */
15148 plane = crtc->plane;
b70709a6 15149 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15150 crtc->plane = !plane;
b17d48e2 15151 intel_crtc_disable_noatomic(&crtc->base);
24929352 15152 crtc->plane = plane;
24929352 15153 }
24929352 15154
7fad798e
DV
15155 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15156 crtc->pipe == PIPE_A && !crtc->active) {
15157 /* BIOS forgot to enable pipe A, this mostly happens after
15158 * resume. Force-enable the pipe to fix this, the update_dpms
15159 * call below we restore the pipe to the right state, but leave
15160 * the required bits on. */
15161 intel_enable_pipe_a(dev);
15162 }
15163
24929352
DV
15164 /* Adjust the state of the output pipe according to whether we
15165 * have active connectors/encoders. */
b17d48e2
ML
15166 enable = false;
15167 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15168 enable |= encoder->connectors_active;
24929352 15169
b17d48e2
ML
15170 if (!enable)
15171 intel_crtc_disable_noatomic(&crtc->base);
24929352 15172
53d9f4e9 15173 if (crtc->active != crtc->base.state->active) {
24929352
DV
15174
15175 /* This can happen either due to bugs in the get_hw_state
b17d48e2
ML
15176 * functions or because of calls to intel_crtc_disable_noatomic,
15177 * or because the pipe is force-enabled due to the
24929352
DV
15178 * pipe A quirk. */
15179 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15180 crtc->base.base.id,
83d65738 15181 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
15182 crtc->active ? "enabled" : "disabled");
15183
83d65738 15184 crtc->base.state->enable = crtc->active;
49d6fa21 15185 crtc->base.state->active = crtc->active;
24929352
DV
15186 crtc->base.enabled = crtc->active;
15187
15188 /* Because we only establish the connector -> encoder ->
15189 * crtc links if something is active, this means the
15190 * crtc is now deactivated. Break the links. connector
15191 * -> encoder links are only establish when things are
15192 * actually up, hence no need to break them. */
15193 WARN_ON(crtc->active);
15194
15195 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15196 WARN_ON(encoder->connectors_active);
15197 encoder->base.crtc = NULL;
15198 }
15199 }
c5ab3bc0 15200
a3ed6aad 15201 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15202 /*
15203 * We start out with underrun reporting disabled to avoid races.
15204 * For correct bookkeeping mark this on active crtcs.
15205 *
c5ab3bc0
DV
15206 * Also on gmch platforms we dont have any hardware bits to
15207 * disable the underrun reporting. Which means we need to start
15208 * out with underrun reporting disabled also on inactive pipes,
15209 * since otherwise we'll complain about the garbage we read when
15210 * e.g. coming up after runtime pm.
15211 *
4cc31489
DV
15212 * No protection against concurrent access is required - at
15213 * worst a fifo underrun happens which also sets this to false.
15214 */
15215 crtc->cpu_fifo_underrun_disabled = true;
15216 crtc->pch_fifo_underrun_disabled = true;
15217 }
24929352
DV
15218}
15219
15220static void intel_sanitize_encoder(struct intel_encoder *encoder)
15221{
15222 struct intel_connector *connector;
15223 struct drm_device *dev = encoder->base.dev;
15224
15225 /* We need to check both for a crtc link (meaning that the
15226 * encoder is active and trying to read from a pipe) and the
15227 * pipe itself being active. */
15228 bool has_active_crtc = encoder->base.crtc &&
15229 to_intel_crtc(encoder->base.crtc)->active;
15230
15231 if (encoder->connectors_active && !has_active_crtc) {
15232 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15233 encoder->base.base.id,
8e329a03 15234 encoder->base.name);
24929352
DV
15235
15236 /* Connector is active, but has no active pipe. This is
15237 * fallout from our resume register restoring. Disable
15238 * the encoder manually again. */
15239 if (encoder->base.crtc) {
15240 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15241 encoder->base.base.id,
8e329a03 15242 encoder->base.name);
24929352 15243 encoder->disable(encoder);
a62d1497
VS
15244 if (encoder->post_disable)
15245 encoder->post_disable(encoder);
24929352 15246 }
7f1950fb
EE
15247 encoder->base.crtc = NULL;
15248 encoder->connectors_active = false;
24929352
DV
15249
15250 /* Inconsistent output/port/pipe state happens presumably due to
15251 * a bug in one of the get_hw_state functions. Or someplace else
15252 * in our code, like the register restore mess on resume. Clamp
15253 * things to off as a safer default. */
3a3371ff 15254 for_each_intel_connector(dev, connector) {
24929352
DV
15255 if (connector->encoder != encoder)
15256 continue;
7f1950fb
EE
15257 connector->base.dpms = DRM_MODE_DPMS_OFF;
15258 connector->base.encoder = NULL;
24929352
DV
15259 }
15260 }
15261 /* Enabled encoders without active connectors will be fixed in
15262 * the crtc fixup. */
15263}
15264
04098753 15265void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15266{
15267 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 15268 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15269
04098753
ID
15270 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15271 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15272 i915_disable_vga(dev);
15273 }
15274}
15275
15276void i915_redisable_vga(struct drm_device *dev)
15277{
15278 struct drm_i915_private *dev_priv = dev->dev_private;
15279
8dc8a27c
PZ
15280 /* This function can be called both from intel_modeset_setup_hw_state or
15281 * at a very early point in our resume sequence, where the power well
15282 * structures are not yet restored. Since this function is at a very
15283 * paranoid "someone might have enabled VGA while we were not looking"
15284 * level, just check if the power well is enabled instead of trying to
15285 * follow the "don't touch the power well if we don't need it" policy
15286 * the rest of the driver uses. */
f458ebbc 15287 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15288 return;
15289
04098753 15290 i915_redisable_vga_power_on(dev);
0fde901f
KM
15291}
15292
98ec7739
VS
15293static bool primary_get_hw_state(struct intel_crtc *crtc)
15294{
15295 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15296
d032ffa0
ML
15297 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15298}
15299
15300static void readout_plane_state(struct intel_crtc *crtc,
15301 struct intel_crtc_state *crtc_state)
15302{
15303 struct intel_plane *p;
15304 struct drm_plane_state *drm_plane_state;
15305 bool active = crtc_state->base.active;
15306
15307 if (active) {
15308 crtc_state->quirks |= PIPE_CONFIG_QUIRK_INITIAL_PLANES;
15309
15310 /* apply to previous sw state too */
15311 to_intel_crtc_state(crtc->base.state)->quirks |=
15312 PIPE_CONFIG_QUIRK_INITIAL_PLANES;
15313 }
98ec7739 15314
d032ffa0
ML
15315 for_each_intel_plane(crtc->base.dev, p) {
15316 bool visible = active;
15317
15318 if (crtc->pipe != p->pipe)
15319 continue;
15320
15321 drm_plane_state = p->base.state;
15322 if (active && p->base.type == DRM_PLANE_TYPE_PRIMARY) {
15323 visible = primary_get_hw_state(crtc);
15324 to_intel_plane_state(drm_plane_state)->visible = visible;
15325 } else {
15326 /*
15327 * unknown state, assume it's off to force a transition
15328 * to on when calculating state changes.
15329 */
15330 to_intel_plane_state(drm_plane_state)->visible = false;
15331 }
15332
15333 if (visible) {
15334 crtc_state->base.plane_mask |=
15335 1 << drm_plane_index(&p->base);
15336 } else if (crtc_state->base.state) {
15337 /* Make this unconditional for atomic hw readout. */
15338 crtc_state->base.plane_mask &=
15339 ~(1 << drm_plane_index(&p->base));
15340 }
15341 }
98ec7739
VS
15342}
15343
30e984df 15344static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15345{
15346 struct drm_i915_private *dev_priv = dev->dev_private;
15347 enum pipe pipe;
24929352
DV
15348 struct intel_crtc *crtc;
15349 struct intel_encoder *encoder;
15350 struct intel_connector *connector;
5358901f 15351 int i;
24929352 15352
d3fcc808 15353 for_each_intel_crtc(dev, crtc) {
6e3c9717 15354 memset(crtc->config, 0, sizeof(*crtc->config));
f7217905 15355 crtc->config->base.crtc = &crtc->base;
3b117c8f 15356
6e3c9717 15357 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
9953599b 15358
0e8ffe1b 15359 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 15360 crtc->config);
24929352 15361
83d65738 15362 crtc->base.state->enable = crtc->active;
49d6fa21 15363 crtc->base.state->active = crtc->active;
24929352 15364 crtc->base.enabled = crtc->active;
b8b7fade 15365 crtc->base.hwmode = crtc->config->base.adjusted_mode;
b70709a6 15366
d032ffa0 15367 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
24929352
DV
15368
15369 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15370 crtc->base.base.id,
15371 crtc->active ? "enabled" : "disabled");
15372 }
15373
5358901f
DV
15374 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15375 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15376
3e369b76
ACO
15377 pll->on = pll->get_hw_state(dev_priv, pll,
15378 &pll->config.hw_state);
5358901f 15379 pll->active = 0;
3e369b76 15380 pll->config.crtc_mask = 0;
d3fcc808 15381 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 15382 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 15383 pll->active++;
3e369b76 15384 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 15385 }
5358901f 15386 }
5358901f 15387
1e6f2ddc 15388 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15389 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 15390
3e369b76 15391 if (pll->config.crtc_mask)
bd2bb1b9 15392 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
15393 }
15394
b2784e15 15395 for_each_intel_encoder(dev, encoder) {
24929352
DV
15396 pipe = 0;
15397
15398 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15399 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15400 encoder->base.crtc = &crtc->base;
6e3c9717 15401 encoder->get_config(encoder, crtc->config);
24929352
DV
15402 } else {
15403 encoder->base.crtc = NULL;
15404 }
15405
15406 encoder->connectors_active = false;
6f2bcceb 15407 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15408 encoder->base.base.id,
8e329a03 15409 encoder->base.name,
24929352 15410 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15411 pipe_name(pipe));
24929352
DV
15412 }
15413
3a3371ff 15414 for_each_intel_connector(dev, connector) {
24929352
DV
15415 if (connector->get_hw_state(connector)) {
15416 connector->base.dpms = DRM_MODE_DPMS_ON;
15417 connector->encoder->connectors_active = true;
15418 connector->base.encoder = &connector->encoder->base;
15419 } else {
15420 connector->base.dpms = DRM_MODE_DPMS_OFF;
15421 connector->base.encoder = NULL;
15422 }
15423 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15424 connector->base.base.id,
c23cc417 15425 connector->base.name,
24929352
DV
15426 connector->base.encoder ? "enabled" : "disabled");
15427 }
30e984df
DV
15428}
15429
15430/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15431 * and i915 state tracking structures. */
15432void intel_modeset_setup_hw_state(struct drm_device *dev,
15433 bool force_restore)
15434{
15435 struct drm_i915_private *dev_priv = dev->dev_private;
15436 enum pipe pipe;
30e984df
DV
15437 struct intel_crtc *crtc;
15438 struct intel_encoder *encoder;
35c95375 15439 int i;
30e984df
DV
15440
15441 intel_modeset_readout_hw_state(dev);
24929352 15442
babea61d
JB
15443 /*
15444 * Now that we have the config, copy it to each CRTC struct
15445 * Note that this could go away if we move to using crtc_config
15446 * checking everywhere.
15447 */
d3fcc808 15448 for_each_intel_crtc(dev, crtc) {
d330a953 15449 if (crtc->active && i915.fastboot) {
6e3c9717
ACO
15450 intel_mode_from_pipe_config(&crtc->base.mode,
15451 crtc->config);
babea61d
JB
15452 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15453 crtc->base.base.id);
15454 drm_mode_debug_printmodeline(&crtc->base.mode);
15455 }
15456 }
15457
24929352 15458 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15459 for_each_intel_encoder(dev, encoder) {
24929352
DV
15460 intel_sanitize_encoder(encoder);
15461 }
15462
055e393f 15463 for_each_pipe(dev_priv, pipe) {
24929352
DV
15464 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15465 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15466 intel_dump_pipe_config(crtc, crtc->config,
15467 "[setup_hw_state]");
24929352 15468 }
9a935856 15469
d29b2f9d
ACO
15470 intel_modeset_update_connector_atomic_state(dev);
15471
35c95375
DV
15472 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15473 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15474
15475 if (!pll->on || pll->active)
15476 continue;
15477
15478 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15479
15480 pll->disable(dev_priv, pll);
15481 pll->on = false;
15482 }
15483
26e1fe4f 15484 if (IS_VALLEYVIEW(dev))
6eb1a681
VS
15485 vlv_wm_get_hw_state(dev);
15486 else if (IS_GEN9(dev))
3078999f
PB
15487 skl_wm_get_hw_state(dev);
15488 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
15489 ilk_wm_get_hw_state(dev);
15490
45e2b5f6 15491 if (force_restore) {
7d0bc1ea
VS
15492 i915_redisable_vga(dev);
15493
f30da187
DV
15494 /*
15495 * We need to use raw interfaces for restoring state to avoid
15496 * checking (bogus) intermediate states.
15497 */
055e393f 15498 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
15499 struct drm_crtc *crtc =
15500 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187 15501
83a57153 15502 intel_crtc_restore_mode(crtc);
45e2b5f6
DV
15503 }
15504 } else {
15505 intel_modeset_update_staged_output_state(dev);
15506 }
8af6cf88
DV
15507
15508 intel_modeset_check_state(dev);
2c7111db
CW
15509}
15510
15511void intel_modeset_gem_init(struct drm_device *dev)
15512{
92122789 15513 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 15514 struct drm_crtc *c;
2ff8fde1 15515 struct drm_i915_gem_object *obj;
e0d6149b 15516 int ret;
484b41dd 15517
ae48434c
ID
15518 mutex_lock(&dev->struct_mutex);
15519 intel_init_gt_powersave(dev);
15520 mutex_unlock(&dev->struct_mutex);
15521
92122789
JB
15522 /*
15523 * There may be no VBT; and if the BIOS enabled SSC we can
15524 * just keep using it to avoid unnecessary flicker. Whereas if the
15525 * BIOS isn't using it, don't assume it will work even if the VBT
15526 * indicates as much.
15527 */
15528 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15529 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15530 DREF_SSC1_ENABLE);
15531
1833b134 15532 intel_modeset_init_hw(dev);
02e792fb
DV
15533
15534 intel_setup_overlay(dev);
484b41dd
JB
15535
15536 /*
15537 * Make sure any fbs we allocated at startup are properly
15538 * pinned & fenced. When we do the allocation it's too early
15539 * for this.
15540 */
70e1e0ec 15541 for_each_crtc(dev, c) {
2ff8fde1
MR
15542 obj = intel_fb_obj(c->primary->fb);
15543 if (obj == NULL)
484b41dd
JB
15544 continue;
15545
e0d6149b
TU
15546 mutex_lock(&dev->struct_mutex);
15547 ret = intel_pin_and_fence_fb_obj(c->primary,
15548 c->primary->fb,
15549 c->primary->state,
91af127f 15550 NULL, NULL);
e0d6149b
TU
15551 mutex_unlock(&dev->struct_mutex);
15552 if (ret) {
484b41dd
JB
15553 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15554 to_intel_crtc(c)->pipe);
66e514c1
DA
15555 drm_framebuffer_unreference(c->primary->fb);
15556 c->primary->fb = NULL;
36750f28 15557 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 15558 update_state_fb(c->primary);
36750f28 15559 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15560 }
15561 }
0962c3c9
VS
15562
15563 intel_backlight_register(dev);
79e53945
JB
15564}
15565
4932e2c3
ID
15566void intel_connector_unregister(struct intel_connector *intel_connector)
15567{
15568 struct drm_connector *connector = &intel_connector->base;
15569
15570 intel_panel_destroy_backlight(connector);
34ea3d38 15571 drm_connector_unregister(connector);
4932e2c3
ID
15572}
15573
79e53945
JB
15574void intel_modeset_cleanup(struct drm_device *dev)
15575{
652c393a 15576 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 15577 struct drm_connector *connector;
652c393a 15578
2eb5252e
ID
15579 intel_disable_gt_powersave(dev);
15580
0962c3c9
VS
15581 intel_backlight_unregister(dev);
15582
fd0c0642
DV
15583 /*
15584 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15585 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15586 * experience fancy races otherwise.
15587 */
2aeb7d3a 15588 intel_irq_uninstall(dev_priv);
eb21b92b 15589
fd0c0642
DV
15590 /*
15591 * Due to the hpd irq storm handling the hotplug work can re-arm the
15592 * poll handlers. Hence disable polling after hpd handling is shut down.
15593 */
f87ea761 15594 drm_kms_helper_poll_fini(dev);
fd0c0642 15595
652c393a
JB
15596 mutex_lock(&dev->struct_mutex);
15597
723bfd70
JB
15598 intel_unregister_dsm_handler();
15599
7ff0ebcc 15600 intel_fbc_disable(dev);
e70236a8 15601
69341a5e
KH
15602 mutex_unlock(&dev->struct_mutex);
15603
1630fe75
CW
15604 /* flush any delayed tasks or pending work */
15605 flush_scheduled_work();
15606
db31af1d
JN
15607 /* destroy the backlight and sysfs files before encoders/connectors */
15608 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
15609 struct intel_connector *intel_connector;
15610
15611 intel_connector = to_intel_connector(connector);
15612 intel_connector->unregister(intel_connector);
db31af1d 15613 }
d9255d57 15614
79e53945 15615 drm_mode_config_cleanup(dev);
4d7bb011
DV
15616
15617 intel_cleanup_overlay(dev);
ae48434c
ID
15618
15619 mutex_lock(&dev->struct_mutex);
15620 intel_cleanup_gt_powersave(dev);
15621 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15622}
15623
f1c79df3
ZW
15624/*
15625 * Return which encoder is currently attached for connector.
15626 */
df0e9248 15627struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15628{
df0e9248
CW
15629 return &intel_attached_encoder(connector)->base;
15630}
f1c79df3 15631
df0e9248
CW
15632void intel_connector_attach_encoder(struct intel_connector *connector,
15633 struct intel_encoder *encoder)
15634{
15635 connector->encoder = encoder;
15636 drm_mode_connector_attach_encoder(&connector->base,
15637 &encoder->base);
79e53945 15638}
28d52043
DA
15639
15640/*
15641 * set vga decode state - true == enable VGA decode
15642 */
15643int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15644{
15645 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15646 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15647 u16 gmch_ctrl;
15648
75fa041d
CW
15649 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15650 DRM_ERROR("failed to read control word\n");
15651 return -EIO;
15652 }
15653
c0cc8a55
CW
15654 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15655 return 0;
15656
28d52043
DA
15657 if (state)
15658 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15659 else
15660 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15661
15662 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15663 DRM_ERROR("failed to write control word\n");
15664 return -EIO;
15665 }
15666
28d52043
DA
15667 return 0;
15668}
c4a1d9e4 15669
c4a1d9e4 15670struct intel_display_error_state {
ff57f1b0
PZ
15671
15672 u32 power_well_driver;
15673
63b66e5b
CW
15674 int num_transcoders;
15675
c4a1d9e4
CW
15676 struct intel_cursor_error_state {
15677 u32 control;
15678 u32 position;
15679 u32 base;
15680 u32 size;
52331309 15681 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15682
15683 struct intel_pipe_error_state {
ddf9c536 15684 bool power_domain_on;
c4a1d9e4 15685 u32 source;
f301b1e1 15686 u32 stat;
52331309 15687 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15688
15689 struct intel_plane_error_state {
15690 u32 control;
15691 u32 stride;
15692 u32 size;
15693 u32 pos;
15694 u32 addr;
15695 u32 surface;
15696 u32 tile_offset;
52331309 15697 } plane[I915_MAX_PIPES];
63b66e5b
CW
15698
15699 struct intel_transcoder_error_state {
ddf9c536 15700 bool power_domain_on;
63b66e5b
CW
15701 enum transcoder cpu_transcoder;
15702
15703 u32 conf;
15704
15705 u32 htotal;
15706 u32 hblank;
15707 u32 hsync;
15708 u32 vtotal;
15709 u32 vblank;
15710 u32 vsync;
15711 } transcoder[4];
c4a1d9e4
CW
15712};
15713
15714struct intel_display_error_state *
15715intel_display_capture_error_state(struct drm_device *dev)
15716{
fbee40df 15717 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 15718 struct intel_display_error_state *error;
63b66e5b
CW
15719 int transcoders[] = {
15720 TRANSCODER_A,
15721 TRANSCODER_B,
15722 TRANSCODER_C,
15723 TRANSCODER_EDP,
15724 };
c4a1d9e4
CW
15725 int i;
15726
63b66e5b
CW
15727 if (INTEL_INFO(dev)->num_pipes == 0)
15728 return NULL;
15729
9d1cb914 15730 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15731 if (error == NULL)
15732 return NULL;
15733
190be112 15734 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
15735 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15736
055e393f 15737 for_each_pipe(dev_priv, i) {
ddf9c536 15738 error->pipe[i].power_domain_on =
f458ebbc
DV
15739 __intel_display_power_is_enabled(dev_priv,
15740 POWER_DOMAIN_PIPE(i));
ddf9c536 15741 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15742 continue;
15743
5efb3e28
VS
15744 error->cursor[i].control = I915_READ(CURCNTR(i));
15745 error->cursor[i].position = I915_READ(CURPOS(i));
15746 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15747
15748 error->plane[i].control = I915_READ(DSPCNTR(i));
15749 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 15750 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 15751 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15752 error->plane[i].pos = I915_READ(DSPPOS(i));
15753 }
ca291363
PZ
15754 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15755 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
15756 if (INTEL_INFO(dev)->gen >= 4) {
15757 error->plane[i].surface = I915_READ(DSPSURF(i));
15758 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15759 }
15760
c4a1d9e4 15761 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15762
3abfce77 15763 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 15764 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15765 }
15766
15767 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15768 if (HAS_DDI(dev_priv->dev))
15769 error->num_transcoders++; /* Account for eDP. */
15770
15771 for (i = 0; i < error->num_transcoders; i++) {
15772 enum transcoder cpu_transcoder = transcoders[i];
15773
ddf9c536 15774 error->transcoder[i].power_domain_on =
f458ebbc 15775 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15776 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15777 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15778 continue;
15779
63b66e5b
CW
15780 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15781
15782 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15783 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15784 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15785 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15786 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15787 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15788 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15789 }
15790
15791 return error;
15792}
15793
edc3d884
MK
15794#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15795
c4a1d9e4 15796void
edc3d884 15797intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15798 struct drm_device *dev,
15799 struct intel_display_error_state *error)
15800{
055e393f 15801 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15802 int i;
15803
63b66e5b
CW
15804 if (!error)
15805 return;
15806
edc3d884 15807 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15808 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15809 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15810 error->power_well_driver);
055e393f 15811 for_each_pipe(dev_priv, i) {
edc3d884 15812 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
15813 err_printf(m, " Power: %s\n",
15814 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 15815 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15816 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15817
15818 err_printf(m, "Plane [%d]:\n", i);
15819 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15820 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15821 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15822 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15823 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15824 }
4b71a570 15825 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15826 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15827 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15828 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15829 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15830 }
15831
edc3d884
MK
15832 err_printf(m, "Cursor [%d]:\n", i);
15833 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15834 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15835 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15836 }
63b66e5b
CW
15837
15838 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 15839 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 15840 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
15841 err_printf(m, " Power: %s\n",
15842 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
15843 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15844 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15845 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15846 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15847 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15848 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15849 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15850 }
c4a1d9e4 15851}
e2fcdaa9
VS
15852
15853void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15854{
15855 struct intel_crtc *crtc;
15856
15857 for_each_intel_crtc(dev, crtc) {
15858 struct intel_unpin_work *work;
e2fcdaa9 15859
5e2d7afc 15860 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
15861
15862 work = crtc->unpin_work;
15863
15864 if (work && work->event &&
15865 work->event->base.file_priv == file) {
15866 kfree(work->event);
15867 work->event = NULL;
15868 }
15869
5e2d7afc 15870 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
15871 }
15872}