drm/i915: reduce line width in {pch, i9xx}_get_hpd_pins()
[deliverable/linux.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
d4906093 644static bool
a93e255f
ACO
645i9xx_find_best_dpll(const intel_limit_t *limit,
646 struct intel_crtc_state *crtc_state,
cec2f356
SP
647 int target, int refclk, intel_clock_t *match_clock,
648 intel_clock_t *best_clock)
79e53945 649{
a93e255f 650 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 651 struct drm_device *dev = crtc->base.dev;
79e53945 652 intel_clock_t clock;
79e53945
JB
653 int err = target;
654
a93e255f 655 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 656 /*
a210b028
DV
657 * For LVDS just rely on its current settings for dual-channel.
658 * We haven't figured out how to reliably set up different
659 * single/dual channel state, if we even can.
79e53945 660 */
1974cad0 661 if (intel_is_dual_link_lvds(dev))
79e53945
JB
662 clock.p2 = limit->p2.p2_fast;
663 else
664 clock.p2 = limit->p2.p2_slow;
665 } else {
666 if (target < limit->p2.dot_limit)
667 clock.p2 = limit->p2.p2_slow;
668 else
669 clock.p2 = limit->p2.p2_fast;
670 }
671
0206e353 672 memset(best_clock, 0, sizeof(*best_clock));
79e53945 673
42158660
ZY
674 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
675 clock.m1++) {
676 for (clock.m2 = limit->m2.min;
677 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 678 if (clock.m2 >= clock.m1)
42158660
ZY
679 break;
680 for (clock.n = limit->n.min;
681 clock.n <= limit->n.max; clock.n++) {
682 for (clock.p1 = limit->p1.min;
683 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
684 int this_err;
685
ac58c3f0
DV
686 i9xx_clock(refclk, &clock);
687 if (!intel_PLL_is_valid(dev, limit,
688 &clock))
689 continue;
690 if (match_clock &&
691 clock.p != match_clock->p)
692 continue;
693
694 this_err = abs(clock.dot - target);
695 if (this_err < err) {
696 *best_clock = clock;
697 err = this_err;
698 }
699 }
700 }
701 }
702 }
703
704 return (err != target);
705}
706
707static bool
a93e255f
ACO
708pnv_find_best_dpll(const intel_limit_t *limit,
709 struct intel_crtc_state *crtc_state,
ee9300bb
DV
710 int target, int refclk, intel_clock_t *match_clock,
711 intel_clock_t *best_clock)
79e53945 712{
a93e255f 713 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 714 struct drm_device *dev = crtc->base.dev;
79e53945 715 intel_clock_t clock;
79e53945
JB
716 int err = target;
717
a93e255f 718 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 719 /*
a210b028
DV
720 * For LVDS just rely on its current settings for dual-channel.
721 * We haven't figured out how to reliably set up different
722 * single/dual channel state, if we even can.
79e53945 723 */
1974cad0 724 if (intel_is_dual_link_lvds(dev))
79e53945
JB
725 clock.p2 = limit->p2.p2_fast;
726 else
727 clock.p2 = limit->p2.p2_slow;
728 } else {
729 if (target < limit->p2.dot_limit)
730 clock.p2 = limit->p2.p2_slow;
731 else
732 clock.p2 = limit->p2.p2_fast;
733 }
734
0206e353 735 memset(best_clock, 0, sizeof(*best_clock));
79e53945 736
42158660
ZY
737 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
738 clock.m1++) {
739 for (clock.m2 = limit->m2.min;
740 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
741 for (clock.n = limit->n.min;
742 clock.n <= limit->n.max; clock.n++) {
743 for (clock.p1 = limit->p1.min;
744 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
745 int this_err;
746
ac58c3f0 747 pineview_clock(refclk, &clock);
1b894b59
CW
748 if (!intel_PLL_is_valid(dev, limit,
749 &clock))
79e53945 750 continue;
cec2f356
SP
751 if (match_clock &&
752 clock.p != match_clock->p)
753 continue;
79e53945
JB
754
755 this_err = abs(clock.dot - target);
756 if (this_err < err) {
757 *best_clock = clock;
758 err = this_err;
759 }
760 }
761 }
762 }
763 }
764
765 return (err != target);
766}
767
d4906093 768static bool
a93e255f
ACO
769g4x_find_best_dpll(const intel_limit_t *limit,
770 struct intel_crtc_state *crtc_state,
ee9300bb
DV
771 int target, int refclk, intel_clock_t *match_clock,
772 intel_clock_t *best_clock)
d4906093 773{
a93e255f 774 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 775 struct drm_device *dev = crtc->base.dev;
d4906093
ML
776 intel_clock_t clock;
777 int max_n;
778 bool found;
6ba770dc
AJ
779 /* approximately equals target * 0.00585 */
780 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
781 found = false;
782
a93e255f 783 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 784 if (intel_is_dual_link_lvds(dev))
d4906093
ML
785 clock.p2 = limit->p2.p2_fast;
786 else
787 clock.p2 = limit->p2.p2_slow;
788 } else {
789 if (target < limit->p2.dot_limit)
790 clock.p2 = limit->p2.p2_slow;
791 else
792 clock.p2 = limit->p2.p2_fast;
793 }
794
795 memset(best_clock, 0, sizeof(*best_clock));
796 max_n = limit->n.max;
f77f13e2 797 /* based on hardware requirement, prefer smaller n to precision */
d4906093 798 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 799 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
800 for (clock.m1 = limit->m1.max;
801 clock.m1 >= limit->m1.min; clock.m1--) {
802 for (clock.m2 = limit->m2.max;
803 clock.m2 >= limit->m2.min; clock.m2--) {
804 for (clock.p1 = limit->p1.max;
805 clock.p1 >= limit->p1.min; clock.p1--) {
806 int this_err;
807
ac58c3f0 808 i9xx_clock(refclk, &clock);
1b894b59
CW
809 if (!intel_PLL_is_valid(dev, limit,
810 &clock))
d4906093 811 continue;
1b894b59
CW
812
813 this_err = abs(clock.dot - target);
d4906093
ML
814 if (this_err < err_most) {
815 *best_clock = clock;
816 err_most = this_err;
817 max_n = clock.n;
818 found = true;
819 }
820 }
821 }
822 }
823 }
2c07245f
ZW
824 return found;
825}
826
d5dd62bd
ID
827/*
828 * Check if the calculated PLL configuration is more optimal compared to the
829 * best configuration and error found so far. Return the calculated error.
830 */
831static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
832 const intel_clock_t *calculated_clock,
833 const intel_clock_t *best_clock,
834 unsigned int best_error_ppm,
835 unsigned int *error_ppm)
836{
9ca3ba01
ID
837 /*
838 * For CHV ignore the error and consider only the P value.
839 * Prefer a bigger P value based on HW requirements.
840 */
841 if (IS_CHERRYVIEW(dev)) {
842 *error_ppm = 0;
843
844 return calculated_clock->p > best_clock->p;
845 }
846
24be4e46
ID
847 if (WARN_ON_ONCE(!target_freq))
848 return false;
849
d5dd62bd
ID
850 *error_ppm = div_u64(1000000ULL *
851 abs(target_freq - calculated_clock->dot),
852 target_freq);
853 /*
854 * Prefer a better P value over a better (smaller) error if the error
855 * is small. Ensure this preference for future configurations too by
856 * setting the error to 0.
857 */
858 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
859 *error_ppm = 0;
860
861 return true;
862 }
863
864 return *error_ppm + 10 < best_error_ppm;
865}
866
a0c4da24 867static bool
a93e255f
ACO
868vlv_find_best_dpll(const intel_limit_t *limit,
869 struct intel_crtc_state *crtc_state,
ee9300bb
DV
870 int target, int refclk, intel_clock_t *match_clock,
871 intel_clock_t *best_clock)
a0c4da24 872{
a93e255f 873 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 874 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 875 intel_clock_t clock;
69e4f900 876 unsigned int bestppm = 1000000;
27e639bf
VS
877 /* min update 19.2 MHz */
878 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 879 bool found = false;
a0c4da24 880
6b4bf1c4
VS
881 target *= 5; /* fast clock */
882
883 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
884
885 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 886 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 887 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 888 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 889 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 890 clock.p = clock.p1 * clock.p2;
a0c4da24 891 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 892 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 893 unsigned int ppm;
69e4f900 894
6b4bf1c4
VS
895 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
896 refclk * clock.m1);
897
898 vlv_clock(refclk, &clock);
43b0ac53 899
f01b7962
VS
900 if (!intel_PLL_is_valid(dev, limit,
901 &clock))
43b0ac53
VS
902 continue;
903
d5dd62bd
ID
904 if (!vlv_PLL_is_optimal(dev, target,
905 &clock,
906 best_clock,
907 bestppm, &ppm))
908 continue;
6b4bf1c4 909
d5dd62bd
ID
910 *best_clock = clock;
911 bestppm = ppm;
912 found = true;
a0c4da24
JB
913 }
914 }
915 }
916 }
a0c4da24 917
49e497ef 918 return found;
a0c4da24 919}
a4fc5ed6 920
ef9348c8 921static bool
a93e255f
ACO
922chv_find_best_dpll(const intel_limit_t *limit,
923 struct intel_crtc_state *crtc_state,
ef9348c8
CML
924 int target, int refclk, intel_clock_t *match_clock,
925 intel_clock_t *best_clock)
926{
a93e255f 927 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 928 struct drm_device *dev = crtc->base.dev;
9ca3ba01 929 unsigned int best_error_ppm;
ef9348c8
CML
930 intel_clock_t clock;
931 uint64_t m2;
932 int found = false;
933
934 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 935 best_error_ppm = 1000000;
ef9348c8
CML
936
937 /*
938 * Based on hardware doc, the n always set to 1, and m1 always
939 * set to 2. If requires to support 200Mhz refclk, we need to
940 * revisit this because n may not 1 anymore.
941 */
942 clock.n = 1, clock.m1 = 2;
943 target *= 5; /* fast clock */
944
945 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
946 for (clock.p2 = limit->p2.p2_fast;
947 clock.p2 >= limit->p2.p2_slow;
948 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 949 unsigned int error_ppm;
ef9348c8
CML
950
951 clock.p = clock.p1 * clock.p2;
952
953 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
954 clock.n) << 22, refclk * clock.m1);
955
956 if (m2 > INT_MAX/clock.m1)
957 continue;
958
959 clock.m2 = m2;
960
961 chv_clock(refclk, &clock);
962
963 if (!intel_PLL_is_valid(dev, limit, &clock))
964 continue;
965
9ca3ba01
ID
966 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
967 best_error_ppm, &error_ppm))
968 continue;
969
970 *best_clock = clock;
971 best_error_ppm = error_ppm;
972 found = true;
ef9348c8
CML
973 }
974 }
975
976 return found;
977}
978
5ab7b0b7
ID
979bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
980 intel_clock_t *best_clock)
981{
982 int refclk = i9xx_get_refclk(crtc_state, 0);
983
984 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
985 target_clock, refclk, NULL, best_clock);
986}
987
20ddf665
VS
988bool intel_crtc_active(struct drm_crtc *crtc)
989{
990 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
991
992 /* Be paranoid as we can arrive here with only partial
993 * state retrieved from the hardware during setup.
994 *
241bfc38 995 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
996 * as Haswell has gained clock readout/fastboot support.
997 *
66e514c1 998 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 999 * properly reconstruct framebuffers.
c3d1f436
MR
1000 *
1001 * FIXME: The intel_crtc->active here should be switched to
1002 * crtc->state->active once we have proper CRTC states wired up
1003 * for atomic.
20ddf665 1004 */
c3d1f436 1005 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1006 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1007}
1008
a5c961d1
PZ
1009enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1010 enum pipe pipe)
1011{
1012 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1014
6e3c9717 1015 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1016}
1017
fbf49ea2
VS
1018static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1019{
1020 struct drm_i915_private *dev_priv = dev->dev_private;
1021 u32 reg = PIPEDSL(pipe);
1022 u32 line1, line2;
1023 u32 line_mask;
1024
1025 if (IS_GEN2(dev))
1026 line_mask = DSL_LINEMASK_GEN2;
1027 else
1028 line_mask = DSL_LINEMASK_GEN3;
1029
1030 line1 = I915_READ(reg) & line_mask;
1031 mdelay(5);
1032 line2 = I915_READ(reg) & line_mask;
1033
1034 return line1 == line2;
1035}
1036
ab7ad7f6
KP
1037/*
1038 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1039 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1040 *
1041 * After disabling a pipe, we can't wait for vblank in the usual way,
1042 * spinning on the vblank interrupt status bit, since we won't actually
1043 * see an interrupt when the pipe is disabled.
1044 *
ab7ad7f6
KP
1045 * On Gen4 and above:
1046 * wait for the pipe register state bit to turn off
1047 *
1048 * Otherwise:
1049 * wait for the display line value to settle (it usually
1050 * ends up stopping at the start of the next frame).
58e10eb9 1051 *
9d0498a2 1052 */
575f7ab7 1053static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1054{
575f7ab7 1055 struct drm_device *dev = crtc->base.dev;
9d0498a2 1056 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1057 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1058 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1059
1060 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 1061 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1062
1063 /* Wait for the Pipe State to go off */
58e10eb9
CW
1064 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1065 100))
284637d9 1066 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1067 } else {
ab7ad7f6 1068 /* Wait for the display line to settle */
fbf49ea2 1069 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1070 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1071 }
79e53945
JB
1072}
1073
b0ea7d37
DL
1074/*
1075 * ibx_digital_port_connected - is the specified port connected?
1076 * @dev_priv: i915 private structure
1077 * @port: the port to test
1078 *
1079 * Returns true if @port is connected, false otherwise.
1080 */
1081bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1082 struct intel_digital_port *port)
1083{
1084 u32 bit;
1085
c36346e3 1086 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1087 switch (port->port) {
c36346e3
DL
1088 case PORT_B:
1089 bit = SDE_PORTB_HOTPLUG;
1090 break;
1091 case PORT_C:
1092 bit = SDE_PORTC_HOTPLUG;
1093 break;
1094 case PORT_D:
1095 bit = SDE_PORTD_HOTPLUG;
1096 break;
1097 default:
1098 return true;
1099 }
1100 } else {
eba905b2 1101 switch (port->port) {
c36346e3
DL
1102 case PORT_B:
1103 bit = SDE_PORTB_HOTPLUG_CPT;
1104 break;
1105 case PORT_C:
1106 bit = SDE_PORTC_HOTPLUG_CPT;
1107 break;
1108 case PORT_D:
1109 bit = SDE_PORTD_HOTPLUG_CPT;
1110 break;
1111 default:
1112 return true;
1113 }
b0ea7d37
DL
1114 }
1115
1116 return I915_READ(SDEISR) & bit;
1117}
1118
b24e7179
JB
1119static const char *state_string(bool enabled)
1120{
1121 return enabled ? "on" : "off";
1122}
1123
1124/* Only for pre-ILK configs */
55607e8a
DV
1125void assert_pll(struct drm_i915_private *dev_priv,
1126 enum pipe pipe, bool state)
b24e7179
JB
1127{
1128 int reg;
1129 u32 val;
1130 bool cur_state;
1131
1132 reg = DPLL(pipe);
1133 val = I915_READ(reg);
1134 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1135 I915_STATE_WARN(cur_state != state,
b24e7179
JB
1136 "PLL state assertion failure (expected %s, current %s)\n",
1137 state_string(state), state_string(cur_state));
1138}
b24e7179 1139
23538ef1
JN
1140/* XXX: the dsi pll is shared between MIPI DSI ports */
1141static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1142{
1143 u32 val;
1144 bool cur_state;
1145
a580516d 1146 mutex_lock(&dev_priv->sb_lock);
23538ef1 1147 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1148 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1149
1150 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1151 I915_STATE_WARN(cur_state != state,
23538ef1
JN
1152 "DSI PLL state assertion failure (expected %s, current %s)\n",
1153 state_string(state), state_string(cur_state));
1154}
1155#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1156#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1157
55607e8a 1158struct intel_shared_dpll *
e2b78267
DV
1159intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1160{
1161 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1162
6e3c9717 1163 if (crtc->config->shared_dpll < 0)
e2b78267
DV
1164 return NULL;
1165
6e3c9717 1166 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
e2b78267
DV
1167}
1168
040484af 1169/* For ILK+ */
55607e8a
DV
1170void assert_shared_dpll(struct drm_i915_private *dev_priv,
1171 struct intel_shared_dpll *pll,
1172 bool state)
040484af 1173{
040484af 1174 bool cur_state;
5358901f 1175 struct intel_dpll_hw_state hw_state;
040484af 1176
92b27b08 1177 if (WARN (!pll,
46edb027 1178 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1179 return;
ee7b9f93 1180
5358901f 1181 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
e2c719b7 1182 I915_STATE_WARN(cur_state != state,
5358901f
DV
1183 "%s assertion failure (expected %s, current %s)\n",
1184 pll->name, state_string(state), state_string(cur_state));
040484af 1185}
040484af
JB
1186
1187static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1188 enum pipe pipe, bool state)
1189{
1190 int reg;
1191 u32 val;
1192 bool cur_state;
ad80a810
PZ
1193 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1194 pipe);
040484af 1195
affa9354
PZ
1196 if (HAS_DDI(dev_priv->dev)) {
1197 /* DDI does not have a specific FDI_TX register */
ad80a810 1198 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1199 val = I915_READ(reg);
ad80a810 1200 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1201 } else {
1202 reg = FDI_TX_CTL(pipe);
1203 val = I915_READ(reg);
1204 cur_state = !!(val & FDI_TX_ENABLE);
1205 }
e2c719b7 1206 I915_STATE_WARN(cur_state != state,
040484af
JB
1207 "FDI TX state assertion failure (expected %s, current %s)\n",
1208 state_string(state), state_string(cur_state));
1209}
1210#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1211#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1212
1213static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1214 enum pipe pipe, bool state)
1215{
1216 int reg;
1217 u32 val;
1218 bool cur_state;
1219
d63fa0dc
PZ
1220 reg = FDI_RX_CTL(pipe);
1221 val = I915_READ(reg);
1222 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1223 I915_STATE_WARN(cur_state != state,
040484af
JB
1224 "FDI RX state assertion failure (expected %s, current %s)\n",
1225 state_string(state), state_string(cur_state));
1226}
1227#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1228#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1229
1230static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1231 enum pipe pipe)
1232{
1233 int reg;
1234 u32 val;
1235
1236 /* ILK FDI PLL is always enabled */
3d13ef2e 1237 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1238 return;
1239
bf507ef7 1240 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1241 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1242 return;
1243
040484af
JB
1244 reg = FDI_TX_CTL(pipe);
1245 val = I915_READ(reg);
e2c719b7 1246 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1247}
1248
55607e8a
DV
1249void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1250 enum pipe pipe, bool state)
040484af
JB
1251{
1252 int reg;
1253 u32 val;
55607e8a 1254 bool cur_state;
040484af
JB
1255
1256 reg = FDI_RX_CTL(pipe);
1257 val = I915_READ(reg);
55607e8a 1258 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1259 I915_STATE_WARN(cur_state != state,
55607e8a
DV
1260 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1261 state_string(state), state_string(cur_state));
040484af
JB
1262}
1263
b680c37a
DV
1264void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1265 enum pipe pipe)
ea0760cf 1266{
bedd4dba
JN
1267 struct drm_device *dev = dev_priv->dev;
1268 int pp_reg;
ea0760cf
JB
1269 u32 val;
1270 enum pipe panel_pipe = PIPE_A;
0de3b485 1271 bool locked = true;
ea0760cf 1272
bedd4dba
JN
1273 if (WARN_ON(HAS_DDI(dev)))
1274 return;
1275
1276 if (HAS_PCH_SPLIT(dev)) {
1277 u32 port_sel;
1278
ea0760cf 1279 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1280 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1281
1282 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1283 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1284 panel_pipe = PIPE_B;
1285 /* XXX: else fix for eDP */
1286 } else if (IS_VALLEYVIEW(dev)) {
1287 /* presumably write lock depends on pipe, not port select */
1288 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1289 panel_pipe = pipe;
ea0760cf
JB
1290 } else {
1291 pp_reg = PP_CONTROL;
bedd4dba
JN
1292 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1293 panel_pipe = PIPE_B;
ea0760cf
JB
1294 }
1295
1296 val = I915_READ(pp_reg);
1297 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1298 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1299 locked = false;
1300
e2c719b7 1301 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1302 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1303 pipe_name(pipe));
ea0760cf
JB
1304}
1305
93ce0ba6
JN
1306static void assert_cursor(struct drm_i915_private *dev_priv,
1307 enum pipe pipe, bool state)
1308{
1309 struct drm_device *dev = dev_priv->dev;
1310 bool cur_state;
1311
d9d82081 1312 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1313 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1314 else
5efb3e28 1315 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1316
e2c719b7 1317 I915_STATE_WARN(cur_state != state,
93ce0ba6
JN
1318 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1319 pipe_name(pipe), state_string(state), state_string(cur_state));
1320}
1321#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1322#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1323
b840d907
JB
1324void assert_pipe(struct drm_i915_private *dev_priv,
1325 enum pipe pipe, bool state)
b24e7179
JB
1326{
1327 int reg;
1328 u32 val;
63d7bbe9 1329 bool cur_state;
702e7a56
PZ
1330 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1331 pipe);
b24e7179 1332
b6b5d049
VS
1333 /* if we need the pipe quirk it must be always on */
1334 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1335 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1336 state = true;
1337
f458ebbc 1338 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1339 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1340 cur_state = false;
1341 } else {
1342 reg = PIPECONF(cpu_transcoder);
1343 val = I915_READ(reg);
1344 cur_state = !!(val & PIPECONF_ENABLE);
1345 }
1346
e2c719b7 1347 I915_STATE_WARN(cur_state != state,
63d7bbe9 1348 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1349 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1350}
1351
931872fc
CW
1352static void assert_plane(struct drm_i915_private *dev_priv,
1353 enum plane plane, bool state)
b24e7179
JB
1354{
1355 int reg;
1356 u32 val;
931872fc 1357 bool cur_state;
b24e7179
JB
1358
1359 reg = DSPCNTR(plane);
1360 val = I915_READ(reg);
931872fc 1361 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1362 I915_STATE_WARN(cur_state != state,
931872fc
CW
1363 "plane %c assertion failure (expected %s, current %s)\n",
1364 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1365}
1366
931872fc
CW
1367#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1368#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1369
b24e7179
JB
1370static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1371 enum pipe pipe)
1372{
653e1026 1373 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1374 int reg, i;
1375 u32 val;
1376 int cur_pipe;
1377
653e1026
VS
1378 /* Primary planes are fixed to pipes on gen4+ */
1379 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1380 reg = DSPCNTR(pipe);
1381 val = I915_READ(reg);
e2c719b7 1382 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1383 "plane %c assertion failure, should be disabled but not\n",
1384 plane_name(pipe));
19ec1358 1385 return;
28c05794 1386 }
19ec1358 1387
b24e7179 1388 /* Need to check both planes against the pipe */
055e393f 1389 for_each_pipe(dev_priv, i) {
b24e7179
JB
1390 reg = DSPCNTR(i);
1391 val = I915_READ(reg);
1392 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1393 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1394 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1395 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1396 plane_name(i), pipe_name(pipe));
b24e7179
JB
1397 }
1398}
1399
19332d7a
JB
1400static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1401 enum pipe pipe)
1402{
20674eef 1403 struct drm_device *dev = dev_priv->dev;
1fe47785 1404 int reg, sprite;
19332d7a
JB
1405 u32 val;
1406
7feb8b88 1407 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1408 for_each_sprite(dev_priv, pipe, sprite) {
7feb8b88 1409 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1410 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1411 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1412 sprite, pipe_name(pipe));
1413 }
1414 } else if (IS_VALLEYVIEW(dev)) {
3bdcfc0c 1415 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 1416 reg = SPCNTR(pipe, sprite);
20674eef 1417 val = I915_READ(reg);
e2c719b7 1418 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1419 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1420 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1421 }
1422 } else if (INTEL_INFO(dev)->gen >= 7) {
1423 reg = SPRCTL(pipe);
19332d7a 1424 val = I915_READ(reg);
e2c719b7 1425 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1426 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1427 plane_name(pipe), pipe_name(pipe));
1428 } else if (INTEL_INFO(dev)->gen >= 5) {
1429 reg = DVSCNTR(pipe);
19332d7a 1430 val = I915_READ(reg);
e2c719b7 1431 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1432 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1433 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1434 }
1435}
1436
08c71e5e
VS
1437static void assert_vblank_disabled(struct drm_crtc *crtc)
1438{
e2c719b7 1439 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1440 drm_crtc_vblank_put(crtc);
1441}
1442
89eff4be 1443static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1444{
1445 u32 val;
1446 bool enabled;
1447
e2c719b7 1448 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1449
92f2584a
JB
1450 val = I915_READ(PCH_DREF_CONTROL);
1451 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1452 DREF_SUPERSPREAD_SOURCE_MASK));
e2c719b7 1453 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
92f2584a
JB
1454}
1455
ab9412ba
DV
1456static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1457 enum pipe pipe)
92f2584a
JB
1458{
1459 int reg;
1460 u32 val;
1461 bool enabled;
1462
ab9412ba 1463 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1464 val = I915_READ(reg);
1465 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1466 I915_STATE_WARN(enabled,
9db4a9c7
JB
1467 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1468 pipe_name(pipe));
92f2584a
JB
1469}
1470
4e634389
KP
1471static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1472 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1473{
1474 if ((val & DP_PORT_EN) == 0)
1475 return false;
1476
1477 if (HAS_PCH_CPT(dev_priv->dev)) {
1478 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1479 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1480 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1481 return false;
44f37d1f
CML
1482 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1483 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1484 return false;
f0575e92
KP
1485 } else {
1486 if ((val & DP_PIPE_MASK) != (pipe << 30))
1487 return false;
1488 }
1489 return true;
1490}
1491
1519b995
KP
1492static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1493 enum pipe pipe, u32 val)
1494{
dc0fa718 1495 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1496 return false;
1497
1498 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1499 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1500 return false;
44f37d1f
CML
1501 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1502 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1503 return false;
1519b995 1504 } else {
dc0fa718 1505 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1506 return false;
1507 }
1508 return true;
1509}
1510
1511static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1512 enum pipe pipe, u32 val)
1513{
1514 if ((val & LVDS_PORT_EN) == 0)
1515 return false;
1516
1517 if (HAS_PCH_CPT(dev_priv->dev)) {
1518 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1519 return false;
1520 } else {
1521 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1522 return false;
1523 }
1524 return true;
1525}
1526
1527static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1528 enum pipe pipe, u32 val)
1529{
1530 if ((val & ADPA_DAC_ENABLE) == 0)
1531 return false;
1532 if (HAS_PCH_CPT(dev_priv->dev)) {
1533 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1534 return false;
1535 } else {
1536 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1537 return false;
1538 }
1539 return true;
1540}
1541
291906f1 1542static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1543 enum pipe pipe, int reg, u32 port_sel)
291906f1 1544{
47a05eca 1545 u32 val = I915_READ(reg);
e2c719b7 1546 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1547 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1548 reg, pipe_name(pipe));
de9a35ab 1549
e2c719b7 1550 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1551 && (val & DP_PIPEB_SELECT),
de9a35ab 1552 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1553}
1554
1555static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1556 enum pipe pipe, int reg)
1557{
47a05eca 1558 u32 val = I915_READ(reg);
e2c719b7 1559 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1560 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1561 reg, pipe_name(pipe));
de9a35ab 1562
e2c719b7 1563 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1564 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1565 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1566}
1567
1568static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1569 enum pipe pipe)
1570{
1571 int reg;
1572 u32 val;
291906f1 1573
f0575e92
KP
1574 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1575 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1576 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1577
1578 reg = PCH_ADPA;
1579 val = I915_READ(reg);
e2c719b7 1580 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1581 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1582 pipe_name(pipe));
291906f1
JB
1583
1584 reg = PCH_LVDS;
1585 val = I915_READ(reg);
e2c719b7 1586 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1587 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1588 pipe_name(pipe));
291906f1 1589
e2debe91
PZ
1590 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1591 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1592 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1593}
1594
40e9cf64
JB
1595static void intel_init_dpio(struct drm_device *dev)
1596{
1597 struct drm_i915_private *dev_priv = dev->dev_private;
1598
1599 if (!IS_VALLEYVIEW(dev))
1600 return;
1601
a09caddd
CML
1602 /*
1603 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1604 * CHV x1 PHY (DP/HDMI D)
1605 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1606 */
1607 if (IS_CHERRYVIEW(dev)) {
1608 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1609 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1610 } else {
1611 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1612 }
5382f5f3
JB
1613}
1614
d288f65f 1615static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1616 const struct intel_crtc_state *pipe_config)
87442f73 1617{
426115cf
DV
1618 struct drm_device *dev = crtc->base.dev;
1619 struct drm_i915_private *dev_priv = dev->dev_private;
1620 int reg = DPLL(crtc->pipe);
d288f65f 1621 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1622
426115cf 1623 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1624
1625 /* No really, not for ILK+ */
1626 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1627
1628 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1629 if (IS_MOBILE(dev_priv->dev))
426115cf 1630 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1631
426115cf
DV
1632 I915_WRITE(reg, dpll);
1633 POSTING_READ(reg);
1634 udelay(150);
1635
1636 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1637 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1638
d288f65f 1639 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1640 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1641
1642 /* We do this three times for luck */
426115cf 1643 I915_WRITE(reg, dpll);
87442f73
DV
1644 POSTING_READ(reg);
1645 udelay(150); /* wait for warmup */
426115cf 1646 I915_WRITE(reg, dpll);
87442f73
DV
1647 POSTING_READ(reg);
1648 udelay(150); /* wait for warmup */
426115cf 1649 I915_WRITE(reg, dpll);
87442f73
DV
1650 POSTING_READ(reg);
1651 udelay(150); /* wait for warmup */
1652}
1653
d288f65f 1654static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1655 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1656{
1657 struct drm_device *dev = crtc->base.dev;
1658 struct drm_i915_private *dev_priv = dev->dev_private;
1659 int pipe = crtc->pipe;
1660 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1661 u32 tmp;
1662
1663 assert_pipe_disabled(dev_priv, crtc->pipe);
1664
1665 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1666
a580516d 1667 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1668
1669 /* Enable back the 10bit clock to display controller */
1670 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1671 tmp |= DPIO_DCLKP_EN;
1672 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1673
54433e91
VS
1674 mutex_unlock(&dev_priv->sb_lock);
1675
9d556c99
CML
1676 /*
1677 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1678 */
1679 udelay(1);
1680
1681 /* Enable PLL */
d288f65f 1682 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1683
1684 /* Check PLL is locked */
a11b0703 1685 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1686 DRM_ERROR("PLL %d failed to lock\n", pipe);
1687
a11b0703 1688 /* not sure when this should be written */
d288f65f 1689 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703 1690 POSTING_READ(DPLL_MD(pipe));
9d556c99
CML
1691}
1692
1c4e0274
VS
1693static int intel_num_dvo_pipes(struct drm_device *dev)
1694{
1695 struct intel_crtc *crtc;
1696 int count = 0;
1697
1698 for_each_intel_crtc(dev, crtc)
3538b9df 1699 count += crtc->base.state->active &&
409ee761 1700 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1701
1702 return count;
1703}
1704
66e3d5c0 1705static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1706{
66e3d5c0
DV
1707 struct drm_device *dev = crtc->base.dev;
1708 struct drm_i915_private *dev_priv = dev->dev_private;
1709 int reg = DPLL(crtc->pipe);
6e3c9717 1710 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1711
66e3d5c0 1712 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1713
63d7bbe9 1714 /* No really, not for ILK+ */
3d13ef2e 1715 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1716
1717 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1718 if (IS_MOBILE(dev) && !IS_I830(dev))
1719 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1720
1c4e0274
VS
1721 /* Enable DVO 2x clock on both PLLs if necessary */
1722 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1723 /*
1724 * It appears to be important that we don't enable this
1725 * for the current pipe before otherwise configuring the
1726 * PLL. No idea how this should be handled if multiple
1727 * DVO outputs are enabled simultaneosly.
1728 */
1729 dpll |= DPLL_DVO_2X_MODE;
1730 I915_WRITE(DPLL(!crtc->pipe),
1731 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1732 }
66e3d5c0
DV
1733
1734 /* Wait for the clocks to stabilize. */
1735 POSTING_READ(reg);
1736 udelay(150);
1737
1738 if (INTEL_INFO(dev)->gen >= 4) {
1739 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1740 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1741 } else {
1742 /* The pixel multiplier can only be updated once the
1743 * DPLL is enabled and the clocks are stable.
1744 *
1745 * So write it again.
1746 */
1747 I915_WRITE(reg, dpll);
1748 }
63d7bbe9
JB
1749
1750 /* We do this three times for luck */
66e3d5c0 1751 I915_WRITE(reg, dpll);
63d7bbe9
JB
1752 POSTING_READ(reg);
1753 udelay(150); /* wait for warmup */
66e3d5c0 1754 I915_WRITE(reg, dpll);
63d7bbe9
JB
1755 POSTING_READ(reg);
1756 udelay(150); /* wait for warmup */
66e3d5c0 1757 I915_WRITE(reg, dpll);
63d7bbe9
JB
1758 POSTING_READ(reg);
1759 udelay(150); /* wait for warmup */
1760}
1761
1762/**
50b44a44 1763 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1764 * @dev_priv: i915 private structure
1765 * @pipe: pipe PLL to disable
1766 *
1767 * Disable the PLL for @pipe, making sure the pipe is off first.
1768 *
1769 * Note! This is for pre-ILK only.
1770 */
1c4e0274 1771static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1772{
1c4e0274
VS
1773 struct drm_device *dev = crtc->base.dev;
1774 struct drm_i915_private *dev_priv = dev->dev_private;
1775 enum pipe pipe = crtc->pipe;
1776
1777 /* Disable DVO 2x clock on both PLLs if necessary */
1778 if (IS_I830(dev) &&
409ee761 1779 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1780 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1781 I915_WRITE(DPLL(PIPE_B),
1782 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1783 I915_WRITE(DPLL(PIPE_A),
1784 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1785 }
1786
b6b5d049
VS
1787 /* Don't disable pipe or pipe PLLs if needed */
1788 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1789 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1790 return;
1791
1792 /* Make sure the pipe isn't still relying on us */
1793 assert_pipe_disabled(dev_priv, pipe);
1794
50b44a44
DV
1795 I915_WRITE(DPLL(pipe), 0);
1796 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1797}
1798
f6071166
JB
1799static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1800{
1801 u32 val = 0;
1802
1803 /* Make sure the pipe isn't still relying on us */
1804 assert_pipe_disabled(dev_priv, pipe);
1805
e5cbfbfb
ID
1806 /*
1807 * Leave integrated clock source and reference clock enabled for pipe B.
1808 * The latter is needed for VGA hotplug / manual detection.
1809 */
f6071166 1810 if (pipe == PIPE_B)
e5cbfbfb 1811 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1812 I915_WRITE(DPLL(pipe), val);
1813 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1814
1815}
1816
1817static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1818{
d752048d 1819 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1820 u32 val;
1821
a11b0703
VS
1822 /* Make sure the pipe isn't still relying on us */
1823 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1824
a11b0703 1825 /* Set PLL en = 0 */
d17ec4ce 1826 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1827 if (pipe != PIPE_A)
1828 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1829 I915_WRITE(DPLL(pipe), val);
1830 POSTING_READ(DPLL(pipe));
d752048d 1831
a580516d 1832 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1833
1834 /* Disable 10bit clock to display controller */
1835 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1836 val &= ~DPIO_DCLKP_EN;
1837 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1838
61407f6d
VS
1839 /* disable left/right clock distribution */
1840 if (pipe != PIPE_B) {
1841 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1842 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1843 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1844 } else {
1845 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1846 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1847 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1848 }
1849
a580516d 1850 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1851}
1852
e4607fcf 1853void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1854 struct intel_digital_port *dport,
1855 unsigned int expected_mask)
89b667f8
JB
1856{
1857 u32 port_mask;
00fc31b7 1858 int dpll_reg;
89b667f8 1859
e4607fcf
CML
1860 switch (dport->port) {
1861 case PORT_B:
89b667f8 1862 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1863 dpll_reg = DPLL(0);
e4607fcf
CML
1864 break;
1865 case PORT_C:
89b667f8 1866 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1867 dpll_reg = DPLL(0);
9b6de0a1 1868 expected_mask <<= 4;
00fc31b7
CML
1869 break;
1870 case PORT_D:
1871 port_mask = DPLL_PORTD_READY_MASK;
1872 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1873 break;
1874 default:
1875 BUG();
1876 }
89b667f8 1877
9b6de0a1
VS
1878 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1879 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1880 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1881}
1882
b14b1055
DV
1883static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1884{
1885 struct drm_device *dev = crtc->base.dev;
1886 struct drm_i915_private *dev_priv = dev->dev_private;
1887 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1888
be19f0ff
CW
1889 if (WARN_ON(pll == NULL))
1890 return;
1891
3e369b76 1892 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1893 if (pll->active == 0) {
1894 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1895 WARN_ON(pll->on);
1896 assert_shared_dpll_disabled(dev_priv, pll);
1897
1898 pll->mode_set(dev_priv, pll);
1899 }
1900}
1901
92f2584a 1902/**
85b3894f 1903 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1904 * @dev_priv: i915 private structure
1905 * @pipe: pipe PLL to enable
1906 *
1907 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1908 * drives the transcoder clock.
1909 */
85b3894f 1910static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1911{
3d13ef2e
DL
1912 struct drm_device *dev = crtc->base.dev;
1913 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1914 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1915
87a875bb 1916 if (WARN_ON(pll == NULL))
48da64a8
CW
1917 return;
1918
3e369b76 1919 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1920 return;
ee7b9f93 1921
74dd6928 1922 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1923 pll->name, pll->active, pll->on,
e2b78267 1924 crtc->base.base.id);
92f2584a 1925
cdbd2316
DV
1926 if (pll->active++) {
1927 WARN_ON(!pll->on);
e9d6944e 1928 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1929 return;
1930 }
f4a091c7 1931 WARN_ON(pll->on);
ee7b9f93 1932
bd2bb1b9
PZ
1933 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1934
46edb027 1935 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1936 pll->enable(dev_priv, pll);
ee7b9f93 1937 pll->on = true;
92f2584a
JB
1938}
1939
f6daaec2 1940static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1941{
3d13ef2e
DL
1942 struct drm_device *dev = crtc->base.dev;
1943 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1944 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1945
92f2584a 1946 /* PCH only available on ILK+ */
3d13ef2e 1947 BUG_ON(INTEL_INFO(dev)->gen < 5);
eddfcbcd
ML
1948 if (pll == NULL)
1949 return;
92f2584a 1950
eddfcbcd 1951 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
48da64a8 1952 return;
7a419866 1953
46edb027
DV
1954 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1955 pll->name, pll->active, pll->on,
e2b78267 1956 crtc->base.base.id);
7a419866 1957
48da64a8 1958 if (WARN_ON(pll->active == 0)) {
e9d6944e 1959 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1960 return;
1961 }
1962
e9d6944e 1963 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1964 WARN_ON(!pll->on);
cdbd2316 1965 if (--pll->active)
7a419866 1966 return;
ee7b9f93 1967
46edb027 1968 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1969 pll->disable(dev_priv, pll);
ee7b9f93 1970 pll->on = false;
bd2bb1b9
PZ
1971
1972 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1973}
1974
b8a4f404
PZ
1975static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1976 enum pipe pipe)
040484af 1977{
23670b32 1978 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1979 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1981 uint32_t reg, val, pipeconf_val;
040484af
JB
1982
1983 /* PCH only available on ILK+ */
55522f37 1984 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1985
1986 /* Make sure PCH DPLL is enabled */
e72f9fbf 1987 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1988 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1989
1990 /* FDI must be feeding us bits for PCH ports */
1991 assert_fdi_tx_enabled(dev_priv, pipe);
1992 assert_fdi_rx_enabled(dev_priv, pipe);
1993
23670b32
DV
1994 if (HAS_PCH_CPT(dev)) {
1995 /* Workaround: Set the timing override bit before enabling the
1996 * pch transcoder. */
1997 reg = TRANS_CHICKEN2(pipe);
1998 val = I915_READ(reg);
1999 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2000 I915_WRITE(reg, val);
59c859d6 2001 }
23670b32 2002
ab9412ba 2003 reg = PCH_TRANSCONF(pipe);
040484af 2004 val = I915_READ(reg);
5f7f726d 2005 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
2006
2007 if (HAS_PCH_IBX(dev_priv->dev)) {
2008 /*
c5de7c6f
VS
2009 * Make the BPC in transcoder be consistent with
2010 * that in pipeconf reg. For HDMI we must use 8bpc
2011 * here for both 8bpc and 12bpc.
e9bcff5c 2012 */
dfd07d72 2013 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
2014 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
2015 val |= PIPECONF_8BPC;
2016 else
2017 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 2018 }
5f7f726d
PZ
2019
2020 val &= ~TRANS_INTERLACE_MASK;
2021 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 2022 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 2023 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
2024 val |= TRANS_LEGACY_INTERLACED_ILK;
2025 else
2026 val |= TRANS_INTERLACED;
5f7f726d
PZ
2027 else
2028 val |= TRANS_PROGRESSIVE;
2029
040484af
JB
2030 I915_WRITE(reg, val | TRANS_ENABLE);
2031 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 2032 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
2033}
2034
8fb033d7 2035static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 2036 enum transcoder cpu_transcoder)
040484af 2037{
8fb033d7 2038 u32 val, pipeconf_val;
8fb033d7
PZ
2039
2040 /* PCH only available on ILK+ */
55522f37 2041 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 2042
8fb033d7 2043 /* FDI must be feeding us bits for PCH ports */
1a240d4d 2044 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 2045 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 2046
223a6fdf
PZ
2047 /* Workaround: set timing override bit. */
2048 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2049 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
2050 I915_WRITE(_TRANSA_CHICKEN2, val);
2051
25f3ef11 2052 val = TRANS_ENABLE;
937bb610 2053 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 2054
9a76b1c6
PZ
2055 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2056 PIPECONF_INTERLACED_ILK)
a35f2679 2057 val |= TRANS_INTERLACED;
8fb033d7
PZ
2058 else
2059 val |= TRANS_PROGRESSIVE;
2060
ab9412ba
DV
2061 I915_WRITE(LPT_TRANSCONF, val);
2062 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 2063 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
2064}
2065
b8a4f404
PZ
2066static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2067 enum pipe pipe)
040484af 2068{
23670b32
DV
2069 struct drm_device *dev = dev_priv->dev;
2070 uint32_t reg, val;
040484af
JB
2071
2072 /* FDI relies on the transcoder */
2073 assert_fdi_tx_disabled(dev_priv, pipe);
2074 assert_fdi_rx_disabled(dev_priv, pipe);
2075
291906f1
JB
2076 /* Ports must be off as well */
2077 assert_pch_ports_disabled(dev_priv, pipe);
2078
ab9412ba 2079 reg = PCH_TRANSCONF(pipe);
040484af
JB
2080 val = I915_READ(reg);
2081 val &= ~TRANS_ENABLE;
2082 I915_WRITE(reg, val);
2083 /* wait for PCH transcoder off, transcoder state */
2084 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 2085 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
2086
2087 if (!HAS_PCH_IBX(dev)) {
2088 /* Workaround: Clear the timing override chicken bit again. */
2089 reg = TRANS_CHICKEN2(pipe);
2090 val = I915_READ(reg);
2091 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2092 I915_WRITE(reg, val);
2093 }
040484af
JB
2094}
2095
ab4d966c 2096static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 2097{
8fb033d7
PZ
2098 u32 val;
2099
ab9412ba 2100 val = I915_READ(LPT_TRANSCONF);
8fb033d7 2101 val &= ~TRANS_ENABLE;
ab9412ba 2102 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2103 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2104 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2105 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2106
2107 /* Workaround: clear timing override bit. */
2108 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2109 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2110 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2111}
2112
b24e7179 2113/**
309cfea8 2114 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2115 * @crtc: crtc responsible for the pipe
b24e7179 2116 *
0372264a 2117 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2118 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2119 */
e1fdc473 2120static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2121{
0372264a
PZ
2122 struct drm_device *dev = crtc->base.dev;
2123 struct drm_i915_private *dev_priv = dev->dev_private;
2124 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2125 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2126 pipe);
1a240d4d 2127 enum pipe pch_transcoder;
b24e7179
JB
2128 int reg;
2129 u32 val;
2130
58c6eaa2 2131 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2132 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2133 assert_sprites_disabled(dev_priv, pipe);
2134
681e5811 2135 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2136 pch_transcoder = TRANSCODER_A;
2137 else
2138 pch_transcoder = pipe;
2139
b24e7179
JB
2140 /*
2141 * A pipe without a PLL won't actually be able to drive bits from
2142 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2143 * need the check.
2144 */
50360403 2145 if (HAS_GMCH_DISPLAY(dev_priv->dev))
409ee761 2146 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2147 assert_dsi_pll_enabled(dev_priv);
2148 else
2149 assert_pll_enabled(dev_priv, pipe);
040484af 2150 else {
6e3c9717 2151 if (crtc->config->has_pch_encoder) {
040484af 2152 /* if driving the PCH, we need FDI enabled */
cc391bbb 2153 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2154 assert_fdi_tx_pll_enabled(dev_priv,
2155 (enum pipe) cpu_transcoder);
040484af
JB
2156 }
2157 /* FIXME: assert CPU port conditions for SNB+ */
2158 }
b24e7179 2159
702e7a56 2160 reg = PIPECONF(cpu_transcoder);
b24e7179 2161 val = I915_READ(reg);
7ad25d48 2162 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2163 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2164 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2165 return;
7ad25d48 2166 }
00d70b15
CW
2167
2168 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2169 POSTING_READ(reg);
b24e7179
JB
2170}
2171
2172/**
309cfea8 2173 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2174 * @crtc: crtc whose pipes is to be disabled
b24e7179 2175 *
575f7ab7
VS
2176 * Disable the pipe of @crtc, making sure that various hardware
2177 * specific requirements are met, if applicable, e.g. plane
2178 * disabled, panel fitter off, etc.
b24e7179
JB
2179 *
2180 * Will wait until the pipe has shut down before returning.
2181 */
575f7ab7 2182static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2183{
575f7ab7 2184 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2185 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2186 enum pipe pipe = crtc->pipe;
b24e7179
JB
2187 int reg;
2188 u32 val;
2189
2190 /*
2191 * Make sure planes won't keep trying to pump pixels to us,
2192 * or we might hang the display.
2193 */
2194 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2195 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2196 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2197
702e7a56 2198 reg = PIPECONF(cpu_transcoder);
b24e7179 2199 val = I915_READ(reg);
00d70b15
CW
2200 if ((val & PIPECONF_ENABLE) == 0)
2201 return;
2202
67adc644
VS
2203 /*
2204 * Double wide has implications for planes
2205 * so best keep it disabled when not needed.
2206 */
6e3c9717 2207 if (crtc->config->double_wide)
67adc644
VS
2208 val &= ~PIPECONF_DOUBLE_WIDE;
2209
2210 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2211 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2212 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2213 val &= ~PIPECONF_ENABLE;
2214
2215 I915_WRITE(reg, val);
2216 if ((val & PIPECONF_ENABLE) == 0)
2217 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2218}
2219
693db184
CW
2220static bool need_vtd_wa(struct drm_device *dev)
2221{
2222#ifdef CONFIG_INTEL_IOMMU
2223 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2224 return true;
2225#endif
2226 return false;
2227}
2228
50470bb0 2229unsigned int
6761dd31
TU
2230intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2231 uint64_t fb_format_modifier)
a57ce0b2 2232{
6761dd31
TU
2233 unsigned int tile_height;
2234 uint32_t pixel_bytes;
a57ce0b2 2235
b5d0e9bf
DL
2236 switch (fb_format_modifier) {
2237 case DRM_FORMAT_MOD_NONE:
2238 tile_height = 1;
2239 break;
2240 case I915_FORMAT_MOD_X_TILED:
2241 tile_height = IS_GEN2(dev) ? 16 : 8;
2242 break;
2243 case I915_FORMAT_MOD_Y_TILED:
2244 tile_height = 32;
2245 break;
2246 case I915_FORMAT_MOD_Yf_TILED:
6761dd31
TU
2247 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2248 switch (pixel_bytes) {
b5d0e9bf 2249 default:
6761dd31 2250 case 1:
b5d0e9bf
DL
2251 tile_height = 64;
2252 break;
6761dd31
TU
2253 case 2:
2254 case 4:
b5d0e9bf
DL
2255 tile_height = 32;
2256 break;
6761dd31 2257 case 8:
b5d0e9bf
DL
2258 tile_height = 16;
2259 break;
6761dd31 2260 case 16:
b5d0e9bf
DL
2261 WARN_ONCE(1,
2262 "128-bit pixels are not supported for display!");
2263 tile_height = 16;
2264 break;
2265 }
2266 break;
2267 default:
2268 MISSING_CASE(fb_format_modifier);
2269 tile_height = 1;
2270 break;
2271 }
091df6cb 2272
6761dd31
TU
2273 return tile_height;
2274}
2275
2276unsigned int
2277intel_fb_align_height(struct drm_device *dev, unsigned int height,
2278 uint32_t pixel_format, uint64_t fb_format_modifier)
2279{
2280 return ALIGN(height, intel_tile_height(dev, pixel_format,
2281 fb_format_modifier));
a57ce0b2
JB
2282}
2283
f64b98cd
TU
2284static int
2285intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2286 const struct drm_plane_state *plane_state)
2287{
50470bb0 2288 struct intel_rotation_info *info = &view->rotation_info;
50470bb0 2289
f64b98cd
TU
2290 *view = i915_ggtt_view_normal;
2291
50470bb0
TU
2292 if (!plane_state)
2293 return 0;
2294
121920fa 2295 if (!intel_rotation_90_or_270(plane_state->rotation))
50470bb0
TU
2296 return 0;
2297
9abc4648 2298 *view = i915_ggtt_view_rotated;
50470bb0
TU
2299
2300 info->height = fb->height;
2301 info->pixel_format = fb->pixel_format;
2302 info->pitch = fb->pitches[0];
2303 info->fb_modifier = fb->modifier[0];
2304
f64b98cd
TU
2305 return 0;
2306}
2307
4e9a86b6
VS
2308static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2309{
2310 if (INTEL_INFO(dev_priv)->gen >= 9)
2311 return 256 * 1024;
985b8bb4
VS
2312 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2313 IS_VALLEYVIEW(dev_priv))
4e9a86b6
VS
2314 return 128 * 1024;
2315 else if (INTEL_INFO(dev_priv)->gen >= 4)
2316 return 4 * 1024;
2317 else
44c5905e 2318 return 0;
4e9a86b6
VS
2319}
2320
127bd2ac 2321int
850c4cdc
TU
2322intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2323 struct drm_framebuffer *fb,
82bc3b2d 2324 const struct drm_plane_state *plane_state,
a4872ba6 2325 struct intel_engine_cs *pipelined)
6b95a207 2326{
850c4cdc 2327 struct drm_device *dev = fb->dev;
ce453d81 2328 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2329 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2330 struct i915_ggtt_view view;
6b95a207
KH
2331 u32 alignment;
2332 int ret;
2333
ebcdd39e
MR
2334 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2335
7b911adc
TU
2336 switch (fb->modifier[0]) {
2337 case DRM_FORMAT_MOD_NONE:
4e9a86b6 2338 alignment = intel_linear_alignment(dev_priv);
6b95a207 2339 break;
7b911adc 2340 case I915_FORMAT_MOD_X_TILED:
1fada4cc
DL
2341 if (INTEL_INFO(dev)->gen >= 9)
2342 alignment = 256 * 1024;
2343 else {
2344 /* pin() will align the object as required by fence */
2345 alignment = 0;
2346 }
6b95a207 2347 break;
7b911adc 2348 case I915_FORMAT_MOD_Y_TILED:
1327b9a1
DL
2349 case I915_FORMAT_MOD_Yf_TILED:
2350 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2351 "Y tiling bo slipped through, driver bug!\n"))
2352 return -EINVAL;
2353 alignment = 1 * 1024 * 1024;
2354 break;
6b95a207 2355 default:
7b911adc
TU
2356 MISSING_CASE(fb->modifier[0]);
2357 return -EINVAL;
6b95a207
KH
2358 }
2359
f64b98cd
TU
2360 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2361 if (ret)
2362 return ret;
2363
693db184
CW
2364 /* Note that the w/a also requires 64 PTE of padding following the
2365 * bo. We currently fill all unused PTE with the shadow page and so
2366 * we should always have valid PTE following the scanout preventing
2367 * the VT-d warning.
2368 */
2369 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2370 alignment = 256 * 1024;
2371
d6dd6843
PZ
2372 /*
2373 * Global gtt pte registers are special registers which actually forward
2374 * writes to a chunk of system memory. Which means that there is no risk
2375 * that the register values disappear as soon as we call
2376 * intel_runtime_pm_put(), so it is correct to wrap only the
2377 * pin/unpin/fence and not more.
2378 */
2379 intel_runtime_pm_get(dev_priv);
2380
ce453d81 2381 dev_priv->mm.interruptible = false;
e6617330 2382 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
f64b98cd 2383 &view);
48b956c5 2384 if (ret)
ce453d81 2385 goto err_interruptible;
6b95a207
KH
2386
2387 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2388 * fence, whereas 965+ only requires a fence if using
2389 * framebuffer compression. For simplicity, we always install
2390 * a fence as the cost is not that onerous.
2391 */
06d98131 2392 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2393 if (ret)
2394 goto err_unpin;
1690e1eb 2395
9a5a53b3 2396 i915_gem_object_pin_fence(obj);
6b95a207 2397
ce453d81 2398 dev_priv->mm.interruptible = true;
d6dd6843 2399 intel_runtime_pm_put(dev_priv);
6b95a207 2400 return 0;
48b956c5
CW
2401
2402err_unpin:
f64b98cd 2403 i915_gem_object_unpin_from_display_plane(obj, &view);
ce453d81
CW
2404err_interruptible:
2405 dev_priv->mm.interruptible = true;
d6dd6843 2406 intel_runtime_pm_put(dev_priv);
48b956c5 2407 return ret;
6b95a207
KH
2408}
2409
82bc3b2d
TU
2410static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2411 const struct drm_plane_state *plane_state)
1690e1eb 2412{
82bc3b2d 2413 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd
TU
2414 struct i915_ggtt_view view;
2415 int ret;
82bc3b2d 2416
ebcdd39e
MR
2417 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2418
f64b98cd
TU
2419 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2420 WARN_ONCE(ret, "Couldn't get view from plane state!");
2421
1690e1eb 2422 i915_gem_object_unpin_fence(obj);
f64b98cd 2423 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2424}
2425
c2c75131
DV
2426/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2427 * is assumed to be a power-of-two. */
4e9a86b6
VS
2428unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2429 int *x, int *y,
bc752862
CW
2430 unsigned int tiling_mode,
2431 unsigned int cpp,
2432 unsigned int pitch)
c2c75131 2433{
bc752862
CW
2434 if (tiling_mode != I915_TILING_NONE) {
2435 unsigned int tile_rows, tiles;
c2c75131 2436
bc752862
CW
2437 tile_rows = *y / 8;
2438 *y %= 8;
c2c75131 2439
bc752862
CW
2440 tiles = *x / (512/cpp);
2441 *x %= 512/cpp;
2442
2443 return tile_rows * pitch * 8 + tiles * 4096;
2444 } else {
4e9a86b6 2445 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
bc752862
CW
2446 unsigned int offset;
2447
2448 offset = *y * pitch + *x * cpp;
4e9a86b6
VS
2449 *y = (offset & alignment) / pitch;
2450 *x = ((offset & alignment) - *y * pitch) / cpp;
2451 return offset & ~alignment;
bc752862 2452 }
c2c75131
DV
2453}
2454
b35d63fa 2455static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2456{
2457 switch (format) {
2458 case DISPPLANE_8BPP:
2459 return DRM_FORMAT_C8;
2460 case DISPPLANE_BGRX555:
2461 return DRM_FORMAT_XRGB1555;
2462 case DISPPLANE_BGRX565:
2463 return DRM_FORMAT_RGB565;
2464 default:
2465 case DISPPLANE_BGRX888:
2466 return DRM_FORMAT_XRGB8888;
2467 case DISPPLANE_RGBX888:
2468 return DRM_FORMAT_XBGR8888;
2469 case DISPPLANE_BGRX101010:
2470 return DRM_FORMAT_XRGB2101010;
2471 case DISPPLANE_RGBX101010:
2472 return DRM_FORMAT_XBGR2101010;
2473 }
2474}
2475
bc8d7dff
DL
2476static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2477{
2478 switch (format) {
2479 case PLANE_CTL_FORMAT_RGB_565:
2480 return DRM_FORMAT_RGB565;
2481 default:
2482 case PLANE_CTL_FORMAT_XRGB_8888:
2483 if (rgb_order) {
2484 if (alpha)
2485 return DRM_FORMAT_ABGR8888;
2486 else
2487 return DRM_FORMAT_XBGR8888;
2488 } else {
2489 if (alpha)
2490 return DRM_FORMAT_ARGB8888;
2491 else
2492 return DRM_FORMAT_XRGB8888;
2493 }
2494 case PLANE_CTL_FORMAT_XRGB_2101010:
2495 if (rgb_order)
2496 return DRM_FORMAT_XBGR2101010;
2497 else
2498 return DRM_FORMAT_XRGB2101010;
2499 }
2500}
2501
5724dbd1 2502static bool
f6936e29
DV
2503intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2504 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2505{
2506 struct drm_device *dev = crtc->base.dev;
2507 struct drm_i915_gem_object *obj = NULL;
2508 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2509 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2510 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2511 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2512 PAGE_SIZE);
2513
2514 size_aligned -= base_aligned;
46f297fb 2515
ff2652ea
CW
2516 if (plane_config->size == 0)
2517 return false;
2518
f37b5c2b
DV
2519 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2520 base_aligned,
2521 base_aligned,
2522 size_aligned);
46f297fb 2523 if (!obj)
484b41dd 2524 return false;
46f297fb 2525
49af449b
DL
2526 obj->tiling_mode = plane_config->tiling;
2527 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2528 obj->stride = fb->pitches[0];
46f297fb 2529
6bf129df
DL
2530 mode_cmd.pixel_format = fb->pixel_format;
2531 mode_cmd.width = fb->width;
2532 mode_cmd.height = fb->height;
2533 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2534 mode_cmd.modifier[0] = fb->modifier[0];
2535 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb
JB
2536
2537 mutex_lock(&dev->struct_mutex);
6bf129df 2538 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2539 &mode_cmd, obj)) {
46f297fb
JB
2540 DRM_DEBUG_KMS("intel fb init failed\n");
2541 goto out_unref_obj;
2542 }
46f297fb 2543 mutex_unlock(&dev->struct_mutex);
484b41dd 2544
f6936e29 2545 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2546 return true;
46f297fb
JB
2547
2548out_unref_obj:
2549 drm_gem_object_unreference(&obj->base);
2550 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2551 return false;
2552}
2553
afd65eb4
MR
2554/* Update plane->state->fb to match plane->fb after driver-internal updates */
2555static void
2556update_state_fb(struct drm_plane *plane)
2557{
2558 if (plane->fb == plane->state->fb)
2559 return;
2560
2561 if (plane->state->fb)
2562 drm_framebuffer_unreference(plane->state->fb);
2563 plane->state->fb = plane->fb;
2564 if (plane->state->fb)
2565 drm_framebuffer_reference(plane->state->fb);
2566}
2567
5724dbd1 2568static void
f6936e29
DV
2569intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2570 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2571{
2572 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2573 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2574 struct drm_crtc *c;
2575 struct intel_crtc *i;
2ff8fde1 2576 struct drm_i915_gem_object *obj;
88595ac9
DV
2577 struct drm_plane *primary = intel_crtc->base.primary;
2578 struct drm_framebuffer *fb;
484b41dd 2579
2d14030b 2580 if (!plane_config->fb)
484b41dd
JB
2581 return;
2582
f6936e29 2583 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2584 fb = &plane_config->fb->base;
2585 goto valid_fb;
f55548b5 2586 }
484b41dd 2587
2d14030b 2588 kfree(plane_config->fb);
484b41dd
JB
2589
2590 /*
2591 * Failed to alloc the obj, check to see if we should share
2592 * an fb with another CRTC instead
2593 */
70e1e0ec 2594 for_each_crtc(dev, c) {
484b41dd
JB
2595 i = to_intel_crtc(c);
2596
2597 if (c == &intel_crtc->base)
2598 continue;
2599
2ff8fde1
MR
2600 if (!i->active)
2601 continue;
2602
88595ac9
DV
2603 fb = c->primary->fb;
2604 if (!fb)
484b41dd
JB
2605 continue;
2606
88595ac9 2607 obj = intel_fb_obj(fb);
2ff8fde1 2608 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2609 drm_framebuffer_reference(fb);
2610 goto valid_fb;
484b41dd
JB
2611 }
2612 }
88595ac9
DV
2613
2614 return;
2615
2616valid_fb:
2617 obj = intel_fb_obj(fb);
2618 if (obj->tiling_mode != I915_TILING_NONE)
2619 dev_priv->preserve_bios_swizzle = true;
2620
2621 primary->fb = fb;
36750f28 2622 primary->crtc = primary->state->crtc = &intel_crtc->base;
88595ac9 2623 update_state_fb(primary);
36750f28 2624 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
88595ac9 2625 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
46f297fb
JB
2626}
2627
29b9bde6
DV
2628static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2629 struct drm_framebuffer *fb,
2630 int x, int y)
81255565
JB
2631{
2632 struct drm_device *dev = crtc->dev;
2633 struct drm_i915_private *dev_priv = dev->dev_private;
2634 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2635 struct drm_plane *primary = crtc->primary;
2636 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2637 struct drm_i915_gem_object *obj;
81255565 2638 int plane = intel_crtc->plane;
e506a0c6 2639 unsigned long linear_offset;
81255565 2640 u32 dspcntr;
f45651ba 2641 u32 reg = DSPCNTR(plane);
48404c1e 2642 int pixel_size;
f45651ba 2643
b70709a6 2644 if (!visible || !fb) {
fdd508a6
VS
2645 I915_WRITE(reg, 0);
2646 if (INTEL_INFO(dev)->gen >= 4)
2647 I915_WRITE(DSPSURF(plane), 0);
2648 else
2649 I915_WRITE(DSPADDR(plane), 0);
2650 POSTING_READ(reg);
2651 return;
2652 }
2653
c9ba6fad
VS
2654 obj = intel_fb_obj(fb);
2655 if (WARN_ON(obj == NULL))
2656 return;
2657
2658 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2659
f45651ba
VS
2660 dspcntr = DISPPLANE_GAMMA_ENABLE;
2661
fdd508a6 2662 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2663
2664 if (INTEL_INFO(dev)->gen < 4) {
2665 if (intel_crtc->pipe == PIPE_B)
2666 dspcntr |= DISPPLANE_SEL_PIPE_B;
2667
2668 /* pipesrc and dspsize control the size that is scaled from,
2669 * which should always be the user's requested size.
2670 */
2671 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2672 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2673 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2674 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2675 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2676 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2677 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2678 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2679 I915_WRITE(PRIMPOS(plane), 0);
2680 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2681 }
81255565 2682
57779d06
VS
2683 switch (fb->pixel_format) {
2684 case DRM_FORMAT_C8:
81255565
JB
2685 dspcntr |= DISPPLANE_8BPP;
2686 break;
57779d06 2687 case DRM_FORMAT_XRGB1555:
57779d06 2688 dspcntr |= DISPPLANE_BGRX555;
81255565 2689 break;
57779d06
VS
2690 case DRM_FORMAT_RGB565:
2691 dspcntr |= DISPPLANE_BGRX565;
2692 break;
2693 case DRM_FORMAT_XRGB8888:
57779d06
VS
2694 dspcntr |= DISPPLANE_BGRX888;
2695 break;
2696 case DRM_FORMAT_XBGR8888:
57779d06
VS
2697 dspcntr |= DISPPLANE_RGBX888;
2698 break;
2699 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2700 dspcntr |= DISPPLANE_BGRX101010;
2701 break;
2702 case DRM_FORMAT_XBGR2101010:
57779d06 2703 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2704 break;
2705 default:
baba133a 2706 BUG();
81255565 2707 }
57779d06 2708
f45651ba
VS
2709 if (INTEL_INFO(dev)->gen >= 4 &&
2710 obj->tiling_mode != I915_TILING_NONE)
2711 dspcntr |= DISPPLANE_TILED;
81255565 2712
de1aa629
VS
2713 if (IS_G4X(dev))
2714 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2715
b9897127 2716 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2717
c2c75131
DV
2718 if (INTEL_INFO(dev)->gen >= 4) {
2719 intel_crtc->dspaddr_offset =
4e9a86b6
VS
2720 intel_gen4_compute_page_offset(dev_priv,
2721 &x, &y, obj->tiling_mode,
b9897127 2722 pixel_size,
bc752862 2723 fb->pitches[0]);
c2c75131
DV
2724 linear_offset -= intel_crtc->dspaddr_offset;
2725 } else {
e506a0c6 2726 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2727 }
e506a0c6 2728
8e7d688b 2729 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2730 dspcntr |= DISPPLANE_ROTATE_180;
2731
6e3c9717
ACO
2732 x += (intel_crtc->config->pipe_src_w - 1);
2733 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2734
2735 /* Finding the last pixel of the last line of the display
2736 data and adding to linear_offset*/
2737 linear_offset +=
6e3c9717
ACO
2738 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2739 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2740 }
2741
2742 I915_WRITE(reg, dspcntr);
2743
01f2c773 2744 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2745 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2746 I915_WRITE(DSPSURF(plane),
2747 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2748 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2749 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2750 } else
f343c5f6 2751 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2752 POSTING_READ(reg);
17638cd6
JB
2753}
2754
29b9bde6
DV
2755static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2756 struct drm_framebuffer *fb,
2757 int x, int y)
17638cd6
JB
2758{
2759 struct drm_device *dev = crtc->dev;
2760 struct drm_i915_private *dev_priv = dev->dev_private;
2761 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2762 struct drm_plane *primary = crtc->primary;
2763 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2764 struct drm_i915_gem_object *obj;
17638cd6 2765 int plane = intel_crtc->plane;
e506a0c6 2766 unsigned long linear_offset;
17638cd6 2767 u32 dspcntr;
f45651ba 2768 u32 reg = DSPCNTR(plane);
48404c1e 2769 int pixel_size;
f45651ba 2770
b70709a6 2771 if (!visible || !fb) {
fdd508a6
VS
2772 I915_WRITE(reg, 0);
2773 I915_WRITE(DSPSURF(plane), 0);
2774 POSTING_READ(reg);
2775 return;
2776 }
2777
c9ba6fad
VS
2778 obj = intel_fb_obj(fb);
2779 if (WARN_ON(obj == NULL))
2780 return;
2781
2782 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2783
f45651ba
VS
2784 dspcntr = DISPPLANE_GAMMA_ENABLE;
2785
fdd508a6 2786 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2787
2788 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2789 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2790
57779d06
VS
2791 switch (fb->pixel_format) {
2792 case DRM_FORMAT_C8:
17638cd6
JB
2793 dspcntr |= DISPPLANE_8BPP;
2794 break;
57779d06
VS
2795 case DRM_FORMAT_RGB565:
2796 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2797 break;
57779d06 2798 case DRM_FORMAT_XRGB8888:
57779d06
VS
2799 dspcntr |= DISPPLANE_BGRX888;
2800 break;
2801 case DRM_FORMAT_XBGR8888:
57779d06
VS
2802 dspcntr |= DISPPLANE_RGBX888;
2803 break;
2804 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2805 dspcntr |= DISPPLANE_BGRX101010;
2806 break;
2807 case DRM_FORMAT_XBGR2101010:
57779d06 2808 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2809 break;
2810 default:
baba133a 2811 BUG();
17638cd6
JB
2812 }
2813
2814 if (obj->tiling_mode != I915_TILING_NONE)
2815 dspcntr |= DISPPLANE_TILED;
17638cd6 2816
f45651ba 2817 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2818 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2819
b9897127 2820 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2821 intel_crtc->dspaddr_offset =
4e9a86b6
VS
2822 intel_gen4_compute_page_offset(dev_priv,
2823 &x, &y, obj->tiling_mode,
b9897127 2824 pixel_size,
bc752862 2825 fb->pitches[0]);
c2c75131 2826 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2827 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2828 dspcntr |= DISPPLANE_ROTATE_180;
2829
2830 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2831 x += (intel_crtc->config->pipe_src_w - 1);
2832 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2833
2834 /* Finding the last pixel of the last line of the display
2835 data and adding to linear_offset*/
2836 linear_offset +=
6e3c9717
ACO
2837 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2838 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2839 }
2840 }
2841
2842 I915_WRITE(reg, dspcntr);
17638cd6 2843
01f2c773 2844 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2845 I915_WRITE(DSPSURF(plane),
2846 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2847 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2848 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2849 } else {
2850 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2851 I915_WRITE(DSPLINOFF(plane), linear_offset);
2852 }
17638cd6 2853 POSTING_READ(reg);
17638cd6
JB
2854}
2855
b321803d
DL
2856u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2857 uint32_t pixel_format)
2858{
2859 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2860
2861 /*
2862 * The stride is either expressed as a multiple of 64 bytes
2863 * chunks for linear buffers or in number of tiles for tiled
2864 * buffers.
2865 */
2866 switch (fb_modifier) {
2867 case DRM_FORMAT_MOD_NONE:
2868 return 64;
2869 case I915_FORMAT_MOD_X_TILED:
2870 if (INTEL_INFO(dev)->gen == 2)
2871 return 128;
2872 return 512;
2873 case I915_FORMAT_MOD_Y_TILED:
2874 /* No need to check for old gens and Y tiling since this is
2875 * about the display engine and those will be blocked before
2876 * we get here.
2877 */
2878 return 128;
2879 case I915_FORMAT_MOD_Yf_TILED:
2880 if (bits_per_pixel == 8)
2881 return 64;
2882 else
2883 return 128;
2884 default:
2885 MISSING_CASE(fb_modifier);
2886 return 64;
2887 }
2888}
2889
121920fa
TU
2890unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2891 struct drm_i915_gem_object *obj)
2892{
9abc4648 2893 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
121920fa
TU
2894
2895 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
9abc4648 2896 view = &i915_ggtt_view_rotated;
121920fa
TU
2897
2898 return i915_gem_obj_ggtt_offset_view(obj, view);
2899}
2900
a1b2278e
CK
2901/*
2902 * This function detaches (aka. unbinds) unused scalers in hardware
2903 */
0583236e 2904static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e
CK
2905{
2906 struct drm_device *dev;
2907 struct drm_i915_private *dev_priv;
2908 struct intel_crtc_scaler_state *scaler_state;
2909 int i;
2910
a1b2278e
CK
2911 dev = intel_crtc->base.dev;
2912 dev_priv = dev->dev_private;
2913 scaler_state = &intel_crtc->config->scaler_state;
2914
2915 /* loop through and disable scalers that aren't in use */
2916 for (i = 0; i < intel_crtc->num_scalers; i++) {
2917 if (!scaler_state->scalers[i].in_use) {
2918 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2919 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2920 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2921 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2922 intel_crtc->base.base.id, intel_crtc->pipe, i);
2923 }
2924 }
2925}
2926
6156a456 2927u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2928{
6156a456 2929 switch (pixel_format) {
d161cf7a 2930 case DRM_FORMAT_C8:
c34ce3d1 2931 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2932 case DRM_FORMAT_RGB565:
c34ce3d1 2933 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2934 case DRM_FORMAT_XBGR8888:
c34ce3d1 2935 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2936 case DRM_FORMAT_XRGB8888:
c34ce3d1 2937 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2938 /*
2939 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2940 * to be already pre-multiplied. We need to add a knob (or a different
2941 * DRM_FORMAT) for user-space to configure that.
2942 */
f75fb42a 2943 case DRM_FORMAT_ABGR8888:
c34ce3d1 2944 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2945 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2946 case DRM_FORMAT_ARGB8888:
c34ce3d1 2947 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2948 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2949 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2950 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2951 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2952 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2953 case DRM_FORMAT_YUYV:
c34ce3d1 2954 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2955 case DRM_FORMAT_YVYU:
c34ce3d1 2956 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2957 case DRM_FORMAT_UYVY:
c34ce3d1 2958 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2959 case DRM_FORMAT_VYUY:
c34ce3d1 2960 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2961 default:
4249eeef 2962 MISSING_CASE(pixel_format);
70d21f0e 2963 }
8cfcba41 2964
c34ce3d1 2965 return 0;
6156a456 2966}
70d21f0e 2967
6156a456
CK
2968u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2969{
6156a456 2970 switch (fb_modifier) {
30af77c4 2971 case DRM_FORMAT_MOD_NONE:
70d21f0e 2972 break;
30af77c4 2973 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2974 return PLANE_CTL_TILED_X;
b321803d 2975 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2976 return PLANE_CTL_TILED_Y;
b321803d 2977 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2978 return PLANE_CTL_TILED_YF;
70d21f0e 2979 default:
6156a456 2980 MISSING_CASE(fb_modifier);
70d21f0e 2981 }
8cfcba41 2982
c34ce3d1 2983 return 0;
6156a456 2984}
70d21f0e 2985
6156a456
CK
2986u32 skl_plane_ctl_rotation(unsigned int rotation)
2987{
3b7a5119 2988 switch (rotation) {
6156a456
CK
2989 case BIT(DRM_ROTATE_0):
2990 break;
1e8df167
SJ
2991 /*
2992 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2993 * while i915 HW rotation is clockwise, thats why this swapping.
2994 */
3b7a5119 2995 case BIT(DRM_ROTATE_90):
1e8df167 2996 return PLANE_CTL_ROTATE_270;
3b7a5119 2997 case BIT(DRM_ROTATE_180):
c34ce3d1 2998 return PLANE_CTL_ROTATE_180;
3b7a5119 2999 case BIT(DRM_ROTATE_270):
1e8df167 3000 return PLANE_CTL_ROTATE_90;
6156a456
CK
3001 default:
3002 MISSING_CASE(rotation);
3003 }
3004
c34ce3d1 3005 return 0;
6156a456
CK
3006}
3007
3008static void skylake_update_primary_plane(struct drm_crtc *crtc,
3009 struct drm_framebuffer *fb,
3010 int x, int y)
3011{
3012 struct drm_device *dev = crtc->dev;
3013 struct drm_i915_private *dev_priv = dev->dev_private;
3014 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
3015 struct drm_plane *plane = crtc->primary;
3016 bool visible = to_intel_plane_state(plane->state)->visible;
6156a456
CK
3017 struct drm_i915_gem_object *obj;
3018 int pipe = intel_crtc->pipe;
3019 u32 plane_ctl, stride_div, stride;
3020 u32 tile_height, plane_offset, plane_size;
3021 unsigned int rotation;
3022 int x_offset, y_offset;
3023 unsigned long surf_addr;
6156a456
CK
3024 struct intel_crtc_state *crtc_state = intel_crtc->config;
3025 struct intel_plane_state *plane_state;
3026 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3027 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3028 int scaler_id = -1;
3029
6156a456
CK
3030 plane_state = to_intel_plane_state(plane->state);
3031
b70709a6 3032 if (!visible || !fb) {
6156a456
CK
3033 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3034 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3035 POSTING_READ(PLANE_CTL(pipe, 0));
3036 return;
3b7a5119 3037 }
70d21f0e 3038
6156a456
CK
3039 plane_ctl = PLANE_CTL_ENABLE |
3040 PLANE_CTL_PIPE_GAMMA_ENABLE |
3041 PLANE_CTL_PIPE_CSC_ENABLE;
3042
3043 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3044 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3045 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3046
3047 rotation = plane->state->rotation;
3048 plane_ctl |= skl_plane_ctl_rotation(rotation);
3049
b321803d
DL
3050 obj = intel_fb_obj(fb);
3051 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3052 fb->pixel_format);
3b7a5119
SJ
3053 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3054
6156a456
CK
3055 /*
3056 * FIXME: intel_plane_state->src, dst aren't set when transitional
3057 * update_plane helpers are called from legacy paths.
3058 * Once full atomic crtc is available, below check can be avoided.
3059 */
3060 if (drm_rect_width(&plane_state->src)) {
3061 scaler_id = plane_state->scaler_id;
3062 src_x = plane_state->src.x1 >> 16;
3063 src_y = plane_state->src.y1 >> 16;
3064 src_w = drm_rect_width(&plane_state->src) >> 16;
3065 src_h = drm_rect_height(&plane_state->src) >> 16;
3066 dst_x = plane_state->dst.x1;
3067 dst_y = plane_state->dst.y1;
3068 dst_w = drm_rect_width(&plane_state->dst);
3069 dst_h = drm_rect_height(&plane_state->dst);
3070
3071 WARN_ON(x != src_x || y != src_y);
3072 } else {
3073 src_w = intel_crtc->config->pipe_src_w;
3074 src_h = intel_crtc->config->pipe_src_h;
3075 }
3076
3b7a5119
SJ
3077 if (intel_rotation_90_or_270(rotation)) {
3078 /* stride = Surface height in tiles */
2614f17d 3079 tile_height = intel_tile_height(dev, fb->pixel_format,
3b7a5119
SJ
3080 fb->modifier[0]);
3081 stride = DIV_ROUND_UP(fb->height, tile_height);
6156a456 3082 x_offset = stride * tile_height - y - src_h;
3b7a5119 3083 y_offset = x;
6156a456 3084 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3085 } else {
3086 stride = fb->pitches[0] / stride_div;
3087 x_offset = x;
3088 y_offset = y;
6156a456 3089 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3090 }
3091 plane_offset = y_offset << 16 | x_offset;
b321803d 3092
70d21f0e 3093 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3094 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3095 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3096 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3097
3098 if (scaler_id >= 0) {
3099 uint32_t ps_ctrl = 0;
3100
3101 WARN_ON(!dst_w || !dst_h);
3102 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3103 crtc_state->scaler_state.scalers[scaler_id].mode;
3104 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3105 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3106 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3107 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3108 I915_WRITE(PLANE_POS(pipe, 0), 0);
3109 } else {
3110 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3111 }
3112
121920fa 3113 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3114
3115 POSTING_READ(PLANE_SURF(pipe, 0));
3116}
3117
17638cd6
JB
3118/* Assume fb object is pinned & idle & fenced and just update base pointers */
3119static int
3120intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3121 int x, int y, enum mode_set_atomic state)
3122{
3123 struct drm_device *dev = crtc->dev;
3124 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 3125
6b8e6ed0
CW
3126 if (dev_priv->display.disable_fbc)
3127 dev_priv->display.disable_fbc(dev);
81255565 3128
29b9bde6
DV
3129 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3130
3131 return 0;
81255565
JB
3132}
3133
7514747d 3134static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3135{
96a02917
VS
3136 struct drm_crtc *crtc;
3137
70e1e0ec 3138 for_each_crtc(dev, crtc) {
96a02917
VS
3139 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3140 enum plane plane = intel_crtc->plane;
3141
3142 intel_prepare_page_flip(dev, plane);
3143 intel_finish_page_flip_plane(dev, plane);
3144 }
7514747d
VS
3145}
3146
3147static void intel_update_primary_planes(struct drm_device *dev)
3148{
3149 struct drm_i915_private *dev_priv = dev->dev_private;
3150 struct drm_crtc *crtc;
96a02917 3151
70e1e0ec 3152 for_each_crtc(dev, crtc) {
96a02917
VS
3153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3154
51fd371b 3155 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
3156 /*
3157 * FIXME: Once we have proper support for primary planes (and
3158 * disabling them without disabling the entire crtc) allow again
66e514c1 3159 * a NULL crtc->primary->fb.
947fdaad 3160 */
f4510a27 3161 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 3162 dev_priv->display.update_primary_plane(crtc,
66e514c1 3163 crtc->primary->fb,
262ca2b0
MR
3164 crtc->x,
3165 crtc->y);
51fd371b 3166 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
3167 }
3168}
3169
7514747d
VS
3170void intel_prepare_reset(struct drm_device *dev)
3171{
3172 /* no reset support for gen2 */
3173 if (IS_GEN2(dev))
3174 return;
3175
3176 /* reset doesn't touch the display */
3177 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3178 return;
3179
3180 drm_modeset_lock_all(dev);
f98ce92f
VS
3181 /*
3182 * Disabling the crtcs gracefully seems nicer. Also the
3183 * g33 docs say we should at least disable all the planes.
3184 */
6b72d486 3185 intel_display_suspend(dev);
7514747d
VS
3186}
3187
3188void intel_finish_reset(struct drm_device *dev)
3189{
3190 struct drm_i915_private *dev_priv = to_i915(dev);
3191
3192 /*
3193 * Flips in the rings will be nuked by the reset,
3194 * so complete all pending flips so that user space
3195 * will get its events and not get stuck.
3196 */
3197 intel_complete_page_flips(dev);
3198
3199 /* no reset support for gen2 */
3200 if (IS_GEN2(dev))
3201 return;
3202
3203 /* reset doesn't touch the display */
3204 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3205 /*
3206 * Flips in the rings have been nuked by the reset,
3207 * so update the base address of all primary
3208 * planes to the the last fb to make sure we're
3209 * showing the correct fb after a reset.
3210 */
3211 intel_update_primary_planes(dev);
3212 return;
3213 }
3214
3215 /*
3216 * The display has been reset as well,
3217 * so need a full re-initialization.
3218 */
3219 intel_runtime_pm_disable_interrupts(dev_priv);
3220 intel_runtime_pm_enable_interrupts(dev_priv);
3221
3222 intel_modeset_init_hw(dev);
3223
3224 spin_lock_irq(&dev_priv->irq_lock);
3225 if (dev_priv->display.hpd_irq_setup)
3226 dev_priv->display.hpd_irq_setup(dev);
3227 spin_unlock_irq(&dev_priv->irq_lock);
3228
3229 intel_modeset_setup_hw_state(dev, true);
3230
3231 intel_hpd_init(dev_priv);
3232
3233 drm_modeset_unlock_all(dev);
3234}
3235
2e2f351d 3236static void
14667a4b
CW
3237intel_finish_fb(struct drm_framebuffer *old_fb)
3238{
2ff8fde1 3239 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
2e2f351d 3240 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
14667a4b
CW
3241 bool was_interruptible = dev_priv->mm.interruptible;
3242 int ret;
3243
14667a4b
CW
3244 /* Big Hammer, we also need to ensure that any pending
3245 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3246 * current scanout is retired before unpinning the old
2e2f351d
CW
3247 * framebuffer. Note that we rely on userspace rendering
3248 * into the buffer attached to the pipe they are waiting
3249 * on. If not, userspace generates a GPU hang with IPEHR
3250 * point to the MI_WAIT_FOR_EVENT.
14667a4b
CW
3251 *
3252 * This should only fail upon a hung GPU, in which case we
3253 * can safely continue.
3254 */
3255 dev_priv->mm.interruptible = false;
2e2f351d 3256 ret = i915_gem_object_wait_rendering(obj, true);
14667a4b
CW
3257 dev_priv->mm.interruptible = was_interruptible;
3258
2e2f351d 3259 WARN_ON(ret);
14667a4b
CW
3260}
3261
7d5e3799
CW
3262static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3263{
3264 struct drm_device *dev = crtc->dev;
3265 struct drm_i915_private *dev_priv = dev->dev_private;
3266 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3267 bool pending;
3268
3269 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3270 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3271 return false;
3272
5e2d7afc 3273 spin_lock_irq(&dev->event_lock);
7d5e3799 3274 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3275 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3276
3277 return pending;
3278}
3279
e30e8f75
GP
3280static void intel_update_pipe_size(struct intel_crtc *crtc)
3281{
3282 struct drm_device *dev = crtc->base.dev;
3283 struct drm_i915_private *dev_priv = dev->dev_private;
3284 const struct drm_display_mode *adjusted_mode;
3285
3286 if (!i915.fastboot)
3287 return;
3288
3289 /*
3290 * Update pipe size and adjust fitter if needed: the reason for this is
3291 * that in compute_mode_changes we check the native mode (not the pfit
3292 * mode) to see if we can flip rather than do a full mode set. In the
3293 * fastboot case, we'll flip, but if we don't update the pipesrc and
3294 * pfit state, we'll end up with a big fb scanned out into the wrong
3295 * sized surface.
3296 *
3297 * To fix this properly, we need to hoist the checks up into
3298 * compute_mode_changes (or above), check the actual pfit state and
3299 * whether the platform allows pfit disable with pipe active, and only
3300 * then update the pipesrc and pfit state, even on the flip path.
3301 */
3302
6e3c9717 3303 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
3304
3305 I915_WRITE(PIPESRC(crtc->pipe),
3306 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3307 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 3308 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
3309 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3310 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3311 I915_WRITE(PF_CTL(crtc->pipe), 0);
3312 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3313 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3314 }
6e3c9717
ACO
3315 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3316 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3317}
3318
5e84e1a4
ZW
3319static void intel_fdi_normal_train(struct drm_crtc *crtc)
3320{
3321 struct drm_device *dev = crtc->dev;
3322 struct drm_i915_private *dev_priv = dev->dev_private;
3323 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3324 int pipe = intel_crtc->pipe;
3325 u32 reg, temp;
3326
3327 /* enable normal train */
3328 reg = FDI_TX_CTL(pipe);
3329 temp = I915_READ(reg);
61e499bf 3330 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3331 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3332 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3333 } else {
3334 temp &= ~FDI_LINK_TRAIN_NONE;
3335 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3336 }
5e84e1a4
ZW
3337 I915_WRITE(reg, temp);
3338
3339 reg = FDI_RX_CTL(pipe);
3340 temp = I915_READ(reg);
3341 if (HAS_PCH_CPT(dev)) {
3342 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3343 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3344 } else {
3345 temp &= ~FDI_LINK_TRAIN_NONE;
3346 temp |= FDI_LINK_TRAIN_NONE;
3347 }
3348 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3349
3350 /* wait one idle pattern time */
3351 POSTING_READ(reg);
3352 udelay(1000);
357555c0
JB
3353
3354 /* IVB wants error correction enabled */
3355 if (IS_IVYBRIDGE(dev))
3356 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3357 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3358}
3359
8db9d77b
ZW
3360/* The FDI link training functions for ILK/Ibexpeak. */
3361static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3362{
3363 struct drm_device *dev = crtc->dev;
3364 struct drm_i915_private *dev_priv = dev->dev_private;
3365 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3366 int pipe = intel_crtc->pipe;
5eddb70b 3367 u32 reg, temp, tries;
8db9d77b 3368
1c8562f6 3369 /* FDI needs bits from pipe first */
0fc932b8 3370 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3371
e1a44743
AJ
3372 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3373 for train result */
5eddb70b
CW
3374 reg = FDI_RX_IMR(pipe);
3375 temp = I915_READ(reg);
e1a44743
AJ
3376 temp &= ~FDI_RX_SYMBOL_LOCK;
3377 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3378 I915_WRITE(reg, temp);
3379 I915_READ(reg);
e1a44743
AJ
3380 udelay(150);
3381
8db9d77b 3382 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3383 reg = FDI_TX_CTL(pipe);
3384 temp = I915_READ(reg);
627eb5a3 3385 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3386 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3387 temp &= ~FDI_LINK_TRAIN_NONE;
3388 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3389 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3390
5eddb70b
CW
3391 reg = FDI_RX_CTL(pipe);
3392 temp = I915_READ(reg);
8db9d77b
ZW
3393 temp &= ~FDI_LINK_TRAIN_NONE;
3394 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3395 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3396
3397 POSTING_READ(reg);
8db9d77b
ZW
3398 udelay(150);
3399
5b2adf89 3400 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3401 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3402 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3403 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3404
5eddb70b 3405 reg = FDI_RX_IIR(pipe);
e1a44743 3406 for (tries = 0; tries < 5; tries++) {
5eddb70b 3407 temp = I915_READ(reg);
8db9d77b
ZW
3408 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3409
3410 if ((temp & FDI_RX_BIT_LOCK)) {
3411 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3412 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3413 break;
3414 }
8db9d77b 3415 }
e1a44743 3416 if (tries == 5)
5eddb70b 3417 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3418
3419 /* Train 2 */
5eddb70b
CW
3420 reg = FDI_TX_CTL(pipe);
3421 temp = I915_READ(reg);
8db9d77b
ZW
3422 temp &= ~FDI_LINK_TRAIN_NONE;
3423 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3424 I915_WRITE(reg, temp);
8db9d77b 3425
5eddb70b
CW
3426 reg = FDI_RX_CTL(pipe);
3427 temp = I915_READ(reg);
8db9d77b
ZW
3428 temp &= ~FDI_LINK_TRAIN_NONE;
3429 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3430 I915_WRITE(reg, temp);
8db9d77b 3431
5eddb70b
CW
3432 POSTING_READ(reg);
3433 udelay(150);
8db9d77b 3434
5eddb70b 3435 reg = FDI_RX_IIR(pipe);
e1a44743 3436 for (tries = 0; tries < 5; tries++) {
5eddb70b 3437 temp = I915_READ(reg);
8db9d77b
ZW
3438 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3439
3440 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3441 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3442 DRM_DEBUG_KMS("FDI train 2 done.\n");
3443 break;
3444 }
8db9d77b 3445 }
e1a44743 3446 if (tries == 5)
5eddb70b 3447 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3448
3449 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3450
8db9d77b
ZW
3451}
3452
0206e353 3453static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3454 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3455 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3456 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3457 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3458};
3459
3460/* The FDI link training functions for SNB/Cougarpoint. */
3461static void gen6_fdi_link_train(struct drm_crtc *crtc)
3462{
3463 struct drm_device *dev = crtc->dev;
3464 struct drm_i915_private *dev_priv = dev->dev_private;
3465 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3466 int pipe = intel_crtc->pipe;
fa37d39e 3467 u32 reg, temp, i, retry;
8db9d77b 3468
e1a44743
AJ
3469 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3470 for train result */
5eddb70b
CW
3471 reg = FDI_RX_IMR(pipe);
3472 temp = I915_READ(reg);
e1a44743
AJ
3473 temp &= ~FDI_RX_SYMBOL_LOCK;
3474 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3475 I915_WRITE(reg, temp);
3476
3477 POSTING_READ(reg);
e1a44743
AJ
3478 udelay(150);
3479
8db9d77b 3480 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3481 reg = FDI_TX_CTL(pipe);
3482 temp = I915_READ(reg);
627eb5a3 3483 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3484 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3485 temp &= ~FDI_LINK_TRAIN_NONE;
3486 temp |= FDI_LINK_TRAIN_PATTERN_1;
3487 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3488 /* SNB-B */
3489 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3490 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3491
d74cf324
DV
3492 I915_WRITE(FDI_RX_MISC(pipe),
3493 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3494
5eddb70b
CW
3495 reg = FDI_RX_CTL(pipe);
3496 temp = I915_READ(reg);
8db9d77b
ZW
3497 if (HAS_PCH_CPT(dev)) {
3498 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3499 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3500 } else {
3501 temp &= ~FDI_LINK_TRAIN_NONE;
3502 temp |= FDI_LINK_TRAIN_PATTERN_1;
3503 }
5eddb70b
CW
3504 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3505
3506 POSTING_READ(reg);
8db9d77b
ZW
3507 udelay(150);
3508
0206e353 3509 for (i = 0; i < 4; i++) {
5eddb70b
CW
3510 reg = FDI_TX_CTL(pipe);
3511 temp = I915_READ(reg);
8db9d77b
ZW
3512 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3513 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3514 I915_WRITE(reg, temp);
3515
3516 POSTING_READ(reg);
8db9d77b
ZW
3517 udelay(500);
3518
fa37d39e
SP
3519 for (retry = 0; retry < 5; retry++) {
3520 reg = FDI_RX_IIR(pipe);
3521 temp = I915_READ(reg);
3522 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3523 if (temp & FDI_RX_BIT_LOCK) {
3524 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3525 DRM_DEBUG_KMS("FDI train 1 done.\n");
3526 break;
3527 }
3528 udelay(50);
8db9d77b 3529 }
fa37d39e
SP
3530 if (retry < 5)
3531 break;
8db9d77b
ZW
3532 }
3533 if (i == 4)
5eddb70b 3534 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3535
3536 /* Train 2 */
5eddb70b
CW
3537 reg = FDI_TX_CTL(pipe);
3538 temp = I915_READ(reg);
8db9d77b
ZW
3539 temp &= ~FDI_LINK_TRAIN_NONE;
3540 temp |= FDI_LINK_TRAIN_PATTERN_2;
3541 if (IS_GEN6(dev)) {
3542 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3543 /* SNB-B */
3544 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3545 }
5eddb70b 3546 I915_WRITE(reg, temp);
8db9d77b 3547
5eddb70b
CW
3548 reg = FDI_RX_CTL(pipe);
3549 temp = I915_READ(reg);
8db9d77b
ZW
3550 if (HAS_PCH_CPT(dev)) {
3551 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3552 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3553 } else {
3554 temp &= ~FDI_LINK_TRAIN_NONE;
3555 temp |= FDI_LINK_TRAIN_PATTERN_2;
3556 }
5eddb70b
CW
3557 I915_WRITE(reg, temp);
3558
3559 POSTING_READ(reg);
8db9d77b
ZW
3560 udelay(150);
3561
0206e353 3562 for (i = 0; i < 4; i++) {
5eddb70b
CW
3563 reg = FDI_TX_CTL(pipe);
3564 temp = I915_READ(reg);
8db9d77b
ZW
3565 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3566 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3567 I915_WRITE(reg, temp);
3568
3569 POSTING_READ(reg);
8db9d77b
ZW
3570 udelay(500);
3571
fa37d39e
SP
3572 for (retry = 0; retry < 5; retry++) {
3573 reg = FDI_RX_IIR(pipe);
3574 temp = I915_READ(reg);
3575 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3576 if (temp & FDI_RX_SYMBOL_LOCK) {
3577 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3578 DRM_DEBUG_KMS("FDI train 2 done.\n");
3579 break;
3580 }
3581 udelay(50);
8db9d77b 3582 }
fa37d39e
SP
3583 if (retry < 5)
3584 break;
8db9d77b
ZW
3585 }
3586 if (i == 4)
5eddb70b 3587 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3588
3589 DRM_DEBUG_KMS("FDI train done.\n");
3590}
3591
357555c0
JB
3592/* Manual link training for Ivy Bridge A0 parts */
3593static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3594{
3595 struct drm_device *dev = crtc->dev;
3596 struct drm_i915_private *dev_priv = dev->dev_private;
3597 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3598 int pipe = intel_crtc->pipe;
139ccd3f 3599 u32 reg, temp, i, j;
357555c0
JB
3600
3601 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3602 for train result */
3603 reg = FDI_RX_IMR(pipe);
3604 temp = I915_READ(reg);
3605 temp &= ~FDI_RX_SYMBOL_LOCK;
3606 temp &= ~FDI_RX_BIT_LOCK;
3607 I915_WRITE(reg, temp);
3608
3609 POSTING_READ(reg);
3610 udelay(150);
3611
01a415fd
DV
3612 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3613 I915_READ(FDI_RX_IIR(pipe)));
3614
139ccd3f
JB
3615 /* Try each vswing and preemphasis setting twice before moving on */
3616 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3617 /* disable first in case we need to retry */
3618 reg = FDI_TX_CTL(pipe);
3619 temp = I915_READ(reg);
3620 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3621 temp &= ~FDI_TX_ENABLE;
3622 I915_WRITE(reg, temp);
357555c0 3623
139ccd3f
JB
3624 reg = FDI_RX_CTL(pipe);
3625 temp = I915_READ(reg);
3626 temp &= ~FDI_LINK_TRAIN_AUTO;
3627 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3628 temp &= ~FDI_RX_ENABLE;
3629 I915_WRITE(reg, temp);
357555c0 3630
139ccd3f 3631 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3632 reg = FDI_TX_CTL(pipe);
3633 temp = I915_READ(reg);
139ccd3f 3634 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3635 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3636 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3637 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3638 temp |= snb_b_fdi_train_param[j/2];
3639 temp |= FDI_COMPOSITE_SYNC;
3640 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3641
139ccd3f
JB
3642 I915_WRITE(FDI_RX_MISC(pipe),
3643 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3644
139ccd3f 3645 reg = FDI_RX_CTL(pipe);
357555c0 3646 temp = I915_READ(reg);
139ccd3f
JB
3647 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3648 temp |= FDI_COMPOSITE_SYNC;
3649 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3650
139ccd3f
JB
3651 POSTING_READ(reg);
3652 udelay(1); /* should be 0.5us */
357555c0 3653
139ccd3f
JB
3654 for (i = 0; i < 4; i++) {
3655 reg = FDI_RX_IIR(pipe);
3656 temp = I915_READ(reg);
3657 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3658
139ccd3f
JB
3659 if (temp & FDI_RX_BIT_LOCK ||
3660 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3661 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3662 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3663 i);
3664 break;
3665 }
3666 udelay(1); /* should be 0.5us */
3667 }
3668 if (i == 4) {
3669 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3670 continue;
3671 }
357555c0 3672
139ccd3f 3673 /* Train 2 */
357555c0
JB
3674 reg = FDI_TX_CTL(pipe);
3675 temp = I915_READ(reg);
139ccd3f
JB
3676 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3677 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3678 I915_WRITE(reg, temp);
3679
3680 reg = FDI_RX_CTL(pipe);
3681 temp = I915_READ(reg);
3682 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3683 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3684 I915_WRITE(reg, temp);
3685
3686 POSTING_READ(reg);
139ccd3f 3687 udelay(2); /* should be 1.5us */
357555c0 3688
139ccd3f
JB
3689 for (i = 0; i < 4; i++) {
3690 reg = FDI_RX_IIR(pipe);
3691 temp = I915_READ(reg);
3692 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3693
139ccd3f
JB
3694 if (temp & FDI_RX_SYMBOL_LOCK ||
3695 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3696 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3697 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3698 i);
3699 goto train_done;
3700 }
3701 udelay(2); /* should be 1.5us */
357555c0 3702 }
139ccd3f
JB
3703 if (i == 4)
3704 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3705 }
357555c0 3706
139ccd3f 3707train_done:
357555c0
JB
3708 DRM_DEBUG_KMS("FDI train done.\n");
3709}
3710
88cefb6c 3711static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3712{
88cefb6c 3713 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3714 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3715 int pipe = intel_crtc->pipe;
5eddb70b 3716 u32 reg, temp;
79e53945 3717
c64e311e 3718
c98e9dcf 3719 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3720 reg = FDI_RX_CTL(pipe);
3721 temp = I915_READ(reg);
627eb5a3 3722 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3723 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3724 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3725 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3726
3727 POSTING_READ(reg);
c98e9dcf
JB
3728 udelay(200);
3729
3730 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3731 temp = I915_READ(reg);
3732 I915_WRITE(reg, temp | FDI_PCDCLK);
3733
3734 POSTING_READ(reg);
c98e9dcf
JB
3735 udelay(200);
3736
20749730
PZ
3737 /* Enable CPU FDI TX PLL, always on for Ironlake */
3738 reg = FDI_TX_CTL(pipe);
3739 temp = I915_READ(reg);
3740 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3741 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3742
20749730
PZ
3743 POSTING_READ(reg);
3744 udelay(100);
6be4a607 3745 }
0e23b99d
JB
3746}
3747
88cefb6c
DV
3748static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3749{
3750 struct drm_device *dev = intel_crtc->base.dev;
3751 struct drm_i915_private *dev_priv = dev->dev_private;
3752 int pipe = intel_crtc->pipe;
3753 u32 reg, temp;
3754
3755 /* Switch from PCDclk to Rawclk */
3756 reg = FDI_RX_CTL(pipe);
3757 temp = I915_READ(reg);
3758 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3759
3760 /* Disable CPU FDI TX PLL */
3761 reg = FDI_TX_CTL(pipe);
3762 temp = I915_READ(reg);
3763 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3764
3765 POSTING_READ(reg);
3766 udelay(100);
3767
3768 reg = FDI_RX_CTL(pipe);
3769 temp = I915_READ(reg);
3770 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3771
3772 /* Wait for the clocks to turn off. */
3773 POSTING_READ(reg);
3774 udelay(100);
3775}
3776
0fc932b8
JB
3777static void ironlake_fdi_disable(struct drm_crtc *crtc)
3778{
3779 struct drm_device *dev = crtc->dev;
3780 struct drm_i915_private *dev_priv = dev->dev_private;
3781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3782 int pipe = intel_crtc->pipe;
3783 u32 reg, temp;
3784
3785 /* disable CPU FDI tx and PCH FDI rx */
3786 reg = FDI_TX_CTL(pipe);
3787 temp = I915_READ(reg);
3788 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3789 POSTING_READ(reg);
3790
3791 reg = FDI_RX_CTL(pipe);
3792 temp = I915_READ(reg);
3793 temp &= ~(0x7 << 16);
dfd07d72 3794 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3795 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3796
3797 POSTING_READ(reg);
3798 udelay(100);
3799
3800 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3801 if (HAS_PCH_IBX(dev))
6f06ce18 3802 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3803
3804 /* still set train pattern 1 */
3805 reg = FDI_TX_CTL(pipe);
3806 temp = I915_READ(reg);
3807 temp &= ~FDI_LINK_TRAIN_NONE;
3808 temp |= FDI_LINK_TRAIN_PATTERN_1;
3809 I915_WRITE(reg, temp);
3810
3811 reg = FDI_RX_CTL(pipe);
3812 temp = I915_READ(reg);
3813 if (HAS_PCH_CPT(dev)) {
3814 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3815 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3816 } else {
3817 temp &= ~FDI_LINK_TRAIN_NONE;
3818 temp |= FDI_LINK_TRAIN_PATTERN_1;
3819 }
3820 /* BPC in FDI rx is consistent with that in PIPECONF */
3821 temp &= ~(0x07 << 16);
dfd07d72 3822 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3823 I915_WRITE(reg, temp);
3824
3825 POSTING_READ(reg);
3826 udelay(100);
3827}
3828
5dce5b93
CW
3829bool intel_has_pending_fb_unpin(struct drm_device *dev)
3830{
3831 struct intel_crtc *crtc;
3832
3833 /* Note that we don't need to be called with mode_config.lock here
3834 * as our list of CRTC objects is static for the lifetime of the
3835 * device and so cannot disappear as we iterate. Similarly, we can
3836 * happily treat the predicates as racy, atomic checks as userspace
3837 * cannot claim and pin a new fb without at least acquring the
3838 * struct_mutex and so serialising with us.
3839 */
d3fcc808 3840 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3841 if (atomic_read(&crtc->unpin_work_count) == 0)
3842 continue;
3843
3844 if (crtc->unpin_work)
3845 intel_wait_for_vblank(dev, crtc->pipe);
3846
3847 return true;
3848 }
3849
3850 return false;
3851}
3852
d6bbafa1
CW
3853static void page_flip_completed(struct intel_crtc *intel_crtc)
3854{
3855 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3856 struct intel_unpin_work *work = intel_crtc->unpin_work;
3857
3858 /* ensure that the unpin work is consistent wrt ->pending. */
3859 smp_rmb();
3860 intel_crtc->unpin_work = NULL;
3861
3862 if (work->event)
3863 drm_send_vblank_event(intel_crtc->base.dev,
3864 intel_crtc->pipe,
3865 work->event);
3866
3867 drm_crtc_vblank_put(&intel_crtc->base);
3868
3869 wake_up_all(&dev_priv->pending_flip_queue);
3870 queue_work(dev_priv->wq, &work->work);
3871
3872 trace_i915_flip_complete(intel_crtc->plane,
3873 work->pending_flip_obj);
3874}
3875
46a55d30 3876void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3877{
0f91128d 3878 struct drm_device *dev = crtc->dev;
5bb61643 3879 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3880
2c10d571 3881 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3882 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3883 !intel_crtc_has_pending_flip(crtc),
3884 60*HZ) == 0)) {
3885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3886
5e2d7afc 3887 spin_lock_irq(&dev->event_lock);
9c787942
CW
3888 if (intel_crtc->unpin_work) {
3889 WARN_ONCE(1, "Removing stuck page flip\n");
3890 page_flip_completed(intel_crtc);
3891 }
5e2d7afc 3892 spin_unlock_irq(&dev->event_lock);
9c787942 3893 }
5bb61643 3894
975d568a
CW
3895 if (crtc->primary->fb) {
3896 mutex_lock(&dev->struct_mutex);
3897 intel_finish_fb(crtc->primary->fb);
3898 mutex_unlock(&dev->struct_mutex);
3899 }
e6c3a2a6
CW
3900}
3901
e615efe4
ED
3902/* Program iCLKIP clock to the desired frequency */
3903static void lpt_program_iclkip(struct drm_crtc *crtc)
3904{
3905 struct drm_device *dev = crtc->dev;
3906 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3907 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3908 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3909 u32 temp;
3910
a580516d 3911 mutex_lock(&dev_priv->sb_lock);
09153000 3912
e615efe4
ED
3913 /* It is necessary to ungate the pixclk gate prior to programming
3914 * the divisors, and gate it back when it is done.
3915 */
3916 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3917
3918 /* Disable SSCCTL */
3919 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3920 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3921 SBI_SSCCTL_DISABLE,
3922 SBI_ICLK);
e615efe4
ED
3923
3924 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3925 if (clock == 20000) {
e615efe4
ED
3926 auxdiv = 1;
3927 divsel = 0x41;
3928 phaseinc = 0x20;
3929 } else {
3930 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3931 * but the adjusted_mode->crtc_clock in in KHz. To get the
3932 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3933 * convert the virtual clock precision to KHz here for higher
3934 * precision.
3935 */
3936 u32 iclk_virtual_root_freq = 172800 * 1000;
3937 u32 iclk_pi_range = 64;
3938 u32 desired_divisor, msb_divisor_value, pi_value;
3939
12d7ceed 3940 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3941 msb_divisor_value = desired_divisor / iclk_pi_range;
3942 pi_value = desired_divisor % iclk_pi_range;
3943
3944 auxdiv = 0;
3945 divsel = msb_divisor_value - 2;
3946 phaseinc = pi_value;
3947 }
3948
3949 /* This should not happen with any sane values */
3950 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3951 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3952 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3953 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3954
3955 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3956 clock,
e615efe4
ED
3957 auxdiv,
3958 divsel,
3959 phasedir,
3960 phaseinc);
3961
3962 /* Program SSCDIVINTPHASE6 */
988d6ee8 3963 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3964 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3965 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3966 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3967 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3968 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3969 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3970 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3971
3972 /* Program SSCAUXDIV */
988d6ee8 3973 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3974 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3975 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3976 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3977
3978 /* Enable modulator and associated divider */
988d6ee8 3979 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3980 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3981 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3982
3983 /* Wait for initialization time */
3984 udelay(24);
3985
3986 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000 3987
a580516d 3988 mutex_unlock(&dev_priv->sb_lock);
e615efe4
ED
3989}
3990
275f01b2
DV
3991static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3992 enum pipe pch_transcoder)
3993{
3994 struct drm_device *dev = crtc->base.dev;
3995 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3996 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
3997
3998 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3999 I915_READ(HTOTAL(cpu_transcoder)));
4000 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4001 I915_READ(HBLANK(cpu_transcoder)));
4002 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4003 I915_READ(HSYNC(cpu_transcoder)));
4004
4005 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4006 I915_READ(VTOTAL(cpu_transcoder)));
4007 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4008 I915_READ(VBLANK(cpu_transcoder)));
4009 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4010 I915_READ(VSYNC(cpu_transcoder)));
4011 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4012 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4013}
4014
003632d9 4015static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4016{
4017 struct drm_i915_private *dev_priv = dev->dev_private;
4018 uint32_t temp;
4019
4020 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4021 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4022 return;
4023
4024 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4025 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4026
003632d9
ACO
4027 temp &= ~FDI_BC_BIFURCATION_SELECT;
4028 if (enable)
4029 temp |= FDI_BC_BIFURCATION_SELECT;
4030
4031 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4032 I915_WRITE(SOUTH_CHICKEN1, temp);
4033 POSTING_READ(SOUTH_CHICKEN1);
4034}
4035
4036static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4037{
4038 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4039
4040 switch (intel_crtc->pipe) {
4041 case PIPE_A:
4042 break;
4043 case PIPE_B:
6e3c9717 4044 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4045 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4046 else
003632d9 4047 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4048
4049 break;
4050 case PIPE_C:
003632d9 4051 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4052
4053 break;
4054 default:
4055 BUG();
4056 }
4057}
4058
f67a559d
JB
4059/*
4060 * Enable PCH resources required for PCH ports:
4061 * - PCH PLLs
4062 * - FDI training & RX/TX
4063 * - update transcoder timings
4064 * - DP transcoding bits
4065 * - transcoder
4066 */
4067static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4068{
4069 struct drm_device *dev = crtc->dev;
4070 struct drm_i915_private *dev_priv = dev->dev_private;
4071 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4072 int pipe = intel_crtc->pipe;
ee7b9f93 4073 u32 reg, temp;
2c07245f 4074
ab9412ba 4075 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4076
1fbc0d78
DV
4077 if (IS_IVYBRIDGE(dev))
4078 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4079
cd986abb
DV
4080 /* Write the TU size bits before fdi link training, so that error
4081 * detection works. */
4082 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4083 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4084
c98e9dcf 4085 /* For PCH output, training FDI link */
674cf967 4086 dev_priv->display.fdi_link_train(crtc);
2c07245f 4087
3ad8a208
DV
4088 /* We need to program the right clock selection before writing the pixel
4089 * mutliplier into the DPLL. */
303b81e0 4090 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4091 u32 sel;
4b645f14 4092
c98e9dcf 4093 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4094 temp |= TRANS_DPLL_ENABLE(pipe);
4095 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 4096 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
4097 temp |= sel;
4098 else
4099 temp &= ~sel;
c98e9dcf 4100 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4101 }
5eddb70b 4102
3ad8a208
DV
4103 /* XXX: pch pll's can be enabled any time before we enable the PCH
4104 * transcoder, and we actually should do this to not upset any PCH
4105 * transcoder that already use the clock when we share it.
4106 *
4107 * Note that enable_shared_dpll tries to do the right thing, but
4108 * get_shared_dpll unconditionally resets the pll - we need that to have
4109 * the right LVDS enable sequence. */
85b3894f 4110 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4111
d9b6cb56
JB
4112 /* set transcoder timing, panel must allow it */
4113 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4114 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4115
303b81e0 4116 intel_fdi_normal_train(crtc);
5e84e1a4 4117
c98e9dcf 4118 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4119 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 4120 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
4121 reg = TRANS_DP_CTL(pipe);
4122 temp = I915_READ(reg);
4123 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4124 TRANS_DP_SYNC_MASK |
4125 TRANS_DP_BPC_MASK);
e3ef4479 4126 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4127 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
4128
4129 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4130 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 4131 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4132 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4133
4134 switch (intel_trans_dp_port_sel(crtc)) {
4135 case PCH_DP_B:
5eddb70b 4136 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
4137 break;
4138 case PCH_DP_C:
5eddb70b 4139 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
4140 break;
4141 case PCH_DP_D:
5eddb70b 4142 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4143 break;
4144 default:
e95d41e1 4145 BUG();
32f9d658 4146 }
2c07245f 4147
5eddb70b 4148 I915_WRITE(reg, temp);
6be4a607 4149 }
b52eb4dc 4150
b8a4f404 4151 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4152}
4153
1507e5bd
PZ
4154static void lpt_pch_enable(struct drm_crtc *crtc)
4155{
4156 struct drm_device *dev = crtc->dev;
4157 struct drm_i915_private *dev_priv = dev->dev_private;
4158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4159 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4160
ab9412ba 4161 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4162
8c52b5e8 4163 lpt_program_iclkip(crtc);
1507e5bd 4164
0540e488 4165 /* Set transcoder timing. */
275f01b2 4166 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4167
937bb610 4168 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4169}
4170
190f68c5
ACO
4171struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4172 struct intel_crtc_state *crtc_state)
ee7b9f93 4173{
e2b78267 4174 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 4175 struct intel_shared_dpll *pll;
de419ab6 4176 struct intel_shared_dpll_config *shared_dpll;
e2b78267 4177 enum intel_dpll_id i;
ee7b9f93 4178
de419ab6
ML
4179 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4180
98b6bd99
DV
4181 if (HAS_PCH_IBX(dev_priv->dev)) {
4182 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 4183 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 4184 pll = &dev_priv->shared_dplls[i];
98b6bd99 4185
46edb027
DV
4186 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4187 crtc->base.base.id, pll->name);
98b6bd99 4188
de419ab6 4189 WARN_ON(shared_dpll[i].crtc_mask);
f2a69f44 4190
98b6bd99
DV
4191 goto found;
4192 }
4193
bcddf610
S
4194 if (IS_BROXTON(dev_priv->dev)) {
4195 /* PLL is attached to port in bxt */
4196 struct intel_encoder *encoder;
4197 struct intel_digital_port *intel_dig_port;
4198
4199 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4200 if (WARN_ON(!encoder))
4201 return NULL;
4202
4203 intel_dig_port = enc_to_dig_port(&encoder->base);
4204 /* 1:1 mapping between ports and PLLs */
4205 i = (enum intel_dpll_id)intel_dig_port->port;
4206 pll = &dev_priv->shared_dplls[i];
4207 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4208 crtc->base.base.id, pll->name);
de419ab6 4209 WARN_ON(shared_dpll[i].crtc_mask);
bcddf610
S
4210
4211 goto found;
4212 }
4213
e72f9fbf
DV
4214 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4215 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
4216
4217 /* Only want to check enabled timings first */
de419ab6 4218 if (shared_dpll[i].crtc_mask == 0)
ee7b9f93
JB
4219 continue;
4220
190f68c5 4221 if (memcmp(&crtc_state->dpll_hw_state,
de419ab6
ML
4222 &shared_dpll[i].hw_state,
4223 sizeof(crtc_state->dpll_hw_state)) == 0) {
8bd31e67 4224 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 4225 crtc->base.base.id, pll->name,
de419ab6 4226 shared_dpll[i].crtc_mask,
8bd31e67 4227 pll->active);
ee7b9f93
JB
4228 goto found;
4229 }
4230 }
4231
4232 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
4233 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4234 pll = &dev_priv->shared_dplls[i];
de419ab6 4235 if (shared_dpll[i].crtc_mask == 0) {
46edb027
DV
4236 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4237 crtc->base.base.id, pll->name);
ee7b9f93
JB
4238 goto found;
4239 }
4240 }
4241
4242 return NULL;
4243
4244found:
de419ab6
ML
4245 if (shared_dpll[i].crtc_mask == 0)
4246 shared_dpll[i].hw_state =
4247 crtc_state->dpll_hw_state;
f2a69f44 4248
190f68c5 4249 crtc_state->shared_dpll = i;
46edb027
DV
4250 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4251 pipe_name(crtc->pipe));
ee7b9f93 4252
de419ab6 4253 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
e04c7350 4254
ee7b9f93
JB
4255 return pll;
4256}
4257
de419ab6 4258static void intel_shared_dpll_commit(struct drm_atomic_state *state)
8bd31e67 4259{
de419ab6
ML
4260 struct drm_i915_private *dev_priv = to_i915(state->dev);
4261 struct intel_shared_dpll_config *shared_dpll;
8bd31e67
ACO
4262 struct intel_shared_dpll *pll;
4263 enum intel_dpll_id i;
4264
de419ab6
ML
4265 if (!to_intel_atomic_state(state)->dpll_set)
4266 return;
8bd31e67 4267
de419ab6 4268 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
8bd31e67
ACO
4269 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4270 pll = &dev_priv->shared_dplls[i];
de419ab6 4271 pll->config = shared_dpll[i];
8bd31e67
ACO
4272 }
4273}
4274
a1520318 4275static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4276{
4277 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4278 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4279 u32 temp;
4280
4281 temp = I915_READ(dslreg);
4282 udelay(500);
4283 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4284 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4285 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4286 }
4287}
4288
86adf9d7
ML
4289static int
4290skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4291 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4292 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4293{
86adf9d7
ML
4294 struct intel_crtc_scaler_state *scaler_state =
4295 &crtc_state->scaler_state;
4296 struct intel_crtc *intel_crtc =
4297 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4298 int need_scaling;
6156a456
CK
4299
4300 need_scaling = intel_rotation_90_or_270(rotation) ?
4301 (src_h != dst_w || src_w != dst_h):
4302 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4303
4304 /*
4305 * if plane is being disabled or scaler is no more required or force detach
4306 * - free scaler binded to this plane/crtc
4307 * - in order to do this, update crtc->scaler_usage
4308 *
4309 * Here scaler state in crtc_state is set free so that
4310 * scaler can be assigned to other user. Actual register
4311 * update to free the scaler is done in plane/panel-fit programming.
4312 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4313 */
86adf9d7 4314 if (force_detach || !need_scaling) {
a1b2278e 4315 if (*scaler_id >= 0) {
86adf9d7 4316 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4317 scaler_state->scalers[*scaler_id].in_use = 0;
4318
86adf9d7
ML
4319 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4320 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4321 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4322 scaler_state->scaler_users);
4323 *scaler_id = -1;
4324 }
4325 return 0;
4326 }
4327
4328 /* range checks */
4329 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4330 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4331
4332 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4333 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4334 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4335 "size is out of scaler range\n",
86adf9d7 4336 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4337 return -EINVAL;
4338 }
4339
86adf9d7
ML
4340 /* mark this plane as a scaler user in crtc_state */
4341 scaler_state->scaler_users |= (1 << scaler_user);
4342 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4343 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4344 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4345 scaler_state->scaler_users);
4346
4347 return 0;
4348}
4349
4350/**
4351 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4352 *
4353 * @state: crtc's scaler state
4354 * @force_detach: whether to forcibly disable scaler
4355 *
4356 * Return
4357 * 0 - scaler_usage updated successfully
4358 * error - requested scaling cannot be supported or other error condition
4359 */
4360int skl_update_scaler_crtc(struct intel_crtc_state *state, int force_detach)
4361{
4362 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4363 struct drm_display_mode *adjusted_mode =
4364 &state->base.adjusted_mode;
4365
4366 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4367 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4368
4369 return skl_update_scaler(state, force_detach, SKL_CRTC_INDEX,
4370 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4371 state->pipe_src_w, state->pipe_src_h,
4372 adjusted_mode->hdisplay, adjusted_mode->hdisplay);
4373}
4374
4375/**
4376 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4377 *
4378 * @state: crtc's scaler state
86adf9d7
ML
4379 * @plane_state: atomic plane state to update
4380 *
4381 * Return
4382 * 0 - scaler_usage updated successfully
4383 * error - requested scaling cannot be supported or other error condition
4384 */
da20eabd
ML
4385static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4386 struct intel_plane_state *plane_state)
86adf9d7
ML
4387{
4388
4389 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4390 struct intel_plane *intel_plane =
4391 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4392 struct drm_framebuffer *fb = plane_state->base.fb;
4393 int ret;
4394
4395 bool force_detach = !fb || !plane_state->visible;
4396
4397 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4398 intel_plane->base.base.id, intel_crtc->pipe,
4399 drm_plane_index(&intel_plane->base));
4400
4401 ret = skl_update_scaler(crtc_state, force_detach,
4402 drm_plane_index(&intel_plane->base),
4403 &plane_state->scaler_id,
4404 plane_state->base.rotation,
4405 drm_rect_width(&plane_state->src) >> 16,
4406 drm_rect_height(&plane_state->src) >> 16,
4407 drm_rect_width(&plane_state->dst),
4408 drm_rect_height(&plane_state->dst));
4409
4410 if (ret || plane_state->scaler_id < 0)
4411 return ret;
4412
a1b2278e 4413 /* check colorkey */
818ed961 4414 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
86adf9d7 4415 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
818ed961 4416 intel_plane->base.base.id);
a1b2278e
CK
4417 return -EINVAL;
4418 }
4419
4420 /* Check src format */
86adf9d7
ML
4421 switch (fb->pixel_format) {
4422 case DRM_FORMAT_RGB565:
4423 case DRM_FORMAT_XBGR8888:
4424 case DRM_FORMAT_XRGB8888:
4425 case DRM_FORMAT_ABGR8888:
4426 case DRM_FORMAT_ARGB8888:
4427 case DRM_FORMAT_XRGB2101010:
4428 case DRM_FORMAT_XBGR2101010:
4429 case DRM_FORMAT_YUYV:
4430 case DRM_FORMAT_YVYU:
4431 case DRM_FORMAT_UYVY:
4432 case DRM_FORMAT_VYUY:
4433 break;
4434 default:
4435 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4436 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4437 return -EINVAL;
a1b2278e
CK
4438 }
4439
a1b2278e
CK
4440 return 0;
4441}
4442
4443static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
bd2e244f
JB
4444{
4445 struct drm_device *dev = crtc->base.dev;
4446 struct drm_i915_private *dev_priv = dev->dev_private;
4447 int pipe = crtc->pipe;
a1b2278e
CK
4448 struct intel_crtc_scaler_state *scaler_state =
4449 &crtc->config->scaler_state;
4450
4451 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4452
4453 /* To update pfit, first update scaler state */
86adf9d7 4454 skl_update_scaler_crtc(crtc->config, !enable);
a1b2278e
CK
4455 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4456 skl_detach_scalers(crtc);
4457 if (!enable)
4458 return;
bd2e244f 4459
6e3c9717 4460 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4461 int id;
4462
4463 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4464 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4465 return;
4466 }
4467
4468 id = scaler_state->scaler_id;
4469 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4470 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4471 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4472 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4473
4474 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4475 }
4476}
4477
b074cec8
JB
4478static void ironlake_pfit_enable(struct intel_crtc *crtc)
4479{
4480 struct drm_device *dev = crtc->base.dev;
4481 struct drm_i915_private *dev_priv = dev->dev_private;
4482 int pipe = crtc->pipe;
4483
6e3c9717 4484 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4485 /* Force use of hard-coded filter coefficients
4486 * as some pre-programmed values are broken,
4487 * e.g. x201.
4488 */
4489 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4490 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4491 PF_PIPE_SEL_IVB(pipe));
4492 else
4493 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4494 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4495 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4496 }
4497}
4498
20bc8673 4499void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4500{
cea165c3
VS
4501 struct drm_device *dev = crtc->base.dev;
4502 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4503
6e3c9717 4504 if (!crtc->config->ips_enabled)
d77e4531
PZ
4505 return;
4506
cea165c3
VS
4507 /* We can only enable IPS after we enable a plane and wait for a vblank */
4508 intel_wait_for_vblank(dev, crtc->pipe);
4509
d77e4531 4510 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4511 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4512 mutex_lock(&dev_priv->rps.hw_lock);
4513 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4514 mutex_unlock(&dev_priv->rps.hw_lock);
4515 /* Quoting Art Runyan: "its not safe to expect any particular
4516 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4517 * mailbox." Moreover, the mailbox may return a bogus state,
4518 * so we need to just enable it and continue on.
2a114cc1
BW
4519 */
4520 } else {
4521 I915_WRITE(IPS_CTL, IPS_ENABLE);
4522 /* The bit only becomes 1 in the next vblank, so this wait here
4523 * is essentially intel_wait_for_vblank. If we don't have this
4524 * and don't wait for vblanks until the end of crtc_enable, then
4525 * the HW state readout code will complain that the expected
4526 * IPS_CTL value is not the one we read. */
4527 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4528 DRM_ERROR("Timed out waiting for IPS enable\n");
4529 }
d77e4531
PZ
4530}
4531
20bc8673 4532void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4533{
4534 struct drm_device *dev = crtc->base.dev;
4535 struct drm_i915_private *dev_priv = dev->dev_private;
4536
6e3c9717 4537 if (!crtc->config->ips_enabled)
d77e4531
PZ
4538 return;
4539
4540 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4541 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4542 mutex_lock(&dev_priv->rps.hw_lock);
4543 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4544 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4545 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4546 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4547 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4548 } else {
2a114cc1 4549 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4550 POSTING_READ(IPS_CTL);
4551 }
d77e4531
PZ
4552
4553 /* We need to wait for a vblank before we can disable the plane. */
4554 intel_wait_for_vblank(dev, crtc->pipe);
4555}
4556
4557/** Loads the palette/gamma unit for the CRTC with the prepared values */
4558static void intel_crtc_load_lut(struct drm_crtc *crtc)
4559{
4560 struct drm_device *dev = crtc->dev;
4561 struct drm_i915_private *dev_priv = dev->dev_private;
4562 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4563 enum pipe pipe = intel_crtc->pipe;
4564 int palreg = PALETTE(pipe);
4565 int i;
4566 bool reenable_ips = false;
4567
4568 /* The clocks have to be on to load the palette. */
53d9f4e9 4569 if (!crtc->state->active)
d77e4531
PZ
4570 return;
4571
50360403 4572 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
409ee761 4573 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4574 assert_dsi_pll_enabled(dev_priv);
4575 else
4576 assert_pll_enabled(dev_priv, pipe);
4577 }
4578
4579 /* use legacy palette for Ironlake */
7a1db49a 4580 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4581 palreg = LGC_PALETTE(pipe);
4582
4583 /* Workaround : Do not read or write the pipe palette/gamma data while
4584 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4585 */
6e3c9717 4586 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4587 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4588 GAMMA_MODE_MODE_SPLIT)) {
4589 hsw_disable_ips(intel_crtc);
4590 reenable_ips = true;
4591 }
4592
4593 for (i = 0; i < 256; i++) {
4594 I915_WRITE(palreg + 4 * i,
4595 (intel_crtc->lut_r[i] << 16) |
4596 (intel_crtc->lut_g[i] << 8) |
4597 intel_crtc->lut_b[i]);
4598 }
4599
4600 if (reenable_ips)
4601 hsw_enable_ips(intel_crtc);
4602}
4603
7cac945f 4604static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4605{
7cac945f 4606 if (intel_crtc->overlay) {
d3eedb1a
VS
4607 struct drm_device *dev = intel_crtc->base.dev;
4608 struct drm_i915_private *dev_priv = dev->dev_private;
4609
4610 mutex_lock(&dev->struct_mutex);
4611 dev_priv->mm.interruptible = false;
4612 (void) intel_overlay_switch_off(intel_crtc->overlay);
4613 dev_priv->mm.interruptible = true;
4614 mutex_unlock(&dev->struct_mutex);
4615 }
4616
4617 /* Let userspace switch the overlay on again. In most cases userspace
4618 * has to recompute where to put it anyway.
4619 */
4620}
4621
87d4300a
ML
4622/**
4623 * intel_post_enable_primary - Perform operations after enabling primary plane
4624 * @crtc: the CRTC whose primary plane was just enabled
4625 *
4626 * Performs potentially sleeping operations that must be done after the primary
4627 * plane is enabled, such as updating FBC and IPS. Note that this may be
4628 * called due to an explicit primary plane update, or due to an implicit
4629 * re-enable that is caused when a sprite plane is updated to no longer
4630 * completely hide the primary plane.
4631 */
4632static void
4633intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4634{
4635 struct drm_device *dev = crtc->dev;
87d4300a 4636 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4637 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4638 int pipe = intel_crtc->pipe;
a5c4d7bc 4639
87d4300a
ML
4640 /*
4641 * BDW signals flip done immediately if the plane
4642 * is disabled, even if the plane enable is already
4643 * armed to occur at the next vblank :(
4644 */
4645 if (IS_BROADWELL(dev))
4646 intel_wait_for_vblank(dev, pipe);
a5c4d7bc 4647
87d4300a
ML
4648 /*
4649 * FIXME IPS should be fine as long as one plane is
4650 * enabled, but in practice it seems to have problems
4651 * when going from primary only to sprite only and vice
4652 * versa.
4653 */
a5c4d7bc
VS
4654 hsw_enable_ips(intel_crtc);
4655
f99d7069 4656 /*
87d4300a
ML
4657 * Gen2 reports pipe underruns whenever all planes are disabled.
4658 * So don't enable underrun reporting before at least some planes
4659 * are enabled.
4660 * FIXME: Need to fix the logic to work when we turn off all planes
4661 * but leave the pipe running.
f99d7069 4662 */
87d4300a
ML
4663 if (IS_GEN2(dev))
4664 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4665
4666 /* Underruns don't raise interrupts, so check manually. */
4667 if (HAS_GMCH_DISPLAY(dev))
4668 i9xx_check_fifo_underruns(dev_priv);
a5c4d7bc
VS
4669}
4670
87d4300a
ML
4671/**
4672 * intel_pre_disable_primary - Perform operations before disabling primary plane
4673 * @crtc: the CRTC whose primary plane is to be disabled
4674 *
4675 * Performs potentially sleeping operations that must be done before the
4676 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4677 * be called due to an explicit primary plane update, or due to an implicit
4678 * disable that is caused when a sprite plane completely hides the primary
4679 * plane.
4680 */
4681static void
4682intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4683{
4684 struct drm_device *dev = crtc->dev;
4685 struct drm_i915_private *dev_priv = dev->dev_private;
4686 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4687 int pipe = intel_crtc->pipe;
a5c4d7bc 4688
87d4300a
ML
4689 /*
4690 * Gen2 reports pipe underruns whenever all planes are disabled.
4691 * So diasble underrun reporting before all the planes get disabled.
4692 * FIXME: Need to fix the logic to work when we turn off all planes
4693 * but leave the pipe running.
4694 */
4695 if (IS_GEN2(dev))
4696 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4697
87d4300a
ML
4698 /*
4699 * Vblank time updates from the shadow to live plane control register
4700 * are blocked if the memory self-refresh mode is active at that
4701 * moment. So to make sure the plane gets truly disabled, disable
4702 * first the self-refresh mode. The self-refresh enable bit in turn
4703 * will be checked/applied by the HW only at the next frame start
4704 * event which is after the vblank start event, so we need to have a
4705 * wait-for-vblank between disabling the plane and the pipe.
4706 */
4707 if (HAS_GMCH_DISPLAY(dev))
4708 intel_set_memory_cxsr(dev_priv, false);
4709
87d4300a
ML
4710 /*
4711 * FIXME IPS should be fine as long as one plane is
4712 * enabled, but in practice it seems to have problems
4713 * when going from primary only to sprite only and vice
4714 * versa.
4715 */
a5c4d7bc 4716 hsw_disable_ips(intel_crtc);
87d4300a
ML
4717}
4718
ac21b225
ML
4719static void intel_post_plane_update(struct intel_crtc *crtc)
4720{
4721 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4722 struct drm_device *dev = crtc->base.dev;
4723 struct drm_plane *plane;
4724
4725 if (atomic->wait_vblank)
4726 intel_wait_for_vblank(dev, crtc->pipe);
4727
4728 intel_frontbuffer_flip(dev, atomic->fb_bits);
4729
4730 if (atomic->update_fbc) {
4731 mutex_lock(&dev->struct_mutex);
4732 intel_fbc_update(dev);
4733 mutex_unlock(&dev->struct_mutex);
4734 }
4735
4736 if (atomic->post_enable_primary)
4737 intel_post_enable_primary(&crtc->base);
4738
4739 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4740 intel_update_sprite_watermarks(plane, &crtc->base,
4741 0, 0, 0, false, false);
4742
4743 memset(atomic, 0, sizeof(*atomic));
4744}
4745
4746static void intel_pre_plane_update(struct intel_crtc *crtc)
4747{
4748 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4749 struct drm_i915_private *dev_priv = dev->dev_private;
ac21b225
ML
4750 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4751 struct drm_plane *p;
4752
4753 /* Track fb's for any planes being disabled */
4754
4755 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4756 struct intel_plane *plane = to_intel_plane(p);
4757 unsigned fb_bits = 0;
4758
4759 switch (p->type) {
4760 case DRM_PLANE_TYPE_PRIMARY:
4761 fb_bits = INTEL_FRONTBUFFER_PRIMARY(plane->pipe);
4762 break;
4763 case DRM_PLANE_TYPE_CURSOR:
4764 fb_bits = INTEL_FRONTBUFFER_CURSOR(plane->pipe);
4765 break;
4766 case DRM_PLANE_TYPE_OVERLAY:
4767 fb_bits = INTEL_FRONTBUFFER_SPRITE(plane->pipe);
4768 break;
4769 }
4770
4771 mutex_lock(&dev->struct_mutex);
4772 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL, fb_bits);
4773 mutex_unlock(&dev->struct_mutex);
4774 }
4775
4776 if (atomic->wait_for_flips)
4777 intel_crtc_wait_for_pending_flips(&crtc->base);
4778
eddfcbcd
ML
4779 if (atomic->disable_fbc &&
4780 dev_priv->fbc.crtc == crtc) {
4781 mutex_lock(&dev->struct_mutex);
4782 if (dev_priv->fbc.crtc == crtc)
4783 intel_fbc_disable(dev);
4784 mutex_unlock(&dev->struct_mutex);
4785 }
ac21b225
ML
4786
4787 if (atomic->pre_disable_primary)
4788 intel_pre_disable_primary(&crtc->base);
4789}
4790
d032ffa0 4791static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4792{
4793 struct drm_device *dev = crtc->dev;
4794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4795 struct drm_plane *p;
87d4300a
ML
4796 int pipe = intel_crtc->pipe;
4797
7cac945f 4798 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4799
d032ffa0
ML
4800 drm_for_each_plane_mask(p, dev, plane_mask)
4801 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4802
f99d7069
DV
4803 /*
4804 * FIXME: Once we grow proper nuclear flip support out of this we need
4805 * to compute the mask of flip planes precisely. For the time being
4806 * consider this a flip to a NULL plane.
4807 */
4808 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4809}
4810
f67a559d
JB
4811static void ironlake_crtc_enable(struct drm_crtc *crtc)
4812{
4813 struct drm_device *dev = crtc->dev;
4814 struct drm_i915_private *dev_priv = dev->dev_private;
4815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4816 struct intel_encoder *encoder;
f67a559d 4817 int pipe = intel_crtc->pipe;
f67a559d 4818
53d9f4e9 4819 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4820 return;
4821
6e3c9717 4822 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4823 intel_prepare_shared_dpll(intel_crtc);
4824
6e3c9717 4825 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4826 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4827
4828 intel_set_pipe_timings(intel_crtc);
4829
6e3c9717 4830 if (intel_crtc->config->has_pch_encoder) {
29407aab 4831 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4832 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4833 }
4834
4835 ironlake_set_pipeconf(crtc);
4836
f67a559d 4837 intel_crtc->active = true;
8664281b 4838
a72e4c9f
DV
4839 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4840 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4841
f6736a1a 4842 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4843 if (encoder->pre_enable)
4844 encoder->pre_enable(encoder);
f67a559d 4845
6e3c9717 4846 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4847 /* Note: FDI PLL enabling _must_ be done before we enable the
4848 * cpu pipes, hence this is separate from all the other fdi/pch
4849 * enabling. */
88cefb6c 4850 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4851 } else {
4852 assert_fdi_tx_disabled(dev_priv, pipe);
4853 assert_fdi_rx_disabled(dev_priv, pipe);
4854 }
f67a559d 4855
b074cec8 4856 ironlake_pfit_enable(intel_crtc);
f67a559d 4857
9c54c0dd
JB
4858 /*
4859 * On ILK+ LUT must be loaded before the pipe is running but with
4860 * clocks enabled
4861 */
4862 intel_crtc_load_lut(crtc);
4863
f37fcc2a 4864 intel_update_watermarks(crtc);
e1fdc473 4865 intel_enable_pipe(intel_crtc);
f67a559d 4866
6e3c9717 4867 if (intel_crtc->config->has_pch_encoder)
f67a559d 4868 ironlake_pch_enable(crtc);
c98e9dcf 4869
f9b61ff6
DV
4870 assert_vblank_disabled(crtc);
4871 drm_crtc_vblank_on(crtc);
4872
fa5c73b1
DV
4873 for_each_encoder_on_crtc(dev, crtc, encoder)
4874 encoder->enable(encoder);
61b77ddd
DV
4875
4876 if (HAS_PCH_CPT(dev))
a1520318 4877 cpt_verify_modeset(dev, intel_crtc->pipe);
6be4a607
JB
4878}
4879
42db64ef
PZ
4880/* IPS only exists on ULT machines and is tied to pipe A. */
4881static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4882{
f5adf94e 4883 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4884}
4885
4f771f10
PZ
4886static void haswell_crtc_enable(struct drm_crtc *crtc)
4887{
4888 struct drm_device *dev = crtc->dev;
4889 struct drm_i915_private *dev_priv = dev->dev_private;
4890 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4891 struct intel_encoder *encoder;
99d736a2
ML
4892 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4893 struct intel_crtc_state *pipe_config =
4894 to_intel_crtc_state(crtc->state);
4f771f10 4895
53d9f4e9 4896 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4897 return;
4898
df8ad70c
DV
4899 if (intel_crtc_to_shared_dpll(intel_crtc))
4900 intel_enable_shared_dpll(intel_crtc);
4901
6e3c9717 4902 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4903 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
4904
4905 intel_set_pipe_timings(intel_crtc);
4906
6e3c9717
ACO
4907 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4908 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4909 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4910 }
4911
6e3c9717 4912 if (intel_crtc->config->has_pch_encoder) {
229fca97 4913 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4914 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4915 }
4916
4917 haswell_set_pipeconf(crtc);
4918
4919 intel_set_pipe_csc(crtc);
4920
4f771f10 4921 intel_crtc->active = true;
8664281b 4922
a72e4c9f 4923 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
4924 for_each_encoder_on_crtc(dev, crtc, encoder)
4925 if (encoder->pre_enable)
4926 encoder->pre_enable(encoder);
4927
6e3c9717 4928 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
4929 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4930 true);
4fe9467d
ID
4931 dev_priv->display.fdi_link_train(crtc);
4932 }
4933
1f544388 4934 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4935
ff6d9f55 4936 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 4937 skylake_pfit_update(intel_crtc, 1);
ff6d9f55 4938 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 4939 ironlake_pfit_enable(intel_crtc);
ff6d9f55
JB
4940 else
4941 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10
PZ
4942
4943 /*
4944 * On ILK+ LUT must be loaded before the pipe is running but with
4945 * clocks enabled
4946 */
4947 intel_crtc_load_lut(crtc);
4948
1f544388 4949 intel_ddi_set_pipe_settings(crtc);
8228c251 4950 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4951
f37fcc2a 4952 intel_update_watermarks(crtc);
e1fdc473 4953 intel_enable_pipe(intel_crtc);
42db64ef 4954
6e3c9717 4955 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4956 lpt_pch_enable(crtc);
4f771f10 4957
6e3c9717 4958 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4959 intel_ddi_set_vc_payload_alloc(crtc, true);
4960
f9b61ff6
DV
4961 assert_vblank_disabled(crtc);
4962 drm_crtc_vblank_on(crtc);
4963
8807e55b 4964 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4965 encoder->enable(encoder);
8807e55b
JN
4966 intel_opregion_notify_encoder(encoder, true);
4967 }
4f771f10 4968
e4916946
PZ
4969 /* If we change the relative order between pipe/planes enabling, we need
4970 * to change the workaround. */
99d736a2
ML
4971 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4972 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4973 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4974 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4975 }
4f771f10
PZ
4976}
4977
3f8dce3a
DV
4978static void ironlake_pfit_disable(struct intel_crtc *crtc)
4979{
4980 struct drm_device *dev = crtc->base.dev;
4981 struct drm_i915_private *dev_priv = dev->dev_private;
4982 int pipe = crtc->pipe;
4983
4984 /* To avoid upsetting the power well on haswell only disable the pfit if
4985 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 4986 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4987 I915_WRITE(PF_CTL(pipe), 0);
4988 I915_WRITE(PF_WIN_POS(pipe), 0);
4989 I915_WRITE(PF_WIN_SZ(pipe), 0);
4990 }
4991}
4992
6be4a607
JB
4993static void ironlake_crtc_disable(struct drm_crtc *crtc)
4994{
4995 struct drm_device *dev = crtc->dev;
4996 struct drm_i915_private *dev_priv = dev->dev_private;
4997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4998 struct intel_encoder *encoder;
6be4a607 4999 int pipe = intel_crtc->pipe;
5eddb70b 5000 u32 reg, temp;
b52eb4dc 5001
ea9d758d
DV
5002 for_each_encoder_on_crtc(dev, crtc, encoder)
5003 encoder->disable(encoder);
5004
f9b61ff6
DV
5005 drm_crtc_vblank_off(crtc);
5006 assert_vblank_disabled(crtc);
5007
6e3c9717 5008 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 5009 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 5010
575f7ab7 5011 intel_disable_pipe(intel_crtc);
32f9d658 5012
3f8dce3a 5013 ironlake_pfit_disable(intel_crtc);
2c07245f 5014
5a74f70a
VS
5015 if (intel_crtc->config->has_pch_encoder)
5016 ironlake_fdi_disable(crtc);
5017
bf49ec8c
DV
5018 for_each_encoder_on_crtc(dev, crtc, encoder)
5019 if (encoder->post_disable)
5020 encoder->post_disable(encoder);
2c07245f 5021
6e3c9717 5022 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5023 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5024
d925c59a
DV
5025 if (HAS_PCH_CPT(dev)) {
5026 /* disable TRANS_DP_CTL */
5027 reg = TRANS_DP_CTL(pipe);
5028 temp = I915_READ(reg);
5029 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5030 TRANS_DP_PORT_SEL_MASK);
5031 temp |= TRANS_DP_PORT_SEL_NONE;
5032 I915_WRITE(reg, temp);
5033
5034 /* disable DPLL_SEL */
5035 temp = I915_READ(PCH_DPLL_SEL);
11887397 5036 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5037 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5038 }
e3421a18 5039
d925c59a
DV
5040 ironlake_fdi_pll_disable(intel_crtc);
5041 }
6be4a607 5042}
1b3c7a47 5043
4f771f10 5044static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5045{
4f771f10
PZ
5046 struct drm_device *dev = crtc->dev;
5047 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5048 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5049 struct intel_encoder *encoder;
6e3c9717 5050 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5051
8807e55b
JN
5052 for_each_encoder_on_crtc(dev, crtc, encoder) {
5053 intel_opregion_notify_encoder(encoder, false);
4f771f10 5054 encoder->disable(encoder);
8807e55b 5055 }
4f771f10 5056
f9b61ff6
DV
5057 drm_crtc_vblank_off(crtc);
5058 assert_vblank_disabled(crtc);
5059
6e3c9717 5060 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
5061 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5062 false);
575f7ab7 5063 intel_disable_pipe(intel_crtc);
4f771f10 5064
6e3c9717 5065 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5066 intel_ddi_set_vc_payload_alloc(crtc, false);
5067
ad80a810 5068 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5069
ff6d9f55 5070 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5071 skylake_pfit_update(intel_crtc, 0);
ff6d9f55 5072 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5073 ironlake_pfit_disable(intel_crtc);
ff6d9f55
JB
5074 else
5075 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10 5076
1f544388 5077 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5078
6e3c9717 5079 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 5080 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 5081 intel_ddi_fdi_disable(crtc);
83616634 5082 }
4f771f10 5083
97b040aa
ID
5084 for_each_encoder_on_crtc(dev, crtc, encoder)
5085 if (encoder->post_disable)
5086 encoder->post_disable(encoder);
4f771f10
PZ
5087}
5088
2dd24552
JB
5089static void i9xx_pfit_enable(struct intel_crtc *crtc)
5090{
5091 struct drm_device *dev = crtc->base.dev;
5092 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5093 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5094
681a8504 5095 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5096 return;
5097
2dd24552 5098 /*
c0b03411
DV
5099 * The panel fitter should only be adjusted whilst the pipe is disabled,
5100 * according to register description and PRM.
2dd24552 5101 */
c0b03411
DV
5102 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5103 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5104
b074cec8
JB
5105 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5106 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5107
5108 /* Border color in case we don't scale up to the full screen. Black by
5109 * default, change to something else for debugging. */
5110 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5111}
5112
d05410f9
DA
5113static enum intel_display_power_domain port_to_power_domain(enum port port)
5114{
5115 switch (port) {
5116 case PORT_A:
5117 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5118 case PORT_B:
5119 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5120 case PORT_C:
5121 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5122 case PORT_D:
5123 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5124 default:
5125 WARN_ON_ONCE(1);
5126 return POWER_DOMAIN_PORT_OTHER;
5127 }
5128}
5129
77d22dca
ID
5130#define for_each_power_domain(domain, mask) \
5131 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5132 if ((1 << (domain)) & (mask))
5133
319be8ae
ID
5134enum intel_display_power_domain
5135intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5136{
5137 struct drm_device *dev = intel_encoder->base.dev;
5138 struct intel_digital_port *intel_dig_port;
5139
5140 switch (intel_encoder->type) {
5141 case INTEL_OUTPUT_UNKNOWN:
5142 /* Only DDI platforms should ever use this output type */
5143 WARN_ON_ONCE(!HAS_DDI(dev));
5144 case INTEL_OUTPUT_DISPLAYPORT:
5145 case INTEL_OUTPUT_HDMI:
5146 case INTEL_OUTPUT_EDP:
5147 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5148 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5149 case INTEL_OUTPUT_DP_MST:
5150 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5151 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5152 case INTEL_OUTPUT_ANALOG:
5153 return POWER_DOMAIN_PORT_CRT;
5154 case INTEL_OUTPUT_DSI:
5155 return POWER_DOMAIN_PORT_DSI;
5156 default:
5157 return POWER_DOMAIN_PORT_OTHER;
5158 }
5159}
5160
5161static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5162{
319be8ae
ID
5163 struct drm_device *dev = crtc->dev;
5164 struct intel_encoder *intel_encoder;
5165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5166 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
5167 unsigned long mask;
5168 enum transcoder transcoder;
5169
5170 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5171
5172 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5173 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
5174 if (intel_crtc->config->pch_pfit.enabled ||
5175 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
5176 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5177
319be8ae
ID
5178 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5179 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5180
77d22dca
ID
5181 return mask;
5182}
5183
679dacd4 5184static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
77d22dca 5185{
679dacd4 5186 struct drm_device *dev = state->dev;
77d22dca
ID
5187 struct drm_i915_private *dev_priv = dev->dev_private;
5188 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5189 struct intel_crtc *crtc;
5190
5191 /*
5192 * First get all needed power domains, then put all unneeded, to avoid
5193 * any unnecessary toggling of the power wells.
5194 */
d3fcc808 5195 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5196 enum intel_display_power_domain domain;
5197
83d65738 5198 if (!crtc->base.state->enable)
77d22dca
ID
5199 continue;
5200
319be8ae 5201 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
5202
5203 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5204 intel_display_power_get(dev_priv, domain);
5205 }
5206
27c329ed
ML
5207 if (dev_priv->display.modeset_commit_cdclk) {
5208 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5209
5210 if (cdclk != dev_priv->cdclk_freq &&
5211 !WARN_ON(!state->allow_modeset))
5212 dev_priv->display.modeset_commit_cdclk(state);
5213 }
50f6e502 5214
d3fcc808 5215 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5216 enum intel_display_power_domain domain;
5217
5218 for_each_power_domain(domain, crtc->enabled_power_domains)
5219 intel_display_power_put(dev_priv, domain);
5220
5221 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5222 }
5223
5224 intel_display_set_init_power(dev_priv, false);
5225}
5226
560a7ae4
DL
5227static void intel_update_max_cdclk(struct drm_device *dev)
5228{
5229 struct drm_i915_private *dev_priv = dev->dev_private;
5230
5231 if (IS_SKYLAKE(dev)) {
5232 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5233
5234 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5235 dev_priv->max_cdclk_freq = 675000;
5236 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5237 dev_priv->max_cdclk_freq = 540000;
5238 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5239 dev_priv->max_cdclk_freq = 450000;
5240 else
5241 dev_priv->max_cdclk_freq = 337500;
5242 } else if (IS_BROADWELL(dev)) {
5243 /*
5244 * FIXME with extra cooling we can allow
5245 * 540 MHz for ULX and 675 Mhz for ULT.
5246 * How can we know if extra cooling is
5247 * available? PCI ID, VTB, something else?
5248 */
5249 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5250 dev_priv->max_cdclk_freq = 450000;
5251 else if (IS_BDW_ULX(dev))
5252 dev_priv->max_cdclk_freq = 450000;
5253 else if (IS_BDW_ULT(dev))
5254 dev_priv->max_cdclk_freq = 540000;
5255 else
5256 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5257 } else if (IS_CHERRYVIEW(dev)) {
5258 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5259 } else if (IS_VALLEYVIEW(dev)) {
5260 dev_priv->max_cdclk_freq = 400000;
5261 } else {
5262 /* otherwise assume cdclk is fixed */
5263 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5264 }
5265
5266 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5267 dev_priv->max_cdclk_freq);
5268}
5269
5270static void intel_update_cdclk(struct drm_device *dev)
5271{
5272 struct drm_i915_private *dev_priv = dev->dev_private;
5273
5274 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5275 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5276 dev_priv->cdclk_freq);
5277
5278 /*
5279 * Program the gmbus_freq based on the cdclk frequency.
5280 * BSpec erroneously claims we should aim for 4MHz, but
5281 * in fact 1MHz is the correct frequency.
5282 */
5283 if (IS_VALLEYVIEW(dev)) {
5284 /*
5285 * Program the gmbus_freq based on the cdclk frequency.
5286 * BSpec erroneously claims we should aim for 4MHz, but
5287 * in fact 1MHz is the correct frequency.
5288 */
5289 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5290 }
5291
5292 if (dev_priv->max_cdclk_freq == 0)
5293 intel_update_max_cdclk(dev);
5294}
5295
70d0c574 5296static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5297{
5298 struct drm_i915_private *dev_priv = dev->dev_private;
5299 uint32_t divider;
5300 uint32_t ratio;
5301 uint32_t current_freq;
5302 int ret;
5303
5304 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5305 switch (frequency) {
5306 case 144000:
5307 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5308 ratio = BXT_DE_PLL_RATIO(60);
5309 break;
5310 case 288000:
5311 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5312 ratio = BXT_DE_PLL_RATIO(60);
5313 break;
5314 case 384000:
5315 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5316 ratio = BXT_DE_PLL_RATIO(60);
5317 break;
5318 case 576000:
5319 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5320 ratio = BXT_DE_PLL_RATIO(60);
5321 break;
5322 case 624000:
5323 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5324 ratio = BXT_DE_PLL_RATIO(65);
5325 break;
5326 case 19200:
5327 /*
5328 * Bypass frequency with DE PLL disabled. Init ratio, divider
5329 * to suppress GCC warning.
5330 */
5331 ratio = 0;
5332 divider = 0;
5333 break;
5334 default:
5335 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5336
5337 return;
5338 }
5339
5340 mutex_lock(&dev_priv->rps.hw_lock);
5341 /* Inform power controller of upcoming frequency change */
5342 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5343 0x80000000);
5344 mutex_unlock(&dev_priv->rps.hw_lock);
5345
5346 if (ret) {
5347 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5348 ret, frequency);
5349 return;
5350 }
5351
5352 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5353 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5354 current_freq = current_freq * 500 + 1000;
5355
5356 /*
5357 * DE PLL has to be disabled when
5358 * - setting to 19.2MHz (bypass, PLL isn't used)
5359 * - before setting to 624MHz (PLL needs toggling)
5360 * - before setting to any frequency from 624MHz (PLL needs toggling)
5361 */
5362 if (frequency == 19200 || frequency == 624000 ||
5363 current_freq == 624000) {
5364 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5365 /* Timeout 200us */
5366 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5367 1))
5368 DRM_ERROR("timout waiting for DE PLL unlock\n");
5369 }
5370
5371 if (frequency != 19200) {
5372 uint32_t val;
5373
5374 val = I915_READ(BXT_DE_PLL_CTL);
5375 val &= ~BXT_DE_PLL_RATIO_MASK;
5376 val |= ratio;
5377 I915_WRITE(BXT_DE_PLL_CTL, val);
5378
5379 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5380 /* Timeout 200us */
5381 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5382 DRM_ERROR("timeout waiting for DE PLL lock\n");
5383
5384 val = I915_READ(CDCLK_CTL);
5385 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5386 val |= divider;
5387 /*
5388 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5389 * enable otherwise.
5390 */
5391 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5392 if (frequency >= 500000)
5393 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5394
5395 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5396 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5397 val |= (frequency - 1000) / 500;
5398 I915_WRITE(CDCLK_CTL, val);
5399 }
5400
5401 mutex_lock(&dev_priv->rps.hw_lock);
5402 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5403 DIV_ROUND_UP(frequency, 25000));
5404 mutex_unlock(&dev_priv->rps.hw_lock);
5405
5406 if (ret) {
5407 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5408 ret, frequency);
5409 return;
5410 }
5411
a47871bd 5412 intel_update_cdclk(dev);
f8437dd1
VK
5413}
5414
5415void broxton_init_cdclk(struct drm_device *dev)
5416{
5417 struct drm_i915_private *dev_priv = dev->dev_private;
5418 uint32_t val;
5419
5420 /*
5421 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5422 * or else the reset will hang because there is no PCH to respond.
5423 * Move the handshake programming to initialization sequence.
5424 * Previously was left up to BIOS.
5425 */
5426 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5427 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5428 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5429
5430 /* Enable PG1 for cdclk */
5431 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5432
5433 /* check if cd clock is enabled */
5434 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5435 DRM_DEBUG_KMS("Display already initialized\n");
5436 return;
5437 }
5438
5439 /*
5440 * FIXME:
5441 * - The initial CDCLK needs to be read from VBT.
5442 * Need to make this change after VBT has changes for BXT.
5443 * - check if setting the max (or any) cdclk freq is really necessary
5444 * here, it belongs to modeset time
5445 */
5446 broxton_set_cdclk(dev, 624000);
5447
5448 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5449 POSTING_READ(DBUF_CTL);
5450
f8437dd1
VK
5451 udelay(10);
5452
5453 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5454 DRM_ERROR("DBuf power enable timeout!\n");
5455}
5456
5457void broxton_uninit_cdclk(struct drm_device *dev)
5458{
5459 struct drm_i915_private *dev_priv = dev->dev_private;
5460
5461 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5462 POSTING_READ(DBUF_CTL);
5463
f8437dd1
VK
5464 udelay(10);
5465
5466 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5467 DRM_ERROR("DBuf power disable timeout!\n");
5468
5469 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5470 broxton_set_cdclk(dev, 19200);
5471
5472 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5473}
5474
5d96d8af
DL
5475static const struct skl_cdclk_entry {
5476 unsigned int freq;
5477 unsigned int vco;
5478} skl_cdclk_frequencies[] = {
5479 { .freq = 308570, .vco = 8640 },
5480 { .freq = 337500, .vco = 8100 },
5481 { .freq = 432000, .vco = 8640 },
5482 { .freq = 450000, .vco = 8100 },
5483 { .freq = 540000, .vco = 8100 },
5484 { .freq = 617140, .vco = 8640 },
5485 { .freq = 675000, .vco = 8100 },
5486};
5487
5488static unsigned int skl_cdclk_decimal(unsigned int freq)
5489{
5490 return (freq - 1000) / 500;
5491}
5492
5493static unsigned int skl_cdclk_get_vco(unsigned int freq)
5494{
5495 unsigned int i;
5496
5497 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5498 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5499
5500 if (e->freq == freq)
5501 return e->vco;
5502 }
5503
5504 return 8100;
5505}
5506
5507static void
5508skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5509{
5510 unsigned int min_freq;
5511 u32 val;
5512
5513 /* select the minimum CDCLK before enabling DPLL 0 */
5514 val = I915_READ(CDCLK_CTL);
5515 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5516 val |= CDCLK_FREQ_337_308;
5517
5518 if (required_vco == 8640)
5519 min_freq = 308570;
5520 else
5521 min_freq = 337500;
5522
5523 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5524
5525 I915_WRITE(CDCLK_CTL, val);
5526 POSTING_READ(CDCLK_CTL);
5527
5528 /*
5529 * We always enable DPLL0 with the lowest link rate possible, but still
5530 * taking into account the VCO required to operate the eDP panel at the
5531 * desired frequency. The usual DP link rates operate with a VCO of
5532 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5533 * The modeset code is responsible for the selection of the exact link
5534 * rate later on, with the constraint of choosing a frequency that
5535 * works with required_vco.
5536 */
5537 val = I915_READ(DPLL_CTRL1);
5538
5539 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5540 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5541 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5542 if (required_vco == 8640)
5543 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5544 SKL_DPLL0);
5545 else
5546 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5547 SKL_DPLL0);
5548
5549 I915_WRITE(DPLL_CTRL1, val);
5550 POSTING_READ(DPLL_CTRL1);
5551
5552 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5553
5554 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5555 DRM_ERROR("DPLL0 not locked\n");
5556}
5557
5558static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5559{
5560 int ret;
5561 u32 val;
5562
5563 /* inform PCU we want to change CDCLK */
5564 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5565 mutex_lock(&dev_priv->rps.hw_lock);
5566 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5567 mutex_unlock(&dev_priv->rps.hw_lock);
5568
5569 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5570}
5571
5572static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5573{
5574 unsigned int i;
5575
5576 for (i = 0; i < 15; i++) {
5577 if (skl_cdclk_pcu_ready(dev_priv))
5578 return true;
5579 udelay(10);
5580 }
5581
5582 return false;
5583}
5584
5585static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5586{
560a7ae4 5587 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5588 u32 freq_select, pcu_ack;
5589
5590 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5591
5592 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5593 DRM_ERROR("failed to inform PCU about cdclk change\n");
5594 return;
5595 }
5596
5597 /* set CDCLK_CTL */
5598 switch(freq) {
5599 case 450000:
5600 case 432000:
5601 freq_select = CDCLK_FREQ_450_432;
5602 pcu_ack = 1;
5603 break;
5604 case 540000:
5605 freq_select = CDCLK_FREQ_540;
5606 pcu_ack = 2;
5607 break;
5608 case 308570:
5609 case 337500:
5610 default:
5611 freq_select = CDCLK_FREQ_337_308;
5612 pcu_ack = 0;
5613 break;
5614 case 617140:
5615 case 675000:
5616 freq_select = CDCLK_FREQ_675_617;
5617 pcu_ack = 3;
5618 break;
5619 }
5620
5621 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5622 POSTING_READ(CDCLK_CTL);
5623
5624 /* inform PCU of the change */
5625 mutex_lock(&dev_priv->rps.hw_lock);
5626 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5627 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5628
5629 intel_update_cdclk(dev);
5d96d8af
DL
5630}
5631
5632void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5633{
5634 /* disable DBUF power */
5635 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5636 POSTING_READ(DBUF_CTL);
5637
5638 udelay(10);
5639
5640 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5641 DRM_ERROR("DBuf power disable timeout\n");
5642
5643 /* disable DPLL0 */
5644 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5645 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5646 DRM_ERROR("Couldn't disable DPLL0\n");
5647
5648 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5649}
5650
5651void skl_init_cdclk(struct drm_i915_private *dev_priv)
5652{
5653 u32 val;
5654 unsigned int required_vco;
5655
5656 /* enable PCH reset handshake */
5657 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5658 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5659
5660 /* enable PG1 and Misc I/O */
5661 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5662
5663 /* DPLL0 already enabed !? */
5664 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5665 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5666 return;
5667 }
5668
5669 /* enable DPLL0 */
5670 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5671 skl_dpll0_enable(dev_priv, required_vco);
5672
5673 /* set CDCLK to the frequency the BIOS chose */
5674 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5675
5676 /* enable DBUF power */
5677 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5678 POSTING_READ(DBUF_CTL);
5679
5680 udelay(10);
5681
5682 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5683 DRM_ERROR("DBuf power enable timeout\n");
5684}
5685
dfcab17e 5686/* returns HPLL frequency in kHz */
f8bf63fd 5687static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 5688{
586f49dc 5689 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 5690
586f49dc 5691 /* Obtain SKU information */
a580516d 5692 mutex_lock(&dev_priv->sb_lock);
586f49dc
JB
5693 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5694 CCK_FUSE_HPLL_FREQ_MASK;
a580516d 5695 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5696
dfcab17e 5697 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
5698}
5699
5700/* Adjust CDclk dividers to allow high res or save power if possible */
5701static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5702{
5703 struct drm_i915_private *dev_priv = dev->dev_private;
5704 u32 val, cmd;
5705
164dfd28
VK
5706 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5707 != dev_priv->cdclk_freq);
d60c4473 5708
dfcab17e 5709 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5710 cmd = 2;
dfcab17e 5711 else if (cdclk == 266667)
30a970c6
JB
5712 cmd = 1;
5713 else
5714 cmd = 0;
5715
5716 mutex_lock(&dev_priv->rps.hw_lock);
5717 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5718 val &= ~DSPFREQGUAR_MASK;
5719 val |= (cmd << DSPFREQGUAR_SHIFT);
5720 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5721 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5722 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5723 50)) {
5724 DRM_ERROR("timed out waiting for CDclk change\n");
5725 }
5726 mutex_unlock(&dev_priv->rps.hw_lock);
5727
54433e91
VS
5728 mutex_lock(&dev_priv->sb_lock);
5729
dfcab17e 5730 if (cdclk == 400000) {
6bcda4f0 5731 u32 divider;
30a970c6 5732
6bcda4f0 5733 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5734
30a970c6
JB
5735 /* adjust cdclk divider */
5736 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 5737 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
5738 val |= divider;
5739 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5740
5741 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5742 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5743 50))
5744 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5745 }
5746
30a970c6
JB
5747 /* adjust self-refresh exit latency value */
5748 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5749 val &= ~0x7f;
5750
5751 /*
5752 * For high bandwidth configs, we set a higher latency in the bunit
5753 * so that the core display fetch happens in time to avoid underruns.
5754 */
dfcab17e 5755 if (cdclk == 400000)
30a970c6
JB
5756 val |= 4500 / 250; /* 4.5 usec */
5757 else
5758 val |= 3000 / 250; /* 3.0 usec */
5759 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5760
a580516d 5761 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5762
b6283055 5763 intel_update_cdclk(dev);
30a970c6
JB
5764}
5765
383c5a6a
VS
5766static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5767{
5768 struct drm_i915_private *dev_priv = dev->dev_private;
5769 u32 val, cmd;
5770
164dfd28
VK
5771 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5772 != dev_priv->cdclk_freq);
383c5a6a
VS
5773
5774 switch (cdclk) {
383c5a6a
VS
5775 case 333333:
5776 case 320000:
383c5a6a 5777 case 266667:
383c5a6a 5778 case 200000:
383c5a6a
VS
5779 break;
5780 default:
5f77eeb0 5781 MISSING_CASE(cdclk);
383c5a6a
VS
5782 return;
5783 }
5784
9d0d3fda
VS
5785 /*
5786 * Specs are full of misinformation, but testing on actual
5787 * hardware has shown that we just need to write the desired
5788 * CCK divider into the Punit register.
5789 */
5790 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5791
383c5a6a
VS
5792 mutex_lock(&dev_priv->rps.hw_lock);
5793 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5794 val &= ~DSPFREQGUAR_MASK_CHV;
5795 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5796 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5797 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5798 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5799 50)) {
5800 DRM_ERROR("timed out waiting for CDclk change\n");
5801 }
5802 mutex_unlock(&dev_priv->rps.hw_lock);
5803
b6283055 5804 intel_update_cdclk(dev);
383c5a6a
VS
5805}
5806
30a970c6
JB
5807static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5808 int max_pixclk)
5809{
6bcda4f0 5810 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5811 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5812
30a970c6
JB
5813 /*
5814 * Really only a few cases to deal with, as only 4 CDclks are supported:
5815 * 200MHz
5816 * 267MHz
29dc7ef3 5817 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5818 * 400MHz (VLV only)
5819 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5820 * of the lower bin and adjust if needed.
e37c67a1
VS
5821 *
5822 * We seem to get an unstable or solid color picture at 200MHz.
5823 * Not sure what's wrong. For now use 200MHz only when all pipes
5824 * are off.
30a970c6 5825 */
6cca3195
VS
5826 if (!IS_CHERRYVIEW(dev_priv) &&
5827 max_pixclk > freq_320*limit/100)
dfcab17e 5828 return 400000;
6cca3195 5829 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5830 return freq_320;
e37c67a1 5831 else if (max_pixclk > 0)
dfcab17e 5832 return 266667;
e37c67a1
VS
5833 else
5834 return 200000;
30a970c6
JB
5835}
5836
f8437dd1
VK
5837static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5838 int max_pixclk)
5839{
5840 /*
5841 * FIXME:
5842 * - remove the guardband, it's not needed on BXT
5843 * - set 19.2MHz bypass frequency if there are no active pipes
5844 */
5845 if (max_pixclk > 576000*9/10)
5846 return 624000;
5847 else if (max_pixclk > 384000*9/10)
5848 return 576000;
5849 else if (max_pixclk > 288000*9/10)
5850 return 384000;
5851 else if (max_pixclk > 144000*9/10)
5852 return 288000;
5853 else
5854 return 144000;
5855}
5856
a821fc46
ACO
5857/* Compute the max pixel clock for new configuration. Uses atomic state if
5858 * that's non-NULL, look at current state otherwise. */
5859static int intel_mode_max_pixclk(struct drm_device *dev,
5860 struct drm_atomic_state *state)
30a970c6 5861{
30a970c6 5862 struct intel_crtc *intel_crtc;
304603f4 5863 struct intel_crtc_state *crtc_state;
30a970c6
JB
5864 int max_pixclk = 0;
5865
d3fcc808 5866 for_each_intel_crtc(dev, intel_crtc) {
27c329ed 5867 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
304603f4
ACO
5868 if (IS_ERR(crtc_state))
5869 return PTR_ERR(crtc_state);
5870
5871 if (!crtc_state->base.enable)
5872 continue;
5873
5874 max_pixclk = max(max_pixclk,
5875 crtc_state->base.adjusted_mode.crtc_clock);
30a970c6
JB
5876 }
5877
5878 return max_pixclk;
5879}
5880
27c329ed 5881static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 5882{
27c329ed
ML
5883 struct drm_device *dev = state->dev;
5884 struct drm_i915_private *dev_priv = dev->dev_private;
5885 int max_pixclk = intel_mode_max_pixclk(dev, state);
30a970c6 5886
304603f4
ACO
5887 if (max_pixclk < 0)
5888 return max_pixclk;
30a970c6 5889
27c329ed
ML
5890 to_intel_atomic_state(state)->cdclk =
5891 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 5892
27c329ed
ML
5893 return 0;
5894}
304603f4 5895
27c329ed
ML
5896static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5897{
5898 struct drm_device *dev = state->dev;
5899 struct drm_i915_private *dev_priv = dev->dev_private;
5900 int max_pixclk = intel_mode_max_pixclk(dev, state);
85a96e7a 5901
27c329ed
ML
5902 if (max_pixclk < 0)
5903 return max_pixclk;
85a96e7a 5904
27c329ed
ML
5905 to_intel_atomic_state(state)->cdclk =
5906 broxton_calc_cdclk(dev_priv, max_pixclk);
85a96e7a 5907
27c329ed 5908 return 0;
30a970c6
JB
5909}
5910
1e69cd74
VS
5911static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5912{
5913 unsigned int credits, default_credits;
5914
5915 if (IS_CHERRYVIEW(dev_priv))
5916 default_credits = PFI_CREDIT(12);
5917 else
5918 default_credits = PFI_CREDIT(8);
5919
164dfd28 5920 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
1e69cd74
VS
5921 /* CHV suggested value is 31 or 63 */
5922 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 5923 credits = PFI_CREDIT_63;
1e69cd74
VS
5924 else
5925 credits = PFI_CREDIT(15);
5926 } else {
5927 credits = default_credits;
5928 }
5929
5930 /*
5931 * WA - write default credits before re-programming
5932 * FIXME: should we also set the resend bit here?
5933 */
5934 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5935 default_credits);
5936
5937 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5938 credits | PFI_CREDIT_RESEND);
5939
5940 /*
5941 * FIXME is this guaranteed to clear
5942 * immediately or should we poll for it?
5943 */
5944 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5945}
5946
27c329ed 5947static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 5948{
a821fc46 5949 struct drm_device *dev = old_state->dev;
27c329ed 5950 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
30a970c6 5951 struct drm_i915_private *dev_priv = dev->dev_private;
30a970c6 5952
27c329ed
ML
5953 /*
5954 * FIXME: We can end up here with all power domains off, yet
5955 * with a CDCLK frequency other than the minimum. To account
5956 * for this take the PIPE-A power domain, which covers the HW
5957 * blocks needed for the following programming. This can be
5958 * removed once it's guaranteed that we get here either with
5959 * the minimum CDCLK set, or the required power domains
5960 * enabled.
5961 */
5962 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 5963
27c329ed
ML
5964 if (IS_CHERRYVIEW(dev))
5965 cherryview_set_cdclk(dev, req_cdclk);
5966 else
5967 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 5968
27c329ed 5969 vlv_program_pfi_credits(dev_priv);
1e69cd74 5970
27c329ed 5971 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
5972}
5973
89b667f8
JB
5974static void valleyview_crtc_enable(struct drm_crtc *crtc)
5975{
5976 struct drm_device *dev = crtc->dev;
a72e4c9f 5977 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
5978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5979 struct intel_encoder *encoder;
5980 int pipe = intel_crtc->pipe;
23538ef1 5981 bool is_dsi;
89b667f8 5982
53d9f4e9 5983 if (WARN_ON(intel_crtc->active))
89b667f8
JB
5984 return;
5985
409ee761 5986 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 5987
1ae0d137
VS
5988 if (!is_dsi) {
5989 if (IS_CHERRYVIEW(dev))
6e3c9717 5990 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5991 else
6e3c9717 5992 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5993 }
5b18e57c 5994
6e3c9717 5995 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5996 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5997
5998 intel_set_pipe_timings(intel_crtc);
5999
c14b0485
VS
6000 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6001 struct drm_i915_private *dev_priv = dev->dev_private;
6002
6003 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6004 I915_WRITE(CHV_CANVAS(pipe), 0);
6005 }
6006
5b18e57c
DV
6007 i9xx_set_pipeconf(intel_crtc);
6008
89b667f8 6009 intel_crtc->active = true;
89b667f8 6010
a72e4c9f 6011 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6012
89b667f8
JB
6013 for_each_encoder_on_crtc(dev, crtc, encoder)
6014 if (encoder->pre_pll_enable)
6015 encoder->pre_pll_enable(encoder);
6016
9d556c99
CML
6017 if (!is_dsi) {
6018 if (IS_CHERRYVIEW(dev))
6e3c9717 6019 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6020 else
6e3c9717 6021 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6022 }
89b667f8
JB
6023
6024 for_each_encoder_on_crtc(dev, crtc, encoder)
6025 if (encoder->pre_enable)
6026 encoder->pre_enable(encoder);
6027
2dd24552
JB
6028 i9xx_pfit_enable(intel_crtc);
6029
63cbb074
VS
6030 intel_crtc_load_lut(crtc);
6031
f37fcc2a 6032 intel_update_watermarks(crtc);
e1fdc473 6033 intel_enable_pipe(intel_crtc);
be6a6f8e 6034
4b3a9526
VS
6035 assert_vblank_disabled(crtc);
6036 drm_crtc_vblank_on(crtc);
6037
f9b61ff6
DV
6038 for_each_encoder_on_crtc(dev, crtc, encoder)
6039 encoder->enable(encoder);
89b667f8
JB
6040}
6041
f13c2ef3
DV
6042static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6043{
6044 struct drm_device *dev = crtc->base.dev;
6045 struct drm_i915_private *dev_priv = dev->dev_private;
6046
6e3c9717
ACO
6047 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6048 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6049}
6050
0b8765c6 6051static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6052{
6053 struct drm_device *dev = crtc->dev;
a72e4c9f 6054 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6055 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6056 struct intel_encoder *encoder;
79e53945 6057 int pipe = intel_crtc->pipe;
79e53945 6058
53d9f4e9 6059 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6060 return;
6061
f13c2ef3
DV
6062 i9xx_set_pll_dividers(intel_crtc);
6063
6e3c9717 6064 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6065 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6066
6067 intel_set_pipe_timings(intel_crtc);
6068
5b18e57c
DV
6069 i9xx_set_pipeconf(intel_crtc);
6070
f7abfe8b 6071 intel_crtc->active = true;
6b383a7f 6072
4a3436e8 6073 if (!IS_GEN2(dev))
a72e4c9f 6074 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6075
9d6d9f19
MK
6076 for_each_encoder_on_crtc(dev, crtc, encoder)
6077 if (encoder->pre_enable)
6078 encoder->pre_enable(encoder);
6079
f6736a1a
DV
6080 i9xx_enable_pll(intel_crtc);
6081
2dd24552
JB
6082 i9xx_pfit_enable(intel_crtc);
6083
63cbb074
VS
6084 intel_crtc_load_lut(crtc);
6085
f37fcc2a 6086 intel_update_watermarks(crtc);
e1fdc473 6087 intel_enable_pipe(intel_crtc);
be6a6f8e 6088
4b3a9526
VS
6089 assert_vblank_disabled(crtc);
6090 drm_crtc_vblank_on(crtc);
6091
f9b61ff6
DV
6092 for_each_encoder_on_crtc(dev, crtc, encoder)
6093 encoder->enable(encoder);
0b8765c6 6094}
79e53945 6095
87476d63
DV
6096static void i9xx_pfit_disable(struct intel_crtc *crtc)
6097{
6098 struct drm_device *dev = crtc->base.dev;
6099 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6100
6e3c9717 6101 if (!crtc->config->gmch_pfit.control)
328d8e82 6102 return;
87476d63 6103
328d8e82 6104 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6105
328d8e82
DV
6106 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6107 I915_READ(PFIT_CONTROL));
6108 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6109}
6110
0b8765c6
JB
6111static void i9xx_crtc_disable(struct drm_crtc *crtc)
6112{
6113 struct drm_device *dev = crtc->dev;
6114 struct drm_i915_private *dev_priv = dev->dev_private;
6115 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6116 struct intel_encoder *encoder;
0b8765c6 6117 int pipe = intel_crtc->pipe;
ef9c3aee 6118
6304cd91
VS
6119 /*
6120 * On gen2 planes are double buffered but the pipe isn't, so we must
6121 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6122 * We also need to wait on all gmch platforms because of the
6123 * self-refresh mode constraint explained above.
6304cd91 6124 */
564ed191 6125 intel_wait_for_vblank(dev, pipe);
6304cd91 6126
4b3a9526
VS
6127 for_each_encoder_on_crtc(dev, crtc, encoder)
6128 encoder->disable(encoder);
6129
f9b61ff6
DV
6130 drm_crtc_vblank_off(crtc);
6131 assert_vblank_disabled(crtc);
6132
575f7ab7 6133 intel_disable_pipe(intel_crtc);
24a1f16d 6134
87476d63 6135 i9xx_pfit_disable(intel_crtc);
24a1f16d 6136
89b667f8
JB
6137 for_each_encoder_on_crtc(dev, crtc, encoder)
6138 if (encoder->post_disable)
6139 encoder->post_disable(encoder);
6140
409ee761 6141 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6142 if (IS_CHERRYVIEW(dev))
6143 chv_disable_pll(dev_priv, pipe);
6144 else if (IS_VALLEYVIEW(dev))
6145 vlv_disable_pll(dev_priv, pipe);
6146 else
1c4e0274 6147 i9xx_disable_pll(intel_crtc);
076ed3b2 6148 }
0b8765c6 6149
4a3436e8 6150 if (!IS_GEN2(dev))
a72e4c9f 6151 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6152}
6153
b17d48e2
ML
6154static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6155{
6156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6157 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6158 enum intel_display_power_domain domain;
6159 unsigned long domains;
6160
6161 if (!intel_crtc->active)
6162 return;
6163
a539205a
ML
6164 if (to_intel_plane_state(crtc->primary->state)->visible) {
6165 intel_crtc_wait_for_pending_flips(crtc);
6166 intel_pre_disable_primary(crtc);
6167 }
6168
d032ffa0 6169 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
b17d48e2
ML
6170 dev_priv->display.crtc_disable(crtc);
6171
6172 domains = intel_crtc->enabled_power_domains;
6173 for_each_power_domain(domain, domains)
6174 intel_display_power_put(dev_priv, domain);
6175 intel_crtc->enabled_power_domains = 0;
6176}
6177
6b72d486
ML
6178/*
6179 * turn all crtc's off, but do not adjust state
6180 * This has to be paired with a call to intel_modeset_setup_hw_state.
6181 */
9716c691 6182void intel_display_suspend(struct drm_device *dev)
ee7b9f93 6183{
6b72d486
ML
6184 struct drm_crtc *crtc;
6185
b17d48e2
ML
6186 for_each_crtc(dev, crtc)
6187 intel_crtc_disable_noatomic(crtc);
ee7b9f93
JB
6188}
6189
b04c5bd6 6190/* Master function to enable/disable CRTC and corresponding power wells */
5da76e94 6191int intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
6192{
6193 struct drm_device *dev = crtc->dev;
5da76e94
ML
6194 struct drm_mode_config *config = &dev->mode_config;
6195 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
0e572fe7 6196 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5da76e94
ML
6197 struct intel_crtc_state *pipe_config;
6198 struct drm_atomic_state *state;
6199 int ret;
976f8a20 6200
1b509259 6201 if (enable == intel_crtc->active)
5da76e94 6202 return 0;
0e572fe7 6203
1b509259 6204 if (enable && !crtc->state->enable)
5da76e94 6205 return 0;
1b509259 6206
5da76e94
ML
6207 /* this function should be called with drm_modeset_lock_all for now */
6208 if (WARN_ON(!ctx))
6209 return -EIO;
6210 lockdep_assert_held(&ctx->ww_ctx);
1b509259 6211
5da76e94
ML
6212 state = drm_atomic_state_alloc(dev);
6213 if (WARN_ON(!state))
6214 return -ENOMEM;
1b509259 6215
5da76e94
ML
6216 state->acquire_ctx = ctx;
6217 state->allow_modeset = true;
6218
6219 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6220 if (IS_ERR(pipe_config)) {
6221 ret = PTR_ERR(pipe_config);
6222 goto err;
0e572fe7 6223 }
5da76e94
ML
6224 pipe_config->base.active = enable;
6225
6226 ret = intel_set_mode(state);
6227 if (!ret)
6228 return ret;
6229
6230err:
6231 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6232 drm_atomic_state_free(state);
6233 return ret;
b04c5bd6
BF
6234}
6235
6236/**
6237 * Sets the power management mode of the pipe and plane.
6238 */
6239void intel_crtc_update_dpms(struct drm_crtc *crtc)
6240{
6241 struct drm_device *dev = crtc->dev;
6242 struct intel_encoder *intel_encoder;
6243 bool enable = false;
6244
6245 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6246 enable |= intel_encoder->connectors_active;
6247
6248 intel_crtc_control(crtc, enable);
cdd59983
CW
6249}
6250
ea5b213a 6251void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6252{
4ef69c7a 6253 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6254
ea5b213a
CW
6255 drm_encoder_cleanup(encoder);
6256 kfree(intel_encoder);
7e7d76c3
JB
6257}
6258
9237329d 6259/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
6260 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6261 * state of the entire output pipe. */
9237329d 6262static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 6263{
5ab432ef
DV
6264 if (mode == DRM_MODE_DPMS_ON) {
6265 encoder->connectors_active = true;
6266
b2cabb0e 6267 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
6268 } else {
6269 encoder->connectors_active = false;
6270
b2cabb0e 6271 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 6272 }
79e53945
JB
6273}
6274
0a91ca29
DV
6275/* Cross check the actual hw state with our own modeset state tracking (and it's
6276 * internal consistency). */
b980514c 6277static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6278{
0a91ca29
DV
6279 if (connector->get_hw_state(connector)) {
6280 struct intel_encoder *encoder = connector->encoder;
6281 struct drm_crtc *crtc;
6282 bool encoder_enabled;
6283 enum pipe pipe;
6284
6285 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6286 connector->base.base.id,
c23cc417 6287 connector->base.name);
0a91ca29 6288
0e32b39c
DA
6289 /* there is no real hw state for MST connectors */
6290 if (connector->mst_port)
6291 return;
6292
e2c719b7 6293 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 6294 "wrong connector dpms state\n");
e2c719b7 6295 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 6296 "active connector not linked to encoder\n");
0a91ca29 6297
36cd7444 6298 if (encoder) {
e2c719b7 6299 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
6300 "encoder->connectors_active not set\n");
6301
6302 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
6303 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6304 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 6305 return;
0a91ca29 6306
36cd7444 6307 crtc = encoder->base.crtc;
0a91ca29 6308
83d65738
MR
6309 I915_STATE_WARN(!crtc->state->enable,
6310 "crtc not enabled\n");
e2c719b7
RC
6311 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6312 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
6313 "encoder active on the wrong pipe\n");
6314 }
0a91ca29 6315 }
79e53945
JB
6316}
6317
08d9bc92
ACO
6318int intel_connector_init(struct intel_connector *connector)
6319{
6320 struct drm_connector_state *connector_state;
6321
6322 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6323 if (!connector_state)
6324 return -ENOMEM;
6325
6326 connector->base.state = connector_state;
6327 return 0;
6328}
6329
6330struct intel_connector *intel_connector_alloc(void)
6331{
6332 struct intel_connector *connector;
6333
6334 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6335 if (!connector)
6336 return NULL;
6337
6338 if (intel_connector_init(connector) < 0) {
6339 kfree(connector);
6340 return NULL;
6341 }
6342
6343 return connector;
6344}
6345
5ab432ef
DV
6346/* Even simpler default implementation, if there's really no special case to
6347 * consider. */
6348void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 6349{
5ab432ef
DV
6350 /* All the simple cases only support two dpms states. */
6351 if (mode != DRM_MODE_DPMS_ON)
6352 mode = DRM_MODE_DPMS_OFF;
d4270e57 6353
5ab432ef
DV
6354 if (mode == connector->dpms)
6355 return;
6356
6357 connector->dpms = mode;
6358
6359 /* Only need to change hw state when actually enabled */
c9976dcf
CW
6360 if (connector->encoder)
6361 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 6362
b980514c 6363 intel_modeset_check_state(connector->dev);
79e53945
JB
6364}
6365
f0947c37
DV
6366/* Simple connector->get_hw_state implementation for encoders that support only
6367 * one connector and no cloning and hence the encoder state determines the state
6368 * of the connector. */
6369bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6370{
24929352 6371 enum pipe pipe = 0;
f0947c37 6372 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6373
f0947c37 6374 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6375}
6376
6d293983 6377static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6378{
6d293983
ACO
6379 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6380 return crtc_state->fdi_lanes;
d272ddfa
VS
6381
6382 return 0;
6383}
6384
6d293983 6385static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6386 struct intel_crtc_state *pipe_config)
1857e1da 6387{
6d293983
ACO
6388 struct drm_atomic_state *state = pipe_config->base.state;
6389 struct intel_crtc *other_crtc;
6390 struct intel_crtc_state *other_crtc_state;
6391
1857e1da
DV
6392 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6393 pipe_name(pipe), pipe_config->fdi_lanes);
6394 if (pipe_config->fdi_lanes > 4) {
6395 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6396 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6397 return -EINVAL;
1857e1da
DV
6398 }
6399
bafb6553 6400 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6401 if (pipe_config->fdi_lanes > 2) {
6402 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6403 pipe_config->fdi_lanes);
6d293983 6404 return -EINVAL;
1857e1da 6405 } else {
6d293983 6406 return 0;
1857e1da
DV
6407 }
6408 }
6409
6410 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6411 return 0;
1857e1da
DV
6412
6413 /* Ivybridge 3 pipe is really complicated */
6414 switch (pipe) {
6415 case PIPE_A:
6d293983 6416 return 0;
1857e1da 6417 case PIPE_B:
6d293983
ACO
6418 if (pipe_config->fdi_lanes <= 2)
6419 return 0;
6420
6421 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6422 other_crtc_state =
6423 intel_atomic_get_crtc_state(state, other_crtc);
6424 if (IS_ERR(other_crtc_state))
6425 return PTR_ERR(other_crtc_state);
6426
6427 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6428 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6429 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6430 return -EINVAL;
1857e1da 6431 }
6d293983 6432 return 0;
1857e1da 6433 case PIPE_C:
251cc67c
VS
6434 if (pipe_config->fdi_lanes > 2) {
6435 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6436 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6437 return -EINVAL;
251cc67c 6438 }
6d293983
ACO
6439
6440 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6441 other_crtc_state =
6442 intel_atomic_get_crtc_state(state, other_crtc);
6443 if (IS_ERR(other_crtc_state))
6444 return PTR_ERR(other_crtc_state);
6445
6446 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6447 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6448 return -EINVAL;
1857e1da 6449 }
6d293983 6450 return 0;
1857e1da
DV
6451 default:
6452 BUG();
6453 }
6454}
6455
e29c22c0
DV
6456#define RETRY 1
6457static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6458 struct intel_crtc_state *pipe_config)
877d48d5 6459{
1857e1da 6460 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 6461 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6462 int lane, link_bw, fdi_dotclock, ret;
6463 bool needs_recompute = false;
877d48d5 6464
e29c22c0 6465retry:
877d48d5
DV
6466 /* FDI is a binary signal running at ~2.7GHz, encoding
6467 * each output octet as 10 bits. The actual frequency
6468 * is stored as a divider into a 100MHz clock, and the
6469 * mode pixel clock is stored in units of 1KHz.
6470 * Hence the bw of each lane in terms of the mode signal
6471 * is:
6472 */
6473 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6474
241bfc38 6475 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6476
2bd89a07 6477 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6478 pipe_config->pipe_bpp);
6479
6480 pipe_config->fdi_lanes = lane;
6481
2bd89a07 6482 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6483 link_bw, &pipe_config->fdi_m_n);
1857e1da 6484
6d293983
ACO
6485 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6486 intel_crtc->pipe, pipe_config);
6487 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6488 pipe_config->pipe_bpp -= 2*3;
6489 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6490 pipe_config->pipe_bpp);
6491 needs_recompute = true;
6492 pipe_config->bw_constrained = true;
6493
6494 goto retry;
6495 }
6496
6497 if (needs_recompute)
6498 return RETRY;
6499
6d293983 6500 return ret;
877d48d5
DV
6501}
6502
8cfb3407
VS
6503static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6504 struct intel_crtc_state *pipe_config)
6505{
6506 if (pipe_config->pipe_bpp > 24)
6507 return false;
6508
6509 /* HSW can handle pixel rate up to cdclk? */
6510 if (IS_HASWELL(dev_priv->dev))
6511 return true;
6512
6513 /*
b432e5cf
VS
6514 * We compare against max which means we must take
6515 * the increased cdclk requirement into account when
6516 * calculating the new cdclk.
6517 *
6518 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6519 */
6520 return ilk_pipe_pixel_rate(pipe_config) <=
6521 dev_priv->max_cdclk_freq * 95 / 100;
6522}
6523
42db64ef 6524static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6525 struct intel_crtc_state *pipe_config)
42db64ef 6526{
8cfb3407
VS
6527 struct drm_device *dev = crtc->base.dev;
6528 struct drm_i915_private *dev_priv = dev->dev_private;
6529
d330a953 6530 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6531 hsw_crtc_supports_ips(crtc) &&
6532 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6533}
6534
a43f6e0f 6535static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6536 struct intel_crtc_state *pipe_config)
79e53945 6537{
a43f6e0f 6538 struct drm_device *dev = crtc->base.dev;
8bd31e67 6539 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 6540 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 6541
ad3a4479 6542 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6543 if (INTEL_INFO(dev)->gen < 4) {
44913155 6544 int clock_limit = dev_priv->max_cdclk_freq;
cf532bb2
VS
6545
6546 /*
6547 * Enable pixel doubling when the dot clock
6548 * is > 90% of the (display) core speed.
6549 *
b397c96b
VS
6550 * GDG double wide on either pipe,
6551 * otherwise pipe A only.
cf532bb2 6552 */
b397c96b 6553 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 6554 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 6555 clock_limit *= 2;
cf532bb2 6556 pipe_config->double_wide = true;
ad3a4479
VS
6557 }
6558
241bfc38 6559 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 6560 return -EINVAL;
2c07245f 6561 }
89749350 6562
1d1d0e27
VS
6563 /*
6564 * Pipe horizontal size must be even in:
6565 * - DVO ganged mode
6566 * - LVDS dual channel mode
6567 * - Double wide pipe
6568 */
a93e255f 6569 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6570 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6571 pipe_config->pipe_src_w &= ~1;
6572
8693a824
DL
6573 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6574 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6575 */
6576 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6577 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 6578 return -EINVAL;
44f46b42 6579
f5adf94e 6580 if (HAS_IPS(dev))
a43f6e0f
DV
6581 hsw_compute_ips_config(crtc, pipe_config);
6582
877d48d5 6583 if (pipe_config->has_pch_encoder)
a43f6e0f 6584 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6585
cf5a15be 6586 return 0;
79e53945
JB
6587}
6588
1652d19e
VS
6589static int skylake_get_display_clock_speed(struct drm_device *dev)
6590{
6591 struct drm_i915_private *dev_priv = to_i915(dev);
6592 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6593 uint32_t cdctl = I915_READ(CDCLK_CTL);
6594 uint32_t linkrate;
6595
414355a7 6596 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6597 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6598
6599 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6600 return 540000;
6601
6602 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6603 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6604
71cd8423
DL
6605 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6606 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6607 /* vco 8640 */
6608 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6609 case CDCLK_FREQ_450_432:
6610 return 432000;
6611 case CDCLK_FREQ_337_308:
6612 return 308570;
6613 case CDCLK_FREQ_675_617:
6614 return 617140;
6615 default:
6616 WARN(1, "Unknown cd freq selection\n");
6617 }
6618 } else {
6619 /* vco 8100 */
6620 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6621 case CDCLK_FREQ_450_432:
6622 return 450000;
6623 case CDCLK_FREQ_337_308:
6624 return 337500;
6625 case CDCLK_FREQ_675_617:
6626 return 675000;
6627 default:
6628 WARN(1, "Unknown cd freq selection\n");
6629 }
6630 }
6631
6632 /* error case, do as if DPLL0 isn't enabled */
6633 return 24000;
6634}
6635
6636static int broadwell_get_display_clock_speed(struct drm_device *dev)
6637{
6638 struct drm_i915_private *dev_priv = dev->dev_private;
6639 uint32_t lcpll = I915_READ(LCPLL_CTL);
6640 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6641
6642 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6643 return 800000;
6644 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6645 return 450000;
6646 else if (freq == LCPLL_CLK_FREQ_450)
6647 return 450000;
6648 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6649 return 540000;
6650 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6651 return 337500;
6652 else
6653 return 675000;
6654}
6655
6656static int haswell_get_display_clock_speed(struct drm_device *dev)
6657{
6658 struct drm_i915_private *dev_priv = dev->dev_private;
6659 uint32_t lcpll = I915_READ(LCPLL_CTL);
6660 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6661
6662 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6663 return 800000;
6664 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6665 return 450000;
6666 else if (freq == LCPLL_CLK_FREQ_450)
6667 return 450000;
6668 else if (IS_HSW_ULT(dev))
6669 return 337500;
6670 else
6671 return 540000;
79e53945
JB
6672}
6673
25eb05fc
JB
6674static int valleyview_get_display_clock_speed(struct drm_device *dev)
6675{
d197b7d3 6676 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
6677 u32 val;
6678 int divider;
6679
6bcda4f0
VS
6680 if (dev_priv->hpll_freq == 0)
6681 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6682
a580516d 6683 mutex_lock(&dev_priv->sb_lock);
d197b7d3 6684 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
a580516d 6685 mutex_unlock(&dev_priv->sb_lock);
d197b7d3
VS
6686
6687 divider = val & DISPLAY_FREQUENCY_VALUES;
6688
7d007f40
VS
6689 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6690 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6691 "cdclk change in progress\n");
6692
6bcda4f0 6693 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
6694}
6695
b37a6434
VS
6696static int ilk_get_display_clock_speed(struct drm_device *dev)
6697{
6698 return 450000;
6699}
6700
e70236a8
JB
6701static int i945_get_display_clock_speed(struct drm_device *dev)
6702{
6703 return 400000;
6704}
79e53945 6705
e70236a8 6706static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6707{
e907f170 6708 return 333333;
e70236a8 6709}
79e53945 6710
e70236a8
JB
6711static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6712{
6713 return 200000;
6714}
79e53945 6715
257a7ffc
DV
6716static int pnv_get_display_clock_speed(struct drm_device *dev)
6717{
6718 u16 gcfgc = 0;
6719
6720 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6721
6722 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6723 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6724 return 266667;
257a7ffc 6725 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6726 return 333333;
257a7ffc 6727 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6728 return 444444;
257a7ffc
DV
6729 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6730 return 200000;
6731 default:
6732 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6733 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6734 return 133333;
257a7ffc 6735 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6736 return 166667;
257a7ffc
DV
6737 }
6738}
6739
e70236a8
JB
6740static int i915gm_get_display_clock_speed(struct drm_device *dev)
6741{
6742 u16 gcfgc = 0;
79e53945 6743
e70236a8
JB
6744 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6745
6746 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6747 return 133333;
e70236a8
JB
6748 else {
6749 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6750 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6751 return 333333;
e70236a8
JB
6752 default:
6753 case GC_DISPLAY_CLOCK_190_200_MHZ:
6754 return 190000;
79e53945 6755 }
e70236a8
JB
6756 }
6757}
6758
6759static int i865_get_display_clock_speed(struct drm_device *dev)
6760{
e907f170 6761 return 266667;
e70236a8
JB
6762}
6763
1b1d2716 6764static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6765{
6766 u16 hpllcc = 0;
1b1d2716 6767
65cd2b3f
VS
6768 /*
6769 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6770 * encoding is different :(
6771 * FIXME is this the right way to detect 852GM/852GMV?
6772 */
6773 if (dev->pdev->revision == 0x1)
6774 return 133333;
6775
1b1d2716
VS
6776 pci_bus_read_config_word(dev->pdev->bus,
6777 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6778
e70236a8
JB
6779 /* Assume that the hardware is in the high speed state. This
6780 * should be the default.
6781 */
6782 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6783 case GC_CLOCK_133_200:
1b1d2716 6784 case GC_CLOCK_133_200_2:
e70236a8
JB
6785 case GC_CLOCK_100_200:
6786 return 200000;
6787 case GC_CLOCK_166_250:
6788 return 250000;
6789 case GC_CLOCK_100_133:
e907f170 6790 return 133333;
1b1d2716
VS
6791 case GC_CLOCK_133_266:
6792 case GC_CLOCK_133_266_2:
6793 case GC_CLOCK_166_266:
6794 return 266667;
e70236a8 6795 }
79e53945 6796
e70236a8
JB
6797 /* Shouldn't happen */
6798 return 0;
6799}
79e53945 6800
e70236a8
JB
6801static int i830_get_display_clock_speed(struct drm_device *dev)
6802{
e907f170 6803 return 133333;
79e53945
JB
6804}
6805
34edce2f
VS
6806static unsigned int intel_hpll_vco(struct drm_device *dev)
6807{
6808 struct drm_i915_private *dev_priv = dev->dev_private;
6809 static const unsigned int blb_vco[8] = {
6810 [0] = 3200000,
6811 [1] = 4000000,
6812 [2] = 5333333,
6813 [3] = 4800000,
6814 [4] = 6400000,
6815 };
6816 static const unsigned int pnv_vco[8] = {
6817 [0] = 3200000,
6818 [1] = 4000000,
6819 [2] = 5333333,
6820 [3] = 4800000,
6821 [4] = 2666667,
6822 };
6823 static const unsigned int cl_vco[8] = {
6824 [0] = 3200000,
6825 [1] = 4000000,
6826 [2] = 5333333,
6827 [3] = 6400000,
6828 [4] = 3333333,
6829 [5] = 3566667,
6830 [6] = 4266667,
6831 };
6832 static const unsigned int elk_vco[8] = {
6833 [0] = 3200000,
6834 [1] = 4000000,
6835 [2] = 5333333,
6836 [3] = 4800000,
6837 };
6838 static const unsigned int ctg_vco[8] = {
6839 [0] = 3200000,
6840 [1] = 4000000,
6841 [2] = 5333333,
6842 [3] = 6400000,
6843 [4] = 2666667,
6844 [5] = 4266667,
6845 };
6846 const unsigned int *vco_table;
6847 unsigned int vco;
6848 uint8_t tmp = 0;
6849
6850 /* FIXME other chipsets? */
6851 if (IS_GM45(dev))
6852 vco_table = ctg_vco;
6853 else if (IS_G4X(dev))
6854 vco_table = elk_vco;
6855 else if (IS_CRESTLINE(dev))
6856 vco_table = cl_vco;
6857 else if (IS_PINEVIEW(dev))
6858 vco_table = pnv_vco;
6859 else if (IS_G33(dev))
6860 vco_table = blb_vco;
6861 else
6862 return 0;
6863
6864 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6865
6866 vco = vco_table[tmp & 0x7];
6867 if (vco == 0)
6868 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6869 else
6870 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6871
6872 return vco;
6873}
6874
6875static int gm45_get_display_clock_speed(struct drm_device *dev)
6876{
6877 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6878 uint16_t tmp = 0;
6879
6880 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6881
6882 cdclk_sel = (tmp >> 12) & 0x1;
6883
6884 switch (vco) {
6885 case 2666667:
6886 case 4000000:
6887 case 5333333:
6888 return cdclk_sel ? 333333 : 222222;
6889 case 3200000:
6890 return cdclk_sel ? 320000 : 228571;
6891 default:
6892 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6893 return 222222;
6894 }
6895}
6896
6897static int i965gm_get_display_clock_speed(struct drm_device *dev)
6898{
6899 static const uint8_t div_3200[] = { 16, 10, 8 };
6900 static const uint8_t div_4000[] = { 20, 12, 10 };
6901 static const uint8_t div_5333[] = { 24, 16, 14 };
6902 const uint8_t *div_table;
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 >> 8) & 0x1f) - 1;
6909
6910 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6911 goto fail;
6912
6913 switch (vco) {
6914 case 3200000:
6915 div_table = div_3200;
6916 break;
6917 case 4000000:
6918 div_table = div_4000;
6919 break;
6920 case 5333333:
6921 div_table = div_5333;
6922 break;
6923 default:
6924 goto fail;
6925 }
6926
6927 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6928
caf4e252 6929fail:
34edce2f
VS
6930 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6931 return 200000;
6932}
6933
6934static int g33_get_display_clock_speed(struct drm_device *dev)
6935{
6936 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6937 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6938 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6939 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6940 const uint8_t *div_table;
6941 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6942 uint16_t tmp = 0;
6943
6944 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6945
6946 cdclk_sel = (tmp >> 4) & 0x7;
6947
6948 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6949 goto fail;
6950
6951 switch (vco) {
6952 case 3200000:
6953 div_table = div_3200;
6954 break;
6955 case 4000000:
6956 div_table = div_4000;
6957 break;
6958 case 4800000:
6959 div_table = div_4800;
6960 break;
6961 case 5333333:
6962 div_table = div_5333;
6963 break;
6964 default:
6965 goto fail;
6966 }
6967
6968 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6969
caf4e252 6970fail:
34edce2f
VS
6971 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6972 return 190476;
6973}
6974
2c07245f 6975static void
a65851af 6976intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6977{
a65851af
VS
6978 while (*num > DATA_LINK_M_N_MASK ||
6979 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6980 *num >>= 1;
6981 *den >>= 1;
6982 }
6983}
6984
a65851af
VS
6985static void compute_m_n(unsigned int m, unsigned int n,
6986 uint32_t *ret_m, uint32_t *ret_n)
6987{
6988 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6989 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6990 intel_reduce_m_n_ratio(ret_m, ret_n);
6991}
6992
e69d0bc1
DV
6993void
6994intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6995 int pixel_clock, int link_clock,
6996 struct intel_link_m_n *m_n)
2c07245f 6997{
e69d0bc1 6998 m_n->tu = 64;
a65851af
VS
6999
7000 compute_m_n(bits_per_pixel * pixel_clock,
7001 link_clock * nlanes * 8,
7002 &m_n->gmch_m, &m_n->gmch_n);
7003
7004 compute_m_n(pixel_clock, link_clock,
7005 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7006}
7007
a7615030
CW
7008static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7009{
d330a953
JN
7010 if (i915.panel_use_ssc >= 0)
7011 return i915.panel_use_ssc != 0;
41aa3448 7012 return dev_priv->vbt.lvds_use_ssc
435793df 7013 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7014}
7015
a93e255f
ACO
7016static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7017 int num_connectors)
c65d77d8 7018{
a93e255f 7019 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
7020 struct drm_i915_private *dev_priv = dev->dev_private;
7021 int refclk;
7022
a93e255f
ACO
7023 WARN_ON(!crtc_state->base.state);
7024
5ab7b0b7 7025 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 7026 refclk = 100000;
a93e255f 7027 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 7028 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
7029 refclk = dev_priv->vbt.lvds_ssc_freq;
7030 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
7031 } else if (!IS_GEN2(dev)) {
7032 refclk = 96000;
7033 } else {
7034 refclk = 48000;
7035 }
7036
7037 return refclk;
7038}
7039
7429e9d4 7040static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7041{
7df00d7a 7042 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7043}
f47709a9 7044
7429e9d4
DV
7045static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7046{
7047 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7048}
7049
f47709a9 7050static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7051 struct intel_crtc_state *crtc_state,
a7516a05
JB
7052 intel_clock_t *reduced_clock)
7053{
f47709a9 7054 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7055 u32 fp, fp2 = 0;
7056
7057 if (IS_PINEVIEW(dev)) {
190f68c5 7058 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7059 if (reduced_clock)
7429e9d4 7060 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7061 } else {
190f68c5 7062 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7063 if (reduced_clock)
7429e9d4 7064 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7065 }
7066
190f68c5 7067 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7068
f47709a9 7069 crtc->lowfreq_avail = false;
a93e255f 7070 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7071 reduced_clock) {
190f68c5 7072 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7073 crtc->lowfreq_avail = true;
a7516a05 7074 } else {
190f68c5 7075 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7076 }
7077}
7078
5e69f97f
CML
7079static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7080 pipe)
89b667f8
JB
7081{
7082 u32 reg_val;
7083
7084 /*
7085 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7086 * and set it to a reasonable value instead.
7087 */
ab3c759a 7088 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7089 reg_val &= 0xffffff00;
7090 reg_val |= 0x00000030;
ab3c759a 7091 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7092
ab3c759a 7093 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7094 reg_val &= 0x8cffffff;
7095 reg_val = 0x8c000000;
ab3c759a 7096 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7097
ab3c759a 7098 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7099 reg_val &= 0xffffff00;
ab3c759a 7100 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7101
ab3c759a 7102 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7103 reg_val &= 0x00ffffff;
7104 reg_val |= 0xb0000000;
ab3c759a 7105 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7106}
7107
b551842d
DV
7108static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7109 struct intel_link_m_n *m_n)
7110{
7111 struct drm_device *dev = crtc->base.dev;
7112 struct drm_i915_private *dev_priv = dev->dev_private;
7113 int pipe = crtc->pipe;
7114
e3b95f1e
DV
7115 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7116 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7117 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7118 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7119}
7120
7121static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7122 struct intel_link_m_n *m_n,
7123 struct intel_link_m_n *m2_n2)
b551842d
DV
7124{
7125 struct drm_device *dev = crtc->base.dev;
7126 struct drm_i915_private *dev_priv = dev->dev_private;
7127 int pipe = crtc->pipe;
6e3c9717 7128 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7129
7130 if (INTEL_INFO(dev)->gen >= 5) {
7131 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7132 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7133 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7134 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7135 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7136 * for gen < 8) and if DRRS is supported (to make sure the
7137 * registers are not unnecessarily accessed).
7138 */
44395bfe 7139 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7140 crtc->config->has_drrs) {
f769cd24
VK
7141 I915_WRITE(PIPE_DATA_M2(transcoder),
7142 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7143 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7144 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7145 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7146 }
b551842d 7147 } else {
e3b95f1e
DV
7148 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7149 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7150 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7151 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7152 }
7153}
7154
fe3cd48d 7155void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7156{
fe3cd48d
R
7157 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7158
7159 if (m_n == M1_N1) {
7160 dp_m_n = &crtc->config->dp_m_n;
7161 dp_m2_n2 = &crtc->config->dp_m2_n2;
7162 } else if (m_n == M2_N2) {
7163
7164 /*
7165 * M2_N2 registers are not supported. Hence m2_n2 divider value
7166 * needs to be programmed into M1_N1.
7167 */
7168 dp_m_n = &crtc->config->dp_m2_n2;
7169 } else {
7170 DRM_ERROR("Unsupported divider value\n");
7171 return;
7172 }
7173
6e3c9717
ACO
7174 if (crtc->config->has_pch_encoder)
7175 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7176 else
fe3cd48d 7177 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7178}
7179
d288f65f 7180static void vlv_update_pll(struct intel_crtc *crtc,
5cec258b 7181 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7182{
7183 u32 dpll, dpll_md;
7184
7185 /*
7186 * Enable DPIO clock input. We should never disable the reference
7187 * clock for pipe B, since VGA hotplug / manual detection depends
7188 * on it.
7189 */
7190 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7191 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7192 /* We should never disable this, set it here for state tracking */
7193 if (crtc->pipe == PIPE_B)
7194 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7195 dpll |= DPLL_VCO_ENABLE;
d288f65f 7196 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7197
d288f65f 7198 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7199 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7200 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7201}
7202
d288f65f 7203static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7204 const struct intel_crtc_state *pipe_config)
a0c4da24 7205{
f47709a9 7206 struct drm_device *dev = crtc->base.dev;
a0c4da24 7207 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7208 int pipe = crtc->pipe;
bdd4b6a6 7209 u32 mdiv;
a0c4da24 7210 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7211 u32 coreclk, reg_val;
a0c4da24 7212
a580516d 7213 mutex_lock(&dev_priv->sb_lock);
09153000 7214
d288f65f
VS
7215 bestn = pipe_config->dpll.n;
7216 bestm1 = pipe_config->dpll.m1;
7217 bestm2 = pipe_config->dpll.m2;
7218 bestp1 = pipe_config->dpll.p1;
7219 bestp2 = pipe_config->dpll.p2;
a0c4da24 7220
89b667f8
JB
7221 /* See eDP HDMI DPIO driver vbios notes doc */
7222
7223 /* PLL B needs special handling */
bdd4b6a6 7224 if (pipe == PIPE_B)
5e69f97f 7225 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7226
7227 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7228 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7229
7230 /* Disable target IRef on PLL */
ab3c759a 7231 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7232 reg_val &= 0x00ffffff;
ab3c759a 7233 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7234
7235 /* Disable fast lock */
ab3c759a 7236 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7237
7238 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7239 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7240 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7241 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7242 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7243
7244 /*
7245 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7246 * but we don't support that).
7247 * Note: don't use the DAC post divider as it seems unstable.
7248 */
7249 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7250 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7251
a0c4da24 7252 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7253 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7254
89b667f8 7255 /* Set HBR and RBR LPF coefficients */
d288f65f 7256 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7257 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7258 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7259 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7260 0x009f0003);
89b667f8 7261 else
ab3c759a 7262 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7263 0x00d0000f);
7264
681a8504 7265 if (pipe_config->has_dp_encoder) {
89b667f8 7266 /* Use SSC source */
bdd4b6a6 7267 if (pipe == PIPE_A)
ab3c759a 7268 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7269 0x0df40000);
7270 else
ab3c759a 7271 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7272 0x0df70000);
7273 } else { /* HDMI or VGA */
7274 /* Use bend source */
bdd4b6a6 7275 if (pipe == PIPE_A)
ab3c759a 7276 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7277 0x0df70000);
7278 else
ab3c759a 7279 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7280 0x0df40000);
7281 }
a0c4da24 7282
ab3c759a 7283 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7284 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7285 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7286 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7287 coreclk |= 0x01000000;
ab3c759a 7288 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7289
ab3c759a 7290 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7291 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7292}
7293
d288f65f 7294static void chv_update_pll(struct intel_crtc *crtc,
5cec258b 7295 struct intel_crtc_state *pipe_config)
1ae0d137 7296{
d288f65f 7297 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
7298 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7299 DPLL_VCO_ENABLE;
7300 if (crtc->pipe != PIPE_A)
d288f65f 7301 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7302
d288f65f
VS
7303 pipe_config->dpll_hw_state.dpll_md =
7304 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7305}
7306
d288f65f 7307static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7308 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7309{
7310 struct drm_device *dev = crtc->base.dev;
7311 struct drm_i915_private *dev_priv = dev->dev_private;
7312 int pipe = crtc->pipe;
7313 int dpll_reg = DPLL(crtc->pipe);
7314 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7315 u32 loopfilter, tribuf_calcntr;
9d556c99 7316 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7317 u32 dpio_val;
9cbe40c1 7318 int vco;
9d556c99 7319
d288f65f
VS
7320 bestn = pipe_config->dpll.n;
7321 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7322 bestm1 = pipe_config->dpll.m1;
7323 bestm2 = pipe_config->dpll.m2 >> 22;
7324 bestp1 = pipe_config->dpll.p1;
7325 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7326 vco = pipe_config->dpll.vco;
a945ce7e 7327 dpio_val = 0;
9cbe40c1 7328 loopfilter = 0;
9d556c99
CML
7329
7330 /*
7331 * Enable Refclk and SSC
7332 */
a11b0703 7333 I915_WRITE(dpll_reg,
d288f65f 7334 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7335
a580516d 7336 mutex_lock(&dev_priv->sb_lock);
9d556c99 7337
9d556c99
CML
7338 /* p1 and p2 divider */
7339 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7340 5 << DPIO_CHV_S1_DIV_SHIFT |
7341 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7342 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7343 1 << DPIO_CHV_K_DIV_SHIFT);
7344
7345 /* Feedback post-divider - m2 */
7346 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7347
7348 /* Feedback refclk divider - n and m1 */
7349 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7350 DPIO_CHV_M1_DIV_BY_2 |
7351 1 << DPIO_CHV_N_DIV_SHIFT);
7352
7353 /* M2 fraction division */
a945ce7e
VP
7354 if (bestm2_frac)
7355 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7356
7357 /* M2 fraction division enable */
a945ce7e
VP
7358 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7359 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7360 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7361 if (bestm2_frac)
7362 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7363 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7364
de3a0fde
VP
7365 /* Program digital lock detect threshold */
7366 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7367 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7368 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7369 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7370 if (!bestm2_frac)
7371 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7372 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7373
9d556c99 7374 /* Loop filter */
9cbe40c1
VP
7375 if (vco == 5400000) {
7376 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7377 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7378 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7379 tribuf_calcntr = 0x9;
7380 } else if (vco <= 6200000) {
7381 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7382 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7383 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7384 tribuf_calcntr = 0x9;
7385 } else if (vco <= 6480000) {
7386 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7387 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7388 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7389 tribuf_calcntr = 0x8;
7390 } else {
7391 /* Not supported. Apply the same limits as in the max case */
7392 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7393 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7394 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7395 tribuf_calcntr = 0;
7396 }
9d556c99
CML
7397 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7398
968040b2 7399 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7400 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7401 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7402 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7403
9d556c99
CML
7404 /* AFC Recal */
7405 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7406 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7407 DPIO_AFC_RECAL);
7408
a580516d 7409 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7410}
7411
d288f65f
VS
7412/**
7413 * vlv_force_pll_on - forcibly enable just the PLL
7414 * @dev_priv: i915 private structure
7415 * @pipe: pipe PLL to enable
7416 * @dpll: PLL configuration
7417 *
7418 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7419 * in cases where we need the PLL enabled even when @pipe is not going to
7420 * be enabled.
7421 */
7422void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7423 const struct dpll *dpll)
7424{
7425 struct intel_crtc *crtc =
7426 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 7427 struct intel_crtc_state pipe_config = {
a93e255f 7428 .base.crtc = &crtc->base,
d288f65f
VS
7429 .pixel_multiplier = 1,
7430 .dpll = *dpll,
7431 };
7432
7433 if (IS_CHERRYVIEW(dev)) {
7434 chv_update_pll(crtc, &pipe_config);
7435 chv_prepare_pll(crtc, &pipe_config);
7436 chv_enable_pll(crtc, &pipe_config);
7437 } else {
7438 vlv_update_pll(crtc, &pipe_config);
7439 vlv_prepare_pll(crtc, &pipe_config);
7440 vlv_enable_pll(crtc, &pipe_config);
7441 }
7442}
7443
7444/**
7445 * vlv_force_pll_off - forcibly disable just the PLL
7446 * @dev_priv: i915 private structure
7447 * @pipe: pipe PLL to disable
7448 *
7449 * Disable the PLL for @pipe. To be used in cases where we need
7450 * the PLL enabled even when @pipe is not going to be enabled.
7451 */
7452void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7453{
7454 if (IS_CHERRYVIEW(dev))
7455 chv_disable_pll(to_i915(dev), pipe);
7456 else
7457 vlv_disable_pll(to_i915(dev), pipe);
7458}
7459
f47709a9 7460static void i9xx_update_pll(struct intel_crtc *crtc,
190f68c5 7461 struct intel_crtc_state *crtc_state,
f47709a9 7462 intel_clock_t *reduced_clock,
eb1cbe48
DV
7463 int num_connectors)
7464{
f47709a9 7465 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7466 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7467 u32 dpll;
7468 bool is_sdvo;
190f68c5 7469 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7470
190f68c5 7471 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7472
a93e255f
ACO
7473 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7474 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7475
7476 dpll = DPLL_VGA_MODE_DIS;
7477
a93e255f 7478 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7479 dpll |= DPLLB_MODE_LVDS;
7480 else
7481 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7482
ef1b460d 7483 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7484 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7485 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7486 }
198a037f
DV
7487
7488 if (is_sdvo)
4a33e48d 7489 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7490
190f68c5 7491 if (crtc_state->has_dp_encoder)
4a33e48d 7492 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7493
7494 /* compute bitmask from p1 value */
7495 if (IS_PINEVIEW(dev))
7496 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7497 else {
7498 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7499 if (IS_G4X(dev) && reduced_clock)
7500 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7501 }
7502 switch (clock->p2) {
7503 case 5:
7504 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7505 break;
7506 case 7:
7507 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7508 break;
7509 case 10:
7510 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7511 break;
7512 case 14:
7513 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7514 break;
7515 }
7516 if (INTEL_INFO(dev)->gen >= 4)
7517 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7518
190f68c5 7519 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7520 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7521 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7522 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7523 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7524 else
7525 dpll |= PLL_REF_INPUT_DREFCLK;
7526
7527 dpll |= DPLL_VCO_ENABLE;
190f68c5 7528 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7529
eb1cbe48 7530 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7531 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7532 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7533 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7534 }
7535}
7536
f47709a9 7537static void i8xx_update_pll(struct intel_crtc *crtc,
190f68c5 7538 struct intel_crtc_state *crtc_state,
f47709a9 7539 intel_clock_t *reduced_clock,
eb1cbe48
DV
7540 int num_connectors)
7541{
f47709a9 7542 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7543 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7544 u32 dpll;
190f68c5 7545 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7546
190f68c5 7547 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7548
eb1cbe48
DV
7549 dpll = DPLL_VGA_MODE_DIS;
7550
a93e255f 7551 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7552 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7553 } else {
7554 if (clock->p1 == 2)
7555 dpll |= PLL_P1_DIVIDE_BY_TWO;
7556 else
7557 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7558 if (clock->p2 == 4)
7559 dpll |= PLL_P2_DIVIDE_BY_4;
7560 }
7561
a93e255f 7562 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7563 dpll |= DPLL_DVO_2X_MODE;
7564
a93e255f 7565 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7566 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7567 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7568 else
7569 dpll |= PLL_REF_INPUT_DREFCLK;
7570
7571 dpll |= DPLL_VCO_ENABLE;
190f68c5 7572 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7573}
7574
8a654f3b 7575static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7576{
7577 struct drm_device *dev = intel_crtc->base.dev;
7578 struct drm_i915_private *dev_priv = dev->dev_private;
7579 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7580 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 7581 struct drm_display_mode *adjusted_mode =
6e3c9717 7582 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7583 uint32_t crtc_vtotal, crtc_vblank_end;
7584 int vsyncshift = 0;
4d8a62ea
DV
7585
7586 /* We need to be careful not to changed the adjusted mode, for otherwise
7587 * the hw state checker will get angry at the mismatch. */
7588 crtc_vtotal = adjusted_mode->crtc_vtotal;
7589 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7590
609aeaca 7591 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7592 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7593 crtc_vtotal -= 1;
7594 crtc_vblank_end -= 1;
609aeaca 7595
409ee761 7596 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7597 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7598 else
7599 vsyncshift = adjusted_mode->crtc_hsync_start -
7600 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7601 if (vsyncshift < 0)
7602 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7603 }
7604
7605 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7606 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7607
fe2b8f9d 7608 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7609 (adjusted_mode->crtc_hdisplay - 1) |
7610 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7611 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7612 (adjusted_mode->crtc_hblank_start - 1) |
7613 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7614 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7615 (adjusted_mode->crtc_hsync_start - 1) |
7616 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7617
fe2b8f9d 7618 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7619 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7620 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7621 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7622 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7623 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7624 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7625 (adjusted_mode->crtc_vsync_start - 1) |
7626 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7627
b5e508d4
PZ
7628 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7629 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7630 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7631 * bits. */
7632 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7633 (pipe == PIPE_B || pipe == PIPE_C))
7634 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7635
b0e77b9c
PZ
7636 /* pipesrc controls the size that is scaled from, which should
7637 * always be the user's requested size.
7638 */
7639 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7640 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7641 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7642}
7643
1bd1bd80 7644static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7645 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7646{
7647 struct drm_device *dev = crtc->base.dev;
7648 struct drm_i915_private *dev_priv = dev->dev_private;
7649 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7650 uint32_t tmp;
7651
7652 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7653 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7654 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7655 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7656 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7657 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7658 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7659 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7660 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7661
7662 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7663 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7664 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7665 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7666 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7667 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7668 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7669 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7670 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7671
7672 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7673 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7674 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7675 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7676 }
7677
7678 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7679 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7680 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7681
2d112de7
ACO
7682 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7683 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7684}
7685
f6a83288 7686void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7687 struct intel_crtc_state *pipe_config)
babea61d 7688{
2d112de7
ACO
7689 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7690 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7691 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7692 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7693
2d112de7
ACO
7694 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7695 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7696 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7697 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7698
2d112de7 7699 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 7700
2d112de7
ACO
7701 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7702 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
7703}
7704
84b046f3
DV
7705static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7706{
7707 struct drm_device *dev = intel_crtc->base.dev;
7708 struct drm_i915_private *dev_priv = dev->dev_private;
7709 uint32_t pipeconf;
7710
9f11a9e4 7711 pipeconf = 0;
84b046f3 7712
b6b5d049
VS
7713 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7714 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7715 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7716
6e3c9717 7717 if (intel_crtc->config->double_wide)
cf532bb2 7718 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7719
ff9ce46e
DV
7720 /* only g4x and later have fancy bpc/dither controls */
7721 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 7722 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7723 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7724 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7725 PIPECONF_DITHER_TYPE_SP;
84b046f3 7726
6e3c9717 7727 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7728 case 18:
7729 pipeconf |= PIPECONF_6BPC;
7730 break;
7731 case 24:
7732 pipeconf |= PIPECONF_8BPC;
7733 break;
7734 case 30:
7735 pipeconf |= PIPECONF_10BPC;
7736 break;
7737 default:
7738 /* Case prevented by intel_choose_pipe_bpp_dither. */
7739 BUG();
84b046f3
DV
7740 }
7741 }
7742
7743 if (HAS_PIPE_CXSR(dev)) {
7744 if (intel_crtc->lowfreq_avail) {
7745 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7746 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7747 } else {
7748 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7749 }
7750 }
7751
6e3c9717 7752 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7753 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7754 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7755 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7756 else
7757 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7758 } else
84b046f3
DV
7759 pipeconf |= PIPECONF_PROGRESSIVE;
7760
6e3c9717 7761 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 7762 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7763
84b046f3
DV
7764 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7765 POSTING_READ(PIPECONF(intel_crtc->pipe));
7766}
7767
190f68c5
ACO
7768static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7769 struct intel_crtc_state *crtc_state)
79e53945 7770{
c7653199 7771 struct drm_device *dev = crtc->base.dev;
79e53945 7772 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7773 int refclk, num_connectors = 0;
652c393a 7774 intel_clock_t clock, reduced_clock;
a16af721 7775 bool ok, has_reduced_clock = false;
e9fd1c02 7776 bool is_lvds = false, is_dsi = false;
5eddb70b 7777 struct intel_encoder *encoder;
d4906093 7778 const intel_limit_t *limit;
55bb9992 7779 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 7780 struct drm_connector *connector;
55bb9992
ACO
7781 struct drm_connector_state *connector_state;
7782 int i;
79e53945 7783
dd3cd74a
ACO
7784 memset(&crtc_state->dpll_hw_state, 0,
7785 sizeof(crtc_state->dpll_hw_state));
7786
da3ced29 7787 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
7788 if (connector_state->crtc != &crtc->base)
7789 continue;
7790
7791 encoder = to_intel_encoder(connector_state->best_encoder);
7792
5eddb70b 7793 switch (encoder->type) {
79e53945
JB
7794 case INTEL_OUTPUT_LVDS:
7795 is_lvds = true;
7796 break;
e9fd1c02
JN
7797 case INTEL_OUTPUT_DSI:
7798 is_dsi = true;
7799 break;
6847d71b
PZ
7800 default:
7801 break;
79e53945 7802 }
43565a06 7803
c751ce4f 7804 num_connectors++;
79e53945
JB
7805 }
7806
f2335330 7807 if (is_dsi)
5b18e57c 7808 return 0;
f2335330 7809
190f68c5 7810 if (!crtc_state->clock_set) {
a93e255f 7811 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7812
e9fd1c02
JN
7813 /*
7814 * Returns a set of divisors for the desired target clock with
7815 * the given refclk, or FALSE. The returned values represent
7816 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7817 * 2) / p1 / p2.
7818 */
a93e255f
ACO
7819 limit = intel_limit(crtc_state, refclk);
7820 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7821 crtc_state->port_clock,
e9fd1c02 7822 refclk, NULL, &clock);
f2335330 7823 if (!ok) {
e9fd1c02
JN
7824 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7825 return -EINVAL;
7826 }
79e53945 7827
f2335330
JN
7828 if (is_lvds && dev_priv->lvds_downclock_avail) {
7829 /*
7830 * Ensure we match the reduced clock's P to the target
7831 * clock. If the clocks don't match, we can't switch
7832 * the display clock by using the FP0/FP1. In such case
7833 * we will disable the LVDS downclock feature.
7834 */
7835 has_reduced_clock =
a93e255f 7836 dev_priv->display.find_dpll(limit, crtc_state,
f2335330
JN
7837 dev_priv->lvds_downclock,
7838 refclk, &clock,
7839 &reduced_clock);
7840 }
7841 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7842 crtc_state->dpll.n = clock.n;
7843 crtc_state->dpll.m1 = clock.m1;
7844 crtc_state->dpll.m2 = clock.m2;
7845 crtc_state->dpll.p1 = clock.p1;
7846 crtc_state->dpll.p2 = clock.p2;
f47709a9 7847 }
7026d4ac 7848
e9fd1c02 7849 if (IS_GEN2(dev)) {
190f68c5 7850 i8xx_update_pll(crtc, crtc_state,
2a8f64ca
VP
7851 has_reduced_clock ? &reduced_clock : NULL,
7852 num_connectors);
9d556c99 7853 } else if (IS_CHERRYVIEW(dev)) {
190f68c5 7854 chv_update_pll(crtc, crtc_state);
e9fd1c02 7855 } else if (IS_VALLEYVIEW(dev)) {
190f68c5 7856 vlv_update_pll(crtc, crtc_state);
e9fd1c02 7857 } else {
190f68c5 7858 i9xx_update_pll(crtc, crtc_state,
eb1cbe48 7859 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 7860 num_connectors);
e9fd1c02 7861 }
79e53945 7862
c8f7a0db 7863 return 0;
f564048e
EA
7864}
7865
2fa2fe9a 7866static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7867 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7868{
7869 struct drm_device *dev = crtc->base.dev;
7870 struct drm_i915_private *dev_priv = dev->dev_private;
7871 uint32_t tmp;
7872
dc9e7dec
VS
7873 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7874 return;
7875
2fa2fe9a 7876 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7877 if (!(tmp & PFIT_ENABLE))
7878 return;
2fa2fe9a 7879
06922821 7880 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7881 if (INTEL_INFO(dev)->gen < 4) {
7882 if (crtc->pipe != PIPE_B)
7883 return;
2fa2fe9a
DV
7884 } else {
7885 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7886 return;
7887 }
7888
06922821 7889 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7890 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7891 if (INTEL_INFO(dev)->gen < 5)
7892 pipe_config->gmch_pfit.lvds_border_bits =
7893 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7894}
7895
acbec814 7896static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7897 struct intel_crtc_state *pipe_config)
acbec814
JB
7898{
7899 struct drm_device *dev = crtc->base.dev;
7900 struct drm_i915_private *dev_priv = dev->dev_private;
7901 int pipe = pipe_config->cpu_transcoder;
7902 intel_clock_t clock;
7903 u32 mdiv;
662c6ecb 7904 int refclk = 100000;
acbec814 7905
f573de5a
SK
7906 /* In case of MIPI DPLL will not even be used */
7907 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7908 return;
7909
a580516d 7910 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7911 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7912 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7913
7914 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7915 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7916 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7917 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7918 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7919
f646628b 7920 vlv_clock(refclk, &clock);
acbec814 7921
f646628b
VS
7922 /* clock.dot is the fast clock */
7923 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
7924}
7925
5724dbd1
DL
7926static void
7927i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7928 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7929{
7930 struct drm_device *dev = crtc->base.dev;
7931 struct drm_i915_private *dev_priv = dev->dev_private;
7932 u32 val, base, offset;
7933 int pipe = crtc->pipe, plane = crtc->plane;
7934 int fourcc, pixel_format;
6761dd31 7935 unsigned int aligned_height;
b113d5ee 7936 struct drm_framebuffer *fb;
1b842c89 7937 struct intel_framebuffer *intel_fb;
1ad292b5 7938
42a7b088
DL
7939 val = I915_READ(DSPCNTR(plane));
7940 if (!(val & DISPLAY_PLANE_ENABLE))
7941 return;
7942
d9806c9f 7943 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7944 if (!intel_fb) {
1ad292b5
JB
7945 DRM_DEBUG_KMS("failed to alloc fb\n");
7946 return;
7947 }
7948
1b842c89
DL
7949 fb = &intel_fb->base;
7950
18c5247e
DV
7951 if (INTEL_INFO(dev)->gen >= 4) {
7952 if (val & DISPPLANE_TILED) {
49af449b 7953 plane_config->tiling = I915_TILING_X;
18c5247e
DV
7954 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7955 }
7956 }
1ad292b5
JB
7957
7958 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7959 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
7960 fb->pixel_format = fourcc;
7961 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
7962
7963 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 7964 if (plane_config->tiling)
1ad292b5
JB
7965 offset = I915_READ(DSPTILEOFF(plane));
7966 else
7967 offset = I915_READ(DSPLINOFF(plane));
7968 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7969 } else {
7970 base = I915_READ(DSPADDR(plane));
7971 }
7972 plane_config->base = base;
7973
7974 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7975 fb->width = ((val >> 16) & 0xfff) + 1;
7976 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7977
7978 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7979 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7980
b113d5ee 7981 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
7982 fb->pixel_format,
7983 fb->modifier[0]);
1ad292b5 7984
f37b5c2b 7985 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7986
2844a921
DL
7987 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7988 pipe_name(pipe), plane, fb->width, fb->height,
7989 fb->bits_per_pixel, base, fb->pitches[0],
7990 plane_config->size);
1ad292b5 7991
2d14030b 7992 plane_config->fb = intel_fb;
1ad292b5
JB
7993}
7994
70b23a98 7995static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7996 struct intel_crtc_state *pipe_config)
70b23a98
VS
7997{
7998 struct drm_device *dev = crtc->base.dev;
7999 struct drm_i915_private *dev_priv = dev->dev_private;
8000 int pipe = pipe_config->cpu_transcoder;
8001 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8002 intel_clock_t clock;
8003 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8004 int refclk = 100000;
8005
a580516d 8006 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8007 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8008 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8009 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8010 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
a580516d 8011 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8012
8013 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8014 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8015 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8016 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8017 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8018
8019 chv_clock(refclk, &clock);
8020
8021 /* clock.dot is the fast clock */
8022 pipe_config->port_clock = clock.dot / 5;
8023}
8024
0e8ffe1b 8025static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8026 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8027{
8028 struct drm_device *dev = crtc->base.dev;
8029 struct drm_i915_private *dev_priv = dev->dev_private;
8030 uint32_t tmp;
8031
f458ebbc
DV
8032 if (!intel_display_power_is_enabled(dev_priv,
8033 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
8034 return false;
8035
e143a21c 8036 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8037 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8038
0e8ffe1b
DV
8039 tmp = I915_READ(PIPECONF(crtc->pipe));
8040 if (!(tmp & PIPECONF_ENABLE))
8041 return false;
8042
42571aef
VS
8043 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8044 switch (tmp & PIPECONF_BPC_MASK) {
8045 case PIPECONF_6BPC:
8046 pipe_config->pipe_bpp = 18;
8047 break;
8048 case PIPECONF_8BPC:
8049 pipe_config->pipe_bpp = 24;
8050 break;
8051 case PIPECONF_10BPC:
8052 pipe_config->pipe_bpp = 30;
8053 break;
8054 default:
8055 break;
8056 }
8057 }
8058
b5a9fa09
DV
8059 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8060 pipe_config->limited_color_range = true;
8061
282740f7
VS
8062 if (INTEL_INFO(dev)->gen < 4)
8063 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8064
1bd1bd80
DV
8065 intel_get_pipe_timings(crtc, pipe_config);
8066
2fa2fe9a
DV
8067 i9xx_get_pfit_config(crtc, pipe_config);
8068
6c49f241
DV
8069 if (INTEL_INFO(dev)->gen >= 4) {
8070 tmp = I915_READ(DPLL_MD(crtc->pipe));
8071 pipe_config->pixel_multiplier =
8072 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8073 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8074 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8075 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8076 tmp = I915_READ(DPLL(crtc->pipe));
8077 pipe_config->pixel_multiplier =
8078 ((tmp & SDVO_MULTIPLIER_MASK)
8079 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8080 } else {
8081 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8082 * port and will be fixed up in the encoder->get_config
8083 * function. */
8084 pipe_config->pixel_multiplier = 1;
8085 }
8bcc2795
DV
8086 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8087 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
8088 /*
8089 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8090 * on 830. Filter it out here so that we don't
8091 * report errors due to that.
8092 */
8093 if (IS_I830(dev))
8094 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8095
8bcc2795
DV
8096 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8097 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8098 } else {
8099 /* Mask out read-only status bits. */
8100 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8101 DPLL_PORTC_READY_MASK |
8102 DPLL_PORTB_READY_MASK);
8bcc2795 8103 }
6c49f241 8104
70b23a98
VS
8105 if (IS_CHERRYVIEW(dev))
8106 chv_crtc_clock_get(crtc, pipe_config);
8107 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8108 vlv_crtc_clock_get(crtc, pipe_config);
8109 else
8110 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8111
0e8ffe1b
DV
8112 return true;
8113}
8114
dde86e2d 8115static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8116{
8117 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8118 struct intel_encoder *encoder;
74cfd7ac 8119 u32 val, final;
13d83a67 8120 bool has_lvds = false;
199e5d79 8121 bool has_cpu_edp = false;
199e5d79 8122 bool has_panel = false;
99eb6a01
KP
8123 bool has_ck505 = false;
8124 bool can_ssc = false;
13d83a67
JB
8125
8126 /* We need to take the global config into account */
b2784e15 8127 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8128 switch (encoder->type) {
8129 case INTEL_OUTPUT_LVDS:
8130 has_panel = true;
8131 has_lvds = true;
8132 break;
8133 case INTEL_OUTPUT_EDP:
8134 has_panel = true;
2de6905f 8135 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8136 has_cpu_edp = true;
8137 break;
6847d71b
PZ
8138 default:
8139 break;
13d83a67
JB
8140 }
8141 }
8142
99eb6a01 8143 if (HAS_PCH_IBX(dev)) {
41aa3448 8144 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8145 can_ssc = has_ck505;
8146 } else {
8147 has_ck505 = false;
8148 can_ssc = true;
8149 }
8150
2de6905f
ID
8151 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8152 has_panel, has_lvds, has_ck505);
13d83a67
JB
8153
8154 /* Ironlake: try to setup display ref clock before DPLL
8155 * enabling. This is only under driver's control after
8156 * PCH B stepping, previous chipset stepping should be
8157 * ignoring this setting.
8158 */
74cfd7ac
CW
8159 val = I915_READ(PCH_DREF_CONTROL);
8160
8161 /* As we must carefully and slowly disable/enable each source in turn,
8162 * compute the final state we want first and check if we need to
8163 * make any changes at all.
8164 */
8165 final = val;
8166 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8167 if (has_ck505)
8168 final |= DREF_NONSPREAD_CK505_ENABLE;
8169 else
8170 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8171
8172 final &= ~DREF_SSC_SOURCE_MASK;
8173 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8174 final &= ~DREF_SSC1_ENABLE;
8175
8176 if (has_panel) {
8177 final |= DREF_SSC_SOURCE_ENABLE;
8178
8179 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8180 final |= DREF_SSC1_ENABLE;
8181
8182 if (has_cpu_edp) {
8183 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8184 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8185 else
8186 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8187 } else
8188 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8189 } else {
8190 final |= DREF_SSC_SOURCE_DISABLE;
8191 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8192 }
8193
8194 if (final == val)
8195 return;
8196
13d83a67 8197 /* Always enable nonspread source */
74cfd7ac 8198 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8199
99eb6a01 8200 if (has_ck505)
74cfd7ac 8201 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8202 else
74cfd7ac 8203 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8204
199e5d79 8205 if (has_panel) {
74cfd7ac
CW
8206 val &= ~DREF_SSC_SOURCE_MASK;
8207 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8208
199e5d79 8209 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8210 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8211 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8212 val |= DREF_SSC1_ENABLE;
e77166b5 8213 } else
74cfd7ac 8214 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8215
8216 /* Get SSC going before enabling the outputs */
74cfd7ac 8217 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8218 POSTING_READ(PCH_DREF_CONTROL);
8219 udelay(200);
8220
74cfd7ac 8221 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8222
8223 /* Enable CPU source on CPU attached eDP */
199e5d79 8224 if (has_cpu_edp) {
99eb6a01 8225 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8226 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8227 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8228 } else
74cfd7ac 8229 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8230 } else
74cfd7ac 8231 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8232
74cfd7ac 8233 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8234 POSTING_READ(PCH_DREF_CONTROL);
8235 udelay(200);
8236 } else {
8237 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8238
74cfd7ac 8239 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8240
8241 /* Turn off CPU output */
74cfd7ac 8242 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8243
74cfd7ac 8244 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8245 POSTING_READ(PCH_DREF_CONTROL);
8246 udelay(200);
8247
8248 /* Turn off the SSC source */
74cfd7ac
CW
8249 val &= ~DREF_SSC_SOURCE_MASK;
8250 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8251
8252 /* Turn off SSC1 */
74cfd7ac 8253 val &= ~DREF_SSC1_ENABLE;
199e5d79 8254
74cfd7ac 8255 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8256 POSTING_READ(PCH_DREF_CONTROL);
8257 udelay(200);
8258 }
74cfd7ac
CW
8259
8260 BUG_ON(val != final);
13d83a67
JB
8261}
8262
f31f2d55 8263static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8264{
f31f2d55 8265 uint32_t tmp;
dde86e2d 8266
0ff066a9
PZ
8267 tmp = I915_READ(SOUTH_CHICKEN2);
8268 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8269 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8270
0ff066a9
PZ
8271 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8272 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8273 DRM_ERROR("FDI mPHY reset assert timeout\n");
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) == 0, 100))
8281 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8282}
8283
8284/* WaMPhyProgramming:hsw */
8285static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8286{
8287 uint32_t tmp;
dde86e2d
PZ
8288
8289 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8290 tmp &= ~(0xFF << 24);
8291 tmp |= (0x12 << 24);
8292 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8293
dde86e2d
PZ
8294 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8295 tmp |= (1 << 11);
8296 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8297
8298 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8299 tmp |= (1 << 11);
8300 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8301
dde86e2d
PZ
8302 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8303 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8304 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8305
8306 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8307 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8308 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8309
0ff066a9
PZ
8310 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8311 tmp &= ~(7 << 13);
8312 tmp |= (5 << 13);
8313 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8314
0ff066a9
PZ
8315 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8316 tmp &= ~(7 << 13);
8317 tmp |= (5 << 13);
8318 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8319
8320 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8321 tmp &= ~0xFF;
8322 tmp |= 0x1C;
8323 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8324
8325 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8326 tmp &= ~0xFF;
8327 tmp |= 0x1C;
8328 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8329
8330 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8331 tmp &= ~(0xFF << 16);
8332 tmp |= (0x1C << 16);
8333 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8334
8335 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8336 tmp &= ~(0xFF << 16);
8337 tmp |= (0x1C << 16);
8338 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8339
0ff066a9
PZ
8340 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8341 tmp |= (1 << 27);
8342 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8343
0ff066a9
PZ
8344 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8345 tmp |= (1 << 27);
8346 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8347
0ff066a9
PZ
8348 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8349 tmp &= ~(0xF << 28);
8350 tmp |= (4 << 28);
8351 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8352
0ff066a9
PZ
8353 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8354 tmp &= ~(0xF << 28);
8355 tmp |= (4 << 28);
8356 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8357}
8358
2fa86a1f
PZ
8359/* Implements 3 different sequences from BSpec chapter "Display iCLK
8360 * Programming" based on the parameters passed:
8361 * - Sequence to enable CLKOUT_DP
8362 * - Sequence to enable CLKOUT_DP without spread
8363 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8364 */
8365static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8366 bool with_fdi)
f31f2d55
PZ
8367{
8368 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8369 uint32_t reg, tmp;
8370
8371 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8372 with_spread = true;
8373 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8374 with_fdi, "LP PCH doesn't have FDI\n"))
8375 with_fdi = false;
f31f2d55 8376
a580516d 8377 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8378
8379 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8380 tmp &= ~SBI_SSCCTL_DISABLE;
8381 tmp |= SBI_SSCCTL_PATHALT;
8382 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8383
8384 udelay(24);
8385
2fa86a1f
PZ
8386 if (with_spread) {
8387 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8388 tmp &= ~SBI_SSCCTL_PATHALT;
8389 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8390
2fa86a1f
PZ
8391 if (with_fdi) {
8392 lpt_reset_fdi_mphy(dev_priv);
8393 lpt_program_fdi_mphy(dev_priv);
8394 }
8395 }
dde86e2d 8396
2fa86a1f
PZ
8397 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8398 SBI_GEN0 : SBI_DBUFF0;
8399 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8400 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8401 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8402
a580516d 8403 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8404}
8405
47701c3b
PZ
8406/* Sequence to disable CLKOUT_DP */
8407static void lpt_disable_clkout_dp(struct drm_device *dev)
8408{
8409 struct drm_i915_private *dev_priv = dev->dev_private;
8410 uint32_t reg, tmp;
8411
a580516d 8412 mutex_lock(&dev_priv->sb_lock);
47701c3b
PZ
8413
8414 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8415 SBI_GEN0 : SBI_DBUFF0;
8416 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8417 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8418 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8419
8420 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8421 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8422 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8423 tmp |= SBI_SSCCTL_PATHALT;
8424 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8425 udelay(32);
8426 }
8427 tmp |= SBI_SSCCTL_DISABLE;
8428 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8429 }
8430
a580516d 8431 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8432}
8433
bf8fa3d3
PZ
8434static void lpt_init_pch_refclk(struct drm_device *dev)
8435{
bf8fa3d3
PZ
8436 struct intel_encoder *encoder;
8437 bool has_vga = false;
8438
b2784e15 8439 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8440 switch (encoder->type) {
8441 case INTEL_OUTPUT_ANALOG:
8442 has_vga = true;
8443 break;
6847d71b
PZ
8444 default:
8445 break;
bf8fa3d3
PZ
8446 }
8447 }
8448
47701c3b
PZ
8449 if (has_vga)
8450 lpt_enable_clkout_dp(dev, true, true);
8451 else
8452 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
8453}
8454
dde86e2d
PZ
8455/*
8456 * Initialize reference clocks when the driver loads
8457 */
8458void intel_init_pch_refclk(struct drm_device *dev)
8459{
8460 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8461 ironlake_init_pch_refclk(dev);
8462 else if (HAS_PCH_LPT(dev))
8463 lpt_init_pch_refclk(dev);
8464}
8465
55bb9992 8466static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8467{
55bb9992 8468 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8469 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8470 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8471 struct drm_connector *connector;
55bb9992 8472 struct drm_connector_state *connector_state;
d9d444cb 8473 struct intel_encoder *encoder;
55bb9992 8474 int num_connectors = 0, i;
d9d444cb
JB
8475 bool is_lvds = false;
8476
da3ced29 8477 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8478 if (connector_state->crtc != crtc_state->base.crtc)
8479 continue;
8480
8481 encoder = to_intel_encoder(connector_state->best_encoder);
8482
d9d444cb
JB
8483 switch (encoder->type) {
8484 case INTEL_OUTPUT_LVDS:
8485 is_lvds = true;
8486 break;
6847d71b
PZ
8487 default:
8488 break;
d9d444cb
JB
8489 }
8490 num_connectors++;
8491 }
8492
8493 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8494 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8495 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8496 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8497 }
8498
8499 return 120000;
8500}
8501
6ff93609 8502static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8503{
c8203565 8504 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8505 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8506 int pipe = intel_crtc->pipe;
c8203565
PZ
8507 uint32_t val;
8508
78114071 8509 val = 0;
c8203565 8510
6e3c9717 8511 switch (intel_crtc->config->pipe_bpp) {
c8203565 8512 case 18:
dfd07d72 8513 val |= PIPECONF_6BPC;
c8203565
PZ
8514 break;
8515 case 24:
dfd07d72 8516 val |= PIPECONF_8BPC;
c8203565
PZ
8517 break;
8518 case 30:
dfd07d72 8519 val |= PIPECONF_10BPC;
c8203565
PZ
8520 break;
8521 case 36:
dfd07d72 8522 val |= PIPECONF_12BPC;
c8203565
PZ
8523 break;
8524 default:
cc769b62
PZ
8525 /* Case prevented by intel_choose_pipe_bpp_dither. */
8526 BUG();
c8203565
PZ
8527 }
8528
6e3c9717 8529 if (intel_crtc->config->dither)
c8203565
PZ
8530 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8531
6e3c9717 8532 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8533 val |= PIPECONF_INTERLACED_ILK;
8534 else
8535 val |= PIPECONF_PROGRESSIVE;
8536
6e3c9717 8537 if (intel_crtc->config->limited_color_range)
3685a8f3 8538 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8539
c8203565
PZ
8540 I915_WRITE(PIPECONF(pipe), val);
8541 POSTING_READ(PIPECONF(pipe));
8542}
8543
86d3efce
VS
8544/*
8545 * Set up the pipe CSC unit.
8546 *
8547 * Currently only full range RGB to limited range RGB conversion
8548 * is supported, but eventually this should handle various
8549 * RGB<->YCbCr scenarios as well.
8550 */
50f3b016 8551static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8552{
8553 struct drm_device *dev = crtc->dev;
8554 struct drm_i915_private *dev_priv = dev->dev_private;
8555 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8556 int pipe = intel_crtc->pipe;
8557 uint16_t coeff = 0x7800; /* 1.0 */
8558
8559 /*
8560 * TODO: Check what kind of values actually come out of the pipe
8561 * with these coeff/postoff values and adjust to get the best
8562 * accuracy. Perhaps we even need to take the bpc value into
8563 * consideration.
8564 */
8565
6e3c9717 8566 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8567 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8568
8569 /*
8570 * GY/GU and RY/RU should be the other way around according
8571 * to BSpec, but reality doesn't agree. Just set them up in
8572 * a way that results in the correct picture.
8573 */
8574 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8575 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8576
8577 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8578 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8579
8580 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8581 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8582
8583 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8584 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8585 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8586
8587 if (INTEL_INFO(dev)->gen > 6) {
8588 uint16_t postoff = 0;
8589
6e3c9717 8590 if (intel_crtc->config->limited_color_range)
32cf0cb0 8591 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8592
8593 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8594 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8595 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8596
8597 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8598 } else {
8599 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8600
6e3c9717 8601 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8602 mode |= CSC_BLACK_SCREEN_OFFSET;
8603
8604 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8605 }
8606}
8607
6ff93609 8608static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8609{
756f85cf
PZ
8610 struct drm_device *dev = crtc->dev;
8611 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 8612 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8613 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8614 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
8615 uint32_t val;
8616
3eff4faa 8617 val = 0;
ee2b0b38 8618
6e3c9717 8619 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
8620 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8621
6e3c9717 8622 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8623 val |= PIPECONF_INTERLACED_ILK;
8624 else
8625 val |= PIPECONF_PROGRESSIVE;
8626
702e7a56
PZ
8627 I915_WRITE(PIPECONF(cpu_transcoder), val);
8628 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
8629
8630 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8631 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 8632
3cdf122c 8633 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
8634 val = 0;
8635
6e3c9717 8636 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8637 case 18:
8638 val |= PIPEMISC_DITHER_6_BPC;
8639 break;
8640 case 24:
8641 val |= PIPEMISC_DITHER_8_BPC;
8642 break;
8643 case 30:
8644 val |= PIPEMISC_DITHER_10_BPC;
8645 break;
8646 case 36:
8647 val |= PIPEMISC_DITHER_12_BPC;
8648 break;
8649 default:
8650 /* Case prevented by pipe_config_set_bpp. */
8651 BUG();
8652 }
8653
6e3c9717 8654 if (intel_crtc->config->dither)
756f85cf
PZ
8655 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8656
8657 I915_WRITE(PIPEMISC(pipe), val);
8658 }
ee2b0b38
PZ
8659}
8660
6591c6e4 8661static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8662 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8663 intel_clock_t *clock,
8664 bool *has_reduced_clock,
8665 intel_clock_t *reduced_clock)
8666{
8667 struct drm_device *dev = crtc->dev;
8668 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8669 int refclk;
d4906093 8670 const intel_limit_t *limit;
a16af721 8671 bool ret, is_lvds = false;
79e53945 8672
a93e255f 8673 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
79e53945 8674
55bb9992 8675 refclk = ironlake_get_refclk(crtc_state);
79e53945 8676
d4906093
ML
8677 /*
8678 * Returns a set of divisors for the desired target clock with the given
8679 * refclk, or FALSE. The returned values represent the clock equation:
8680 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8681 */
a93e255f
ACO
8682 limit = intel_limit(crtc_state, refclk);
8683 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8684 crtc_state->port_clock,
ee9300bb 8685 refclk, NULL, clock);
6591c6e4
PZ
8686 if (!ret)
8687 return false;
cda4b7d3 8688
ddc9003c 8689 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
8690 /*
8691 * Ensure we match the reduced clock's P to the target clock.
8692 * If the clocks don't match, we can't switch the display clock
8693 * by using the FP0/FP1. In such case we will disable the LVDS
8694 * downclock feature.
8695 */
ee9300bb 8696 *has_reduced_clock =
a93e255f 8697 dev_priv->display.find_dpll(limit, crtc_state,
ee9300bb
DV
8698 dev_priv->lvds_downclock,
8699 refclk, clock,
8700 reduced_clock);
652c393a 8701 }
61e9653f 8702
6591c6e4
PZ
8703 return true;
8704}
8705
d4b1931c
PZ
8706int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8707{
8708 /*
8709 * Account for spread spectrum to avoid
8710 * oversubscribing the link. Max center spread
8711 * is 2.5%; use 5% for safety's sake.
8712 */
8713 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8714 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8715}
8716
7429e9d4 8717static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8718{
7429e9d4 8719 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8720}
8721
de13a2e3 8722static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8723 struct intel_crtc_state *crtc_state,
7429e9d4 8724 u32 *fp,
9a7c7890 8725 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8726{
de13a2e3 8727 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8728 struct drm_device *dev = crtc->dev;
8729 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8730 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8731 struct drm_connector *connector;
55bb9992
ACO
8732 struct drm_connector_state *connector_state;
8733 struct intel_encoder *encoder;
de13a2e3 8734 uint32_t dpll;
55bb9992 8735 int factor, num_connectors = 0, i;
09ede541 8736 bool is_lvds = false, is_sdvo = false;
79e53945 8737
da3ced29 8738 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8739 if (connector_state->crtc != crtc_state->base.crtc)
8740 continue;
8741
8742 encoder = to_intel_encoder(connector_state->best_encoder);
8743
8744 switch (encoder->type) {
79e53945
JB
8745 case INTEL_OUTPUT_LVDS:
8746 is_lvds = true;
8747 break;
8748 case INTEL_OUTPUT_SDVO:
7d57382e 8749 case INTEL_OUTPUT_HDMI:
79e53945 8750 is_sdvo = true;
79e53945 8751 break;
6847d71b
PZ
8752 default:
8753 break;
79e53945 8754 }
43565a06 8755
c751ce4f 8756 num_connectors++;
79e53945 8757 }
79e53945 8758
c1858123 8759 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8760 factor = 21;
8761 if (is_lvds) {
8762 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8763 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8764 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8765 factor = 25;
190f68c5 8766 } else if (crtc_state->sdvo_tv_clock)
8febb297 8767 factor = 20;
c1858123 8768
190f68c5 8769 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8770 *fp |= FP_CB_TUNE;
2c07245f 8771
9a7c7890
DV
8772 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8773 *fp2 |= FP_CB_TUNE;
8774
5eddb70b 8775 dpll = 0;
2c07245f 8776
a07d6787
EA
8777 if (is_lvds)
8778 dpll |= DPLLB_MODE_LVDS;
8779 else
8780 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8781
190f68c5 8782 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8783 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8784
8785 if (is_sdvo)
4a33e48d 8786 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8787 if (crtc_state->has_dp_encoder)
4a33e48d 8788 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8789
a07d6787 8790 /* compute bitmask from p1 value */
190f68c5 8791 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8792 /* also FPA1 */
190f68c5 8793 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8794
190f68c5 8795 switch (crtc_state->dpll.p2) {
a07d6787
EA
8796 case 5:
8797 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8798 break;
8799 case 7:
8800 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8801 break;
8802 case 10:
8803 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8804 break;
8805 case 14:
8806 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8807 break;
79e53945
JB
8808 }
8809
b4c09f3b 8810 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8811 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8812 else
8813 dpll |= PLL_REF_INPUT_DREFCLK;
8814
959e16d6 8815 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8816}
8817
190f68c5
ACO
8818static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8819 struct intel_crtc_state *crtc_state)
de13a2e3 8820{
c7653199 8821 struct drm_device *dev = crtc->base.dev;
de13a2e3 8822 intel_clock_t clock, reduced_clock;
cbbab5bd 8823 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8824 bool ok, has_reduced_clock = false;
8b47047b 8825 bool is_lvds = false;
e2b78267 8826 struct intel_shared_dpll *pll;
de13a2e3 8827
dd3cd74a
ACO
8828 memset(&crtc_state->dpll_hw_state, 0,
8829 sizeof(crtc_state->dpll_hw_state));
8830
409ee761 8831 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 8832
5dc5298b
PZ
8833 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8834 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8835
190f68c5 8836 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8837 &has_reduced_clock, &reduced_clock);
190f68c5 8838 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8839 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8840 return -EINVAL;
79e53945 8841 }
f47709a9 8842 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8843 if (!crtc_state->clock_set) {
8844 crtc_state->dpll.n = clock.n;
8845 crtc_state->dpll.m1 = clock.m1;
8846 crtc_state->dpll.m2 = clock.m2;
8847 crtc_state->dpll.p1 = clock.p1;
8848 crtc_state->dpll.p2 = clock.p2;
f47709a9 8849 }
79e53945 8850
5dc5298b 8851 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8852 if (crtc_state->has_pch_encoder) {
8853 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8854 if (has_reduced_clock)
7429e9d4 8855 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8856
190f68c5 8857 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8858 &fp, &reduced_clock,
8859 has_reduced_clock ? &fp2 : NULL);
8860
190f68c5
ACO
8861 crtc_state->dpll_hw_state.dpll = dpll;
8862 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8863 if (has_reduced_clock)
190f68c5 8864 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8865 else
190f68c5 8866 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8867
190f68c5 8868 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 8869 if (pll == NULL) {
84f44ce7 8870 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8871 pipe_name(crtc->pipe));
4b645f14
JB
8872 return -EINVAL;
8873 }
3fb37703 8874 }
79e53945 8875
ab585dea 8876 if (is_lvds && has_reduced_clock)
c7653199 8877 crtc->lowfreq_avail = true;
bcd644e0 8878 else
c7653199 8879 crtc->lowfreq_avail = false;
e2b78267 8880
c8f7a0db 8881 return 0;
79e53945
JB
8882}
8883
eb14cb74
VS
8884static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8885 struct intel_link_m_n *m_n)
8886{
8887 struct drm_device *dev = crtc->base.dev;
8888 struct drm_i915_private *dev_priv = dev->dev_private;
8889 enum pipe pipe = crtc->pipe;
8890
8891 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8892 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8893 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8894 & ~TU_SIZE_MASK;
8895 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8896 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8897 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8898}
8899
8900static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8901 enum transcoder transcoder,
b95af8be
VK
8902 struct intel_link_m_n *m_n,
8903 struct intel_link_m_n *m2_n2)
72419203
DV
8904{
8905 struct drm_device *dev = crtc->base.dev;
8906 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8907 enum pipe pipe = crtc->pipe;
72419203 8908
eb14cb74
VS
8909 if (INTEL_INFO(dev)->gen >= 5) {
8910 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8911 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8912 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8913 & ~TU_SIZE_MASK;
8914 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8915 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8916 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8917 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8918 * gen < 8) and if DRRS is supported (to make sure the
8919 * registers are not unnecessarily read).
8920 */
8921 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8922 crtc->config->has_drrs) {
b95af8be
VK
8923 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8924 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8925 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8926 & ~TU_SIZE_MASK;
8927 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8928 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8929 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8930 }
eb14cb74
VS
8931 } else {
8932 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8933 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8934 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8935 & ~TU_SIZE_MASK;
8936 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8937 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8938 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8939 }
8940}
8941
8942void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8943 struct intel_crtc_state *pipe_config)
eb14cb74 8944{
681a8504 8945 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8946 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8947 else
8948 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8949 &pipe_config->dp_m_n,
8950 &pipe_config->dp_m2_n2);
eb14cb74 8951}
72419203 8952
eb14cb74 8953static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8954 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8955{
8956 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8957 &pipe_config->fdi_m_n, NULL);
72419203
DV
8958}
8959
bd2e244f 8960static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8961 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8962{
8963 struct drm_device *dev = crtc->base.dev;
8964 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
8965 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8966 uint32_t ps_ctrl = 0;
8967 int id = -1;
8968 int i;
bd2e244f 8969
a1b2278e
CK
8970 /* find scaler attached to this pipe */
8971 for (i = 0; i < crtc->num_scalers; i++) {
8972 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8973 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8974 id = i;
8975 pipe_config->pch_pfit.enabled = true;
8976 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8977 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8978 break;
8979 }
8980 }
bd2e244f 8981
a1b2278e
CK
8982 scaler_state->scaler_id = id;
8983 if (id >= 0) {
8984 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8985 } else {
8986 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8987 }
8988}
8989
5724dbd1
DL
8990static void
8991skylake_get_initial_plane_config(struct intel_crtc *crtc,
8992 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8993{
8994 struct drm_device *dev = crtc->base.dev;
8995 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 8996 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8997 int pipe = crtc->pipe;
8998 int fourcc, pixel_format;
6761dd31 8999 unsigned int aligned_height;
bc8d7dff 9000 struct drm_framebuffer *fb;
1b842c89 9001 struct intel_framebuffer *intel_fb;
bc8d7dff 9002
d9806c9f 9003 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9004 if (!intel_fb) {
bc8d7dff
DL
9005 DRM_DEBUG_KMS("failed to alloc fb\n");
9006 return;
9007 }
9008
1b842c89
DL
9009 fb = &intel_fb->base;
9010
bc8d7dff 9011 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9012 if (!(val & PLANE_CTL_ENABLE))
9013 goto error;
9014
bc8d7dff
DL
9015 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9016 fourcc = skl_format_to_fourcc(pixel_format,
9017 val & PLANE_CTL_ORDER_RGBX,
9018 val & PLANE_CTL_ALPHA_MASK);
9019 fb->pixel_format = fourcc;
9020 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9021
40f46283
DL
9022 tiling = val & PLANE_CTL_TILED_MASK;
9023 switch (tiling) {
9024 case PLANE_CTL_TILED_LINEAR:
9025 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9026 break;
9027 case PLANE_CTL_TILED_X:
9028 plane_config->tiling = I915_TILING_X;
9029 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9030 break;
9031 case PLANE_CTL_TILED_Y:
9032 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9033 break;
9034 case PLANE_CTL_TILED_YF:
9035 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9036 break;
9037 default:
9038 MISSING_CASE(tiling);
9039 goto error;
9040 }
9041
bc8d7dff
DL
9042 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9043 plane_config->base = base;
9044
9045 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9046
9047 val = I915_READ(PLANE_SIZE(pipe, 0));
9048 fb->height = ((val >> 16) & 0xfff) + 1;
9049 fb->width = ((val >> 0) & 0x1fff) + 1;
9050
9051 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
9052 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9053 fb->pixel_format);
bc8d7dff
DL
9054 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9055
9056 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9057 fb->pixel_format,
9058 fb->modifier[0]);
bc8d7dff 9059
f37b5c2b 9060 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9061
9062 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9063 pipe_name(pipe), fb->width, fb->height,
9064 fb->bits_per_pixel, base, fb->pitches[0],
9065 plane_config->size);
9066
2d14030b 9067 plane_config->fb = intel_fb;
bc8d7dff
DL
9068 return;
9069
9070error:
9071 kfree(fb);
9072}
9073
2fa2fe9a 9074static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9075 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9076{
9077 struct drm_device *dev = crtc->base.dev;
9078 struct drm_i915_private *dev_priv = dev->dev_private;
9079 uint32_t tmp;
9080
9081 tmp = I915_READ(PF_CTL(crtc->pipe));
9082
9083 if (tmp & PF_ENABLE) {
fd4daa9c 9084 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9085 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9086 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9087
9088 /* We currently do not free assignements of panel fitters on
9089 * ivb/hsw (since we don't use the higher upscaling modes which
9090 * differentiates them) so just WARN about this case for now. */
9091 if (IS_GEN7(dev)) {
9092 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9093 PF_PIPE_SEL_IVB(crtc->pipe));
9094 }
2fa2fe9a 9095 }
79e53945
JB
9096}
9097
5724dbd1
DL
9098static void
9099ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9100 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9101{
9102 struct drm_device *dev = crtc->base.dev;
9103 struct drm_i915_private *dev_priv = dev->dev_private;
9104 u32 val, base, offset;
aeee5a49 9105 int pipe = crtc->pipe;
4c6baa59 9106 int fourcc, pixel_format;
6761dd31 9107 unsigned int aligned_height;
b113d5ee 9108 struct drm_framebuffer *fb;
1b842c89 9109 struct intel_framebuffer *intel_fb;
4c6baa59 9110
42a7b088
DL
9111 val = I915_READ(DSPCNTR(pipe));
9112 if (!(val & DISPLAY_PLANE_ENABLE))
9113 return;
9114
d9806c9f 9115 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9116 if (!intel_fb) {
4c6baa59
JB
9117 DRM_DEBUG_KMS("failed to alloc fb\n");
9118 return;
9119 }
9120
1b842c89
DL
9121 fb = &intel_fb->base;
9122
18c5247e
DV
9123 if (INTEL_INFO(dev)->gen >= 4) {
9124 if (val & DISPPLANE_TILED) {
49af449b 9125 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9126 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9127 }
9128 }
4c6baa59
JB
9129
9130 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9131 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9132 fb->pixel_format = fourcc;
9133 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9134
aeee5a49 9135 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9136 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9137 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9138 } else {
49af449b 9139 if (plane_config->tiling)
aeee5a49 9140 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9141 else
aeee5a49 9142 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9143 }
9144 plane_config->base = base;
9145
9146 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9147 fb->width = ((val >> 16) & 0xfff) + 1;
9148 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9149
9150 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9151 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9152
b113d5ee 9153 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9154 fb->pixel_format,
9155 fb->modifier[0]);
4c6baa59 9156
f37b5c2b 9157 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9158
2844a921
DL
9159 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9160 pipe_name(pipe), fb->width, fb->height,
9161 fb->bits_per_pixel, base, fb->pitches[0],
9162 plane_config->size);
b113d5ee 9163
2d14030b 9164 plane_config->fb = intel_fb;
4c6baa59
JB
9165}
9166
0e8ffe1b 9167static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9168 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9169{
9170 struct drm_device *dev = crtc->base.dev;
9171 struct drm_i915_private *dev_priv = dev->dev_private;
9172 uint32_t tmp;
9173
f458ebbc
DV
9174 if (!intel_display_power_is_enabled(dev_priv,
9175 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
9176 return false;
9177
e143a21c 9178 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 9179 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 9180
0e8ffe1b
DV
9181 tmp = I915_READ(PIPECONF(crtc->pipe));
9182 if (!(tmp & PIPECONF_ENABLE))
9183 return false;
9184
42571aef
VS
9185 switch (tmp & PIPECONF_BPC_MASK) {
9186 case PIPECONF_6BPC:
9187 pipe_config->pipe_bpp = 18;
9188 break;
9189 case PIPECONF_8BPC:
9190 pipe_config->pipe_bpp = 24;
9191 break;
9192 case PIPECONF_10BPC:
9193 pipe_config->pipe_bpp = 30;
9194 break;
9195 case PIPECONF_12BPC:
9196 pipe_config->pipe_bpp = 36;
9197 break;
9198 default:
9199 break;
9200 }
9201
b5a9fa09
DV
9202 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9203 pipe_config->limited_color_range = true;
9204
ab9412ba 9205 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
9206 struct intel_shared_dpll *pll;
9207
88adfff1
DV
9208 pipe_config->has_pch_encoder = true;
9209
627eb5a3
DV
9210 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9211 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9212 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9213
9214 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9215
c0d43d62 9216 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
9217 pipe_config->shared_dpll =
9218 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9219 } else {
9220 tmp = I915_READ(PCH_DPLL_SEL);
9221 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9222 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9223 else
9224 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9225 }
66e985c0
DV
9226
9227 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9228
9229 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9230 &pipe_config->dpll_hw_state));
c93f54cf
DV
9231
9232 tmp = pipe_config->dpll_hw_state.dpll;
9233 pipe_config->pixel_multiplier =
9234 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9235 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9236
9237 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9238 } else {
9239 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9240 }
9241
1bd1bd80
DV
9242 intel_get_pipe_timings(crtc, pipe_config);
9243
2fa2fe9a
DV
9244 ironlake_get_pfit_config(crtc, pipe_config);
9245
0e8ffe1b
DV
9246 return true;
9247}
9248
be256dc7
PZ
9249static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9250{
9251 struct drm_device *dev = dev_priv->dev;
be256dc7 9252 struct intel_crtc *crtc;
be256dc7 9253
d3fcc808 9254 for_each_intel_crtc(dev, crtc)
e2c719b7 9255 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9256 pipe_name(crtc->pipe));
9257
e2c719b7
RC
9258 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9259 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9260 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9261 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9262 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9263 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9264 "CPU PWM1 enabled\n");
c5107b87 9265 if (IS_HASWELL(dev))
e2c719b7 9266 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9267 "CPU PWM2 enabled\n");
e2c719b7 9268 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9269 "PCH PWM1 enabled\n");
e2c719b7 9270 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9271 "Utility pin enabled\n");
e2c719b7 9272 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9273
9926ada1
PZ
9274 /*
9275 * In theory we can still leave IRQs enabled, as long as only the HPD
9276 * interrupts remain enabled. We used to check for that, but since it's
9277 * gen-specific and since we only disable LCPLL after we fully disable
9278 * the interrupts, the check below should be enough.
9279 */
e2c719b7 9280 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9281}
9282
9ccd5aeb
PZ
9283static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9284{
9285 struct drm_device *dev = dev_priv->dev;
9286
9287 if (IS_HASWELL(dev))
9288 return I915_READ(D_COMP_HSW);
9289 else
9290 return I915_READ(D_COMP_BDW);
9291}
9292
3c4c9b81
PZ
9293static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9294{
9295 struct drm_device *dev = dev_priv->dev;
9296
9297 if (IS_HASWELL(dev)) {
9298 mutex_lock(&dev_priv->rps.hw_lock);
9299 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9300 val))
f475dadf 9301 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9302 mutex_unlock(&dev_priv->rps.hw_lock);
9303 } else {
9ccd5aeb
PZ
9304 I915_WRITE(D_COMP_BDW, val);
9305 POSTING_READ(D_COMP_BDW);
3c4c9b81 9306 }
be256dc7
PZ
9307}
9308
9309/*
9310 * This function implements pieces of two sequences from BSpec:
9311 * - Sequence for display software to disable LCPLL
9312 * - Sequence for display software to allow package C8+
9313 * The steps implemented here are just the steps that actually touch the LCPLL
9314 * register. Callers should take care of disabling all the display engine
9315 * functions, doing the mode unset, fixing interrupts, etc.
9316 */
6ff58d53
PZ
9317static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9318 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9319{
9320 uint32_t val;
9321
9322 assert_can_disable_lcpll(dev_priv);
9323
9324 val = I915_READ(LCPLL_CTL);
9325
9326 if (switch_to_fclk) {
9327 val |= LCPLL_CD_SOURCE_FCLK;
9328 I915_WRITE(LCPLL_CTL, val);
9329
9330 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9331 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9332 DRM_ERROR("Switching to FCLK failed\n");
9333
9334 val = I915_READ(LCPLL_CTL);
9335 }
9336
9337 val |= LCPLL_PLL_DISABLE;
9338 I915_WRITE(LCPLL_CTL, val);
9339 POSTING_READ(LCPLL_CTL);
9340
9341 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9342 DRM_ERROR("LCPLL still locked\n");
9343
9ccd5aeb 9344 val = hsw_read_dcomp(dev_priv);
be256dc7 9345 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9346 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9347 ndelay(100);
9348
9ccd5aeb
PZ
9349 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9350 1))
be256dc7
PZ
9351 DRM_ERROR("D_COMP RCOMP still in progress\n");
9352
9353 if (allow_power_down) {
9354 val = I915_READ(LCPLL_CTL);
9355 val |= LCPLL_POWER_DOWN_ALLOW;
9356 I915_WRITE(LCPLL_CTL, val);
9357 POSTING_READ(LCPLL_CTL);
9358 }
9359}
9360
9361/*
9362 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9363 * source.
9364 */
6ff58d53 9365static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9366{
9367 uint32_t val;
9368
9369 val = I915_READ(LCPLL_CTL);
9370
9371 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9372 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9373 return;
9374
a8a8bd54
PZ
9375 /*
9376 * Make sure we're not on PC8 state before disabling PC8, otherwise
9377 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9378 */
59bad947 9379 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9380
be256dc7
PZ
9381 if (val & LCPLL_POWER_DOWN_ALLOW) {
9382 val &= ~LCPLL_POWER_DOWN_ALLOW;
9383 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9384 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9385 }
9386
9ccd5aeb 9387 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9388 val |= D_COMP_COMP_FORCE;
9389 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9390 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9391
9392 val = I915_READ(LCPLL_CTL);
9393 val &= ~LCPLL_PLL_DISABLE;
9394 I915_WRITE(LCPLL_CTL, val);
9395
9396 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9397 DRM_ERROR("LCPLL not locked yet\n");
9398
9399 if (val & LCPLL_CD_SOURCE_FCLK) {
9400 val = I915_READ(LCPLL_CTL);
9401 val &= ~LCPLL_CD_SOURCE_FCLK;
9402 I915_WRITE(LCPLL_CTL, val);
9403
9404 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9405 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9406 DRM_ERROR("Switching back to LCPLL failed\n");
9407 }
215733fa 9408
59bad947 9409 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9410 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9411}
9412
765dab67
PZ
9413/*
9414 * Package states C8 and deeper are really deep PC states that can only be
9415 * reached when all the devices on the system allow it, so even if the graphics
9416 * device allows PC8+, it doesn't mean the system will actually get to these
9417 * states. Our driver only allows PC8+ when going into runtime PM.
9418 *
9419 * The requirements for PC8+ are that all the outputs are disabled, the power
9420 * well is disabled and most interrupts are disabled, and these are also
9421 * requirements for runtime PM. When these conditions are met, we manually do
9422 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9423 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9424 * hang the machine.
9425 *
9426 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9427 * the state of some registers, so when we come back from PC8+ we need to
9428 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9429 * need to take care of the registers kept by RC6. Notice that this happens even
9430 * if we don't put the device in PCI D3 state (which is what currently happens
9431 * because of the runtime PM support).
9432 *
9433 * For more, read "Display Sequences for Package C8" on the hardware
9434 * documentation.
9435 */
a14cb6fc 9436void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9437{
c67a470b
PZ
9438 struct drm_device *dev = dev_priv->dev;
9439 uint32_t val;
9440
c67a470b
PZ
9441 DRM_DEBUG_KMS("Enabling package C8+\n");
9442
c67a470b
PZ
9443 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9444 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9445 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9446 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9447 }
9448
9449 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9450 hsw_disable_lcpll(dev_priv, true, true);
9451}
9452
a14cb6fc 9453void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9454{
9455 struct drm_device *dev = dev_priv->dev;
9456 uint32_t val;
9457
c67a470b
PZ
9458 DRM_DEBUG_KMS("Disabling package C8+\n");
9459
9460 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9461 lpt_init_pch_refclk(dev);
9462
9463 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9464 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9465 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9466 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9467 }
9468
9469 intel_prepare_ddi(dev);
c67a470b
PZ
9470}
9471
27c329ed 9472static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9473{
a821fc46 9474 struct drm_device *dev = old_state->dev;
27c329ed 9475 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
f8437dd1 9476
27c329ed 9477 broxton_set_cdclk(dev, req_cdclk);
f8437dd1
VK
9478}
9479
b432e5cf 9480/* compute the max rate for new configuration */
27c329ed 9481static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9482{
b432e5cf 9483 struct intel_crtc *intel_crtc;
27c329ed 9484 struct intel_crtc_state *crtc_state;
b432e5cf 9485 int max_pixel_rate = 0;
b432e5cf 9486
27c329ed
ML
9487 for_each_intel_crtc(state->dev, intel_crtc) {
9488 int pixel_rate;
9489
9490 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9491 if (IS_ERR(crtc_state))
9492 return PTR_ERR(crtc_state);
9493
9494 if (!crtc_state->base.enable)
b432e5cf
VS
9495 continue;
9496
27c329ed 9497 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9498
9499 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
27c329ed 9500 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
b432e5cf
VS
9501 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9502
9503 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9504 }
9505
9506 return max_pixel_rate;
9507}
9508
9509static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9510{
9511 struct drm_i915_private *dev_priv = dev->dev_private;
9512 uint32_t val, data;
9513 int ret;
9514
9515 if (WARN((I915_READ(LCPLL_CTL) &
9516 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9517 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9518 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9519 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9520 "trying to change cdclk frequency with cdclk not enabled\n"))
9521 return;
9522
9523 mutex_lock(&dev_priv->rps.hw_lock);
9524 ret = sandybridge_pcode_write(dev_priv,
9525 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9526 mutex_unlock(&dev_priv->rps.hw_lock);
9527 if (ret) {
9528 DRM_ERROR("failed to inform pcode about cdclk change\n");
9529 return;
9530 }
9531
9532 val = I915_READ(LCPLL_CTL);
9533 val |= LCPLL_CD_SOURCE_FCLK;
9534 I915_WRITE(LCPLL_CTL, val);
9535
9536 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9537 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9538 DRM_ERROR("Switching to FCLK failed\n");
9539
9540 val = I915_READ(LCPLL_CTL);
9541 val &= ~LCPLL_CLK_FREQ_MASK;
9542
9543 switch (cdclk) {
9544 case 450000:
9545 val |= LCPLL_CLK_FREQ_450;
9546 data = 0;
9547 break;
9548 case 540000:
9549 val |= LCPLL_CLK_FREQ_54O_BDW;
9550 data = 1;
9551 break;
9552 case 337500:
9553 val |= LCPLL_CLK_FREQ_337_5_BDW;
9554 data = 2;
9555 break;
9556 case 675000:
9557 val |= LCPLL_CLK_FREQ_675_BDW;
9558 data = 3;
9559 break;
9560 default:
9561 WARN(1, "invalid cdclk frequency\n");
9562 return;
9563 }
9564
9565 I915_WRITE(LCPLL_CTL, val);
9566
9567 val = I915_READ(LCPLL_CTL);
9568 val &= ~LCPLL_CD_SOURCE_FCLK;
9569 I915_WRITE(LCPLL_CTL, val);
9570
9571 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9572 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9573 DRM_ERROR("Switching back to LCPLL failed\n");
9574
9575 mutex_lock(&dev_priv->rps.hw_lock);
9576 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9577 mutex_unlock(&dev_priv->rps.hw_lock);
9578
9579 intel_update_cdclk(dev);
9580
9581 WARN(cdclk != dev_priv->cdclk_freq,
9582 "cdclk requested %d kHz but got %d kHz\n",
9583 cdclk, dev_priv->cdclk_freq);
9584}
9585
27c329ed 9586static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9587{
27c329ed
ML
9588 struct drm_i915_private *dev_priv = to_i915(state->dev);
9589 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9590 int cdclk;
9591
9592 /*
9593 * FIXME should also account for plane ratio
9594 * once 64bpp pixel formats are supported.
9595 */
27c329ed 9596 if (max_pixclk > 540000)
b432e5cf 9597 cdclk = 675000;
27c329ed 9598 else if (max_pixclk > 450000)
b432e5cf 9599 cdclk = 540000;
27c329ed 9600 else if (max_pixclk > 337500)
b432e5cf
VS
9601 cdclk = 450000;
9602 else
9603 cdclk = 337500;
9604
9605 /*
9606 * FIXME move the cdclk caclulation to
9607 * compute_config() so we can fail gracegully.
9608 */
9609 if (cdclk > dev_priv->max_cdclk_freq) {
9610 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9611 cdclk, dev_priv->max_cdclk_freq);
9612 cdclk = dev_priv->max_cdclk_freq;
9613 }
9614
27c329ed 9615 to_intel_atomic_state(state)->cdclk = cdclk;
b432e5cf
VS
9616
9617 return 0;
9618}
9619
27c329ed 9620static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9621{
27c329ed
ML
9622 struct drm_device *dev = old_state->dev;
9623 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
b432e5cf 9624
27c329ed 9625 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9626}
9627
190f68c5
ACO
9628static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9629 struct intel_crtc_state *crtc_state)
09b4ddf9 9630{
190f68c5 9631 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 9632 return -EINVAL;
716c2e55 9633
c7653199 9634 crtc->lowfreq_avail = false;
644cef34 9635
c8f7a0db 9636 return 0;
79e53945
JB
9637}
9638
3760b59c
S
9639static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9640 enum port port,
9641 struct intel_crtc_state *pipe_config)
9642{
9643 switch (port) {
9644 case PORT_A:
9645 pipe_config->ddi_pll_sel = SKL_DPLL0;
9646 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9647 break;
9648 case PORT_B:
9649 pipe_config->ddi_pll_sel = SKL_DPLL1;
9650 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9651 break;
9652 case PORT_C:
9653 pipe_config->ddi_pll_sel = SKL_DPLL2;
9654 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9655 break;
9656 default:
9657 DRM_ERROR("Incorrect port type\n");
9658 }
9659}
9660
96b7dfb7
S
9661static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9662 enum port port,
5cec258b 9663 struct intel_crtc_state *pipe_config)
96b7dfb7 9664{
3148ade7 9665 u32 temp, dpll_ctl1;
96b7dfb7
S
9666
9667 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9668 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9669
9670 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
9671 case SKL_DPLL0:
9672 /*
9673 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9674 * of the shared DPLL framework and thus needs to be read out
9675 * separately
9676 */
9677 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9678 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9679 break;
96b7dfb7
S
9680 case SKL_DPLL1:
9681 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9682 break;
9683 case SKL_DPLL2:
9684 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9685 break;
9686 case SKL_DPLL3:
9687 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9688 break;
96b7dfb7
S
9689 }
9690}
9691
7d2c8175
DL
9692static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9693 enum port port,
5cec258b 9694 struct intel_crtc_state *pipe_config)
7d2c8175
DL
9695{
9696 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9697
9698 switch (pipe_config->ddi_pll_sel) {
9699 case PORT_CLK_SEL_WRPLL1:
9700 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9701 break;
9702 case PORT_CLK_SEL_WRPLL2:
9703 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9704 break;
9705 }
9706}
9707
26804afd 9708static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9709 struct intel_crtc_state *pipe_config)
26804afd
DV
9710{
9711 struct drm_device *dev = crtc->base.dev;
9712 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9713 struct intel_shared_dpll *pll;
26804afd
DV
9714 enum port port;
9715 uint32_t tmp;
9716
9717 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9718
9719 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9720
96b7dfb7
S
9721 if (IS_SKYLAKE(dev))
9722 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9723 else if (IS_BROXTON(dev))
9724 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9725 else
9726 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9727
d452c5b6
DV
9728 if (pipe_config->shared_dpll >= 0) {
9729 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9730
9731 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9732 &pipe_config->dpll_hw_state));
9733 }
9734
26804afd
DV
9735 /*
9736 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9737 * DDI E. So just check whether this pipe is wired to DDI E and whether
9738 * the PCH transcoder is on.
9739 */
ca370455
DL
9740 if (INTEL_INFO(dev)->gen < 9 &&
9741 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9742 pipe_config->has_pch_encoder = true;
9743
9744 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9745 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9746 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9747
9748 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9749 }
9750}
9751
0e8ffe1b 9752static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9753 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9754{
9755 struct drm_device *dev = crtc->base.dev;
9756 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 9757 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
9758 uint32_t tmp;
9759
f458ebbc 9760 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
9761 POWER_DOMAIN_PIPE(crtc->pipe)))
9762 return false;
9763
e143a21c 9764 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
9765 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9766
eccb140b
DV
9767 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9768 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9769 enum pipe trans_edp_pipe;
9770 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9771 default:
9772 WARN(1, "unknown pipe linked to edp transcoder\n");
9773 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9774 case TRANS_DDI_EDP_INPUT_A_ON:
9775 trans_edp_pipe = PIPE_A;
9776 break;
9777 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9778 trans_edp_pipe = PIPE_B;
9779 break;
9780 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9781 trans_edp_pipe = PIPE_C;
9782 break;
9783 }
9784
9785 if (trans_edp_pipe == crtc->pipe)
9786 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9787 }
9788
f458ebbc 9789 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 9790 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
9791 return false;
9792
eccb140b 9793 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
9794 if (!(tmp & PIPECONF_ENABLE))
9795 return false;
9796
26804afd 9797 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9798
1bd1bd80
DV
9799 intel_get_pipe_timings(crtc, pipe_config);
9800
a1b2278e
CK
9801 if (INTEL_INFO(dev)->gen >= 9) {
9802 skl_init_scalers(dev, crtc, pipe_config);
9803 }
9804
2fa2fe9a 9805 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
af99ceda
CK
9806
9807 if (INTEL_INFO(dev)->gen >= 9) {
9808 pipe_config->scaler_state.scaler_id = -1;
9809 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9810 }
9811
bd2e244f 9812 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
ff6d9f55 9813 if (INTEL_INFO(dev)->gen == 9)
bd2e244f 9814 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9815 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 9816 ironlake_get_pfit_config(crtc, pipe_config);
ff6d9f55
JB
9817 else
9818 MISSING_CASE(INTEL_INFO(dev)->gen);
bd2e244f 9819 }
88adfff1 9820
e59150dc
JB
9821 if (IS_HASWELL(dev))
9822 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9823 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9824
ebb69c95
CT
9825 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9826 pipe_config->pixel_multiplier =
9827 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9828 } else {
9829 pipe_config->pixel_multiplier = 1;
9830 }
6c49f241 9831
0e8ffe1b
DV
9832 return true;
9833}
9834
560b85bb
CW
9835static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9836{
9837 struct drm_device *dev = crtc->dev;
9838 struct drm_i915_private *dev_priv = dev->dev_private;
9839 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9840 uint32_t cntl = 0, size = 0;
560b85bb 9841
dc41c154 9842 if (base) {
3dd512fb
MR
9843 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9844 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
9845 unsigned int stride = roundup_pow_of_two(width) * 4;
9846
9847 switch (stride) {
9848 default:
9849 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9850 width, stride);
9851 stride = 256;
9852 /* fallthrough */
9853 case 256:
9854 case 512:
9855 case 1024:
9856 case 2048:
9857 break;
4b0e333e
CW
9858 }
9859
dc41c154
VS
9860 cntl |= CURSOR_ENABLE |
9861 CURSOR_GAMMA_ENABLE |
9862 CURSOR_FORMAT_ARGB |
9863 CURSOR_STRIDE(stride);
9864
9865 size = (height << 12) | width;
4b0e333e 9866 }
560b85bb 9867
dc41c154
VS
9868 if (intel_crtc->cursor_cntl != 0 &&
9869 (intel_crtc->cursor_base != base ||
9870 intel_crtc->cursor_size != size ||
9871 intel_crtc->cursor_cntl != cntl)) {
9872 /* On these chipsets we can only modify the base/size/stride
9873 * whilst the cursor is disabled.
9874 */
9875 I915_WRITE(_CURACNTR, 0);
4b0e333e 9876 POSTING_READ(_CURACNTR);
dc41c154 9877 intel_crtc->cursor_cntl = 0;
4b0e333e 9878 }
560b85bb 9879
99d1f387 9880 if (intel_crtc->cursor_base != base) {
9db4a9c7 9881 I915_WRITE(_CURABASE, base);
99d1f387
VS
9882 intel_crtc->cursor_base = base;
9883 }
4726e0b0 9884
dc41c154
VS
9885 if (intel_crtc->cursor_size != size) {
9886 I915_WRITE(CURSIZE, size);
9887 intel_crtc->cursor_size = size;
4b0e333e 9888 }
560b85bb 9889
4b0e333e 9890 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
9891 I915_WRITE(_CURACNTR, cntl);
9892 POSTING_READ(_CURACNTR);
4b0e333e 9893 intel_crtc->cursor_cntl = cntl;
560b85bb 9894 }
560b85bb
CW
9895}
9896
560b85bb 9897static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
9898{
9899 struct drm_device *dev = crtc->dev;
9900 struct drm_i915_private *dev_priv = dev->dev_private;
9901 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9902 int pipe = intel_crtc->pipe;
4b0e333e
CW
9903 uint32_t cntl;
9904
9905 cntl = 0;
9906 if (base) {
9907 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 9908 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
9909 case 64:
9910 cntl |= CURSOR_MODE_64_ARGB_AX;
9911 break;
9912 case 128:
9913 cntl |= CURSOR_MODE_128_ARGB_AX;
9914 break;
9915 case 256:
9916 cntl |= CURSOR_MODE_256_ARGB_AX;
9917 break;
9918 default:
3dd512fb 9919 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 9920 return;
65a21cd6 9921 }
4b0e333e 9922 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
9923
9924 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9925 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 9926 }
65a21cd6 9927
8e7d688b 9928 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
9929 cntl |= CURSOR_ROTATE_180;
9930
4b0e333e
CW
9931 if (intel_crtc->cursor_cntl != cntl) {
9932 I915_WRITE(CURCNTR(pipe), cntl);
9933 POSTING_READ(CURCNTR(pipe));
9934 intel_crtc->cursor_cntl = cntl;
65a21cd6 9935 }
4b0e333e 9936
65a21cd6 9937 /* and commit changes on next vblank */
5efb3e28
VS
9938 I915_WRITE(CURBASE(pipe), base);
9939 POSTING_READ(CURBASE(pipe));
99d1f387
VS
9940
9941 intel_crtc->cursor_base = base;
65a21cd6
JB
9942}
9943
cda4b7d3 9944/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
9945static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9946 bool on)
cda4b7d3
CW
9947{
9948 struct drm_device *dev = crtc->dev;
9949 struct drm_i915_private *dev_priv = dev->dev_private;
9950 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9951 int pipe = intel_crtc->pipe;
3d7d6510
MR
9952 int x = crtc->cursor_x;
9953 int y = crtc->cursor_y;
d6e4db15 9954 u32 base = 0, pos = 0;
cda4b7d3 9955
d6e4db15 9956 if (on)
cda4b7d3 9957 base = intel_crtc->cursor_addr;
cda4b7d3 9958
6e3c9717 9959 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
9960 base = 0;
9961
6e3c9717 9962 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
9963 base = 0;
9964
9965 if (x < 0) {
3dd512fb 9966 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
cda4b7d3
CW
9967 base = 0;
9968
9969 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9970 x = -x;
9971 }
9972 pos |= x << CURSOR_X_SHIFT;
9973
9974 if (y < 0) {
3dd512fb 9975 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
cda4b7d3
CW
9976 base = 0;
9977
9978 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9979 y = -y;
9980 }
9981 pos |= y << CURSOR_Y_SHIFT;
9982
4b0e333e 9983 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
9984 return;
9985
5efb3e28
VS
9986 I915_WRITE(CURPOS(pipe), pos);
9987
4398ad45
VS
9988 /* ILK+ do this automagically */
9989 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 9990 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
3dd512fb
MR
9991 base += (intel_crtc->base.cursor->state->crtc_h *
9992 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
4398ad45
VS
9993 }
9994
8ac54669 9995 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
9996 i845_update_cursor(crtc, base);
9997 else
9998 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
9999}
10000
dc41c154
VS
10001static bool cursor_size_ok(struct drm_device *dev,
10002 uint32_t width, uint32_t height)
10003{
10004 if (width == 0 || height == 0)
10005 return false;
10006
10007 /*
10008 * 845g/865g are special in that they are only limited by
10009 * the width of their cursors, the height is arbitrary up to
10010 * the precision of the register. Everything else requires
10011 * square cursors, limited to a few power-of-two sizes.
10012 */
10013 if (IS_845G(dev) || IS_I865G(dev)) {
10014 if ((width & 63) != 0)
10015 return false;
10016
10017 if (width > (IS_845G(dev) ? 64 : 512))
10018 return false;
10019
10020 if (height > 1023)
10021 return false;
10022 } else {
10023 switch (width | height) {
10024 case 256:
10025 case 128:
10026 if (IS_GEN2(dev))
10027 return false;
10028 case 64:
10029 break;
10030 default:
10031 return false;
10032 }
10033 }
10034
10035 return true;
10036}
10037
79e53945 10038static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 10039 u16 *blue, uint32_t start, uint32_t size)
79e53945 10040{
7203425a 10041 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 10042 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 10043
7203425a 10044 for (i = start; i < end; i++) {
79e53945
JB
10045 intel_crtc->lut_r[i] = red[i] >> 8;
10046 intel_crtc->lut_g[i] = green[i] >> 8;
10047 intel_crtc->lut_b[i] = blue[i] >> 8;
10048 }
10049
10050 intel_crtc_load_lut(crtc);
10051}
10052
79e53945
JB
10053/* VESA 640x480x72Hz mode to set on the pipe */
10054static struct drm_display_mode load_detect_mode = {
10055 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10056 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10057};
10058
a8bb6818
DV
10059struct drm_framebuffer *
10060__intel_framebuffer_create(struct drm_device *dev,
10061 struct drm_mode_fb_cmd2 *mode_cmd,
10062 struct drm_i915_gem_object *obj)
d2dff872
CW
10063{
10064 struct intel_framebuffer *intel_fb;
10065 int ret;
10066
10067 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10068 if (!intel_fb) {
6ccb81f2 10069 drm_gem_object_unreference(&obj->base);
d2dff872
CW
10070 return ERR_PTR(-ENOMEM);
10071 }
10072
10073 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10074 if (ret)
10075 goto err;
d2dff872
CW
10076
10077 return &intel_fb->base;
dd4916c5 10078err:
6ccb81f2 10079 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
10080 kfree(intel_fb);
10081
10082 return ERR_PTR(ret);
d2dff872
CW
10083}
10084
b5ea642a 10085static struct drm_framebuffer *
a8bb6818
DV
10086intel_framebuffer_create(struct drm_device *dev,
10087 struct drm_mode_fb_cmd2 *mode_cmd,
10088 struct drm_i915_gem_object *obj)
10089{
10090 struct drm_framebuffer *fb;
10091 int ret;
10092
10093 ret = i915_mutex_lock_interruptible(dev);
10094 if (ret)
10095 return ERR_PTR(ret);
10096 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10097 mutex_unlock(&dev->struct_mutex);
10098
10099 return fb;
10100}
10101
d2dff872
CW
10102static u32
10103intel_framebuffer_pitch_for_width(int width, int bpp)
10104{
10105 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10106 return ALIGN(pitch, 64);
10107}
10108
10109static u32
10110intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10111{
10112 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10113 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10114}
10115
10116static struct drm_framebuffer *
10117intel_framebuffer_create_for_mode(struct drm_device *dev,
10118 struct drm_display_mode *mode,
10119 int depth, int bpp)
10120{
10121 struct drm_i915_gem_object *obj;
0fed39bd 10122 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10123
10124 obj = i915_gem_alloc_object(dev,
10125 intel_framebuffer_size_for_mode(mode, bpp));
10126 if (obj == NULL)
10127 return ERR_PTR(-ENOMEM);
10128
10129 mode_cmd.width = mode->hdisplay;
10130 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10131 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10132 bpp);
5ca0c34a 10133 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
10134
10135 return intel_framebuffer_create(dev, &mode_cmd, obj);
10136}
10137
10138static struct drm_framebuffer *
10139mode_fits_in_fbdev(struct drm_device *dev,
10140 struct drm_display_mode *mode)
10141{
4520f53a 10142#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
10143 struct drm_i915_private *dev_priv = dev->dev_private;
10144 struct drm_i915_gem_object *obj;
10145 struct drm_framebuffer *fb;
10146
4c0e5528 10147 if (!dev_priv->fbdev)
d2dff872
CW
10148 return NULL;
10149
4c0e5528 10150 if (!dev_priv->fbdev->fb)
d2dff872
CW
10151 return NULL;
10152
4c0e5528
DV
10153 obj = dev_priv->fbdev->fb->obj;
10154 BUG_ON(!obj);
10155
8bcd4553 10156 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10157 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10158 fb->bits_per_pixel))
d2dff872
CW
10159 return NULL;
10160
01f2c773 10161 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10162 return NULL;
10163
10164 return fb;
4520f53a
DV
10165#else
10166 return NULL;
10167#endif
d2dff872
CW
10168}
10169
d3a40d1b
ACO
10170static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10171 struct drm_crtc *crtc,
10172 struct drm_display_mode *mode,
10173 struct drm_framebuffer *fb,
10174 int x, int y)
10175{
10176 struct drm_plane_state *plane_state;
10177 int hdisplay, vdisplay;
10178 int ret;
10179
10180 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10181 if (IS_ERR(plane_state))
10182 return PTR_ERR(plane_state);
10183
10184 if (mode)
10185 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10186 else
10187 hdisplay = vdisplay = 0;
10188
10189 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10190 if (ret)
10191 return ret;
10192 drm_atomic_set_fb_for_plane(plane_state, fb);
10193 plane_state->crtc_x = 0;
10194 plane_state->crtc_y = 0;
10195 plane_state->crtc_w = hdisplay;
10196 plane_state->crtc_h = vdisplay;
10197 plane_state->src_x = x << 16;
10198 plane_state->src_y = y << 16;
10199 plane_state->src_w = hdisplay << 16;
10200 plane_state->src_h = vdisplay << 16;
10201
10202 return 0;
10203}
10204
d2434ab7 10205bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10206 struct drm_display_mode *mode,
51fd371b
RC
10207 struct intel_load_detect_pipe *old,
10208 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10209{
10210 struct intel_crtc *intel_crtc;
d2434ab7
DV
10211 struct intel_encoder *intel_encoder =
10212 intel_attached_encoder(connector);
79e53945 10213 struct drm_crtc *possible_crtc;
4ef69c7a 10214 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10215 struct drm_crtc *crtc = NULL;
10216 struct drm_device *dev = encoder->dev;
94352cf9 10217 struct drm_framebuffer *fb;
51fd371b 10218 struct drm_mode_config *config = &dev->mode_config;
83a57153 10219 struct drm_atomic_state *state = NULL;
944b0c76 10220 struct drm_connector_state *connector_state;
4be07317 10221 struct intel_crtc_state *crtc_state;
51fd371b 10222 int ret, i = -1;
79e53945 10223
d2dff872 10224 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10225 connector->base.id, connector->name,
8e329a03 10226 encoder->base.id, encoder->name);
d2dff872 10227
51fd371b
RC
10228retry:
10229 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10230 if (ret)
10231 goto fail_unlock;
6e9f798d 10232
79e53945
JB
10233 /*
10234 * Algorithm gets a little messy:
7a5e4805 10235 *
79e53945
JB
10236 * - if the connector already has an assigned crtc, use it (but make
10237 * sure it's on first)
7a5e4805 10238 *
79e53945
JB
10239 * - try to find the first unused crtc that can drive this connector,
10240 * and use that if we find one
79e53945
JB
10241 */
10242
10243 /* See if we already have a CRTC for this connector */
10244 if (encoder->crtc) {
10245 crtc = encoder->crtc;
8261b191 10246
51fd371b 10247 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de
DV
10248 if (ret)
10249 goto fail_unlock;
10250 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b
RC
10251 if (ret)
10252 goto fail_unlock;
7b24056b 10253
24218aac 10254 old->dpms_mode = connector->dpms;
8261b191
CW
10255 old->load_detect_temp = false;
10256
10257 /* Make sure the crtc and connector are running */
24218aac
DV
10258 if (connector->dpms != DRM_MODE_DPMS_ON)
10259 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 10260
7173188d 10261 return true;
79e53945
JB
10262 }
10263
10264 /* Find an unused one (if possible) */
70e1e0ec 10265 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10266 i++;
10267 if (!(encoder->possible_crtcs & (1 << i)))
10268 continue;
83d65738 10269 if (possible_crtc->state->enable)
a459249c
VS
10270 continue;
10271 /* This can occur when applying the pipe A quirk on resume. */
10272 if (to_intel_crtc(possible_crtc)->new_enabled)
10273 continue;
10274
10275 crtc = possible_crtc;
10276 break;
79e53945
JB
10277 }
10278
10279 /*
10280 * If we didn't find an unused CRTC, don't use any.
10281 */
10282 if (!crtc) {
7173188d 10283 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 10284 goto fail_unlock;
79e53945
JB
10285 }
10286
51fd371b
RC
10287 ret = drm_modeset_lock(&crtc->mutex, ctx);
10288 if (ret)
4d02e2de
DV
10289 goto fail_unlock;
10290 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10291 if (ret)
51fd371b 10292 goto fail_unlock;
fc303101
DV
10293 intel_encoder->new_crtc = to_intel_crtc(crtc);
10294 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
10295
10296 intel_crtc = to_intel_crtc(crtc);
412b61d8 10297 intel_crtc->new_enabled = true;
24218aac 10298 old->dpms_mode = connector->dpms;
8261b191 10299 old->load_detect_temp = true;
d2dff872 10300 old->release_fb = NULL;
79e53945 10301
83a57153
ACO
10302 state = drm_atomic_state_alloc(dev);
10303 if (!state)
10304 return false;
10305
10306 state->acquire_ctx = ctx;
10307
944b0c76
ACO
10308 connector_state = drm_atomic_get_connector_state(state, connector);
10309 if (IS_ERR(connector_state)) {
10310 ret = PTR_ERR(connector_state);
10311 goto fail;
10312 }
10313
10314 connector_state->crtc = crtc;
10315 connector_state->best_encoder = &intel_encoder->base;
10316
4be07317
ACO
10317 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10318 if (IS_ERR(crtc_state)) {
10319 ret = PTR_ERR(crtc_state);
10320 goto fail;
10321 }
10322
49d6fa21 10323 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10324
6492711d
CW
10325 if (!mode)
10326 mode = &load_detect_mode;
79e53945 10327
d2dff872
CW
10328 /* We need a framebuffer large enough to accommodate all accesses
10329 * that the plane may generate whilst we perform load detection.
10330 * We can not rely on the fbcon either being present (we get called
10331 * during its initialisation to detect all boot displays, or it may
10332 * not even exist) or that it is large enough to satisfy the
10333 * requested mode.
10334 */
94352cf9
DV
10335 fb = mode_fits_in_fbdev(dev, mode);
10336 if (fb == NULL) {
d2dff872 10337 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
10338 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10339 old->release_fb = fb;
d2dff872
CW
10340 } else
10341 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10342 if (IS_ERR(fb)) {
d2dff872 10343 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10344 goto fail;
79e53945 10345 }
79e53945 10346
d3a40d1b
ACO
10347 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10348 if (ret)
10349 goto fail;
10350
8c7b5ccb
ACO
10351 drm_mode_copy(&crtc_state->base.mode, mode);
10352
568c634a 10353 if (intel_set_mode(state)) {
6492711d 10354 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
10355 if (old->release_fb)
10356 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 10357 goto fail;
79e53945 10358 }
9128b040 10359 crtc->primary->crtc = crtc;
7173188d 10360
79e53945 10361 /* let the connector get through one full cycle before testing */
9d0498a2 10362 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10363 return true;
412b61d8
VS
10364
10365 fail:
83d65738 10366 intel_crtc->new_enabled = crtc->state->enable;
51fd371b 10367fail_unlock:
e5d958ef
ACO
10368 drm_atomic_state_free(state);
10369 state = NULL;
83a57153 10370
51fd371b
RC
10371 if (ret == -EDEADLK) {
10372 drm_modeset_backoff(ctx);
10373 goto retry;
10374 }
10375
412b61d8 10376 return false;
79e53945
JB
10377}
10378
d2434ab7 10379void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10380 struct intel_load_detect_pipe *old,
10381 struct drm_modeset_acquire_ctx *ctx)
79e53945 10382{
83a57153 10383 struct drm_device *dev = connector->dev;
d2434ab7
DV
10384 struct intel_encoder *intel_encoder =
10385 intel_attached_encoder(connector);
4ef69c7a 10386 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 10387 struct drm_crtc *crtc = encoder->crtc;
412b61d8 10388 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83a57153 10389 struct drm_atomic_state *state;
944b0c76 10390 struct drm_connector_state *connector_state;
4be07317 10391 struct intel_crtc_state *crtc_state;
d3a40d1b 10392 int ret;
79e53945 10393
d2dff872 10394 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10395 connector->base.id, connector->name,
8e329a03 10396 encoder->base.id, encoder->name);
d2dff872 10397
8261b191 10398 if (old->load_detect_temp) {
83a57153 10399 state = drm_atomic_state_alloc(dev);
944b0c76
ACO
10400 if (!state)
10401 goto fail;
83a57153
ACO
10402
10403 state->acquire_ctx = ctx;
10404
944b0c76
ACO
10405 connector_state = drm_atomic_get_connector_state(state, connector);
10406 if (IS_ERR(connector_state))
10407 goto fail;
10408
4be07317
ACO
10409 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10410 if (IS_ERR(crtc_state))
10411 goto fail;
10412
fc303101
DV
10413 to_intel_connector(connector)->new_encoder = NULL;
10414 intel_encoder->new_crtc = NULL;
412b61d8 10415 intel_crtc->new_enabled = false;
944b0c76
ACO
10416
10417 connector_state->best_encoder = NULL;
10418 connector_state->crtc = NULL;
10419
49d6fa21 10420 crtc_state->base.enable = crtc_state->base.active = false;
4be07317 10421
d3a40d1b
ACO
10422 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10423 0, 0);
10424 if (ret)
10425 goto fail;
10426
568c634a 10427 ret = intel_set_mode(state);
2bfb4627
ACO
10428 if (ret)
10429 goto fail;
d2dff872 10430
36206361
DV
10431 if (old->release_fb) {
10432 drm_framebuffer_unregister_private(old->release_fb);
10433 drm_framebuffer_unreference(old->release_fb);
10434 }
d2dff872 10435
0622a53c 10436 return;
79e53945
JB
10437 }
10438
c751ce4f 10439 /* Switch crtc and encoder back off if necessary */
24218aac
DV
10440 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10441 connector->funcs->dpms(connector, old->dpms_mode);
944b0c76
ACO
10442
10443 return;
10444fail:
10445 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10446 drm_atomic_state_free(state);
79e53945
JB
10447}
10448
da4a1efa 10449static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10450 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10451{
10452 struct drm_i915_private *dev_priv = dev->dev_private;
10453 u32 dpll = pipe_config->dpll_hw_state.dpll;
10454
10455 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10456 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10457 else if (HAS_PCH_SPLIT(dev))
10458 return 120000;
10459 else if (!IS_GEN2(dev))
10460 return 96000;
10461 else
10462 return 48000;
10463}
10464
79e53945 10465/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10466static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10467 struct intel_crtc_state *pipe_config)
79e53945 10468{
f1f644dc 10469 struct drm_device *dev = crtc->base.dev;
79e53945 10470 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10471 int pipe = pipe_config->cpu_transcoder;
293623f7 10472 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10473 u32 fp;
10474 intel_clock_t clock;
da4a1efa 10475 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10476
10477 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10478 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10479 else
293623f7 10480 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10481
10482 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10483 if (IS_PINEVIEW(dev)) {
10484 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10485 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10486 } else {
10487 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10488 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10489 }
10490
a6c45cf0 10491 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10492 if (IS_PINEVIEW(dev))
10493 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10494 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10495 else
10496 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10497 DPLL_FPA01_P1_POST_DIV_SHIFT);
10498
10499 switch (dpll & DPLL_MODE_MASK) {
10500 case DPLLB_MODE_DAC_SERIAL:
10501 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10502 5 : 10;
10503 break;
10504 case DPLLB_MODE_LVDS:
10505 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10506 7 : 14;
10507 break;
10508 default:
28c97730 10509 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10510 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10511 return;
79e53945
JB
10512 }
10513
ac58c3f0 10514 if (IS_PINEVIEW(dev))
da4a1efa 10515 pineview_clock(refclk, &clock);
ac58c3f0 10516 else
da4a1efa 10517 i9xx_clock(refclk, &clock);
79e53945 10518 } else {
0fb58223 10519 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10520 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10521
10522 if (is_lvds) {
10523 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10524 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10525
10526 if (lvds & LVDS_CLKB_POWER_UP)
10527 clock.p2 = 7;
10528 else
10529 clock.p2 = 14;
79e53945
JB
10530 } else {
10531 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10532 clock.p1 = 2;
10533 else {
10534 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10535 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10536 }
10537 if (dpll & PLL_P2_DIVIDE_BY_4)
10538 clock.p2 = 4;
10539 else
10540 clock.p2 = 2;
79e53945 10541 }
da4a1efa
VS
10542
10543 i9xx_clock(refclk, &clock);
79e53945
JB
10544 }
10545
18442d08
VS
10546 /*
10547 * This value includes pixel_multiplier. We will use
241bfc38 10548 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10549 * encoder's get_config() function.
10550 */
10551 pipe_config->port_clock = clock.dot;
f1f644dc
JB
10552}
10553
6878da05
VS
10554int intel_dotclock_calculate(int link_freq,
10555 const struct intel_link_m_n *m_n)
f1f644dc 10556{
f1f644dc
JB
10557 /*
10558 * The calculation for the data clock is:
1041a02f 10559 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10560 * But we want to avoid losing precison if possible, so:
1041a02f 10561 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10562 *
10563 * and the link clock is simpler:
1041a02f 10564 * link_clock = (m * link_clock) / n
f1f644dc
JB
10565 */
10566
6878da05
VS
10567 if (!m_n->link_n)
10568 return 0;
f1f644dc 10569
6878da05
VS
10570 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10571}
f1f644dc 10572
18442d08 10573static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10574 struct intel_crtc_state *pipe_config)
6878da05
VS
10575{
10576 struct drm_device *dev = crtc->base.dev;
79e53945 10577
18442d08
VS
10578 /* read out port_clock from the DPLL */
10579 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10580
f1f644dc 10581 /*
18442d08 10582 * This value does not include pixel_multiplier.
241bfc38 10583 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
10584 * agree once we know their relationship in the encoder's
10585 * get_config() function.
79e53945 10586 */
2d112de7 10587 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
10588 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10589 &pipe_config->fdi_m_n);
79e53945
JB
10590}
10591
10592/** Returns the currently programmed mode of the given pipe. */
10593struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10594 struct drm_crtc *crtc)
10595{
548f245b 10596 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10597 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10598 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10599 struct drm_display_mode *mode;
5cec258b 10600 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
10601 int htot = I915_READ(HTOTAL(cpu_transcoder));
10602 int hsync = I915_READ(HSYNC(cpu_transcoder));
10603 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10604 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10605 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10606
10607 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10608 if (!mode)
10609 return NULL;
10610
f1f644dc
JB
10611 /*
10612 * Construct a pipe_config sufficient for getting the clock info
10613 * back out of crtc_clock_get.
10614 *
10615 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10616 * to use a real value here instead.
10617 */
293623f7 10618 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 10619 pipe_config.pixel_multiplier = 1;
293623f7
VS
10620 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10621 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10622 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
10623 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10624
773ae034 10625 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
10626 mode->hdisplay = (htot & 0xffff) + 1;
10627 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10628 mode->hsync_start = (hsync & 0xffff) + 1;
10629 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10630 mode->vdisplay = (vtot & 0xffff) + 1;
10631 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10632 mode->vsync_start = (vsync & 0xffff) + 1;
10633 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10634
10635 drm_mode_set_name(mode);
79e53945
JB
10636
10637 return mode;
10638}
10639
652c393a
JB
10640static void intel_decrease_pllclock(struct drm_crtc *crtc)
10641{
10642 struct drm_device *dev = crtc->dev;
fbee40df 10643 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10644 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 10645
baff296c 10646 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
10647 return;
10648
10649 if (!dev_priv->lvds_downclock_avail)
10650 return;
10651
10652 /*
10653 * Since this is called by a timer, we should never get here in
10654 * the manual case.
10655 */
10656 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
10657 int pipe = intel_crtc->pipe;
10658 int dpll_reg = DPLL(pipe);
10659 int dpll;
f6e5b160 10660
44d98a61 10661 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 10662
8ac5a6d5 10663 assert_panel_unlocked(dev_priv, pipe);
652c393a 10664
dc257cf1 10665 dpll = I915_READ(dpll_reg);
652c393a
JB
10666 dpll |= DISPLAY_RATE_SELECT_FPA1;
10667 I915_WRITE(dpll_reg, dpll);
9d0498a2 10668 intel_wait_for_vblank(dev, pipe);
652c393a
JB
10669 dpll = I915_READ(dpll_reg);
10670 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 10671 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
10672 }
10673
10674}
10675
f047e395
CW
10676void intel_mark_busy(struct drm_device *dev)
10677{
c67a470b
PZ
10678 struct drm_i915_private *dev_priv = dev->dev_private;
10679
f62a0076
CW
10680 if (dev_priv->mm.busy)
10681 return;
10682
43694d69 10683 intel_runtime_pm_get(dev_priv);
c67a470b 10684 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10685 if (INTEL_INFO(dev)->gen >= 6)
10686 gen6_rps_busy(dev_priv);
f62a0076 10687 dev_priv->mm.busy = true;
f047e395
CW
10688}
10689
10690void intel_mark_idle(struct drm_device *dev)
652c393a 10691{
c67a470b 10692 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10693 struct drm_crtc *crtc;
652c393a 10694
f62a0076
CW
10695 if (!dev_priv->mm.busy)
10696 return;
10697
10698 dev_priv->mm.busy = false;
10699
70e1e0ec 10700 for_each_crtc(dev, crtc) {
f4510a27 10701 if (!crtc->primary->fb)
652c393a
JB
10702 continue;
10703
725a5b54 10704 intel_decrease_pllclock(crtc);
652c393a 10705 }
b29c19b6 10706
3d13ef2e 10707 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10708 gen6_rps_idle(dev->dev_private);
bb4cdd53 10709
43694d69 10710 intel_runtime_pm_put(dev_priv);
652c393a
JB
10711}
10712
79e53945
JB
10713static void intel_crtc_destroy(struct drm_crtc *crtc)
10714{
10715 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10716 struct drm_device *dev = crtc->dev;
10717 struct intel_unpin_work *work;
67e77c5a 10718
5e2d7afc 10719 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10720 work = intel_crtc->unpin_work;
10721 intel_crtc->unpin_work = NULL;
5e2d7afc 10722 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10723
10724 if (work) {
10725 cancel_work_sync(&work->work);
10726 kfree(work);
10727 }
79e53945
JB
10728
10729 drm_crtc_cleanup(crtc);
67e77c5a 10730
79e53945
JB
10731 kfree(intel_crtc);
10732}
10733
6b95a207
KH
10734static void intel_unpin_work_fn(struct work_struct *__work)
10735{
10736 struct intel_unpin_work *work =
10737 container_of(__work, struct intel_unpin_work, work);
b4a98e57 10738 struct drm_device *dev = work->crtc->dev;
f99d7069 10739 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 10740
b4a98e57 10741 mutex_lock(&dev->struct_mutex);
82bc3b2d 10742 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
05394f39 10743 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10744
7ff0ebcc 10745 intel_fbc_update(dev);
f06cc1b9
JH
10746
10747 if (work->flip_queued_req)
146d84f0 10748 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10749 mutex_unlock(&dev->struct_mutex);
10750
f99d7069 10751 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
89ed88ba 10752 drm_framebuffer_unreference(work->old_fb);
f99d7069 10753
b4a98e57
CW
10754 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10755 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10756
6b95a207
KH
10757 kfree(work);
10758}
10759
1afe3e9d 10760static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10761 struct drm_crtc *crtc)
6b95a207 10762{
6b95a207
KH
10763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10764 struct intel_unpin_work *work;
6b95a207
KH
10765 unsigned long flags;
10766
10767 /* Ignore early vblank irqs */
10768 if (intel_crtc == NULL)
10769 return;
10770
f326038a
DV
10771 /*
10772 * This is called both by irq handlers and the reset code (to complete
10773 * lost pageflips) so needs the full irqsave spinlocks.
10774 */
6b95a207
KH
10775 spin_lock_irqsave(&dev->event_lock, flags);
10776 work = intel_crtc->unpin_work;
e7d841ca
CW
10777
10778 /* Ensure we don't miss a work->pending update ... */
10779 smp_rmb();
10780
10781 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10782 spin_unlock_irqrestore(&dev->event_lock, flags);
10783 return;
10784 }
10785
d6bbafa1 10786 page_flip_completed(intel_crtc);
0af7e4df 10787
6b95a207 10788 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10789}
10790
1afe3e9d
JB
10791void intel_finish_page_flip(struct drm_device *dev, int pipe)
10792{
fbee40df 10793 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10794 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10795
49b14a5c 10796 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10797}
10798
10799void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10800{
fbee40df 10801 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10802 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10803
49b14a5c 10804 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10805}
10806
75f7f3ec
VS
10807/* Is 'a' after or equal to 'b'? */
10808static bool g4x_flip_count_after_eq(u32 a, u32 b)
10809{
10810 return !((a - b) & 0x80000000);
10811}
10812
10813static bool page_flip_finished(struct intel_crtc *crtc)
10814{
10815 struct drm_device *dev = crtc->base.dev;
10816 struct drm_i915_private *dev_priv = dev->dev_private;
10817
bdfa7542
VS
10818 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10819 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10820 return true;
10821
75f7f3ec
VS
10822 /*
10823 * The relevant registers doen't exist on pre-ctg.
10824 * As the flip done interrupt doesn't trigger for mmio
10825 * flips on gmch platforms, a flip count check isn't
10826 * really needed there. But since ctg has the registers,
10827 * include it in the check anyway.
10828 */
10829 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10830 return true;
10831
10832 /*
10833 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10834 * used the same base address. In that case the mmio flip might
10835 * have completed, but the CS hasn't even executed the flip yet.
10836 *
10837 * A flip count check isn't enough as the CS might have updated
10838 * the base address just after start of vblank, but before we
10839 * managed to process the interrupt. This means we'd complete the
10840 * CS flip too soon.
10841 *
10842 * Combining both checks should get us a good enough result. It may
10843 * still happen that the CS flip has been executed, but has not
10844 * yet actually completed. But in case the base address is the same
10845 * anyway, we don't really care.
10846 */
10847 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10848 crtc->unpin_work->gtt_offset &&
10849 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10850 crtc->unpin_work->flip_count);
10851}
10852
6b95a207
KH
10853void intel_prepare_page_flip(struct drm_device *dev, int plane)
10854{
fbee40df 10855 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10856 struct intel_crtc *intel_crtc =
10857 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10858 unsigned long flags;
10859
f326038a
DV
10860
10861 /*
10862 * This is called both by irq handlers and the reset code (to complete
10863 * lost pageflips) so needs the full irqsave spinlocks.
10864 *
10865 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10866 * generate a page-flip completion irq, i.e. every modeset
10867 * is also accompanied by a spurious intel_prepare_page_flip().
10868 */
6b95a207 10869 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10870 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10871 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10872 spin_unlock_irqrestore(&dev->event_lock, flags);
10873}
10874
eba905b2 10875static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
10876{
10877 /* Ensure that the work item is consistent when activating it ... */
10878 smp_wmb();
10879 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10880 /* and that it is marked active as soon as the irq could fire. */
10881 smp_wmb();
10882}
10883
8c9f3aaf
JB
10884static int intel_gen2_queue_flip(struct drm_device *dev,
10885 struct drm_crtc *crtc,
10886 struct drm_framebuffer *fb,
ed8d1975 10887 struct drm_i915_gem_object *obj,
a4872ba6 10888 struct intel_engine_cs *ring,
ed8d1975 10889 uint32_t flags)
8c9f3aaf 10890{
8c9f3aaf 10891 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10892 u32 flip_mask;
10893 int ret;
10894
6d90c952 10895 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10896 if (ret)
4fa62c89 10897 return ret;
8c9f3aaf
JB
10898
10899 /* Can't queue multiple flips, so wait for the previous
10900 * one to finish before executing the next.
10901 */
10902 if (intel_crtc->plane)
10903 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10904 else
10905 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10906 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10907 intel_ring_emit(ring, MI_NOOP);
10908 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10909 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10910 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10911 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 10912 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
10913
10914 intel_mark_page_flip_active(intel_crtc);
09246732 10915 __intel_ring_advance(ring);
83d4092b 10916 return 0;
8c9f3aaf
JB
10917}
10918
10919static int intel_gen3_queue_flip(struct drm_device *dev,
10920 struct drm_crtc *crtc,
10921 struct drm_framebuffer *fb,
ed8d1975 10922 struct drm_i915_gem_object *obj,
a4872ba6 10923 struct intel_engine_cs *ring,
ed8d1975 10924 uint32_t flags)
8c9f3aaf 10925{
8c9f3aaf 10926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10927 u32 flip_mask;
10928 int ret;
10929
6d90c952 10930 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10931 if (ret)
4fa62c89 10932 return ret;
8c9f3aaf
JB
10933
10934 if (intel_crtc->plane)
10935 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10936 else
10937 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10938 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10939 intel_ring_emit(ring, MI_NOOP);
10940 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10941 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10942 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10943 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
10944 intel_ring_emit(ring, MI_NOOP);
10945
e7d841ca 10946 intel_mark_page_flip_active(intel_crtc);
09246732 10947 __intel_ring_advance(ring);
83d4092b 10948 return 0;
8c9f3aaf
JB
10949}
10950
10951static int intel_gen4_queue_flip(struct drm_device *dev,
10952 struct drm_crtc *crtc,
10953 struct drm_framebuffer *fb,
ed8d1975 10954 struct drm_i915_gem_object *obj,
a4872ba6 10955 struct intel_engine_cs *ring,
ed8d1975 10956 uint32_t flags)
8c9f3aaf
JB
10957{
10958 struct drm_i915_private *dev_priv = dev->dev_private;
10959 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10960 uint32_t pf, pipesrc;
10961 int ret;
10962
6d90c952 10963 ret = intel_ring_begin(ring, 4);
8c9f3aaf 10964 if (ret)
4fa62c89 10965 return ret;
8c9f3aaf
JB
10966
10967 /* i965+ uses the linear or tiled offsets from the
10968 * Display Registers (which do not change across a page-flip)
10969 * so we need only reprogram the base address.
10970 */
6d90c952
DV
10971 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10972 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10973 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10974 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 10975 obj->tiling_mode);
8c9f3aaf
JB
10976
10977 /* XXX Enabling the panel-fitter across page-flip is so far
10978 * untested on non-native modes, so ignore it for now.
10979 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10980 */
10981 pf = 0;
10982 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10983 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10984
10985 intel_mark_page_flip_active(intel_crtc);
09246732 10986 __intel_ring_advance(ring);
83d4092b 10987 return 0;
8c9f3aaf
JB
10988}
10989
10990static int intel_gen6_queue_flip(struct drm_device *dev,
10991 struct drm_crtc *crtc,
10992 struct drm_framebuffer *fb,
ed8d1975 10993 struct drm_i915_gem_object *obj,
a4872ba6 10994 struct intel_engine_cs *ring,
ed8d1975 10995 uint32_t flags)
8c9f3aaf
JB
10996{
10997 struct drm_i915_private *dev_priv = dev->dev_private;
10998 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10999 uint32_t pf, pipesrc;
11000 int ret;
11001
6d90c952 11002 ret = intel_ring_begin(ring, 4);
8c9f3aaf 11003 if (ret)
4fa62c89 11004 return ret;
8c9f3aaf 11005
6d90c952
DV
11006 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11007 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11008 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 11009 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 11010
dc257cf1
DV
11011 /* Contrary to the suggestions in the documentation,
11012 * "Enable Panel Fitter" does not seem to be required when page
11013 * flipping with a non-native mode, and worse causes a normal
11014 * modeset to fail.
11015 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11016 */
11017 pf = 0;
8c9f3aaf 11018 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 11019 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
11020
11021 intel_mark_page_flip_active(intel_crtc);
09246732 11022 __intel_ring_advance(ring);
83d4092b 11023 return 0;
8c9f3aaf
JB
11024}
11025
7c9017e5
JB
11026static int intel_gen7_queue_flip(struct drm_device *dev,
11027 struct drm_crtc *crtc,
11028 struct drm_framebuffer *fb,
ed8d1975 11029 struct drm_i915_gem_object *obj,
a4872ba6 11030 struct intel_engine_cs *ring,
ed8d1975 11031 uint32_t flags)
7c9017e5 11032{
7c9017e5 11033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 11034 uint32_t plane_bit = 0;
ffe74d75
CW
11035 int len, ret;
11036
eba905b2 11037 switch (intel_crtc->plane) {
cb05d8de
DV
11038 case PLANE_A:
11039 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11040 break;
11041 case PLANE_B:
11042 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11043 break;
11044 case PLANE_C:
11045 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11046 break;
11047 default:
11048 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 11049 return -ENODEV;
cb05d8de
DV
11050 }
11051
ffe74d75 11052 len = 4;
f476828a 11053 if (ring->id == RCS) {
ffe74d75 11054 len += 6;
f476828a
DL
11055 /*
11056 * On Gen 8, SRM is now taking an extra dword to accommodate
11057 * 48bits addresses, and we need a NOOP for the batch size to
11058 * stay even.
11059 */
11060 if (IS_GEN8(dev))
11061 len += 2;
11062 }
ffe74d75 11063
f66fab8e
VS
11064 /*
11065 * BSpec MI_DISPLAY_FLIP for IVB:
11066 * "The full packet must be contained within the same cache line."
11067 *
11068 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11069 * cacheline, if we ever start emitting more commands before
11070 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11071 * then do the cacheline alignment, and finally emit the
11072 * MI_DISPLAY_FLIP.
11073 */
11074 ret = intel_ring_cacheline_align(ring);
11075 if (ret)
4fa62c89 11076 return ret;
f66fab8e 11077
ffe74d75 11078 ret = intel_ring_begin(ring, len);
7c9017e5 11079 if (ret)
4fa62c89 11080 return ret;
7c9017e5 11081
ffe74d75
CW
11082 /* Unmask the flip-done completion message. Note that the bspec says that
11083 * we should do this for both the BCS and RCS, and that we must not unmask
11084 * more than one flip event at any time (or ensure that one flip message
11085 * can be sent by waiting for flip-done prior to queueing new flips).
11086 * Experimentation says that BCS works despite DERRMR masking all
11087 * flip-done completion events and that unmasking all planes at once
11088 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11089 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11090 */
11091 if (ring->id == RCS) {
11092 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11093 intel_ring_emit(ring, DERRMR);
11094 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11095 DERRMR_PIPEB_PRI_FLIP_DONE |
11096 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
11097 if (IS_GEN8(dev))
11098 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11099 MI_SRM_LRM_GLOBAL_GTT);
11100 else
11101 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11102 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
11103 intel_ring_emit(ring, DERRMR);
11104 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
11105 if (IS_GEN8(dev)) {
11106 intel_ring_emit(ring, 0);
11107 intel_ring_emit(ring, MI_NOOP);
11108 }
ffe74d75
CW
11109 }
11110
cb05d8de 11111 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 11112 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 11113 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 11114 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
11115
11116 intel_mark_page_flip_active(intel_crtc);
09246732 11117 __intel_ring_advance(ring);
83d4092b 11118 return 0;
7c9017e5
JB
11119}
11120
84c33a64
SG
11121static bool use_mmio_flip(struct intel_engine_cs *ring,
11122 struct drm_i915_gem_object *obj)
11123{
11124 /*
11125 * This is not being used for older platforms, because
11126 * non-availability of flip done interrupt forces us to use
11127 * CS flips. Older platforms derive flip done using some clever
11128 * tricks involving the flip_pending status bits and vblank irqs.
11129 * So using MMIO flips there would disrupt this mechanism.
11130 */
11131
8e09bf83
CW
11132 if (ring == NULL)
11133 return true;
11134
84c33a64
SG
11135 if (INTEL_INFO(ring->dev)->gen < 5)
11136 return false;
11137
11138 if (i915.use_mmio_flip < 0)
11139 return false;
11140 else if (i915.use_mmio_flip > 0)
11141 return true;
14bf993e
OM
11142 else if (i915.enable_execlists)
11143 return true;
84c33a64 11144 else
b4716185 11145 return ring != i915_gem_request_get_ring(obj->last_write_req);
84c33a64
SG
11146}
11147
ff944564
DL
11148static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11149{
11150 struct drm_device *dev = intel_crtc->base.dev;
11151 struct drm_i915_private *dev_priv = dev->dev_private;
11152 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564
DL
11153 const enum pipe pipe = intel_crtc->pipe;
11154 u32 ctl, stride;
11155
11156 ctl = I915_READ(PLANE_CTL(pipe, 0));
11157 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11158 switch (fb->modifier[0]) {
11159 case DRM_FORMAT_MOD_NONE:
11160 break;
11161 case I915_FORMAT_MOD_X_TILED:
ff944564 11162 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11163 break;
11164 case I915_FORMAT_MOD_Y_TILED:
11165 ctl |= PLANE_CTL_TILED_Y;
11166 break;
11167 case I915_FORMAT_MOD_Yf_TILED:
11168 ctl |= PLANE_CTL_TILED_YF;
11169 break;
11170 default:
11171 MISSING_CASE(fb->modifier[0]);
11172 }
ff944564
DL
11173
11174 /*
11175 * The stride is either expressed as a multiple of 64 bytes chunks for
11176 * linear buffers or in number of tiles for tiled buffers.
11177 */
2ebef630
TU
11178 stride = fb->pitches[0] /
11179 intel_fb_stride_alignment(dev, fb->modifier[0],
11180 fb->pixel_format);
ff944564
DL
11181
11182 /*
11183 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11184 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11185 */
11186 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11187 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11188
11189 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11190 POSTING_READ(PLANE_SURF(pipe, 0));
11191}
11192
11193static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
11194{
11195 struct drm_device *dev = intel_crtc->base.dev;
11196 struct drm_i915_private *dev_priv = dev->dev_private;
11197 struct intel_framebuffer *intel_fb =
11198 to_intel_framebuffer(intel_crtc->base.primary->fb);
11199 struct drm_i915_gem_object *obj = intel_fb->obj;
11200 u32 dspcntr;
11201 u32 reg;
11202
84c33a64
SG
11203 reg = DSPCNTR(intel_crtc->plane);
11204 dspcntr = I915_READ(reg);
11205
c5d97472
DL
11206 if (obj->tiling_mode != I915_TILING_NONE)
11207 dspcntr |= DISPPLANE_TILED;
11208 else
11209 dspcntr &= ~DISPPLANE_TILED;
11210
84c33a64
SG
11211 I915_WRITE(reg, dspcntr);
11212
11213 I915_WRITE(DSPSURF(intel_crtc->plane),
11214 intel_crtc->unpin_work->gtt_offset);
11215 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 11216
ff944564
DL
11217}
11218
11219/*
11220 * XXX: This is the temporary way to update the plane registers until we get
11221 * around to using the usual plane update functions for MMIO flips
11222 */
11223static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11224{
11225 struct drm_device *dev = intel_crtc->base.dev;
11226 bool atomic_update;
11227 u32 start_vbl_count;
11228
11229 intel_mark_page_flip_active(intel_crtc);
11230
11231 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11232
11233 if (INTEL_INFO(dev)->gen >= 9)
11234 skl_do_mmio_flip(intel_crtc);
11235 else
11236 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11237 ilk_do_mmio_flip(intel_crtc);
11238
9362c7c5
ACO
11239 if (atomic_update)
11240 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
11241}
11242
9362c7c5 11243static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11244{
b2cfe0ab
CW
11245 struct intel_mmio_flip *mmio_flip =
11246 container_of(work, struct intel_mmio_flip, work);
84c33a64 11247
eed29a5b
DV
11248 if (mmio_flip->req)
11249 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11250 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11251 false, NULL,
11252 &mmio_flip->i915->rps.mmioflips));
84c33a64 11253
b2cfe0ab
CW
11254 intel_do_mmio_flip(mmio_flip->crtc);
11255
eed29a5b 11256 i915_gem_request_unreference__unlocked(mmio_flip->req);
b2cfe0ab 11257 kfree(mmio_flip);
84c33a64
SG
11258}
11259
11260static int intel_queue_mmio_flip(struct drm_device *dev,
11261 struct drm_crtc *crtc,
11262 struct drm_framebuffer *fb,
11263 struct drm_i915_gem_object *obj,
11264 struct intel_engine_cs *ring,
11265 uint32_t flags)
11266{
b2cfe0ab
CW
11267 struct intel_mmio_flip *mmio_flip;
11268
11269 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11270 if (mmio_flip == NULL)
11271 return -ENOMEM;
84c33a64 11272
bcafc4e3 11273 mmio_flip->i915 = to_i915(dev);
eed29a5b 11274 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11275 mmio_flip->crtc = to_intel_crtc(crtc);
536f5b5e 11276
b2cfe0ab
CW
11277 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11278 schedule_work(&mmio_flip->work);
84c33a64 11279
84c33a64
SG
11280 return 0;
11281}
11282
8c9f3aaf
JB
11283static int intel_default_queue_flip(struct drm_device *dev,
11284 struct drm_crtc *crtc,
11285 struct drm_framebuffer *fb,
ed8d1975 11286 struct drm_i915_gem_object *obj,
a4872ba6 11287 struct intel_engine_cs *ring,
ed8d1975 11288 uint32_t flags)
8c9f3aaf
JB
11289{
11290 return -ENODEV;
11291}
11292
d6bbafa1
CW
11293static bool __intel_pageflip_stall_check(struct drm_device *dev,
11294 struct drm_crtc *crtc)
11295{
11296 struct drm_i915_private *dev_priv = dev->dev_private;
11297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11298 struct intel_unpin_work *work = intel_crtc->unpin_work;
11299 u32 addr;
11300
11301 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11302 return true;
11303
11304 if (!work->enable_stall_check)
11305 return false;
11306
11307 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11308 if (work->flip_queued_req &&
11309 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11310 return false;
11311
1e3feefd 11312 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11313 }
11314
1e3feefd 11315 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11316 return false;
11317
11318 /* Potential stall - if we see that the flip has happened,
11319 * assume a missed interrupt. */
11320 if (INTEL_INFO(dev)->gen >= 4)
11321 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11322 else
11323 addr = I915_READ(DSPADDR(intel_crtc->plane));
11324
11325 /* There is a potential issue here with a false positive after a flip
11326 * to the same address. We could address this by checking for a
11327 * non-incrementing frame counter.
11328 */
11329 return addr == work->gtt_offset;
11330}
11331
11332void intel_check_page_flip(struct drm_device *dev, int pipe)
11333{
11334 struct drm_i915_private *dev_priv = dev->dev_private;
11335 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11336 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11337 struct intel_unpin_work *work;
f326038a 11338
6c51d46f 11339 WARN_ON(!in_interrupt());
d6bbafa1
CW
11340
11341 if (crtc == NULL)
11342 return;
11343
f326038a 11344 spin_lock(&dev->event_lock);
6ad790c0
CW
11345 work = intel_crtc->unpin_work;
11346 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11347 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11348 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11349 page_flip_completed(intel_crtc);
6ad790c0 11350 work = NULL;
d6bbafa1 11351 }
6ad790c0
CW
11352 if (work != NULL &&
11353 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11354 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11355 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11356}
11357
6b95a207
KH
11358static int intel_crtc_page_flip(struct drm_crtc *crtc,
11359 struct drm_framebuffer *fb,
ed8d1975
KP
11360 struct drm_pending_vblank_event *event,
11361 uint32_t page_flip_flags)
6b95a207
KH
11362{
11363 struct drm_device *dev = crtc->dev;
11364 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11365 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11366 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11367 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11368 struct drm_plane *primary = crtc->primary;
a071fa00 11369 enum pipe pipe = intel_crtc->pipe;
6b95a207 11370 struct intel_unpin_work *work;
a4872ba6 11371 struct intel_engine_cs *ring;
cf5d8a46 11372 bool mmio_flip;
52e68630 11373 int ret;
6b95a207 11374
2ff8fde1
MR
11375 /*
11376 * drm_mode_page_flip_ioctl() should already catch this, but double
11377 * check to be safe. In the future we may enable pageflipping from
11378 * a disabled primary plane.
11379 */
11380 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11381 return -EBUSY;
11382
e6a595d2 11383 /* Can't change pixel format via MI display flips. */
f4510a27 11384 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11385 return -EINVAL;
11386
11387 /*
11388 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11389 * Note that pitch changes could also affect these register.
11390 */
11391 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11392 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11393 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11394 return -EINVAL;
11395
f900db47
CW
11396 if (i915_terminally_wedged(&dev_priv->gpu_error))
11397 goto out_hang;
11398
b14c5679 11399 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11400 if (work == NULL)
11401 return -ENOMEM;
11402
6b95a207 11403 work->event = event;
b4a98e57 11404 work->crtc = crtc;
ab8d6675 11405 work->old_fb = old_fb;
6b95a207
KH
11406 INIT_WORK(&work->work, intel_unpin_work_fn);
11407
87b6b101 11408 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11409 if (ret)
11410 goto free_work;
11411
6b95a207 11412 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11413 spin_lock_irq(&dev->event_lock);
6b95a207 11414 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11415 /* Before declaring the flip queue wedged, check if
11416 * the hardware completed the operation behind our backs.
11417 */
11418 if (__intel_pageflip_stall_check(dev, crtc)) {
11419 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11420 page_flip_completed(intel_crtc);
11421 } else {
11422 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11423 spin_unlock_irq(&dev->event_lock);
468f0b44 11424
d6bbafa1
CW
11425 drm_crtc_vblank_put(crtc);
11426 kfree(work);
11427 return -EBUSY;
11428 }
6b95a207
KH
11429 }
11430 intel_crtc->unpin_work = work;
5e2d7afc 11431 spin_unlock_irq(&dev->event_lock);
6b95a207 11432
b4a98e57
CW
11433 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11434 flush_workqueue(dev_priv->wq);
11435
75dfca80 11436 /* Reference the objects for the scheduled work. */
ab8d6675 11437 drm_framebuffer_reference(work->old_fb);
05394f39 11438 drm_gem_object_reference(&obj->base);
6b95a207 11439
f4510a27 11440 crtc->primary->fb = fb;
afd65eb4 11441 update_state_fb(crtc->primary);
1ed1f968 11442
e1f99ce6 11443 work->pending_flip_obj = obj;
e1f99ce6 11444
89ed88ba
CW
11445 ret = i915_mutex_lock_interruptible(dev);
11446 if (ret)
11447 goto cleanup;
11448
b4a98e57 11449 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11450 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11451
75f7f3ec 11452 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 11453 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 11454
4fa62c89
VS
11455 if (IS_VALLEYVIEW(dev)) {
11456 ring = &dev_priv->ring[BCS];
ab8d6675 11457 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
11458 /* vlv: DISPLAY_FLIP fails to change tiling */
11459 ring = NULL;
48bf5b2d 11460 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 11461 ring = &dev_priv->ring[BCS];
4fa62c89 11462 } else if (INTEL_INFO(dev)->gen >= 7) {
b4716185 11463 ring = i915_gem_request_get_ring(obj->last_write_req);
4fa62c89
VS
11464 if (ring == NULL || ring->id != RCS)
11465 ring = &dev_priv->ring[BCS];
11466 } else {
11467 ring = &dev_priv->ring[RCS];
11468 }
11469
cf5d8a46
CW
11470 mmio_flip = use_mmio_flip(ring, obj);
11471
11472 /* When using CS flips, we want to emit semaphores between rings.
11473 * However, when using mmio flips we will create a task to do the
11474 * synchronisation, so all we want here is to pin the framebuffer
11475 * into the display plane and skip any waits.
11476 */
82bc3b2d 11477 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
cf5d8a46 11478 crtc->primary->state,
b4716185 11479 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
8c9f3aaf
JB
11480 if (ret)
11481 goto cleanup_pending;
6b95a207 11482
121920fa
TU
11483 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11484 + intel_crtc->dspaddr_offset;
4fa62c89 11485
cf5d8a46 11486 if (mmio_flip) {
84c33a64
SG
11487 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11488 page_flip_flags);
d6bbafa1
CW
11489 if (ret)
11490 goto cleanup_unpin;
11491
f06cc1b9
JH
11492 i915_gem_request_assign(&work->flip_queued_req,
11493 obj->last_write_req);
d6bbafa1 11494 } else {
d94b5030
CW
11495 if (obj->last_write_req) {
11496 ret = i915_gem_check_olr(obj->last_write_req);
11497 if (ret)
11498 goto cleanup_unpin;
11499 }
11500
84c33a64 11501 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
11502 page_flip_flags);
11503 if (ret)
11504 goto cleanup_unpin;
11505
f06cc1b9
JH
11506 i915_gem_request_assign(&work->flip_queued_req,
11507 intel_ring_get_request(ring));
d6bbafa1
CW
11508 }
11509
1e3feefd 11510 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11511 work->enable_stall_check = true;
4fa62c89 11512
ab8d6675 11513 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a071fa00
DV
11514 INTEL_FRONTBUFFER_PRIMARY(pipe));
11515
7ff0ebcc 11516 intel_fbc_disable(dev);
f99d7069 11517 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
11518 mutex_unlock(&dev->struct_mutex);
11519
e5510fac
JB
11520 trace_i915_flip_request(intel_crtc->plane, obj);
11521
6b95a207 11522 return 0;
96b099fd 11523
4fa62c89 11524cleanup_unpin:
82bc3b2d 11525 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 11526cleanup_pending:
b4a98e57 11527 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11528 mutex_unlock(&dev->struct_mutex);
11529cleanup:
f4510a27 11530 crtc->primary->fb = old_fb;
afd65eb4 11531 update_state_fb(crtc->primary);
89ed88ba
CW
11532
11533 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11534 drm_framebuffer_unreference(work->old_fb);
96b099fd 11535
5e2d7afc 11536 spin_lock_irq(&dev->event_lock);
96b099fd 11537 intel_crtc->unpin_work = NULL;
5e2d7afc 11538 spin_unlock_irq(&dev->event_lock);
96b099fd 11539
87b6b101 11540 drm_crtc_vblank_put(crtc);
7317c75e 11541free_work:
96b099fd
CW
11542 kfree(work);
11543
f900db47 11544 if (ret == -EIO) {
02e0efb5
ML
11545 struct drm_atomic_state *state;
11546 struct drm_plane_state *plane_state;
11547
f900db47 11548out_hang:
02e0efb5
ML
11549 state = drm_atomic_state_alloc(dev);
11550 if (!state)
11551 return -ENOMEM;
11552 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11553
11554retry:
11555 plane_state = drm_atomic_get_plane_state(state, primary);
11556 ret = PTR_ERR_OR_ZERO(plane_state);
11557 if (!ret) {
11558 drm_atomic_set_fb_for_plane(plane_state, fb);
11559
11560 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11561 if (!ret)
11562 ret = drm_atomic_commit(state);
11563 }
11564
11565 if (ret == -EDEADLK) {
11566 drm_modeset_backoff(state->acquire_ctx);
11567 drm_atomic_state_clear(state);
11568 goto retry;
11569 }
11570
11571 if (ret)
11572 drm_atomic_state_free(state);
11573
f0d3dad3 11574 if (ret == 0 && event) {
5e2d7afc 11575 spin_lock_irq(&dev->event_lock);
a071fa00 11576 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11577 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11578 }
f900db47 11579 }
96b099fd 11580 return ret;
6b95a207
KH
11581}
11582
da20eabd
ML
11583
11584/**
11585 * intel_wm_need_update - Check whether watermarks need updating
11586 * @plane: drm plane
11587 * @state: new plane state
11588 *
11589 * Check current plane state versus the new one to determine whether
11590 * watermarks need to be recalculated.
11591 *
11592 * Returns true or false.
11593 */
11594static bool intel_wm_need_update(struct drm_plane *plane,
11595 struct drm_plane_state *state)
11596{
11597 /* Update watermarks on tiling changes. */
11598 if (!plane->state->fb || !state->fb ||
11599 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11600 plane->state->rotation != state->rotation)
11601 return true;
11602
11603 if (plane->state->crtc_w != state->crtc_w)
11604 return true;
11605
11606 return false;
11607}
11608
11609int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11610 struct drm_plane_state *plane_state)
11611{
11612 struct drm_crtc *crtc = crtc_state->crtc;
11613 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11614 struct drm_plane *plane = plane_state->plane;
11615 struct drm_device *dev = crtc->dev;
11616 struct drm_i915_private *dev_priv = dev->dev_private;
11617 struct intel_plane_state *old_plane_state =
11618 to_intel_plane_state(plane->state);
11619 int idx = intel_crtc->base.base.id, ret;
11620 int i = drm_plane_index(plane);
11621 bool mode_changed = needs_modeset(crtc_state);
11622 bool was_crtc_enabled = crtc->state->active;
11623 bool is_crtc_enabled = crtc_state->active;
11624
11625 bool turn_off, turn_on, visible, was_visible;
11626 struct drm_framebuffer *fb = plane_state->fb;
11627
11628 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11629 plane->type != DRM_PLANE_TYPE_CURSOR) {
11630 ret = skl_update_scaler_plane(
11631 to_intel_crtc_state(crtc_state),
11632 to_intel_plane_state(plane_state));
11633 if (ret)
11634 return ret;
11635 }
11636
11637 /*
11638 * Disabling a plane is always okay; we just need to update
11639 * fb tracking in a special way since cleanup_fb() won't
11640 * get called by the plane helpers.
11641 */
11642 if (old_plane_state->base.fb && !fb)
11643 intel_crtc->atomic.disabled_planes |= 1 << i;
11644
da20eabd
ML
11645 was_visible = old_plane_state->visible;
11646 visible = to_intel_plane_state(plane_state)->visible;
11647
11648 if (!was_crtc_enabled && WARN_ON(was_visible))
11649 was_visible = false;
11650
11651 if (!is_crtc_enabled && WARN_ON(visible))
11652 visible = false;
11653
11654 if (!was_visible && !visible)
11655 return 0;
11656
11657 turn_off = was_visible && (!visible || mode_changed);
11658 turn_on = visible && (!was_visible || mode_changed);
11659
11660 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11661 plane->base.id, fb ? fb->base.id : -1);
11662
11663 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11664 plane->base.id, was_visible, visible,
11665 turn_off, turn_on, mode_changed);
11666
11667 if (intel_wm_need_update(plane, plane_state))
11668 intel_crtc->atomic.update_wm = true;
11669
11670 switch (plane->type) {
11671 case DRM_PLANE_TYPE_PRIMARY:
11672 if (visible)
11673 intel_crtc->atomic.fb_bits |=
11674 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
11675
11676 intel_crtc->atomic.wait_for_flips = true;
11677 intel_crtc->atomic.pre_disable_primary = turn_off;
11678 intel_crtc->atomic.post_enable_primary = turn_on;
11679
11680 if (turn_off)
11681 intel_crtc->atomic.disable_fbc = true;
11682
11683 /*
11684 * FBC does not work on some platforms for rotated
11685 * planes, so disable it when rotation is not 0 and
11686 * update it when rotation is set back to 0.
11687 *
11688 * FIXME: This is redundant with the fbc update done in
11689 * the primary plane enable function except that that
11690 * one is done too late. We eventually need to unify
11691 * this.
11692 */
11693
11694 if (visible &&
11695 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11696 dev_priv->fbc.crtc == intel_crtc &&
11697 plane_state->rotation != BIT(DRM_ROTATE_0))
11698 intel_crtc->atomic.disable_fbc = true;
11699
11700 /*
11701 * BDW signals flip done immediately if the plane
11702 * is disabled, even if the plane enable is already
11703 * armed to occur at the next vblank :(
11704 */
11705 if (turn_on && IS_BROADWELL(dev))
11706 intel_crtc->atomic.wait_vblank = true;
11707
11708 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11709 break;
11710 case DRM_PLANE_TYPE_CURSOR:
11711 if (visible)
11712 intel_crtc->atomic.fb_bits |=
11713 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
11714 break;
11715 case DRM_PLANE_TYPE_OVERLAY:
11716 /*
11717 * 'prepare' is never called when plane is being disabled, so
11718 * we need to handle frontbuffer tracking as a special case
11719 */
11720 if (visible)
11721 intel_crtc->atomic.fb_bits |=
11722 INTEL_FRONTBUFFER_SPRITE(intel_crtc->pipe);
11723
d032ffa0 11724 if (turn_off && !mode_changed) {
da20eabd
ML
11725 intel_crtc->atomic.wait_vblank = true;
11726 intel_crtc->atomic.update_sprite_watermarks |=
11727 1 << i;
11728 }
11729 break;
11730 }
11731 return 0;
11732}
11733
6d3a1ce7
ML
11734static bool encoders_cloneable(const struct intel_encoder *a,
11735 const struct intel_encoder *b)
11736{
11737 /* masks could be asymmetric, so check both ways */
11738 return a == b || (a->cloneable & (1 << b->type) &&
11739 b->cloneable & (1 << a->type));
11740}
11741
11742static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11743 struct intel_crtc *crtc,
11744 struct intel_encoder *encoder)
11745{
11746 struct intel_encoder *source_encoder;
11747 struct drm_connector *connector;
11748 struct drm_connector_state *connector_state;
11749 int i;
11750
11751 for_each_connector_in_state(state, connector, connector_state, i) {
11752 if (connector_state->crtc != &crtc->base)
11753 continue;
11754
11755 source_encoder =
11756 to_intel_encoder(connector_state->best_encoder);
11757 if (!encoders_cloneable(encoder, source_encoder))
11758 return false;
11759 }
11760
11761 return true;
11762}
11763
11764static bool check_encoder_cloning(struct drm_atomic_state *state,
11765 struct intel_crtc *crtc)
11766{
11767 struct intel_encoder *encoder;
11768 struct drm_connector *connector;
11769 struct drm_connector_state *connector_state;
11770 int i;
11771
11772 for_each_connector_in_state(state, connector, connector_state, i) {
11773 if (connector_state->crtc != &crtc->base)
11774 continue;
11775
11776 encoder = to_intel_encoder(connector_state->best_encoder);
11777 if (!check_single_encoder_cloning(state, crtc, encoder))
11778 return false;
11779 }
11780
11781 return true;
11782}
11783
d032ffa0
ML
11784static void intel_crtc_check_initial_planes(struct drm_crtc *crtc,
11785 struct drm_crtc_state *crtc_state)
11786{
11787 struct intel_crtc_state *pipe_config =
11788 to_intel_crtc_state(crtc_state);
11789 struct drm_plane *p;
11790 unsigned visible_mask = 0;
11791
11792 drm_for_each_plane_mask(p, crtc->dev, crtc_state->plane_mask) {
11793 struct drm_plane_state *plane_state =
11794 drm_atomic_get_existing_plane_state(crtc_state->state, p);
11795
11796 if (WARN_ON(!plane_state))
11797 continue;
11798
11799 if (!plane_state->fb)
11800 crtc_state->plane_mask &=
11801 ~(1 << drm_plane_index(p));
11802 else if (to_intel_plane_state(plane_state)->visible)
11803 visible_mask |= 1 << drm_plane_index(p);
11804 }
11805
11806 if (!visible_mask)
11807 return;
11808
11809 pipe_config->quirks &= ~PIPE_CONFIG_QUIRK_INITIAL_PLANES;
11810}
11811
6d3a1ce7
ML
11812static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11813 struct drm_crtc_state *crtc_state)
11814{
cf5a15be 11815 struct drm_device *dev = crtc->dev;
ad421372 11816 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 11817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11818 struct intel_crtc_state *pipe_config =
11819 to_intel_crtc_state(crtc_state);
6d3a1ce7 11820 struct drm_atomic_state *state = crtc_state->state;
ad421372 11821 int ret, idx = crtc->base.id;
6d3a1ce7
ML
11822 bool mode_changed = needs_modeset(crtc_state);
11823
11824 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11825 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11826 return -EINVAL;
11827 }
11828
11829 I915_STATE_WARN(crtc->state->active != intel_crtc->active,
11830 "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
11831 idx, crtc->state->active, intel_crtc->active);
11832
d032ffa0
ML
11833 /* plane mask is fixed up after all initial planes are calculated */
11834 if (pipe_config->quirks & PIPE_CONFIG_QUIRK_INITIAL_PLANES)
11835 intel_crtc_check_initial_planes(crtc, crtc_state);
11836
eddfcbcd
ML
11837 if (mode_changed)
11838 intel_crtc->atomic.update_wm = !crtc_state->active;
11839
ad421372
ML
11840 if (mode_changed && crtc_state->enable &&
11841 dev_priv->display.crtc_compute_clock &&
11842 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11843 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11844 pipe_config);
11845 if (ret)
11846 return ret;
11847 }
11848
cf5a15be 11849 return intel_atomic_setup_scalers(dev, intel_crtc, pipe_config);
6d3a1ce7
ML
11850}
11851
65b38e0d 11852static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11853 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11854 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11855 .atomic_begin = intel_begin_crtc_commit,
11856 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11857 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11858};
11859
9a935856
DV
11860/**
11861 * intel_modeset_update_staged_output_state
11862 *
11863 * Updates the staged output configuration state, e.g. after we've read out the
11864 * current hw state.
11865 */
11866static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 11867{
7668851f 11868 struct intel_crtc *crtc;
9a935856
DV
11869 struct intel_encoder *encoder;
11870 struct intel_connector *connector;
f6e5b160 11871
3a3371ff 11872 for_each_intel_connector(dev, connector) {
9a935856
DV
11873 connector->new_encoder =
11874 to_intel_encoder(connector->base.encoder);
11875 }
f6e5b160 11876
b2784e15 11877 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11878 encoder->new_crtc =
11879 to_intel_crtc(encoder->base.crtc);
11880 }
7668851f 11881
d3fcc808 11882 for_each_intel_crtc(dev, crtc) {
83d65738 11883 crtc->new_enabled = crtc->base.state->enable;
7668851f 11884 }
f6e5b160
CW
11885}
11886
d29b2f9d
ACO
11887/* Transitional helper to copy current connector/encoder state to
11888 * connector->state. This is needed so that code that is partially
11889 * converted to atomic does the right thing.
11890 */
11891static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11892{
11893 struct intel_connector *connector;
11894
11895 for_each_intel_connector(dev, connector) {
11896 if (connector->base.encoder) {
11897 connector->base.state->best_encoder =
11898 connector->base.encoder;
11899 connector->base.state->crtc =
11900 connector->base.encoder->crtc;
11901 } else {
11902 connector->base.state->best_encoder = NULL;
11903 connector->base.state->crtc = NULL;
11904 }
11905 }
11906}
11907
050f7aeb 11908static void
eba905b2 11909connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11910 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11911{
11912 int bpp = pipe_config->pipe_bpp;
11913
11914 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11915 connector->base.base.id,
c23cc417 11916 connector->base.name);
050f7aeb
DV
11917
11918 /* Don't use an invalid EDID bpc value */
11919 if (connector->base.display_info.bpc &&
11920 connector->base.display_info.bpc * 3 < bpp) {
11921 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11922 bpp, connector->base.display_info.bpc*3);
11923 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11924 }
11925
11926 /* Clamp bpp to 8 on screens without EDID 1.4 */
11927 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11928 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11929 bpp);
11930 pipe_config->pipe_bpp = 24;
11931 }
11932}
11933
4e53c2e0 11934static int
050f7aeb 11935compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11936 struct intel_crtc_state *pipe_config)
4e53c2e0 11937{
050f7aeb 11938 struct drm_device *dev = crtc->base.dev;
1486017f 11939 struct drm_atomic_state *state;
da3ced29
ACO
11940 struct drm_connector *connector;
11941 struct drm_connector_state *connector_state;
1486017f 11942 int bpp, i;
4e53c2e0 11943
d328c9d7 11944 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
4e53c2e0 11945 bpp = 10*3;
d328c9d7
DV
11946 else if (INTEL_INFO(dev)->gen >= 5)
11947 bpp = 12*3;
11948 else
11949 bpp = 8*3;
11950
4e53c2e0 11951
4e53c2e0
DV
11952 pipe_config->pipe_bpp = bpp;
11953
1486017f
ACO
11954 state = pipe_config->base.state;
11955
4e53c2e0 11956 /* Clamp display bpp to EDID value */
da3ced29
ACO
11957 for_each_connector_in_state(state, connector, connector_state, i) {
11958 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11959 continue;
11960
da3ced29
ACO
11961 connected_sink_compute_bpp(to_intel_connector(connector),
11962 pipe_config);
4e53c2e0
DV
11963 }
11964
11965 return bpp;
11966}
11967
644db711
DV
11968static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11969{
11970 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11971 "type: 0x%x flags: 0x%x\n",
1342830c 11972 mode->crtc_clock,
644db711
DV
11973 mode->crtc_hdisplay, mode->crtc_hsync_start,
11974 mode->crtc_hsync_end, mode->crtc_htotal,
11975 mode->crtc_vdisplay, mode->crtc_vsync_start,
11976 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11977}
11978
c0b03411 11979static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11980 struct intel_crtc_state *pipe_config,
c0b03411
DV
11981 const char *context)
11982{
6a60cd87
CK
11983 struct drm_device *dev = crtc->base.dev;
11984 struct drm_plane *plane;
11985 struct intel_plane *intel_plane;
11986 struct intel_plane_state *state;
11987 struct drm_framebuffer *fb;
11988
11989 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11990 context, pipe_config, pipe_name(crtc->pipe));
c0b03411
DV
11991
11992 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11993 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11994 pipe_config->pipe_bpp, pipe_config->dither);
11995 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11996 pipe_config->has_pch_encoder,
11997 pipe_config->fdi_lanes,
11998 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11999 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12000 pipe_config->fdi_m_n.tu);
eb14cb74
VS
12001 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12002 pipe_config->has_dp_encoder,
12003 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12004 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12005 pipe_config->dp_m_n.tu);
b95af8be
VK
12006
12007 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
12008 pipe_config->has_dp_encoder,
12009 pipe_config->dp_m2_n2.gmch_m,
12010 pipe_config->dp_m2_n2.gmch_n,
12011 pipe_config->dp_m2_n2.link_m,
12012 pipe_config->dp_m2_n2.link_n,
12013 pipe_config->dp_m2_n2.tu);
12014
55072d19
DV
12015 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12016 pipe_config->has_audio,
12017 pipe_config->has_infoframe);
12018
c0b03411 12019 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 12020 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 12021 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
12022 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12023 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 12024 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
12025 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12026 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
12027 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12028 crtc->num_scalers,
12029 pipe_config->scaler_state.scaler_users,
12030 pipe_config->scaler_state.scaler_id);
c0b03411
DV
12031 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12032 pipe_config->gmch_pfit.control,
12033 pipe_config->gmch_pfit.pgm_ratios,
12034 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 12035 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 12036 pipe_config->pch_pfit.pos,
fd4daa9c
CW
12037 pipe_config->pch_pfit.size,
12038 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 12039 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 12040 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 12041
415ff0f6
TU
12042 if (IS_BROXTON(dev)) {
12043 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
12044 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
12045 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
12046 pipe_config->ddi_pll_sel,
12047 pipe_config->dpll_hw_state.ebb0,
12048 pipe_config->dpll_hw_state.pll0,
12049 pipe_config->dpll_hw_state.pll1,
12050 pipe_config->dpll_hw_state.pll2,
12051 pipe_config->dpll_hw_state.pll3,
12052 pipe_config->dpll_hw_state.pll6,
12053 pipe_config->dpll_hw_state.pll8,
12054 pipe_config->dpll_hw_state.pcsdw12);
12055 } else if (IS_SKYLAKE(dev)) {
12056 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12057 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12058 pipe_config->ddi_pll_sel,
12059 pipe_config->dpll_hw_state.ctrl1,
12060 pipe_config->dpll_hw_state.cfgcr1,
12061 pipe_config->dpll_hw_state.cfgcr2);
12062 } else if (HAS_DDI(dev)) {
12063 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
12064 pipe_config->ddi_pll_sel,
12065 pipe_config->dpll_hw_state.wrpll);
12066 } else {
12067 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12068 "fp0: 0x%x, fp1: 0x%x\n",
12069 pipe_config->dpll_hw_state.dpll,
12070 pipe_config->dpll_hw_state.dpll_md,
12071 pipe_config->dpll_hw_state.fp0,
12072 pipe_config->dpll_hw_state.fp1);
12073 }
12074
6a60cd87
CK
12075 DRM_DEBUG_KMS("planes on this crtc\n");
12076 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12077 intel_plane = to_intel_plane(plane);
12078 if (intel_plane->pipe != crtc->pipe)
12079 continue;
12080
12081 state = to_intel_plane_state(plane->state);
12082 fb = state->base.fb;
12083 if (!fb) {
12084 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12085 "disabled, scaler_id = %d\n",
12086 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12087 plane->base.id, intel_plane->pipe,
12088 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12089 drm_plane_index(plane), state->scaler_id);
12090 continue;
12091 }
12092
12093 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12094 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12095 plane->base.id, intel_plane->pipe,
12096 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12097 drm_plane_index(plane));
12098 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12099 fb->base.id, fb->width, fb->height, fb->pixel_format);
12100 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12101 state->scaler_id,
12102 state->src.x1 >> 16, state->src.y1 >> 16,
12103 drm_rect_width(&state->src) >> 16,
12104 drm_rect_height(&state->src) >> 16,
12105 state->dst.x1, state->dst.y1,
12106 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12107 }
c0b03411
DV
12108}
12109
5448a00d 12110static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12111{
5448a00d
ACO
12112 struct drm_device *dev = state->dev;
12113 struct intel_encoder *encoder;
da3ced29 12114 struct drm_connector *connector;
5448a00d 12115 struct drm_connector_state *connector_state;
00f0b378 12116 unsigned int used_ports = 0;
5448a00d 12117 int i;
00f0b378
VS
12118
12119 /*
12120 * Walk the connector list instead of the encoder
12121 * list to detect the problem on ddi platforms
12122 * where there's just one encoder per digital port.
12123 */
da3ced29 12124 for_each_connector_in_state(state, connector, connector_state, i) {
5448a00d 12125 if (!connector_state->best_encoder)
00f0b378
VS
12126 continue;
12127
5448a00d
ACO
12128 encoder = to_intel_encoder(connector_state->best_encoder);
12129
12130 WARN_ON(!connector_state->crtc);
00f0b378
VS
12131
12132 switch (encoder->type) {
12133 unsigned int port_mask;
12134 case INTEL_OUTPUT_UNKNOWN:
12135 if (WARN_ON(!HAS_DDI(dev)))
12136 break;
12137 case INTEL_OUTPUT_DISPLAYPORT:
12138 case INTEL_OUTPUT_HDMI:
12139 case INTEL_OUTPUT_EDP:
12140 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12141
12142 /* the same port mustn't appear more than once */
12143 if (used_ports & port_mask)
12144 return false;
12145
12146 used_ports |= port_mask;
12147 default:
12148 break;
12149 }
12150 }
12151
12152 return true;
12153}
12154
83a57153
ACO
12155static void
12156clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12157{
12158 struct drm_crtc_state tmp_state;
663a3640 12159 struct intel_crtc_scaler_state scaler_state;
4978cc93
ACO
12160 struct intel_dpll_hw_state dpll_hw_state;
12161 enum intel_dpll_id shared_dpll;
8504c74c 12162 uint32_t ddi_pll_sel;
83a57153 12163
7546a384
ACO
12164 /* FIXME: before the switch to atomic started, a new pipe_config was
12165 * kzalloc'd. Code that depends on any field being zero should be
12166 * fixed, so that the crtc_state can be safely duplicated. For now,
12167 * only fields that are know to not cause problems are preserved. */
12168
83a57153 12169 tmp_state = crtc_state->base;
663a3640 12170 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12171 shared_dpll = crtc_state->shared_dpll;
12172 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12173 ddi_pll_sel = crtc_state->ddi_pll_sel;
4978cc93 12174
83a57153 12175 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12176
83a57153 12177 crtc_state->base = tmp_state;
663a3640 12178 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12179 crtc_state->shared_dpll = shared_dpll;
12180 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12181 crtc_state->ddi_pll_sel = ddi_pll_sel;
83a57153
ACO
12182}
12183
548ee15b 12184static int
b8cecdf5 12185intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12186 struct intel_crtc_state *pipe_config)
ee7b9f93 12187{
b359283a 12188 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12189 struct intel_encoder *encoder;
da3ced29 12190 struct drm_connector *connector;
0b901879 12191 struct drm_connector_state *connector_state;
d328c9d7 12192 int base_bpp, ret = -EINVAL;
0b901879 12193 int i;
e29c22c0 12194 bool retry = true;
ee7b9f93 12195
83a57153 12196 clear_intel_crtc_state(pipe_config);
7758a113 12197
e143a21c
DV
12198 pipe_config->cpu_transcoder =
12199 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12200
2960bc9c
ID
12201 /*
12202 * Sanitize sync polarity flags based on requested ones. If neither
12203 * positive or negative polarity is requested, treat this as meaning
12204 * negative polarity.
12205 */
2d112de7 12206 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12207 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12208 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12209
2d112de7 12210 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12211 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12212 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12213
050f7aeb
DV
12214 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12215 * plane pixel format and any sink constraints into account. Returns the
12216 * source plane bpp so that dithering can be selected on mismatches
12217 * after encoders and crtc also have had their say. */
d328c9d7
DV
12218 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12219 pipe_config);
12220 if (base_bpp < 0)
4e53c2e0
DV
12221 goto fail;
12222
e41a56be
VS
12223 /*
12224 * Determine the real pipe dimensions. Note that stereo modes can
12225 * increase the actual pipe size due to the frame doubling and
12226 * insertion of additional space for blanks between the frame. This
12227 * is stored in the crtc timings. We use the requested mode to do this
12228 * computation to clearly distinguish it from the adjusted mode, which
12229 * can be changed by the connectors in the below retry loop.
12230 */
2d112de7 12231 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12232 &pipe_config->pipe_src_w,
12233 &pipe_config->pipe_src_h);
e41a56be 12234
e29c22c0 12235encoder_retry:
ef1b460d 12236 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12237 pipe_config->port_clock = 0;
ef1b460d 12238 pipe_config->pixel_multiplier = 1;
ff9a6750 12239
135c81b8 12240 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12241 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12242 CRTC_STEREO_DOUBLE);
135c81b8 12243
7758a113
DV
12244 /* Pass our mode to the connectors and the CRTC to give them a chance to
12245 * adjust it according to limitations or connector properties, and also
12246 * a chance to reject the mode entirely.
47f1c6c9 12247 */
da3ced29 12248 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12249 if (connector_state->crtc != crtc)
7758a113 12250 continue;
7ae89233 12251
0b901879
ACO
12252 encoder = to_intel_encoder(connector_state->best_encoder);
12253
efea6e8e
DV
12254 if (!(encoder->compute_config(encoder, pipe_config))) {
12255 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12256 goto fail;
12257 }
ee7b9f93 12258 }
47f1c6c9 12259
ff9a6750
DV
12260 /* Set default port clock if not overwritten by the encoder. Needs to be
12261 * done afterwards in case the encoder adjusts the mode. */
12262 if (!pipe_config->port_clock)
2d112de7 12263 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12264 * pipe_config->pixel_multiplier;
ff9a6750 12265
a43f6e0f 12266 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12267 if (ret < 0) {
7758a113
DV
12268 DRM_DEBUG_KMS("CRTC fixup failed\n");
12269 goto fail;
ee7b9f93 12270 }
e29c22c0
DV
12271
12272 if (ret == RETRY) {
12273 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12274 ret = -EINVAL;
12275 goto fail;
12276 }
12277
12278 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12279 retry = false;
12280 goto encoder_retry;
12281 }
12282
d328c9d7 12283 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
4e53c2e0 12284 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12285 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12286
cdba954e
ACO
12287 /* Check if we need to force a modeset */
12288 if (pipe_config->has_audio !=
85a96e7a 12289 to_intel_crtc_state(crtc->state)->has_audio) {
cdba954e 12290 pipe_config->base.mode_changed = true;
85a96e7a
ML
12291 ret = drm_atomic_add_affected_planes(state, crtc);
12292 }
cdba954e
ACO
12293
12294 /*
12295 * Note we have an issue here with infoframes: current code
12296 * only updates them on the full mode set path per hw
12297 * requirements. So here we should be checking for any
12298 * required changes and forcing a mode set.
12299 */
7758a113 12300fail:
548ee15b 12301 return ret;
ee7b9f93 12302}
47f1c6c9 12303
ea9d758d 12304static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 12305{
ea9d758d 12306 struct drm_encoder *encoder;
f6e5b160 12307 struct drm_device *dev = crtc->dev;
f6e5b160 12308
ea9d758d
DV
12309 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12310 if (encoder->crtc == crtc)
12311 return true;
12312
12313 return false;
12314}
12315
12316static void
0a9ab303 12317intel_modeset_update_state(struct drm_atomic_state *state)
ea9d758d 12318{
0a9ab303 12319 struct drm_device *dev = state->dev;
ea9d758d 12320 struct intel_encoder *intel_encoder;
0a9ab303
ACO
12321 struct drm_crtc *crtc;
12322 struct drm_crtc_state *crtc_state;
ea9d758d
DV
12323 struct drm_connector *connector;
12324
de419ab6 12325 intel_shared_dpll_commit(state);
ba41c0de 12326
b2784e15 12327 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
12328 if (!intel_encoder->base.crtc)
12329 continue;
12330
69024de8
ML
12331 crtc = intel_encoder->base.crtc;
12332 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12333 if (!crtc_state || !needs_modeset(crtc->state))
12334 continue;
ea9d758d 12335
69024de8 12336 intel_encoder->connectors_active = false;
ea9d758d
DV
12337 }
12338
3cb480bc 12339 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
f7217905 12340 intel_modeset_update_staged_output_state(state->dev);
ea9d758d 12341
7668851f 12342 /* Double check state. */
0a9ab303
ACO
12343 for_each_crtc(dev, crtc) {
12344 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
3cb480bc
ML
12345
12346 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12347
12348 /* Update hwmode for vblank functions */
12349 if (crtc->state->active)
12350 crtc->hwmode = crtc->state->adjusted_mode;
12351 else
12352 crtc->hwmode.crtc_clock = 0;
ea9d758d
DV
12353 }
12354
12355 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12356 if (!connector->encoder || !connector->encoder->crtc)
12357 continue;
12358
69024de8
ML
12359 crtc = connector->encoder->crtc;
12360 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12361 if (!crtc_state || !needs_modeset(crtc->state))
12362 continue;
ea9d758d 12363
53d9f4e9 12364 if (crtc->state->active) {
69024de8
ML
12365 struct drm_property *dpms_property =
12366 dev->mode_config.dpms_property;
68d34720 12367
69024de8
ML
12368 connector->dpms = DRM_MODE_DPMS_ON;
12369 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
ea9d758d 12370
69024de8
ML
12371 intel_encoder = to_intel_encoder(connector->encoder);
12372 intel_encoder->connectors_active = true;
12373 } else
12374 connector->dpms = DRM_MODE_DPMS_OFF;
ea9d758d 12375 }
ea9d758d
DV
12376}
12377
3bd26263 12378static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12379{
3bd26263 12380 int diff;
f1f644dc
JB
12381
12382 if (clock1 == clock2)
12383 return true;
12384
12385 if (!clock1 || !clock2)
12386 return false;
12387
12388 diff = abs(clock1 - clock2);
12389
12390 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12391 return true;
12392
12393 return false;
12394}
12395
25c5b266
DV
12396#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12397 list_for_each_entry((intel_crtc), \
12398 &(dev)->mode_config.crtc_list, \
12399 base.head) \
0973f18f 12400 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12401
0e8ffe1b 12402static bool
2fa2fe9a 12403intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
12404 struct intel_crtc_state *current_config,
12405 struct intel_crtc_state *pipe_config)
0e8ffe1b 12406{
66e985c0
DV
12407#define PIPE_CONF_CHECK_X(name) \
12408 if (current_config->name != pipe_config->name) { \
12409 DRM_ERROR("mismatch in " #name " " \
12410 "(expected 0x%08x, found 0x%08x)\n", \
12411 current_config->name, \
12412 pipe_config->name); \
12413 return false; \
12414 }
12415
08a24034
DV
12416#define PIPE_CONF_CHECK_I(name) \
12417 if (current_config->name != pipe_config->name) { \
12418 DRM_ERROR("mismatch in " #name " " \
12419 "(expected %i, found %i)\n", \
12420 current_config->name, \
12421 pipe_config->name); \
12422 return false; \
88adfff1
DV
12423 }
12424
b95af8be
VK
12425/* This is required for BDW+ where there is only one set of registers for
12426 * switching between high and low RR.
12427 * This macro can be used whenever a comparison has to be made between one
12428 * hw state and multiple sw state variables.
12429 */
12430#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12431 if ((current_config->name != pipe_config->name) && \
12432 (current_config->alt_name != pipe_config->name)) { \
12433 DRM_ERROR("mismatch in " #name " " \
12434 "(expected %i or %i, found %i)\n", \
12435 current_config->name, \
12436 current_config->alt_name, \
12437 pipe_config->name); \
12438 return false; \
12439 }
12440
1bd1bd80
DV
12441#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12442 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 12443 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12444 "(expected %i, found %i)\n", \
12445 current_config->name & (mask), \
12446 pipe_config->name & (mask)); \
12447 return false; \
12448 }
12449
5e550656
VS
12450#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12451 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12452 DRM_ERROR("mismatch in " #name " " \
12453 "(expected %i, found %i)\n", \
12454 current_config->name, \
12455 pipe_config->name); \
12456 return false; \
12457 }
12458
bb760063
DV
12459#define PIPE_CONF_QUIRK(quirk) \
12460 ((current_config->quirks | pipe_config->quirks) & (quirk))
12461
eccb140b
DV
12462 PIPE_CONF_CHECK_I(cpu_transcoder);
12463
08a24034
DV
12464 PIPE_CONF_CHECK_I(has_pch_encoder);
12465 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
12466 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12467 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12468 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12469 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12470 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 12471
eb14cb74 12472 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
12473
12474 if (INTEL_INFO(dev)->gen < 8) {
12475 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12476 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12477 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12478 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12479 PIPE_CONF_CHECK_I(dp_m_n.tu);
12480
12481 if (current_config->has_drrs) {
12482 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12483 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12484 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12485 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12486 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12487 }
12488 } else {
12489 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12490 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12491 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12492 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12493 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12494 }
eb14cb74 12495
2d112de7
ACO
12496 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12497 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12498 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12499 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12500 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12501 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12502
2d112de7
ACO
12503 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12504 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12505 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12506 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12507 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12508 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12509
c93f54cf 12510 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12511 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
12512 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12513 IS_VALLEYVIEW(dev))
12514 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12515 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12516
9ed109a7
DV
12517 PIPE_CONF_CHECK_I(has_audio);
12518
2d112de7 12519 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12520 DRM_MODE_FLAG_INTERLACE);
12521
bb760063 12522 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12523 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12524 DRM_MODE_FLAG_PHSYNC);
2d112de7 12525 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12526 DRM_MODE_FLAG_NHSYNC);
2d112de7 12527 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12528 DRM_MODE_FLAG_PVSYNC);
2d112de7 12529 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12530 DRM_MODE_FLAG_NVSYNC);
12531 }
045ac3b5 12532
37327abd
VS
12533 PIPE_CONF_CHECK_I(pipe_src_w);
12534 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 12535
9953599b
DV
12536 /*
12537 * FIXME: BIOS likes to set up a cloned config with lvds+external
12538 * screen. Since we don't yet re-compute the pipe config when moving
12539 * just the lvds port away to another pipe the sw tracking won't match.
12540 *
12541 * Proper atomic modesets with recomputed global state will fix this.
12542 * Until then just don't check gmch state for inherited modes.
12543 */
12544 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12545 PIPE_CONF_CHECK_I(gmch_pfit.control);
12546 /* pfit ratios are autocomputed by the hw on gen4+ */
12547 if (INTEL_INFO(dev)->gen < 4)
12548 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12549 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12550 }
12551
fd4daa9c
CW
12552 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12553 if (current_config->pch_pfit.enabled) {
12554 PIPE_CONF_CHECK_I(pch_pfit.pos);
12555 PIPE_CONF_CHECK_I(pch_pfit.size);
12556 }
2fa2fe9a 12557
a1b2278e
CK
12558 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12559
e59150dc
JB
12560 /* BDW+ don't expose a synchronous way to read the state */
12561 if (IS_HASWELL(dev))
12562 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12563
282740f7
VS
12564 PIPE_CONF_CHECK_I(double_wide);
12565
26804afd
DV
12566 PIPE_CONF_CHECK_X(ddi_pll_sel);
12567
c0d43d62 12568 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 12569 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12570 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12571 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12572 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12573 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
12574 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12575 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12576 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12577
42571aef
VS
12578 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12579 PIPE_CONF_CHECK_I(pipe_bpp);
12580
2d112de7 12581 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12582 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12583
66e985c0 12584#undef PIPE_CONF_CHECK_X
08a24034 12585#undef PIPE_CONF_CHECK_I
b95af8be 12586#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12587#undef PIPE_CONF_CHECK_FLAGS
5e550656 12588#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12589#undef PIPE_CONF_QUIRK
88adfff1 12590
0e8ffe1b
DV
12591 return true;
12592}
12593
08db6652
DL
12594static void check_wm_state(struct drm_device *dev)
12595{
12596 struct drm_i915_private *dev_priv = dev->dev_private;
12597 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12598 struct intel_crtc *intel_crtc;
12599 int plane;
12600
12601 if (INTEL_INFO(dev)->gen < 9)
12602 return;
12603
12604 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12605 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12606
12607 for_each_intel_crtc(dev, intel_crtc) {
12608 struct skl_ddb_entry *hw_entry, *sw_entry;
12609 const enum pipe pipe = intel_crtc->pipe;
12610
12611 if (!intel_crtc->active)
12612 continue;
12613
12614 /* planes */
dd740780 12615 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12616 hw_entry = &hw_ddb.plane[pipe][plane];
12617 sw_entry = &sw_ddb->plane[pipe][plane];
12618
12619 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12620 continue;
12621
12622 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12623 "(expected (%u,%u), found (%u,%u))\n",
12624 pipe_name(pipe), plane + 1,
12625 sw_entry->start, sw_entry->end,
12626 hw_entry->start, hw_entry->end);
12627 }
12628
12629 /* cursor */
12630 hw_entry = &hw_ddb.cursor[pipe];
12631 sw_entry = &sw_ddb->cursor[pipe];
12632
12633 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12634 continue;
12635
12636 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12637 "(expected (%u,%u), found (%u,%u))\n",
12638 pipe_name(pipe),
12639 sw_entry->start, sw_entry->end,
12640 hw_entry->start, hw_entry->end);
12641 }
12642}
12643
91d1b4bd
DV
12644static void
12645check_connector_state(struct drm_device *dev)
8af6cf88 12646{
8af6cf88
DV
12647 struct intel_connector *connector;
12648
3a3371ff 12649 for_each_intel_connector(dev, connector) {
8af6cf88
DV
12650 /* This also checks the encoder/connector hw state with the
12651 * ->get_hw_state callbacks. */
12652 intel_connector_check_state(connector);
12653
e2c719b7 12654 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
8af6cf88
DV
12655 "connector's staged encoder doesn't match current encoder\n");
12656 }
91d1b4bd
DV
12657}
12658
12659static void
12660check_encoder_state(struct drm_device *dev)
12661{
12662 struct intel_encoder *encoder;
12663 struct intel_connector *connector;
8af6cf88 12664
b2784e15 12665 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12666 bool enabled = false;
12667 bool active = false;
12668 enum pipe pipe, tracked_pipe;
12669
12670 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12671 encoder->base.base.id,
8e329a03 12672 encoder->base.name);
8af6cf88 12673
e2c719b7 12674 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
8af6cf88 12675 "encoder's stage crtc doesn't match current crtc\n");
e2c719b7 12676 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
12677 "encoder's active_connectors set, but no crtc\n");
12678
3a3371ff 12679 for_each_intel_connector(dev, connector) {
8af6cf88
DV
12680 if (connector->base.encoder != &encoder->base)
12681 continue;
12682 enabled = true;
12683 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12684 active = true;
12685 }
0e32b39c
DA
12686 /*
12687 * for MST connectors if we unplug the connector is gone
12688 * away but the encoder is still connected to a crtc
12689 * until a modeset happens in response to the hotplug.
12690 */
12691 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12692 continue;
12693
e2c719b7 12694 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12695 "encoder's enabled state mismatch "
12696 "(expected %i, found %i)\n",
12697 !!encoder->base.crtc, enabled);
e2c719b7 12698 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
12699 "active encoder with no crtc\n");
12700
e2c719b7 12701 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
12702 "encoder's computed active state doesn't match tracked active state "
12703 "(expected %i, found %i)\n", active, encoder->connectors_active);
12704
12705 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 12706 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
12707 "encoder's hw state doesn't match sw tracking "
12708 "(expected %i, found %i)\n",
12709 encoder->connectors_active, active);
12710
12711 if (!encoder->base.crtc)
12712 continue;
12713
12714 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 12715 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
12716 "active encoder's pipe doesn't match"
12717 "(expected %i, found %i)\n",
12718 tracked_pipe, pipe);
12719
12720 }
91d1b4bd
DV
12721}
12722
12723static void
12724check_crtc_state(struct drm_device *dev)
12725{
fbee40df 12726 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12727 struct intel_crtc *crtc;
12728 struct intel_encoder *encoder;
5cec258b 12729 struct intel_crtc_state pipe_config;
8af6cf88 12730
d3fcc808 12731 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
12732 bool enabled = false;
12733 bool active = false;
12734
045ac3b5
JB
12735 memset(&pipe_config, 0, sizeof(pipe_config));
12736
8af6cf88
DV
12737 DRM_DEBUG_KMS("[CRTC:%d]\n",
12738 crtc->base.base.id);
12739
83d65738 12740 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
8af6cf88
DV
12741 "active crtc, but not enabled in sw tracking\n");
12742
b2784e15 12743 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12744 if (encoder->base.crtc != &crtc->base)
12745 continue;
12746 enabled = true;
12747 if (encoder->connectors_active)
12748 active = true;
12749 }
6c49f241 12750
e2c719b7 12751 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
12752 "crtc's computed active state doesn't match tracked active state "
12753 "(expected %i, found %i)\n", active, crtc->active);
83d65738 12754 I915_STATE_WARN(enabled != crtc->base.state->enable,
8af6cf88 12755 "crtc's computed enabled state doesn't match tracked enabled state "
83d65738
MR
12756 "(expected %i, found %i)\n", enabled,
12757 crtc->base.state->enable);
8af6cf88 12758
0e8ffe1b
DV
12759 active = dev_priv->display.get_pipe_config(crtc,
12760 &pipe_config);
d62cf62a 12761
b6b5d049
VS
12762 /* hw state is inconsistent with the pipe quirk */
12763 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12764 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
12765 active = crtc->active;
12766
b2784e15 12767 for_each_intel_encoder(dev, encoder) {
3eaba51c 12768 enum pipe pipe;
6c49f241
DV
12769 if (encoder->base.crtc != &crtc->base)
12770 continue;
1d37b689 12771 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
12772 encoder->get_config(encoder, &pipe_config);
12773 }
12774
e2c719b7 12775 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
12776 "crtc active state doesn't match with hw state "
12777 "(expected %i, found %i)\n", crtc->active, active);
12778
53d9f4e9
ML
12779 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12780 "transitional active state does not match atomic hw state "
12781 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12782
c0b03411 12783 if (active &&
6e3c9717 12784 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 12785 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
12786 intel_dump_pipe_config(crtc, &pipe_config,
12787 "[hw state]");
6e3c9717 12788 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
12789 "[sw state]");
12790 }
8af6cf88
DV
12791 }
12792}
12793
91d1b4bd
DV
12794static void
12795check_shared_dpll_state(struct drm_device *dev)
12796{
fbee40df 12797 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12798 struct intel_crtc *crtc;
12799 struct intel_dpll_hw_state dpll_hw_state;
12800 int i;
5358901f
DV
12801
12802 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12803 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12804 int enabled_crtcs = 0, active_crtcs = 0;
12805 bool active;
12806
12807 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12808
12809 DRM_DEBUG_KMS("%s\n", pll->name);
12810
12811 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12812
e2c719b7 12813 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12814 "more active pll users than references: %i vs %i\n",
3e369b76 12815 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12816 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12817 "pll in active use but not on in sw tracking\n");
e2c719b7 12818 I915_STATE_WARN(pll->on && !pll->active,
35c95375 12819 "pll in on but not on in use in sw tracking\n");
e2c719b7 12820 I915_STATE_WARN(pll->on != active,
5358901f
DV
12821 "pll on state mismatch (expected %i, found %i)\n",
12822 pll->on, active);
12823
d3fcc808 12824 for_each_intel_crtc(dev, crtc) {
83d65738 12825 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
12826 enabled_crtcs++;
12827 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12828 active_crtcs++;
12829 }
e2c719b7 12830 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
12831 "pll active crtcs mismatch (expected %i, found %i)\n",
12832 pll->active, active_crtcs);
e2c719b7 12833 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 12834 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 12835 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 12836
e2c719b7 12837 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12838 sizeof(dpll_hw_state)),
12839 "pll hw state mismatch\n");
5358901f 12840 }
8af6cf88
DV
12841}
12842
91d1b4bd
DV
12843void
12844intel_modeset_check_state(struct drm_device *dev)
12845{
08db6652 12846 check_wm_state(dev);
91d1b4bd
DV
12847 check_connector_state(dev);
12848 check_encoder_state(dev);
12849 check_crtc_state(dev);
12850 check_shared_dpll_state(dev);
12851}
12852
5cec258b 12853void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
12854 int dotclock)
12855{
12856 /*
12857 * FDI already provided one idea for the dotclock.
12858 * Yell if the encoder disagrees.
12859 */
2d112de7 12860 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 12861 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 12862 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
12863}
12864
80715b2f
VS
12865static void update_scanline_offset(struct intel_crtc *crtc)
12866{
12867 struct drm_device *dev = crtc->base.dev;
12868
12869 /*
12870 * The scanline counter increments at the leading edge of hsync.
12871 *
12872 * On most platforms it starts counting from vtotal-1 on the
12873 * first active line. That means the scanline counter value is
12874 * always one less than what we would expect. Ie. just after
12875 * start of vblank, which also occurs at start of hsync (on the
12876 * last active line), the scanline counter will read vblank_start-1.
12877 *
12878 * On gen2 the scanline counter starts counting from 1 instead
12879 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12880 * to keep the value positive), instead of adding one.
12881 *
12882 * On HSW+ the behaviour of the scanline counter depends on the output
12883 * type. For DP ports it behaves like most other platforms, but on HDMI
12884 * there's an extra 1 line difference. So we need to add two instead of
12885 * one to the value.
12886 */
12887 if (IS_GEN2(dev)) {
6e3c9717 12888 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12889 int vtotal;
12890
12891 vtotal = mode->crtc_vtotal;
12892 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12893 vtotal /= 2;
12894
12895 crtc->scanline_offset = vtotal - 1;
12896 } else if (HAS_DDI(dev) &&
409ee761 12897 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12898 crtc->scanline_offset = 2;
12899 } else
12900 crtc->scanline_offset = 1;
12901}
12902
ad421372 12903static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12904{
225da59b 12905 struct drm_device *dev = state->dev;
ed6739ef 12906 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 12907 struct intel_shared_dpll_config *shared_dpll = NULL;
ed6739ef 12908 struct intel_crtc *intel_crtc;
0a9ab303
ACO
12909 struct intel_crtc_state *intel_crtc_state;
12910 struct drm_crtc *crtc;
12911 struct drm_crtc_state *crtc_state;
0a9ab303 12912 int i;
ed6739ef
ACO
12913
12914 if (!dev_priv->display.crtc_compute_clock)
ad421372 12915 return;
ed6739ef 12916
0a9ab303 12917 for_each_crtc_in_state(state, crtc, crtc_state, i) {
ad421372
ML
12918 int dpll;
12919
0a9ab303 12920 intel_crtc = to_intel_crtc(crtc);
4978cc93 12921 intel_crtc_state = to_intel_crtc_state(crtc_state);
ad421372 12922 dpll = intel_crtc_state->shared_dpll;
0a9ab303 12923
ad421372 12924 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
225da59b
ACO
12925 continue;
12926
ad421372 12927 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
0a9ab303 12928
ad421372
ML
12929 if (!shared_dpll)
12930 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 12931
ad421372
ML
12932 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
12933 }
ed6739ef
ACO
12934}
12935
99d736a2
ML
12936/*
12937 * This implements the workaround described in the "notes" section of the mode
12938 * set sequence documentation. When going from no pipes or single pipe to
12939 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12940 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12941 */
12942static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12943{
12944 struct drm_crtc_state *crtc_state;
12945 struct intel_crtc *intel_crtc;
12946 struct drm_crtc *crtc;
12947 struct intel_crtc_state *first_crtc_state = NULL;
12948 struct intel_crtc_state *other_crtc_state = NULL;
12949 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12950 int i;
12951
12952 /* look at all crtc's that are going to be enabled in during modeset */
12953 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12954 intel_crtc = to_intel_crtc(crtc);
12955
12956 if (!crtc_state->active || !needs_modeset(crtc_state))
12957 continue;
12958
12959 if (first_crtc_state) {
12960 other_crtc_state = to_intel_crtc_state(crtc_state);
12961 break;
12962 } else {
12963 first_crtc_state = to_intel_crtc_state(crtc_state);
12964 first_pipe = intel_crtc->pipe;
12965 }
12966 }
12967
12968 /* No workaround needed? */
12969 if (!first_crtc_state)
12970 return 0;
12971
12972 /* w/a possibly needed, check how many crtc's are already enabled. */
12973 for_each_intel_crtc(state->dev, intel_crtc) {
12974 struct intel_crtc_state *pipe_config;
12975
12976 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12977 if (IS_ERR(pipe_config))
12978 return PTR_ERR(pipe_config);
12979
12980 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12981
12982 if (!pipe_config->base.active ||
12983 needs_modeset(&pipe_config->base))
12984 continue;
12985
12986 /* 2 or more enabled crtcs means no need for w/a */
12987 if (enabled_pipe != INVALID_PIPE)
12988 return 0;
12989
12990 enabled_pipe = intel_crtc->pipe;
12991 }
12992
12993 if (enabled_pipe != INVALID_PIPE)
12994 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12995 else if (other_crtc_state)
12996 other_crtc_state->hsw_workaround_pipe = first_pipe;
12997
12998 return 0;
12999}
13000
27c329ed
ML
13001static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13002{
13003 struct drm_crtc *crtc;
13004 struct drm_crtc_state *crtc_state;
13005 int ret = 0;
13006
13007 /* add all active pipes to the state */
13008 for_each_crtc(state->dev, crtc) {
13009 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13010 if (IS_ERR(crtc_state))
13011 return PTR_ERR(crtc_state);
13012
13013 if (!crtc_state->active || needs_modeset(crtc_state))
13014 continue;
13015
13016 crtc_state->mode_changed = true;
13017
13018 ret = drm_atomic_add_affected_connectors(state, crtc);
13019 if (ret)
13020 break;
13021
13022 ret = drm_atomic_add_affected_planes(state, crtc);
13023 if (ret)
13024 break;
13025 }
13026
13027 return ret;
13028}
13029
13030
054518dd 13031/* Code that should eventually be part of atomic_check() */
c347a676 13032static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd
ACO
13033{
13034 struct drm_device *dev = state->dev;
27c329ed 13035 struct drm_i915_private *dev_priv = dev->dev_private;
054518dd
ACO
13036 int ret;
13037
b359283a
ML
13038 if (!check_digital_port_conflicts(state)) {
13039 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13040 return -EINVAL;
13041 }
13042
054518dd
ACO
13043 /*
13044 * See if the config requires any additional preparation, e.g.
13045 * to adjust global state with pipes off. We need to do this
13046 * here so we can get the modeset_pipe updated config for the new
13047 * mode set on this crtc. For other crtcs we need to use the
13048 * adjusted_mode bits in the crtc directly.
13049 */
27c329ed
ML
13050 if (dev_priv->display.modeset_calc_cdclk) {
13051 unsigned int cdclk;
b432e5cf 13052
27c329ed
ML
13053 ret = dev_priv->display.modeset_calc_cdclk(state);
13054
13055 cdclk = to_intel_atomic_state(state)->cdclk;
13056 if (!ret && cdclk != dev_priv->cdclk_freq)
13057 ret = intel_modeset_all_pipes(state);
13058
13059 if (ret < 0)
054518dd 13060 return ret;
27c329ed
ML
13061 } else
13062 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
054518dd 13063
ad421372 13064 intel_modeset_clear_plls(state);
054518dd 13065
99d736a2 13066 if (IS_HASWELL(dev))
ad421372 13067 return haswell_mode_set_planes_workaround(state);
99d736a2 13068
ad421372 13069 return 0;
c347a676
ACO
13070}
13071
13072static int
13073intel_modeset_compute_config(struct drm_atomic_state *state)
13074{
13075 struct drm_crtc *crtc;
13076 struct drm_crtc_state *crtc_state;
13077 int ret, i;
61333b60 13078 bool any_ms = false;
c347a676
ACO
13079
13080 ret = drm_atomic_helper_check_modeset(state->dev, state);
054518dd
ACO
13081 if (ret)
13082 return ret;
13083
c347a676 13084 for_each_crtc_in_state(state, crtc, crtc_state, i) {
61333b60
ML
13085 if (!crtc_state->enable) {
13086 if (needs_modeset(crtc_state))
13087 any_ms = true;
c347a676 13088 continue;
61333b60 13089 }
c347a676 13090
d032ffa0
ML
13091 if (to_intel_crtc_state(crtc_state)->quirks &
13092 PIPE_CONFIG_QUIRK_INITIAL_PLANES) {
13093 ret = drm_atomic_add_affected_planes(state, crtc);
13094 if (ret)
13095 return ret;
13096
13097 /*
13098 * We ought to handle i915.fastboot here.
13099 * If no modeset is required and the primary plane has
13100 * a fb, update the members of crtc_state as needed,
13101 * and run the necessary updates during vblank evasion.
13102 */
13103 }
13104
b359283a
ML
13105 if (!needs_modeset(crtc_state)) {
13106 ret = drm_atomic_add_affected_connectors(state, crtc);
13107 if (ret)
13108 return ret;
13109 }
13110
13111 ret = intel_modeset_pipe_config(crtc,
13112 to_intel_crtc_state(crtc_state));
c347a676
ACO
13113 if (ret)
13114 return ret;
13115
61333b60
ML
13116 if (needs_modeset(crtc_state))
13117 any_ms = true;
13118
c347a676
ACO
13119 intel_dump_pipe_config(to_intel_crtc(crtc),
13120 to_intel_crtc_state(crtc_state),
13121 "[modeset]");
13122 }
13123
61333b60
ML
13124 if (any_ms) {
13125 ret = intel_modeset_checks(state);
13126
13127 if (ret)
13128 return ret;
27c329ed
ML
13129 } else
13130 to_intel_atomic_state(state)->cdclk =
13131 to_i915(state->dev)->cdclk_freq;
c347a676
ACO
13132
13133 return drm_atomic_helper_check_planes(state->dev, state);
054518dd
ACO
13134}
13135
c72d969b 13136static int __intel_set_mode(struct drm_atomic_state *state)
a6778b3c 13137{
c72d969b 13138 struct drm_device *dev = state->dev;
fbee40df 13139 struct drm_i915_private *dev_priv = dev->dev_private;
0a9ab303
ACO
13140 struct drm_crtc *crtc;
13141 struct drm_crtc_state *crtc_state;
c0c36b94 13142 int ret = 0;
0a9ab303 13143 int i;
61333b60 13144 bool any_ms = false;
a6778b3c 13145
d4afb8cc
ACO
13146 ret = drm_atomic_helper_prepare_planes(dev, state);
13147 if (ret)
13148 return ret;
13149
1c5e19f8
ML
13150 drm_atomic_helper_swap_state(dev, state);
13151
0a9ab303 13152 for_each_crtc_in_state(state, crtc, crtc_state, i) {
a539205a
ML
13153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13154
61333b60
ML
13155 if (!needs_modeset(crtc->state))
13156 continue;
13157
13158 any_ms = true;
a539205a 13159 intel_pre_plane_update(intel_crtc);
460da916 13160
a539205a
ML
13161 if (crtc_state->active) {
13162 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13163 dev_priv->display.crtc_disable(crtc);
eddfcbcd
ML
13164 intel_crtc->active = false;
13165 intel_disable_shared_dpll(intel_crtc);
a539205a 13166 }
b8cecdf5 13167 }
7758a113 13168
ea9d758d
DV
13169 /* Only after disabling all output pipelines that will be changed can we
13170 * update the the output configuration. */
0a9ab303 13171 intel_modeset_update_state(state);
f6e5b160 13172
a821fc46
ACO
13173 /* The state has been swaped above, so state actually contains the
13174 * old state now. */
61333b60
ML
13175 if (any_ms)
13176 modeset_update_crtc_power_domains(state);
47fab737 13177
a6778b3c 13178 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
0a9ab303 13179 for_each_crtc_in_state(state, crtc, crtc_state, i) {
a539205a
ML
13180 if (needs_modeset(crtc->state) && crtc->state->active) {
13181 update_scanline_offset(to_intel_crtc(crtc));
13182 dev_priv->display.crtc_enable(crtc);
13183 }
80715b2f 13184
a539205a 13185 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
80715b2f 13186 }
a6778b3c 13187
a6778b3c 13188 /* FIXME: add subpixel order */
83a57153 13189
d4afb8cc
ACO
13190 drm_atomic_helper_cleanup_planes(dev, state);
13191
2bfb4627
ACO
13192 drm_atomic_state_free(state);
13193
9eb45f22 13194 return 0;
f6e5b160
CW
13195}
13196
568c634a 13197static int intel_set_mode_checked(struct drm_atomic_state *state)
f30da187 13198{
568c634a 13199 struct drm_device *dev = state->dev;
f30da187
DV
13200 int ret;
13201
568c634a 13202 ret = __intel_set_mode(state);
f30da187 13203 if (ret == 0)
568c634a 13204 intel_modeset_check_state(dev);
f30da187
DV
13205
13206 return ret;
13207}
13208
568c634a 13209static int intel_set_mode(struct drm_atomic_state *state)
7f27126e 13210{
568c634a 13211 int ret;
83a57153 13212
568c634a 13213 ret = intel_modeset_compute_config(state);
83a57153 13214 if (ret)
568c634a 13215 return ret;
7f27126e 13216
568c634a 13217 return intel_set_mode_checked(state);
7f27126e
JB
13218}
13219
c0c36b94
CW
13220void intel_crtc_restore_mode(struct drm_crtc *crtc)
13221{
83a57153
ACO
13222 struct drm_device *dev = crtc->dev;
13223 struct drm_atomic_state *state;
4be07317 13224 struct intel_crtc *intel_crtc;
83a57153
ACO
13225 struct intel_encoder *encoder;
13226 struct intel_connector *connector;
13227 struct drm_connector_state *connector_state;
4be07317 13228 struct intel_crtc_state *crtc_state;
2bfb4627 13229 int ret;
83a57153
ACO
13230
13231 state = drm_atomic_state_alloc(dev);
13232 if (!state) {
13233 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13234 crtc->base.id);
13235 return;
13236 }
13237
13238 state->acquire_ctx = dev->mode_config.acquire_ctx;
13239
13240 /* The force restore path in the HW readout code relies on the staged
13241 * config still keeping the user requested config while the actual
13242 * state has been overwritten by the configuration read from HW. We
13243 * need to copy the staged config to the atomic state, otherwise the
13244 * mode set will just reapply the state the HW is already in. */
13245 for_each_intel_encoder(dev, encoder) {
13246 if (&encoder->new_crtc->base != crtc)
13247 continue;
13248
13249 for_each_intel_connector(dev, connector) {
13250 if (connector->new_encoder != encoder)
13251 continue;
13252
13253 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13254 if (IS_ERR(connector_state)) {
13255 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13256 connector->base.base.id,
13257 connector->base.name,
13258 PTR_ERR(connector_state));
13259 continue;
13260 }
13261
13262 connector_state->crtc = crtc;
13263 connector_state->best_encoder = &encoder->base;
13264 }
13265 }
13266
4be07317
ACO
13267 for_each_intel_crtc(dev, intel_crtc) {
13268 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
13269 continue;
13270
13271 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
13272 if (IS_ERR(crtc_state)) {
13273 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13274 intel_crtc->base.base.id,
13275 PTR_ERR(crtc_state));
13276 continue;
13277 }
13278
49d6fa21
ML
13279 crtc_state->base.active = crtc_state->base.enable =
13280 intel_crtc->new_enabled;
8c7b5ccb
ACO
13281
13282 if (&intel_crtc->base == crtc)
13283 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
4be07317
ACO
13284 }
13285
d3a40d1b
ACO
13286 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13287 crtc->primary->fb, crtc->x, crtc->y);
13288
568c634a 13289 ret = intel_set_mode(state);
2bfb4627
ACO
13290 if (ret)
13291 drm_atomic_state_free(state);
c0c36b94
CW
13292}
13293
25c5b266
DV
13294#undef for_each_intel_crtc_masked
13295
b7885264
ACO
13296static bool intel_connector_in_mode_set(struct intel_connector *connector,
13297 struct drm_mode_set *set)
13298{
13299 int ro;
13300
13301 for (ro = 0; ro < set->num_connectors; ro++)
13302 if (set->connectors[ro] == &connector->base)
13303 return true;
13304
13305 return false;
13306}
13307
2e431051 13308static int
9a935856
DV
13309intel_modeset_stage_output_state(struct drm_device *dev,
13310 struct drm_mode_set *set,
944b0c76 13311 struct drm_atomic_state *state)
50f56119 13312{
9a935856 13313 struct intel_connector *connector;
d5432a9d 13314 struct drm_connector *drm_connector;
944b0c76 13315 struct drm_connector_state *connector_state;
d5432a9d
ACO
13316 struct drm_crtc *crtc;
13317 struct drm_crtc_state *crtc_state;
13318 int i, ret;
50f56119 13319
9abdda74 13320 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
13321 * of connectors. For paranoia, double-check this. */
13322 WARN_ON(!set->fb && (set->num_connectors != 0));
13323 WARN_ON(set->fb && (set->num_connectors == 0));
13324
3a3371ff 13325 for_each_intel_connector(dev, connector) {
b7885264
ACO
13326 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13327
d5432a9d
ACO
13328 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13329 continue;
13330
13331 connector_state =
13332 drm_atomic_get_connector_state(state, &connector->base);
13333 if (IS_ERR(connector_state))
13334 return PTR_ERR(connector_state);
13335
b7885264
ACO
13336 if (in_mode_set) {
13337 int pipe = to_intel_crtc(set->crtc)->pipe;
d5432a9d
ACO
13338 connector_state->best_encoder =
13339 &intel_find_encoder(connector, pipe)->base;
50f56119
DV
13340 }
13341
d5432a9d 13342 if (connector->base.state->crtc != set->crtc)
b7885264
ACO
13343 continue;
13344
9a935856
DV
13345 /* If we disable the crtc, disable all its connectors. Also, if
13346 * the connector is on the changing crtc but not on the new
13347 * connector list, disable it. */
b7885264 13348 if (!set->fb || !in_mode_set) {
d5432a9d 13349 connector_state->best_encoder = NULL;
9a935856
DV
13350
13351 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13352 connector->base.base.id,
c23cc417 13353 connector->base.name);
9a935856 13354 }
50f56119 13355 }
9a935856 13356 /* connector->new_encoder is now updated for all connectors. */
50f56119 13357
d5432a9d
ACO
13358 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13359 connector = to_intel_connector(drm_connector);
13360
13361 if (!connector_state->best_encoder) {
13362 ret = drm_atomic_set_crtc_for_connector(connector_state,
13363 NULL);
13364 if (ret)
13365 return ret;
7668851f 13366
50f56119 13367 continue;
d5432a9d 13368 }
50f56119 13369
d5432a9d
ACO
13370 if (intel_connector_in_mode_set(connector, set)) {
13371 struct drm_crtc *crtc = connector->base.state->crtc;
13372
13373 /* If this connector was in a previous crtc, add it
13374 * to the state. We might need to disable it. */
13375 if (crtc) {
13376 crtc_state =
13377 drm_atomic_get_crtc_state(state, crtc);
13378 if (IS_ERR(crtc_state))
13379 return PTR_ERR(crtc_state);
13380 }
13381
13382 ret = drm_atomic_set_crtc_for_connector(connector_state,
13383 set->crtc);
13384 if (ret)
13385 return ret;
13386 }
50f56119
DV
13387
13388 /* Make sure the new CRTC will work with the encoder */
d5432a9d
ACO
13389 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13390 connector_state->crtc)) {
5e2b584e 13391 return -EINVAL;
50f56119 13392 }
944b0c76 13393
9a935856
DV
13394 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13395 connector->base.base.id,
c23cc417 13396 connector->base.name,
d5432a9d 13397 connector_state->crtc->base.id);
944b0c76 13398
d5432a9d
ACO
13399 if (connector_state->best_encoder != &connector->encoder->base)
13400 connector->encoder =
13401 to_intel_encoder(connector_state->best_encoder);
0e32b39c 13402 }
7668851f 13403
d5432a9d 13404 for_each_crtc_in_state(state, crtc, crtc_state, i) {
49d6fa21
ML
13405 bool has_connectors;
13406
d5432a9d
ACO
13407 ret = drm_atomic_add_affected_connectors(state, crtc);
13408 if (ret)
13409 return ret;
4be07317 13410
49d6fa21
ML
13411 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13412 if (has_connectors != crtc_state->enable)
13413 crtc_state->enable =
13414 crtc_state->active = has_connectors;
7668851f
VS
13415 }
13416
8c7b5ccb
ACO
13417 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13418 set->fb, set->x, set->y);
13419 if (ret)
13420 return ret;
13421
13422 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13423 if (IS_ERR(crtc_state))
13424 return PTR_ERR(crtc_state);
13425
ce52299c
MR
13426 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
13427 if (ret)
13428 return ret;
8c7b5ccb
ACO
13429
13430 if (set->num_connectors)
13431 crtc_state->active = true;
13432
2e431051
DV
13433 return 0;
13434}
13435
13436static int intel_crtc_set_config(struct drm_mode_set *set)
13437{
13438 struct drm_device *dev;
83a57153 13439 struct drm_atomic_state *state = NULL;
2e431051 13440 int ret;
2e431051 13441
8d3e375e
DV
13442 BUG_ON(!set);
13443 BUG_ON(!set->crtc);
13444 BUG_ON(!set->crtc->helper_private);
2e431051 13445
7e53f3a4
DV
13446 /* Enforce sane interface api - has been abused by the fb helper. */
13447 BUG_ON(!set->mode && set->fb);
13448 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 13449
2e431051
DV
13450 if (set->fb) {
13451 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13452 set->crtc->base.id, set->fb->base.id,
13453 (int)set->num_connectors, set->x, set->y);
13454 } else {
13455 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
13456 }
13457
13458 dev = set->crtc->dev;
13459
83a57153 13460 state = drm_atomic_state_alloc(dev);
7cbf41d6
ACO
13461 if (!state)
13462 return -ENOMEM;
83a57153
ACO
13463
13464 state->acquire_ctx = dev->mode_config.acquire_ctx;
13465
462a425a 13466 ret = intel_modeset_stage_output_state(dev, set, state);
2e431051 13467 if (ret)
7cbf41d6 13468 goto out;
2e431051 13469
568c634a
ACO
13470 ret = intel_modeset_compute_config(state);
13471 if (ret)
7cbf41d6 13472 goto out;
50f52756 13473
1f9954d0
JB
13474 intel_update_pipe_size(to_intel_crtc(set->crtc));
13475
568c634a 13476 ret = intel_set_mode_checked(state);
2d05eae1 13477 if (ret) {
bf67dfeb
DV
13478 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13479 set->crtc->base.id, ret);
2d05eae1 13480 }
50f56119 13481
7cbf41d6 13482out:
2bfb4627
ACO
13483 if (ret)
13484 drm_atomic_state_free(state);
50f56119
DV
13485 return ret;
13486}
f6e5b160
CW
13487
13488static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13489 .gamma_set = intel_crtc_gamma_set,
50f56119 13490 .set_config = intel_crtc_set_config,
f6e5b160
CW
13491 .destroy = intel_crtc_destroy,
13492 .page_flip = intel_crtc_page_flip,
1356837e
MR
13493 .atomic_duplicate_state = intel_crtc_duplicate_state,
13494 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13495};
13496
5358901f
DV
13497static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13498 struct intel_shared_dpll *pll,
13499 struct intel_dpll_hw_state *hw_state)
ee7b9f93 13500{
5358901f 13501 uint32_t val;
ee7b9f93 13502
f458ebbc 13503 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
13504 return false;
13505
5358901f 13506 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
13507 hw_state->dpll = val;
13508 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13509 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
13510
13511 return val & DPLL_VCO_ENABLE;
13512}
13513
15bdd4cf
DV
13514static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13515 struct intel_shared_dpll *pll)
13516{
3e369b76
ACO
13517 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13518 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
13519}
13520
e7b903d2
DV
13521static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13522 struct intel_shared_dpll *pll)
13523{
e7b903d2 13524 /* PCH refclock must be enabled first */
89eff4be 13525 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 13526
3e369b76 13527 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
13528
13529 /* Wait for the clocks to stabilize. */
13530 POSTING_READ(PCH_DPLL(pll->id));
13531 udelay(150);
13532
13533 /* The pixel multiplier can only be updated once the
13534 * DPLL is enabled and the clocks are stable.
13535 *
13536 * So write it again.
13537 */
3e369b76 13538 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 13539 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13540 udelay(200);
13541}
13542
13543static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13544 struct intel_shared_dpll *pll)
13545{
13546 struct drm_device *dev = dev_priv->dev;
13547 struct intel_crtc *crtc;
e7b903d2
DV
13548
13549 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 13550 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
13551 if (intel_crtc_to_shared_dpll(crtc) == pll)
13552 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
13553 }
13554
15bdd4cf
DV
13555 I915_WRITE(PCH_DPLL(pll->id), 0);
13556 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13557 udelay(200);
13558}
13559
46edb027
DV
13560static char *ibx_pch_dpll_names[] = {
13561 "PCH DPLL A",
13562 "PCH DPLL B",
13563};
13564
7c74ade1 13565static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 13566{
e7b903d2 13567 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
13568 int i;
13569
7c74ade1 13570 dev_priv->num_shared_dpll = 2;
ee7b9f93 13571
e72f9fbf 13572 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
13573 dev_priv->shared_dplls[i].id = i;
13574 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 13575 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
13576 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13577 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
13578 dev_priv->shared_dplls[i].get_hw_state =
13579 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
13580 }
13581}
13582
7c74ade1
DV
13583static void intel_shared_dpll_init(struct drm_device *dev)
13584{
e7b903d2 13585 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 13586
b6283055
VS
13587 intel_update_cdclk(dev);
13588
9cd86933
DV
13589 if (HAS_DDI(dev))
13590 intel_ddi_pll_init(dev);
13591 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
13592 ibx_pch_dpll_init(dev);
13593 else
13594 dev_priv->num_shared_dpll = 0;
13595
13596 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
13597}
13598
6beb8c23
MR
13599/**
13600 * intel_prepare_plane_fb - Prepare fb for usage on plane
13601 * @plane: drm plane to prepare for
13602 * @fb: framebuffer to prepare for presentation
13603 *
13604 * Prepares a framebuffer for usage on a display plane. Generally this
13605 * involves pinning the underlying object and updating the frontbuffer tracking
13606 * bits. Some older platforms need special physical address handling for
13607 * cursor planes.
13608 *
13609 * Returns 0 on success, negative error code on failure.
13610 */
13611int
13612intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee
TU
13613 struct drm_framebuffer *fb,
13614 const struct drm_plane_state *new_state)
465c120c
MR
13615{
13616 struct drm_device *dev = plane->dev;
6beb8c23
MR
13617 struct intel_plane *intel_plane = to_intel_plane(plane);
13618 enum pipe pipe = intel_plane->pipe;
13619 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13620 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13621 unsigned frontbuffer_bits = 0;
13622 int ret = 0;
465c120c 13623
ea2c67bb 13624 if (!obj)
465c120c
MR
13625 return 0;
13626
6beb8c23
MR
13627 switch (plane->type) {
13628 case DRM_PLANE_TYPE_PRIMARY:
13629 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13630 break;
13631 case DRM_PLANE_TYPE_CURSOR:
13632 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13633 break;
13634 case DRM_PLANE_TYPE_OVERLAY:
13635 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13636 break;
13637 }
465c120c 13638
6beb8c23 13639 mutex_lock(&dev->struct_mutex);
465c120c 13640
6beb8c23
MR
13641 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13642 INTEL_INFO(dev)->cursor_needs_physical) {
13643 int align = IS_I830(dev) ? 16 * 1024 : 256;
13644 ret = i915_gem_object_attach_phys(obj, align);
13645 if (ret)
13646 DRM_DEBUG_KMS("failed to attach phys object\n");
13647 } else {
82bc3b2d 13648 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
6beb8c23 13649 }
465c120c 13650
6beb8c23
MR
13651 if (ret == 0)
13652 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
fdd508a6 13653
4c34574f 13654 mutex_unlock(&dev->struct_mutex);
465c120c 13655
6beb8c23
MR
13656 return ret;
13657}
13658
38f3ce3a
MR
13659/**
13660 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13661 * @plane: drm plane to clean up for
13662 * @fb: old framebuffer that was on plane
13663 *
13664 * Cleans up a framebuffer that has just been removed from a plane.
13665 */
13666void
13667intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee
TU
13668 struct drm_framebuffer *fb,
13669 const struct drm_plane_state *old_state)
38f3ce3a
MR
13670{
13671 struct drm_device *dev = plane->dev;
13672 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13673
13674 if (WARN_ON(!obj))
13675 return;
13676
13677 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13678 !INTEL_INFO(dev)->cursor_needs_physical) {
13679 mutex_lock(&dev->struct_mutex);
82bc3b2d 13680 intel_unpin_fb_obj(fb, old_state);
38f3ce3a
MR
13681 mutex_unlock(&dev->struct_mutex);
13682 }
465c120c
MR
13683}
13684
6156a456
CK
13685int
13686skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13687{
13688 int max_scale;
13689 struct drm_device *dev;
13690 struct drm_i915_private *dev_priv;
13691 int crtc_clock, cdclk;
13692
13693 if (!intel_crtc || !crtc_state)
13694 return DRM_PLANE_HELPER_NO_SCALING;
13695
13696 dev = intel_crtc->base.dev;
13697 dev_priv = dev->dev_private;
13698 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 13699 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456
CK
13700
13701 if (!crtc_clock || !cdclk)
13702 return DRM_PLANE_HELPER_NO_SCALING;
13703
13704 /*
13705 * skl max scale is lower of:
13706 * close to 3 but not 3, -1 is for that purpose
13707 * or
13708 * cdclk/crtc_clock
13709 */
13710 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13711
13712 return max_scale;
13713}
13714
465c120c 13715static int
3c692a41 13716intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13717 struct intel_crtc_state *crtc_state,
3c692a41
GP
13718 struct intel_plane_state *state)
13719{
2b875c22
MR
13720 struct drm_crtc *crtc = state->base.crtc;
13721 struct drm_framebuffer *fb = state->base.fb;
6156a456 13722 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13723 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13724 bool can_position = false;
465c120c 13725
061e4b8d
ML
13726 /* use scaler when colorkey is not required */
13727 if (INTEL_INFO(plane->dev)->gen >= 9 &&
818ed961 13728 state->ckey.flags == I915_SET_COLORKEY_NONE) {
061e4b8d
ML
13729 min_scale = 1;
13730 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
d8106366 13731 can_position = true;
6156a456 13732 }
d8106366 13733
061e4b8d
ML
13734 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13735 &state->dst, &state->clip,
da20eabd
ML
13736 min_scale, max_scale,
13737 can_position, true,
13738 &state->visible);
14af293f
GP
13739}
13740
13741static void
13742intel_commit_primary_plane(struct drm_plane *plane,
13743 struct intel_plane_state *state)
13744{
2b875c22
MR
13745 struct drm_crtc *crtc = state->base.crtc;
13746 struct drm_framebuffer *fb = state->base.fb;
13747 struct drm_device *dev = plane->dev;
14af293f 13748 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 13749 struct intel_crtc *intel_crtc;
14af293f
GP
13750 struct drm_rect *src = &state->src;
13751
ea2c67bb
MR
13752 crtc = crtc ? crtc : plane->crtc;
13753 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
13754
13755 plane->fb = fb;
9dc806fc
MR
13756 crtc->x = src->x1 >> 16;
13757 crtc->y = src->y1 >> 16;
ccc759dc 13758
a539205a 13759 if (!crtc->state->active)
302d19ac 13760 return;
465c120c 13761
302d19ac
ML
13762 if (state->visible)
13763 /* FIXME: kill this fastboot hack */
13764 intel_update_pipe_size(intel_crtc);
13765
13766 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
465c120c
MR
13767}
13768
a8ad0d8e
ML
13769static void
13770intel_disable_primary_plane(struct drm_plane *plane,
7fabf5ef 13771 struct drm_crtc *crtc)
a8ad0d8e
ML
13772{
13773 struct drm_device *dev = plane->dev;
13774 struct drm_i915_private *dev_priv = dev->dev_private;
13775
a8ad0d8e
ML
13776 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13777}
13778
32b7eeec 13779static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 13780{
32b7eeec 13781 struct drm_device *dev = crtc->dev;
140fd38d 13782 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 13783 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3c692a41 13784
a539205a
ML
13785 if (!needs_modeset(crtc->state))
13786 intel_pre_plane_update(intel_crtc);
3c692a41 13787
32b7eeec
MR
13788 if (intel_crtc->atomic.update_wm)
13789 intel_update_watermarks(crtc);
3c692a41 13790
32b7eeec 13791 intel_runtime_pm_get(dev_priv);
3c692a41 13792
c34c9ee4 13793 /* Perform vblank evasion around commit operation */
a539205a 13794 if (crtc->state->active)
c34c9ee4
MR
13795 intel_crtc->atomic.evade =
13796 intel_pipe_update_start(intel_crtc,
13797 &intel_crtc->atomic.start_vbl_count);
0583236e
ML
13798
13799 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13800 skl_detach_scalers(intel_crtc);
32b7eeec
MR
13801}
13802
13803static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13804{
13805 struct drm_device *dev = crtc->dev;
13806 struct drm_i915_private *dev_priv = dev->dev_private;
13807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
32b7eeec 13808
c34c9ee4
MR
13809 if (intel_crtc->atomic.evade)
13810 intel_pipe_update_end(intel_crtc,
13811 intel_crtc->atomic.start_vbl_count);
3c692a41 13812
140fd38d 13813 intel_runtime_pm_put(dev_priv);
3c692a41 13814
ac21b225 13815 intel_post_plane_update(intel_crtc);
3c692a41
GP
13816}
13817
cf4c7c12 13818/**
4a3b8769
MR
13819 * intel_plane_destroy - destroy a plane
13820 * @plane: plane to destroy
cf4c7c12 13821 *
4a3b8769
MR
13822 * Common destruction function for all types of planes (primary, cursor,
13823 * sprite).
cf4c7c12 13824 */
4a3b8769 13825void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13826{
13827 struct intel_plane *intel_plane = to_intel_plane(plane);
13828 drm_plane_cleanup(plane);
13829 kfree(intel_plane);
13830}
13831
65a3fea0 13832const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13833 .update_plane = drm_atomic_helper_update_plane,
13834 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13835 .destroy = intel_plane_destroy,
c196e1d6 13836 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13837 .atomic_get_property = intel_plane_atomic_get_property,
13838 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13839 .atomic_duplicate_state = intel_plane_duplicate_state,
13840 .atomic_destroy_state = intel_plane_destroy_state,
13841
465c120c
MR
13842};
13843
13844static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13845 int pipe)
13846{
13847 struct intel_plane *primary;
8e7d688b 13848 struct intel_plane_state *state;
465c120c
MR
13849 const uint32_t *intel_primary_formats;
13850 int num_formats;
13851
13852 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13853 if (primary == NULL)
13854 return NULL;
13855
8e7d688b
MR
13856 state = intel_create_plane_state(&primary->base);
13857 if (!state) {
ea2c67bb
MR
13858 kfree(primary);
13859 return NULL;
13860 }
8e7d688b 13861 primary->base.state = &state->base;
ea2c67bb 13862
465c120c
MR
13863 primary->can_scale = false;
13864 primary->max_downscale = 1;
6156a456
CK
13865 if (INTEL_INFO(dev)->gen >= 9) {
13866 primary->can_scale = true;
af99ceda 13867 state->scaler_id = -1;
6156a456 13868 }
465c120c
MR
13869 primary->pipe = pipe;
13870 primary->plane = pipe;
c59cb179
MR
13871 primary->check_plane = intel_check_primary_plane;
13872 primary->commit_plane = intel_commit_primary_plane;
a8ad0d8e 13873 primary->disable_plane = intel_disable_primary_plane;
465c120c
MR
13874 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13875 primary->plane = !pipe;
13876
6c0fd451
DL
13877 if (INTEL_INFO(dev)->gen >= 9) {
13878 intel_primary_formats = skl_primary_formats;
13879 num_formats = ARRAY_SIZE(skl_primary_formats);
13880 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
13881 intel_primary_formats = i965_primary_formats;
13882 num_formats = ARRAY_SIZE(i965_primary_formats);
6c0fd451
DL
13883 } else {
13884 intel_primary_formats = i8xx_primary_formats;
13885 num_formats = ARRAY_SIZE(i8xx_primary_formats);
465c120c
MR
13886 }
13887
13888 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13889 &intel_plane_funcs,
465c120c
MR
13890 intel_primary_formats, num_formats,
13891 DRM_PLANE_TYPE_PRIMARY);
48404c1e 13892
3b7a5119
SJ
13893 if (INTEL_INFO(dev)->gen >= 4)
13894 intel_create_rotation_property(dev, primary);
48404c1e 13895
ea2c67bb
MR
13896 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13897
465c120c
MR
13898 return &primary->base;
13899}
13900
3b7a5119
SJ
13901void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13902{
13903 if (!dev->mode_config.rotation_property) {
13904 unsigned long flags = BIT(DRM_ROTATE_0) |
13905 BIT(DRM_ROTATE_180);
13906
13907 if (INTEL_INFO(dev)->gen >= 9)
13908 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13909
13910 dev->mode_config.rotation_property =
13911 drm_mode_create_rotation_property(dev, flags);
13912 }
13913 if (dev->mode_config.rotation_property)
13914 drm_object_attach_property(&plane->base.base,
13915 dev->mode_config.rotation_property,
13916 plane->base.state->rotation);
13917}
13918
3d7d6510 13919static int
852e787c 13920intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 13921 struct intel_crtc_state *crtc_state,
852e787c 13922 struct intel_plane_state *state)
3d7d6510 13923{
061e4b8d 13924 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 13925 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 13926 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
757f9a3e
GP
13927 unsigned stride;
13928 int ret;
3d7d6510 13929
061e4b8d
ML
13930 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13931 &state->dst, &state->clip,
3d7d6510
MR
13932 DRM_PLANE_HELPER_NO_SCALING,
13933 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13934 true, true, &state->visible);
757f9a3e
GP
13935 if (ret)
13936 return ret;
13937
757f9a3e
GP
13938 /* if we want to turn off the cursor ignore width and height */
13939 if (!obj)
da20eabd 13940 return 0;
757f9a3e 13941
757f9a3e 13942 /* Check for which cursor types we support */
061e4b8d 13943 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
13944 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13945 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13946 return -EINVAL;
13947 }
13948
ea2c67bb
MR
13949 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13950 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13951 DRM_DEBUG_KMS("buffer is too small\n");
13952 return -ENOMEM;
13953 }
13954
3a656b54 13955 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 13956 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 13957 return -EINVAL;
32b7eeec
MR
13958 }
13959
da20eabd 13960 return 0;
852e787c 13961}
3d7d6510 13962
a8ad0d8e
ML
13963static void
13964intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 13965 struct drm_crtc *crtc)
a8ad0d8e 13966{
a8ad0d8e
ML
13967 intel_crtc_update_cursor(crtc, false);
13968}
13969
f4a2cf29 13970static void
852e787c
GP
13971intel_commit_cursor_plane(struct drm_plane *plane,
13972 struct intel_plane_state *state)
13973{
2b875c22 13974 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
13975 struct drm_device *dev = plane->dev;
13976 struct intel_crtc *intel_crtc;
2b875c22 13977 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13978 uint32_t addr;
852e787c 13979
ea2c67bb
MR
13980 crtc = crtc ? crtc : plane->crtc;
13981 intel_crtc = to_intel_crtc(crtc);
13982
2b875c22 13983 plane->fb = state->base.fb;
ea2c67bb
MR
13984 crtc->cursor_x = state->base.crtc_x;
13985 crtc->cursor_y = state->base.crtc_y;
13986
a912f12f
GP
13987 if (intel_crtc->cursor_bo == obj)
13988 goto update;
4ed91096 13989
f4a2cf29 13990 if (!obj)
a912f12f 13991 addr = 0;
f4a2cf29 13992 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 13993 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 13994 else
a912f12f 13995 addr = obj->phys_handle->busaddr;
852e787c 13996
a912f12f
GP
13997 intel_crtc->cursor_addr = addr;
13998 intel_crtc->cursor_bo = obj;
852e787c 13999
302d19ac 14000update:
a539205a 14001 if (crtc->state->active)
a912f12f 14002 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
14003}
14004
3d7d6510
MR
14005static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14006 int pipe)
14007{
14008 struct intel_plane *cursor;
8e7d688b 14009 struct intel_plane_state *state;
3d7d6510
MR
14010
14011 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14012 if (cursor == NULL)
14013 return NULL;
14014
8e7d688b
MR
14015 state = intel_create_plane_state(&cursor->base);
14016 if (!state) {
ea2c67bb
MR
14017 kfree(cursor);
14018 return NULL;
14019 }
8e7d688b 14020 cursor->base.state = &state->base;
ea2c67bb 14021
3d7d6510
MR
14022 cursor->can_scale = false;
14023 cursor->max_downscale = 1;
14024 cursor->pipe = pipe;
14025 cursor->plane = pipe;
c59cb179
MR
14026 cursor->check_plane = intel_check_cursor_plane;
14027 cursor->commit_plane = intel_commit_cursor_plane;
a8ad0d8e 14028 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
14029
14030 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 14031 &intel_plane_funcs,
3d7d6510
MR
14032 intel_cursor_formats,
14033 ARRAY_SIZE(intel_cursor_formats),
14034 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
14035
14036 if (INTEL_INFO(dev)->gen >= 4) {
14037 if (!dev->mode_config.rotation_property)
14038 dev->mode_config.rotation_property =
14039 drm_mode_create_rotation_property(dev,
14040 BIT(DRM_ROTATE_0) |
14041 BIT(DRM_ROTATE_180));
14042 if (dev->mode_config.rotation_property)
14043 drm_object_attach_property(&cursor->base.base,
14044 dev->mode_config.rotation_property,
8e7d688b 14045 state->base.rotation);
4398ad45
VS
14046 }
14047
af99ceda
CK
14048 if (INTEL_INFO(dev)->gen >=9)
14049 state->scaler_id = -1;
14050
ea2c67bb
MR
14051 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14052
3d7d6510
MR
14053 return &cursor->base;
14054}
14055
549e2bfb
CK
14056static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14057 struct intel_crtc_state *crtc_state)
14058{
14059 int i;
14060 struct intel_scaler *intel_scaler;
14061 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14062
14063 for (i = 0; i < intel_crtc->num_scalers; i++) {
14064 intel_scaler = &scaler_state->scalers[i];
14065 intel_scaler->in_use = 0;
549e2bfb
CK
14066 intel_scaler->mode = PS_SCALER_MODE_DYN;
14067 }
14068
14069 scaler_state->scaler_id = -1;
14070}
14071
b358d0a6 14072static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14073{
fbee40df 14074 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14075 struct intel_crtc *intel_crtc;
f5de6e07 14076 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14077 struct drm_plane *primary = NULL;
14078 struct drm_plane *cursor = NULL;
465c120c 14079 int i, ret;
79e53945 14080
955382f3 14081 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14082 if (intel_crtc == NULL)
14083 return;
14084
f5de6e07
ACO
14085 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14086 if (!crtc_state)
14087 goto fail;
550acefd
ACO
14088 intel_crtc->config = crtc_state;
14089 intel_crtc->base.state = &crtc_state->base;
07878248 14090 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14091
549e2bfb
CK
14092 /* initialize shared scalers */
14093 if (INTEL_INFO(dev)->gen >= 9) {
14094 if (pipe == PIPE_C)
14095 intel_crtc->num_scalers = 1;
14096 else
14097 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14098
14099 skl_init_scalers(dev, intel_crtc, crtc_state);
14100 }
14101
465c120c 14102 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14103 if (!primary)
14104 goto fail;
14105
14106 cursor = intel_cursor_plane_create(dev, pipe);
14107 if (!cursor)
14108 goto fail;
14109
465c120c 14110 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
14111 cursor, &intel_crtc_funcs);
14112 if (ret)
14113 goto fail;
79e53945
JB
14114
14115 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
14116 for (i = 0; i < 256; i++) {
14117 intel_crtc->lut_r[i] = i;
14118 intel_crtc->lut_g[i] = i;
14119 intel_crtc->lut_b[i] = i;
14120 }
14121
1f1c2e24
VS
14122 /*
14123 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14124 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14125 */
80824003
JB
14126 intel_crtc->pipe = pipe;
14127 intel_crtc->plane = pipe;
3a77c4c4 14128 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14129 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14130 intel_crtc->plane = !pipe;
80824003
JB
14131 }
14132
4b0e333e
CW
14133 intel_crtc->cursor_base = ~0;
14134 intel_crtc->cursor_cntl = ~0;
dc41c154 14135 intel_crtc->cursor_size = ~0;
8d7849db 14136
22fd0fab
JB
14137 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14138 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14139 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14140 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14141
79e53945 14142 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
14143
14144 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14145 return;
14146
14147fail:
14148 if (primary)
14149 drm_plane_cleanup(primary);
14150 if (cursor)
14151 drm_plane_cleanup(cursor);
f5de6e07 14152 kfree(crtc_state);
3d7d6510 14153 kfree(intel_crtc);
79e53945
JB
14154}
14155
752aa88a
JB
14156enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14157{
14158 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14159 struct drm_device *dev = connector->base.dev;
752aa88a 14160
51fd371b 14161 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14162
d3babd3f 14163 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14164 return INVALID_PIPE;
14165
14166 return to_intel_crtc(encoder->crtc)->pipe;
14167}
14168
08d7b3d1 14169int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14170 struct drm_file *file)
08d7b3d1 14171{
08d7b3d1 14172 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14173 struct drm_crtc *drmmode_crtc;
c05422d5 14174 struct intel_crtc *crtc;
08d7b3d1 14175
7707e653 14176 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14177
7707e653 14178 if (!drmmode_crtc) {
08d7b3d1 14179 DRM_ERROR("no such CRTC id\n");
3f2c2057 14180 return -ENOENT;
08d7b3d1
CW
14181 }
14182
7707e653 14183 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14184 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14185
c05422d5 14186 return 0;
08d7b3d1
CW
14187}
14188
66a9278e 14189static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14190{
66a9278e
DV
14191 struct drm_device *dev = encoder->base.dev;
14192 struct intel_encoder *source_encoder;
79e53945 14193 int index_mask = 0;
79e53945
JB
14194 int entry = 0;
14195
b2784e15 14196 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14197 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14198 index_mask |= (1 << entry);
14199
79e53945
JB
14200 entry++;
14201 }
4ef69c7a 14202
79e53945
JB
14203 return index_mask;
14204}
14205
4d302442
CW
14206static bool has_edp_a(struct drm_device *dev)
14207{
14208 struct drm_i915_private *dev_priv = dev->dev_private;
14209
14210 if (!IS_MOBILE(dev))
14211 return false;
14212
14213 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14214 return false;
14215
e3589908 14216 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14217 return false;
14218
14219 return true;
14220}
14221
84b4e042
JB
14222static bool intel_crt_present(struct drm_device *dev)
14223{
14224 struct drm_i915_private *dev_priv = dev->dev_private;
14225
884497ed
DL
14226 if (INTEL_INFO(dev)->gen >= 9)
14227 return false;
14228
cf404ce4 14229 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14230 return false;
14231
14232 if (IS_CHERRYVIEW(dev))
14233 return false;
14234
14235 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14236 return false;
14237
14238 return true;
14239}
14240
79e53945
JB
14241static void intel_setup_outputs(struct drm_device *dev)
14242{
725e30ad 14243 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14244 struct intel_encoder *encoder;
cb0953d7 14245 bool dpd_is_edp = false;
79e53945 14246
c9093354 14247 intel_lvds_init(dev);
79e53945 14248
84b4e042 14249 if (intel_crt_present(dev))
79935fca 14250 intel_crt_init(dev);
cb0953d7 14251
c776eb2e
VK
14252 if (IS_BROXTON(dev)) {
14253 /*
14254 * FIXME: Broxton doesn't support port detection via the
14255 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14256 * detect the ports.
14257 */
14258 intel_ddi_init(dev, PORT_A);
14259 intel_ddi_init(dev, PORT_B);
14260 intel_ddi_init(dev, PORT_C);
14261 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14262 int found;
14263
de31facd
JB
14264 /*
14265 * Haswell uses DDI functions to detect digital outputs.
14266 * On SKL pre-D0 the strap isn't connected, so we assume
14267 * it's there.
14268 */
0e72a5b5 14269 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
de31facd
JB
14270 /* WaIgnoreDDIAStrap: skl */
14271 if (found ||
14272 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
0e72a5b5
ED
14273 intel_ddi_init(dev, PORT_A);
14274
14275 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14276 * register */
14277 found = I915_READ(SFUSE_STRAP);
14278
14279 if (found & SFUSE_STRAP_DDIB_DETECTED)
14280 intel_ddi_init(dev, PORT_B);
14281 if (found & SFUSE_STRAP_DDIC_DETECTED)
14282 intel_ddi_init(dev, PORT_C);
14283 if (found & SFUSE_STRAP_DDID_DETECTED)
14284 intel_ddi_init(dev, PORT_D);
14285 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14286 int found;
5d8a7752 14287 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14288
14289 if (has_edp_a(dev))
14290 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14291
dc0fa718 14292 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14293 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 14294 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 14295 if (!found)
e2debe91 14296 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14297 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14298 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14299 }
14300
dc0fa718 14301 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14302 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14303
dc0fa718 14304 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14305 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14306
5eb08b69 14307 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14308 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14309
270b3042 14310 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14311 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 14312 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
14313 /*
14314 * The DP_DETECTED bit is the latched state of the DDC
14315 * SDA pin at boot. However since eDP doesn't require DDC
14316 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14317 * eDP ports may have been muxed to an alternate function.
14318 * Thus we can't rely on the DP_DETECTED bit alone to detect
14319 * eDP ports. Consult the VBT as well as DP_DETECTED to
14320 * detect eDP ports.
14321 */
d2182a66
VS
14322 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14323 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
14324 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14325 PORT_B);
e17ac6db
VS
14326 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14327 intel_dp_is_edp(dev, PORT_B))
14328 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 14329
d2182a66
VS
14330 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14331 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
14332 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14333 PORT_C);
e17ac6db
VS
14334 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14335 intel_dp_is_edp(dev, PORT_C))
14336 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 14337
9418c1f1 14338 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14339 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
14340 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14341 PORT_D);
e17ac6db
VS
14342 /* eDP not supported on port D, so don't check VBT */
14343 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14344 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
14345 }
14346
3cfca973 14347 intel_dsi_init(dev);
103a196f 14348 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 14349 bool found = false;
7d57382e 14350
e2debe91 14351 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14352 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 14353 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
14354 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14355 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14356 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14357 }
27185ae1 14358
e7281eab 14359 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14360 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14361 }
13520b05
KH
14362
14363 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14364
e2debe91 14365 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14366 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 14367 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 14368 }
27185ae1 14369
e2debe91 14370 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14371
b01f2c3a
JB
14372 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14373 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14374 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14375 }
e7281eab 14376 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14377 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14378 }
27185ae1 14379
b01f2c3a 14380 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 14381 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14382 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14383 } else if (IS_GEN2(dev))
79e53945
JB
14384 intel_dvo_init(dev);
14385
103a196f 14386 if (SUPPORTS_TV(dev))
79e53945
JB
14387 intel_tv_init(dev);
14388
0bc12bcb 14389 intel_psr_init(dev);
7c8f8a70 14390
b2784e15 14391 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14392 encoder->base.possible_crtcs = encoder->crtc_mask;
14393 encoder->base.possible_clones =
66a9278e 14394 intel_encoder_clones(encoder);
79e53945 14395 }
47356eb6 14396
dde86e2d 14397 intel_init_pch_refclk(dev);
270b3042
DV
14398
14399 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14400}
14401
14402static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14403{
60a5ca01 14404 struct drm_device *dev = fb->dev;
79e53945 14405 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14406
ef2d633e 14407 drm_framebuffer_cleanup(fb);
60a5ca01 14408 mutex_lock(&dev->struct_mutex);
ef2d633e 14409 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14410 drm_gem_object_unreference(&intel_fb->obj->base);
14411 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14412 kfree(intel_fb);
14413}
14414
14415static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14416 struct drm_file *file,
79e53945
JB
14417 unsigned int *handle)
14418{
14419 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14420 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14421
05394f39 14422 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14423}
14424
14425static const struct drm_framebuffer_funcs intel_fb_funcs = {
14426 .destroy = intel_user_framebuffer_destroy,
14427 .create_handle = intel_user_framebuffer_create_handle,
14428};
14429
b321803d
DL
14430static
14431u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14432 uint32_t pixel_format)
14433{
14434 u32 gen = INTEL_INFO(dev)->gen;
14435
14436 if (gen >= 9) {
14437 /* "The stride in bytes must not exceed the of the size of 8K
14438 * pixels and 32K bytes."
14439 */
14440 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14441 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14442 return 32*1024;
14443 } else if (gen >= 4) {
14444 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14445 return 16*1024;
14446 else
14447 return 32*1024;
14448 } else if (gen >= 3) {
14449 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14450 return 8*1024;
14451 else
14452 return 16*1024;
14453 } else {
14454 /* XXX DSPC is limited to 4k tiled */
14455 return 8*1024;
14456 }
14457}
14458
b5ea642a
DV
14459static int intel_framebuffer_init(struct drm_device *dev,
14460 struct intel_framebuffer *intel_fb,
14461 struct drm_mode_fb_cmd2 *mode_cmd,
14462 struct drm_i915_gem_object *obj)
79e53945 14463{
6761dd31 14464 unsigned int aligned_height;
79e53945 14465 int ret;
b321803d 14466 u32 pitch_limit, stride_alignment;
79e53945 14467
dd4916c5
DV
14468 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14469
2a80eada
DV
14470 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14471 /* Enforce that fb modifier and tiling mode match, but only for
14472 * X-tiled. This is needed for FBC. */
14473 if (!!(obj->tiling_mode == I915_TILING_X) !=
14474 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14475 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14476 return -EINVAL;
14477 }
14478 } else {
14479 if (obj->tiling_mode == I915_TILING_X)
14480 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14481 else if (obj->tiling_mode == I915_TILING_Y) {
14482 DRM_DEBUG("No Y tiling for legacy addfb\n");
14483 return -EINVAL;
14484 }
14485 }
14486
9a8f0a12
TU
14487 /* Passed in modifier sanity checking. */
14488 switch (mode_cmd->modifier[0]) {
14489 case I915_FORMAT_MOD_Y_TILED:
14490 case I915_FORMAT_MOD_Yf_TILED:
14491 if (INTEL_INFO(dev)->gen < 9) {
14492 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14493 mode_cmd->modifier[0]);
14494 return -EINVAL;
14495 }
14496 case DRM_FORMAT_MOD_NONE:
14497 case I915_FORMAT_MOD_X_TILED:
14498 break;
14499 default:
c0f40428
JB
14500 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14501 mode_cmd->modifier[0]);
57cd6508 14502 return -EINVAL;
c16ed4be 14503 }
57cd6508 14504
b321803d
DL
14505 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14506 mode_cmd->pixel_format);
14507 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14508 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14509 mode_cmd->pitches[0], stride_alignment);
57cd6508 14510 return -EINVAL;
c16ed4be 14511 }
57cd6508 14512
b321803d
DL
14513 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14514 mode_cmd->pixel_format);
a35cdaa0 14515 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14516 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14517 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14518 "tiled" : "linear",
a35cdaa0 14519 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14520 return -EINVAL;
c16ed4be 14521 }
5d7bd705 14522
2a80eada 14523 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14524 mode_cmd->pitches[0] != obj->stride) {
14525 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14526 mode_cmd->pitches[0], obj->stride);
5d7bd705 14527 return -EINVAL;
c16ed4be 14528 }
5d7bd705 14529
57779d06 14530 /* Reject formats not supported by any plane early. */
308e5bcb 14531 switch (mode_cmd->pixel_format) {
57779d06 14532 case DRM_FORMAT_C8:
04b3924d
VS
14533 case DRM_FORMAT_RGB565:
14534 case DRM_FORMAT_XRGB8888:
14535 case DRM_FORMAT_ARGB8888:
57779d06
VS
14536 break;
14537 case DRM_FORMAT_XRGB1555:
c16ed4be 14538 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14539 DRM_DEBUG("unsupported pixel format: %s\n",
14540 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14541 return -EINVAL;
c16ed4be 14542 }
57779d06 14543 break;
57779d06 14544 case DRM_FORMAT_ABGR8888:
6c0fd451
DL
14545 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14546 DRM_DEBUG("unsupported pixel format: %s\n",
14547 drm_get_format_name(mode_cmd->pixel_format));
14548 return -EINVAL;
14549 }
14550 break;
14551 case DRM_FORMAT_XBGR8888:
04b3924d 14552 case DRM_FORMAT_XRGB2101010:
57779d06 14553 case DRM_FORMAT_XBGR2101010:
c16ed4be 14554 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14555 DRM_DEBUG("unsupported pixel format: %s\n",
14556 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14557 return -EINVAL;
c16ed4be 14558 }
b5626747 14559 break;
7531208b
DL
14560 case DRM_FORMAT_ABGR2101010:
14561 if (!IS_VALLEYVIEW(dev)) {
14562 DRM_DEBUG("unsupported pixel format: %s\n",
14563 drm_get_format_name(mode_cmd->pixel_format));
14564 return -EINVAL;
14565 }
14566 break;
04b3924d
VS
14567 case DRM_FORMAT_YUYV:
14568 case DRM_FORMAT_UYVY:
14569 case DRM_FORMAT_YVYU:
14570 case DRM_FORMAT_VYUY:
c16ed4be 14571 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14572 DRM_DEBUG("unsupported pixel format: %s\n",
14573 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14574 return -EINVAL;
c16ed4be 14575 }
57cd6508
CW
14576 break;
14577 default:
4ee62c76
VS
14578 DRM_DEBUG("unsupported pixel format: %s\n",
14579 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14580 return -EINVAL;
14581 }
14582
90f9a336
VS
14583 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14584 if (mode_cmd->offsets[0] != 0)
14585 return -EINVAL;
14586
ec2c981e 14587 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14588 mode_cmd->pixel_format,
14589 mode_cmd->modifier[0]);
53155c0a
DV
14590 /* FIXME drm helper for size checks (especially planar formats)? */
14591 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14592 return -EINVAL;
14593
c7d73f6a
DV
14594 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14595 intel_fb->obj = obj;
80075d49 14596 intel_fb->obj->framebuffer_references++;
c7d73f6a 14597
79e53945
JB
14598 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14599 if (ret) {
14600 DRM_ERROR("framebuffer init failed %d\n", ret);
14601 return ret;
14602 }
14603
79e53945
JB
14604 return 0;
14605}
14606
79e53945
JB
14607static struct drm_framebuffer *
14608intel_user_framebuffer_create(struct drm_device *dev,
14609 struct drm_file *filp,
308e5bcb 14610 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14611{
05394f39 14612 struct drm_i915_gem_object *obj;
79e53945 14613
308e5bcb
JB
14614 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14615 mode_cmd->handles[0]));
c8725226 14616 if (&obj->base == NULL)
cce13ff7 14617 return ERR_PTR(-ENOENT);
79e53945 14618
d2dff872 14619 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
14620}
14621
4520f53a 14622#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 14623static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14624{
14625}
14626#endif
14627
79e53945 14628static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14629 .fb_create = intel_user_framebuffer_create,
0632fef6 14630 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14631 .atomic_check = intel_atomic_check,
14632 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14633 .atomic_state_alloc = intel_atomic_state_alloc,
14634 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14635};
14636
e70236a8
JB
14637/* Set up chip specific display functions */
14638static void intel_init_display(struct drm_device *dev)
14639{
14640 struct drm_i915_private *dev_priv = dev->dev_private;
14641
ee9300bb
DV
14642 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14643 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14644 else if (IS_CHERRYVIEW(dev))
14645 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14646 else if (IS_VALLEYVIEW(dev))
14647 dev_priv->display.find_dpll = vlv_find_best_dpll;
14648 else if (IS_PINEVIEW(dev))
14649 dev_priv->display.find_dpll = pnv_find_best_dpll;
14650 else
14651 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14652
bc8d7dff
DL
14653 if (INTEL_INFO(dev)->gen >= 9) {
14654 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14655 dev_priv->display.get_initial_plane_config =
14656 skylake_get_initial_plane_config;
bc8d7dff
DL
14657 dev_priv->display.crtc_compute_clock =
14658 haswell_crtc_compute_clock;
14659 dev_priv->display.crtc_enable = haswell_crtc_enable;
14660 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14661 dev_priv->display.update_primary_plane =
14662 skylake_update_primary_plane;
14663 } else if (HAS_DDI(dev)) {
0e8ffe1b 14664 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14665 dev_priv->display.get_initial_plane_config =
14666 ironlake_get_initial_plane_config;
797d0259
ACO
14667 dev_priv->display.crtc_compute_clock =
14668 haswell_crtc_compute_clock;
4f771f10
PZ
14669 dev_priv->display.crtc_enable = haswell_crtc_enable;
14670 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14671 dev_priv->display.update_primary_plane =
14672 ironlake_update_primary_plane;
09b4ddf9 14673 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14674 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14675 dev_priv->display.get_initial_plane_config =
14676 ironlake_get_initial_plane_config;
3fb37703
ACO
14677 dev_priv->display.crtc_compute_clock =
14678 ironlake_crtc_compute_clock;
76e5a89c
DV
14679 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14680 dev_priv->display.crtc_disable = ironlake_crtc_disable;
262ca2b0
MR
14681 dev_priv->display.update_primary_plane =
14682 ironlake_update_primary_plane;
89b667f8
JB
14683 } else if (IS_VALLEYVIEW(dev)) {
14684 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14685 dev_priv->display.get_initial_plane_config =
14686 i9xx_get_initial_plane_config;
d6dfee7a 14687 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14688 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14689 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14690 dev_priv->display.update_primary_plane =
14691 i9xx_update_primary_plane;
f564048e 14692 } else {
0e8ffe1b 14693 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14694 dev_priv->display.get_initial_plane_config =
14695 i9xx_get_initial_plane_config;
d6dfee7a 14696 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14697 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14698 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14699 dev_priv->display.update_primary_plane =
14700 i9xx_update_primary_plane;
f564048e 14701 }
e70236a8 14702
e70236a8 14703 /* Returns the core display clock speed */
1652d19e
VS
14704 if (IS_SKYLAKE(dev))
14705 dev_priv->display.get_display_clock_speed =
14706 skylake_get_display_clock_speed;
14707 else if (IS_BROADWELL(dev))
14708 dev_priv->display.get_display_clock_speed =
14709 broadwell_get_display_clock_speed;
14710 else if (IS_HASWELL(dev))
14711 dev_priv->display.get_display_clock_speed =
14712 haswell_get_display_clock_speed;
14713 else if (IS_VALLEYVIEW(dev))
25eb05fc
JB
14714 dev_priv->display.get_display_clock_speed =
14715 valleyview_get_display_clock_speed;
b37a6434
VS
14716 else if (IS_GEN5(dev))
14717 dev_priv->display.get_display_clock_speed =
14718 ilk_get_display_clock_speed;
a7c66cd8 14719 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
34edce2f 14720 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
e70236a8
JB
14721 dev_priv->display.get_display_clock_speed =
14722 i945_get_display_clock_speed;
34edce2f
VS
14723 else if (IS_GM45(dev))
14724 dev_priv->display.get_display_clock_speed =
14725 gm45_get_display_clock_speed;
14726 else if (IS_CRESTLINE(dev))
14727 dev_priv->display.get_display_clock_speed =
14728 i965gm_get_display_clock_speed;
14729 else if (IS_PINEVIEW(dev))
14730 dev_priv->display.get_display_clock_speed =
14731 pnv_get_display_clock_speed;
14732 else if (IS_G33(dev) || IS_G4X(dev))
14733 dev_priv->display.get_display_clock_speed =
14734 g33_get_display_clock_speed;
e70236a8
JB
14735 else if (IS_I915G(dev))
14736 dev_priv->display.get_display_clock_speed =
14737 i915_get_display_clock_speed;
257a7ffc 14738 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14739 dev_priv->display.get_display_clock_speed =
14740 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
14741 else if (IS_PINEVIEW(dev))
14742 dev_priv->display.get_display_clock_speed =
14743 pnv_get_display_clock_speed;
e70236a8
JB
14744 else if (IS_I915GM(dev))
14745 dev_priv->display.get_display_clock_speed =
14746 i915gm_get_display_clock_speed;
14747 else if (IS_I865G(dev))
14748 dev_priv->display.get_display_clock_speed =
14749 i865_get_display_clock_speed;
f0f8a9ce 14750 else if (IS_I85X(dev))
e70236a8 14751 dev_priv->display.get_display_clock_speed =
1b1d2716 14752 i85x_get_display_clock_speed;
623e01e5
VS
14753 else { /* 830 */
14754 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14755 dev_priv->display.get_display_clock_speed =
14756 i830_get_display_clock_speed;
623e01e5 14757 }
e70236a8 14758
7c10a2b5 14759 if (IS_GEN5(dev)) {
3bb11b53 14760 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14761 } else if (IS_GEN6(dev)) {
14762 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14763 } else if (IS_IVYBRIDGE(dev)) {
14764 /* FIXME: detect B0+ stepping and use auto training */
14765 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14766 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14767 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
27c329ed
ML
14768 if (IS_BROADWELL(dev)) {
14769 dev_priv->display.modeset_commit_cdclk =
14770 broadwell_modeset_commit_cdclk;
14771 dev_priv->display.modeset_calc_cdclk =
14772 broadwell_modeset_calc_cdclk;
14773 }
30a970c6 14774 } else if (IS_VALLEYVIEW(dev)) {
27c329ed
ML
14775 dev_priv->display.modeset_commit_cdclk =
14776 valleyview_modeset_commit_cdclk;
14777 dev_priv->display.modeset_calc_cdclk =
14778 valleyview_modeset_calc_cdclk;
f8437dd1 14779 } else if (IS_BROXTON(dev)) {
27c329ed
ML
14780 dev_priv->display.modeset_commit_cdclk =
14781 broxton_modeset_commit_cdclk;
14782 dev_priv->display.modeset_calc_cdclk =
14783 broxton_modeset_calc_cdclk;
e70236a8 14784 }
8c9f3aaf 14785
8c9f3aaf
JB
14786 switch (INTEL_INFO(dev)->gen) {
14787 case 2:
14788 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14789 break;
14790
14791 case 3:
14792 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14793 break;
14794
14795 case 4:
14796 case 5:
14797 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14798 break;
14799
14800 case 6:
14801 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14802 break;
7c9017e5 14803 case 7:
4e0bbc31 14804 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14805 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14806 break;
830c81db 14807 case 9:
ba343e02
TU
14808 /* Drop through - unsupported since execlist only. */
14809 default:
14810 /* Default just returns -ENODEV to indicate unsupported */
14811 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14812 }
7bd688cd
JN
14813
14814 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
14815
14816 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
14817}
14818
b690e96c
JB
14819/*
14820 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14821 * resume, or other times. This quirk makes sure that's the case for
14822 * affected systems.
14823 */
0206e353 14824static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14825{
14826 struct drm_i915_private *dev_priv = dev->dev_private;
14827
14828 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14829 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14830}
14831
b6b5d049
VS
14832static void quirk_pipeb_force(struct drm_device *dev)
14833{
14834 struct drm_i915_private *dev_priv = dev->dev_private;
14835
14836 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14837 DRM_INFO("applying pipe b force quirk\n");
14838}
14839
435793df
KP
14840/*
14841 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14842 */
14843static void quirk_ssc_force_disable(struct drm_device *dev)
14844{
14845 struct drm_i915_private *dev_priv = dev->dev_private;
14846 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14847 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14848}
14849
4dca20ef 14850/*
5a15ab5b
CE
14851 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14852 * brightness value
4dca20ef
CE
14853 */
14854static void quirk_invert_brightness(struct drm_device *dev)
14855{
14856 struct drm_i915_private *dev_priv = dev->dev_private;
14857 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14858 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14859}
14860
9c72cc6f
SD
14861/* Some VBT's incorrectly indicate no backlight is present */
14862static void quirk_backlight_present(struct drm_device *dev)
14863{
14864 struct drm_i915_private *dev_priv = dev->dev_private;
14865 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14866 DRM_INFO("applying backlight present quirk\n");
14867}
14868
b690e96c
JB
14869struct intel_quirk {
14870 int device;
14871 int subsystem_vendor;
14872 int subsystem_device;
14873 void (*hook)(struct drm_device *dev);
14874};
14875
5f85f176
EE
14876/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14877struct intel_dmi_quirk {
14878 void (*hook)(struct drm_device *dev);
14879 const struct dmi_system_id (*dmi_id_list)[];
14880};
14881
14882static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14883{
14884 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14885 return 1;
14886}
14887
14888static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14889 {
14890 .dmi_id_list = &(const struct dmi_system_id[]) {
14891 {
14892 .callback = intel_dmi_reverse_brightness,
14893 .ident = "NCR Corporation",
14894 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14895 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14896 },
14897 },
14898 { } /* terminating entry */
14899 },
14900 .hook = quirk_invert_brightness,
14901 },
14902};
14903
c43b5634 14904static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14905 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14906 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14907
b690e96c
JB
14908 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14909 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14910
5f080c0f
VS
14911 /* 830 needs to leave pipe A & dpll A up */
14912 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14913
b6b5d049
VS
14914 /* 830 needs to leave pipe B & dpll B up */
14915 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14916
435793df
KP
14917 /* Lenovo U160 cannot use SSC on LVDS */
14918 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14919
14920 /* Sony Vaio Y cannot use SSC on LVDS */
14921 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14922
be505f64
AH
14923 /* Acer Aspire 5734Z must invert backlight brightness */
14924 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14925
14926 /* Acer/eMachines G725 */
14927 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14928
14929 /* Acer/eMachines e725 */
14930 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14931
14932 /* Acer/Packard Bell NCL20 */
14933 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14934
14935 /* Acer Aspire 4736Z */
14936 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14937
14938 /* Acer Aspire 5336 */
14939 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14940
14941 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14942 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14943
dfb3d47b
SD
14944 /* Acer C720 Chromebook (Core i3 4005U) */
14945 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14946
b2a9601c 14947 /* Apple Macbook 2,1 (Core 2 T7400) */
14948 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14949
d4967d8c
SD
14950 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14951 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14952
14953 /* HP Chromebook 14 (Celeron 2955U) */
14954 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14955
14956 /* Dell Chromebook 11 */
14957 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14958};
14959
14960static void intel_init_quirks(struct drm_device *dev)
14961{
14962 struct pci_dev *d = dev->pdev;
14963 int i;
14964
14965 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14966 struct intel_quirk *q = &intel_quirks[i];
14967
14968 if (d->device == q->device &&
14969 (d->subsystem_vendor == q->subsystem_vendor ||
14970 q->subsystem_vendor == PCI_ANY_ID) &&
14971 (d->subsystem_device == q->subsystem_device ||
14972 q->subsystem_device == PCI_ANY_ID))
14973 q->hook(dev);
14974 }
5f85f176
EE
14975 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14976 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14977 intel_dmi_quirks[i].hook(dev);
14978 }
b690e96c
JB
14979}
14980
9cce37f4
JB
14981/* Disable the VGA plane that we never use */
14982static void i915_disable_vga(struct drm_device *dev)
14983{
14984 struct drm_i915_private *dev_priv = dev->dev_private;
14985 u8 sr1;
766aa1c4 14986 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 14987
2b37c616 14988 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 14989 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14990 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14991 sr1 = inb(VGA_SR_DATA);
14992 outb(sr1 | 1<<5, VGA_SR_DATA);
14993 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14994 udelay(300);
14995
01f5a626 14996 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14997 POSTING_READ(vga_reg);
14998}
14999
f817586c
DV
15000void intel_modeset_init_hw(struct drm_device *dev)
15001{
b6283055 15002 intel_update_cdclk(dev);
a8f78b58 15003 intel_prepare_ddi(dev);
f817586c 15004 intel_init_clock_gating(dev);
8090c6b9 15005 intel_enable_gt_powersave(dev);
f817586c
DV
15006}
15007
79e53945
JB
15008void intel_modeset_init(struct drm_device *dev)
15009{
652c393a 15010 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 15011 int sprite, ret;
8cc87b75 15012 enum pipe pipe;
46f297fb 15013 struct intel_crtc *crtc;
79e53945
JB
15014
15015 drm_mode_config_init(dev);
15016
15017 dev->mode_config.min_width = 0;
15018 dev->mode_config.min_height = 0;
15019
019d96cb
DA
15020 dev->mode_config.preferred_depth = 24;
15021 dev->mode_config.prefer_shadow = 1;
15022
25bab385
TU
15023 dev->mode_config.allow_fb_modifiers = true;
15024
e6ecefaa 15025 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15026
b690e96c
JB
15027 intel_init_quirks(dev);
15028
1fa61106
ED
15029 intel_init_pm(dev);
15030
e3c74757
BW
15031 if (INTEL_INFO(dev)->num_pipes == 0)
15032 return;
15033
e70236a8 15034 intel_init_display(dev);
7c10a2b5 15035 intel_init_audio(dev);
e70236a8 15036
a6c45cf0
CW
15037 if (IS_GEN2(dev)) {
15038 dev->mode_config.max_width = 2048;
15039 dev->mode_config.max_height = 2048;
15040 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15041 dev->mode_config.max_width = 4096;
15042 dev->mode_config.max_height = 4096;
79e53945 15043 } else {
a6c45cf0
CW
15044 dev->mode_config.max_width = 8192;
15045 dev->mode_config.max_height = 8192;
79e53945 15046 }
068be561 15047
dc41c154
VS
15048 if (IS_845G(dev) || IS_I865G(dev)) {
15049 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15050 dev->mode_config.cursor_height = 1023;
15051 } else if (IS_GEN2(dev)) {
068be561
DL
15052 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15053 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15054 } else {
15055 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15056 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15057 }
15058
5d4545ae 15059 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 15060
28c97730 15061 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15062 INTEL_INFO(dev)->num_pipes,
15063 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15064
055e393f 15065 for_each_pipe(dev_priv, pipe) {
8cc87b75 15066 intel_crtc_init(dev, pipe);
3bdcfc0c 15067 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15068 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15069 if (ret)
06da8da2 15070 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15071 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15072 }
79e53945
JB
15073 }
15074
f42bb70d
JB
15075 intel_init_dpio(dev);
15076
e72f9fbf 15077 intel_shared_dpll_init(dev);
ee7b9f93 15078
9cce37f4
JB
15079 /* Just disable it once at startup */
15080 i915_disable_vga(dev);
79e53945 15081 intel_setup_outputs(dev);
11be49eb
CW
15082
15083 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 15084 intel_fbc_disable(dev);
fa9fa083 15085
6e9f798d 15086 drm_modeset_lock_all(dev);
fa9fa083 15087 intel_modeset_setup_hw_state(dev, false);
6e9f798d 15088 drm_modeset_unlock_all(dev);
46f297fb 15089
d3fcc808 15090 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
15091 if (!crtc->active)
15092 continue;
15093
46f297fb 15094 /*
46f297fb
JB
15095 * Note that reserving the BIOS fb up front prevents us
15096 * from stuffing other stolen allocations like the ring
15097 * on top. This prevents some ugliness at boot time, and
15098 * can even allow for smooth boot transitions if the BIOS
15099 * fb is large enough for the active pipe configuration.
15100 */
5724dbd1
DL
15101 if (dev_priv->display.get_initial_plane_config) {
15102 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
15103 &crtc->plane_config);
15104 /*
15105 * If the fb is shared between multiple heads, we'll
15106 * just get the first one.
15107 */
f6936e29 15108 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
46f297fb 15109 }
46f297fb 15110 }
2c7111db
CW
15111}
15112
7fad798e
DV
15113static void intel_enable_pipe_a(struct drm_device *dev)
15114{
15115 struct intel_connector *connector;
15116 struct drm_connector *crt = NULL;
15117 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15118 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15119
15120 /* We can't just switch on the pipe A, we need to set things up with a
15121 * proper mode and output configuration. As a gross hack, enable pipe A
15122 * by enabling the load detect pipe once. */
3a3371ff 15123 for_each_intel_connector(dev, connector) {
7fad798e
DV
15124 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15125 crt = &connector->base;
15126 break;
15127 }
15128 }
15129
15130 if (!crt)
15131 return;
15132
208bf9fd 15133 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15134 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15135}
15136
fa555837
DV
15137static bool
15138intel_check_plane_mapping(struct intel_crtc *crtc)
15139{
7eb552ae
BW
15140 struct drm_device *dev = crtc->base.dev;
15141 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
15142 u32 reg, val;
15143
7eb552ae 15144 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15145 return true;
15146
15147 reg = DSPCNTR(!crtc->plane);
15148 val = I915_READ(reg);
15149
15150 if ((val & DISPLAY_PLANE_ENABLE) &&
15151 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15152 return false;
15153
15154 return true;
15155}
15156
24929352
DV
15157static void intel_sanitize_crtc(struct intel_crtc *crtc)
15158{
15159 struct drm_device *dev = crtc->base.dev;
15160 struct drm_i915_private *dev_priv = dev->dev_private;
b17d48e2 15161 struct intel_encoder *encoder;
fa555837 15162 u32 reg;
b17d48e2 15163 bool enable;
24929352 15164
24929352 15165 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 15166 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
15167 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15168
d3eaf884 15169 /* restore vblank interrupts to correct state */
9625604c 15170 drm_crtc_vblank_reset(&crtc->base);
d297e103
VS
15171 if (crtc->active) {
15172 update_scanline_offset(crtc);
9625604c
DV
15173 drm_crtc_vblank_on(&crtc->base);
15174 }
d3eaf884 15175
24929352 15176 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15177 * disable the crtc (and hence change the state) if it is wrong. Note
15178 * that gen4+ has a fixed plane -> pipe mapping. */
15179 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15180 bool plane;
15181
24929352
DV
15182 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15183 crtc->base.base.id);
15184
15185 /* Pipe has the wrong plane attached and the plane is active.
15186 * Temporarily change the plane mapping and disable everything
15187 * ... */
15188 plane = crtc->plane;
b70709a6 15189 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15190 crtc->plane = !plane;
b17d48e2 15191 intel_crtc_disable_noatomic(&crtc->base);
24929352 15192 crtc->plane = plane;
24929352 15193 }
24929352 15194
7fad798e
DV
15195 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15196 crtc->pipe == PIPE_A && !crtc->active) {
15197 /* BIOS forgot to enable pipe A, this mostly happens after
15198 * resume. Force-enable the pipe to fix this, the update_dpms
15199 * call below we restore the pipe to the right state, but leave
15200 * the required bits on. */
15201 intel_enable_pipe_a(dev);
15202 }
15203
24929352
DV
15204 /* Adjust the state of the output pipe according to whether we
15205 * have active connectors/encoders. */
b17d48e2
ML
15206 enable = false;
15207 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15208 enable |= encoder->connectors_active;
24929352 15209
b17d48e2
ML
15210 if (!enable)
15211 intel_crtc_disable_noatomic(&crtc->base);
24929352 15212
53d9f4e9 15213 if (crtc->active != crtc->base.state->active) {
24929352
DV
15214
15215 /* This can happen either due to bugs in the get_hw_state
b17d48e2
ML
15216 * functions or because of calls to intel_crtc_disable_noatomic,
15217 * or because the pipe is force-enabled due to the
24929352
DV
15218 * pipe A quirk. */
15219 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15220 crtc->base.base.id,
83d65738 15221 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
15222 crtc->active ? "enabled" : "disabled");
15223
83d65738 15224 crtc->base.state->enable = crtc->active;
49d6fa21 15225 crtc->base.state->active = crtc->active;
24929352
DV
15226 crtc->base.enabled = crtc->active;
15227
15228 /* Because we only establish the connector -> encoder ->
15229 * crtc links if something is active, this means the
15230 * crtc is now deactivated. Break the links. connector
15231 * -> encoder links are only establish when things are
15232 * actually up, hence no need to break them. */
15233 WARN_ON(crtc->active);
15234
15235 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15236 WARN_ON(encoder->connectors_active);
15237 encoder->base.crtc = NULL;
15238 }
15239 }
c5ab3bc0 15240
a3ed6aad 15241 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15242 /*
15243 * We start out with underrun reporting disabled to avoid races.
15244 * For correct bookkeeping mark this on active crtcs.
15245 *
c5ab3bc0
DV
15246 * Also on gmch platforms we dont have any hardware bits to
15247 * disable the underrun reporting. Which means we need to start
15248 * out with underrun reporting disabled also on inactive pipes,
15249 * since otherwise we'll complain about the garbage we read when
15250 * e.g. coming up after runtime pm.
15251 *
4cc31489
DV
15252 * No protection against concurrent access is required - at
15253 * worst a fifo underrun happens which also sets this to false.
15254 */
15255 crtc->cpu_fifo_underrun_disabled = true;
15256 crtc->pch_fifo_underrun_disabled = true;
15257 }
24929352
DV
15258}
15259
15260static void intel_sanitize_encoder(struct intel_encoder *encoder)
15261{
15262 struct intel_connector *connector;
15263 struct drm_device *dev = encoder->base.dev;
15264
15265 /* We need to check both for a crtc link (meaning that the
15266 * encoder is active and trying to read from a pipe) and the
15267 * pipe itself being active. */
15268 bool has_active_crtc = encoder->base.crtc &&
15269 to_intel_crtc(encoder->base.crtc)->active;
15270
15271 if (encoder->connectors_active && !has_active_crtc) {
15272 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15273 encoder->base.base.id,
8e329a03 15274 encoder->base.name);
24929352
DV
15275
15276 /* Connector is active, but has no active pipe. This is
15277 * fallout from our resume register restoring. Disable
15278 * the encoder manually again. */
15279 if (encoder->base.crtc) {
15280 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15281 encoder->base.base.id,
8e329a03 15282 encoder->base.name);
24929352 15283 encoder->disable(encoder);
a62d1497
VS
15284 if (encoder->post_disable)
15285 encoder->post_disable(encoder);
24929352 15286 }
7f1950fb
EE
15287 encoder->base.crtc = NULL;
15288 encoder->connectors_active = false;
24929352
DV
15289
15290 /* Inconsistent output/port/pipe state happens presumably due to
15291 * a bug in one of the get_hw_state functions. Or someplace else
15292 * in our code, like the register restore mess on resume. Clamp
15293 * things to off as a safer default. */
3a3371ff 15294 for_each_intel_connector(dev, connector) {
24929352
DV
15295 if (connector->encoder != encoder)
15296 continue;
7f1950fb
EE
15297 connector->base.dpms = DRM_MODE_DPMS_OFF;
15298 connector->base.encoder = NULL;
24929352
DV
15299 }
15300 }
15301 /* Enabled encoders without active connectors will be fixed in
15302 * the crtc fixup. */
15303}
15304
04098753 15305void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15306{
15307 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 15308 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15309
04098753
ID
15310 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15311 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15312 i915_disable_vga(dev);
15313 }
15314}
15315
15316void i915_redisable_vga(struct drm_device *dev)
15317{
15318 struct drm_i915_private *dev_priv = dev->dev_private;
15319
8dc8a27c
PZ
15320 /* This function can be called both from intel_modeset_setup_hw_state or
15321 * at a very early point in our resume sequence, where the power well
15322 * structures are not yet restored. Since this function is at a very
15323 * paranoid "someone might have enabled VGA while we were not looking"
15324 * level, just check if the power well is enabled instead of trying to
15325 * follow the "don't touch the power well if we don't need it" policy
15326 * the rest of the driver uses. */
f458ebbc 15327 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15328 return;
15329
04098753 15330 i915_redisable_vga_power_on(dev);
0fde901f
KM
15331}
15332
98ec7739
VS
15333static bool primary_get_hw_state(struct intel_crtc *crtc)
15334{
15335 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15336
d032ffa0
ML
15337 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15338}
15339
15340static void readout_plane_state(struct intel_crtc *crtc,
15341 struct intel_crtc_state *crtc_state)
15342{
15343 struct intel_plane *p;
15344 struct drm_plane_state *drm_plane_state;
15345 bool active = crtc_state->base.active;
15346
15347 if (active) {
15348 crtc_state->quirks |= PIPE_CONFIG_QUIRK_INITIAL_PLANES;
15349
15350 /* apply to previous sw state too */
15351 to_intel_crtc_state(crtc->base.state)->quirks |=
15352 PIPE_CONFIG_QUIRK_INITIAL_PLANES;
15353 }
98ec7739 15354
d032ffa0
ML
15355 for_each_intel_plane(crtc->base.dev, p) {
15356 bool visible = active;
15357
15358 if (crtc->pipe != p->pipe)
15359 continue;
15360
15361 drm_plane_state = p->base.state;
15362 if (active && p->base.type == DRM_PLANE_TYPE_PRIMARY) {
15363 visible = primary_get_hw_state(crtc);
15364 to_intel_plane_state(drm_plane_state)->visible = visible;
15365 } else {
15366 /*
15367 * unknown state, assume it's off to force a transition
15368 * to on when calculating state changes.
15369 */
15370 to_intel_plane_state(drm_plane_state)->visible = false;
15371 }
15372
15373 if (visible) {
15374 crtc_state->base.plane_mask |=
15375 1 << drm_plane_index(&p->base);
15376 } else if (crtc_state->base.state) {
15377 /* Make this unconditional for atomic hw readout. */
15378 crtc_state->base.plane_mask &=
15379 ~(1 << drm_plane_index(&p->base));
15380 }
15381 }
98ec7739
VS
15382}
15383
30e984df 15384static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15385{
15386 struct drm_i915_private *dev_priv = dev->dev_private;
15387 enum pipe pipe;
24929352
DV
15388 struct intel_crtc *crtc;
15389 struct intel_encoder *encoder;
15390 struct intel_connector *connector;
5358901f 15391 int i;
24929352 15392
d3fcc808 15393 for_each_intel_crtc(dev, crtc) {
6e3c9717 15394 memset(crtc->config, 0, sizeof(*crtc->config));
f7217905 15395 crtc->config->base.crtc = &crtc->base;
3b117c8f 15396
6e3c9717 15397 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
9953599b 15398
0e8ffe1b 15399 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 15400 crtc->config);
24929352 15401
83d65738 15402 crtc->base.state->enable = crtc->active;
49d6fa21 15403 crtc->base.state->active = crtc->active;
24929352 15404 crtc->base.enabled = crtc->active;
b8b7fade 15405 crtc->base.hwmode = crtc->config->base.adjusted_mode;
b70709a6 15406
d032ffa0 15407 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
24929352
DV
15408
15409 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15410 crtc->base.base.id,
15411 crtc->active ? "enabled" : "disabled");
15412 }
15413
5358901f
DV
15414 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15415 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15416
3e369b76
ACO
15417 pll->on = pll->get_hw_state(dev_priv, pll,
15418 &pll->config.hw_state);
5358901f 15419 pll->active = 0;
3e369b76 15420 pll->config.crtc_mask = 0;
d3fcc808 15421 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 15422 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 15423 pll->active++;
3e369b76 15424 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 15425 }
5358901f 15426 }
5358901f 15427
1e6f2ddc 15428 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15429 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 15430
3e369b76 15431 if (pll->config.crtc_mask)
bd2bb1b9 15432 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
15433 }
15434
b2784e15 15435 for_each_intel_encoder(dev, encoder) {
24929352
DV
15436 pipe = 0;
15437
15438 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15439 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15440 encoder->base.crtc = &crtc->base;
6e3c9717 15441 encoder->get_config(encoder, crtc->config);
24929352
DV
15442 } else {
15443 encoder->base.crtc = NULL;
15444 }
15445
15446 encoder->connectors_active = false;
6f2bcceb 15447 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15448 encoder->base.base.id,
8e329a03 15449 encoder->base.name,
24929352 15450 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15451 pipe_name(pipe));
24929352
DV
15452 }
15453
3a3371ff 15454 for_each_intel_connector(dev, connector) {
24929352
DV
15455 if (connector->get_hw_state(connector)) {
15456 connector->base.dpms = DRM_MODE_DPMS_ON;
15457 connector->encoder->connectors_active = true;
15458 connector->base.encoder = &connector->encoder->base;
15459 } else {
15460 connector->base.dpms = DRM_MODE_DPMS_OFF;
15461 connector->base.encoder = NULL;
15462 }
15463 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15464 connector->base.base.id,
c23cc417 15465 connector->base.name,
24929352
DV
15466 connector->base.encoder ? "enabled" : "disabled");
15467 }
30e984df
DV
15468}
15469
15470/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15471 * and i915 state tracking structures. */
15472void intel_modeset_setup_hw_state(struct drm_device *dev,
15473 bool force_restore)
15474{
15475 struct drm_i915_private *dev_priv = dev->dev_private;
15476 enum pipe pipe;
30e984df
DV
15477 struct intel_crtc *crtc;
15478 struct intel_encoder *encoder;
35c95375 15479 int i;
30e984df
DV
15480
15481 intel_modeset_readout_hw_state(dev);
24929352 15482
babea61d
JB
15483 /*
15484 * Now that we have the config, copy it to each CRTC struct
15485 * Note that this could go away if we move to using crtc_config
15486 * checking everywhere.
15487 */
d3fcc808 15488 for_each_intel_crtc(dev, crtc) {
d330a953 15489 if (crtc->active && i915.fastboot) {
6e3c9717
ACO
15490 intel_mode_from_pipe_config(&crtc->base.mode,
15491 crtc->config);
babea61d
JB
15492 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15493 crtc->base.base.id);
15494 drm_mode_debug_printmodeline(&crtc->base.mode);
15495 }
15496 }
15497
24929352 15498 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15499 for_each_intel_encoder(dev, encoder) {
24929352
DV
15500 intel_sanitize_encoder(encoder);
15501 }
15502
055e393f 15503 for_each_pipe(dev_priv, pipe) {
24929352
DV
15504 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15505 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15506 intel_dump_pipe_config(crtc, crtc->config,
15507 "[setup_hw_state]");
24929352 15508 }
9a935856 15509
d29b2f9d
ACO
15510 intel_modeset_update_connector_atomic_state(dev);
15511
35c95375
DV
15512 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15513 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15514
15515 if (!pll->on || pll->active)
15516 continue;
15517
15518 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15519
15520 pll->disable(dev_priv, pll);
15521 pll->on = false;
15522 }
15523
3078999f
PB
15524 if (IS_GEN9(dev))
15525 skl_wm_get_hw_state(dev);
15526 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
15527 ilk_wm_get_hw_state(dev);
15528
45e2b5f6 15529 if (force_restore) {
7d0bc1ea
VS
15530 i915_redisable_vga(dev);
15531
f30da187
DV
15532 /*
15533 * We need to use raw interfaces for restoring state to avoid
15534 * checking (bogus) intermediate states.
15535 */
055e393f 15536 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
15537 struct drm_crtc *crtc =
15538 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187 15539
83a57153 15540 intel_crtc_restore_mode(crtc);
45e2b5f6
DV
15541 }
15542 } else {
15543 intel_modeset_update_staged_output_state(dev);
15544 }
8af6cf88
DV
15545
15546 intel_modeset_check_state(dev);
2c7111db
CW
15547}
15548
15549void intel_modeset_gem_init(struct drm_device *dev)
15550{
92122789 15551 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 15552 struct drm_crtc *c;
2ff8fde1 15553 struct drm_i915_gem_object *obj;
e0d6149b 15554 int ret;
484b41dd 15555
ae48434c
ID
15556 mutex_lock(&dev->struct_mutex);
15557 intel_init_gt_powersave(dev);
15558 mutex_unlock(&dev->struct_mutex);
15559
92122789
JB
15560 /*
15561 * There may be no VBT; and if the BIOS enabled SSC we can
15562 * just keep using it to avoid unnecessary flicker. Whereas if the
15563 * BIOS isn't using it, don't assume it will work even if the VBT
15564 * indicates as much.
15565 */
15566 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15567 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15568 DREF_SSC1_ENABLE);
15569
1833b134 15570 intel_modeset_init_hw(dev);
02e792fb
DV
15571
15572 intel_setup_overlay(dev);
484b41dd
JB
15573
15574 /*
15575 * Make sure any fbs we allocated at startup are properly
15576 * pinned & fenced. When we do the allocation it's too early
15577 * for this.
15578 */
70e1e0ec 15579 for_each_crtc(dev, c) {
2ff8fde1
MR
15580 obj = intel_fb_obj(c->primary->fb);
15581 if (obj == NULL)
484b41dd
JB
15582 continue;
15583
e0d6149b
TU
15584 mutex_lock(&dev->struct_mutex);
15585 ret = intel_pin_and_fence_fb_obj(c->primary,
15586 c->primary->fb,
15587 c->primary->state,
15588 NULL);
15589 mutex_unlock(&dev->struct_mutex);
15590 if (ret) {
484b41dd
JB
15591 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15592 to_intel_crtc(c)->pipe);
66e514c1
DA
15593 drm_framebuffer_unreference(c->primary->fb);
15594 c->primary->fb = NULL;
36750f28 15595 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 15596 update_state_fb(c->primary);
36750f28 15597 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15598 }
15599 }
0962c3c9
VS
15600
15601 intel_backlight_register(dev);
79e53945
JB
15602}
15603
4932e2c3
ID
15604void intel_connector_unregister(struct intel_connector *intel_connector)
15605{
15606 struct drm_connector *connector = &intel_connector->base;
15607
15608 intel_panel_destroy_backlight(connector);
34ea3d38 15609 drm_connector_unregister(connector);
4932e2c3
ID
15610}
15611
79e53945
JB
15612void intel_modeset_cleanup(struct drm_device *dev)
15613{
652c393a 15614 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 15615 struct drm_connector *connector;
652c393a 15616
2eb5252e
ID
15617 intel_disable_gt_powersave(dev);
15618
0962c3c9
VS
15619 intel_backlight_unregister(dev);
15620
fd0c0642
DV
15621 /*
15622 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15623 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15624 * experience fancy races otherwise.
15625 */
2aeb7d3a 15626 intel_irq_uninstall(dev_priv);
eb21b92b 15627
fd0c0642
DV
15628 /*
15629 * Due to the hpd irq storm handling the hotplug work can re-arm the
15630 * poll handlers. Hence disable polling after hpd handling is shut down.
15631 */
f87ea761 15632 drm_kms_helper_poll_fini(dev);
fd0c0642 15633
652c393a
JB
15634 mutex_lock(&dev->struct_mutex);
15635
723bfd70
JB
15636 intel_unregister_dsm_handler();
15637
7ff0ebcc 15638 intel_fbc_disable(dev);
e70236a8 15639
69341a5e
KH
15640 mutex_unlock(&dev->struct_mutex);
15641
1630fe75
CW
15642 /* flush any delayed tasks or pending work */
15643 flush_scheduled_work();
15644
db31af1d
JN
15645 /* destroy the backlight and sysfs files before encoders/connectors */
15646 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
15647 struct intel_connector *intel_connector;
15648
15649 intel_connector = to_intel_connector(connector);
15650 intel_connector->unregister(intel_connector);
db31af1d 15651 }
d9255d57 15652
79e53945 15653 drm_mode_config_cleanup(dev);
4d7bb011
DV
15654
15655 intel_cleanup_overlay(dev);
ae48434c
ID
15656
15657 mutex_lock(&dev->struct_mutex);
15658 intel_cleanup_gt_powersave(dev);
15659 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15660}
15661
f1c79df3
ZW
15662/*
15663 * Return which encoder is currently attached for connector.
15664 */
df0e9248 15665struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15666{
df0e9248
CW
15667 return &intel_attached_encoder(connector)->base;
15668}
f1c79df3 15669
df0e9248
CW
15670void intel_connector_attach_encoder(struct intel_connector *connector,
15671 struct intel_encoder *encoder)
15672{
15673 connector->encoder = encoder;
15674 drm_mode_connector_attach_encoder(&connector->base,
15675 &encoder->base);
79e53945 15676}
28d52043
DA
15677
15678/*
15679 * set vga decode state - true == enable VGA decode
15680 */
15681int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15682{
15683 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15684 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15685 u16 gmch_ctrl;
15686
75fa041d
CW
15687 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15688 DRM_ERROR("failed to read control word\n");
15689 return -EIO;
15690 }
15691
c0cc8a55
CW
15692 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15693 return 0;
15694
28d52043
DA
15695 if (state)
15696 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15697 else
15698 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15699
15700 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15701 DRM_ERROR("failed to write control word\n");
15702 return -EIO;
15703 }
15704
28d52043
DA
15705 return 0;
15706}
c4a1d9e4 15707
c4a1d9e4 15708struct intel_display_error_state {
ff57f1b0
PZ
15709
15710 u32 power_well_driver;
15711
63b66e5b
CW
15712 int num_transcoders;
15713
c4a1d9e4
CW
15714 struct intel_cursor_error_state {
15715 u32 control;
15716 u32 position;
15717 u32 base;
15718 u32 size;
52331309 15719 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15720
15721 struct intel_pipe_error_state {
ddf9c536 15722 bool power_domain_on;
c4a1d9e4 15723 u32 source;
f301b1e1 15724 u32 stat;
52331309 15725 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15726
15727 struct intel_plane_error_state {
15728 u32 control;
15729 u32 stride;
15730 u32 size;
15731 u32 pos;
15732 u32 addr;
15733 u32 surface;
15734 u32 tile_offset;
52331309 15735 } plane[I915_MAX_PIPES];
63b66e5b
CW
15736
15737 struct intel_transcoder_error_state {
ddf9c536 15738 bool power_domain_on;
63b66e5b
CW
15739 enum transcoder cpu_transcoder;
15740
15741 u32 conf;
15742
15743 u32 htotal;
15744 u32 hblank;
15745 u32 hsync;
15746 u32 vtotal;
15747 u32 vblank;
15748 u32 vsync;
15749 } transcoder[4];
c4a1d9e4
CW
15750};
15751
15752struct intel_display_error_state *
15753intel_display_capture_error_state(struct drm_device *dev)
15754{
fbee40df 15755 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 15756 struct intel_display_error_state *error;
63b66e5b
CW
15757 int transcoders[] = {
15758 TRANSCODER_A,
15759 TRANSCODER_B,
15760 TRANSCODER_C,
15761 TRANSCODER_EDP,
15762 };
c4a1d9e4
CW
15763 int i;
15764
63b66e5b
CW
15765 if (INTEL_INFO(dev)->num_pipes == 0)
15766 return NULL;
15767
9d1cb914 15768 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15769 if (error == NULL)
15770 return NULL;
15771
190be112 15772 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
15773 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15774
055e393f 15775 for_each_pipe(dev_priv, i) {
ddf9c536 15776 error->pipe[i].power_domain_on =
f458ebbc
DV
15777 __intel_display_power_is_enabled(dev_priv,
15778 POWER_DOMAIN_PIPE(i));
ddf9c536 15779 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15780 continue;
15781
5efb3e28
VS
15782 error->cursor[i].control = I915_READ(CURCNTR(i));
15783 error->cursor[i].position = I915_READ(CURPOS(i));
15784 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15785
15786 error->plane[i].control = I915_READ(DSPCNTR(i));
15787 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 15788 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 15789 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15790 error->plane[i].pos = I915_READ(DSPPOS(i));
15791 }
ca291363
PZ
15792 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15793 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
15794 if (INTEL_INFO(dev)->gen >= 4) {
15795 error->plane[i].surface = I915_READ(DSPSURF(i));
15796 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15797 }
15798
c4a1d9e4 15799 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15800
3abfce77 15801 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 15802 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15803 }
15804
15805 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15806 if (HAS_DDI(dev_priv->dev))
15807 error->num_transcoders++; /* Account for eDP. */
15808
15809 for (i = 0; i < error->num_transcoders; i++) {
15810 enum transcoder cpu_transcoder = transcoders[i];
15811
ddf9c536 15812 error->transcoder[i].power_domain_on =
f458ebbc 15813 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15814 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15815 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15816 continue;
15817
63b66e5b
CW
15818 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15819
15820 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15821 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15822 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15823 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15824 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15825 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15826 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15827 }
15828
15829 return error;
15830}
15831
edc3d884
MK
15832#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15833
c4a1d9e4 15834void
edc3d884 15835intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15836 struct drm_device *dev,
15837 struct intel_display_error_state *error)
15838{
055e393f 15839 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15840 int i;
15841
63b66e5b
CW
15842 if (!error)
15843 return;
15844
edc3d884 15845 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15846 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15847 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15848 error->power_well_driver);
055e393f 15849 for_each_pipe(dev_priv, i) {
edc3d884 15850 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
15851 err_printf(m, " Power: %s\n",
15852 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 15853 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15854 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15855
15856 err_printf(m, "Plane [%d]:\n", i);
15857 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15858 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15859 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15860 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15861 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15862 }
4b71a570 15863 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15864 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15865 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15866 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15867 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15868 }
15869
edc3d884
MK
15870 err_printf(m, "Cursor [%d]:\n", i);
15871 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15872 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15873 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15874 }
63b66e5b
CW
15875
15876 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 15877 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 15878 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
15879 err_printf(m, " Power: %s\n",
15880 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
15881 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15882 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15883 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15884 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15885 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15886 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15887 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15888 }
c4a1d9e4 15889}
e2fcdaa9
VS
15890
15891void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15892{
15893 struct intel_crtc *crtc;
15894
15895 for_each_intel_crtc(dev, crtc) {
15896 struct intel_unpin_work *work;
e2fcdaa9 15897
5e2d7afc 15898 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
15899
15900 work = crtc->unpin_work;
15901
15902 if (work && work->event &&
15903 work->event->base.file_priv == file) {
15904 kfree(work->event);
15905 work->event = NULL;
15906 }
15907
5e2d7afc 15908 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
15909 }
15910}
This page took 3.307782 seconds and 5 git commands to generate.