]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Actually respect DSPSURF alignment restrictions
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
e5510fac 39#include "i915_trace.h"
319c1d42 40#include <drm/drm_atomic.h>
c196e1d6 41#include <drm/drm_atomic_helper.h>
760285e7
DH
42#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
465c120c
MR
44#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
c0f372b3 46#include <linux/dma_remapping.h>
79e53945 47
465c120c 48/* Primary plane formats for gen <= 3 */
568db4f2 49static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
50 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
465c120c 52 DRM_FORMAT_XRGB1555,
67fe7dc5 53 DRM_FORMAT_XRGB8888,
465c120c
MR
54};
55
56/* Primary plane formats for gen >= 4 */
568db4f2 57static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
58 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
60 DRM_FORMAT_XRGB8888,
61 DRM_FORMAT_XBGR8888,
62 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
64};
65
66static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
465c120c 70 DRM_FORMAT_XBGR8888,
67fe7dc5 71 DRM_FORMAT_ARGB8888,
465c120c
MR
72 DRM_FORMAT_ABGR8888,
73 DRM_FORMAT_XRGB2101010,
465c120c 74 DRM_FORMAT_XBGR2101010,
465c120c
MR
75};
76
3d7d6510
MR
77/* Cursor formats */
78static const uint32_t intel_cursor_formats[] = {
79 DRM_FORMAT_ARGB8888,
80};
81
6b383a7f 82static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 83
f1f644dc 84static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 85 struct intel_crtc_state *pipe_config);
18442d08 86static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 87 struct intel_crtc_state *pipe_config);
f1f644dc 88
568c634a 89static int intel_set_mode(struct drm_atomic_state *state);
eb1bfe80
JB
90static int intel_framebuffer_init(struct drm_device *dev,
91 struct intel_framebuffer *ifb,
92 struct drm_mode_fb_cmd2 *mode_cmd,
93 struct drm_i915_gem_object *obj);
5b18e57c
DV
94static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
95static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 96static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
97 struct intel_link_m_n *m_n,
98 struct intel_link_m_n *m2_n2);
29407aab 99static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
100static void haswell_set_pipeconf(struct drm_crtc *crtc);
101static void intel_set_pipe_csc(struct drm_crtc *crtc);
d288f65f 102static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 103 const struct intel_crtc_state *pipe_config);
d288f65f 104static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 105 const struct intel_crtc_state *pipe_config);
ea2c67bb
MR
106static void intel_begin_crtc_commit(struct drm_crtc *crtc);
107static void intel_finish_crtc_commit(struct drm_crtc *crtc);
549e2bfb
CK
108static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
109 struct intel_crtc_state *crtc_state);
5ab7b0b7
ID
110static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
111 int num_connectors);
ce22dba9
ML
112static void intel_crtc_enable_planes(struct drm_crtc *crtc);
113static void intel_crtc_disable_planes(struct drm_crtc *crtc);
e7457a9a 114
0e32b39c
DA
115static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
116{
117 if (!connector->mst_port)
118 return connector->encoder;
119 else
120 return &connector->mst_port->mst_encoders[pipe]->base;
121}
122
79e53945 123typedef struct {
0206e353 124 int min, max;
79e53945
JB
125} intel_range_t;
126
127typedef struct {
0206e353
AJ
128 int dot_limit;
129 int p2_slow, p2_fast;
79e53945
JB
130} intel_p2_t;
131
d4906093
ML
132typedef struct intel_limit intel_limit_t;
133struct intel_limit {
0206e353
AJ
134 intel_range_t dot, vco, n, m, m1, m2, p, p1;
135 intel_p2_t p2;
d4906093 136};
79e53945 137
d2acd215
DV
138int
139intel_pch_rawclk(struct drm_device *dev)
140{
141 struct drm_i915_private *dev_priv = dev->dev_private;
142
143 WARN_ON(!HAS_PCH_SPLIT(dev));
144
145 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
146}
147
021357ac
CW
148static inline u32 /* units of 100MHz */
149intel_fdi_link_freq(struct drm_device *dev)
150{
8b99e68c
CW
151 if (IS_GEN5(dev)) {
152 struct drm_i915_private *dev_priv = dev->dev_private;
153 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
154 } else
155 return 27;
021357ac
CW
156}
157
5d536e28 158static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 159 .dot = { .min = 25000, .max = 350000 },
9c333719 160 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 161 .n = { .min = 2, .max = 16 },
0206e353
AJ
162 .m = { .min = 96, .max = 140 },
163 .m1 = { .min = 18, .max = 26 },
164 .m2 = { .min = 6, .max = 16 },
165 .p = { .min = 4, .max = 128 },
166 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
167 .p2 = { .dot_limit = 165000,
168 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
169};
170
5d536e28
DV
171static const intel_limit_t intel_limits_i8xx_dvo = {
172 .dot = { .min = 25000, .max = 350000 },
9c333719 173 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 174 .n = { .min = 2, .max = 16 },
5d536e28
DV
175 .m = { .min = 96, .max = 140 },
176 .m1 = { .min = 18, .max = 26 },
177 .m2 = { .min = 6, .max = 16 },
178 .p = { .min = 4, .max = 128 },
179 .p1 = { .min = 2, .max = 33 },
180 .p2 = { .dot_limit = 165000,
181 .p2_slow = 4, .p2_fast = 4 },
182};
183
e4b36699 184static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 185 .dot = { .min = 25000, .max = 350000 },
9c333719 186 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 187 .n = { .min = 2, .max = 16 },
0206e353
AJ
188 .m = { .min = 96, .max = 140 },
189 .m1 = { .min = 18, .max = 26 },
190 .m2 = { .min = 6, .max = 16 },
191 .p = { .min = 4, .max = 128 },
192 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
193 .p2 = { .dot_limit = 165000,
194 .p2_slow = 14, .p2_fast = 7 },
e4b36699 195};
273e27ca 196
e4b36699 197static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
198 .dot = { .min = 20000, .max = 400000 },
199 .vco = { .min = 1400000, .max = 2800000 },
200 .n = { .min = 1, .max = 6 },
201 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
202 .m1 = { .min = 8, .max = 18 },
203 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
204 .p = { .min = 5, .max = 80 },
205 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
206 .p2 = { .dot_limit = 200000,
207 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
208};
209
210static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
211 .dot = { .min = 20000, .max = 400000 },
212 .vco = { .min = 1400000, .max = 2800000 },
213 .n = { .min = 1, .max = 6 },
214 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
215 .m1 = { .min = 8, .max = 18 },
216 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
217 .p = { .min = 7, .max = 98 },
218 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
219 .p2 = { .dot_limit = 112000,
220 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
221};
222
273e27ca 223
e4b36699 224static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
225 .dot = { .min = 25000, .max = 270000 },
226 .vco = { .min = 1750000, .max = 3500000},
227 .n = { .min = 1, .max = 4 },
228 .m = { .min = 104, .max = 138 },
229 .m1 = { .min = 17, .max = 23 },
230 .m2 = { .min = 5, .max = 11 },
231 .p = { .min = 10, .max = 30 },
232 .p1 = { .min = 1, .max = 3},
233 .p2 = { .dot_limit = 270000,
234 .p2_slow = 10,
235 .p2_fast = 10
044c7c41 236 },
e4b36699
KP
237};
238
239static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
240 .dot = { .min = 22000, .max = 400000 },
241 .vco = { .min = 1750000, .max = 3500000},
242 .n = { .min = 1, .max = 4 },
243 .m = { .min = 104, .max = 138 },
244 .m1 = { .min = 16, .max = 23 },
245 .m2 = { .min = 5, .max = 11 },
246 .p = { .min = 5, .max = 80 },
247 .p1 = { .min = 1, .max = 8},
248 .p2 = { .dot_limit = 165000,
249 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
250};
251
252static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
253 .dot = { .min = 20000, .max = 115000 },
254 .vco = { .min = 1750000, .max = 3500000 },
255 .n = { .min = 1, .max = 3 },
256 .m = { .min = 104, .max = 138 },
257 .m1 = { .min = 17, .max = 23 },
258 .m2 = { .min = 5, .max = 11 },
259 .p = { .min = 28, .max = 112 },
260 .p1 = { .min = 2, .max = 8 },
261 .p2 = { .dot_limit = 0,
262 .p2_slow = 14, .p2_fast = 14
044c7c41 263 },
e4b36699
KP
264};
265
266static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
267 .dot = { .min = 80000, .max = 224000 },
268 .vco = { .min = 1750000, .max = 3500000 },
269 .n = { .min = 1, .max = 3 },
270 .m = { .min = 104, .max = 138 },
271 .m1 = { .min = 17, .max = 23 },
272 .m2 = { .min = 5, .max = 11 },
273 .p = { .min = 14, .max = 42 },
274 .p1 = { .min = 2, .max = 6 },
275 .p2 = { .dot_limit = 0,
276 .p2_slow = 7, .p2_fast = 7
044c7c41 277 },
e4b36699
KP
278};
279
f2b115e6 280static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
281 .dot = { .min = 20000, .max = 400000},
282 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 283 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
284 .n = { .min = 3, .max = 6 },
285 .m = { .min = 2, .max = 256 },
273e27ca 286 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
287 .m1 = { .min = 0, .max = 0 },
288 .m2 = { .min = 0, .max = 254 },
289 .p = { .min = 5, .max = 80 },
290 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
291 .p2 = { .dot_limit = 200000,
292 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
293};
294
f2b115e6 295static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
296 .dot = { .min = 20000, .max = 400000 },
297 .vco = { .min = 1700000, .max = 3500000 },
298 .n = { .min = 3, .max = 6 },
299 .m = { .min = 2, .max = 256 },
300 .m1 = { .min = 0, .max = 0 },
301 .m2 = { .min = 0, .max = 254 },
302 .p = { .min = 7, .max = 112 },
303 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
304 .p2 = { .dot_limit = 112000,
305 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
306};
307
273e27ca
EA
308/* Ironlake / Sandybridge
309 *
310 * We calculate clock using (register_value + 2) for N/M1/M2, so here
311 * the range value for them is (actual_value - 2).
312 */
b91ad0ec 313static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
314 .dot = { .min = 25000, .max = 350000 },
315 .vco = { .min = 1760000, .max = 3510000 },
316 .n = { .min = 1, .max = 5 },
317 .m = { .min = 79, .max = 127 },
318 .m1 = { .min = 12, .max = 22 },
319 .m2 = { .min = 5, .max = 9 },
320 .p = { .min = 5, .max = 80 },
321 .p1 = { .min = 1, .max = 8 },
322 .p2 = { .dot_limit = 225000,
323 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
324};
325
b91ad0ec 326static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
327 .dot = { .min = 25000, .max = 350000 },
328 .vco = { .min = 1760000, .max = 3510000 },
329 .n = { .min = 1, .max = 3 },
330 .m = { .min = 79, .max = 118 },
331 .m1 = { .min = 12, .max = 22 },
332 .m2 = { .min = 5, .max = 9 },
333 .p = { .min = 28, .max = 112 },
334 .p1 = { .min = 2, .max = 8 },
335 .p2 = { .dot_limit = 225000,
336 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
337};
338
339static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
340 .dot = { .min = 25000, .max = 350000 },
341 .vco = { .min = 1760000, .max = 3510000 },
342 .n = { .min = 1, .max = 3 },
343 .m = { .min = 79, .max = 127 },
344 .m1 = { .min = 12, .max = 22 },
345 .m2 = { .min = 5, .max = 9 },
346 .p = { .min = 14, .max = 56 },
347 .p1 = { .min = 2, .max = 8 },
348 .p2 = { .dot_limit = 225000,
349 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
350};
351
273e27ca 352/* LVDS 100mhz refclk limits. */
b91ad0ec 353static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
354 .dot = { .min = 25000, .max = 350000 },
355 .vco = { .min = 1760000, .max = 3510000 },
356 .n = { .min = 1, .max = 2 },
357 .m = { .min = 79, .max = 126 },
358 .m1 = { .min = 12, .max = 22 },
359 .m2 = { .min = 5, .max = 9 },
360 .p = { .min = 28, .max = 112 },
0206e353 361 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
362 .p2 = { .dot_limit = 225000,
363 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
364};
365
366static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
367 .dot = { .min = 25000, .max = 350000 },
368 .vco = { .min = 1760000, .max = 3510000 },
369 .n = { .min = 1, .max = 3 },
370 .m = { .min = 79, .max = 126 },
371 .m1 = { .min = 12, .max = 22 },
372 .m2 = { .min = 5, .max = 9 },
373 .p = { .min = 14, .max = 42 },
0206e353 374 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
375 .p2 = { .dot_limit = 225000,
376 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
377};
378
dc730512 379static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
380 /*
381 * These are the data rate limits (measured in fast clocks)
382 * since those are the strictest limits we have. The fast
383 * clock and actual rate limits are more relaxed, so checking
384 * them would make no difference.
385 */
386 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 387 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 388 .n = { .min = 1, .max = 7 },
a0c4da24
JB
389 .m1 = { .min = 2, .max = 3 },
390 .m2 = { .min = 11, .max = 156 },
b99ab663 391 .p1 = { .min = 2, .max = 3 },
5fdc9c49 392 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
393};
394
ef9348c8
CML
395static const intel_limit_t intel_limits_chv = {
396 /*
397 * These are the data rate limits (measured in fast clocks)
398 * since those are the strictest limits we have. The fast
399 * clock and actual rate limits are more relaxed, so checking
400 * them would make no difference.
401 */
402 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 403 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
404 .n = { .min = 1, .max = 1 },
405 .m1 = { .min = 2, .max = 2 },
406 .m2 = { .min = 24 << 22, .max = 175 << 22 },
407 .p1 = { .min = 2, .max = 4 },
408 .p2 = { .p2_slow = 1, .p2_fast = 14 },
409};
410
5ab7b0b7
ID
411static const intel_limit_t intel_limits_bxt = {
412 /* FIXME: find real dot limits */
413 .dot = { .min = 0, .max = INT_MAX },
414 .vco = { .min = 4800000, .max = 6480000 },
415 .n = { .min = 1, .max = 1 },
416 .m1 = { .min = 2, .max = 2 },
417 /* FIXME: find real m2 limits */
418 .m2 = { .min = 2 << 22, .max = 255 << 22 },
419 .p1 = { .min = 2, .max = 4 },
420 .p2 = { .p2_slow = 1, .p2_fast = 20 },
421};
422
6b4bf1c4
VS
423static void vlv_clock(int refclk, intel_clock_t *clock)
424{
425 clock->m = clock->m1 * clock->m2;
426 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
427 if (WARN_ON(clock->n == 0 || clock->p == 0))
428 return;
fb03ac01
VS
429 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
430 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
431}
432
cdba954e
ACO
433static bool
434needs_modeset(struct drm_crtc_state *state)
435{
436 return state->mode_changed || state->active_changed;
437}
438
e0638cdf
PZ
439/**
440 * Returns whether any output on the specified pipe is of the specified type
441 */
4093561b 442bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 443{
409ee761 444 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
445 struct intel_encoder *encoder;
446
409ee761 447 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
448 if (encoder->type == type)
449 return true;
450
451 return false;
452}
453
d0737e1d
ACO
454/**
455 * Returns whether any output on the specified pipe will have the specified
456 * type after a staged modeset is complete, i.e., the same as
457 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
458 * encoder->crtc.
459 */
a93e255f
ACO
460static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
461 int type)
d0737e1d 462{
a93e255f 463 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 464 struct drm_connector *connector;
a93e255f 465 struct drm_connector_state *connector_state;
d0737e1d 466 struct intel_encoder *encoder;
a93e255f
ACO
467 int i, num_connectors = 0;
468
da3ced29 469 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
470 if (connector_state->crtc != crtc_state->base.crtc)
471 continue;
472
473 num_connectors++;
d0737e1d 474
a93e255f
ACO
475 encoder = to_intel_encoder(connector_state->best_encoder);
476 if (encoder->type == type)
d0737e1d 477 return true;
a93e255f
ACO
478 }
479
480 WARN_ON(num_connectors == 0);
d0737e1d
ACO
481
482 return false;
483}
484
a93e255f
ACO
485static const intel_limit_t *
486intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
2c07245f 487{
a93e255f 488 struct drm_device *dev = crtc_state->base.crtc->dev;
2c07245f 489 const intel_limit_t *limit;
b91ad0ec 490
a93e255f 491 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 492 if (intel_is_dual_link_lvds(dev)) {
1b894b59 493 if (refclk == 100000)
b91ad0ec
ZW
494 limit = &intel_limits_ironlake_dual_lvds_100m;
495 else
496 limit = &intel_limits_ironlake_dual_lvds;
497 } else {
1b894b59 498 if (refclk == 100000)
b91ad0ec
ZW
499 limit = &intel_limits_ironlake_single_lvds_100m;
500 else
501 limit = &intel_limits_ironlake_single_lvds;
502 }
c6bb3538 503 } else
b91ad0ec 504 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
505
506 return limit;
507}
508
a93e255f
ACO
509static const intel_limit_t *
510intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 511{
a93e255f 512 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
513 const intel_limit_t *limit;
514
a93e255f 515 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 516 if (intel_is_dual_link_lvds(dev))
e4b36699 517 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 518 else
e4b36699 519 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
520 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
521 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 522 limit = &intel_limits_g4x_hdmi;
a93e255f 523 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 524 limit = &intel_limits_g4x_sdvo;
044c7c41 525 } else /* The option is for other outputs */
e4b36699 526 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
527
528 return limit;
529}
530
a93e255f
ACO
531static const intel_limit_t *
532intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 533{
a93e255f 534 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
535 const intel_limit_t *limit;
536
5ab7b0b7
ID
537 if (IS_BROXTON(dev))
538 limit = &intel_limits_bxt;
539 else if (HAS_PCH_SPLIT(dev))
a93e255f 540 limit = intel_ironlake_limit(crtc_state, refclk);
2c07245f 541 else if (IS_G4X(dev)) {
a93e255f 542 limit = intel_g4x_limit(crtc_state);
f2b115e6 543 } else if (IS_PINEVIEW(dev)) {
a93e255f 544 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 545 limit = &intel_limits_pineview_lvds;
2177832f 546 else
f2b115e6 547 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
548 } else if (IS_CHERRYVIEW(dev)) {
549 limit = &intel_limits_chv;
a0c4da24 550 } else if (IS_VALLEYVIEW(dev)) {
dc730512 551 limit = &intel_limits_vlv;
a6c45cf0 552 } else if (!IS_GEN2(dev)) {
a93e255f 553 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
554 limit = &intel_limits_i9xx_lvds;
555 else
556 limit = &intel_limits_i9xx_sdvo;
79e53945 557 } else {
a93e255f 558 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 559 limit = &intel_limits_i8xx_lvds;
a93e255f 560 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 561 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
562 else
563 limit = &intel_limits_i8xx_dac;
79e53945
JB
564 }
565 return limit;
566}
567
f2b115e6
AJ
568/* m1 is reserved as 0 in Pineview, n is a ring counter */
569static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 570{
2177832f
SL
571 clock->m = clock->m2 + 2;
572 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
573 if (WARN_ON(clock->n == 0 || clock->p == 0))
574 return;
fb03ac01
VS
575 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
576 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
577}
578
7429e9d4
DV
579static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
580{
581 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
582}
583
ac58c3f0 584static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 585{
7429e9d4 586 clock->m = i9xx_dpll_compute_m(clock);
79e53945 587 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
588 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
589 return;
fb03ac01
VS
590 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
591 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
592}
593
ef9348c8
CML
594static void chv_clock(int refclk, intel_clock_t *clock)
595{
596 clock->m = clock->m1 * clock->m2;
597 clock->p = clock->p1 * clock->p2;
598 if (WARN_ON(clock->n == 0 || clock->p == 0))
599 return;
600 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
601 clock->n << 22);
602 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
603}
604
7c04d1d9 605#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
606/**
607 * Returns whether the given set of divisors are valid for a given refclk with
608 * the given connectors.
609 */
610
1b894b59
CW
611static bool intel_PLL_is_valid(struct drm_device *dev,
612 const intel_limit_t *limit,
613 const intel_clock_t *clock)
79e53945 614{
f01b7962
VS
615 if (clock->n < limit->n.min || limit->n.max < clock->n)
616 INTELPllInvalid("n out of range\n");
79e53945 617 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 618 INTELPllInvalid("p1 out of range\n");
79e53945 619 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 620 INTELPllInvalid("m2 out of range\n");
79e53945 621 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 622 INTELPllInvalid("m1 out of range\n");
f01b7962 623
5ab7b0b7 624 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
625 if (clock->m1 <= clock->m2)
626 INTELPllInvalid("m1 <= m2\n");
627
5ab7b0b7 628 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
629 if (clock->p < limit->p.min || limit->p.max < clock->p)
630 INTELPllInvalid("p out of range\n");
631 if (clock->m < limit->m.min || limit->m.max < clock->m)
632 INTELPllInvalid("m out of range\n");
633 }
634
79e53945 635 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 636 INTELPllInvalid("vco out of range\n");
79e53945
JB
637 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
638 * connector, etc., rather than just a single range.
639 */
640 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 641 INTELPllInvalid("dot out of range\n");
79e53945
JB
642
643 return true;
644}
645
d4906093 646static bool
a93e255f
ACO
647i9xx_find_best_dpll(const intel_limit_t *limit,
648 struct intel_crtc_state *crtc_state,
cec2f356
SP
649 int target, int refclk, intel_clock_t *match_clock,
650 intel_clock_t *best_clock)
79e53945 651{
a93e255f 652 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 653 struct drm_device *dev = crtc->base.dev;
79e53945 654 intel_clock_t clock;
79e53945
JB
655 int err = target;
656
a93e255f 657 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 658 /*
a210b028
DV
659 * For LVDS just rely on its current settings for dual-channel.
660 * We haven't figured out how to reliably set up different
661 * single/dual channel state, if we even can.
79e53945 662 */
1974cad0 663 if (intel_is_dual_link_lvds(dev))
79e53945
JB
664 clock.p2 = limit->p2.p2_fast;
665 else
666 clock.p2 = limit->p2.p2_slow;
667 } else {
668 if (target < limit->p2.dot_limit)
669 clock.p2 = limit->p2.p2_slow;
670 else
671 clock.p2 = limit->p2.p2_fast;
672 }
673
0206e353 674 memset(best_clock, 0, sizeof(*best_clock));
79e53945 675
42158660
ZY
676 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
677 clock.m1++) {
678 for (clock.m2 = limit->m2.min;
679 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 680 if (clock.m2 >= clock.m1)
42158660
ZY
681 break;
682 for (clock.n = limit->n.min;
683 clock.n <= limit->n.max; clock.n++) {
684 for (clock.p1 = limit->p1.min;
685 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
686 int this_err;
687
ac58c3f0
DV
688 i9xx_clock(refclk, &clock);
689 if (!intel_PLL_is_valid(dev, limit,
690 &clock))
691 continue;
692 if (match_clock &&
693 clock.p != match_clock->p)
694 continue;
695
696 this_err = abs(clock.dot - target);
697 if (this_err < err) {
698 *best_clock = clock;
699 err = this_err;
700 }
701 }
702 }
703 }
704 }
705
706 return (err != target);
707}
708
709static bool
a93e255f
ACO
710pnv_find_best_dpll(const intel_limit_t *limit,
711 struct intel_crtc_state *crtc_state,
ee9300bb
DV
712 int target, int refclk, intel_clock_t *match_clock,
713 intel_clock_t *best_clock)
79e53945 714{
a93e255f 715 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 716 struct drm_device *dev = crtc->base.dev;
79e53945 717 intel_clock_t clock;
79e53945
JB
718 int err = target;
719
a93e255f 720 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 721 /*
a210b028
DV
722 * For LVDS just rely on its current settings for dual-channel.
723 * We haven't figured out how to reliably set up different
724 * single/dual channel state, if we even can.
79e53945 725 */
1974cad0 726 if (intel_is_dual_link_lvds(dev))
79e53945
JB
727 clock.p2 = limit->p2.p2_fast;
728 else
729 clock.p2 = limit->p2.p2_slow;
730 } else {
731 if (target < limit->p2.dot_limit)
732 clock.p2 = limit->p2.p2_slow;
733 else
734 clock.p2 = limit->p2.p2_fast;
735 }
736
0206e353 737 memset(best_clock, 0, sizeof(*best_clock));
79e53945 738
42158660
ZY
739 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
740 clock.m1++) {
741 for (clock.m2 = limit->m2.min;
742 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
743 for (clock.n = limit->n.min;
744 clock.n <= limit->n.max; clock.n++) {
745 for (clock.p1 = limit->p1.min;
746 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
747 int this_err;
748
ac58c3f0 749 pineview_clock(refclk, &clock);
1b894b59
CW
750 if (!intel_PLL_is_valid(dev, limit,
751 &clock))
79e53945 752 continue;
cec2f356
SP
753 if (match_clock &&
754 clock.p != match_clock->p)
755 continue;
79e53945
JB
756
757 this_err = abs(clock.dot - target);
758 if (this_err < err) {
759 *best_clock = clock;
760 err = this_err;
761 }
762 }
763 }
764 }
765 }
766
767 return (err != target);
768}
769
d4906093 770static bool
a93e255f
ACO
771g4x_find_best_dpll(const intel_limit_t *limit,
772 struct intel_crtc_state *crtc_state,
ee9300bb
DV
773 int target, int refclk, intel_clock_t *match_clock,
774 intel_clock_t *best_clock)
d4906093 775{
a93e255f 776 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 777 struct drm_device *dev = crtc->base.dev;
d4906093
ML
778 intel_clock_t clock;
779 int max_n;
780 bool found;
6ba770dc
AJ
781 /* approximately equals target * 0.00585 */
782 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
783 found = false;
784
a93e255f 785 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 786 if (intel_is_dual_link_lvds(dev))
d4906093
ML
787 clock.p2 = limit->p2.p2_fast;
788 else
789 clock.p2 = limit->p2.p2_slow;
790 } else {
791 if (target < limit->p2.dot_limit)
792 clock.p2 = limit->p2.p2_slow;
793 else
794 clock.p2 = limit->p2.p2_fast;
795 }
796
797 memset(best_clock, 0, sizeof(*best_clock));
798 max_n = limit->n.max;
f77f13e2 799 /* based on hardware requirement, prefer smaller n to precision */
d4906093 800 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 801 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
802 for (clock.m1 = limit->m1.max;
803 clock.m1 >= limit->m1.min; clock.m1--) {
804 for (clock.m2 = limit->m2.max;
805 clock.m2 >= limit->m2.min; clock.m2--) {
806 for (clock.p1 = limit->p1.max;
807 clock.p1 >= limit->p1.min; clock.p1--) {
808 int this_err;
809
ac58c3f0 810 i9xx_clock(refclk, &clock);
1b894b59
CW
811 if (!intel_PLL_is_valid(dev, limit,
812 &clock))
d4906093 813 continue;
1b894b59
CW
814
815 this_err = abs(clock.dot - target);
d4906093
ML
816 if (this_err < err_most) {
817 *best_clock = clock;
818 err_most = this_err;
819 max_n = clock.n;
820 found = true;
821 }
822 }
823 }
824 }
825 }
2c07245f
ZW
826 return found;
827}
828
d5dd62bd
ID
829/*
830 * Check if the calculated PLL configuration is more optimal compared to the
831 * best configuration and error found so far. Return the calculated error.
832 */
833static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
834 const intel_clock_t *calculated_clock,
835 const intel_clock_t *best_clock,
836 unsigned int best_error_ppm,
837 unsigned int *error_ppm)
838{
9ca3ba01
ID
839 /*
840 * For CHV ignore the error and consider only the P value.
841 * Prefer a bigger P value based on HW requirements.
842 */
843 if (IS_CHERRYVIEW(dev)) {
844 *error_ppm = 0;
845
846 return calculated_clock->p > best_clock->p;
847 }
848
24be4e46
ID
849 if (WARN_ON_ONCE(!target_freq))
850 return false;
851
d5dd62bd
ID
852 *error_ppm = div_u64(1000000ULL *
853 abs(target_freq - calculated_clock->dot),
854 target_freq);
855 /*
856 * Prefer a better P value over a better (smaller) error if the error
857 * is small. Ensure this preference for future configurations too by
858 * setting the error to 0.
859 */
860 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
861 *error_ppm = 0;
862
863 return true;
864 }
865
866 return *error_ppm + 10 < best_error_ppm;
867}
868
a0c4da24 869static bool
a93e255f
ACO
870vlv_find_best_dpll(const intel_limit_t *limit,
871 struct intel_crtc_state *crtc_state,
ee9300bb
DV
872 int target, int refclk, intel_clock_t *match_clock,
873 intel_clock_t *best_clock)
a0c4da24 874{
a93e255f 875 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 876 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 877 intel_clock_t clock;
69e4f900 878 unsigned int bestppm = 1000000;
27e639bf
VS
879 /* min update 19.2 MHz */
880 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 881 bool found = false;
a0c4da24 882
6b4bf1c4
VS
883 target *= 5; /* fast clock */
884
885 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
886
887 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 888 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 889 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 890 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 891 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 892 clock.p = clock.p1 * clock.p2;
a0c4da24 893 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 894 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 895 unsigned int ppm;
69e4f900 896
6b4bf1c4
VS
897 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
898 refclk * clock.m1);
899
900 vlv_clock(refclk, &clock);
43b0ac53 901
f01b7962
VS
902 if (!intel_PLL_is_valid(dev, limit,
903 &clock))
43b0ac53
VS
904 continue;
905
d5dd62bd
ID
906 if (!vlv_PLL_is_optimal(dev, target,
907 &clock,
908 best_clock,
909 bestppm, &ppm))
910 continue;
6b4bf1c4 911
d5dd62bd
ID
912 *best_clock = clock;
913 bestppm = ppm;
914 found = true;
a0c4da24
JB
915 }
916 }
917 }
918 }
a0c4da24 919
49e497ef 920 return found;
a0c4da24 921}
a4fc5ed6 922
ef9348c8 923static bool
a93e255f
ACO
924chv_find_best_dpll(const intel_limit_t *limit,
925 struct intel_crtc_state *crtc_state,
ef9348c8
CML
926 int target, int refclk, intel_clock_t *match_clock,
927 intel_clock_t *best_clock)
928{
a93e255f 929 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 930 struct drm_device *dev = crtc->base.dev;
9ca3ba01 931 unsigned int best_error_ppm;
ef9348c8
CML
932 intel_clock_t clock;
933 uint64_t m2;
934 int found = false;
935
936 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 937 best_error_ppm = 1000000;
ef9348c8
CML
938
939 /*
940 * Based on hardware doc, the n always set to 1, and m1 always
941 * set to 2. If requires to support 200Mhz refclk, we need to
942 * revisit this because n may not 1 anymore.
943 */
944 clock.n = 1, clock.m1 = 2;
945 target *= 5; /* fast clock */
946
947 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
948 for (clock.p2 = limit->p2.p2_fast;
949 clock.p2 >= limit->p2.p2_slow;
950 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 951 unsigned int error_ppm;
ef9348c8
CML
952
953 clock.p = clock.p1 * clock.p2;
954
955 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
956 clock.n) << 22, refclk * clock.m1);
957
958 if (m2 > INT_MAX/clock.m1)
959 continue;
960
961 clock.m2 = m2;
962
963 chv_clock(refclk, &clock);
964
965 if (!intel_PLL_is_valid(dev, limit, &clock))
966 continue;
967
9ca3ba01
ID
968 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
969 best_error_ppm, &error_ppm))
970 continue;
971
972 *best_clock = clock;
973 best_error_ppm = error_ppm;
974 found = true;
ef9348c8
CML
975 }
976 }
977
978 return found;
979}
980
5ab7b0b7
ID
981bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
982 intel_clock_t *best_clock)
983{
984 int refclk = i9xx_get_refclk(crtc_state, 0);
985
986 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
987 target_clock, refclk, NULL, best_clock);
988}
989
20ddf665
VS
990bool intel_crtc_active(struct drm_crtc *crtc)
991{
992 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
993
994 /* Be paranoid as we can arrive here with only partial
995 * state retrieved from the hardware during setup.
996 *
241bfc38 997 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
998 * as Haswell has gained clock readout/fastboot support.
999 *
66e514c1 1000 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 1001 * properly reconstruct framebuffers.
c3d1f436
MR
1002 *
1003 * FIXME: The intel_crtc->active here should be switched to
1004 * crtc->state->active once we have proper CRTC states wired up
1005 * for atomic.
20ddf665 1006 */
c3d1f436 1007 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1008 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1009}
1010
a5c961d1
PZ
1011enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1012 enum pipe pipe)
1013{
1014 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1015 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1016
6e3c9717 1017 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1018}
1019
fbf49ea2
VS
1020static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1021{
1022 struct drm_i915_private *dev_priv = dev->dev_private;
1023 u32 reg = PIPEDSL(pipe);
1024 u32 line1, line2;
1025 u32 line_mask;
1026
1027 if (IS_GEN2(dev))
1028 line_mask = DSL_LINEMASK_GEN2;
1029 else
1030 line_mask = DSL_LINEMASK_GEN3;
1031
1032 line1 = I915_READ(reg) & line_mask;
1033 mdelay(5);
1034 line2 = I915_READ(reg) & line_mask;
1035
1036 return line1 == line2;
1037}
1038
ab7ad7f6
KP
1039/*
1040 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1041 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1042 *
1043 * After disabling a pipe, we can't wait for vblank in the usual way,
1044 * spinning on the vblank interrupt status bit, since we won't actually
1045 * see an interrupt when the pipe is disabled.
1046 *
ab7ad7f6
KP
1047 * On Gen4 and above:
1048 * wait for the pipe register state bit to turn off
1049 *
1050 * Otherwise:
1051 * wait for the display line value to settle (it usually
1052 * ends up stopping at the start of the next frame).
58e10eb9 1053 *
9d0498a2 1054 */
575f7ab7 1055static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1056{
575f7ab7 1057 struct drm_device *dev = crtc->base.dev;
9d0498a2 1058 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1059 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1060 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1061
1062 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 1063 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1064
1065 /* Wait for the Pipe State to go off */
58e10eb9
CW
1066 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1067 100))
284637d9 1068 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1069 } else {
ab7ad7f6 1070 /* Wait for the display line to settle */
fbf49ea2 1071 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1072 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1073 }
79e53945
JB
1074}
1075
b0ea7d37
DL
1076/*
1077 * ibx_digital_port_connected - is the specified port connected?
1078 * @dev_priv: i915 private structure
1079 * @port: the port to test
1080 *
1081 * Returns true if @port is connected, false otherwise.
1082 */
1083bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1084 struct intel_digital_port *port)
1085{
1086 u32 bit;
1087
c36346e3 1088 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1089 switch (port->port) {
c36346e3
DL
1090 case PORT_B:
1091 bit = SDE_PORTB_HOTPLUG;
1092 break;
1093 case PORT_C:
1094 bit = SDE_PORTC_HOTPLUG;
1095 break;
1096 case PORT_D:
1097 bit = SDE_PORTD_HOTPLUG;
1098 break;
1099 default:
1100 return true;
1101 }
1102 } else {
eba905b2 1103 switch (port->port) {
c36346e3
DL
1104 case PORT_B:
1105 bit = SDE_PORTB_HOTPLUG_CPT;
1106 break;
1107 case PORT_C:
1108 bit = SDE_PORTC_HOTPLUG_CPT;
1109 break;
1110 case PORT_D:
1111 bit = SDE_PORTD_HOTPLUG_CPT;
1112 break;
1113 default:
1114 return true;
1115 }
b0ea7d37
DL
1116 }
1117
1118 return I915_READ(SDEISR) & bit;
1119}
1120
b24e7179
JB
1121static const char *state_string(bool enabled)
1122{
1123 return enabled ? "on" : "off";
1124}
1125
1126/* Only for pre-ILK configs */
55607e8a
DV
1127void assert_pll(struct drm_i915_private *dev_priv,
1128 enum pipe pipe, bool state)
b24e7179
JB
1129{
1130 int reg;
1131 u32 val;
1132 bool cur_state;
1133
1134 reg = DPLL(pipe);
1135 val = I915_READ(reg);
1136 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1137 I915_STATE_WARN(cur_state != state,
b24e7179
JB
1138 "PLL state assertion failure (expected %s, current %s)\n",
1139 state_string(state), state_string(cur_state));
1140}
b24e7179 1141
23538ef1
JN
1142/* XXX: the dsi pll is shared between MIPI DSI ports */
1143static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1144{
1145 u32 val;
1146 bool cur_state;
1147
a580516d 1148 mutex_lock(&dev_priv->sb_lock);
23538ef1 1149 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1150 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1151
1152 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1153 I915_STATE_WARN(cur_state != state,
23538ef1
JN
1154 "DSI PLL state assertion failure (expected %s, current %s)\n",
1155 state_string(state), state_string(cur_state));
1156}
1157#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1158#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1159
55607e8a 1160struct intel_shared_dpll *
e2b78267
DV
1161intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1162{
1163 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1164
6e3c9717 1165 if (crtc->config->shared_dpll < 0)
e2b78267
DV
1166 return NULL;
1167
6e3c9717 1168 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
e2b78267
DV
1169}
1170
040484af 1171/* For ILK+ */
55607e8a
DV
1172void assert_shared_dpll(struct drm_i915_private *dev_priv,
1173 struct intel_shared_dpll *pll,
1174 bool state)
040484af 1175{
040484af 1176 bool cur_state;
5358901f 1177 struct intel_dpll_hw_state hw_state;
040484af 1178
92b27b08 1179 if (WARN (!pll,
46edb027 1180 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1181 return;
ee7b9f93 1182
5358901f 1183 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
e2c719b7 1184 I915_STATE_WARN(cur_state != state,
5358901f
DV
1185 "%s assertion failure (expected %s, current %s)\n",
1186 pll->name, state_string(state), state_string(cur_state));
040484af 1187}
040484af
JB
1188
1189static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1190 enum pipe pipe, bool state)
1191{
1192 int reg;
1193 u32 val;
1194 bool cur_state;
ad80a810
PZ
1195 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1196 pipe);
040484af 1197
affa9354
PZ
1198 if (HAS_DDI(dev_priv->dev)) {
1199 /* DDI does not have a specific FDI_TX register */
ad80a810 1200 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1201 val = I915_READ(reg);
ad80a810 1202 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1203 } else {
1204 reg = FDI_TX_CTL(pipe);
1205 val = I915_READ(reg);
1206 cur_state = !!(val & FDI_TX_ENABLE);
1207 }
e2c719b7 1208 I915_STATE_WARN(cur_state != state,
040484af
JB
1209 "FDI TX state assertion failure (expected %s, current %s)\n",
1210 state_string(state), state_string(cur_state));
1211}
1212#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1213#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1214
1215static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1216 enum pipe pipe, bool state)
1217{
1218 int reg;
1219 u32 val;
1220 bool cur_state;
1221
d63fa0dc
PZ
1222 reg = FDI_RX_CTL(pipe);
1223 val = I915_READ(reg);
1224 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1225 I915_STATE_WARN(cur_state != state,
040484af
JB
1226 "FDI RX state assertion failure (expected %s, current %s)\n",
1227 state_string(state), state_string(cur_state));
1228}
1229#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1230#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1231
1232static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1233 enum pipe pipe)
1234{
1235 int reg;
1236 u32 val;
1237
1238 /* ILK FDI PLL is always enabled */
3d13ef2e 1239 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1240 return;
1241
bf507ef7 1242 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1243 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1244 return;
1245
040484af
JB
1246 reg = FDI_TX_CTL(pipe);
1247 val = I915_READ(reg);
e2c719b7 1248 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1249}
1250
55607e8a
DV
1251void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1252 enum pipe pipe, bool state)
040484af
JB
1253{
1254 int reg;
1255 u32 val;
55607e8a 1256 bool cur_state;
040484af
JB
1257
1258 reg = FDI_RX_CTL(pipe);
1259 val = I915_READ(reg);
55607e8a 1260 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1261 I915_STATE_WARN(cur_state != state,
55607e8a
DV
1262 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1263 state_string(state), state_string(cur_state));
040484af
JB
1264}
1265
b680c37a
DV
1266void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1267 enum pipe pipe)
ea0760cf 1268{
bedd4dba
JN
1269 struct drm_device *dev = dev_priv->dev;
1270 int pp_reg;
ea0760cf
JB
1271 u32 val;
1272 enum pipe panel_pipe = PIPE_A;
0de3b485 1273 bool locked = true;
ea0760cf 1274
bedd4dba
JN
1275 if (WARN_ON(HAS_DDI(dev)))
1276 return;
1277
1278 if (HAS_PCH_SPLIT(dev)) {
1279 u32 port_sel;
1280
ea0760cf 1281 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1282 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1283
1284 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1285 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1286 panel_pipe = PIPE_B;
1287 /* XXX: else fix for eDP */
1288 } else if (IS_VALLEYVIEW(dev)) {
1289 /* presumably write lock depends on pipe, not port select */
1290 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1291 panel_pipe = pipe;
ea0760cf
JB
1292 } else {
1293 pp_reg = PP_CONTROL;
bedd4dba
JN
1294 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1295 panel_pipe = PIPE_B;
ea0760cf
JB
1296 }
1297
1298 val = I915_READ(pp_reg);
1299 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1300 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1301 locked = false;
1302
e2c719b7 1303 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1304 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1305 pipe_name(pipe));
ea0760cf
JB
1306}
1307
93ce0ba6
JN
1308static void assert_cursor(struct drm_i915_private *dev_priv,
1309 enum pipe pipe, bool state)
1310{
1311 struct drm_device *dev = dev_priv->dev;
1312 bool cur_state;
1313
d9d82081 1314 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1315 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1316 else
5efb3e28 1317 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1318
e2c719b7 1319 I915_STATE_WARN(cur_state != state,
93ce0ba6
JN
1320 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1321 pipe_name(pipe), state_string(state), state_string(cur_state));
1322}
1323#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1324#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1325
b840d907
JB
1326void assert_pipe(struct drm_i915_private *dev_priv,
1327 enum pipe pipe, bool state)
b24e7179
JB
1328{
1329 int reg;
1330 u32 val;
63d7bbe9 1331 bool cur_state;
702e7a56
PZ
1332 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1333 pipe);
b24e7179 1334
b6b5d049
VS
1335 /* if we need the pipe quirk it must be always on */
1336 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1337 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1338 state = true;
1339
f458ebbc 1340 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1341 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1342 cur_state = false;
1343 } else {
1344 reg = PIPECONF(cpu_transcoder);
1345 val = I915_READ(reg);
1346 cur_state = !!(val & PIPECONF_ENABLE);
1347 }
1348
e2c719b7 1349 I915_STATE_WARN(cur_state != state,
63d7bbe9 1350 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1351 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1352}
1353
931872fc
CW
1354static void assert_plane(struct drm_i915_private *dev_priv,
1355 enum plane plane, bool state)
b24e7179
JB
1356{
1357 int reg;
1358 u32 val;
931872fc 1359 bool cur_state;
b24e7179
JB
1360
1361 reg = DSPCNTR(plane);
1362 val = I915_READ(reg);
931872fc 1363 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1364 I915_STATE_WARN(cur_state != state,
931872fc
CW
1365 "plane %c assertion failure (expected %s, current %s)\n",
1366 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1367}
1368
931872fc
CW
1369#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1370#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1371
b24e7179
JB
1372static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1373 enum pipe pipe)
1374{
653e1026 1375 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1376 int reg, i;
1377 u32 val;
1378 int cur_pipe;
1379
653e1026
VS
1380 /* Primary planes are fixed to pipes on gen4+ */
1381 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1382 reg = DSPCNTR(pipe);
1383 val = I915_READ(reg);
e2c719b7 1384 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1385 "plane %c assertion failure, should be disabled but not\n",
1386 plane_name(pipe));
19ec1358 1387 return;
28c05794 1388 }
19ec1358 1389
b24e7179 1390 /* Need to check both planes against the pipe */
055e393f 1391 for_each_pipe(dev_priv, i) {
b24e7179
JB
1392 reg = DSPCNTR(i);
1393 val = I915_READ(reg);
1394 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1395 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1396 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1397 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1398 plane_name(i), pipe_name(pipe));
b24e7179
JB
1399 }
1400}
1401
19332d7a
JB
1402static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1403 enum pipe pipe)
1404{
20674eef 1405 struct drm_device *dev = dev_priv->dev;
1fe47785 1406 int reg, sprite;
19332d7a
JB
1407 u32 val;
1408
7feb8b88 1409 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1410 for_each_sprite(dev_priv, pipe, sprite) {
7feb8b88 1411 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1412 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1413 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1414 sprite, pipe_name(pipe));
1415 }
1416 } else if (IS_VALLEYVIEW(dev)) {
3bdcfc0c 1417 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 1418 reg = SPCNTR(pipe, sprite);
20674eef 1419 val = I915_READ(reg);
e2c719b7 1420 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1421 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1422 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1423 }
1424 } else if (INTEL_INFO(dev)->gen >= 7) {
1425 reg = SPRCTL(pipe);
19332d7a 1426 val = I915_READ(reg);
e2c719b7 1427 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1428 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1429 plane_name(pipe), pipe_name(pipe));
1430 } else if (INTEL_INFO(dev)->gen >= 5) {
1431 reg = DVSCNTR(pipe);
19332d7a 1432 val = I915_READ(reg);
e2c719b7 1433 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1434 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1435 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1436 }
1437}
1438
08c71e5e
VS
1439static void assert_vblank_disabled(struct drm_crtc *crtc)
1440{
e2c719b7 1441 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1442 drm_crtc_vblank_put(crtc);
1443}
1444
89eff4be 1445static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1446{
1447 u32 val;
1448 bool enabled;
1449
e2c719b7 1450 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1451
92f2584a
JB
1452 val = I915_READ(PCH_DREF_CONTROL);
1453 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1454 DREF_SUPERSPREAD_SOURCE_MASK));
e2c719b7 1455 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
92f2584a
JB
1456}
1457
ab9412ba
DV
1458static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1459 enum pipe pipe)
92f2584a
JB
1460{
1461 int reg;
1462 u32 val;
1463 bool enabled;
1464
ab9412ba 1465 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1466 val = I915_READ(reg);
1467 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1468 I915_STATE_WARN(enabled,
9db4a9c7
JB
1469 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1470 pipe_name(pipe));
92f2584a
JB
1471}
1472
4e634389
KP
1473static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1474 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1475{
1476 if ((val & DP_PORT_EN) == 0)
1477 return false;
1478
1479 if (HAS_PCH_CPT(dev_priv->dev)) {
1480 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1481 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1482 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1483 return false;
44f37d1f
CML
1484 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1485 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1486 return false;
f0575e92
KP
1487 } else {
1488 if ((val & DP_PIPE_MASK) != (pipe << 30))
1489 return false;
1490 }
1491 return true;
1492}
1493
1519b995
KP
1494static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1495 enum pipe pipe, u32 val)
1496{
dc0fa718 1497 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1498 return false;
1499
1500 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1501 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1502 return false;
44f37d1f
CML
1503 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1504 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1505 return false;
1519b995 1506 } else {
dc0fa718 1507 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1508 return false;
1509 }
1510 return true;
1511}
1512
1513static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1514 enum pipe pipe, u32 val)
1515{
1516 if ((val & LVDS_PORT_EN) == 0)
1517 return false;
1518
1519 if (HAS_PCH_CPT(dev_priv->dev)) {
1520 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1521 return false;
1522 } else {
1523 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1524 return false;
1525 }
1526 return true;
1527}
1528
1529static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1530 enum pipe pipe, u32 val)
1531{
1532 if ((val & ADPA_DAC_ENABLE) == 0)
1533 return false;
1534 if (HAS_PCH_CPT(dev_priv->dev)) {
1535 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1536 return false;
1537 } else {
1538 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1539 return false;
1540 }
1541 return true;
1542}
1543
291906f1 1544static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1545 enum pipe pipe, int reg, u32 port_sel)
291906f1 1546{
47a05eca 1547 u32 val = I915_READ(reg);
e2c719b7 1548 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1549 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1550 reg, pipe_name(pipe));
de9a35ab 1551
e2c719b7 1552 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1553 && (val & DP_PIPEB_SELECT),
de9a35ab 1554 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1555}
1556
1557static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1558 enum pipe pipe, int reg)
1559{
47a05eca 1560 u32 val = I915_READ(reg);
e2c719b7 1561 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1562 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1563 reg, pipe_name(pipe));
de9a35ab 1564
e2c719b7 1565 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1566 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1567 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1568}
1569
1570static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1571 enum pipe pipe)
1572{
1573 int reg;
1574 u32 val;
291906f1 1575
f0575e92
KP
1576 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1577 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1578 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1579
1580 reg = PCH_ADPA;
1581 val = I915_READ(reg);
e2c719b7 1582 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1583 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1584 pipe_name(pipe));
291906f1
JB
1585
1586 reg = PCH_LVDS;
1587 val = I915_READ(reg);
e2c719b7 1588 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1589 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1590 pipe_name(pipe));
291906f1 1591
e2debe91
PZ
1592 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1593 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1594 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1595}
1596
40e9cf64
JB
1597static void intel_init_dpio(struct drm_device *dev)
1598{
1599 struct drm_i915_private *dev_priv = dev->dev_private;
1600
1601 if (!IS_VALLEYVIEW(dev))
1602 return;
1603
a09caddd
CML
1604 /*
1605 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1606 * CHV x1 PHY (DP/HDMI D)
1607 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1608 */
1609 if (IS_CHERRYVIEW(dev)) {
1610 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1611 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1612 } else {
1613 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1614 }
5382f5f3
JB
1615}
1616
d288f65f 1617static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1618 const struct intel_crtc_state *pipe_config)
87442f73 1619{
426115cf
DV
1620 struct drm_device *dev = crtc->base.dev;
1621 struct drm_i915_private *dev_priv = dev->dev_private;
1622 int reg = DPLL(crtc->pipe);
d288f65f 1623 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1624
426115cf 1625 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1626
1627 /* No really, not for ILK+ */
1628 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1629
1630 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1631 if (IS_MOBILE(dev_priv->dev))
426115cf 1632 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1633
426115cf
DV
1634 I915_WRITE(reg, dpll);
1635 POSTING_READ(reg);
1636 udelay(150);
1637
1638 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1639 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1640
d288f65f 1641 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1642 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1643
1644 /* We do this three times for luck */
426115cf 1645 I915_WRITE(reg, dpll);
87442f73
DV
1646 POSTING_READ(reg);
1647 udelay(150); /* wait for warmup */
426115cf 1648 I915_WRITE(reg, dpll);
87442f73
DV
1649 POSTING_READ(reg);
1650 udelay(150); /* wait for warmup */
426115cf 1651 I915_WRITE(reg, dpll);
87442f73
DV
1652 POSTING_READ(reg);
1653 udelay(150); /* wait for warmup */
1654}
1655
d288f65f 1656static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1657 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1658{
1659 struct drm_device *dev = crtc->base.dev;
1660 struct drm_i915_private *dev_priv = dev->dev_private;
1661 int pipe = crtc->pipe;
1662 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1663 u32 tmp;
1664
1665 assert_pipe_disabled(dev_priv, crtc->pipe);
1666
1667 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1668
a580516d 1669 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1670
1671 /* Enable back the 10bit clock to display controller */
1672 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1673 tmp |= DPIO_DCLKP_EN;
1674 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1675
54433e91
VS
1676 mutex_unlock(&dev_priv->sb_lock);
1677
9d556c99
CML
1678 /*
1679 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1680 */
1681 udelay(1);
1682
1683 /* Enable PLL */
d288f65f 1684 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1685
1686 /* Check PLL is locked */
a11b0703 1687 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1688 DRM_ERROR("PLL %d failed to lock\n", pipe);
1689
a11b0703 1690 /* not sure when this should be written */
d288f65f 1691 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703 1692 POSTING_READ(DPLL_MD(pipe));
9d556c99
CML
1693}
1694
1c4e0274
VS
1695static int intel_num_dvo_pipes(struct drm_device *dev)
1696{
1697 struct intel_crtc *crtc;
1698 int count = 0;
1699
1700 for_each_intel_crtc(dev, crtc)
3538b9df 1701 count += crtc->base.state->active &&
409ee761 1702 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1703
1704 return count;
1705}
1706
66e3d5c0 1707static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1708{
66e3d5c0
DV
1709 struct drm_device *dev = crtc->base.dev;
1710 struct drm_i915_private *dev_priv = dev->dev_private;
1711 int reg = DPLL(crtc->pipe);
6e3c9717 1712 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1713
66e3d5c0 1714 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1715
63d7bbe9 1716 /* No really, not for ILK+ */
3d13ef2e 1717 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1718
1719 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1720 if (IS_MOBILE(dev) && !IS_I830(dev))
1721 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1722
1c4e0274
VS
1723 /* Enable DVO 2x clock on both PLLs if necessary */
1724 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1725 /*
1726 * It appears to be important that we don't enable this
1727 * for the current pipe before otherwise configuring the
1728 * PLL. No idea how this should be handled if multiple
1729 * DVO outputs are enabled simultaneosly.
1730 */
1731 dpll |= DPLL_DVO_2X_MODE;
1732 I915_WRITE(DPLL(!crtc->pipe),
1733 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1734 }
66e3d5c0
DV
1735
1736 /* Wait for the clocks to stabilize. */
1737 POSTING_READ(reg);
1738 udelay(150);
1739
1740 if (INTEL_INFO(dev)->gen >= 4) {
1741 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1742 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1743 } else {
1744 /* The pixel multiplier can only be updated once the
1745 * DPLL is enabled and the clocks are stable.
1746 *
1747 * So write it again.
1748 */
1749 I915_WRITE(reg, dpll);
1750 }
63d7bbe9
JB
1751
1752 /* We do this three times for luck */
66e3d5c0 1753 I915_WRITE(reg, dpll);
63d7bbe9
JB
1754 POSTING_READ(reg);
1755 udelay(150); /* wait for warmup */
66e3d5c0 1756 I915_WRITE(reg, dpll);
63d7bbe9
JB
1757 POSTING_READ(reg);
1758 udelay(150); /* wait for warmup */
66e3d5c0 1759 I915_WRITE(reg, dpll);
63d7bbe9
JB
1760 POSTING_READ(reg);
1761 udelay(150); /* wait for warmup */
1762}
1763
1764/**
50b44a44 1765 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1766 * @dev_priv: i915 private structure
1767 * @pipe: pipe PLL to disable
1768 *
1769 * Disable the PLL for @pipe, making sure the pipe is off first.
1770 *
1771 * Note! This is for pre-ILK only.
1772 */
1c4e0274 1773static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1774{
1c4e0274
VS
1775 struct drm_device *dev = crtc->base.dev;
1776 struct drm_i915_private *dev_priv = dev->dev_private;
1777 enum pipe pipe = crtc->pipe;
1778
1779 /* Disable DVO 2x clock on both PLLs if necessary */
1780 if (IS_I830(dev) &&
409ee761 1781 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1782 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1783 I915_WRITE(DPLL(PIPE_B),
1784 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1785 I915_WRITE(DPLL(PIPE_A),
1786 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1787 }
1788
b6b5d049
VS
1789 /* Don't disable pipe or pipe PLLs if needed */
1790 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1791 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1792 return;
1793
1794 /* Make sure the pipe isn't still relying on us */
1795 assert_pipe_disabled(dev_priv, pipe);
1796
50b44a44
DV
1797 I915_WRITE(DPLL(pipe), 0);
1798 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1799}
1800
f6071166
JB
1801static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1802{
1803 u32 val = 0;
1804
1805 /* Make sure the pipe isn't still relying on us */
1806 assert_pipe_disabled(dev_priv, pipe);
1807
e5cbfbfb
ID
1808 /*
1809 * Leave integrated clock source and reference clock enabled for pipe B.
1810 * The latter is needed for VGA hotplug / manual detection.
1811 */
f6071166 1812 if (pipe == PIPE_B)
e5cbfbfb 1813 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1814 I915_WRITE(DPLL(pipe), val);
1815 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1816
1817}
1818
1819static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1820{
d752048d 1821 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1822 u32 val;
1823
a11b0703
VS
1824 /* Make sure the pipe isn't still relying on us */
1825 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1826
a11b0703 1827 /* Set PLL en = 0 */
d17ec4ce 1828 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1829 if (pipe != PIPE_A)
1830 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1831 I915_WRITE(DPLL(pipe), val);
1832 POSTING_READ(DPLL(pipe));
d752048d 1833
a580516d 1834 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1835
1836 /* Disable 10bit clock to display controller */
1837 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1838 val &= ~DPIO_DCLKP_EN;
1839 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1840
61407f6d
VS
1841 /* disable left/right clock distribution */
1842 if (pipe != PIPE_B) {
1843 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1844 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1845 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1846 } else {
1847 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1848 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1849 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1850 }
1851
a580516d 1852 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1853}
1854
e4607fcf 1855void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1856 struct intel_digital_port *dport,
1857 unsigned int expected_mask)
89b667f8
JB
1858{
1859 u32 port_mask;
00fc31b7 1860 int dpll_reg;
89b667f8 1861
e4607fcf
CML
1862 switch (dport->port) {
1863 case PORT_B:
89b667f8 1864 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1865 dpll_reg = DPLL(0);
e4607fcf
CML
1866 break;
1867 case PORT_C:
89b667f8 1868 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1869 dpll_reg = DPLL(0);
9b6de0a1 1870 expected_mask <<= 4;
00fc31b7
CML
1871 break;
1872 case PORT_D:
1873 port_mask = DPLL_PORTD_READY_MASK;
1874 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1875 break;
1876 default:
1877 BUG();
1878 }
89b667f8 1879
9b6de0a1
VS
1880 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1881 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1882 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1883}
1884
b14b1055
DV
1885static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1886{
1887 struct drm_device *dev = crtc->base.dev;
1888 struct drm_i915_private *dev_priv = dev->dev_private;
1889 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1890
be19f0ff
CW
1891 if (WARN_ON(pll == NULL))
1892 return;
1893
3e369b76 1894 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1895 if (pll->active == 0) {
1896 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1897 WARN_ON(pll->on);
1898 assert_shared_dpll_disabled(dev_priv, pll);
1899
1900 pll->mode_set(dev_priv, pll);
1901 }
1902}
1903
92f2584a 1904/**
85b3894f 1905 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1906 * @dev_priv: i915 private structure
1907 * @pipe: pipe PLL to enable
1908 *
1909 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1910 * drives the transcoder clock.
1911 */
85b3894f 1912static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1913{
3d13ef2e
DL
1914 struct drm_device *dev = crtc->base.dev;
1915 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1916 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1917
87a875bb 1918 if (WARN_ON(pll == NULL))
48da64a8
CW
1919 return;
1920
3e369b76 1921 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1922 return;
ee7b9f93 1923
74dd6928 1924 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1925 pll->name, pll->active, pll->on,
e2b78267 1926 crtc->base.base.id);
92f2584a 1927
cdbd2316
DV
1928 if (pll->active++) {
1929 WARN_ON(!pll->on);
e9d6944e 1930 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1931 return;
1932 }
f4a091c7 1933 WARN_ON(pll->on);
ee7b9f93 1934
bd2bb1b9
PZ
1935 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1936
46edb027 1937 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1938 pll->enable(dev_priv, pll);
ee7b9f93 1939 pll->on = true;
92f2584a
JB
1940}
1941
f6daaec2 1942static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1943{
3d13ef2e
DL
1944 struct drm_device *dev = crtc->base.dev;
1945 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1946 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1947
92f2584a 1948 /* PCH only available on ILK+ */
3d13ef2e 1949 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1950 if (WARN_ON(pll == NULL))
ee7b9f93 1951 return;
92f2584a 1952
3e369b76 1953 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1954 return;
7a419866 1955
46edb027
DV
1956 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1957 pll->name, pll->active, pll->on,
e2b78267 1958 crtc->base.base.id);
7a419866 1959
48da64a8 1960 if (WARN_ON(pll->active == 0)) {
e9d6944e 1961 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1962 return;
1963 }
1964
e9d6944e 1965 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1966 WARN_ON(!pll->on);
cdbd2316 1967 if (--pll->active)
7a419866 1968 return;
ee7b9f93 1969
46edb027 1970 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1971 pll->disable(dev_priv, pll);
ee7b9f93 1972 pll->on = false;
bd2bb1b9
PZ
1973
1974 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1975}
1976
b8a4f404
PZ
1977static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1978 enum pipe pipe)
040484af 1979{
23670b32 1980 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1981 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1983 uint32_t reg, val, pipeconf_val;
040484af
JB
1984
1985 /* PCH only available on ILK+ */
55522f37 1986 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1987
1988 /* Make sure PCH DPLL is enabled */
e72f9fbf 1989 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1990 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1991
1992 /* FDI must be feeding us bits for PCH ports */
1993 assert_fdi_tx_enabled(dev_priv, pipe);
1994 assert_fdi_rx_enabled(dev_priv, pipe);
1995
23670b32
DV
1996 if (HAS_PCH_CPT(dev)) {
1997 /* Workaround: Set the timing override bit before enabling the
1998 * pch transcoder. */
1999 reg = TRANS_CHICKEN2(pipe);
2000 val = I915_READ(reg);
2001 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2002 I915_WRITE(reg, val);
59c859d6 2003 }
23670b32 2004
ab9412ba 2005 reg = PCH_TRANSCONF(pipe);
040484af 2006 val = I915_READ(reg);
5f7f726d 2007 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
2008
2009 if (HAS_PCH_IBX(dev_priv->dev)) {
2010 /*
c5de7c6f
VS
2011 * Make the BPC in transcoder be consistent with
2012 * that in pipeconf reg. For HDMI we must use 8bpc
2013 * here for both 8bpc and 12bpc.
e9bcff5c 2014 */
dfd07d72 2015 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
2016 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
2017 val |= PIPECONF_8BPC;
2018 else
2019 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 2020 }
5f7f726d
PZ
2021
2022 val &= ~TRANS_INTERLACE_MASK;
2023 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 2024 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 2025 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
2026 val |= TRANS_LEGACY_INTERLACED_ILK;
2027 else
2028 val |= TRANS_INTERLACED;
5f7f726d
PZ
2029 else
2030 val |= TRANS_PROGRESSIVE;
2031
040484af
JB
2032 I915_WRITE(reg, val | TRANS_ENABLE);
2033 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 2034 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
2035}
2036
8fb033d7 2037static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 2038 enum transcoder cpu_transcoder)
040484af 2039{
8fb033d7 2040 u32 val, pipeconf_val;
8fb033d7
PZ
2041
2042 /* PCH only available on ILK+ */
55522f37 2043 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 2044
8fb033d7 2045 /* FDI must be feeding us bits for PCH ports */
1a240d4d 2046 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 2047 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 2048
223a6fdf
PZ
2049 /* Workaround: set timing override bit. */
2050 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2051 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
2052 I915_WRITE(_TRANSA_CHICKEN2, val);
2053
25f3ef11 2054 val = TRANS_ENABLE;
937bb610 2055 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 2056
9a76b1c6
PZ
2057 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2058 PIPECONF_INTERLACED_ILK)
a35f2679 2059 val |= TRANS_INTERLACED;
8fb033d7
PZ
2060 else
2061 val |= TRANS_PROGRESSIVE;
2062
ab9412ba
DV
2063 I915_WRITE(LPT_TRANSCONF, val);
2064 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 2065 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
2066}
2067
b8a4f404
PZ
2068static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2069 enum pipe pipe)
040484af 2070{
23670b32
DV
2071 struct drm_device *dev = dev_priv->dev;
2072 uint32_t reg, val;
040484af
JB
2073
2074 /* FDI relies on the transcoder */
2075 assert_fdi_tx_disabled(dev_priv, pipe);
2076 assert_fdi_rx_disabled(dev_priv, pipe);
2077
291906f1
JB
2078 /* Ports must be off as well */
2079 assert_pch_ports_disabled(dev_priv, pipe);
2080
ab9412ba 2081 reg = PCH_TRANSCONF(pipe);
040484af
JB
2082 val = I915_READ(reg);
2083 val &= ~TRANS_ENABLE;
2084 I915_WRITE(reg, val);
2085 /* wait for PCH transcoder off, transcoder state */
2086 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 2087 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
2088
2089 if (!HAS_PCH_IBX(dev)) {
2090 /* Workaround: Clear the timing override chicken bit again. */
2091 reg = TRANS_CHICKEN2(pipe);
2092 val = I915_READ(reg);
2093 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2094 I915_WRITE(reg, val);
2095 }
040484af
JB
2096}
2097
ab4d966c 2098static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 2099{
8fb033d7
PZ
2100 u32 val;
2101
ab9412ba 2102 val = I915_READ(LPT_TRANSCONF);
8fb033d7 2103 val &= ~TRANS_ENABLE;
ab9412ba 2104 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2105 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2106 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2107 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2108
2109 /* Workaround: clear timing override bit. */
2110 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2111 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2112 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2113}
2114
b24e7179 2115/**
309cfea8 2116 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2117 * @crtc: crtc responsible for the pipe
b24e7179 2118 *
0372264a 2119 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2120 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2121 */
e1fdc473 2122static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2123{
0372264a
PZ
2124 struct drm_device *dev = crtc->base.dev;
2125 struct drm_i915_private *dev_priv = dev->dev_private;
2126 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2127 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2128 pipe);
1a240d4d 2129 enum pipe pch_transcoder;
b24e7179
JB
2130 int reg;
2131 u32 val;
2132
58c6eaa2 2133 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2134 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2135 assert_sprites_disabled(dev_priv, pipe);
2136
681e5811 2137 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2138 pch_transcoder = TRANSCODER_A;
2139 else
2140 pch_transcoder = pipe;
2141
b24e7179
JB
2142 /*
2143 * A pipe without a PLL won't actually be able to drive bits from
2144 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2145 * need the check.
2146 */
50360403 2147 if (HAS_GMCH_DISPLAY(dev_priv->dev))
409ee761 2148 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2149 assert_dsi_pll_enabled(dev_priv);
2150 else
2151 assert_pll_enabled(dev_priv, pipe);
040484af 2152 else {
6e3c9717 2153 if (crtc->config->has_pch_encoder) {
040484af 2154 /* if driving the PCH, we need FDI enabled */
cc391bbb 2155 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2156 assert_fdi_tx_pll_enabled(dev_priv,
2157 (enum pipe) cpu_transcoder);
040484af
JB
2158 }
2159 /* FIXME: assert CPU port conditions for SNB+ */
2160 }
b24e7179 2161
702e7a56 2162 reg = PIPECONF(cpu_transcoder);
b24e7179 2163 val = I915_READ(reg);
7ad25d48 2164 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2165 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2166 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2167 return;
7ad25d48 2168 }
00d70b15
CW
2169
2170 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2171 POSTING_READ(reg);
b24e7179
JB
2172}
2173
2174/**
309cfea8 2175 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2176 * @crtc: crtc whose pipes is to be disabled
b24e7179 2177 *
575f7ab7
VS
2178 * Disable the pipe of @crtc, making sure that various hardware
2179 * specific requirements are met, if applicable, e.g. plane
2180 * disabled, panel fitter off, etc.
b24e7179
JB
2181 *
2182 * Will wait until the pipe has shut down before returning.
2183 */
575f7ab7 2184static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2185{
575f7ab7 2186 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2187 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2188 enum pipe pipe = crtc->pipe;
b24e7179
JB
2189 int reg;
2190 u32 val;
2191
2192 /*
2193 * Make sure planes won't keep trying to pump pixels to us,
2194 * or we might hang the display.
2195 */
2196 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2197 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2198 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2199
702e7a56 2200 reg = PIPECONF(cpu_transcoder);
b24e7179 2201 val = I915_READ(reg);
00d70b15
CW
2202 if ((val & PIPECONF_ENABLE) == 0)
2203 return;
2204
67adc644
VS
2205 /*
2206 * Double wide has implications for planes
2207 * so best keep it disabled when not needed.
2208 */
6e3c9717 2209 if (crtc->config->double_wide)
67adc644
VS
2210 val &= ~PIPECONF_DOUBLE_WIDE;
2211
2212 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2213 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2214 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2215 val &= ~PIPECONF_ENABLE;
2216
2217 I915_WRITE(reg, val);
2218 if ((val & PIPECONF_ENABLE) == 0)
2219 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2220}
2221
2222/**
262ca2b0 2223 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2224 * @plane: plane to be enabled
2225 * @crtc: crtc for the plane
b24e7179 2226 *
fdd508a6 2227 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2228 */
fdd508a6
VS
2229static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2230 struct drm_crtc *crtc)
b24e7179 2231{
fdd508a6
VS
2232 struct drm_device *dev = plane->dev;
2233 struct drm_i915_private *dev_priv = dev->dev_private;
2234 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2235
2236 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2237 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b70709a6 2238 to_intel_plane_state(plane->state)->visible = true;
939c2fe8 2239
fdd508a6
VS
2240 dev_priv->display.update_primary_plane(crtc, plane->fb,
2241 crtc->x, crtc->y);
b24e7179
JB
2242}
2243
693db184
CW
2244static bool need_vtd_wa(struct drm_device *dev)
2245{
2246#ifdef CONFIG_INTEL_IOMMU
2247 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2248 return true;
2249#endif
2250 return false;
2251}
2252
50470bb0 2253unsigned int
6761dd31
TU
2254intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2255 uint64_t fb_format_modifier)
a57ce0b2 2256{
6761dd31
TU
2257 unsigned int tile_height;
2258 uint32_t pixel_bytes;
a57ce0b2 2259
b5d0e9bf
DL
2260 switch (fb_format_modifier) {
2261 case DRM_FORMAT_MOD_NONE:
2262 tile_height = 1;
2263 break;
2264 case I915_FORMAT_MOD_X_TILED:
2265 tile_height = IS_GEN2(dev) ? 16 : 8;
2266 break;
2267 case I915_FORMAT_MOD_Y_TILED:
2268 tile_height = 32;
2269 break;
2270 case I915_FORMAT_MOD_Yf_TILED:
6761dd31
TU
2271 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2272 switch (pixel_bytes) {
b5d0e9bf 2273 default:
6761dd31 2274 case 1:
b5d0e9bf
DL
2275 tile_height = 64;
2276 break;
6761dd31
TU
2277 case 2:
2278 case 4:
b5d0e9bf
DL
2279 tile_height = 32;
2280 break;
6761dd31 2281 case 8:
b5d0e9bf
DL
2282 tile_height = 16;
2283 break;
6761dd31 2284 case 16:
b5d0e9bf
DL
2285 WARN_ONCE(1,
2286 "128-bit pixels are not supported for display!");
2287 tile_height = 16;
2288 break;
2289 }
2290 break;
2291 default:
2292 MISSING_CASE(fb_format_modifier);
2293 tile_height = 1;
2294 break;
2295 }
091df6cb 2296
6761dd31
TU
2297 return tile_height;
2298}
2299
2300unsigned int
2301intel_fb_align_height(struct drm_device *dev, unsigned int height,
2302 uint32_t pixel_format, uint64_t fb_format_modifier)
2303{
2304 return ALIGN(height, intel_tile_height(dev, pixel_format,
2305 fb_format_modifier));
a57ce0b2
JB
2306}
2307
f64b98cd
TU
2308static int
2309intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2310 const struct drm_plane_state *plane_state)
2311{
50470bb0 2312 struct intel_rotation_info *info = &view->rotation_info;
50470bb0 2313
f64b98cd
TU
2314 *view = i915_ggtt_view_normal;
2315
50470bb0
TU
2316 if (!plane_state)
2317 return 0;
2318
121920fa 2319 if (!intel_rotation_90_or_270(plane_state->rotation))
50470bb0
TU
2320 return 0;
2321
9abc4648 2322 *view = i915_ggtt_view_rotated;
50470bb0
TU
2323
2324 info->height = fb->height;
2325 info->pixel_format = fb->pixel_format;
2326 info->pitch = fb->pitches[0];
2327 info->fb_modifier = fb->modifier[0];
2328
f64b98cd
TU
2329 return 0;
2330}
2331
4e9a86b6
VS
2332static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2333{
2334 if (INTEL_INFO(dev_priv)->gen >= 9)
2335 return 256 * 1024;
2336 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv))
2337 return 128 * 1024;
2338 else if (INTEL_INFO(dev_priv)->gen >= 4)
2339 return 4 * 1024;
2340 else
2341 return 64 * 1024;
2342}
2343
127bd2ac 2344int
850c4cdc
TU
2345intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2346 struct drm_framebuffer *fb,
82bc3b2d 2347 const struct drm_plane_state *plane_state,
a4872ba6 2348 struct intel_engine_cs *pipelined)
6b95a207 2349{
850c4cdc 2350 struct drm_device *dev = fb->dev;
ce453d81 2351 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2352 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2353 struct i915_ggtt_view view;
6b95a207
KH
2354 u32 alignment;
2355 int ret;
2356
ebcdd39e
MR
2357 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2358
7b911adc
TU
2359 switch (fb->modifier[0]) {
2360 case DRM_FORMAT_MOD_NONE:
4e9a86b6 2361 alignment = intel_linear_alignment(dev_priv);
6b95a207 2362 break;
7b911adc 2363 case I915_FORMAT_MOD_X_TILED:
1fada4cc
DL
2364 if (INTEL_INFO(dev)->gen >= 9)
2365 alignment = 256 * 1024;
2366 else {
2367 /* pin() will align the object as required by fence */
2368 alignment = 0;
2369 }
6b95a207 2370 break;
7b911adc 2371 case I915_FORMAT_MOD_Y_TILED:
1327b9a1
DL
2372 case I915_FORMAT_MOD_Yf_TILED:
2373 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2374 "Y tiling bo slipped through, driver bug!\n"))
2375 return -EINVAL;
2376 alignment = 1 * 1024 * 1024;
2377 break;
6b95a207 2378 default:
7b911adc
TU
2379 MISSING_CASE(fb->modifier[0]);
2380 return -EINVAL;
6b95a207
KH
2381 }
2382
f64b98cd
TU
2383 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2384 if (ret)
2385 return ret;
2386
693db184
CW
2387 /* Note that the w/a also requires 64 PTE of padding following the
2388 * bo. We currently fill all unused PTE with the shadow page and so
2389 * we should always have valid PTE following the scanout preventing
2390 * the VT-d warning.
2391 */
2392 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2393 alignment = 256 * 1024;
2394
d6dd6843
PZ
2395 /*
2396 * Global gtt pte registers are special registers which actually forward
2397 * writes to a chunk of system memory. Which means that there is no risk
2398 * that the register values disappear as soon as we call
2399 * intel_runtime_pm_put(), so it is correct to wrap only the
2400 * pin/unpin/fence and not more.
2401 */
2402 intel_runtime_pm_get(dev_priv);
2403
ce453d81 2404 dev_priv->mm.interruptible = false;
e6617330 2405 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
f64b98cd 2406 &view);
48b956c5 2407 if (ret)
ce453d81 2408 goto err_interruptible;
6b95a207
KH
2409
2410 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2411 * fence, whereas 965+ only requires a fence if using
2412 * framebuffer compression. For simplicity, we always install
2413 * a fence as the cost is not that onerous.
2414 */
06d98131 2415 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2416 if (ret)
2417 goto err_unpin;
1690e1eb 2418
9a5a53b3 2419 i915_gem_object_pin_fence(obj);
6b95a207 2420
ce453d81 2421 dev_priv->mm.interruptible = true;
d6dd6843 2422 intel_runtime_pm_put(dev_priv);
6b95a207 2423 return 0;
48b956c5
CW
2424
2425err_unpin:
f64b98cd 2426 i915_gem_object_unpin_from_display_plane(obj, &view);
ce453d81
CW
2427err_interruptible:
2428 dev_priv->mm.interruptible = true;
d6dd6843 2429 intel_runtime_pm_put(dev_priv);
48b956c5 2430 return ret;
6b95a207
KH
2431}
2432
82bc3b2d
TU
2433static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2434 const struct drm_plane_state *plane_state)
1690e1eb 2435{
82bc3b2d 2436 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd
TU
2437 struct i915_ggtt_view view;
2438 int ret;
82bc3b2d 2439
ebcdd39e
MR
2440 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2441
f64b98cd
TU
2442 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2443 WARN_ONCE(ret, "Couldn't get view from plane state!");
2444
1690e1eb 2445 i915_gem_object_unpin_fence(obj);
f64b98cd 2446 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2447}
2448
c2c75131
DV
2449/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2450 * is assumed to be a power-of-two. */
4e9a86b6
VS
2451unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2452 int *x, int *y,
bc752862
CW
2453 unsigned int tiling_mode,
2454 unsigned int cpp,
2455 unsigned int pitch)
c2c75131 2456{
bc752862
CW
2457 if (tiling_mode != I915_TILING_NONE) {
2458 unsigned int tile_rows, tiles;
c2c75131 2459
bc752862
CW
2460 tile_rows = *y / 8;
2461 *y %= 8;
c2c75131 2462
bc752862
CW
2463 tiles = *x / (512/cpp);
2464 *x %= 512/cpp;
2465
2466 return tile_rows * pitch * 8 + tiles * 4096;
2467 } else {
4e9a86b6 2468 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
bc752862
CW
2469 unsigned int offset;
2470
2471 offset = *y * pitch + *x * cpp;
4e9a86b6
VS
2472 *y = (offset & alignment) / pitch;
2473 *x = ((offset & alignment) - *y * pitch) / cpp;
2474 return offset & ~alignment;
bc752862 2475 }
c2c75131
DV
2476}
2477
b35d63fa 2478static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2479{
2480 switch (format) {
2481 case DISPPLANE_8BPP:
2482 return DRM_FORMAT_C8;
2483 case DISPPLANE_BGRX555:
2484 return DRM_FORMAT_XRGB1555;
2485 case DISPPLANE_BGRX565:
2486 return DRM_FORMAT_RGB565;
2487 default:
2488 case DISPPLANE_BGRX888:
2489 return DRM_FORMAT_XRGB8888;
2490 case DISPPLANE_RGBX888:
2491 return DRM_FORMAT_XBGR8888;
2492 case DISPPLANE_BGRX101010:
2493 return DRM_FORMAT_XRGB2101010;
2494 case DISPPLANE_RGBX101010:
2495 return DRM_FORMAT_XBGR2101010;
2496 }
2497}
2498
bc8d7dff
DL
2499static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2500{
2501 switch (format) {
2502 case PLANE_CTL_FORMAT_RGB_565:
2503 return DRM_FORMAT_RGB565;
2504 default:
2505 case PLANE_CTL_FORMAT_XRGB_8888:
2506 if (rgb_order) {
2507 if (alpha)
2508 return DRM_FORMAT_ABGR8888;
2509 else
2510 return DRM_FORMAT_XBGR8888;
2511 } else {
2512 if (alpha)
2513 return DRM_FORMAT_ARGB8888;
2514 else
2515 return DRM_FORMAT_XRGB8888;
2516 }
2517 case PLANE_CTL_FORMAT_XRGB_2101010:
2518 if (rgb_order)
2519 return DRM_FORMAT_XBGR2101010;
2520 else
2521 return DRM_FORMAT_XRGB2101010;
2522 }
2523}
2524
5724dbd1 2525static bool
f6936e29
DV
2526intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2527 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2528{
2529 struct drm_device *dev = crtc->base.dev;
2530 struct drm_i915_gem_object *obj = NULL;
2531 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2532 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2533 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2534 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2535 PAGE_SIZE);
2536
2537 size_aligned -= base_aligned;
46f297fb 2538
ff2652ea
CW
2539 if (plane_config->size == 0)
2540 return false;
2541
f37b5c2b
DV
2542 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2543 base_aligned,
2544 base_aligned,
2545 size_aligned);
46f297fb 2546 if (!obj)
484b41dd 2547 return false;
46f297fb 2548
49af449b
DL
2549 obj->tiling_mode = plane_config->tiling;
2550 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2551 obj->stride = fb->pitches[0];
46f297fb 2552
6bf129df
DL
2553 mode_cmd.pixel_format = fb->pixel_format;
2554 mode_cmd.width = fb->width;
2555 mode_cmd.height = fb->height;
2556 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2557 mode_cmd.modifier[0] = fb->modifier[0];
2558 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb
JB
2559
2560 mutex_lock(&dev->struct_mutex);
6bf129df 2561 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2562 &mode_cmd, obj)) {
46f297fb
JB
2563 DRM_DEBUG_KMS("intel fb init failed\n");
2564 goto out_unref_obj;
2565 }
46f297fb 2566 mutex_unlock(&dev->struct_mutex);
484b41dd 2567
f6936e29 2568 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2569 return true;
46f297fb
JB
2570
2571out_unref_obj:
2572 drm_gem_object_unreference(&obj->base);
2573 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2574 return false;
2575}
2576
afd65eb4
MR
2577/* Update plane->state->fb to match plane->fb after driver-internal updates */
2578static void
2579update_state_fb(struct drm_plane *plane)
2580{
2581 if (plane->fb == plane->state->fb)
2582 return;
2583
2584 if (plane->state->fb)
2585 drm_framebuffer_unreference(plane->state->fb);
2586 plane->state->fb = plane->fb;
2587 if (plane->state->fb)
2588 drm_framebuffer_reference(plane->state->fb);
2589}
2590
5724dbd1 2591static void
f6936e29
DV
2592intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2593 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2594{
2595 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2596 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2597 struct drm_crtc *c;
2598 struct intel_crtc *i;
2ff8fde1 2599 struct drm_i915_gem_object *obj;
88595ac9
DV
2600 struct drm_plane *primary = intel_crtc->base.primary;
2601 struct drm_framebuffer *fb;
484b41dd 2602
2d14030b 2603 if (!plane_config->fb)
484b41dd
JB
2604 return;
2605
f6936e29 2606 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2607 fb = &plane_config->fb->base;
2608 goto valid_fb;
f55548b5 2609 }
484b41dd 2610
2d14030b 2611 kfree(plane_config->fb);
484b41dd
JB
2612
2613 /*
2614 * Failed to alloc the obj, check to see if we should share
2615 * an fb with another CRTC instead
2616 */
70e1e0ec 2617 for_each_crtc(dev, c) {
484b41dd
JB
2618 i = to_intel_crtc(c);
2619
2620 if (c == &intel_crtc->base)
2621 continue;
2622
2ff8fde1
MR
2623 if (!i->active)
2624 continue;
2625
88595ac9
DV
2626 fb = c->primary->fb;
2627 if (!fb)
484b41dd
JB
2628 continue;
2629
88595ac9 2630 obj = intel_fb_obj(fb);
2ff8fde1 2631 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2632 drm_framebuffer_reference(fb);
2633 goto valid_fb;
484b41dd
JB
2634 }
2635 }
88595ac9
DV
2636
2637 return;
2638
2639valid_fb:
2640 obj = intel_fb_obj(fb);
2641 if (obj->tiling_mode != I915_TILING_NONE)
2642 dev_priv->preserve_bios_swizzle = true;
2643
2644 primary->fb = fb;
36750f28 2645 primary->crtc = primary->state->crtc = &intel_crtc->base;
88595ac9 2646 update_state_fb(primary);
36750f28 2647 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
88595ac9 2648 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
46f297fb
JB
2649}
2650
29b9bde6
DV
2651static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2652 struct drm_framebuffer *fb,
2653 int x, int y)
81255565
JB
2654{
2655 struct drm_device *dev = crtc->dev;
2656 struct drm_i915_private *dev_priv = dev->dev_private;
2657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2658 struct drm_plane *primary = crtc->primary;
2659 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2660 struct drm_i915_gem_object *obj;
81255565 2661 int plane = intel_crtc->plane;
e506a0c6 2662 unsigned long linear_offset;
81255565 2663 u32 dspcntr;
f45651ba 2664 u32 reg = DSPCNTR(plane);
48404c1e 2665 int pixel_size;
f45651ba 2666
b70709a6 2667 if (!visible || !fb) {
fdd508a6
VS
2668 I915_WRITE(reg, 0);
2669 if (INTEL_INFO(dev)->gen >= 4)
2670 I915_WRITE(DSPSURF(plane), 0);
2671 else
2672 I915_WRITE(DSPADDR(plane), 0);
2673 POSTING_READ(reg);
2674 return;
2675 }
2676
c9ba6fad
VS
2677 obj = intel_fb_obj(fb);
2678 if (WARN_ON(obj == NULL))
2679 return;
2680
2681 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2682
f45651ba
VS
2683 dspcntr = DISPPLANE_GAMMA_ENABLE;
2684
fdd508a6 2685 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2686
2687 if (INTEL_INFO(dev)->gen < 4) {
2688 if (intel_crtc->pipe == PIPE_B)
2689 dspcntr |= DISPPLANE_SEL_PIPE_B;
2690
2691 /* pipesrc and dspsize control the size that is scaled from,
2692 * which should always be the user's requested size.
2693 */
2694 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2695 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2696 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2697 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2698 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2699 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2700 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2701 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2702 I915_WRITE(PRIMPOS(plane), 0);
2703 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2704 }
81255565 2705
57779d06
VS
2706 switch (fb->pixel_format) {
2707 case DRM_FORMAT_C8:
81255565
JB
2708 dspcntr |= DISPPLANE_8BPP;
2709 break;
57779d06 2710 case DRM_FORMAT_XRGB1555:
57779d06 2711 dspcntr |= DISPPLANE_BGRX555;
81255565 2712 break;
57779d06
VS
2713 case DRM_FORMAT_RGB565:
2714 dspcntr |= DISPPLANE_BGRX565;
2715 break;
2716 case DRM_FORMAT_XRGB8888:
57779d06
VS
2717 dspcntr |= DISPPLANE_BGRX888;
2718 break;
2719 case DRM_FORMAT_XBGR8888:
57779d06
VS
2720 dspcntr |= DISPPLANE_RGBX888;
2721 break;
2722 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2723 dspcntr |= DISPPLANE_BGRX101010;
2724 break;
2725 case DRM_FORMAT_XBGR2101010:
57779d06 2726 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2727 break;
2728 default:
baba133a 2729 BUG();
81255565 2730 }
57779d06 2731
f45651ba
VS
2732 if (INTEL_INFO(dev)->gen >= 4 &&
2733 obj->tiling_mode != I915_TILING_NONE)
2734 dspcntr |= DISPPLANE_TILED;
81255565 2735
de1aa629
VS
2736 if (IS_G4X(dev))
2737 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2738
b9897127 2739 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2740
c2c75131
DV
2741 if (INTEL_INFO(dev)->gen >= 4) {
2742 intel_crtc->dspaddr_offset =
4e9a86b6
VS
2743 intel_gen4_compute_page_offset(dev_priv,
2744 &x, &y, obj->tiling_mode,
b9897127 2745 pixel_size,
bc752862 2746 fb->pitches[0]);
c2c75131
DV
2747 linear_offset -= intel_crtc->dspaddr_offset;
2748 } else {
e506a0c6 2749 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2750 }
e506a0c6 2751
8e7d688b 2752 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2753 dspcntr |= DISPPLANE_ROTATE_180;
2754
6e3c9717
ACO
2755 x += (intel_crtc->config->pipe_src_w - 1);
2756 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2757
2758 /* Finding the last pixel of the last line of the display
2759 data and adding to linear_offset*/
2760 linear_offset +=
6e3c9717
ACO
2761 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2762 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2763 }
2764
2765 I915_WRITE(reg, dspcntr);
2766
01f2c773 2767 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2768 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2769 I915_WRITE(DSPSURF(plane),
2770 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2771 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2772 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2773 } else
f343c5f6 2774 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2775 POSTING_READ(reg);
17638cd6
JB
2776}
2777
29b9bde6
DV
2778static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2779 struct drm_framebuffer *fb,
2780 int x, int y)
17638cd6
JB
2781{
2782 struct drm_device *dev = crtc->dev;
2783 struct drm_i915_private *dev_priv = dev->dev_private;
2784 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2785 struct drm_plane *primary = crtc->primary;
2786 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2787 struct drm_i915_gem_object *obj;
17638cd6 2788 int plane = intel_crtc->plane;
e506a0c6 2789 unsigned long linear_offset;
17638cd6 2790 u32 dspcntr;
f45651ba 2791 u32 reg = DSPCNTR(plane);
48404c1e 2792 int pixel_size;
f45651ba 2793
b70709a6 2794 if (!visible || !fb) {
fdd508a6
VS
2795 I915_WRITE(reg, 0);
2796 I915_WRITE(DSPSURF(plane), 0);
2797 POSTING_READ(reg);
2798 return;
2799 }
2800
c9ba6fad
VS
2801 obj = intel_fb_obj(fb);
2802 if (WARN_ON(obj == NULL))
2803 return;
2804
2805 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2806
f45651ba
VS
2807 dspcntr = DISPPLANE_GAMMA_ENABLE;
2808
fdd508a6 2809 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2810
2811 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2812 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2813
57779d06
VS
2814 switch (fb->pixel_format) {
2815 case DRM_FORMAT_C8:
17638cd6
JB
2816 dspcntr |= DISPPLANE_8BPP;
2817 break;
57779d06
VS
2818 case DRM_FORMAT_RGB565:
2819 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2820 break;
57779d06 2821 case DRM_FORMAT_XRGB8888:
57779d06
VS
2822 dspcntr |= DISPPLANE_BGRX888;
2823 break;
2824 case DRM_FORMAT_XBGR8888:
57779d06
VS
2825 dspcntr |= DISPPLANE_RGBX888;
2826 break;
2827 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2828 dspcntr |= DISPPLANE_BGRX101010;
2829 break;
2830 case DRM_FORMAT_XBGR2101010:
57779d06 2831 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2832 break;
2833 default:
baba133a 2834 BUG();
17638cd6
JB
2835 }
2836
2837 if (obj->tiling_mode != I915_TILING_NONE)
2838 dspcntr |= DISPPLANE_TILED;
17638cd6 2839
f45651ba 2840 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2841 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2842
b9897127 2843 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2844 intel_crtc->dspaddr_offset =
4e9a86b6
VS
2845 intel_gen4_compute_page_offset(dev_priv,
2846 &x, &y, obj->tiling_mode,
b9897127 2847 pixel_size,
bc752862 2848 fb->pitches[0]);
c2c75131 2849 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2850 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2851 dspcntr |= DISPPLANE_ROTATE_180;
2852
2853 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2854 x += (intel_crtc->config->pipe_src_w - 1);
2855 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2856
2857 /* Finding the last pixel of the last line of the display
2858 data and adding to linear_offset*/
2859 linear_offset +=
6e3c9717
ACO
2860 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2861 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2862 }
2863 }
2864
2865 I915_WRITE(reg, dspcntr);
17638cd6 2866
01f2c773 2867 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2868 I915_WRITE(DSPSURF(plane),
2869 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2870 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2871 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2872 } else {
2873 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2874 I915_WRITE(DSPLINOFF(plane), linear_offset);
2875 }
17638cd6 2876 POSTING_READ(reg);
17638cd6
JB
2877}
2878
b321803d
DL
2879u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2880 uint32_t pixel_format)
2881{
2882 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2883
2884 /*
2885 * The stride is either expressed as a multiple of 64 bytes
2886 * chunks for linear buffers or in number of tiles for tiled
2887 * buffers.
2888 */
2889 switch (fb_modifier) {
2890 case DRM_FORMAT_MOD_NONE:
2891 return 64;
2892 case I915_FORMAT_MOD_X_TILED:
2893 if (INTEL_INFO(dev)->gen == 2)
2894 return 128;
2895 return 512;
2896 case I915_FORMAT_MOD_Y_TILED:
2897 /* No need to check for old gens and Y tiling since this is
2898 * about the display engine and those will be blocked before
2899 * we get here.
2900 */
2901 return 128;
2902 case I915_FORMAT_MOD_Yf_TILED:
2903 if (bits_per_pixel == 8)
2904 return 64;
2905 else
2906 return 128;
2907 default:
2908 MISSING_CASE(fb_modifier);
2909 return 64;
2910 }
2911}
2912
121920fa
TU
2913unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2914 struct drm_i915_gem_object *obj)
2915{
9abc4648 2916 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
121920fa
TU
2917
2918 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
9abc4648 2919 view = &i915_ggtt_view_rotated;
121920fa
TU
2920
2921 return i915_gem_obj_ggtt_offset_view(obj, view);
2922}
2923
a1b2278e
CK
2924/*
2925 * This function detaches (aka. unbinds) unused scalers in hardware
2926 */
2927void skl_detach_scalers(struct intel_crtc *intel_crtc)
2928{
2929 struct drm_device *dev;
2930 struct drm_i915_private *dev_priv;
2931 struct intel_crtc_scaler_state *scaler_state;
2932 int i;
2933
2934 if (!intel_crtc || !intel_crtc->config)
2935 return;
2936
2937 dev = intel_crtc->base.dev;
2938 dev_priv = dev->dev_private;
2939 scaler_state = &intel_crtc->config->scaler_state;
2940
2941 /* loop through and disable scalers that aren't in use */
2942 for (i = 0; i < intel_crtc->num_scalers; i++) {
2943 if (!scaler_state->scalers[i].in_use) {
2944 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2945 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2946 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2947 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2948 intel_crtc->base.base.id, intel_crtc->pipe, i);
2949 }
2950 }
2951}
2952
6156a456 2953u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2954{
6156a456 2955 switch (pixel_format) {
d161cf7a 2956 case DRM_FORMAT_C8:
c34ce3d1 2957 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2958 case DRM_FORMAT_RGB565:
c34ce3d1 2959 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2960 case DRM_FORMAT_XBGR8888:
c34ce3d1 2961 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2962 case DRM_FORMAT_XRGB8888:
c34ce3d1 2963 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2964 /*
2965 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2966 * to be already pre-multiplied. We need to add a knob (or a different
2967 * DRM_FORMAT) for user-space to configure that.
2968 */
f75fb42a 2969 case DRM_FORMAT_ABGR8888:
c34ce3d1 2970 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2971 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2972 case DRM_FORMAT_ARGB8888:
c34ce3d1 2973 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2974 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2975 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2976 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2977 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2978 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2979 case DRM_FORMAT_YUYV:
c34ce3d1 2980 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2981 case DRM_FORMAT_YVYU:
c34ce3d1 2982 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2983 case DRM_FORMAT_UYVY:
c34ce3d1 2984 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2985 case DRM_FORMAT_VYUY:
c34ce3d1 2986 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2987 default:
4249eeef 2988 MISSING_CASE(pixel_format);
70d21f0e 2989 }
8cfcba41 2990
c34ce3d1 2991 return 0;
6156a456 2992}
70d21f0e 2993
6156a456
CK
2994u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2995{
6156a456 2996 switch (fb_modifier) {
30af77c4 2997 case DRM_FORMAT_MOD_NONE:
70d21f0e 2998 break;
30af77c4 2999 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 3000 return PLANE_CTL_TILED_X;
b321803d 3001 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 3002 return PLANE_CTL_TILED_Y;
b321803d 3003 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 3004 return PLANE_CTL_TILED_YF;
70d21f0e 3005 default:
6156a456 3006 MISSING_CASE(fb_modifier);
70d21f0e 3007 }
8cfcba41 3008
c34ce3d1 3009 return 0;
6156a456 3010}
70d21f0e 3011
6156a456
CK
3012u32 skl_plane_ctl_rotation(unsigned int rotation)
3013{
3b7a5119 3014 switch (rotation) {
6156a456
CK
3015 case BIT(DRM_ROTATE_0):
3016 break;
1e8df167
SJ
3017 /*
3018 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3019 * while i915 HW rotation is clockwise, thats why this swapping.
3020 */
3b7a5119 3021 case BIT(DRM_ROTATE_90):
1e8df167 3022 return PLANE_CTL_ROTATE_270;
3b7a5119 3023 case BIT(DRM_ROTATE_180):
c34ce3d1 3024 return PLANE_CTL_ROTATE_180;
3b7a5119 3025 case BIT(DRM_ROTATE_270):
1e8df167 3026 return PLANE_CTL_ROTATE_90;
6156a456
CK
3027 default:
3028 MISSING_CASE(rotation);
3029 }
3030
c34ce3d1 3031 return 0;
6156a456
CK
3032}
3033
3034static void skylake_update_primary_plane(struct drm_crtc *crtc,
3035 struct drm_framebuffer *fb,
3036 int x, int y)
3037{
3038 struct drm_device *dev = crtc->dev;
3039 struct drm_i915_private *dev_priv = dev->dev_private;
3040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
3041 struct drm_plane *plane = crtc->primary;
3042 bool visible = to_intel_plane_state(plane->state)->visible;
6156a456
CK
3043 struct drm_i915_gem_object *obj;
3044 int pipe = intel_crtc->pipe;
3045 u32 plane_ctl, stride_div, stride;
3046 u32 tile_height, plane_offset, plane_size;
3047 unsigned int rotation;
3048 int x_offset, y_offset;
3049 unsigned long surf_addr;
6156a456
CK
3050 struct intel_crtc_state *crtc_state = intel_crtc->config;
3051 struct intel_plane_state *plane_state;
3052 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3053 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3054 int scaler_id = -1;
3055
6156a456
CK
3056 plane_state = to_intel_plane_state(plane->state);
3057
b70709a6 3058 if (!visible || !fb) {
6156a456
CK
3059 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3060 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3061 POSTING_READ(PLANE_CTL(pipe, 0));
3062 return;
3b7a5119 3063 }
70d21f0e 3064
6156a456
CK
3065 plane_ctl = PLANE_CTL_ENABLE |
3066 PLANE_CTL_PIPE_GAMMA_ENABLE |
3067 PLANE_CTL_PIPE_CSC_ENABLE;
3068
3069 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3070 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3071 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3072
3073 rotation = plane->state->rotation;
3074 plane_ctl |= skl_plane_ctl_rotation(rotation);
3075
b321803d
DL
3076 obj = intel_fb_obj(fb);
3077 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3078 fb->pixel_format);
3b7a5119
SJ
3079 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3080
6156a456
CK
3081 /*
3082 * FIXME: intel_plane_state->src, dst aren't set when transitional
3083 * update_plane helpers are called from legacy paths.
3084 * Once full atomic crtc is available, below check can be avoided.
3085 */
3086 if (drm_rect_width(&plane_state->src)) {
3087 scaler_id = plane_state->scaler_id;
3088 src_x = plane_state->src.x1 >> 16;
3089 src_y = plane_state->src.y1 >> 16;
3090 src_w = drm_rect_width(&plane_state->src) >> 16;
3091 src_h = drm_rect_height(&plane_state->src) >> 16;
3092 dst_x = plane_state->dst.x1;
3093 dst_y = plane_state->dst.y1;
3094 dst_w = drm_rect_width(&plane_state->dst);
3095 dst_h = drm_rect_height(&plane_state->dst);
3096
3097 WARN_ON(x != src_x || y != src_y);
3098 } else {
3099 src_w = intel_crtc->config->pipe_src_w;
3100 src_h = intel_crtc->config->pipe_src_h;
3101 }
3102
3b7a5119
SJ
3103 if (intel_rotation_90_or_270(rotation)) {
3104 /* stride = Surface height in tiles */
2614f17d 3105 tile_height = intel_tile_height(dev, fb->pixel_format,
3b7a5119
SJ
3106 fb->modifier[0]);
3107 stride = DIV_ROUND_UP(fb->height, tile_height);
6156a456 3108 x_offset = stride * tile_height - y - src_h;
3b7a5119 3109 y_offset = x;
6156a456 3110 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3111 } else {
3112 stride = fb->pitches[0] / stride_div;
3113 x_offset = x;
3114 y_offset = y;
6156a456 3115 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3116 }
3117 plane_offset = y_offset << 16 | x_offset;
b321803d 3118
70d21f0e 3119 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3120 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3121 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3122 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3123
3124 if (scaler_id >= 0) {
3125 uint32_t ps_ctrl = 0;
3126
3127 WARN_ON(!dst_w || !dst_h);
3128 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3129 crtc_state->scaler_state.scalers[scaler_id].mode;
3130 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3131 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3132 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3133 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3134 I915_WRITE(PLANE_POS(pipe, 0), 0);
3135 } else {
3136 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3137 }
3138
121920fa 3139 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3140
3141 POSTING_READ(PLANE_SURF(pipe, 0));
3142}
3143
17638cd6
JB
3144/* Assume fb object is pinned & idle & fenced and just update base pointers */
3145static int
3146intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3147 int x, int y, enum mode_set_atomic state)
3148{
3149 struct drm_device *dev = crtc->dev;
3150 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 3151
6b8e6ed0
CW
3152 if (dev_priv->display.disable_fbc)
3153 dev_priv->display.disable_fbc(dev);
81255565 3154
29b9bde6
DV
3155 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3156
3157 return 0;
81255565
JB
3158}
3159
7514747d 3160static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3161{
96a02917
VS
3162 struct drm_crtc *crtc;
3163
70e1e0ec 3164 for_each_crtc(dev, crtc) {
96a02917
VS
3165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3166 enum plane plane = intel_crtc->plane;
3167
3168 intel_prepare_page_flip(dev, plane);
3169 intel_finish_page_flip_plane(dev, plane);
3170 }
7514747d
VS
3171}
3172
3173static void intel_update_primary_planes(struct drm_device *dev)
3174{
3175 struct drm_i915_private *dev_priv = dev->dev_private;
3176 struct drm_crtc *crtc;
96a02917 3177
70e1e0ec 3178 for_each_crtc(dev, crtc) {
96a02917
VS
3179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3180
51fd371b 3181 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
3182 /*
3183 * FIXME: Once we have proper support for primary planes (and
3184 * disabling them without disabling the entire crtc) allow again
66e514c1 3185 * a NULL crtc->primary->fb.
947fdaad 3186 */
f4510a27 3187 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 3188 dev_priv->display.update_primary_plane(crtc,
66e514c1 3189 crtc->primary->fb,
262ca2b0
MR
3190 crtc->x,
3191 crtc->y);
51fd371b 3192 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
3193 }
3194}
3195
7514747d
VS
3196void intel_prepare_reset(struct drm_device *dev)
3197{
3198 /* no reset support for gen2 */
3199 if (IS_GEN2(dev))
3200 return;
3201
3202 /* reset doesn't touch the display */
3203 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3204 return;
3205
3206 drm_modeset_lock_all(dev);
f98ce92f
VS
3207 /*
3208 * Disabling the crtcs gracefully seems nicer. Also the
3209 * g33 docs say we should at least disable all the planes.
3210 */
6b72d486 3211 intel_display_suspend(dev);
7514747d
VS
3212}
3213
3214void intel_finish_reset(struct drm_device *dev)
3215{
3216 struct drm_i915_private *dev_priv = to_i915(dev);
3217
3218 /*
3219 * Flips in the rings will be nuked by the reset,
3220 * so complete all pending flips so that user space
3221 * will get its events and not get stuck.
3222 */
3223 intel_complete_page_flips(dev);
3224
3225 /* no reset support for gen2 */
3226 if (IS_GEN2(dev))
3227 return;
3228
3229 /* reset doesn't touch the display */
3230 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3231 /*
3232 * Flips in the rings have been nuked by the reset,
3233 * so update the base address of all primary
3234 * planes to the the last fb to make sure we're
3235 * showing the correct fb after a reset.
3236 */
3237 intel_update_primary_planes(dev);
3238 return;
3239 }
3240
3241 /*
3242 * The display has been reset as well,
3243 * so need a full re-initialization.
3244 */
3245 intel_runtime_pm_disable_interrupts(dev_priv);
3246 intel_runtime_pm_enable_interrupts(dev_priv);
3247
3248 intel_modeset_init_hw(dev);
3249
3250 spin_lock_irq(&dev_priv->irq_lock);
3251 if (dev_priv->display.hpd_irq_setup)
3252 dev_priv->display.hpd_irq_setup(dev);
3253 spin_unlock_irq(&dev_priv->irq_lock);
3254
3255 intel_modeset_setup_hw_state(dev, true);
3256
3257 intel_hpd_init(dev_priv);
3258
3259 drm_modeset_unlock_all(dev);
3260}
3261
2e2f351d 3262static void
14667a4b
CW
3263intel_finish_fb(struct drm_framebuffer *old_fb)
3264{
2ff8fde1 3265 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
2e2f351d 3266 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
14667a4b
CW
3267 bool was_interruptible = dev_priv->mm.interruptible;
3268 int ret;
3269
14667a4b
CW
3270 /* Big Hammer, we also need to ensure that any pending
3271 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3272 * current scanout is retired before unpinning the old
2e2f351d
CW
3273 * framebuffer. Note that we rely on userspace rendering
3274 * into the buffer attached to the pipe they are waiting
3275 * on. If not, userspace generates a GPU hang with IPEHR
3276 * point to the MI_WAIT_FOR_EVENT.
14667a4b
CW
3277 *
3278 * This should only fail upon a hung GPU, in which case we
3279 * can safely continue.
3280 */
3281 dev_priv->mm.interruptible = false;
2e2f351d 3282 ret = i915_gem_object_wait_rendering(obj, true);
14667a4b
CW
3283 dev_priv->mm.interruptible = was_interruptible;
3284
2e2f351d 3285 WARN_ON(ret);
14667a4b
CW
3286}
3287
7d5e3799
CW
3288static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3289{
3290 struct drm_device *dev = crtc->dev;
3291 struct drm_i915_private *dev_priv = dev->dev_private;
3292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3293 bool pending;
3294
3295 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3296 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3297 return false;
3298
5e2d7afc 3299 spin_lock_irq(&dev->event_lock);
7d5e3799 3300 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3301 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3302
3303 return pending;
3304}
3305
e30e8f75
GP
3306static void intel_update_pipe_size(struct intel_crtc *crtc)
3307{
3308 struct drm_device *dev = crtc->base.dev;
3309 struct drm_i915_private *dev_priv = dev->dev_private;
3310 const struct drm_display_mode *adjusted_mode;
3311
3312 if (!i915.fastboot)
3313 return;
3314
3315 /*
3316 * Update pipe size and adjust fitter if needed: the reason for this is
3317 * that in compute_mode_changes we check the native mode (not the pfit
3318 * mode) to see if we can flip rather than do a full mode set. In the
3319 * fastboot case, we'll flip, but if we don't update the pipesrc and
3320 * pfit state, we'll end up with a big fb scanned out into the wrong
3321 * sized surface.
3322 *
3323 * To fix this properly, we need to hoist the checks up into
3324 * compute_mode_changes (or above), check the actual pfit state and
3325 * whether the platform allows pfit disable with pipe active, and only
3326 * then update the pipesrc and pfit state, even on the flip path.
3327 */
3328
6e3c9717 3329 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
3330
3331 I915_WRITE(PIPESRC(crtc->pipe),
3332 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3333 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 3334 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
3335 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3336 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3337 I915_WRITE(PF_CTL(crtc->pipe), 0);
3338 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3339 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3340 }
6e3c9717
ACO
3341 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3342 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3343}
3344
5e84e1a4
ZW
3345static void intel_fdi_normal_train(struct drm_crtc *crtc)
3346{
3347 struct drm_device *dev = crtc->dev;
3348 struct drm_i915_private *dev_priv = dev->dev_private;
3349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3350 int pipe = intel_crtc->pipe;
3351 u32 reg, temp;
3352
3353 /* enable normal train */
3354 reg = FDI_TX_CTL(pipe);
3355 temp = I915_READ(reg);
61e499bf 3356 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3357 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3358 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3359 } else {
3360 temp &= ~FDI_LINK_TRAIN_NONE;
3361 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3362 }
5e84e1a4
ZW
3363 I915_WRITE(reg, temp);
3364
3365 reg = FDI_RX_CTL(pipe);
3366 temp = I915_READ(reg);
3367 if (HAS_PCH_CPT(dev)) {
3368 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3369 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3370 } else {
3371 temp &= ~FDI_LINK_TRAIN_NONE;
3372 temp |= FDI_LINK_TRAIN_NONE;
3373 }
3374 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3375
3376 /* wait one idle pattern time */
3377 POSTING_READ(reg);
3378 udelay(1000);
357555c0
JB
3379
3380 /* IVB wants error correction enabled */
3381 if (IS_IVYBRIDGE(dev))
3382 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3383 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3384}
3385
8db9d77b
ZW
3386/* The FDI link training functions for ILK/Ibexpeak. */
3387static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3388{
3389 struct drm_device *dev = crtc->dev;
3390 struct drm_i915_private *dev_priv = dev->dev_private;
3391 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3392 int pipe = intel_crtc->pipe;
5eddb70b 3393 u32 reg, temp, tries;
8db9d77b 3394
1c8562f6 3395 /* FDI needs bits from pipe first */
0fc932b8 3396 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3397
e1a44743
AJ
3398 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3399 for train result */
5eddb70b
CW
3400 reg = FDI_RX_IMR(pipe);
3401 temp = I915_READ(reg);
e1a44743
AJ
3402 temp &= ~FDI_RX_SYMBOL_LOCK;
3403 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3404 I915_WRITE(reg, temp);
3405 I915_READ(reg);
e1a44743
AJ
3406 udelay(150);
3407
8db9d77b 3408 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3409 reg = FDI_TX_CTL(pipe);
3410 temp = I915_READ(reg);
627eb5a3 3411 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3412 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3413 temp &= ~FDI_LINK_TRAIN_NONE;
3414 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3415 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3416
5eddb70b
CW
3417 reg = FDI_RX_CTL(pipe);
3418 temp = I915_READ(reg);
8db9d77b
ZW
3419 temp &= ~FDI_LINK_TRAIN_NONE;
3420 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3421 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3422
3423 POSTING_READ(reg);
8db9d77b
ZW
3424 udelay(150);
3425
5b2adf89 3426 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3427 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3428 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3429 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3430
5eddb70b 3431 reg = FDI_RX_IIR(pipe);
e1a44743 3432 for (tries = 0; tries < 5; tries++) {
5eddb70b 3433 temp = I915_READ(reg);
8db9d77b
ZW
3434 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3435
3436 if ((temp & FDI_RX_BIT_LOCK)) {
3437 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3438 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3439 break;
3440 }
8db9d77b 3441 }
e1a44743 3442 if (tries == 5)
5eddb70b 3443 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3444
3445 /* Train 2 */
5eddb70b
CW
3446 reg = FDI_TX_CTL(pipe);
3447 temp = I915_READ(reg);
8db9d77b
ZW
3448 temp &= ~FDI_LINK_TRAIN_NONE;
3449 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3450 I915_WRITE(reg, temp);
8db9d77b 3451
5eddb70b
CW
3452 reg = FDI_RX_CTL(pipe);
3453 temp = I915_READ(reg);
8db9d77b
ZW
3454 temp &= ~FDI_LINK_TRAIN_NONE;
3455 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3456 I915_WRITE(reg, temp);
8db9d77b 3457
5eddb70b
CW
3458 POSTING_READ(reg);
3459 udelay(150);
8db9d77b 3460
5eddb70b 3461 reg = FDI_RX_IIR(pipe);
e1a44743 3462 for (tries = 0; tries < 5; tries++) {
5eddb70b 3463 temp = I915_READ(reg);
8db9d77b
ZW
3464 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3465
3466 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3467 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3468 DRM_DEBUG_KMS("FDI train 2 done.\n");
3469 break;
3470 }
8db9d77b 3471 }
e1a44743 3472 if (tries == 5)
5eddb70b 3473 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3474
3475 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3476
8db9d77b
ZW
3477}
3478
0206e353 3479static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3480 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3481 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3482 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3483 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3484};
3485
3486/* The FDI link training functions for SNB/Cougarpoint. */
3487static void gen6_fdi_link_train(struct drm_crtc *crtc)
3488{
3489 struct drm_device *dev = crtc->dev;
3490 struct drm_i915_private *dev_priv = dev->dev_private;
3491 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3492 int pipe = intel_crtc->pipe;
fa37d39e 3493 u32 reg, temp, i, retry;
8db9d77b 3494
e1a44743
AJ
3495 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3496 for train result */
5eddb70b
CW
3497 reg = FDI_RX_IMR(pipe);
3498 temp = I915_READ(reg);
e1a44743
AJ
3499 temp &= ~FDI_RX_SYMBOL_LOCK;
3500 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3501 I915_WRITE(reg, temp);
3502
3503 POSTING_READ(reg);
e1a44743
AJ
3504 udelay(150);
3505
8db9d77b 3506 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3507 reg = FDI_TX_CTL(pipe);
3508 temp = I915_READ(reg);
627eb5a3 3509 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3510 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3511 temp &= ~FDI_LINK_TRAIN_NONE;
3512 temp |= FDI_LINK_TRAIN_PATTERN_1;
3513 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3514 /* SNB-B */
3515 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3516 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3517
d74cf324
DV
3518 I915_WRITE(FDI_RX_MISC(pipe),
3519 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3520
5eddb70b
CW
3521 reg = FDI_RX_CTL(pipe);
3522 temp = I915_READ(reg);
8db9d77b
ZW
3523 if (HAS_PCH_CPT(dev)) {
3524 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3525 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3526 } else {
3527 temp &= ~FDI_LINK_TRAIN_NONE;
3528 temp |= FDI_LINK_TRAIN_PATTERN_1;
3529 }
5eddb70b
CW
3530 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3531
3532 POSTING_READ(reg);
8db9d77b
ZW
3533 udelay(150);
3534
0206e353 3535 for (i = 0; i < 4; i++) {
5eddb70b
CW
3536 reg = FDI_TX_CTL(pipe);
3537 temp = I915_READ(reg);
8db9d77b
ZW
3538 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3539 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3540 I915_WRITE(reg, temp);
3541
3542 POSTING_READ(reg);
8db9d77b
ZW
3543 udelay(500);
3544
fa37d39e
SP
3545 for (retry = 0; retry < 5; retry++) {
3546 reg = FDI_RX_IIR(pipe);
3547 temp = I915_READ(reg);
3548 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3549 if (temp & FDI_RX_BIT_LOCK) {
3550 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3551 DRM_DEBUG_KMS("FDI train 1 done.\n");
3552 break;
3553 }
3554 udelay(50);
8db9d77b 3555 }
fa37d39e
SP
3556 if (retry < 5)
3557 break;
8db9d77b
ZW
3558 }
3559 if (i == 4)
5eddb70b 3560 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3561
3562 /* Train 2 */
5eddb70b
CW
3563 reg = FDI_TX_CTL(pipe);
3564 temp = I915_READ(reg);
8db9d77b
ZW
3565 temp &= ~FDI_LINK_TRAIN_NONE;
3566 temp |= FDI_LINK_TRAIN_PATTERN_2;
3567 if (IS_GEN6(dev)) {
3568 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3569 /* SNB-B */
3570 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3571 }
5eddb70b 3572 I915_WRITE(reg, temp);
8db9d77b 3573
5eddb70b
CW
3574 reg = FDI_RX_CTL(pipe);
3575 temp = I915_READ(reg);
8db9d77b
ZW
3576 if (HAS_PCH_CPT(dev)) {
3577 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3578 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3579 } else {
3580 temp &= ~FDI_LINK_TRAIN_NONE;
3581 temp |= FDI_LINK_TRAIN_PATTERN_2;
3582 }
5eddb70b
CW
3583 I915_WRITE(reg, temp);
3584
3585 POSTING_READ(reg);
8db9d77b
ZW
3586 udelay(150);
3587
0206e353 3588 for (i = 0; i < 4; i++) {
5eddb70b
CW
3589 reg = FDI_TX_CTL(pipe);
3590 temp = I915_READ(reg);
8db9d77b
ZW
3591 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3592 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3593 I915_WRITE(reg, temp);
3594
3595 POSTING_READ(reg);
8db9d77b
ZW
3596 udelay(500);
3597
fa37d39e
SP
3598 for (retry = 0; retry < 5; retry++) {
3599 reg = FDI_RX_IIR(pipe);
3600 temp = I915_READ(reg);
3601 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3602 if (temp & FDI_RX_SYMBOL_LOCK) {
3603 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3604 DRM_DEBUG_KMS("FDI train 2 done.\n");
3605 break;
3606 }
3607 udelay(50);
8db9d77b 3608 }
fa37d39e
SP
3609 if (retry < 5)
3610 break;
8db9d77b
ZW
3611 }
3612 if (i == 4)
5eddb70b 3613 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3614
3615 DRM_DEBUG_KMS("FDI train done.\n");
3616}
3617
357555c0
JB
3618/* Manual link training for Ivy Bridge A0 parts */
3619static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3620{
3621 struct drm_device *dev = crtc->dev;
3622 struct drm_i915_private *dev_priv = dev->dev_private;
3623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3624 int pipe = intel_crtc->pipe;
139ccd3f 3625 u32 reg, temp, i, j;
357555c0
JB
3626
3627 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3628 for train result */
3629 reg = FDI_RX_IMR(pipe);
3630 temp = I915_READ(reg);
3631 temp &= ~FDI_RX_SYMBOL_LOCK;
3632 temp &= ~FDI_RX_BIT_LOCK;
3633 I915_WRITE(reg, temp);
3634
3635 POSTING_READ(reg);
3636 udelay(150);
3637
01a415fd
DV
3638 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3639 I915_READ(FDI_RX_IIR(pipe)));
3640
139ccd3f
JB
3641 /* Try each vswing and preemphasis setting twice before moving on */
3642 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3643 /* disable first in case we need to retry */
3644 reg = FDI_TX_CTL(pipe);
3645 temp = I915_READ(reg);
3646 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3647 temp &= ~FDI_TX_ENABLE;
3648 I915_WRITE(reg, temp);
357555c0 3649
139ccd3f
JB
3650 reg = FDI_RX_CTL(pipe);
3651 temp = I915_READ(reg);
3652 temp &= ~FDI_LINK_TRAIN_AUTO;
3653 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3654 temp &= ~FDI_RX_ENABLE;
3655 I915_WRITE(reg, temp);
357555c0 3656
139ccd3f 3657 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3658 reg = FDI_TX_CTL(pipe);
3659 temp = I915_READ(reg);
139ccd3f 3660 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3661 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3662 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3663 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3664 temp |= snb_b_fdi_train_param[j/2];
3665 temp |= FDI_COMPOSITE_SYNC;
3666 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3667
139ccd3f
JB
3668 I915_WRITE(FDI_RX_MISC(pipe),
3669 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3670
139ccd3f 3671 reg = FDI_RX_CTL(pipe);
357555c0 3672 temp = I915_READ(reg);
139ccd3f
JB
3673 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3674 temp |= FDI_COMPOSITE_SYNC;
3675 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3676
139ccd3f
JB
3677 POSTING_READ(reg);
3678 udelay(1); /* should be 0.5us */
357555c0 3679
139ccd3f
JB
3680 for (i = 0; i < 4; i++) {
3681 reg = FDI_RX_IIR(pipe);
3682 temp = I915_READ(reg);
3683 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3684
139ccd3f
JB
3685 if (temp & FDI_RX_BIT_LOCK ||
3686 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3687 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3688 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3689 i);
3690 break;
3691 }
3692 udelay(1); /* should be 0.5us */
3693 }
3694 if (i == 4) {
3695 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3696 continue;
3697 }
357555c0 3698
139ccd3f 3699 /* Train 2 */
357555c0
JB
3700 reg = FDI_TX_CTL(pipe);
3701 temp = I915_READ(reg);
139ccd3f
JB
3702 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3703 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3704 I915_WRITE(reg, temp);
3705
3706 reg = FDI_RX_CTL(pipe);
3707 temp = I915_READ(reg);
3708 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3709 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3710 I915_WRITE(reg, temp);
3711
3712 POSTING_READ(reg);
139ccd3f 3713 udelay(2); /* should be 1.5us */
357555c0 3714
139ccd3f
JB
3715 for (i = 0; i < 4; i++) {
3716 reg = FDI_RX_IIR(pipe);
3717 temp = I915_READ(reg);
3718 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3719
139ccd3f
JB
3720 if (temp & FDI_RX_SYMBOL_LOCK ||
3721 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3722 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3723 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3724 i);
3725 goto train_done;
3726 }
3727 udelay(2); /* should be 1.5us */
357555c0 3728 }
139ccd3f
JB
3729 if (i == 4)
3730 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3731 }
357555c0 3732
139ccd3f 3733train_done:
357555c0
JB
3734 DRM_DEBUG_KMS("FDI train done.\n");
3735}
3736
88cefb6c 3737static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3738{
88cefb6c 3739 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3740 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3741 int pipe = intel_crtc->pipe;
5eddb70b 3742 u32 reg, temp;
79e53945 3743
c64e311e 3744
c98e9dcf 3745 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3746 reg = FDI_RX_CTL(pipe);
3747 temp = I915_READ(reg);
627eb5a3 3748 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3749 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3750 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3751 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3752
3753 POSTING_READ(reg);
c98e9dcf
JB
3754 udelay(200);
3755
3756 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3757 temp = I915_READ(reg);
3758 I915_WRITE(reg, temp | FDI_PCDCLK);
3759
3760 POSTING_READ(reg);
c98e9dcf
JB
3761 udelay(200);
3762
20749730
PZ
3763 /* Enable CPU FDI TX PLL, always on for Ironlake */
3764 reg = FDI_TX_CTL(pipe);
3765 temp = I915_READ(reg);
3766 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3767 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3768
20749730
PZ
3769 POSTING_READ(reg);
3770 udelay(100);
6be4a607 3771 }
0e23b99d
JB
3772}
3773
88cefb6c
DV
3774static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3775{
3776 struct drm_device *dev = intel_crtc->base.dev;
3777 struct drm_i915_private *dev_priv = dev->dev_private;
3778 int pipe = intel_crtc->pipe;
3779 u32 reg, temp;
3780
3781 /* Switch from PCDclk to Rawclk */
3782 reg = FDI_RX_CTL(pipe);
3783 temp = I915_READ(reg);
3784 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3785
3786 /* Disable CPU FDI TX PLL */
3787 reg = FDI_TX_CTL(pipe);
3788 temp = I915_READ(reg);
3789 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3790
3791 POSTING_READ(reg);
3792 udelay(100);
3793
3794 reg = FDI_RX_CTL(pipe);
3795 temp = I915_READ(reg);
3796 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3797
3798 /* Wait for the clocks to turn off. */
3799 POSTING_READ(reg);
3800 udelay(100);
3801}
3802
0fc932b8
JB
3803static void ironlake_fdi_disable(struct drm_crtc *crtc)
3804{
3805 struct drm_device *dev = crtc->dev;
3806 struct drm_i915_private *dev_priv = dev->dev_private;
3807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3808 int pipe = intel_crtc->pipe;
3809 u32 reg, temp;
3810
3811 /* disable CPU FDI tx and PCH FDI rx */
3812 reg = FDI_TX_CTL(pipe);
3813 temp = I915_READ(reg);
3814 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3815 POSTING_READ(reg);
3816
3817 reg = FDI_RX_CTL(pipe);
3818 temp = I915_READ(reg);
3819 temp &= ~(0x7 << 16);
dfd07d72 3820 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3821 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3822
3823 POSTING_READ(reg);
3824 udelay(100);
3825
3826 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3827 if (HAS_PCH_IBX(dev))
6f06ce18 3828 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3829
3830 /* still set train pattern 1 */
3831 reg = FDI_TX_CTL(pipe);
3832 temp = I915_READ(reg);
3833 temp &= ~FDI_LINK_TRAIN_NONE;
3834 temp |= FDI_LINK_TRAIN_PATTERN_1;
3835 I915_WRITE(reg, temp);
3836
3837 reg = FDI_RX_CTL(pipe);
3838 temp = I915_READ(reg);
3839 if (HAS_PCH_CPT(dev)) {
3840 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3841 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3842 } else {
3843 temp &= ~FDI_LINK_TRAIN_NONE;
3844 temp |= FDI_LINK_TRAIN_PATTERN_1;
3845 }
3846 /* BPC in FDI rx is consistent with that in PIPECONF */
3847 temp &= ~(0x07 << 16);
dfd07d72 3848 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3849 I915_WRITE(reg, temp);
3850
3851 POSTING_READ(reg);
3852 udelay(100);
3853}
3854
5dce5b93
CW
3855bool intel_has_pending_fb_unpin(struct drm_device *dev)
3856{
3857 struct intel_crtc *crtc;
3858
3859 /* Note that we don't need to be called with mode_config.lock here
3860 * as our list of CRTC objects is static for the lifetime of the
3861 * device and so cannot disappear as we iterate. Similarly, we can
3862 * happily treat the predicates as racy, atomic checks as userspace
3863 * cannot claim and pin a new fb without at least acquring the
3864 * struct_mutex and so serialising with us.
3865 */
d3fcc808 3866 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3867 if (atomic_read(&crtc->unpin_work_count) == 0)
3868 continue;
3869
3870 if (crtc->unpin_work)
3871 intel_wait_for_vblank(dev, crtc->pipe);
3872
3873 return true;
3874 }
3875
3876 return false;
3877}
3878
d6bbafa1
CW
3879static void page_flip_completed(struct intel_crtc *intel_crtc)
3880{
3881 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3882 struct intel_unpin_work *work = intel_crtc->unpin_work;
3883
3884 /* ensure that the unpin work is consistent wrt ->pending. */
3885 smp_rmb();
3886 intel_crtc->unpin_work = NULL;
3887
3888 if (work->event)
3889 drm_send_vblank_event(intel_crtc->base.dev,
3890 intel_crtc->pipe,
3891 work->event);
3892
3893 drm_crtc_vblank_put(&intel_crtc->base);
3894
3895 wake_up_all(&dev_priv->pending_flip_queue);
3896 queue_work(dev_priv->wq, &work->work);
3897
3898 trace_i915_flip_complete(intel_crtc->plane,
3899 work->pending_flip_obj);
3900}
3901
46a55d30 3902void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3903{
0f91128d 3904 struct drm_device *dev = crtc->dev;
5bb61643 3905 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3906
2c10d571 3907 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3908 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3909 !intel_crtc_has_pending_flip(crtc),
3910 60*HZ) == 0)) {
3911 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3912
5e2d7afc 3913 spin_lock_irq(&dev->event_lock);
9c787942
CW
3914 if (intel_crtc->unpin_work) {
3915 WARN_ONCE(1, "Removing stuck page flip\n");
3916 page_flip_completed(intel_crtc);
3917 }
5e2d7afc 3918 spin_unlock_irq(&dev->event_lock);
9c787942 3919 }
5bb61643 3920
975d568a
CW
3921 if (crtc->primary->fb) {
3922 mutex_lock(&dev->struct_mutex);
3923 intel_finish_fb(crtc->primary->fb);
3924 mutex_unlock(&dev->struct_mutex);
3925 }
e6c3a2a6
CW
3926}
3927
e615efe4
ED
3928/* Program iCLKIP clock to the desired frequency */
3929static void lpt_program_iclkip(struct drm_crtc *crtc)
3930{
3931 struct drm_device *dev = crtc->dev;
3932 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3933 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3934 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3935 u32 temp;
3936
a580516d 3937 mutex_lock(&dev_priv->sb_lock);
09153000 3938
e615efe4
ED
3939 /* It is necessary to ungate the pixclk gate prior to programming
3940 * the divisors, and gate it back when it is done.
3941 */
3942 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3943
3944 /* Disable SSCCTL */
3945 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3946 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3947 SBI_SSCCTL_DISABLE,
3948 SBI_ICLK);
e615efe4
ED
3949
3950 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3951 if (clock == 20000) {
e615efe4
ED
3952 auxdiv = 1;
3953 divsel = 0x41;
3954 phaseinc = 0x20;
3955 } else {
3956 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3957 * but the adjusted_mode->crtc_clock in in KHz. To get the
3958 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3959 * convert the virtual clock precision to KHz here for higher
3960 * precision.
3961 */
3962 u32 iclk_virtual_root_freq = 172800 * 1000;
3963 u32 iclk_pi_range = 64;
3964 u32 desired_divisor, msb_divisor_value, pi_value;
3965
12d7ceed 3966 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3967 msb_divisor_value = desired_divisor / iclk_pi_range;
3968 pi_value = desired_divisor % iclk_pi_range;
3969
3970 auxdiv = 0;
3971 divsel = msb_divisor_value - 2;
3972 phaseinc = pi_value;
3973 }
3974
3975 /* This should not happen with any sane values */
3976 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3977 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3978 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3979 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3980
3981 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3982 clock,
e615efe4
ED
3983 auxdiv,
3984 divsel,
3985 phasedir,
3986 phaseinc);
3987
3988 /* Program SSCDIVINTPHASE6 */
988d6ee8 3989 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3990 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3991 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3992 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3993 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3994 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3995 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3996 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3997
3998 /* Program SSCAUXDIV */
988d6ee8 3999 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4000 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4001 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4002 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4003
4004 /* Enable modulator and associated divider */
988d6ee8 4005 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4006 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4007 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
4008
4009 /* Wait for initialization time */
4010 udelay(24);
4011
4012 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000 4013
a580516d 4014 mutex_unlock(&dev_priv->sb_lock);
e615efe4
ED
4015}
4016
275f01b2
DV
4017static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4018 enum pipe pch_transcoder)
4019{
4020 struct drm_device *dev = crtc->base.dev;
4021 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4022 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4023
4024 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4025 I915_READ(HTOTAL(cpu_transcoder)));
4026 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4027 I915_READ(HBLANK(cpu_transcoder)));
4028 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4029 I915_READ(HSYNC(cpu_transcoder)));
4030
4031 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4032 I915_READ(VTOTAL(cpu_transcoder)));
4033 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4034 I915_READ(VBLANK(cpu_transcoder)));
4035 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4036 I915_READ(VSYNC(cpu_transcoder)));
4037 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4038 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4039}
4040
003632d9 4041static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4042{
4043 struct drm_i915_private *dev_priv = dev->dev_private;
4044 uint32_t temp;
4045
4046 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4047 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4048 return;
4049
4050 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4051 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4052
003632d9
ACO
4053 temp &= ~FDI_BC_BIFURCATION_SELECT;
4054 if (enable)
4055 temp |= FDI_BC_BIFURCATION_SELECT;
4056
4057 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4058 I915_WRITE(SOUTH_CHICKEN1, temp);
4059 POSTING_READ(SOUTH_CHICKEN1);
4060}
4061
4062static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4063{
4064 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4065
4066 switch (intel_crtc->pipe) {
4067 case PIPE_A:
4068 break;
4069 case PIPE_B:
6e3c9717 4070 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4071 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4072 else
003632d9 4073 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4074
4075 break;
4076 case PIPE_C:
003632d9 4077 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4078
4079 break;
4080 default:
4081 BUG();
4082 }
4083}
4084
f67a559d
JB
4085/*
4086 * Enable PCH resources required for PCH ports:
4087 * - PCH PLLs
4088 * - FDI training & RX/TX
4089 * - update transcoder timings
4090 * - DP transcoding bits
4091 * - transcoder
4092 */
4093static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4094{
4095 struct drm_device *dev = crtc->dev;
4096 struct drm_i915_private *dev_priv = dev->dev_private;
4097 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4098 int pipe = intel_crtc->pipe;
ee7b9f93 4099 u32 reg, temp;
2c07245f 4100
ab9412ba 4101 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4102
1fbc0d78
DV
4103 if (IS_IVYBRIDGE(dev))
4104 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4105
cd986abb
DV
4106 /* Write the TU size bits before fdi link training, so that error
4107 * detection works. */
4108 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4109 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4110
c98e9dcf 4111 /* For PCH output, training FDI link */
674cf967 4112 dev_priv->display.fdi_link_train(crtc);
2c07245f 4113
3ad8a208
DV
4114 /* We need to program the right clock selection before writing the pixel
4115 * mutliplier into the DPLL. */
303b81e0 4116 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4117 u32 sel;
4b645f14 4118
c98e9dcf 4119 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4120 temp |= TRANS_DPLL_ENABLE(pipe);
4121 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 4122 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
4123 temp |= sel;
4124 else
4125 temp &= ~sel;
c98e9dcf 4126 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4127 }
5eddb70b 4128
3ad8a208
DV
4129 /* XXX: pch pll's can be enabled any time before we enable the PCH
4130 * transcoder, and we actually should do this to not upset any PCH
4131 * transcoder that already use the clock when we share it.
4132 *
4133 * Note that enable_shared_dpll tries to do the right thing, but
4134 * get_shared_dpll unconditionally resets the pll - we need that to have
4135 * the right LVDS enable sequence. */
85b3894f 4136 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4137
d9b6cb56
JB
4138 /* set transcoder timing, panel must allow it */
4139 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4140 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4141
303b81e0 4142 intel_fdi_normal_train(crtc);
5e84e1a4 4143
c98e9dcf 4144 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4145 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 4146 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
4147 reg = TRANS_DP_CTL(pipe);
4148 temp = I915_READ(reg);
4149 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4150 TRANS_DP_SYNC_MASK |
4151 TRANS_DP_BPC_MASK);
e3ef4479 4152 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4153 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
4154
4155 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4156 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 4157 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4158 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4159
4160 switch (intel_trans_dp_port_sel(crtc)) {
4161 case PCH_DP_B:
5eddb70b 4162 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
4163 break;
4164 case PCH_DP_C:
5eddb70b 4165 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
4166 break;
4167 case PCH_DP_D:
5eddb70b 4168 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4169 break;
4170 default:
e95d41e1 4171 BUG();
32f9d658 4172 }
2c07245f 4173
5eddb70b 4174 I915_WRITE(reg, temp);
6be4a607 4175 }
b52eb4dc 4176
b8a4f404 4177 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4178}
4179
1507e5bd
PZ
4180static void lpt_pch_enable(struct drm_crtc *crtc)
4181{
4182 struct drm_device *dev = crtc->dev;
4183 struct drm_i915_private *dev_priv = dev->dev_private;
4184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4185 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4186
ab9412ba 4187 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4188
8c52b5e8 4189 lpt_program_iclkip(crtc);
1507e5bd 4190
0540e488 4191 /* Set transcoder timing. */
275f01b2 4192 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4193
937bb610 4194 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4195}
4196
190f68c5
ACO
4197struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4198 struct intel_crtc_state *crtc_state)
ee7b9f93 4199{
e2b78267 4200 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 4201 struct intel_shared_dpll *pll;
de419ab6 4202 struct intel_shared_dpll_config *shared_dpll;
e2b78267 4203 enum intel_dpll_id i;
ee7b9f93 4204
de419ab6
ML
4205 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4206
98b6bd99
DV
4207 if (HAS_PCH_IBX(dev_priv->dev)) {
4208 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 4209 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 4210 pll = &dev_priv->shared_dplls[i];
98b6bd99 4211
46edb027
DV
4212 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4213 crtc->base.base.id, pll->name);
98b6bd99 4214
de419ab6 4215 WARN_ON(shared_dpll[i].crtc_mask);
f2a69f44 4216
98b6bd99
DV
4217 goto found;
4218 }
4219
bcddf610
S
4220 if (IS_BROXTON(dev_priv->dev)) {
4221 /* PLL is attached to port in bxt */
4222 struct intel_encoder *encoder;
4223 struct intel_digital_port *intel_dig_port;
4224
4225 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4226 if (WARN_ON(!encoder))
4227 return NULL;
4228
4229 intel_dig_port = enc_to_dig_port(&encoder->base);
4230 /* 1:1 mapping between ports and PLLs */
4231 i = (enum intel_dpll_id)intel_dig_port->port;
4232 pll = &dev_priv->shared_dplls[i];
4233 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4234 crtc->base.base.id, pll->name);
de419ab6 4235 WARN_ON(shared_dpll[i].crtc_mask);
bcddf610
S
4236
4237 goto found;
4238 }
4239
e72f9fbf
DV
4240 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4241 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
4242
4243 /* Only want to check enabled timings first */
de419ab6 4244 if (shared_dpll[i].crtc_mask == 0)
ee7b9f93
JB
4245 continue;
4246
190f68c5 4247 if (memcmp(&crtc_state->dpll_hw_state,
de419ab6
ML
4248 &shared_dpll[i].hw_state,
4249 sizeof(crtc_state->dpll_hw_state)) == 0) {
8bd31e67 4250 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 4251 crtc->base.base.id, pll->name,
de419ab6 4252 shared_dpll[i].crtc_mask,
8bd31e67 4253 pll->active);
ee7b9f93
JB
4254 goto found;
4255 }
4256 }
4257
4258 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
4259 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4260 pll = &dev_priv->shared_dplls[i];
de419ab6 4261 if (shared_dpll[i].crtc_mask == 0) {
46edb027
DV
4262 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4263 crtc->base.base.id, pll->name);
ee7b9f93
JB
4264 goto found;
4265 }
4266 }
4267
4268 return NULL;
4269
4270found:
de419ab6
ML
4271 if (shared_dpll[i].crtc_mask == 0)
4272 shared_dpll[i].hw_state =
4273 crtc_state->dpll_hw_state;
f2a69f44 4274
190f68c5 4275 crtc_state->shared_dpll = i;
46edb027
DV
4276 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4277 pipe_name(crtc->pipe));
ee7b9f93 4278
de419ab6 4279 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
e04c7350 4280
ee7b9f93
JB
4281 return pll;
4282}
4283
de419ab6 4284static void intel_shared_dpll_commit(struct drm_atomic_state *state)
8bd31e67 4285{
de419ab6
ML
4286 struct drm_i915_private *dev_priv = to_i915(state->dev);
4287 struct intel_shared_dpll_config *shared_dpll;
8bd31e67
ACO
4288 struct intel_shared_dpll *pll;
4289 enum intel_dpll_id i;
4290
de419ab6
ML
4291 if (!to_intel_atomic_state(state)->dpll_set)
4292 return;
8bd31e67 4293
de419ab6 4294 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
8bd31e67
ACO
4295 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4296 pll = &dev_priv->shared_dplls[i];
de419ab6 4297 pll->config = shared_dpll[i];
8bd31e67
ACO
4298 }
4299}
4300
a1520318 4301static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4302{
4303 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4304 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4305 u32 temp;
4306
4307 temp = I915_READ(dslreg);
4308 udelay(500);
4309 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4310 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4311 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4312 }
4313}
4314
a1b2278e
CK
4315/**
4316 * skl_update_scaler_users - Stages update to crtc's scaler state
4317 * @intel_crtc: crtc
4318 * @crtc_state: crtc_state
4319 * @plane: plane (NULL indicates crtc is requesting update)
4320 * @plane_state: plane's state
4321 * @force_detach: request unconditional detachment of scaler
4322 *
4323 * This function updates scaler state for requested plane or crtc.
4324 * To request scaler usage update for a plane, caller shall pass plane pointer.
4325 * To request scaler usage update for crtc, caller shall pass plane pointer
4326 * as NULL.
4327 *
4328 * Return
4329 * 0 - scaler_usage updated successfully
4330 * error - requested scaling cannot be supported or other error condition
4331 */
4332int
4333skl_update_scaler_users(
4334 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4335 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4336 int force_detach)
4337{
4338 int need_scaling;
4339 int idx;
4340 int src_w, src_h, dst_w, dst_h;
4341 int *scaler_id;
4342 struct drm_framebuffer *fb;
4343 struct intel_crtc_scaler_state *scaler_state;
6156a456 4344 unsigned int rotation;
a1b2278e
CK
4345
4346 if (!intel_crtc || !crtc_state)
4347 return 0;
4348
4349 scaler_state = &crtc_state->scaler_state;
4350
4351 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4352 fb = intel_plane ? plane_state->base.fb : NULL;
4353
4354 if (intel_plane) {
4355 src_w = drm_rect_width(&plane_state->src) >> 16;
4356 src_h = drm_rect_height(&plane_state->src) >> 16;
4357 dst_w = drm_rect_width(&plane_state->dst);
4358 dst_h = drm_rect_height(&plane_state->dst);
4359 scaler_id = &plane_state->scaler_id;
6156a456 4360 rotation = plane_state->base.rotation;
a1b2278e
CK
4361 } else {
4362 struct drm_display_mode *adjusted_mode =
4363 &crtc_state->base.adjusted_mode;
4364 src_w = crtc_state->pipe_src_w;
4365 src_h = crtc_state->pipe_src_h;
4366 dst_w = adjusted_mode->hdisplay;
4367 dst_h = adjusted_mode->vdisplay;
4368 scaler_id = &scaler_state->scaler_id;
6156a456 4369 rotation = DRM_ROTATE_0;
a1b2278e 4370 }
6156a456
CK
4371
4372 need_scaling = intel_rotation_90_or_270(rotation) ?
4373 (src_h != dst_w || src_w != dst_h):
4374 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4375
4376 /*
4377 * if plane is being disabled or scaler is no more required or force detach
4378 * - free scaler binded to this plane/crtc
4379 * - in order to do this, update crtc->scaler_usage
4380 *
4381 * Here scaler state in crtc_state is set free so that
4382 * scaler can be assigned to other user. Actual register
4383 * update to free the scaler is done in plane/panel-fit programming.
4384 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4385 */
4386 if (force_detach || !need_scaling || (intel_plane &&
4387 (!fb || !plane_state->visible))) {
4388 if (*scaler_id >= 0) {
4389 scaler_state->scaler_users &= ~(1 << idx);
4390 scaler_state->scalers[*scaler_id].in_use = 0;
4391
4392 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4393 "crtc_state = %p scaler_users = 0x%x\n",
4394 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4395 intel_plane ? intel_plane->base.base.id :
4396 intel_crtc->base.base.id, crtc_state,
4397 scaler_state->scaler_users);
4398 *scaler_id = -1;
4399 }
4400 return 0;
4401 }
4402
4403 /* range checks */
4404 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4405 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4406
4407 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4408 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4409 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4410 "size is out of scaler range\n",
4411 intel_plane ? "PLANE" : "CRTC",
4412 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4413 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4414 return -EINVAL;
4415 }
4416
4417 /* check colorkey */
225c228a
CK
4418 if (WARN_ON(intel_plane &&
4419 intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
4420 DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
4421 intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4422 return -EINVAL;
4423 }
4424
4425 /* Check src format */
4426 if (intel_plane) {
4427 switch (fb->pixel_format) {
4428 case DRM_FORMAT_RGB565:
4429 case DRM_FORMAT_XBGR8888:
4430 case DRM_FORMAT_XRGB8888:
4431 case DRM_FORMAT_ABGR8888:
4432 case DRM_FORMAT_ARGB8888:
4433 case DRM_FORMAT_XRGB2101010:
a1b2278e 4434 case DRM_FORMAT_XBGR2101010:
a1b2278e
CK
4435 case DRM_FORMAT_YUYV:
4436 case DRM_FORMAT_YVYU:
4437 case DRM_FORMAT_UYVY:
4438 case DRM_FORMAT_VYUY:
4439 break;
4440 default:
4441 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4442 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4443 return -EINVAL;
4444 }
4445 }
4446
4447 /* mark this plane as a scaler user in crtc_state */
4448 scaler_state->scaler_users |= (1 << idx);
4449 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4450 "crtc_state = %p scaler_users = 0x%x\n",
4451 intel_plane ? "PLANE" : "CRTC",
4452 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4453 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4454 return 0;
4455}
4456
4457static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
bd2e244f
JB
4458{
4459 struct drm_device *dev = crtc->base.dev;
4460 struct drm_i915_private *dev_priv = dev->dev_private;
4461 int pipe = crtc->pipe;
a1b2278e
CK
4462 struct intel_crtc_scaler_state *scaler_state =
4463 &crtc->config->scaler_state;
4464
4465 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4466
4467 /* To update pfit, first update scaler state */
4468 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4469 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4470 skl_detach_scalers(crtc);
4471 if (!enable)
4472 return;
bd2e244f 4473
6e3c9717 4474 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4475 int id;
4476
4477 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4478 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4479 return;
4480 }
4481
4482 id = scaler_state->scaler_id;
4483 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4484 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4485 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4486 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4487
4488 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4489 }
4490}
4491
b074cec8
JB
4492static void ironlake_pfit_enable(struct intel_crtc *crtc)
4493{
4494 struct drm_device *dev = crtc->base.dev;
4495 struct drm_i915_private *dev_priv = dev->dev_private;
4496 int pipe = crtc->pipe;
4497
6e3c9717 4498 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4499 /* Force use of hard-coded filter coefficients
4500 * as some pre-programmed values are broken,
4501 * e.g. x201.
4502 */
4503 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4504 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4505 PF_PIPE_SEL_IVB(pipe));
4506 else
4507 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4508 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4509 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4510 }
4511}
4512
4a3b8769 4513static void intel_enable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4514{
4515 struct drm_device *dev = crtc->dev;
4516 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4517 struct drm_plane *plane;
bb53d4ae
VS
4518 struct intel_plane *intel_plane;
4519
af2b653b
MR
4520 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4521 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
4522 if (intel_plane->pipe == pipe)
4523 intel_plane_restore(&intel_plane->base);
af2b653b 4524 }
bb53d4ae
VS
4525}
4526
20bc8673 4527void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4528{
cea165c3
VS
4529 struct drm_device *dev = crtc->base.dev;
4530 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4531
6e3c9717 4532 if (!crtc->config->ips_enabled)
d77e4531
PZ
4533 return;
4534
cea165c3
VS
4535 /* We can only enable IPS after we enable a plane and wait for a vblank */
4536 intel_wait_for_vblank(dev, crtc->pipe);
4537
d77e4531 4538 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4539 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4540 mutex_lock(&dev_priv->rps.hw_lock);
4541 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4542 mutex_unlock(&dev_priv->rps.hw_lock);
4543 /* Quoting Art Runyan: "its not safe to expect any particular
4544 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4545 * mailbox." Moreover, the mailbox may return a bogus state,
4546 * so we need to just enable it and continue on.
2a114cc1
BW
4547 */
4548 } else {
4549 I915_WRITE(IPS_CTL, IPS_ENABLE);
4550 /* The bit only becomes 1 in the next vblank, so this wait here
4551 * is essentially intel_wait_for_vblank. If we don't have this
4552 * and don't wait for vblanks until the end of crtc_enable, then
4553 * the HW state readout code will complain that the expected
4554 * IPS_CTL value is not the one we read. */
4555 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4556 DRM_ERROR("Timed out waiting for IPS enable\n");
4557 }
d77e4531
PZ
4558}
4559
20bc8673 4560void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4561{
4562 struct drm_device *dev = crtc->base.dev;
4563 struct drm_i915_private *dev_priv = dev->dev_private;
4564
6e3c9717 4565 if (!crtc->config->ips_enabled)
d77e4531
PZ
4566 return;
4567
4568 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4569 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4570 mutex_lock(&dev_priv->rps.hw_lock);
4571 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4572 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4573 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4574 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4575 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4576 } else {
2a114cc1 4577 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4578 POSTING_READ(IPS_CTL);
4579 }
d77e4531
PZ
4580
4581 /* We need to wait for a vblank before we can disable the plane. */
4582 intel_wait_for_vblank(dev, crtc->pipe);
4583}
4584
4585/** Loads the palette/gamma unit for the CRTC with the prepared values */
4586static void intel_crtc_load_lut(struct drm_crtc *crtc)
4587{
4588 struct drm_device *dev = crtc->dev;
4589 struct drm_i915_private *dev_priv = dev->dev_private;
4590 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4591 enum pipe pipe = intel_crtc->pipe;
4592 int palreg = PALETTE(pipe);
4593 int i;
4594 bool reenable_ips = false;
4595
4596 /* The clocks have to be on to load the palette. */
53d9f4e9 4597 if (!crtc->state->active)
d77e4531
PZ
4598 return;
4599
50360403 4600 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
409ee761 4601 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4602 assert_dsi_pll_enabled(dev_priv);
4603 else
4604 assert_pll_enabled(dev_priv, pipe);
4605 }
4606
4607 /* use legacy palette for Ironlake */
7a1db49a 4608 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4609 palreg = LGC_PALETTE(pipe);
4610
4611 /* Workaround : Do not read or write the pipe palette/gamma data while
4612 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4613 */
6e3c9717 4614 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4615 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4616 GAMMA_MODE_MODE_SPLIT)) {
4617 hsw_disable_ips(intel_crtc);
4618 reenable_ips = true;
4619 }
4620
4621 for (i = 0; i < 256; i++) {
4622 I915_WRITE(palreg + 4 * i,
4623 (intel_crtc->lut_r[i] << 16) |
4624 (intel_crtc->lut_g[i] << 8) |
4625 intel_crtc->lut_b[i]);
4626 }
4627
4628 if (reenable_ips)
4629 hsw_enable_ips(intel_crtc);
4630}
4631
7cac945f 4632static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4633{
7cac945f 4634 if (intel_crtc->overlay) {
d3eedb1a
VS
4635 struct drm_device *dev = intel_crtc->base.dev;
4636 struct drm_i915_private *dev_priv = dev->dev_private;
4637
4638 mutex_lock(&dev->struct_mutex);
4639 dev_priv->mm.interruptible = false;
4640 (void) intel_overlay_switch_off(intel_crtc->overlay);
4641 dev_priv->mm.interruptible = true;
4642 mutex_unlock(&dev->struct_mutex);
4643 }
4644
4645 /* Let userspace switch the overlay on again. In most cases userspace
4646 * has to recompute where to put it anyway.
4647 */
4648}
4649
87d4300a
ML
4650/**
4651 * intel_post_enable_primary - Perform operations after enabling primary plane
4652 * @crtc: the CRTC whose primary plane was just enabled
4653 *
4654 * Performs potentially sleeping operations that must be done after the primary
4655 * plane is enabled, such as updating FBC and IPS. Note that this may be
4656 * called due to an explicit primary plane update, or due to an implicit
4657 * re-enable that is caused when a sprite plane is updated to no longer
4658 * completely hide the primary plane.
4659 */
4660static void
4661intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4662{
4663 struct drm_device *dev = crtc->dev;
87d4300a 4664 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4666 int pipe = intel_crtc->pipe;
a5c4d7bc 4667
87d4300a
ML
4668 /*
4669 * BDW signals flip done immediately if the plane
4670 * is disabled, even if the plane enable is already
4671 * armed to occur at the next vblank :(
4672 */
4673 if (IS_BROADWELL(dev))
4674 intel_wait_for_vblank(dev, pipe);
a5c4d7bc 4675
87d4300a
ML
4676 /*
4677 * FIXME IPS should be fine as long as one plane is
4678 * enabled, but in practice it seems to have problems
4679 * when going from primary only to sprite only and vice
4680 * versa.
4681 */
a5c4d7bc
VS
4682 hsw_enable_ips(intel_crtc);
4683
4684 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4685 intel_fbc_update(dev);
a5c4d7bc 4686 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4687
4688 /*
87d4300a
ML
4689 * Gen2 reports pipe underruns whenever all planes are disabled.
4690 * So don't enable underrun reporting before at least some planes
4691 * are enabled.
4692 * FIXME: Need to fix the logic to work when we turn off all planes
4693 * but leave the pipe running.
f99d7069 4694 */
87d4300a
ML
4695 if (IS_GEN2(dev))
4696 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4697
4698 /* Underruns don't raise interrupts, so check manually. */
4699 if (HAS_GMCH_DISPLAY(dev))
4700 i9xx_check_fifo_underruns(dev_priv);
a5c4d7bc
VS
4701}
4702
87d4300a
ML
4703/**
4704 * intel_pre_disable_primary - Perform operations before disabling primary plane
4705 * @crtc: the CRTC whose primary plane is to be disabled
4706 *
4707 * Performs potentially sleeping operations that must be done before the
4708 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4709 * be called due to an explicit primary plane update, or due to an implicit
4710 * disable that is caused when a sprite plane completely hides the primary
4711 * plane.
4712 */
4713static void
4714intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4715{
4716 struct drm_device *dev = crtc->dev;
4717 struct drm_i915_private *dev_priv = dev->dev_private;
4718 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4719 int pipe = intel_crtc->pipe;
a5c4d7bc 4720
87d4300a
ML
4721 /*
4722 * Gen2 reports pipe underruns whenever all planes are disabled.
4723 * So diasble underrun reporting before all the planes get disabled.
4724 * FIXME: Need to fix the logic to work when we turn off all planes
4725 * but leave the pipe running.
4726 */
4727 if (IS_GEN2(dev))
4728 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4729
87d4300a
ML
4730 /*
4731 * Vblank time updates from the shadow to live plane control register
4732 * are blocked if the memory self-refresh mode is active at that
4733 * moment. So to make sure the plane gets truly disabled, disable
4734 * first the self-refresh mode. The self-refresh enable bit in turn
4735 * will be checked/applied by the HW only at the next frame start
4736 * event which is after the vblank start event, so we need to have a
4737 * wait-for-vblank between disabling the plane and the pipe.
4738 */
4739 if (HAS_GMCH_DISPLAY(dev))
4740 intel_set_memory_cxsr(dev_priv, false);
4741
4742 mutex_lock(&dev->struct_mutex);
e35fef21 4743 if (dev_priv->fbc.crtc == intel_crtc)
7ff0ebcc 4744 intel_fbc_disable(dev);
87d4300a 4745 mutex_unlock(&dev->struct_mutex);
a5c4d7bc 4746
87d4300a
ML
4747 /*
4748 * FIXME IPS should be fine as long as one plane is
4749 * enabled, but in practice it seems to have problems
4750 * when going from primary only to sprite only and vice
4751 * versa.
4752 */
a5c4d7bc 4753 hsw_disable_ips(intel_crtc);
87d4300a
ML
4754}
4755
4756static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4757{
2d847d45
RV
4758 struct drm_device *dev = crtc->dev;
4759 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4760 int pipe = intel_crtc->pipe;
4761
87d4300a
ML
4762 intel_enable_primary_hw_plane(crtc->primary, crtc);
4763 intel_enable_sprite_planes(crtc);
c0165304
ML
4764 if (to_intel_plane_state(crtc->cursor->state)->visible)
4765 intel_crtc_update_cursor(crtc, true);
87d4300a
ML
4766
4767 intel_post_enable_primary(crtc);
2d847d45
RV
4768
4769 /*
4770 * FIXME: Once we grow proper nuclear flip support out of this we need
4771 * to compute the mask of flip planes precisely. For the time being
4772 * consider this a flip to a NULL plane.
4773 */
4774 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
87d4300a
ML
4775}
4776
4777static void intel_crtc_disable_planes(struct drm_crtc *crtc)
4778{
4779 struct drm_device *dev = crtc->dev;
4780 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4781 struct intel_plane *intel_plane;
4782 int pipe = intel_crtc->pipe;
4783
4784 intel_crtc_wait_for_pending_flips(crtc);
4785
4786 intel_pre_disable_primary(crtc);
a5c4d7bc 4787
7cac945f 4788 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8
ML
4789 for_each_intel_plane(dev, intel_plane) {
4790 if (intel_plane->pipe == pipe) {
4791 struct drm_crtc *from = intel_plane->base.crtc;
4792
4793 intel_plane->disable_plane(&intel_plane->base,
4794 from ?: crtc, true);
4795 }
4796 }
f98551ae 4797
f99d7069
DV
4798 /*
4799 * FIXME: Once we grow proper nuclear flip support out of this we need
4800 * to compute the mask of flip planes precisely. For the time being
4801 * consider this a flip to a NULL plane.
4802 */
4803 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4804}
4805
f67a559d
JB
4806static void ironlake_crtc_enable(struct drm_crtc *crtc)
4807{
4808 struct drm_device *dev = crtc->dev;
4809 struct drm_i915_private *dev_priv = dev->dev_private;
4810 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4811 struct intel_encoder *encoder;
f67a559d 4812 int pipe = intel_crtc->pipe;
f67a559d 4813
53d9f4e9 4814 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4815 return;
4816
6e3c9717 4817 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4818 intel_prepare_shared_dpll(intel_crtc);
4819
6e3c9717 4820 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4821 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4822
4823 intel_set_pipe_timings(intel_crtc);
4824
6e3c9717 4825 if (intel_crtc->config->has_pch_encoder) {
29407aab 4826 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4827 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4828 }
4829
4830 ironlake_set_pipeconf(crtc);
4831
f67a559d 4832 intel_crtc->active = true;
8664281b 4833
a72e4c9f
DV
4834 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4835 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4836
f6736a1a 4837 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4838 if (encoder->pre_enable)
4839 encoder->pre_enable(encoder);
f67a559d 4840
6e3c9717 4841 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4842 /* Note: FDI PLL enabling _must_ be done before we enable the
4843 * cpu pipes, hence this is separate from all the other fdi/pch
4844 * enabling. */
88cefb6c 4845 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4846 } else {
4847 assert_fdi_tx_disabled(dev_priv, pipe);
4848 assert_fdi_rx_disabled(dev_priv, pipe);
4849 }
f67a559d 4850
b074cec8 4851 ironlake_pfit_enable(intel_crtc);
f67a559d 4852
9c54c0dd
JB
4853 /*
4854 * On ILK+ LUT must be loaded before the pipe is running but with
4855 * clocks enabled
4856 */
4857 intel_crtc_load_lut(crtc);
4858
f37fcc2a 4859 intel_update_watermarks(crtc);
e1fdc473 4860 intel_enable_pipe(intel_crtc);
f67a559d 4861
6e3c9717 4862 if (intel_crtc->config->has_pch_encoder)
f67a559d 4863 ironlake_pch_enable(crtc);
c98e9dcf 4864
f9b61ff6
DV
4865 assert_vblank_disabled(crtc);
4866 drm_crtc_vblank_on(crtc);
4867
fa5c73b1
DV
4868 for_each_encoder_on_crtc(dev, crtc, encoder)
4869 encoder->enable(encoder);
61b77ddd
DV
4870
4871 if (HAS_PCH_CPT(dev))
a1520318 4872 cpt_verify_modeset(dev, intel_crtc->pipe);
6be4a607
JB
4873}
4874
42db64ef
PZ
4875/* IPS only exists on ULT machines and is tied to pipe A. */
4876static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4877{
f5adf94e 4878 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4879}
4880
4f771f10
PZ
4881static void haswell_crtc_enable(struct drm_crtc *crtc)
4882{
4883 struct drm_device *dev = crtc->dev;
4884 struct drm_i915_private *dev_priv = dev->dev_private;
4885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4886 struct intel_encoder *encoder;
99d736a2
ML
4887 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4888 struct intel_crtc_state *pipe_config =
4889 to_intel_crtc_state(crtc->state);
4f771f10 4890
53d9f4e9 4891 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4892 return;
4893
df8ad70c
DV
4894 if (intel_crtc_to_shared_dpll(intel_crtc))
4895 intel_enable_shared_dpll(intel_crtc);
4896
6e3c9717 4897 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4898 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
4899
4900 intel_set_pipe_timings(intel_crtc);
4901
6e3c9717
ACO
4902 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4903 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4904 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4905 }
4906
6e3c9717 4907 if (intel_crtc->config->has_pch_encoder) {
229fca97 4908 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4909 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4910 }
4911
4912 haswell_set_pipeconf(crtc);
4913
4914 intel_set_pipe_csc(crtc);
4915
4f771f10 4916 intel_crtc->active = true;
8664281b 4917
a72e4c9f 4918 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
4919 for_each_encoder_on_crtc(dev, crtc, encoder)
4920 if (encoder->pre_enable)
4921 encoder->pre_enable(encoder);
4922
6e3c9717 4923 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
4924 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4925 true);
4fe9467d
ID
4926 dev_priv->display.fdi_link_train(crtc);
4927 }
4928
1f544388 4929 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4930
ff6d9f55 4931 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 4932 skylake_pfit_update(intel_crtc, 1);
ff6d9f55 4933 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 4934 ironlake_pfit_enable(intel_crtc);
ff6d9f55
JB
4935 else
4936 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10
PZ
4937
4938 /*
4939 * On ILK+ LUT must be loaded before the pipe is running but with
4940 * clocks enabled
4941 */
4942 intel_crtc_load_lut(crtc);
4943
1f544388 4944 intel_ddi_set_pipe_settings(crtc);
8228c251 4945 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4946
f37fcc2a 4947 intel_update_watermarks(crtc);
e1fdc473 4948 intel_enable_pipe(intel_crtc);
42db64ef 4949
6e3c9717 4950 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4951 lpt_pch_enable(crtc);
4f771f10 4952
6e3c9717 4953 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4954 intel_ddi_set_vc_payload_alloc(crtc, true);
4955
f9b61ff6
DV
4956 assert_vblank_disabled(crtc);
4957 drm_crtc_vblank_on(crtc);
4958
8807e55b 4959 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4960 encoder->enable(encoder);
8807e55b
JN
4961 intel_opregion_notify_encoder(encoder, true);
4962 }
4f771f10 4963
e4916946
PZ
4964 /* If we change the relative order between pipe/planes enabling, we need
4965 * to change the workaround. */
99d736a2
ML
4966 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4967 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4968 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4969 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4970 }
4f771f10
PZ
4971}
4972
3f8dce3a
DV
4973static void ironlake_pfit_disable(struct intel_crtc *crtc)
4974{
4975 struct drm_device *dev = crtc->base.dev;
4976 struct drm_i915_private *dev_priv = dev->dev_private;
4977 int pipe = crtc->pipe;
4978
4979 /* To avoid upsetting the power well on haswell only disable the pfit if
4980 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 4981 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4982 I915_WRITE(PF_CTL(pipe), 0);
4983 I915_WRITE(PF_WIN_POS(pipe), 0);
4984 I915_WRITE(PF_WIN_SZ(pipe), 0);
4985 }
4986}
4987
6be4a607
JB
4988static void ironlake_crtc_disable(struct drm_crtc *crtc)
4989{
4990 struct drm_device *dev = crtc->dev;
4991 struct drm_i915_private *dev_priv = dev->dev_private;
4992 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4993 struct intel_encoder *encoder;
6be4a607 4994 int pipe = intel_crtc->pipe;
5eddb70b 4995 u32 reg, temp;
b52eb4dc 4996
53d9f4e9 4997 if (WARN_ON(!intel_crtc->active))
f7abfe8b
CW
4998 return;
4999
ea9d758d
DV
5000 for_each_encoder_on_crtc(dev, crtc, encoder)
5001 encoder->disable(encoder);
5002
f9b61ff6
DV
5003 drm_crtc_vblank_off(crtc);
5004 assert_vblank_disabled(crtc);
5005
6e3c9717 5006 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 5007 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 5008
575f7ab7 5009 intel_disable_pipe(intel_crtc);
32f9d658 5010
3f8dce3a 5011 ironlake_pfit_disable(intel_crtc);
2c07245f 5012
5a74f70a
VS
5013 if (intel_crtc->config->has_pch_encoder)
5014 ironlake_fdi_disable(crtc);
5015
bf49ec8c
DV
5016 for_each_encoder_on_crtc(dev, crtc, encoder)
5017 if (encoder->post_disable)
5018 encoder->post_disable(encoder);
2c07245f 5019
6e3c9717 5020 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5021 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5022
d925c59a
DV
5023 if (HAS_PCH_CPT(dev)) {
5024 /* disable TRANS_DP_CTL */
5025 reg = TRANS_DP_CTL(pipe);
5026 temp = I915_READ(reg);
5027 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5028 TRANS_DP_PORT_SEL_MASK);
5029 temp |= TRANS_DP_PORT_SEL_NONE;
5030 I915_WRITE(reg, temp);
5031
5032 /* disable DPLL_SEL */
5033 temp = I915_READ(PCH_DPLL_SEL);
11887397 5034 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5035 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5036 }
e3421a18 5037
d925c59a 5038 /* disable PCH DPLL */
e72f9fbf 5039 intel_disable_shared_dpll(intel_crtc);
8db9d77b 5040
d925c59a
DV
5041 ironlake_fdi_pll_disable(intel_crtc);
5042 }
6b383a7f 5043
f7abfe8b 5044 intel_crtc->active = false;
46ba614c 5045 intel_update_watermarks(crtc);
d1ebd816
BW
5046
5047 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5048 intel_fbc_update(dev);
d1ebd816 5049 mutex_unlock(&dev->struct_mutex);
6be4a607 5050}
1b3c7a47 5051
4f771f10 5052static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5053{
4f771f10
PZ
5054 struct drm_device *dev = crtc->dev;
5055 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5056 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5057 struct intel_encoder *encoder;
6e3c9717 5058 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5059
53d9f4e9 5060 if (WARN_ON(!intel_crtc->active))
4f771f10
PZ
5061 return;
5062
8807e55b
JN
5063 for_each_encoder_on_crtc(dev, crtc, encoder) {
5064 intel_opregion_notify_encoder(encoder, false);
4f771f10 5065 encoder->disable(encoder);
8807e55b 5066 }
4f771f10 5067
f9b61ff6
DV
5068 drm_crtc_vblank_off(crtc);
5069 assert_vblank_disabled(crtc);
5070
6e3c9717 5071 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
5072 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5073 false);
575f7ab7 5074 intel_disable_pipe(intel_crtc);
4f771f10 5075
6e3c9717 5076 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5077 intel_ddi_set_vc_payload_alloc(crtc, false);
5078
ad80a810 5079 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5080
ff6d9f55 5081 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5082 skylake_pfit_update(intel_crtc, 0);
ff6d9f55 5083 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5084 ironlake_pfit_disable(intel_crtc);
ff6d9f55
JB
5085 else
5086 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10 5087
1f544388 5088 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5089
6e3c9717 5090 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 5091 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 5092 intel_ddi_fdi_disable(crtc);
83616634 5093 }
4f771f10 5094
97b040aa
ID
5095 for_each_encoder_on_crtc(dev, crtc, encoder)
5096 if (encoder->post_disable)
5097 encoder->post_disable(encoder);
5098
4f771f10 5099 intel_crtc->active = false;
46ba614c 5100 intel_update_watermarks(crtc);
4f771f10
PZ
5101
5102 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5103 intel_fbc_update(dev);
4f771f10 5104 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
5105
5106 if (intel_crtc_to_shared_dpll(intel_crtc))
5107 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
5108}
5109
2dd24552
JB
5110static void i9xx_pfit_enable(struct intel_crtc *crtc)
5111{
5112 struct drm_device *dev = crtc->base.dev;
5113 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5114 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5115
681a8504 5116 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5117 return;
5118
2dd24552 5119 /*
c0b03411
DV
5120 * The panel fitter should only be adjusted whilst the pipe is disabled,
5121 * according to register description and PRM.
2dd24552 5122 */
c0b03411
DV
5123 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5124 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5125
b074cec8
JB
5126 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5127 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5128
5129 /* Border color in case we don't scale up to the full screen. Black by
5130 * default, change to something else for debugging. */
5131 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5132}
5133
d05410f9
DA
5134static enum intel_display_power_domain port_to_power_domain(enum port port)
5135{
5136 switch (port) {
5137 case PORT_A:
5138 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5139 case PORT_B:
5140 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5141 case PORT_C:
5142 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5143 case PORT_D:
5144 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5145 default:
5146 WARN_ON_ONCE(1);
5147 return POWER_DOMAIN_PORT_OTHER;
5148 }
5149}
5150
77d22dca
ID
5151#define for_each_power_domain(domain, mask) \
5152 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5153 if ((1 << (domain)) & (mask))
5154
319be8ae
ID
5155enum intel_display_power_domain
5156intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5157{
5158 struct drm_device *dev = intel_encoder->base.dev;
5159 struct intel_digital_port *intel_dig_port;
5160
5161 switch (intel_encoder->type) {
5162 case INTEL_OUTPUT_UNKNOWN:
5163 /* Only DDI platforms should ever use this output type */
5164 WARN_ON_ONCE(!HAS_DDI(dev));
5165 case INTEL_OUTPUT_DISPLAYPORT:
5166 case INTEL_OUTPUT_HDMI:
5167 case INTEL_OUTPUT_EDP:
5168 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5169 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5170 case INTEL_OUTPUT_DP_MST:
5171 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5172 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5173 case INTEL_OUTPUT_ANALOG:
5174 return POWER_DOMAIN_PORT_CRT;
5175 case INTEL_OUTPUT_DSI:
5176 return POWER_DOMAIN_PORT_DSI;
5177 default:
5178 return POWER_DOMAIN_PORT_OTHER;
5179 }
5180}
5181
5182static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5183{
319be8ae
ID
5184 struct drm_device *dev = crtc->dev;
5185 struct intel_encoder *intel_encoder;
5186 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5187 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
5188 unsigned long mask;
5189 enum transcoder transcoder;
5190
5191 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5192
5193 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5194 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
5195 if (intel_crtc->config->pch_pfit.enabled ||
5196 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
5197 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5198
319be8ae
ID
5199 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5200 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5201
77d22dca
ID
5202 return mask;
5203}
5204
679dacd4 5205static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
77d22dca 5206{
679dacd4 5207 struct drm_device *dev = state->dev;
77d22dca
ID
5208 struct drm_i915_private *dev_priv = dev->dev_private;
5209 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5210 struct intel_crtc *crtc;
5211
5212 /*
5213 * First get all needed power domains, then put all unneeded, to avoid
5214 * any unnecessary toggling of the power wells.
5215 */
d3fcc808 5216 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5217 enum intel_display_power_domain domain;
5218
83d65738 5219 if (!crtc->base.state->enable)
77d22dca
ID
5220 continue;
5221
319be8ae 5222 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
5223
5224 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5225 intel_display_power_get(dev_priv, domain);
5226 }
5227
50f6e502 5228 if (dev_priv->display.modeset_global_resources)
679dacd4 5229 dev_priv->display.modeset_global_resources(state);
50f6e502 5230
d3fcc808 5231 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5232 enum intel_display_power_domain domain;
5233
5234 for_each_power_domain(domain, crtc->enabled_power_domains)
5235 intel_display_power_put(dev_priv, domain);
5236
5237 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5238 }
5239
5240 intel_display_set_init_power(dev_priv, false);
5241}
5242
560a7ae4
DL
5243static void intel_update_max_cdclk(struct drm_device *dev)
5244{
5245 struct drm_i915_private *dev_priv = dev->dev_private;
5246
5247 if (IS_SKYLAKE(dev)) {
5248 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5249
5250 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5251 dev_priv->max_cdclk_freq = 675000;
5252 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5253 dev_priv->max_cdclk_freq = 540000;
5254 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5255 dev_priv->max_cdclk_freq = 450000;
5256 else
5257 dev_priv->max_cdclk_freq = 337500;
5258 } else if (IS_BROADWELL(dev)) {
5259 /*
5260 * FIXME with extra cooling we can allow
5261 * 540 MHz for ULX and 675 Mhz for ULT.
5262 * How can we know if extra cooling is
5263 * available? PCI ID, VTB, something else?
5264 */
5265 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5266 dev_priv->max_cdclk_freq = 450000;
5267 else if (IS_BDW_ULX(dev))
5268 dev_priv->max_cdclk_freq = 450000;
5269 else if (IS_BDW_ULT(dev))
5270 dev_priv->max_cdclk_freq = 540000;
5271 else
5272 dev_priv->max_cdclk_freq = 675000;
5273 } else if (IS_VALLEYVIEW(dev)) {
5274 dev_priv->max_cdclk_freq = 400000;
5275 } else {
5276 /* otherwise assume cdclk is fixed */
5277 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5278 }
5279
5280 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5281 dev_priv->max_cdclk_freq);
5282}
5283
5284static void intel_update_cdclk(struct drm_device *dev)
5285{
5286 struct drm_i915_private *dev_priv = dev->dev_private;
5287
5288 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5289 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5290 dev_priv->cdclk_freq);
5291
5292 /*
5293 * Program the gmbus_freq based on the cdclk frequency.
5294 * BSpec erroneously claims we should aim for 4MHz, but
5295 * in fact 1MHz is the correct frequency.
5296 */
5297 if (IS_VALLEYVIEW(dev)) {
5298 /*
5299 * Program the gmbus_freq based on the cdclk frequency.
5300 * BSpec erroneously claims we should aim for 4MHz, but
5301 * in fact 1MHz is the correct frequency.
5302 */
5303 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5304 }
5305
5306 if (dev_priv->max_cdclk_freq == 0)
5307 intel_update_max_cdclk(dev);
5308}
5309
70d0c574 5310static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5311{
5312 struct drm_i915_private *dev_priv = dev->dev_private;
5313 uint32_t divider;
5314 uint32_t ratio;
5315 uint32_t current_freq;
5316 int ret;
5317
5318 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5319 switch (frequency) {
5320 case 144000:
5321 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5322 ratio = BXT_DE_PLL_RATIO(60);
5323 break;
5324 case 288000:
5325 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5326 ratio = BXT_DE_PLL_RATIO(60);
5327 break;
5328 case 384000:
5329 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5330 ratio = BXT_DE_PLL_RATIO(60);
5331 break;
5332 case 576000:
5333 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5334 ratio = BXT_DE_PLL_RATIO(60);
5335 break;
5336 case 624000:
5337 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5338 ratio = BXT_DE_PLL_RATIO(65);
5339 break;
5340 case 19200:
5341 /*
5342 * Bypass frequency with DE PLL disabled. Init ratio, divider
5343 * to suppress GCC warning.
5344 */
5345 ratio = 0;
5346 divider = 0;
5347 break;
5348 default:
5349 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5350
5351 return;
5352 }
5353
5354 mutex_lock(&dev_priv->rps.hw_lock);
5355 /* Inform power controller of upcoming frequency change */
5356 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5357 0x80000000);
5358 mutex_unlock(&dev_priv->rps.hw_lock);
5359
5360 if (ret) {
5361 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5362 ret, frequency);
5363 return;
5364 }
5365
5366 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5367 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5368 current_freq = current_freq * 500 + 1000;
5369
5370 /*
5371 * DE PLL has to be disabled when
5372 * - setting to 19.2MHz (bypass, PLL isn't used)
5373 * - before setting to 624MHz (PLL needs toggling)
5374 * - before setting to any frequency from 624MHz (PLL needs toggling)
5375 */
5376 if (frequency == 19200 || frequency == 624000 ||
5377 current_freq == 624000) {
5378 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5379 /* Timeout 200us */
5380 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5381 1))
5382 DRM_ERROR("timout waiting for DE PLL unlock\n");
5383 }
5384
5385 if (frequency != 19200) {
5386 uint32_t val;
5387
5388 val = I915_READ(BXT_DE_PLL_CTL);
5389 val &= ~BXT_DE_PLL_RATIO_MASK;
5390 val |= ratio;
5391 I915_WRITE(BXT_DE_PLL_CTL, val);
5392
5393 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5394 /* Timeout 200us */
5395 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5396 DRM_ERROR("timeout waiting for DE PLL lock\n");
5397
5398 val = I915_READ(CDCLK_CTL);
5399 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5400 val |= divider;
5401 /*
5402 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5403 * enable otherwise.
5404 */
5405 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5406 if (frequency >= 500000)
5407 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5408
5409 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5410 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5411 val |= (frequency - 1000) / 500;
5412 I915_WRITE(CDCLK_CTL, val);
5413 }
5414
5415 mutex_lock(&dev_priv->rps.hw_lock);
5416 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5417 DIV_ROUND_UP(frequency, 25000));
5418 mutex_unlock(&dev_priv->rps.hw_lock);
5419
5420 if (ret) {
5421 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5422 ret, frequency);
5423 return;
5424 }
5425
a47871bd 5426 intel_update_cdclk(dev);
f8437dd1
VK
5427}
5428
5429void broxton_init_cdclk(struct drm_device *dev)
5430{
5431 struct drm_i915_private *dev_priv = dev->dev_private;
5432 uint32_t val;
5433
5434 /*
5435 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5436 * or else the reset will hang because there is no PCH to respond.
5437 * Move the handshake programming to initialization sequence.
5438 * Previously was left up to BIOS.
5439 */
5440 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5441 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5442 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5443
5444 /* Enable PG1 for cdclk */
5445 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5446
5447 /* check if cd clock is enabled */
5448 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5449 DRM_DEBUG_KMS("Display already initialized\n");
5450 return;
5451 }
5452
5453 /*
5454 * FIXME:
5455 * - The initial CDCLK needs to be read from VBT.
5456 * Need to make this change after VBT has changes for BXT.
5457 * - check if setting the max (or any) cdclk freq is really necessary
5458 * here, it belongs to modeset time
5459 */
5460 broxton_set_cdclk(dev, 624000);
5461
5462 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5463 POSTING_READ(DBUF_CTL);
5464
f8437dd1
VK
5465 udelay(10);
5466
5467 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5468 DRM_ERROR("DBuf power enable timeout!\n");
5469}
5470
5471void broxton_uninit_cdclk(struct drm_device *dev)
5472{
5473 struct drm_i915_private *dev_priv = dev->dev_private;
5474
5475 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5476 POSTING_READ(DBUF_CTL);
5477
f8437dd1
VK
5478 udelay(10);
5479
5480 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5481 DRM_ERROR("DBuf power disable timeout!\n");
5482
5483 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5484 broxton_set_cdclk(dev, 19200);
5485
5486 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5487}
5488
5d96d8af
DL
5489static const struct skl_cdclk_entry {
5490 unsigned int freq;
5491 unsigned int vco;
5492} skl_cdclk_frequencies[] = {
5493 { .freq = 308570, .vco = 8640 },
5494 { .freq = 337500, .vco = 8100 },
5495 { .freq = 432000, .vco = 8640 },
5496 { .freq = 450000, .vco = 8100 },
5497 { .freq = 540000, .vco = 8100 },
5498 { .freq = 617140, .vco = 8640 },
5499 { .freq = 675000, .vco = 8100 },
5500};
5501
5502static unsigned int skl_cdclk_decimal(unsigned int freq)
5503{
5504 return (freq - 1000) / 500;
5505}
5506
5507static unsigned int skl_cdclk_get_vco(unsigned int freq)
5508{
5509 unsigned int i;
5510
5511 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5512 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5513
5514 if (e->freq == freq)
5515 return e->vco;
5516 }
5517
5518 return 8100;
5519}
5520
5521static void
5522skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5523{
5524 unsigned int min_freq;
5525 u32 val;
5526
5527 /* select the minimum CDCLK before enabling DPLL 0 */
5528 val = I915_READ(CDCLK_CTL);
5529 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5530 val |= CDCLK_FREQ_337_308;
5531
5532 if (required_vco == 8640)
5533 min_freq = 308570;
5534 else
5535 min_freq = 337500;
5536
5537 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5538
5539 I915_WRITE(CDCLK_CTL, val);
5540 POSTING_READ(CDCLK_CTL);
5541
5542 /*
5543 * We always enable DPLL0 with the lowest link rate possible, but still
5544 * taking into account the VCO required to operate the eDP panel at the
5545 * desired frequency. The usual DP link rates operate with a VCO of
5546 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5547 * The modeset code is responsible for the selection of the exact link
5548 * rate later on, with the constraint of choosing a frequency that
5549 * works with required_vco.
5550 */
5551 val = I915_READ(DPLL_CTRL1);
5552
5553 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5554 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5555 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5556 if (required_vco == 8640)
5557 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5558 SKL_DPLL0);
5559 else
5560 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5561 SKL_DPLL0);
5562
5563 I915_WRITE(DPLL_CTRL1, val);
5564 POSTING_READ(DPLL_CTRL1);
5565
5566 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5567
5568 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5569 DRM_ERROR("DPLL0 not locked\n");
5570}
5571
5572static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5573{
5574 int ret;
5575 u32 val;
5576
5577 /* inform PCU we want to change CDCLK */
5578 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5579 mutex_lock(&dev_priv->rps.hw_lock);
5580 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5581 mutex_unlock(&dev_priv->rps.hw_lock);
5582
5583 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5584}
5585
5586static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5587{
5588 unsigned int i;
5589
5590 for (i = 0; i < 15; i++) {
5591 if (skl_cdclk_pcu_ready(dev_priv))
5592 return true;
5593 udelay(10);
5594 }
5595
5596 return false;
5597}
5598
5599static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5600{
560a7ae4 5601 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5602 u32 freq_select, pcu_ack;
5603
5604 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5605
5606 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5607 DRM_ERROR("failed to inform PCU about cdclk change\n");
5608 return;
5609 }
5610
5611 /* set CDCLK_CTL */
5612 switch(freq) {
5613 case 450000:
5614 case 432000:
5615 freq_select = CDCLK_FREQ_450_432;
5616 pcu_ack = 1;
5617 break;
5618 case 540000:
5619 freq_select = CDCLK_FREQ_540;
5620 pcu_ack = 2;
5621 break;
5622 case 308570:
5623 case 337500:
5624 default:
5625 freq_select = CDCLK_FREQ_337_308;
5626 pcu_ack = 0;
5627 break;
5628 case 617140:
5629 case 675000:
5630 freq_select = CDCLK_FREQ_675_617;
5631 pcu_ack = 3;
5632 break;
5633 }
5634
5635 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5636 POSTING_READ(CDCLK_CTL);
5637
5638 /* inform PCU of the change */
5639 mutex_lock(&dev_priv->rps.hw_lock);
5640 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5641 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5642
5643 intel_update_cdclk(dev);
5d96d8af
DL
5644}
5645
5646void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5647{
5648 /* disable DBUF power */
5649 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5650 POSTING_READ(DBUF_CTL);
5651
5652 udelay(10);
5653
5654 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5655 DRM_ERROR("DBuf power disable timeout\n");
5656
5657 /* disable DPLL0 */
5658 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5659 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5660 DRM_ERROR("Couldn't disable DPLL0\n");
5661
5662 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5663}
5664
5665void skl_init_cdclk(struct drm_i915_private *dev_priv)
5666{
5667 u32 val;
5668 unsigned int required_vco;
5669
5670 /* enable PCH reset handshake */
5671 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5672 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5673
5674 /* enable PG1 and Misc I/O */
5675 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5676
5677 /* DPLL0 already enabed !? */
5678 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5679 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5680 return;
5681 }
5682
5683 /* enable DPLL0 */
5684 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5685 skl_dpll0_enable(dev_priv, required_vco);
5686
5687 /* set CDCLK to the frequency the BIOS chose */
5688 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5689
5690 /* enable DBUF power */
5691 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5692 POSTING_READ(DBUF_CTL);
5693
5694 udelay(10);
5695
5696 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5697 DRM_ERROR("DBuf power enable timeout\n");
5698}
5699
dfcab17e 5700/* returns HPLL frequency in kHz */
f8bf63fd 5701static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 5702{
586f49dc 5703 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 5704
586f49dc 5705 /* Obtain SKU information */
a580516d 5706 mutex_lock(&dev_priv->sb_lock);
586f49dc
JB
5707 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5708 CCK_FUSE_HPLL_FREQ_MASK;
a580516d 5709 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5710
dfcab17e 5711 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
5712}
5713
5714/* Adjust CDclk dividers to allow high res or save power if possible */
5715static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5716{
5717 struct drm_i915_private *dev_priv = dev->dev_private;
5718 u32 val, cmd;
5719
164dfd28
VK
5720 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5721 != dev_priv->cdclk_freq);
d60c4473 5722
dfcab17e 5723 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5724 cmd = 2;
dfcab17e 5725 else if (cdclk == 266667)
30a970c6
JB
5726 cmd = 1;
5727 else
5728 cmd = 0;
5729
5730 mutex_lock(&dev_priv->rps.hw_lock);
5731 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5732 val &= ~DSPFREQGUAR_MASK;
5733 val |= (cmd << DSPFREQGUAR_SHIFT);
5734 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5735 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5736 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5737 50)) {
5738 DRM_ERROR("timed out waiting for CDclk change\n");
5739 }
5740 mutex_unlock(&dev_priv->rps.hw_lock);
5741
54433e91
VS
5742 mutex_lock(&dev_priv->sb_lock);
5743
dfcab17e 5744 if (cdclk == 400000) {
6bcda4f0 5745 u32 divider;
30a970c6 5746
6bcda4f0 5747 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5748
30a970c6
JB
5749 /* adjust cdclk divider */
5750 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 5751 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
5752 val |= divider;
5753 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5754
5755 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5756 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5757 50))
5758 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5759 }
5760
30a970c6
JB
5761 /* adjust self-refresh exit latency value */
5762 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5763 val &= ~0x7f;
5764
5765 /*
5766 * For high bandwidth configs, we set a higher latency in the bunit
5767 * so that the core display fetch happens in time to avoid underruns.
5768 */
dfcab17e 5769 if (cdclk == 400000)
30a970c6
JB
5770 val |= 4500 / 250; /* 4.5 usec */
5771 else
5772 val |= 3000 / 250; /* 3.0 usec */
5773 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5774
a580516d 5775 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5776
b6283055 5777 intel_update_cdclk(dev);
30a970c6
JB
5778}
5779
383c5a6a
VS
5780static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5781{
5782 struct drm_i915_private *dev_priv = dev->dev_private;
5783 u32 val, cmd;
5784
164dfd28
VK
5785 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5786 != dev_priv->cdclk_freq);
383c5a6a
VS
5787
5788 switch (cdclk) {
383c5a6a
VS
5789 case 333333:
5790 case 320000:
383c5a6a 5791 case 266667:
383c5a6a 5792 case 200000:
383c5a6a
VS
5793 break;
5794 default:
5f77eeb0 5795 MISSING_CASE(cdclk);
383c5a6a
VS
5796 return;
5797 }
5798
9d0d3fda
VS
5799 /*
5800 * Specs are full of misinformation, but testing on actual
5801 * hardware has shown that we just need to write the desired
5802 * CCK divider into the Punit register.
5803 */
5804 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5805
383c5a6a
VS
5806 mutex_lock(&dev_priv->rps.hw_lock);
5807 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5808 val &= ~DSPFREQGUAR_MASK_CHV;
5809 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5810 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5811 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5812 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5813 50)) {
5814 DRM_ERROR("timed out waiting for CDclk change\n");
5815 }
5816 mutex_unlock(&dev_priv->rps.hw_lock);
5817
b6283055 5818 intel_update_cdclk(dev);
383c5a6a
VS
5819}
5820
30a970c6
JB
5821static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5822 int max_pixclk)
5823{
6bcda4f0 5824 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5825 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5826
30a970c6
JB
5827 /*
5828 * Really only a few cases to deal with, as only 4 CDclks are supported:
5829 * 200MHz
5830 * 267MHz
29dc7ef3 5831 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5832 * 400MHz (VLV only)
5833 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5834 * of the lower bin and adjust if needed.
e37c67a1
VS
5835 *
5836 * We seem to get an unstable or solid color picture at 200MHz.
5837 * Not sure what's wrong. For now use 200MHz only when all pipes
5838 * are off.
30a970c6 5839 */
6cca3195
VS
5840 if (!IS_CHERRYVIEW(dev_priv) &&
5841 max_pixclk > freq_320*limit/100)
dfcab17e 5842 return 400000;
6cca3195 5843 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5844 return freq_320;
e37c67a1 5845 else if (max_pixclk > 0)
dfcab17e 5846 return 266667;
e37c67a1
VS
5847 else
5848 return 200000;
30a970c6
JB
5849}
5850
f8437dd1
VK
5851static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5852 int max_pixclk)
5853{
5854 /*
5855 * FIXME:
5856 * - remove the guardband, it's not needed on BXT
5857 * - set 19.2MHz bypass frequency if there are no active pipes
5858 */
5859 if (max_pixclk > 576000*9/10)
5860 return 624000;
5861 else if (max_pixclk > 384000*9/10)
5862 return 576000;
5863 else if (max_pixclk > 288000*9/10)
5864 return 384000;
5865 else if (max_pixclk > 144000*9/10)
5866 return 288000;
5867 else
5868 return 144000;
5869}
5870
a821fc46
ACO
5871/* Compute the max pixel clock for new configuration. Uses atomic state if
5872 * that's non-NULL, look at current state otherwise. */
5873static int intel_mode_max_pixclk(struct drm_device *dev,
5874 struct drm_atomic_state *state)
30a970c6 5875{
30a970c6 5876 struct intel_crtc *intel_crtc;
304603f4 5877 struct intel_crtc_state *crtc_state;
30a970c6
JB
5878 int max_pixclk = 0;
5879
d3fcc808 5880 for_each_intel_crtc(dev, intel_crtc) {
a821fc46
ACO
5881 if (state)
5882 crtc_state =
5883 intel_atomic_get_crtc_state(state, intel_crtc);
5884 else
5885 crtc_state = intel_crtc->config;
304603f4
ACO
5886 if (IS_ERR(crtc_state))
5887 return PTR_ERR(crtc_state);
5888
5889 if (!crtc_state->base.enable)
5890 continue;
5891
5892 max_pixclk = max(max_pixclk,
5893 crtc_state->base.adjusted_mode.crtc_clock);
30a970c6
JB
5894 }
5895
5896 return max_pixclk;
5897}
5898
0a9ab303 5899static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
30a970c6 5900{
304603f4 5901 struct drm_i915_private *dev_priv = to_i915(state->dev);
0a9ab303
ACO
5902 struct drm_crtc *crtc;
5903 struct drm_crtc_state *crtc_state;
a821fc46 5904 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
85a96e7a 5905 int cdclk, ret = 0;
30a970c6 5906
304603f4
ACO
5907 if (max_pixclk < 0)
5908 return max_pixclk;
30a970c6 5909
f8437dd1
VK
5910 if (IS_VALLEYVIEW(dev_priv))
5911 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5912 else
5913 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5914
5915 if (cdclk == dev_priv->cdclk_freq)
304603f4 5916 return 0;
30a970c6 5917
0a9ab303
ACO
5918 /* add all active pipes to the state */
5919 for_each_crtc(state->dev, crtc) {
0a9ab303
ACO
5920 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5921 if (IS_ERR(crtc_state))
5922 return PTR_ERR(crtc_state);
0a9ab303 5923
85a96e7a
ML
5924 if (!crtc_state->active || needs_modeset(crtc_state))
5925 continue;
304603f4 5926
85a96e7a
ML
5927 crtc_state->mode_changed = true;
5928
5929 ret = drm_atomic_add_affected_connectors(state, crtc);
5930 if (ret)
5931 break;
5932
5933 ret = drm_atomic_add_affected_planes(state, crtc);
5934 if (ret)
5935 break;
5936 }
5937
5938 return ret;
30a970c6
JB
5939}
5940
1e69cd74
VS
5941static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5942{
5943 unsigned int credits, default_credits;
5944
5945 if (IS_CHERRYVIEW(dev_priv))
5946 default_credits = PFI_CREDIT(12);
5947 else
5948 default_credits = PFI_CREDIT(8);
5949
164dfd28 5950 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
1e69cd74
VS
5951 /* CHV suggested value is 31 or 63 */
5952 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 5953 credits = PFI_CREDIT_63;
1e69cd74
VS
5954 else
5955 credits = PFI_CREDIT(15);
5956 } else {
5957 credits = default_credits;
5958 }
5959
5960 /*
5961 * WA - write default credits before re-programming
5962 * FIXME: should we also set the resend bit here?
5963 */
5964 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5965 default_credits);
5966
5967 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5968 credits | PFI_CREDIT_RESEND);
5969
5970 /*
5971 * FIXME is this guaranteed to clear
5972 * immediately or should we poll for it?
5973 */
5974 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5975}
5976
a821fc46 5977static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
30a970c6 5978{
a821fc46 5979 struct drm_device *dev = old_state->dev;
30a970c6 5980 struct drm_i915_private *dev_priv = dev->dev_private;
a821fc46 5981 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
304603f4
ACO
5982 int req_cdclk;
5983
a821fc46
ACO
5984 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
5985 * never fail. */
304603f4
ACO
5986 if (WARN_ON(max_pixclk < 0))
5987 return;
30a970c6 5988
304603f4 5989 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
30a970c6 5990
164dfd28 5991 if (req_cdclk != dev_priv->cdclk_freq) {
738c05c0
ID
5992 /*
5993 * FIXME: We can end up here with all power domains off, yet
5994 * with a CDCLK frequency other than the minimum. To account
5995 * for this take the PIPE-A power domain, which covers the HW
5996 * blocks needed for the following programming. This can be
5997 * removed once it's guaranteed that we get here either with
5998 * the minimum CDCLK set, or the required power domains
5999 * enabled.
6000 */
6001 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
6002
383c5a6a
VS
6003 if (IS_CHERRYVIEW(dev))
6004 cherryview_set_cdclk(dev, req_cdclk);
6005 else
6006 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6007
1e69cd74
VS
6008 vlv_program_pfi_credits(dev_priv);
6009
738c05c0 6010 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
383c5a6a 6011 }
30a970c6
JB
6012}
6013
89b667f8
JB
6014static void valleyview_crtc_enable(struct drm_crtc *crtc)
6015{
6016 struct drm_device *dev = crtc->dev;
a72e4c9f 6017 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6018 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6019 struct intel_encoder *encoder;
6020 int pipe = intel_crtc->pipe;
23538ef1 6021 bool is_dsi;
89b667f8 6022
53d9f4e9 6023 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6024 return;
6025
409ee761 6026 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 6027
1ae0d137
VS
6028 if (!is_dsi) {
6029 if (IS_CHERRYVIEW(dev))
6e3c9717 6030 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 6031 else
6e3c9717 6032 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 6033 }
5b18e57c 6034
6e3c9717 6035 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6036 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6037
6038 intel_set_pipe_timings(intel_crtc);
6039
c14b0485
VS
6040 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6041 struct drm_i915_private *dev_priv = dev->dev_private;
6042
6043 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6044 I915_WRITE(CHV_CANVAS(pipe), 0);
6045 }
6046
5b18e57c
DV
6047 i9xx_set_pipeconf(intel_crtc);
6048
89b667f8 6049 intel_crtc->active = true;
89b667f8 6050
a72e4c9f 6051 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6052
89b667f8
JB
6053 for_each_encoder_on_crtc(dev, crtc, encoder)
6054 if (encoder->pre_pll_enable)
6055 encoder->pre_pll_enable(encoder);
6056
9d556c99
CML
6057 if (!is_dsi) {
6058 if (IS_CHERRYVIEW(dev))
6e3c9717 6059 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6060 else
6e3c9717 6061 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6062 }
89b667f8
JB
6063
6064 for_each_encoder_on_crtc(dev, crtc, encoder)
6065 if (encoder->pre_enable)
6066 encoder->pre_enable(encoder);
6067
2dd24552
JB
6068 i9xx_pfit_enable(intel_crtc);
6069
63cbb074
VS
6070 intel_crtc_load_lut(crtc);
6071
f37fcc2a 6072 intel_update_watermarks(crtc);
e1fdc473 6073 intel_enable_pipe(intel_crtc);
be6a6f8e 6074
4b3a9526
VS
6075 assert_vblank_disabled(crtc);
6076 drm_crtc_vblank_on(crtc);
6077
f9b61ff6
DV
6078 for_each_encoder_on_crtc(dev, crtc, encoder)
6079 encoder->enable(encoder);
89b667f8
JB
6080}
6081
f13c2ef3
DV
6082static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6083{
6084 struct drm_device *dev = crtc->base.dev;
6085 struct drm_i915_private *dev_priv = dev->dev_private;
6086
6e3c9717
ACO
6087 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6088 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6089}
6090
0b8765c6 6091static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6092{
6093 struct drm_device *dev = crtc->dev;
a72e4c9f 6094 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6096 struct intel_encoder *encoder;
79e53945 6097 int pipe = intel_crtc->pipe;
79e53945 6098
53d9f4e9 6099 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6100 return;
6101
f13c2ef3
DV
6102 i9xx_set_pll_dividers(intel_crtc);
6103
6e3c9717 6104 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6105 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6106
6107 intel_set_pipe_timings(intel_crtc);
6108
5b18e57c
DV
6109 i9xx_set_pipeconf(intel_crtc);
6110
f7abfe8b 6111 intel_crtc->active = true;
6b383a7f 6112
4a3436e8 6113 if (!IS_GEN2(dev))
a72e4c9f 6114 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6115
9d6d9f19
MK
6116 for_each_encoder_on_crtc(dev, crtc, encoder)
6117 if (encoder->pre_enable)
6118 encoder->pre_enable(encoder);
6119
f6736a1a
DV
6120 i9xx_enable_pll(intel_crtc);
6121
2dd24552
JB
6122 i9xx_pfit_enable(intel_crtc);
6123
63cbb074
VS
6124 intel_crtc_load_lut(crtc);
6125
f37fcc2a 6126 intel_update_watermarks(crtc);
e1fdc473 6127 intel_enable_pipe(intel_crtc);
be6a6f8e 6128
4b3a9526
VS
6129 assert_vblank_disabled(crtc);
6130 drm_crtc_vblank_on(crtc);
6131
f9b61ff6
DV
6132 for_each_encoder_on_crtc(dev, crtc, encoder)
6133 encoder->enable(encoder);
0b8765c6 6134}
79e53945 6135
87476d63
DV
6136static void i9xx_pfit_disable(struct intel_crtc *crtc)
6137{
6138 struct drm_device *dev = crtc->base.dev;
6139 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6140
6e3c9717 6141 if (!crtc->config->gmch_pfit.control)
328d8e82 6142 return;
87476d63 6143
328d8e82 6144 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6145
328d8e82
DV
6146 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6147 I915_READ(PFIT_CONTROL));
6148 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6149}
6150
0b8765c6
JB
6151static void i9xx_crtc_disable(struct drm_crtc *crtc)
6152{
6153 struct drm_device *dev = crtc->dev;
6154 struct drm_i915_private *dev_priv = dev->dev_private;
6155 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6156 struct intel_encoder *encoder;
0b8765c6 6157 int pipe = intel_crtc->pipe;
ef9c3aee 6158
53d9f4e9 6159 if (WARN_ON(!intel_crtc->active))
f7abfe8b
CW
6160 return;
6161
6304cd91
VS
6162 /*
6163 * On gen2 planes are double buffered but the pipe isn't, so we must
6164 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6165 * We also need to wait on all gmch platforms because of the
6166 * self-refresh mode constraint explained above.
6304cd91 6167 */
564ed191 6168 intel_wait_for_vblank(dev, pipe);
6304cd91 6169
4b3a9526
VS
6170 for_each_encoder_on_crtc(dev, crtc, encoder)
6171 encoder->disable(encoder);
6172
f9b61ff6
DV
6173 drm_crtc_vblank_off(crtc);
6174 assert_vblank_disabled(crtc);
6175
575f7ab7 6176 intel_disable_pipe(intel_crtc);
24a1f16d 6177
87476d63 6178 i9xx_pfit_disable(intel_crtc);
24a1f16d 6179
89b667f8
JB
6180 for_each_encoder_on_crtc(dev, crtc, encoder)
6181 if (encoder->post_disable)
6182 encoder->post_disable(encoder);
6183
409ee761 6184 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6185 if (IS_CHERRYVIEW(dev))
6186 chv_disable_pll(dev_priv, pipe);
6187 else if (IS_VALLEYVIEW(dev))
6188 vlv_disable_pll(dev_priv, pipe);
6189 else
1c4e0274 6190 i9xx_disable_pll(intel_crtc);
076ed3b2 6191 }
0b8765c6 6192
4a3436e8 6193 if (!IS_GEN2(dev))
a72e4c9f 6194 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 6195
f7abfe8b 6196 intel_crtc->active = false;
46ba614c 6197 intel_update_watermarks(crtc);
f37fcc2a 6198
efa9624e 6199 mutex_lock(&dev->struct_mutex);
7ff0ebcc 6200 intel_fbc_update(dev);
efa9624e 6201 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
6202}
6203
b17d48e2
ML
6204static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6205{
6206 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6207 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6208 enum intel_display_power_domain domain;
6209 unsigned long domains;
6210
6211 if (!intel_crtc->active)
6212 return;
6213
6214 intel_crtc_disable_planes(crtc);
6215 dev_priv->display.crtc_disable(crtc);
6216
6217 domains = intel_crtc->enabled_power_domains;
6218 for_each_power_domain(domain, domains)
6219 intel_display_power_put(dev_priv, domain);
6220 intel_crtc->enabled_power_domains = 0;
6221}
6222
6b72d486
ML
6223/*
6224 * turn all crtc's off, but do not adjust state
6225 * This has to be paired with a call to intel_modeset_setup_hw_state.
6226 */
9716c691 6227void intel_display_suspend(struct drm_device *dev)
ee7b9f93 6228{
6b72d486
ML
6229 struct drm_crtc *crtc;
6230
b17d48e2
ML
6231 for_each_crtc(dev, crtc)
6232 intel_crtc_disable_noatomic(crtc);
ee7b9f93
JB
6233}
6234
b04c5bd6 6235/* Master function to enable/disable CRTC and corresponding power wells */
5da76e94 6236int intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
6237{
6238 struct drm_device *dev = crtc->dev;
5da76e94
ML
6239 struct drm_mode_config *config = &dev->mode_config;
6240 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
0e572fe7 6241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5da76e94
ML
6242 struct intel_crtc_state *pipe_config;
6243 struct drm_atomic_state *state;
6244 int ret;
976f8a20 6245
1b509259 6246 if (enable == intel_crtc->active)
5da76e94 6247 return 0;
0e572fe7 6248
1b509259 6249 if (enable && !crtc->state->enable)
5da76e94 6250 return 0;
1b509259 6251
5da76e94
ML
6252 /* this function should be called with drm_modeset_lock_all for now */
6253 if (WARN_ON(!ctx))
6254 return -EIO;
6255 lockdep_assert_held(&ctx->ww_ctx);
1b509259 6256
5da76e94
ML
6257 state = drm_atomic_state_alloc(dev);
6258 if (WARN_ON(!state))
6259 return -ENOMEM;
1b509259 6260
5da76e94
ML
6261 state->acquire_ctx = ctx;
6262 state->allow_modeset = true;
6263
6264 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6265 if (IS_ERR(pipe_config)) {
6266 ret = PTR_ERR(pipe_config);
6267 goto err;
0e572fe7 6268 }
5da76e94
ML
6269 pipe_config->base.active = enable;
6270
6271 ret = intel_set_mode(state);
6272 if (!ret)
6273 return ret;
6274
6275err:
6276 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6277 drm_atomic_state_free(state);
6278 return ret;
b04c5bd6
BF
6279}
6280
6281/**
6282 * Sets the power management mode of the pipe and plane.
6283 */
6284void intel_crtc_update_dpms(struct drm_crtc *crtc)
6285{
6286 struct drm_device *dev = crtc->dev;
6287 struct intel_encoder *intel_encoder;
6288 bool enable = false;
6289
6290 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6291 enable |= intel_encoder->connectors_active;
6292
6293 intel_crtc_control(crtc, enable);
cdd59983
CW
6294}
6295
ea5b213a 6296void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6297{
4ef69c7a 6298 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6299
ea5b213a
CW
6300 drm_encoder_cleanup(encoder);
6301 kfree(intel_encoder);
7e7d76c3
JB
6302}
6303
9237329d 6304/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
6305 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6306 * state of the entire output pipe. */
9237329d 6307static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 6308{
5ab432ef
DV
6309 if (mode == DRM_MODE_DPMS_ON) {
6310 encoder->connectors_active = true;
6311
b2cabb0e 6312 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
6313 } else {
6314 encoder->connectors_active = false;
6315
b2cabb0e 6316 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 6317 }
79e53945
JB
6318}
6319
0a91ca29
DV
6320/* Cross check the actual hw state with our own modeset state tracking (and it's
6321 * internal consistency). */
b980514c 6322static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6323{
0a91ca29
DV
6324 if (connector->get_hw_state(connector)) {
6325 struct intel_encoder *encoder = connector->encoder;
6326 struct drm_crtc *crtc;
6327 bool encoder_enabled;
6328 enum pipe pipe;
6329
6330 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6331 connector->base.base.id,
c23cc417 6332 connector->base.name);
0a91ca29 6333
0e32b39c
DA
6334 /* there is no real hw state for MST connectors */
6335 if (connector->mst_port)
6336 return;
6337
e2c719b7 6338 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 6339 "wrong connector dpms state\n");
e2c719b7 6340 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 6341 "active connector not linked to encoder\n");
0a91ca29 6342
36cd7444 6343 if (encoder) {
e2c719b7 6344 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
6345 "encoder->connectors_active not set\n");
6346
6347 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
6348 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6349 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 6350 return;
0a91ca29 6351
36cd7444 6352 crtc = encoder->base.crtc;
0a91ca29 6353
83d65738
MR
6354 I915_STATE_WARN(!crtc->state->enable,
6355 "crtc not enabled\n");
e2c719b7
RC
6356 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6357 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
6358 "encoder active on the wrong pipe\n");
6359 }
0a91ca29 6360 }
79e53945
JB
6361}
6362
08d9bc92
ACO
6363int intel_connector_init(struct intel_connector *connector)
6364{
6365 struct drm_connector_state *connector_state;
6366
6367 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6368 if (!connector_state)
6369 return -ENOMEM;
6370
6371 connector->base.state = connector_state;
6372 return 0;
6373}
6374
6375struct intel_connector *intel_connector_alloc(void)
6376{
6377 struct intel_connector *connector;
6378
6379 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6380 if (!connector)
6381 return NULL;
6382
6383 if (intel_connector_init(connector) < 0) {
6384 kfree(connector);
6385 return NULL;
6386 }
6387
6388 return connector;
6389}
6390
5ab432ef
DV
6391/* Even simpler default implementation, if there's really no special case to
6392 * consider. */
6393void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 6394{
5ab432ef
DV
6395 /* All the simple cases only support two dpms states. */
6396 if (mode != DRM_MODE_DPMS_ON)
6397 mode = DRM_MODE_DPMS_OFF;
d4270e57 6398
5ab432ef
DV
6399 if (mode == connector->dpms)
6400 return;
6401
6402 connector->dpms = mode;
6403
6404 /* Only need to change hw state when actually enabled */
c9976dcf
CW
6405 if (connector->encoder)
6406 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 6407
b980514c 6408 intel_modeset_check_state(connector->dev);
79e53945
JB
6409}
6410
f0947c37
DV
6411/* Simple connector->get_hw_state implementation for encoders that support only
6412 * one connector and no cloning and hence the encoder state determines the state
6413 * of the connector. */
6414bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6415{
24929352 6416 enum pipe pipe = 0;
f0947c37 6417 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6418
f0947c37 6419 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6420}
6421
6d293983 6422static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6423{
6d293983
ACO
6424 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6425 return crtc_state->fdi_lanes;
d272ddfa
VS
6426
6427 return 0;
6428}
6429
6d293983 6430static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6431 struct intel_crtc_state *pipe_config)
1857e1da 6432{
6d293983
ACO
6433 struct drm_atomic_state *state = pipe_config->base.state;
6434 struct intel_crtc *other_crtc;
6435 struct intel_crtc_state *other_crtc_state;
6436
1857e1da
DV
6437 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6438 pipe_name(pipe), pipe_config->fdi_lanes);
6439 if (pipe_config->fdi_lanes > 4) {
6440 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6441 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6442 return -EINVAL;
1857e1da
DV
6443 }
6444
bafb6553 6445 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6446 if (pipe_config->fdi_lanes > 2) {
6447 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6448 pipe_config->fdi_lanes);
6d293983 6449 return -EINVAL;
1857e1da 6450 } else {
6d293983 6451 return 0;
1857e1da
DV
6452 }
6453 }
6454
6455 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6456 return 0;
1857e1da
DV
6457
6458 /* Ivybridge 3 pipe is really complicated */
6459 switch (pipe) {
6460 case PIPE_A:
6d293983 6461 return 0;
1857e1da 6462 case PIPE_B:
6d293983
ACO
6463 if (pipe_config->fdi_lanes <= 2)
6464 return 0;
6465
6466 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6467 other_crtc_state =
6468 intel_atomic_get_crtc_state(state, other_crtc);
6469 if (IS_ERR(other_crtc_state))
6470 return PTR_ERR(other_crtc_state);
6471
6472 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6473 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6474 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6475 return -EINVAL;
1857e1da 6476 }
6d293983 6477 return 0;
1857e1da 6478 case PIPE_C:
251cc67c
VS
6479 if (pipe_config->fdi_lanes > 2) {
6480 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6481 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6482 return -EINVAL;
251cc67c 6483 }
6d293983
ACO
6484
6485 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6486 other_crtc_state =
6487 intel_atomic_get_crtc_state(state, other_crtc);
6488 if (IS_ERR(other_crtc_state))
6489 return PTR_ERR(other_crtc_state);
6490
6491 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6492 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6493 return -EINVAL;
1857e1da 6494 }
6d293983 6495 return 0;
1857e1da
DV
6496 default:
6497 BUG();
6498 }
6499}
6500
e29c22c0
DV
6501#define RETRY 1
6502static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6503 struct intel_crtc_state *pipe_config)
877d48d5 6504{
1857e1da 6505 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 6506 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6507 int lane, link_bw, fdi_dotclock, ret;
6508 bool needs_recompute = false;
877d48d5 6509
e29c22c0 6510retry:
877d48d5
DV
6511 /* FDI is a binary signal running at ~2.7GHz, encoding
6512 * each output octet as 10 bits. The actual frequency
6513 * is stored as a divider into a 100MHz clock, and the
6514 * mode pixel clock is stored in units of 1KHz.
6515 * Hence the bw of each lane in terms of the mode signal
6516 * is:
6517 */
6518 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6519
241bfc38 6520 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6521
2bd89a07 6522 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6523 pipe_config->pipe_bpp);
6524
6525 pipe_config->fdi_lanes = lane;
6526
2bd89a07 6527 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6528 link_bw, &pipe_config->fdi_m_n);
1857e1da 6529
6d293983
ACO
6530 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6531 intel_crtc->pipe, pipe_config);
6532 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6533 pipe_config->pipe_bpp -= 2*3;
6534 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6535 pipe_config->pipe_bpp);
6536 needs_recompute = true;
6537 pipe_config->bw_constrained = true;
6538
6539 goto retry;
6540 }
6541
6542 if (needs_recompute)
6543 return RETRY;
6544
6d293983 6545 return ret;
877d48d5
DV
6546}
6547
8cfb3407
VS
6548static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6549 struct intel_crtc_state *pipe_config)
6550{
6551 if (pipe_config->pipe_bpp > 24)
6552 return false;
6553
6554 /* HSW can handle pixel rate up to cdclk? */
6555 if (IS_HASWELL(dev_priv->dev))
6556 return true;
6557
6558 /*
b432e5cf
VS
6559 * We compare against max which means we must take
6560 * the increased cdclk requirement into account when
6561 * calculating the new cdclk.
6562 *
6563 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6564 */
6565 return ilk_pipe_pixel_rate(pipe_config) <=
6566 dev_priv->max_cdclk_freq * 95 / 100;
6567}
6568
42db64ef 6569static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6570 struct intel_crtc_state *pipe_config)
42db64ef 6571{
8cfb3407
VS
6572 struct drm_device *dev = crtc->base.dev;
6573 struct drm_i915_private *dev_priv = dev->dev_private;
6574
d330a953 6575 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6576 hsw_crtc_supports_ips(crtc) &&
6577 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6578}
6579
a43f6e0f 6580static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6581 struct intel_crtc_state *pipe_config)
79e53945 6582{
a43f6e0f 6583 struct drm_device *dev = crtc->base.dev;
8bd31e67 6584 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 6585 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
d03c93d4 6586 int ret;
89749350 6587
ad3a4479 6588 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6589 if (INTEL_INFO(dev)->gen < 4) {
44913155 6590 int clock_limit = dev_priv->max_cdclk_freq;
cf532bb2
VS
6591
6592 /*
6593 * Enable pixel doubling when the dot clock
6594 * is > 90% of the (display) core speed.
6595 *
b397c96b
VS
6596 * GDG double wide on either pipe,
6597 * otherwise pipe A only.
cf532bb2 6598 */
b397c96b 6599 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 6600 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 6601 clock_limit *= 2;
cf532bb2 6602 pipe_config->double_wide = true;
ad3a4479
VS
6603 }
6604
241bfc38 6605 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 6606 return -EINVAL;
2c07245f 6607 }
89749350 6608
1d1d0e27
VS
6609 /*
6610 * Pipe horizontal size must be even in:
6611 * - DVO ganged mode
6612 * - LVDS dual channel mode
6613 * - Double wide pipe
6614 */
a93e255f 6615 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6616 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6617 pipe_config->pipe_src_w &= ~1;
6618
8693a824
DL
6619 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6620 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6621 */
6622 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6623 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 6624 return -EINVAL;
44f46b42 6625
f5adf94e 6626 if (HAS_IPS(dev))
a43f6e0f
DV
6627 hsw_compute_ips_config(crtc, pipe_config);
6628
877d48d5 6629 if (pipe_config->has_pch_encoder)
a43f6e0f 6630 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6631
d03c93d4
CK
6632 /* FIXME: remove below call once atomic mode set is place and all crtc
6633 * related checks called from atomic_crtc_check function */
6634 ret = 0;
6635 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6636 crtc, pipe_config->base.state);
6637 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6638
6639 return ret;
79e53945
JB
6640}
6641
1652d19e
VS
6642static int skylake_get_display_clock_speed(struct drm_device *dev)
6643{
6644 struct drm_i915_private *dev_priv = to_i915(dev);
6645 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6646 uint32_t cdctl = I915_READ(CDCLK_CTL);
6647 uint32_t linkrate;
6648
414355a7 6649 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6650 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6651
6652 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6653 return 540000;
6654
6655 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6656 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6657
71cd8423
DL
6658 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6659 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6660 /* vco 8640 */
6661 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6662 case CDCLK_FREQ_450_432:
6663 return 432000;
6664 case CDCLK_FREQ_337_308:
6665 return 308570;
6666 case CDCLK_FREQ_675_617:
6667 return 617140;
6668 default:
6669 WARN(1, "Unknown cd freq selection\n");
6670 }
6671 } else {
6672 /* vco 8100 */
6673 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6674 case CDCLK_FREQ_450_432:
6675 return 450000;
6676 case CDCLK_FREQ_337_308:
6677 return 337500;
6678 case CDCLK_FREQ_675_617:
6679 return 675000;
6680 default:
6681 WARN(1, "Unknown cd freq selection\n");
6682 }
6683 }
6684
6685 /* error case, do as if DPLL0 isn't enabled */
6686 return 24000;
6687}
6688
6689static int broadwell_get_display_clock_speed(struct drm_device *dev)
6690{
6691 struct drm_i915_private *dev_priv = dev->dev_private;
6692 uint32_t lcpll = I915_READ(LCPLL_CTL);
6693 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6694
6695 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6696 return 800000;
6697 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6698 return 450000;
6699 else if (freq == LCPLL_CLK_FREQ_450)
6700 return 450000;
6701 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6702 return 540000;
6703 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6704 return 337500;
6705 else
6706 return 675000;
6707}
6708
6709static int haswell_get_display_clock_speed(struct drm_device *dev)
6710{
6711 struct drm_i915_private *dev_priv = dev->dev_private;
6712 uint32_t lcpll = I915_READ(LCPLL_CTL);
6713 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6714
6715 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6716 return 800000;
6717 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6718 return 450000;
6719 else if (freq == LCPLL_CLK_FREQ_450)
6720 return 450000;
6721 else if (IS_HSW_ULT(dev))
6722 return 337500;
6723 else
6724 return 540000;
79e53945
JB
6725}
6726
25eb05fc
JB
6727static int valleyview_get_display_clock_speed(struct drm_device *dev)
6728{
d197b7d3 6729 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
6730 u32 val;
6731 int divider;
6732
6bcda4f0
VS
6733 if (dev_priv->hpll_freq == 0)
6734 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6735
a580516d 6736 mutex_lock(&dev_priv->sb_lock);
d197b7d3 6737 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
a580516d 6738 mutex_unlock(&dev_priv->sb_lock);
d197b7d3
VS
6739
6740 divider = val & DISPLAY_FREQUENCY_VALUES;
6741
7d007f40
VS
6742 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6743 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6744 "cdclk change in progress\n");
6745
6bcda4f0 6746 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
6747}
6748
b37a6434
VS
6749static int ilk_get_display_clock_speed(struct drm_device *dev)
6750{
6751 return 450000;
6752}
6753
e70236a8
JB
6754static int i945_get_display_clock_speed(struct drm_device *dev)
6755{
6756 return 400000;
6757}
79e53945 6758
e70236a8 6759static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6760{
e907f170 6761 return 333333;
e70236a8 6762}
79e53945 6763
e70236a8
JB
6764static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6765{
6766 return 200000;
6767}
79e53945 6768
257a7ffc
DV
6769static int pnv_get_display_clock_speed(struct drm_device *dev)
6770{
6771 u16 gcfgc = 0;
6772
6773 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6774
6775 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6776 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6777 return 266667;
257a7ffc 6778 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6779 return 333333;
257a7ffc 6780 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6781 return 444444;
257a7ffc
DV
6782 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6783 return 200000;
6784 default:
6785 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6786 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6787 return 133333;
257a7ffc 6788 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6789 return 166667;
257a7ffc
DV
6790 }
6791}
6792
e70236a8
JB
6793static int i915gm_get_display_clock_speed(struct drm_device *dev)
6794{
6795 u16 gcfgc = 0;
79e53945 6796
e70236a8
JB
6797 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6798
6799 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6800 return 133333;
e70236a8
JB
6801 else {
6802 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6803 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6804 return 333333;
e70236a8
JB
6805 default:
6806 case GC_DISPLAY_CLOCK_190_200_MHZ:
6807 return 190000;
79e53945 6808 }
e70236a8
JB
6809 }
6810}
6811
6812static int i865_get_display_clock_speed(struct drm_device *dev)
6813{
e907f170 6814 return 266667;
e70236a8
JB
6815}
6816
1b1d2716 6817static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6818{
6819 u16 hpllcc = 0;
1b1d2716 6820
65cd2b3f
VS
6821 /*
6822 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6823 * encoding is different :(
6824 * FIXME is this the right way to detect 852GM/852GMV?
6825 */
6826 if (dev->pdev->revision == 0x1)
6827 return 133333;
6828
1b1d2716
VS
6829 pci_bus_read_config_word(dev->pdev->bus,
6830 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6831
e70236a8
JB
6832 /* Assume that the hardware is in the high speed state. This
6833 * should be the default.
6834 */
6835 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6836 case GC_CLOCK_133_200:
1b1d2716 6837 case GC_CLOCK_133_200_2:
e70236a8
JB
6838 case GC_CLOCK_100_200:
6839 return 200000;
6840 case GC_CLOCK_166_250:
6841 return 250000;
6842 case GC_CLOCK_100_133:
e907f170 6843 return 133333;
1b1d2716
VS
6844 case GC_CLOCK_133_266:
6845 case GC_CLOCK_133_266_2:
6846 case GC_CLOCK_166_266:
6847 return 266667;
e70236a8 6848 }
79e53945 6849
e70236a8
JB
6850 /* Shouldn't happen */
6851 return 0;
6852}
79e53945 6853
e70236a8
JB
6854static int i830_get_display_clock_speed(struct drm_device *dev)
6855{
e907f170 6856 return 133333;
79e53945
JB
6857}
6858
34edce2f
VS
6859static unsigned int intel_hpll_vco(struct drm_device *dev)
6860{
6861 struct drm_i915_private *dev_priv = dev->dev_private;
6862 static const unsigned int blb_vco[8] = {
6863 [0] = 3200000,
6864 [1] = 4000000,
6865 [2] = 5333333,
6866 [3] = 4800000,
6867 [4] = 6400000,
6868 };
6869 static const unsigned int pnv_vco[8] = {
6870 [0] = 3200000,
6871 [1] = 4000000,
6872 [2] = 5333333,
6873 [3] = 4800000,
6874 [4] = 2666667,
6875 };
6876 static const unsigned int cl_vco[8] = {
6877 [0] = 3200000,
6878 [1] = 4000000,
6879 [2] = 5333333,
6880 [3] = 6400000,
6881 [4] = 3333333,
6882 [5] = 3566667,
6883 [6] = 4266667,
6884 };
6885 static const unsigned int elk_vco[8] = {
6886 [0] = 3200000,
6887 [1] = 4000000,
6888 [2] = 5333333,
6889 [3] = 4800000,
6890 };
6891 static const unsigned int ctg_vco[8] = {
6892 [0] = 3200000,
6893 [1] = 4000000,
6894 [2] = 5333333,
6895 [3] = 6400000,
6896 [4] = 2666667,
6897 [5] = 4266667,
6898 };
6899 const unsigned int *vco_table;
6900 unsigned int vco;
6901 uint8_t tmp = 0;
6902
6903 /* FIXME other chipsets? */
6904 if (IS_GM45(dev))
6905 vco_table = ctg_vco;
6906 else if (IS_G4X(dev))
6907 vco_table = elk_vco;
6908 else if (IS_CRESTLINE(dev))
6909 vco_table = cl_vco;
6910 else if (IS_PINEVIEW(dev))
6911 vco_table = pnv_vco;
6912 else if (IS_G33(dev))
6913 vco_table = blb_vco;
6914 else
6915 return 0;
6916
6917 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6918
6919 vco = vco_table[tmp & 0x7];
6920 if (vco == 0)
6921 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6922 else
6923 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6924
6925 return vco;
6926}
6927
6928static int gm45_get_display_clock_speed(struct drm_device *dev)
6929{
6930 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6931 uint16_t tmp = 0;
6932
6933 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6934
6935 cdclk_sel = (tmp >> 12) & 0x1;
6936
6937 switch (vco) {
6938 case 2666667:
6939 case 4000000:
6940 case 5333333:
6941 return cdclk_sel ? 333333 : 222222;
6942 case 3200000:
6943 return cdclk_sel ? 320000 : 228571;
6944 default:
6945 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6946 return 222222;
6947 }
6948}
6949
6950static int i965gm_get_display_clock_speed(struct drm_device *dev)
6951{
6952 static const uint8_t div_3200[] = { 16, 10, 8 };
6953 static const uint8_t div_4000[] = { 20, 12, 10 };
6954 static const uint8_t div_5333[] = { 24, 16, 14 };
6955 const uint8_t *div_table;
6956 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6957 uint16_t tmp = 0;
6958
6959 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6960
6961 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6962
6963 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6964 goto fail;
6965
6966 switch (vco) {
6967 case 3200000:
6968 div_table = div_3200;
6969 break;
6970 case 4000000:
6971 div_table = div_4000;
6972 break;
6973 case 5333333:
6974 div_table = div_5333;
6975 break;
6976 default:
6977 goto fail;
6978 }
6979
6980 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6981
caf4e252 6982fail:
34edce2f
VS
6983 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6984 return 200000;
6985}
6986
6987static int g33_get_display_clock_speed(struct drm_device *dev)
6988{
6989 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6990 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6991 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6992 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6993 const uint8_t *div_table;
6994 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6995 uint16_t tmp = 0;
6996
6997 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6998
6999 cdclk_sel = (tmp >> 4) & 0x7;
7000
7001 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7002 goto fail;
7003
7004 switch (vco) {
7005 case 3200000:
7006 div_table = div_3200;
7007 break;
7008 case 4000000:
7009 div_table = div_4000;
7010 break;
7011 case 4800000:
7012 div_table = div_4800;
7013 break;
7014 case 5333333:
7015 div_table = div_5333;
7016 break;
7017 default:
7018 goto fail;
7019 }
7020
7021 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7022
caf4e252 7023fail:
34edce2f
VS
7024 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7025 return 190476;
7026}
7027
2c07245f 7028static void
a65851af 7029intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7030{
a65851af
VS
7031 while (*num > DATA_LINK_M_N_MASK ||
7032 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7033 *num >>= 1;
7034 *den >>= 1;
7035 }
7036}
7037
a65851af
VS
7038static void compute_m_n(unsigned int m, unsigned int n,
7039 uint32_t *ret_m, uint32_t *ret_n)
7040{
7041 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7042 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7043 intel_reduce_m_n_ratio(ret_m, ret_n);
7044}
7045
e69d0bc1
DV
7046void
7047intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7048 int pixel_clock, int link_clock,
7049 struct intel_link_m_n *m_n)
2c07245f 7050{
e69d0bc1 7051 m_n->tu = 64;
a65851af
VS
7052
7053 compute_m_n(bits_per_pixel * pixel_clock,
7054 link_clock * nlanes * 8,
7055 &m_n->gmch_m, &m_n->gmch_n);
7056
7057 compute_m_n(pixel_clock, link_clock,
7058 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7059}
7060
a7615030
CW
7061static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7062{
d330a953
JN
7063 if (i915.panel_use_ssc >= 0)
7064 return i915.panel_use_ssc != 0;
41aa3448 7065 return dev_priv->vbt.lvds_use_ssc
435793df 7066 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7067}
7068
a93e255f
ACO
7069static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7070 int num_connectors)
c65d77d8 7071{
a93e255f 7072 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
7073 struct drm_i915_private *dev_priv = dev->dev_private;
7074 int refclk;
7075
a93e255f
ACO
7076 WARN_ON(!crtc_state->base.state);
7077
5ab7b0b7 7078 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 7079 refclk = 100000;
a93e255f 7080 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 7081 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
7082 refclk = dev_priv->vbt.lvds_ssc_freq;
7083 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
7084 } else if (!IS_GEN2(dev)) {
7085 refclk = 96000;
7086 } else {
7087 refclk = 48000;
7088 }
7089
7090 return refclk;
7091}
7092
7429e9d4 7093static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7094{
7df00d7a 7095 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7096}
f47709a9 7097
7429e9d4
DV
7098static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7099{
7100 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7101}
7102
f47709a9 7103static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7104 struct intel_crtc_state *crtc_state,
a7516a05
JB
7105 intel_clock_t *reduced_clock)
7106{
f47709a9 7107 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7108 u32 fp, fp2 = 0;
7109
7110 if (IS_PINEVIEW(dev)) {
190f68c5 7111 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7112 if (reduced_clock)
7429e9d4 7113 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7114 } else {
190f68c5 7115 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7116 if (reduced_clock)
7429e9d4 7117 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7118 }
7119
190f68c5 7120 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7121
f47709a9 7122 crtc->lowfreq_avail = false;
a93e255f 7123 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7124 reduced_clock) {
190f68c5 7125 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7126 crtc->lowfreq_avail = true;
a7516a05 7127 } else {
190f68c5 7128 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7129 }
7130}
7131
5e69f97f
CML
7132static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7133 pipe)
89b667f8
JB
7134{
7135 u32 reg_val;
7136
7137 /*
7138 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7139 * and set it to a reasonable value instead.
7140 */
ab3c759a 7141 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7142 reg_val &= 0xffffff00;
7143 reg_val |= 0x00000030;
ab3c759a 7144 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7145
ab3c759a 7146 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7147 reg_val &= 0x8cffffff;
7148 reg_val = 0x8c000000;
ab3c759a 7149 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7150
ab3c759a 7151 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7152 reg_val &= 0xffffff00;
ab3c759a 7153 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7154
ab3c759a 7155 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7156 reg_val &= 0x00ffffff;
7157 reg_val |= 0xb0000000;
ab3c759a 7158 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7159}
7160
b551842d
DV
7161static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7162 struct intel_link_m_n *m_n)
7163{
7164 struct drm_device *dev = crtc->base.dev;
7165 struct drm_i915_private *dev_priv = dev->dev_private;
7166 int pipe = crtc->pipe;
7167
e3b95f1e
DV
7168 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7169 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7170 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7171 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7172}
7173
7174static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7175 struct intel_link_m_n *m_n,
7176 struct intel_link_m_n *m2_n2)
b551842d
DV
7177{
7178 struct drm_device *dev = crtc->base.dev;
7179 struct drm_i915_private *dev_priv = dev->dev_private;
7180 int pipe = crtc->pipe;
6e3c9717 7181 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7182
7183 if (INTEL_INFO(dev)->gen >= 5) {
7184 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7185 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7186 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7187 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7188 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7189 * for gen < 8) and if DRRS is supported (to make sure the
7190 * registers are not unnecessarily accessed).
7191 */
44395bfe 7192 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7193 crtc->config->has_drrs) {
f769cd24
VK
7194 I915_WRITE(PIPE_DATA_M2(transcoder),
7195 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7196 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7197 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7198 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7199 }
b551842d 7200 } else {
e3b95f1e
DV
7201 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7202 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7203 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7204 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7205 }
7206}
7207
fe3cd48d 7208void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7209{
fe3cd48d
R
7210 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7211
7212 if (m_n == M1_N1) {
7213 dp_m_n = &crtc->config->dp_m_n;
7214 dp_m2_n2 = &crtc->config->dp_m2_n2;
7215 } else if (m_n == M2_N2) {
7216
7217 /*
7218 * M2_N2 registers are not supported. Hence m2_n2 divider value
7219 * needs to be programmed into M1_N1.
7220 */
7221 dp_m_n = &crtc->config->dp_m2_n2;
7222 } else {
7223 DRM_ERROR("Unsupported divider value\n");
7224 return;
7225 }
7226
6e3c9717
ACO
7227 if (crtc->config->has_pch_encoder)
7228 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7229 else
fe3cd48d 7230 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7231}
7232
d288f65f 7233static void vlv_update_pll(struct intel_crtc *crtc,
5cec258b 7234 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7235{
7236 u32 dpll, dpll_md;
7237
7238 /*
7239 * Enable DPIO clock input. We should never disable the reference
7240 * clock for pipe B, since VGA hotplug / manual detection depends
7241 * on it.
7242 */
7243 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7244 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7245 /* We should never disable this, set it here for state tracking */
7246 if (crtc->pipe == PIPE_B)
7247 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7248 dpll |= DPLL_VCO_ENABLE;
d288f65f 7249 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7250
d288f65f 7251 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7252 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7253 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7254}
7255
d288f65f 7256static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7257 const struct intel_crtc_state *pipe_config)
a0c4da24 7258{
f47709a9 7259 struct drm_device *dev = crtc->base.dev;
a0c4da24 7260 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7261 int pipe = crtc->pipe;
bdd4b6a6 7262 u32 mdiv;
a0c4da24 7263 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7264 u32 coreclk, reg_val;
a0c4da24 7265
a580516d 7266 mutex_lock(&dev_priv->sb_lock);
09153000 7267
d288f65f
VS
7268 bestn = pipe_config->dpll.n;
7269 bestm1 = pipe_config->dpll.m1;
7270 bestm2 = pipe_config->dpll.m2;
7271 bestp1 = pipe_config->dpll.p1;
7272 bestp2 = pipe_config->dpll.p2;
a0c4da24 7273
89b667f8
JB
7274 /* See eDP HDMI DPIO driver vbios notes doc */
7275
7276 /* PLL B needs special handling */
bdd4b6a6 7277 if (pipe == PIPE_B)
5e69f97f 7278 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7279
7280 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7281 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7282
7283 /* Disable target IRef on PLL */
ab3c759a 7284 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7285 reg_val &= 0x00ffffff;
ab3c759a 7286 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7287
7288 /* Disable fast lock */
ab3c759a 7289 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7290
7291 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7292 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7293 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7294 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7295 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7296
7297 /*
7298 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7299 * but we don't support that).
7300 * Note: don't use the DAC post divider as it seems unstable.
7301 */
7302 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7303 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7304
a0c4da24 7305 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7306 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7307
89b667f8 7308 /* Set HBR and RBR LPF coefficients */
d288f65f 7309 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7310 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7311 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7312 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7313 0x009f0003);
89b667f8 7314 else
ab3c759a 7315 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7316 0x00d0000f);
7317
681a8504 7318 if (pipe_config->has_dp_encoder) {
89b667f8 7319 /* Use SSC source */
bdd4b6a6 7320 if (pipe == PIPE_A)
ab3c759a 7321 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7322 0x0df40000);
7323 else
ab3c759a 7324 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7325 0x0df70000);
7326 } else { /* HDMI or VGA */
7327 /* Use bend source */
bdd4b6a6 7328 if (pipe == PIPE_A)
ab3c759a 7329 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7330 0x0df70000);
7331 else
ab3c759a 7332 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7333 0x0df40000);
7334 }
a0c4da24 7335
ab3c759a 7336 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7337 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7338 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7339 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7340 coreclk |= 0x01000000;
ab3c759a 7341 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7342
ab3c759a 7343 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7344 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7345}
7346
d288f65f 7347static void chv_update_pll(struct intel_crtc *crtc,
5cec258b 7348 struct intel_crtc_state *pipe_config)
1ae0d137 7349{
d288f65f 7350 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
7351 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7352 DPLL_VCO_ENABLE;
7353 if (crtc->pipe != PIPE_A)
d288f65f 7354 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7355
d288f65f
VS
7356 pipe_config->dpll_hw_state.dpll_md =
7357 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7358}
7359
d288f65f 7360static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7361 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7362{
7363 struct drm_device *dev = crtc->base.dev;
7364 struct drm_i915_private *dev_priv = dev->dev_private;
7365 int pipe = crtc->pipe;
7366 int dpll_reg = DPLL(crtc->pipe);
7367 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7368 u32 loopfilter, tribuf_calcntr;
9d556c99 7369 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7370 u32 dpio_val;
9cbe40c1 7371 int vco;
9d556c99 7372
d288f65f
VS
7373 bestn = pipe_config->dpll.n;
7374 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7375 bestm1 = pipe_config->dpll.m1;
7376 bestm2 = pipe_config->dpll.m2 >> 22;
7377 bestp1 = pipe_config->dpll.p1;
7378 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7379 vco = pipe_config->dpll.vco;
a945ce7e 7380 dpio_val = 0;
9cbe40c1 7381 loopfilter = 0;
9d556c99
CML
7382
7383 /*
7384 * Enable Refclk and SSC
7385 */
a11b0703 7386 I915_WRITE(dpll_reg,
d288f65f 7387 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7388
a580516d 7389 mutex_lock(&dev_priv->sb_lock);
9d556c99 7390
9d556c99
CML
7391 /* p1 and p2 divider */
7392 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7393 5 << DPIO_CHV_S1_DIV_SHIFT |
7394 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7395 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7396 1 << DPIO_CHV_K_DIV_SHIFT);
7397
7398 /* Feedback post-divider - m2 */
7399 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7400
7401 /* Feedback refclk divider - n and m1 */
7402 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7403 DPIO_CHV_M1_DIV_BY_2 |
7404 1 << DPIO_CHV_N_DIV_SHIFT);
7405
7406 /* M2 fraction division */
a945ce7e
VP
7407 if (bestm2_frac)
7408 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7409
7410 /* M2 fraction division enable */
a945ce7e
VP
7411 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7412 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7413 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7414 if (bestm2_frac)
7415 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7416 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7417
de3a0fde
VP
7418 /* Program digital lock detect threshold */
7419 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7420 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7421 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7422 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7423 if (!bestm2_frac)
7424 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7425 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7426
9d556c99 7427 /* Loop filter */
9cbe40c1
VP
7428 if (vco == 5400000) {
7429 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7430 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7431 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7432 tribuf_calcntr = 0x9;
7433 } else if (vco <= 6200000) {
7434 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7435 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7436 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7437 tribuf_calcntr = 0x9;
7438 } else if (vco <= 6480000) {
7439 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7440 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7441 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7442 tribuf_calcntr = 0x8;
7443 } else {
7444 /* Not supported. Apply the same limits as in the max case */
7445 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7446 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7447 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7448 tribuf_calcntr = 0;
7449 }
9d556c99
CML
7450 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7451
968040b2 7452 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7453 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7454 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7455 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7456
9d556c99
CML
7457 /* AFC Recal */
7458 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7459 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7460 DPIO_AFC_RECAL);
7461
a580516d 7462 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7463}
7464
d288f65f
VS
7465/**
7466 * vlv_force_pll_on - forcibly enable just the PLL
7467 * @dev_priv: i915 private structure
7468 * @pipe: pipe PLL to enable
7469 * @dpll: PLL configuration
7470 *
7471 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7472 * in cases where we need the PLL enabled even when @pipe is not going to
7473 * be enabled.
7474 */
7475void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7476 const struct dpll *dpll)
7477{
7478 struct intel_crtc *crtc =
7479 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 7480 struct intel_crtc_state pipe_config = {
a93e255f 7481 .base.crtc = &crtc->base,
d288f65f
VS
7482 .pixel_multiplier = 1,
7483 .dpll = *dpll,
7484 };
7485
7486 if (IS_CHERRYVIEW(dev)) {
7487 chv_update_pll(crtc, &pipe_config);
7488 chv_prepare_pll(crtc, &pipe_config);
7489 chv_enable_pll(crtc, &pipe_config);
7490 } else {
7491 vlv_update_pll(crtc, &pipe_config);
7492 vlv_prepare_pll(crtc, &pipe_config);
7493 vlv_enable_pll(crtc, &pipe_config);
7494 }
7495}
7496
7497/**
7498 * vlv_force_pll_off - forcibly disable just the PLL
7499 * @dev_priv: i915 private structure
7500 * @pipe: pipe PLL to disable
7501 *
7502 * Disable the PLL for @pipe. To be used in cases where we need
7503 * the PLL enabled even when @pipe is not going to be enabled.
7504 */
7505void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7506{
7507 if (IS_CHERRYVIEW(dev))
7508 chv_disable_pll(to_i915(dev), pipe);
7509 else
7510 vlv_disable_pll(to_i915(dev), pipe);
7511}
7512
f47709a9 7513static void i9xx_update_pll(struct intel_crtc *crtc,
190f68c5 7514 struct intel_crtc_state *crtc_state,
f47709a9 7515 intel_clock_t *reduced_clock,
eb1cbe48
DV
7516 int num_connectors)
7517{
f47709a9 7518 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7519 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7520 u32 dpll;
7521 bool is_sdvo;
190f68c5 7522 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7523
190f68c5 7524 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7525
a93e255f
ACO
7526 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7527 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7528
7529 dpll = DPLL_VGA_MODE_DIS;
7530
a93e255f 7531 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7532 dpll |= DPLLB_MODE_LVDS;
7533 else
7534 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7535
ef1b460d 7536 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7537 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7538 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7539 }
198a037f
DV
7540
7541 if (is_sdvo)
4a33e48d 7542 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7543
190f68c5 7544 if (crtc_state->has_dp_encoder)
4a33e48d 7545 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7546
7547 /* compute bitmask from p1 value */
7548 if (IS_PINEVIEW(dev))
7549 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7550 else {
7551 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7552 if (IS_G4X(dev) && reduced_clock)
7553 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7554 }
7555 switch (clock->p2) {
7556 case 5:
7557 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7558 break;
7559 case 7:
7560 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7561 break;
7562 case 10:
7563 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7564 break;
7565 case 14:
7566 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7567 break;
7568 }
7569 if (INTEL_INFO(dev)->gen >= 4)
7570 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7571
190f68c5 7572 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7573 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7574 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7575 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7576 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7577 else
7578 dpll |= PLL_REF_INPUT_DREFCLK;
7579
7580 dpll |= DPLL_VCO_ENABLE;
190f68c5 7581 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7582
eb1cbe48 7583 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7584 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7585 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7586 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7587 }
7588}
7589
f47709a9 7590static void i8xx_update_pll(struct intel_crtc *crtc,
190f68c5 7591 struct intel_crtc_state *crtc_state,
f47709a9 7592 intel_clock_t *reduced_clock,
eb1cbe48
DV
7593 int num_connectors)
7594{
f47709a9 7595 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7596 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7597 u32 dpll;
190f68c5 7598 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7599
190f68c5 7600 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7601
eb1cbe48
DV
7602 dpll = DPLL_VGA_MODE_DIS;
7603
a93e255f 7604 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7605 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7606 } else {
7607 if (clock->p1 == 2)
7608 dpll |= PLL_P1_DIVIDE_BY_TWO;
7609 else
7610 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7611 if (clock->p2 == 4)
7612 dpll |= PLL_P2_DIVIDE_BY_4;
7613 }
7614
a93e255f 7615 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7616 dpll |= DPLL_DVO_2X_MODE;
7617
a93e255f 7618 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7619 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7620 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7621 else
7622 dpll |= PLL_REF_INPUT_DREFCLK;
7623
7624 dpll |= DPLL_VCO_ENABLE;
190f68c5 7625 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7626}
7627
8a654f3b 7628static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7629{
7630 struct drm_device *dev = intel_crtc->base.dev;
7631 struct drm_i915_private *dev_priv = dev->dev_private;
7632 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7633 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 7634 struct drm_display_mode *adjusted_mode =
6e3c9717 7635 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7636 uint32_t crtc_vtotal, crtc_vblank_end;
7637 int vsyncshift = 0;
4d8a62ea
DV
7638
7639 /* We need to be careful not to changed the adjusted mode, for otherwise
7640 * the hw state checker will get angry at the mismatch. */
7641 crtc_vtotal = adjusted_mode->crtc_vtotal;
7642 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7643
609aeaca 7644 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7645 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7646 crtc_vtotal -= 1;
7647 crtc_vblank_end -= 1;
609aeaca 7648
409ee761 7649 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7650 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7651 else
7652 vsyncshift = adjusted_mode->crtc_hsync_start -
7653 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7654 if (vsyncshift < 0)
7655 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7656 }
7657
7658 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7659 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7660
fe2b8f9d 7661 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7662 (adjusted_mode->crtc_hdisplay - 1) |
7663 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7664 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7665 (adjusted_mode->crtc_hblank_start - 1) |
7666 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7667 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7668 (adjusted_mode->crtc_hsync_start - 1) |
7669 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7670
fe2b8f9d 7671 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7672 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7673 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7674 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7675 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7676 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7677 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7678 (adjusted_mode->crtc_vsync_start - 1) |
7679 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7680
b5e508d4
PZ
7681 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7682 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7683 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7684 * bits. */
7685 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7686 (pipe == PIPE_B || pipe == PIPE_C))
7687 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7688
b0e77b9c
PZ
7689 /* pipesrc controls the size that is scaled from, which should
7690 * always be the user's requested size.
7691 */
7692 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7693 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7694 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7695}
7696
1bd1bd80 7697static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7698 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7699{
7700 struct drm_device *dev = crtc->base.dev;
7701 struct drm_i915_private *dev_priv = dev->dev_private;
7702 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7703 uint32_t tmp;
7704
7705 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7706 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7707 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7708 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7709 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7710 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7711 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7712 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7713 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7714
7715 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7716 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7717 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7718 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7719 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7720 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7721 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7722 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7723 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7724
7725 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7726 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7727 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7728 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7729 }
7730
7731 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7732 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7733 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7734
2d112de7
ACO
7735 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7736 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7737}
7738
f6a83288 7739void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7740 struct intel_crtc_state *pipe_config)
babea61d 7741{
2d112de7
ACO
7742 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7743 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7744 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7745 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7746
2d112de7
ACO
7747 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7748 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7749 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7750 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7751
2d112de7 7752 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 7753
2d112de7
ACO
7754 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7755 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
7756}
7757
84b046f3
DV
7758static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7759{
7760 struct drm_device *dev = intel_crtc->base.dev;
7761 struct drm_i915_private *dev_priv = dev->dev_private;
7762 uint32_t pipeconf;
7763
9f11a9e4 7764 pipeconf = 0;
84b046f3 7765
b6b5d049
VS
7766 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7767 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7768 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7769
6e3c9717 7770 if (intel_crtc->config->double_wide)
cf532bb2 7771 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7772
ff9ce46e
DV
7773 /* only g4x and later have fancy bpc/dither controls */
7774 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 7775 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7776 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7777 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7778 PIPECONF_DITHER_TYPE_SP;
84b046f3 7779
6e3c9717 7780 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7781 case 18:
7782 pipeconf |= PIPECONF_6BPC;
7783 break;
7784 case 24:
7785 pipeconf |= PIPECONF_8BPC;
7786 break;
7787 case 30:
7788 pipeconf |= PIPECONF_10BPC;
7789 break;
7790 default:
7791 /* Case prevented by intel_choose_pipe_bpp_dither. */
7792 BUG();
84b046f3
DV
7793 }
7794 }
7795
7796 if (HAS_PIPE_CXSR(dev)) {
7797 if (intel_crtc->lowfreq_avail) {
7798 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7799 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7800 } else {
7801 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7802 }
7803 }
7804
6e3c9717 7805 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7806 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7807 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7808 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7809 else
7810 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7811 } else
84b046f3
DV
7812 pipeconf |= PIPECONF_PROGRESSIVE;
7813
6e3c9717 7814 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 7815 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7816
84b046f3
DV
7817 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7818 POSTING_READ(PIPECONF(intel_crtc->pipe));
7819}
7820
190f68c5
ACO
7821static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7822 struct intel_crtc_state *crtc_state)
79e53945 7823{
c7653199 7824 struct drm_device *dev = crtc->base.dev;
79e53945 7825 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7826 int refclk, num_connectors = 0;
652c393a 7827 intel_clock_t clock, reduced_clock;
a16af721 7828 bool ok, has_reduced_clock = false;
e9fd1c02 7829 bool is_lvds = false, is_dsi = false;
5eddb70b 7830 struct intel_encoder *encoder;
d4906093 7831 const intel_limit_t *limit;
55bb9992 7832 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 7833 struct drm_connector *connector;
55bb9992
ACO
7834 struct drm_connector_state *connector_state;
7835 int i;
79e53945 7836
dd3cd74a
ACO
7837 memset(&crtc_state->dpll_hw_state, 0,
7838 sizeof(crtc_state->dpll_hw_state));
7839
da3ced29 7840 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
7841 if (connector_state->crtc != &crtc->base)
7842 continue;
7843
7844 encoder = to_intel_encoder(connector_state->best_encoder);
7845
5eddb70b 7846 switch (encoder->type) {
79e53945
JB
7847 case INTEL_OUTPUT_LVDS:
7848 is_lvds = true;
7849 break;
e9fd1c02
JN
7850 case INTEL_OUTPUT_DSI:
7851 is_dsi = true;
7852 break;
6847d71b
PZ
7853 default:
7854 break;
79e53945 7855 }
43565a06 7856
c751ce4f 7857 num_connectors++;
79e53945
JB
7858 }
7859
f2335330 7860 if (is_dsi)
5b18e57c 7861 return 0;
f2335330 7862
190f68c5 7863 if (!crtc_state->clock_set) {
a93e255f 7864 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7865
e9fd1c02
JN
7866 /*
7867 * Returns a set of divisors for the desired target clock with
7868 * the given refclk, or FALSE. The returned values represent
7869 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7870 * 2) / p1 / p2.
7871 */
a93e255f
ACO
7872 limit = intel_limit(crtc_state, refclk);
7873 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7874 crtc_state->port_clock,
e9fd1c02 7875 refclk, NULL, &clock);
f2335330 7876 if (!ok) {
e9fd1c02
JN
7877 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7878 return -EINVAL;
7879 }
79e53945 7880
f2335330
JN
7881 if (is_lvds && dev_priv->lvds_downclock_avail) {
7882 /*
7883 * Ensure we match the reduced clock's P to the target
7884 * clock. If the clocks don't match, we can't switch
7885 * the display clock by using the FP0/FP1. In such case
7886 * we will disable the LVDS downclock feature.
7887 */
7888 has_reduced_clock =
a93e255f 7889 dev_priv->display.find_dpll(limit, crtc_state,
f2335330
JN
7890 dev_priv->lvds_downclock,
7891 refclk, &clock,
7892 &reduced_clock);
7893 }
7894 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7895 crtc_state->dpll.n = clock.n;
7896 crtc_state->dpll.m1 = clock.m1;
7897 crtc_state->dpll.m2 = clock.m2;
7898 crtc_state->dpll.p1 = clock.p1;
7899 crtc_state->dpll.p2 = clock.p2;
f47709a9 7900 }
7026d4ac 7901
e9fd1c02 7902 if (IS_GEN2(dev)) {
190f68c5 7903 i8xx_update_pll(crtc, crtc_state,
2a8f64ca
VP
7904 has_reduced_clock ? &reduced_clock : NULL,
7905 num_connectors);
9d556c99 7906 } else if (IS_CHERRYVIEW(dev)) {
190f68c5 7907 chv_update_pll(crtc, crtc_state);
e9fd1c02 7908 } else if (IS_VALLEYVIEW(dev)) {
190f68c5 7909 vlv_update_pll(crtc, crtc_state);
e9fd1c02 7910 } else {
190f68c5 7911 i9xx_update_pll(crtc, crtc_state,
eb1cbe48 7912 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 7913 num_connectors);
e9fd1c02 7914 }
79e53945 7915
c8f7a0db 7916 return 0;
f564048e
EA
7917}
7918
2fa2fe9a 7919static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7920 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7921{
7922 struct drm_device *dev = crtc->base.dev;
7923 struct drm_i915_private *dev_priv = dev->dev_private;
7924 uint32_t tmp;
7925
dc9e7dec
VS
7926 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7927 return;
7928
2fa2fe9a 7929 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7930 if (!(tmp & PFIT_ENABLE))
7931 return;
2fa2fe9a 7932
06922821 7933 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7934 if (INTEL_INFO(dev)->gen < 4) {
7935 if (crtc->pipe != PIPE_B)
7936 return;
2fa2fe9a
DV
7937 } else {
7938 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7939 return;
7940 }
7941
06922821 7942 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7943 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7944 if (INTEL_INFO(dev)->gen < 5)
7945 pipe_config->gmch_pfit.lvds_border_bits =
7946 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7947}
7948
acbec814 7949static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7950 struct intel_crtc_state *pipe_config)
acbec814
JB
7951{
7952 struct drm_device *dev = crtc->base.dev;
7953 struct drm_i915_private *dev_priv = dev->dev_private;
7954 int pipe = pipe_config->cpu_transcoder;
7955 intel_clock_t clock;
7956 u32 mdiv;
662c6ecb 7957 int refclk = 100000;
acbec814 7958
f573de5a
SK
7959 /* In case of MIPI DPLL will not even be used */
7960 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7961 return;
7962
a580516d 7963 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7964 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7965 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7966
7967 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7968 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7969 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7970 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7971 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7972
f646628b 7973 vlv_clock(refclk, &clock);
acbec814 7974
f646628b
VS
7975 /* clock.dot is the fast clock */
7976 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
7977}
7978
5724dbd1
DL
7979static void
7980i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7981 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7982{
7983 struct drm_device *dev = crtc->base.dev;
7984 struct drm_i915_private *dev_priv = dev->dev_private;
7985 u32 val, base, offset;
7986 int pipe = crtc->pipe, plane = crtc->plane;
7987 int fourcc, pixel_format;
6761dd31 7988 unsigned int aligned_height;
b113d5ee 7989 struct drm_framebuffer *fb;
1b842c89 7990 struct intel_framebuffer *intel_fb;
1ad292b5 7991
42a7b088
DL
7992 val = I915_READ(DSPCNTR(plane));
7993 if (!(val & DISPLAY_PLANE_ENABLE))
7994 return;
7995
d9806c9f 7996 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7997 if (!intel_fb) {
1ad292b5
JB
7998 DRM_DEBUG_KMS("failed to alloc fb\n");
7999 return;
8000 }
8001
1b842c89
DL
8002 fb = &intel_fb->base;
8003
18c5247e
DV
8004 if (INTEL_INFO(dev)->gen >= 4) {
8005 if (val & DISPPLANE_TILED) {
49af449b 8006 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8007 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8008 }
8009 }
1ad292b5
JB
8010
8011 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8012 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8013 fb->pixel_format = fourcc;
8014 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8015
8016 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8017 if (plane_config->tiling)
1ad292b5
JB
8018 offset = I915_READ(DSPTILEOFF(plane));
8019 else
8020 offset = I915_READ(DSPLINOFF(plane));
8021 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8022 } else {
8023 base = I915_READ(DSPADDR(plane));
8024 }
8025 plane_config->base = base;
8026
8027 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8028 fb->width = ((val >> 16) & 0xfff) + 1;
8029 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8030
8031 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8032 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8033
b113d5ee 8034 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8035 fb->pixel_format,
8036 fb->modifier[0]);
1ad292b5 8037
f37b5c2b 8038 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8039
2844a921
DL
8040 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8041 pipe_name(pipe), plane, fb->width, fb->height,
8042 fb->bits_per_pixel, base, fb->pitches[0],
8043 plane_config->size);
1ad292b5 8044
2d14030b 8045 plane_config->fb = intel_fb;
1ad292b5
JB
8046}
8047
70b23a98 8048static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8049 struct intel_crtc_state *pipe_config)
70b23a98
VS
8050{
8051 struct drm_device *dev = crtc->base.dev;
8052 struct drm_i915_private *dev_priv = dev->dev_private;
8053 int pipe = pipe_config->cpu_transcoder;
8054 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8055 intel_clock_t clock;
8056 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8057 int refclk = 100000;
8058
a580516d 8059 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8060 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8061 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8062 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8063 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
a580516d 8064 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8065
8066 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8067 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8068 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8069 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8070 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8071
8072 chv_clock(refclk, &clock);
8073
8074 /* clock.dot is the fast clock */
8075 pipe_config->port_clock = clock.dot / 5;
8076}
8077
0e8ffe1b 8078static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8079 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8080{
8081 struct drm_device *dev = crtc->base.dev;
8082 struct drm_i915_private *dev_priv = dev->dev_private;
8083 uint32_t tmp;
8084
f458ebbc
DV
8085 if (!intel_display_power_is_enabled(dev_priv,
8086 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
8087 return false;
8088
e143a21c 8089 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8090 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8091
0e8ffe1b
DV
8092 tmp = I915_READ(PIPECONF(crtc->pipe));
8093 if (!(tmp & PIPECONF_ENABLE))
8094 return false;
8095
42571aef
VS
8096 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8097 switch (tmp & PIPECONF_BPC_MASK) {
8098 case PIPECONF_6BPC:
8099 pipe_config->pipe_bpp = 18;
8100 break;
8101 case PIPECONF_8BPC:
8102 pipe_config->pipe_bpp = 24;
8103 break;
8104 case PIPECONF_10BPC:
8105 pipe_config->pipe_bpp = 30;
8106 break;
8107 default:
8108 break;
8109 }
8110 }
8111
b5a9fa09
DV
8112 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8113 pipe_config->limited_color_range = true;
8114
282740f7
VS
8115 if (INTEL_INFO(dev)->gen < 4)
8116 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8117
1bd1bd80
DV
8118 intel_get_pipe_timings(crtc, pipe_config);
8119
2fa2fe9a
DV
8120 i9xx_get_pfit_config(crtc, pipe_config);
8121
6c49f241
DV
8122 if (INTEL_INFO(dev)->gen >= 4) {
8123 tmp = I915_READ(DPLL_MD(crtc->pipe));
8124 pipe_config->pixel_multiplier =
8125 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8126 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8127 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8128 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8129 tmp = I915_READ(DPLL(crtc->pipe));
8130 pipe_config->pixel_multiplier =
8131 ((tmp & SDVO_MULTIPLIER_MASK)
8132 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8133 } else {
8134 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8135 * port and will be fixed up in the encoder->get_config
8136 * function. */
8137 pipe_config->pixel_multiplier = 1;
8138 }
8bcc2795
DV
8139 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8140 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
8141 /*
8142 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8143 * on 830. Filter it out here so that we don't
8144 * report errors due to that.
8145 */
8146 if (IS_I830(dev))
8147 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8148
8bcc2795
DV
8149 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8150 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8151 } else {
8152 /* Mask out read-only status bits. */
8153 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8154 DPLL_PORTC_READY_MASK |
8155 DPLL_PORTB_READY_MASK);
8bcc2795 8156 }
6c49f241 8157
70b23a98
VS
8158 if (IS_CHERRYVIEW(dev))
8159 chv_crtc_clock_get(crtc, pipe_config);
8160 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8161 vlv_crtc_clock_get(crtc, pipe_config);
8162 else
8163 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8164
0e8ffe1b
DV
8165 return true;
8166}
8167
dde86e2d 8168static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8169{
8170 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8171 struct intel_encoder *encoder;
74cfd7ac 8172 u32 val, final;
13d83a67 8173 bool has_lvds = false;
199e5d79 8174 bool has_cpu_edp = false;
199e5d79 8175 bool has_panel = false;
99eb6a01
KP
8176 bool has_ck505 = false;
8177 bool can_ssc = false;
13d83a67
JB
8178
8179 /* We need to take the global config into account */
b2784e15 8180 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8181 switch (encoder->type) {
8182 case INTEL_OUTPUT_LVDS:
8183 has_panel = true;
8184 has_lvds = true;
8185 break;
8186 case INTEL_OUTPUT_EDP:
8187 has_panel = true;
2de6905f 8188 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8189 has_cpu_edp = true;
8190 break;
6847d71b
PZ
8191 default:
8192 break;
13d83a67
JB
8193 }
8194 }
8195
99eb6a01 8196 if (HAS_PCH_IBX(dev)) {
41aa3448 8197 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8198 can_ssc = has_ck505;
8199 } else {
8200 has_ck505 = false;
8201 can_ssc = true;
8202 }
8203
2de6905f
ID
8204 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8205 has_panel, has_lvds, has_ck505);
13d83a67
JB
8206
8207 /* Ironlake: try to setup display ref clock before DPLL
8208 * enabling. This is only under driver's control after
8209 * PCH B stepping, previous chipset stepping should be
8210 * ignoring this setting.
8211 */
74cfd7ac
CW
8212 val = I915_READ(PCH_DREF_CONTROL);
8213
8214 /* As we must carefully and slowly disable/enable each source in turn,
8215 * compute the final state we want first and check if we need to
8216 * make any changes at all.
8217 */
8218 final = val;
8219 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8220 if (has_ck505)
8221 final |= DREF_NONSPREAD_CK505_ENABLE;
8222 else
8223 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8224
8225 final &= ~DREF_SSC_SOURCE_MASK;
8226 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8227 final &= ~DREF_SSC1_ENABLE;
8228
8229 if (has_panel) {
8230 final |= DREF_SSC_SOURCE_ENABLE;
8231
8232 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8233 final |= DREF_SSC1_ENABLE;
8234
8235 if (has_cpu_edp) {
8236 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8237 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8238 else
8239 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8240 } else
8241 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8242 } else {
8243 final |= DREF_SSC_SOURCE_DISABLE;
8244 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8245 }
8246
8247 if (final == val)
8248 return;
8249
13d83a67 8250 /* Always enable nonspread source */
74cfd7ac 8251 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8252
99eb6a01 8253 if (has_ck505)
74cfd7ac 8254 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8255 else
74cfd7ac 8256 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8257
199e5d79 8258 if (has_panel) {
74cfd7ac
CW
8259 val &= ~DREF_SSC_SOURCE_MASK;
8260 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8261
199e5d79 8262 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8263 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8264 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8265 val |= DREF_SSC1_ENABLE;
e77166b5 8266 } else
74cfd7ac 8267 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8268
8269 /* Get SSC going before enabling the outputs */
74cfd7ac 8270 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8271 POSTING_READ(PCH_DREF_CONTROL);
8272 udelay(200);
8273
74cfd7ac 8274 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8275
8276 /* Enable CPU source on CPU attached eDP */
199e5d79 8277 if (has_cpu_edp) {
99eb6a01 8278 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8279 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8280 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8281 } else
74cfd7ac 8282 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8283 } else
74cfd7ac 8284 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8285
74cfd7ac 8286 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8287 POSTING_READ(PCH_DREF_CONTROL);
8288 udelay(200);
8289 } else {
8290 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8291
74cfd7ac 8292 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8293
8294 /* Turn off CPU output */
74cfd7ac 8295 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8296
74cfd7ac 8297 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8298 POSTING_READ(PCH_DREF_CONTROL);
8299 udelay(200);
8300
8301 /* Turn off the SSC source */
74cfd7ac
CW
8302 val &= ~DREF_SSC_SOURCE_MASK;
8303 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8304
8305 /* Turn off SSC1 */
74cfd7ac 8306 val &= ~DREF_SSC1_ENABLE;
199e5d79 8307
74cfd7ac 8308 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8309 POSTING_READ(PCH_DREF_CONTROL);
8310 udelay(200);
8311 }
74cfd7ac
CW
8312
8313 BUG_ON(val != final);
13d83a67
JB
8314}
8315
f31f2d55 8316static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8317{
f31f2d55 8318 uint32_t tmp;
dde86e2d 8319
0ff066a9
PZ
8320 tmp = I915_READ(SOUTH_CHICKEN2);
8321 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8322 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8323
0ff066a9
PZ
8324 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8325 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8326 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8327
0ff066a9
PZ
8328 tmp = I915_READ(SOUTH_CHICKEN2);
8329 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8330 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8331
0ff066a9
PZ
8332 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8333 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8334 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8335}
8336
8337/* WaMPhyProgramming:hsw */
8338static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8339{
8340 uint32_t tmp;
dde86e2d
PZ
8341
8342 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8343 tmp &= ~(0xFF << 24);
8344 tmp |= (0x12 << 24);
8345 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8346
dde86e2d
PZ
8347 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8348 tmp |= (1 << 11);
8349 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8350
8351 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8352 tmp |= (1 << 11);
8353 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8354
dde86e2d
PZ
8355 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8356 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8357 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8358
8359 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8360 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8361 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8362
0ff066a9
PZ
8363 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8364 tmp &= ~(7 << 13);
8365 tmp |= (5 << 13);
8366 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8367
0ff066a9
PZ
8368 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8369 tmp &= ~(7 << 13);
8370 tmp |= (5 << 13);
8371 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8372
8373 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8374 tmp &= ~0xFF;
8375 tmp |= 0x1C;
8376 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8377
8378 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8379 tmp &= ~0xFF;
8380 tmp |= 0x1C;
8381 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8382
8383 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8384 tmp &= ~(0xFF << 16);
8385 tmp |= (0x1C << 16);
8386 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8387
8388 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8389 tmp &= ~(0xFF << 16);
8390 tmp |= (0x1C << 16);
8391 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8392
0ff066a9
PZ
8393 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8394 tmp |= (1 << 27);
8395 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8396
0ff066a9
PZ
8397 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8398 tmp |= (1 << 27);
8399 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8400
0ff066a9
PZ
8401 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8402 tmp &= ~(0xF << 28);
8403 tmp |= (4 << 28);
8404 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8405
0ff066a9
PZ
8406 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8407 tmp &= ~(0xF << 28);
8408 tmp |= (4 << 28);
8409 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8410}
8411
2fa86a1f
PZ
8412/* Implements 3 different sequences from BSpec chapter "Display iCLK
8413 * Programming" based on the parameters passed:
8414 * - Sequence to enable CLKOUT_DP
8415 * - Sequence to enable CLKOUT_DP without spread
8416 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8417 */
8418static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8419 bool with_fdi)
f31f2d55
PZ
8420{
8421 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8422 uint32_t reg, tmp;
8423
8424 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8425 with_spread = true;
8426 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8427 with_fdi, "LP PCH doesn't have FDI\n"))
8428 with_fdi = false;
f31f2d55 8429
a580516d 8430 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8431
8432 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8433 tmp &= ~SBI_SSCCTL_DISABLE;
8434 tmp |= SBI_SSCCTL_PATHALT;
8435 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8436
8437 udelay(24);
8438
2fa86a1f
PZ
8439 if (with_spread) {
8440 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8441 tmp &= ~SBI_SSCCTL_PATHALT;
8442 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8443
2fa86a1f
PZ
8444 if (with_fdi) {
8445 lpt_reset_fdi_mphy(dev_priv);
8446 lpt_program_fdi_mphy(dev_priv);
8447 }
8448 }
dde86e2d 8449
2fa86a1f
PZ
8450 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8451 SBI_GEN0 : SBI_DBUFF0;
8452 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8453 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8454 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8455
a580516d 8456 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8457}
8458
47701c3b
PZ
8459/* Sequence to disable CLKOUT_DP */
8460static void lpt_disable_clkout_dp(struct drm_device *dev)
8461{
8462 struct drm_i915_private *dev_priv = dev->dev_private;
8463 uint32_t reg, tmp;
8464
a580516d 8465 mutex_lock(&dev_priv->sb_lock);
47701c3b
PZ
8466
8467 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8468 SBI_GEN0 : SBI_DBUFF0;
8469 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8470 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8471 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8472
8473 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8474 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8475 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8476 tmp |= SBI_SSCCTL_PATHALT;
8477 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8478 udelay(32);
8479 }
8480 tmp |= SBI_SSCCTL_DISABLE;
8481 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8482 }
8483
a580516d 8484 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8485}
8486
bf8fa3d3
PZ
8487static void lpt_init_pch_refclk(struct drm_device *dev)
8488{
bf8fa3d3
PZ
8489 struct intel_encoder *encoder;
8490 bool has_vga = false;
8491
b2784e15 8492 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8493 switch (encoder->type) {
8494 case INTEL_OUTPUT_ANALOG:
8495 has_vga = true;
8496 break;
6847d71b
PZ
8497 default:
8498 break;
bf8fa3d3
PZ
8499 }
8500 }
8501
47701c3b
PZ
8502 if (has_vga)
8503 lpt_enable_clkout_dp(dev, true, true);
8504 else
8505 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
8506}
8507
dde86e2d
PZ
8508/*
8509 * Initialize reference clocks when the driver loads
8510 */
8511void intel_init_pch_refclk(struct drm_device *dev)
8512{
8513 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8514 ironlake_init_pch_refclk(dev);
8515 else if (HAS_PCH_LPT(dev))
8516 lpt_init_pch_refclk(dev);
8517}
8518
55bb9992 8519static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8520{
55bb9992 8521 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8522 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8523 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8524 struct drm_connector *connector;
55bb9992 8525 struct drm_connector_state *connector_state;
d9d444cb 8526 struct intel_encoder *encoder;
55bb9992 8527 int num_connectors = 0, i;
d9d444cb
JB
8528 bool is_lvds = false;
8529
da3ced29 8530 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8531 if (connector_state->crtc != crtc_state->base.crtc)
8532 continue;
8533
8534 encoder = to_intel_encoder(connector_state->best_encoder);
8535
d9d444cb
JB
8536 switch (encoder->type) {
8537 case INTEL_OUTPUT_LVDS:
8538 is_lvds = true;
8539 break;
6847d71b
PZ
8540 default:
8541 break;
d9d444cb
JB
8542 }
8543 num_connectors++;
8544 }
8545
8546 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8547 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8548 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8549 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8550 }
8551
8552 return 120000;
8553}
8554
6ff93609 8555static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8556{
c8203565 8557 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8558 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8559 int pipe = intel_crtc->pipe;
c8203565
PZ
8560 uint32_t val;
8561
78114071 8562 val = 0;
c8203565 8563
6e3c9717 8564 switch (intel_crtc->config->pipe_bpp) {
c8203565 8565 case 18:
dfd07d72 8566 val |= PIPECONF_6BPC;
c8203565
PZ
8567 break;
8568 case 24:
dfd07d72 8569 val |= PIPECONF_8BPC;
c8203565
PZ
8570 break;
8571 case 30:
dfd07d72 8572 val |= PIPECONF_10BPC;
c8203565
PZ
8573 break;
8574 case 36:
dfd07d72 8575 val |= PIPECONF_12BPC;
c8203565
PZ
8576 break;
8577 default:
cc769b62
PZ
8578 /* Case prevented by intel_choose_pipe_bpp_dither. */
8579 BUG();
c8203565
PZ
8580 }
8581
6e3c9717 8582 if (intel_crtc->config->dither)
c8203565
PZ
8583 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8584
6e3c9717 8585 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8586 val |= PIPECONF_INTERLACED_ILK;
8587 else
8588 val |= PIPECONF_PROGRESSIVE;
8589
6e3c9717 8590 if (intel_crtc->config->limited_color_range)
3685a8f3 8591 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8592
c8203565
PZ
8593 I915_WRITE(PIPECONF(pipe), val);
8594 POSTING_READ(PIPECONF(pipe));
8595}
8596
86d3efce
VS
8597/*
8598 * Set up the pipe CSC unit.
8599 *
8600 * Currently only full range RGB to limited range RGB conversion
8601 * is supported, but eventually this should handle various
8602 * RGB<->YCbCr scenarios as well.
8603 */
50f3b016 8604static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8605{
8606 struct drm_device *dev = crtc->dev;
8607 struct drm_i915_private *dev_priv = dev->dev_private;
8608 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8609 int pipe = intel_crtc->pipe;
8610 uint16_t coeff = 0x7800; /* 1.0 */
8611
8612 /*
8613 * TODO: Check what kind of values actually come out of the pipe
8614 * with these coeff/postoff values and adjust to get the best
8615 * accuracy. Perhaps we even need to take the bpc value into
8616 * consideration.
8617 */
8618
6e3c9717 8619 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8620 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8621
8622 /*
8623 * GY/GU and RY/RU should be the other way around according
8624 * to BSpec, but reality doesn't agree. Just set them up in
8625 * a way that results in the correct picture.
8626 */
8627 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8628 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8629
8630 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8631 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8632
8633 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8634 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8635
8636 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8637 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8638 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8639
8640 if (INTEL_INFO(dev)->gen > 6) {
8641 uint16_t postoff = 0;
8642
6e3c9717 8643 if (intel_crtc->config->limited_color_range)
32cf0cb0 8644 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8645
8646 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8647 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8648 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8649
8650 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8651 } else {
8652 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8653
6e3c9717 8654 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8655 mode |= CSC_BLACK_SCREEN_OFFSET;
8656
8657 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8658 }
8659}
8660
6ff93609 8661static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8662{
756f85cf
PZ
8663 struct drm_device *dev = crtc->dev;
8664 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 8665 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8666 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8667 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
8668 uint32_t val;
8669
3eff4faa 8670 val = 0;
ee2b0b38 8671
6e3c9717 8672 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
8673 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8674
6e3c9717 8675 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8676 val |= PIPECONF_INTERLACED_ILK;
8677 else
8678 val |= PIPECONF_PROGRESSIVE;
8679
702e7a56
PZ
8680 I915_WRITE(PIPECONF(cpu_transcoder), val);
8681 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
8682
8683 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8684 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 8685
3cdf122c 8686 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
8687 val = 0;
8688
6e3c9717 8689 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8690 case 18:
8691 val |= PIPEMISC_DITHER_6_BPC;
8692 break;
8693 case 24:
8694 val |= PIPEMISC_DITHER_8_BPC;
8695 break;
8696 case 30:
8697 val |= PIPEMISC_DITHER_10_BPC;
8698 break;
8699 case 36:
8700 val |= PIPEMISC_DITHER_12_BPC;
8701 break;
8702 default:
8703 /* Case prevented by pipe_config_set_bpp. */
8704 BUG();
8705 }
8706
6e3c9717 8707 if (intel_crtc->config->dither)
756f85cf
PZ
8708 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8709
8710 I915_WRITE(PIPEMISC(pipe), val);
8711 }
ee2b0b38
PZ
8712}
8713
6591c6e4 8714static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8715 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8716 intel_clock_t *clock,
8717 bool *has_reduced_clock,
8718 intel_clock_t *reduced_clock)
8719{
8720 struct drm_device *dev = crtc->dev;
8721 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8722 int refclk;
d4906093 8723 const intel_limit_t *limit;
a16af721 8724 bool ret, is_lvds = false;
79e53945 8725
a93e255f 8726 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
79e53945 8727
55bb9992 8728 refclk = ironlake_get_refclk(crtc_state);
79e53945 8729
d4906093
ML
8730 /*
8731 * Returns a set of divisors for the desired target clock with the given
8732 * refclk, or FALSE. The returned values represent the clock equation:
8733 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8734 */
a93e255f
ACO
8735 limit = intel_limit(crtc_state, refclk);
8736 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8737 crtc_state->port_clock,
ee9300bb 8738 refclk, NULL, clock);
6591c6e4
PZ
8739 if (!ret)
8740 return false;
cda4b7d3 8741
ddc9003c 8742 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
8743 /*
8744 * Ensure we match the reduced clock's P to the target clock.
8745 * If the clocks don't match, we can't switch the display clock
8746 * by using the FP0/FP1. In such case we will disable the LVDS
8747 * downclock feature.
8748 */
ee9300bb 8749 *has_reduced_clock =
a93e255f 8750 dev_priv->display.find_dpll(limit, crtc_state,
ee9300bb
DV
8751 dev_priv->lvds_downclock,
8752 refclk, clock,
8753 reduced_clock);
652c393a 8754 }
61e9653f 8755
6591c6e4
PZ
8756 return true;
8757}
8758
d4b1931c
PZ
8759int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8760{
8761 /*
8762 * Account for spread spectrum to avoid
8763 * oversubscribing the link. Max center spread
8764 * is 2.5%; use 5% for safety's sake.
8765 */
8766 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8767 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8768}
8769
7429e9d4 8770static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8771{
7429e9d4 8772 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8773}
8774
de13a2e3 8775static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8776 struct intel_crtc_state *crtc_state,
7429e9d4 8777 u32 *fp,
9a7c7890 8778 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8779{
de13a2e3 8780 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8781 struct drm_device *dev = crtc->dev;
8782 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8783 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8784 struct drm_connector *connector;
55bb9992
ACO
8785 struct drm_connector_state *connector_state;
8786 struct intel_encoder *encoder;
de13a2e3 8787 uint32_t dpll;
55bb9992 8788 int factor, num_connectors = 0, i;
09ede541 8789 bool is_lvds = false, is_sdvo = false;
79e53945 8790
da3ced29 8791 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8792 if (connector_state->crtc != crtc_state->base.crtc)
8793 continue;
8794
8795 encoder = to_intel_encoder(connector_state->best_encoder);
8796
8797 switch (encoder->type) {
79e53945
JB
8798 case INTEL_OUTPUT_LVDS:
8799 is_lvds = true;
8800 break;
8801 case INTEL_OUTPUT_SDVO:
7d57382e 8802 case INTEL_OUTPUT_HDMI:
79e53945 8803 is_sdvo = true;
79e53945 8804 break;
6847d71b
PZ
8805 default:
8806 break;
79e53945 8807 }
43565a06 8808
c751ce4f 8809 num_connectors++;
79e53945 8810 }
79e53945 8811
c1858123 8812 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8813 factor = 21;
8814 if (is_lvds) {
8815 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8816 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8817 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8818 factor = 25;
190f68c5 8819 } else if (crtc_state->sdvo_tv_clock)
8febb297 8820 factor = 20;
c1858123 8821
190f68c5 8822 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8823 *fp |= FP_CB_TUNE;
2c07245f 8824
9a7c7890
DV
8825 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8826 *fp2 |= FP_CB_TUNE;
8827
5eddb70b 8828 dpll = 0;
2c07245f 8829
a07d6787
EA
8830 if (is_lvds)
8831 dpll |= DPLLB_MODE_LVDS;
8832 else
8833 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8834
190f68c5 8835 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8836 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8837
8838 if (is_sdvo)
4a33e48d 8839 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8840 if (crtc_state->has_dp_encoder)
4a33e48d 8841 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8842
a07d6787 8843 /* compute bitmask from p1 value */
190f68c5 8844 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8845 /* also FPA1 */
190f68c5 8846 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8847
190f68c5 8848 switch (crtc_state->dpll.p2) {
a07d6787
EA
8849 case 5:
8850 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8851 break;
8852 case 7:
8853 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8854 break;
8855 case 10:
8856 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8857 break;
8858 case 14:
8859 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8860 break;
79e53945
JB
8861 }
8862
b4c09f3b 8863 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8864 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8865 else
8866 dpll |= PLL_REF_INPUT_DREFCLK;
8867
959e16d6 8868 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8869}
8870
190f68c5
ACO
8871static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8872 struct intel_crtc_state *crtc_state)
de13a2e3 8873{
c7653199 8874 struct drm_device *dev = crtc->base.dev;
de13a2e3 8875 intel_clock_t clock, reduced_clock;
cbbab5bd 8876 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8877 bool ok, has_reduced_clock = false;
8b47047b 8878 bool is_lvds = false;
e2b78267 8879 struct intel_shared_dpll *pll;
de13a2e3 8880
dd3cd74a
ACO
8881 memset(&crtc_state->dpll_hw_state, 0,
8882 sizeof(crtc_state->dpll_hw_state));
8883
409ee761 8884 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 8885
5dc5298b
PZ
8886 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8887 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8888
190f68c5 8889 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8890 &has_reduced_clock, &reduced_clock);
190f68c5 8891 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8892 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8893 return -EINVAL;
79e53945 8894 }
f47709a9 8895 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8896 if (!crtc_state->clock_set) {
8897 crtc_state->dpll.n = clock.n;
8898 crtc_state->dpll.m1 = clock.m1;
8899 crtc_state->dpll.m2 = clock.m2;
8900 crtc_state->dpll.p1 = clock.p1;
8901 crtc_state->dpll.p2 = clock.p2;
f47709a9 8902 }
79e53945 8903
5dc5298b 8904 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8905 if (crtc_state->has_pch_encoder) {
8906 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8907 if (has_reduced_clock)
7429e9d4 8908 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8909
190f68c5 8910 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8911 &fp, &reduced_clock,
8912 has_reduced_clock ? &fp2 : NULL);
8913
190f68c5
ACO
8914 crtc_state->dpll_hw_state.dpll = dpll;
8915 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8916 if (has_reduced_clock)
190f68c5 8917 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8918 else
190f68c5 8919 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8920
190f68c5 8921 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 8922 if (pll == NULL) {
84f44ce7 8923 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8924 pipe_name(crtc->pipe));
4b645f14
JB
8925 return -EINVAL;
8926 }
3fb37703 8927 }
79e53945 8928
ab585dea 8929 if (is_lvds && has_reduced_clock)
c7653199 8930 crtc->lowfreq_avail = true;
bcd644e0 8931 else
c7653199 8932 crtc->lowfreq_avail = false;
e2b78267 8933
c8f7a0db 8934 return 0;
79e53945
JB
8935}
8936
eb14cb74
VS
8937static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8938 struct intel_link_m_n *m_n)
8939{
8940 struct drm_device *dev = crtc->base.dev;
8941 struct drm_i915_private *dev_priv = dev->dev_private;
8942 enum pipe pipe = crtc->pipe;
8943
8944 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8945 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8946 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8947 & ~TU_SIZE_MASK;
8948 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8949 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8950 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8951}
8952
8953static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8954 enum transcoder transcoder,
b95af8be
VK
8955 struct intel_link_m_n *m_n,
8956 struct intel_link_m_n *m2_n2)
72419203
DV
8957{
8958 struct drm_device *dev = crtc->base.dev;
8959 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8960 enum pipe pipe = crtc->pipe;
72419203 8961
eb14cb74
VS
8962 if (INTEL_INFO(dev)->gen >= 5) {
8963 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8964 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8965 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8966 & ~TU_SIZE_MASK;
8967 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8968 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8969 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8970 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8971 * gen < 8) and if DRRS is supported (to make sure the
8972 * registers are not unnecessarily read).
8973 */
8974 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8975 crtc->config->has_drrs) {
b95af8be
VK
8976 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8977 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8978 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8979 & ~TU_SIZE_MASK;
8980 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8981 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8982 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8983 }
eb14cb74
VS
8984 } else {
8985 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8986 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8987 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8988 & ~TU_SIZE_MASK;
8989 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8990 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8991 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8992 }
8993}
8994
8995void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8996 struct intel_crtc_state *pipe_config)
eb14cb74 8997{
681a8504 8998 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8999 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9000 else
9001 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9002 &pipe_config->dp_m_n,
9003 &pipe_config->dp_m2_n2);
eb14cb74 9004}
72419203 9005
eb14cb74 9006static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9007 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9008{
9009 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9010 &pipe_config->fdi_m_n, NULL);
72419203
DV
9011}
9012
bd2e244f 9013static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9014 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9015{
9016 struct drm_device *dev = crtc->base.dev;
9017 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9018 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9019 uint32_t ps_ctrl = 0;
9020 int id = -1;
9021 int i;
bd2e244f 9022
a1b2278e
CK
9023 /* find scaler attached to this pipe */
9024 for (i = 0; i < crtc->num_scalers; i++) {
9025 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9026 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9027 id = i;
9028 pipe_config->pch_pfit.enabled = true;
9029 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9030 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9031 break;
9032 }
9033 }
bd2e244f 9034
a1b2278e
CK
9035 scaler_state->scaler_id = id;
9036 if (id >= 0) {
9037 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9038 } else {
9039 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9040 }
9041}
9042
5724dbd1
DL
9043static void
9044skylake_get_initial_plane_config(struct intel_crtc *crtc,
9045 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9046{
9047 struct drm_device *dev = crtc->base.dev;
9048 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9049 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9050 int pipe = crtc->pipe;
9051 int fourcc, pixel_format;
6761dd31 9052 unsigned int aligned_height;
bc8d7dff 9053 struct drm_framebuffer *fb;
1b842c89 9054 struct intel_framebuffer *intel_fb;
bc8d7dff 9055
d9806c9f 9056 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9057 if (!intel_fb) {
bc8d7dff
DL
9058 DRM_DEBUG_KMS("failed to alloc fb\n");
9059 return;
9060 }
9061
1b842c89
DL
9062 fb = &intel_fb->base;
9063
bc8d7dff 9064 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9065 if (!(val & PLANE_CTL_ENABLE))
9066 goto error;
9067
bc8d7dff
DL
9068 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9069 fourcc = skl_format_to_fourcc(pixel_format,
9070 val & PLANE_CTL_ORDER_RGBX,
9071 val & PLANE_CTL_ALPHA_MASK);
9072 fb->pixel_format = fourcc;
9073 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9074
40f46283
DL
9075 tiling = val & PLANE_CTL_TILED_MASK;
9076 switch (tiling) {
9077 case PLANE_CTL_TILED_LINEAR:
9078 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9079 break;
9080 case PLANE_CTL_TILED_X:
9081 plane_config->tiling = I915_TILING_X;
9082 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9083 break;
9084 case PLANE_CTL_TILED_Y:
9085 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9086 break;
9087 case PLANE_CTL_TILED_YF:
9088 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9089 break;
9090 default:
9091 MISSING_CASE(tiling);
9092 goto error;
9093 }
9094
bc8d7dff
DL
9095 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9096 plane_config->base = base;
9097
9098 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9099
9100 val = I915_READ(PLANE_SIZE(pipe, 0));
9101 fb->height = ((val >> 16) & 0xfff) + 1;
9102 fb->width = ((val >> 0) & 0x1fff) + 1;
9103
9104 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
9105 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9106 fb->pixel_format);
bc8d7dff
DL
9107 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9108
9109 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9110 fb->pixel_format,
9111 fb->modifier[0]);
bc8d7dff 9112
f37b5c2b 9113 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9114
9115 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9116 pipe_name(pipe), fb->width, fb->height,
9117 fb->bits_per_pixel, base, fb->pitches[0],
9118 plane_config->size);
9119
2d14030b 9120 plane_config->fb = intel_fb;
bc8d7dff
DL
9121 return;
9122
9123error:
9124 kfree(fb);
9125}
9126
2fa2fe9a 9127static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9128 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9129{
9130 struct drm_device *dev = crtc->base.dev;
9131 struct drm_i915_private *dev_priv = dev->dev_private;
9132 uint32_t tmp;
9133
9134 tmp = I915_READ(PF_CTL(crtc->pipe));
9135
9136 if (tmp & PF_ENABLE) {
fd4daa9c 9137 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9138 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9139 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9140
9141 /* We currently do not free assignements of panel fitters on
9142 * ivb/hsw (since we don't use the higher upscaling modes which
9143 * differentiates them) so just WARN about this case for now. */
9144 if (IS_GEN7(dev)) {
9145 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9146 PF_PIPE_SEL_IVB(crtc->pipe));
9147 }
2fa2fe9a 9148 }
79e53945
JB
9149}
9150
5724dbd1
DL
9151static void
9152ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9153 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9154{
9155 struct drm_device *dev = crtc->base.dev;
9156 struct drm_i915_private *dev_priv = dev->dev_private;
9157 u32 val, base, offset;
aeee5a49 9158 int pipe = crtc->pipe;
4c6baa59 9159 int fourcc, pixel_format;
6761dd31 9160 unsigned int aligned_height;
b113d5ee 9161 struct drm_framebuffer *fb;
1b842c89 9162 struct intel_framebuffer *intel_fb;
4c6baa59 9163
42a7b088
DL
9164 val = I915_READ(DSPCNTR(pipe));
9165 if (!(val & DISPLAY_PLANE_ENABLE))
9166 return;
9167
d9806c9f 9168 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9169 if (!intel_fb) {
4c6baa59
JB
9170 DRM_DEBUG_KMS("failed to alloc fb\n");
9171 return;
9172 }
9173
1b842c89
DL
9174 fb = &intel_fb->base;
9175
18c5247e
DV
9176 if (INTEL_INFO(dev)->gen >= 4) {
9177 if (val & DISPPLANE_TILED) {
49af449b 9178 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9179 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9180 }
9181 }
4c6baa59
JB
9182
9183 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9184 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9185 fb->pixel_format = fourcc;
9186 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9187
aeee5a49 9188 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9189 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9190 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9191 } else {
49af449b 9192 if (plane_config->tiling)
aeee5a49 9193 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9194 else
aeee5a49 9195 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9196 }
9197 plane_config->base = base;
9198
9199 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9200 fb->width = ((val >> 16) & 0xfff) + 1;
9201 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9202
9203 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9204 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9205
b113d5ee 9206 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9207 fb->pixel_format,
9208 fb->modifier[0]);
4c6baa59 9209
f37b5c2b 9210 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9211
2844a921
DL
9212 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9213 pipe_name(pipe), fb->width, fb->height,
9214 fb->bits_per_pixel, base, fb->pitches[0],
9215 plane_config->size);
b113d5ee 9216
2d14030b 9217 plane_config->fb = intel_fb;
4c6baa59
JB
9218}
9219
0e8ffe1b 9220static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9221 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9222{
9223 struct drm_device *dev = crtc->base.dev;
9224 struct drm_i915_private *dev_priv = dev->dev_private;
9225 uint32_t tmp;
9226
f458ebbc
DV
9227 if (!intel_display_power_is_enabled(dev_priv,
9228 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
9229 return false;
9230
e143a21c 9231 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 9232 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 9233
0e8ffe1b
DV
9234 tmp = I915_READ(PIPECONF(crtc->pipe));
9235 if (!(tmp & PIPECONF_ENABLE))
9236 return false;
9237
42571aef
VS
9238 switch (tmp & PIPECONF_BPC_MASK) {
9239 case PIPECONF_6BPC:
9240 pipe_config->pipe_bpp = 18;
9241 break;
9242 case PIPECONF_8BPC:
9243 pipe_config->pipe_bpp = 24;
9244 break;
9245 case PIPECONF_10BPC:
9246 pipe_config->pipe_bpp = 30;
9247 break;
9248 case PIPECONF_12BPC:
9249 pipe_config->pipe_bpp = 36;
9250 break;
9251 default:
9252 break;
9253 }
9254
b5a9fa09
DV
9255 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9256 pipe_config->limited_color_range = true;
9257
ab9412ba 9258 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
9259 struct intel_shared_dpll *pll;
9260
88adfff1
DV
9261 pipe_config->has_pch_encoder = true;
9262
627eb5a3
DV
9263 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9264 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9265 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9266
9267 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9268
c0d43d62 9269 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
9270 pipe_config->shared_dpll =
9271 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9272 } else {
9273 tmp = I915_READ(PCH_DPLL_SEL);
9274 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9275 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9276 else
9277 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9278 }
66e985c0
DV
9279
9280 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9281
9282 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9283 &pipe_config->dpll_hw_state));
c93f54cf
DV
9284
9285 tmp = pipe_config->dpll_hw_state.dpll;
9286 pipe_config->pixel_multiplier =
9287 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9288 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9289
9290 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9291 } else {
9292 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9293 }
9294
1bd1bd80
DV
9295 intel_get_pipe_timings(crtc, pipe_config);
9296
2fa2fe9a
DV
9297 ironlake_get_pfit_config(crtc, pipe_config);
9298
0e8ffe1b
DV
9299 return true;
9300}
9301
be256dc7
PZ
9302static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9303{
9304 struct drm_device *dev = dev_priv->dev;
be256dc7 9305 struct intel_crtc *crtc;
be256dc7 9306
d3fcc808 9307 for_each_intel_crtc(dev, crtc)
e2c719b7 9308 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9309 pipe_name(crtc->pipe));
9310
e2c719b7
RC
9311 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9312 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9313 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9314 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9315 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9316 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9317 "CPU PWM1 enabled\n");
c5107b87 9318 if (IS_HASWELL(dev))
e2c719b7 9319 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9320 "CPU PWM2 enabled\n");
e2c719b7 9321 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9322 "PCH PWM1 enabled\n");
e2c719b7 9323 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9324 "Utility pin enabled\n");
e2c719b7 9325 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9326
9926ada1
PZ
9327 /*
9328 * In theory we can still leave IRQs enabled, as long as only the HPD
9329 * interrupts remain enabled. We used to check for that, but since it's
9330 * gen-specific and since we only disable LCPLL after we fully disable
9331 * the interrupts, the check below should be enough.
9332 */
e2c719b7 9333 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9334}
9335
9ccd5aeb
PZ
9336static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9337{
9338 struct drm_device *dev = dev_priv->dev;
9339
9340 if (IS_HASWELL(dev))
9341 return I915_READ(D_COMP_HSW);
9342 else
9343 return I915_READ(D_COMP_BDW);
9344}
9345
3c4c9b81
PZ
9346static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9347{
9348 struct drm_device *dev = dev_priv->dev;
9349
9350 if (IS_HASWELL(dev)) {
9351 mutex_lock(&dev_priv->rps.hw_lock);
9352 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9353 val))
f475dadf 9354 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9355 mutex_unlock(&dev_priv->rps.hw_lock);
9356 } else {
9ccd5aeb
PZ
9357 I915_WRITE(D_COMP_BDW, val);
9358 POSTING_READ(D_COMP_BDW);
3c4c9b81 9359 }
be256dc7
PZ
9360}
9361
9362/*
9363 * This function implements pieces of two sequences from BSpec:
9364 * - Sequence for display software to disable LCPLL
9365 * - Sequence for display software to allow package C8+
9366 * The steps implemented here are just the steps that actually touch the LCPLL
9367 * register. Callers should take care of disabling all the display engine
9368 * functions, doing the mode unset, fixing interrupts, etc.
9369 */
6ff58d53
PZ
9370static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9371 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9372{
9373 uint32_t val;
9374
9375 assert_can_disable_lcpll(dev_priv);
9376
9377 val = I915_READ(LCPLL_CTL);
9378
9379 if (switch_to_fclk) {
9380 val |= LCPLL_CD_SOURCE_FCLK;
9381 I915_WRITE(LCPLL_CTL, val);
9382
9383 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9384 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9385 DRM_ERROR("Switching to FCLK failed\n");
9386
9387 val = I915_READ(LCPLL_CTL);
9388 }
9389
9390 val |= LCPLL_PLL_DISABLE;
9391 I915_WRITE(LCPLL_CTL, val);
9392 POSTING_READ(LCPLL_CTL);
9393
9394 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9395 DRM_ERROR("LCPLL still locked\n");
9396
9ccd5aeb 9397 val = hsw_read_dcomp(dev_priv);
be256dc7 9398 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9399 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9400 ndelay(100);
9401
9ccd5aeb
PZ
9402 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9403 1))
be256dc7
PZ
9404 DRM_ERROR("D_COMP RCOMP still in progress\n");
9405
9406 if (allow_power_down) {
9407 val = I915_READ(LCPLL_CTL);
9408 val |= LCPLL_POWER_DOWN_ALLOW;
9409 I915_WRITE(LCPLL_CTL, val);
9410 POSTING_READ(LCPLL_CTL);
9411 }
9412}
9413
9414/*
9415 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9416 * source.
9417 */
6ff58d53 9418static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9419{
9420 uint32_t val;
9421
9422 val = I915_READ(LCPLL_CTL);
9423
9424 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9425 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9426 return;
9427
a8a8bd54
PZ
9428 /*
9429 * Make sure we're not on PC8 state before disabling PC8, otherwise
9430 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9431 */
59bad947 9432 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9433
be256dc7
PZ
9434 if (val & LCPLL_POWER_DOWN_ALLOW) {
9435 val &= ~LCPLL_POWER_DOWN_ALLOW;
9436 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9437 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9438 }
9439
9ccd5aeb 9440 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9441 val |= D_COMP_COMP_FORCE;
9442 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9443 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9444
9445 val = I915_READ(LCPLL_CTL);
9446 val &= ~LCPLL_PLL_DISABLE;
9447 I915_WRITE(LCPLL_CTL, val);
9448
9449 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9450 DRM_ERROR("LCPLL not locked yet\n");
9451
9452 if (val & LCPLL_CD_SOURCE_FCLK) {
9453 val = I915_READ(LCPLL_CTL);
9454 val &= ~LCPLL_CD_SOURCE_FCLK;
9455 I915_WRITE(LCPLL_CTL, val);
9456
9457 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9458 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9459 DRM_ERROR("Switching back to LCPLL failed\n");
9460 }
215733fa 9461
59bad947 9462 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9463 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9464}
9465
765dab67
PZ
9466/*
9467 * Package states C8 and deeper are really deep PC states that can only be
9468 * reached when all the devices on the system allow it, so even if the graphics
9469 * device allows PC8+, it doesn't mean the system will actually get to these
9470 * states. Our driver only allows PC8+ when going into runtime PM.
9471 *
9472 * The requirements for PC8+ are that all the outputs are disabled, the power
9473 * well is disabled and most interrupts are disabled, and these are also
9474 * requirements for runtime PM. When these conditions are met, we manually do
9475 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9476 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9477 * hang the machine.
9478 *
9479 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9480 * the state of some registers, so when we come back from PC8+ we need to
9481 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9482 * need to take care of the registers kept by RC6. Notice that this happens even
9483 * if we don't put the device in PCI D3 state (which is what currently happens
9484 * because of the runtime PM support).
9485 *
9486 * For more, read "Display Sequences for Package C8" on the hardware
9487 * documentation.
9488 */
a14cb6fc 9489void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9490{
c67a470b
PZ
9491 struct drm_device *dev = dev_priv->dev;
9492 uint32_t val;
9493
c67a470b
PZ
9494 DRM_DEBUG_KMS("Enabling package C8+\n");
9495
c67a470b
PZ
9496 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9497 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9498 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9499 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9500 }
9501
9502 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9503 hsw_disable_lcpll(dev_priv, true, true);
9504}
9505
a14cb6fc 9506void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9507{
9508 struct drm_device *dev = dev_priv->dev;
9509 uint32_t val;
9510
c67a470b
PZ
9511 DRM_DEBUG_KMS("Disabling package C8+\n");
9512
9513 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9514 lpt_init_pch_refclk(dev);
9515
9516 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9517 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9518 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9519 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9520 }
9521
9522 intel_prepare_ddi(dev);
c67a470b
PZ
9523}
9524
a821fc46 9525static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
f8437dd1 9526{
a821fc46 9527 struct drm_device *dev = old_state->dev;
f8437dd1 9528 struct drm_i915_private *dev_priv = dev->dev_private;
a821fc46 9529 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
f8437dd1
VK
9530 int req_cdclk;
9531
9532 /* see the comment in valleyview_modeset_global_resources */
9533 if (WARN_ON(max_pixclk < 0))
9534 return;
9535
9536 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9537
9538 if (req_cdclk != dev_priv->cdclk_freq)
9539 broxton_set_cdclk(dev, req_cdclk);
9540}
9541
b432e5cf
VS
9542/* compute the max rate for new configuration */
9543static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
9544{
9545 struct drm_device *dev = dev_priv->dev;
9546 struct intel_crtc *intel_crtc;
9547 struct drm_crtc *crtc;
9548 int max_pixel_rate = 0;
9549 int pixel_rate;
9550
9551 for_each_crtc(dev, crtc) {
9552 if (!crtc->state->enable)
9553 continue;
9554
9555 intel_crtc = to_intel_crtc(crtc);
9556 pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
9557
9558 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9559 if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
9560 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9561
9562 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9563 }
9564
9565 return max_pixel_rate;
9566}
9567
9568static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9569{
9570 struct drm_i915_private *dev_priv = dev->dev_private;
9571 uint32_t val, data;
9572 int ret;
9573
9574 if (WARN((I915_READ(LCPLL_CTL) &
9575 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9576 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9577 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9578 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9579 "trying to change cdclk frequency with cdclk not enabled\n"))
9580 return;
9581
9582 mutex_lock(&dev_priv->rps.hw_lock);
9583 ret = sandybridge_pcode_write(dev_priv,
9584 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9585 mutex_unlock(&dev_priv->rps.hw_lock);
9586 if (ret) {
9587 DRM_ERROR("failed to inform pcode about cdclk change\n");
9588 return;
9589 }
9590
9591 val = I915_READ(LCPLL_CTL);
9592 val |= LCPLL_CD_SOURCE_FCLK;
9593 I915_WRITE(LCPLL_CTL, val);
9594
9595 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9596 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9597 DRM_ERROR("Switching to FCLK failed\n");
9598
9599 val = I915_READ(LCPLL_CTL);
9600 val &= ~LCPLL_CLK_FREQ_MASK;
9601
9602 switch (cdclk) {
9603 case 450000:
9604 val |= LCPLL_CLK_FREQ_450;
9605 data = 0;
9606 break;
9607 case 540000:
9608 val |= LCPLL_CLK_FREQ_54O_BDW;
9609 data = 1;
9610 break;
9611 case 337500:
9612 val |= LCPLL_CLK_FREQ_337_5_BDW;
9613 data = 2;
9614 break;
9615 case 675000:
9616 val |= LCPLL_CLK_FREQ_675_BDW;
9617 data = 3;
9618 break;
9619 default:
9620 WARN(1, "invalid cdclk frequency\n");
9621 return;
9622 }
9623
9624 I915_WRITE(LCPLL_CTL, val);
9625
9626 val = I915_READ(LCPLL_CTL);
9627 val &= ~LCPLL_CD_SOURCE_FCLK;
9628 I915_WRITE(LCPLL_CTL, val);
9629
9630 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9631 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9632 DRM_ERROR("Switching back to LCPLL failed\n");
9633
9634 mutex_lock(&dev_priv->rps.hw_lock);
9635 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9636 mutex_unlock(&dev_priv->rps.hw_lock);
9637
9638 intel_update_cdclk(dev);
9639
9640 WARN(cdclk != dev_priv->cdclk_freq,
9641 "cdclk requested %d kHz but got %d kHz\n",
9642 cdclk, dev_priv->cdclk_freq);
9643}
9644
9645static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
9646 int max_pixel_rate)
9647{
9648 int cdclk;
9649
9650 /*
9651 * FIXME should also account for plane ratio
9652 * once 64bpp pixel formats are supported.
9653 */
9654 if (max_pixel_rate > 540000)
9655 cdclk = 675000;
9656 else if (max_pixel_rate > 450000)
9657 cdclk = 540000;
9658 else if (max_pixel_rate > 337500)
9659 cdclk = 450000;
9660 else
9661 cdclk = 337500;
9662
9663 /*
9664 * FIXME move the cdclk caclulation to
9665 * compute_config() so we can fail gracegully.
9666 */
9667 if (cdclk > dev_priv->max_cdclk_freq) {
9668 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9669 cdclk, dev_priv->max_cdclk_freq);
9670 cdclk = dev_priv->max_cdclk_freq;
9671 }
9672
9673 return cdclk;
9674}
9675
9676static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
9677{
9678 struct drm_i915_private *dev_priv = to_i915(state->dev);
9679 struct drm_crtc *crtc;
9680 struct drm_crtc_state *crtc_state;
9681 int max_pixclk = ilk_max_pixel_rate(dev_priv);
9682 int cdclk, i;
9683
9684 cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
9685
9686 if (cdclk == dev_priv->cdclk_freq)
9687 return 0;
9688
9689 /* add all active pipes to the state */
9690 for_each_crtc(state->dev, crtc) {
9691 if (!crtc->state->enable)
9692 continue;
9693
9694 crtc_state = drm_atomic_get_crtc_state(state, crtc);
9695 if (IS_ERR(crtc_state))
9696 return PTR_ERR(crtc_state);
9697 }
9698
9699 /* disable/enable all currently active pipes while we change cdclk */
9700 for_each_crtc_in_state(state, crtc, crtc_state, i)
9701 if (crtc_state->enable)
9702 crtc_state->mode_changed = true;
9703
9704 return 0;
9705}
9706
9707static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
9708{
9709 struct drm_device *dev = state->dev;
9710 struct drm_i915_private *dev_priv = dev->dev_private;
9711 int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
9712 int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
9713
9714 if (req_cdclk != dev_priv->cdclk_freq)
9715 broadwell_set_cdclk(dev, req_cdclk);
9716}
9717
190f68c5
ACO
9718static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9719 struct intel_crtc_state *crtc_state)
09b4ddf9 9720{
190f68c5 9721 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 9722 return -EINVAL;
716c2e55 9723
c7653199 9724 crtc->lowfreq_avail = false;
644cef34 9725
c8f7a0db 9726 return 0;
79e53945
JB
9727}
9728
3760b59c
S
9729static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9730 enum port port,
9731 struct intel_crtc_state *pipe_config)
9732{
9733 switch (port) {
9734 case PORT_A:
9735 pipe_config->ddi_pll_sel = SKL_DPLL0;
9736 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9737 break;
9738 case PORT_B:
9739 pipe_config->ddi_pll_sel = SKL_DPLL1;
9740 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9741 break;
9742 case PORT_C:
9743 pipe_config->ddi_pll_sel = SKL_DPLL2;
9744 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9745 break;
9746 default:
9747 DRM_ERROR("Incorrect port type\n");
9748 }
9749}
9750
96b7dfb7
S
9751static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9752 enum port port,
5cec258b 9753 struct intel_crtc_state *pipe_config)
96b7dfb7 9754{
3148ade7 9755 u32 temp, dpll_ctl1;
96b7dfb7
S
9756
9757 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9758 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9759
9760 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
9761 case SKL_DPLL0:
9762 /*
9763 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9764 * of the shared DPLL framework and thus needs to be read out
9765 * separately
9766 */
9767 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9768 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9769 break;
96b7dfb7
S
9770 case SKL_DPLL1:
9771 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9772 break;
9773 case SKL_DPLL2:
9774 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9775 break;
9776 case SKL_DPLL3:
9777 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9778 break;
96b7dfb7
S
9779 }
9780}
9781
7d2c8175
DL
9782static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9783 enum port port,
5cec258b 9784 struct intel_crtc_state *pipe_config)
7d2c8175
DL
9785{
9786 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9787
9788 switch (pipe_config->ddi_pll_sel) {
9789 case PORT_CLK_SEL_WRPLL1:
9790 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9791 break;
9792 case PORT_CLK_SEL_WRPLL2:
9793 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9794 break;
9795 }
9796}
9797
26804afd 9798static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9799 struct intel_crtc_state *pipe_config)
26804afd
DV
9800{
9801 struct drm_device *dev = crtc->base.dev;
9802 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9803 struct intel_shared_dpll *pll;
26804afd
DV
9804 enum port port;
9805 uint32_t tmp;
9806
9807 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9808
9809 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9810
96b7dfb7
S
9811 if (IS_SKYLAKE(dev))
9812 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9813 else if (IS_BROXTON(dev))
9814 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9815 else
9816 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9817
d452c5b6
DV
9818 if (pipe_config->shared_dpll >= 0) {
9819 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9820
9821 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9822 &pipe_config->dpll_hw_state));
9823 }
9824
26804afd
DV
9825 /*
9826 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9827 * DDI E. So just check whether this pipe is wired to DDI E and whether
9828 * the PCH transcoder is on.
9829 */
ca370455
DL
9830 if (INTEL_INFO(dev)->gen < 9 &&
9831 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9832 pipe_config->has_pch_encoder = true;
9833
9834 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9835 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9836 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9837
9838 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9839 }
9840}
9841
0e8ffe1b 9842static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9843 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9844{
9845 struct drm_device *dev = crtc->base.dev;
9846 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 9847 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
9848 uint32_t tmp;
9849
f458ebbc 9850 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
9851 POWER_DOMAIN_PIPE(crtc->pipe)))
9852 return false;
9853
e143a21c 9854 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
9855 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9856
eccb140b
DV
9857 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9858 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9859 enum pipe trans_edp_pipe;
9860 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9861 default:
9862 WARN(1, "unknown pipe linked to edp transcoder\n");
9863 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9864 case TRANS_DDI_EDP_INPUT_A_ON:
9865 trans_edp_pipe = PIPE_A;
9866 break;
9867 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9868 trans_edp_pipe = PIPE_B;
9869 break;
9870 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9871 trans_edp_pipe = PIPE_C;
9872 break;
9873 }
9874
9875 if (trans_edp_pipe == crtc->pipe)
9876 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9877 }
9878
f458ebbc 9879 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 9880 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
9881 return false;
9882
eccb140b 9883 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
9884 if (!(tmp & PIPECONF_ENABLE))
9885 return false;
9886
26804afd 9887 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9888
1bd1bd80
DV
9889 intel_get_pipe_timings(crtc, pipe_config);
9890
a1b2278e
CK
9891 if (INTEL_INFO(dev)->gen >= 9) {
9892 skl_init_scalers(dev, crtc, pipe_config);
9893 }
9894
2fa2fe9a 9895 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
af99ceda
CK
9896
9897 if (INTEL_INFO(dev)->gen >= 9) {
9898 pipe_config->scaler_state.scaler_id = -1;
9899 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9900 }
9901
bd2e244f 9902 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
ff6d9f55 9903 if (INTEL_INFO(dev)->gen == 9)
bd2e244f 9904 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9905 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 9906 ironlake_get_pfit_config(crtc, pipe_config);
ff6d9f55
JB
9907 else
9908 MISSING_CASE(INTEL_INFO(dev)->gen);
bd2e244f 9909 }
88adfff1 9910
e59150dc
JB
9911 if (IS_HASWELL(dev))
9912 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9913 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9914
ebb69c95
CT
9915 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9916 pipe_config->pixel_multiplier =
9917 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9918 } else {
9919 pipe_config->pixel_multiplier = 1;
9920 }
6c49f241 9921
0e8ffe1b
DV
9922 return true;
9923}
9924
560b85bb
CW
9925static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9926{
9927 struct drm_device *dev = crtc->dev;
9928 struct drm_i915_private *dev_priv = dev->dev_private;
9929 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9930 uint32_t cntl = 0, size = 0;
560b85bb 9931
dc41c154 9932 if (base) {
3dd512fb
MR
9933 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9934 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
9935 unsigned int stride = roundup_pow_of_two(width) * 4;
9936
9937 switch (stride) {
9938 default:
9939 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9940 width, stride);
9941 stride = 256;
9942 /* fallthrough */
9943 case 256:
9944 case 512:
9945 case 1024:
9946 case 2048:
9947 break;
4b0e333e
CW
9948 }
9949
dc41c154
VS
9950 cntl |= CURSOR_ENABLE |
9951 CURSOR_GAMMA_ENABLE |
9952 CURSOR_FORMAT_ARGB |
9953 CURSOR_STRIDE(stride);
9954
9955 size = (height << 12) | width;
4b0e333e 9956 }
560b85bb 9957
dc41c154
VS
9958 if (intel_crtc->cursor_cntl != 0 &&
9959 (intel_crtc->cursor_base != base ||
9960 intel_crtc->cursor_size != size ||
9961 intel_crtc->cursor_cntl != cntl)) {
9962 /* On these chipsets we can only modify the base/size/stride
9963 * whilst the cursor is disabled.
9964 */
9965 I915_WRITE(_CURACNTR, 0);
4b0e333e 9966 POSTING_READ(_CURACNTR);
dc41c154 9967 intel_crtc->cursor_cntl = 0;
4b0e333e 9968 }
560b85bb 9969
99d1f387 9970 if (intel_crtc->cursor_base != base) {
9db4a9c7 9971 I915_WRITE(_CURABASE, base);
99d1f387
VS
9972 intel_crtc->cursor_base = base;
9973 }
4726e0b0 9974
dc41c154
VS
9975 if (intel_crtc->cursor_size != size) {
9976 I915_WRITE(CURSIZE, size);
9977 intel_crtc->cursor_size = size;
4b0e333e 9978 }
560b85bb 9979
4b0e333e 9980 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
9981 I915_WRITE(_CURACNTR, cntl);
9982 POSTING_READ(_CURACNTR);
4b0e333e 9983 intel_crtc->cursor_cntl = cntl;
560b85bb 9984 }
560b85bb
CW
9985}
9986
560b85bb 9987static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
9988{
9989 struct drm_device *dev = crtc->dev;
9990 struct drm_i915_private *dev_priv = dev->dev_private;
9991 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9992 int pipe = intel_crtc->pipe;
4b0e333e
CW
9993 uint32_t cntl;
9994
9995 cntl = 0;
9996 if (base) {
9997 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 9998 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
9999 case 64:
10000 cntl |= CURSOR_MODE_64_ARGB_AX;
10001 break;
10002 case 128:
10003 cntl |= CURSOR_MODE_128_ARGB_AX;
10004 break;
10005 case 256:
10006 cntl |= CURSOR_MODE_256_ARGB_AX;
10007 break;
10008 default:
3dd512fb 10009 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 10010 return;
65a21cd6 10011 }
4b0e333e 10012 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
10013
10014 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
10015 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 10016 }
65a21cd6 10017
8e7d688b 10018 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
10019 cntl |= CURSOR_ROTATE_180;
10020
4b0e333e
CW
10021 if (intel_crtc->cursor_cntl != cntl) {
10022 I915_WRITE(CURCNTR(pipe), cntl);
10023 POSTING_READ(CURCNTR(pipe));
10024 intel_crtc->cursor_cntl = cntl;
65a21cd6 10025 }
4b0e333e 10026
65a21cd6 10027 /* and commit changes on next vblank */
5efb3e28
VS
10028 I915_WRITE(CURBASE(pipe), base);
10029 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10030
10031 intel_crtc->cursor_base = base;
65a21cd6
JB
10032}
10033
cda4b7d3 10034/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
10035static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10036 bool on)
cda4b7d3
CW
10037{
10038 struct drm_device *dev = crtc->dev;
10039 struct drm_i915_private *dev_priv = dev->dev_private;
10040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10041 int pipe = intel_crtc->pipe;
3d7d6510
MR
10042 int x = crtc->cursor_x;
10043 int y = crtc->cursor_y;
d6e4db15 10044 u32 base = 0, pos = 0;
cda4b7d3 10045
d6e4db15 10046 if (on)
cda4b7d3 10047 base = intel_crtc->cursor_addr;
cda4b7d3 10048
6e3c9717 10049 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
10050 base = 0;
10051
6e3c9717 10052 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
10053 base = 0;
10054
10055 if (x < 0) {
3dd512fb 10056 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
cda4b7d3
CW
10057 base = 0;
10058
10059 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10060 x = -x;
10061 }
10062 pos |= x << CURSOR_X_SHIFT;
10063
10064 if (y < 0) {
3dd512fb 10065 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
cda4b7d3
CW
10066 base = 0;
10067
10068 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10069 y = -y;
10070 }
10071 pos |= y << CURSOR_Y_SHIFT;
10072
4b0e333e 10073 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
10074 return;
10075
5efb3e28
VS
10076 I915_WRITE(CURPOS(pipe), pos);
10077
4398ad45
VS
10078 /* ILK+ do this automagically */
10079 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 10080 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
3dd512fb
MR
10081 base += (intel_crtc->base.cursor->state->crtc_h *
10082 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
4398ad45
VS
10083 }
10084
8ac54669 10085 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
10086 i845_update_cursor(crtc, base);
10087 else
10088 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
10089}
10090
dc41c154
VS
10091static bool cursor_size_ok(struct drm_device *dev,
10092 uint32_t width, uint32_t height)
10093{
10094 if (width == 0 || height == 0)
10095 return false;
10096
10097 /*
10098 * 845g/865g are special in that they are only limited by
10099 * the width of their cursors, the height is arbitrary up to
10100 * the precision of the register. Everything else requires
10101 * square cursors, limited to a few power-of-two sizes.
10102 */
10103 if (IS_845G(dev) || IS_I865G(dev)) {
10104 if ((width & 63) != 0)
10105 return false;
10106
10107 if (width > (IS_845G(dev) ? 64 : 512))
10108 return false;
10109
10110 if (height > 1023)
10111 return false;
10112 } else {
10113 switch (width | height) {
10114 case 256:
10115 case 128:
10116 if (IS_GEN2(dev))
10117 return false;
10118 case 64:
10119 break;
10120 default:
10121 return false;
10122 }
10123 }
10124
10125 return true;
10126}
10127
79e53945 10128static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 10129 u16 *blue, uint32_t start, uint32_t size)
79e53945 10130{
7203425a 10131 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 10132 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 10133
7203425a 10134 for (i = start; i < end; i++) {
79e53945
JB
10135 intel_crtc->lut_r[i] = red[i] >> 8;
10136 intel_crtc->lut_g[i] = green[i] >> 8;
10137 intel_crtc->lut_b[i] = blue[i] >> 8;
10138 }
10139
10140 intel_crtc_load_lut(crtc);
10141}
10142
79e53945
JB
10143/* VESA 640x480x72Hz mode to set on the pipe */
10144static struct drm_display_mode load_detect_mode = {
10145 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10146 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10147};
10148
a8bb6818
DV
10149struct drm_framebuffer *
10150__intel_framebuffer_create(struct drm_device *dev,
10151 struct drm_mode_fb_cmd2 *mode_cmd,
10152 struct drm_i915_gem_object *obj)
d2dff872
CW
10153{
10154 struct intel_framebuffer *intel_fb;
10155 int ret;
10156
10157 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10158 if (!intel_fb) {
6ccb81f2 10159 drm_gem_object_unreference(&obj->base);
d2dff872
CW
10160 return ERR_PTR(-ENOMEM);
10161 }
10162
10163 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10164 if (ret)
10165 goto err;
d2dff872
CW
10166
10167 return &intel_fb->base;
dd4916c5 10168err:
6ccb81f2 10169 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
10170 kfree(intel_fb);
10171
10172 return ERR_PTR(ret);
d2dff872
CW
10173}
10174
b5ea642a 10175static struct drm_framebuffer *
a8bb6818
DV
10176intel_framebuffer_create(struct drm_device *dev,
10177 struct drm_mode_fb_cmd2 *mode_cmd,
10178 struct drm_i915_gem_object *obj)
10179{
10180 struct drm_framebuffer *fb;
10181 int ret;
10182
10183 ret = i915_mutex_lock_interruptible(dev);
10184 if (ret)
10185 return ERR_PTR(ret);
10186 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10187 mutex_unlock(&dev->struct_mutex);
10188
10189 return fb;
10190}
10191
d2dff872
CW
10192static u32
10193intel_framebuffer_pitch_for_width(int width, int bpp)
10194{
10195 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10196 return ALIGN(pitch, 64);
10197}
10198
10199static u32
10200intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10201{
10202 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10203 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10204}
10205
10206static struct drm_framebuffer *
10207intel_framebuffer_create_for_mode(struct drm_device *dev,
10208 struct drm_display_mode *mode,
10209 int depth, int bpp)
10210{
10211 struct drm_i915_gem_object *obj;
0fed39bd 10212 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10213
10214 obj = i915_gem_alloc_object(dev,
10215 intel_framebuffer_size_for_mode(mode, bpp));
10216 if (obj == NULL)
10217 return ERR_PTR(-ENOMEM);
10218
10219 mode_cmd.width = mode->hdisplay;
10220 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10221 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10222 bpp);
5ca0c34a 10223 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
10224
10225 return intel_framebuffer_create(dev, &mode_cmd, obj);
10226}
10227
10228static struct drm_framebuffer *
10229mode_fits_in_fbdev(struct drm_device *dev,
10230 struct drm_display_mode *mode)
10231{
4520f53a 10232#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
10233 struct drm_i915_private *dev_priv = dev->dev_private;
10234 struct drm_i915_gem_object *obj;
10235 struct drm_framebuffer *fb;
10236
4c0e5528 10237 if (!dev_priv->fbdev)
d2dff872
CW
10238 return NULL;
10239
4c0e5528 10240 if (!dev_priv->fbdev->fb)
d2dff872
CW
10241 return NULL;
10242
4c0e5528
DV
10243 obj = dev_priv->fbdev->fb->obj;
10244 BUG_ON(!obj);
10245
8bcd4553 10246 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10247 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10248 fb->bits_per_pixel))
d2dff872
CW
10249 return NULL;
10250
01f2c773 10251 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10252 return NULL;
10253
10254 return fb;
4520f53a
DV
10255#else
10256 return NULL;
10257#endif
d2dff872
CW
10258}
10259
d3a40d1b
ACO
10260static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10261 struct drm_crtc *crtc,
10262 struct drm_display_mode *mode,
10263 struct drm_framebuffer *fb,
10264 int x, int y)
10265{
10266 struct drm_plane_state *plane_state;
10267 int hdisplay, vdisplay;
10268 int ret;
10269
10270 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10271 if (IS_ERR(plane_state))
10272 return PTR_ERR(plane_state);
10273
10274 if (mode)
10275 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10276 else
10277 hdisplay = vdisplay = 0;
10278
10279 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10280 if (ret)
10281 return ret;
10282 drm_atomic_set_fb_for_plane(plane_state, fb);
10283 plane_state->crtc_x = 0;
10284 plane_state->crtc_y = 0;
10285 plane_state->crtc_w = hdisplay;
10286 plane_state->crtc_h = vdisplay;
10287 plane_state->src_x = x << 16;
10288 plane_state->src_y = y << 16;
10289 plane_state->src_w = hdisplay << 16;
10290 plane_state->src_h = vdisplay << 16;
10291
10292 return 0;
10293}
10294
d2434ab7 10295bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10296 struct drm_display_mode *mode,
51fd371b
RC
10297 struct intel_load_detect_pipe *old,
10298 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10299{
10300 struct intel_crtc *intel_crtc;
d2434ab7
DV
10301 struct intel_encoder *intel_encoder =
10302 intel_attached_encoder(connector);
79e53945 10303 struct drm_crtc *possible_crtc;
4ef69c7a 10304 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10305 struct drm_crtc *crtc = NULL;
10306 struct drm_device *dev = encoder->dev;
94352cf9 10307 struct drm_framebuffer *fb;
51fd371b 10308 struct drm_mode_config *config = &dev->mode_config;
83a57153 10309 struct drm_atomic_state *state = NULL;
944b0c76 10310 struct drm_connector_state *connector_state;
4be07317 10311 struct intel_crtc_state *crtc_state;
51fd371b 10312 int ret, i = -1;
79e53945 10313
d2dff872 10314 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10315 connector->base.id, connector->name,
8e329a03 10316 encoder->base.id, encoder->name);
d2dff872 10317
51fd371b
RC
10318retry:
10319 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10320 if (ret)
10321 goto fail_unlock;
6e9f798d 10322
79e53945
JB
10323 /*
10324 * Algorithm gets a little messy:
7a5e4805 10325 *
79e53945
JB
10326 * - if the connector already has an assigned crtc, use it (but make
10327 * sure it's on first)
7a5e4805 10328 *
79e53945
JB
10329 * - try to find the first unused crtc that can drive this connector,
10330 * and use that if we find one
79e53945
JB
10331 */
10332
10333 /* See if we already have a CRTC for this connector */
10334 if (encoder->crtc) {
10335 crtc = encoder->crtc;
8261b191 10336
51fd371b 10337 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de
DV
10338 if (ret)
10339 goto fail_unlock;
10340 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b
RC
10341 if (ret)
10342 goto fail_unlock;
7b24056b 10343
24218aac 10344 old->dpms_mode = connector->dpms;
8261b191
CW
10345 old->load_detect_temp = false;
10346
10347 /* Make sure the crtc and connector are running */
24218aac
DV
10348 if (connector->dpms != DRM_MODE_DPMS_ON)
10349 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 10350
7173188d 10351 return true;
79e53945
JB
10352 }
10353
10354 /* Find an unused one (if possible) */
70e1e0ec 10355 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10356 i++;
10357 if (!(encoder->possible_crtcs & (1 << i)))
10358 continue;
83d65738 10359 if (possible_crtc->state->enable)
a459249c
VS
10360 continue;
10361 /* This can occur when applying the pipe A quirk on resume. */
10362 if (to_intel_crtc(possible_crtc)->new_enabled)
10363 continue;
10364
10365 crtc = possible_crtc;
10366 break;
79e53945
JB
10367 }
10368
10369 /*
10370 * If we didn't find an unused CRTC, don't use any.
10371 */
10372 if (!crtc) {
7173188d 10373 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 10374 goto fail_unlock;
79e53945
JB
10375 }
10376
51fd371b
RC
10377 ret = drm_modeset_lock(&crtc->mutex, ctx);
10378 if (ret)
4d02e2de
DV
10379 goto fail_unlock;
10380 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10381 if (ret)
51fd371b 10382 goto fail_unlock;
fc303101
DV
10383 intel_encoder->new_crtc = to_intel_crtc(crtc);
10384 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
10385
10386 intel_crtc = to_intel_crtc(crtc);
412b61d8 10387 intel_crtc->new_enabled = true;
24218aac 10388 old->dpms_mode = connector->dpms;
8261b191 10389 old->load_detect_temp = true;
d2dff872 10390 old->release_fb = NULL;
79e53945 10391
83a57153
ACO
10392 state = drm_atomic_state_alloc(dev);
10393 if (!state)
10394 return false;
10395
10396 state->acquire_ctx = ctx;
10397
944b0c76
ACO
10398 connector_state = drm_atomic_get_connector_state(state, connector);
10399 if (IS_ERR(connector_state)) {
10400 ret = PTR_ERR(connector_state);
10401 goto fail;
10402 }
10403
10404 connector_state->crtc = crtc;
10405 connector_state->best_encoder = &intel_encoder->base;
10406
4be07317
ACO
10407 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10408 if (IS_ERR(crtc_state)) {
10409 ret = PTR_ERR(crtc_state);
10410 goto fail;
10411 }
10412
49d6fa21 10413 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10414
6492711d
CW
10415 if (!mode)
10416 mode = &load_detect_mode;
79e53945 10417
d2dff872
CW
10418 /* We need a framebuffer large enough to accommodate all accesses
10419 * that the plane may generate whilst we perform load detection.
10420 * We can not rely on the fbcon either being present (we get called
10421 * during its initialisation to detect all boot displays, or it may
10422 * not even exist) or that it is large enough to satisfy the
10423 * requested mode.
10424 */
94352cf9
DV
10425 fb = mode_fits_in_fbdev(dev, mode);
10426 if (fb == NULL) {
d2dff872 10427 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
10428 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10429 old->release_fb = fb;
d2dff872
CW
10430 } else
10431 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10432 if (IS_ERR(fb)) {
d2dff872 10433 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10434 goto fail;
79e53945 10435 }
79e53945 10436
d3a40d1b
ACO
10437 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10438 if (ret)
10439 goto fail;
10440
8c7b5ccb
ACO
10441 drm_mode_copy(&crtc_state->base.mode, mode);
10442
568c634a 10443 if (intel_set_mode(state)) {
6492711d 10444 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
10445 if (old->release_fb)
10446 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 10447 goto fail;
79e53945 10448 }
9128b040 10449 crtc->primary->crtc = crtc;
7173188d 10450
79e53945 10451 /* let the connector get through one full cycle before testing */
9d0498a2 10452 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10453 return true;
412b61d8
VS
10454
10455 fail:
83d65738 10456 intel_crtc->new_enabled = crtc->state->enable;
51fd371b 10457fail_unlock:
e5d958ef
ACO
10458 drm_atomic_state_free(state);
10459 state = NULL;
83a57153 10460
51fd371b
RC
10461 if (ret == -EDEADLK) {
10462 drm_modeset_backoff(ctx);
10463 goto retry;
10464 }
10465
412b61d8 10466 return false;
79e53945
JB
10467}
10468
d2434ab7 10469void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10470 struct intel_load_detect_pipe *old,
10471 struct drm_modeset_acquire_ctx *ctx)
79e53945 10472{
83a57153 10473 struct drm_device *dev = connector->dev;
d2434ab7
DV
10474 struct intel_encoder *intel_encoder =
10475 intel_attached_encoder(connector);
4ef69c7a 10476 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 10477 struct drm_crtc *crtc = encoder->crtc;
412b61d8 10478 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83a57153 10479 struct drm_atomic_state *state;
944b0c76 10480 struct drm_connector_state *connector_state;
4be07317 10481 struct intel_crtc_state *crtc_state;
d3a40d1b 10482 int ret;
79e53945 10483
d2dff872 10484 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10485 connector->base.id, connector->name,
8e329a03 10486 encoder->base.id, encoder->name);
d2dff872 10487
8261b191 10488 if (old->load_detect_temp) {
83a57153 10489 state = drm_atomic_state_alloc(dev);
944b0c76
ACO
10490 if (!state)
10491 goto fail;
83a57153
ACO
10492
10493 state->acquire_ctx = ctx;
10494
944b0c76
ACO
10495 connector_state = drm_atomic_get_connector_state(state, connector);
10496 if (IS_ERR(connector_state))
10497 goto fail;
10498
4be07317
ACO
10499 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10500 if (IS_ERR(crtc_state))
10501 goto fail;
10502
fc303101
DV
10503 to_intel_connector(connector)->new_encoder = NULL;
10504 intel_encoder->new_crtc = NULL;
412b61d8 10505 intel_crtc->new_enabled = false;
944b0c76
ACO
10506
10507 connector_state->best_encoder = NULL;
10508 connector_state->crtc = NULL;
10509
49d6fa21 10510 crtc_state->base.enable = crtc_state->base.active = false;
4be07317 10511
d3a40d1b
ACO
10512 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10513 0, 0);
10514 if (ret)
10515 goto fail;
10516
568c634a 10517 ret = intel_set_mode(state);
2bfb4627
ACO
10518 if (ret)
10519 goto fail;
d2dff872 10520
36206361
DV
10521 if (old->release_fb) {
10522 drm_framebuffer_unregister_private(old->release_fb);
10523 drm_framebuffer_unreference(old->release_fb);
10524 }
d2dff872 10525
0622a53c 10526 return;
79e53945
JB
10527 }
10528
c751ce4f 10529 /* Switch crtc and encoder back off if necessary */
24218aac
DV
10530 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10531 connector->funcs->dpms(connector, old->dpms_mode);
944b0c76
ACO
10532
10533 return;
10534fail:
10535 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10536 drm_atomic_state_free(state);
79e53945
JB
10537}
10538
da4a1efa 10539static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10540 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10541{
10542 struct drm_i915_private *dev_priv = dev->dev_private;
10543 u32 dpll = pipe_config->dpll_hw_state.dpll;
10544
10545 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10546 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10547 else if (HAS_PCH_SPLIT(dev))
10548 return 120000;
10549 else if (!IS_GEN2(dev))
10550 return 96000;
10551 else
10552 return 48000;
10553}
10554
79e53945 10555/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10556static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10557 struct intel_crtc_state *pipe_config)
79e53945 10558{
f1f644dc 10559 struct drm_device *dev = crtc->base.dev;
79e53945 10560 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10561 int pipe = pipe_config->cpu_transcoder;
293623f7 10562 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10563 u32 fp;
10564 intel_clock_t clock;
da4a1efa 10565 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10566
10567 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10568 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10569 else
293623f7 10570 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10571
10572 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10573 if (IS_PINEVIEW(dev)) {
10574 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10575 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10576 } else {
10577 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10578 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10579 }
10580
a6c45cf0 10581 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10582 if (IS_PINEVIEW(dev))
10583 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10584 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10585 else
10586 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10587 DPLL_FPA01_P1_POST_DIV_SHIFT);
10588
10589 switch (dpll & DPLL_MODE_MASK) {
10590 case DPLLB_MODE_DAC_SERIAL:
10591 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10592 5 : 10;
10593 break;
10594 case DPLLB_MODE_LVDS:
10595 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10596 7 : 14;
10597 break;
10598 default:
28c97730 10599 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10600 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10601 return;
79e53945
JB
10602 }
10603
ac58c3f0 10604 if (IS_PINEVIEW(dev))
da4a1efa 10605 pineview_clock(refclk, &clock);
ac58c3f0 10606 else
da4a1efa 10607 i9xx_clock(refclk, &clock);
79e53945 10608 } else {
0fb58223 10609 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10610 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10611
10612 if (is_lvds) {
10613 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10614 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10615
10616 if (lvds & LVDS_CLKB_POWER_UP)
10617 clock.p2 = 7;
10618 else
10619 clock.p2 = 14;
79e53945
JB
10620 } else {
10621 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10622 clock.p1 = 2;
10623 else {
10624 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10625 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10626 }
10627 if (dpll & PLL_P2_DIVIDE_BY_4)
10628 clock.p2 = 4;
10629 else
10630 clock.p2 = 2;
79e53945 10631 }
da4a1efa
VS
10632
10633 i9xx_clock(refclk, &clock);
79e53945
JB
10634 }
10635
18442d08
VS
10636 /*
10637 * This value includes pixel_multiplier. We will use
241bfc38 10638 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10639 * encoder's get_config() function.
10640 */
10641 pipe_config->port_clock = clock.dot;
f1f644dc
JB
10642}
10643
6878da05
VS
10644int intel_dotclock_calculate(int link_freq,
10645 const struct intel_link_m_n *m_n)
f1f644dc 10646{
f1f644dc
JB
10647 /*
10648 * The calculation for the data clock is:
1041a02f 10649 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10650 * But we want to avoid losing precison if possible, so:
1041a02f 10651 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10652 *
10653 * and the link clock is simpler:
1041a02f 10654 * link_clock = (m * link_clock) / n
f1f644dc
JB
10655 */
10656
6878da05
VS
10657 if (!m_n->link_n)
10658 return 0;
f1f644dc 10659
6878da05
VS
10660 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10661}
f1f644dc 10662
18442d08 10663static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10664 struct intel_crtc_state *pipe_config)
6878da05
VS
10665{
10666 struct drm_device *dev = crtc->base.dev;
79e53945 10667
18442d08
VS
10668 /* read out port_clock from the DPLL */
10669 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10670
f1f644dc 10671 /*
18442d08 10672 * This value does not include pixel_multiplier.
241bfc38 10673 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
10674 * agree once we know their relationship in the encoder's
10675 * get_config() function.
79e53945 10676 */
2d112de7 10677 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
10678 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10679 &pipe_config->fdi_m_n);
79e53945
JB
10680}
10681
10682/** Returns the currently programmed mode of the given pipe. */
10683struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10684 struct drm_crtc *crtc)
10685{
548f245b 10686 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10687 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10688 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10689 struct drm_display_mode *mode;
5cec258b 10690 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
10691 int htot = I915_READ(HTOTAL(cpu_transcoder));
10692 int hsync = I915_READ(HSYNC(cpu_transcoder));
10693 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10694 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10695 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10696
10697 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10698 if (!mode)
10699 return NULL;
10700
f1f644dc
JB
10701 /*
10702 * Construct a pipe_config sufficient for getting the clock info
10703 * back out of crtc_clock_get.
10704 *
10705 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10706 * to use a real value here instead.
10707 */
293623f7 10708 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 10709 pipe_config.pixel_multiplier = 1;
293623f7
VS
10710 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10711 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10712 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
10713 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10714
773ae034 10715 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
10716 mode->hdisplay = (htot & 0xffff) + 1;
10717 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10718 mode->hsync_start = (hsync & 0xffff) + 1;
10719 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10720 mode->vdisplay = (vtot & 0xffff) + 1;
10721 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10722 mode->vsync_start = (vsync & 0xffff) + 1;
10723 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10724
10725 drm_mode_set_name(mode);
79e53945
JB
10726
10727 return mode;
10728}
10729
652c393a
JB
10730static void intel_decrease_pllclock(struct drm_crtc *crtc)
10731{
10732 struct drm_device *dev = crtc->dev;
fbee40df 10733 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10734 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 10735
baff296c 10736 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
10737 return;
10738
10739 if (!dev_priv->lvds_downclock_avail)
10740 return;
10741
10742 /*
10743 * Since this is called by a timer, we should never get here in
10744 * the manual case.
10745 */
10746 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
10747 int pipe = intel_crtc->pipe;
10748 int dpll_reg = DPLL(pipe);
10749 int dpll;
f6e5b160 10750
44d98a61 10751 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 10752
8ac5a6d5 10753 assert_panel_unlocked(dev_priv, pipe);
652c393a 10754
dc257cf1 10755 dpll = I915_READ(dpll_reg);
652c393a
JB
10756 dpll |= DISPLAY_RATE_SELECT_FPA1;
10757 I915_WRITE(dpll_reg, dpll);
9d0498a2 10758 intel_wait_for_vblank(dev, pipe);
652c393a
JB
10759 dpll = I915_READ(dpll_reg);
10760 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 10761 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
10762 }
10763
10764}
10765
f047e395
CW
10766void intel_mark_busy(struct drm_device *dev)
10767{
c67a470b
PZ
10768 struct drm_i915_private *dev_priv = dev->dev_private;
10769
f62a0076
CW
10770 if (dev_priv->mm.busy)
10771 return;
10772
43694d69 10773 intel_runtime_pm_get(dev_priv);
c67a470b 10774 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10775 if (INTEL_INFO(dev)->gen >= 6)
10776 gen6_rps_busy(dev_priv);
f62a0076 10777 dev_priv->mm.busy = true;
f047e395
CW
10778}
10779
10780void intel_mark_idle(struct drm_device *dev)
652c393a 10781{
c67a470b 10782 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10783 struct drm_crtc *crtc;
652c393a 10784
f62a0076
CW
10785 if (!dev_priv->mm.busy)
10786 return;
10787
10788 dev_priv->mm.busy = false;
10789
70e1e0ec 10790 for_each_crtc(dev, crtc) {
f4510a27 10791 if (!crtc->primary->fb)
652c393a
JB
10792 continue;
10793
725a5b54 10794 intel_decrease_pllclock(crtc);
652c393a 10795 }
b29c19b6 10796
3d13ef2e 10797 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10798 gen6_rps_idle(dev->dev_private);
bb4cdd53 10799
43694d69 10800 intel_runtime_pm_put(dev_priv);
652c393a
JB
10801}
10802
79e53945
JB
10803static void intel_crtc_destroy(struct drm_crtc *crtc)
10804{
10805 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10806 struct drm_device *dev = crtc->dev;
10807 struct intel_unpin_work *work;
67e77c5a 10808
5e2d7afc 10809 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10810 work = intel_crtc->unpin_work;
10811 intel_crtc->unpin_work = NULL;
5e2d7afc 10812 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10813
10814 if (work) {
10815 cancel_work_sync(&work->work);
10816 kfree(work);
10817 }
79e53945
JB
10818
10819 drm_crtc_cleanup(crtc);
67e77c5a 10820
79e53945
JB
10821 kfree(intel_crtc);
10822}
10823
6b95a207
KH
10824static void intel_unpin_work_fn(struct work_struct *__work)
10825{
10826 struct intel_unpin_work *work =
10827 container_of(__work, struct intel_unpin_work, work);
b4a98e57 10828 struct drm_device *dev = work->crtc->dev;
f99d7069 10829 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 10830
b4a98e57 10831 mutex_lock(&dev->struct_mutex);
82bc3b2d 10832 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
05394f39 10833 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10834
7ff0ebcc 10835 intel_fbc_update(dev);
f06cc1b9
JH
10836
10837 if (work->flip_queued_req)
146d84f0 10838 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10839 mutex_unlock(&dev->struct_mutex);
10840
f99d7069 10841 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
89ed88ba 10842 drm_framebuffer_unreference(work->old_fb);
f99d7069 10843
b4a98e57
CW
10844 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10845 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10846
6b95a207
KH
10847 kfree(work);
10848}
10849
1afe3e9d 10850static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10851 struct drm_crtc *crtc)
6b95a207 10852{
6b95a207
KH
10853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10854 struct intel_unpin_work *work;
6b95a207
KH
10855 unsigned long flags;
10856
10857 /* Ignore early vblank irqs */
10858 if (intel_crtc == NULL)
10859 return;
10860
f326038a
DV
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 */
6b95a207
KH
10865 spin_lock_irqsave(&dev->event_lock, flags);
10866 work = intel_crtc->unpin_work;
e7d841ca
CW
10867
10868 /* Ensure we don't miss a work->pending update ... */
10869 smp_rmb();
10870
10871 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10872 spin_unlock_irqrestore(&dev->event_lock, flags);
10873 return;
10874 }
10875
d6bbafa1 10876 page_flip_completed(intel_crtc);
0af7e4df 10877
6b95a207 10878 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10879}
10880
1afe3e9d
JB
10881void intel_finish_page_flip(struct drm_device *dev, int pipe)
10882{
fbee40df 10883 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10884 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10885
49b14a5c 10886 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10887}
10888
10889void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10890{
fbee40df 10891 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10892 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10893
49b14a5c 10894 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10895}
10896
75f7f3ec
VS
10897/* Is 'a' after or equal to 'b'? */
10898static bool g4x_flip_count_after_eq(u32 a, u32 b)
10899{
10900 return !((a - b) & 0x80000000);
10901}
10902
10903static bool page_flip_finished(struct intel_crtc *crtc)
10904{
10905 struct drm_device *dev = crtc->base.dev;
10906 struct drm_i915_private *dev_priv = dev->dev_private;
10907
bdfa7542
VS
10908 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10909 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10910 return true;
10911
75f7f3ec
VS
10912 /*
10913 * The relevant registers doen't exist on pre-ctg.
10914 * As the flip done interrupt doesn't trigger for mmio
10915 * flips on gmch platforms, a flip count check isn't
10916 * really needed there. But since ctg has the registers,
10917 * include it in the check anyway.
10918 */
10919 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10920 return true;
10921
10922 /*
10923 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10924 * used the same base address. In that case the mmio flip might
10925 * have completed, but the CS hasn't even executed the flip yet.
10926 *
10927 * A flip count check isn't enough as the CS might have updated
10928 * the base address just after start of vblank, but before we
10929 * managed to process the interrupt. This means we'd complete the
10930 * CS flip too soon.
10931 *
10932 * Combining both checks should get us a good enough result. It may
10933 * still happen that the CS flip has been executed, but has not
10934 * yet actually completed. But in case the base address is the same
10935 * anyway, we don't really care.
10936 */
10937 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10938 crtc->unpin_work->gtt_offset &&
10939 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10940 crtc->unpin_work->flip_count);
10941}
10942
6b95a207
KH
10943void intel_prepare_page_flip(struct drm_device *dev, int plane)
10944{
fbee40df 10945 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10946 struct intel_crtc *intel_crtc =
10947 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10948 unsigned long flags;
10949
f326038a
DV
10950
10951 /*
10952 * This is called both by irq handlers and the reset code (to complete
10953 * lost pageflips) so needs the full irqsave spinlocks.
10954 *
10955 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10956 * generate a page-flip completion irq, i.e. every modeset
10957 * is also accompanied by a spurious intel_prepare_page_flip().
10958 */
6b95a207 10959 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10960 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10961 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10962 spin_unlock_irqrestore(&dev->event_lock, flags);
10963}
10964
eba905b2 10965static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
10966{
10967 /* Ensure that the work item is consistent when activating it ... */
10968 smp_wmb();
10969 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10970 /* and that it is marked active as soon as the irq could fire. */
10971 smp_wmb();
10972}
10973
8c9f3aaf
JB
10974static int intel_gen2_queue_flip(struct drm_device *dev,
10975 struct drm_crtc *crtc,
10976 struct drm_framebuffer *fb,
ed8d1975 10977 struct drm_i915_gem_object *obj,
a4872ba6 10978 struct intel_engine_cs *ring,
ed8d1975 10979 uint32_t flags)
8c9f3aaf 10980{
8c9f3aaf 10981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10982 u32 flip_mask;
10983 int ret;
10984
6d90c952 10985 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10986 if (ret)
4fa62c89 10987 return ret;
8c9f3aaf
JB
10988
10989 /* Can't queue multiple flips, so wait for the previous
10990 * one to finish before executing the next.
10991 */
10992 if (intel_crtc->plane)
10993 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10994 else
10995 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10996 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10997 intel_ring_emit(ring, MI_NOOP);
10998 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10999 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11000 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 11001 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 11002 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
11003
11004 intel_mark_page_flip_active(intel_crtc);
09246732 11005 __intel_ring_advance(ring);
83d4092b 11006 return 0;
8c9f3aaf
JB
11007}
11008
11009static int intel_gen3_queue_flip(struct drm_device *dev,
11010 struct drm_crtc *crtc,
11011 struct drm_framebuffer *fb,
ed8d1975 11012 struct drm_i915_gem_object *obj,
a4872ba6 11013 struct intel_engine_cs *ring,
ed8d1975 11014 uint32_t flags)
8c9f3aaf 11015{
8c9f3aaf 11016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
11017 u32 flip_mask;
11018 int ret;
11019
6d90c952 11020 ret = intel_ring_begin(ring, 6);
8c9f3aaf 11021 if (ret)
4fa62c89 11022 return ret;
8c9f3aaf
JB
11023
11024 if (intel_crtc->plane)
11025 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11026 else
11027 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
11028 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11029 intel_ring_emit(ring, MI_NOOP);
11030 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11031 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11032 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 11033 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
11034 intel_ring_emit(ring, MI_NOOP);
11035
e7d841ca 11036 intel_mark_page_flip_active(intel_crtc);
09246732 11037 __intel_ring_advance(ring);
83d4092b 11038 return 0;
8c9f3aaf
JB
11039}
11040
11041static int intel_gen4_queue_flip(struct drm_device *dev,
11042 struct drm_crtc *crtc,
11043 struct drm_framebuffer *fb,
ed8d1975 11044 struct drm_i915_gem_object *obj,
a4872ba6 11045 struct intel_engine_cs *ring,
ed8d1975 11046 uint32_t flags)
8c9f3aaf
JB
11047{
11048 struct drm_i915_private *dev_priv = dev->dev_private;
11049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11050 uint32_t pf, pipesrc;
11051 int ret;
11052
6d90c952 11053 ret = intel_ring_begin(ring, 4);
8c9f3aaf 11054 if (ret)
4fa62c89 11055 return ret;
8c9f3aaf
JB
11056
11057 /* i965+ uses the linear or tiled offsets from the
11058 * Display Registers (which do not change across a page-flip)
11059 * so we need only reprogram the base address.
11060 */
6d90c952
DV
11061 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11062 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11063 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 11064 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 11065 obj->tiling_mode);
8c9f3aaf
JB
11066
11067 /* XXX Enabling the panel-fitter across page-flip is so far
11068 * untested on non-native modes, so ignore it for now.
11069 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11070 */
11071 pf = 0;
11072 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 11073 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
11074
11075 intel_mark_page_flip_active(intel_crtc);
09246732 11076 __intel_ring_advance(ring);
83d4092b 11077 return 0;
8c9f3aaf
JB
11078}
11079
11080static int intel_gen6_queue_flip(struct drm_device *dev,
11081 struct drm_crtc *crtc,
11082 struct drm_framebuffer *fb,
ed8d1975 11083 struct drm_i915_gem_object *obj,
a4872ba6 11084 struct intel_engine_cs *ring,
ed8d1975 11085 uint32_t flags)
8c9f3aaf
JB
11086{
11087 struct drm_i915_private *dev_priv = dev->dev_private;
11088 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11089 uint32_t pf, pipesrc;
11090 int ret;
11091
6d90c952 11092 ret = intel_ring_begin(ring, 4);
8c9f3aaf 11093 if (ret)
4fa62c89 11094 return ret;
8c9f3aaf 11095
6d90c952
DV
11096 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11097 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11098 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 11099 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 11100
dc257cf1
DV
11101 /* Contrary to the suggestions in the documentation,
11102 * "Enable Panel Fitter" does not seem to be required when page
11103 * flipping with a non-native mode, and worse causes a normal
11104 * modeset to fail.
11105 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11106 */
11107 pf = 0;
8c9f3aaf 11108 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 11109 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
11110
11111 intel_mark_page_flip_active(intel_crtc);
09246732 11112 __intel_ring_advance(ring);
83d4092b 11113 return 0;
8c9f3aaf
JB
11114}
11115
7c9017e5
JB
11116static int intel_gen7_queue_flip(struct drm_device *dev,
11117 struct drm_crtc *crtc,
11118 struct drm_framebuffer *fb,
ed8d1975 11119 struct drm_i915_gem_object *obj,
a4872ba6 11120 struct intel_engine_cs *ring,
ed8d1975 11121 uint32_t flags)
7c9017e5 11122{
7c9017e5 11123 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 11124 uint32_t plane_bit = 0;
ffe74d75
CW
11125 int len, ret;
11126
eba905b2 11127 switch (intel_crtc->plane) {
cb05d8de
DV
11128 case PLANE_A:
11129 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11130 break;
11131 case PLANE_B:
11132 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11133 break;
11134 case PLANE_C:
11135 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11136 break;
11137 default:
11138 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 11139 return -ENODEV;
cb05d8de
DV
11140 }
11141
ffe74d75 11142 len = 4;
f476828a 11143 if (ring->id == RCS) {
ffe74d75 11144 len += 6;
f476828a
DL
11145 /*
11146 * On Gen 8, SRM is now taking an extra dword to accommodate
11147 * 48bits addresses, and we need a NOOP for the batch size to
11148 * stay even.
11149 */
11150 if (IS_GEN8(dev))
11151 len += 2;
11152 }
ffe74d75 11153
f66fab8e
VS
11154 /*
11155 * BSpec MI_DISPLAY_FLIP for IVB:
11156 * "The full packet must be contained within the same cache line."
11157 *
11158 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11159 * cacheline, if we ever start emitting more commands before
11160 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11161 * then do the cacheline alignment, and finally emit the
11162 * MI_DISPLAY_FLIP.
11163 */
11164 ret = intel_ring_cacheline_align(ring);
11165 if (ret)
4fa62c89 11166 return ret;
f66fab8e 11167
ffe74d75 11168 ret = intel_ring_begin(ring, len);
7c9017e5 11169 if (ret)
4fa62c89 11170 return ret;
7c9017e5 11171
ffe74d75
CW
11172 /* Unmask the flip-done completion message. Note that the bspec says that
11173 * we should do this for both the BCS and RCS, and that we must not unmask
11174 * more than one flip event at any time (or ensure that one flip message
11175 * can be sent by waiting for flip-done prior to queueing new flips).
11176 * Experimentation says that BCS works despite DERRMR masking all
11177 * flip-done completion events and that unmasking all planes at once
11178 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11179 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11180 */
11181 if (ring->id == RCS) {
11182 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11183 intel_ring_emit(ring, DERRMR);
11184 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11185 DERRMR_PIPEB_PRI_FLIP_DONE |
11186 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
11187 if (IS_GEN8(dev))
11188 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11189 MI_SRM_LRM_GLOBAL_GTT);
11190 else
11191 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11192 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
11193 intel_ring_emit(ring, DERRMR);
11194 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
11195 if (IS_GEN8(dev)) {
11196 intel_ring_emit(ring, 0);
11197 intel_ring_emit(ring, MI_NOOP);
11198 }
ffe74d75
CW
11199 }
11200
cb05d8de 11201 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 11202 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 11203 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 11204 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
11205
11206 intel_mark_page_flip_active(intel_crtc);
09246732 11207 __intel_ring_advance(ring);
83d4092b 11208 return 0;
7c9017e5
JB
11209}
11210
84c33a64
SG
11211static bool use_mmio_flip(struct intel_engine_cs *ring,
11212 struct drm_i915_gem_object *obj)
11213{
11214 /*
11215 * This is not being used for older platforms, because
11216 * non-availability of flip done interrupt forces us to use
11217 * CS flips. Older platforms derive flip done using some clever
11218 * tricks involving the flip_pending status bits and vblank irqs.
11219 * So using MMIO flips there would disrupt this mechanism.
11220 */
11221
8e09bf83
CW
11222 if (ring == NULL)
11223 return true;
11224
84c33a64
SG
11225 if (INTEL_INFO(ring->dev)->gen < 5)
11226 return false;
11227
11228 if (i915.use_mmio_flip < 0)
11229 return false;
11230 else if (i915.use_mmio_flip > 0)
11231 return true;
14bf993e
OM
11232 else if (i915.enable_execlists)
11233 return true;
84c33a64 11234 else
b4716185 11235 return ring != i915_gem_request_get_ring(obj->last_write_req);
84c33a64
SG
11236}
11237
ff944564
DL
11238static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11239{
11240 struct drm_device *dev = intel_crtc->base.dev;
11241 struct drm_i915_private *dev_priv = dev->dev_private;
11242 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564
DL
11243 const enum pipe pipe = intel_crtc->pipe;
11244 u32 ctl, stride;
11245
11246 ctl = I915_READ(PLANE_CTL(pipe, 0));
11247 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11248 switch (fb->modifier[0]) {
11249 case DRM_FORMAT_MOD_NONE:
11250 break;
11251 case I915_FORMAT_MOD_X_TILED:
ff944564 11252 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11253 break;
11254 case I915_FORMAT_MOD_Y_TILED:
11255 ctl |= PLANE_CTL_TILED_Y;
11256 break;
11257 case I915_FORMAT_MOD_Yf_TILED:
11258 ctl |= PLANE_CTL_TILED_YF;
11259 break;
11260 default:
11261 MISSING_CASE(fb->modifier[0]);
11262 }
ff944564
DL
11263
11264 /*
11265 * The stride is either expressed as a multiple of 64 bytes chunks for
11266 * linear buffers or in number of tiles for tiled buffers.
11267 */
2ebef630
TU
11268 stride = fb->pitches[0] /
11269 intel_fb_stride_alignment(dev, fb->modifier[0],
11270 fb->pixel_format);
ff944564
DL
11271
11272 /*
11273 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11274 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11275 */
11276 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11277 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11278
11279 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11280 POSTING_READ(PLANE_SURF(pipe, 0));
11281}
11282
11283static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
11284{
11285 struct drm_device *dev = intel_crtc->base.dev;
11286 struct drm_i915_private *dev_priv = dev->dev_private;
11287 struct intel_framebuffer *intel_fb =
11288 to_intel_framebuffer(intel_crtc->base.primary->fb);
11289 struct drm_i915_gem_object *obj = intel_fb->obj;
11290 u32 dspcntr;
11291 u32 reg;
11292
84c33a64
SG
11293 reg = DSPCNTR(intel_crtc->plane);
11294 dspcntr = I915_READ(reg);
11295
c5d97472
DL
11296 if (obj->tiling_mode != I915_TILING_NONE)
11297 dspcntr |= DISPPLANE_TILED;
11298 else
11299 dspcntr &= ~DISPPLANE_TILED;
11300
84c33a64
SG
11301 I915_WRITE(reg, dspcntr);
11302
11303 I915_WRITE(DSPSURF(intel_crtc->plane),
11304 intel_crtc->unpin_work->gtt_offset);
11305 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 11306
ff944564
DL
11307}
11308
11309/*
11310 * XXX: This is the temporary way to update the plane registers until we get
11311 * around to using the usual plane update functions for MMIO flips
11312 */
11313static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11314{
11315 struct drm_device *dev = intel_crtc->base.dev;
11316 bool atomic_update;
11317 u32 start_vbl_count;
11318
11319 intel_mark_page_flip_active(intel_crtc);
11320
11321 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11322
11323 if (INTEL_INFO(dev)->gen >= 9)
11324 skl_do_mmio_flip(intel_crtc);
11325 else
11326 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11327 ilk_do_mmio_flip(intel_crtc);
11328
9362c7c5
ACO
11329 if (atomic_update)
11330 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
11331}
11332
9362c7c5 11333static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11334{
b2cfe0ab
CW
11335 struct intel_mmio_flip *mmio_flip =
11336 container_of(work, struct intel_mmio_flip, work);
84c33a64 11337
eed29a5b
DV
11338 if (mmio_flip->req)
11339 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11340 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11341 false, NULL,
11342 &mmio_flip->i915->rps.mmioflips));
84c33a64 11343
b2cfe0ab
CW
11344 intel_do_mmio_flip(mmio_flip->crtc);
11345
eed29a5b 11346 i915_gem_request_unreference__unlocked(mmio_flip->req);
b2cfe0ab 11347 kfree(mmio_flip);
84c33a64
SG
11348}
11349
11350static int intel_queue_mmio_flip(struct drm_device *dev,
11351 struct drm_crtc *crtc,
11352 struct drm_framebuffer *fb,
11353 struct drm_i915_gem_object *obj,
11354 struct intel_engine_cs *ring,
11355 uint32_t flags)
11356{
b2cfe0ab
CW
11357 struct intel_mmio_flip *mmio_flip;
11358
11359 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11360 if (mmio_flip == NULL)
11361 return -ENOMEM;
84c33a64 11362
bcafc4e3 11363 mmio_flip->i915 = to_i915(dev);
eed29a5b 11364 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11365 mmio_flip->crtc = to_intel_crtc(crtc);
536f5b5e 11366
b2cfe0ab
CW
11367 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11368 schedule_work(&mmio_flip->work);
84c33a64 11369
84c33a64
SG
11370 return 0;
11371}
11372
8c9f3aaf
JB
11373static int intel_default_queue_flip(struct drm_device *dev,
11374 struct drm_crtc *crtc,
11375 struct drm_framebuffer *fb,
ed8d1975 11376 struct drm_i915_gem_object *obj,
a4872ba6 11377 struct intel_engine_cs *ring,
ed8d1975 11378 uint32_t flags)
8c9f3aaf
JB
11379{
11380 return -ENODEV;
11381}
11382
d6bbafa1
CW
11383static bool __intel_pageflip_stall_check(struct drm_device *dev,
11384 struct drm_crtc *crtc)
11385{
11386 struct drm_i915_private *dev_priv = dev->dev_private;
11387 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11388 struct intel_unpin_work *work = intel_crtc->unpin_work;
11389 u32 addr;
11390
11391 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11392 return true;
11393
11394 if (!work->enable_stall_check)
11395 return false;
11396
11397 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11398 if (work->flip_queued_req &&
11399 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11400 return false;
11401
1e3feefd 11402 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11403 }
11404
1e3feefd 11405 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11406 return false;
11407
11408 /* Potential stall - if we see that the flip has happened,
11409 * assume a missed interrupt. */
11410 if (INTEL_INFO(dev)->gen >= 4)
11411 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11412 else
11413 addr = I915_READ(DSPADDR(intel_crtc->plane));
11414
11415 /* There is a potential issue here with a false positive after a flip
11416 * to the same address. We could address this by checking for a
11417 * non-incrementing frame counter.
11418 */
11419 return addr == work->gtt_offset;
11420}
11421
11422void intel_check_page_flip(struct drm_device *dev, int pipe)
11423{
11424 struct drm_i915_private *dev_priv = dev->dev_private;
11425 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11427 struct intel_unpin_work *work;
f326038a 11428
6c51d46f 11429 WARN_ON(!in_interrupt());
d6bbafa1
CW
11430
11431 if (crtc == NULL)
11432 return;
11433
f326038a 11434 spin_lock(&dev->event_lock);
6ad790c0
CW
11435 work = intel_crtc->unpin_work;
11436 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11437 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11438 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11439 page_flip_completed(intel_crtc);
6ad790c0 11440 work = NULL;
d6bbafa1 11441 }
6ad790c0
CW
11442 if (work != NULL &&
11443 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11444 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11445 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11446}
11447
6b95a207
KH
11448static int intel_crtc_page_flip(struct drm_crtc *crtc,
11449 struct drm_framebuffer *fb,
ed8d1975
KP
11450 struct drm_pending_vblank_event *event,
11451 uint32_t page_flip_flags)
6b95a207
KH
11452{
11453 struct drm_device *dev = crtc->dev;
11454 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11455 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11456 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11457 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11458 struct drm_plane *primary = crtc->primary;
a071fa00 11459 enum pipe pipe = intel_crtc->pipe;
6b95a207 11460 struct intel_unpin_work *work;
a4872ba6 11461 struct intel_engine_cs *ring;
cf5d8a46 11462 bool mmio_flip;
52e68630 11463 int ret;
6b95a207 11464
2ff8fde1
MR
11465 /*
11466 * drm_mode_page_flip_ioctl() should already catch this, but double
11467 * check to be safe. In the future we may enable pageflipping from
11468 * a disabled primary plane.
11469 */
11470 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11471 return -EBUSY;
11472
e6a595d2 11473 /* Can't change pixel format via MI display flips. */
f4510a27 11474 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11475 return -EINVAL;
11476
11477 /*
11478 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11479 * Note that pitch changes could also affect these register.
11480 */
11481 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11482 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11483 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11484 return -EINVAL;
11485
f900db47
CW
11486 if (i915_terminally_wedged(&dev_priv->gpu_error))
11487 goto out_hang;
11488
b14c5679 11489 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11490 if (work == NULL)
11491 return -ENOMEM;
11492
6b95a207 11493 work->event = event;
b4a98e57 11494 work->crtc = crtc;
ab8d6675 11495 work->old_fb = old_fb;
6b95a207
KH
11496 INIT_WORK(&work->work, intel_unpin_work_fn);
11497
87b6b101 11498 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11499 if (ret)
11500 goto free_work;
11501
6b95a207 11502 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11503 spin_lock_irq(&dev->event_lock);
6b95a207 11504 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11505 /* Before declaring the flip queue wedged, check if
11506 * the hardware completed the operation behind our backs.
11507 */
11508 if (__intel_pageflip_stall_check(dev, crtc)) {
11509 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11510 page_flip_completed(intel_crtc);
11511 } else {
11512 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11513 spin_unlock_irq(&dev->event_lock);
468f0b44 11514
d6bbafa1
CW
11515 drm_crtc_vblank_put(crtc);
11516 kfree(work);
11517 return -EBUSY;
11518 }
6b95a207
KH
11519 }
11520 intel_crtc->unpin_work = work;
5e2d7afc 11521 spin_unlock_irq(&dev->event_lock);
6b95a207 11522
b4a98e57
CW
11523 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11524 flush_workqueue(dev_priv->wq);
11525
75dfca80 11526 /* Reference the objects for the scheduled work. */
ab8d6675 11527 drm_framebuffer_reference(work->old_fb);
05394f39 11528 drm_gem_object_reference(&obj->base);
6b95a207 11529
f4510a27 11530 crtc->primary->fb = fb;
afd65eb4 11531 update_state_fb(crtc->primary);
1ed1f968 11532
e1f99ce6 11533 work->pending_flip_obj = obj;
e1f99ce6 11534
89ed88ba
CW
11535 ret = i915_mutex_lock_interruptible(dev);
11536 if (ret)
11537 goto cleanup;
11538
b4a98e57 11539 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11540 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11541
75f7f3ec 11542 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 11543 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 11544
4fa62c89
VS
11545 if (IS_VALLEYVIEW(dev)) {
11546 ring = &dev_priv->ring[BCS];
ab8d6675 11547 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
11548 /* vlv: DISPLAY_FLIP fails to change tiling */
11549 ring = NULL;
48bf5b2d 11550 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 11551 ring = &dev_priv->ring[BCS];
4fa62c89 11552 } else if (INTEL_INFO(dev)->gen >= 7) {
b4716185 11553 ring = i915_gem_request_get_ring(obj->last_write_req);
4fa62c89
VS
11554 if (ring == NULL || ring->id != RCS)
11555 ring = &dev_priv->ring[BCS];
11556 } else {
11557 ring = &dev_priv->ring[RCS];
11558 }
11559
cf5d8a46
CW
11560 mmio_flip = use_mmio_flip(ring, obj);
11561
11562 /* When using CS flips, we want to emit semaphores between rings.
11563 * However, when using mmio flips we will create a task to do the
11564 * synchronisation, so all we want here is to pin the framebuffer
11565 * into the display plane and skip any waits.
11566 */
82bc3b2d 11567 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
cf5d8a46 11568 crtc->primary->state,
b4716185 11569 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
8c9f3aaf
JB
11570 if (ret)
11571 goto cleanup_pending;
6b95a207 11572
121920fa
TU
11573 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11574 + intel_crtc->dspaddr_offset;
4fa62c89 11575
cf5d8a46 11576 if (mmio_flip) {
84c33a64
SG
11577 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11578 page_flip_flags);
d6bbafa1
CW
11579 if (ret)
11580 goto cleanup_unpin;
11581
f06cc1b9
JH
11582 i915_gem_request_assign(&work->flip_queued_req,
11583 obj->last_write_req);
d6bbafa1 11584 } else {
d94b5030
CW
11585 if (obj->last_write_req) {
11586 ret = i915_gem_check_olr(obj->last_write_req);
11587 if (ret)
11588 goto cleanup_unpin;
11589 }
11590
84c33a64 11591 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
11592 page_flip_flags);
11593 if (ret)
11594 goto cleanup_unpin;
11595
f06cc1b9
JH
11596 i915_gem_request_assign(&work->flip_queued_req,
11597 intel_ring_get_request(ring));
d6bbafa1
CW
11598 }
11599
1e3feefd 11600 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11601 work->enable_stall_check = true;
4fa62c89 11602
ab8d6675 11603 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a071fa00
DV
11604 INTEL_FRONTBUFFER_PRIMARY(pipe));
11605
7ff0ebcc 11606 intel_fbc_disable(dev);
f99d7069 11607 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
11608 mutex_unlock(&dev->struct_mutex);
11609
e5510fac
JB
11610 trace_i915_flip_request(intel_crtc->plane, obj);
11611
6b95a207 11612 return 0;
96b099fd 11613
4fa62c89 11614cleanup_unpin:
82bc3b2d 11615 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 11616cleanup_pending:
b4a98e57 11617 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11618 mutex_unlock(&dev->struct_mutex);
11619cleanup:
f4510a27 11620 crtc->primary->fb = old_fb;
afd65eb4 11621 update_state_fb(crtc->primary);
89ed88ba
CW
11622
11623 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11624 drm_framebuffer_unreference(work->old_fb);
96b099fd 11625
5e2d7afc 11626 spin_lock_irq(&dev->event_lock);
96b099fd 11627 intel_crtc->unpin_work = NULL;
5e2d7afc 11628 spin_unlock_irq(&dev->event_lock);
96b099fd 11629
87b6b101 11630 drm_crtc_vblank_put(crtc);
7317c75e 11631free_work:
96b099fd
CW
11632 kfree(work);
11633
f900db47 11634 if (ret == -EIO) {
02e0efb5
ML
11635 struct drm_atomic_state *state;
11636 struct drm_plane_state *plane_state;
11637
f900db47 11638out_hang:
02e0efb5
ML
11639 state = drm_atomic_state_alloc(dev);
11640 if (!state)
11641 return -ENOMEM;
11642 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11643
11644retry:
11645 plane_state = drm_atomic_get_plane_state(state, primary);
11646 ret = PTR_ERR_OR_ZERO(plane_state);
11647 if (!ret) {
11648 drm_atomic_set_fb_for_plane(plane_state, fb);
11649
11650 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11651 if (!ret)
11652 ret = drm_atomic_commit(state);
11653 }
11654
11655 if (ret == -EDEADLK) {
11656 drm_modeset_backoff(state->acquire_ctx);
11657 drm_atomic_state_clear(state);
11658 goto retry;
11659 }
11660
11661 if (ret)
11662 drm_atomic_state_free(state);
11663
f0d3dad3 11664 if (ret == 0 && event) {
5e2d7afc 11665 spin_lock_irq(&dev->event_lock);
a071fa00 11666 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11667 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11668 }
f900db47 11669 }
96b099fd 11670 return ret;
6b95a207
KH
11671}
11672
65b38e0d 11673static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11674 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11675 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11676 .atomic_begin = intel_begin_crtc_commit,
11677 .atomic_flush = intel_finish_crtc_commit,
f6e5b160
CW
11678};
11679
9a935856
DV
11680/**
11681 * intel_modeset_update_staged_output_state
11682 *
11683 * Updates the staged output configuration state, e.g. after we've read out the
11684 * current hw state.
11685 */
11686static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 11687{
7668851f 11688 struct intel_crtc *crtc;
9a935856
DV
11689 struct intel_encoder *encoder;
11690 struct intel_connector *connector;
f6e5b160 11691
3a3371ff 11692 for_each_intel_connector(dev, connector) {
9a935856
DV
11693 connector->new_encoder =
11694 to_intel_encoder(connector->base.encoder);
11695 }
f6e5b160 11696
b2784e15 11697 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11698 encoder->new_crtc =
11699 to_intel_crtc(encoder->base.crtc);
11700 }
7668851f 11701
d3fcc808 11702 for_each_intel_crtc(dev, crtc) {
83d65738 11703 crtc->new_enabled = crtc->base.state->enable;
7668851f 11704 }
f6e5b160
CW
11705}
11706
d29b2f9d
ACO
11707/* Transitional helper to copy current connector/encoder state to
11708 * connector->state. This is needed so that code that is partially
11709 * converted to atomic does the right thing.
11710 */
11711static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11712{
11713 struct intel_connector *connector;
11714
11715 for_each_intel_connector(dev, connector) {
11716 if (connector->base.encoder) {
11717 connector->base.state->best_encoder =
11718 connector->base.encoder;
11719 connector->base.state->crtc =
11720 connector->base.encoder->crtc;
11721 } else {
11722 connector->base.state->best_encoder = NULL;
11723 connector->base.state->crtc = NULL;
11724 }
11725 }
11726}
11727
050f7aeb 11728static void
eba905b2 11729connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11730 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11731{
11732 int bpp = pipe_config->pipe_bpp;
11733
11734 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11735 connector->base.base.id,
c23cc417 11736 connector->base.name);
050f7aeb
DV
11737
11738 /* Don't use an invalid EDID bpc value */
11739 if (connector->base.display_info.bpc &&
11740 connector->base.display_info.bpc * 3 < bpp) {
11741 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11742 bpp, connector->base.display_info.bpc*3);
11743 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11744 }
11745
11746 /* Clamp bpp to 8 on screens without EDID 1.4 */
11747 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11748 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11749 bpp);
11750 pipe_config->pipe_bpp = 24;
11751 }
11752}
11753
4e53c2e0 11754static int
050f7aeb 11755compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11756 struct intel_crtc_state *pipe_config)
4e53c2e0 11757{
050f7aeb 11758 struct drm_device *dev = crtc->base.dev;
1486017f 11759 struct drm_atomic_state *state;
da3ced29
ACO
11760 struct drm_connector *connector;
11761 struct drm_connector_state *connector_state;
1486017f 11762 int bpp, i;
4e53c2e0 11763
d328c9d7 11764 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
4e53c2e0 11765 bpp = 10*3;
d328c9d7
DV
11766 else if (INTEL_INFO(dev)->gen >= 5)
11767 bpp = 12*3;
11768 else
11769 bpp = 8*3;
11770
4e53c2e0 11771
4e53c2e0
DV
11772 pipe_config->pipe_bpp = bpp;
11773
1486017f
ACO
11774 state = pipe_config->base.state;
11775
4e53c2e0 11776 /* Clamp display bpp to EDID value */
da3ced29
ACO
11777 for_each_connector_in_state(state, connector, connector_state, i) {
11778 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11779 continue;
11780
da3ced29
ACO
11781 connected_sink_compute_bpp(to_intel_connector(connector),
11782 pipe_config);
4e53c2e0
DV
11783 }
11784
11785 return bpp;
11786}
11787
644db711
DV
11788static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11789{
11790 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11791 "type: 0x%x flags: 0x%x\n",
1342830c 11792 mode->crtc_clock,
644db711
DV
11793 mode->crtc_hdisplay, mode->crtc_hsync_start,
11794 mode->crtc_hsync_end, mode->crtc_htotal,
11795 mode->crtc_vdisplay, mode->crtc_vsync_start,
11796 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11797}
11798
c0b03411 11799static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11800 struct intel_crtc_state *pipe_config,
c0b03411
DV
11801 const char *context)
11802{
6a60cd87
CK
11803 struct drm_device *dev = crtc->base.dev;
11804 struct drm_plane *plane;
11805 struct intel_plane *intel_plane;
11806 struct intel_plane_state *state;
11807 struct drm_framebuffer *fb;
11808
11809 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11810 context, pipe_config, pipe_name(crtc->pipe));
c0b03411
DV
11811
11812 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11813 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11814 pipe_config->pipe_bpp, pipe_config->dither);
11815 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11816 pipe_config->has_pch_encoder,
11817 pipe_config->fdi_lanes,
11818 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11819 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11820 pipe_config->fdi_m_n.tu);
eb14cb74
VS
11821 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11822 pipe_config->has_dp_encoder,
11823 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11824 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11825 pipe_config->dp_m_n.tu);
b95af8be
VK
11826
11827 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11828 pipe_config->has_dp_encoder,
11829 pipe_config->dp_m2_n2.gmch_m,
11830 pipe_config->dp_m2_n2.gmch_n,
11831 pipe_config->dp_m2_n2.link_m,
11832 pipe_config->dp_m2_n2.link_n,
11833 pipe_config->dp_m2_n2.tu);
11834
55072d19
DV
11835 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11836 pipe_config->has_audio,
11837 pipe_config->has_infoframe);
11838
c0b03411 11839 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11840 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11841 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11842 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11843 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 11844 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
11845 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11846 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
11847 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11848 crtc->num_scalers,
11849 pipe_config->scaler_state.scaler_users,
11850 pipe_config->scaler_state.scaler_id);
c0b03411
DV
11851 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11852 pipe_config->gmch_pfit.control,
11853 pipe_config->gmch_pfit.pgm_ratios,
11854 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 11855 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 11856 pipe_config->pch_pfit.pos,
fd4daa9c
CW
11857 pipe_config->pch_pfit.size,
11858 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 11859 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 11860 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 11861
415ff0f6
TU
11862 if (IS_BROXTON(dev)) {
11863 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11864 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11865 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11866 pipe_config->ddi_pll_sel,
11867 pipe_config->dpll_hw_state.ebb0,
11868 pipe_config->dpll_hw_state.pll0,
11869 pipe_config->dpll_hw_state.pll1,
11870 pipe_config->dpll_hw_state.pll2,
11871 pipe_config->dpll_hw_state.pll3,
11872 pipe_config->dpll_hw_state.pll6,
11873 pipe_config->dpll_hw_state.pll8,
11874 pipe_config->dpll_hw_state.pcsdw12);
11875 } else if (IS_SKYLAKE(dev)) {
11876 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11877 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11878 pipe_config->ddi_pll_sel,
11879 pipe_config->dpll_hw_state.ctrl1,
11880 pipe_config->dpll_hw_state.cfgcr1,
11881 pipe_config->dpll_hw_state.cfgcr2);
11882 } else if (HAS_DDI(dev)) {
11883 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11884 pipe_config->ddi_pll_sel,
11885 pipe_config->dpll_hw_state.wrpll);
11886 } else {
11887 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11888 "fp0: 0x%x, fp1: 0x%x\n",
11889 pipe_config->dpll_hw_state.dpll,
11890 pipe_config->dpll_hw_state.dpll_md,
11891 pipe_config->dpll_hw_state.fp0,
11892 pipe_config->dpll_hw_state.fp1);
11893 }
11894
6a60cd87
CK
11895 DRM_DEBUG_KMS("planes on this crtc\n");
11896 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11897 intel_plane = to_intel_plane(plane);
11898 if (intel_plane->pipe != crtc->pipe)
11899 continue;
11900
11901 state = to_intel_plane_state(plane->state);
11902 fb = state->base.fb;
11903 if (!fb) {
11904 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11905 "disabled, scaler_id = %d\n",
11906 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11907 plane->base.id, intel_plane->pipe,
11908 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11909 drm_plane_index(plane), state->scaler_id);
11910 continue;
11911 }
11912
11913 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11914 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11915 plane->base.id, intel_plane->pipe,
11916 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11917 drm_plane_index(plane));
11918 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11919 fb->base.id, fb->width, fb->height, fb->pixel_format);
11920 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11921 state->scaler_id,
11922 state->src.x1 >> 16, state->src.y1 >> 16,
11923 drm_rect_width(&state->src) >> 16,
11924 drm_rect_height(&state->src) >> 16,
11925 state->dst.x1, state->dst.y1,
11926 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11927 }
c0b03411
DV
11928}
11929
bc079e8b
VS
11930static bool encoders_cloneable(const struct intel_encoder *a,
11931 const struct intel_encoder *b)
accfc0c5 11932{
bc079e8b
VS
11933 /* masks could be asymmetric, so check both ways */
11934 return a == b || (a->cloneable & (1 << b->type) &&
11935 b->cloneable & (1 << a->type));
11936}
11937
98a221da
ACO
11938static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11939 struct intel_crtc *crtc,
bc079e8b
VS
11940 struct intel_encoder *encoder)
11941{
bc079e8b 11942 struct intel_encoder *source_encoder;
da3ced29 11943 struct drm_connector *connector;
98a221da
ACO
11944 struct drm_connector_state *connector_state;
11945 int i;
bc079e8b 11946
da3ced29 11947 for_each_connector_in_state(state, connector, connector_state, i) {
98a221da 11948 if (connector_state->crtc != &crtc->base)
bc079e8b
VS
11949 continue;
11950
98a221da
ACO
11951 source_encoder =
11952 to_intel_encoder(connector_state->best_encoder);
bc079e8b
VS
11953 if (!encoders_cloneable(encoder, source_encoder))
11954 return false;
11955 }
11956
11957 return true;
11958}
11959
98a221da
ACO
11960static bool check_encoder_cloning(struct drm_atomic_state *state,
11961 struct intel_crtc *crtc)
bc079e8b 11962{
accfc0c5 11963 struct intel_encoder *encoder;
da3ced29 11964 struct drm_connector *connector;
98a221da
ACO
11965 struct drm_connector_state *connector_state;
11966 int i;
accfc0c5 11967
da3ced29 11968 for_each_connector_in_state(state, connector, connector_state, i) {
98a221da
ACO
11969 if (connector_state->crtc != &crtc->base)
11970 continue;
11971
11972 encoder = to_intel_encoder(connector_state->best_encoder);
11973 if (!check_single_encoder_cloning(state, crtc, encoder))
bc079e8b 11974 return false;
accfc0c5
DV
11975 }
11976
bc079e8b 11977 return true;
accfc0c5
DV
11978}
11979
5448a00d 11980static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11981{
5448a00d
ACO
11982 struct drm_device *dev = state->dev;
11983 struct intel_encoder *encoder;
da3ced29 11984 struct drm_connector *connector;
5448a00d 11985 struct drm_connector_state *connector_state;
00f0b378 11986 unsigned int used_ports = 0;
5448a00d 11987 int i;
00f0b378
VS
11988
11989 /*
11990 * Walk the connector list instead of the encoder
11991 * list to detect the problem on ddi platforms
11992 * where there's just one encoder per digital port.
11993 */
da3ced29 11994 for_each_connector_in_state(state, connector, connector_state, i) {
5448a00d 11995 if (!connector_state->best_encoder)
00f0b378
VS
11996 continue;
11997
5448a00d
ACO
11998 encoder = to_intel_encoder(connector_state->best_encoder);
11999
12000 WARN_ON(!connector_state->crtc);
00f0b378
VS
12001
12002 switch (encoder->type) {
12003 unsigned int port_mask;
12004 case INTEL_OUTPUT_UNKNOWN:
12005 if (WARN_ON(!HAS_DDI(dev)))
12006 break;
12007 case INTEL_OUTPUT_DISPLAYPORT:
12008 case INTEL_OUTPUT_HDMI:
12009 case INTEL_OUTPUT_EDP:
12010 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12011
12012 /* the same port mustn't appear more than once */
12013 if (used_ports & port_mask)
12014 return false;
12015
12016 used_ports |= port_mask;
12017 default:
12018 break;
12019 }
12020 }
12021
12022 return true;
12023}
12024
83a57153
ACO
12025static void
12026clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12027{
12028 struct drm_crtc_state tmp_state;
663a3640 12029 struct intel_crtc_scaler_state scaler_state;
4978cc93
ACO
12030 struct intel_dpll_hw_state dpll_hw_state;
12031 enum intel_dpll_id shared_dpll;
8504c74c 12032 uint32_t ddi_pll_sel;
83a57153 12033
7546a384
ACO
12034 /* FIXME: before the switch to atomic started, a new pipe_config was
12035 * kzalloc'd. Code that depends on any field being zero should be
12036 * fixed, so that the crtc_state can be safely duplicated. For now,
12037 * only fields that are know to not cause problems are preserved. */
12038
83a57153 12039 tmp_state = crtc_state->base;
663a3640 12040 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12041 shared_dpll = crtc_state->shared_dpll;
12042 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12043 ddi_pll_sel = crtc_state->ddi_pll_sel;
4978cc93 12044
83a57153 12045 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12046
83a57153 12047 crtc_state->base = tmp_state;
663a3640 12048 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12049 crtc_state->shared_dpll = shared_dpll;
12050 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12051 crtc_state->ddi_pll_sel = ddi_pll_sel;
83a57153
ACO
12052}
12053
548ee15b 12054static int
b8cecdf5 12055intel_modeset_pipe_config(struct drm_crtc *crtc,
568c634a 12056 struct drm_atomic_state *state)
ee7b9f93 12057{
568c634a
ACO
12058 struct drm_crtc_state *crtc_state;
12059 struct intel_crtc_state *pipe_config;
7758a113 12060 struct intel_encoder *encoder;
da3ced29 12061 struct drm_connector *connector;
0b901879 12062 struct drm_connector_state *connector_state;
d328c9d7 12063 int base_bpp, ret = -EINVAL;
0b901879 12064 int i;
e29c22c0 12065 bool retry = true;
ee7b9f93 12066
98a221da 12067 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
accfc0c5 12068 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
548ee15b 12069 return -EINVAL;
accfc0c5
DV
12070 }
12071
5448a00d 12072 if (!check_digital_port_conflicts(state)) {
00f0b378 12073 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
548ee15b 12074 return -EINVAL;
00f0b378
VS
12075 }
12076
568c634a
ACO
12077 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12078 if (WARN_ON(!crtc_state))
12079 return -EINVAL;
12080
12081 pipe_config = to_intel_crtc_state(crtc_state);
12082
cdba954e
ACO
12083 /*
12084 * XXX: Add all connectors to make the crtc state match the encoders.
12085 */
12086 if (!needs_modeset(&pipe_config->base)) {
12087 ret = drm_atomic_add_affected_connectors(state, crtc);
12088 if (ret)
12089 return ret;
12090 }
12091
83a57153 12092 clear_intel_crtc_state(pipe_config);
7758a113 12093
e143a21c
DV
12094 pipe_config->cpu_transcoder =
12095 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12096
2960bc9c
ID
12097 /*
12098 * Sanitize sync polarity flags based on requested ones. If neither
12099 * positive or negative polarity is requested, treat this as meaning
12100 * negative polarity.
12101 */
2d112de7 12102 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12103 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12104 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12105
2d112de7 12106 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12107 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12108 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12109
050f7aeb
DV
12110 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12111 * plane pixel format and any sink constraints into account. Returns the
12112 * source plane bpp so that dithering can be selected on mismatches
12113 * after encoders and crtc also have had their say. */
d328c9d7
DV
12114 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12115 pipe_config);
12116 if (base_bpp < 0)
4e53c2e0
DV
12117 goto fail;
12118
e41a56be
VS
12119 /*
12120 * Determine the real pipe dimensions. Note that stereo modes can
12121 * increase the actual pipe size due to the frame doubling and
12122 * insertion of additional space for blanks between the frame. This
12123 * is stored in the crtc timings. We use the requested mode to do this
12124 * computation to clearly distinguish it from the adjusted mode, which
12125 * can be changed by the connectors in the below retry loop.
12126 */
2d112de7 12127 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12128 &pipe_config->pipe_src_w,
12129 &pipe_config->pipe_src_h);
e41a56be 12130
e29c22c0 12131encoder_retry:
ef1b460d 12132 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12133 pipe_config->port_clock = 0;
ef1b460d 12134 pipe_config->pixel_multiplier = 1;
ff9a6750 12135
135c81b8 12136 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12137 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12138 CRTC_STEREO_DOUBLE);
135c81b8 12139
7758a113
DV
12140 /* Pass our mode to the connectors and the CRTC to give them a chance to
12141 * adjust it according to limitations or connector properties, and also
12142 * a chance to reject the mode entirely.
47f1c6c9 12143 */
da3ced29 12144 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12145 if (connector_state->crtc != crtc)
7758a113 12146 continue;
7ae89233 12147
0b901879
ACO
12148 encoder = to_intel_encoder(connector_state->best_encoder);
12149
efea6e8e
DV
12150 if (!(encoder->compute_config(encoder, pipe_config))) {
12151 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12152 goto fail;
12153 }
ee7b9f93 12154 }
47f1c6c9 12155
ff9a6750
DV
12156 /* Set default port clock if not overwritten by the encoder. Needs to be
12157 * done afterwards in case the encoder adjusts the mode. */
12158 if (!pipe_config->port_clock)
2d112de7 12159 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12160 * pipe_config->pixel_multiplier;
ff9a6750 12161
a43f6e0f 12162 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12163 if (ret < 0) {
7758a113
DV
12164 DRM_DEBUG_KMS("CRTC fixup failed\n");
12165 goto fail;
ee7b9f93 12166 }
e29c22c0
DV
12167
12168 if (ret == RETRY) {
12169 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12170 ret = -EINVAL;
12171 goto fail;
12172 }
12173
12174 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12175 retry = false;
12176 goto encoder_retry;
12177 }
12178
d328c9d7 12179 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
4e53c2e0 12180 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12181 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12182
cdba954e
ACO
12183 /* Check if we need to force a modeset */
12184 if (pipe_config->has_audio !=
85a96e7a 12185 to_intel_crtc_state(crtc->state)->has_audio) {
cdba954e 12186 pipe_config->base.mode_changed = true;
85a96e7a
ML
12187 ret = drm_atomic_add_affected_planes(state, crtc);
12188 }
cdba954e
ACO
12189
12190 /*
12191 * Note we have an issue here with infoframes: current code
12192 * only updates them on the full mode set path per hw
12193 * requirements. So here we should be checking for any
12194 * required changes and forcing a mode set.
12195 */
7758a113 12196fail:
548ee15b 12197 return ret;
ee7b9f93 12198}
47f1c6c9 12199
ea9d758d 12200static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 12201{
ea9d758d 12202 struct drm_encoder *encoder;
f6e5b160 12203 struct drm_device *dev = crtc->dev;
f6e5b160 12204
ea9d758d
DV
12205 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12206 if (encoder->crtc == crtc)
12207 return true;
12208
12209 return false;
12210}
12211
12212static void
0a9ab303 12213intel_modeset_update_state(struct drm_atomic_state *state)
ea9d758d 12214{
0a9ab303 12215 struct drm_device *dev = state->dev;
ea9d758d 12216 struct intel_encoder *intel_encoder;
0a9ab303
ACO
12217 struct drm_crtc *crtc;
12218 struct drm_crtc_state *crtc_state;
ea9d758d
DV
12219 struct drm_connector *connector;
12220
de419ab6 12221 intel_shared_dpll_commit(state);
ba41c0de 12222
b2784e15 12223 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
12224 if (!intel_encoder->base.crtc)
12225 continue;
12226
69024de8
ML
12227 crtc = intel_encoder->base.crtc;
12228 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12229 if (!crtc_state || !needs_modeset(crtc->state))
12230 continue;
ea9d758d 12231
69024de8 12232 intel_encoder->connectors_active = false;
ea9d758d
DV
12233 }
12234
3cb480bc 12235 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
f7217905 12236 intel_modeset_update_staged_output_state(state->dev);
ea9d758d 12237
7668851f 12238 /* Double check state. */
0a9ab303
ACO
12239 for_each_crtc(dev, crtc) {
12240 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
3cb480bc
ML
12241
12242 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12243
12244 /* Update hwmode for vblank functions */
12245 if (crtc->state->active)
12246 crtc->hwmode = crtc->state->adjusted_mode;
12247 else
12248 crtc->hwmode.crtc_clock = 0;
ea9d758d
DV
12249 }
12250
12251 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12252 if (!connector->encoder || !connector->encoder->crtc)
12253 continue;
12254
69024de8
ML
12255 crtc = connector->encoder->crtc;
12256 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12257 if (!crtc_state || !needs_modeset(crtc->state))
12258 continue;
ea9d758d 12259
53d9f4e9 12260 if (crtc->state->active) {
69024de8
ML
12261 struct drm_property *dpms_property =
12262 dev->mode_config.dpms_property;
68d34720 12263
69024de8
ML
12264 connector->dpms = DRM_MODE_DPMS_ON;
12265 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
ea9d758d 12266
69024de8
ML
12267 intel_encoder = to_intel_encoder(connector->encoder);
12268 intel_encoder->connectors_active = true;
12269 } else
12270 connector->dpms = DRM_MODE_DPMS_OFF;
ea9d758d 12271 }
ea9d758d
DV
12272}
12273
3bd26263 12274static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12275{
3bd26263 12276 int diff;
f1f644dc
JB
12277
12278 if (clock1 == clock2)
12279 return true;
12280
12281 if (!clock1 || !clock2)
12282 return false;
12283
12284 diff = abs(clock1 - clock2);
12285
12286 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12287 return true;
12288
12289 return false;
12290}
12291
25c5b266
DV
12292#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12293 list_for_each_entry((intel_crtc), \
12294 &(dev)->mode_config.crtc_list, \
12295 base.head) \
0973f18f 12296 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12297
0e8ffe1b 12298static bool
2fa2fe9a 12299intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
12300 struct intel_crtc_state *current_config,
12301 struct intel_crtc_state *pipe_config)
0e8ffe1b 12302{
66e985c0
DV
12303#define PIPE_CONF_CHECK_X(name) \
12304 if (current_config->name != pipe_config->name) { \
12305 DRM_ERROR("mismatch in " #name " " \
12306 "(expected 0x%08x, found 0x%08x)\n", \
12307 current_config->name, \
12308 pipe_config->name); \
12309 return false; \
12310 }
12311
08a24034
DV
12312#define PIPE_CONF_CHECK_I(name) \
12313 if (current_config->name != pipe_config->name) { \
12314 DRM_ERROR("mismatch in " #name " " \
12315 "(expected %i, found %i)\n", \
12316 current_config->name, \
12317 pipe_config->name); \
12318 return false; \
88adfff1
DV
12319 }
12320
b95af8be
VK
12321/* This is required for BDW+ where there is only one set of registers for
12322 * switching between high and low RR.
12323 * This macro can be used whenever a comparison has to be made between one
12324 * hw state and multiple sw state variables.
12325 */
12326#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12327 if ((current_config->name != pipe_config->name) && \
12328 (current_config->alt_name != pipe_config->name)) { \
12329 DRM_ERROR("mismatch in " #name " " \
12330 "(expected %i or %i, found %i)\n", \
12331 current_config->name, \
12332 current_config->alt_name, \
12333 pipe_config->name); \
12334 return false; \
12335 }
12336
1bd1bd80
DV
12337#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12338 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 12339 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12340 "(expected %i, found %i)\n", \
12341 current_config->name & (mask), \
12342 pipe_config->name & (mask)); \
12343 return false; \
12344 }
12345
5e550656
VS
12346#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12347 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12348 DRM_ERROR("mismatch in " #name " " \
12349 "(expected %i, found %i)\n", \
12350 current_config->name, \
12351 pipe_config->name); \
12352 return false; \
12353 }
12354
bb760063
DV
12355#define PIPE_CONF_QUIRK(quirk) \
12356 ((current_config->quirks | pipe_config->quirks) & (quirk))
12357
eccb140b
DV
12358 PIPE_CONF_CHECK_I(cpu_transcoder);
12359
08a24034
DV
12360 PIPE_CONF_CHECK_I(has_pch_encoder);
12361 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
12362 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12363 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12364 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12365 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12366 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 12367
eb14cb74 12368 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
12369
12370 if (INTEL_INFO(dev)->gen < 8) {
12371 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12372 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12373 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12374 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12375 PIPE_CONF_CHECK_I(dp_m_n.tu);
12376
12377 if (current_config->has_drrs) {
12378 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12379 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12380 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12381 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12382 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12383 }
12384 } else {
12385 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12386 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12387 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12388 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12389 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12390 }
eb14cb74 12391
2d112de7
ACO
12392 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12393 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12394 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12395 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12396 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12397 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12398
2d112de7
ACO
12399 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12400 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12401 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12402 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12403 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12404 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12405
c93f54cf 12406 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12407 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
12408 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12409 IS_VALLEYVIEW(dev))
12410 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12411 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12412
9ed109a7
DV
12413 PIPE_CONF_CHECK_I(has_audio);
12414
2d112de7 12415 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12416 DRM_MODE_FLAG_INTERLACE);
12417
bb760063 12418 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12419 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12420 DRM_MODE_FLAG_PHSYNC);
2d112de7 12421 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12422 DRM_MODE_FLAG_NHSYNC);
2d112de7 12423 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12424 DRM_MODE_FLAG_PVSYNC);
2d112de7 12425 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12426 DRM_MODE_FLAG_NVSYNC);
12427 }
045ac3b5 12428
37327abd
VS
12429 PIPE_CONF_CHECK_I(pipe_src_w);
12430 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 12431
9953599b
DV
12432 /*
12433 * FIXME: BIOS likes to set up a cloned config with lvds+external
12434 * screen. Since we don't yet re-compute the pipe config when moving
12435 * just the lvds port away to another pipe the sw tracking won't match.
12436 *
12437 * Proper atomic modesets with recomputed global state will fix this.
12438 * Until then just don't check gmch state for inherited modes.
12439 */
12440 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12441 PIPE_CONF_CHECK_I(gmch_pfit.control);
12442 /* pfit ratios are autocomputed by the hw on gen4+ */
12443 if (INTEL_INFO(dev)->gen < 4)
12444 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12445 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12446 }
12447
fd4daa9c
CW
12448 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12449 if (current_config->pch_pfit.enabled) {
12450 PIPE_CONF_CHECK_I(pch_pfit.pos);
12451 PIPE_CONF_CHECK_I(pch_pfit.size);
12452 }
2fa2fe9a 12453
a1b2278e
CK
12454 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12455
e59150dc
JB
12456 /* BDW+ don't expose a synchronous way to read the state */
12457 if (IS_HASWELL(dev))
12458 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12459
282740f7
VS
12460 PIPE_CONF_CHECK_I(double_wide);
12461
26804afd
DV
12462 PIPE_CONF_CHECK_X(ddi_pll_sel);
12463
c0d43d62 12464 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 12465 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12466 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12467 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12468 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12469 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
12470 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12471 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12472 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12473
42571aef
VS
12474 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12475 PIPE_CONF_CHECK_I(pipe_bpp);
12476
2d112de7 12477 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12478 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12479
66e985c0 12480#undef PIPE_CONF_CHECK_X
08a24034 12481#undef PIPE_CONF_CHECK_I
b95af8be 12482#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12483#undef PIPE_CONF_CHECK_FLAGS
5e550656 12484#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12485#undef PIPE_CONF_QUIRK
88adfff1 12486
0e8ffe1b
DV
12487 return true;
12488}
12489
08db6652
DL
12490static void check_wm_state(struct drm_device *dev)
12491{
12492 struct drm_i915_private *dev_priv = dev->dev_private;
12493 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12494 struct intel_crtc *intel_crtc;
12495 int plane;
12496
12497 if (INTEL_INFO(dev)->gen < 9)
12498 return;
12499
12500 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12501 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12502
12503 for_each_intel_crtc(dev, intel_crtc) {
12504 struct skl_ddb_entry *hw_entry, *sw_entry;
12505 const enum pipe pipe = intel_crtc->pipe;
12506
12507 if (!intel_crtc->active)
12508 continue;
12509
12510 /* planes */
dd740780 12511 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12512 hw_entry = &hw_ddb.plane[pipe][plane];
12513 sw_entry = &sw_ddb->plane[pipe][plane];
12514
12515 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12516 continue;
12517
12518 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12519 "(expected (%u,%u), found (%u,%u))\n",
12520 pipe_name(pipe), plane + 1,
12521 sw_entry->start, sw_entry->end,
12522 hw_entry->start, hw_entry->end);
12523 }
12524
12525 /* cursor */
12526 hw_entry = &hw_ddb.cursor[pipe];
12527 sw_entry = &sw_ddb->cursor[pipe];
12528
12529 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12530 continue;
12531
12532 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12533 "(expected (%u,%u), found (%u,%u))\n",
12534 pipe_name(pipe),
12535 sw_entry->start, sw_entry->end,
12536 hw_entry->start, hw_entry->end);
12537 }
12538}
12539
91d1b4bd
DV
12540static void
12541check_connector_state(struct drm_device *dev)
8af6cf88 12542{
8af6cf88
DV
12543 struct intel_connector *connector;
12544
3a3371ff 12545 for_each_intel_connector(dev, connector) {
8af6cf88
DV
12546 /* This also checks the encoder/connector hw state with the
12547 * ->get_hw_state callbacks. */
12548 intel_connector_check_state(connector);
12549
e2c719b7 12550 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
8af6cf88
DV
12551 "connector's staged encoder doesn't match current encoder\n");
12552 }
91d1b4bd
DV
12553}
12554
12555static void
12556check_encoder_state(struct drm_device *dev)
12557{
12558 struct intel_encoder *encoder;
12559 struct intel_connector *connector;
8af6cf88 12560
b2784e15 12561 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12562 bool enabled = false;
12563 bool active = false;
12564 enum pipe pipe, tracked_pipe;
12565
12566 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12567 encoder->base.base.id,
8e329a03 12568 encoder->base.name);
8af6cf88 12569
e2c719b7 12570 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
8af6cf88 12571 "encoder's stage crtc doesn't match current crtc\n");
e2c719b7 12572 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
12573 "encoder's active_connectors set, but no crtc\n");
12574
3a3371ff 12575 for_each_intel_connector(dev, connector) {
8af6cf88
DV
12576 if (connector->base.encoder != &encoder->base)
12577 continue;
12578 enabled = true;
12579 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12580 active = true;
12581 }
0e32b39c
DA
12582 /*
12583 * for MST connectors if we unplug the connector is gone
12584 * away but the encoder is still connected to a crtc
12585 * until a modeset happens in response to the hotplug.
12586 */
12587 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12588 continue;
12589
e2c719b7 12590 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12591 "encoder's enabled state mismatch "
12592 "(expected %i, found %i)\n",
12593 !!encoder->base.crtc, enabled);
e2c719b7 12594 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
12595 "active encoder with no crtc\n");
12596
e2c719b7 12597 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
12598 "encoder's computed active state doesn't match tracked active state "
12599 "(expected %i, found %i)\n", active, encoder->connectors_active);
12600
12601 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 12602 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
12603 "encoder's hw state doesn't match sw tracking "
12604 "(expected %i, found %i)\n",
12605 encoder->connectors_active, active);
12606
12607 if (!encoder->base.crtc)
12608 continue;
12609
12610 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 12611 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
12612 "active encoder's pipe doesn't match"
12613 "(expected %i, found %i)\n",
12614 tracked_pipe, pipe);
12615
12616 }
91d1b4bd
DV
12617}
12618
12619static void
12620check_crtc_state(struct drm_device *dev)
12621{
fbee40df 12622 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12623 struct intel_crtc *crtc;
12624 struct intel_encoder *encoder;
5cec258b 12625 struct intel_crtc_state pipe_config;
8af6cf88 12626
d3fcc808 12627 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
12628 bool enabled = false;
12629 bool active = false;
12630
045ac3b5
JB
12631 memset(&pipe_config, 0, sizeof(pipe_config));
12632
8af6cf88
DV
12633 DRM_DEBUG_KMS("[CRTC:%d]\n",
12634 crtc->base.base.id);
12635
83d65738 12636 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
8af6cf88
DV
12637 "active crtc, but not enabled in sw tracking\n");
12638
b2784e15 12639 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12640 if (encoder->base.crtc != &crtc->base)
12641 continue;
12642 enabled = true;
12643 if (encoder->connectors_active)
12644 active = true;
12645 }
6c49f241 12646
e2c719b7 12647 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
12648 "crtc's computed active state doesn't match tracked active state "
12649 "(expected %i, found %i)\n", active, crtc->active);
83d65738 12650 I915_STATE_WARN(enabled != crtc->base.state->enable,
8af6cf88 12651 "crtc's computed enabled state doesn't match tracked enabled state "
83d65738
MR
12652 "(expected %i, found %i)\n", enabled,
12653 crtc->base.state->enable);
8af6cf88 12654
0e8ffe1b
DV
12655 active = dev_priv->display.get_pipe_config(crtc,
12656 &pipe_config);
d62cf62a 12657
b6b5d049
VS
12658 /* hw state is inconsistent with the pipe quirk */
12659 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12660 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
12661 active = crtc->active;
12662
b2784e15 12663 for_each_intel_encoder(dev, encoder) {
3eaba51c 12664 enum pipe pipe;
6c49f241
DV
12665 if (encoder->base.crtc != &crtc->base)
12666 continue;
1d37b689 12667 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
12668 encoder->get_config(encoder, &pipe_config);
12669 }
12670
e2c719b7 12671 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
12672 "crtc active state doesn't match with hw state "
12673 "(expected %i, found %i)\n", crtc->active, active);
12674
53d9f4e9
ML
12675 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12676 "transitional active state does not match atomic hw state "
12677 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12678
c0b03411 12679 if (active &&
6e3c9717 12680 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 12681 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
12682 intel_dump_pipe_config(crtc, &pipe_config,
12683 "[hw state]");
6e3c9717 12684 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
12685 "[sw state]");
12686 }
8af6cf88
DV
12687 }
12688}
12689
91d1b4bd
DV
12690static void
12691check_shared_dpll_state(struct drm_device *dev)
12692{
fbee40df 12693 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12694 struct intel_crtc *crtc;
12695 struct intel_dpll_hw_state dpll_hw_state;
12696 int i;
5358901f
DV
12697
12698 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12699 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12700 int enabled_crtcs = 0, active_crtcs = 0;
12701 bool active;
12702
12703 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12704
12705 DRM_DEBUG_KMS("%s\n", pll->name);
12706
12707 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12708
e2c719b7 12709 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12710 "more active pll users than references: %i vs %i\n",
3e369b76 12711 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12712 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12713 "pll in active use but not on in sw tracking\n");
e2c719b7 12714 I915_STATE_WARN(pll->on && !pll->active,
35c95375 12715 "pll in on but not on in use in sw tracking\n");
e2c719b7 12716 I915_STATE_WARN(pll->on != active,
5358901f
DV
12717 "pll on state mismatch (expected %i, found %i)\n",
12718 pll->on, active);
12719
d3fcc808 12720 for_each_intel_crtc(dev, crtc) {
83d65738 12721 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
12722 enabled_crtcs++;
12723 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12724 active_crtcs++;
12725 }
e2c719b7 12726 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
12727 "pll active crtcs mismatch (expected %i, found %i)\n",
12728 pll->active, active_crtcs);
e2c719b7 12729 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 12730 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 12731 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 12732
e2c719b7 12733 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12734 sizeof(dpll_hw_state)),
12735 "pll hw state mismatch\n");
5358901f 12736 }
8af6cf88
DV
12737}
12738
91d1b4bd
DV
12739void
12740intel_modeset_check_state(struct drm_device *dev)
12741{
08db6652 12742 check_wm_state(dev);
91d1b4bd
DV
12743 check_connector_state(dev);
12744 check_encoder_state(dev);
12745 check_crtc_state(dev);
12746 check_shared_dpll_state(dev);
12747}
12748
5cec258b 12749void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
12750 int dotclock)
12751{
12752 /*
12753 * FDI already provided one idea for the dotclock.
12754 * Yell if the encoder disagrees.
12755 */
2d112de7 12756 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 12757 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 12758 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
12759}
12760
80715b2f
VS
12761static void update_scanline_offset(struct intel_crtc *crtc)
12762{
12763 struct drm_device *dev = crtc->base.dev;
12764
12765 /*
12766 * The scanline counter increments at the leading edge of hsync.
12767 *
12768 * On most platforms it starts counting from vtotal-1 on the
12769 * first active line. That means the scanline counter value is
12770 * always one less than what we would expect. Ie. just after
12771 * start of vblank, which also occurs at start of hsync (on the
12772 * last active line), the scanline counter will read vblank_start-1.
12773 *
12774 * On gen2 the scanline counter starts counting from 1 instead
12775 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12776 * to keep the value positive), instead of adding one.
12777 *
12778 * On HSW+ the behaviour of the scanline counter depends on the output
12779 * type. For DP ports it behaves like most other platforms, but on HDMI
12780 * there's an extra 1 line difference. So we need to add two instead of
12781 * one to the value.
12782 */
12783 if (IS_GEN2(dev)) {
6e3c9717 12784 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12785 int vtotal;
12786
12787 vtotal = mode->crtc_vtotal;
12788 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12789 vtotal /= 2;
12790
12791 crtc->scanline_offset = vtotal - 1;
12792 } else if (HAS_DDI(dev) &&
409ee761 12793 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12794 crtc->scanline_offset = 2;
12795 } else
12796 crtc->scanline_offset = 1;
12797}
12798
c347a676 12799static int intel_modeset_setup_plls(struct drm_atomic_state *state)
ed6739ef 12800{
225da59b 12801 struct drm_device *dev = state->dev;
ed6739ef 12802 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303 12803 unsigned clear_pipes = 0;
ed6739ef 12804 struct intel_crtc *intel_crtc;
0a9ab303
ACO
12805 struct intel_crtc_state *intel_crtc_state;
12806 struct drm_crtc *crtc;
12807 struct drm_crtc_state *crtc_state;
ed6739ef 12808 int ret = 0;
0a9ab303 12809 int i;
ed6739ef
ACO
12810
12811 if (!dev_priv->display.crtc_compute_clock)
12812 return 0;
12813
0a9ab303
ACO
12814 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12815 intel_crtc = to_intel_crtc(crtc);
4978cc93 12816 intel_crtc_state = to_intel_crtc_state(crtc_state);
0a9ab303 12817
4978cc93 12818 if (needs_modeset(crtc_state)) {
0a9ab303 12819 clear_pipes |= 1 << intel_crtc->pipe;
4978cc93 12820 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
4978cc93 12821 }
0a9ab303
ACO
12822 }
12823
de419ab6
ML
12824 if (clear_pipes) {
12825 struct intel_shared_dpll_config *shared_dpll =
12826 intel_atomic_get_shared_dpll_state(state);
12827
12828 for (i = 0; i < dev_priv->num_shared_dpll; i++)
12829 shared_dpll[i].crtc_mask &= ~clear_pipes;
12830 }
ed6739ef 12831
0a9ab303
ACO
12832 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12833 if (!needs_modeset(crtc_state) || !crtc_state->enable)
225da59b
ACO
12834 continue;
12835
0a9ab303
ACO
12836 intel_crtc = to_intel_crtc(crtc);
12837 intel_crtc_state = to_intel_crtc_state(crtc_state);
12838
ed6739ef 12839 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
0a9ab303 12840 intel_crtc_state);
de419ab6
ML
12841 if (ret)
12842 return ret;
ed6739ef
ACO
12843 }
12844
ed6739ef
ACO
12845 return ret;
12846}
12847
99d736a2
ML
12848/*
12849 * This implements the workaround described in the "notes" section of the mode
12850 * set sequence documentation. When going from no pipes or single pipe to
12851 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12852 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12853 */
12854static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12855{
12856 struct drm_crtc_state *crtc_state;
12857 struct intel_crtc *intel_crtc;
12858 struct drm_crtc *crtc;
12859 struct intel_crtc_state *first_crtc_state = NULL;
12860 struct intel_crtc_state *other_crtc_state = NULL;
12861 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12862 int i;
12863
12864 /* look at all crtc's that are going to be enabled in during modeset */
12865 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12866 intel_crtc = to_intel_crtc(crtc);
12867
12868 if (!crtc_state->active || !needs_modeset(crtc_state))
12869 continue;
12870
12871 if (first_crtc_state) {
12872 other_crtc_state = to_intel_crtc_state(crtc_state);
12873 break;
12874 } else {
12875 first_crtc_state = to_intel_crtc_state(crtc_state);
12876 first_pipe = intel_crtc->pipe;
12877 }
12878 }
12879
12880 /* No workaround needed? */
12881 if (!first_crtc_state)
12882 return 0;
12883
12884 /* w/a possibly needed, check how many crtc's are already enabled. */
12885 for_each_intel_crtc(state->dev, intel_crtc) {
12886 struct intel_crtc_state *pipe_config;
12887
12888 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12889 if (IS_ERR(pipe_config))
12890 return PTR_ERR(pipe_config);
12891
12892 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12893
12894 if (!pipe_config->base.active ||
12895 needs_modeset(&pipe_config->base))
12896 continue;
12897
12898 /* 2 or more enabled crtcs means no need for w/a */
12899 if (enabled_pipe != INVALID_PIPE)
12900 return 0;
12901
12902 enabled_pipe = intel_crtc->pipe;
12903 }
12904
12905 if (enabled_pipe != INVALID_PIPE)
12906 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12907 else if (other_crtc_state)
12908 other_crtc_state->hsw_workaround_pipe = first_pipe;
12909
12910 return 0;
12911}
12912
054518dd 12913/* Code that should eventually be part of atomic_check() */
c347a676 12914static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd
ACO
12915{
12916 struct drm_device *dev = state->dev;
12917 int ret;
12918
12919 /*
12920 * See if the config requires any additional preparation, e.g.
12921 * to adjust global state with pipes off. We need to do this
12922 * here so we can get the modeset_pipe updated config for the new
12923 * mode set on this crtc. For other crtcs we need to use the
12924 * adjusted_mode bits in the crtc directly.
12925 */
b432e5cf
VS
12926 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
12927 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
12928 ret = valleyview_modeset_global_pipes(state);
12929 else
12930 ret = broadwell_modeset_global_pipes(state);
12931
054518dd
ACO
12932 if (ret)
12933 return ret;
12934 }
12935
99d736a2 12936 ret = intel_modeset_setup_plls(state);
054518dd
ACO
12937 if (ret)
12938 return ret;
12939
99d736a2
ML
12940 if (IS_HASWELL(dev))
12941 ret = haswell_mode_set_planes_workaround(state);
12942
12943 return ret;
c347a676
ACO
12944}
12945
12946static int
12947intel_modeset_compute_config(struct drm_atomic_state *state)
12948{
12949 struct drm_crtc *crtc;
12950 struct drm_crtc_state *crtc_state;
12951 int ret, i;
12952
12953 ret = drm_atomic_helper_check_modeset(state->dev, state);
054518dd
ACO
12954 if (ret)
12955 return ret;
12956
c347a676
ACO
12957 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12958 if (!crtc_state->enable &&
12959 WARN_ON(crtc_state->active))
12960 crtc_state->active = false;
12961
12962 if (!crtc_state->enable)
12963 continue;
12964
12965 ret = intel_modeset_pipe_config(crtc, state);
12966 if (ret)
12967 return ret;
12968
12969 intel_dump_pipe_config(to_intel_crtc(crtc),
12970 to_intel_crtc_state(crtc_state),
12971 "[modeset]");
12972 }
12973
12974 ret = intel_modeset_checks(state);
12975 if (ret)
12976 return ret;
12977
12978 return drm_atomic_helper_check_planes(state->dev, state);
054518dd
ACO
12979}
12980
c72d969b 12981static int __intel_set_mode(struct drm_atomic_state *state)
a6778b3c 12982{
c72d969b 12983 struct drm_device *dev = state->dev;
fbee40df 12984 struct drm_i915_private *dev_priv = dev->dev_private;
0a9ab303
ACO
12985 struct drm_crtc *crtc;
12986 struct drm_crtc_state *crtc_state;
c0c36b94 12987 int ret = 0;
0a9ab303 12988 int i;
a6778b3c 12989
d4afb8cc
ACO
12990 ret = drm_atomic_helper_prepare_planes(dev, state);
12991 if (ret)
12992 return ret;
12993
1c5e19f8
ML
12994 drm_atomic_helper_swap_state(dev, state);
12995
0a9ab303 12996 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1c5e19f8 12997 if (!needs_modeset(crtc->state) || !crtc_state->active)
0a9ab303 12998 continue;
460da916 12999
69024de8
ML
13000 intel_crtc_disable_planes(crtc);
13001 dev_priv->display.crtc_disable(crtc);
b8cecdf5 13002 }
7758a113 13003
ea9d758d
DV
13004 /* Only after disabling all output pipelines that will be changed can we
13005 * update the the output configuration. */
0a9ab303 13006 intel_modeset_update_state(state);
f6e5b160 13007
a821fc46
ACO
13008 /* The state has been swaped above, so state actually contains the
13009 * old state now. */
13010
304603f4 13011 modeset_update_crtc_power_domains(state);
47fab737 13012
a6778b3c 13013 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
0a9ab303 13014 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5ac1c4bc
ML
13015 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
13016
53d9f4e9 13017 if (!needs_modeset(crtc->state) || !crtc->state->active)
0a9ab303
ACO
13018 continue;
13019
13020 update_scanline_offset(to_intel_crtc(crtc));
80715b2f 13021
0a9ab303
ACO
13022 dev_priv->display.crtc_enable(crtc);
13023 intel_crtc_enable_planes(crtc);
80715b2f 13024 }
a6778b3c 13025
a6778b3c 13026 /* FIXME: add subpixel order */
83a57153 13027
d4afb8cc
ACO
13028 drm_atomic_helper_cleanup_planes(dev, state);
13029
2bfb4627
ACO
13030 drm_atomic_state_free(state);
13031
9eb45f22 13032 return 0;
f6e5b160
CW
13033}
13034
568c634a 13035static int intel_set_mode_checked(struct drm_atomic_state *state)
f30da187 13036{
568c634a 13037 struct drm_device *dev = state->dev;
f30da187
DV
13038 int ret;
13039
568c634a 13040 ret = __intel_set_mode(state);
f30da187 13041 if (ret == 0)
568c634a 13042 intel_modeset_check_state(dev);
f30da187
DV
13043
13044 return ret;
13045}
13046
568c634a 13047static int intel_set_mode(struct drm_atomic_state *state)
7f27126e 13048{
568c634a 13049 int ret;
83a57153 13050
568c634a 13051 ret = intel_modeset_compute_config(state);
83a57153 13052 if (ret)
568c634a 13053 return ret;
7f27126e 13054
568c634a 13055 return intel_set_mode_checked(state);
7f27126e
JB
13056}
13057
c0c36b94
CW
13058void intel_crtc_restore_mode(struct drm_crtc *crtc)
13059{
83a57153
ACO
13060 struct drm_device *dev = crtc->dev;
13061 struct drm_atomic_state *state;
4be07317 13062 struct intel_crtc *intel_crtc;
83a57153
ACO
13063 struct intel_encoder *encoder;
13064 struct intel_connector *connector;
13065 struct drm_connector_state *connector_state;
4be07317 13066 struct intel_crtc_state *crtc_state;
2bfb4627 13067 int ret;
83a57153
ACO
13068
13069 state = drm_atomic_state_alloc(dev);
13070 if (!state) {
13071 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13072 crtc->base.id);
13073 return;
13074 }
13075
13076 state->acquire_ctx = dev->mode_config.acquire_ctx;
13077
13078 /* The force restore path in the HW readout code relies on the staged
13079 * config still keeping the user requested config while the actual
13080 * state has been overwritten by the configuration read from HW. We
13081 * need to copy the staged config to the atomic state, otherwise the
13082 * mode set will just reapply the state the HW is already in. */
13083 for_each_intel_encoder(dev, encoder) {
13084 if (&encoder->new_crtc->base != crtc)
13085 continue;
13086
13087 for_each_intel_connector(dev, connector) {
13088 if (connector->new_encoder != encoder)
13089 continue;
13090
13091 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13092 if (IS_ERR(connector_state)) {
13093 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13094 connector->base.base.id,
13095 connector->base.name,
13096 PTR_ERR(connector_state));
13097 continue;
13098 }
13099
13100 connector_state->crtc = crtc;
13101 connector_state->best_encoder = &encoder->base;
13102 }
13103 }
13104
4be07317
ACO
13105 for_each_intel_crtc(dev, intel_crtc) {
13106 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
13107 continue;
13108
13109 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
13110 if (IS_ERR(crtc_state)) {
13111 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13112 intel_crtc->base.base.id,
13113 PTR_ERR(crtc_state));
13114 continue;
13115 }
13116
49d6fa21
ML
13117 crtc_state->base.active = crtc_state->base.enable =
13118 intel_crtc->new_enabled;
8c7b5ccb
ACO
13119
13120 if (&intel_crtc->base == crtc)
13121 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
4be07317
ACO
13122 }
13123
d3a40d1b
ACO
13124 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13125 crtc->primary->fb, crtc->x, crtc->y);
13126
568c634a 13127 ret = intel_set_mode(state);
2bfb4627
ACO
13128 if (ret)
13129 drm_atomic_state_free(state);
c0c36b94
CW
13130}
13131
25c5b266
DV
13132#undef for_each_intel_crtc_masked
13133
b7885264
ACO
13134static bool intel_connector_in_mode_set(struct intel_connector *connector,
13135 struct drm_mode_set *set)
13136{
13137 int ro;
13138
13139 for (ro = 0; ro < set->num_connectors; ro++)
13140 if (set->connectors[ro] == &connector->base)
13141 return true;
13142
13143 return false;
13144}
13145
2e431051 13146static int
9a935856
DV
13147intel_modeset_stage_output_state(struct drm_device *dev,
13148 struct drm_mode_set *set,
944b0c76 13149 struct drm_atomic_state *state)
50f56119 13150{
9a935856 13151 struct intel_connector *connector;
d5432a9d 13152 struct drm_connector *drm_connector;
944b0c76 13153 struct drm_connector_state *connector_state;
d5432a9d
ACO
13154 struct drm_crtc *crtc;
13155 struct drm_crtc_state *crtc_state;
13156 int i, ret;
50f56119 13157
9abdda74 13158 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
13159 * of connectors. For paranoia, double-check this. */
13160 WARN_ON(!set->fb && (set->num_connectors != 0));
13161 WARN_ON(set->fb && (set->num_connectors == 0));
13162
3a3371ff 13163 for_each_intel_connector(dev, connector) {
b7885264
ACO
13164 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13165
d5432a9d
ACO
13166 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13167 continue;
13168
13169 connector_state =
13170 drm_atomic_get_connector_state(state, &connector->base);
13171 if (IS_ERR(connector_state))
13172 return PTR_ERR(connector_state);
13173
b7885264
ACO
13174 if (in_mode_set) {
13175 int pipe = to_intel_crtc(set->crtc)->pipe;
d5432a9d
ACO
13176 connector_state->best_encoder =
13177 &intel_find_encoder(connector, pipe)->base;
50f56119
DV
13178 }
13179
d5432a9d 13180 if (connector->base.state->crtc != set->crtc)
b7885264
ACO
13181 continue;
13182
9a935856
DV
13183 /* If we disable the crtc, disable all its connectors. Also, if
13184 * the connector is on the changing crtc but not on the new
13185 * connector list, disable it. */
b7885264 13186 if (!set->fb || !in_mode_set) {
d5432a9d 13187 connector_state->best_encoder = NULL;
9a935856
DV
13188
13189 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13190 connector->base.base.id,
c23cc417 13191 connector->base.name);
9a935856 13192 }
50f56119 13193 }
9a935856 13194 /* connector->new_encoder is now updated for all connectors. */
50f56119 13195
d5432a9d
ACO
13196 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13197 connector = to_intel_connector(drm_connector);
13198
13199 if (!connector_state->best_encoder) {
13200 ret = drm_atomic_set_crtc_for_connector(connector_state,
13201 NULL);
13202 if (ret)
13203 return ret;
7668851f 13204
50f56119 13205 continue;
d5432a9d 13206 }
50f56119 13207
d5432a9d
ACO
13208 if (intel_connector_in_mode_set(connector, set)) {
13209 struct drm_crtc *crtc = connector->base.state->crtc;
13210
13211 /* If this connector was in a previous crtc, add it
13212 * to the state. We might need to disable it. */
13213 if (crtc) {
13214 crtc_state =
13215 drm_atomic_get_crtc_state(state, crtc);
13216 if (IS_ERR(crtc_state))
13217 return PTR_ERR(crtc_state);
13218 }
13219
13220 ret = drm_atomic_set_crtc_for_connector(connector_state,
13221 set->crtc);
13222 if (ret)
13223 return ret;
13224 }
50f56119
DV
13225
13226 /* Make sure the new CRTC will work with the encoder */
d5432a9d
ACO
13227 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13228 connector_state->crtc)) {
5e2b584e 13229 return -EINVAL;
50f56119 13230 }
944b0c76 13231
9a935856
DV
13232 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13233 connector->base.base.id,
c23cc417 13234 connector->base.name,
d5432a9d 13235 connector_state->crtc->base.id);
944b0c76 13236
d5432a9d
ACO
13237 if (connector_state->best_encoder != &connector->encoder->base)
13238 connector->encoder =
13239 to_intel_encoder(connector_state->best_encoder);
0e32b39c 13240 }
7668851f 13241
d5432a9d 13242 for_each_crtc_in_state(state, crtc, crtc_state, i) {
49d6fa21
ML
13243 bool has_connectors;
13244
d5432a9d
ACO
13245 ret = drm_atomic_add_affected_connectors(state, crtc);
13246 if (ret)
13247 return ret;
4be07317 13248
49d6fa21
ML
13249 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13250 if (has_connectors != crtc_state->enable)
13251 crtc_state->enable =
13252 crtc_state->active = has_connectors;
7668851f
VS
13253 }
13254
8c7b5ccb
ACO
13255 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13256 set->fb, set->x, set->y);
13257 if (ret)
13258 return ret;
13259
13260 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13261 if (IS_ERR(crtc_state))
13262 return PTR_ERR(crtc_state);
13263
13264 if (set->mode)
13265 drm_mode_copy(&crtc_state->mode, set->mode);
13266
13267 if (set->num_connectors)
13268 crtc_state->active = true;
13269
2e431051
DV
13270 return 0;
13271}
13272
13273static int intel_crtc_set_config(struct drm_mode_set *set)
13274{
13275 struct drm_device *dev;
83a57153 13276 struct drm_atomic_state *state = NULL;
2e431051 13277 int ret;
2e431051 13278
8d3e375e
DV
13279 BUG_ON(!set);
13280 BUG_ON(!set->crtc);
13281 BUG_ON(!set->crtc->helper_private);
2e431051 13282
7e53f3a4
DV
13283 /* Enforce sane interface api - has been abused by the fb helper. */
13284 BUG_ON(!set->mode && set->fb);
13285 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 13286
2e431051
DV
13287 if (set->fb) {
13288 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13289 set->crtc->base.id, set->fb->base.id,
13290 (int)set->num_connectors, set->x, set->y);
13291 } else {
13292 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
13293 }
13294
13295 dev = set->crtc->dev;
13296
83a57153 13297 state = drm_atomic_state_alloc(dev);
7cbf41d6
ACO
13298 if (!state)
13299 return -ENOMEM;
83a57153
ACO
13300
13301 state->acquire_ctx = dev->mode_config.acquire_ctx;
13302
462a425a 13303 ret = intel_modeset_stage_output_state(dev, set, state);
2e431051 13304 if (ret)
7cbf41d6 13305 goto out;
2e431051 13306
568c634a
ACO
13307 ret = intel_modeset_compute_config(state);
13308 if (ret)
7cbf41d6 13309 goto out;
50f52756 13310
1f9954d0
JB
13311 intel_update_pipe_size(to_intel_crtc(set->crtc));
13312
568c634a 13313 ret = intel_set_mode_checked(state);
2d05eae1 13314 if (ret) {
bf67dfeb
DV
13315 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13316 set->crtc->base.id, ret);
2d05eae1 13317 }
50f56119 13318
7cbf41d6 13319out:
2bfb4627
ACO
13320 if (ret)
13321 drm_atomic_state_free(state);
50f56119
DV
13322 return ret;
13323}
f6e5b160
CW
13324
13325static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13326 .gamma_set = intel_crtc_gamma_set,
50f56119 13327 .set_config = intel_crtc_set_config,
f6e5b160
CW
13328 .destroy = intel_crtc_destroy,
13329 .page_flip = intel_crtc_page_flip,
1356837e
MR
13330 .atomic_duplicate_state = intel_crtc_duplicate_state,
13331 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13332};
13333
5358901f
DV
13334static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13335 struct intel_shared_dpll *pll,
13336 struct intel_dpll_hw_state *hw_state)
ee7b9f93 13337{
5358901f 13338 uint32_t val;
ee7b9f93 13339
f458ebbc 13340 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
13341 return false;
13342
5358901f 13343 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
13344 hw_state->dpll = val;
13345 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13346 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
13347
13348 return val & DPLL_VCO_ENABLE;
13349}
13350
15bdd4cf
DV
13351static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13352 struct intel_shared_dpll *pll)
13353{
3e369b76
ACO
13354 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13355 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
13356}
13357
e7b903d2
DV
13358static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13359 struct intel_shared_dpll *pll)
13360{
e7b903d2 13361 /* PCH refclock must be enabled first */
89eff4be 13362 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 13363
3e369b76 13364 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
13365
13366 /* Wait for the clocks to stabilize. */
13367 POSTING_READ(PCH_DPLL(pll->id));
13368 udelay(150);
13369
13370 /* The pixel multiplier can only be updated once the
13371 * DPLL is enabled and the clocks are stable.
13372 *
13373 * So write it again.
13374 */
3e369b76 13375 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 13376 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13377 udelay(200);
13378}
13379
13380static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13381 struct intel_shared_dpll *pll)
13382{
13383 struct drm_device *dev = dev_priv->dev;
13384 struct intel_crtc *crtc;
e7b903d2
DV
13385
13386 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 13387 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
13388 if (intel_crtc_to_shared_dpll(crtc) == pll)
13389 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
13390 }
13391
15bdd4cf
DV
13392 I915_WRITE(PCH_DPLL(pll->id), 0);
13393 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13394 udelay(200);
13395}
13396
46edb027
DV
13397static char *ibx_pch_dpll_names[] = {
13398 "PCH DPLL A",
13399 "PCH DPLL B",
13400};
13401
7c74ade1 13402static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 13403{
e7b903d2 13404 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
13405 int i;
13406
7c74ade1 13407 dev_priv->num_shared_dpll = 2;
ee7b9f93 13408
e72f9fbf 13409 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
13410 dev_priv->shared_dplls[i].id = i;
13411 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 13412 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
13413 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13414 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
13415 dev_priv->shared_dplls[i].get_hw_state =
13416 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
13417 }
13418}
13419
7c74ade1
DV
13420static void intel_shared_dpll_init(struct drm_device *dev)
13421{
e7b903d2 13422 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 13423
b6283055
VS
13424 intel_update_cdclk(dev);
13425
9cd86933
DV
13426 if (HAS_DDI(dev))
13427 intel_ddi_pll_init(dev);
13428 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
13429 ibx_pch_dpll_init(dev);
13430 else
13431 dev_priv->num_shared_dpll = 0;
13432
13433 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
13434}
13435
1fc0a8f7
TU
13436/**
13437 * intel_wm_need_update - Check whether watermarks need updating
13438 * @plane: drm plane
13439 * @state: new plane state
13440 *
13441 * Check current plane state versus the new one to determine whether
13442 * watermarks need to be recalculated.
13443 *
13444 * Returns true or false.
13445 */
13446bool intel_wm_need_update(struct drm_plane *plane,
13447 struct drm_plane_state *state)
13448{
13449 /* Update watermarks on tiling changes. */
13450 if (!plane->state->fb || !state->fb ||
13451 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13452 plane->state->rotation != state->rotation)
13453 return true;
13454
13455 return false;
13456}
13457
6beb8c23
MR
13458/**
13459 * intel_prepare_plane_fb - Prepare fb for usage on plane
13460 * @plane: drm plane to prepare for
13461 * @fb: framebuffer to prepare for presentation
13462 *
13463 * Prepares a framebuffer for usage on a display plane. Generally this
13464 * involves pinning the underlying object and updating the frontbuffer tracking
13465 * bits. Some older platforms need special physical address handling for
13466 * cursor planes.
13467 *
13468 * Returns 0 on success, negative error code on failure.
13469 */
13470int
13471intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee
TU
13472 struct drm_framebuffer *fb,
13473 const struct drm_plane_state *new_state)
465c120c
MR
13474{
13475 struct drm_device *dev = plane->dev;
6beb8c23
MR
13476 struct intel_plane *intel_plane = to_intel_plane(plane);
13477 enum pipe pipe = intel_plane->pipe;
13478 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13479 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13480 unsigned frontbuffer_bits = 0;
13481 int ret = 0;
465c120c 13482
ea2c67bb 13483 if (!obj)
465c120c
MR
13484 return 0;
13485
6beb8c23
MR
13486 switch (plane->type) {
13487 case DRM_PLANE_TYPE_PRIMARY:
13488 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13489 break;
13490 case DRM_PLANE_TYPE_CURSOR:
13491 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13492 break;
13493 case DRM_PLANE_TYPE_OVERLAY:
13494 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13495 break;
13496 }
465c120c 13497
6beb8c23 13498 mutex_lock(&dev->struct_mutex);
465c120c 13499
6beb8c23
MR
13500 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13501 INTEL_INFO(dev)->cursor_needs_physical) {
13502 int align = IS_I830(dev) ? 16 * 1024 : 256;
13503 ret = i915_gem_object_attach_phys(obj, align);
13504 if (ret)
13505 DRM_DEBUG_KMS("failed to attach phys object\n");
13506 } else {
82bc3b2d 13507 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
6beb8c23 13508 }
465c120c 13509
6beb8c23
MR
13510 if (ret == 0)
13511 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
fdd508a6 13512
4c34574f 13513 mutex_unlock(&dev->struct_mutex);
465c120c 13514
6beb8c23
MR
13515 return ret;
13516}
13517
38f3ce3a
MR
13518/**
13519 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13520 * @plane: drm plane to clean up for
13521 * @fb: old framebuffer that was on plane
13522 *
13523 * Cleans up a framebuffer that has just been removed from a plane.
13524 */
13525void
13526intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee
TU
13527 struct drm_framebuffer *fb,
13528 const struct drm_plane_state *old_state)
38f3ce3a
MR
13529{
13530 struct drm_device *dev = plane->dev;
13531 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13532
13533 if (WARN_ON(!obj))
13534 return;
13535
13536 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13537 !INTEL_INFO(dev)->cursor_needs_physical) {
13538 mutex_lock(&dev->struct_mutex);
82bc3b2d 13539 intel_unpin_fb_obj(fb, old_state);
38f3ce3a
MR
13540 mutex_unlock(&dev->struct_mutex);
13541 }
465c120c
MR
13542}
13543
6156a456
CK
13544int
13545skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13546{
13547 int max_scale;
13548 struct drm_device *dev;
13549 struct drm_i915_private *dev_priv;
13550 int crtc_clock, cdclk;
13551
13552 if (!intel_crtc || !crtc_state)
13553 return DRM_PLANE_HELPER_NO_SCALING;
13554
13555 dev = intel_crtc->base.dev;
13556 dev_priv = dev->dev_private;
13557 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13558 cdclk = dev_priv->display.get_display_clock_speed(dev);
13559
13560 if (!crtc_clock || !cdclk)
13561 return DRM_PLANE_HELPER_NO_SCALING;
13562
13563 /*
13564 * skl max scale is lower of:
13565 * close to 3 but not 3, -1 is for that purpose
13566 * or
13567 * cdclk/crtc_clock
13568 */
13569 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13570
13571 return max_scale;
13572}
13573
465c120c 13574static int
3c692a41
GP
13575intel_check_primary_plane(struct drm_plane *plane,
13576 struct intel_plane_state *state)
13577{
32b7eeec
MR
13578 struct drm_device *dev = plane->dev;
13579 struct drm_i915_private *dev_priv = dev->dev_private;
2b875c22 13580 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13581 struct intel_crtc *intel_crtc;
6156a456 13582 struct intel_crtc_state *crtc_state;
2b875c22 13583 struct drm_framebuffer *fb = state->base.fb;
3c692a41
GP
13584 struct drm_rect *dest = &state->dst;
13585 struct drm_rect *src = &state->src;
13586 const struct drm_rect *clip = &state->clip;
d8106366 13587 bool can_position = false;
6156a456
CK
13588 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13589 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
465c120c
MR
13590 int ret;
13591
ea2c67bb
MR
13592 crtc = crtc ? crtc : plane->crtc;
13593 intel_crtc = to_intel_crtc(crtc);
6156a456
CK
13594 crtc_state = state->base.state ?
13595 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
ea2c67bb 13596
6156a456 13597 if (INTEL_INFO(dev)->gen >= 9) {
225c228a
CK
13598 /* use scaler when colorkey is not required */
13599 if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
13600 min_scale = 1;
13601 max_scale = skl_max_scale(intel_crtc, crtc_state);
13602 }
d8106366 13603 can_position = true;
6156a456 13604 }
d8106366 13605
c59cb179
MR
13606 ret = drm_plane_helper_check_update(plane, crtc, fb,
13607 src, dest, clip,
6156a456
CK
13608 min_scale,
13609 max_scale,
d8106366
SJ
13610 can_position, true,
13611 &state->visible);
c59cb179
MR
13612 if (ret)
13613 return ret;
465c120c 13614
32b7eeec 13615 if (intel_crtc->active) {
b70709a6
ML
13616 struct intel_plane_state *old_state =
13617 to_intel_plane_state(plane->state);
13618
32b7eeec
MR
13619 intel_crtc->atomic.wait_for_flips = true;
13620
13621 /*
13622 * FBC does not work on some platforms for rotated
13623 * planes, so disable it when rotation is not 0 and
13624 * update it when rotation is set back to 0.
13625 *
13626 * FIXME: This is redundant with the fbc update done in
13627 * the primary plane enable function except that that
13628 * one is done too late. We eventually need to unify
13629 * this.
13630 */
b70709a6 13631 if (state->visible &&
32b7eeec 13632 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
e35fef21 13633 dev_priv->fbc.crtc == intel_crtc &&
8e7d688b 13634 state->base.rotation != BIT(DRM_ROTATE_0)) {
32b7eeec
MR
13635 intel_crtc->atomic.disable_fbc = true;
13636 }
13637
b70709a6 13638 if (state->visible && !old_state->visible) {
32b7eeec
MR
13639 /*
13640 * BDW signals flip done immediately if the plane
13641 * is disabled, even if the plane enable is already
13642 * armed to occur at the next vblank :(
13643 */
b70709a6 13644 if (IS_BROADWELL(dev))
32b7eeec 13645 intel_crtc->atomic.wait_vblank = true;
fb9d6cf8
ML
13646
13647 if (crtc_state && !needs_modeset(&crtc_state->base))
13648 intel_crtc->atomic.post_enable_primary = true;
32b7eeec
MR
13649 }
13650
fb9d6cf8
ML
13651 if (!state->visible && old_state->visible &&
13652 crtc_state && !needs_modeset(&crtc_state->base))
13653 intel_crtc->atomic.pre_disable_primary = true;
13654
32b7eeec
MR
13655 intel_crtc->atomic.fb_bits |=
13656 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13657
13658 intel_crtc->atomic.update_fbc = true;
0fda6568 13659
1fc0a8f7 13660 if (intel_wm_need_update(plane, &state->base))
0fda6568 13661 intel_crtc->atomic.update_wm = true;
ccc759dc
GP
13662 }
13663
6156a456
CK
13664 if (INTEL_INFO(dev)->gen >= 9) {
13665 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13666 to_intel_plane(plane), state, 0);
13667 if (ret)
13668 return ret;
13669 }
13670
14af293f
GP
13671 return 0;
13672}
13673
13674static void
13675intel_commit_primary_plane(struct drm_plane *plane,
13676 struct intel_plane_state *state)
13677{
2b875c22
MR
13678 struct drm_crtc *crtc = state->base.crtc;
13679 struct drm_framebuffer *fb = state->base.fb;
13680 struct drm_device *dev = plane->dev;
14af293f 13681 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 13682 struct intel_crtc *intel_crtc;
14af293f
GP
13683 struct drm_rect *src = &state->src;
13684
ea2c67bb
MR
13685 crtc = crtc ? crtc : plane->crtc;
13686 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
13687
13688 plane->fb = fb;
9dc806fc
MR
13689 crtc->x = src->x1 >> 16;
13690 crtc->y = src->y1 >> 16;
ccc759dc 13691
ccc759dc 13692 if (intel_crtc->active) {
27321ae8 13693 if (state->visible)
ccc759dc
GP
13694 /* FIXME: kill this fastboot hack */
13695 intel_update_pipe_size(intel_crtc);
465c120c 13696
27321ae8
ML
13697 dev_priv->display.update_primary_plane(crtc, plane->fb,
13698 crtc->x, crtc->y);
ccc759dc 13699 }
465c120c
MR
13700}
13701
a8ad0d8e
ML
13702static void
13703intel_disable_primary_plane(struct drm_plane *plane,
13704 struct drm_crtc *crtc,
13705 bool force)
13706{
13707 struct drm_device *dev = plane->dev;
13708 struct drm_i915_private *dev_priv = dev->dev_private;
13709
a8ad0d8e
ML
13710 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13711}
13712
32b7eeec 13713static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 13714{
32b7eeec 13715 struct drm_device *dev = crtc->dev;
140fd38d 13716 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 13717 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5c2db188 13718 struct drm_crtc_state *crtc_state = intel_crtc->base.state;
ea2c67bb
MR
13719 struct intel_plane *intel_plane;
13720 struct drm_plane *p;
13721 unsigned fb_bits = 0;
13722
13723 /* Track fb's for any planes being disabled */
13724 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13725 intel_plane = to_intel_plane(p);
13726
13727 if (intel_crtc->atomic.disabled_planes &
13728 (1 << drm_plane_index(p))) {
13729 switch (p->type) {
13730 case DRM_PLANE_TYPE_PRIMARY:
13731 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13732 break;
13733 case DRM_PLANE_TYPE_CURSOR:
13734 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13735 break;
13736 case DRM_PLANE_TYPE_OVERLAY:
13737 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13738 break;
13739 }
3c692a41 13740
ea2c67bb
MR
13741 mutex_lock(&dev->struct_mutex);
13742 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13743 mutex_unlock(&dev->struct_mutex);
13744 }
13745 }
3c692a41 13746
32b7eeec
MR
13747 if (intel_crtc->atomic.wait_for_flips)
13748 intel_crtc_wait_for_pending_flips(crtc);
3c692a41 13749
32b7eeec
MR
13750 if (intel_crtc->atomic.disable_fbc)
13751 intel_fbc_disable(dev);
3c692a41 13752
32b7eeec
MR
13753 if (intel_crtc->atomic.pre_disable_primary)
13754 intel_pre_disable_primary(crtc);
3c692a41 13755
32b7eeec
MR
13756 if (intel_crtc->atomic.update_wm)
13757 intel_update_watermarks(crtc);
3c692a41 13758
32b7eeec 13759 intel_runtime_pm_get(dev_priv);
3c692a41 13760
c34c9ee4 13761 /* Perform vblank evasion around commit operation */
5c2db188 13762 if (crtc_state->active && !needs_modeset(crtc_state))
c34c9ee4
MR
13763 intel_crtc->atomic.evade =
13764 intel_pipe_update_start(intel_crtc,
13765 &intel_crtc->atomic.start_vbl_count);
32b7eeec
MR
13766}
13767
13768static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13769{
13770 struct drm_device *dev = crtc->dev;
13771 struct drm_i915_private *dev_priv = dev->dev_private;
13772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13773 struct drm_plane *p;
13774
c34c9ee4
MR
13775 if (intel_crtc->atomic.evade)
13776 intel_pipe_update_end(intel_crtc,
13777 intel_crtc->atomic.start_vbl_count);
3c692a41 13778
140fd38d 13779 intel_runtime_pm_put(dev_priv);
3c692a41 13780
8a8f7f44 13781 if (intel_crtc->atomic.wait_vblank && intel_crtc->active)
32b7eeec
MR
13782 intel_wait_for_vblank(dev, intel_crtc->pipe);
13783
13784 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13785
13786 if (intel_crtc->atomic.update_fbc) {
ccc759dc 13787 mutex_lock(&dev->struct_mutex);
7ff0ebcc 13788 intel_fbc_update(dev);
ccc759dc 13789 mutex_unlock(&dev->struct_mutex);
38f3ce3a 13790 }
3c692a41 13791
32b7eeec
MR
13792 if (intel_crtc->atomic.post_enable_primary)
13793 intel_post_enable_primary(crtc);
3c692a41 13794
32b7eeec
MR
13795 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13796 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13797 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13798 false, false);
13799
13800 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
3c692a41
GP
13801}
13802
cf4c7c12 13803/**
4a3b8769
MR
13804 * intel_plane_destroy - destroy a plane
13805 * @plane: plane to destroy
cf4c7c12 13806 *
4a3b8769
MR
13807 * Common destruction function for all types of planes (primary, cursor,
13808 * sprite).
cf4c7c12 13809 */
4a3b8769 13810void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13811{
13812 struct intel_plane *intel_plane = to_intel_plane(plane);
13813 drm_plane_cleanup(plane);
13814 kfree(intel_plane);
13815}
13816
65a3fea0 13817const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13818 .update_plane = drm_atomic_helper_update_plane,
13819 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13820 .destroy = intel_plane_destroy,
c196e1d6 13821 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13822 .atomic_get_property = intel_plane_atomic_get_property,
13823 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13824 .atomic_duplicate_state = intel_plane_duplicate_state,
13825 .atomic_destroy_state = intel_plane_destroy_state,
13826
465c120c
MR
13827};
13828
13829static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13830 int pipe)
13831{
13832 struct intel_plane *primary;
8e7d688b 13833 struct intel_plane_state *state;
465c120c
MR
13834 const uint32_t *intel_primary_formats;
13835 int num_formats;
13836
13837 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13838 if (primary == NULL)
13839 return NULL;
13840
8e7d688b
MR
13841 state = intel_create_plane_state(&primary->base);
13842 if (!state) {
ea2c67bb
MR
13843 kfree(primary);
13844 return NULL;
13845 }
8e7d688b 13846 primary->base.state = &state->base;
ea2c67bb 13847
465c120c
MR
13848 primary->can_scale = false;
13849 primary->max_downscale = 1;
6156a456
CK
13850 if (INTEL_INFO(dev)->gen >= 9) {
13851 primary->can_scale = true;
af99ceda 13852 state->scaler_id = -1;
6156a456 13853 }
465c120c
MR
13854 primary->pipe = pipe;
13855 primary->plane = pipe;
c59cb179
MR
13856 primary->check_plane = intel_check_primary_plane;
13857 primary->commit_plane = intel_commit_primary_plane;
a8ad0d8e 13858 primary->disable_plane = intel_disable_primary_plane;
08e221fb 13859 primary->ckey.flags = I915_SET_COLORKEY_NONE;
465c120c
MR
13860 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13861 primary->plane = !pipe;
13862
6c0fd451
DL
13863 if (INTEL_INFO(dev)->gen >= 9) {
13864 intel_primary_formats = skl_primary_formats;
13865 num_formats = ARRAY_SIZE(skl_primary_formats);
13866 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
13867 intel_primary_formats = i965_primary_formats;
13868 num_formats = ARRAY_SIZE(i965_primary_formats);
6c0fd451
DL
13869 } else {
13870 intel_primary_formats = i8xx_primary_formats;
13871 num_formats = ARRAY_SIZE(i8xx_primary_formats);
465c120c
MR
13872 }
13873
13874 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13875 &intel_plane_funcs,
465c120c
MR
13876 intel_primary_formats, num_formats,
13877 DRM_PLANE_TYPE_PRIMARY);
48404c1e 13878
3b7a5119
SJ
13879 if (INTEL_INFO(dev)->gen >= 4)
13880 intel_create_rotation_property(dev, primary);
48404c1e 13881
ea2c67bb
MR
13882 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13883
465c120c
MR
13884 return &primary->base;
13885}
13886
3b7a5119
SJ
13887void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13888{
13889 if (!dev->mode_config.rotation_property) {
13890 unsigned long flags = BIT(DRM_ROTATE_0) |
13891 BIT(DRM_ROTATE_180);
13892
13893 if (INTEL_INFO(dev)->gen >= 9)
13894 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13895
13896 dev->mode_config.rotation_property =
13897 drm_mode_create_rotation_property(dev, flags);
13898 }
13899 if (dev->mode_config.rotation_property)
13900 drm_object_attach_property(&plane->base.base,
13901 dev->mode_config.rotation_property,
13902 plane->base.state->rotation);
13903}
13904
3d7d6510 13905static int
852e787c
GP
13906intel_check_cursor_plane(struct drm_plane *plane,
13907 struct intel_plane_state *state)
3d7d6510 13908{
2b875c22 13909 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13910 struct drm_device *dev = plane->dev;
2b875c22 13911 struct drm_framebuffer *fb = state->base.fb;
852e787c
GP
13912 struct drm_rect *dest = &state->dst;
13913 struct drm_rect *src = &state->src;
13914 const struct drm_rect *clip = &state->clip;
757f9a3e 13915 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ea2c67bb 13916 struct intel_crtc *intel_crtc;
757f9a3e
GP
13917 unsigned stride;
13918 int ret;
3d7d6510 13919
ea2c67bb
MR
13920 crtc = crtc ? crtc : plane->crtc;
13921 intel_crtc = to_intel_crtc(crtc);
13922
757f9a3e 13923 ret = drm_plane_helper_check_update(plane, crtc, fb,
852e787c 13924 src, dest, clip,
3d7d6510
MR
13925 DRM_PLANE_HELPER_NO_SCALING,
13926 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13927 true, true, &state->visible);
757f9a3e
GP
13928 if (ret)
13929 return ret;
13930
13931
13932 /* if we want to turn off the cursor ignore width and height */
13933 if (!obj)
32b7eeec 13934 goto finish;
757f9a3e 13935
757f9a3e 13936 /* Check for which cursor types we support */
ea2c67bb
MR
13937 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13938 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13939 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13940 return -EINVAL;
13941 }
13942
ea2c67bb
MR
13943 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13944 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13945 DRM_DEBUG_KMS("buffer is too small\n");
13946 return -ENOMEM;
13947 }
13948
3a656b54 13949 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e
GP
13950 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13951 ret = -EINVAL;
13952 }
757f9a3e 13953
32b7eeec
MR
13954finish:
13955 if (intel_crtc->active) {
3749f463 13956 if (plane->state->crtc_w != state->base.crtc_w)
32b7eeec
MR
13957 intel_crtc->atomic.update_wm = true;
13958
13959 intel_crtc->atomic.fb_bits |=
13960 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13961 }
13962
757f9a3e 13963 return ret;
852e787c 13964}
3d7d6510 13965
a8ad0d8e
ML
13966static void
13967intel_disable_cursor_plane(struct drm_plane *plane,
13968 struct drm_crtc *crtc,
13969 bool force)
13970{
13971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13972
13973 if (!force) {
13974 plane->fb = NULL;
13975 intel_crtc->cursor_bo = NULL;
13976 intel_crtc->cursor_addr = 0;
13977 }
13978
13979 intel_crtc_update_cursor(crtc, false);
13980}
13981
f4a2cf29 13982static void
852e787c
GP
13983intel_commit_cursor_plane(struct drm_plane *plane,
13984 struct intel_plane_state *state)
13985{
2b875c22 13986 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
13987 struct drm_device *dev = plane->dev;
13988 struct intel_crtc *intel_crtc;
2b875c22 13989 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13990 uint32_t addr;
852e787c 13991
ea2c67bb
MR
13992 crtc = crtc ? crtc : plane->crtc;
13993 intel_crtc = to_intel_crtc(crtc);
13994
2b875c22 13995 plane->fb = state->base.fb;
ea2c67bb
MR
13996 crtc->cursor_x = state->base.crtc_x;
13997 crtc->cursor_y = state->base.crtc_y;
13998
a912f12f
GP
13999 if (intel_crtc->cursor_bo == obj)
14000 goto update;
4ed91096 14001
f4a2cf29 14002 if (!obj)
a912f12f 14003 addr = 0;
f4a2cf29 14004 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 14005 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 14006 else
a912f12f 14007 addr = obj->phys_handle->busaddr;
852e787c 14008
a912f12f
GP
14009 intel_crtc->cursor_addr = addr;
14010 intel_crtc->cursor_bo = obj;
14011update:
852e787c 14012
32b7eeec 14013 if (intel_crtc->active)
a912f12f 14014 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
14015}
14016
3d7d6510
MR
14017static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14018 int pipe)
14019{
14020 struct intel_plane *cursor;
8e7d688b 14021 struct intel_plane_state *state;
3d7d6510
MR
14022
14023 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14024 if (cursor == NULL)
14025 return NULL;
14026
8e7d688b
MR
14027 state = intel_create_plane_state(&cursor->base);
14028 if (!state) {
ea2c67bb
MR
14029 kfree(cursor);
14030 return NULL;
14031 }
8e7d688b 14032 cursor->base.state = &state->base;
ea2c67bb 14033
3d7d6510
MR
14034 cursor->can_scale = false;
14035 cursor->max_downscale = 1;
14036 cursor->pipe = pipe;
14037 cursor->plane = pipe;
c59cb179
MR
14038 cursor->check_plane = intel_check_cursor_plane;
14039 cursor->commit_plane = intel_commit_cursor_plane;
a8ad0d8e 14040 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
14041
14042 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 14043 &intel_plane_funcs,
3d7d6510
MR
14044 intel_cursor_formats,
14045 ARRAY_SIZE(intel_cursor_formats),
14046 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
14047
14048 if (INTEL_INFO(dev)->gen >= 4) {
14049 if (!dev->mode_config.rotation_property)
14050 dev->mode_config.rotation_property =
14051 drm_mode_create_rotation_property(dev,
14052 BIT(DRM_ROTATE_0) |
14053 BIT(DRM_ROTATE_180));
14054 if (dev->mode_config.rotation_property)
14055 drm_object_attach_property(&cursor->base.base,
14056 dev->mode_config.rotation_property,
8e7d688b 14057 state->base.rotation);
4398ad45
VS
14058 }
14059
af99ceda
CK
14060 if (INTEL_INFO(dev)->gen >=9)
14061 state->scaler_id = -1;
14062
ea2c67bb
MR
14063 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14064
3d7d6510
MR
14065 return &cursor->base;
14066}
14067
549e2bfb
CK
14068static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14069 struct intel_crtc_state *crtc_state)
14070{
14071 int i;
14072 struct intel_scaler *intel_scaler;
14073 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14074
14075 for (i = 0; i < intel_crtc->num_scalers; i++) {
14076 intel_scaler = &scaler_state->scalers[i];
14077 intel_scaler->in_use = 0;
14078 intel_scaler->id = i;
14079
14080 intel_scaler->mode = PS_SCALER_MODE_DYN;
14081 }
14082
14083 scaler_state->scaler_id = -1;
14084}
14085
b358d0a6 14086static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 14087{
fbee40df 14088 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 14089 struct intel_crtc *intel_crtc;
f5de6e07 14090 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
14091 struct drm_plane *primary = NULL;
14092 struct drm_plane *cursor = NULL;
465c120c 14093 int i, ret;
79e53945 14094
955382f3 14095 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
14096 if (intel_crtc == NULL)
14097 return;
14098
f5de6e07
ACO
14099 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14100 if (!crtc_state)
14101 goto fail;
550acefd
ACO
14102 intel_crtc->config = crtc_state;
14103 intel_crtc->base.state = &crtc_state->base;
07878248 14104 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 14105
549e2bfb
CK
14106 /* initialize shared scalers */
14107 if (INTEL_INFO(dev)->gen >= 9) {
14108 if (pipe == PIPE_C)
14109 intel_crtc->num_scalers = 1;
14110 else
14111 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14112
14113 skl_init_scalers(dev, intel_crtc, crtc_state);
14114 }
14115
465c120c 14116 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14117 if (!primary)
14118 goto fail;
14119
14120 cursor = intel_cursor_plane_create(dev, pipe);
14121 if (!cursor)
14122 goto fail;
14123
465c120c 14124 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
14125 cursor, &intel_crtc_funcs);
14126 if (ret)
14127 goto fail;
79e53945
JB
14128
14129 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
14130 for (i = 0; i < 256; i++) {
14131 intel_crtc->lut_r[i] = i;
14132 intel_crtc->lut_g[i] = i;
14133 intel_crtc->lut_b[i] = i;
14134 }
14135
1f1c2e24
VS
14136 /*
14137 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14138 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14139 */
80824003
JB
14140 intel_crtc->pipe = pipe;
14141 intel_crtc->plane = pipe;
3a77c4c4 14142 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14143 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14144 intel_crtc->plane = !pipe;
80824003
JB
14145 }
14146
4b0e333e
CW
14147 intel_crtc->cursor_base = ~0;
14148 intel_crtc->cursor_cntl = ~0;
dc41c154 14149 intel_crtc->cursor_size = ~0;
8d7849db 14150
22fd0fab
JB
14151 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14152 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14153 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14154 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14155
79e53945 14156 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
14157
14158 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14159 return;
14160
14161fail:
14162 if (primary)
14163 drm_plane_cleanup(primary);
14164 if (cursor)
14165 drm_plane_cleanup(cursor);
f5de6e07 14166 kfree(crtc_state);
3d7d6510 14167 kfree(intel_crtc);
79e53945
JB
14168}
14169
752aa88a
JB
14170enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14171{
14172 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14173 struct drm_device *dev = connector->base.dev;
752aa88a 14174
51fd371b 14175 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14176
d3babd3f 14177 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14178 return INVALID_PIPE;
14179
14180 return to_intel_crtc(encoder->crtc)->pipe;
14181}
14182
08d7b3d1 14183int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14184 struct drm_file *file)
08d7b3d1 14185{
08d7b3d1 14186 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14187 struct drm_crtc *drmmode_crtc;
c05422d5 14188 struct intel_crtc *crtc;
08d7b3d1 14189
7707e653 14190 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14191
7707e653 14192 if (!drmmode_crtc) {
08d7b3d1 14193 DRM_ERROR("no such CRTC id\n");
3f2c2057 14194 return -ENOENT;
08d7b3d1
CW
14195 }
14196
7707e653 14197 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14198 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14199
c05422d5 14200 return 0;
08d7b3d1
CW
14201}
14202
66a9278e 14203static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14204{
66a9278e
DV
14205 struct drm_device *dev = encoder->base.dev;
14206 struct intel_encoder *source_encoder;
79e53945 14207 int index_mask = 0;
79e53945
JB
14208 int entry = 0;
14209
b2784e15 14210 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14211 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14212 index_mask |= (1 << entry);
14213
79e53945
JB
14214 entry++;
14215 }
4ef69c7a 14216
79e53945
JB
14217 return index_mask;
14218}
14219
4d302442
CW
14220static bool has_edp_a(struct drm_device *dev)
14221{
14222 struct drm_i915_private *dev_priv = dev->dev_private;
14223
14224 if (!IS_MOBILE(dev))
14225 return false;
14226
14227 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14228 return false;
14229
e3589908 14230 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14231 return false;
14232
14233 return true;
14234}
14235
84b4e042
JB
14236static bool intel_crt_present(struct drm_device *dev)
14237{
14238 struct drm_i915_private *dev_priv = dev->dev_private;
14239
884497ed
DL
14240 if (INTEL_INFO(dev)->gen >= 9)
14241 return false;
14242
cf404ce4 14243 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14244 return false;
14245
14246 if (IS_CHERRYVIEW(dev))
14247 return false;
14248
14249 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14250 return false;
14251
14252 return true;
14253}
14254
79e53945
JB
14255static void intel_setup_outputs(struct drm_device *dev)
14256{
725e30ad 14257 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14258 struct intel_encoder *encoder;
cb0953d7 14259 bool dpd_is_edp = false;
79e53945 14260
c9093354 14261 intel_lvds_init(dev);
79e53945 14262
84b4e042 14263 if (intel_crt_present(dev))
79935fca 14264 intel_crt_init(dev);
cb0953d7 14265
c776eb2e
VK
14266 if (IS_BROXTON(dev)) {
14267 /*
14268 * FIXME: Broxton doesn't support port detection via the
14269 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14270 * detect the ports.
14271 */
14272 intel_ddi_init(dev, PORT_A);
14273 intel_ddi_init(dev, PORT_B);
14274 intel_ddi_init(dev, PORT_C);
14275 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14276 int found;
14277
de31facd
JB
14278 /*
14279 * Haswell uses DDI functions to detect digital outputs.
14280 * On SKL pre-D0 the strap isn't connected, so we assume
14281 * it's there.
14282 */
0e72a5b5 14283 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
de31facd
JB
14284 /* WaIgnoreDDIAStrap: skl */
14285 if (found ||
14286 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
0e72a5b5
ED
14287 intel_ddi_init(dev, PORT_A);
14288
14289 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14290 * register */
14291 found = I915_READ(SFUSE_STRAP);
14292
14293 if (found & SFUSE_STRAP_DDIB_DETECTED)
14294 intel_ddi_init(dev, PORT_B);
14295 if (found & SFUSE_STRAP_DDIC_DETECTED)
14296 intel_ddi_init(dev, PORT_C);
14297 if (found & SFUSE_STRAP_DDID_DETECTED)
14298 intel_ddi_init(dev, PORT_D);
14299 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14300 int found;
5d8a7752 14301 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14302
14303 if (has_edp_a(dev))
14304 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14305
dc0fa718 14306 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14307 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 14308 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 14309 if (!found)
e2debe91 14310 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14311 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14312 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14313 }
14314
dc0fa718 14315 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14316 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14317
dc0fa718 14318 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14319 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14320
5eb08b69 14321 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14322 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14323
270b3042 14324 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14325 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 14326 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
14327 /*
14328 * The DP_DETECTED bit is the latched state of the DDC
14329 * SDA pin at boot. However since eDP doesn't require DDC
14330 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14331 * eDP ports may have been muxed to an alternate function.
14332 * Thus we can't rely on the DP_DETECTED bit alone to detect
14333 * eDP ports. Consult the VBT as well as DP_DETECTED to
14334 * detect eDP ports.
14335 */
d2182a66
VS
14336 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14337 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
14338 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14339 PORT_B);
e17ac6db
VS
14340 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14341 intel_dp_is_edp(dev, PORT_B))
14342 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 14343
d2182a66
VS
14344 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14345 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
14346 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14347 PORT_C);
e17ac6db
VS
14348 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14349 intel_dp_is_edp(dev, PORT_C))
14350 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 14351
9418c1f1 14352 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14353 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
14354 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14355 PORT_D);
e17ac6db
VS
14356 /* eDP not supported on port D, so don't check VBT */
14357 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14358 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
14359 }
14360
3cfca973 14361 intel_dsi_init(dev);
103a196f 14362 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 14363 bool found = false;
7d57382e 14364
e2debe91 14365 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14366 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 14367 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
14368 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14369 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14370 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14371 }
27185ae1 14372
e7281eab 14373 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14374 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14375 }
13520b05
KH
14376
14377 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14378
e2debe91 14379 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14380 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 14381 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 14382 }
27185ae1 14383
e2debe91 14384 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14385
b01f2c3a
JB
14386 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14387 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14388 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14389 }
e7281eab 14390 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14391 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14392 }
27185ae1 14393
b01f2c3a 14394 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 14395 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14396 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14397 } else if (IS_GEN2(dev))
79e53945
JB
14398 intel_dvo_init(dev);
14399
103a196f 14400 if (SUPPORTS_TV(dev))
79e53945
JB
14401 intel_tv_init(dev);
14402
0bc12bcb 14403 intel_psr_init(dev);
7c8f8a70 14404
b2784e15 14405 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14406 encoder->base.possible_crtcs = encoder->crtc_mask;
14407 encoder->base.possible_clones =
66a9278e 14408 intel_encoder_clones(encoder);
79e53945 14409 }
47356eb6 14410
dde86e2d 14411 intel_init_pch_refclk(dev);
270b3042
DV
14412
14413 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14414}
14415
14416static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14417{
60a5ca01 14418 struct drm_device *dev = fb->dev;
79e53945 14419 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14420
ef2d633e 14421 drm_framebuffer_cleanup(fb);
60a5ca01 14422 mutex_lock(&dev->struct_mutex);
ef2d633e 14423 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14424 drm_gem_object_unreference(&intel_fb->obj->base);
14425 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14426 kfree(intel_fb);
14427}
14428
14429static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14430 struct drm_file *file,
79e53945
JB
14431 unsigned int *handle)
14432{
14433 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14434 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14435
05394f39 14436 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14437}
14438
14439static const struct drm_framebuffer_funcs intel_fb_funcs = {
14440 .destroy = intel_user_framebuffer_destroy,
14441 .create_handle = intel_user_framebuffer_create_handle,
14442};
14443
b321803d
DL
14444static
14445u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14446 uint32_t pixel_format)
14447{
14448 u32 gen = INTEL_INFO(dev)->gen;
14449
14450 if (gen >= 9) {
14451 /* "The stride in bytes must not exceed the of the size of 8K
14452 * pixels and 32K bytes."
14453 */
14454 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14455 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14456 return 32*1024;
14457 } else if (gen >= 4) {
14458 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14459 return 16*1024;
14460 else
14461 return 32*1024;
14462 } else if (gen >= 3) {
14463 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14464 return 8*1024;
14465 else
14466 return 16*1024;
14467 } else {
14468 /* XXX DSPC is limited to 4k tiled */
14469 return 8*1024;
14470 }
14471}
14472
b5ea642a
DV
14473static int intel_framebuffer_init(struct drm_device *dev,
14474 struct intel_framebuffer *intel_fb,
14475 struct drm_mode_fb_cmd2 *mode_cmd,
14476 struct drm_i915_gem_object *obj)
79e53945 14477{
6761dd31 14478 unsigned int aligned_height;
79e53945 14479 int ret;
b321803d 14480 u32 pitch_limit, stride_alignment;
79e53945 14481
dd4916c5
DV
14482 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14483
2a80eada
DV
14484 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14485 /* Enforce that fb modifier and tiling mode match, but only for
14486 * X-tiled. This is needed for FBC. */
14487 if (!!(obj->tiling_mode == I915_TILING_X) !=
14488 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14489 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14490 return -EINVAL;
14491 }
14492 } else {
14493 if (obj->tiling_mode == I915_TILING_X)
14494 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14495 else if (obj->tiling_mode == I915_TILING_Y) {
14496 DRM_DEBUG("No Y tiling for legacy addfb\n");
14497 return -EINVAL;
14498 }
14499 }
14500
9a8f0a12
TU
14501 /* Passed in modifier sanity checking. */
14502 switch (mode_cmd->modifier[0]) {
14503 case I915_FORMAT_MOD_Y_TILED:
14504 case I915_FORMAT_MOD_Yf_TILED:
14505 if (INTEL_INFO(dev)->gen < 9) {
14506 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14507 mode_cmd->modifier[0]);
14508 return -EINVAL;
14509 }
14510 case DRM_FORMAT_MOD_NONE:
14511 case I915_FORMAT_MOD_X_TILED:
14512 break;
14513 default:
c0f40428
JB
14514 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14515 mode_cmd->modifier[0]);
57cd6508 14516 return -EINVAL;
c16ed4be 14517 }
57cd6508 14518
b321803d
DL
14519 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14520 mode_cmd->pixel_format);
14521 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14522 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14523 mode_cmd->pitches[0], stride_alignment);
57cd6508 14524 return -EINVAL;
c16ed4be 14525 }
57cd6508 14526
b321803d
DL
14527 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14528 mode_cmd->pixel_format);
a35cdaa0 14529 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14530 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14531 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14532 "tiled" : "linear",
a35cdaa0 14533 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14534 return -EINVAL;
c16ed4be 14535 }
5d7bd705 14536
2a80eada 14537 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14538 mode_cmd->pitches[0] != obj->stride) {
14539 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14540 mode_cmd->pitches[0], obj->stride);
5d7bd705 14541 return -EINVAL;
c16ed4be 14542 }
5d7bd705 14543
57779d06 14544 /* Reject formats not supported by any plane early. */
308e5bcb 14545 switch (mode_cmd->pixel_format) {
57779d06 14546 case DRM_FORMAT_C8:
04b3924d
VS
14547 case DRM_FORMAT_RGB565:
14548 case DRM_FORMAT_XRGB8888:
14549 case DRM_FORMAT_ARGB8888:
57779d06
VS
14550 break;
14551 case DRM_FORMAT_XRGB1555:
c16ed4be 14552 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14553 DRM_DEBUG("unsupported pixel format: %s\n",
14554 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14555 return -EINVAL;
c16ed4be 14556 }
57779d06 14557 break;
57779d06 14558 case DRM_FORMAT_ABGR8888:
6c0fd451
DL
14559 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14560 DRM_DEBUG("unsupported pixel format: %s\n",
14561 drm_get_format_name(mode_cmd->pixel_format));
14562 return -EINVAL;
14563 }
14564 break;
14565 case DRM_FORMAT_XBGR8888:
04b3924d 14566 case DRM_FORMAT_XRGB2101010:
57779d06 14567 case DRM_FORMAT_XBGR2101010:
c16ed4be 14568 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14569 DRM_DEBUG("unsupported pixel format: %s\n",
14570 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14571 return -EINVAL;
c16ed4be 14572 }
b5626747 14573 break;
7531208b
DL
14574 case DRM_FORMAT_ABGR2101010:
14575 if (!IS_VALLEYVIEW(dev)) {
14576 DRM_DEBUG("unsupported pixel format: %s\n",
14577 drm_get_format_name(mode_cmd->pixel_format));
14578 return -EINVAL;
14579 }
14580 break;
04b3924d
VS
14581 case DRM_FORMAT_YUYV:
14582 case DRM_FORMAT_UYVY:
14583 case DRM_FORMAT_YVYU:
14584 case DRM_FORMAT_VYUY:
c16ed4be 14585 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14586 DRM_DEBUG("unsupported pixel format: %s\n",
14587 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14588 return -EINVAL;
c16ed4be 14589 }
57cd6508
CW
14590 break;
14591 default:
4ee62c76
VS
14592 DRM_DEBUG("unsupported pixel format: %s\n",
14593 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14594 return -EINVAL;
14595 }
14596
90f9a336
VS
14597 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14598 if (mode_cmd->offsets[0] != 0)
14599 return -EINVAL;
14600
ec2c981e 14601 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14602 mode_cmd->pixel_format,
14603 mode_cmd->modifier[0]);
53155c0a
DV
14604 /* FIXME drm helper for size checks (especially planar formats)? */
14605 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14606 return -EINVAL;
14607
c7d73f6a
DV
14608 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14609 intel_fb->obj = obj;
80075d49 14610 intel_fb->obj->framebuffer_references++;
c7d73f6a 14611
79e53945
JB
14612 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14613 if (ret) {
14614 DRM_ERROR("framebuffer init failed %d\n", ret);
14615 return ret;
14616 }
14617
79e53945
JB
14618 return 0;
14619}
14620
79e53945
JB
14621static struct drm_framebuffer *
14622intel_user_framebuffer_create(struct drm_device *dev,
14623 struct drm_file *filp,
308e5bcb 14624 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14625{
05394f39 14626 struct drm_i915_gem_object *obj;
79e53945 14627
308e5bcb
JB
14628 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14629 mode_cmd->handles[0]));
c8725226 14630 if (&obj->base == NULL)
cce13ff7 14631 return ERR_PTR(-ENOENT);
79e53945 14632
d2dff872 14633 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
14634}
14635
4520f53a 14636#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 14637static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14638{
14639}
14640#endif
14641
79e53945 14642static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14643 .fb_create = intel_user_framebuffer_create,
0632fef6 14644 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14645 .atomic_check = intel_atomic_check,
14646 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14647 .atomic_state_alloc = intel_atomic_state_alloc,
14648 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14649};
14650
e70236a8
JB
14651/* Set up chip specific display functions */
14652static void intel_init_display(struct drm_device *dev)
14653{
14654 struct drm_i915_private *dev_priv = dev->dev_private;
14655
ee9300bb
DV
14656 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14657 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14658 else if (IS_CHERRYVIEW(dev))
14659 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14660 else if (IS_VALLEYVIEW(dev))
14661 dev_priv->display.find_dpll = vlv_find_best_dpll;
14662 else if (IS_PINEVIEW(dev))
14663 dev_priv->display.find_dpll = pnv_find_best_dpll;
14664 else
14665 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14666
bc8d7dff
DL
14667 if (INTEL_INFO(dev)->gen >= 9) {
14668 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14669 dev_priv->display.get_initial_plane_config =
14670 skylake_get_initial_plane_config;
bc8d7dff
DL
14671 dev_priv->display.crtc_compute_clock =
14672 haswell_crtc_compute_clock;
14673 dev_priv->display.crtc_enable = haswell_crtc_enable;
14674 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14675 dev_priv->display.update_primary_plane =
14676 skylake_update_primary_plane;
14677 } else if (HAS_DDI(dev)) {
0e8ffe1b 14678 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14679 dev_priv->display.get_initial_plane_config =
14680 ironlake_get_initial_plane_config;
797d0259
ACO
14681 dev_priv->display.crtc_compute_clock =
14682 haswell_crtc_compute_clock;
4f771f10
PZ
14683 dev_priv->display.crtc_enable = haswell_crtc_enable;
14684 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14685 dev_priv->display.update_primary_plane =
14686 ironlake_update_primary_plane;
09b4ddf9 14687 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14688 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14689 dev_priv->display.get_initial_plane_config =
14690 ironlake_get_initial_plane_config;
3fb37703
ACO
14691 dev_priv->display.crtc_compute_clock =
14692 ironlake_crtc_compute_clock;
76e5a89c
DV
14693 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14694 dev_priv->display.crtc_disable = ironlake_crtc_disable;
262ca2b0
MR
14695 dev_priv->display.update_primary_plane =
14696 ironlake_update_primary_plane;
89b667f8
JB
14697 } else if (IS_VALLEYVIEW(dev)) {
14698 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14699 dev_priv->display.get_initial_plane_config =
14700 i9xx_get_initial_plane_config;
d6dfee7a 14701 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14702 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14703 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14704 dev_priv->display.update_primary_plane =
14705 i9xx_update_primary_plane;
f564048e 14706 } else {
0e8ffe1b 14707 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14708 dev_priv->display.get_initial_plane_config =
14709 i9xx_get_initial_plane_config;
d6dfee7a 14710 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14711 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14712 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14713 dev_priv->display.update_primary_plane =
14714 i9xx_update_primary_plane;
f564048e 14715 }
e70236a8 14716
e70236a8 14717 /* Returns the core display clock speed */
1652d19e
VS
14718 if (IS_SKYLAKE(dev))
14719 dev_priv->display.get_display_clock_speed =
14720 skylake_get_display_clock_speed;
14721 else if (IS_BROADWELL(dev))
14722 dev_priv->display.get_display_clock_speed =
14723 broadwell_get_display_clock_speed;
14724 else if (IS_HASWELL(dev))
14725 dev_priv->display.get_display_clock_speed =
14726 haswell_get_display_clock_speed;
14727 else if (IS_VALLEYVIEW(dev))
25eb05fc
JB
14728 dev_priv->display.get_display_clock_speed =
14729 valleyview_get_display_clock_speed;
b37a6434
VS
14730 else if (IS_GEN5(dev))
14731 dev_priv->display.get_display_clock_speed =
14732 ilk_get_display_clock_speed;
a7c66cd8 14733 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
34edce2f 14734 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
e70236a8
JB
14735 dev_priv->display.get_display_clock_speed =
14736 i945_get_display_clock_speed;
34edce2f
VS
14737 else if (IS_GM45(dev))
14738 dev_priv->display.get_display_clock_speed =
14739 gm45_get_display_clock_speed;
14740 else if (IS_CRESTLINE(dev))
14741 dev_priv->display.get_display_clock_speed =
14742 i965gm_get_display_clock_speed;
14743 else if (IS_PINEVIEW(dev))
14744 dev_priv->display.get_display_clock_speed =
14745 pnv_get_display_clock_speed;
14746 else if (IS_G33(dev) || IS_G4X(dev))
14747 dev_priv->display.get_display_clock_speed =
14748 g33_get_display_clock_speed;
e70236a8
JB
14749 else if (IS_I915G(dev))
14750 dev_priv->display.get_display_clock_speed =
14751 i915_get_display_clock_speed;
257a7ffc 14752 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14753 dev_priv->display.get_display_clock_speed =
14754 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
14755 else if (IS_PINEVIEW(dev))
14756 dev_priv->display.get_display_clock_speed =
14757 pnv_get_display_clock_speed;
e70236a8
JB
14758 else if (IS_I915GM(dev))
14759 dev_priv->display.get_display_clock_speed =
14760 i915gm_get_display_clock_speed;
14761 else if (IS_I865G(dev))
14762 dev_priv->display.get_display_clock_speed =
14763 i865_get_display_clock_speed;
f0f8a9ce 14764 else if (IS_I85X(dev))
e70236a8 14765 dev_priv->display.get_display_clock_speed =
1b1d2716 14766 i85x_get_display_clock_speed;
623e01e5
VS
14767 else { /* 830 */
14768 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14769 dev_priv->display.get_display_clock_speed =
14770 i830_get_display_clock_speed;
623e01e5 14771 }
e70236a8 14772
7c10a2b5 14773 if (IS_GEN5(dev)) {
3bb11b53 14774 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14775 } else if (IS_GEN6(dev)) {
14776 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14777 } else if (IS_IVYBRIDGE(dev)) {
14778 /* FIXME: detect B0+ stepping and use auto training */
14779 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14780 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14781 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
b432e5cf
VS
14782 if (IS_BROADWELL(dev))
14783 dev_priv->display.modeset_global_resources =
14784 broadwell_modeset_global_resources;
30a970c6
JB
14785 } else if (IS_VALLEYVIEW(dev)) {
14786 dev_priv->display.modeset_global_resources =
14787 valleyview_modeset_global_resources;
f8437dd1
VK
14788 } else if (IS_BROXTON(dev)) {
14789 dev_priv->display.modeset_global_resources =
14790 broxton_modeset_global_resources;
e70236a8 14791 }
8c9f3aaf 14792
8c9f3aaf
JB
14793 switch (INTEL_INFO(dev)->gen) {
14794 case 2:
14795 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14796 break;
14797
14798 case 3:
14799 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14800 break;
14801
14802 case 4:
14803 case 5:
14804 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14805 break;
14806
14807 case 6:
14808 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14809 break;
7c9017e5 14810 case 7:
4e0bbc31 14811 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14812 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14813 break;
830c81db 14814 case 9:
ba343e02
TU
14815 /* Drop through - unsupported since execlist only. */
14816 default:
14817 /* Default just returns -ENODEV to indicate unsupported */
14818 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14819 }
7bd688cd
JN
14820
14821 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
14822
14823 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
14824}
14825
b690e96c
JB
14826/*
14827 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14828 * resume, or other times. This quirk makes sure that's the case for
14829 * affected systems.
14830 */
0206e353 14831static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14832{
14833 struct drm_i915_private *dev_priv = dev->dev_private;
14834
14835 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14836 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14837}
14838
b6b5d049
VS
14839static void quirk_pipeb_force(struct drm_device *dev)
14840{
14841 struct drm_i915_private *dev_priv = dev->dev_private;
14842
14843 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14844 DRM_INFO("applying pipe b force quirk\n");
14845}
14846
435793df
KP
14847/*
14848 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14849 */
14850static void quirk_ssc_force_disable(struct drm_device *dev)
14851{
14852 struct drm_i915_private *dev_priv = dev->dev_private;
14853 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14854 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14855}
14856
4dca20ef 14857/*
5a15ab5b
CE
14858 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14859 * brightness value
4dca20ef
CE
14860 */
14861static void quirk_invert_brightness(struct drm_device *dev)
14862{
14863 struct drm_i915_private *dev_priv = dev->dev_private;
14864 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14865 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14866}
14867
9c72cc6f
SD
14868/* Some VBT's incorrectly indicate no backlight is present */
14869static void quirk_backlight_present(struct drm_device *dev)
14870{
14871 struct drm_i915_private *dev_priv = dev->dev_private;
14872 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14873 DRM_INFO("applying backlight present quirk\n");
14874}
14875
b690e96c
JB
14876struct intel_quirk {
14877 int device;
14878 int subsystem_vendor;
14879 int subsystem_device;
14880 void (*hook)(struct drm_device *dev);
14881};
14882
5f85f176
EE
14883/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14884struct intel_dmi_quirk {
14885 void (*hook)(struct drm_device *dev);
14886 const struct dmi_system_id (*dmi_id_list)[];
14887};
14888
14889static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14890{
14891 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14892 return 1;
14893}
14894
14895static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14896 {
14897 .dmi_id_list = &(const struct dmi_system_id[]) {
14898 {
14899 .callback = intel_dmi_reverse_brightness,
14900 .ident = "NCR Corporation",
14901 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14902 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14903 },
14904 },
14905 { } /* terminating entry */
14906 },
14907 .hook = quirk_invert_brightness,
14908 },
14909};
14910
c43b5634 14911static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14912 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14913 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14914
b690e96c
JB
14915 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14916 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14917
5f080c0f
VS
14918 /* 830 needs to leave pipe A & dpll A up */
14919 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14920
b6b5d049
VS
14921 /* 830 needs to leave pipe B & dpll B up */
14922 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14923
435793df
KP
14924 /* Lenovo U160 cannot use SSC on LVDS */
14925 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14926
14927 /* Sony Vaio Y cannot use SSC on LVDS */
14928 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14929
be505f64
AH
14930 /* Acer Aspire 5734Z must invert backlight brightness */
14931 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14932
14933 /* Acer/eMachines G725 */
14934 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14935
14936 /* Acer/eMachines e725 */
14937 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14938
14939 /* Acer/Packard Bell NCL20 */
14940 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14941
14942 /* Acer Aspire 4736Z */
14943 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14944
14945 /* Acer Aspire 5336 */
14946 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14947
14948 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14949 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14950
dfb3d47b
SD
14951 /* Acer C720 Chromebook (Core i3 4005U) */
14952 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14953
b2a9601c 14954 /* Apple Macbook 2,1 (Core 2 T7400) */
14955 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14956
d4967d8c
SD
14957 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14958 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14959
14960 /* HP Chromebook 14 (Celeron 2955U) */
14961 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14962
14963 /* Dell Chromebook 11 */
14964 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14965};
14966
14967static void intel_init_quirks(struct drm_device *dev)
14968{
14969 struct pci_dev *d = dev->pdev;
14970 int i;
14971
14972 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14973 struct intel_quirk *q = &intel_quirks[i];
14974
14975 if (d->device == q->device &&
14976 (d->subsystem_vendor == q->subsystem_vendor ||
14977 q->subsystem_vendor == PCI_ANY_ID) &&
14978 (d->subsystem_device == q->subsystem_device ||
14979 q->subsystem_device == PCI_ANY_ID))
14980 q->hook(dev);
14981 }
5f85f176
EE
14982 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14983 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14984 intel_dmi_quirks[i].hook(dev);
14985 }
b690e96c
JB
14986}
14987
9cce37f4
JB
14988/* Disable the VGA plane that we never use */
14989static void i915_disable_vga(struct drm_device *dev)
14990{
14991 struct drm_i915_private *dev_priv = dev->dev_private;
14992 u8 sr1;
766aa1c4 14993 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 14994
2b37c616 14995 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 14996 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14997 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14998 sr1 = inb(VGA_SR_DATA);
14999 outb(sr1 | 1<<5, VGA_SR_DATA);
15000 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15001 udelay(300);
15002
01f5a626 15003 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
15004 POSTING_READ(vga_reg);
15005}
15006
f817586c
DV
15007void intel_modeset_init_hw(struct drm_device *dev)
15008{
b6283055 15009 intel_update_cdclk(dev);
a8f78b58 15010 intel_prepare_ddi(dev);
f817586c 15011 intel_init_clock_gating(dev);
8090c6b9 15012 intel_enable_gt_powersave(dev);
f817586c
DV
15013}
15014
79e53945
JB
15015void intel_modeset_init(struct drm_device *dev)
15016{
652c393a 15017 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 15018 int sprite, ret;
8cc87b75 15019 enum pipe pipe;
46f297fb 15020 struct intel_crtc *crtc;
79e53945
JB
15021
15022 drm_mode_config_init(dev);
15023
15024 dev->mode_config.min_width = 0;
15025 dev->mode_config.min_height = 0;
15026
019d96cb
DA
15027 dev->mode_config.preferred_depth = 24;
15028 dev->mode_config.prefer_shadow = 1;
15029
25bab385
TU
15030 dev->mode_config.allow_fb_modifiers = true;
15031
e6ecefaa 15032 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 15033
b690e96c
JB
15034 intel_init_quirks(dev);
15035
1fa61106
ED
15036 intel_init_pm(dev);
15037
e3c74757
BW
15038 if (INTEL_INFO(dev)->num_pipes == 0)
15039 return;
15040
e70236a8 15041 intel_init_display(dev);
7c10a2b5 15042 intel_init_audio(dev);
e70236a8 15043
a6c45cf0
CW
15044 if (IS_GEN2(dev)) {
15045 dev->mode_config.max_width = 2048;
15046 dev->mode_config.max_height = 2048;
15047 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
15048 dev->mode_config.max_width = 4096;
15049 dev->mode_config.max_height = 4096;
79e53945 15050 } else {
a6c45cf0
CW
15051 dev->mode_config.max_width = 8192;
15052 dev->mode_config.max_height = 8192;
79e53945 15053 }
068be561 15054
dc41c154
VS
15055 if (IS_845G(dev) || IS_I865G(dev)) {
15056 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15057 dev->mode_config.cursor_height = 1023;
15058 } else if (IS_GEN2(dev)) {
068be561
DL
15059 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15060 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15061 } else {
15062 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15063 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15064 }
15065
5d4545ae 15066 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 15067
28c97730 15068 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
15069 INTEL_INFO(dev)->num_pipes,
15070 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 15071
055e393f 15072 for_each_pipe(dev_priv, pipe) {
8cc87b75 15073 intel_crtc_init(dev, pipe);
3bdcfc0c 15074 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 15075 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 15076 if (ret)
06da8da2 15077 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 15078 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 15079 }
79e53945
JB
15080 }
15081
f42bb70d
JB
15082 intel_init_dpio(dev);
15083
e72f9fbf 15084 intel_shared_dpll_init(dev);
ee7b9f93 15085
9cce37f4
JB
15086 /* Just disable it once at startup */
15087 i915_disable_vga(dev);
79e53945 15088 intel_setup_outputs(dev);
11be49eb
CW
15089
15090 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 15091 intel_fbc_disable(dev);
fa9fa083 15092
6e9f798d 15093 drm_modeset_lock_all(dev);
fa9fa083 15094 intel_modeset_setup_hw_state(dev, false);
6e9f798d 15095 drm_modeset_unlock_all(dev);
46f297fb 15096
d3fcc808 15097 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
15098 if (!crtc->active)
15099 continue;
15100
46f297fb 15101 /*
46f297fb
JB
15102 * Note that reserving the BIOS fb up front prevents us
15103 * from stuffing other stolen allocations like the ring
15104 * on top. This prevents some ugliness at boot time, and
15105 * can even allow for smooth boot transitions if the BIOS
15106 * fb is large enough for the active pipe configuration.
15107 */
5724dbd1
DL
15108 if (dev_priv->display.get_initial_plane_config) {
15109 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
15110 &crtc->plane_config);
15111 /*
15112 * If the fb is shared between multiple heads, we'll
15113 * just get the first one.
15114 */
f6936e29 15115 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
46f297fb 15116 }
46f297fb 15117 }
2c7111db
CW
15118}
15119
7fad798e
DV
15120static void intel_enable_pipe_a(struct drm_device *dev)
15121{
15122 struct intel_connector *connector;
15123 struct drm_connector *crt = NULL;
15124 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15125 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15126
15127 /* We can't just switch on the pipe A, we need to set things up with a
15128 * proper mode and output configuration. As a gross hack, enable pipe A
15129 * by enabling the load detect pipe once. */
3a3371ff 15130 for_each_intel_connector(dev, connector) {
7fad798e
DV
15131 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15132 crt = &connector->base;
15133 break;
15134 }
15135 }
15136
15137 if (!crt)
15138 return;
15139
208bf9fd 15140 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15141 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15142}
15143
fa555837
DV
15144static bool
15145intel_check_plane_mapping(struct intel_crtc *crtc)
15146{
7eb552ae
BW
15147 struct drm_device *dev = crtc->base.dev;
15148 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
15149 u32 reg, val;
15150
7eb552ae 15151 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15152 return true;
15153
15154 reg = DSPCNTR(!crtc->plane);
15155 val = I915_READ(reg);
15156
15157 if ((val & DISPLAY_PLANE_ENABLE) &&
15158 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15159 return false;
15160
15161 return true;
15162}
15163
24929352
DV
15164static void intel_sanitize_crtc(struct intel_crtc *crtc)
15165{
15166 struct drm_device *dev = crtc->base.dev;
15167 struct drm_i915_private *dev_priv = dev->dev_private;
b17d48e2 15168 struct intel_encoder *encoder;
fa555837 15169 u32 reg;
b17d48e2 15170 bool enable;
24929352 15171
24929352 15172 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 15173 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
15174 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15175
d3eaf884 15176 /* restore vblank interrupts to correct state */
9625604c 15177 drm_crtc_vblank_reset(&crtc->base);
d297e103
VS
15178 if (crtc->active) {
15179 update_scanline_offset(crtc);
9625604c
DV
15180 drm_crtc_vblank_on(&crtc->base);
15181 }
d3eaf884 15182
24929352 15183 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15184 * disable the crtc (and hence change the state) if it is wrong. Note
15185 * that gen4+ has a fixed plane -> pipe mapping. */
15186 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15187 bool plane;
15188
24929352
DV
15189 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15190 crtc->base.base.id);
15191
15192 /* Pipe has the wrong plane attached and the plane is active.
15193 * Temporarily change the plane mapping and disable everything
15194 * ... */
15195 plane = crtc->plane;
b70709a6 15196 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15197 crtc->plane = !plane;
b17d48e2 15198 intel_crtc_disable_noatomic(&crtc->base);
24929352 15199 crtc->plane = plane;
24929352 15200 }
24929352 15201
7fad798e
DV
15202 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15203 crtc->pipe == PIPE_A && !crtc->active) {
15204 /* BIOS forgot to enable pipe A, this mostly happens after
15205 * resume. Force-enable the pipe to fix this, the update_dpms
15206 * call below we restore the pipe to the right state, but leave
15207 * the required bits on. */
15208 intel_enable_pipe_a(dev);
15209 }
15210
24929352
DV
15211 /* Adjust the state of the output pipe according to whether we
15212 * have active connectors/encoders. */
b17d48e2
ML
15213 enable = false;
15214 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15215 enable |= encoder->connectors_active;
24929352 15216
b17d48e2
ML
15217 if (!enable)
15218 intel_crtc_disable_noatomic(&crtc->base);
24929352 15219
53d9f4e9 15220 if (crtc->active != crtc->base.state->active) {
24929352
DV
15221
15222 /* This can happen either due to bugs in the get_hw_state
b17d48e2
ML
15223 * functions or because of calls to intel_crtc_disable_noatomic,
15224 * or because the pipe is force-enabled due to the
24929352
DV
15225 * pipe A quirk. */
15226 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15227 crtc->base.base.id,
83d65738 15228 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
15229 crtc->active ? "enabled" : "disabled");
15230
83d65738 15231 crtc->base.state->enable = crtc->active;
49d6fa21 15232 crtc->base.state->active = crtc->active;
24929352
DV
15233 crtc->base.enabled = crtc->active;
15234
15235 /* Because we only establish the connector -> encoder ->
15236 * crtc links if something is active, this means the
15237 * crtc is now deactivated. Break the links. connector
15238 * -> encoder links are only establish when things are
15239 * actually up, hence no need to break them. */
15240 WARN_ON(crtc->active);
15241
15242 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15243 WARN_ON(encoder->connectors_active);
15244 encoder->base.crtc = NULL;
15245 }
15246 }
c5ab3bc0 15247
a3ed6aad 15248 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15249 /*
15250 * We start out with underrun reporting disabled to avoid races.
15251 * For correct bookkeeping mark this on active crtcs.
15252 *
c5ab3bc0
DV
15253 * Also on gmch platforms we dont have any hardware bits to
15254 * disable the underrun reporting. Which means we need to start
15255 * out with underrun reporting disabled also on inactive pipes,
15256 * since otherwise we'll complain about the garbage we read when
15257 * e.g. coming up after runtime pm.
15258 *
4cc31489
DV
15259 * No protection against concurrent access is required - at
15260 * worst a fifo underrun happens which also sets this to false.
15261 */
15262 crtc->cpu_fifo_underrun_disabled = true;
15263 crtc->pch_fifo_underrun_disabled = true;
15264 }
24929352
DV
15265}
15266
15267static void intel_sanitize_encoder(struct intel_encoder *encoder)
15268{
15269 struct intel_connector *connector;
15270 struct drm_device *dev = encoder->base.dev;
15271
15272 /* We need to check both for a crtc link (meaning that the
15273 * encoder is active and trying to read from a pipe) and the
15274 * pipe itself being active. */
15275 bool has_active_crtc = encoder->base.crtc &&
15276 to_intel_crtc(encoder->base.crtc)->active;
15277
15278 if (encoder->connectors_active && !has_active_crtc) {
15279 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15280 encoder->base.base.id,
8e329a03 15281 encoder->base.name);
24929352
DV
15282
15283 /* Connector is active, but has no active pipe. This is
15284 * fallout from our resume register restoring. Disable
15285 * the encoder manually again. */
15286 if (encoder->base.crtc) {
15287 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15288 encoder->base.base.id,
8e329a03 15289 encoder->base.name);
24929352 15290 encoder->disable(encoder);
a62d1497
VS
15291 if (encoder->post_disable)
15292 encoder->post_disable(encoder);
24929352 15293 }
7f1950fb
EE
15294 encoder->base.crtc = NULL;
15295 encoder->connectors_active = false;
24929352
DV
15296
15297 /* Inconsistent output/port/pipe state happens presumably due to
15298 * a bug in one of the get_hw_state functions. Or someplace else
15299 * in our code, like the register restore mess on resume. Clamp
15300 * things to off as a safer default. */
3a3371ff 15301 for_each_intel_connector(dev, connector) {
24929352
DV
15302 if (connector->encoder != encoder)
15303 continue;
7f1950fb
EE
15304 connector->base.dpms = DRM_MODE_DPMS_OFF;
15305 connector->base.encoder = NULL;
24929352
DV
15306 }
15307 }
15308 /* Enabled encoders without active connectors will be fixed in
15309 * the crtc fixup. */
15310}
15311
04098753 15312void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15313{
15314 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 15315 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15316
04098753
ID
15317 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15318 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15319 i915_disable_vga(dev);
15320 }
15321}
15322
15323void i915_redisable_vga(struct drm_device *dev)
15324{
15325 struct drm_i915_private *dev_priv = dev->dev_private;
15326
8dc8a27c
PZ
15327 /* This function can be called both from intel_modeset_setup_hw_state or
15328 * at a very early point in our resume sequence, where the power well
15329 * structures are not yet restored. Since this function is at a very
15330 * paranoid "someone might have enabled VGA while we were not looking"
15331 * level, just check if the power well is enabled instead of trying to
15332 * follow the "don't touch the power well if we don't need it" policy
15333 * the rest of the driver uses. */
f458ebbc 15334 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15335 return;
15336
04098753 15337 i915_redisable_vga_power_on(dev);
0fde901f
KM
15338}
15339
98ec7739
VS
15340static bool primary_get_hw_state(struct intel_crtc *crtc)
15341{
15342 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15343
15344 if (!crtc->active)
15345 return false;
15346
15347 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15348}
15349
30e984df 15350static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15351{
15352 struct drm_i915_private *dev_priv = dev->dev_private;
15353 enum pipe pipe;
24929352
DV
15354 struct intel_crtc *crtc;
15355 struct intel_encoder *encoder;
15356 struct intel_connector *connector;
5358901f 15357 int i;
24929352 15358
d3fcc808 15359 for_each_intel_crtc(dev, crtc) {
b70709a6
ML
15360 struct drm_plane *primary = crtc->base.primary;
15361 struct intel_plane_state *plane_state;
15362
6e3c9717 15363 memset(crtc->config, 0, sizeof(*crtc->config));
f7217905 15364 crtc->config->base.crtc = &crtc->base;
3b117c8f 15365
6e3c9717 15366 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
9953599b 15367
0e8ffe1b 15368 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 15369 crtc->config);
24929352 15370
83d65738 15371 crtc->base.state->enable = crtc->active;
49d6fa21 15372 crtc->base.state->active = crtc->active;
24929352 15373 crtc->base.enabled = crtc->active;
b8b7fade 15374 crtc->base.hwmode = crtc->config->base.adjusted_mode;
b70709a6
ML
15375
15376 plane_state = to_intel_plane_state(primary->state);
15377 plane_state->visible = primary_get_hw_state(crtc);
24929352
DV
15378
15379 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15380 crtc->base.base.id,
15381 crtc->active ? "enabled" : "disabled");
15382 }
15383
5358901f
DV
15384 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15385 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15386
3e369b76
ACO
15387 pll->on = pll->get_hw_state(dev_priv, pll,
15388 &pll->config.hw_state);
5358901f 15389 pll->active = 0;
3e369b76 15390 pll->config.crtc_mask = 0;
d3fcc808 15391 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 15392 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 15393 pll->active++;
3e369b76 15394 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 15395 }
5358901f 15396 }
5358901f 15397
1e6f2ddc 15398 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15399 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 15400
3e369b76 15401 if (pll->config.crtc_mask)
bd2bb1b9 15402 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
15403 }
15404
b2784e15 15405 for_each_intel_encoder(dev, encoder) {
24929352
DV
15406 pipe = 0;
15407
15408 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15409 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15410 encoder->base.crtc = &crtc->base;
6e3c9717 15411 encoder->get_config(encoder, crtc->config);
24929352
DV
15412 } else {
15413 encoder->base.crtc = NULL;
15414 }
15415
15416 encoder->connectors_active = false;
6f2bcceb 15417 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15418 encoder->base.base.id,
8e329a03 15419 encoder->base.name,
24929352 15420 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15421 pipe_name(pipe));
24929352
DV
15422 }
15423
3a3371ff 15424 for_each_intel_connector(dev, connector) {
24929352
DV
15425 if (connector->get_hw_state(connector)) {
15426 connector->base.dpms = DRM_MODE_DPMS_ON;
15427 connector->encoder->connectors_active = true;
15428 connector->base.encoder = &connector->encoder->base;
15429 } else {
15430 connector->base.dpms = DRM_MODE_DPMS_OFF;
15431 connector->base.encoder = NULL;
15432 }
15433 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15434 connector->base.base.id,
c23cc417 15435 connector->base.name,
24929352
DV
15436 connector->base.encoder ? "enabled" : "disabled");
15437 }
30e984df
DV
15438}
15439
15440/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15441 * and i915 state tracking structures. */
15442void intel_modeset_setup_hw_state(struct drm_device *dev,
15443 bool force_restore)
15444{
15445 struct drm_i915_private *dev_priv = dev->dev_private;
15446 enum pipe pipe;
30e984df
DV
15447 struct intel_crtc *crtc;
15448 struct intel_encoder *encoder;
35c95375 15449 int i;
30e984df
DV
15450
15451 intel_modeset_readout_hw_state(dev);
24929352 15452
babea61d
JB
15453 /*
15454 * Now that we have the config, copy it to each CRTC struct
15455 * Note that this could go away if we move to using crtc_config
15456 * checking everywhere.
15457 */
d3fcc808 15458 for_each_intel_crtc(dev, crtc) {
d330a953 15459 if (crtc->active && i915.fastboot) {
6e3c9717
ACO
15460 intel_mode_from_pipe_config(&crtc->base.mode,
15461 crtc->config);
babea61d
JB
15462 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15463 crtc->base.base.id);
15464 drm_mode_debug_printmodeline(&crtc->base.mode);
15465 }
15466 }
15467
24929352 15468 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15469 for_each_intel_encoder(dev, encoder) {
24929352
DV
15470 intel_sanitize_encoder(encoder);
15471 }
15472
055e393f 15473 for_each_pipe(dev_priv, pipe) {
24929352
DV
15474 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15475 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15476 intel_dump_pipe_config(crtc, crtc->config,
15477 "[setup_hw_state]");
24929352 15478 }
9a935856 15479
d29b2f9d
ACO
15480 intel_modeset_update_connector_atomic_state(dev);
15481
35c95375
DV
15482 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15483 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15484
15485 if (!pll->on || pll->active)
15486 continue;
15487
15488 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15489
15490 pll->disable(dev_priv, pll);
15491 pll->on = false;
15492 }
15493
3078999f
PB
15494 if (IS_GEN9(dev))
15495 skl_wm_get_hw_state(dev);
15496 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
15497 ilk_wm_get_hw_state(dev);
15498
45e2b5f6 15499 if (force_restore) {
7d0bc1ea
VS
15500 i915_redisable_vga(dev);
15501
f30da187
DV
15502 /*
15503 * We need to use raw interfaces for restoring state to avoid
15504 * checking (bogus) intermediate states.
15505 */
055e393f 15506 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
15507 struct drm_crtc *crtc =
15508 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187 15509
83a57153 15510 intel_crtc_restore_mode(crtc);
45e2b5f6
DV
15511 }
15512 } else {
15513 intel_modeset_update_staged_output_state(dev);
15514 }
8af6cf88
DV
15515
15516 intel_modeset_check_state(dev);
2c7111db
CW
15517}
15518
15519void intel_modeset_gem_init(struct drm_device *dev)
15520{
92122789 15521 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 15522 struct drm_crtc *c;
2ff8fde1 15523 struct drm_i915_gem_object *obj;
e0d6149b 15524 int ret;
484b41dd 15525
ae48434c
ID
15526 mutex_lock(&dev->struct_mutex);
15527 intel_init_gt_powersave(dev);
15528 mutex_unlock(&dev->struct_mutex);
15529
92122789
JB
15530 /*
15531 * There may be no VBT; and if the BIOS enabled SSC we can
15532 * just keep using it to avoid unnecessary flicker. Whereas if the
15533 * BIOS isn't using it, don't assume it will work even if the VBT
15534 * indicates as much.
15535 */
15536 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15537 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15538 DREF_SSC1_ENABLE);
15539
1833b134 15540 intel_modeset_init_hw(dev);
02e792fb
DV
15541
15542 intel_setup_overlay(dev);
484b41dd
JB
15543
15544 /*
15545 * Make sure any fbs we allocated at startup are properly
15546 * pinned & fenced. When we do the allocation it's too early
15547 * for this.
15548 */
70e1e0ec 15549 for_each_crtc(dev, c) {
2ff8fde1
MR
15550 obj = intel_fb_obj(c->primary->fb);
15551 if (obj == NULL)
484b41dd
JB
15552 continue;
15553
e0d6149b
TU
15554 mutex_lock(&dev->struct_mutex);
15555 ret = intel_pin_and_fence_fb_obj(c->primary,
15556 c->primary->fb,
15557 c->primary->state,
15558 NULL);
15559 mutex_unlock(&dev->struct_mutex);
15560 if (ret) {
484b41dd
JB
15561 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15562 to_intel_crtc(c)->pipe);
66e514c1
DA
15563 drm_framebuffer_unreference(c->primary->fb);
15564 c->primary->fb = NULL;
36750f28 15565 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 15566 update_state_fb(c->primary);
36750f28 15567 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15568 }
15569 }
0962c3c9
VS
15570
15571 intel_backlight_register(dev);
79e53945
JB
15572}
15573
4932e2c3
ID
15574void intel_connector_unregister(struct intel_connector *intel_connector)
15575{
15576 struct drm_connector *connector = &intel_connector->base;
15577
15578 intel_panel_destroy_backlight(connector);
34ea3d38 15579 drm_connector_unregister(connector);
4932e2c3
ID
15580}
15581
79e53945
JB
15582void intel_modeset_cleanup(struct drm_device *dev)
15583{
652c393a 15584 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 15585 struct drm_connector *connector;
652c393a 15586
2eb5252e
ID
15587 intel_disable_gt_powersave(dev);
15588
0962c3c9
VS
15589 intel_backlight_unregister(dev);
15590
fd0c0642
DV
15591 /*
15592 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15593 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15594 * experience fancy races otherwise.
15595 */
2aeb7d3a 15596 intel_irq_uninstall(dev_priv);
eb21b92b 15597
fd0c0642
DV
15598 /*
15599 * Due to the hpd irq storm handling the hotplug work can re-arm the
15600 * poll handlers. Hence disable polling after hpd handling is shut down.
15601 */
f87ea761 15602 drm_kms_helper_poll_fini(dev);
fd0c0642 15603
652c393a
JB
15604 mutex_lock(&dev->struct_mutex);
15605
723bfd70
JB
15606 intel_unregister_dsm_handler();
15607
7ff0ebcc 15608 intel_fbc_disable(dev);
e70236a8 15609
69341a5e
KH
15610 mutex_unlock(&dev->struct_mutex);
15611
1630fe75
CW
15612 /* flush any delayed tasks or pending work */
15613 flush_scheduled_work();
15614
db31af1d
JN
15615 /* destroy the backlight and sysfs files before encoders/connectors */
15616 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
15617 struct intel_connector *intel_connector;
15618
15619 intel_connector = to_intel_connector(connector);
15620 intel_connector->unregister(intel_connector);
db31af1d 15621 }
d9255d57 15622
79e53945 15623 drm_mode_config_cleanup(dev);
4d7bb011
DV
15624
15625 intel_cleanup_overlay(dev);
ae48434c
ID
15626
15627 mutex_lock(&dev->struct_mutex);
15628 intel_cleanup_gt_powersave(dev);
15629 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15630}
15631
f1c79df3
ZW
15632/*
15633 * Return which encoder is currently attached for connector.
15634 */
df0e9248 15635struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15636{
df0e9248
CW
15637 return &intel_attached_encoder(connector)->base;
15638}
f1c79df3 15639
df0e9248
CW
15640void intel_connector_attach_encoder(struct intel_connector *connector,
15641 struct intel_encoder *encoder)
15642{
15643 connector->encoder = encoder;
15644 drm_mode_connector_attach_encoder(&connector->base,
15645 &encoder->base);
79e53945 15646}
28d52043
DA
15647
15648/*
15649 * set vga decode state - true == enable VGA decode
15650 */
15651int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15652{
15653 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15654 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15655 u16 gmch_ctrl;
15656
75fa041d
CW
15657 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15658 DRM_ERROR("failed to read control word\n");
15659 return -EIO;
15660 }
15661
c0cc8a55
CW
15662 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15663 return 0;
15664
28d52043
DA
15665 if (state)
15666 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15667 else
15668 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15669
15670 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15671 DRM_ERROR("failed to write control word\n");
15672 return -EIO;
15673 }
15674
28d52043
DA
15675 return 0;
15676}
c4a1d9e4 15677
c4a1d9e4 15678struct intel_display_error_state {
ff57f1b0
PZ
15679
15680 u32 power_well_driver;
15681
63b66e5b
CW
15682 int num_transcoders;
15683
c4a1d9e4
CW
15684 struct intel_cursor_error_state {
15685 u32 control;
15686 u32 position;
15687 u32 base;
15688 u32 size;
52331309 15689 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15690
15691 struct intel_pipe_error_state {
ddf9c536 15692 bool power_domain_on;
c4a1d9e4 15693 u32 source;
f301b1e1 15694 u32 stat;
52331309 15695 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15696
15697 struct intel_plane_error_state {
15698 u32 control;
15699 u32 stride;
15700 u32 size;
15701 u32 pos;
15702 u32 addr;
15703 u32 surface;
15704 u32 tile_offset;
52331309 15705 } plane[I915_MAX_PIPES];
63b66e5b
CW
15706
15707 struct intel_transcoder_error_state {
ddf9c536 15708 bool power_domain_on;
63b66e5b
CW
15709 enum transcoder cpu_transcoder;
15710
15711 u32 conf;
15712
15713 u32 htotal;
15714 u32 hblank;
15715 u32 hsync;
15716 u32 vtotal;
15717 u32 vblank;
15718 u32 vsync;
15719 } transcoder[4];
c4a1d9e4
CW
15720};
15721
15722struct intel_display_error_state *
15723intel_display_capture_error_state(struct drm_device *dev)
15724{
fbee40df 15725 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 15726 struct intel_display_error_state *error;
63b66e5b
CW
15727 int transcoders[] = {
15728 TRANSCODER_A,
15729 TRANSCODER_B,
15730 TRANSCODER_C,
15731 TRANSCODER_EDP,
15732 };
c4a1d9e4
CW
15733 int i;
15734
63b66e5b
CW
15735 if (INTEL_INFO(dev)->num_pipes == 0)
15736 return NULL;
15737
9d1cb914 15738 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15739 if (error == NULL)
15740 return NULL;
15741
190be112 15742 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
15743 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15744
055e393f 15745 for_each_pipe(dev_priv, i) {
ddf9c536 15746 error->pipe[i].power_domain_on =
f458ebbc
DV
15747 __intel_display_power_is_enabled(dev_priv,
15748 POWER_DOMAIN_PIPE(i));
ddf9c536 15749 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15750 continue;
15751
5efb3e28
VS
15752 error->cursor[i].control = I915_READ(CURCNTR(i));
15753 error->cursor[i].position = I915_READ(CURPOS(i));
15754 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15755
15756 error->plane[i].control = I915_READ(DSPCNTR(i));
15757 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 15758 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 15759 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15760 error->plane[i].pos = I915_READ(DSPPOS(i));
15761 }
ca291363
PZ
15762 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15763 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
15764 if (INTEL_INFO(dev)->gen >= 4) {
15765 error->plane[i].surface = I915_READ(DSPSURF(i));
15766 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15767 }
15768
c4a1d9e4 15769 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15770
3abfce77 15771 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 15772 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15773 }
15774
15775 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15776 if (HAS_DDI(dev_priv->dev))
15777 error->num_transcoders++; /* Account for eDP. */
15778
15779 for (i = 0; i < error->num_transcoders; i++) {
15780 enum transcoder cpu_transcoder = transcoders[i];
15781
ddf9c536 15782 error->transcoder[i].power_domain_on =
f458ebbc 15783 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15784 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15785 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15786 continue;
15787
63b66e5b
CW
15788 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15789
15790 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15791 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15792 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15793 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15794 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15795 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15796 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15797 }
15798
15799 return error;
15800}
15801
edc3d884
MK
15802#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15803
c4a1d9e4 15804void
edc3d884 15805intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15806 struct drm_device *dev,
15807 struct intel_display_error_state *error)
15808{
055e393f 15809 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15810 int i;
15811
63b66e5b
CW
15812 if (!error)
15813 return;
15814
edc3d884 15815 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15816 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15817 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15818 error->power_well_driver);
055e393f 15819 for_each_pipe(dev_priv, i) {
edc3d884 15820 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
15821 err_printf(m, " Power: %s\n",
15822 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 15823 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15824 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15825
15826 err_printf(m, "Plane [%d]:\n", i);
15827 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15828 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15829 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15830 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15831 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15832 }
4b71a570 15833 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15834 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15835 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15836 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15837 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15838 }
15839
edc3d884
MK
15840 err_printf(m, "Cursor [%d]:\n", i);
15841 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15842 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15843 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15844 }
63b66e5b
CW
15845
15846 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 15847 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 15848 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
15849 err_printf(m, " Power: %s\n",
15850 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
15851 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15852 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15853 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15854 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15855 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15856 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15857 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15858 }
c4a1d9e4 15859}
e2fcdaa9
VS
15860
15861void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15862{
15863 struct intel_crtc *crtc;
15864
15865 for_each_intel_crtc(dev, crtc) {
15866 struct intel_unpin_work *work;
e2fcdaa9 15867
5e2d7afc 15868 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
15869
15870 work = crtc->unpin_work;
15871
15872 if (work && work->event &&
15873 work->event->base.file_priv == file) {
15874 kfree(work->event);
15875 work->event = NULL;
15876 }
15877
5e2d7afc 15878 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
15879 }
15880}