]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Make intel_display_suspend atomic, v2.
[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)
1701 count += crtc->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) &&
1c4e0274
VS
1782 intel_num_dvo_pipes(dev) == 1) {
1783 I915_WRITE(DPLL(PIPE_B),
1784 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1785 I915_WRITE(DPLL(PIPE_A),
1786 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1787 }
1788
b6b5d049
VS
1789 /* Don't disable pipe or pipe PLLs if needed */
1790 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1791 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1792 return;
1793
1794 /* Make sure the pipe isn't still relying on us */
1795 assert_pipe_disabled(dev_priv, pipe);
1796
50b44a44
DV
1797 I915_WRITE(DPLL(pipe), 0);
1798 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1799}
1800
f6071166
JB
1801static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1802{
1803 u32 val = 0;
1804
1805 /* Make sure the pipe isn't still relying on us */
1806 assert_pipe_disabled(dev_priv, pipe);
1807
e5cbfbfb
ID
1808 /*
1809 * Leave integrated clock source and reference clock enabled for pipe B.
1810 * The latter is needed for VGA hotplug / manual detection.
1811 */
f6071166 1812 if (pipe == PIPE_B)
e5cbfbfb 1813 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1814 I915_WRITE(DPLL(pipe), val);
1815 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1816
1817}
1818
1819static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1820{
d752048d 1821 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1822 u32 val;
1823
a11b0703
VS
1824 /* Make sure the pipe isn't still relying on us */
1825 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1826
a11b0703 1827 /* Set PLL en = 0 */
d17ec4ce 1828 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1829 if (pipe != PIPE_A)
1830 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1831 I915_WRITE(DPLL(pipe), val);
1832 POSTING_READ(DPLL(pipe));
d752048d 1833
a580516d 1834 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1835
1836 /* Disable 10bit clock to display controller */
1837 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1838 val &= ~DPIO_DCLKP_EN;
1839 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1840
61407f6d
VS
1841 /* disable left/right clock distribution */
1842 if (pipe != PIPE_B) {
1843 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1844 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1845 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1846 } else {
1847 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1848 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1849 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1850 }
1851
a580516d 1852 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1853}
1854
e4607fcf 1855void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1856 struct intel_digital_port *dport,
1857 unsigned int expected_mask)
89b667f8
JB
1858{
1859 u32 port_mask;
00fc31b7 1860 int dpll_reg;
89b667f8 1861
e4607fcf
CML
1862 switch (dport->port) {
1863 case PORT_B:
89b667f8 1864 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1865 dpll_reg = DPLL(0);
e4607fcf
CML
1866 break;
1867 case PORT_C:
89b667f8 1868 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1869 dpll_reg = DPLL(0);
9b6de0a1 1870 expected_mask <<= 4;
00fc31b7
CML
1871 break;
1872 case PORT_D:
1873 port_mask = DPLL_PORTD_READY_MASK;
1874 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1875 break;
1876 default:
1877 BUG();
1878 }
89b667f8 1879
9b6de0a1
VS
1880 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1881 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1882 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1883}
1884
b14b1055
DV
1885static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1886{
1887 struct drm_device *dev = crtc->base.dev;
1888 struct drm_i915_private *dev_priv = dev->dev_private;
1889 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1890
be19f0ff
CW
1891 if (WARN_ON(pll == NULL))
1892 return;
1893
3e369b76 1894 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1895 if (pll->active == 0) {
1896 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1897 WARN_ON(pll->on);
1898 assert_shared_dpll_disabled(dev_priv, pll);
1899
1900 pll->mode_set(dev_priv, pll);
1901 }
1902}
1903
92f2584a 1904/**
85b3894f 1905 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1906 * @dev_priv: i915 private structure
1907 * @pipe: pipe PLL to enable
1908 *
1909 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1910 * drives the transcoder clock.
1911 */
85b3894f 1912static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1913{
3d13ef2e
DL
1914 struct drm_device *dev = crtc->base.dev;
1915 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1916 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1917
87a875bb 1918 if (WARN_ON(pll == NULL))
48da64a8
CW
1919 return;
1920
3e369b76 1921 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1922 return;
ee7b9f93 1923
74dd6928 1924 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1925 pll->name, pll->active, pll->on,
e2b78267 1926 crtc->base.base.id);
92f2584a 1927
cdbd2316
DV
1928 if (pll->active++) {
1929 WARN_ON(!pll->on);
e9d6944e 1930 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1931 return;
1932 }
f4a091c7 1933 WARN_ON(pll->on);
ee7b9f93 1934
bd2bb1b9
PZ
1935 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1936
46edb027 1937 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1938 pll->enable(dev_priv, pll);
ee7b9f93 1939 pll->on = true;
92f2584a
JB
1940}
1941
f6daaec2 1942static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1943{
3d13ef2e
DL
1944 struct drm_device *dev = crtc->base.dev;
1945 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1946 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1947
92f2584a 1948 /* PCH only available on ILK+ */
3d13ef2e 1949 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1950 if (WARN_ON(pll == NULL))
ee7b9f93 1951 return;
92f2584a 1952
3e369b76 1953 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1954 return;
7a419866 1955
46edb027
DV
1956 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1957 pll->name, pll->active, pll->on,
e2b78267 1958 crtc->base.base.id);
7a419866 1959
48da64a8 1960 if (WARN_ON(pll->active == 0)) {
e9d6944e 1961 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1962 return;
1963 }
1964
e9d6944e 1965 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1966 WARN_ON(!pll->on);
cdbd2316 1967 if (--pll->active)
7a419866 1968 return;
ee7b9f93 1969
46edb027 1970 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1971 pll->disable(dev_priv, pll);
ee7b9f93 1972 pll->on = false;
bd2bb1b9
PZ
1973
1974 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1975}
1976
b8a4f404
PZ
1977static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1978 enum pipe pipe)
040484af 1979{
23670b32 1980 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1981 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1982 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1983 uint32_t reg, val, pipeconf_val;
040484af
JB
1984
1985 /* PCH only available on ILK+ */
55522f37 1986 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1987
1988 /* Make sure PCH DPLL is enabled */
e72f9fbf 1989 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1990 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1991
1992 /* FDI must be feeding us bits for PCH ports */
1993 assert_fdi_tx_enabled(dev_priv, pipe);
1994 assert_fdi_rx_enabled(dev_priv, pipe);
1995
23670b32
DV
1996 if (HAS_PCH_CPT(dev)) {
1997 /* Workaround: Set the timing override bit before enabling the
1998 * pch transcoder. */
1999 reg = TRANS_CHICKEN2(pipe);
2000 val = I915_READ(reg);
2001 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2002 I915_WRITE(reg, val);
59c859d6 2003 }
23670b32 2004
ab9412ba 2005 reg = PCH_TRANSCONF(pipe);
040484af 2006 val = I915_READ(reg);
5f7f726d 2007 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
2008
2009 if (HAS_PCH_IBX(dev_priv->dev)) {
2010 /*
2011 * make the BPC in transcoder be consistent with
2012 * that in pipeconf reg.
2013 */
dfd07d72
DV
2014 val &= ~PIPECONF_BPC_MASK;
2015 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 2016 }
5f7f726d
PZ
2017
2018 val &= ~TRANS_INTERLACE_MASK;
2019 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 2020 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 2021 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
2022 val |= TRANS_LEGACY_INTERLACED_ILK;
2023 else
2024 val |= TRANS_INTERLACED;
5f7f726d
PZ
2025 else
2026 val |= TRANS_PROGRESSIVE;
2027
040484af
JB
2028 I915_WRITE(reg, val | TRANS_ENABLE);
2029 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 2030 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
2031}
2032
8fb033d7 2033static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 2034 enum transcoder cpu_transcoder)
040484af 2035{
8fb033d7 2036 u32 val, pipeconf_val;
8fb033d7
PZ
2037
2038 /* PCH only available on ILK+ */
55522f37 2039 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 2040
8fb033d7 2041 /* FDI must be feeding us bits for PCH ports */
1a240d4d 2042 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 2043 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 2044
223a6fdf
PZ
2045 /* Workaround: set timing override bit. */
2046 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2047 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
2048 I915_WRITE(_TRANSA_CHICKEN2, val);
2049
25f3ef11 2050 val = TRANS_ENABLE;
937bb610 2051 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 2052
9a76b1c6
PZ
2053 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2054 PIPECONF_INTERLACED_ILK)
a35f2679 2055 val |= TRANS_INTERLACED;
8fb033d7
PZ
2056 else
2057 val |= TRANS_PROGRESSIVE;
2058
ab9412ba
DV
2059 I915_WRITE(LPT_TRANSCONF, val);
2060 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 2061 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
2062}
2063
b8a4f404
PZ
2064static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2065 enum pipe pipe)
040484af 2066{
23670b32
DV
2067 struct drm_device *dev = dev_priv->dev;
2068 uint32_t reg, val;
040484af
JB
2069
2070 /* FDI relies on the transcoder */
2071 assert_fdi_tx_disabled(dev_priv, pipe);
2072 assert_fdi_rx_disabled(dev_priv, pipe);
2073
291906f1
JB
2074 /* Ports must be off as well */
2075 assert_pch_ports_disabled(dev_priv, pipe);
2076
ab9412ba 2077 reg = PCH_TRANSCONF(pipe);
040484af
JB
2078 val = I915_READ(reg);
2079 val &= ~TRANS_ENABLE;
2080 I915_WRITE(reg, val);
2081 /* wait for PCH transcoder off, transcoder state */
2082 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 2083 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
2084
2085 if (!HAS_PCH_IBX(dev)) {
2086 /* Workaround: Clear the timing override chicken bit again. */
2087 reg = TRANS_CHICKEN2(pipe);
2088 val = I915_READ(reg);
2089 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2090 I915_WRITE(reg, val);
2091 }
040484af
JB
2092}
2093
ab4d966c 2094static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 2095{
8fb033d7
PZ
2096 u32 val;
2097
ab9412ba 2098 val = I915_READ(LPT_TRANSCONF);
8fb033d7 2099 val &= ~TRANS_ENABLE;
ab9412ba 2100 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2101 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2102 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2103 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2104
2105 /* Workaround: clear timing override bit. */
2106 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2107 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2108 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2109}
2110
b24e7179 2111/**
309cfea8 2112 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2113 * @crtc: crtc responsible for the pipe
b24e7179 2114 *
0372264a 2115 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2116 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2117 */
e1fdc473 2118static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2119{
0372264a
PZ
2120 struct drm_device *dev = crtc->base.dev;
2121 struct drm_i915_private *dev_priv = dev->dev_private;
2122 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2123 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2124 pipe);
1a240d4d 2125 enum pipe pch_transcoder;
b24e7179
JB
2126 int reg;
2127 u32 val;
2128
58c6eaa2 2129 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2130 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2131 assert_sprites_disabled(dev_priv, pipe);
2132
681e5811 2133 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2134 pch_transcoder = TRANSCODER_A;
2135 else
2136 pch_transcoder = pipe;
2137
b24e7179
JB
2138 /*
2139 * A pipe without a PLL won't actually be able to drive bits from
2140 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2141 * need the check.
2142 */
50360403 2143 if (HAS_GMCH_DISPLAY(dev_priv->dev))
409ee761 2144 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2145 assert_dsi_pll_enabled(dev_priv);
2146 else
2147 assert_pll_enabled(dev_priv, pipe);
040484af 2148 else {
6e3c9717 2149 if (crtc->config->has_pch_encoder) {
040484af 2150 /* if driving the PCH, we need FDI enabled */
cc391bbb 2151 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2152 assert_fdi_tx_pll_enabled(dev_priv,
2153 (enum pipe) cpu_transcoder);
040484af
JB
2154 }
2155 /* FIXME: assert CPU port conditions for SNB+ */
2156 }
b24e7179 2157
702e7a56 2158 reg = PIPECONF(cpu_transcoder);
b24e7179 2159 val = I915_READ(reg);
7ad25d48 2160 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2161 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2162 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2163 return;
7ad25d48 2164 }
00d70b15
CW
2165
2166 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2167 POSTING_READ(reg);
b24e7179
JB
2168}
2169
2170/**
309cfea8 2171 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2172 * @crtc: crtc whose pipes is to be disabled
b24e7179 2173 *
575f7ab7
VS
2174 * Disable the pipe of @crtc, making sure that various hardware
2175 * specific requirements are met, if applicable, e.g. plane
2176 * disabled, panel fitter off, etc.
b24e7179
JB
2177 *
2178 * Will wait until the pipe has shut down before returning.
2179 */
575f7ab7 2180static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2181{
575f7ab7 2182 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2183 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2184 enum pipe pipe = crtc->pipe;
b24e7179
JB
2185 int reg;
2186 u32 val;
2187
2188 /*
2189 * Make sure planes won't keep trying to pump pixels to us,
2190 * or we might hang the display.
2191 */
2192 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2193 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2194 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2195
702e7a56 2196 reg = PIPECONF(cpu_transcoder);
b24e7179 2197 val = I915_READ(reg);
00d70b15
CW
2198 if ((val & PIPECONF_ENABLE) == 0)
2199 return;
2200
67adc644
VS
2201 /*
2202 * Double wide has implications for planes
2203 * so best keep it disabled when not needed.
2204 */
6e3c9717 2205 if (crtc->config->double_wide)
67adc644
VS
2206 val &= ~PIPECONF_DOUBLE_WIDE;
2207
2208 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2209 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2210 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2211 val &= ~PIPECONF_ENABLE;
2212
2213 I915_WRITE(reg, val);
2214 if ((val & PIPECONF_ENABLE) == 0)
2215 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2216}
2217
2218/**
262ca2b0 2219 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2220 * @plane: plane to be enabled
2221 * @crtc: crtc for the plane
b24e7179 2222 *
fdd508a6 2223 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2224 */
fdd508a6
VS
2225static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2226 struct drm_crtc *crtc)
b24e7179 2227{
fdd508a6
VS
2228 struct drm_device *dev = plane->dev;
2229 struct drm_i915_private *dev_priv = dev->dev_private;
2230 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2231
2232 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2233 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b70709a6 2234 to_intel_plane_state(plane->state)->visible = true;
939c2fe8 2235
fdd508a6
VS
2236 dev_priv->display.update_primary_plane(crtc, plane->fb,
2237 crtc->x, crtc->y);
b24e7179
JB
2238}
2239
693db184
CW
2240static bool need_vtd_wa(struct drm_device *dev)
2241{
2242#ifdef CONFIG_INTEL_IOMMU
2243 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2244 return true;
2245#endif
2246 return false;
2247}
2248
50470bb0 2249unsigned int
6761dd31
TU
2250intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2251 uint64_t fb_format_modifier)
a57ce0b2 2252{
6761dd31
TU
2253 unsigned int tile_height;
2254 uint32_t pixel_bytes;
a57ce0b2 2255
b5d0e9bf
DL
2256 switch (fb_format_modifier) {
2257 case DRM_FORMAT_MOD_NONE:
2258 tile_height = 1;
2259 break;
2260 case I915_FORMAT_MOD_X_TILED:
2261 tile_height = IS_GEN2(dev) ? 16 : 8;
2262 break;
2263 case I915_FORMAT_MOD_Y_TILED:
2264 tile_height = 32;
2265 break;
2266 case I915_FORMAT_MOD_Yf_TILED:
6761dd31
TU
2267 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2268 switch (pixel_bytes) {
b5d0e9bf 2269 default:
6761dd31 2270 case 1:
b5d0e9bf
DL
2271 tile_height = 64;
2272 break;
6761dd31
TU
2273 case 2:
2274 case 4:
b5d0e9bf
DL
2275 tile_height = 32;
2276 break;
6761dd31 2277 case 8:
b5d0e9bf
DL
2278 tile_height = 16;
2279 break;
6761dd31 2280 case 16:
b5d0e9bf
DL
2281 WARN_ONCE(1,
2282 "128-bit pixels are not supported for display!");
2283 tile_height = 16;
2284 break;
2285 }
2286 break;
2287 default:
2288 MISSING_CASE(fb_format_modifier);
2289 tile_height = 1;
2290 break;
2291 }
091df6cb 2292
6761dd31
TU
2293 return tile_height;
2294}
2295
2296unsigned int
2297intel_fb_align_height(struct drm_device *dev, unsigned int height,
2298 uint32_t pixel_format, uint64_t fb_format_modifier)
2299{
2300 return ALIGN(height, intel_tile_height(dev, pixel_format,
2301 fb_format_modifier));
a57ce0b2
JB
2302}
2303
f64b98cd
TU
2304static int
2305intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2306 const struct drm_plane_state *plane_state)
2307{
50470bb0 2308 struct intel_rotation_info *info = &view->rotation_info;
50470bb0 2309
f64b98cd
TU
2310 *view = i915_ggtt_view_normal;
2311
50470bb0
TU
2312 if (!plane_state)
2313 return 0;
2314
121920fa 2315 if (!intel_rotation_90_or_270(plane_state->rotation))
50470bb0
TU
2316 return 0;
2317
9abc4648 2318 *view = i915_ggtt_view_rotated;
50470bb0
TU
2319
2320 info->height = fb->height;
2321 info->pixel_format = fb->pixel_format;
2322 info->pitch = fb->pitches[0];
2323 info->fb_modifier = fb->modifier[0];
2324
f64b98cd
TU
2325 return 0;
2326}
2327
127bd2ac 2328int
850c4cdc
TU
2329intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2330 struct drm_framebuffer *fb,
82bc3b2d 2331 const struct drm_plane_state *plane_state,
a4872ba6 2332 struct intel_engine_cs *pipelined)
6b95a207 2333{
850c4cdc 2334 struct drm_device *dev = fb->dev;
ce453d81 2335 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2336 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2337 struct i915_ggtt_view view;
6b95a207
KH
2338 u32 alignment;
2339 int ret;
2340
ebcdd39e
MR
2341 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2342
7b911adc
TU
2343 switch (fb->modifier[0]) {
2344 case DRM_FORMAT_MOD_NONE:
1fada4cc
DL
2345 if (INTEL_INFO(dev)->gen >= 9)
2346 alignment = 256 * 1024;
2347 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
534843da 2348 alignment = 128 * 1024;
a6c45cf0 2349 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2350 alignment = 4 * 1024;
2351 else
2352 alignment = 64 * 1024;
6b95a207 2353 break;
7b911adc 2354 case I915_FORMAT_MOD_X_TILED:
1fada4cc
DL
2355 if (INTEL_INFO(dev)->gen >= 9)
2356 alignment = 256 * 1024;
2357 else {
2358 /* pin() will align the object as required by fence */
2359 alignment = 0;
2360 }
6b95a207 2361 break;
7b911adc 2362 case I915_FORMAT_MOD_Y_TILED:
1327b9a1
DL
2363 case I915_FORMAT_MOD_Yf_TILED:
2364 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2365 "Y tiling bo slipped through, driver bug!\n"))
2366 return -EINVAL;
2367 alignment = 1 * 1024 * 1024;
2368 break;
6b95a207 2369 default:
7b911adc
TU
2370 MISSING_CASE(fb->modifier[0]);
2371 return -EINVAL;
6b95a207
KH
2372 }
2373
f64b98cd
TU
2374 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2375 if (ret)
2376 return ret;
2377
693db184
CW
2378 /* Note that the w/a also requires 64 PTE of padding following the
2379 * bo. We currently fill all unused PTE with the shadow page and so
2380 * we should always have valid PTE following the scanout preventing
2381 * the VT-d warning.
2382 */
2383 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2384 alignment = 256 * 1024;
2385
d6dd6843
PZ
2386 /*
2387 * Global gtt pte registers are special registers which actually forward
2388 * writes to a chunk of system memory. Which means that there is no risk
2389 * that the register values disappear as soon as we call
2390 * intel_runtime_pm_put(), so it is correct to wrap only the
2391 * pin/unpin/fence and not more.
2392 */
2393 intel_runtime_pm_get(dev_priv);
2394
ce453d81 2395 dev_priv->mm.interruptible = false;
e6617330 2396 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
f64b98cd 2397 &view);
48b956c5 2398 if (ret)
ce453d81 2399 goto err_interruptible;
6b95a207
KH
2400
2401 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2402 * fence, whereas 965+ only requires a fence if using
2403 * framebuffer compression. For simplicity, we always install
2404 * a fence as the cost is not that onerous.
2405 */
06d98131 2406 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2407 if (ret)
2408 goto err_unpin;
1690e1eb 2409
9a5a53b3 2410 i915_gem_object_pin_fence(obj);
6b95a207 2411
ce453d81 2412 dev_priv->mm.interruptible = true;
d6dd6843 2413 intel_runtime_pm_put(dev_priv);
6b95a207 2414 return 0;
48b956c5
CW
2415
2416err_unpin:
f64b98cd 2417 i915_gem_object_unpin_from_display_plane(obj, &view);
ce453d81
CW
2418err_interruptible:
2419 dev_priv->mm.interruptible = true;
d6dd6843 2420 intel_runtime_pm_put(dev_priv);
48b956c5 2421 return ret;
6b95a207
KH
2422}
2423
82bc3b2d
TU
2424static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2425 const struct drm_plane_state *plane_state)
1690e1eb 2426{
82bc3b2d 2427 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd
TU
2428 struct i915_ggtt_view view;
2429 int ret;
82bc3b2d 2430
ebcdd39e
MR
2431 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2432
f64b98cd
TU
2433 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2434 WARN_ONCE(ret, "Couldn't get view from plane state!");
2435
1690e1eb 2436 i915_gem_object_unpin_fence(obj);
f64b98cd 2437 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2438}
2439
c2c75131
DV
2440/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2441 * is assumed to be a power-of-two. */
bc752862
CW
2442unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2443 unsigned int tiling_mode,
2444 unsigned int cpp,
2445 unsigned int pitch)
c2c75131 2446{
bc752862
CW
2447 if (tiling_mode != I915_TILING_NONE) {
2448 unsigned int tile_rows, tiles;
c2c75131 2449
bc752862
CW
2450 tile_rows = *y / 8;
2451 *y %= 8;
c2c75131 2452
bc752862
CW
2453 tiles = *x / (512/cpp);
2454 *x %= 512/cpp;
2455
2456 return tile_rows * pitch * 8 + tiles * 4096;
2457 } else {
2458 unsigned int offset;
2459
2460 offset = *y * pitch + *x * cpp;
2461 *y = 0;
2462 *x = (offset & 4095) / cpp;
2463 return offset & -4096;
2464 }
c2c75131
DV
2465}
2466
b35d63fa 2467static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2468{
2469 switch (format) {
2470 case DISPPLANE_8BPP:
2471 return DRM_FORMAT_C8;
2472 case DISPPLANE_BGRX555:
2473 return DRM_FORMAT_XRGB1555;
2474 case DISPPLANE_BGRX565:
2475 return DRM_FORMAT_RGB565;
2476 default:
2477 case DISPPLANE_BGRX888:
2478 return DRM_FORMAT_XRGB8888;
2479 case DISPPLANE_RGBX888:
2480 return DRM_FORMAT_XBGR8888;
2481 case DISPPLANE_BGRX101010:
2482 return DRM_FORMAT_XRGB2101010;
2483 case DISPPLANE_RGBX101010:
2484 return DRM_FORMAT_XBGR2101010;
2485 }
2486}
2487
bc8d7dff
DL
2488static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2489{
2490 switch (format) {
2491 case PLANE_CTL_FORMAT_RGB_565:
2492 return DRM_FORMAT_RGB565;
2493 default:
2494 case PLANE_CTL_FORMAT_XRGB_8888:
2495 if (rgb_order) {
2496 if (alpha)
2497 return DRM_FORMAT_ABGR8888;
2498 else
2499 return DRM_FORMAT_XBGR8888;
2500 } else {
2501 if (alpha)
2502 return DRM_FORMAT_ARGB8888;
2503 else
2504 return DRM_FORMAT_XRGB8888;
2505 }
2506 case PLANE_CTL_FORMAT_XRGB_2101010:
2507 if (rgb_order)
2508 return DRM_FORMAT_XBGR2101010;
2509 else
2510 return DRM_FORMAT_XRGB2101010;
2511 }
2512}
2513
5724dbd1 2514static bool
f6936e29
DV
2515intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2516 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2517{
2518 struct drm_device *dev = crtc->base.dev;
2519 struct drm_i915_gem_object *obj = NULL;
2520 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2521 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2522 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2523 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2524 PAGE_SIZE);
2525
2526 size_aligned -= base_aligned;
46f297fb 2527
ff2652ea
CW
2528 if (plane_config->size == 0)
2529 return false;
2530
f37b5c2b
DV
2531 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2532 base_aligned,
2533 base_aligned,
2534 size_aligned);
46f297fb 2535 if (!obj)
484b41dd 2536 return false;
46f297fb 2537
49af449b
DL
2538 obj->tiling_mode = plane_config->tiling;
2539 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2540 obj->stride = fb->pitches[0];
46f297fb 2541
6bf129df
DL
2542 mode_cmd.pixel_format = fb->pixel_format;
2543 mode_cmd.width = fb->width;
2544 mode_cmd.height = fb->height;
2545 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2546 mode_cmd.modifier[0] = fb->modifier[0];
2547 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb
JB
2548
2549 mutex_lock(&dev->struct_mutex);
6bf129df 2550 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2551 &mode_cmd, obj)) {
46f297fb
JB
2552 DRM_DEBUG_KMS("intel fb init failed\n");
2553 goto out_unref_obj;
2554 }
46f297fb 2555 mutex_unlock(&dev->struct_mutex);
484b41dd 2556
f6936e29 2557 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2558 return true;
46f297fb
JB
2559
2560out_unref_obj:
2561 drm_gem_object_unreference(&obj->base);
2562 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2563 return false;
2564}
2565
afd65eb4
MR
2566/* Update plane->state->fb to match plane->fb after driver-internal updates */
2567static void
2568update_state_fb(struct drm_plane *plane)
2569{
2570 if (plane->fb == plane->state->fb)
2571 return;
2572
2573 if (plane->state->fb)
2574 drm_framebuffer_unreference(plane->state->fb);
2575 plane->state->fb = plane->fb;
2576 if (plane->state->fb)
2577 drm_framebuffer_reference(plane->state->fb);
2578}
2579
5724dbd1 2580static void
f6936e29
DV
2581intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2582 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2583{
2584 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2585 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2586 struct drm_crtc *c;
2587 struct intel_crtc *i;
2ff8fde1 2588 struct drm_i915_gem_object *obj;
88595ac9
DV
2589 struct drm_plane *primary = intel_crtc->base.primary;
2590 struct drm_framebuffer *fb;
484b41dd 2591
2d14030b 2592 if (!plane_config->fb)
484b41dd
JB
2593 return;
2594
f6936e29 2595 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2596 fb = &plane_config->fb->base;
2597 goto valid_fb;
f55548b5 2598 }
484b41dd 2599
2d14030b 2600 kfree(plane_config->fb);
484b41dd
JB
2601
2602 /*
2603 * Failed to alloc the obj, check to see if we should share
2604 * an fb with another CRTC instead
2605 */
70e1e0ec 2606 for_each_crtc(dev, c) {
484b41dd
JB
2607 i = to_intel_crtc(c);
2608
2609 if (c == &intel_crtc->base)
2610 continue;
2611
2ff8fde1
MR
2612 if (!i->active)
2613 continue;
2614
88595ac9
DV
2615 fb = c->primary->fb;
2616 if (!fb)
484b41dd
JB
2617 continue;
2618
88595ac9 2619 obj = intel_fb_obj(fb);
2ff8fde1 2620 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2621 drm_framebuffer_reference(fb);
2622 goto valid_fb;
484b41dd
JB
2623 }
2624 }
88595ac9
DV
2625
2626 return;
2627
2628valid_fb:
2629 obj = intel_fb_obj(fb);
2630 if (obj->tiling_mode != I915_TILING_NONE)
2631 dev_priv->preserve_bios_swizzle = true;
2632
2633 primary->fb = fb;
36750f28 2634 primary->crtc = primary->state->crtc = &intel_crtc->base;
88595ac9 2635 update_state_fb(primary);
36750f28 2636 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
88595ac9 2637 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
46f297fb
JB
2638}
2639
29b9bde6
DV
2640static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2641 struct drm_framebuffer *fb,
2642 int x, int y)
81255565
JB
2643{
2644 struct drm_device *dev = crtc->dev;
2645 struct drm_i915_private *dev_priv = dev->dev_private;
2646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2647 struct drm_plane *primary = crtc->primary;
2648 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2649 struct drm_i915_gem_object *obj;
81255565 2650 int plane = intel_crtc->plane;
e506a0c6 2651 unsigned long linear_offset;
81255565 2652 u32 dspcntr;
f45651ba 2653 u32 reg = DSPCNTR(plane);
48404c1e 2654 int pixel_size;
f45651ba 2655
b70709a6 2656 if (!visible || !fb) {
fdd508a6
VS
2657 I915_WRITE(reg, 0);
2658 if (INTEL_INFO(dev)->gen >= 4)
2659 I915_WRITE(DSPSURF(plane), 0);
2660 else
2661 I915_WRITE(DSPADDR(plane), 0);
2662 POSTING_READ(reg);
2663 return;
2664 }
2665
c9ba6fad
VS
2666 obj = intel_fb_obj(fb);
2667 if (WARN_ON(obj == NULL))
2668 return;
2669
2670 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2671
f45651ba
VS
2672 dspcntr = DISPPLANE_GAMMA_ENABLE;
2673
fdd508a6 2674 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2675
2676 if (INTEL_INFO(dev)->gen < 4) {
2677 if (intel_crtc->pipe == PIPE_B)
2678 dspcntr |= DISPPLANE_SEL_PIPE_B;
2679
2680 /* pipesrc and dspsize control the size that is scaled from,
2681 * which should always be the user's requested size.
2682 */
2683 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2684 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2685 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2686 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2687 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2688 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2689 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2690 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2691 I915_WRITE(PRIMPOS(plane), 0);
2692 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2693 }
81255565 2694
57779d06
VS
2695 switch (fb->pixel_format) {
2696 case DRM_FORMAT_C8:
81255565
JB
2697 dspcntr |= DISPPLANE_8BPP;
2698 break;
57779d06 2699 case DRM_FORMAT_XRGB1555:
57779d06 2700 dspcntr |= DISPPLANE_BGRX555;
81255565 2701 break;
57779d06
VS
2702 case DRM_FORMAT_RGB565:
2703 dspcntr |= DISPPLANE_BGRX565;
2704 break;
2705 case DRM_FORMAT_XRGB8888:
57779d06
VS
2706 dspcntr |= DISPPLANE_BGRX888;
2707 break;
2708 case DRM_FORMAT_XBGR8888:
57779d06
VS
2709 dspcntr |= DISPPLANE_RGBX888;
2710 break;
2711 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2712 dspcntr |= DISPPLANE_BGRX101010;
2713 break;
2714 case DRM_FORMAT_XBGR2101010:
57779d06 2715 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2716 break;
2717 default:
baba133a 2718 BUG();
81255565 2719 }
57779d06 2720
f45651ba
VS
2721 if (INTEL_INFO(dev)->gen >= 4 &&
2722 obj->tiling_mode != I915_TILING_NONE)
2723 dspcntr |= DISPPLANE_TILED;
81255565 2724
de1aa629
VS
2725 if (IS_G4X(dev))
2726 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2727
b9897127 2728 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2729
c2c75131
DV
2730 if (INTEL_INFO(dev)->gen >= 4) {
2731 intel_crtc->dspaddr_offset =
bc752862 2732 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2733 pixel_size,
bc752862 2734 fb->pitches[0]);
c2c75131
DV
2735 linear_offset -= intel_crtc->dspaddr_offset;
2736 } else {
e506a0c6 2737 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2738 }
e506a0c6 2739
8e7d688b 2740 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2741 dspcntr |= DISPPLANE_ROTATE_180;
2742
6e3c9717
ACO
2743 x += (intel_crtc->config->pipe_src_w - 1);
2744 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2745
2746 /* Finding the last pixel of the last line of the display
2747 data and adding to linear_offset*/
2748 linear_offset +=
6e3c9717
ACO
2749 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2750 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2751 }
2752
2753 I915_WRITE(reg, dspcntr);
2754
01f2c773 2755 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2756 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2757 I915_WRITE(DSPSURF(plane),
2758 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2759 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2760 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2761 } else
f343c5f6 2762 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2763 POSTING_READ(reg);
17638cd6
JB
2764}
2765
29b9bde6
DV
2766static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2767 struct drm_framebuffer *fb,
2768 int x, int y)
17638cd6
JB
2769{
2770 struct drm_device *dev = crtc->dev;
2771 struct drm_i915_private *dev_priv = dev->dev_private;
2772 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2773 struct drm_plane *primary = crtc->primary;
2774 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2775 struct drm_i915_gem_object *obj;
17638cd6 2776 int plane = intel_crtc->plane;
e506a0c6 2777 unsigned long linear_offset;
17638cd6 2778 u32 dspcntr;
f45651ba 2779 u32 reg = DSPCNTR(plane);
48404c1e 2780 int pixel_size;
f45651ba 2781
b70709a6 2782 if (!visible || !fb) {
fdd508a6
VS
2783 I915_WRITE(reg, 0);
2784 I915_WRITE(DSPSURF(plane), 0);
2785 POSTING_READ(reg);
2786 return;
2787 }
2788
c9ba6fad
VS
2789 obj = intel_fb_obj(fb);
2790 if (WARN_ON(obj == NULL))
2791 return;
2792
2793 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2794
f45651ba
VS
2795 dspcntr = DISPPLANE_GAMMA_ENABLE;
2796
fdd508a6 2797 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2798
2799 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2800 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2801
57779d06
VS
2802 switch (fb->pixel_format) {
2803 case DRM_FORMAT_C8:
17638cd6
JB
2804 dspcntr |= DISPPLANE_8BPP;
2805 break;
57779d06
VS
2806 case DRM_FORMAT_RGB565:
2807 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2808 break;
57779d06 2809 case DRM_FORMAT_XRGB8888:
57779d06
VS
2810 dspcntr |= DISPPLANE_BGRX888;
2811 break;
2812 case DRM_FORMAT_XBGR8888:
57779d06
VS
2813 dspcntr |= DISPPLANE_RGBX888;
2814 break;
2815 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2816 dspcntr |= DISPPLANE_BGRX101010;
2817 break;
2818 case DRM_FORMAT_XBGR2101010:
57779d06 2819 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2820 break;
2821 default:
baba133a 2822 BUG();
17638cd6
JB
2823 }
2824
2825 if (obj->tiling_mode != I915_TILING_NONE)
2826 dspcntr |= DISPPLANE_TILED;
17638cd6 2827
f45651ba 2828 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2829 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2830
b9897127 2831 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2832 intel_crtc->dspaddr_offset =
bc752862 2833 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2834 pixel_size,
bc752862 2835 fb->pitches[0]);
c2c75131 2836 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2837 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2838 dspcntr |= DISPPLANE_ROTATE_180;
2839
2840 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2841 x += (intel_crtc->config->pipe_src_w - 1);
2842 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2843
2844 /* Finding the last pixel of the last line of the display
2845 data and adding to linear_offset*/
2846 linear_offset +=
6e3c9717
ACO
2847 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2848 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2849 }
2850 }
2851
2852 I915_WRITE(reg, dspcntr);
17638cd6 2853
01f2c773 2854 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2855 I915_WRITE(DSPSURF(plane),
2856 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2857 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2858 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2859 } else {
2860 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2861 I915_WRITE(DSPLINOFF(plane), linear_offset);
2862 }
17638cd6 2863 POSTING_READ(reg);
17638cd6
JB
2864}
2865
b321803d
DL
2866u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2867 uint32_t pixel_format)
2868{
2869 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2870
2871 /*
2872 * The stride is either expressed as a multiple of 64 bytes
2873 * chunks for linear buffers or in number of tiles for tiled
2874 * buffers.
2875 */
2876 switch (fb_modifier) {
2877 case DRM_FORMAT_MOD_NONE:
2878 return 64;
2879 case I915_FORMAT_MOD_X_TILED:
2880 if (INTEL_INFO(dev)->gen == 2)
2881 return 128;
2882 return 512;
2883 case I915_FORMAT_MOD_Y_TILED:
2884 /* No need to check for old gens and Y tiling since this is
2885 * about the display engine and those will be blocked before
2886 * we get here.
2887 */
2888 return 128;
2889 case I915_FORMAT_MOD_Yf_TILED:
2890 if (bits_per_pixel == 8)
2891 return 64;
2892 else
2893 return 128;
2894 default:
2895 MISSING_CASE(fb_modifier);
2896 return 64;
2897 }
2898}
2899
121920fa
TU
2900unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2901 struct drm_i915_gem_object *obj)
2902{
9abc4648 2903 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
121920fa
TU
2904
2905 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
9abc4648 2906 view = &i915_ggtt_view_rotated;
121920fa
TU
2907
2908 return i915_gem_obj_ggtt_offset_view(obj, view);
2909}
2910
a1b2278e
CK
2911/*
2912 * This function detaches (aka. unbinds) unused scalers in hardware
2913 */
2914void skl_detach_scalers(struct intel_crtc *intel_crtc)
2915{
2916 struct drm_device *dev;
2917 struct drm_i915_private *dev_priv;
2918 struct intel_crtc_scaler_state *scaler_state;
2919 int i;
2920
2921 if (!intel_crtc || !intel_crtc->config)
2922 return;
2923
2924 dev = intel_crtc->base.dev;
2925 dev_priv = dev->dev_private;
2926 scaler_state = &intel_crtc->config->scaler_state;
2927
2928 /* loop through and disable scalers that aren't in use */
2929 for (i = 0; i < intel_crtc->num_scalers; i++) {
2930 if (!scaler_state->scalers[i].in_use) {
2931 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2932 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2933 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2934 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2935 intel_crtc->base.base.id, intel_crtc->pipe, i);
2936 }
2937 }
2938}
2939
6156a456 2940u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2941{
6156a456 2942 switch (pixel_format) {
d161cf7a 2943 case DRM_FORMAT_C8:
c34ce3d1 2944 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2945 case DRM_FORMAT_RGB565:
c34ce3d1 2946 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2947 case DRM_FORMAT_XBGR8888:
c34ce3d1 2948 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2949 case DRM_FORMAT_XRGB8888:
c34ce3d1 2950 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2951 /*
2952 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2953 * to be already pre-multiplied. We need to add a knob (or a different
2954 * DRM_FORMAT) for user-space to configure that.
2955 */
f75fb42a 2956 case DRM_FORMAT_ABGR8888:
c34ce3d1 2957 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2958 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2959 case DRM_FORMAT_ARGB8888:
c34ce3d1 2960 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2961 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2962 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2963 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2964 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2965 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2966 case DRM_FORMAT_YUYV:
c34ce3d1 2967 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2968 case DRM_FORMAT_YVYU:
c34ce3d1 2969 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2970 case DRM_FORMAT_UYVY:
c34ce3d1 2971 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2972 case DRM_FORMAT_VYUY:
c34ce3d1 2973 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2974 default:
4249eeef 2975 MISSING_CASE(pixel_format);
70d21f0e 2976 }
8cfcba41 2977
c34ce3d1 2978 return 0;
6156a456 2979}
70d21f0e 2980
6156a456
CK
2981u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2982{
6156a456 2983 switch (fb_modifier) {
30af77c4 2984 case DRM_FORMAT_MOD_NONE:
70d21f0e 2985 break;
30af77c4 2986 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2987 return PLANE_CTL_TILED_X;
b321803d 2988 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2989 return PLANE_CTL_TILED_Y;
b321803d 2990 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2991 return PLANE_CTL_TILED_YF;
70d21f0e 2992 default:
6156a456 2993 MISSING_CASE(fb_modifier);
70d21f0e 2994 }
8cfcba41 2995
c34ce3d1 2996 return 0;
6156a456 2997}
70d21f0e 2998
6156a456
CK
2999u32 skl_plane_ctl_rotation(unsigned int rotation)
3000{
3b7a5119 3001 switch (rotation) {
6156a456
CK
3002 case BIT(DRM_ROTATE_0):
3003 break;
1e8df167
SJ
3004 /*
3005 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3006 * while i915 HW rotation is clockwise, thats why this swapping.
3007 */
3b7a5119 3008 case BIT(DRM_ROTATE_90):
1e8df167 3009 return PLANE_CTL_ROTATE_270;
3b7a5119 3010 case BIT(DRM_ROTATE_180):
c34ce3d1 3011 return PLANE_CTL_ROTATE_180;
3b7a5119 3012 case BIT(DRM_ROTATE_270):
1e8df167 3013 return PLANE_CTL_ROTATE_90;
6156a456
CK
3014 default:
3015 MISSING_CASE(rotation);
3016 }
3017
c34ce3d1 3018 return 0;
6156a456
CK
3019}
3020
3021static void skylake_update_primary_plane(struct drm_crtc *crtc,
3022 struct drm_framebuffer *fb,
3023 int x, int y)
3024{
3025 struct drm_device *dev = crtc->dev;
3026 struct drm_i915_private *dev_priv = dev->dev_private;
3027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
3028 struct drm_plane *plane = crtc->primary;
3029 bool visible = to_intel_plane_state(plane->state)->visible;
6156a456
CK
3030 struct drm_i915_gem_object *obj;
3031 int pipe = intel_crtc->pipe;
3032 u32 plane_ctl, stride_div, stride;
3033 u32 tile_height, plane_offset, plane_size;
3034 unsigned int rotation;
3035 int x_offset, y_offset;
3036 unsigned long surf_addr;
6156a456
CK
3037 struct intel_crtc_state *crtc_state = intel_crtc->config;
3038 struct intel_plane_state *plane_state;
3039 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3040 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3041 int scaler_id = -1;
3042
6156a456
CK
3043 plane_state = to_intel_plane_state(plane->state);
3044
b70709a6 3045 if (!visible || !fb) {
6156a456
CK
3046 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3047 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3048 POSTING_READ(PLANE_CTL(pipe, 0));
3049 return;
3b7a5119 3050 }
70d21f0e 3051
6156a456
CK
3052 plane_ctl = PLANE_CTL_ENABLE |
3053 PLANE_CTL_PIPE_GAMMA_ENABLE |
3054 PLANE_CTL_PIPE_CSC_ENABLE;
3055
3056 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3057 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3058 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3059
3060 rotation = plane->state->rotation;
3061 plane_ctl |= skl_plane_ctl_rotation(rotation);
3062
b321803d
DL
3063 obj = intel_fb_obj(fb);
3064 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3065 fb->pixel_format);
3b7a5119
SJ
3066 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3067
6156a456
CK
3068 /*
3069 * FIXME: intel_plane_state->src, dst aren't set when transitional
3070 * update_plane helpers are called from legacy paths.
3071 * Once full atomic crtc is available, below check can be avoided.
3072 */
3073 if (drm_rect_width(&plane_state->src)) {
3074 scaler_id = plane_state->scaler_id;
3075 src_x = plane_state->src.x1 >> 16;
3076 src_y = plane_state->src.y1 >> 16;
3077 src_w = drm_rect_width(&plane_state->src) >> 16;
3078 src_h = drm_rect_height(&plane_state->src) >> 16;
3079 dst_x = plane_state->dst.x1;
3080 dst_y = plane_state->dst.y1;
3081 dst_w = drm_rect_width(&plane_state->dst);
3082 dst_h = drm_rect_height(&plane_state->dst);
3083
3084 WARN_ON(x != src_x || y != src_y);
3085 } else {
3086 src_w = intel_crtc->config->pipe_src_w;
3087 src_h = intel_crtc->config->pipe_src_h;
3088 }
3089
3b7a5119
SJ
3090 if (intel_rotation_90_or_270(rotation)) {
3091 /* stride = Surface height in tiles */
2614f17d 3092 tile_height = intel_tile_height(dev, fb->pixel_format,
3b7a5119
SJ
3093 fb->modifier[0]);
3094 stride = DIV_ROUND_UP(fb->height, tile_height);
6156a456 3095 x_offset = stride * tile_height - y - src_h;
3b7a5119 3096 y_offset = x;
6156a456 3097 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3098 } else {
3099 stride = fb->pitches[0] / stride_div;
3100 x_offset = x;
3101 y_offset = y;
6156a456 3102 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3103 }
3104 plane_offset = y_offset << 16 | x_offset;
b321803d 3105
70d21f0e 3106 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3107 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3108 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3109 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3110
3111 if (scaler_id >= 0) {
3112 uint32_t ps_ctrl = 0;
3113
3114 WARN_ON(!dst_w || !dst_h);
3115 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3116 crtc_state->scaler_state.scalers[scaler_id].mode;
3117 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3118 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3119 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3120 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3121 I915_WRITE(PLANE_POS(pipe, 0), 0);
3122 } else {
3123 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3124 }
3125
121920fa 3126 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3127
3128 POSTING_READ(PLANE_SURF(pipe, 0));
3129}
3130
17638cd6
JB
3131/* Assume fb object is pinned & idle & fenced and just update base pointers */
3132static int
3133intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3134 int x, int y, enum mode_set_atomic state)
3135{
3136 struct drm_device *dev = crtc->dev;
3137 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 3138
6b8e6ed0
CW
3139 if (dev_priv->display.disable_fbc)
3140 dev_priv->display.disable_fbc(dev);
81255565 3141
29b9bde6
DV
3142 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3143
3144 return 0;
81255565
JB
3145}
3146
7514747d 3147static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3148{
96a02917
VS
3149 struct drm_crtc *crtc;
3150
70e1e0ec 3151 for_each_crtc(dev, crtc) {
96a02917
VS
3152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3153 enum plane plane = intel_crtc->plane;
3154
3155 intel_prepare_page_flip(dev, plane);
3156 intel_finish_page_flip_plane(dev, plane);
3157 }
7514747d
VS
3158}
3159
3160static void intel_update_primary_planes(struct drm_device *dev)
3161{
3162 struct drm_i915_private *dev_priv = dev->dev_private;
3163 struct drm_crtc *crtc;
96a02917 3164
70e1e0ec 3165 for_each_crtc(dev, crtc) {
96a02917
VS
3166 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3167
51fd371b 3168 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
3169 /*
3170 * FIXME: Once we have proper support for primary planes (and
3171 * disabling them without disabling the entire crtc) allow again
66e514c1 3172 * a NULL crtc->primary->fb.
947fdaad 3173 */
f4510a27 3174 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 3175 dev_priv->display.update_primary_plane(crtc,
66e514c1 3176 crtc->primary->fb,
262ca2b0
MR
3177 crtc->x,
3178 crtc->y);
51fd371b 3179 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
3180 }
3181}
3182
7514747d
VS
3183void intel_prepare_reset(struct drm_device *dev)
3184{
3185 /* no reset support for gen2 */
3186 if (IS_GEN2(dev))
3187 return;
3188
3189 /* reset doesn't touch the display */
3190 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3191 return;
3192
3193 drm_modeset_lock_all(dev);
f98ce92f
VS
3194 /*
3195 * Disabling the crtcs gracefully seems nicer. Also the
3196 * g33 docs say we should at least disable all the planes.
3197 */
6b72d486 3198 intel_display_suspend(dev);
7514747d
VS
3199}
3200
3201void intel_finish_reset(struct drm_device *dev)
3202{
3203 struct drm_i915_private *dev_priv = to_i915(dev);
3204
3205 /*
3206 * Flips in the rings will be nuked by the reset,
3207 * so complete all pending flips so that user space
3208 * will get its events and not get stuck.
3209 */
3210 intel_complete_page_flips(dev);
3211
3212 /* no reset support for gen2 */
3213 if (IS_GEN2(dev))
3214 return;
3215
3216 /* reset doesn't touch the display */
3217 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3218 /*
3219 * Flips in the rings have been nuked by the reset,
3220 * so update the base address of all primary
3221 * planes to the the last fb to make sure we're
3222 * showing the correct fb after a reset.
3223 */
3224 intel_update_primary_planes(dev);
3225 return;
3226 }
3227
3228 /*
3229 * The display has been reset as well,
3230 * so need a full re-initialization.
3231 */
3232 intel_runtime_pm_disable_interrupts(dev_priv);
3233 intel_runtime_pm_enable_interrupts(dev_priv);
3234
3235 intel_modeset_init_hw(dev);
3236
3237 spin_lock_irq(&dev_priv->irq_lock);
3238 if (dev_priv->display.hpd_irq_setup)
3239 dev_priv->display.hpd_irq_setup(dev);
3240 spin_unlock_irq(&dev_priv->irq_lock);
3241
3242 intel_modeset_setup_hw_state(dev, true);
3243
3244 intel_hpd_init(dev_priv);
3245
3246 drm_modeset_unlock_all(dev);
3247}
3248
2e2f351d 3249static void
14667a4b
CW
3250intel_finish_fb(struct drm_framebuffer *old_fb)
3251{
2ff8fde1 3252 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
2e2f351d 3253 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
14667a4b
CW
3254 bool was_interruptible = dev_priv->mm.interruptible;
3255 int ret;
3256
14667a4b
CW
3257 /* Big Hammer, we also need to ensure that any pending
3258 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3259 * current scanout is retired before unpinning the old
2e2f351d
CW
3260 * framebuffer. Note that we rely on userspace rendering
3261 * into the buffer attached to the pipe they are waiting
3262 * on. If not, userspace generates a GPU hang with IPEHR
3263 * point to the MI_WAIT_FOR_EVENT.
14667a4b
CW
3264 *
3265 * This should only fail upon a hung GPU, in which case we
3266 * can safely continue.
3267 */
3268 dev_priv->mm.interruptible = false;
2e2f351d 3269 ret = i915_gem_object_wait_rendering(obj, true);
14667a4b
CW
3270 dev_priv->mm.interruptible = was_interruptible;
3271
2e2f351d 3272 WARN_ON(ret);
14667a4b
CW
3273}
3274
7d5e3799
CW
3275static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3276{
3277 struct drm_device *dev = crtc->dev;
3278 struct drm_i915_private *dev_priv = dev->dev_private;
3279 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3280 bool pending;
3281
3282 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3283 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3284 return false;
3285
5e2d7afc 3286 spin_lock_irq(&dev->event_lock);
7d5e3799 3287 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3288 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3289
3290 return pending;
3291}
3292
e30e8f75
GP
3293static void intel_update_pipe_size(struct intel_crtc *crtc)
3294{
3295 struct drm_device *dev = crtc->base.dev;
3296 struct drm_i915_private *dev_priv = dev->dev_private;
3297 const struct drm_display_mode *adjusted_mode;
3298
3299 if (!i915.fastboot)
3300 return;
3301
3302 /*
3303 * Update pipe size and adjust fitter if needed: the reason for this is
3304 * that in compute_mode_changes we check the native mode (not the pfit
3305 * mode) to see if we can flip rather than do a full mode set. In the
3306 * fastboot case, we'll flip, but if we don't update the pipesrc and
3307 * pfit state, we'll end up with a big fb scanned out into the wrong
3308 * sized surface.
3309 *
3310 * To fix this properly, we need to hoist the checks up into
3311 * compute_mode_changes (or above), check the actual pfit state and
3312 * whether the platform allows pfit disable with pipe active, and only
3313 * then update the pipesrc and pfit state, even on the flip path.
3314 */
3315
6e3c9717 3316 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
3317
3318 I915_WRITE(PIPESRC(crtc->pipe),
3319 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3320 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 3321 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
3322 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3323 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3324 I915_WRITE(PF_CTL(crtc->pipe), 0);
3325 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3326 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3327 }
6e3c9717
ACO
3328 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3329 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3330}
3331
5e84e1a4
ZW
3332static void intel_fdi_normal_train(struct drm_crtc *crtc)
3333{
3334 struct drm_device *dev = crtc->dev;
3335 struct drm_i915_private *dev_priv = dev->dev_private;
3336 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3337 int pipe = intel_crtc->pipe;
3338 u32 reg, temp;
3339
3340 /* enable normal train */
3341 reg = FDI_TX_CTL(pipe);
3342 temp = I915_READ(reg);
61e499bf 3343 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3344 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3345 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3346 } else {
3347 temp &= ~FDI_LINK_TRAIN_NONE;
3348 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3349 }
5e84e1a4
ZW
3350 I915_WRITE(reg, temp);
3351
3352 reg = FDI_RX_CTL(pipe);
3353 temp = I915_READ(reg);
3354 if (HAS_PCH_CPT(dev)) {
3355 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3356 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3357 } else {
3358 temp &= ~FDI_LINK_TRAIN_NONE;
3359 temp |= FDI_LINK_TRAIN_NONE;
3360 }
3361 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3362
3363 /* wait one idle pattern time */
3364 POSTING_READ(reg);
3365 udelay(1000);
357555c0
JB
3366
3367 /* IVB wants error correction enabled */
3368 if (IS_IVYBRIDGE(dev))
3369 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3370 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3371}
3372
8db9d77b
ZW
3373/* The FDI link training functions for ILK/Ibexpeak. */
3374static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3375{
3376 struct drm_device *dev = crtc->dev;
3377 struct drm_i915_private *dev_priv = dev->dev_private;
3378 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3379 int pipe = intel_crtc->pipe;
5eddb70b 3380 u32 reg, temp, tries;
8db9d77b 3381
1c8562f6 3382 /* FDI needs bits from pipe first */
0fc932b8 3383 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3384
e1a44743
AJ
3385 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3386 for train result */
5eddb70b
CW
3387 reg = FDI_RX_IMR(pipe);
3388 temp = I915_READ(reg);
e1a44743
AJ
3389 temp &= ~FDI_RX_SYMBOL_LOCK;
3390 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3391 I915_WRITE(reg, temp);
3392 I915_READ(reg);
e1a44743
AJ
3393 udelay(150);
3394
8db9d77b 3395 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3396 reg = FDI_TX_CTL(pipe);
3397 temp = I915_READ(reg);
627eb5a3 3398 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3399 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3400 temp &= ~FDI_LINK_TRAIN_NONE;
3401 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3402 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3403
5eddb70b
CW
3404 reg = FDI_RX_CTL(pipe);
3405 temp = I915_READ(reg);
8db9d77b
ZW
3406 temp &= ~FDI_LINK_TRAIN_NONE;
3407 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3408 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3409
3410 POSTING_READ(reg);
8db9d77b
ZW
3411 udelay(150);
3412
5b2adf89 3413 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3414 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3415 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3416 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3417
5eddb70b 3418 reg = FDI_RX_IIR(pipe);
e1a44743 3419 for (tries = 0; tries < 5; tries++) {
5eddb70b 3420 temp = I915_READ(reg);
8db9d77b
ZW
3421 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3422
3423 if ((temp & FDI_RX_BIT_LOCK)) {
3424 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3425 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3426 break;
3427 }
8db9d77b 3428 }
e1a44743 3429 if (tries == 5)
5eddb70b 3430 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3431
3432 /* Train 2 */
5eddb70b
CW
3433 reg = FDI_TX_CTL(pipe);
3434 temp = I915_READ(reg);
8db9d77b
ZW
3435 temp &= ~FDI_LINK_TRAIN_NONE;
3436 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3437 I915_WRITE(reg, temp);
8db9d77b 3438
5eddb70b
CW
3439 reg = FDI_RX_CTL(pipe);
3440 temp = I915_READ(reg);
8db9d77b
ZW
3441 temp &= ~FDI_LINK_TRAIN_NONE;
3442 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3443 I915_WRITE(reg, temp);
8db9d77b 3444
5eddb70b
CW
3445 POSTING_READ(reg);
3446 udelay(150);
8db9d77b 3447
5eddb70b 3448 reg = FDI_RX_IIR(pipe);
e1a44743 3449 for (tries = 0; tries < 5; tries++) {
5eddb70b 3450 temp = I915_READ(reg);
8db9d77b
ZW
3451 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3452
3453 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3454 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3455 DRM_DEBUG_KMS("FDI train 2 done.\n");
3456 break;
3457 }
8db9d77b 3458 }
e1a44743 3459 if (tries == 5)
5eddb70b 3460 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3461
3462 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3463
8db9d77b
ZW
3464}
3465
0206e353 3466static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3467 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3468 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3469 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3470 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3471};
3472
3473/* The FDI link training functions for SNB/Cougarpoint. */
3474static void gen6_fdi_link_train(struct drm_crtc *crtc)
3475{
3476 struct drm_device *dev = crtc->dev;
3477 struct drm_i915_private *dev_priv = dev->dev_private;
3478 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3479 int pipe = intel_crtc->pipe;
fa37d39e 3480 u32 reg, temp, i, retry;
8db9d77b 3481
e1a44743
AJ
3482 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3483 for train result */
5eddb70b
CW
3484 reg = FDI_RX_IMR(pipe);
3485 temp = I915_READ(reg);
e1a44743
AJ
3486 temp &= ~FDI_RX_SYMBOL_LOCK;
3487 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3488 I915_WRITE(reg, temp);
3489
3490 POSTING_READ(reg);
e1a44743
AJ
3491 udelay(150);
3492
8db9d77b 3493 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3494 reg = FDI_TX_CTL(pipe);
3495 temp = I915_READ(reg);
627eb5a3 3496 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3497 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3498 temp &= ~FDI_LINK_TRAIN_NONE;
3499 temp |= FDI_LINK_TRAIN_PATTERN_1;
3500 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3501 /* SNB-B */
3502 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3503 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3504
d74cf324
DV
3505 I915_WRITE(FDI_RX_MISC(pipe),
3506 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3507
5eddb70b
CW
3508 reg = FDI_RX_CTL(pipe);
3509 temp = I915_READ(reg);
8db9d77b
ZW
3510 if (HAS_PCH_CPT(dev)) {
3511 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3512 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3513 } else {
3514 temp &= ~FDI_LINK_TRAIN_NONE;
3515 temp |= FDI_LINK_TRAIN_PATTERN_1;
3516 }
5eddb70b
CW
3517 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3518
3519 POSTING_READ(reg);
8db9d77b
ZW
3520 udelay(150);
3521
0206e353 3522 for (i = 0; i < 4; i++) {
5eddb70b
CW
3523 reg = FDI_TX_CTL(pipe);
3524 temp = I915_READ(reg);
8db9d77b
ZW
3525 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3526 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3527 I915_WRITE(reg, temp);
3528
3529 POSTING_READ(reg);
8db9d77b
ZW
3530 udelay(500);
3531
fa37d39e
SP
3532 for (retry = 0; retry < 5; retry++) {
3533 reg = FDI_RX_IIR(pipe);
3534 temp = I915_READ(reg);
3535 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3536 if (temp & FDI_RX_BIT_LOCK) {
3537 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3538 DRM_DEBUG_KMS("FDI train 1 done.\n");
3539 break;
3540 }
3541 udelay(50);
8db9d77b 3542 }
fa37d39e
SP
3543 if (retry < 5)
3544 break;
8db9d77b
ZW
3545 }
3546 if (i == 4)
5eddb70b 3547 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3548
3549 /* Train 2 */
5eddb70b
CW
3550 reg = FDI_TX_CTL(pipe);
3551 temp = I915_READ(reg);
8db9d77b
ZW
3552 temp &= ~FDI_LINK_TRAIN_NONE;
3553 temp |= FDI_LINK_TRAIN_PATTERN_2;
3554 if (IS_GEN6(dev)) {
3555 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3556 /* SNB-B */
3557 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3558 }
5eddb70b 3559 I915_WRITE(reg, temp);
8db9d77b 3560
5eddb70b
CW
3561 reg = FDI_RX_CTL(pipe);
3562 temp = I915_READ(reg);
8db9d77b
ZW
3563 if (HAS_PCH_CPT(dev)) {
3564 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3565 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3566 } else {
3567 temp &= ~FDI_LINK_TRAIN_NONE;
3568 temp |= FDI_LINK_TRAIN_PATTERN_2;
3569 }
5eddb70b
CW
3570 I915_WRITE(reg, temp);
3571
3572 POSTING_READ(reg);
8db9d77b
ZW
3573 udelay(150);
3574
0206e353 3575 for (i = 0; i < 4; i++) {
5eddb70b
CW
3576 reg = FDI_TX_CTL(pipe);
3577 temp = I915_READ(reg);
8db9d77b
ZW
3578 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3579 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3580 I915_WRITE(reg, temp);
3581
3582 POSTING_READ(reg);
8db9d77b
ZW
3583 udelay(500);
3584
fa37d39e
SP
3585 for (retry = 0; retry < 5; retry++) {
3586 reg = FDI_RX_IIR(pipe);
3587 temp = I915_READ(reg);
3588 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3589 if (temp & FDI_RX_SYMBOL_LOCK) {
3590 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3591 DRM_DEBUG_KMS("FDI train 2 done.\n");
3592 break;
3593 }
3594 udelay(50);
8db9d77b 3595 }
fa37d39e
SP
3596 if (retry < 5)
3597 break;
8db9d77b
ZW
3598 }
3599 if (i == 4)
5eddb70b 3600 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3601
3602 DRM_DEBUG_KMS("FDI train done.\n");
3603}
3604
357555c0
JB
3605/* Manual link training for Ivy Bridge A0 parts */
3606static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3607{
3608 struct drm_device *dev = crtc->dev;
3609 struct drm_i915_private *dev_priv = dev->dev_private;
3610 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3611 int pipe = intel_crtc->pipe;
139ccd3f 3612 u32 reg, temp, i, j;
357555c0
JB
3613
3614 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3615 for train result */
3616 reg = FDI_RX_IMR(pipe);
3617 temp = I915_READ(reg);
3618 temp &= ~FDI_RX_SYMBOL_LOCK;
3619 temp &= ~FDI_RX_BIT_LOCK;
3620 I915_WRITE(reg, temp);
3621
3622 POSTING_READ(reg);
3623 udelay(150);
3624
01a415fd
DV
3625 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3626 I915_READ(FDI_RX_IIR(pipe)));
3627
139ccd3f
JB
3628 /* Try each vswing and preemphasis setting twice before moving on */
3629 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3630 /* disable first in case we need to retry */
3631 reg = FDI_TX_CTL(pipe);
3632 temp = I915_READ(reg);
3633 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3634 temp &= ~FDI_TX_ENABLE;
3635 I915_WRITE(reg, temp);
357555c0 3636
139ccd3f
JB
3637 reg = FDI_RX_CTL(pipe);
3638 temp = I915_READ(reg);
3639 temp &= ~FDI_LINK_TRAIN_AUTO;
3640 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3641 temp &= ~FDI_RX_ENABLE;
3642 I915_WRITE(reg, temp);
357555c0 3643
139ccd3f 3644 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3645 reg = FDI_TX_CTL(pipe);
3646 temp = I915_READ(reg);
139ccd3f 3647 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3648 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3649 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3650 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3651 temp |= snb_b_fdi_train_param[j/2];
3652 temp |= FDI_COMPOSITE_SYNC;
3653 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3654
139ccd3f
JB
3655 I915_WRITE(FDI_RX_MISC(pipe),
3656 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3657
139ccd3f 3658 reg = FDI_RX_CTL(pipe);
357555c0 3659 temp = I915_READ(reg);
139ccd3f
JB
3660 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3661 temp |= FDI_COMPOSITE_SYNC;
3662 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3663
139ccd3f
JB
3664 POSTING_READ(reg);
3665 udelay(1); /* should be 0.5us */
357555c0 3666
139ccd3f
JB
3667 for (i = 0; i < 4; i++) {
3668 reg = FDI_RX_IIR(pipe);
3669 temp = I915_READ(reg);
3670 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3671
139ccd3f
JB
3672 if (temp & FDI_RX_BIT_LOCK ||
3673 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3674 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3675 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3676 i);
3677 break;
3678 }
3679 udelay(1); /* should be 0.5us */
3680 }
3681 if (i == 4) {
3682 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3683 continue;
3684 }
357555c0 3685
139ccd3f 3686 /* Train 2 */
357555c0
JB
3687 reg = FDI_TX_CTL(pipe);
3688 temp = I915_READ(reg);
139ccd3f
JB
3689 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3690 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3691 I915_WRITE(reg, temp);
3692
3693 reg = FDI_RX_CTL(pipe);
3694 temp = I915_READ(reg);
3695 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3696 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3697 I915_WRITE(reg, temp);
3698
3699 POSTING_READ(reg);
139ccd3f 3700 udelay(2); /* should be 1.5us */
357555c0 3701
139ccd3f
JB
3702 for (i = 0; i < 4; i++) {
3703 reg = FDI_RX_IIR(pipe);
3704 temp = I915_READ(reg);
3705 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3706
139ccd3f
JB
3707 if (temp & FDI_RX_SYMBOL_LOCK ||
3708 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3709 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3710 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3711 i);
3712 goto train_done;
3713 }
3714 udelay(2); /* should be 1.5us */
357555c0 3715 }
139ccd3f
JB
3716 if (i == 4)
3717 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3718 }
357555c0 3719
139ccd3f 3720train_done:
357555c0
JB
3721 DRM_DEBUG_KMS("FDI train done.\n");
3722}
3723
88cefb6c 3724static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3725{
88cefb6c 3726 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3727 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3728 int pipe = intel_crtc->pipe;
5eddb70b 3729 u32 reg, temp;
79e53945 3730
c64e311e 3731
c98e9dcf 3732 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3733 reg = FDI_RX_CTL(pipe);
3734 temp = I915_READ(reg);
627eb5a3 3735 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3736 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3737 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3738 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3739
3740 POSTING_READ(reg);
c98e9dcf
JB
3741 udelay(200);
3742
3743 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3744 temp = I915_READ(reg);
3745 I915_WRITE(reg, temp | FDI_PCDCLK);
3746
3747 POSTING_READ(reg);
c98e9dcf
JB
3748 udelay(200);
3749
20749730
PZ
3750 /* Enable CPU FDI TX PLL, always on for Ironlake */
3751 reg = FDI_TX_CTL(pipe);
3752 temp = I915_READ(reg);
3753 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3754 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3755
20749730
PZ
3756 POSTING_READ(reg);
3757 udelay(100);
6be4a607 3758 }
0e23b99d
JB
3759}
3760
88cefb6c
DV
3761static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3762{
3763 struct drm_device *dev = intel_crtc->base.dev;
3764 struct drm_i915_private *dev_priv = dev->dev_private;
3765 int pipe = intel_crtc->pipe;
3766 u32 reg, temp;
3767
3768 /* Switch from PCDclk to Rawclk */
3769 reg = FDI_RX_CTL(pipe);
3770 temp = I915_READ(reg);
3771 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3772
3773 /* Disable CPU FDI TX PLL */
3774 reg = FDI_TX_CTL(pipe);
3775 temp = I915_READ(reg);
3776 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3777
3778 POSTING_READ(reg);
3779 udelay(100);
3780
3781 reg = FDI_RX_CTL(pipe);
3782 temp = I915_READ(reg);
3783 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3784
3785 /* Wait for the clocks to turn off. */
3786 POSTING_READ(reg);
3787 udelay(100);
3788}
3789
0fc932b8
JB
3790static void ironlake_fdi_disable(struct drm_crtc *crtc)
3791{
3792 struct drm_device *dev = crtc->dev;
3793 struct drm_i915_private *dev_priv = dev->dev_private;
3794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3795 int pipe = intel_crtc->pipe;
3796 u32 reg, temp;
3797
3798 /* disable CPU FDI tx and PCH FDI rx */
3799 reg = FDI_TX_CTL(pipe);
3800 temp = I915_READ(reg);
3801 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3802 POSTING_READ(reg);
3803
3804 reg = FDI_RX_CTL(pipe);
3805 temp = I915_READ(reg);
3806 temp &= ~(0x7 << 16);
dfd07d72 3807 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3808 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3809
3810 POSTING_READ(reg);
3811 udelay(100);
3812
3813 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3814 if (HAS_PCH_IBX(dev))
6f06ce18 3815 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3816
3817 /* still set train pattern 1 */
3818 reg = FDI_TX_CTL(pipe);
3819 temp = I915_READ(reg);
3820 temp &= ~FDI_LINK_TRAIN_NONE;
3821 temp |= FDI_LINK_TRAIN_PATTERN_1;
3822 I915_WRITE(reg, temp);
3823
3824 reg = FDI_RX_CTL(pipe);
3825 temp = I915_READ(reg);
3826 if (HAS_PCH_CPT(dev)) {
3827 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3828 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3829 } else {
3830 temp &= ~FDI_LINK_TRAIN_NONE;
3831 temp |= FDI_LINK_TRAIN_PATTERN_1;
3832 }
3833 /* BPC in FDI rx is consistent with that in PIPECONF */
3834 temp &= ~(0x07 << 16);
dfd07d72 3835 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3836 I915_WRITE(reg, temp);
3837
3838 POSTING_READ(reg);
3839 udelay(100);
3840}
3841
5dce5b93
CW
3842bool intel_has_pending_fb_unpin(struct drm_device *dev)
3843{
3844 struct intel_crtc *crtc;
3845
3846 /* Note that we don't need to be called with mode_config.lock here
3847 * as our list of CRTC objects is static for the lifetime of the
3848 * device and so cannot disappear as we iterate. Similarly, we can
3849 * happily treat the predicates as racy, atomic checks as userspace
3850 * cannot claim and pin a new fb without at least acquring the
3851 * struct_mutex and so serialising with us.
3852 */
d3fcc808 3853 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3854 if (atomic_read(&crtc->unpin_work_count) == 0)
3855 continue;
3856
3857 if (crtc->unpin_work)
3858 intel_wait_for_vblank(dev, crtc->pipe);
3859
3860 return true;
3861 }
3862
3863 return false;
3864}
3865
d6bbafa1
CW
3866static void page_flip_completed(struct intel_crtc *intel_crtc)
3867{
3868 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3869 struct intel_unpin_work *work = intel_crtc->unpin_work;
3870
3871 /* ensure that the unpin work is consistent wrt ->pending. */
3872 smp_rmb();
3873 intel_crtc->unpin_work = NULL;
3874
3875 if (work->event)
3876 drm_send_vblank_event(intel_crtc->base.dev,
3877 intel_crtc->pipe,
3878 work->event);
3879
3880 drm_crtc_vblank_put(&intel_crtc->base);
3881
3882 wake_up_all(&dev_priv->pending_flip_queue);
3883 queue_work(dev_priv->wq, &work->work);
3884
3885 trace_i915_flip_complete(intel_crtc->plane,
3886 work->pending_flip_obj);
3887}
3888
46a55d30 3889void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3890{
0f91128d 3891 struct drm_device *dev = crtc->dev;
5bb61643 3892 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3893
2c10d571 3894 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3895 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3896 !intel_crtc_has_pending_flip(crtc),
3897 60*HZ) == 0)) {
3898 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3899
5e2d7afc 3900 spin_lock_irq(&dev->event_lock);
9c787942
CW
3901 if (intel_crtc->unpin_work) {
3902 WARN_ONCE(1, "Removing stuck page flip\n");
3903 page_flip_completed(intel_crtc);
3904 }
5e2d7afc 3905 spin_unlock_irq(&dev->event_lock);
9c787942 3906 }
5bb61643 3907
975d568a
CW
3908 if (crtc->primary->fb) {
3909 mutex_lock(&dev->struct_mutex);
3910 intel_finish_fb(crtc->primary->fb);
3911 mutex_unlock(&dev->struct_mutex);
3912 }
e6c3a2a6
CW
3913}
3914
e615efe4
ED
3915/* Program iCLKIP clock to the desired frequency */
3916static void lpt_program_iclkip(struct drm_crtc *crtc)
3917{
3918 struct drm_device *dev = crtc->dev;
3919 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3920 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3921 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3922 u32 temp;
3923
a580516d 3924 mutex_lock(&dev_priv->sb_lock);
09153000 3925
e615efe4
ED
3926 /* It is necessary to ungate the pixclk gate prior to programming
3927 * the divisors, and gate it back when it is done.
3928 */
3929 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3930
3931 /* Disable SSCCTL */
3932 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3933 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3934 SBI_SSCCTL_DISABLE,
3935 SBI_ICLK);
e615efe4
ED
3936
3937 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3938 if (clock == 20000) {
e615efe4
ED
3939 auxdiv = 1;
3940 divsel = 0x41;
3941 phaseinc = 0x20;
3942 } else {
3943 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3944 * but the adjusted_mode->crtc_clock in in KHz. To get the
3945 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3946 * convert the virtual clock precision to KHz here for higher
3947 * precision.
3948 */
3949 u32 iclk_virtual_root_freq = 172800 * 1000;
3950 u32 iclk_pi_range = 64;
3951 u32 desired_divisor, msb_divisor_value, pi_value;
3952
12d7ceed 3953 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3954 msb_divisor_value = desired_divisor / iclk_pi_range;
3955 pi_value = desired_divisor % iclk_pi_range;
3956
3957 auxdiv = 0;
3958 divsel = msb_divisor_value - 2;
3959 phaseinc = pi_value;
3960 }
3961
3962 /* This should not happen with any sane values */
3963 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3964 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3965 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3966 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3967
3968 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3969 clock,
e615efe4
ED
3970 auxdiv,
3971 divsel,
3972 phasedir,
3973 phaseinc);
3974
3975 /* Program SSCDIVINTPHASE6 */
988d6ee8 3976 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3977 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3978 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3979 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3980 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3981 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3982 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3983 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3984
3985 /* Program SSCAUXDIV */
988d6ee8 3986 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3987 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3988 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3989 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3990
3991 /* Enable modulator and associated divider */
988d6ee8 3992 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3993 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3994 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3995
3996 /* Wait for initialization time */
3997 udelay(24);
3998
3999 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000 4000
a580516d 4001 mutex_unlock(&dev_priv->sb_lock);
e615efe4
ED
4002}
4003
275f01b2
DV
4004static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4005 enum pipe pch_transcoder)
4006{
4007 struct drm_device *dev = crtc->base.dev;
4008 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4009 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4010
4011 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4012 I915_READ(HTOTAL(cpu_transcoder)));
4013 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4014 I915_READ(HBLANK(cpu_transcoder)));
4015 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4016 I915_READ(HSYNC(cpu_transcoder)));
4017
4018 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4019 I915_READ(VTOTAL(cpu_transcoder)));
4020 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4021 I915_READ(VBLANK(cpu_transcoder)));
4022 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4023 I915_READ(VSYNC(cpu_transcoder)));
4024 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4025 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4026}
4027
003632d9 4028static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4029{
4030 struct drm_i915_private *dev_priv = dev->dev_private;
4031 uint32_t temp;
4032
4033 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4034 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4035 return;
4036
4037 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4038 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4039
003632d9
ACO
4040 temp &= ~FDI_BC_BIFURCATION_SELECT;
4041 if (enable)
4042 temp |= FDI_BC_BIFURCATION_SELECT;
4043
4044 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4045 I915_WRITE(SOUTH_CHICKEN1, temp);
4046 POSTING_READ(SOUTH_CHICKEN1);
4047}
4048
4049static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4050{
4051 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4052
4053 switch (intel_crtc->pipe) {
4054 case PIPE_A:
4055 break;
4056 case PIPE_B:
6e3c9717 4057 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4058 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4059 else
003632d9 4060 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4061
4062 break;
4063 case PIPE_C:
003632d9 4064 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4065
4066 break;
4067 default:
4068 BUG();
4069 }
4070}
4071
f67a559d
JB
4072/*
4073 * Enable PCH resources required for PCH ports:
4074 * - PCH PLLs
4075 * - FDI training & RX/TX
4076 * - update transcoder timings
4077 * - DP transcoding bits
4078 * - transcoder
4079 */
4080static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4081{
4082 struct drm_device *dev = crtc->dev;
4083 struct drm_i915_private *dev_priv = dev->dev_private;
4084 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4085 int pipe = intel_crtc->pipe;
ee7b9f93 4086 u32 reg, temp;
2c07245f 4087
ab9412ba 4088 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4089
1fbc0d78
DV
4090 if (IS_IVYBRIDGE(dev))
4091 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4092
cd986abb
DV
4093 /* Write the TU size bits before fdi link training, so that error
4094 * detection works. */
4095 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4096 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4097
c98e9dcf 4098 /* For PCH output, training FDI link */
674cf967 4099 dev_priv->display.fdi_link_train(crtc);
2c07245f 4100
3ad8a208
DV
4101 /* We need to program the right clock selection before writing the pixel
4102 * mutliplier into the DPLL. */
303b81e0 4103 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4104 u32 sel;
4b645f14 4105
c98e9dcf 4106 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4107 temp |= TRANS_DPLL_ENABLE(pipe);
4108 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 4109 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
4110 temp |= sel;
4111 else
4112 temp &= ~sel;
c98e9dcf 4113 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4114 }
5eddb70b 4115
3ad8a208
DV
4116 /* XXX: pch pll's can be enabled any time before we enable the PCH
4117 * transcoder, and we actually should do this to not upset any PCH
4118 * transcoder that already use the clock when we share it.
4119 *
4120 * Note that enable_shared_dpll tries to do the right thing, but
4121 * get_shared_dpll unconditionally resets the pll - we need that to have
4122 * the right LVDS enable sequence. */
85b3894f 4123 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4124
d9b6cb56
JB
4125 /* set transcoder timing, panel must allow it */
4126 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4127 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4128
303b81e0 4129 intel_fdi_normal_train(crtc);
5e84e1a4 4130
c98e9dcf 4131 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4132 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 4133 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
4134 reg = TRANS_DP_CTL(pipe);
4135 temp = I915_READ(reg);
4136 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4137 TRANS_DP_SYNC_MASK |
4138 TRANS_DP_BPC_MASK);
e3ef4479 4139 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4140 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
4141
4142 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4143 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 4144 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4145 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4146
4147 switch (intel_trans_dp_port_sel(crtc)) {
4148 case PCH_DP_B:
5eddb70b 4149 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
4150 break;
4151 case PCH_DP_C:
5eddb70b 4152 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
4153 break;
4154 case PCH_DP_D:
5eddb70b 4155 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4156 break;
4157 default:
e95d41e1 4158 BUG();
32f9d658 4159 }
2c07245f 4160
5eddb70b 4161 I915_WRITE(reg, temp);
6be4a607 4162 }
b52eb4dc 4163
b8a4f404 4164 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4165}
4166
1507e5bd
PZ
4167static void lpt_pch_enable(struct drm_crtc *crtc)
4168{
4169 struct drm_device *dev = crtc->dev;
4170 struct drm_i915_private *dev_priv = dev->dev_private;
4171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4172 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4173
ab9412ba 4174 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4175
8c52b5e8 4176 lpt_program_iclkip(crtc);
1507e5bd 4177
0540e488 4178 /* Set transcoder timing. */
275f01b2 4179 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4180
937bb610 4181 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4182}
4183
190f68c5
ACO
4184struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4185 struct intel_crtc_state *crtc_state)
ee7b9f93 4186{
e2b78267 4187 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 4188 struct intel_shared_dpll *pll;
de419ab6 4189 struct intel_shared_dpll_config *shared_dpll;
e2b78267 4190 enum intel_dpll_id i;
ee7b9f93 4191
de419ab6
ML
4192 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4193
98b6bd99
DV
4194 if (HAS_PCH_IBX(dev_priv->dev)) {
4195 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 4196 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 4197 pll = &dev_priv->shared_dplls[i];
98b6bd99 4198
46edb027
DV
4199 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4200 crtc->base.base.id, pll->name);
98b6bd99 4201
de419ab6 4202 WARN_ON(shared_dpll[i].crtc_mask);
f2a69f44 4203
98b6bd99
DV
4204 goto found;
4205 }
4206
bcddf610
S
4207 if (IS_BROXTON(dev_priv->dev)) {
4208 /* PLL is attached to port in bxt */
4209 struct intel_encoder *encoder;
4210 struct intel_digital_port *intel_dig_port;
4211
4212 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4213 if (WARN_ON(!encoder))
4214 return NULL;
4215
4216 intel_dig_port = enc_to_dig_port(&encoder->base);
4217 /* 1:1 mapping between ports and PLLs */
4218 i = (enum intel_dpll_id)intel_dig_port->port;
4219 pll = &dev_priv->shared_dplls[i];
4220 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4221 crtc->base.base.id, pll->name);
de419ab6 4222 WARN_ON(shared_dpll[i].crtc_mask);
bcddf610
S
4223
4224 goto found;
4225 }
4226
e72f9fbf
DV
4227 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4228 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
4229
4230 /* Only want to check enabled timings first */
de419ab6 4231 if (shared_dpll[i].crtc_mask == 0)
ee7b9f93
JB
4232 continue;
4233
190f68c5 4234 if (memcmp(&crtc_state->dpll_hw_state,
de419ab6
ML
4235 &shared_dpll[i].hw_state,
4236 sizeof(crtc_state->dpll_hw_state)) == 0) {
8bd31e67 4237 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 4238 crtc->base.base.id, pll->name,
de419ab6 4239 shared_dpll[i].crtc_mask,
8bd31e67 4240 pll->active);
ee7b9f93
JB
4241 goto found;
4242 }
4243 }
4244
4245 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
4246 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4247 pll = &dev_priv->shared_dplls[i];
de419ab6 4248 if (shared_dpll[i].crtc_mask == 0) {
46edb027
DV
4249 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4250 crtc->base.base.id, pll->name);
ee7b9f93
JB
4251 goto found;
4252 }
4253 }
4254
4255 return NULL;
4256
4257found:
de419ab6
ML
4258 if (shared_dpll[i].crtc_mask == 0)
4259 shared_dpll[i].hw_state =
4260 crtc_state->dpll_hw_state;
f2a69f44 4261
190f68c5 4262 crtc_state->shared_dpll = i;
46edb027
DV
4263 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4264 pipe_name(crtc->pipe));
ee7b9f93 4265
de419ab6 4266 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
e04c7350 4267
ee7b9f93
JB
4268 return pll;
4269}
4270
de419ab6 4271static void intel_shared_dpll_commit(struct drm_atomic_state *state)
8bd31e67 4272{
de419ab6
ML
4273 struct drm_i915_private *dev_priv = to_i915(state->dev);
4274 struct intel_shared_dpll_config *shared_dpll;
8bd31e67
ACO
4275 struct intel_shared_dpll *pll;
4276 enum intel_dpll_id i;
4277
de419ab6
ML
4278 if (!to_intel_atomic_state(state)->dpll_set)
4279 return;
8bd31e67 4280
de419ab6 4281 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
8bd31e67
ACO
4282 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4283 pll = &dev_priv->shared_dplls[i];
de419ab6 4284 pll->config = shared_dpll[i];
8bd31e67
ACO
4285 }
4286}
4287
a1520318 4288static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4289{
4290 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4291 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4292 u32 temp;
4293
4294 temp = I915_READ(dslreg);
4295 udelay(500);
4296 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4297 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4298 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4299 }
4300}
4301
a1b2278e
CK
4302/**
4303 * skl_update_scaler_users - Stages update to crtc's scaler state
4304 * @intel_crtc: crtc
4305 * @crtc_state: crtc_state
4306 * @plane: plane (NULL indicates crtc is requesting update)
4307 * @plane_state: plane's state
4308 * @force_detach: request unconditional detachment of scaler
4309 *
4310 * This function updates scaler state for requested plane or crtc.
4311 * To request scaler usage update for a plane, caller shall pass plane pointer.
4312 * To request scaler usage update for crtc, caller shall pass plane pointer
4313 * as NULL.
4314 *
4315 * Return
4316 * 0 - scaler_usage updated successfully
4317 * error - requested scaling cannot be supported or other error condition
4318 */
4319int
4320skl_update_scaler_users(
4321 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4322 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4323 int force_detach)
4324{
4325 int need_scaling;
4326 int idx;
4327 int src_w, src_h, dst_w, dst_h;
4328 int *scaler_id;
4329 struct drm_framebuffer *fb;
4330 struct intel_crtc_scaler_state *scaler_state;
6156a456 4331 unsigned int rotation;
a1b2278e
CK
4332
4333 if (!intel_crtc || !crtc_state)
4334 return 0;
4335
4336 scaler_state = &crtc_state->scaler_state;
4337
4338 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4339 fb = intel_plane ? plane_state->base.fb : NULL;
4340
4341 if (intel_plane) {
4342 src_w = drm_rect_width(&plane_state->src) >> 16;
4343 src_h = drm_rect_height(&plane_state->src) >> 16;
4344 dst_w = drm_rect_width(&plane_state->dst);
4345 dst_h = drm_rect_height(&plane_state->dst);
4346 scaler_id = &plane_state->scaler_id;
6156a456 4347 rotation = plane_state->base.rotation;
a1b2278e
CK
4348 } else {
4349 struct drm_display_mode *adjusted_mode =
4350 &crtc_state->base.adjusted_mode;
4351 src_w = crtc_state->pipe_src_w;
4352 src_h = crtc_state->pipe_src_h;
4353 dst_w = adjusted_mode->hdisplay;
4354 dst_h = adjusted_mode->vdisplay;
4355 scaler_id = &scaler_state->scaler_id;
6156a456 4356 rotation = DRM_ROTATE_0;
a1b2278e 4357 }
6156a456
CK
4358
4359 need_scaling = intel_rotation_90_or_270(rotation) ?
4360 (src_h != dst_w || src_w != dst_h):
4361 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4362
4363 /*
4364 * if plane is being disabled or scaler is no more required or force detach
4365 * - free scaler binded to this plane/crtc
4366 * - in order to do this, update crtc->scaler_usage
4367 *
4368 * Here scaler state in crtc_state is set free so that
4369 * scaler can be assigned to other user. Actual register
4370 * update to free the scaler is done in plane/panel-fit programming.
4371 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4372 */
4373 if (force_detach || !need_scaling || (intel_plane &&
4374 (!fb || !plane_state->visible))) {
4375 if (*scaler_id >= 0) {
4376 scaler_state->scaler_users &= ~(1 << idx);
4377 scaler_state->scalers[*scaler_id].in_use = 0;
4378
4379 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4380 "crtc_state = %p scaler_users = 0x%x\n",
4381 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4382 intel_plane ? intel_plane->base.base.id :
4383 intel_crtc->base.base.id, crtc_state,
4384 scaler_state->scaler_users);
4385 *scaler_id = -1;
4386 }
4387 return 0;
4388 }
4389
4390 /* range checks */
4391 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4392 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4393
4394 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4395 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4396 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4397 "size is out of scaler range\n",
4398 intel_plane ? "PLANE" : "CRTC",
4399 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4400 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4401 return -EINVAL;
4402 }
4403
4404 /* check colorkey */
225c228a
CK
4405 if (WARN_ON(intel_plane &&
4406 intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
4407 DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
4408 intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4409 return -EINVAL;
4410 }
4411
4412 /* Check src format */
4413 if (intel_plane) {
4414 switch (fb->pixel_format) {
4415 case DRM_FORMAT_RGB565:
4416 case DRM_FORMAT_XBGR8888:
4417 case DRM_FORMAT_XRGB8888:
4418 case DRM_FORMAT_ABGR8888:
4419 case DRM_FORMAT_ARGB8888:
4420 case DRM_FORMAT_XRGB2101010:
a1b2278e 4421 case DRM_FORMAT_XBGR2101010:
a1b2278e
CK
4422 case DRM_FORMAT_YUYV:
4423 case DRM_FORMAT_YVYU:
4424 case DRM_FORMAT_UYVY:
4425 case DRM_FORMAT_VYUY:
4426 break;
4427 default:
4428 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4429 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4430 return -EINVAL;
4431 }
4432 }
4433
4434 /* mark this plane as a scaler user in crtc_state */
4435 scaler_state->scaler_users |= (1 << idx);
4436 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4437 "crtc_state = %p scaler_users = 0x%x\n",
4438 intel_plane ? "PLANE" : "CRTC",
4439 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4440 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4441 return 0;
4442}
4443
4444static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
bd2e244f
JB
4445{
4446 struct drm_device *dev = crtc->base.dev;
4447 struct drm_i915_private *dev_priv = dev->dev_private;
4448 int pipe = crtc->pipe;
a1b2278e
CK
4449 struct intel_crtc_scaler_state *scaler_state =
4450 &crtc->config->scaler_state;
4451
4452 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4453
4454 /* To update pfit, first update scaler state */
4455 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4456 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4457 skl_detach_scalers(crtc);
4458 if (!enable)
4459 return;
bd2e244f 4460
6e3c9717 4461 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4462 int id;
4463
4464 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4465 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4466 return;
4467 }
4468
4469 id = scaler_state->scaler_id;
4470 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4471 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4472 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4473 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4474
4475 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4476 }
4477}
4478
b074cec8
JB
4479static void ironlake_pfit_enable(struct intel_crtc *crtc)
4480{
4481 struct drm_device *dev = crtc->base.dev;
4482 struct drm_i915_private *dev_priv = dev->dev_private;
4483 int pipe = crtc->pipe;
4484
6e3c9717 4485 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4486 /* Force use of hard-coded filter coefficients
4487 * as some pre-programmed values are broken,
4488 * e.g. x201.
4489 */
4490 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4491 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4492 PF_PIPE_SEL_IVB(pipe));
4493 else
4494 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4495 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4496 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4497 }
4498}
4499
4a3b8769 4500static void intel_enable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4501{
4502 struct drm_device *dev = crtc->dev;
4503 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4504 struct drm_plane *plane;
bb53d4ae
VS
4505 struct intel_plane *intel_plane;
4506
af2b653b
MR
4507 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4508 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
4509 if (intel_plane->pipe == pipe)
4510 intel_plane_restore(&intel_plane->base);
af2b653b 4511 }
bb53d4ae
VS
4512}
4513
20bc8673 4514void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4515{
cea165c3
VS
4516 struct drm_device *dev = crtc->base.dev;
4517 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4518
6e3c9717 4519 if (!crtc->config->ips_enabled)
d77e4531
PZ
4520 return;
4521
cea165c3
VS
4522 /* We can only enable IPS after we enable a plane and wait for a vblank */
4523 intel_wait_for_vblank(dev, crtc->pipe);
4524
d77e4531 4525 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4526 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4527 mutex_lock(&dev_priv->rps.hw_lock);
4528 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4529 mutex_unlock(&dev_priv->rps.hw_lock);
4530 /* Quoting Art Runyan: "its not safe to expect any particular
4531 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4532 * mailbox." Moreover, the mailbox may return a bogus state,
4533 * so we need to just enable it and continue on.
2a114cc1
BW
4534 */
4535 } else {
4536 I915_WRITE(IPS_CTL, IPS_ENABLE);
4537 /* The bit only becomes 1 in the next vblank, so this wait here
4538 * is essentially intel_wait_for_vblank. If we don't have this
4539 * and don't wait for vblanks until the end of crtc_enable, then
4540 * the HW state readout code will complain that the expected
4541 * IPS_CTL value is not the one we read. */
4542 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4543 DRM_ERROR("Timed out waiting for IPS enable\n");
4544 }
d77e4531
PZ
4545}
4546
20bc8673 4547void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4548{
4549 struct drm_device *dev = crtc->base.dev;
4550 struct drm_i915_private *dev_priv = dev->dev_private;
4551
6e3c9717 4552 if (!crtc->config->ips_enabled)
d77e4531
PZ
4553 return;
4554
4555 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4556 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4557 mutex_lock(&dev_priv->rps.hw_lock);
4558 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4559 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4560 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4561 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4562 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4563 } else {
2a114cc1 4564 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4565 POSTING_READ(IPS_CTL);
4566 }
d77e4531
PZ
4567
4568 /* We need to wait for a vblank before we can disable the plane. */
4569 intel_wait_for_vblank(dev, crtc->pipe);
4570}
4571
4572/** Loads the palette/gamma unit for the CRTC with the prepared values */
4573static void intel_crtc_load_lut(struct drm_crtc *crtc)
4574{
4575 struct drm_device *dev = crtc->dev;
4576 struct drm_i915_private *dev_priv = dev->dev_private;
4577 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4578 enum pipe pipe = intel_crtc->pipe;
4579 int palreg = PALETTE(pipe);
4580 int i;
4581 bool reenable_ips = false;
4582
4583 /* The clocks have to be on to load the palette. */
53d9f4e9 4584 if (!crtc->state->active)
d77e4531
PZ
4585 return;
4586
50360403 4587 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
409ee761 4588 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4589 assert_dsi_pll_enabled(dev_priv);
4590 else
4591 assert_pll_enabled(dev_priv, pipe);
4592 }
4593
4594 /* use legacy palette for Ironlake */
7a1db49a 4595 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4596 palreg = LGC_PALETTE(pipe);
4597
4598 /* Workaround : Do not read or write the pipe palette/gamma data while
4599 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4600 */
6e3c9717 4601 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4602 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4603 GAMMA_MODE_MODE_SPLIT)) {
4604 hsw_disable_ips(intel_crtc);
4605 reenable_ips = true;
4606 }
4607
4608 for (i = 0; i < 256; i++) {
4609 I915_WRITE(palreg + 4 * i,
4610 (intel_crtc->lut_r[i] << 16) |
4611 (intel_crtc->lut_g[i] << 8) |
4612 intel_crtc->lut_b[i]);
4613 }
4614
4615 if (reenable_ips)
4616 hsw_enable_ips(intel_crtc);
4617}
4618
7cac945f 4619static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4620{
7cac945f 4621 if (intel_crtc->overlay) {
d3eedb1a
VS
4622 struct drm_device *dev = intel_crtc->base.dev;
4623 struct drm_i915_private *dev_priv = dev->dev_private;
4624
4625 mutex_lock(&dev->struct_mutex);
4626 dev_priv->mm.interruptible = false;
4627 (void) intel_overlay_switch_off(intel_crtc->overlay);
4628 dev_priv->mm.interruptible = true;
4629 mutex_unlock(&dev->struct_mutex);
4630 }
4631
4632 /* Let userspace switch the overlay on again. In most cases userspace
4633 * has to recompute where to put it anyway.
4634 */
4635}
4636
87d4300a
ML
4637/**
4638 * intel_post_enable_primary - Perform operations after enabling primary plane
4639 * @crtc: the CRTC whose primary plane was just enabled
4640 *
4641 * Performs potentially sleeping operations that must be done after the primary
4642 * plane is enabled, such as updating FBC and IPS. Note that this may be
4643 * called due to an explicit primary plane update, or due to an implicit
4644 * re-enable that is caused when a sprite plane is updated to no longer
4645 * completely hide the primary plane.
4646 */
4647static void
4648intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4649{
4650 struct drm_device *dev = crtc->dev;
87d4300a 4651 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4653 int pipe = intel_crtc->pipe;
a5c4d7bc 4654
87d4300a
ML
4655 /*
4656 * BDW signals flip done immediately if the plane
4657 * is disabled, even if the plane enable is already
4658 * armed to occur at the next vblank :(
4659 */
4660 if (IS_BROADWELL(dev))
4661 intel_wait_for_vblank(dev, pipe);
a5c4d7bc 4662
87d4300a
ML
4663 /*
4664 * FIXME IPS should be fine as long as one plane is
4665 * enabled, but in practice it seems to have problems
4666 * when going from primary only to sprite only and vice
4667 * versa.
4668 */
a5c4d7bc
VS
4669 hsw_enable_ips(intel_crtc);
4670
4671 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4672 intel_fbc_update(dev);
a5c4d7bc 4673 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4674
4675 /*
87d4300a
ML
4676 * Gen2 reports pipe underruns whenever all planes are disabled.
4677 * So don't enable underrun reporting before at least some planes
4678 * are enabled.
4679 * FIXME: Need to fix the logic to work when we turn off all planes
4680 * but leave the pipe running.
f99d7069 4681 */
87d4300a
ML
4682 if (IS_GEN2(dev))
4683 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4684
4685 /* Underruns don't raise interrupts, so check manually. */
4686 if (HAS_GMCH_DISPLAY(dev))
4687 i9xx_check_fifo_underruns(dev_priv);
a5c4d7bc
VS
4688}
4689
87d4300a
ML
4690/**
4691 * intel_pre_disable_primary - Perform operations before disabling primary plane
4692 * @crtc: the CRTC whose primary plane is to be disabled
4693 *
4694 * Performs potentially sleeping operations that must be done before the
4695 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4696 * be called due to an explicit primary plane update, or due to an implicit
4697 * disable that is caused when a sprite plane completely hides the primary
4698 * plane.
4699 */
4700static void
4701intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4702{
4703 struct drm_device *dev = crtc->dev;
4704 struct drm_i915_private *dev_priv = dev->dev_private;
4705 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4706 int pipe = intel_crtc->pipe;
a5c4d7bc 4707
87d4300a
ML
4708 /*
4709 * Gen2 reports pipe underruns whenever all planes are disabled.
4710 * So diasble underrun reporting before all the planes get disabled.
4711 * FIXME: Need to fix the logic to work when we turn off all planes
4712 * but leave the pipe running.
4713 */
4714 if (IS_GEN2(dev))
4715 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4716
87d4300a
ML
4717 /*
4718 * Vblank time updates from the shadow to live plane control register
4719 * are blocked if the memory self-refresh mode is active at that
4720 * moment. So to make sure the plane gets truly disabled, disable
4721 * first the self-refresh mode. The self-refresh enable bit in turn
4722 * will be checked/applied by the HW only at the next frame start
4723 * event which is after the vblank start event, so we need to have a
4724 * wait-for-vblank between disabling the plane and the pipe.
4725 */
4726 if (HAS_GMCH_DISPLAY(dev))
4727 intel_set_memory_cxsr(dev_priv, false);
4728
4729 mutex_lock(&dev->struct_mutex);
e35fef21 4730 if (dev_priv->fbc.crtc == intel_crtc)
7ff0ebcc 4731 intel_fbc_disable(dev);
87d4300a 4732 mutex_unlock(&dev->struct_mutex);
a5c4d7bc 4733
87d4300a
ML
4734 /*
4735 * FIXME IPS should be fine as long as one plane is
4736 * enabled, but in practice it seems to have problems
4737 * when going from primary only to sprite only and vice
4738 * versa.
4739 */
a5c4d7bc 4740 hsw_disable_ips(intel_crtc);
87d4300a
ML
4741}
4742
4743static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4744{
2d847d45
RV
4745 struct drm_device *dev = crtc->dev;
4746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4747 int pipe = intel_crtc->pipe;
4748
87d4300a
ML
4749 intel_enable_primary_hw_plane(crtc->primary, crtc);
4750 intel_enable_sprite_planes(crtc);
4751 intel_crtc_update_cursor(crtc, true);
87d4300a
ML
4752
4753 intel_post_enable_primary(crtc);
2d847d45
RV
4754
4755 /*
4756 * FIXME: Once we grow proper nuclear flip support out of this we need
4757 * to compute the mask of flip planes precisely. For the time being
4758 * consider this a flip to a NULL plane.
4759 */
4760 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
87d4300a
ML
4761}
4762
4763static void intel_crtc_disable_planes(struct drm_crtc *crtc)
4764{
4765 struct drm_device *dev = crtc->dev;
4766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4767 struct intel_plane *intel_plane;
4768 int pipe = intel_crtc->pipe;
4769
4770 intel_crtc_wait_for_pending_flips(crtc);
4771
4772 intel_pre_disable_primary(crtc);
a5c4d7bc 4773
7cac945f 4774 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8
ML
4775 for_each_intel_plane(dev, intel_plane) {
4776 if (intel_plane->pipe == pipe) {
4777 struct drm_crtc *from = intel_plane->base.crtc;
4778
4779 intel_plane->disable_plane(&intel_plane->base,
4780 from ?: crtc, true);
4781 }
4782 }
f98551ae 4783
f99d7069
DV
4784 /*
4785 * FIXME: Once we grow proper nuclear flip support out of this we need
4786 * to compute the mask of flip planes precisely. For the time being
4787 * consider this a flip to a NULL plane.
4788 */
4789 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4790}
4791
f67a559d
JB
4792static void ironlake_crtc_enable(struct drm_crtc *crtc)
4793{
4794 struct drm_device *dev = crtc->dev;
4795 struct drm_i915_private *dev_priv = dev->dev_private;
4796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4797 struct intel_encoder *encoder;
f67a559d 4798 int pipe = intel_crtc->pipe;
f67a559d 4799
53d9f4e9 4800 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4801 return;
4802
6e3c9717 4803 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4804 intel_prepare_shared_dpll(intel_crtc);
4805
6e3c9717 4806 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4807 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4808
4809 intel_set_pipe_timings(intel_crtc);
4810
6e3c9717 4811 if (intel_crtc->config->has_pch_encoder) {
29407aab 4812 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4813 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4814 }
4815
4816 ironlake_set_pipeconf(crtc);
4817
f67a559d 4818 intel_crtc->active = true;
8664281b 4819
a72e4c9f
DV
4820 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4821 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4822
f6736a1a 4823 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4824 if (encoder->pre_enable)
4825 encoder->pre_enable(encoder);
f67a559d 4826
6e3c9717 4827 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4828 /* Note: FDI PLL enabling _must_ be done before we enable the
4829 * cpu pipes, hence this is separate from all the other fdi/pch
4830 * enabling. */
88cefb6c 4831 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4832 } else {
4833 assert_fdi_tx_disabled(dev_priv, pipe);
4834 assert_fdi_rx_disabled(dev_priv, pipe);
4835 }
f67a559d 4836
b074cec8 4837 ironlake_pfit_enable(intel_crtc);
f67a559d 4838
9c54c0dd
JB
4839 /*
4840 * On ILK+ LUT must be loaded before the pipe is running but with
4841 * clocks enabled
4842 */
4843 intel_crtc_load_lut(crtc);
4844
f37fcc2a 4845 intel_update_watermarks(crtc);
e1fdc473 4846 intel_enable_pipe(intel_crtc);
f67a559d 4847
6e3c9717 4848 if (intel_crtc->config->has_pch_encoder)
f67a559d 4849 ironlake_pch_enable(crtc);
c98e9dcf 4850
f9b61ff6
DV
4851 assert_vblank_disabled(crtc);
4852 drm_crtc_vblank_on(crtc);
4853
fa5c73b1
DV
4854 for_each_encoder_on_crtc(dev, crtc, encoder)
4855 encoder->enable(encoder);
61b77ddd
DV
4856
4857 if (HAS_PCH_CPT(dev))
a1520318 4858 cpt_verify_modeset(dev, intel_crtc->pipe);
6be4a607
JB
4859}
4860
42db64ef
PZ
4861/* IPS only exists on ULT machines and is tied to pipe A. */
4862static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4863{
f5adf94e 4864 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4865}
4866
e4916946
PZ
4867/*
4868 * This implements the workaround described in the "notes" section of the mode
4869 * set sequence documentation. When going from no pipes or single pipe to
4870 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4871 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4872 */
4873static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4874{
4875 struct drm_device *dev = crtc->base.dev;
4876 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4877
4878 /* We want to get the other_active_crtc only if there's only 1 other
4879 * active crtc. */
d3fcc808 4880 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4881 if (!crtc_it->active || crtc_it == crtc)
4882 continue;
4883
4884 if (other_active_crtc)
4885 return;
4886
4887 other_active_crtc = crtc_it;
4888 }
4889 if (!other_active_crtc)
4890 return;
4891
4892 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4893 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4894}
4895
4f771f10
PZ
4896static void haswell_crtc_enable(struct drm_crtc *crtc)
4897{
4898 struct drm_device *dev = crtc->dev;
4899 struct drm_i915_private *dev_priv = dev->dev_private;
4900 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4901 struct intel_encoder *encoder;
4902 int pipe = intel_crtc->pipe;
4f771f10 4903
53d9f4e9 4904 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4905 return;
4906
df8ad70c
DV
4907 if (intel_crtc_to_shared_dpll(intel_crtc))
4908 intel_enable_shared_dpll(intel_crtc);
4909
6e3c9717 4910 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4911 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
4912
4913 intel_set_pipe_timings(intel_crtc);
4914
6e3c9717
ACO
4915 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4916 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4917 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4918 }
4919
6e3c9717 4920 if (intel_crtc->config->has_pch_encoder) {
229fca97 4921 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4922 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4923 }
4924
4925 haswell_set_pipeconf(crtc);
4926
4927 intel_set_pipe_csc(crtc);
4928
4f771f10 4929 intel_crtc->active = true;
8664281b 4930
a72e4c9f 4931 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
4932 for_each_encoder_on_crtc(dev, crtc, encoder)
4933 if (encoder->pre_enable)
4934 encoder->pre_enable(encoder);
4935
6e3c9717 4936 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
4937 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4938 true);
4fe9467d
ID
4939 dev_priv->display.fdi_link_train(crtc);
4940 }
4941
1f544388 4942 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4943
ff6d9f55 4944 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 4945 skylake_pfit_update(intel_crtc, 1);
ff6d9f55 4946 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 4947 ironlake_pfit_enable(intel_crtc);
ff6d9f55
JB
4948 else
4949 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10
PZ
4950
4951 /*
4952 * On ILK+ LUT must be loaded before the pipe is running but with
4953 * clocks enabled
4954 */
4955 intel_crtc_load_lut(crtc);
4956
1f544388 4957 intel_ddi_set_pipe_settings(crtc);
8228c251 4958 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4959
f37fcc2a 4960 intel_update_watermarks(crtc);
e1fdc473 4961 intel_enable_pipe(intel_crtc);
42db64ef 4962
6e3c9717 4963 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4964 lpt_pch_enable(crtc);
4f771f10 4965
6e3c9717 4966 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4967 intel_ddi_set_vc_payload_alloc(crtc, true);
4968
f9b61ff6
DV
4969 assert_vblank_disabled(crtc);
4970 drm_crtc_vblank_on(crtc);
4971
8807e55b 4972 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4973 encoder->enable(encoder);
8807e55b
JN
4974 intel_opregion_notify_encoder(encoder, true);
4975 }
4f771f10 4976
e4916946
PZ
4977 /* If we change the relative order between pipe/planes enabling, we need
4978 * to change the workaround. */
4979 haswell_mode_set_planes_workaround(intel_crtc);
4f771f10
PZ
4980}
4981
3f8dce3a
DV
4982static void ironlake_pfit_disable(struct intel_crtc *crtc)
4983{
4984 struct drm_device *dev = crtc->base.dev;
4985 struct drm_i915_private *dev_priv = dev->dev_private;
4986 int pipe = crtc->pipe;
4987
4988 /* To avoid upsetting the power well on haswell only disable the pfit if
4989 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 4990 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4991 I915_WRITE(PF_CTL(pipe), 0);
4992 I915_WRITE(PF_WIN_POS(pipe), 0);
4993 I915_WRITE(PF_WIN_SZ(pipe), 0);
4994 }
4995}
4996
6be4a607
JB
4997static void ironlake_crtc_disable(struct drm_crtc *crtc)
4998{
4999 struct drm_device *dev = crtc->dev;
5000 struct drm_i915_private *dev_priv = dev->dev_private;
5001 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5002 struct intel_encoder *encoder;
6be4a607 5003 int pipe = intel_crtc->pipe;
5eddb70b 5004 u32 reg, temp;
b52eb4dc 5005
53d9f4e9 5006 if (WARN_ON(!intel_crtc->active))
f7abfe8b
CW
5007 return;
5008
ea9d758d
DV
5009 for_each_encoder_on_crtc(dev, crtc, encoder)
5010 encoder->disable(encoder);
5011
f9b61ff6
DV
5012 drm_crtc_vblank_off(crtc);
5013 assert_vblank_disabled(crtc);
5014
6e3c9717 5015 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 5016 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 5017
575f7ab7 5018 intel_disable_pipe(intel_crtc);
32f9d658 5019
3f8dce3a 5020 ironlake_pfit_disable(intel_crtc);
2c07245f 5021
5a74f70a
VS
5022 if (intel_crtc->config->has_pch_encoder)
5023 ironlake_fdi_disable(crtc);
5024
bf49ec8c
DV
5025 for_each_encoder_on_crtc(dev, crtc, encoder)
5026 if (encoder->post_disable)
5027 encoder->post_disable(encoder);
2c07245f 5028
6e3c9717 5029 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5030 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5031
d925c59a
DV
5032 if (HAS_PCH_CPT(dev)) {
5033 /* disable TRANS_DP_CTL */
5034 reg = TRANS_DP_CTL(pipe);
5035 temp = I915_READ(reg);
5036 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5037 TRANS_DP_PORT_SEL_MASK);
5038 temp |= TRANS_DP_PORT_SEL_NONE;
5039 I915_WRITE(reg, temp);
5040
5041 /* disable DPLL_SEL */
5042 temp = I915_READ(PCH_DPLL_SEL);
11887397 5043 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5044 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5045 }
e3421a18 5046
d925c59a 5047 /* disable PCH DPLL */
e72f9fbf 5048 intel_disable_shared_dpll(intel_crtc);
8db9d77b 5049
d925c59a
DV
5050 ironlake_fdi_pll_disable(intel_crtc);
5051 }
6b383a7f 5052
f7abfe8b 5053 intel_crtc->active = false;
46ba614c 5054 intel_update_watermarks(crtc);
d1ebd816
BW
5055
5056 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5057 intel_fbc_update(dev);
d1ebd816 5058 mutex_unlock(&dev->struct_mutex);
6be4a607 5059}
1b3c7a47 5060
4f771f10 5061static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5062{
4f771f10
PZ
5063 struct drm_device *dev = crtc->dev;
5064 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5065 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5066 struct intel_encoder *encoder;
6e3c9717 5067 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5068
53d9f4e9 5069 if (WARN_ON(!intel_crtc->active))
4f771f10
PZ
5070 return;
5071
8807e55b
JN
5072 for_each_encoder_on_crtc(dev, crtc, encoder) {
5073 intel_opregion_notify_encoder(encoder, false);
4f771f10 5074 encoder->disable(encoder);
8807e55b 5075 }
4f771f10 5076
f9b61ff6
DV
5077 drm_crtc_vblank_off(crtc);
5078 assert_vblank_disabled(crtc);
5079
6e3c9717 5080 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
5081 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5082 false);
575f7ab7 5083 intel_disable_pipe(intel_crtc);
4f771f10 5084
6e3c9717 5085 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5086 intel_ddi_set_vc_payload_alloc(crtc, false);
5087
ad80a810 5088 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5089
ff6d9f55 5090 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5091 skylake_pfit_update(intel_crtc, 0);
ff6d9f55 5092 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5093 ironlake_pfit_disable(intel_crtc);
ff6d9f55
JB
5094 else
5095 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10 5096
1f544388 5097 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5098
6e3c9717 5099 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 5100 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 5101 intel_ddi_fdi_disable(crtc);
83616634 5102 }
4f771f10 5103
97b040aa
ID
5104 for_each_encoder_on_crtc(dev, crtc, encoder)
5105 if (encoder->post_disable)
5106 encoder->post_disable(encoder);
5107
4f771f10 5108 intel_crtc->active = false;
46ba614c 5109 intel_update_watermarks(crtc);
4f771f10
PZ
5110
5111 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5112 intel_fbc_update(dev);
4f771f10 5113 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
5114
5115 if (intel_crtc_to_shared_dpll(intel_crtc))
5116 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
5117}
5118
2dd24552
JB
5119static void i9xx_pfit_enable(struct intel_crtc *crtc)
5120{
5121 struct drm_device *dev = crtc->base.dev;
5122 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5123 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5124
681a8504 5125 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5126 return;
5127
2dd24552 5128 /*
c0b03411
DV
5129 * The panel fitter should only be adjusted whilst the pipe is disabled,
5130 * according to register description and PRM.
2dd24552 5131 */
c0b03411
DV
5132 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5133 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5134
b074cec8
JB
5135 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5136 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5137
5138 /* Border color in case we don't scale up to the full screen. Black by
5139 * default, change to something else for debugging. */
5140 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5141}
5142
d05410f9
DA
5143static enum intel_display_power_domain port_to_power_domain(enum port port)
5144{
5145 switch (port) {
5146 case PORT_A:
5147 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5148 case PORT_B:
5149 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5150 case PORT_C:
5151 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5152 case PORT_D:
5153 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5154 default:
5155 WARN_ON_ONCE(1);
5156 return POWER_DOMAIN_PORT_OTHER;
5157 }
5158}
5159
77d22dca
ID
5160#define for_each_power_domain(domain, mask) \
5161 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5162 if ((1 << (domain)) & (mask))
5163
319be8ae
ID
5164enum intel_display_power_domain
5165intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5166{
5167 struct drm_device *dev = intel_encoder->base.dev;
5168 struct intel_digital_port *intel_dig_port;
5169
5170 switch (intel_encoder->type) {
5171 case INTEL_OUTPUT_UNKNOWN:
5172 /* Only DDI platforms should ever use this output type */
5173 WARN_ON_ONCE(!HAS_DDI(dev));
5174 case INTEL_OUTPUT_DISPLAYPORT:
5175 case INTEL_OUTPUT_HDMI:
5176 case INTEL_OUTPUT_EDP:
5177 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5178 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5179 case INTEL_OUTPUT_DP_MST:
5180 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5181 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5182 case INTEL_OUTPUT_ANALOG:
5183 return POWER_DOMAIN_PORT_CRT;
5184 case INTEL_OUTPUT_DSI:
5185 return POWER_DOMAIN_PORT_DSI;
5186 default:
5187 return POWER_DOMAIN_PORT_OTHER;
5188 }
5189}
5190
5191static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5192{
319be8ae
ID
5193 struct drm_device *dev = crtc->dev;
5194 struct intel_encoder *intel_encoder;
5195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5196 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
5197 unsigned long mask;
5198 enum transcoder transcoder;
5199
5200 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5201
5202 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5203 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
5204 if (intel_crtc->config->pch_pfit.enabled ||
5205 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
5206 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5207
319be8ae
ID
5208 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5209 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5210
77d22dca
ID
5211 return mask;
5212}
5213
679dacd4 5214static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
77d22dca 5215{
679dacd4 5216 struct drm_device *dev = state->dev;
77d22dca
ID
5217 struct drm_i915_private *dev_priv = dev->dev_private;
5218 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5219 struct intel_crtc *crtc;
5220
5221 /*
5222 * First get all needed power domains, then put all unneeded, to avoid
5223 * any unnecessary toggling of the power wells.
5224 */
d3fcc808 5225 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5226 enum intel_display_power_domain domain;
5227
83d65738 5228 if (!crtc->base.state->enable)
77d22dca
ID
5229 continue;
5230
319be8ae 5231 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
5232
5233 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5234 intel_display_power_get(dev_priv, domain);
5235 }
5236
50f6e502 5237 if (dev_priv->display.modeset_global_resources)
679dacd4 5238 dev_priv->display.modeset_global_resources(state);
50f6e502 5239
d3fcc808 5240 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5241 enum intel_display_power_domain domain;
5242
5243 for_each_power_domain(domain, crtc->enabled_power_domains)
5244 intel_display_power_put(dev_priv, domain);
5245
5246 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5247 }
5248
5249 intel_display_set_init_power(dev_priv, false);
5250}
5251
560a7ae4
DL
5252static void intel_update_max_cdclk(struct drm_device *dev)
5253{
5254 struct drm_i915_private *dev_priv = dev->dev_private;
5255
5256 if (IS_SKYLAKE(dev)) {
5257 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5258
5259 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5260 dev_priv->max_cdclk_freq = 675000;
5261 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5262 dev_priv->max_cdclk_freq = 540000;
5263 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5264 dev_priv->max_cdclk_freq = 450000;
5265 else
5266 dev_priv->max_cdclk_freq = 337500;
5267 } else if (IS_BROADWELL(dev)) {
5268 /*
5269 * FIXME with extra cooling we can allow
5270 * 540 MHz for ULX and 675 Mhz for ULT.
5271 * How can we know if extra cooling is
5272 * available? PCI ID, VTB, something else?
5273 */
5274 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5275 dev_priv->max_cdclk_freq = 450000;
5276 else if (IS_BDW_ULX(dev))
5277 dev_priv->max_cdclk_freq = 450000;
5278 else if (IS_BDW_ULT(dev))
5279 dev_priv->max_cdclk_freq = 540000;
5280 else
5281 dev_priv->max_cdclk_freq = 675000;
5282 } else if (IS_VALLEYVIEW(dev)) {
5283 dev_priv->max_cdclk_freq = 400000;
5284 } else {
5285 /* otherwise assume cdclk is fixed */
5286 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5287 }
5288
5289 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5290 dev_priv->max_cdclk_freq);
5291}
5292
5293static void intel_update_cdclk(struct drm_device *dev)
5294{
5295 struct drm_i915_private *dev_priv = dev->dev_private;
5296
5297 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5298 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5299 dev_priv->cdclk_freq);
5300
5301 /*
5302 * Program the gmbus_freq based on the cdclk frequency.
5303 * BSpec erroneously claims we should aim for 4MHz, but
5304 * in fact 1MHz is the correct frequency.
5305 */
5306 if (IS_VALLEYVIEW(dev)) {
5307 /*
5308 * Program the gmbus_freq based on the cdclk frequency.
5309 * BSpec erroneously claims we should aim for 4MHz, but
5310 * in fact 1MHz is the correct frequency.
5311 */
5312 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5313 }
5314
5315 if (dev_priv->max_cdclk_freq == 0)
5316 intel_update_max_cdclk(dev);
5317}
5318
70d0c574 5319static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5320{
5321 struct drm_i915_private *dev_priv = dev->dev_private;
5322 uint32_t divider;
5323 uint32_t ratio;
5324 uint32_t current_freq;
5325 int ret;
5326
5327 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5328 switch (frequency) {
5329 case 144000:
5330 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5331 ratio = BXT_DE_PLL_RATIO(60);
5332 break;
5333 case 288000:
5334 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5335 ratio = BXT_DE_PLL_RATIO(60);
5336 break;
5337 case 384000:
5338 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5339 ratio = BXT_DE_PLL_RATIO(60);
5340 break;
5341 case 576000:
5342 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5343 ratio = BXT_DE_PLL_RATIO(60);
5344 break;
5345 case 624000:
5346 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5347 ratio = BXT_DE_PLL_RATIO(65);
5348 break;
5349 case 19200:
5350 /*
5351 * Bypass frequency with DE PLL disabled. Init ratio, divider
5352 * to suppress GCC warning.
5353 */
5354 ratio = 0;
5355 divider = 0;
5356 break;
5357 default:
5358 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5359
5360 return;
5361 }
5362
5363 mutex_lock(&dev_priv->rps.hw_lock);
5364 /* Inform power controller of upcoming frequency change */
5365 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5366 0x80000000);
5367 mutex_unlock(&dev_priv->rps.hw_lock);
5368
5369 if (ret) {
5370 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5371 ret, frequency);
5372 return;
5373 }
5374
5375 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5376 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5377 current_freq = current_freq * 500 + 1000;
5378
5379 /*
5380 * DE PLL has to be disabled when
5381 * - setting to 19.2MHz (bypass, PLL isn't used)
5382 * - before setting to 624MHz (PLL needs toggling)
5383 * - before setting to any frequency from 624MHz (PLL needs toggling)
5384 */
5385 if (frequency == 19200 || frequency == 624000 ||
5386 current_freq == 624000) {
5387 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5388 /* Timeout 200us */
5389 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5390 1))
5391 DRM_ERROR("timout waiting for DE PLL unlock\n");
5392 }
5393
5394 if (frequency != 19200) {
5395 uint32_t val;
5396
5397 val = I915_READ(BXT_DE_PLL_CTL);
5398 val &= ~BXT_DE_PLL_RATIO_MASK;
5399 val |= ratio;
5400 I915_WRITE(BXT_DE_PLL_CTL, val);
5401
5402 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5403 /* Timeout 200us */
5404 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5405 DRM_ERROR("timeout waiting for DE PLL lock\n");
5406
5407 val = I915_READ(CDCLK_CTL);
5408 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5409 val |= divider;
5410 /*
5411 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5412 * enable otherwise.
5413 */
5414 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5415 if (frequency >= 500000)
5416 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5417
5418 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5419 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5420 val |= (frequency - 1000) / 500;
5421 I915_WRITE(CDCLK_CTL, val);
5422 }
5423
5424 mutex_lock(&dev_priv->rps.hw_lock);
5425 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5426 DIV_ROUND_UP(frequency, 25000));
5427 mutex_unlock(&dev_priv->rps.hw_lock);
5428
5429 if (ret) {
5430 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5431 ret, frequency);
5432 return;
5433 }
5434
a47871bd 5435 intel_update_cdclk(dev);
f8437dd1
VK
5436}
5437
5438void broxton_init_cdclk(struct drm_device *dev)
5439{
5440 struct drm_i915_private *dev_priv = dev->dev_private;
5441 uint32_t val;
5442
5443 /*
5444 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5445 * or else the reset will hang because there is no PCH to respond.
5446 * Move the handshake programming to initialization sequence.
5447 * Previously was left up to BIOS.
5448 */
5449 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5450 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5451 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5452
5453 /* Enable PG1 for cdclk */
5454 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5455
5456 /* check if cd clock is enabled */
5457 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5458 DRM_DEBUG_KMS("Display already initialized\n");
5459 return;
5460 }
5461
5462 /*
5463 * FIXME:
5464 * - The initial CDCLK needs to be read from VBT.
5465 * Need to make this change after VBT has changes for BXT.
5466 * - check if setting the max (or any) cdclk freq is really necessary
5467 * here, it belongs to modeset time
5468 */
5469 broxton_set_cdclk(dev, 624000);
5470
5471 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5472 POSTING_READ(DBUF_CTL);
5473
f8437dd1
VK
5474 udelay(10);
5475
5476 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5477 DRM_ERROR("DBuf power enable timeout!\n");
5478}
5479
5480void broxton_uninit_cdclk(struct drm_device *dev)
5481{
5482 struct drm_i915_private *dev_priv = dev->dev_private;
5483
5484 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5485 POSTING_READ(DBUF_CTL);
5486
f8437dd1
VK
5487 udelay(10);
5488
5489 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5490 DRM_ERROR("DBuf power disable timeout!\n");
5491
5492 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5493 broxton_set_cdclk(dev, 19200);
5494
5495 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5496}
5497
5d96d8af
DL
5498static const struct skl_cdclk_entry {
5499 unsigned int freq;
5500 unsigned int vco;
5501} skl_cdclk_frequencies[] = {
5502 { .freq = 308570, .vco = 8640 },
5503 { .freq = 337500, .vco = 8100 },
5504 { .freq = 432000, .vco = 8640 },
5505 { .freq = 450000, .vco = 8100 },
5506 { .freq = 540000, .vco = 8100 },
5507 { .freq = 617140, .vco = 8640 },
5508 { .freq = 675000, .vco = 8100 },
5509};
5510
5511static unsigned int skl_cdclk_decimal(unsigned int freq)
5512{
5513 return (freq - 1000) / 500;
5514}
5515
5516static unsigned int skl_cdclk_get_vco(unsigned int freq)
5517{
5518 unsigned int i;
5519
5520 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5521 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5522
5523 if (e->freq == freq)
5524 return e->vco;
5525 }
5526
5527 return 8100;
5528}
5529
5530static void
5531skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5532{
5533 unsigned int min_freq;
5534 u32 val;
5535
5536 /* select the minimum CDCLK before enabling DPLL 0 */
5537 val = I915_READ(CDCLK_CTL);
5538 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5539 val |= CDCLK_FREQ_337_308;
5540
5541 if (required_vco == 8640)
5542 min_freq = 308570;
5543 else
5544 min_freq = 337500;
5545
5546 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5547
5548 I915_WRITE(CDCLK_CTL, val);
5549 POSTING_READ(CDCLK_CTL);
5550
5551 /*
5552 * We always enable DPLL0 with the lowest link rate possible, but still
5553 * taking into account the VCO required to operate the eDP panel at the
5554 * desired frequency. The usual DP link rates operate with a VCO of
5555 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5556 * The modeset code is responsible for the selection of the exact link
5557 * rate later on, with the constraint of choosing a frequency that
5558 * works with required_vco.
5559 */
5560 val = I915_READ(DPLL_CTRL1);
5561
5562 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5563 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5564 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5565 if (required_vco == 8640)
5566 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5567 SKL_DPLL0);
5568 else
5569 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5570 SKL_DPLL0);
5571
5572 I915_WRITE(DPLL_CTRL1, val);
5573 POSTING_READ(DPLL_CTRL1);
5574
5575 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5576
5577 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5578 DRM_ERROR("DPLL0 not locked\n");
5579}
5580
5581static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5582{
5583 int ret;
5584 u32 val;
5585
5586 /* inform PCU we want to change CDCLK */
5587 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5588 mutex_lock(&dev_priv->rps.hw_lock);
5589 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5590 mutex_unlock(&dev_priv->rps.hw_lock);
5591
5592 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5593}
5594
5595static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5596{
5597 unsigned int i;
5598
5599 for (i = 0; i < 15; i++) {
5600 if (skl_cdclk_pcu_ready(dev_priv))
5601 return true;
5602 udelay(10);
5603 }
5604
5605 return false;
5606}
5607
5608static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5609{
560a7ae4 5610 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5611 u32 freq_select, pcu_ack;
5612
5613 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5614
5615 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5616 DRM_ERROR("failed to inform PCU about cdclk change\n");
5617 return;
5618 }
5619
5620 /* set CDCLK_CTL */
5621 switch(freq) {
5622 case 450000:
5623 case 432000:
5624 freq_select = CDCLK_FREQ_450_432;
5625 pcu_ack = 1;
5626 break;
5627 case 540000:
5628 freq_select = CDCLK_FREQ_540;
5629 pcu_ack = 2;
5630 break;
5631 case 308570:
5632 case 337500:
5633 default:
5634 freq_select = CDCLK_FREQ_337_308;
5635 pcu_ack = 0;
5636 break;
5637 case 617140:
5638 case 675000:
5639 freq_select = CDCLK_FREQ_675_617;
5640 pcu_ack = 3;
5641 break;
5642 }
5643
5644 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5645 POSTING_READ(CDCLK_CTL);
5646
5647 /* inform PCU of the change */
5648 mutex_lock(&dev_priv->rps.hw_lock);
5649 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5650 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5651
5652 intel_update_cdclk(dev);
5d96d8af
DL
5653}
5654
5655void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5656{
5657 /* disable DBUF power */
5658 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5659 POSTING_READ(DBUF_CTL);
5660
5661 udelay(10);
5662
5663 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5664 DRM_ERROR("DBuf power disable timeout\n");
5665
5666 /* disable DPLL0 */
5667 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5668 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5669 DRM_ERROR("Couldn't disable DPLL0\n");
5670
5671 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5672}
5673
5674void skl_init_cdclk(struct drm_i915_private *dev_priv)
5675{
5676 u32 val;
5677 unsigned int required_vco;
5678
5679 /* enable PCH reset handshake */
5680 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5681 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5682
5683 /* enable PG1 and Misc I/O */
5684 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5685
5686 /* DPLL0 already enabed !? */
5687 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5688 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5689 return;
5690 }
5691
5692 /* enable DPLL0 */
5693 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5694 skl_dpll0_enable(dev_priv, required_vco);
5695
5696 /* set CDCLK to the frequency the BIOS chose */
5697 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5698
5699 /* enable DBUF power */
5700 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5701 POSTING_READ(DBUF_CTL);
5702
5703 udelay(10);
5704
5705 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5706 DRM_ERROR("DBuf power enable timeout\n");
5707}
5708
dfcab17e 5709/* returns HPLL frequency in kHz */
f8bf63fd 5710static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 5711{
586f49dc 5712 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 5713
586f49dc 5714 /* Obtain SKU information */
a580516d 5715 mutex_lock(&dev_priv->sb_lock);
586f49dc
JB
5716 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5717 CCK_FUSE_HPLL_FREQ_MASK;
a580516d 5718 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5719
dfcab17e 5720 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
5721}
5722
5723/* Adjust CDclk dividers to allow high res or save power if possible */
5724static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5725{
5726 struct drm_i915_private *dev_priv = dev->dev_private;
5727 u32 val, cmd;
5728
164dfd28
VK
5729 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5730 != dev_priv->cdclk_freq);
d60c4473 5731
dfcab17e 5732 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5733 cmd = 2;
dfcab17e 5734 else if (cdclk == 266667)
30a970c6
JB
5735 cmd = 1;
5736 else
5737 cmd = 0;
5738
5739 mutex_lock(&dev_priv->rps.hw_lock);
5740 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5741 val &= ~DSPFREQGUAR_MASK;
5742 val |= (cmd << DSPFREQGUAR_SHIFT);
5743 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5744 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5745 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5746 50)) {
5747 DRM_ERROR("timed out waiting for CDclk change\n");
5748 }
5749 mutex_unlock(&dev_priv->rps.hw_lock);
5750
54433e91
VS
5751 mutex_lock(&dev_priv->sb_lock);
5752
dfcab17e 5753 if (cdclk == 400000) {
6bcda4f0 5754 u32 divider;
30a970c6 5755
6bcda4f0 5756 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5757
30a970c6
JB
5758 /* adjust cdclk divider */
5759 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 5760 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
5761 val |= divider;
5762 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5763
5764 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5765 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5766 50))
5767 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5768 }
5769
30a970c6
JB
5770 /* adjust self-refresh exit latency value */
5771 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5772 val &= ~0x7f;
5773
5774 /*
5775 * For high bandwidth configs, we set a higher latency in the bunit
5776 * so that the core display fetch happens in time to avoid underruns.
5777 */
dfcab17e 5778 if (cdclk == 400000)
30a970c6
JB
5779 val |= 4500 / 250; /* 4.5 usec */
5780 else
5781 val |= 3000 / 250; /* 3.0 usec */
5782 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5783
a580516d 5784 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5785
b6283055 5786 intel_update_cdclk(dev);
30a970c6
JB
5787}
5788
383c5a6a
VS
5789static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5790{
5791 struct drm_i915_private *dev_priv = dev->dev_private;
5792 u32 val, cmd;
5793
164dfd28
VK
5794 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5795 != dev_priv->cdclk_freq);
383c5a6a
VS
5796
5797 switch (cdclk) {
383c5a6a
VS
5798 case 333333:
5799 case 320000:
383c5a6a 5800 case 266667:
383c5a6a 5801 case 200000:
383c5a6a
VS
5802 break;
5803 default:
5f77eeb0 5804 MISSING_CASE(cdclk);
383c5a6a
VS
5805 return;
5806 }
5807
9d0d3fda
VS
5808 /*
5809 * Specs are full of misinformation, but testing on actual
5810 * hardware has shown that we just need to write the desired
5811 * CCK divider into the Punit register.
5812 */
5813 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5814
383c5a6a
VS
5815 mutex_lock(&dev_priv->rps.hw_lock);
5816 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5817 val &= ~DSPFREQGUAR_MASK_CHV;
5818 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5819 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5820 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5821 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5822 50)) {
5823 DRM_ERROR("timed out waiting for CDclk change\n");
5824 }
5825 mutex_unlock(&dev_priv->rps.hw_lock);
5826
b6283055 5827 intel_update_cdclk(dev);
383c5a6a
VS
5828}
5829
30a970c6
JB
5830static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5831 int max_pixclk)
5832{
6bcda4f0 5833 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5834 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5835
30a970c6
JB
5836 /*
5837 * Really only a few cases to deal with, as only 4 CDclks are supported:
5838 * 200MHz
5839 * 267MHz
29dc7ef3 5840 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5841 * 400MHz (VLV only)
5842 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5843 * of the lower bin and adjust if needed.
e37c67a1
VS
5844 *
5845 * We seem to get an unstable or solid color picture at 200MHz.
5846 * Not sure what's wrong. For now use 200MHz only when all pipes
5847 * are off.
30a970c6 5848 */
6cca3195
VS
5849 if (!IS_CHERRYVIEW(dev_priv) &&
5850 max_pixclk > freq_320*limit/100)
dfcab17e 5851 return 400000;
6cca3195 5852 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5853 return freq_320;
e37c67a1 5854 else if (max_pixclk > 0)
dfcab17e 5855 return 266667;
e37c67a1
VS
5856 else
5857 return 200000;
30a970c6
JB
5858}
5859
f8437dd1
VK
5860static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5861 int max_pixclk)
5862{
5863 /*
5864 * FIXME:
5865 * - remove the guardband, it's not needed on BXT
5866 * - set 19.2MHz bypass frequency if there are no active pipes
5867 */
5868 if (max_pixclk > 576000*9/10)
5869 return 624000;
5870 else if (max_pixclk > 384000*9/10)
5871 return 576000;
5872 else if (max_pixclk > 288000*9/10)
5873 return 384000;
5874 else if (max_pixclk > 144000*9/10)
5875 return 288000;
5876 else
5877 return 144000;
5878}
5879
a821fc46
ACO
5880/* Compute the max pixel clock for new configuration. Uses atomic state if
5881 * that's non-NULL, look at current state otherwise. */
5882static int intel_mode_max_pixclk(struct drm_device *dev,
5883 struct drm_atomic_state *state)
30a970c6 5884{
30a970c6 5885 struct intel_crtc *intel_crtc;
304603f4 5886 struct intel_crtc_state *crtc_state;
30a970c6
JB
5887 int max_pixclk = 0;
5888
d3fcc808 5889 for_each_intel_crtc(dev, intel_crtc) {
a821fc46
ACO
5890 if (state)
5891 crtc_state =
5892 intel_atomic_get_crtc_state(state, intel_crtc);
5893 else
5894 crtc_state = intel_crtc->config;
304603f4
ACO
5895 if (IS_ERR(crtc_state))
5896 return PTR_ERR(crtc_state);
5897
5898 if (!crtc_state->base.enable)
5899 continue;
5900
5901 max_pixclk = max(max_pixclk,
5902 crtc_state->base.adjusted_mode.crtc_clock);
30a970c6
JB
5903 }
5904
5905 return max_pixclk;
5906}
5907
0a9ab303 5908static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
30a970c6 5909{
304603f4 5910 struct drm_i915_private *dev_priv = to_i915(state->dev);
0a9ab303
ACO
5911 struct drm_crtc *crtc;
5912 struct drm_crtc_state *crtc_state;
a821fc46 5913 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
85a96e7a 5914 int cdclk, ret = 0;
30a970c6 5915
304603f4
ACO
5916 if (max_pixclk < 0)
5917 return max_pixclk;
30a970c6 5918
f8437dd1
VK
5919 if (IS_VALLEYVIEW(dev_priv))
5920 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5921 else
5922 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5923
5924 if (cdclk == dev_priv->cdclk_freq)
304603f4 5925 return 0;
30a970c6 5926
0a9ab303
ACO
5927 /* add all active pipes to the state */
5928 for_each_crtc(state->dev, crtc) {
0a9ab303
ACO
5929 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5930 if (IS_ERR(crtc_state))
5931 return PTR_ERR(crtc_state);
0a9ab303 5932
85a96e7a
ML
5933 if (!crtc_state->active || needs_modeset(crtc_state))
5934 continue;
304603f4 5935
85a96e7a
ML
5936 crtc_state->mode_changed = true;
5937
5938 ret = drm_atomic_add_affected_connectors(state, crtc);
5939 if (ret)
5940 break;
5941
5942 ret = drm_atomic_add_affected_planes(state, crtc);
5943 if (ret)
5944 break;
5945 }
5946
5947 return ret;
30a970c6
JB
5948}
5949
1e69cd74
VS
5950static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5951{
5952 unsigned int credits, default_credits;
5953
5954 if (IS_CHERRYVIEW(dev_priv))
5955 default_credits = PFI_CREDIT(12);
5956 else
5957 default_credits = PFI_CREDIT(8);
5958
164dfd28 5959 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
1e69cd74
VS
5960 /* CHV suggested value is 31 or 63 */
5961 if (IS_CHERRYVIEW(dev_priv))
5962 credits = PFI_CREDIT_31;
5963 else
5964 credits = PFI_CREDIT(15);
5965 } else {
5966 credits = default_credits;
5967 }
5968
5969 /*
5970 * WA - write default credits before re-programming
5971 * FIXME: should we also set the resend bit here?
5972 */
5973 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5974 default_credits);
5975
5976 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5977 credits | PFI_CREDIT_RESEND);
5978
5979 /*
5980 * FIXME is this guaranteed to clear
5981 * immediately or should we poll for it?
5982 */
5983 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5984}
5985
a821fc46 5986static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
30a970c6 5987{
a821fc46 5988 struct drm_device *dev = old_state->dev;
30a970c6 5989 struct drm_i915_private *dev_priv = dev->dev_private;
a821fc46 5990 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
304603f4
ACO
5991 int req_cdclk;
5992
a821fc46
ACO
5993 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
5994 * never fail. */
304603f4
ACO
5995 if (WARN_ON(max_pixclk < 0))
5996 return;
30a970c6 5997
304603f4 5998 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
30a970c6 5999
164dfd28 6000 if (req_cdclk != dev_priv->cdclk_freq) {
738c05c0
ID
6001 /*
6002 * FIXME: We can end up here with all power domains off, yet
6003 * with a CDCLK frequency other than the minimum. To account
6004 * for this take the PIPE-A power domain, which covers the HW
6005 * blocks needed for the following programming. This can be
6006 * removed once it's guaranteed that we get here either with
6007 * the minimum CDCLK set, or the required power domains
6008 * enabled.
6009 */
6010 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
6011
383c5a6a
VS
6012 if (IS_CHERRYVIEW(dev))
6013 cherryview_set_cdclk(dev, req_cdclk);
6014 else
6015 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 6016
1e69cd74
VS
6017 vlv_program_pfi_credits(dev_priv);
6018
738c05c0 6019 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
383c5a6a 6020 }
30a970c6
JB
6021}
6022
89b667f8
JB
6023static void valleyview_crtc_enable(struct drm_crtc *crtc)
6024{
6025 struct drm_device *dev = crtc->dev;
a72e4c9f 6026 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
6027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6028 struct intel_encoder *encoder;
6029 int pipe = intel_crtc->pipe;
23538ef1 6030 bool is_dsi;
89b667f8 6031
53d9f4e9 6032 if (WARN_ON(intel_crtc->active))
89b667f8
JB
6033 return;
6034
409ee761 6035 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 6036
1ae0d137
VS
6037 if (!is_dsi) {
6038 if (IS_CHERRYVIEW(dev))
6e3c9717 6039 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 6040 else
6e3c9717 6041 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 6042 }
5b18e57c 6043
6e3c9717 6044 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6045 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6046
6047 intel_set_pipe_timings(intel_crtc);
6048
c14b0485
VS
6049 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6050 struct drm_i915_private *dev_priv = dev->dev_private;
6051
6052 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6053 I915_WRITE(CHV_CANVAS(pipe), 0);
6054 }
6055
5b18e57c
DV
6056 i9xx_set_pipeconf(intel_crtc);
6057
89b667f8 6058 intel_crtc->active = true;
89b667f8 6059
a72e4c9f 6060 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6061
89b667f8
JB
6062 for_each_encoder_on_crtc(dev, crtc, encoder)
6063 if (encoder->pre_pll_enable)
6064 encoder->pre_pll_enable(encoder);
6065
9d556c99
CML
6066 if (!is_dsi) {
6067 if (IS_CHERRYVIEW(dev))
6e3c9717 6068 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6069 else
6e3c9717 6070 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6071 }
89b667f8
JB
6072
6073 for_each_encoder_on_crtc(dev, crtc, encoder)
6074 if (encoder->pre_enable)
6075 encoder->pre_enable(encoder);
6076
2dd24552
JB
6077 i9xx_pfit_enable(intel_crtc);
6078
63cbb074
VS
6079 intel_crtc_load_lut(crtc);
6080
f37fcc2a 6081 intel_update_watermarks(crtc);
e1fdc473 6082 intel_enable_pipe(intel_crtc);
be6a6f8e 6083
4b3a9526
VS
6084 assert_vblank_disabled(crtc);
6085 drm_crtc_vblank_on(crtc);
6086
f9b61ff6
DV
6087 for_each_encoder_on_crtc(dev, crtc, encoder)
6088 encoder->enable(encoder);
89b667f8
JB
6089}
6090
f13c2ef3
DV
6091static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6092{
6093 struct drm_device *dev = crtc->base.dev;
6094 struct drm_i915_private *dev_priv = dev->dev_private;
6095
6e3c9717
ACO
6096 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6097 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6098}
6099
0b8765c6 6100static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6101{
6102 struct drm_device *dev = crtc->dev;
a72e4c9f 6103 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6104 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6105 struct intel_encoder *encoder;
79e53945 6106 int pipe = intel_crtc->pipe;
79e53945 6107
53d9f4e9 6108 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6109 return;
6110
f13c2ef3
DV
6111 i9xx_set_pll_dividers(intel_crtc);
6112
6e3c9717 6113 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6114 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6115
6116 intel_set_pipe_timings(intel_crtc);
6117
5b18e57c
DV
6118 i9xx_set_pipeconf(intel_crtc);
6119
f7abfe8b 6120 intel_crtc->active = true;
6b383a7f 6121
4a3436e8 6122 if (!IS_GEN2(dev))
a72e4c9f 6123 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6124
9d6d9f19
MK
6125 for_each_encoder_on_crtc(dev, crtc, encoder)
6126 if (encoder->pre_enable)
6127 encoder->pre_enable(encoder);
6128
f6736a1a
DV
6129 i9xx_enable_pll(intel_crtc);
6130
2dd24552
JB
6131 i9xx_pfit_enable(intel_crtc);
6132
63cbb074
VS
6133 intel_crtc_load_lut(crtc);
6134
f37fcc2a 6135 intel_update_watermarks(crtc);
e1fdc473 6136 intel_enable_pipe(intel_crtc);
be6a6f8e 6137
4b3a9526
VS
6138 assert_vblank_disabled(crtc);
6139 drm_crtc_vblank_on(crtc);
6140
f9b61ff6
DV
6141 for_each_encoder_on_crtc(dev, crtc, encoder)
6142 encoder->enable(encoder);
0b8765c6 6143}
79e53945 6144
87476d63
DV
6145static void i9xx_pfit_disable(struct intel_crtc *crtc)
6146{
6147 struct drm_device *dev = crtc->base.dev;
6148 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6149
6e3c9717 6150 if (!crtc->config->gmch_pfit.control)
328d8e82 6151 return;
87476d63 6152
328d8e82 6153 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6154
328d8e82
DV
6155 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6156 I915_READ(PFIT_CONTROL));
6157 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6158}
6159
0b8765c6
JB
6160static void i9xx_crtc_disable(struct drm_crtc *crtc)
6161{
6162 struct drm_device *dev = crtc->dev;
6163 struct drm_i915_private *dev_priv = dev->dev_private;
6164 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6165 struct intel_encoder *encoder;
0b8765c6 6166 int pipe = intel_crtc->pipe;
ef9c3aee 6167
53d9f4e9 6168 if (WARN_ON(!intel_crtc->active))
f7abfe8b
CW
6169 return;
6170
6304cd91
VS
6171 /*
6172 * On gen2 planes are double buffered but the pipe isn't, so we must
6173 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6174 * We also need to wait on all gmch platforms because of the
6175 * self-refresh mode constraint explained above.
6304cd91 6176 */
564ed191 6177 intel_wait_for_vblank(dev, pipe);
6304cd91 6178
4b3a9526
VS
6179 for_each_encoder_on_crtc(dev, crtc, encoder)
6180 encoder->disable(encoder);
6181
f9b61ff6
DV
6182 drm_crtc_vblank_off(crtc);
6183 assert_vblank_disabled(crtc);
6184
575f7ab7 6185 intel_disable_pipe(intel_crtc);
24a1f16d 6186
87476d63 6187 i9xx_pfit_disable(intel_crtc);
24a1f16d 6188
89b667f8
JB
6189 for_each_encoder_on_crtc(dev, crtc, encoder)
6190 if (encoder->post_disable)
6191 encoder->post_disable(encoder);
6192
409ee761 6193 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6194 if (IS_CHERRYVIEW(dev))
6195 chv_disable_pll(dev_priv, pipe);
6196 else if (IS_VALLEYVIEW(dev))
6197 vlv_disable_pll(dev_priv, pipe);
6198 else
1c4e0274 6199 i9xx_disable_pll(intel_crtc);
076ed3b2 6200 }
0b8765c6 6201
4a3436e8 6202 if (!IS_GEN2(dev))
a72e4c9f 6203 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 6204
f7abfe8b 6205 intel_crtc->active = false;
46ba614c 6206 intel_update_watermarks(crtc);
f37fcc2a 6207
efa9624e 6208 mutex_lock(&dev->struct_mutex);
7ff0ebcc 6209 intel_fbc_update(dev);
efa9624e 6210 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
6211}
6212
6b72d486
ML
6213/*
6214 * turn all crtc's off, but do not adjust state
6215 * This has to be paired with a call to intel_modeset_setup_hw_state.
6216 */
06ea0b08 6217int intel_display_suspend(struct drm_device *dev)
6b72d486 6218{
06ea0b08
ML
6219 struct drm_mode_config *config = &dev->mode_config;
6220 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6221 struct drm_atomic_state *state;
6b72d486 6222 struct drm_crtc *crtc;
06ea0b08
ML
6223 unsigned crtc_mask = 0;
6224 int ret = 0;
6225
6226 if (WARN_ON(!ctx))
6227 return 0;
6228
6229 lockdep_assert_held(&ctx->ww_ctx);
6230 state = drm_atomic_state_alloc(dev);
6231 if (WARN_ON(!state))
6232 return -ENOMEM;
6233
6234 state->acquire_ctx = ctx;
6235 state->allow_modeset = true;
6b72d486
ML
6236
6237 for_each_crtc(dev, crtc) {
06ea0b08
ML
6238 struct drm_crtc_state *crtc_state =
6239 drm_atomic_get_crtc_state(state, crtc);
6b72d486 6240
06ea0b08
ML
6241 ret = PTR_ERR_OR_ZERO(crtc_state);
6242 if (ret)
6243 goto free;
6244
6245 if (!crtc_state->active)
6b72d486
ML
6246 continue;
6247
06ea0b08
ML
6248 crtc_state->active = false;
6249 crtc_mask |= 1 << drm_crtc_index(crtc);
6250 }
6b72d486 6251
06ea0b08
ML
6252 if (crtc_mask) {
6253 ret = intel_set_mode(state);
6254
6255 if (!ret) {
6256 for_each_crtc(dev, crtc)
6257 if (crtc_mask & (1 << drm_crtc_index(crtc)))
6258 crtc->state->active = true;
6259
6260 return ret;
6261 }
6b72d486 6262 }
06ea0b08
ML
6263
6264free:
6265 if (ret)
6266 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6267 drm_atomic_state_free(state);
6268 return ret;
6b72d486
ML
6269}
6270
b04c5bd6 6271/* Master function to enable/disable CRTC and corresponding power wells */
5da76e94 6272int intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
6273{
6274 struct drm_device *dev = crtc->dev;
5da76e94
ML
6275 struct drm_mode_config *config = &dev->mode_config;
6276 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
0e572fe7 6277 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5da76e94
ML
6278 struct intel_crtc_state *pipe_config;
6279 struct drm_atomic_state *state;
6280 int ret;
976f8a20 6281
1b509259 6282 if (enable == intel_crtc->active)
5da76e94 6283 return 0;
1b509259
ML
6284
6285 if (enable && !crtc->state->enable)
5da76e94 6286 return 0;
1b509259 6287
5da76e94
ML
6288 /* this function should be called with drm_modeset_lock_all for now */
6289 if (WARN_ON(!ctx))
6290 return -EIO;
6291 lockdep_assert_held(&ctx->ww_ctx);
1b509259 6292
5da76e94
ML
6293 state = drm_atomic_state_alloc(dev);
6294 if (WARN_ON(!state))
6295 return -ENOMEM;
1b509259 6296
5da76e94
ML
6297 state->acquire_ctx = ctx;
6298 state->allow_modeset = true;
6299
6300 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6301 if (IS_ERR(pipe_config)) {
6302 ret = PTR_ERR(pipe_config);
6303 goto err;
0e572fe7 6304 }
5da76e94
ML
6305 pipe_config->base.active = enable;
6306
6307 ret = intel_set_mode(state);
6308 if (!ret)
6309 return ret;
6310
6311err:
6312 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6313 drm_atomic_state_free(state);
6314 return ret;
b04c5bd6
BF
6315}
6316
6317/**
6318 * Sets the power management mode of the pipe and plane.
6319 */
6320void intel_crtc_update_dpms(struct drm_crtc *crtc)
6321{
6322 struct drm_device *dev = crtc->dev;
6323 struct intel_encoder *intel_encoder;
6324 bool enable = false;
6325
6326 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6327 enable |= intel_encoder->connectors_active;
6328
6329 intel_crtc_control(crtc, enable);
976f8a20
DV
6330}
6331
ea5b213a 6332void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6333{
4ef69c7a 6334 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6335
ea5b213a
CW
6336 drm_encoder_cleanup(encoder);
6337 kfree(intel_encoder);
7e7d76c3
JB
6338}
6339
9237329d 6340/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
6341 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6342 * state of the entire output pipe. */
9237329d 6343static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 6344{
5ab432ef
DV
6345 if (mode == DRM_MODE_DPMS_ON) {
6346 encoder->connectors_active = true;
6347
b2cabb0e 6348 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
6349 } else {
6350 encoder->connectors_active = false;
6351
b2cabb0e 6352 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 6353 }
79e53945
JB
6354}
6355
0a91ca29
DV
6356/* Cross check the actual hw state with our own modeset state tracking (and it's
6357 * internal consistency). */
b980514c 6358static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6359{
0a91ca29
DV
6360 if (connector->get_hw_state(connector)) {
6361 struct intel_encoder *encoder = connector->encoder;
6362 struct drm_crtc *crtc;
6363 bool encoder_enabled;
6364 enum pipe pipe;
6365
6366 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6367 connector->base.base.id,
c23cc417 6368 connector->base.name);
0a91ca29 6369
0e32b39c
DA
6370 /* there is no real hw state for MST connectors */
6371 if (connector->mst_port)
6372 return;
6373
e2c719b7 6374 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 6375 "wrong connector dpms state\n");
e2c719b7 6376 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 6377 "active connector not linked to encoder\n");
0a91ca29 6378
36cd7444 6379 if (encoder) {
e2c719b7 6380 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
6381 "encoder->connectors_active not set\n");
6382
6383 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
6384 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6385 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 6386 return;
0a91ca29 6387
36cd7444 6388 crtc = encoder->base.crtc;
0a91ca29 6389
83d65738
MR
6390 I915_STATE_WARN(!crtc->state->enable,
6391 "crtc not enabled\n");
e2c719b7
RC
6392 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6393 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
6394 "encoder active on the wrong pipe\n");
6395 }
0a91ca29 6396 }
79e53945
JB
6397}
6398
08d9bc92
ACO
6399int intel_connector_init(struct intel_connector *connector)
6400{
6401 struct drm_connector_state *connector_state;
6402
6403 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6404 if (!connector_state)
6405 return -ENOMEM;
6406
6407 connector->base.state = connector_state;
6408 return 0;
6409}
6410
6411struct intel_connector *intel_connector_alloc(void)
6412{
6413 struct intel_connector *connector;
6414
6415 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6416 if (!connector)
6417 return NULL;
6418
6419 if (intel_connector_init(connector) < 0) {
6420 kfree(connector);
6421 return NULL;
6422 }
6423
6424 return connector;
6425}
6426
5ab432ef
DV
6427/* Even simpler default implementation, if there's really no special case to
6428 * consider. */
6429void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 6430{
5ab432ef
DV
6431 /* All the simple cases only support two dpms states. */
6432 if (mode != DRM_MODE_DPMS_ON)
6433 mode = DRM_MODE_DPMS_OFF;
d4270e57 6434
5ab432ef
DV
6435 if (mode == connector->dpms)
6436 return;
6437
6438 connector->dpms = mode;
6439
6440 /* Only need to change hw state when actually enabled */
c9976dcf
CW
6441 if (connector->encoder)
6442 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 6443
b980514c 6444 intel_modeset_check_state(connector->dev);
79e53945
JB
6445}
6446
f0947c37
DV
6447/* Simple connector->get_hw_state implementation for encoders that support only
6448 * one connector and no cloning and hence the encoder state determines the state
6449 * of the connector. */
6450bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6451{
24929352 6452 enum pipe pipe = 0;
f0947c37 6453 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6454
f0947c37 6455 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6456}
6457
6d293983 6458static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6459{
6d293983
ACO
6460 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6461 return crtc_state->fdi_lanes;
d272ddfa
VS
6462
6463 return 0;
6464}
6465
6d293983 6466static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6467 struct intel_crtc_state *pipe_config)
1857e1da 6468{
6d293983
ACO
6469 struct drm_atomic_state *state = pipe_config->base.state;
6470 struct intel_crtc *other_crtc;
6471 struct intel_crtc_state *other_crtc_state;
6472
1857e1da
DV
6473 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6474 pipe_name(pipe), pipe_config->fdi_lanes);
6475 if (pipe_config->fdi_lanes > 4) {
6476 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6477 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6478 return -EINVAL;
1857e1da
DV
6479 }
6480
bafb6553 6481 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6482 if (pipe_config->fdi_lanes > 2) {
6483 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6484 pipe_config->fdi_lanes);
6d293983 6485 return -EINVAL;
1857e1da 6486 } else {
6d293983 6487 return 0;
1857e1da
DV
6488 }
6489 }
6490
6491 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6492 return 0;
1857e1da
DV
6493
6494 /* Ivybridge 3 pipe is really complicated */
6495 switch (pipe) {
6496 case PIPE_A:
6d293983 6497 return 0;
1857e1da 6498 case PIPE_B:
6d293983
ACO
6499 if (pipe_config->fdi_lanes <= 2)
6500 return 0;
6501
6502 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6503 other_crtc_state =
6504 intel_atomic_get_crtc_state(state, other_crtc);
6505 if (IS_ERR(other_crtc_state))
6506 return PTR_ERR(other_crtc_state);
6507
6508 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6509 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6510 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6511 return -EINVAL;
1857e1da 6512 }
6d293983 6513 return 0;
1857e1da 6514 case PIPE_C:
251cc67c
VS
6515 if (pipe_config->fdi_lanes > 2) {
6516 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6517 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6518 return -EINVAL;
251cc67c 6519 }
6d293983
ACO
6520
6521 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6522 other_crtc_state =
6523 intel_atomic_get_crtc_state(state, other_crtc);
6524 if (IS_ERR(other_crtc_state))
6525 return PTR_ERR(other_crtc_state);
6526
6527 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6528 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6529 return -EINVAL;
1857e1da 6530 }
6d293983 6531 return 0;
1857e1da
DV
6532 default:
6533 BUG();
6534 }
6535}
6536
e29c22c0
DV
6537#define RETRY 1
6538static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6539 struct intel_crtc_state *pipe_config)
877d48d5 6540{
1857e1da 6541 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 6542 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6543 int lane, link_bw, fdi_dotclock, ret;
6544 bool needs_recompute = false;
877d48d5 6545
e29c22c0 6546retry:
877d48d5
DV
6547 /* FDI is a binary signal running at ~2.7GHz, encoding
6548 * each output octet as 10 bits. The actual frequency
6549 * is stored as a divider into a 100MHz clock, and the
6550 * mode pixel clock is stored in units of 1KHz.
6551 * Hence the bw of each lane in terms of the mode signal
6552 * is:
6553 */
6554 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6555
241bfc38 6556 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6557
2bd89a07 6558 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6559 pipe_config->pipe_bpp);
6560
6561 pipe_config->fdi_lanes = lane;
6562
2bd89a07 6563 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6564 link_bw, &pipe_config->fdi_m_n);
1857e1da 6565
6d293983
ACO
6566 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6567 intel_crtc->pipe, pipe_config);
6568 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6569 pipe_config->pipe_bpp -= 2*3;
6570 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6571 pipe_config->pipe_bpp);
6572 needs_recompute = true;
6573 pipe_config->bw_constrained = true;
6574
6575 goto retry;
6576 }
6577
6578 if (needs_recompute)
6579 return RETRY;
6580
6d293983 6581 return ret;
877d48d5
DV
6582}
6583
8cfb3407
VS
6584static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6585 struct intel_crtc_state *pipe_config)
6586{
6587 if (pipe_config->pipe_bpp > 24)
6588 return false;
6589
6590 /* HSW can handle pixel rate up to cdclk? */
6591 if (IS_HASWELL(dev_priv->dev))
6592 return true;
6593
6594 /*
b432e5cf
VS
6595 * We compare against max which means we must take
6596 * the increased cdclk requirement into account when
6597 * calculating the new cdclk.
6598 *
6599 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6600 */
6601 return ilk_pipe_pixel_rate(pipe_config) <=
6602 dev_priv->max_cdclk_freq * 95 / 100;
6603}
6604
42db64ef 6605static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6606 struct intel_crtc_state *pipe_config)
42db64ef 6607{
8cfb3407
VS
6608 struct drm_device *dev = crtc->base.dev;
6609 struct drm_i915_private *dev_priv = dev->dev_private;
6610
d330a953 6611 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6612 hsw_crtc_supports_ips(crtc) &&
6613 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6614}
6615
a43f6e0f 6616static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6617 struct intel_crtc_state *pipe_config)
79e53945 6618{
a43f6e0f 6619 struct drm_device *dev = crtc->base.dev;
8bd31e67 6620 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 6621 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
d03c93d4 6622 int ret;
89749350 6623
ad3a4479 6624 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6625 if (INTEL_INFO(dev)->gen < 4) {
44913155 6626 int clock_limit = dev_priv->max_cdclk_freq;
cf532bb2
VS
6627
6628 /*
6629 * Enable pixel doubling when the dot clock
6630 * is > 90% of the (display) core speed.
6631 *
b397c96b
VS
6632 * GDG double wide on either pipe,
6633 * otherwise pipe A only.
cf532bb2 6634 */
b397c96b 6635 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 6636 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 6637 clock_limit *= 2;
cf532bb2 6638 pipe_config->double_wide = true;
ad3a4479
VS
6639 }
6640
241bfc38 6641 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 6642 return -EINVAL;
2c07245f 6643 }
89749350 6644
1d1d0e27
VS
6645 /*
6646 * Pipe horizontal size must be even in:
6647 * - DVO ganged mode
6648 * - LVDS dual channel mode
6649 * - Double wide pipe
6650 */
a93e255f 6651 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6652 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6653 pipe_config->pipe_src_w &= ~1;
6654
8693a824
DL
6655 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6656 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6657 */
6658 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6659 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 6660 return -EINVAL;
44f46b42 6661
f5adf94e 6662 if (HAS_IPS(dev))
a43f6e0f
DV
6663 hsw_compute_ips_config(crtc, pipe_config);
6664
877d48d5 6665 if (pipe_config->has_pch_encoder)
a43f6e0f 6666 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6667
d03c93d4
CK
6668 /* FIXME: remove below call once atomic mode set is place and all crtc
6669 * related checks called from atomic_crtc_check function */
6670 ret = 0;
6671 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6672 crtc, pipe_config->base.state);
6673 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6674
6675 return ret;
79e53945
JB
6676}
6677
1652d19e
VS
6678static int skylake_get_display_clock_speed(struct drm_device *dev)
6679{
6680 struct drm_i915_private *dev_priv = to_i915(dev);
6681 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6682 uint32_t cdctl = I915_READ(CDCLK_CTL);
6683 uint32_t linkrate;
6684
414355a7 6685 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6686 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6687
6688 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6689 return 540000;
6690
6691 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6692 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6693
71cd8423
DL
6694 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6695 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6696 /* vco 8640 */
6697 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6698 case CDCLK_FREQ_450_432:
6699 return 432000;
6700 case CDCLK_FREQ_337_308:
6701 return 308570;
6702 case CDCLK_FREQ_675_617:
6703 return 617140;
6704 default:
6705 WARN(1, "Unknown cd freq selection\n");
6706 }
6707 } else {
6708 /* vco 8100 */
6709 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6710 case CDCLK_FREQ_450_432:
6711 return 450000;
6712 case CDCLK_FREQ_337_308:
6713 return 337500;
6714 case CDCLK_FREQ_675_617:
6715 return 675000;
6716 default:
6717 WARN(1, "Unknown cd freq selection\n");
6718 }
6719 }
6720
6721 /* error case, do as if DPLL0 isn't enabled */
6722 return 24000;
6723}
6724
6725static int broadwell_get_display_clock_speed(struct drm_device *dev)
6726{
6727 struct drm_i915_private *dev_priv = dev->dev_private;
6728 uint32_t lcpll = I915_READ(LCPLL_CTL);
6729 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6730
6731 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6732 return 800000;
6733 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6734 return 450000;
6735 else if (freq == LCPLL_CLK_FREQ_450)
6736 return 450000;
6737 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6738 return 540000;
6739 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6740 return 337500;
6741 else
6742 return 675000;
6743}
6744
6745static int haswell_get_display_clock_speed(struct drm_device *dev)
6746{
6747 struct drm_i915_private *dev_priv = dev->dev_private;
6748 uint32_t lcpll = I915_READ(LCPLL_CTL);
6749 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6750
6751 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6752 return 800000;
6753 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6754 return 450000;
6755 else if (freq == LCPLL_CLK_FREQ_450)
6756 return 450000;
6757 else if (IS_HSW_ULT(dev))
6758 return 337500;
6759 else
6760 return 540000;
79e53945
JB
6761}
6762
25eb05fc
JB
6763static int valleyview_get_display_clock_speed(struct drm_device *dev)
6764{
d197b7d3 6765 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
6766 u32 val;
6767 int divider;
6768
6bcda4f0
VS
6769 if (dev_priv->hpll_freq == 0)
6770 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6771
a580516d 6772 mutex_lock(&dev_priv->sb_lock);
d197b7d3 6773 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
a580516d 6774 mutex_unlock(&dev_priv->sb_lock);
d197b7d3
VS
6775
6776 divider = val & DISPLAY_FREQUENCY_VALUES;
6777
7d007f40
VS
6778 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6779 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6780 "cdclk change in progress\n");
6781
6bcda4f0 6782 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
6783}
6784
b37a6434
VS
6785static int ilk_get_display_clock_speed(struct drm_device *dev)
6786{
6787 return 450000;
6788}
6789
e70236a8
JB
6790static int i945_get_display_clock_speed(struct drm_device *dev)
6791{
6792 return 400000;
6793}
79e53945 6794
e70236a8 6795static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6796{
e907f170 6797 return 333333;
e70236a8 6798}
79e53945 6799
e70236a8
JB
6800static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6801{
6802 return 200000;
6803}
79e53945 6804
257a7ffc
DV
6805static int pnv_get_display_clock_speed(struct drm_device *dev)
6806{
6807 u16 gcfgc = 0;
6808
6809 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6810
6811 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6812 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6813 return 266667;
257a7ffc 6814 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6815 return 333333;
257a7ffc 6816 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6817 return 444444;
257a7ffc
DV
6818 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6819 return 200000;
6820 default:
6821 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6822 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6823 return 133333;
257a7ffc 6824 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6825 return 166667;
257a7ffc
DV
6826 }
6827}
6828
e70236a8
JB
6829static int i915gm_get_display_clock_speed(struct drm_device *dev)
6830{
6831 u16 gcfgc = 0;
79e53945 6832
e70236a8
JB
6833 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6834
6835 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6836 return 133333;
e70236a8
JB
6837 else {
6838 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6839 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6840 return 333333;
e70236a8
JB
6841 default:
6842 case GC_DISPLAY_CLOCK_190_200_MHZ:
6843 return 190000;
79e53945 6844 }
e70236a8
JB
6845 }
6846}
6847
6848static int i865_get_display_clock_speed(struct drm_device *dev)
6849{
e907f170 6850 return 266667;
e70236a8
JB
6851}
6852
1b1d2716 6853static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6854{
6855 u16 hpllcc = 0;
1b1d2716 6856
65cd2b3f
VS
6857 /*
6858 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6859 * encoding is different :(
6860 * FIXME is this the right way to detect 852GM/852GMV?
6861 */
6862 if (dev->pdev->revision == 0x1)
6863 return 133333;
6864
1b1d2716
VS
6865 pci_bus_read_config_word(dev->pdev->bus,
6866 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6867
e70236a8
JB
6868 /* Assume that the hardware is in the high speed state. This
6869 * should be the default.
6870 */
6871 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6872 case GC_CLOCK_133_200:
1b1d2716 6873 case GC_CLOCK_133_200_2:
e70236a8
JB
6874 case GC_CLOCK_100_200:
6875 return 200000;
6876 case GC_CLOCK_166_250:
6877 return 250000;
6878 case GC_CLOCK_100_133:
e907f170 6879 return 133333;
1b1d2716
VS
6880 case GC_CLOCK_133_266:
6881 case GC_CLOCK_133_266_2:
6882 case GC_CLOCK_166_266:
6883 return 266667;
e70236a8 6884 }
79e53945 6885
e70236a8
JB
6886 /* Shouldn't happen */
6887 return 0;
6888}
79e53945 6889
e70236a8
JB
6890static int i830_get_display_clock_speed(struct drm_device *dev)
6891{
e907f170 6892 return 133333;
79e53945
JB
6893}
6894
34edce2f
VS
6895static unsigned int intel_hpll_vco(struct drm_device *dev)
6896{
6897 struct drm_i915_private *dev_priv = dev->dev_private;
6898 static const unsigned int blb_vco[8] = {
6899 [0] = 3200000,
6900 [1] = 4000000,
6901 [2] = 5333333,
6902 [3] = 4800000,
6903 [4] = 6400000,
6904 };
6905 static const unsigned int pnv_vco[8] = {
6906 [0] = 3200000,
6907 [1] = 4000000,
6908 [2] = 5333333,
6909 [3] = 4800000,
6910 [4] = 2666667,
6911 };
6912 static const unsigned int cl_vco[8] = {
6913 [0] = 3200000,
6914 [1] = 4000000,
6915 [2] = 5333333,
6916 [3] = 6400000,
6917 [4] = 3333333,
6918 [5] = 3566667,
6919 [6] = 4266667,
6920 };
6921 static const unsigned int elk_vco[8] = {
6922 [0] = 3200000,
6923 [1] = 4000000,
6924 [2] = 5333333,
6925 [3] = 4800000,
6926 };
6927 static const unsigned int ctg_vco[8] = {
6928 [0] = 3200000,
6929 [1] = 4000000,
6930 [2] = 5333333,
6931 [3] = 6400000,
6932 [4] = 2666667,
6933 [5] = 4266667,
6934 };
6935 const unsigned int *vco_table;
6936 unsigned int vco;
6937 uint8_t tmp = 0;
6938
6939 /* FIXME other chipsets? */
6940 if (IS_GM45(dev))
6941 vco_table = ctg_vco;
6942 else if (IS_G4X(dev))
6943 vco_table = elk_vco;
6944 else if (IS_CRESTLINE(dev))
6945 vco_table = cl_vco;
6946 else if (IS_PINEVIEW(dev))
6947 vco_table = pnv_vco;
6948 else if (IS_G33(dev))
6949 vco_table = blb_vco;
6950 else
6951 return 0;
6952
6953 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6954
6955 vco = vco_table[tmp & 0x7];
6956 if (vco == 0)
6957 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6958 else
6959 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6960
6961 return vco;
6962}
6963
6964static int gm45_get_display_clock_speed(struct drm_device *dev)
6965{
6966 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6967 uint16_t tmp = 0;
6968
6969 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6970
6971 cdclk_sel = (tmp >> 12) & 0x1;
6972
6973 switch (vco) {
6974 case 2666667:
6975 case 4000000:
6976 case 5333333:
6977 return cdclk_sel ? 333333 : 222222;
6978 case 3200000:
6979 return cdclk_sel ? 320000 : 228571;
6980 default:
6981 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6982 return 222222;
6983 }
6984}
6985
6986static int i965gm_get_display_clock_speed(struct drm_device *dev)
6987{
6988 static const uint8_t div_3200[] = { 16, 10, 8 };
6989 static const uint8_t div_4000[] = { 20, 12, 10 };
6990 static const uint8_t div_5333[] = { 24, 16, 14 };
6991 const uint8_t *div_table;
6992 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6993 uint16_t tmp = 0;
6994
6995 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6996
6997 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6998
6999 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7000 goto fail;
7001
7002 switch (vco) {
7003 case 3200000:
7004 div_table = div_3200;
7005 break;
7006 case 4000000:
7007 div_table = div_4000;
7008 break;
7009 case 5333333:
7010 div_table = div_5333;
7011 break;
7012 default:
7013 goto fail;
7014 }
7015
7016 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7017
7018 fail:
7019 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7020 return 200000;
7021}
7022
7023static int g33_get_display_clock_speed(struct drm_device *dev)
7024{
7025 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7026 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7027 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7028 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7029 const uint8_t *div_table;
7030 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7031 uint16_t tmp = 0;
7032
7033 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7034
7035 cdclk_sel = (tmp >> 4) & 0x7;
7036
7037 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7038 goto fail;
7039
7040 switch (vco) {
7041 case 3200000:
7042 div_table = div_3200;
7043 break;
7044 case 4000000:
7045 div_table = div_4000;
7046 break;
7047 case 4800000:
7048 div_table = div_4800;
7049 break;
7050 case 5333333:
7051 div_table = div_5333;
7052 break;
7053 default:
7054 goto fail;
7055 }
7056
7057 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7058
7059 fail:
7060 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7061 return 190476;
7062}
7063
2c07245f 7064static void
a65851af 7065intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 7066{
a65851af
VS
7067 while (*num > DATA_LINK_M_N_MASK ||
7068 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
7069 *num >>= 1;
7070 *den >>= 1;
7071 }
7072}
7073
a65851af
VS
7074static void compute_m_n(unsigned int m, unsigned int n,
7075 uint32_t *ret_m, uint32_t *ret_n)
7076{
7077 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7078 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7079 intel_reduce_m_n_ratio(ret_m, ret_n);
7080}
7081
e69d0bc1
DV
7082void
7083intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7084 int pixel_clock, int link_clock,
7085 struct intel_link_m_n *m_n)
2c07245f 7086{
e69d0bc1 7087 m_n->tu = 64;
a65851af
VS
7088
7089 compute_m_n(bits_per_pixel * pixel_clock,
7090 link_clock * nlanes * 8,
7091 &m_n->gmch_m, &m_n->gmch_n);
7092
7093 compute_m_n(pixel_clock, link_clock,
7094 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
7095}
7096
a7615030
CW
7097static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7098{
d330a953
JN
7099 if (i915.panel_use_ssc >= 0)
7100 return i915.panel_use_ssc != 0;
41aa3448 7101 return dev_priv->vbt.lvds_use_ssc
435793df 7102 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
7103}
7104
a93e255f
ACO
7105static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7106 int num_connectors)
c65d77d8 7107{
a93e255f 7108 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
7109 struct drm_i915_private *dev_priv = dev->dev_private;
7110 int refclk;
7111
a93e255f
ACO
7112 WARN_ON(!crtc_state->base.state);
7113
5ab7b0b7 7114 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 7115 refclk = 100000;
a93e255f 7116 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 7117 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
7118 refclk = dev_priv->vbt.lvds_ssc_freq;
7119 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
7120 } else if (!IS_GEN2(dev)) {
7121 refclk = 96000;
7122 } else {
7123 refclk = 48000;
7124 }
7125
7126 return refclk;
7127}
7128
7429e9d4 7129static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7130{
7df00d7a 7131 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7132}
f47709a9 7133
7429e9d4
DV
7134static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7135{
7136 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7137}
7138
f47709a9 7139static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7140 struct intel_crtc_state *crtc_state,
a7516a05
JB
7141 intel_clock_t *reduced_clock)
7142{
f47709a9 7143 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7144 u32 fp, fp2 = 0;
7145
7146 if (IS_PINEVIEW(dev)) {
190f68c5 7147 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7148 if (reduced_clock)
7429e9d4 7149 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7150 } else {
190f68c5 7151 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7152 if (reduced_clock)
7429e9d4 7153 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7154 }
7155
190f68c5 7156 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7157
f47709a9 7158 crtc->lowfreq_avail = false;
a93e255f 7159 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7160 reduced_clock) {
190f68c5 7161 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7162 crtc->lowfreq_avail = true;
a7516a05 7163 } else {
190f68c5 7164 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7165 }
7166}
7167
5e69f97f
CML
7168static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7169 pipe)
89b667f8
JB
7170{
7171 u32 reg_val;
7172
7173 /*
7174 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7175 * and set it to a reasonable value instead.
7176 */
ab3c759a 7177 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7178 reg_val &= 0xffffff00;
7179 reg_val |= 0x00000030;
ab3c759a 7180 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7181
ab3c759a 7182 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7183 reg_val &= 0x8cffffff;
7184 reg_val = 0x8c000000;
ab3c759a 7185 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7186
ab3c759a 7187 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7188 reg_val &= 0xffffff00;
ab3c759a 7189 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7190
ab3c759a 7191 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7192 reg_val &= 0x00ffffff;
7193 reg_val |= 0xb0000000;
ab3c759a 7194 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7195}
7196
b551842d
DV
7197static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7198 struct intel_link_m_n *m_n)
7199{
7200 struct drm_device *dev = crtc->base.dev;
7201 struct drm_i915_private *dev_priv = dev->dev_private;
7202 int pipe = crtc->pipe;
7203
e3b95f1e
DV
7204 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7205 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7206 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7207 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7208}
7209
7210static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7211 struct intel_link_m_n *m_n,
7212 struct intel_link_m_n *m2_n2)
b551842d
DV
7213{
7214 struct drm_device *dev = crtc->base.dev;
7215 struct drm_i915_private *dev_priv = dev->dev_private;
7216 int pipe = crtc->pipe;
6e3c9717 7217 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7218
7219 if (INTEL_INFO(dev)->gen >= 5) {
7220 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7221 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7222 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7223 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7224 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7225 * for gen < 8) and if DRRS is supported (to make sure the
7226 * registers are not unnecessarily accessed).
7227 */
44395bfe 7228 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7229 crtc->config->has_drrs) {
f769cd24
VK
7230 I915_WRITE(PIPE_DATA_M2(transcoder),
7231 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7232 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7233 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7234 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7235 }
b551842d 7236 } else {
e3b95f1e
DV
7237 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7238 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7239 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7240 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7241 }
7242}
7243
fe3cd48d 7244void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7245{
fe3cd48d
R
7246 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7247
7248 if (m_n == M1_N1) {
7249 dp_m_n = &crtc->config->dp_m_n;
7250 dp_m2_n2 = &crtc->config->dp_m2_n2;
7251 } else if (m_n == M2_N2) {
7252
7253 /*
7254 * M2_N2 registers are not supported. Hence m2_n2 divider value
7255 * needs to be programmed into M1_N1.
7256 */
7257 dp_m_n = &crtc->config->dp_m2_n2;
7258 } else {
7259 DRM_ERROR("Unsupported divider value\n");
7260 return;
7261 }
7262
6e3c9717
ACO
7263 if (crtc->config->has_pch_encoder)
7264 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7265 else
fe3cd48d 7266 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7267}
7268
d288f65f 7269static void vlv_update_pll(struct intel_crtc *crtc,
5cec258b 7270 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7271{
7272 u32 dpll, dpll_md;
7273
7274 /*
7275 * Enable DPIO clock input. We should never disable the reference
7276 * clock for pipe B, since VGA hotplug / manual detection depends
7277 * on it.
7278 */
7279 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7280 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7281 /* We should never disable this, set it here for state tracking */
7282 if (crtc->pipe == PIPE_B)
7283 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7284 dpll |= DPLL_VCO_ENABLE;
d288f65f 7285 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7286
d288f65f 7287 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7288 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7289 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7290}
7291
d288f65f 7292static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7293 const struct intel_crtc_state *pipe_config)
a0c4da24 7294{
f47709a9 7295 struct drm_device *dev = crtc->base.dev;
a0c4da24 7296 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7297 int pipe = crtc->pipe;
bdd4b6a6 7298 u32 mdiv;
a0c4da24 7299 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7300 u32 coreclk, reg_val;
a0c4da24 7301
a580516d 7302 mutex_lock(&dev_priv->sb_lock);
09153000 7303
d288f65f
VS
7304 bestn = pipe_config->dpll.n;
7305 bestm1 = pipe_config->dpll.m1;
7306 bestm2 = pipe_config->dpll.m2;
7307 bestp1 = pipe_config->dpll.p1;
7308 bestp2 = pipe_config->dpll.p2;
a0c4da24 7309
89b667f8
JB
7310 /* See eDP HDMI DPIO driver vbios notes doc */
7311
7312 /* PLL B needs special handling */
bdd4b6a6 7313 if (pipe == PIPE_B)
5e69f97f 7314 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7315
7316 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7317 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7318
7319 /* Disable target IRef on PLL */
ab3c759a 7320 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7321 reg_val &= 0x00ffffff;
ab3c759a 7322 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7323
7324 /* Disable fast lock */
ab3c759a 7325 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7326
7327 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7328 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7329 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7330 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7331 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7332
7333 /*
7334 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7335 * but we don't support that).
7336 * Note: don't use the DAC post divider as it seems unstable.
7337 */
7338 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7339 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7340
a0c4da24 7341 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7342 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7343
89b667f8 7344 /* Set HBR and RBR LPF coefficients */
d288f65f 7345 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7346 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7347 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7348 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7349 0x009f0003);
89b667f8 7350 else
ab3c759a 7351 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7352 0x00d0000f);
7353
681a8504 7354 if (pipe_config->has_dp_encoder) {
89b667f8 7355 /* Use SSC source */
bdd4b6a6 7356 if (pipe == PIPE_A)
ab3c759a 7357 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7358 0x0df40000);
7359 else
ab3c759a 7360 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7361 0x0df70000);
7362 } else { /* HDMI or VGA */
7363 /* Use bend source */
bdd4b6a6 7364 if (pipe == PIPE_A)
ab3c759a 7365 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7366 0x0df70000);
7367 else
ab3c759a 7368 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7369 0x0df40000);
7370 }
a0c4da24 7371
ab3c759a 7372 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7373 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7374 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7375 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7376 coreclk |= 0x01000000;
ab3c759a 7377 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7378
ab3c759a 7379 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7380 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7381}
7382
d288f65f 7383static void chv_update_pll(struct intel_crtc *crtc,
5cec258b 7384 struct intel_crtc_state *pipe_config)
1ae0d137 7385{
d288f65f 7386 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
7387 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7388 DPLL_VCO_ENABLE;
7389 if (crtc->pipe != PIPE_A)
d288f65f 7390 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7391
d288f65f
VS
7392 pipe_config->dpll_hw_state.dpll_md =
7393 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7394}
7395
d288f65f 7396static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7397 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7398{
7399 struct drm_device *dev = crtc->base.dev;
7400 struct drm_i915_private *dev_priv = dev->dev_private;
7401 int pipe = crtc->pipe;
7402 int dpll_reg = DPLL(crtc->pipe);
7403 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7404 u32 loopfilter, tribuf_calcntr;
9d556c99 7405 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7406 u32 dpio_val;
9cbe40c1 7407 int vco;
9d556c99 7408
d288f65f
VS
7409 bestn = pipe_config->dpll.n;
7410 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7411 bestm1 = pipe_config->dpll.m1;
7412 bestm2 = pipe_config->dpll.m2 >> 22;
7413 bestp1 = pipe_config->dpll.p1;
7414 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7415 vco = pipe_config->dpll.vco;
a945ce7e 7416 dpio_val = 0;
9cbe40c1 7417 loopfilter = 0;
9d556c99
CML
7418
7419 /*
7420 * Enable Refclk and SSC
7421 */
a11b0703 7422 I915_WRITE(dpll_reg,
d288f65f 7423 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7424
a580516d 7425 mutex_lock(&dev_priv->sb_lock);
9d556c99 7426
9d556c99
CML
7427 /* p1 and p2 divider */
7428 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7429 5 << DPIO_CHV_S1_DIV_SHIFT |
7430 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7431 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7432 1 << DPIO_CHV_K_DIV_SHIFT);
7433
7434 /* Feedback post-divider - m2 */
7435 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7436
7437 /* Feedback refclk divider - n and m1 */
7438 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7439 DPIO_CHV_M1_DIV_BY_2 |
7440 1 << DPIO_CHV_N_DIV_SHIFT);
7441
7442 /* M2 fraction division */
a945ce7e
VP
7443 if (bestm2_frac)
7444 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7445
7446 /* M2 fraction division enable */
a945ce7e
VP
7447 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7448 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7449 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7450 if (bestm2_frac)
7451 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7452 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7453
de3a0fde
VP
7454 /* Program digital lock detect threshold */
7455 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7456 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7457 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7458 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7459 if (!bestm2_frac)
7460 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7461 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7462
9d556c99 7463 /* Loop filter */
9cbe40c1
VP
7464 if (vco == 5400000) {
7465 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7466 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7467 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7468 tribuf_calcntr = 0x9;
7469 } else if (vco <= 6200000) {
7470 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7471 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7472 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7473 tribuf_calcntr = 0x9;
7474 } else if (vco <= 6480000) {
7475 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7476 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7477 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7478 tribuf_calcntr = 0x8;
7479 } else {
7480 /* Not supported. Apply the same limits as in the max case */
7481 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7482 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7483 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7484 tribuf_calcntr = 0;
7485 }
9d556c99
CML
7486 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7487
968040b2 7488 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7489 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7490 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7491 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7492
9d556c99
CML
7493 /* AFC Recal */
7494 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7495 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7496 DPIO_AFC_RECAL);
7497
a580516d 7498 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7499}
7500
d288f65f
VS
7501/**
7502 * vlv_force_pll_on - forcibly enable just the PLL
7503 * @dev_priv: i915 private structure
7504 * @pipe: pipe PLL to enable
7505 * @dpll: PLL configuration
7506 *
7507 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7508 * in cases where we need the PLL enabled even when @pipe is not going to
7509 * be enabled.
7510 */
7511void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7512 const struct dpll *dpll)
7513{
7514 struct intel_crtc *crtc =
7515 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 7516 struct intel_crtc_state pipe_config = {
a93e255f 7517 .base.crtc = &crtc->base,
d288f65f
VS
7518 .pixel_multiplier = 1,
7519 .dpll = *dpll,
7520 };
7521
7522 if (IS_CHERRYVIEW(dev)) {
7523 chv_update_pll(crtc, &pipe_config);
7524 chv_prepare_pll(crtc, &pipe_config);
7525 chv_enable_pll(crtc, &pipe_config);
7526 } else {
7527 vlv_update_pll(crtc, &pipe_config);
7528 vlv_prepare_pll(crtc, &pipe_config);
7529 vlv_enable_pll(crtc, &pipe_config);
7530 }
7531}
7532
7533/**
7534 * vlv_force_pll_off - forcibly disable just the PLL
7535 * @dev_priv: i915 private structure
7536 * @pipe: pipe PLL to disable
7537 *
7538 * Disable the PLL for @pipe. To be used in cases where we need
7539 * the PLL enabled even when @pipe is not going to be enabled.
7540 */
7541void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7542{
7543 if (IS_CHERRYVIEW(dev))
7544 chv_disable_pll(to_i915(dev), pipe);
7545 else
7546 vlv_disable_pll(to_i915(dev), pipe);
7547}
7548
f47709a9 7549static void i9xx_update_pll(struct intel_crtc *crtc,
190f68c5 7550 struct intel_crtc_state *crtc_state,
f47709a9 7551 intel_clock_t *reduced_clock,
eb1cbe48
DV
7552 int num_connectors)
7553{
f47709a9 7554 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7555 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7556 u32 dpll;
7557 bool is_sdvo;
190f68c5 7558 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7559
190f68c5 7560 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7561
a93e255f
ACO
7562 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7563 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7564
7565 dpll = DPLL_VGA_MODE_DIS;
7566
a93e255f 7567 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7568 dpll |= DPLLB_MODE_LVDS;
7569 else
7570 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7571
ef1b460d 7572 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7573 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7574 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7575 }
198a037f
DV
7576
7577 if (is_sdvo)
4a33e48d 7578 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7579
190f68c5 7580 if (crtc_state->has_dp_encoder)
4a33e48d 7581 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7582
7583 /* compute bitmask from p1 value */
7584 if (IS_PINEVIEW(dev))
7585 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7586 else {
7587 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7588 if (IS_G4X(dev) && reduced_clock)
7589 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7590 }
7591 switch (clock->p2) {
7592 case 5:
7593 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7594 break;
7595 case 7:
7596 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7597 break;
7598 case 10:
7599 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7600 break;
7601 case 14:
7602 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7603 break;
7604 }
7605 if (INTEL_INFO(dev)->gen >= 4)
7606 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7607
190f68c5 7608 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7609 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7610 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7611 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7612 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7613 else
7614 dpll |= PLL_REF_INPUT_DREFCLK;
7615
7616 dpll |= DPLL_VCO_ENABLE;
190f68c5 7617 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7618
eb1cbe48 7619 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7620 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7621 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7622 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7623 }
7624}
7625
f47709a9 7626static void i8xx_update_pll(struct intel_crtc *crtc,
190f68c5 7627 struct intel_crtc_state *crtc_state,
f47709a9 7628 intel_clock_t *reduced_clock,
eb1cbe48
DV
7629 int num_connectors)
7630{
f47709a9 7631 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7632 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7633 u32 dpll;
190f68c5 7634 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7635
190f68c5 7636 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7637
eb1cbe48
DV
7638 dpll = DPLL_VGA_MODE_DIS;
7639
a93e255f 7640 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7641 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7642 } else {
7643 if (clock->p1 == 2)
7644 dpll |= PLL_P1_DIVIDE_BY_TWO;
7645 else
7646 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7647 if (clock->p2 == 4)
7648 dpll |= PLL_P2_DIVIDE_BY_4;
7649 }
7650
a93e255f 7651 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7652 dpll |= DPLL_DVO_2X_MODE;
7653
a93e255f 7654 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7655 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7656 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7657 else
7658 dpll |= PLL_REF_INPUT_DREFCLK;
7659
7660 dpll |= DPLL_VCO_ENABLE;
190f68c5 7661 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7662}
7663
8a654f3b 7664static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7665{
7666 struct drm_device *dev = intel_crtc->base.dev;
7667 struct drm_i915_private *dev_priv = dev->dev_private;
7668 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7669 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 7670 struct drm_display_mode *adjusted_mode =
6e3c9717 7671 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7672 uint32_t crtc_vtotal, crtc_vblank_end;
7673 int vsyncshift = 0;
4d8a62ea
DV
7674
7675 /* We need to be careful not to changed the adjusted mode, for otherwise
7676 * the hw state checker will get angry at the mismatch. */
7677 crtc_vtotal = adjusted_mode->crtc_vtotal;
7678 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7679
609aeaca 7680 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7681 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7682 crtc_vtotal -= 1;
7683 crtc_vblank_end -= 1;
609aeaca 7684
409ee761 7685 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7686 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7687 else
7688 vsyncshift = adjusted_mode->crtc_hsync_start -
7689 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7690 if (vsyncshift < 0)
7691 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7692 }
7693
7694 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7695 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7696
fe2b8f9d 7697 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7698 (adjusted_mode->crtc_hdisplay - 1) |
7699 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7700 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7701 (adjusted_mode->crtc_hblank_start - 1) |
7702 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7703 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7704 (adjusted_mode->crtc_hsync_start - 1) |
7705 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7706
fe2b8f9d 7707 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7708 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7709 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7710 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7711 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7712 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7713 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7714 (adjusted_mode->crtc_vsync_start - 1) |
7715 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7716
b5e508d4
PZ
7717 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7718 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7719 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7720 * bits. */
7721 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7722 (pipe == PIPE_B || pipe == PIPE_C))
7723 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7724
b0e77b9c
PZ
7725 /* pipesrc controls the size that is scaled from, which should
7726 * always be the user's requested size.
7727 */
7728 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7729 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7730 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7731}
7732
1bd1bd80 7733static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7734 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7735{
7736 struct drm_device *dev = crtc->base.dev;
7737 struct drm_i915_private *dev_priv = dev->dev_private;
7738 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7739 uint32_t tmp;
7740
7741 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7742 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7743 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7744 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7745 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7746 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7747 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7748 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7749 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7750
7751 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7752 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7753 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7754 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7755 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7756 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7757 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7758 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7759 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7760
7761 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7762 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7763 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7764 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7765 }
7766
7767 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7768 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7769 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7770
2d112de7
ACO
7771 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7772 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7773}
7774
f6a83288 7775void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7776 struct intel_crtc_state *pipe_config)
babea61d 7777{
2d112de7
ACO
7778 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7779 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7780 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7781 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7782
2d112de7
ACO
7783 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7784 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7785 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7786 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7787
2d112de7 7788 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 7789
2d112de7
ACO
7790 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7791 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
7792}
7793
84b046f3
DV
7794static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7795{
7796 struct drm_device *dev = intel_crtc->base.dev;
7797 struct drm_i915_private *dev_priv = dev->dev_private;
7798 uint32_t pipeconf;
7799
9f11a9e4 7800 pipeconf = 0;
84b046f3 7801
b6b5d049
VS
7802 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7803 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7804 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7805
6e3c9717 7806 if (intel_crtc->config->double_wide)
cf532bb2 7807 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7808
ff9ce46e
DV
7809 /* only g4x and later have fancy bpc/dither controls */
7810 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 7811 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7812 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7813 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7814 PIPECONF_DITHER_TYPE_SP;
84b046f3 7815
6e3c9717 7816 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7817 case 18:
7818 pipeconf |= PIPECONF_6BPC;
7819 break;
7820 case 24:
7821 pipeconf |= PIPECONF_8BPC;
7822 break;
7823 case 30:
7824 pipeconf |= PIPECONF_10BPC;
7825 break;
7826 default:
7827 /* Case prevented by intel_choose_pipe_bpp_dither. */
7828 BUG();
84b046f3
DV
7829 }
7830 }
7831
7832 if (HAS_PIPE_CXSR(dev)) {
7833 if (intel_crtc->lowfreq_avail) {
7834 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7835 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7836 } else {
7837 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7838 }
7839 }
7840
6e3c9717 7841 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7842 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7843 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7844 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7845 else
7846 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7847 } else
84b046f3
DV
7848 pipeconf |= PIPECONF_PROGRESSIVE;
7849
6e3c9717 7850 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 7851 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7852
84b046f3
DV
7853 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7854 POSTING_READ(PIPECONF(intel_crtc->pipe));
7855}
7856
190f68c5
ACO
7857static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7858 struct intel_crtc_state *crtc_state)
79e53945 7859{
c7653199 7860 struct drm_device *dev = crtc->base.dev;
79e53945 7861 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7862 int refclk, num_connectors = 0;
652c393a 7863 intel_clock_t clock, reduced_clock;
a16af721 7864 bool ok, has_reduced_clock = false;
e9fd1c02 7865 bool is_lvds = false, is_dsi = false;
5eddb70b 7866 struct intel_encoder *encoder;
d4906093 7867 const intel_limit_t *limit;
55bb9992 7868 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 7869 struct drm_connector *connector;
55bb9992
ACO
7870 struct drm_connector_state *connector_state;
7871 int i;
79e53945 7872
dd3cd74a
ACO
7873 memset(&crtc_state->dpll_hw_state, 0,
7874 sizeof(crtc_state->dpll_hw_state));
7875
da3ced29 7876 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
7877 if (connector_state->crtc != &crtc->base)
7878 continue;
7879
7880 encoder = to_intel_encoder(connector_state->best_encoder);
7881
5eddb70b 7882 switch (encoder->type) {
79e53945
JB
7883 case INTEL_OUTPUT_LVDS:
7884 is_lvds = true;
7885 break;
e9fd1c02
JN
7886 case INTEL_OUTPUT_DSI:
7887 is_dsi = true;
7888 break;
6847d71b
PZ
7889 default:
7890 break;
79e53945 7891 }
43565a06 7892
c751ce4f 7893 num_connectors++;
79e53945
JB
7894 }
7895
f2335330 7896 if (is_dsi)
5b18e57c 7897 return 0;
f2335330 7898
190f68c5 7899 if (!crtc_state->clock_set) {
a93e255f 7900 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7901
e9fd1c02
JN
7902 /*
7903 * Returns a set of divisors for the desired target clock with
7904 * the given refclk, or FALSE. The returned values represent
7905 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7906 * 2) / p1 / p2.
7907 */
a93e255f
ACO
7908 limit = intel_limit(crtc_state, refclk);
7909 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7910 crtc_state->port_clock,
e9fd1c02 7911 refclk, NULL, &clock);
f2335330 7912 if (!ok) {
e9fd1c02
JN
7913 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7914 return -EINVAL;
7915 }
79e53945 7916
f2335330
JN
7917 if (is_lvds && dev_priv->lvds_downclock_avail) {
7918 /*
7919 * Ensure we match the reduced clock's P to the target
7920 * clock. If the clocks don't match, we can't switch
7921 * the display clock by using the FP0/FP1. In such case
7922 * we will disable the LVDS downclock feature.
7923 */
7924 has_reduced_clock =
a93e255f 7925 dev_priv->display.find_dpll(limit, crtc_state,
f2335330
JN
7926 dev_priv->lvds_downclock,
7927 refclk, &clock,
7928 &reduced_clock);
7929 }
7930 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7931 crtc_state->dpll.n = clock.n;
7932 crtc_state->dpll.m1 = clock.m1;
7933 crtc_state->dpll.m2 = clock.m2;
7934 crtc_state->dpll.p1 = clock.p1;
7935 crtc_state->dpll.p2 = clock.p2;
f47709a9 7936 }
7026d4ac 7937
e9fd1c02 7938 if (IS_GEN2(dev)) {
190f68c5 7939 i8xx_update_pll(crtc, crtc_state,
2a8f64ca
VP
7940 has_reduced_clock ? &reduced_clock : NULL,
7941 num_connectors);
9d556c99 7942 } else if (IS_CHERRYVIEW(dev)) {
190f68c5 7943 chv_update_pll(crtc, crtc_state);
e9fd1c02 7944 } else if (IS_VALLEYVIEW(dev)) {
190f68c5 7945 vlv_update_pll(crtc, crtc_state);
e9fd1c02 7946 } else {
190f68c5 7947 i9xx_update_pll(crtc, crtc_state,
eb1cbe48 7948 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 7949 num_connectors);
e9fd1c02 7950 }
79e53945 7951
c8f7a0db 7952 return 0;
f564048e
EA
7953}
7954
2fa2fe9a 7955static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7956 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7957{
7958 struct drm_device *dev = crtc->base.dev;
7959 struct drm_i915_private *dev_priv = dev->dev_private;
7960 uint32_t tmp;
7961
dc9e7dec
VS
7962 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7963 return;
7964
2fa2fe9a 7965 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7966 if (!(tmp & PFIT_ENABLE))
7967 return;
2fa2fe9a 7968
06922821 7969 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7970 if (INTEL_INFO(dev)->gen < 4) {
7971 if (crtc->pipe != PIPE_B)
7972 return;
2fa2fe9a
DV
7973 } else {
7974 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7975 return;
7976 }
7977
06922821 7978 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7979 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7980 if (INTEL_INFO(dev)->gen < 5)
7981 pipe_config->gmch_pfit.lvds_border_bits =
7982 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7983}
7984
acbec814 7985static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7986 struct intel_crtc_state *pipe_config)
acbec814
JB
7987{
7988 struct drm_device *dev = crtc->base.dev;
7989 struct drm_i915_private *dev_priv = dev->dev_private;
7990 int pipe = pipe_config->cpu_transcoder;
7991 intel_clock_t clock;
7992 u32 mdiv;
662c6ecb 7993 int refclk = 100000;
acbec814 7994
f573de5a
SK
7995 /* In case of MIPI DPLL will not even be used */
7996 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7997 return;
7998
a580516d 7999 mutex_lock(&dev_priv->sb_lock);
ab3c759a 8000 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 8001 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
8002
8003 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8004 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8005 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8006 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8007 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8008
f646628b 8009 vlv_clock(refclk, &clock);
acbec814 8010
f646628b
VS
8011 /* clock.dot is the fast clock */
8012 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
8013}
8014
5724dbd1
DL
8015static void
8016i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8017 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
8018{
8019 struct drm_device *dev = crtc->base.dev;
8020 struct drm_i915_private *dev_priv = dev->dev_private;
8021 u32 val, base, offset;
8022 int pipe = crtc->pipe, plane = crtc->plane;
8023 int fourcc, pixel_format;
6761dd31 8024 unsigned int aligned_height;
b113d5ee 8025 struct drm_framebuffer *fb;
1b842c89 8026 struct intel_framebuffer *intel_fb;
1ad292b5 8027
42a7b088
DL
8028 val = I915_READ(DSPCNTR(plane));
8029 if (!(val & DISPLAY_PLANE_ENABLE))
8030 return;
8031
d9806c9f 8032 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8033 if (!intel_fb) {
1ad292b5
JB
8034 DRM_DEBUG_KMS("failed to alloc fb\n");
8035 return;
8036 }
8037
1b842c89
DL
8038 fb = &intel_fb->base;
8039
18c5247e
DV
8040 if (INTEL_INFO(dev)->gen >= 4) {
8041 if (val & DISPPLANE_TILED) {
49af449b 8042 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8043 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8044 }
8045 }
1ad292b5
JB
8046
8047 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8048 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8049 fb->pixel_format = fourcc;
8050 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
8051
8052 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 8053 if (plane_config->tiling)
1ad292b5
JB
8054 offset = I915_READ(DSPTILEOFF(plane));
8055 else
8056 offset = I915_READ(DSPLINOFF(plane));
8057 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8058 } else {
8059 base = I915_READ(DSPADDR(plane));
8060 }
8061 plane_config->base = base;
8062
8063 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8064 fb->width = ((val >> 16) & 0xfff) + 1;
8065 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
8066
8067 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8068 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 8069
b113d5ee 8070 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8071 fb->pixel_format,
8072 fb->modifier[0]);
1ad292b5 8073
f37b5c2b 8074 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 8075
2844a921
DL
8076 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8077 pipe_name(pipe), plane, fb->width, fb->height,
8078 fb->bits_per_pixel, base, fb->pitches[0],
8079 plane_config->size);
1ad292b5 8080
2d14030b 8081 plane_config->fb = intel_fb;
1ad292b5
JB
8082}
8083
70b23a98 8084static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 8085 struct intel_crtc_state *pipe_config)
70b23a98
VS
8086{
8087 struct drm_device *dev = crtc->base.dev;
8088 struct drm_i915_private *dev_priv = dev->dev_private;
8089 int pipe = pipe_config->cpu_transcoder;
8090 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8091 intel_clock_t clock;
8092 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8093 int refclk = 100000;
8094
a580516d 8095 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
8096 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8097 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8098 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8099 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
a580516d 8100 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
8101
8102 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8103 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8104 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8105 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8106 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8107
8108 chv_clock(refclk, &clock);
8109
8110 /* clock.dot is the fast clock */
8111 pipe_config->port_clock = clock.dot / 5;
8112}
8113
0e8ffe1b 8114static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8115 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8116{
8117 struct drm_device *dev = crtc->base.dev;
8118 struct drm_i915_private *dev_priv = dev->dev_private;
8119 uint32_t tmp;
8120
f458ebbc
DV
8121 if (!intel_display_power_is_enabled(dev_priv,
8122 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
8123 return false;
8124
e143a21c 8125 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8126 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8127
0e8ffe1b
DV
8128 tmp = I915_READ(PIPECONF(crtc->pipe));
8129 if (!(tmp & PIPECONF_ENABLE))
8130 return false;
8131
42571aef
VS
8132 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8133 switch (tmp & PIPECONF_BPC_MASK) {
8134 case PIPECONF_6BPC:
8135 pipe_config->pipe_bpp = 18;
8136 break;
8137 case PIPECONF_8BPC:
8138 pipe_config->pipe_bpp = 24;
8139 break;
8140 case PIPECONF_10BPC:
8141 pipe_config->pipe_bpp = 30;
8142 break;
8143 default:
8144 break;
8145 }
8146 }
8147
b5a9fa09
DV
8148 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8149 pipe_config->limited_color_range = true;
8150
282740f7
VS
8151 if (INTEL_INFO(dev)->gen < 4)
8152 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8153
1bd1bd80
DV
8154 intel_get_pipe_timings(crtc, pipe_config);
8155
2fa2fe9a
DV
8156 i9xx_get_pfit_config(crtc, pipe_config);
8157
6c49f241
DV
8158 if (INTEL_INFO(dev)->gen >= 4) {
8159 tmp = I915_READ(DPLL_MD(crtc->pipe));
8160 pipe_config->pixel_multiplier =
8161 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8162 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8163 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8164 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8165 tmp = I915_READ(DPLL(crtc->pipe));
8166 pipe_config->pixel_multiplier =
8167 ((tmp & SDVO_MULTIPLIER_MASK)
8168 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8169 } else {
8170 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8171 * port and will be fixed up in the encoder->get_config
8172 * function. */
8173 pipe_config->pixel_multiplier = 1;
8174 }
8bcc2795
DV
8175 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8176 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
8177 /*
8178 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8179 * on 830. Filter it out here so that we don't
8180 * report errors due to that.
8181 */
8182 if (IS_I830(dev))
8183 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8184
8bcc2795
DV
8185 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8186 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8187 } else {
8188 /* Mask out read-only status bits. */
8189 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8190 DPLL_PORTC_READY_MASK |
8191 DPLL_PORTB_READY_MASK);
8bcc2795 8192 }
6c49f241 8193
70b23a98
VS
8194 if (IS_CHERRYVIEW(dev))
8195 chv_crtc_clock_get(crtc, pipe_config);
8196 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8197 vlv_crtc_clock_get(crtc, pipe_config);
8198 else
8199 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8200
0e8ffe1b
DV
8201 return true;
8202}
8203
dde86e2d 8204static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8205{
8206 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8207 struct intel_encoder *encoder;
74cfd7ac 8208 u32 val, final;
13d83a67 8209 bool has_lvds = false;
199e5d79 8210 bool has_cpu_edp = false;
199e5d79 8211 bool has_panel = false;
99eb6a01
KP
8212 bool has_ck505 = false;
8213 bool can_ssc = false;
13d83a67
JB
8214
8215 /* We need to take the global config into account */
b2784e15 8216 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8217 switch (encoder->type) {
8218 case INTEL_OUTPUT_LVDS:
8219 has_panel = true;
8220 has_lvds = true;
8221 break;
8222 case INTEL_OUTPUT_EDP:
8223 has_panel = true;
2de6905f 8224 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8225 has_cpu_edp = true;
8226 break;
6847d71b
PZ
8227 default:
8228 break;
13d83a67
JB
8229 }
8230 }
8231
99eb6a01 8232 if (HAS_PCH_IBX(dev)) {
41aa3448 8233 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8234 can_ssc = has_ck505;
8235 } else {
8236 has_ck505 = false;
8237 can_ssc = true;
8238 }
8239
2de6905f
ID
8240 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8241 has_panel, has_lvds, has_ck505);
13d83a67
JB
8242
8243 /* Ironlake: try to setup display ref clock before DPLL
8244 * enabling. This is only under driver's control after
8245 * PCH B stepping, previous chipset stepping should be
8246 * ignoring this setting.
8247 */
74cfd7ac
CW
8248 val = I915_READ(PCH_DREF_CONTROL);
8249
8250 /* As we must carefully and slowly disable/enable each source in turn,
8251 * compute the final state we want first and check if we need to
8252 * make any changes at all.
8253 */
8254 final = val;
8255 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8256 if (has_ck505)
8257 final |= DREF_NONSPREAD_CK505_ENABLE;
8258 else
8259 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8260
8261 final &= ~DREF_SSC_SOURCE_MASK;
8262 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8263 final &= ~DREF_SSC1_ENABLE;
8264
8265 if (has_panel) {
8266 final |= DREF_SSC_SOURCE_ENABLE;
8267
8268 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8269 final |= DREF_SSC1_ENABLE;
8270
8271 if (has_cpu_edp) {
8272 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8273 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8274 else
8275 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8276 } else
8277 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8278 } else {
8279 final |= DREF_SSC_SOURCE_DISABLE;
8280 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8281 }
8282
8283 if (final == val)
8284 return;
8285
13d83a67 8286 /* Always enable nonspread source */
74cfd7ac 8287 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8288
99eb6a01 8289 if (has_ck505)
74cfd7ac 8290 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8291 else
74cfd7ac 8292 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8293
199e5d79 8294 if (has_panel) {
74cfd7ac
CW
8295 val &= ~DREF_SSC_SOURCE_MASK;
8296 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8297
199e5d79 8298 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8299 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8300 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8301 val |= DREF_SSC1_ENABLE;
e77166b5 8302 } else
74cfd7ac 8303 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8304
8305 /* Get SSC going before enabling the outputs */
74cfd7ac 8306 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8307 POSTING_READ(PCH_DREF_CONTROL);
8308 udelay(200);
8309
74cfd7ac 8310 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8311
8312 /* Enable CPU source on CPU attached eDP */
199e5d79 8313 if (has_cpu_edp) {
99eb6a01 8314 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8315 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8316 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8317 } else
74cfd7ac 8318 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8319 } else
74cfd7ac 8320 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8321
74cfd7ac 8322 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8323 POSTING_READ(PCH_DREF_CONTROL);
8324 udelay(200);
8325 } else {
8326 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8327
74cfd7ac 8328 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8329
8330 /* Turn off CPU output */
74cfd7ac 8331 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8332
74cfd7ac 8333 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8334 POSTING_READ(PCH_DREF_CONTROL);
8335 udelay(200);
8336
8337 /* Turn off the SSC source */
74cfd7ac
CW
8338 val &= ~DREF_SSC_SOURCE_MASK;
8339 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8340
8341 /* Turn off SSC1 */
74cfd7ac 8342 val &= ~DREF_SSC1_ENABLE;
199e5d79 8343
74cfd7ac 8344 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8345 POSTING_READ(PCH_DREF_CONTROL);
8346 udelay(200);
8347 }
74cfd7ac
CW
8348
8349 BUG_ON(val != final);
13d83a67
JB
8350}
8351
f31f2d55 8352static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8353{
f31f2d55 8354 uint32_t tmp;
dde86e2d 8355
0ff066a9
PZ
8356 tmp = I915_READ(SOUTH_CHICKEN2);
8357 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8358 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8359
0ff066a9
PZ
8360 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8361 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8362 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8363
0ff066a9
PZ
8364 tmp = I915_READ(SOUTH_CHICKEN2);
8365 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8366 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8367
0ff066a9
PZ
8368 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8369 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8370 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8371}
8372
8373/* WaMPhyProgramming:hsw */
8374static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8375{
8376 uint32_t tmp;
dde86e2d
PZ
8377
8378 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8379 tmp &= ~(0xFF << 24);
8380 tmp |= (0x12 << 24);
8381 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8382
dde86e2d
PZ
8383 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8384 tmp |= (1 << 11);
8385 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8386
8387 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8388 tmp |= (1 << 11);
8389 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8390
dde86e2d
PZ
8391 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8392 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8393 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8394
8395 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8396 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8397 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8398
0ff066a9
PZ
8399 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8400 tmp &= ~(7 << 13);
8401 tmp |= (5 << 13);
8402 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8403
0ff066a9
PZ
8404 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8405 tmp &= ~(7 << 13);
8406 tmp |= (5 << 13);
8407 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8408
8409 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8410 tmp &= ~0xFF;
8411 tmp |= 0x1C;
8412 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8413
8414 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8415 tmp &= ~0xFF;
8416 tmp |= 0x1C;
8417 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8418
8419 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8420 tmp &= ~(0xFF << 16);
8421 tmp |= (0x1C << 16);
8422 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8423
8424 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8425 tmp &= ~(0xFF << 16);
8426 tmp |= (0x1C << 16);
8427 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8428
0ff066a9
PZ
8429 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8430 tmp |= (1 << 27);
8431 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8432
0ff066a9
PZ
8433 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8434 tmp |= (1 << 27);
8435 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8436
0ff066a9
PZ
8437 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8438 tmp &= ~(0xF << 28);
8439 tmp |= (4 << 28);
8440 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8441
0ff066a9
PZ
8442 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8443 tmp &= ~(0xF << 28);
8444 tmp |= (4 << 28);
8445 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8446}
8447
2fa86a1f
PZ
8448/* Implements 3 different sequences from BSpec chapter "Display iCLK
8449 * Programming" based on the parameters passed:
8450 * - Sequence to enable CLKOUT_DP
8451 * - Sequence to enable CLKOUT_DP without spread
8452 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8453 */
8454static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8455 bool with_fdi)
f31f2d55
PZ
8456{
8457 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8458 uint32_t reg, tmp;
8459
8460 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8461 with_spread = true;
8462 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8463 with_fdi, "LP PCH doesn't have FDI\n"))
8464 with_fdi = false;
f31f2d55 8465
a580516d 8466 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8467
8468 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8469 tmp &= ~SBI_SSCCTL_DISABLE;
8470 tmp |= SBI_SSCCTL_PATHALT;
8471 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8472
8473 udelay(24);
8474
2fa86a1f
PZ
8475 if (with_spread) {
8476 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8477 tmp &= ~SBI_SSCCTL_PATHALT;
8478 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8479
2fa86a1f
PZ
8480 if (with_fdi) {
8481 lpt_reset_fdi_mphy(dev_priv);
8482 lpt_program_fdi_mphy(dev_priv);
8483 }
8484 }
dde86e2d 8485
2fa86a1f
PZ
8486 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8487 SBI_GEN0 : SBI_DBUFF0;
8488 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8489 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8490 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8491
a580516d 8492 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8493}
8494
47701c3b
PZ
8495/* Sequence to disable CLKOUT_DP */
8496static void lpt_disable_clkout_dp(struct drm_device *dev)
8497{
8498 struct drm_i915_private *dev_priv = dev->dev_private;
8499 uint32_t reg, tmp;
8500
a580516d 8501 mutex_lock(&dev_priv->sb_lock);
47701c3b
PZ
8502
8503 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8504 SBI_GEN0 : SBI_DBUFF0;
8505 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8506 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8507 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8508
8509 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8510 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8511 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8512 tmp |= SBI_SSCCTL_PATHALT;
8513 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8514 udelay(32);
8515 }
8516 tmp |= SBI_SSCCTL_DISABLE;
8517 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8518 }
8519
a580516d 8520 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8521}
8522
bf8fa3d3
PZ
8523static void lpt_init_pch_refclk(struct drm_device *dev)
8524{
bf8fa3d3
PZ
8525 struct intel_encoder *encoder;
8526 bool has_vga = false;
8527
b2784e15 8528 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8529 switch (encoder->type) {
8530 case INTEL_OUTPUT_ANALOG:
8531 has_vga = true;
8532 break;
6847d71b
PZ
8533 default:
8534 break;
bf8fa3d3
PZ
8535 }
8536 }
8537
47701c3b
PZ
8538 if (has_vga)
8539 lpt_enable_clkout_dp(dev, true, true);
8540 else
8541 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
8542}
8543
dde86e2d
PZ
8544/*
8545 * Initialize reference clocks when the driver loads
8546 */
8547void intel_init_pch_refclk(struct drm_device *dev)
8548{
8549 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8550 ironlake_init_pch_refclk(dev);
8551 else if (HAS_PCH_LPT(dev))
8552 lpt_init_pch_refclk(dev);
8553}
8554
55bb9992 8555static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8556{
55bb9992 8557 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8558 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8559 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8560 struct drm_connector *connector;
55bb9992 8561 struct drm_connector_state *connector_state;
d9d444cb 8562 struct intel_encoder *encoder;
55bb9992 8563 int num_connectors = 0, i;
d9d444cb
JB
8564 bool is_lvds = false;
8565
da3ced29 8566 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8567 if (connector_state->crtc != crtc_state->base.crtc)
8568 continue;
8569
8570 encoder = to_intel_encoder(connector_state->best_encoder);
8571
d9d444cb
JB
8572 switch (encoder->type) {
8573 case INTEL_OUTPUT_LVDS:
8574 is_lvds = true;
8575 break;
6847d71b
PZ
8576 default:
8577 break;
d9d444cb
JB
8578 }
8579 num_connectors++;
8580 }
8581
8582 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8583 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8584 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8585 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8586 }
8587
8588 return 120000;
8589}
8590
6ff93609 8591static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8592{
c8203565 8593 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8594 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8595 int pipe = intel_crtc->pipe;
c8203565
PZ
8596 uint32_t val;
8597
78114071 8598 val = 0;
c8203565 8599
6e3c9717 8600 switch (intel_crtc->config->pipe_bpp) {
c8203565 8601 case 18:
dfd07d72 8602 val |= PIPECONF_6BPC;
c8203565
PZ
8603 break;
8604 case 24:
dfd07d72 8605 val |= PIPECONF_8BPC;
c8203565
PZ
8606 break;
8607 case 30:
dfd07d72 8608 val |= PIPECONF_10BPC;
c8203565
PZ
8609 break;
8610 case 36:
dfd07d72 8611 val |= PIPECONF_12BPC;
c8203565
PZ
8612 break;
8613 default:
cc769b62
PZ
8614 /* Case prevented by intel_choose_pipe_bpp_dither. */
8615 BUG();
c8203565
PZ
8616 }
8617
6e3c9717 8618 if (intel_crtc->config->dither)
c8203565
PZ
8619 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8620
6e3c9717 8621 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8622 val |= PIPECONF_INTERLACED_ILK;
8623 else
8624 val |= PIPECONF_PROGRESSIVE;
8625
6e3c9717 8626 if (intel_crtc->config->limited_color_range)
3685a8f3 8627 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8628
c8203565
PZ
8629 I915_WRITE(PIPECONF(pipe), val);
8630 POSTING_READ(PIPECONF(pipe));
8631}
8632
86d3efce
VS
8633/*
8634 * Set up the pipe CSC unit.
8635 *
8636 * Currently only full range RGB to limited range RGB conversion
8637 * is supported, but eventually this should handle various
8638 * RGB<->YCbCr scenarios as well.
8639 */
50f3b016 8640static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8641{
8642 struct drm_device *dev = crtc->dev;
8643 struct drm_i915_private *dev_priv = dev->dev_private;
8644 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8645 int pipe = intel_crtc->pipe;
8646 uint16_t coeff = 0x7800; /* 1.0 */
8647
8648 /*
8649 * TODO: Check what kind of values actually come out of the pipe
8650 * with these coeff/postoff values and adjust to get the best
8651 * accuracy. Perhaps we even need to take the bpc value into
8652 * consideration.
8653 */
8654
6e3c9717 8655 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8656 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8657
8658 /*
8659 * GY/GU and RY/RU should be the other way around according
8660 * to BSpec, but reality doesn't agree. Just set them up in
8661 * a way that results in the correct picture.
8662 */
8663 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8664 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8665
8666 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8667 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8668
8669 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8670 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8671
8672 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8673 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8674 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8675
8676 if (INTEL_INFO(dev)->gen > 6) {
8677 uint16_t postoff = 0;
8678
6e3c9717 8679 if (intel_crtc->config->limited_color_range)
32cf0cb0 8680 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8681
8682 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8683 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8684 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8685
8686 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8687 } else {
8688 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8689
6e3c9717 8690 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8691 mode |= CSC_BLACK_SCREEN_OFFSET;
8692
8693 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8694 }
8695}
8696
6ff93609 8697static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8698{
756f85cf
PZ
8699 struct drm_device *dev = crtc->dev;
8700 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 8701 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8702 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8703 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
8704 uint32_t val;
8705
3eff4faa 8706 val = 0;
ee2b0b38 8707
6e3c9717 8708 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
8709 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8710
6e3c9717 8711 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8712 val |= PIPECONF_INTERLACED_ILK;
8713 else
8714 val |= PIPECONF_PROGRESSIVE;
8715
702e7a56
PZ
8716 I915_WRITE(PIPECONF(cpu_transcoder), val);
8717 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
8718
8719 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8720 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 8721
3cdf122c 8722 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
8723 val = 0;
8724
6e3c9717 8725 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8726 case 18:
8727 val |= PIPEMISC_DITHER_6_BPC;
8728 break;
8729 case 24:
8730 val |= PIPEMISC_DITHER_8_BPC;
8731 break;
8732 case 30:
8733 val |= PIPEMISC_DITHER_10_BPC;
8734 break;
8735 case 36:
8736 val |= PIPEMISC_DITHER_12_BPC;
8737 break;
8738 default:
8739 /* Case prevented by pipe_config_set_bpp. */
8740 BUG();
8741 }
8742
6e3c9717 8743 if (intel_crtc->config->dither)
756f85cf
PZ
8744 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8745
8746 I915_WRITE(PIPEMISC(pipe), val);
8747 }
ee2b0b38
PZ
8748}
8749
6591c6e4 8750static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8751 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8752 intel_clock_t *clock,
8753 bool *has_reduced_clock,
8754 intel_clock_t *reduced_clock)
8755{
8756 struct drm_device *dev = crtc->dev;
8757 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8758 int refclk;
d4906093 8759 const intel_limit_t *limit;
a16af721 8760 bool ret, is_lvds = false;
79e53945 8761
a93e255f 8762 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
79e53945 8763
55bb9992 8764 refclk = ironlake_get_refclk(crtc_state);
79e53945 8765
d4906093
ML
8766 /*
8767 * Returns a set of divisors for the desired target clock with the given
8768 * refclk, or FALSE. The returned values represent the clock equation:
8769 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8770 */
a93e255f
ACO
8771 limit = intel_limit(crtc_state, refclk);
8772 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8773 crtc_state->port_clock,
ee9300bb 8774 refclk, NULL, clock);
6591c6e4
PZ
8775 if (!ret)
8776 return false;
cda4b7d3 8777
ddc9003c 8778 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
8779 /*
8780 * Ensure we match the reduced clock's P to the target clock.
8781 * If the clocks don't match, we can't switch the display clock
8782 * by using the FP0/FP1. In such case we will disable the LVDS
8783 * downclock feature.
8784 */
ee9300bb 8785 *has_reduced_clock =
a93e255f 8786 dev_priv->display.find_dpll(limit, crtc_state,
ee9300bb
DV
8787 dev_priv->lvds_downclock,
8788 refclk, clock,
8789 reduced_clock);
652c393a 8790 }
61e9653f 8791
6591c6e4
PZ
8792 return true;
8793}
8794
d4b1931c
PZ
8795int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8796{
8797 /*
8798 * Account for spread spectrum to avoid
8799 * oversubscribing the link. Max center spread
8800 * is 2.5%; use 5% for safety's sake.
8801 */
8802 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8803 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8804}
8805
7429e9d4 8806static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8807{
7429e9d4 8808 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8809}
8810
de13a2e3 8811static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8812 struct intel_crtc_state *crtc_state,
7429e9d4 8813 u32 *fp,
9a7c7890 8814 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8815{
de13a2e3 8816 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8817 struct drm_device *dev = crtc->dev;
8818 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8819 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8820 struct drm_connector *connector;
55bb9992
ACO
8821 struct drm_connector_state *connector_state;
8822 struct intel_encoder *encoder;
de13a2e3 8823 uint32_t dpll;
55bb9992 8824 int factor, num_connectors = 0, i;
09ede541 8825 bool is_lvds = false, is_sdvo = false;
79e53945 8826
da3ced29 8827 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8828 if (connector_state->crtc != crtc_state->base.crtc)
8829 continue;
8830
8831 encoder = to_intel_encoder(connector_state->best_encoder);
8832
8833 switch (encoder->type) {
79e53945
JB
8834 case INTEL_OUTPUT_LVDS:
8835 is_lvds = true;
8836 break;
8837 case INTEL_OUTPUT_SDVO:
7d57382e 8838 case INTEL_OUTPUT_HDMI:
79e53945 8839 is_sdvo = true;
79e53945 8840 break;
6847d71b
PZ
8841 default:
8842 break;
79e53945 8843 }
43565a06 8844
c751ce4f 8845 num_connectors++;
79e53945 8846 }
79e53945 8847
c1858123 8848 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8849 factor = 21;
8850 if (is_lvds) {
8851 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8852 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8853 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8854 factor = 25;
190f68c5 8855 } else if (crtc_state->sdvo_tv_clock)
8febb297 8856 factor = 20;
c1858123 8857
190f68c5 8858 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8859 *fp |= FP_CB_TUNE;
2c07245f 8860
9a7c7890
DV
8861 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8862 *fp2 |= FP_CB_TUNE;
8863
5eddb70b 8864 dpll = 0;
2c07245f 8865
a07d6787
EA
8866 if (is_lvds)
8867 dpll |= DPLLB_MODE_LVDS;
8868 else
8869 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8870
190f68c5 8871 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8872 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8873
8874 if (is_sdvo)
4a33e48d 8875 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8876 if (crtc_state->has_dp_encoder)
4a33e48d 8877 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8878
a07d6787 8879 /* compute bitmask from p1 value */
190f68c5 8880 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8881 /* also FPA1 */
190f68c5 8882 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8883
190f68c5 8884 switch (crtc_state->dpll.p2) {
a07d6787
EA
8885 case 5:
8886 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8887 break;
8888 case 7:
8889 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8890 break;
8891 case 10:
8892 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8893 break;
8894 case 14:
8895 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8896 break;
79e53945
JB
8897 }
8898
b4c09f3b 8899 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8900 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8901 else
8902 dpll |= PLL_REF_INPUT_DREFCLK;
8903
959e16d6 8904 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8905}
8906
190f68c5
ACO
8907static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8908 struct intel_crtc_state *crtc_state)
de13a2e3 8909{
c7653199 8910 struct drm_device *dev = crtc->base.dev;
de13a2e3 8911 intel_clock_t clock, reduced_clock;
cbbab5bd 8912 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8913 bool ok, has_reduced_clock = false;
8b47047b 8914 bool is_lvds = false;
e2b78267 8915 struct intel_shared_dpll *pll;
de13a2e3 8916
dd3cd74a
ACO
8917 memset(&crtc_state->dpll_hw_state, 0,
8918 sizeof(crtc_state->dpll_hw_state));
8919
409ee761 8920 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 8921
5dc5298b
PZ
8922 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8923 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8924
190f68c5 8925 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8926 &has_reduced_clock, &reduced_clock);
190f68c5 8927 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8928 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8929 return -EINVAL;
79e53945 8930 }
f47709a9 8931 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8932 if (!crtc_state->clock_set) {
8933 crtc_state->dpll.n = clock.n;
8934 crtc_state->dpll.m1 = clock.m1;
8935 crtc_state->dpll.m2 = clock.m2;
8936 crtc_state->dpll.p1 = clock.p1;
8937 crtc_state->dpll.p2 = clock.p2;
f47709a9 8938 }
79e53945 8939
5dc5298b 8940 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8941 if (crtc_state->has_pch_encoder) {
8942 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8943 if (has_reduced_clock)
7429e9d4 8944 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8945
190f68c5 8946 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8947 &fp, &reduced_clock,
8948 has_reduced_clock ? &fp2 : NULL);
8949
190f68c5
ACO
8950 crtc_state->dpll_hw_state.dpll = dpll;
8951 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8952 if (has_reduced_clock)
190f68c5 8953 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8954 else
190f68c5 8955 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8956
190f68c5 8957 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 8958 if (pll == NULL) {
84f44ce7 8959 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8960 pipe_name(crtc->pipe));
4b645f14
JB
8961 return -EINVAL;
8962 }
3fb37703 8963 }
79e53945 8964
ab585dea 8965 if (is_lvds && has_reduced_clock)
c7653199 8966 crtc->lowfreq_avail = true;
bcd644e0 8967 else
c7653199 8968 crtc->lowfreq_avail = false;
e2b78267 8969
c8f7a0db 8970 return 0;
79e53945
JB
8971}
8972
eb14cb74
VS
8973static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8974 struct intel_link_m_n *m_n)
8975{
8976 struct drm_device *dev = crtc->base.dev;
8977 struct drm_i915_private *dev_priv = dev->dev_private;
8978 enum pipe pipe = crtc->pipe;
8979
8980 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8981 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8982 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8983 & ~TU_SIZE_MASK;
8984 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8985 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8986 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8987}
8988
8989static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8990 enum transcoder transcoder,
b95af8be
VK
8991 struct intel_link_m_n *m_n,
8992 struct intel_link_m_n *m2_n2)
72419203
DV
8993{
8994 struct drm_device *dev = crtc->base.dev;
8995 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8996 enum pipe pipe = crtc->pipe;
72419203 8997
eb14cb74
VS
8998 if (INTEL_INFO(dev)->gen >= 5) {
8999 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9000 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9001 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9002 & ~TU_SIZE_MASK;
9003 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9004 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9005 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
9006 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9007 * gen < 8) and if DRRS is supported (to make sure the
9008 * registers are not unnecessarily read).
9009 */
9010 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 9011 crtc->config->has_drrs) {
b95af8be
VK
9012 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9013 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9014 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9015 & ~TU_SIZE_MASK;
9016 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9017 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9018 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9019 }
eb14cb74
VS
9020 } else {
9021 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9022 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9023 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9024 & ~TU_SIZE_MASK;
9025 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9026 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9027 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9028 }
9029}
9030
9031void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 9032 struct intel_crtc_state *pipe_config)
eb14cb74 9033{
681a8504 9034 if (pipe_config->has_pch_encoder)
eb14cb74
VS
9035 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9036 else
9037 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
9038 &pipe_config->dp_m_n,
9039 &pipe_config->dp_m2_n2);
eb14cb74 9040}
72419203 9041
eb14cb74 9042static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 9043 struct intel_crtc_state *pipe_config)
eb14cb74
VS
9044{
9045 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 9046 &pipe_config->fdi_m_n, NULL);
72419203
DV
9047}
9048
bd2e244f 9049static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9050 struct intel_crtc_state *pipe_config)
bd2e244f
JB
9051{
9052 struct drm_device *dev = crtc->base.dev;
9053 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
9054 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9055 uint32_t ps_ctrl = 0;
9056 int id = -1;
9057 int i;
bd2e244f 9058
a1b2278e
CK
9059 /* find scaler attached to this pipe */
9060 for (i = 0; i < crtc->num_scalers; i++) {
9061 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9062 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9063 id = i;
9064 pipe_config->pch_pfit.enabled = true;
9065 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9066 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9067 break;
9068 }
9069 }
bd2e244f 9070
a1b2278e
CK
9071 scaler_state->scaler_id = id;
9072 if (id >= 0) {
9073 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9074 } else {
9075 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
9076 }
9077}
9078
5724dbd1
DL
9079static void
9080skylake_get_initial_plane_config(struct intel_crtc *crtc,
9081 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
9082{
9083 struct drm_device *dev = crtc->base.dev;
9084 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 9085 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
9086 int pipe = crtc->pipe;
9087 int fourcc, pixel_format;
6761dd31 9088 unsigned int aligned_height;
bc8d7dff 9089 struct drm_framebuffer *fb;
1b842c89 9090 struct intel_framebuffer *intel_fb;
bc8d7dff 9091
d9806c9f 9092 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9093 if (!intel_fb) {
bc8d7dff
DL
9094 DRM_DEBUG_KMS("failed to alloc fb\n");
9095 return;
9096 }
9097
1b842c89
DL
9098 fb = &intel_fb->base;
9099
bc8d7dff 9100 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
9101 if (!(val & PLANE_CTL_ENABLE))
9102 goto error;
9103
bc8d7dff
DL
9104 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9105 fourcc = skl_format_to_fourcc(pixel_format,
9106 val & PLANE_CTL_ORDER_RGBX,
9107 val & PLANE_CTL_ALPHA_MASK);
9108 fb->pixel_format = fourcc;
9109 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9110
40f46283
DL
9111 tiling = val & PLANE_CTL_TILED_MASK;
9112 switch (tiling) {
9113 case PLANE_CTL_TILED_LINEAR:
9114 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9115 break;
9116 case PLANE_CTL_TILED_X:
9117 plane_config->tiling = I915_TILING_X;
9118 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9119 break;
9120 case PLANE_CTL_TILED_Y:
9121 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9122 break;
9123 case PLANE_CTL_TILED_YF:
9124 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9125 break;
9126 default:
9127 MISSING_CASE(tiling);
9128 goto error;
9129 }
9130
bc8d7dff
DL
9131 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9132 plane_config->base = base;
9133
9134 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9135
9136 val = I915_READ(PLANE_SIZE(pipe, 0));
9137 fb->height = ((val >> 16) & 0xfff) + 1;
9138 fb->width = ((val >> 0) & 0x1fff) + 1;
9139
9140 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
9141 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9142 fb->pixel_format);
bc8d7dff
DL
9143 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9144
9145 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9146 fb->pixel_format,
9147 fb->modifier[0]);
bc8d7dff 9148
f37b5c2b 9149 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9150
9151 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9152 pipe_name(pipe), fb->width, fb->height,
9153 fb->bits_per_pixel, base, fb->pitches[0],
9154 plane_config->size);
9155
2d14030b 9156 plane_config->fb = intel_fb;
bc8d7dff
DL
9157 return;
9158
9159error:
9160 kfree(fb);
9161}
9162
2fa2fe9a 9163static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9164 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9165{
9166 struct drm_device *dev = crtc->base.dev;
9167 struct drm_i915_private *dev_priv = dev->dev_private;
9168 uint32_t tmp;
9169
9170 tmp = I915_READ(PF_CTL(crtc->pipe));
9171
9172 if (tmp & PF_ENABLE) {
fd4daa9c 9173 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9174 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9175 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9176
9177 /* We currently do not free assignements of panel fitters on
9178 * ivb/hsw (since we don't use the higher upscaling modes which
9179 * differentiates them) so just WARN about this case for now. */
9180 if (IS_GEN7(dev)) {
9181 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9182 PF_PIPE_SEL_IVB(crtc->pipe));
9183 }
2fa2fe9a 9184 }
79e53945
JB
9185}
9186
5724dbd1
DL
9187static void
9188ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9189 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9190{
9191 struct drm_device *dev = crtc->base.dev;
9192 struct drm_i915_private *dev_priv = dev->dev_private;
9193 u32 val, base, offset;
aeee5a49 9194 int pipe = crtc->pipe;
4c6baa59 9195 int fourcc, pixel_format;
6761dd31 9196 unsigned int aligned_height;
b113d5ee 9197 struct drm_framebuffer *fb;
1b842c89 9198 struct intel_framebuffer *intel_fb;
4c6baa59 9199
42a7b088
DL
9200 val = I915_READ(DSPCNTR(pipe));
9201 if (!(val & DISPLAY_PLANE_ENABLE))
9202 return;
9203
d9806c9f 9204 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9205 if (!intel_fb) {
4c6baa59
JB
9206 DRM_DEBUG_KMS("failed to alloc fb\n");
9207 return;
9208 }
9209
1b842c89
DL
9210 fb = &intel_fb->base;
9211
18c5247e
DV
9212 if (INTEL_INFO(dev)->gen >= 4) {
9213 if (val & DISPPLANE_TILED) {
49af449b 9214 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9215 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9216 }
9217 }
4c6baa59
JB
9218
9219 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9220 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9221 fb->pixel_format = fourcc;
9222 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9223
aeee5a49 9224 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9225 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9226 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9227 } else {
49af449b 9228 if (plane_config->tiling)
aeee5a49 9229 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9230 else
aeee5a49 9231 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9232 }
9233 plane_config->base = base;
9234
9235 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9236 fb->width = ((val >> 16) & 0xfff) + 1;
9237 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9238
9239 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9240 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9241
b113d5ee 9242 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9243 fb->pixel_format,
9244 fb->modifier[0]);
4c6baa59 9245
f37b5c2b 9246 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9247
2844a921
DL
9248 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9249 pipe_name(pipe), fb->width, fb->height,
9250 fb->bits_per_pixel, base, fb->pitches[0],
9251 plane_config->size);
b113d5ee 9252
2d14030b 9253 plane_config->fb = intel_fb;
4c6baa59
JB
9254}
9255
0e8ffe1b 9256static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9257 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9258{
9259 struct drm_device *dev = crtc->base.dev;
9260 struct drm_i915_private *dev_priv = dev->dev_private;
9261 uint32_t tmp;
9262
f458ebbc
DV
9263 if (!intel_display_power_is_enabled(dev_priv,
9264 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
9265 return false;
9266
e143a21c 9267 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 9268 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 9269
0e8ffe1b
DV
9270 tmp = I915_READ(PIPECONF(crtc->pipe));
9271 if (!(tmp & PIPECONF_ENABLE))
9272 return false;
9273
42571aef
VS
9274 switch (tmp & PIPECONF_BPC_MASK) {
9275 case PIPECONF_6BPC:
9276 pipe_config->pipe_bpp = 18;
9277 break;
9278 case PIPECONF_8BPC:
9279 pipe_config->pipe_bpp = 24;
9280 break;
9281 case PIPECONF_10BPC:
9282 pipe_config->pipe_bpp = 30;
9283 break;
9284 case PIPECONF_12BPC:
9285 pipe_config->pipe_bpp = 36;
9286 break;
9287 default:
9288 break;
9289 }
9290
b5a9fa09
DV
9291 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9292 pipe_config->limited_color_range = true;
9293
ab9412ba 9294 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
9295 struct intel_shared_dpll *pll;
9296
88adfff1
DV
9297 pipe_config->has_pch_encoder = true;
9298
627eb5a3
DV
9299 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9300 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9301 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9302
9303 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9304
c0d43d62 9305 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
9306 pipe_config->shared_dpll =
9307 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9308 } else {
9309 tmp = I915_READ(PCH_DPLL_SEL);
9310 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9311 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9312 else
9313 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9314 }
66e985c0
DV
9315
9316 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9317
9318 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9319 &pipe_config->dpll_hw_state));
c93f54cf
DV
9320
9321 tmp = pipe_config->dpll_hw_state.dpll;
9322 pipe_config->pixel_multiplier =
9323 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9324 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9325
9326 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9327 } else {
9328 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9329 }
9330
1bd1bd80
DV
9331 intel_get_pipe_timings(crtc, pipe_config);
9332
2fa2fe9a
DV
9333 ironlake_get_pfit_config(crtc, pipe_config);
9334
0e8ffe1b
DV
9335 return true;
9336}
9337
be256dc7
PZ
9338static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9339{
9340 struct drm_device *dev = dev_priv->dev;
be256dc7 9341 struct intel_crtc *crtc;
be256dc7 9342
d3fcc808 9343 for_each_intel_crtc(dev, crtc)
e2c719b7 9344 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9345 pipe_name(crtc->pipe));
9346
e2c719b7
RC
9347 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9348 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9349 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9350 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9351 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9352 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9353 "CPU PWM1 enabled\n");
c5107b87 9354 if (IS_HASWELL(dev))
e2c719b7 9355 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9356 "CPU PWM2 enabled\n");
e2c719b7 9357 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9358 "PCH PWM1 enabled\n");
e2c719b7 9359 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9360 "Utility pin enabled\n");
e2c719b7 9361 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9362
9926ada1
PZ
9363 /*
9364 * In theory we can still leave IRQs enabled, as long as only the HPD
9365 * interrupts remain enabled. We used to check for that, but since it's
9366 * gen-specific and since we only disable LCPLL after we fully disable
9367 * the interrupts, the check below should be enough.
9368 */
e2c719b7 9369 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9370}
9371
9ccd5aeb
PZ
9372static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9373{
9374 struct drm_device *dev = dev_priv->dev;
9375
9376 if (IS_HASWELL(dev))
9377 return I915_READ(D_COMP_HSW);
9378 else
9379 return I915_READ(D_COMP_BDW);
9380}
9381
3c4c9b81
PZ
9382static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9383{
9384 struct drm_device *dev = dev_priv->dev;
9385
9386 if (IS_HASWELL(dev)) {
9387 mutex_lock(&dev_priv->rps.hw_lock);
9388 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9389 val))
f475dadf 9390 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9391 mutex_unlock(&dev_priv->rps.hw_lock);
9392 } else {
9ccd5aeb
PZ
9393 I915_WRITE(D_COMP_BDW, val);
9394 POSTING_READ(D_COMP_BDW);
3c4c9b81 9395 }
be256dc7
PZ
9396}
9397
9398/*
9399 * This function implements pieces of two sequences from BSpec:
9400 * - Sequence for display software to disable LCPLL
9401 * - Sequence for display software to allow package C8+
9402 * The steps implemented here are just the steps that actually touch the LCPLL
9403 * register. Callers should take care of disabling all the display engine
9404 * functions, doing the mode unset, fixing interrupts, etc.
9405 */
6ff58d53
PZ
9406static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9407 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9408{
9409 uint32_t val;
9410
9411 assert_can_disable_lcpll(dev_priv);
9412
9413 val = I915_READ(LCPLL_CTL);
9414
9415 if (switch_to_fclk) {
9416 val |= LCPLL_CD_SOURCE_FCLK;
9417 I915_WRITE(LCPLL_CTL, val);
9418
9419 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9420 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9421 DRM_ERROR("Switching to FCLK failed\n");
9422
9423 val = I915_READ(LCPLL_CTL);
9424 }
9425
9426 val |= LCPLL_PLL_DISABLE;
9427 I915_WRITE(LCPLL_CTL, val);
9428 POSTING_READ(LCPLL_CTL);
9429
9430 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9431 DRM_ERROR("LCPLL still locked\n");
9432
9ccd5aeb 9433 val = hsw_read_dcomp(dev_priv);
be256dc7 9434 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9435 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9436 ndelay(100);
9437
9ccd5aeb
PZ
9438 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9439 1))
be256dc7
PZ
9440 DRM_ERROR("D_COMP RCOMP still in progress\n");
9441
9442 if (allow_power_down) {
9443 val = I915_READ(LCPLL_CTL);
9444 val |= LCPLL_POWER_DOWN_ALLOW;
9445 I915_WRITE(LCPLL_CTL, val);
9446 POSTING_READ(LCPLL_CTL);
9447 }
9448}
9449
9450/*
9451 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9452 * source.
9453 */
6ff58d53 9454static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9455{
9456 uint32_t val;
9457
9458 val = I915_READ(LCPLL_CTL);
9459
9460 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9461 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9462 return;
9463
a8a8bd54
PZ
9464 /*
9465 * Make sure we're not on PC8 state before disabling PC8, otherwise
9466 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9467 */
59bad947 9468 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9469
be256dc7
PZ
9470 if (val & LCPLL_POWER_DOWN_ALLOW) {
9471 val &= ~LCPLL_POWER_DOWN_ALLOW;
9472 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9473 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9474 }
9475
9ccd5aeb 9476 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9477 val |= D_COMP_COMP_FORCE;
9478 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9479 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9480
9481 val = I915_READ(LCPLL_CTL);
9482 val &= ~LCPLL_PLL_DISABLE;
9483 I915_WRITE(LCPLL_CTL, val);
9484
9485 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9486 DRM_ERROR("LCPLL not locked yet\n");
9487
9488 if (val & LCPLL_CD_SOURCE_FCLK) {
9489 val = I915_READ(LCPLL_CTL);
9490 val &= ~LCPLL_CD_SOURCE_FCLK;
9491 I915_WRITE(LCPLL_CTL, val);
9492
9493 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9494 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9495 DRM_ERROR("Switching back to LCPLL failed\n");
9496 }
215733fa 9497
59bad947 9498 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9499 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9500}
9501
765dab67
PZ
9502/*
9503 * Package states C8 and deeper are really deep PC states that can only be
9504 * reached when all the devices on the system allow it, so even if the graphics
9505 * device allows PC8+, it doesn't mean the system will actually get to these
9506 * states. Our driver only allows PC8+ when going into runtime PM.
9507 *
9508 * The requirements for PC8+ are that all the outputs are disabled, the power
9509 * well is disabled and most interrupts are disabled, and these are also
9510 * requirements for runtime PM. When these conditions are met, we manually do
9511 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9512 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9513 * hang the machine.
9514 *
9515 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9516 * the state of some registers, so when we come back from PC8+ we need to
9517 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9518 * need to take care of the registers kept by RC6. Notice that this happens even
9519 * if we don't put the device in PCI D3 state (which is what currently happens
9520 * because of the runtime PM support).
9521 *
9522 * For more, read "Display Sequences for Package C8" on the hardware
9523 * documentation.
9524 */
a14cb6fc 9525void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9526{
c67a470b
PZ
9527 struct drm_device *dev = dev_priv->dev;
9528 uint32_t val;
9529
c67a470b
PZ
9530 DRM_DEBUG_KMS("Enabling package C8+\n");
9531
c67a470b
PZ
9532 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9533 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9534 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9535 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9536 }
9537
9538 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9539 hsw_disable_lcpll(dev_priv, true, true);
9540}
9541
a14cb6fc 9542void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9543{
9544 struct drm_device *dev = dev_priv->dev;
9545 uint32_t val;
9546
c67a470b
PZ
9547 DRM_DEBUG_KMS("Disabling package C8+\n");
9548
9549 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9550 lpt_init_pch_refclk(dev);
9551
9552 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9553 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9554 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9555 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9556 }
9557
9558 intel_prepare_ddi(dev);
c67a470b
PZ
9559}
9560
a821fc46 9561static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
f8437dd1 9562{
a821fc46 9563 struct drm_device *dev = old_state->dev;
f8437dd1 9564 struct drm_i915_private *dev_priv = dev->dev_private;
a821fc46 9565 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
f8437dd1
VK
9566 int req_cdclk;
9567
9568 /* see the comment in valleyview_modeset_global_resources */
9569 if (WARN_ON(max_pixclk < 0))
9570 return;
9571
9572 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9573
9574 if (req_cdclk != dev_priv->cdclk_freq)
9575 broxton_set_cdclk(dev, req_cdclk);
9576}
9577
b432e5cf
VS
9578/* compute the max rate for new configuration */
9579static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
9580{
9581 struct drm_device *dev = dev_priv->dev;
9582 struct intel_crtc *intel_crtc;
9583 struct drm_crtc *crtc;
9584 int max_pixel_rate = 0;
9585 int pixel_rate;
9586
9587 for_each_crtc(dev, crtc) {
9588 if (!crtc->state->enable)
9589 continue;
9590
9591 intel_crtc = to_intel_crtc(crtc);
9592 pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
9593
9594 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9595 if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
9596 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9597
9598 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9599 }
9600
9601 return max_pixel_rate;
9602}
9603
9604static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9605{
9606 struct drm_i915_private *dev_priv = dev->dev_private;
9607 uint32_t val, data;
9608 int ret;
9609
9610 if (WARN((I915_READ(LCPLL_CTL) &
9611 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9612 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9613 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9614 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9615 "trying to change cdclk frequency with cdclk not enabled\n"))
9616 return;
9617
9618 mutex_lock(&dev_priv->rps.hw_lock);
9619 ret = sandybridge_pcode_write(dev_priv,
9620 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9621 mutex_unlock(&dev_priv->rps.hw_lock);
9622 if (ret) {
9623 DRM_ERROR("failed to inform pcode about cdclk change\n");
9624 return;
9625 }
9626
9627 val = I915_READ(LCPLL_CTL);
9628 val |= LCPLL_CD_SOURCE_FCLK;
9629 I915_WRITE(LCPLL_CTL, val);
9630
9631 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9632 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9633 DRM_ERROR("Switching to FCLK failed\n");
9634
9635 val = I915_READ(LCPLL_CTL);
9636 val &= ~LCPLL_CLK_FREQ_MASK;
9637
9638 switch (cdclk) {
9639 case 450000:
9640 val |= LCPLL_CLK_FREQ_450;
9641 data = 0;
9642 break;
9643 case 540000:
9644 val |= LCPLL_CLK_FREQ_54O_BDW;
9645 data = 1;
9646 break;
9647 case 337500:
9648 val |= LCPLL_CLK_FREQ_337_5_BDW;
9649 data = 2;
9650 break;
9651 case 675000:
9652 val |= LCPLL_CLK_FREQ_675_BDW;
9653 data = 3;
9654 break;
9655 default:
9656 WARN(1, "invalid cdclk frequency\n");
9657 return;
9658 }
9659
9660 I915_WRITE(LCPLL_CTL, val);
9661
9662 val = I915_READ(LCPLL_CTL);
9663 val &= ~LCPLL_CD_SOURCE_FCLK;
9664 I915_WRITE(LCPLL_CTL, val);
9665
9666 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9667 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9668 DRM_ERROR("Switching back to LCPLL failed\n");
9669
9670 mutex_lock(&dev_priv->rps.hw_lock);
9671 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9672 mutex_unlock(&dev_priv->rps.hw_lock);
9673
9674 intel_update_cdclk(dev);
9675
9676 WARN(cdclk != dev_priv->cdclk_freq,
9677 "cdclk requested %d kHz but got %d kHz\n",
9678 cdclk, dev_priv->cdclk_freq);
9679}
9680
9681static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
9682 int max_pixel_rate)
9683{
9684 int cdclk;
9685
9686 /*
9687 * FIXME should also account for plane ratio
9688 * once 64bpp pixel formats are supported.
9689 */
9690 if (max_pixel_rate > 540000)
9691 cdclk = 675000;
9692 else if (max_pixel_rate > 450000)
9693 cdclk = 540000;
9694 else if (max_pixel_rate > 337500)
9695 cdclk = 450000;
9696 else
9697 cdclk = 337500;
9698
9699 /*
9700 * FIXME move the cdclk caclulation to
9701 * compute_config() so we can fail gracegully.
9702 */
9703 if (cdclk > dev_priv->max_cdclk_freq) {
9704 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9705 cdclk, dev_priv->max_cdclk_freq);
9706 cdclk = dev_priv->max_cdclk_freq;
9707 }
9708
9709 return cdclk;
9710}
9711
9712static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
9713{
9714 struct drm_i915_private *dev_priv = to_i915(state->dev);
9715 struct drm_crtc *crtc;
9716 struct drm_crtc_state *crtc_state;
9717 int max_pixclk = ilk_max_pixel_rate(dev_priv);
9718 int cdclk, i;
9719
9720 cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
9721
9722 if (cdclk == dev_priv->cdclk_freq)
9723 return 0;
9724
9725 /* add all active pipes to the state */
9726 for_each_crtc(state->dev, crtc) {
9727 if (!crtc->state->enable)
9728 continue;
9729
9730 crtc_state = drm_atomic_get_crtc_state(state, crtc);
9731 if (IS_ERR(crtc_state))
9732 return PTR_ERR(crtc_state);
9733 }
9734
9735 /* disable/enable all currently active pipes while we change cdclk */
9736 for_each_crtc_in_state(state, crtc, crtc_state, i)
9737 if (crtc_state->enable)
9738 crtc_state->mode_changed = true;
9739
9740 return 0;
9741}
9742
9743static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
9744{
9745 struct drm_device *dev = state->dev;
9746 struct drm_i915_private *dev_priv = dev->dev_private;
9747 int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
9748 int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
9749
9750 if (req_cdclk != dev_priv->cdclk_freq)
9751 broadwell_set_cdclk(dev, req_cdclk);
9752}
9753
190f68c5
ACO
9754static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9755 struct intel_crtc_state *crtc_state)
09b4ddf9 9756{
190f68c5 9757 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 9758 return -EINVAL;
716c2e55 9759
c7653199 9760 crtc->lowfreq_avail = false;
644cef34 9761
c8f7a0db 9762 return 0;
79e53945
JB
9763}
9764
3760b59c
S
9765static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9766 enum port port,
9767 struct intel_crtc_state *pipe_config)
9768{
9769 switch (port) {
9770 case PORT_A:
9771 pipe_config->ddi_pll_sel = SKL_DPLL0;
9772 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9773 break;
9774 case PORT_B:
9775 pipe_config->ddi_pll_sel = SKL_DPLL1;
9776 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9777 break;
9778 case PORT_C:
9779 pipe_config->ddi_pll_sel = SKL_DPLL2;
9780 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9781 break;
9782 default:
9783 DRM_ERROR("Incorrect port type\n");
9784 }
9785}
9786
96b7dfb7
S
9787static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9788 enum port port,
5cec258b 9789 struct intel_crtc_state *pipe_config)
96b7dfb7 9790{
3148ade7 9791 u32 temp, dpll_ctl1;
96b7dfb7
S
9792
9793 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9794 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9795
9796 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
9797 case SKL_DPLL0:
9798 /*
9799 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9800 * of the shared DPLL framework and thus needs to be read out
9801 * separately
9802 */
9803 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9804 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9805 break;
96b7dfb7
S
9806 case SKL_DPLL1:
9807 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9808 break;
9809 case SKL_DPLL2:
9810 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9811 break;
9812 case SKL_DPLL3:
9813 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9814 break;
96b7dfb7
S
9815 }
9816}
9817
7d2c8175
DL
9818static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9819 enum port port,
5cec258b 9820 struct intel_crtc_state *pipe_config)
7d2c8175
DL
9821{
9822 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9823
9824 switch (pipe_config->ddi_pll_sel) {
9825 case PORT_CLK_SEL_WRPLL1:
9826 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9827 break;
9828 case PORT_CLK_SEL_WRPLL2:
9829 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9830 break;
9831 }
9832}
9833
26804afd 9834static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9835 struct intel_crtc_state *pipe_config)
26804afd
DV
9836{
9837 struct drm_device *dev = crtc->base.dev;
9838 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9839 struct intel_shared_dpll *pll;
26804afd
DV
9840 enum port port;
9841 uint32_t tmp;
9842
9843 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9844
9845 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9846
96b7dfb7
S
9847 if (IS_SKYLAKE(dev))
9848 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9849 else if (IS_BROXTON(dev))
9850 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9851 else
9852 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9853
d452c5b6
DV
9854 if (pipe_config->shared_dpll >= 0) {
9855 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9856
9857 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9858 &pipe_config->dpll_hw_state));
9859 }
9860
26804afd
DV
9861 /*
9862 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9863 * DDI E. So just check whether this pipe is wired to DDI E and whether
9864 * the PCH transcoder is on.
9865 */
ca370455
DL
9866 if (INTEL_INFO(dev)->gen < 9 &&
9867 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9868 pipe_config->has_pch_encoder = true;
9869
9870 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9871 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9872 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9873
9874 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9875 }
9876}
9877
0e8ffe1b 9878static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9879 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9880{
9881 struct drm_device *dev = crtc->base.dev;
9882 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 9883 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
9884 uint32_t tmp;
9885
f458ebbc 9886 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
9887 POWER_DOMAIN_PIPE(crtc->pipe)))
9888 return false;
9889
e143a21c 9890 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
9891 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9892
eccb140b
DV
9893 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9894 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9895 enum pipe trans_edp_pipe;
9896 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9897 default:
9898 WARN(1, "unknown pipe linked to edp transcoder\n");
9899 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9900 case TRANS_DDI_EDP_INPUT_A_ON:
9901 trans_edp_pipe = PIPE_A;
9902 break;
9903 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9904 trans_edp_pipe = PIPE_B;
9905 break;
9906 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9907 trans_edp_pipe = PIPE_C;
9908 break;
9909 }
9910
9911 if (trans_edp_pipe == crtc->pipe)
9912 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9913 }
9914
f458ebbc 9915 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 9916 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
9917 return false;
9918
eccb140b 9919 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
9920 if (!(tmp & PIPECONF_ENABLE))
9921 return false;
9922
26804afd 9923 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9924
1bd1bd80
DV
9925 intel_get_pipe_timings(crtc, pipe_config);
9926
a1b2278e
CK
9927 if (INTEL_INFO(dev)->gen >= 9) {
9928 skl_init_scalers(dev, crtc, pipe_config);
9929 }
9930
2fa2fe9a 9931 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
af99ceda
CK
9932
9933 if (INTEL_INFO(dev)->gen >= 9) {
9934 pipe_config->scaler_state.scaler_id = -1;
9935 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9936 }
9937
bd2e244f 9938 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
ff6d9f55 9939 if (INTEL_INFO(dev)->gen == 9)
bd2e244f 9940 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9941 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 9942 ironlake_get_pfit_config(crtc, pipe_config);
ff6d9f55
JB
9943 else
9944 MISSING_CASE(INTEL_INFO(dev)->gen);
bd2e244f 9945 }
88adfff1 9946
e59150dc
JB
9947 if (IS_HASWELL(dev))
9948 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9949 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9950
ebb69c95
CT
9951 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9952 pipe_config->pixel_multiplier =
9953 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9954 } else {
9955 pipe_config->pixel_multiplier = 1;
9956 }
6c49f241 9957
0e8ffe1b
DV
9958 return true;
9959}
9960
560b85bb
CW
9961static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9962{
9963 struct drm_device *dev = crtc->dev;
9964 struct drm_i915_private *dev_priv = dev->dev_private;
9965 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9966 uint32_t cntl = 0, size = 0;
560b85bb 9967
dc41c154 9968 if (base) {
3dd512fb
MR
9969 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9970 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
9971 unsigned int stride = roundup_pow_of_two(width) * 4;
9972
9973 switch (stride) {
9974 default:
9975 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9976 width, stride);
9977 stride = 256;
9978 /* fallthrough */
9979 case 256:
9980 case 512:
9981 case 1024:
9982 case 2048:
9983 break;
4b0e333e
CW
9984 }
9985
dc41c154
VS
9986 cntl |= CURSOR_ENABLE |
9987 CURSOR_GAMMA_ENABLE |
9988 CURSOR_FORMAT_ARGB |
9989 CURSOR_STRIDE(stride);
9990
9991 size = (height << 12) | width;
4b0e333e 9992 }
560b85bb 9993
dc41c154
VS
9994 if (intel_crtc->cursor_cntl != 0 &&
9995 (intel_crtc->cursor_base != base ||
9996 intel_crtc->cursor_size != size ||
9997 intel_crtc->cursor_cntl != cntl)) {
9998 /* On these chipsets we can only modify the base/size/stride
9999 * whilst the cursor is disabled.
10000 */
10001 I915_WRITE(_CURACNTR, 0);
4b0e333e 10002 POSTING_READ(_CURACNTR);
dc41c154 10003 intel_crtc->cursor_cntl = 0;
4b0e333e 10004 }
560b85bb 10005
99d1f387 10006 if (intel_crtc->cursor_base != base) {
9db4a9c7 10007 I915_WRITE(_CURABASE, base);
99d1f387
VS
10008 intel_crtc->cursor_base = base;
10009 }
4726e0b0 10010
dc41c154
VS
10011 if (intel_crtc->cursor_size != size) {
10012 I915_WRITE(CURSIZE, size);
10013 intel_crtc->cursor_size = size;
4b0e333e 10014 }
560b85bb 10015
4b0e333e 10016 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
10017 I915_WRITE(_CURACNTR, cntl);
10018 POSTING_READ(_CURACNTR);
4b0e333e 10019 intel_crtc->cursor_cntl = cntl;
560b85bb 10020 }
560b85bb
CW
10021}
10022
560b85bb 10023static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
10024{
10025 struct drm_device *dev = crtc->dev;
10026 struct drm_i915_private *dev_priv = dev->dev_private;
10027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10028 int pipe = intel_crtc->pipe;
4b0e333e
CW
10029 uint32_t cntl;
10030
10031 cntl = 0;
10032 if (base) {
10033 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 10034 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
10035 case 64:
10036 cntl |= CURSOR_MODE_64_ARGB_AX;
10037 break;
10038 case 128:
10039 cntl |= CURSOR_MODE_128_ARGB_AX;
10040 break;
10041 case 256:
10042 cntl |= CURSOR_MODE_256_ARGB_AX;
10043 break;
10044 default:
3dd512fb 10045 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 10046 return;
65a21cd6 10047 }
4b0e333e 10048 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
10049
10050 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
10051 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 10052 }
65a21cd6 10053
8e7d688b 10054 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
10055 cntl |= CURSOR_ROTATE_180;
10056
4b0e333e
CW
10057 if (intel_crtc->cursor_cntl != cntl) {
10058 I915_WRITE(CURCNTR(pipe), cntl);
10059 POSTING_READ(CURCNTR(pipe));
10060 intel_crtc->cursor_cntl = cntl;
65a21cd6 10061 }
4b0e333e 10062
65a21cd6 10063 /* and commit changes on next vblank */
5efb3e28
VS
10064 I915_WRITE(CURBASE(pipe), base);
10065 POSTING_READ(CURBASE(pipe));
99d1f387
VS
10066
10067 intel_crtc->cursor_base = base;
65a21cd6
JB
10068}
10069
cda4b7d3 10070/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
10071static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10072 bool on)
cda4b7d3
CW
10073{
10074 struct drm_device *dev = crtc->dev;
10075 struct drm_i915_private *dev_priv = dev->dev_private;
10076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10077 int pipe = intel_crtc->pipe;
3d7d6510
MR
10078 int x = crtc->cursor_x;
10079 int y = crtc->cursor_y;
d6e4db15 10080 u32 base = 0, pos = 0;
cda4b7d3 10081
d6e4db15 10082 if (on)
cda4b7d3 10083 base = intel_crtc->cursor_addr;
cda4b7d3 10084
6e3c9717 10085 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
10086 base = 0;
10087
6e3c9717 10088 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
10089 base = 0;
10090
10091 if (x < 0) {
3dd512fb 10092 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
cda4b7d3
CW
10093 base = 0;
10094
10095 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10096 x = -x;
10097 }
10098 pos |= x << CURSOR_X_SHIFT;
10099
10100 if (y < 0) {
3dd512fb 10101 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
cda4b7d3
CW
10102 base = 0;
10103
10104 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10105 y = -y;
10106 }
10107 pos |= y << CURSOR_Y_SHIFT;
10108
4b0e333e 10109 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
10110 return;
10111
5efb3e28
VS
10112 I915_WRITE(CURPOS(pipe), pos);
10113
4398ad45
VS
10114 /* ILK+ do this automagically */
10115 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 10116 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
3dd512fb
MR
10117 base += (intel_crtc->base.cursor->state->crtc_h *
10118 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
4398ad45
VS
10119 }
10120
8ac54669 10121 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
10122 i845_update_cursor(crtc, base);
10123 else
10124 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
10125}
10126
dc41c154
VS
10127static bool cursor_size_ok(struct drm_device *dev,
10128 uint32_t width, uint32_t height)
10129{
10130 if (width == 0 || height == 0)
10131 return false;
10132
10133 /*
10134 * 845g/865g are special in that they are only limited by
10135 * the width of their cursors, the height is arbitrary up to
10136 * the precision of the register. Everything else requires
10137 * square cursors, limited to a few power-of-two sizes.
10138 */
10139 if (IS_845G(dev) || IS_I865G(dev)) {
10140 if ((width & 63) != 0)
10141 return false;
10142
10143 if (width > (IS_845G(dev) ? 64 : 512))
10144 return false;
10145
10146 if (height > 1023)
10147 return false;
10148 } else {
10149 switch (width | height) {
10150 case 256:
10151 case 128:
10152 if (IS_GEN2(dev))
10153 return false;
10154 case 64:
10155 break;
10156 default:
10157 return false;
10158 }
10159 }
10160
10161 return true;
10162}
10163
79e53945 10164static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 10165 u16 *blue, uint32_t start, uint32_t size)
79e53945 10166{
7203425a 10167 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 10168 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 10169
7203425a 10170 for (i = start; i < end; i++) {
79e53945
JB
10171 intel_crtc->lut_r[i] = red[i] >> 8;
10172 intel_crtc->lut_g[i] = green[i] >> 8;
10173 intel_crtc->lut_b[i] = blue[i] >> 8;
10174 }
10175
10176 intel_crtc_load_lut(crtc);
10177}
10178
79e53945
JB
10179/* VESA 640x480x72Hz mode to set on the pipe */
10180static struct drm_display_mode load_detect_mode = {
10181 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10182 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10183};
10184
a8bb6818
DV
10185struct drm_framebuffer *
10186__intel_framebuffer_create(struct drm_device *dev,
10187 struct drm_mode_fb_cmd2 *mode_cmd,
10188 struct drm_i915_gem_object *obj)
d2dff872
CW
10189{
10190 struct intel_framebuffer *intel_fb;
10191 int ret;
10192
10193 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10194 if (!intel_fb) {
6ccb81f2 10195 drm_gem_object_unreference(&obj->base);
d2dff872
CW
10196 return ERR_PTR(-ENOMEM);
10197 }
10198
10199 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10200 if (ret)
10201 goto err;
d2dff872
CW
10202
10203 return &intel_fb->base;
dd4916c5 10204err:
6ccb81f2 10205 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
10206 kfree(intel_fb);
10207
10208 return ERR_PTR(ret);
d2dff872
CW
10209}
10210
b5ea642a 10211static struct drm_framebuffer *
a8bb6818
DV
10212intel_framebuffer_create(struct drm_device *dev,
10213 struct drm_mode_fb_cmd2 *mode_cmd,
10214 struct drm_i915_gem_object *obj)
10215{
10216 struct drm_framebuffer *fb;
10217 int ret;
10218
10219 ret = i915_mutex_lock_interruptible(dev);
10220 if (ret)
10221 return ERR_PTR(ret);
10222 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10223 mutex_unlock(&dev->struct_mutex);
10224
10225 return fb;
10226}
10227
d2dff872
CW
10228static u32
10229intel_framebuffer_pitch_for_width(int width, int bpp)
10230{
10231 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10232 return ALIGN(pitch, 64);
10233}
10234
10235static u32
10236intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10237{
10238 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10239 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10240}
10241
10242static struct drm_framebuffer *
10243intel_framebuffer_create_for_mode(struct drm_device *dev,
10244 struct drm_display_mode *mode,
10245 int depth, int bpp)
10246{
10247 struct drm_i915_gem_object *obj;
0fed39bd 10248 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10249
10250 obj = i915_gem_alloc_object(dev,
10251 intel_framebuffer_size_for_mode(mode, bpp));
10252 if (obj == NULL)
10253 return ERR_PTR(-ENOMEM);
10254
10255 mode_cmd.width = mode->hdisplay;
10256 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10257 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10258 bpp);
5ca0c34a 10259 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
10260
10261 return intel_framebuffer_create(dev, &mode_cmd, obj);
10262}
10263
10264static struct drm_framebuffer *
10265mode_fits_in_fbdev(struct drm_device *dev,
10266 struct drm_display_mode *mode)
10267{
4520f53a 10268#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
10269 struct drm_i915_private *dev_priv = dev->dev_private;
10270 struct drm_i915_gem_object *obj;
10271 struct drm_framebuffer *fb;
10272
4c0e5528 10273 if (!dev_priv->fbdev)
d2dff872
CW
10274 return NULL;
10275
4c0e5528 10276 if (!dev_priv->fbdev->fb)
d2dff872
CW
10277 return NULL;
10278
4c0e5528
DV
10279 obj = dev_priv->fbdev->fb->obj;
10280 BUG_ON(!obj);
10281
8bcd4553 10282 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10283 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10284 fb->bits_per_pixel))
d2dff872
CW
10285 return NULL;
10286
01f2c773 10287 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10288 return NULL;
10289
10290 return fb;
4520f53a
DV
10291#else
10292 return NULL;
10293#endif
d2dff872
CW
10294}
10295
d3a40d1b
ACO
10296static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10297 struct drm_crtc *crtc,
10298 struct drm_display_mode *mode,
10299 struct drm_framebuffer *fb,
10300 int x, int y)
10301{
10302 struct drm_plane_state *plane_state;
10303 int hdisplay, vdisplay;
10304 int ret;
10305
10306 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10307 if (IS_ERR(plane_state))
10308 return PTR_ERR(plane_state);
10309
10310 if (mode)
10311 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10312 else
10313 hdisplay = vdisplay = 0;
10314
10315 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10316 if (ret)
10317 return ret;
10318 drm_atomic_set_fb_for_plane(plane_state, fb);
10319 plane_state->crtc_x = 0;
10320 plane_state->crtc_y = 0;
10321 plane_state->crtc_w = hdisplay;
10322 plane_state->crtc_h = vdisplay;
10323 plane_state->src_x = x << 16;
10324 plane_state->src_y = y << 16;
10325 plane_state->src_w = hdisplay << 16;
10326 plane_state->src_h = vdisplay << 16;
10327
10328 return 0;
10329}
10330
d2434ab7 10331bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10332 struct drm_display_mode *mode,
51fd371b
RC
10333 struct intel_load_detect_pipe *old,
10334 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10335{
10336 struct intel_crtc *intel_crtc;
d2434ab7
DV
10337 struct intel_encoder *intel_encoder =
10338 intel_attached_encoder(connector);
79e53945 10339 struct drm_crtc *possible_crtc;
4ef69c7a 10340 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10341 struct drm_crtc *crtc = NULL;
10342 struct drm_device *dev = encoder->dev;
94352cf9 10343 struct drm_framebuffer *fb;
51fd371b 10344 struct drm_mode_config *config = &dev->mode_config;
83a57153 10345 struct drm_atomic_state *state = NULL;
944b0c76 10346 struct drm_connector_state *connector_state;
4be07317 10347 struct intel_crtc_state *crtc_state;
51fd371b 10348 int ret, i = -1;
79e53945 10349
d2dff872 10350 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10351 connector->base.id, connector->name,
8e329a03 10352 encoder->base.id, encoder->name);
d2dff872 10353
51fd371b
RC
10354retry:
10355 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10356 if (ret)
37ade417 10357 goto fail;
6e9f798d 10358
79e53945
JB
10359 /*
10360 * Algorithm gets a little messy:
7a5e4805 10361 *
79e53945
JB
10362 * - if the connector already has an assigned crtc, use it (but make
10363 * sure it's on first)
7a5e4805 10364 *
79e53945
JB
10365 * - try to find the first unused crtc that can drive this connector,
10366 * and use that if we find one
79e53945
JB
10367 */
10368
10369 /* See if we already have a CRTC for this connector */
10370 if (encoder->crtc) {
10371 crtc = encoder->crtc;
8261b191 10372
51fd371b 10373 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de 10374 if (ret)
37ade417 10375 goto fail;
4d02e2de 10376 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b 10377 if (ret)
37ade417 10378 goto fail;
7b24056b 10379
24218aac 10380 old->dpms_mode = connector->dpms;
8261b191
CW
10381 old->load_detect_temp = false;
10382
10383 /* Make sure the crtc and connector are running */
24218aac
DV
10384 if (connector->dpms != DRM_MODE_DPMS_ON)
10385 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 10386
7173188d 10387 return true;
79e53945
JB
10388 }
10389
10390 /* Find an unused one (if possible) */
70e1e0ec 10391 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10392 i++;
10393 if (!(encoder->possible_crtcs & (1 << i)))
10394 continue;
83d65738 10395 if (possible_crtc->state->enable)
a459249c 10396 continue;
a459249c
VS
10397
10398 crtc = possible_crtc;
10399 break;
79e53945
JB
10400 }
10401
10402 /*
10403 * If we didn't find an unused CRTC, don't use any.
10404 */
10405 if (!crtc) {
7173188d 10406 DRM_DEBUG_KMS("no pipe available for load-detect\n");
37ade417 10407 goto fail;
79e53945
JB
10408 }
10409
51fd371b
RC
10410 ret = drm_modeset_lock(&crtc->mutex, ctx);
10411 if (ret)
37ade417 10412 goto fail;
4d02e2de
DV
10413 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10414 if (ret)
37ade417 10415 goto fail;
79e53945
JB
10416
10417 intel_crtc = to_intel_crtc(crtc);
24218aac 10418 old->dpms_mode = connector->dpms;
8261b191 10419 old->load_detect_temp = true;
d2dff872 10420 old->release_fb = NULL;
79e53945 10421
83a57153
ACO
10422 state = drm_atomic_state_alloc(dev);
10423 if (!state)
10424 return false;
10425
10426 state->acquire_ctx = ctx;
10427
944b0c76
ACO
10428 connector_state = drm_atomic_get_connector_state(state, connector);
10429 if (IS_ERR(connector_state)) {
10430 ret = PTR_ERR(connector_state);
10431 goto fail;
10432 }
10433
10434 connector_state->crtc = crtc;
10435 connector_state->best_encoder = &intel_encoder->base;
10436
4be07317
ACO
10437 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10438 if (IS_ERR(crtc_state)) {
10439 ret = PTR_ERR(crtc_state);
10440 goto fail;
10441 }
10442
49d6fa21 10443 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10444
6492711d
CW
10445 if (!mode)
10446 mode = &load_detect_mode;
79e53945 10447
d2dff872
CW
10448 /* We need a framebuffer large enough to accommodate all accesses
10449 * that the plane may generate whilst we perform load detection.
10450 * We can not rely on the fbcon either being present (we get called
10451 * during its initialisation to detect all boot displays, or it may
10452 * not even exist) or that it is large enough to satisfy the
10453 * requested mode.
10454 */
94352cf9
DV
10455 fb = mode_fits_in_fbdev(dev, mode);
10456 if (fb == NULL) {
d2dff872 10457 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
10458 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10459 old->release_fb = fb;
d2dff872
CW
10460 } else
10461 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10462 if (IS_ERR(fb)) {
d2dff872 10463 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10464 goto fail;
79e53945 10465 }
79e53945 10466
d3a40d1b
ACO
10467 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10468 if (ret)
10469 goto fail;
10470
8c7b5ccb
ACO
10471 drm_mode_copy(&crtc_state->base.mode, mode);
10472
568c634a 10473 if (intel_set_mode(state)) {
6492711d 10474 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
10475 if (old->release_fb)
10476 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 10477 goto fail;
79e53945 10478 }
9128b040 10479 crtc->primary->crtc = crtc;
7173188d 10480
79e53945 10481 /* let the connector get through one full cycle before testing */
9d0498a2 10482 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10483 return true;
412b61d8 10484
37ade417 10485fail:
e5d958ef
ACO
10486 drm_atomic_state_free(state);
10487 state = NULL;
83a57153 10488
51fd371b
RC
10489 if (ret == -EDEADLK) {
10490 drm_modeset_backoff(ctx);
10491 goto retry;
10492 }
10493
412b61d8 10494 return false;
79e53945
JB
10495}
10496
d2434ab7 10497void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10498 struct intel_load_detect_pipe *old,
10499 struct drm_modeset_acquire_ctx *ctx)
79e53945 10500{
83a57153 10501 struct drm_device *dev = connector->dev;
d2434ab7
DV
10502 struct intel_encoder *intel_encoder =
10503 intel_attached_encoder(connector);
4ef69c7a 10504 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 10505 struct drm_crtc *crtc = encoder->crtc;
412b61d8 10506 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83a57153 10507 struct drm_atomic_state *state;
944b0c76 10508 struct drm_connector_state *connector_state;
4be07317 10509 struct intel_crtc_state *crtc_state;
d3a40d1b 10510 int ret;
79e53945 10511
d2dff872 10512 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10513 connector->base.id, connector->name,
8e329a03 10514 encoder->base.id, encoder->name);
d2dff872 10515
8261b191 10516 if (old->load_detect_temp) {
83a57153 10517 state = drm_atomic_state_alloc(dev);
944b0c76
ACO
10518 if (!state)
10519 goto fail;
83a57153
ACO
10520
10521 state->acquire_ctx = ctx;
10522
944b0c76
ACO
10523 connector_state = drm_atomic_get_connector_state(state, connector);
10524 if (IS_ERR(connector_state))
10525 goto fail;
10526
4be07317
ACO
10527 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10528 if (IS_ERR(crtc_state))
10529 goto fail;
10530
944b0c76
ACO
10531 connector_state->best_encoder = NULL;
10532 connector_state->crtc = NULL;
10533
49d6fa21 10534 crtc_state->base.enable = crtc_state->base.active = false;
4be07317 10535
d3a40d1b
ACO
10536 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10537 0, 0);
10538 if (ret)
10539 goto fail;
10540
568c634a 10541 ret = intel_set_mode(state);
2bfb4627
ACO
10542 if (ret)
10543 goto fail;
d2dff872 10544
36206361
DV
10545 if (old->release_fb) {
10546 drm_framebuffer_unregister_private(old->release_fb);
10547 drm_framebuffer_unreference(old->release_fb);
10548 }
d2dff872 10549
0622a53c 10550 return;
79e53945
JB
10551 }
10552
c751ce4f 10553 /* Switch crtc and encoder back off if necessary */
24218aac
DV
10554 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10555 connector->funcs->dpms(connector, old->dpms_mode);
944b0c76
ACO
10556
10557 return;
10558fail:
10559 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10560 drm_atomic_state_free(state);
79e53945
JB
10561}
10562
da4a1efa 10563static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10564 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10565{
10566 struct drm_i915_private *dev_priv = dev->dev_private;
10567 u32 dpll = pipe_config->dpll_hw_state.dpll;
10568
10569 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10570 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10571 else if (HAS_PCH_SPLIT(dev))
10572 return 120000;
10573 else if (!IS_GEN2(dev))
10574 return 96000;
10575 else
10576 return 48000;
10577}
10578
79e53945 10579/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10580static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10581 struct intel_crtc_state *pipe_config)
79e53945 10582{
f1f644dc 10583 struct drm_device *dev = crtc->base.dev;
79e53945 10584 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10585 int pipe = pipe_config->cpu_transcoder;
293623f7 10586 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10587 u32 fp;
10588 intel_clock_t clock;
da4a1efa 10589 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10590
10591 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10592 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10593 else
293623f7 10594 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10595
10596 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10597 if (IS_PINEVIEW(dev)) {
10598 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10599 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10600 } else {
10601 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10602 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10603 }
10604
a6c45cf0 10605 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10606 if (IS_PINEVIEW(dev))
10607 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10608 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10609 else
10610 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10611 DPLL_FPA01_P1_POST_DIV_SHIFT);
10612
10613 switch (dpll & DPLL_MODE_MASK) {
10614 case DPLLB_MODE_DAC_SERIAL:
10615 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10616 5 : 10;
10617 break;
10618 case DPLLB_MODE_LVDS:
10619 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10620 7 : 14;
10621 break;
10622 default:
28c97730 10623 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10624 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10625 return;
79e53945
JB
10626 }
10627
ac58c3f0 10628 if (IS_PINEVIEW(dev))
da4a1efa 10629 pineview_clock(refclk, &clock);
ac58c3f0 10630 else
da4a1efa 10631 i9xx_clock(refclk, &clock);
79e53945 10632 } else {
0fb58223 10633 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10634 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10635
10636 if (is_lvds) {
10637 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10638 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10639
10640 if (lvds & LVDS_CLKB_POWER_UP)
10641 clock.p2 = 7;
10642 else
10643 clock.p2 = 14;
79e53945
JB
10644 } else {
10645 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10646 clock.p1 = 2;
10647 else {
10648 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10649 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10650 }
10651 if (dpll & PLL_P2_DIVIDE_BY_4)
10652 clock.p2 = 4;
10653 else
10654 clock.p2 = 2;
79e53945 10655 }
da4a1efa
VS
10656
10657 i9xx_clock(refclk, &clock);
79e53945
JB
10658 }
10659
18442d08
VS
10660 /*
10661 * This value includes pixel_multiplier. We will use
241bfc38 10662 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10663 * encoder's get_config() function.
10664 */
10665 pipe_config->port_clock = clock.dot;
f1f644dc
JB
10666}
10667
6878da05
VS
10668int intel_dotclock_calculate(int link_freq,
10669 const struct intel_link_m_n *m_n)
f1f644dc 10670{
f1f644dc
JB
10671 /*
10672 * The calculation for the data clock is:
1041a02f 10673 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10674 * But we want to avoid losing precison if possible, so:
1041a02f 10675 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10676 *
10677 * and the link clock is simpler:
1041a02f 10678 * link_clock = (m * link_clock) / n
f1f644dc
JB
10679 */
10680
6878da05
VS
10681 if (!m_n->link_n)
10682 return 0;
f1f644dc 10683
6878da05
VS
10684 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10685}
f1f644dc 10686
18442d08 10687static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10688 struct intel_crtc_state *pipe_config)
6878da05
VS
10689{
10690 struct drm_device *dev = crtc->base.dev;
79e53945 10691
18442d08
VS
10692 /* read out port_clock from the DPLL */
10693 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10694
f1f644dc 10695 /*
18442d08 10696 * This value does not include pixel_multiplier.
241bfc38 10697 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
10698 * agree once we know their relationship in the encoder's
10699 * get_config() function.
79e53945 10700 */
2d112de7 10701 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
10702 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10703 &pipe_config->fdi_m_n);
79e53945
JB
10704}
10705
10706/** Returns the currently programmed mode of the given pipe. */
10707struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10708 struct drm_crtc *crtc)
10709{
548f245b 10710 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10711 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10712 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10713 struct drm_display_mode *mode;
5cec258b 10714 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
10715 int htot = I915_READ(HTOTAL(cpu_transcoder));
10716 int hsync = I915_READ(HSYNC(cpu_transcoder));
10717 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10718 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10719 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10720
10721 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10722 if (!mode)
10723 return NULL;
10724
f1f644dc
JB
10725 /*
10726 * Construct a pipe_config sufficient for getting the clock info
10727 * back out of crtc_clock_get.
10728 *
10729 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10730 * to use a real value here instead.
10731 */
293623f7 10732 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 10733 pipe_config.pixel_multiplier = 1;
293623f7
VS
10734 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10735 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10736 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
10737 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10738
773ae034 10739 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
10740 mode->hdisplay = (htot & 0xffff) + 1;
10741 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10742 mode->hsync_start = (hsync & 0xffff) + 1;
10743 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10744 mode->vdisplay = (vtot & 0xffff) + 1;
10745 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10746 mode->vsync_start = (vsync & 0xffff) + 1;
10747 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10748
10749 drm_mode_set_name(mode);
79e53945
JB
10750
10751 return mode;
10752}
10753
652c393a
JB
10754static void intel_decrease_pllclock(struct drm_crtc *crtc)
10755{
10756 struct drm_device *dev = crtc->dev;
fbee40df 10757 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10758 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 10759
baff296c 10760 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
10761 return;
10762
10763 if (!dev_priv->lvds_downclock_avail)
10764 return;
10765
10766 /*
10767 * Since this is called by a timer, we should never get here in
10768 * the manual case.
10769 */
10770 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
10771 int pipe = intel_crtc->pipe;
10772 int dpll_reg = DPLL(pipe);
10773 int dpll;
f6e5b160 10774
44d98a61 10775 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 10776
8ac5a6d5 10777 assert_panel_unlocked(dev_priv, pipe);
652c393a 10778
dc257cf1 10779 dpll = I915_READ(dpll_reg);
652c393a
JB
10780 dpll |= DISPLAY_RATE_SELECT_FPA1;
10781 I915_WRITE(dpll_reg, dpll);
9d0498a2 10782 intel_wait_for_vblank(dev, pipe);
652c393a
JB
10783 dpll = I915_READ(dpll_reg);
10784 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 10785 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
10786 }
10787
10788}
10789
f047e395
CW
10790void intel_mark_busy(struct drm_device *dev)
10791{
c67a470b
PZ
10792 struct drm_i915_private *dev_priv = dev->dev_private;
10793
f62a0076
CW
10794 if (dev_priv->mm.busy)
10795 return;
10796
43694d69 10797 intel_runtime_pm_get(dev_priv);
c67a470b 10798 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10799 if (INTEL_INFO(dev)->gen >= 6)
10800 gen6_rps_busy(dev_priv);
f62a0076 10801 dev_priv->mm.busy = true;
f047e395
CW
10802}
10803
10804void intel_mark_idle(struct drm_device *dev)
652c393a 10805{
c67a470b 10806 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10807 struct drm_crtc *crtc;
652c393a 10808
f62a0076
CW
10809 if (!dev_priv->mm.busy)
10810 return;
10811
10812 dev_priv->mm.busy = false;
10813
70e1e0ec 10814 for_each_crtc(dev, crtc) {
f4510a27 10815 if (!crtc->primary->fb)
652c393a
JB
10816 continue;
10817
725a5b54 10818 intel_decrease_pllclock(crtc);
652c393a 10819 }
b29c19b6 10820
3d13ef2e 10821 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10822 gen6_rps_idle(dev->dev_private);
bb4cdd53 10823
43694d69 10824 intel_runtime_pm_put(dev_priv);
652c393a
JB
10825}
10826
79e53945
JB
10827static void intel_crtc_destroy(struct drm_crtc *crtc)
10828{
10829 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10830 struct drm_device *dev = crtc->dev;
10831 struct intel_unpin_work *work;
67e77c5a 10832
5e2d7afc 10833 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10834 work = intel_crtc->unpin_work;
10835 intel_crtc->unpin_work = NULL;
5e2d7afc 10836 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10837
10838 if (work) {
10839 cancel_work_sync(&work->work);
10840 kfree(work);
10841 }
79e53945
JB
10842
10843 drm_crtc_cleanup(crtc);
67e77c5a 10844
79e53945
JB
10845 kfree(intel_crtc);
10846}
10847
6b95a207
KH
10848static void intel_unpin_work_fn(struct work_struct *__work)
10849{
10850 struct intel_unpin_work *work =
10851 container_of(__work, struct intel_unpin_work, work);
b4a98e57 10852 struct drm_device *dev = work->crtc->dev;
f99d7069 10853 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 10854
b4a98e57 10855 mutex_lock(&dev->struct_mutex);
82bc3b2d 10856 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
05394f39 10857 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10858
7ff0ebcc 10859 intel_fbc_update(dev);
f06cc1b9
JH
10860
10861 if (work->flip_queued_req)
146d84f0 10862 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10863 mutex_unlock(&dev->struct_mutex);
10864
f99d7069 10865 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
89ed88ba 10866 drm_framebuffer_unreference(work->old_fb);
f99d7069 10867
b4a98e57
CW
10868 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10869 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10870
6b95a207
KH
10871 kfree(work);
10872}
10873
1afe3e9d 10874static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10875 struct drm_crtc *crtc)
6b95a207 10876{
6b95a207
KH
10877 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10878 struct intel_unpin_work *work;
6b95a207
KH
10879 unsigned long flags;
10880
10881 /* Ignore early vblank irqs */
10882 if (intel_crtc == NULL)
10883 return;
10884
f326038a
DV
10885 /*
10886 * This is called both by irq handlers and the reset code (to complete
10887 * lost pageflips) so needs the full irqsave spinlocks.
10888 */
6b95a207
KH
10889 spin_lock_irqsave(&dev->event_lock, flags);
10890 work = intel_crtc->unpin_work;
e7d841ca
CW
10891
10892 /* Ensure we don't miss a work->pending update ... */
10893 smp_rmb();
10894
10895 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10896 spin_unlock_irqrestore(&dev->event_lock, flags);
10897 return;
10898 }
10899
d6bbafa1 10900 page_flip_completed(intel_crtc);
0af7e4df 10901
6b95a207 10902 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10903}
10904
1afe3e9d
JB
10905void intel_finish_page_flip(struct drm_device *dev, int pipe)
10906{
fbee40df 10907 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10908 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10909
49b14a5c 10910 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10911}
10912
10913void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10914{
fbee40df 10915 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10916 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10917
49b14a5c 10918 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10919}
10920
75f7f3ec
VS
10921/* Is 'a' after or equal to 'b'? */
10922static bool g4x_flip_count_after_eq(u32 a, u32 b)
10923{
10924 return !((a - b) & 0x80000000);
10925}
10926
10927static bool page_flip_finished(struct intel_crtc *crtc)
10928{
10929 struct drm_device *dev = crtc->base.dev;
10930 struct drm_i915_private *dev_priv = dev->dev_private;
10931
bdfa7542
VS
10932 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10933 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10934 return true;
10935
75f7f3ec
VS
10936 /*
10937 * The relevant registers doen't exist on pre-ctg.
10938 * As the flip done interrupt doesn't trigger for mmio
10939 * flips on gmch platforms, a flip count check isn't
10940 * really needed there. But since ctg has the registers,
10941 * include it in the check anyway.
10942 */
10943 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10944 return true;
10945
10946 /*
10947 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10948 * used the same base address. In that case the mmio flip might
10949 * have completed, but the CS hasn't even executed the flip yet.
10950 *
10951 * A flip count check isn't enough as the CS might have updated
10952 * the base address just after start of vblank, but before we
10953 * managed to process the interrupt. This means we'd complete the
10954 * CS flip too soon.
10955 *
10956 * Combining both checks should get us a good enough result. It may
10957 * still happen that the CS flip has been executed, but has not
10958 * yet actually completed. But in case the base address is the same
10959 * anyway, we don't really care.
10960 */
10961 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10962 crtc->unpin_work->gtt_offset &&
10963 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10964 crtc->unpin_work->flip_count);
10965}
10966
6b95a207
KH
10967void intel_prepare_page_flip(struct drm_device *dev, int plane)
10968{
fbee40df 10969 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10970 struct intel_crtc *intel_crtc =
10971 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10972 unsigned long flags;
10973
f326038a
DV
10974
10975 /*
10976 * This is called both by irq handlers and the reset code (to complete
10977 * lost pageflips) so needs the full irqsave spinlocks.
10978 *
10979 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10980 * generate a page-flip completion irq, i.e. every modeset
10981 * is also accompanied by a spurious intel_prepare_page_flip().
10982 */
6b95a207 10983 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10984 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10985 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10986 spin_unlock_irqrestore(&dev->event_lock, flags);
10987}
10988
eba905b2 10989static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
10990{
10991 /* Ensure that the work item is consistent when activating it ... */
10992 smp_wmb();
10993 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10994 /* and that it is marked active as soon as the irq could fire. */
10995 smp_wmb();
10996}
10997
8c9f3aaf
JB
10998static int intel_gen2_queue_flip(struct drm_device *dev,
10999 struct drm_crtc *crtc,
11000 struct drm_framebuffer *fb,
ed8d1975 11001 struct drm_i915_gem_object *obj,
a4872ba6 11002 struct intel_engine_cs *ring,
ed8d1975 11003 uint32_t flags)
8c9f3aaf 11004{
8c9f3aaf 11005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
11006 u32 flip_mask;
11007 int ret;
11008
6d90c952 11009 ret = intel_ring_begin(ring, 6);
8c9f3aaf 11010 if (ret)
4fa62c89 11011 return ret;
8c9f3aaf
JB
11012
11013 /* Can't queue multiple flips, so wait for the previous
11014 * one to finish before executing the next.
11015 */
11016 if (intel_crtc->plane)
11017 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11018 else
11019 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
11020 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11021 intel_ring_emit(ring, MI_NOOP);
11022 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11023 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11024 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 11025 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 11026 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
11027
11028 intel_mark_page_flip_active(intel_crtc);
09246732 11029 __intel_ring_advance(ring);
83d4092b 11030 return 0;
8c9f3aaf
JB
11031}
11032
11033static int intel_gen3_queue_flip(struct drm_device *dev,
11034 struct drm_crtc *crtc,
11035 struct drm_framebuffer *fb,
ed8d1975 11036 struct drm_i915_gem_object *obj,
a4872ba6 11037 struct intel_engine_cs *ring,
ed8d1975 11038 uint32_t flags)
8c9f3aaf 11039{
8c9f3aaf 11040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
11041 u32 flip_mask;
11042 int ret;
11043
6d90c952 11044 ret = intel_ring_begin(ring, 6);
8c9f3aaf 11045 if (ret)
4fa62c89 11046 return ret;
8c9f3aaf
JB
11047
11048 if (intel_crtc->plane)
11049 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11050 else
11051 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
11052 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11053 intel_ring_emit(ring, MI_NOOP);
11054 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11055 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11056 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 11057 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
11058 intel_ring_emit(ring, MI_NOOP);
11059
e7d841ca 11060 intel_mark_page_flip_active(intel_crtc);
09246732 11061 __intel_ring_advance(ring);
83d4092b 11062 return 0;
8c9f3aaf
JB
11063}
11064
11065static int intel_gen4_queue_flip(struct drm_device *dev,
11066 struct drm_crtc *crtc,
11067 struct drm_framebuffer *fb,
ed8d1975 11068 struct drm_i915_gem_object *obj,
a4872ba6 11069 struct intel_engine_cs *ring,
ed8d1975 11070 uint32_t flags)
8c9f3aaf
JB
11071{
11072 struct drm_i915_private *dev_priv = dev->dev_private;
11073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11074 uint32_t pf, pipesrc;
11075 int ret;
11076
6d90c952 11077 ret = intel_ring_begin(ring, 4);
8c9f3aaf 11078 if (ret)
4fa62c89 11079 return ret;
8c9f3aaf
JB
11080
11081 /* i965+ uses the linear or tiled offsets from the
11082 * Display Registers (which do not change across a page-flip)
11083 * so we need only reprogram the base address.
11084 */
6d90c952
DV
11085 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11086 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11087 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 11088 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 11089 obj->tiling_mode);
8c9f3aaf
JB
11090
11091 /* XXX Enabling the panel-fitter across page-flip is so far
11092 * untested on non-native modes, so ignore it for now.
11093 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11094 */
11095 pf = 0;
11096 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 11097 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
11098
11099 intel_mark_page_flip_active(intel_crtc);
09246732 11100 __intel_ring_advance(ring);
83d4092b 11101 return 0;
8c9f3aaf
JB
11102}
11103
11104static int intel_gen6_queue_flip(struct drm_device *dev,
11105 struct drm_crtc *crtc,
11106 struct drm_framebuffer *fb,
ed8d1975 11107 struct drm_i915_gem_object *obj,
a4872ba6 11108 struct intel_engine_cs *ring,
ed8d1975 11109 uint32_t flags)
8c9f3aaf
JB
11110{
11111 struct drm_i915_private *dev_priv = dev->dev_private;
11112 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11113 uint32_t pf, pipesrc;
11114 int ret;
11115
6d90c952 11116 ret = intel_ring_begin(ring, 4);
8c9f3aaf 11117 if (ret)
4fa62c89 11118 return ret;
8c9f3aaf 11119
6d90c952
DV
11120 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11121 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11122 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 11123 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 11124
dc257cf1
DV
11125 /* Contrary to the suggestions in the documentation,
11126 * "Enable Panel Fitter" does not seem to be required when page
11127 * flipping with a non-native mode, and worse causes a normal
11128 * modeset to fail.
11129 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11130 */
11131 pf = 0;
8c9f3aaf 11132 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 11133 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
11134
11135 intel_mark_page_flip_active(intel_crtc);
09246732 11136 __intel_ring_advance(ring);
83d4092b 11137 return 0;
8c9f3aaf
JB
11138}
11139
7c9017e5
JB
11140static int intel_gen7_queue_flip(struct drm_device *dev,
11141 struct drm_crtc *crtc,
11142 struct drm_framebuffer *fb,
ed8d1975 11143 struct drm_i915_gem_object *obj,
a4872ba6 11144 struct intel_engine_cs *ring,
ed8d1975 11145 uint32_t flags)
7c9017e5 11146{
7c9017e5 11147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 11148 uint32_t plane_bit = 0;
ffe74d75
CW
11149 int len, ret;
11150
eba905b2 11151 switch (intel_crtc->plane) {
cb05d8de
DV
11152 case PLANE_A:
11153 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11154 break;
11155 case PLANE_B:
11156 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11157 break;
11158 case PLANE_C:
11159 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11160 break;
11161 default:
11162 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 11163 return -ENODEV;
cb05d8de
DV
11164 }
11165
ffe74d75 11166 len = 4;
f476828a 11167 if (ring->id == RCS) {
ffe74d75 11168 len += 6;
f476828a
DL
11169 /*
11170 * On Gen 8, SRM is now taking an extra dword to accommodate
11171 * 48bits addresses, and we need a NOOP for the batch size to
11172 * stay even.
11173 */
11174 if (IS_GEN8(dev))
11175 len += 2;
11176 }
ffe74d75 11177
f66fab8e
VS
11178 /*
11179 * BSpec MI_DISPLAY_FLIP for IVB:
11180 * "The full packet must be contained within the same cache line."
11181 *
11182 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11183 * cacheline, if we ever start emitting more commands before
11184 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11185 * then do the cacheline alignment, and finally emit the
11186 * MI_DISPLAY_FLIP.
11187 */
11188 ret = intel_ring_cacheline_align(ring);
11189 if (ret)
4fa62c89 11190 return ret;
f66fab8e 11191
ffe74d75 11192 ret = intel_ring_begin(ring, len);
7c9017e5 11193 if (ret)
4fa62c89 11194 return ret;
7c9017e5 11195
ffe74d75
CW
11196 /* Unmask the flip-done completion message. Note that the bspec says that
11197 * we should do this for both the BCS and RCS, and that we must not unmask
11198 * more than one flip event at any time (or ensure that one flip message
11199 * can be sent by waiting for flip-done prior to queueing new flips).
11200 * Experimentation says that BCS works despite DERRMR masking all
11201 * flip-done completion events and that unmasking all planes at once
11202 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11203 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11204 */
11205 if (ring->id == RCS) {
11206 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11207 intel_ring_emit(ring, DERRMR);
11208 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11209 DERRMR_PIPEB_PRI_FLIP_DONE |
11210 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
11211 if (IS_GEN8(dev))
11212 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11213 MI_SRM_LRM_GLOBAL_GTT);
11214 else
11215 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11216 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
11217 intel_ring_emit(ring, DERRMR);
11218 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
11219 if (IS_GEN8(dev)) {
11220 intel_ring_emit(ring, 0);
11221 intel_ring_emit(ring, MI_NOOP);
11222 }
ffe74d75
CW
11223 }
11224
cb05d8de 11225 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 11226 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 11227 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 11228 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
11229
11230 intel_mark_page_flip_active(intel_crtc);
09246732 11231 __intel_ring_advance(ring);
83d4092b 11232 return 0;
7c9017e5
JB
11233}
11234
84c33a64
SG
11235static bool use_mmio_flip(struct intel_engine_cs *ring,
11236 struct drm_i915_gem_object *obj)
11237{
11238 /*
11239 * This is not being used for older platforms, because
11240 * non-availability of flip done interrupt forces us to use
11241 * CS flips. Older platforms derive flip done using some clever
11242 * tricks involving the flip_pending status bits and vblank irqs.
11243 * So using MMIO flips there would disrupt this mechanism.
11244 */
11245
8e09bf83
CW
11246 if (ring == NULL)
11247 return true;
11248
84c33a64
SG
11249 if (INTEL_INFO(ring->dev)->gen < 5)
11250 return false;
11251
11252 if (i915.use_mmio_flip < 0)
11253 return false;
11254 else if (i915.use_mmio_flip > 0)
11255 return true;
14bf993e
OM
11256 else if (i915.enable_execlists)
11257 return true;
84c33a64 11258 else
b4716185 11259 return ring != i915_gem_request_get_ring(obj->last_write_req);
84c33a64
SG
11260}
11261
ff944564
DL
11262static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11263{
11264 struct drm_device *dev = intel_crtc->base.dev;
11265 struct drm_i915_private *dev_priv = dev->dev_private;
11266 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564
DL
11267 const enum pipe pipe = intel_crtc->pipe;
11268 u32 ctl, stride;
11269
11270 ctl = I915_READ(PLANE_CTL(pipe, 0));
11271 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11272 switch (fb->modifier[0]) {
11273 case DRM_FORMAT_MOD_NONE:
11274 break;
11275 case I915_FORMAT_MOD_X_TILED:
ff944564 11276 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11277 break;
11278 case I915_FORMAT_MOD_Y_TILED:
11279 ctl |= PLANE_CTL_TILED_Y;
11280 break;
11281 case I915_FORMAT_MOD_Yf_TILED:
11282 ctl |= PLANE_CTL_TILED_YF;
11283 break;
11284 default:
11285 MISSING_CASE(fb->modifier[0]);
11286 }
ff944564
DL
11287
11288 /*
11289 * The stride is either expressed as a multiple of 64 bytes chunks for
11290 * linear buffers or in number of tiles for tiled buffers.
11291 */
2ebef630
TU
11292 stride = fb->pitches[0] /
11293 intel_fb_stride_alignment(dev, fb->modifier[0],
11294 fb->pixel_format);
ff944564
DL
11295
11296 /*
11297 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11298 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11299 */
11300 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11301 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11302
11303 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11304 POSTING_READ(PLANE_SURF(pipe, 0));
11305}
11306
11307static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
11308{
11309 struct drm_device *dev = intel_crtc->base.dev;
11310 struct drm_i915_private *dev_priv = dev->dev_private;
11311 struct intel_framebuffer *intel_fb =
11312 to_intel_framebuffer(intel_crtc->base.primary->fb);
11313 struct drm_i915_gem_object *obj = intel_fb->obj;
11314 u32 dspcntr;
11315 u32 reg;
11316
84c33a64
SG
11317 reg = DSPCNTR(intel_crtc->plane);
11318 dspcntr = I915_READ(reg);
11319
c5d97472
DL
11320 if (obj->tiling_mode != I915_TILING_NONE)
11321 dspcntr |= DISPPLANE_TILED;
11322 else
11323 dspcntr &= ~DISPPLANE_TILED;
11324
84c33a64
SG
11325 I915_WRITE(reg, dspcntr);
11326
11327 I915_WRITE(DSPSURF(intel_crtc->plane),
11328 intel_crtc->unpin_work->gtt_offset);
11329 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 11330
ff944564
DL
11331}
11332
11333/*
11334 * XXX: This is the temporary way to update the plane registers until we get
11335 * around to using the usual plane update functions for MMIO flips
11336 */
11337static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11338{
11339 struct drm_device *dev = intel_crtc->base.dev;
11340 bool atomic_update;
11341 u32 start_vbl_count;
11342
11343 intel_mark_page_flip_active(intel_crtc);
11344
11345 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11346
11347 if (INTEL_INFO(dev)->gen >= 9)
11348 skl_do_mmio_flip(intel_crtc);
11349 else
11350 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11351 ilk_do_mmio_flip(intel_crtc);
11352
9362c7c5
ACO
11353 if (atomic_update)
11354 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
11355}
11356
9362c7c5 11357static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11358{
b2cfe0ab
CW
11359 struct intel_mmio_flip *mmio_flip =
11360 container_of(work, struct intel_mmio_flip, work);
84c33a64 11361
eed29a5b
DV
11362 if (mmio_flip->req)
11363 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11364 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11365 false, NULL,
11366 &mmio_flip->i915->rps.mmioflips));
84c33a64 11367
b2cfe0ab
CW
11368 intel_do_mmio_flip(mmio_flip->crtc);
11369
eed29a5b 11370 i915_gem_request_unreference__unlocked(mmio_flip->req);
b2cfe0ab 11371 kfree(mmio_flip);
84c33a64
SG
11372}
11373
11374static int intel_queue_mmio_flip(struct drm_device *dev,
11375 struct drm_crtc *crtc,
11376 struct drm_framebuffer *fb,
11377 struct drm_i915_gem_object *obj,
11378 struct intel_engine_cs *ring,
11379 uint32_t flags)
11380{
b2cfe0ab
CW
11381 struct intel_mmio_flip *mmio_flip;
11382
11383 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11384 if (mmio_flip == NULL)
11385 return -ENOMEM;
84c33a64 11386
bcafc4e3 11387 mmio_flip->i915 = to_i915(dev);
eed29a5b 11388 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11389 mmio_flip->crtc = to_intel_crtc(crtc);
536f5b5e 11390
b2cfe0ab
CW
11391 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11392 schedule_work(&mmio_flip->work);
84c33a64 11393
84c33a64
SG
11394 return 0;
11395}
11396
8c9f3aaf
JB
11397static int intel_default_queue_flip(struct drm_device *dev,
11398 struct drm_crtc *crtc,
11399 struct drm_framebuffer *fb,
ed8d1975 11400 struct drm_i915_gem_object *obj,
a4872ba6 11401 struct intel_engine_cs *ring,
ed8d1975 11402 uint32_t flags)
8c9f3aaf
JB
11403{
11404 return -ENODEV;
11405}
11406
d6bbafa1
CW
11407static bool __intel_pageflip_stall_check(struct drm_device *dev,
11408 struct drm_crtc *crtc)
11409{
11410 struct drm_i915_private *dev_priv = dev->dev_private;
11411 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11412 struct intel_unpin_work *work = intel_crtc->unpin_work;
11413 u32 addr;
11414
11415 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11416 return true;
11417
11418 if (!work->enable_stall_check)
11419 return false;
11420
11421 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11422 if (work->flip_queued_req &&
11423 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11424 return false;
11425
1e3feefd 11426 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11427 }
11428
1e3feefd 11429 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11430 return false;
11431
11432 /* Potential stall - if we see that the flip has happened,
11433 * assume a missed interrupt. */
11434 if (INTEL_INFO(dev)->gen >= 4)
11435 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11436 else
11437 addr = I915_READ(DSPADDR(intel_crtc->plane));
11438
11439 /* There is a potential issue here with a false positive after a flip
11440 * to the same address. We could address this by checking for a
11441 * non-incrementing frame counter.
11442 */
11443 return addr == work->gtt_offset;
11444}
11445
11446void intel_check_page_flip(struct drm_device *dev, int pipe)
11447{
11448 struct drm_i915_private *dev_priv = dev->dev_private;
11449 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11451 struct intel_unpin_work *work;
f326038a 11452
6c51d46f 11453 WARN_ON(!in_interrupt());
d6bbafa1
CW
11454
11455 if (crtc == NULL)
11456 return;
11457
f326038a 11458 spin_lock(&dev->event_lock);
6ad790c0
CW
11459 work = intel_crtc->unpin_work;
11460 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11461 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11462 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11463 page_flip_completed(intel_crtc);
6ad790c0 11464 work = NULL;
d6bbafa1 11465 }
6ad790c0
CW
11466 if (work != NULL &&
11467 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11468 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11469 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11470}
11471
6b95a207
KH
11472static int intel_crtc_page_flip(struct drm_crtc *crtc,
11473 struct drm_framebuffer *fb,
ed8d1975
KP
11474 struct drm_pending_vblank_event *event,
11475 uint32_t page_flip_flags)
6b95a207
KH
11476{
11477 struct drm_device *dev = crtc->dev;
11478 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11479 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11480 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11481 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11482 struct drm_plane *primary = crtc->primary;
a071fa00 11483 enum pipe pipe = intel_crtc->pipe;
6b95a207 11484 struct intel_unpin_work *work;
a4872ba6 11485 struct intel_engine_cs *ring;
cf5d8a46 11486 bool mmio_flip;
52e68630 11487 int ret;
6b95a207 11488
2ff8fde1
MR
11489 /*
11490 * drm_mode_page_flip_ioctl() should already catch this, but double
11491 * check to be safe. In the future we may enable pageflipping from
11492 * a disabled primary plane.
11493 */
11494 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11495 return -EBUSY;
11496
e6a595d2 11497 /* Can't change pixel format via MI display flips. */
f4510a27 11498 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11499 return -EINVAL;
11500
11501 /*
11502 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11503 * Note that pitch changes could also affect these register.
11504 */
11505 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11506 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11507 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11508 return -EINVAL;
11509
f900db47
CW
11510 if (i915_terminally_wedged(&dev_priv->gpu_error))
11511 goto out_hang;
11512
b14c5679 11513 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11514 if (work == NULL)
11515 return -ENOMEM;
11516
6b95a207 11517 work->event = event;
b4a98e57 11518 work->crtc = crtc;
ab8d6675 11519 work->old_fb = old_fb;
6b95a207
KH
11520 INIT_WORK(&work->work, intel_unpin_work_fn);
11521
87b6b101 11522 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11523 if (ret)
11524 goto free_work;
11525
6b95a207 11526 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11527 spin_lock_irq(&dev->event_lock);
6b95a207 11528 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11529 /* Before declaring the flip queue wedged, check if
11530 * the hardware completed the operation behind our backs.
11531 */
11532 if (__intel_pageflip_stall_check(dev, crtc)) {
11533 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11534 page_flip_completed(intel_crtc);
11535 } else {
11536 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11537 spin_unlock_irq(&dev->event_lock);
468f0b44 11538
d6bbafa1
CW
11539 drm_crtc_vblank_put(crtc);
11540 kfree(work);
11541 return -EBUSY;
11542 }
6b95a207
KH
11543 }
11544 intel_crtc->unpin_work = work;
5e2d7afc 11545 spin_unlock_irq(&dev->event_lock);
6b95a207 11546
b4a98e57
CW
11547 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11548 flush_workqueue(dev_priv->wq);
11549
75dfca80 11550 /* Reference the objects for the scheduled work. */
ab8d6675 11551 drm_framebuffer_reference(work->old_fb);
05394f39 11552 drm_gem_object_reference(&obj->base);
6b95a207 11553
f4510a27 11554 crtc->primary->fb = fb;
afd65eb4 11555 update_state_fb(crtc->primary);
1ed1f968 11556
e1f99ce6 11557 work->pending_flip_obj = obj;
e1f99ce6 11558
89ed88ba
CW
11559 ret = i915_mutex_lock_interruptible(dev);
11560 if (ret)
11561 goto cleanup;
11562
b4a98e57 11563 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11564 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11565
75f7f3ec 11566 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 11567 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 11568
4fa62c89
VS
11569 if (IS_VALLEYVIEW(dev)) {
11570 ring = &dev_priv->ring[BCS];
ab8d6675 11571 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
11572 /* vlv: DISPLAY_FLIP fails to change tiling */
11573 ring = NULL;
48bf5b2d 11574 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 11575 ring = &dev_priv->ring[BCS];
4fa62c89 11576 } else if (INTEL_INFO(dev)->gen >= 7) {
b4716185 11577 ring = i915_gem_request_get_ring(obj->last_write_req);
4fa62c89
VS
11578 if (ring == NULL || ring->id != RCS)
11579 ring = &dev_priv->ring[BCS];
11580 } else {
11581 ring = &dev_priv->ring[RCS];
11582 }
11583
cf5d8a46
CW
11584 mmio_flip = use_mmio_flip(ring, obj);
11585
11586 /* When using CS flips, we want to emit semaphores between rings.
11587 * However, when using mmio flips we will create a task to do the
11588 * synchronisation, so all we want here is to pin the framebuffer
11589 * into the display plane and skip any waits.
11590 */
82bc3b2d 11591 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
cf5d8a46 11592 crtc->primary->state,
b4716185 11593 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
8c9f3aaf
JB
11594 if (ret)
11595 goto cleanup_pending;
6b95a207 11596
121920fa
TU
11597 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11598 + intel_crtc->dspaddr_offset;
4fa62c89 11599
cf5d8a46 11600 if (mmio_flip) {
84c33a64
SG
11601 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11602 page_flip_flags);
d6bbafa1
CW
11603 if (ret)
11604 goto cleanup_unpin;
11605
f06cc1b9
JH
11606 i915_gem_request_assign(&work->flip_queued_req,
11607 obj->last_write_req);
d6bbafa1 11608 } else {
d94b5030
CW
11609 if (obj->last_write_req) {
11610 ret = i915_gem_check_olr(obj->last_write_req);
11611 if (ret)
11612 goto cleanup_unpin;
11613 }
11614
84c33a64 11615 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
11616 page_flip_flags);
11617 if (ret)
11618 goto cleanup_unpin;
11619
f06cc1b9
JH
11620 i915_gem_request_assign(&work->flip_queued_req,
11621 intel_ring_get_request(ring));
d6bbafa1
CW
11622 }
11623
1e3feefd 11624 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11625 work->enable_stall_check = true;
4fa62c89 11626
ab8d6675 11627 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a071fa00
DV
11628 INTEL_FRONTBUFFER_PRIMARY(pipe));
11629
7ff0ebcc 11630 intel_fbc_disable(dev);
f99d7069 11631 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
11632 mutex_unlock(&dev->struct_mutex);
11633
e5510fac
JB
11634 trace_i915_flip_request(intel_crtc->plane, obj);
11635
6b95a207 11636 return 0;
96b099fd 11637
4fa62c89 11638cleanup_unpin:
82bc3b2d 11639 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 11640cleanup_pending:
b4a98e57 11641 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11642 mutex_unlock(&dev->struct_mutex);
11643cleanup:
f4510a27 11644 crtc->primary->fb = old_fb;
afd65eb4 11645 update_state_fb(crtc->primary);
89ed88ba
CW
11646
11647 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11648 drm_framebuffer_unreference(work->old_fb);
96b099fd 11649
5e2d7afc 11650 spin_lock_irq(&dev->event_lock);
96b099fd 11651 intel_crtc->unpin_work = NULL;
5e2d7afc 11652 spin_unlock_irq(&dev->event_lock);
96b099fd 11653
87b6b101 11654 drm_crtc_vblank_put(crtc);
7317c75e 11655free_work:
96b099fd
CW
11656 kfree(work);
11657
f900db47
CW
11658 if (ret == -EIO) {
11659out_hang:
53a366b9 11660 ret = intel_plane_restore(primary);
f0d3dad3 11661 if (ret == 0 && event) {
5e2d7afc 11662 spin_lock_irq(&dev->event_lock);
a071fa00 11663 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11664 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11665 }
f900db47 11666 }
96b099fd 11667 return ret;
6b95a207
KH
11668}
11669
65b38e0d 11670static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11671 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11672 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11673 .atomic_begin = intel_begin_crtc_commit,
11674 .atomic_flush = intel_finish_crtc_commit,
f6e5b160
CW
11675};
11676
d29b2f9d
ACO
11677/* Transitional helper to copy current connector/encoder state to
11678 * connector->state. This is needed so that code that is partially
11679 * converted to atomic does the right thing.
11680 */
11681static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11682{
11683 struct intel_connector *connector;
11684
11685 for_each_intel_connector(dev, connector) {
11686 if (connector->base.encoder) {
11687 connector->base.state->best_encoder =
11688 connector->base.encoder;
11689 connector->base.state->crtc =
11690 connector->base.encoder->crtc;
11691 } else {
11692 connector->base.state->best_encoder = NULL;
11693 connector->base.state->crtc = NULL;
11694 }
11695 }
11696}
11697
050f7aeb 11698static void
eba905b2 11699connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11700 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11701{
11702 int bpp = pipe_config->pipe_bpp;
11703
11704 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11705 connector->base.base.id,
c23cc417 11706 connector->base.name);
050f7aeb
DV
11707
11708 /* Don't use an invalid EDID bpc value */
11709 if (connector->base.display_info.bpc &&
11710 connector->base.display_info.bpc * 3 < bpp) {
11711 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11712 bpp, connector->base.display_info.bpc*3);
11713 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11714 }
11715
11716 /* Clamp bpp to 8 on screens without EDID 1.4 */
11717 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11718 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11719 bpp);
11720 pipe_config->pipe_bpp = 24;
11721 }
11722}
11723
4e53c2e0 11724static int
050f7aeb 11725compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11726 struct intel_crtc_state *pipe_config)
4e53c2e0 11727{
050f7aeb 11728 struct drm_device *dev = crtc->base.dev;
1486017f 11729 struct drm_atomic_state *state;
da3ced29
ACO
11730 struct drm_connector *connector;
11731 struct drm_connector_state *connector_state;
1486017f 11732 int bpp, i;
4e53c2e0 11733
d328c9d7 11734 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
4e53c2e0 11735 bpp = 10*3;
d328c9d7
DV
11736 else if (INTEL_INFO(dev)->gen >= 5)
11737 bpp = 12*3;
11738 else
11739 bpp = 8*3;
11740
4e53c2e0 11741
4e53c2e0
DV
11742 pipe_config->pipe_bpp = bpp;
11743
1486017f
ACO
11744 state = pipe_config->base.state;
11745
4e53c2e0 11746 /* Clamp display bpp to EDID value */
da3ced29
ACO
11747 for_each_connector_in_state(state, connector, connector_state, i) {
11748 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11749 continue;
11750
da3ced29
ACO
11751 connected_sink_compute_bpp(to_intel_connector(connector),
11752 pipe_config);
4e53c2e0
DV
11753 }
11754
11755 return bpp;
11756}
11757
644db711
DV
11758static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11759{
11760 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11761 "type: 0x%x flags: 0x%x\n",
1342830c 11762 mode->crtc_clock,
644db711
DV
11763 mode->crtc_hdisplay, mode->crtc_hsync_start,
11764 mode->crtc_hsync_end, mode->crtc_htotal,
11765 mode->crtc_vdisplay, mode->crtc_vsync_start,
11766 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11767}
11768
c0b03411 11769static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11770 struct intel_crtc_state *pipe_config,
c0b03411
DV
11771 const char *context)
11772{
6a60cd87
CK
11773 struct drm_device *dev = crtc->base.dev;
11774 struct drm_plane *plane;
11775 struct intel_plane *intel_plane;
11776 struct intel_plane_state *state;
11777 struct drm_framebuffer *fb;
11778
11779 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11780 context, pipe_config, pipe_name(crtc->pipe));
c0b03411
DV
11781
11782 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11783 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11784 pipe_config->pipe_bpp, pipe_config->dither);
11785 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11786 pipe_config->has_pch_encoder,
11787 pipe_config->fdi_lanes,
11788 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11789 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11790 pipe_config->fdi_m_n.tu);
eb14cb74
VS
11791 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11792 pipe_config->has_dp_encoder,
11793 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11794 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11795 pipe_config->dp_m_n.tu);
b95af8be
VK
11796
11797 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11798 pipe_config->has_dp_encoder,
11799 pipe_config->dp_m2_n2.gmch_m,
11800 pipe_config->dp_m2_n2.gmch_n,
11801 pipe_config->dp_m2_n2.link_m,
11802 pipe_config->dp_m2_n2.link_n,
11803 pipe_config->dp_m2_n2.tu);
11804
55072d19
DV
11805 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11806 pipe_config->has_audio,
11807 pipe_config->has_infoframe);
11808
c0b03411 11809 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11810 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11811 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11812 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11813 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 11814 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
11815 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11816 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
11817 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11818 crtc->num_scalers,
11819 pipe_config->scaler_state.scaler_users,
11820 pipe_config->scaler_state.scaler_id);
c0b03411
DV
11821 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11822 pipe_config->gmch_pfit.control,
11823 pipe_config->gmch_pfit.pgm_ratios,
11824 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 11825 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 11826 pipe_config->pch_pfit.pos,
fd4daa9c
CW
11827 pipe_config->pch_pfit.size,
11828 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 11829 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 11830 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 11831
415ff0f6
TU
11832 if (IS_BROXTON(dev)) {
11833 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11834 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11835 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11836 pipe_config->ddi_pll_sel,
11837 pipe_config->dpll_hw_state.ebb0,
11838 pipe_config->dpll_hw_state.pll0,
11839 pipe_config->dpll_hw_state.pll1,
11840 pipe_config->dpll_hw_state.pll2,
11841 pipe_config->dpll_hw_state.pll3,
11842 pipe_config->dpll_hw_state.pll6,
11843 pipe_config->dpll_hw_state.pll8,
11844 pipe_config->dpll_hw_state.pcsdw12);
11845 } else if (IS_SKYLAKE(dev)) {
11846 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11847 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11848 pipe_config->ddi_pll_sel,
11849 pipe_config->dpll_hw_state.ctrl1,
11850 pipe_config->dpll_hw_state.cfgcr1,
11851 pipe_config->dpll_hw_state.cfgcr2);
11852 } else if (HAS_DDI(dev)) {
11853 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11854 pipe_config->ddi_pll_sel,
11855 pipe_config->dpll_hw_state.wrpll);
11856 } else {
11857 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11858 "fp0: 0x%x, fp1: 0x%x\n",
11859 pipe_config->dpll_hw_state.dpll,
11860 pipe_config->dpll_hw_state.dpll_md,
11861 pipe_config->dpll_hw_state.fp0,
11862 pipe_config->dpll_hw_state.fp1);
11863 }
11864
6a60cd87
CK
11865 DRM_DEBUG_KMS("planes on this crtc\n");
11866 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11867 intel_plane = to_intel_plane(plane);
11868 if (intel_plane->pipe != crtc->pipe)
11869 continue;
11870
11871 state = to_intel_plane_state(plane->state);
11872 fb = state->base.fb;
11873 if (!fb) {
11874 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11875 "disabled, scaler_id = %d\n",
11876 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11877 plane->base.id, intel_plane->pipe,
11878 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11879 drm_plane_index(plane), state->scaler_id);
11880 continue;
11881 }
11882
11883 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11884 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11885 plane->base.id, intel_plane->pipe,
11886 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11887 drm_plane_index(plane));
11888 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11889 fb->base.id, fb->width, fb->height, fb->pixel_format);
11890 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11891 state->scaler_id,
11892 state->src.x1 >> 16, state->src.y1 >> 16,
11893 drm_rect_width(&state->src) >> 16,
11894 drm_rect_height(&state->src) >> 16,
11895 state->dst.x1, state->dst.y1,
11896 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11897 }
c0b03411
DV
11898}
11899
bc079e8b
VS
11900static bool encoders_cloneable(const struct intel_encoder *a,
11901 const struct intel_encoder *b)
accfc0c5 11902{
bc079e8b
VS
11903 /* masks could be asymmetric, so check both ways */
11904 return a == b || (a->cloneable & (1 << b->type) &&
11905 b->cloneable & (1 << a->type));
11906}
11907
98a221da
ACO
11908static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11909 struct intel_crtc *crtc,
bc079e8b
VS
11910 struct intel_encoder *encoder)
11911{
bc079e8b 11912 struct intel_encoder *source_encoder;
da3ced29 11913 struct drm_connector *connector;
98a221da
ACO
11914 struct drm_connector_state *connector_state;
11915 int i;
bc079e8b 11916
da3ced29 11917 for_each_connector_in_state(state, connector, connector_state, i) {
98a221da 11918 if (connector_state->crtc != &crtc->base)
bc079e8b
VS
11919 continue;
11920
98a221da
ACO
11921 source_encoder =
11922 to_intel_encoder(connector_state->best_encoder);
bc079e8b
VS
11923 if (!encoders_cloneable(encoder, source_encoder))
11924 return false;
11925 }
11926
11927 return true;
11928}
11929
98a221da
ACO
11930static bool check_encoder_cloning(struct drm_atomic_state *state,
11931 struct intel_crtc *crtc)
bc079e8b 11932{
accfc0c5 11933 struct intel_encoder *encoder;
da3ced29 11934 struct drm_connector *connector;
98a221da
ACO
11935 struct drm_connector_state *connector_state;
11936 int i;
accfc0c5 11937
da3ced29 11938 for_each_connector_in_state(state, connector, connector_state, i) {
98a221da
ACO
11939 if (connector_state->crtc != &crtc->base)
11940 continue;
11941
11942 encoder = to_intel_encoder(connector_state->best_encoder);
11943 if (!check_single_encoder_cloning(state, crtc, encoder))
bc079e8b 11944 return false;
accfc0c5
DV
11945 }
11946
bc079e8b 11947 return true;
accfc0c5
DV
11948}
11949
5448a00d 11950static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11951{
5448a00d
ACO
11952 struct drm_device *dev = state->dev;
11953 struct intel_encoder *encoder;
da3ced29 11954 struct drm_connector *connector;
5448a00d 11955 struct drm_connector_state *connector_state;
00f0b378 11956 unsigned int used_ports = 0;
5448a00d 11957 int i;
00f0b378
VS
11958
11959 /*
11960 * Walk the connector list instead of the encoder
11961 * list to detect the problem on ddi platforms
11962 * where there's just one encoder per digital port.
11963 */
da3ced29 11964 for_each_connector_in_state(state, connector, connector_state, i) {
5448a00d 11965 if (!connector_state->best_encoder)
00f0b378
VS
11966 continue;
11967
5448a00d
ACO
11968 encoder = to_intel_encoder(connector_state->best_encoder);
11969
11970 WARN_ON(!connector_state->crtc);
00f0b378
VS
11971
11972 switch (encoder->type) {
11973 unsigned int port_mask;
11974 case INTEL_OUTPUT_UNKNOWN:
11975 if (WARN_ON(!HAS_DDI(dev)))
11976 break;
11977 case INTEL_OUTPUT_DISPLAYPORT:
11978 case INTEL_OUTPUT_HDMI:
11979 case INTEL_OUTPUT_EDP:
11980 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11981
11982 /* the same port mustn't appear more than once */
11983 if (used_ports & port_mask)
11984 return false;
11985
11986 used_ports |= port_mask;
11987 default:
11988 break;
11989 }
11990 }
11991
11992 return true;
11993}
11994
83a57153
ACO
11995static void
11996clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11997{
11998 struct drm_crtc_state tmp_state;
663a3640 11999 struct intel_crtc_scaler_state scaler_state;
4978cc93
ACO
12000 struct intel_dpll_hw_state dpll_hw_state;
12001 enum intel_dpll_id shared_dpll;
8504c74c 12002 uint32_t ddi_pll_sel;
83a57153 12003
7546a384
ACO
12004 /* FIXME: before the switch to atomic started, a new pipe_config was
12005 * kzalloc'd. Code that depends on any field being zero should be
12006 * fixed, so that the crtc_state can be safely duplicated. For now,
12007 * only fields that are know to not cause problems are preserved. */
12008
83a57153 12009 tmp_state = crtc_state->base;
663a3640 12010 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12011 shared_dpll = crtc_state->shared_dpll;
12012 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12013 ddi_pll_sel = crtc_state->ddi_pll_sel;
4978cc93 12014
83a57153 12015 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12016
83a57153 12017 crtc_state->base = tmp_state;
663a3640 12018 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12019 crtc_state->shared_dpll = shared_dpll;
12020 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12021 crtc_state->ddi_pll_sel = ddi_pll_sel;
83a57153
ACO
12022}
12023
548ee15b 12024static int
b8cecdf5 12025intel_modeset_pipe_config(struct drm_crtc *crtc,
568c634a 12026 struct drm_atomic_state *state)
ee7b9f93 12027{
568c634a
ACO
12028 struct drm_crtc_state *crtc_state;
12029 struct intel_crtc_state *pipe_config;
7758a113 12030 struct intel_encoder *encoder;
da3ced29 12031 struct drm_connector *connector;
0b901879 12032 struct drm_connector_state *connector_state;
d328c9d7 12033 int base_bpp, ret = -EINVAL;
0b901879 12034 int i;
e29c22c0 12035 bool retry = true;
ee7b9f93 12036
98a221da 12037 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
accfc0c5 12038 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
548ee15b 12039 return -EINVAL;
accfc0c5
DV
12040 }
12041
5448a00d 12042 if (!check_digital_port_conflicts(state)) {
00f0b378 12043 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
548ee15b 12044 return -EINVAL;
00f0b378
VS
12045 }
12046
568c634a
ACO
12047 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12048 if (WARN_ON(!crtc_state))
12049 return -EINVAL;
12050
12051 pipe_config = to_intel_crtc_state(crtc_state);
12052
cdba954e
ACO
12053 /*
12054 * XXX: Add all connectors to make the crtc state match the encoders.
12055 */
12056 if (!needs_modeset(&pipe_config->base)) {
12057 ret = drm_atomic_add_affected_connectors(state, crtc);
12058 if (ret)
12059 return ret;
12060 }
12061
83a57153 12062 clear_intel_crtc_state(pipe_config);
7758a113 12063
e143a21c
DV
12064 pipe_config->cpu_transcoder =
12065 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12066
2960bc9c
ID
12067 /*
12068 * Sanitize sync polarity flags based on requested ones. If neither
12069 * positive or negative polarity is requested, treat this as meaning
12070 * negative polarity.
12071 */
2d112de7 12072 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12073 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12074 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12075
2d112de7 12076 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12077 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12078 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12079
050f7aeb
DV
12080 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12081 * plane pixel format and any sink constraints into account. Returns the
12082 * source plane bpp so that dithering can be selected on mismatches
12083 * after encoders and crtc also have had their say. */
d328c9d7
DV
12084 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12085 pipe_config);
12086 if (base_bpp < 0)
4e53c2e0
DV
12087 goto fail;
12088
e41a56be
VS
12089 /*
12090 * Determine the real pipe dimensions. Note that stereo modes can
12091 * increase the actual pipe size due to the frame doubling and
12092 * insertion of additional space for blanks between the frame. This
12093 * is stored in the crtc timings. We use the requested mode to do this
12094 * computation to clearly distinguish it from the adjusted mode, which
12095 * can be changed by the connectors in the below retry loop.
12096 */
2d112de7 12097 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12098 &pipe_config->pipe_src_w,
12099 &pipe_config->pipe_src_h);
e41a56be 12100
e29c22c0 12101encoder_retry:
ef1b460d 12102 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12103 pipe_config->port_clock = 0;
ef1b460d 12104 pipe_config->pixel_multiplier = 1;
ff9a6750 12105
135c81b8 12106 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12107 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12108 CRTC_STEREO_DOUBLE);
135c81b8 12109
7758a113
DV
12110 /* Pass our mode to the connectors and the CRTC to give them a chance to
12111 * adjust it according to limitations or connector properties, and also
12112 * a chance to reject the mode entirely.
47f1c6c9 12113 */
da3ced29 12114 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12115 if (connector_state->crtc != crtc)
7758a113 12116 continue;
7ae89233 12117
0b901879
ACO
12118 encoder = to_intel_encoder(connector_state->best_encoder);
12119
efea6e8e
DV
12120 if (!(encoder->compute_config(encoder, pipe_config))) {
12121 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12122 goto fail;
12123 }
ee7b9f93 12124 }
47f1c6c9 12125
ff9a6750
DV
12126 /* Set default port clock if not overwritten by the encoder. Needs to be
12127 * done afterwards in case the encoder adjusts the mode. */
12128 if (!pipe_config->port_clock)
2d112de7 12129 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12130 * pipe_config->pixel_multiplier;
ff9a6750 12131
a43f6e0f 12132 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12133 if (ret < 0) {
7758a113
DV
12134 DRM_DEBUG_KMS("CRTC fixup failed\n");
12135 goto fail;
ee7b9f93 12136 }
e29c22c0
DV
12137
12138 if (ret == RETRY) {
12139 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12140 ret = -EINVAL;
12141 goto fail;
12142 }
12143
12144 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12145 retry = false;
12146 goto encoder_retry;
12147 }
12148
d328c9d7 12149 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
4e53c2e0 12150 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12151 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12152
cdba954e
ACO
12153 /* Check if we need to force a modeset */
12154 if (pipe_config->has_audio !=
85a96e7a 12155 to_intel_crtc_state(crtc->state)->has_audio) {
cdba954e 12156 pipe_config->base.mode_changed = true;
85a96e7a
ML
12157 ret = drm_atomic_add_affected_planes(state, crtc);
12158 }
cdba954e
ACO
12159
12160 /*
12161 * Note we have an issue here with infoframes: current code
12162 * only updates them on the full mode set path per hw
12163 * requirements. So here we should be checking for any
12164 * required changes and forcing a mode set.
12165 */
7758a113 12166fail:
548ee15b 12167 return ret;
ee7b9f93 12168}
47f1c6c9 12169
ea9d758d 12170static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 12171{
ea9d758d 12172 struct drm_encoder *encoder;
f6e5b160 12173 struct drm_device *dev = crtc->dev;
f6e5b160 12174
ea9d758d
DV
12175 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12176 if (encoder->crtc == crtc)
12177 return true;
12178
12179 return false;
12180}
12181
12182static void
0a9ab303 12183intel_modeset_update_state(struct drm_atomic_state *state)
ea9d758d 12184{
0a9ab303 12185 struct drm_device *dev = state->dev;
ea9d758d 12186 struct intel_encoder *intel_encoder;
0a9ab303
ACO
12187 struct drm_crtc *crtc;
12188 struct drm_crtc_state *crtc_state;
ea9d758d
DV
12189 struct drm_connector *connector;
12190
de419ab6 12191 intel_shared_dpll_commit(state);
69024de8 12192 drm_atomic_helper_swap_state(state->dev, state);
ba41c0de 12193
b2784e15 12194 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
12195 if (!intel_encoder->base.crtc)
12196 continue;
12197
69024de8
ML
12198 crtc = intel_encoder->base.crtc;
12199 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12200 if (!crtc_state || !needs_modeset(crtc->state))
12201 continue;
ea9d758d 12202
69024de8 12203 intel_encoder->connectors_active = false;
ea9d758d
DV
12204 }
12205
3cb480bc 12206 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
ea9d758d 12207
7668851f 12208 /* Double check state. */
0a9ab303
ACO
12209 for_each_crtc(dev, crtc) {
12210 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
3cb480bc
ML
12211
12212 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
ea9d758d
DV
12213 }
12214
12215 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12216 if (!connector->encoder || !connector->encoder->crtc)
12217 continue;
12218
69024de8
ML
12219 crtc = connector->encoder->crtc;
12220 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12221 if (!crtc_state || !needs_modeset(crtc->state))
12222 continue;
ea9d758d 12223
53d9f4e9 12224 if (crtc->state->active) {
69024de8
ML
12225 struct drm_property *dpms_property =
12226 dev->mode_config.dpms_property;
68d34720 12227
69024de8
ML
12228 connector->dpms = DRM_MODE_DPMS_ON;
12229 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
ea9d758d 12230
69024de8
ML
12231 intel_encoder = to_intel_encoder(connector->encoder);
12232 intel_encoder->connectors_active = true;
12233 } else
12234 connector->dpms = DRM_MODE_DPMS_OFF;
ea9d758d 12235 }
ea9d758d
DV
12236}
12237
3bd26263 12238static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12239{
3bd26263 12240 int diff;
f1f644dc
JB
12241
12242 if (clock1 == clock2)
12243 return true;
12244
12245 if (!clock1 || !clock2)
12246 return false;
12247
12248 diff = abs(clock1 - clock2);
12249
12250 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12251 return true;
12252
12253 return false;
12254}
12255
25c5b266
DV
12256#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12257 list_for_each_entry((intel_crtc), \
12258 &(dev)->mode_config.crtc_list, \
12259 base.head) \
0973f18f 12260 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12261
0e8ffe1b 12262static bool
2fa2fe9a 12263intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
12264 struct intel_crtc_state *current_config,
12265 struct intel_crtc_state *pipe_config)
0e8ffe1b 12266{
66e985c0
DV
12267#define PIPE_CONF_CHECK_X(name) \
12268 if (current_config->name != pipe_config->name) { \
12269 DRM_ERROR("mismatch in " #name " " \
12270 "(expected 0x%08x, found 0x%08x)\n", \
12271 current_config->name, \
12272 pipe_config->name); \
12273 return false; \
12274 }
12275
08a24034
DV
12276#define PIPE_CONF_CHECK_I(name) \
12277 if (current_config->name != pipe_config->name) { \
12278 DRM_ERROR("mismatch in " #name " " \
12279 "(expected %i, found %i)\n", \
12280 current_config->name, \
12281 pipe_config->name); \
12282 return false; \
88adfff1
DV
12283 }
12284
b95af8be
VK
12285/* This is required for BDW+ where there is only one set of registers for
12286 * switching between high and low RR.
12287 * This macro can be used whenever a comparison has to be made between one
12288 * hw state and multiple sw state variables.
12289 */
12290#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12291 if ((current_config->name != pipe_config->name) && \
12292 (current_config->alt_name != pipe_config->name)) { \
12293 DRM_ERROR("mismatch in " #name " " \
12294 "(expected %i or %i, found %i)\n", \
12295 current_config->name, \
12296 current_config->alt_name, \
12297 pipe_config->name); \
12298 return false; \
12299 }
12300
1bd1bd80
DV
12301#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12302 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 12303 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12304 "(expected %i, found %i)\n", \
12305 current_config->name & (mask), \
12306 pipe_config->name & (mask)); \
12307 return false; \
12308 }
12309
5e550656
VS
12310#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12311 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12312 DRM_ERROR("mismatch in " #name " " \
12313 "(expected %i, found %i)\n", \
12314 current_config->name, \
12315 pipe_config->name); \
12316 return false; \
12317 }
12318
bb760063
DV
12319#define PIPE_CONF_QUIRK(quirk) \
12320 ((current_config->quirks | pipe_config->quirks) & (quirk))
12321
eccb140b
DV
12322 PIPE_CONF_CHECK_I(cpu_transcoder);
12323
08a24034
DV
12324 PIPE_CONF_CHECK_I(has_pch_encoder);
12325 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
12326 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12327 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12328 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12329 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12330 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 12331
eb14cb74 12332 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
12333
12334 if (INTEL_INFO(dev)->gen < 8) {
12335 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12336 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12337 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12338 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12339 PIPE_CONF_CHECK_I(dp_m_n.tu);
12340
12341 if (current_config->has_drrs) {
12342 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12343 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12344 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12345 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12346 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12347 }
12348 } else {
12349 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12350 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12351 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12352 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12353 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12354 }
eb14cb74 12355
2d112de7
ACO
12356 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12357 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12358 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12359 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12360 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12361 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12362
2d112de7
ACO
12363 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12364 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12365 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12366 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12367 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12368 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12369
c93f54cf 12370 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12371 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
12372 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12373 IS_VALLEYVIEW(dev))
12374 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12375 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12376
9ed109a7
DV
12377 PIPE_CONF_CHECK_I(has_audio);
12378
2d112de7 12379 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12380 DRM_MODE_FLAG_INTERLACE);
12381
bb760063 12382 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12383 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12384 DRM_MODE_FLAG_PHSYNC);
2d112de7 12385 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12386 DRM_MODE_FLAG_NHSYNC);
2d112de7 12387 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12388 DRM_MODE_FLAG_PVSYNC);
2d112de7 12389 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12390 DRM_MODE_FLAG_NVSYNC);
12391 }
045ac3b5 12392
37327abd
VS
12393 PIPE_CONF_CHECK_I(pipe_src_w);
12394 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 12395
9953599b
DV
12396 /*
12397 * FIXME: BIOS likes to set up a cloned config with lvds+external
12398 * screen. Since we don't yet re-compute the pipe config when moving
12399 * just the lvds port away to another pipe the sw tracking won't match.
12400 *
12401 * Proper atomic modesets with recomputed global state will fix this.
12402 * Until then just don't check gmch state for inherited modes.
12403 */
12404 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12405 PIPE_CONF_CHECK_I(gmch_pfit.control);
12406 /* pfit ratios are autocomputed by the hw on gen4+ */
12407 if (INTEL_INFO(dev)->gen < 4)
12408 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12409 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12410 }
12411
fd4daa9c
CW
12412 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12413 if (current_config->pch_pfit.enabled) {
12414 PIPE_CONF_CHECK_I(pch_pfit.pos);
12415 PIPE_CONF_CHECK_I(pch_pfit.size);
12416 }
2fa2fe9a 12417
a1b2278e
CK
12418 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12419
e59150dc
JB
12420 /* BDW+ don't expose a synchronous way to read the state */
12421 if (IS_HASWELL(dev))
12422 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12423
282740f7
VS
12424 PIPE_CONF_CHECK_I(double_wide);
12425
26804afd
DV
12426 PIPE_CONF_CHECK_X(ddi_pll_sel);
12427
c0d43d62 12428 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 12429 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12430 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12431 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12432 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12433 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
12434 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12435 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12436 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12437
42571aef
VS
12438 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12439 PIPE_CONF_CHECK_I(pipe_bpp);
12440
2d112de7 12441 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12442 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12443
66e985c0 12444#undef PIPE_CONF_CHECK_X
08a24034 12445#undef PIPE_CONF_CHECK_I
b95af8be 12446#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12447#undef PIPE_CONF_CHECK_FLAGS
5e550656 12448#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12449#undef PIPE_CONF_QUIRK
88adfff1 12450
0e8ffe1b
DV
12451 return true;
12452}
12453
08db6652
DL
12454static void check_wm_state(struct drm_device *dev)
12455{
12456 struct drm_i915_private *dev_priv = dev->dev_private;
12457 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12458 struct intel_crtc *intel_crtc;
12459 int plane;
12460
12461 if (INTEL_INFO(dev)->gen < 9)
12462 return;
12463
12464 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12465 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12466
12467 for_each_intel_crtc(dev, intel_crtc) {
12468 struct skl_ddb_entry *hw_entry, *sw_entry;
12469 const enum pipe pipe = intel_crtc->pipe;
12470
12471 if (!intel_crtc->active)
12472 continue;
12473
12474 /* planes */
dd740780 12475 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12476 hw_entry = &hw_ddb.plane[pipe][plane];
12477 sw_entry = &sw_ddb->plane[pipe][plane];
12478
12479 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12480 continue;
12481
12482 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12483 "(expected (%u,%u), found (%u,%u))\n",
12484 pipe_name(pipe), plane + 1,
12485 sw_entry->start, sw_entry->end,
12486 hw_entry->start, hw_entry->end);
12487 }
12488
12489 /* cursor */
12490 hw_entry = &hw_ddb.cursor[pipe];
12491 sw_entry = &sw_ddb->cursor[pipe];
12492
12493 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12494 continue;
12495
12496 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12497 "(expected (%u,%u), found (%u,%u))\n",
12498 pipe_name(pipe),
12499 sw_entry->start, sw_entry->end,
12500 hw_entry->start, hw_entry->end);
12501 }
12502}
12503
91d1b4bd
DV
12504static void
12505check_connector_state(struct drm_device *dev)
8af6cf88 12506{
8af6cf88
DV
12507 struct intel_connector *connector;
12508
3a3371ff 12509 for_each_intel_connector(dev, connector) {
37ade417
ACO
12510 struct drm_encoder *encoder = connector->base.encoder;
12511 struct drm_connector_state *state = connector->base.state;
12512
8af6cf88
DV
12513 /* This also checks the encoder/connector hw state with the
12514 * ->get_hw_state callbacks. */
12515 intel_connector_check_state(connector);
12516
37ade417 12517 I915_STATE_WARN(state->best_encoder != encoder,
8af6cf88
DV
12518 "connector's staged encoder doesn't match current encoder\n");
12519 }
91d1b4bd
DV
12520}
12521
12522static void
12523check_encoder_state(struct drm_device *dev)
12524{
12525 struct intel_encoder *encoder;
12526 struct intel_connector *connector;
8af6cf88 12527
b2784e15 12528 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12529 bool enabled = false;
12530 bool active = false;
12531 enum pipe pipe, tracked_pipe;
12532
12533 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12534 encoder->base.base.id,
8e329a03 12535 encoder->base.name);
8af6cf88 12536
e2c719b7 12537 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
12538 "encoder's active_connectors set, but no crtc\n");
12539
3a3371ff 12540 for_each_intel_connector(dev, connector) {
8af6cf88
DV
12541 if (connector->base.encoder != &encoder->base)
12542 continue;
12543 enabled = true;
12544 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12545 active = true;
37ade417
ACO
12546
12547 I915_STATE_WARN(connector->base.state->crtc != encoder->base.crtc,
12548 "encoder's stage crtc doesn't match current crtc\n");
8af6cf88 12549 }
0e32b39c
DA
12550 /*
12551 * for MST connectors if we unplug the connector is gone
12552 * away but the encoder is still connected to a crtc
12553 * until a modeset happens in response to the hotplug.
12554 */
12555 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12556 continue;
12557
e2c719b7 12558 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12559 "encoder's enabled state mismatch "
12560 "(expected %i, found %i)\n",
12561 !!encoder->base.crtc, enabled);
e2c719b7 12562 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
12563 "active encoder with no crtc\n");
12564
e2c719b7 12565 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
12566 "encoder's computed active state doesn't match tracked active state "
12567 "(expected %i, found %i)\n", active, encoder->connectors_active);
12568
12569 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 12570 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
12571 "encoder's hw state doesn't match sw tracking "
12572 "(expected %i, found %i)\n",
12573 encoder->connectors_active, active);
12574
12575 if (!encoder->base.crtc)
12576 continue;
12577
12578 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 12579 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
12580 "active encoder's pipe doesn't match"
12581 "(expected %i, found %i)\n",
12582 tracked_pipe, pipe);
12583
12584 }
91d1b4bd
DV
12585}
12586
12587static void
12588check_crtc_state(struct drm_device *dev)
12589{
fbee40df 12590 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12591 struct intel_crtc *crtc;
12592 struct intel_encoder *encoder;
5cec258b 12593 struct intel_crtc_state pipe_config;
8af6cf88 12594
d3fcc808 12595 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
12596 bool enabled = false;
12597 bool active = false;
12598
045ac3b5
JB
12599 memset(&pipe_config, 0, sizeof(pipe_config));
12600
8af6cf88
DV
12601 DRM_DEBUG_KMS("[CRTC:%d]\n",
12602 crtc->base.base.id);
12603
83d65738 12604 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
8af6cf88
DV
12605 "active crtc, but not enabled in sw tracking\n");
12606
b2784e15 12607 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12608 if (encoder->base.crtc != &crtc->base)
12609 continue;
12610 enabled = true;
12611 if (encoder->connectors_active)
12612 active = true;
12613 }
6c49f241 12614
e2c719b7 12615 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
12616 "crtc's computed active state doesn't match tracked active state "
12617 "(expected %i, found %i)\n", active, crtc->active);
83d65738 12618 I915_STATE_WARN(enabled != crtc->base.state->enable,
8af6cf88 12619 "crtc's computed enabled state doesn't match tracked enabled state "
83d65738
MR
12620 "(expected %i, found %i)\n", enabled,
12621 crtc->base.state->enable);
8af6cf88 12622
0e8ffe1b
DV
12623 active = dev_priv->display.get_pipe_config(crtc,
12624 &pipe_config);
d62cf62a 12625
b6b5d049
VS
12626 /* hw state is inconsistent with the pipe quirk */
12627 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12628 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
12629 active = crtc->active;
12630
b2784e15 12631 for_each_intel_encoder(dev, encoder) {
3eaba51c 12632 enum pipe pipe;
6c49f241
DV
12633 if (encoder->base.crtc != &crtc->base)
12634 continue;
1d37b689 12635 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
12636 encoder->get_config(encoder, &pipe_config);
12637 }
12638
e2c719b7 12639 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
12640 "crtc active state doesn't match with hw state "
12641 "(expected %i, found %i)\n", crtc->active, active);
12642
53d9f4e9
ML
12643 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12644 "transitional active state does not match atomic hw state "
12645 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12646
c0b03411 12647 if (active &&
6e3c9717 12648 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 12649 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
12650 intel_dump_pipe_config(crtc, &pipe_config,
12651 "[hw state]");
6e3c9717 12652 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
12653 "[sw state]");
12654 }
8af6cf88
DV
12655 }
12656}
12657
91d1b4bd
DV
12658static void
12659check_shared_dpll_state(struct drm_device *dev)
12660{
fbee40df 12661 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12662 struct intel_crtc *crtc;
12663 struct intel_dpll_hw_state dpll_hw_state;
12664 int i;
5358901f
DV
12665
12666 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12667 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12668 int enabled_crtcs = 0, active_crtcs = 0;
12669 bool active;
12670
12671 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12672
12673 DRM_DEBUG_KMS("%s\n", pll->name);
12674
12675 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12676
e2c719b7 12677 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12678 "more active pll users than references: %i vs %i\n",
3e369b76 12679 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12680 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12681 "pll in active use but not on in sw tracking\n");
e2c719b7 12682 I915_STATE_WARN(pll->on && !pll->active,
35c95375 12683 "pll in on but not on in use in sw tracking\n");
e2c719b7 12684 I915_STATE_WARN(pll->on != active,
5358901f
DV
12685 "pll on state mismatch (expected %i, found %i)\n",
12686 pll->on, active);
12687
d3fcc808 12688 for_each_intel_crtc(dev, crtc) {
83d65738 12689 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
12690 enabled_crtcs++;
12691 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12692 active_crtcs++;
12693 }
e2c719b7 12694 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
12695 "pll active crtcs mismatch (expected %i, found %i)\n",
12696 pll->active, active_crtcs);
e2c719b7 12697 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 12698 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 12699 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 12700
e2c719b7 12701 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12702 sizeof(dpll_hw_state)),
12703 "pll hw state mismatch\n");
5358901f 12704 }
8af6cf88
DV
12705}
12706
91d1b4bd
DV
12707void
12708intel_modeset_check_state(struct drm_device *dev)
12709{
08db6652 12710 check_wm_state(dev);
91d1b4bd
DV
12711 check_connector_state(dev);
12712 check_encoder_state(dev);
12713 check_crtc_state(dev);
12714 check_shared_dpll_state(dev);
12715}
12716
5cec258b 12717void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
12718 int dotclock)
12719{
12720 /*
12721 * FDI already provided one idea for the dotclock.
12722 * Yell if the encoder disagrees.
12723 */
2d112de7 12724 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 12725 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 12726 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
12727}
12728
80715b2f
VS
12729static void update_scanline_offset(struct intel_crtc *crtc)
12730{
12731 struct drm_device *dev = crtc->base.dev;
12732
12733 /*
12734 * The scanline counter increments at the leading edge of hsync.
12735 *
12736 * On most platforms it starts counting from vtotal-1 on the
12737 * first active line. That means the scanline counter value is
12738 * always one less than what we would expect. Ie. just after
12739 * start of vblank, which also occurs at start of hsync (on the
12740 * last active line), the scanline counter will read vblank_start-1.
12741 *
12742 * On gen2 the scanline counter starts counting from 1 instead
12743 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12744 * to keep the value positive), instead of adding one.
12745 *
12746 * On HSW+ the behaviour of the scanline counter depends on the output
12747 * type. For DP ports it behaves like most other platforms, but on HDMI
12748 * there's an extra 1 line difference. So we need to add two instead of
12749 * one to the value.
12750 */
12751 if (IS_GEN2(dev)) {
6e3c9717 12752 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12753 int vtotal;
12754
12755 vtotal = mode->crtc_vtotal;
12756 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12757 vtotal /= 2;
12758
12759 crtc->scanline_offset = vtotal - 1;
12760 } else if (HAS_DDI(dev) &&
409ee761 12761 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12762 crtc->scanline_offset = 2;
12763 } else
12764 crtc->scanline_offset = 1;
12765}
12766
c347a676 12767static int intel_modeset_setup_plls(struct drm_atomic_state *state)
ed6739ef 12768{
225da59b 12769 struct drm_device *dev = state->dev;
ed6739ef 12770 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303 12771 unsigned clear_pipes = 0;
ed6739ef 12772 struct intel_crtc *intel_crtc;
0a9ab303
ACO
12773 struct intel_crtc_state *intel_crtc_state;
12774 struct drm_crtc *crtc;
12775 struct drm_crtc_state *crtc_state;
ed6739ef 12776 int ret = 0;
0a9ab303 12777 int i;
ed6739ef
ACO
12778
12779 if (!dev_priv->display.crtc_compute_clock)
12780 return 0;
12781
0a9ab303
ACO
12782 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12783 intel_crtc = to_intel_crtc(crtc);
4978cc93 12784 intel_crtc_state = to_intel_crtc_state(crtc_state);
0a9ab303 12785
4978cc93 12786 if (needs_modeset(crtc_state)) {
0a9ab303 12787 clear_pipes |= 1 << intel_crtc->pipe;
4978cc93 12788 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
4978cc93 12789 }
0a9ab303
ACO
12790 }
12791
de419ab6
ML
12792 if (clear_pipes) {
12793 struct intel_shared_dpll_config *shared_dpll =
12794 intel_atomic_get_shared_dpll_state(state);
12795
12796 for (i = 0; i < dev_priv->num_shared_dpll; i++)
12797 shared_dpll[i].crtc_mask &= ~clear_pipes;
12798 }
ed6739ef 12799
0a9ab303
ACO
12800 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12801 if (!needs_modeset(crtc_state) || !crtc_state->enable)
225da59b
ACO
12802 continue;
12803
0a9ab303
ACO
12804 intel_crtc = to_intel_crtc(crtc);
12805 intel_crtc_state = to_intel_crtc_state(crtc_state);
12806
ed6739ef 12807 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
0a9ab303 12808 intel_crtc_state);
de419ab6
ML
12809 if (ret)
12810 return ret;
ed6739ef
ACO
12811 }
12812
ed6739ef
ACO
12813 return ret;
12814}
12815
054518dd 12816/* Code that should eventually be part of atomic_check() */
c347a676 12817static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd
ACO
12818{
12819 struct drm_device *dev = state->dev;
12820 int ret;
12821
12822 /*
12823 * See if the config requires any additional preparation, e.g.
12824 * to adjust global state with pipes off. We need to do this
12825 * here so we can get the modeset_pipe updated config for the new
12826 * mode set on this crtc. For other crtcs we need to use the
12827 * adjusted_mode bits in the crtc directly.
12828 */
b432e5cf
VS
12829 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
12830 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
12831 ret = valleyview_modeset_global_pipes(state);
12832 else
12833 ret = broadwell_modeset_global_pipes(state);
12834
054518dd
ACO
12835 if (ret)
12836 return ret;
12837 }
12838
c347a676
ACO
12839 return intel_modeset_setup_plls(state);
12840}
12841
12842static int
12843intel_modeset_compute_config(struct drm_atomic_state *state)
12844{
12845 struct drm_crtc *crtc;
12846 struct drm_crtc_state *crtc_state;
12847 int ret, i;
12848
12849 ret = drm_atomic_helper_check_modeset(state->dev, state);
054518dd
ACO
12850 if (ret)
12851 return ret;
12852
c347a676
ACO
12853 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12854 if (!crtc_state->enable &&
12855 WARN_ON(crtc_state->active))
12856 crtc_state->active = false;
12857
12858 if (!crtc_state->enable)
12859 continue;
12860
12861 ret = intel_modeset_pipe_config(crtc, state);
12862 if (ret)
12863 return ret;
12864
12865 intel_dump_pipe_config(to_intel_crtc(crtc),
12866 to_intel_crtc_state(crtc_state),
12867 "[modeset]");
12868 }
12869
12870 ret = intel_modeset_checks(state);
12871 if (ret)
12872 return ret;
12873
12874 return drm_atomic_helper_check_planes(state->dev, state);
054518dd
ACO
12875}
12876
c72d969b 12877static int __intel_set_mode(struct drm_atomic_state *state)
a6778b3c 12878{
c72d969b 12879 struct drm_device *dev = state->dev;
fbee40df 12880 struct drm_i915_private *dev_priv = dev->dev_private;
0a9ab303
ACO
12881 struct drm_crtc *crtc;
12882 struct drm_crtc_state *crtc_state;
c0c36b94 12883 int ret = 0;
0a9ab303 12884 int i;
a6778b3c 12885
d4afb8cc
ACO
12886 ret = drm_atomic_helper_prepare_planes(dev, state);
12887 if (ret)
12888 return ret;
12889
0a9ab303 12890 for_each_crtc_in_state(state, crtc, crtc_state, i) {
53d9f4e9 12891 if (!needs_modeset(crtc_state) || !crtc->state->active)
0a9ab303 12892 continue;
460da916 12893
69024de8
ML
12894 intel_crtc_disable_planes(crtc);
12895 dev_priv->display.crtc_disable(crtc);
ea9d758d 12896 }
a6778b3c 12897
ea9d758d
DV
12898 /* Only after disabling all output pipelines that will be changed can we
12899 * update the the output configuration. */
0a9ab303 12900 intel_modeset_update_state(state);
f6e5b160 12901
a821fc46
ACO
12902 /* The state has been swaped above, so state actually contains the
12903 * old state now. */
12904
304603f4 12905 modeset_update_crtc_power_domains(state);
47fab737 12906
a6778b3c 12907 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
0a9ab303 12908 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5ac1c4bc
ML
12909 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
12910
53d9f4e9 12911 if (!needs_modeset(crtc->state) || !crtc->state->active)
0a9ab303
ACO
12912 continue;
12913
12914 update_scanline_offset(to_intel_crtc(crtc));
80715b2f 12915
0a9ab303
ACO
12916 dev_priv->display.crtc_enable(crtc);
12917 intel_crtc_enable_planes(crtc);
80715b2f 12918 }
a6778b3c 12919
a6778b3c 12920 /* FIXME: add subpixel order */
83a57153 12921
d4afb8cc
ACO
12922 drm_atomic_helper_cleanup_planes(dev, state);
12923
2bfb4627
ACO
12924 drm_atomic_state_free(state);
12925
9eb45f22 12926 return 0;
f6e5b160
CW
12927}
12928
568c634a 12929static int intel_set_mode_checked(struct drm_atomic_state *state)
f30da187 12930{
568c634a 12931 struct drm_device *dev = state->dev;
f30da187
DV
12932 int ret;
12933
568c634a 12934 ret = __intel_set_mode(state);
f30da187 12935 if (ret == 0)
568c634a 12936 intel_modeset_check_state(dev);
f30da187
DV
12937
12938 return ret;
12939}
12940
568c634a 12941static int intel_set_mode(struct drm_atomic_state *state)
7f27126e 12942{
568c634a 12943 int ret;
83a57153 12944
568c634a 12945 ret = intel_modeset_compute_config(state);
83a57153 12946 if (ret)
568c634a 12947 return ret;
7f27126e 12948
568c634a 12949 return intel_set_mode_checked(state);
7f27126e
JB
12950}
12951
c0c36b94
CW
12952void intel_crtc_restore_mode(struct drm_crtc *crtc)
12953{
83a57153
ACO
12954 struct drm_device *dev = crtc->dev;
12955 struct drm_atomic_state *state;
4be07317 12956 struct intel_crtc *intel_crtc;
83a57153
ACO
12957 struct intel_encoder *encoder;
12958 struct intel_connector *connector;
12959 struct drm_connector_state *connector_state;
4be07317 12960 struct intel_crtc_state *crtc_state;
2bfb4627 12961 int ret;
83a57153
ACO
12962
12963 state = drm_atomic_state_alloc(dev);
12964 if (!state) {
12965 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12966 crtc->base.id);
12967 return;
12968 }
12969
12970 state->acquire_ctx = dev->mode_config.acquire_ctx;
12971
12972 /* The force restore path in the HW readout code relies on the staged
12973 * config still keeping the user requested config while the actual
12974 * state has been overwritten by the configuration read from HW. We
12975 * need to copy the staged config to the atomic state, otherwise the
12976 * mode set will just reapply the state the HW is already in. */
12977 for_each_intel_encoder(dev, encoder) {
37ade417 12978 if (encoder->base.crtc != crtc)
83a57153
ACO
12979 continue;
12980
12981 for_each_intel_connector(dev, connector) {
37ade417 12982 if (connector->base.state->best_encoder != &encoder->base)
83a57153
ACO
12983 continue;
12984
12985 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12986 if (IS_ERR(connector_state)) {
12987 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12988 connector->base.base.id,
12989 connector->base.name,
12990 PTR_ERR(connector_state));
12991 continue;
12992 }
12993
12994 connector_state->crtc = crtc;
83a57153
ACO
12995 }
12996 }
12997
4be07317 12998 for_each_intel_crtc(dev, intel_crtc) {
4be07317
ACO
12999 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
13000 if (IS_ERR(crtc_state)) {
13001 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13002 intel_crtc->base.base.id,
13003 PTR_ERR(crtc_state));
13004 continue;
13005 }
13006
8c7b5ccb
ACO
13007 if (&intel_crtc->base == crtc)
13008 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
4be07317
ACO
13009 }
13010
d3a40d1b
ACO
13011 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13012 crtc->primary->fb, crtc->x, crtc->y);
13013
568c634a 13014 ret = intel_set_mode(state);
2bfb4627
ACO
13015 if (ret)
13016 drm_atomic_state_free(state);
c0c36b94
CW
13017}
13018
25c5b266
DV
13019#undef for_each_intel_crtc_masked
13020
b7885264
ACO
13021static bool intel_connector_in_mode_set(struct intel_connector *connector,
13022 struct drm_mode_set *set)
13023{
13024 int ro;
13025
13026 for (ro = 0; ro < set->num_connectors; ro++)
13027 if (set->connectors[ro] == &connector->base)
13028 return true;
13029
13030 return false;
13031}
13032
2e431051 13033static int
9a935856
DV
13034intel_modeset_stage_output_state(struct drm_device *dev,
13035 struct drm_mode_set *set,
944b0c76 13036 struct drm_atomic_state *state)
50f56119 13037{
9a935856 13038 struct intel_connector *connector;
d5432a9d 13039 struct drm_connector *drm_connector;
944b0c76 13040 struct drm_connector_state *connector_state;
d5432a9d
ACO
13041 struct drm_crtc *crtc;
13042 struct drm_crtc_state *crtc_state;
13043 int i, ret;
50f56119 13044
9abdda74 13045 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
13046 * of connectors. For paranoia, double-check this. */
13047 WARN_ON(!set->fb && (set->num_connectors != 0));
13048 WARN_ON(set->fb && (set->num_connectors == 0));
13049
3a3371ff 13050 for_each_intel_connector(dev, connector) {
b7885264
ACO
13051 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13052
d5432a9d
ACO
13053 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13054 continue;
13055
13056 connector_state =
13057 drm_atomic_get_connector_state(state, &connector->base);
13058 if (IS_ERR(connector_state))
13059 return PTR_ERR(connector_state);
13060
b7885264
ACO
13061 if (in_mode_set) {
13062 int pipe = to_intel_crtc(set->crtc)->pipe;
d5432a9d
ACO
13063 connector_state->best_encoder =
13064 &intel_find_encoder(connector, pipe)->base;
50f56119
DV
13065 }
13066
d5432a9d 13067 if (connector->base.state->crtc != set->crtc)
b7885264
ACO
13068 continue;
13069
9a935856
DV
13070 /* If we disable the crtc, disable all its connectors. Also, if
13071 * the connector is on the changing crtc but not on the new
13072 * connector list, disable it. */
b7885264 13073 if (!set->fb || !in_mode_set) {
d5432a9d 13074 connector_state->best_encoder = NULL;
9a935856
DV
13075
13076 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13077 connector->base.base.id,
c23cc417 13078 connector->base.name);
9a935856 13079 }
50f56119 13080 }
9a935856 13081 /* connector->new_encoder is now updated for all connectors. */
50f56119 13082
d5432a9d
ACO
13083 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13084 connector = to_intel_connector(drm_connector);
13085
13086 if (!connector_state->best_encoder) {
13087 ret = drm_atomic_set_crtc_for_connector(connector_state,
13088 NULL);
13089 if (ret)
13090 return ret;
7668851f 13091
50f56119 13092 continue;
d5432a9d 13093 }
50f56119 13094
d5432a9d
ACO
13095 if (intel_connector_in_mode_set(connector, set)) {
13096 struct drm_crtc *crtc = connector->base.state->crtc;
13097
13098 /* If this connector was in a previous crtc, add it
13099 * to the state. We might need to disable it. */
13100 if (crtc) {
13101 crtc_state =
13102 drm_atomic_get_crtc_state(state, crtc);
13103 if (IS_ERR(crtc_state))
13104 return PTR_ERR(crtc_state);
13105 }
13106
13107 ret = drm_atomic_set_crtc_for_connector(connector_state,
13108 set->crtc);
13109 if (ret)
13110 return ret;
13111 }
50f56119
DV
13112
13113 /* Make sure the new CRTC will work with the encoder */
d5432a9d
ACO
13114 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13115 connector_state->crtc)) {
5e2b584e 13116 return -EINVAL;
50f56119 13117 }
944b0c76 13118
9a935856
DV
13119 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13120 connector->base.base.id,
c23cc417 13121 connector->base.name,
d5432a9d 13122 connector_state->crtc->base.id);
944b0c76 13123
d5432a9d
ACO
13124 if (connector_state->best_encoder != &connector->encoder->base)
13125 connector->encoder =
13126 to_intel_encoder(connector_state->best_encoder);
0e32b39c 13127 }
7668851f 13128
d5432a9d 13129 for_each_crtc_in_state(state, crtc, crtc_state, i) {
49d6fa21
ML
13130 bool has_connectors;
13131
d5432a9d
ACO
13132 ret = drm_atomic_add_affected_connectors(state, crtc);
13133 if (ret)
13134 return ret;
4be07317 13135
49d6fa21
ML
13136 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13137 if (has_connectors != crtc_state->enable)
13138 crtc_state->enable =
13139 crtc_state->active = has_connectors;
7668851f
VS
13140 }
13141
8c7b5ccb
ACO
13142 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13143 set->fb, set->x, set->y);
13144 if (ret)
13145 return ret;
13146
13147 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13148 if (IS_ERR(crtc_state))
13149 return PTR_ERR(crtc_state);
13150
13151 if (set->mode)
13152 drm_mode_copy(&crtc_state->mode, set->mode);
13153
13154 if (set->num_connectors)
13155 crtc_state->active = true;
13156
2e431051
DV
13157 return 0;
13158}
13159
13160static int intel_crtc_set_config(struct drm_mode_set *set)
13161{
13162 struct drm_device *dev;
83a57153 13163 struct drm_atomic_state *state = NULL;
2e431051 13164 int ret;
2e431051 13165
8d3e375e
DV
13166 BUG_ON(!set);
13167 BUG_ON(!set->crtc);
13168 BUG_ON(!set->crtc->helper_private);
2e431051 13169
7e53f3a4
DV
13170 /* Enforce sane interface api - has been abused by the fb helper. */
13171 BUG_ON(!set->mode && set->fb);
13172 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 13173
2e431051
DV
13174 if (set->fb) {
13175 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13176 set->crtc->base.id, set->fb->base.id,
13177 (int)set->num_connectors, set->x, set->y);
13178 } else {
13179 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
13180 }
13181
13182 dev = set->crtc->dev;
13183
83a57153 13184 state = drm_atomic_state_alloc(dev);
7cbf41d6
ACO
13185 if (!state)
13186 return -ENOMEM;
83a57153
ACO
13187
13188 state->acquire_ctx = dev->mode_config.acquire_ctx;
13189
462a425a 13190 ret = intel_modeset_stage_output_state(dev, set, state);
2e431051 13191 if (ret)
7cbf41d6 13192 goto out;
2e431051 13193
568c634a
ACO
13194 ret = intel_modeset_compute_config(state);
13195 if (ret)
7cbf41d6 13196 goto out;
50f52756 13197
1f9954d0
JB
13198 intel_update_pipe_size(to_intel_crtc(set->crtc));
13199
568c634a 13200 ret = intel_set_mode_checked(state);
2d05eae1 13201 if (ret) {
bf67dfeb
DV
13202 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13203 set->crtc->base.id, ret);
2d05eae1 13204 }
50f56119 13205
7cbf41d6 13206out:
2bfb4627
ACO
13207 if (ret)
13208 drm_atomic_state_free(state);
50f56119
DV
13209 return ret;
13210}
f6e5b160
CW
13211
13212static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13213 .gamma_set = intel_crtc_gamma_set,
50f56119 13214 .set_config = intel_crtc_set_config,
f6e5b160
CW
13215 .destroy = intel_crtc_destroy,
13216 .page_flip = intel_crtc_page_flip,
1356837e
MR
13217 .atomic_duplicate_state = intel_crtc_duplicate_state,
13218 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13219};
13220
5358901f
DV
13221static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13222 struct intel_shared_dpll *pll,
13223 struct intel_dpll_hw_state *hw_state)
ee7b9f93 13224{
5358901f 13225 uint32_t val;
ee7b9f93 13226
f458ebbc 13227 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
13228 return false;
13229
5358901f 13230 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
13231 hw_state->dpll = val;
13232 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13233 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
13234
13235 return val & DPLL_VCO_ENABLE;
13236}
13237
15bdd4cf
DV
13238static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13239 struct intel_shared_dpll *pll)
13240{
3e369b76
ACO
13241 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13242 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
13243}
13244
e7b903d2
DV
13245static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13246 struct intel_shared_dpll *pll)
13247{
e7b903d2 13248 /* PCH refclock must be enabled first */
89eff4be 13249 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 13250
3e369b76 13251 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
13252
13253 /* Wait for the clocks to stabilize. */
13254 POSTING_READ(PCH_DPLL(pll->id));
13255 udelay(150);
13256
13257 /* The pixel multiplier can only be updated once the
13258 * DPLL is enabled and the clocks are stable.
13259 *
13260 * So write it again.
13261 */
3e369b76 13262 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 13263 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13264 udelay(200);
13265}
13266
13267static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13268 struct intel_shared_dpll *pll)
13269{
13270 struct drm_device *dev = dev_priv->dev;
13271 struct intel_crtc *crtc;
e7b903d2
DV
13272
13273 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 13274 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
13275 if (intel_crtc_to_shared_dpll(crtc) == pll)
13276 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
13277 }
13278
15bdd4cf
DV
13279 I915_WRITE(PCH_DPLL(pll->id), 0);
13280 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13281 udelay(200);
13282}
13283
46edb027
DV
13284static char *ibx_pch_dpll_names[] = {
13285 "PCH DPLL A",
13286 "PCH DPLL B",
13287};
13288
7c74ade1 13289static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 13290{
e7b903d2 13291 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
13292 int i;
13293
7c74ade1 13294 dev_priv->num_shared_dpll = 2;
ee7b9f93 13295
e72f9fbf 13296 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
13297 dev_priv->shared_dplls[i].id = i;
13298 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 13299 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
13300 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13301 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
13302 dev_priv->shared_dplls[i].get_hw_state =
13303 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
13304 }
13305}
13306
7c74ade1
DV
13307static void intel_shared_dpll_init(struct drm_device *dev)
13308{
e7b903d2 13309 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 13310
b6283055
VS
13311 intel_update_cdclk(dev);
13312
9cd86933
DV
13313 if (HAS_DDI(dev))
13314 intel_ddi_pll_init(dev);
13315 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
13316 ibx_pch_dpll_init(dev);
13317 else
13318 dev_priv->num_shared_dpll = 0;
13319
13320 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
13321}
13322
1fc0a8f7
TU
13323/**
13324 * intel_wm_need_update - Check whether watermarks need updating
13325 * @plane: drm plane
13326 * @state: new plane state
13327 *
13328 * Check current plane state versus the new one to determine whether
13329 * watermarks need to be recalculated.
13330 *
13331 * Returns true or false.
13332 */
13333bool intel_wm_need_update(struct drm_plane *plane,
13334 struct drm_plane_state *state)
13335{
13336 /* Update watermarks on tiling changes. */
13337 if (!plane->state->fb || !state->fb ||
13338 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13339 plane->state->rotation != state->rotation)
13340 return true;
13341
13342 return false;
13343}
13344
6beb8c23
MR
13345/**
13346 * intel_prepare_plane_fb - Prepare fb for usage on plane
13347 * @plane: drm plane to prepare for
13348 * @fb: framebuffer to prepare for presentation
13349 *
13350 * Prepares a framebuffer for usage on a display plane. Generally this
13351 * involves pinning the underlying object and updating the frontbuffer tracking
13352 * bits. Some older platforms need special physical address handling for
13353 * cursor planes.
13354 *
13355 * Returns 0 on success, negative error code on failure.
13356 */
13357int
13358intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee
TU
13359 struct drm_framebuffer *fb,
13360 const struct drm_plane_state *new_state)
465c120c
MR
13361{
13362 struct drm_device *dev = plane->dev;
6beb8c23
MR
13363 struct intel_plane *intel_plane = to_intel_plane(plane);
13364 enum pipe pipe = intel_plane->pipe;
13365 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13366 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13367 unsigned frontbuffer_bits = 0;
13368 int ret = 0;
465c120c 13369
ea2c67bb 13370 if (!obj)
465c120c
MR
13371 return 0;
13372
6beb8c23
MR
13373 switch (plane->type) {
13374 case DRM_PLANE_TYPE_PRIMARY:
13375 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13376 break;
13377 case DRM_PLANE_TYPE_CURSOR:
13378 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13379 break;
13380 case DRM_PLANE_TYPE_OVERLAY:
13381 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13382 break;
13383 }
465c120c 13384
6beb8c23 13385 mutex_lock(&dev->struct_mutex);
465c120c 13386
6beb8c23
MR
13387 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13388 INTEL_INFO(dev)->cursor_needs_physical) {
13389 int align = IS_I830(dev) ? 16 * 1024 : 256;
13390 ret = i915_gem_object_attach_phys(obj, align);
13391 if (ret)
13392 DRM_DEBUG_KMS("failed to attach phys object\n");
13393 } else {
82bc3b2d 13394 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
6beb8c23 13395 }
465c120c 13396
6beb8c23
MR
13397 if (ret == 0)
13398 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
fdd508a6 13399
4c34574f 13400 mutex_unlock(&dev->struct_mutex);
465c120c 13401
6beb8c23
MR
13402 return ret;
13403}
13404
38f3ce3a
MR
13405/**
13406 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13407 * @plane: drm plane to clean up for
13408 * @fb: old framebuffer that was on plane
13409 *
13410 * Cleans up a framebuffer that has just been removed from a plane.
13411 */
13412void
13413intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee
TU
13414 struct drm_framebuffer *fb,
13415 const struct drm_plane_state *old_state)
38f3ce3a
MR
13416{
13417 struct drm_device *dev = plane->dev;
13418 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13419
13420 if (WARN_ON(!obj))
13421 return;
13422
13423 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13424 !INTEL_INFO(dev)->cursor_needs_physical) {
13425 mutex_lock(&dev->struct_mutex);
82bc3b2d 13426 intel_unpin_fb_obj(fb, old_state);
38f3ce3a
MR
13427 mutex_unlock(&dev->struct_mutex);
13428 }
465c120c
MR
13429}
13430
6156a456
CK
13431int
13432skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13433{
13434 int max_scale;
13435 struct drm_device *dev;
13436 struct drm_i915_private *dev_priv;
13437 int crtc_clock, cdclk;
13438
13439 if (!intel_crtc || !crtc_state)
13440 return DRM_PLANE_HELPER_NO_SCALING;
13441
13442 dev = intel_crtc->base.dev;
13443 dev_priv = dev->dev_private;
13444 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13445 cdclk = dev_priv->display.get_display_clock_speed(dev);
13446
13447 if (!crtc_clock || !cdclk)
13448 return DRM_PLANE_HELPER_NO_SCALING;
13449
13450 /*
13451 * skl max scale is lower of:
13452 * close to 3 but not 3, -1 is for that purpose
13453 * or
13454 * cdclk/crtc_clock
13455 */
13456 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13457
13458 return max_scale;
13459}
13460
465c120c 13461static int
3c692a41
GP
13462intel_check_primary_plane(struct drm_plane *plane,
13463 struct intel_plane_state *state)
13464{
32b7eeec
MR
13465 struct drm_device *dev = plane->dev;
13466 struct drm_i915_private *dev_priv = dev->dev_private;
2b875c22 13467 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13468 struct intel_crtc *intel_crtc;
6156a456 13469 struct intel_crtc_state *crtc_state;
2b875c22 13470 struct drm_framebuffer *fb = state->base.fb;
3c692a41
GP
13471 struct drm_rect *dest = &state->dst;
13472 struct drm_rect *src = &state->src;
13473 const struct drm_rect *clip = &state->clip;
d8106366 13474 bool can_position = false;
6156a456
CK
13475 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13476 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
465c120c
MR
13477 int ret;
13478
ea2c67bb
MR
13479 crtc = crtc ? crtc : plane->crtc;
13480 intel_crtc = to_intel_crtc(crtc);
6156a456
CK
13481 crtc_state = state->base.state ?
13482 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
ea2c67bb 13483
6156a456 13484 if (INTEL_INFO(dev)->gen >= 9) {
225c228a
CK
13485 /* use scaler when colorkey is not required */
13486 if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
13487 min_scale = 1;
13488 max_scale = skl_max_scale(intel_crtc, crtc_state);
13489 }
d8106366 13490 can_position = true;
6156a456 13491 }
d8106366 13492
c59cb179
MR
13493 ret = drm_plane_helper_check_update(plane, crtc, fb,
13494 src, dest, clip,
6156a456
CK
13495 min_scale,
13496 max_scale,
d8106366
SJ
13497 can_position, true,
13498 &state->visible);
c59cb179
MR
13499 if (ret)
13500 return ret;
465c120c 13501
32b7eeec 13502 if (intel_crtc->active) {
b70709a6
ML
13503 struct intel_plane_state *old_state =
13504 to_intel_plane_state(plane->state);
13505
32b7eeec
MR
13506 intel_crtc->atomic.wait_for_flips = true;
13507
13508 /*
13509 * FBC does not work on some platforms for rotated
13510 * planes, so disable it when rotation is not 0 and
13511 * update it when rotation is set back to 0.
13512 *
13513 * FIXME: This is redundant with the fbc update done in
13514 * the primary plane enable function except that that
13515 * one is done too late. We eventually need to unify
13516 * this.
13517 */
b70709a6 13518 if (state->visible &&
32b7eeec 13519 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
e35fef21 13520 dev_priv->fbc.crtc == intel_crtc &&
8e7d688b 13521 state->base.rotation != BIT(DRM_ROTATE_0)) {
32b7eeec
MR
13522 intel_crtc->atomic.disable_fbc = true;
13523 }
13524
b70709a6 13525 if (state->visible && !old_state->visible) {
32b7eeec
MR
13526 /*
13527 * BDW signals flip done immediately if the plane
13528 * is disabled, even if the plane enable is already
13529 * armed to occur at the next vblank :(
13530 */
b70709a6 13531 if (IS_BROADWELL(dev))
32b7eeec 13532 intel_crtc->atomic.wait_vblank = true;
fb9d6cf8
ML
13533
13534 if (crtc_state && !needs_modeset(&crtc_state->base))
13535 intel_crtc->atomic.post_enable_primary = true;
32b7eeec
MR
13536 }
13537
fb9d6cf8
ML
13538 if (!state->visible && old_state->visible &&
13539 crtc_state && !needs_modeset(&crtc_state->base))
13540 intel_crtc->atomic.pre_disable_primary = true;
13541
32b7eeec
MR
13542 intel_crtc->atomic.fb_bits |=
13543 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13544
13545 intel_crtc->atomic.update_fbc = true;
0fda6568 13546
1fc0a8f7 13547 if (intel_wm_need_update(plane, &state->base))
0fda6568 13548 intel_crtc->atomic.update_wm = true;
ccc759dc
GP
13549 }
13550
6156a456
CK
13551 if (INTEL_INFO(dev)->gen >= 9) {
13552 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13553 to_intel_plane(plane), state, 0);
13554 if (ret)
13555 return ret;
13556 }
13557
14af293f
GP
13558 return 0;
13559}
13560
13561static void
13562intel_commit_primary_plane(struct drm_plane *plane,
13563 struct intel_plane_state *state)
13564{
2b875c22
MR
13565 struct drm_crtc *crtc = state->base.crtc;
13566 struct drm_framebuffer *fb = state->base.fb;
13567 struct drm_device *dev = plane->dev;
14af293f 13568 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 13569 struct intel_crtc *intel_crtc;
14af293f
GP
13570 struct drm_rect *src = &state->src;
13571
ea2c67bb
MR
13572 crtc = crtc ? crtc : plane->crtc;
13573 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
13574
13575 plane->fb = fb;
9dc806fc
MR
13576 crtc->x = src->x1 >> 16;
13577 crtc->y = src->y1 >> 16;
ccc759dc 13578
ccc759dc 13579 if (intel_crtc->active) {
27321ae8 13580 if (state->visible)
ccc759dc
GP
13581 /* FIXME: kill this fastboot hack */
13582 intel_update_pipe_size(intel_crtc);
465c120c 13583
27321ae8
ML
13584 dev_priv->display.update_primary_plane(crtc, plane->fb,
13585 crtc->x, crtc->y);
ccc759dc 13586 }
465c120c
MR
13587}
13588
a8ad0d8e
ML
13589static void
13590intel_disable_primary_plane(struct drm_plane *plane,
13591 struct drm_crtc *crtc,
13592 bool force)
13593{
13594 struct drm_device *dev = plane->dev;
13595 struct drm_i915_private *dev_priv = dev->dev_private;
13596
a8ad0d8e
ML
13597 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13598}
13599
32b7eeec 13600static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 13601{
32b7eeec 13602 struct drm_device *dev = crtc->dev;
140fd38d 13603 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 13604 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb
MR
13605 struct intel_plane *intel_plane;
13606 struct drm_plane *p;
13607 unsigned fb_bits = 0;
13608
13609 /* Track fb's for any planes being disabled */
13610 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13611 intel_plane = to_intel_plane(p);
13612
13613 if (intel_crtc->atomic.disabled_planes &
13614 (1 << drm_plane_index(p))) {
13615 switch (p->type) {
13616 case DRM_PLANE_TYPE_PRIMARY:
13617 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13618 break;
13619 case DRM_PLANE_TYPE_CURSOR:
13620 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13621 break;
13622 case DRM_PLANE_TYPE_OVERLAY:
13623 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13624 break;
13625 }
3c692a41 13626
ea2c67bb
MR
13627 mutex_lock(&dev->struct_mutex);
13628 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13629 mutex_unlock(&dev->struct_mutex);
13630 }
13631 }
3c692a41 13632
32b7eeec
MR
13633 if (intel_crtc->atomic.wait_for_flips)
13634 intel_crtc_wait_for_pending_flips(crtc);
3c692a41 13635
32b7eeec
MR
13636 if (intel_crtc->atomic.disable_fbc)
13637 intel_fbc_disable(dev);
3c692a41 13638
32b7eeec
MR
13639 if (intel_crtc->atomic.pre_disable_primary)
13640 intel_pre_disable_primary(crtc);
3c692a41 13641
32b7eeec
MR
13642 if (intel_crtc->atomic.update_wm)
13643 intel_update_watermarks(crtc);
3c692a41 13644
32b7eeec 13645 intel_runtime_pm_get(dev_priv);
3c692a41 13646
c34c9ee4
MR
13647 /* Perform vblank evasion around commit operation */
13648 if (intel_crtc->active)
13649 intel_crtc->atomic.evade =
13650 intel_pipe_update_start(intel_crtc,
13651 &intel_crtc->atomic.start_vbl_count);
32b7eeec
MR
13652}
13653
13654static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13655{
13656 struct drm_device *dev = crtc->dev;
13657 struct drm_i915_private *dev_priv = dev->dev_private;
13658 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13659 struct drm_plane *p;
13660
c34c9ee4
MR
13661 if (intel_crtc->atomic.evade)
13662 intel_pipe_update_end(intel_crtc,
13663 intel_crtc->atomic.start_vbl_count);
3c692a41 13664
140fd38d 13665 intel_runtime_pm_put(dev_priv);
3c692a41 13666
8a8f7f44 13667 if (intel_crtc->atomic.wait_vblank && intel_crtc->active)
32b7eeec
MR
13668 intel_wait_for_vblank(dev, intel_crtc->pipe);
13669
13670 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13671
13672 if (intel_crtc->atomic.update_fbc) {
ccc759dc 13673 mutex_lock(&dev->struct_mutex);
7ff0ebcc 13674 intel_fbc_update(dev);
ccc759dc 13675 mutex_unlock(&dev->struct_mutex);
38f3ce3a 13676 }
3c692a41 13677
32b7eeec
MR
13678 if (intel_crtc->atomic.post_enable_primary)
13679 intel_post_enable_primary(crtc);
3c692a41 13680
32b7eeec
MR
13681 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13682 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13683 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13684 false, false);
13685
13686 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
3c692a41
GP
13687}
13688
cf4c7c12 13689/**
4a3b8769
MR
13690 * intel_plane_destroy - destroy a plane
13691 * @plane: plane to destroy
cf4c7c12 13692 *
4a3b8769
MR
13693 * Common destruction function for all types of planes (primary, cursor,
13694 * sprite).
cf4c7c12 13695 */
4a3b8769 13696void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13697{
13698 struct intel_plane *intel_plane = to_intel_plane(plane);
13699 drm_plane_cleanup(plane);
13700 kfree(intel_plane);
13701}
13702
65a3fea0 13703const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13704 .update_plane = drm_atomic_helper_update_plane,
13705 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13706 .destroy = intel_plane_destroy,
c196e1d6 13707 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13708 .atomic_get_property = intel_plane_atomic_get_property,
13709 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13710 .atomic_duplicate_state = intel_plane_duplicate_state,
13711 .atomic_destroy_state = intel_plane_destroy_state,
13712
465c120c
MR
13713};
13714
13715static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13716 int pipe)
13717{
13718 struct intel_plane *primary;
8e7d688b 13719 struct intel_plane_state *state;
465c120c
MR
13720 const uint32_t *intel_primary_formats;
13721 int num_formats;
13722
13723 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13724 if (primary == NULL)
13725 return NULL;
13726
8e7d688b
MR
13727 state = intel_create_plane_state(&primary->base);
13728 if (!state) {
ea2c67bb
MR
13729 kfree(primary);
13730 return NULL;
13731 }
8e7d688b 13732 primary->base.state = &state->base;
ea2c67bb 13733
465c120c
MR
13734 primary->can_scale = false;
13735 primary->max_downscale = 1;
6156a456
CK
13736 if (INTEL_INFO(dev)->gen >= 9) {
13737 primary->can_scale = true;
af99ceda 13738 state->scaler_id = -1;
6156a456 13739 }
465c120c
MR
13740 primary->pipe = pipe;
13741 primary->plane = pipe;
c59cb179
MR
13742 primary->check_plane = intel_check_primary_plane;
13743 primary->commit_plane = intel_commit_primary_plane;
a8ad0d8e 13744 primary->disable_plane = intel_disable_primary_plane;
08e221fb 13745 primary->ckey.flags = I915_SET_COLORKEY_NONE;
465c120c
MR
13746 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13747 primary->plane = !pipe;
13748
6c0fd451
DL
13749 if (INTEL_INFO(dev)->gen >= 9) {
13750 intel_primary_formats = skl_primary_formats;
13751 num_formats = ARRAY_SIZE(skl_primary_formats);
13752 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
13753 intel_primary_formats = i965_primary_formats;
13754 num_formats = ARRAY_SIZE(i965_primary_formats);
6c0fd451
DL
13755 } else {
13756 intel_primary_formats = i8xx_primary_formats;
13757 num_formats = ARRAY_SIZE(i8xx_primary_formats);
465c120c
MR
13758 }
13759
13760 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13761 &intel_plane_funcs,
465c120c
MR
13762 intel_primary_formats, num_formats,
13763 DRM_PLANE_TYPE_PRIMARY);
48404c1e 13764
3b7a5119
SJ
13765 if (INTEL_INFO(dev)->gen >= 4)
13766 intel_create_rotation_property(dev, primary);
48404c1e 13767
ea2c67bb
MR
13768 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13769
465c120c
MR
13770 return &primary->base;
13771}
13772
3b7a5119
SJ
13773void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13774{
13775 if (!dev->mode_config.rotation_property) {
13776 unsigned long flags = BIT(DRM_ROTATE_0) |
13777 BIT(DRM_ROTATE_180);
13778
13779 if (INTEL_INFO(dev)->gen >= 9)
13780 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13781
13782 dev->mode_config.rotation_property =
13783 drm_mode_create_rotation_property(dev, flags);
13784 }
13785 if (dev->mode_config.rotation_property)
13786 drm_object_attach_property(&plane->base.base,
13787 dev->mode_config.rotation_property,
13788 plane->base.state->rotation);
13789}
13790
3d7d6510 13791static int
852e787c
GP
13792intel_check_cursor_plane(struct drm_plane *plane,
13793 struct intel_plane_state *state)
3d7d6510 13794{
2b875c22 13795 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13796 struct drm_device *dev = plane->dev;
2b875c22 13797 struct drm_framebuffer *fb = state->base.fb;
852e787c
GP
13798 struct drm_rect *dest = &state->dst;
13799 struct drm_rect *src = &state->src;
13800 const struct drm_rect *clip = &state->clip;
757f9a3e 13801 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ea2c67bb 13802 struct intel_crtc *intel_crtc;
757f9a3e
GP
13803 unsigned stride;
13804 int ret;
3d7d6510 13805
ea2c67bb
MR
13806 crtc = crtc ? crtc : plane->crtc;
13807 intel_crtc = to_intel_crtc(crtc);
13808
757f9a3e 13809 ret = drm_plane_helper_check_update(plane, crtc, fb,
852e787c 13810 src, dest, clip,
3d7d6510
MR
13811 DRM_PLANE_HELPER_NO_SCALING,
13812 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13813 true, true, &state->visible);
757f9a3e
GP
13814 if (ret)
13815 return ret;
13816
13817
13818 /* if we want to turn off the cursor ignore width and height */
13819 if (!obj)
32b7eeec 13820 goto finish;
757f9a3e 13821
757f9a3e 13822 /* Check for which cursor types we support */
ea2c67bb
MR
13823 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13824 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13825 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13826 return -EINVAL;
13827 }
13828
ea2c67bb
MR
13829 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13830 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13831 DRM_DEBUG_KMS("buffer is too small\n");
13832 return -ENOMEM;
13833 }
13834
3a656b54 13835 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e
GP
13836 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13837 ret = -EINVAL;
13838 }
757f9a3e 13839
32b7eeec
MR
13840finish:
13841 if (intel_crtc->active) {
3749f463 13842 if (plane->state->crtc_w != state->base.crtc_w)
32b7eeec
MR
13843 intel_crtc->atomic.update_wm = true;
13844
13845 intel_crtc->atomic.fb_bits |=
13846 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13847 }
13848
757f9a3e 13849 return ret;
852e787c 13850}
3d7d6510 13851
a8ad0d8e
ML
13852static void
13853intel_disable_cursor_plane(struct drm_plane *plane,
13854 struct drm_crtc *crtc,
13855 bool force)
13856{
13857 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13858
13859 if (!force) {
13860 plane->fb = NULL;
13861 intel_crtc->cursor_bo = NULL;
13862 intel_crtc->cursor_addr = 0;
13863 }
13864
13865 intel_crtc_update_cursor(crtc, false);
13866}
13867
f4a2cf29 13868static void
852e787c
GP
13869intel_commit_cursor_plane(struct drm_plane *plane,
13870 struct intel_plane_state *state)
13871{
2b875c22 13872 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
13873 struct drm_device *dev = plane->dev;
13874 struct intel_crtc *intel_crtc;
2b875c22 13875 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13876 uint32_t addr;
852e787c 13877
ea2c67bb
MR
13878 crtc = crtc ? crtc : plane->crtc;
13879 intel_crtc = to_intel_crtc(crtc);
13880
2b875c22 13881 plane->fb = state->base.fb;
ea2c67bb
MR
13882 crtc->cursor_x = state->base.crtc_x;
13883 crtc->cursor_y = state->base.crtc_y;
13884
a912f12f
GP
13885 if (intel_crtc->cursor_bo == obj)
13886 goto update;
4ed91096 13887
f4a2cf29 13888 if (!obj)
a912f12f 13889 addr = 0;
f4a2cf29 13890 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 13891 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 13892 else
a912f12f 13893 addr = obj->phys_handle->busaddr;
852e787c 13894
a912f12f
GP
13895 intel_crtc->cursor_addr = addr;
13896 intel_crtc->cursor_bo = obj;
13897update:
852e787c 13898
32b7eeec 13899 if (intel_crtc->active)
a912f12f 13900 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
13901}
13902
3d7d6510
MR
13903static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13904 int pipe)
13905{
13906 struct intel_plane *cursor;
8e7d688b 13907 struct intel_plane_state *state;
3d7d6510
MR
13908
13909 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13910 if (cursor == NULL)
13911 return NULL;
13912
8e7d688b
MR
13913 state = intel_create_plane_state(&cursor->base);
13914 if (!state) {
ea2c67bb
MR
13915 kfree(cursor);
13916 return NULL;
13917 }
8e7d688b 13918 cursor->base.state = &state->base;
ea2c67bb 13919
3d7d6510
MR
13920 cursor->can_scale = false;
13921 cursor->max_downscale = 1;
13922 cursor->pipe = pipe;
13923 cursor->plane = pipe;
c59cb179
MR
13924 cursor->check_plane = intel_check_cursor_plane;
13925 cursor->commit_plane = intel_commit_cursor_plane;
a8ad0d8e 13926 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
13927
13928 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 13929 &intel_plane_funcs,
3d7d6510
MR
13930 intel_cursor_formats,
13931 ARRAY_SIZE(intel_cursor_formats),
13932 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
13933
13934 if (INTEL_INFO(dev)->gen >= 4) {
13935 if (!dev->mode_config.rotation_property)
13936 dev->mode_config.rotation_property =
13937 drm_mode_create_rotation_property(dev,
13938 BIT(DRM_ROTATE_0) |
13939 BIT(DRM_ROTATE_180));
13940 if (dev->mode_config.rotation_property)
13941 drm_object_attach_property(&cursor->base.base,
13942 dev->mode_config.rotation_property,
8e7d688b 13943 state->base.rotation);
4398ad45
VS
13944 }
13945
af99ceda
CK
13946 if (INTEL_INFO(dev)->gen >=9)
13947 state->scaler_id = -1;
13948
ea2c67bb
MR
13949 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13950
3d7d6510
MR
13951 return &cursor->base;
13952}
13953
549e2bfb
CK
13954static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13955 struct intel_crtc_state *crtc_state)
13956{
13957 int i;
13958 struct intel_scaler *intel_scaler;
13959 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13960
13961 for (i = 0; i < intel_crtc->num_scalers; i++) {
13962 intel_scaler = &scaler_state->scalers[i];
13963 intel_scaler->in_use = 0;
13964 intel_scaler->id = i;
13965
13966 intel_scaler->mode = PS_SCALER_MODE_DYN;
13967 }
13968
13969 scaler_state->scaler_id = -1;
13970}
13971
b358d0a6 13972static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 13973{
fbee40df 13974 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 13975 struct intel_crtc *intel_crtc;
f5de6e07 13976 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
13977 struct drm_plane *primary = NULL;
13978 struct drm_plane *cursor = NULL;
465c120c 13979 int i, ret;
79e53945 13980
955382f3 13981 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
13982 if (intel_crtc == NULL)
13983 return;
13984
f5de6e07
ACO
13985 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13986 if (!crtc_state)
13987 goto fail;
550acefd
ACO
13988 intel_crtc->config = crtc_state;
13989 intel_crtc->base.state = &crtc_state->base;
07878248 13990 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13991
549e2bfb
CK
13992 /* initialize shared scalers */
13993 if (INTEL_INFO(dev)->gen >= 9) {
13994 if (pipe == PIPE_C)
13995 intel_crtc->num_scalers = 1;
13996 else
13997 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13998
13999 skl_init_scalers(dev, intel_crtc, crtc_state);
14000 }
14001
465c120c 14002 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14003 if (!primary)
14004 goto fail;
14005
14006 cursor = intel_cursor_plane_create(dev, pipe);
14007 if (!cursor)
14008 goto fail;
14009
465c120c 14010 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
14011 cursor, &intel_crtc_funcs);
14012 if (ret)
14013 goto fail;
79e53945
JB
14014
14015 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
14016 for (i = 0; i < 256; i++) {
14017 intel_crtc->lut_r[i] = i;
14018 intel_crtc->lut_g[i] = i;
14019 intel_crtc->lut_b[i] = i;
14020 }
14021
1f1c2e24
VS
14022 /*
14023 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14024 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14025 */
80824003
JB
14026 intel_crtc->pipe = pipe;
14027 intel_crtc->plane = pipe;
3a77c4c4 14028 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14029 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14030 intel_crtc->plane = !pipe;
80824003
JB
14031 }
14032
4b0e333e
CW
14033 intel_crtc->cursor_base = ~0;
14034 intel_crtc->cursor_cntl = ~0;
dc41c154 14035 intel_crtc->cursor_size = ~0;
8d7849db 14036
22fd0fab
JB
14037 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14038 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14039 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14040 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14041
79e53945 14042 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
14043
14044 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14045 return;
14046
14047fail:
14048 if (primary)
14049 drm_plane_cleanup(primary);
14050 if (cursor)
14051 drm_plane_cleanup(cursor);
f5de6e07 14052 kfree(crtc_state);
3d7d6510 14053 kfree(intel_crtc);
79e53945
JB
14054}
14055
752aa88a
JB
14056enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14057{
14058 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14059 struct drm_device *dev = connector->base.dev;
752aa88a 14060
51fd371b 14061 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14062
d3babd3f 14063 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14064 return INVALID_PIPE;
14065
14066 return to_intel_crtc(encoder->crtc)->pipe;
14067}
14068
08d7b3d1 14069int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14070 struct drm_file *file)
08d7b3d1 14071{
08d7b3d1 14072 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14073 struct drm_crtc *drmmode_crtc;
c05422d5 14074 struct intel_crtc *crtc;
08d7b3d1 14075
7707e653 14076 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14077
7707e653 14078 if (!drmmode_crtc) {
08d7b3d1 14079 DRM_ERROR("no such CRTC id\n");
3f2c2057 14080 return -ENOENT;
08d7b3d1
CW
14081 }
14082
7707e653 14083 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14084 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14085
c05422d5 14086 return 0;
08d7b3d1
CW
14087}
14088
66a9278e 14089static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14090{
66a9278e
DV
14091 struct drm_device *dev = encoder->base.dev;
14092 struct intel_encoder *source_encoder;
79e53945 14093 int index_mask = 0;
79e53945
JB
14094 int entry = 0;
14095
b2784e15 14096 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14097 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14098 index_mask |= (1 << entry);
14099
79e53945
JB
14100 entry++;
14101 }
4ef69c7a 14102
79e53945
JB
14103 return index_mask;
14104}
14105
4d302442
CW
14106static bool has_edp_a(struct drm_device *dev)
14107{
14108 struct drm_i915_private *dev_priv = dev->dev_private;
14109
14110 if (!IS_MOBILE(dev))
14111 return false;
14112
14113 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14114 return false;
14115
e3589908 14116 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14117 return false;
14118
14119 return true;
14120}
14121
84b4e042
JB
14122static bool intel_crt_present(struct drm_device *dev)
14123{
14124 struct drm_i915_private *dev_priv = dev->dev_private;
14125
884497ed
DL
14126 if (INTEL_INFO(dev)->gen >= 9)
14127 return false;
14128
cf404ce4 14129 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14130 return false;
14131
14132 if (IS_CHERRYVIEW(dev))
14133 return false;
14134
14135 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14136 return false;
14137
14138 return true;
14139}
14140
79e53945
JB
14141static void intel_setup_outputs(struct drm_device *dev)
14142{
725e30ad 14143 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14144 struct intel_encoder *encoder;
cb0953d7 14145 bool dpd_is_edp = false;
79e53945 14146
c9093354 14147 intel_lvds_init(dev);
79e53945 14148
84b4e042 14149 if (intel_crt_present(dev))
79935fca 14150 intel_crt_init(dev);
cb0953d7 14151
c776eb2e
VK
14152 if (IS_BROXTON(dev)) {
14153 /*
14154 * FIXME: Broxton doesn't support port detection via the
14155 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14156 * detect the ports.
14157 */
14158 intel_ddi_init(dev, PORT_A);
14159 intel_ddi_init(dev, PORT_B);
14160 intel_ddi_init(dev, PORT_C);
14161 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14162 int found;
14163
de31facd
JB
14164 /*
14165 * Haswell uses DDI functions to detect digital outputs.
14166 * On SKL pre-D0 the strap isn't connected, so we assume
14167 * it's there.
14168 */
0e72a5b5 14169 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
de31facd
JB
14170 /* WaIgnoreDDIAStrap: skl */
14171 if (found ||
14172 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
0e72a5b5
ED
14173 intel_ddi_init(dev, PORT_A);
14174
14175 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14176 * register */
14177 found = I915_READ(SFUSE_STRAP);
14178
14179 if (found & SFUSE_STRAP_DDIB_DETECTED)
14180 intel_ddi_init(dev, PORT_B);
14181 if (found & SFUSE_STRAP_DDIC_DETECTED)
14182 intel_ddi_init(dev, PORT_C);
14183 if (found & SFUSE_STRAP_DDID_DETECTED)
14184 intel_ddi_init(dev, PORT_D);
14185 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14186 int found;
5d8a7752 14187 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14188
14189 if (has_edp_a(dev))
14190 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14191
dc0fa718 14192 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14193 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 14194 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 14195 if (!found)
e2debe91 14196 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14197 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14198 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14199 }
14200
dc0fa718 14201 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14202 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14203
dc0fa718 14204 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14205 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14206
5eb08b69 14207 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14208 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14209
270b3042 14210 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14211 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 14212 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
14213 /*
14214 * The DP_DETECTED bit is the latched state of the DDC
14215 * SDA pin at boot. However since eDP doesn't require DDC
14216 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14217 * eDP ports may have been muxed to an alternate function.
14218 * Thus we can't rely on the DP_DETECTED bit alone to detect
14219 * eDP ports. Consult the VBT as well as DP_DETECTED to
14220 * detect eDP ports.
14221 */
d2182a66
VS
14222 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14223 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
14224 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14225 PORT_B);
e17ac6db
VS
14226 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14227 intel_dp_is_edp(dev, PORT_B))
14228 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 14229
d2182a66
VS
14230 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14231 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
14232 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14233 PORT_C);
e17ac6db
VS
14234 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14235 intel_dp_is_edp(dev, PORT_C))
14236 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 14237
9418c1f1 14238 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14239 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
14240 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14241 PORT_D);
e17ac6db
VS
14242 /* eDP not supported on port D, so don't check VBT */
14243 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14244 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
14245 }
14246
3cfca973 14247 intel_dsi_init(dev);
103a196f 14248 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 14249 bool found = false;
7d57382e 14250
e2debe91 14251 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14252 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 14253 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
14254 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14255 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14256 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14257 }
27185ae1 14258
e7281eab 14259 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14260 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14261 }
13520b05
KH
14262
14263 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14264
e2debe91 14265 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14266 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 14267 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 14268 }
27185ae1 14269
e2debe91 14270 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14271
b01f2c3a
JB
14272 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14273 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14274 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14275 }
e7281eab 14276 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14277 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14278 }
27185ae1 14279
b01f2c3a 14280 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 14281 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14282 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14283 } else if (IS_GEN2(dev))
79e53945
JB
14284 intel_dvo_init(dev);
14285
103a196f 14286 if (SUPPORTS_TV(dev))
79e53945
JB
14287 intel_tv_init(dev);
14288
0bc12bcb 14289 intel_psr_init(dev);
7c8f8a70 14290
b2784e15 14291 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14292 encoder->base.possible_crtcs = encoder->crtc_mask;
14293 encoder->base.possible_clones =
66a9278e 14294 intel_encoder_clones(encoder);
79e53945 14295 }
47356eb6 14296
dde86e2d 14297 intel_init_pch_refclk(dev);
270b3042
DV
14298
14299 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14300}
14301
14302static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14303{
60a5ca01 14304 struct drm_device *dev = fb->dev;
79e53945 14305 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14306
ef2d633e 14307 drm_framebuffer_cleanup(fb);
60a5ca01 14308 mutex_lock(&dev->struct_mutex);
ef2d633e 14309 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14310 drm_gem_object_unreference(&intel_fb->obj->base);
14311 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14312 kfree(intel_fb);
14313}
14314
14315static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14316 struct drm_file *file,
79e53945
JB
14317 unsigned int *handle)
14318{
14319 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14320 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14321
05394f39 14322 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14323}
14324
14325static const struct drm_framebuffer_funcs intel_fb_funcs = {
14326 .destroy = intel_user_framebuffer_destroy,
14327 .create_handle = intel_user_framebuffer_create_handle,
14328};
14329
b321803d
DL
14330static
14331u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14332 uint32_t pixel_format)
14333{
14334 u32 gen = INTEL_INFO(dev)->gen;
14335
14336 if (gen >= 9) {
14337 /* "The stride in bytes must not exceed the of the size of 8K
14338 * pixels and 32K bytes."
14339 */
14340 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14341 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14342 return 32*1024;
14343 } else if (gen >= 4) {
14344 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14345 return 16*1024;
14346 else
14347 return 32*1024;
14348 } else if (gen >= 3) {
14349 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14350 return 8*1024;
14351 else
14352 return 16*1024;
14353 } else {
14354 /* XXX DSPC is limited to 4k tiled */
14355 return 8*1024;
14356 }
14357}
14358
b5ea642a
DV
14359static int intel_framebuffer_init(struct drm_device *dev,
14360 struct intel_framebuffer *intel_fb,
14361 struct drm_mode_fb_cmd2 *mode_cmd,
14362 struct drm_i915_gem_object *obj)
79e53945 14363{
6761dd31 14364 unsigned int aligned_height;
79e53945 14365 int ret;
b321803d 14366 u32 pitch_limit, stride_alignment;
79e53945 14367
dd4916c5
DV
14368 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14369
2a80eada
DV
14370 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14371 /* Enforce that fb modifier and tiling mode match, but only for
14372 * X-tiled. This is needed for FBC. */
14373 if (!!(obj->tiling_mode == I915_TILING_X) !=
14374 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14375 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14376 return -EINVAL;
14377 }
14378 } else {
14379 if (obj->tiling_mode == I915_TILING_X)
14380 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14381 else if (obj->tiling_mode == I915_TILING_Y) {
14382 DRM_DEBUG("No Y tiling for legacy addfb\n");
14383 return -EINVAL;
14384 }
14385 }
14386
9a8f0a12
TU
14387 /* Passed in modifier sanity checking. */
14388 switch (mode_cmd->modifier[0]) {
14389 case I915_FORMAT_MOD_Y_TILED:
14390 case I915_FORMAT_MOD_Yf_TILED:
14391 if (INTEL_INFO(dev)->gen < 9) {
14392 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14393 mode_cmd->modifier[0]);
14394 return -EINVAL;
14395 }
14396 case DRM_FORMAT_MOD_NONE:
14397 case I915_FORMAT_MOD_X_TILED:
14398 break;
14399 default:
c0f40428
JB
14400 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14401 mode_cmd->modifier[0]);
57cd6508 14402 return -EINVAL;
c16ed4be 14403 }
57cd6508 14404
b321803d
DL
14405 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14406 mode_cmd->pixel_format);
14407 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14408 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14409 mode_cmd->pitches[0], stride_alignment);
57cd6508 14410 return -EINVAL;
c16ed4be 14411 }
57cd6508 14412
b321803d
DL
14413 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14414 mode_cmd->pixel_format);
a35cdaa0 14415 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14416 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14417 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14418 "tiled" : "linear",
a35cdaa0 14419 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14420 return -EINVAL;
c16ed4be 14421 }
5d7bd705 14422
2a80eada 14423 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14424 mode_cmd->pitches[0] != obj->stride) {
14425 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14426 mode_cmd->pitches[0], obj->stride);
5d7bd705 14427 return -EINVAL;
c16ed4be 14428 }
5d7bd705 14429
57779d06 14430 /* Reject formats not supported by any plane early. */
308e5bcb 14431 switch (mode_cmd->pixel_format) {
57779d06 14432 case DRM_FORMAT_C8:
04b3924d
VS
14433 case DRM_FORMAT_RGB565:
14434 case DRM_FORMAT_XRGB8888:
14435 case DRM_FORMAT_ARGB8888:
57779d06
VS
14436 break;
14437 case DRM_FORMAT_XRGB1555:
c16ed4be 14438 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14439 DRM_DEBUG("unsupported pixel format: %s\n",
14440 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14441 return -EINVAL;
c16ed4be 14442 }
57779d06 14443 break;
57779d06 14444 case DRM_FORMAT_ABGR8888:
6c0fd451
DL
14445 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14446 DRM_DEBUG("unsupported pixel format: %s\n",
14447 drm_get_format_name(mode_cmd->pixel_format));
14448 return -EINVAL;
14449 }
14450 break;
14451 case DRM_FORMAT_XBGR8888:
04b3924d 14452 case DRM_FORMAT_XRGB2101010:
57779d06 14453 case DRM_FORMAT_XBGR2101010:
c16ed4be 14454 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14455 DRM_DEBUG("unsupported pixel format: %s\n",
14456 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14457 return -EINVAL;
c16ed4be 14458 }
b5626747 14459 break;
7531208b
DL
14460 case DRM_FORMAT_ABGR2101010:
14461 if (!IS_VALLEYVIEW(dev)) {
14462 DRM_DEBUG("unsupported pixel format: %s\n",
14463 drm_get_format_name(mode_cmd->pixel_format));
14464 return -EINVAL;
14465 }
14466 break;
04b3924d
VS
14467 case DRM_FORMAT_YUYV:
14468 case DRM_FORMAT_UYVY:
14469 case DRM_FORMAT_YVYU:
14470 case DRM_FORMAT_VYUY:
c16ed4be 14471 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14472 DRM_DEBUG("unsupported pixel format: %s\n",
14473 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14474 return -EINVAL;
c16ed4be 14475 }
57cd6508
CW
14476 break;
14477 default:
4ee62c76
VS
14478 DRM_DEBUG("unsupported pixel format: %s\n",
14479 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14480 return -EINVAL;
14481 }
14482
90f9a336
VS
14483 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14484 if (mode_cmd->offsets[0] != 0)
14485 return -EINVAL;
14486
ec2c981e 14487 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14488 mode_cmd->pixel_format,
14489 mode_cmd->modifier[0]);
53155c0a
DV
14490 /* FIXME drm helper for size checks (especially planar formats)? */
14491 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14492 return -EINVAL;
14493
c7d73f6a
DV
14494 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14495 intel_fb->obj = obj;
80075d49 14496 intel_fb->obj->framebuffer_references++;
c7d73f6a 14497
79e53945
JB
14498 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14499 if (ret) {
14500 DRM_ERROR("framebuffer init failed %d\n", ret);
14501 return ret;
14502 }
14503
79e53945
JB
14504 return 0;
14505}
14506
79e53945
JB
14507static struct drm_framebuffer *
14508intel_user_framebuffer_create(struct drm_device *dev,
14509 struct drm_file *filp,
308e5bcb 14510 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14511{
05394f39 14512 struct drm_i915_gem_object *obj;
79e53945 14513
308e5bcb
JB
14514 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14515 mode_cmd->handles[0]));
c8725226 14516 if (&obj->base == NULL)
cce13ff7 14517 return ERR_PTR(-ENOENT);
79e53945 14518
d2dff872 14519 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
14520}
14521
4520f53a 14522#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 14523static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14524{
14525}
14526#endif
14527
79e53945 14528static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14529 .fb_create = intel_user_framebuffer_create,
0632fef6 14530 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14531 .atomic_check = intel_atomic_check,
14532 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14533 .atomic_state_alloc = intel_atomic_state_alloc,
14534 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14535};
14536
e70236a8
JB
14537/* Set up chip specific display functions */
14538static void intel_init_display(struct drm_device *dev)
14539{
14540 struct drm_i915_private *dev_priv = dev->dev_private;
14541
ee9300bb
DV
14542 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14543 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14544 else if (IS_CHERRYVIEW(dev))
14545 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14546 else if (IS_VALLEYVIEW(dev))
14547 dev_priv->display.find_dpll = vlv_find_best_dpll;
14548 else if (IS_PINEVIEW(dev))
14549 dev_priv->display.find_dpll = pnv_find_best_dpll;
14550 else
14551 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14552
bc8d7dff
DL
14553 if (INTEL_INFO(dev)->gen >= 9) {
14554 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14555 dev_priv->display.get_initial_plane_config =
14556 skylake_get_initial_plane_config;
bc8d7dff
DL
14557 dev_priv->display.crtc_compute_clock =
14558 haswell_crtc_compute_clock;
14559 dev_priv->display.crtc_enable = haswell_crtc_enable;
14560 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14561 dev_priv->display.update_primary_plane =
14562 skylake_update_primary_plane;
14563 } else if (HAS_DDI(dev)) {
0e8ffe1b 14564 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14565 dev_priv->display.get_initial_plane_config =
14566 ironlake_get_initial_plane_config;
797d0259
ACO
14567 dev_priv->display.crtc_compute_clock =
14568 haswell_crtc_compute_clock;
4f771f10
PZ
14569 dev_priv->display.crtc_enable = haswell_crtc_enable;
14570 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14571 dev_priv->display.update_primary_plane =
14572 ironlake_update_primary_plane;
09b4ddf9 14573 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14574 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14575 dev_priv->display.get_initial_plane_config =
14576 ironlake_get_initial_plane_config;
3fb37703
ACO
14577 dev_priv->display.crtc_compute_clock =
14578 ironlake_crtc_compute_clock;
76e5a89c
DV
14579 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14580 dev_priv->display.crtc_disable = ironlake_crtc_disable;
262ca2b0
MR
14581 dev_priv->display.update_primary_plane =
14582 ironlake_update_primary_plane;
89b667f8
JB
14583 } else if (IS_VALLEYVIEW(dev)) {
14584 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14585 dev_priv->display.get_initial_plane_config =
14586 i9xx_get_initial_plane_config;
d6dfee7a 14587 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14588 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14589 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14590 dev_priv->display.update_primary_plane =
14591 i9xx_update_primary_plane;
f564048e 14592 } else {
0e8ffe1b 14593 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14594 dev_priv->display.get_initial_plane_config =
14595 i9xx_get_initial_plane_config;
d6dfee7a 14596 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14597 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14598 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14599 dev_priv->display.update_primary_plane =
14600 i9xx_update_primary_plane;
f564048e 14601 }
e70236a8 14602
e70236a8 14603 /* Returns the core display clock speed */
1652d19e
VS
14604 if (IS_SKYLAKE(dev))
14605 dev_priv->display.get_display_clock_speed =
14606 skylake_get_display_clock_speed;
14607 else if (IS_BROADWELL(dev))
14608 dev_priv->display.get_display_clock_speed =
14609 broadwell_get_display_clock_speed;
14610 else if (IS_HASWELL(dev))
14611 dev_priv->display.get_display_clock_speed =
14612 haswell_get_display_clock_speed;
14613 else if (IS_VALLEYVIEW(dev))
25eb05fc
JB
14614 dev_priv->display.get_display_clock_speed =
14615 valleyview_get_display_clock_speed;
b37a6434
VS
14616 else if (IS_GEN5(dev))
14617 dev_priv->display.get_display_clock_speed =
14618 ilk_get_display_clock_speed;
a7c66cd8 14619 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
34edce2f 14620 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
e70236a8
JB
14621 dev_priv->display.get_display_clock_speed =
14622 i945_get_display_clock_speed;
34edce2f
VS
14623 else if (IS_GM45(dev))
14624 dev_priv->display.get_display_clock_speed =
14625 gm45_get_display_clock_speed;
14626 else if (IS_CRESTLINE(dev))
14627 dev_priv->display.get_display_clock_speed =
14628 i965gm_get_display_clock_speed;
14629 else if (IS_PINEVIEW(dev))
14630 dev_priv->display.get_display_clock_speed =
14631 pnv_get_display_clock_speed;
14632 else if (IS_G33(dev) || IS_G4X(dev))
14633 dev_priv->display.get_display_clock_speed =
14634 g33_get_display_clock_speed;
e70236a8
JB
14635 else if (IS_I915G(dev))
14636 dev_priv->display.get_display_clock_speed =
14637 i915_get_display_clock_speed;
257a7ffc 14638 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14639 dev_priv->display.get_display_clock_speed =
14640 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
14641 else if (IS_PINEVIEW(dev))
14642 dev_priv->display.get_display_clock_speed =
14643 pnv_get_display_clock_speed;
e70236a8
JB
14644 else if (IS_I915GM(dev))
14645 dev_priv->display.get_display_clock_speed =
14646 i915gm_get_display_clock_speed;
14647 else if (IS_I865G(dev))
14648 dev_priv->display.get_display_clock_speed =
14649 i865_get_display_clock_speed;
f0f8a9ce 14650 else if (IS_I85X(dev))
e70236a8 14651 dev_priv->display.get_display_clock_speed =
1b1d2716 14652 i85x_get_display_clock_speed;
623e01e5
VS
14653 else { /* 830 */
14654 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14655 dev_priv->display.get_display_clock_speed =
14656 i830_get_display_clock_speed;
623e01e5 14657 }
e70236a8 14658
7c10a2b5 14659 if (IS_GEN5(dev)) {
3bb11b53 14660 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14661 } else if (IS_GEN6(dev)) {
14662 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14663 } else if (IS_IVYBRIDGE(dev)) {
14664 /* FIXME: detect B0+ stepping and use auto training */
14665 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14666 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14667 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
b432e5cf
VS
14668 if (IS_BROADWELL(dev))
14669 dev_priv->display.modeset_global_resources =
14670 broadwell_modeset_global_resources;
30a970c6
JB
14671 } else if (IS_VALLEYVIEW(dev)) {
14672 dev_priv->display.modeset_global_resources =
14673 valleyview_modeset_global_resources;
f8437dd1
VK
14674 } else if (IS_BROXTON(dev)) {
14675 dev_priv->display.modeset_global_resources =
14676 broxton_modeset_global_resources;
e70236a8 14677 }
8c9f3aaf 14678
8c9f3aaf
JB
14679 switch (INTEL_INFO(dev)->gen) {
14680 case 2:
14681 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14682 break;
14683
14684 case 3:
14685 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14686 break;
14687
14688 case 4:
14689 case 5:
14690 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14691 break;
14692
14693 case 6:
14694 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14695 break;
7c9017e5 14696 case 7:
4e0bbc31 14697 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14698 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14699 break;
830c81db 14700 case 9:
ba343e02
TU
14701 /* Drop through - unsupported since execlist only. */
14702 default:
14703 /* Default just returns -ENODEV to indicate unsupported */
14704 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14705 }
7bd688cd
JN
14706
14707 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
14708
14709 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
14710}
14711
b690e96c
JB
14712/*
14713 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14714 * resume, or other times. This quirk makes sure that's the case for
14715 * affected systems.
14716 */
0206e353 14717static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14718{
14719 struct drm_i915_private *dev_priv = dev->dev_private;
14720
14721 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14722 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14723}
14724
b6b5d049
VS
14725static void quirk_pipeb_force(struct drm_device *dev)
14726{
14727 struct drm_i915_private *dev_priv = dev->dev_private;
14728
14729 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14730 DRM_INFO("applying pipe b force quirk\n");
14731}
14732
435793df
KP
14733/*
14734 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14735 */
14736static void quirk_ssc_force_disable(struct drm_device *dev)
14737{
14738 struct drm_i915_private *dev_priv = dev->dev_private;
14739 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14740 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14741}
14742
4dca20ef 14743/*
5a15ab5b
CE
14744 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14745 * brightness value
4dca20ef
CE
14746 */
14747static void quirk_invert_brightness(struct drm_device *dev)
14748{
14749 struct drm_i915_private *dev_priv = dev->dev_private;
14750 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14751 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14752}
14753
9c72cc6f
SD
14754/* Some VBT's incorrectly indicate no backlight is present */
14755static void quirk_backlight_present(struct drm_device *dev)
14756{
14757 struct drm_i915_private *dev_priv = dev->dev_private;
14758 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14759 DRM_INFO("applying backlight present quirk\n");
14760}
14761
b690e96c
JB
14762struct intel_quirk {
14763 int device;
14764 int subsystem_vendor;
14765 int subsystem_device;
14766 void (*hook)(struct drm_device *dev);
14767};
14768
5f85f176
EE
14769/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14770struct intel_dmi_quirk {
14771 void (*hook)(struct drm_device *dev);
14772 const struct dmi_system_id (*dmi_id_list)[];
14773};
14774
14775static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14776{
14777 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14778 return 1;
14779}
14780
14781static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14782 {
14783 .dmi_id_list = &(const struct dmi_system_id[]) {
14784 {
14785 .callback = intel_dmi_reverse_brightness,
14786 .ident = "NCR Corporation",
14787 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14788 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14789 },
14790 },
14791 { } /* terminating entry */
14792 },
14793 .hook = quirk_invert_brightness,
14794 },
14795};
14796
c43b5634 14797static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14798 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14799 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14800
b690e96c
JB
14801 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14802 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14803
5f080c0f
VS
14804 /* 830 needs to leave pipe A & dpll A up */
14805 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14806
b6b5d049
VS
14807 /* 830 needs to leave pipe B & dpll B up */
14808 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14809
435793df
KP
14810 /* Lenovo U160 cannot use SSC on LVDS */
14811 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14812
14813 /* Sony Vaio Y cannot use SSC on LVDS */
14814 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14815
be505f64
AH
14816 /* Acer Aspire 5734Z must invert backlight brightness */
14817 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14818
14819 /* Acer/eMachines G725 */
14820 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14821
14822 /* Acer/eMachines e725 */
14823 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14824
14825 /* Acer/Packard Bell NCL20 */
14826 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14827
14828 /* Acer Aspire 4736Z */
14829 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14830
14831 /* Acer Aspire 5336 */
14832 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14833
14834 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14835 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14836
dfb3d47b
SD
14837 /* Acer C720 Chromebook (Core i3 4005U) */
14838 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14839
b2a9601c 14840 /* Apple Macbook 2,1 (Core 2 T7400) */
14841 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14842
d4967d8c
SD
14843 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14844 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14845
14846 /* HP Chromebook 14 (Celeron 2955U) */
14847 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14848
14849 /* Dell Chromebook 11 */
14850 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14851};
14852
14853static void intel_init_quirks(struct drm_device *dev)
14854{
14855 struct pci_dev *d = dev->pdev;
14856 int i;
14857
14858 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14859 struct intel_quirk *q = &intel_quirks[i];
14860
14861 if (d->device == q->device &&
14862 (d->subsystem_vendor == q->subsystem_vendor ||
14863 q->subsystem_vendor == PCI_ANY_ID) &&
14864 (d->subsystem_device == q->subsystem_device ||
14865 q->subsystem_device == PCI_ANY_ID))
14866 q->hook(dev);
14867 }
5f85f176
EE
14868 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14869 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14870 intel_dmi_quirks[i].hook(dev);
14871 }
b690e96c
JB
14872}
14873
9cce37f4
JB
14874/* Disable the VGA plane that we never use */
14875static void i915_disable_vga(struct drm_device *dev)
14876{
14877 struct drm_i915_private *dev_priv = dev->dev_private;
14878 u8 sr1;
766aa1c4 14879 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 14880
2b37c616 14881 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 14882 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14883 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14884 sr1 = inb(VGA_SR_DATA);
14885 outb(sr1 | 1<<5, VGA_SR_DATA);
14886 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14887 udelay(300);
14888
01f5a626 14889 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14890 POSTING_READ(vga_reg);
14891}
14892
f817586c
DV
14893void intel_modeset_init_hw(struct drm_device *dev)
14894{
b6283055 14895 intel_update_cdclk(dev);
a8f78b58 14896 intel_prepare_ddi(dev);
f817586c 14897 intel_init_clock_gating(dev);
8090c6b9 14898 intel_enable_gt_powersave(dev);
f817586c
DV
14899}
14900
79e53945
JB
14901void intel_modeset_init(struct drm_device *dev)
14902{
652c393a 14903 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 14904 int sprite, ret;
8cc87b75 14905 enum pipe pipe;
46f297fb 14906 struct intel_crtc *crtc;
79e53945
JB
14907
14908 drm_mode_config_init(dev);
14909
14910 dev->mode_config.min_width = 0;
14911 dev->mode_config.min_height = 0;
14912
019d96cb
DA
14913 dev->mode_config.preferred_depth = 24;
14914 dev->mode_config.prefer_shadow = 1;
14915
25bab385
TU
14916 dev->mode_config.allow_fb_modifiers = true;
14917
e6ecefaa 14918 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14919
b690e96c
JB
14920 intel_init_quirks(dev);
14921
1fa61106
ED
14922 intel_init_pm(dev);
14923
e3c74757
BW
14924 if (INTEL_INFO(dev)->num_pipes == 0)
14925 return;
14926
e70236a8 14927 intel_init_display(dev);
7c10a2b5 14928 intel_init_audio(dev);
e70236a8 14929
a6c45cf0
CW
14930 if (IS_GEN2(dev)) {
14931 dev->mode_config.max_width = 2048;
14932 dev->mode_config.max_height = 2048;
14933 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
14934 dev->mode_config.max_width = 4096;
14935 dev->mode_config.max_height = 4096;
79e53945 14936 } else {
a6c45cf0
CW
14937 dev->mode_config.max_width = 8192;
14938 dev->mode_config.max_height = 8192;
79e53945 14939 }
068be561 14940
dc41c154
VS
14941 if (IS_845G(dev) || IS_I865G(dev)) {
14942 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14943 dev->mode_config.cursor_height = 1023;
14944 } else if (IS_GEN2(dev)) {
068be561
DL
14945 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14946 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14947 } else {
14948 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14949 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14950 }
14951
5d4545ae 14952 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 14953
28c97730 14954 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
14955 INTEL_INFO(dev)->num_pipes,
14956 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 14957
055e393f 14958 for_each_pipe(dev_priv, pipe) {
8cc87b75 14959 intel_crtc_init(dev, pipe);
3bdcfc0c 14960 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 14961 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 14962 if (ret)
06da8da2 14963 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 14964 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 14965 }
79e53945
JB
14966 }
14967
f42bb70d
JB
14968 intel_init_dpio(dev);
14969
e72f9fbf 14970 intel_shared_dpll_init(dev);
ee7b9f93 14971
9cce37f4
JB
14972 /* Just disable it once at startup */
14973 i915_disable_vga(dev);
79e53945 14974 intel_setup_outputs(dev);
11be49eb
CW
14975
14976 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 14977 intel_fbc_disable(dev);
fa9fa083 14978
6e9f798d 14979 drm_modeset_lock_all(dev);
fa9fa083 14980 intel_modeset_setup_hw_state(dev, false);
6e9f798d 14981 drm_modeset_unlock_all(dev);
46f297fb 14982
d3fcc808 14983 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
14984 if (!crtc->active)
14985 continue;
14986
46f297fb 14987 /*
46f297fb
JB
14988 * Note that reserving the BIOS fb up front prevents us
14989 * from stuffing other stolen allocations like the ring
14990 * on top. This prevents some ugliness at boot time, and
14991 * can even allow for smooth boot transitions if the BIOS
14992 * fb is large enough for the active pipe configuration.
14993 */
5724dbd1
DL
14994 if (dev_priv->display.get_initial_plane_config) {
14995 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
14996 &crtc->plane_config);
14997 /*
14998 * If the fb is shared between multiple heads, we'll
14999 * just get the first one.
15000 */
f6936e29 15001 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
46f297fb 15002 }
46f297fb 15003 }
2c7111db
CW
15004}
15005
7fad798e
DV
15006static void intel_enable_pipe_a(struct drm_device *dev)
15007{
15008 struct intel_connector *connector;
15009 struct drm_connector *crt = NULL;
15010 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15011 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15012
15013 /* We can't just switch on the pipe A, we need to set things up with a
15014 * proper mode and output configuration. As a gross hack, enable pipe A
15015 * by enabling the load detect pipe once. */
3a3371ff 15016 for_each_intel_connector(dev, connector) {
7fad798e
DV
15017 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15018 crt = &connector->base;
15019 break;
15020 }
15021 }
15022
15023 if (!crt)
15024 return;
15025
208bf9fd 15026 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15027 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15028}
15029
fa555837
DV
15030static bool
15031intel_check_plane_mapping(struct intel_crtc *crtc)
15032{
7eb552ae
BW
15033 struct drm_device *dev = crtc->base.dev;
15034 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
15035 u32 reg, val;
15036
7eb552ae 15037 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15038 return true;
15039
15040 reg = DSPCNTR(!crtc->plane);
15041 val = I915_READ(reg);
15042
15043 if ((val & DISPLAY_PLANE_ENABLE) &&
15044 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15045 return false;
15046
15047 return true;
15048}
15049
24929352
DV
15050static void intel_sanitize_crtc(struct intel_crtc *crtc)
15051{
15052 struct drm_device *dev = crtc->base.dev;
15053 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 15054 u32 reg;
24929352 15055
24929352 15056 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 15057 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
15058 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15059
d3eaf884 15060 /* restore vblank interrupts to correct state */
9625604c 15061 drm_crtc_vblank_reset(&crtc->base);
d297e103
VS
15062 if (crtc->active) {
15063 update_scanline_offset(crtc);
9625604c
DV
15064 drm_crtc_vblank_on(&crtc->base);
15065 }
d3eaf884 15066
24929352 15067 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15068 * disable the crtc (and hence change the state) if it is wrong. Note
15069 * that gen4+ has a fixed plane -> pipe mapping. */
15070 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15071 struct intel_connector *connector;
15072 bool plane;
15073
24929352
DV
15074 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15075 crtc->base.base.id);
15076
15077 /* Pipe has the wrong plane attached and the plane is active.
15078 * Temporarily change the plane mapping and disable everything
15079 * ... */
15080 plane = crtc->plane;
b70709a6 15081 to_intel_plane_state(crtc->base.primary->state)->visible = true;
37ade417 15082 crtc->base.primary->crtc = &crtc->base;
24929352 15083 crtc->plane = !plane;
1b509259 15084 intel_crtc_control(&crtc->base, false);
24929352
DV
15085 crtc->plane = plane;
15086
15087 /* ... and break all links. */
3a3371ff 15088 for_each_intel_connector(dev, connector) {
24929352
DV
15089 if (connector->encoder->base.crtc != &crtc->base)
15090 continue;
15091
7f1950fb
EE
15092 connector->base.dpms = DRM_MODE_DPMS_OFF;
15093 connector->base.encoder = NULL;
24929352 15094 }
7f1950fb
EE
15095 /* multiple connectors may have the same encoder:
15096 * handle them and break crtc link separately */
3a3371ff 15097 for_each_intel_connector(dev, connector)
7f1950fb
EE
15098 if (connector->encoder->base.crtc == &crtc->base) {
15099 connector->encoder->base.crtc = NULL;
15100 connector->encoder->connectors_active = false;
15101 }
24929352
DV
15102
15103 WARN_ON(crtc->active);
83d65738 15104 crtc->base.state->enable = false;
49d6fa21 15105 crtc->base.state->active = false;
24929352
DV
15106 crtc->base.enabled = false;
15107 }
24929352 15108
7fad798e
DV
15109 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15110 crtc->pipe == PIPE_A && !crtc->active) {
15111 /* BIOS forgot to enable pipe A, this mostly happens after
15112 * resume. Force-enable the pipe to fix this, the update_dpms
15113 * call below we restore the pipe to the right state, but leave
15114 * the required bits on. */
15115 intel_enable_pipe_a(dev);
15116 }
15117
24929352
DV
15118 /* Adjust the state of the output pipe according to whether we
15119 * have active connectors/encoders. */
15120 intel_crtc_update_dpms(&crtc->base);
15121
53d9f4e9 15122 if (crtc->active != crtc->base.state->active) {
24929352
DV
15123 struct intel_encoder *encoder;
15124
15125 /* This can happen either due to bugs in the get_hw_state
15126 * functions or because the pipe is force-enabled due to the
15127 * pipe A quirk. */
15128 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15129 crtc->base.base.id,
83d65738 15130 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
15131 crtc->active ? "enabled" : "disabled");
15132
83d65738 15133 crtc->base.state->enable = crtc->active;
49d6fa21 15134 crtc->base.state->active = crtc->active;
24929352
DV
15135 crtc->base.enabled = crtc->active;
15136
15137 /* Because we only establish the connector -> encoder ->
15138 * crtc links if something is active, this means the
15139 * crtc is now deactivated. Break the links. connector
15140 * -> encoder links are only establish when things are
15141 * actually up, hence no need to break them. */
15142 WARN_ON(crtc->active);
15143
15144 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15145 WARN_ON(encoder->connectors_active);
15146 encoder->base.crtc = NULL;
15147 }
15148 }
c5ab3bc0 15149
a3ed6aad 15150 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15151 /*
15152 * We start out with underrun reporting disabled to avoid races.
15153 * For correct bookkeeping mark this on active crtcs.
15154 *
c5ab3bc0
DV
15155 * Also on gmch platforms we dont have any hardware bits to
15156 * disable the underrun reporting. Which means we need to start
15157 * out with underrun reporting disabled also on inactive pipes,
15158 * since otherwise we'll complain about the garbage we read when
15159 * e.g. coming up after runtime pm.
15160 *
4cc31489
DV
15161 * No protection against concurrent access is required - at
15162 * worst a fifo underrun happens which also sets this to false.
15163 */
15164 crtc->cpu_fifo_underrun_disabled = true;
15165 crtc->pch_fifo_underrun_disabled = true;
15166 }
24929352
DV
15167}
15168
15169static void intel_sanitize_encoder(struct intel_encoder *encoder)
15170{
15171 struct intel_connector *connector;
15172 struct drm_device *dev = encoder->base.dev;
15173
15174 /* We need to check both for a crtc link (meaning that the
15175 * encoder is active and trying to read from a pipe) and the
15176 * pipe itself being active. */
15177 bool has_active_crtc = encoder->base.crtc &&
15178 to_intel_crtc(encoder->base.crtc)->active;
15179
15180 if (encoder->connectors_active && !has_active_crtc) {
15181 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15182 encoder->base.base.id,
8e329a03 15183 encoder->base.name);
24929352
DV
15184
15185 /* Connector is active, but has no active pipe. This is
15186 * fallout from our resume register restoring. Disable
15187 * the encoder manually again. */
15188 if (encoder->base.crtc) {
15189 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15190 encoder->base.base.id,
8e329a03 15191 encoder->base.name);
24929352 15192 encoder->disable(encoder);
a62d1497
VS
15193 if (encoder->post_disable)
15194 encoder->post_disable(encoder);
24929352 15195 }
7f1950fb
EE
15196 encoder->base.crtc = NULL;
15197 encoder->connectors_active = false;
24929352
DV
15198
15199 /* Inconsistent output/port/pipe state happens presumably due to
15200 * a bug in one of the get_hw_state functions. Or someplace else
15201 * in our code, like the register restore mess on resume. Clamp
15202 * things to off as a safer default. */
3a3371ff 15203 for_each_intel_connector(dev, connector) {
24929352
DV
15204 if (connector->encoder != encoder)
15205 continue;
7f1950fb
EE
15206 connector->base.dpms = DRM_MODE_DPMS_OFF;
15207 connector->base.encoder = NULL;
24929352
DV
15208 }
15209 }
15210 /* Enabled encoders without active connectors will be fixed in
15211 * the crtc fixup. */
15212}
15213
04098753 15214void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15215{
15216 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 15217 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15218
04098753
ID
15219 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15220 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15221 i915_disable_vga(dev);
15222 }
15223}
15224
15225void i915_redisable_vga(struct drm_device *dev)
15226{
15227 struct drm_i915_private *dev_priv = dev->dev_private;
15228
8dc8a27c
PZ
15229 /* This function can be called both from intel_modeset_setup_hw_state or
15230 * at a very early point in our resume sequence, where the power well
15231 * structures are not yet restored. Since this function is at a very
15232 * paranoid "someone might have enabled VGA while we were not looking"
15233 * level, just check if the power well is enabled instead of trying to
15234 * follow the "don't touch the power well if we don't need it" policy
15235 * the rest of the driver uses. */
f458ebbc 15236 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15237 return;
15238
04098753 15239 i915_redisable_vga_power_on(dev);
0fde901f
KM
15240}
15241
98ec7739
VS
15242static bool primary_get_hw_state(struct intel_crtc *crtc)
15243{
15244 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15245
37ade417 15246 if (!crtc->base.enabled)
98ec7739
VS
15247 return false;
15248
15249 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15250}
15251
37ade417
ACO
15252static int readout_hw_crtc_state(struct drm_atomic_state *state,
15253 struct intel_crtc *crtc)
24929352 15254{
37ade417
ACO
15255 struct drm_i915_private *dev_priv = to_i915(state->dev);
15256 struct intel_crtc_state *crtc_state;
15257 struct drm_plane *primary = crtc->base.primary;
15258 struct drm_plane_state *drm_plane_state;
15259 struct intel_plane_state *plane_state;
15260 int ret;
24929352 15261
37ade417
ACO
15262 crtc_state = intel_atomic_get_crtc_state(state, crtc);
15263 if (IS_ERR(crtc_state))
15264 return PTR_ERR(crtc_state);
b70709a6 15265
37ade417
ACO
15266 ret = drm_atomic_add_affected_planes(state, &crtc->base);
15267 if (ret)
15268 return ret;
3b117c8f 15269
37ade417
ACO
15270 memset(crtc_state, 0, sizeof(*crtc_state));
15271 crtc_state->base.crtc = &crtc->base;
15272 crtc_state->base.state = state;
9953599b 15273
37ade417 15274 crtc_state->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
24929352 15275
37ade417
ACO
15276 crtc_state->base.enable = crtc_state->base.active =
15277 crtc->base.enabled = dev_priv->display.get_pipe_config(crtc, crtc_state);
b70709a6 15278
37ade417
ACO
15279 /* update transitional state */
15280 crtc->active = crtc_state->base.active;
15281 crtc->config = crtc_state;
24929352 15282
37ade417
ACO
15283 drm_plane_state = drm_atomic_get_plane_state(state, primary);
15284 if (IS_ERR(drm_plane_state))
15285 return PTR_ERR(drm_plane_state);
15286
15287 plane_state = to_intel_plane_state(drm_plane_state);
15288 plane_state->visible = primary_get_hw_state(crtc);
15289
15290 if (plane_state->visible) {
15291 primary->crtc = &crtc->base;
15292 crtc_state->base.plane_mask |= 1 << drm_plane_index(primary);
15293 } else
15294 crtc_state->base.plane_mask &= ~(1 << drm_plane_index(primary));
15295
15296 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15297 crtc->base.base.id,
15298 crtc_state->base.active ? "enabled" : "disabled");
15299
15300 return 0;
15301}
24929352 15302
37ade417
ACO
15303static int readout_hw_pll_state(struct drm_atomic_state *state)
15304{
15305 struct drm_i915_private *dev_priv = to_i915(state->dev);
15306 struct intel_shared_dpll_config *shared_dpll;
15307 struct intel_crtc *crtc;
15308 struct intel_crtc_state *crtc_state;
15309 int i;
15310
15311 shared_dpll = intel_atomic_get_shared_dpll_state(state);
5358901f
DV
15312 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15313 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15314
3e369b76 15315 pll->on = pll->get_hw_state(dev_priv, pll,
37ade417
ACO
15316 &shared_dpll[i].hw_state);
15317
5358901f 15318 pll->active = 0;
37ade417
ACO
15319 shared_dpll[i].crtc_mask = 0;
15320
15321 for_each_intel_crtc(state->dev, crtc) {
15322 crtc_state = intel_atomic_get_crtc_state(state, crtc);
15323 if (IS_ERR(crtc_state))
15324 return PTR_ERR(crtc_state);
15325
15326 if (crtc_state->base.active &&
15327 crtc_state->shared_dpll == i) {
5358901f 15328 pll->active++;
37ade417
ACO
15329 shared_dpll[i].crtc_mask |=
15330 1 << crtc->pipe;
1e6f2ddc 15331 }
5358901f 15332 }
5358901f 15333
1e6f2ddc 15334 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
37ade417
ACO
15335 pll->name, shared_dpll[i].crtc_mask,
15336 pll->on);
bd2bb1b9 15337
37ade417 15338 if (shared_dpll[i].crtc_mask)
bd2bb1b9 15339 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
15340 }
15341
37ade417
ACO
15342 return 0;
15343}
24929352 15344
37ade417
ACO
15345static struct drm_connector_state *
15346get_connector_state_for_encoder(struct drm_atomic_state *state,
15347 struct intel_encoder *encoder)
15348{
15349 struct drm_connector *connector;
15350 struct drm_connector_state *connector_state;
15351 int i;
24929352 15352
37ade417
ACO
15353 for_each_connector_in_state(state, connector, connector_state, i)
15354 if (connector_state->best_encoder == &encoder->base)
15355 return connector_state;
15356
15357 return NULL;
15358}
15359
15360static int readout_hw_connector_encoder_state(struct drm_atomic_state *state)
15361{
15362 struct drm_device *dev = state->dev;
15363 struct drm_i915_private *dev_priv = to_i915(state->dev);
15364 struct intel_crtc *crtc;
15365 struct drm_crtc_state *drm_crtc_state;
15366 struct intel_crtc_state *crtc_state;
15367 struct intel_encoder *encoder;
15368 struct intel_connector *connector;
15369 struct drm_connector_state *connector_state;
15370 enum pipe pipe;
24929352 15371
3a3371ff 15372 for_each_intel_connector(dev, connector) {
37ade417
ACO
15373 connector_state =
15374 drm_atomic_get_connector_state(state, &connector->base);
15375 if (IS_ERR(connector_state))
15376 return PTR_ERR(connector_state);
15377
24929352
DV
15378 if (connector->get_hw_state(connector)) {
15379 connector->base.dpms = DRM_MODE_DPMS_ON;
24929352
DV
15380 connector->base.encoder = &connector->encoder->base;
15381 } else {
15382 connector->base.dpms = DRM_MODE_DPMS_OFF;
15383 connector->base.encoder = NULL;
15384 }
37ade417
ACO
15385
15386 /* We'll update the crtc field when reading encoder state */
15387 connector_state->crtc = NULL;
15388
15389 connector_state->best_encoder = connector->base.encoder;
15390
24929352
DV
15391 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15392 connector->base.base.id,
c23cc417 15393 connector->base.name,
24929352
DV
15394 connector->base.encoder ? "enabled" : "disabled");
15395 }
37ade417
ACO
15396
15397 for_each_intel_encoder(dev, encoder) {
15398 pipe = 0;
15399
15400 connector_state =
15401 get_connector_state_for_encoder(state, encoder);
15402
15403 encoder->connectors_active = !!connector_state;
15404
15405 if (encoder->get_hw_state(encoder, &pipe)) {
15406 encoder->base.crtc =
15407 dev_priv->pipe_to_crtc_mapping[pipe];
15408 crtc = to_intel_crtc(encoder->base.crtc);
15409
15410 drm_crtc_state =
15411 state->crtc_states[drm_crtc_index(&crtc->base)];
15412 crtc_state = to_intel_crtc_state(drm_crtc_state);
15413
15414 encoder->get_config(encoder, crtc_state);
15415
15416 if (connector_state)
15417 connector_state->crtc = &crtc->base;
15418 } else {
15419 encoder->base.crtc = NULL;
15420 }
15421
15422 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
15423 encoder->base.base.id,
15424 encoder->base.name,
15425 encoder->base.crtc ? "enabled" : "disabled",
15426 pipe_name(pipe));
15427 }
15428
15429 return 0;
15430}
15431
15432static struct drm_atomic_state *
15433intel_modeset_readout_hw_state(struct drm_device *dev)
15434{
15435 struct intel_crtc *crtc;
15436 int ret = 0;
15437
15438 struct drm_atomic_state *state;
15439
15440 state = drm_atomic_state_alloc(dev);
15441 if (!state)
15442 return ERR_PTR(-ENOMEM);
15443
15444 state->acquire_ctx = dev->mode_config.acquire_ctx;
15445
15446 for_each_intel_crtc(dev, crtc) {
15447 ret = readout_hw_crtc_state(state, crtc);
15448 if (ret)
15449 goto err_free;
15450 }
15451
15452 ret = readout_hw_pll_state(state);
15453 if (ret)
15454 goto err_free;
15455
15456 ret = readout_hw_connector_encoder_state(state);
15457 if (ret)
15458 goto err_free;
15459
15460 return state;
15461
15462err_free:
15463 drm_atomic_state_free(state);
15464 return ERR_PTR(ret);
30e984df
DV
15465}
15466
15467/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15468 * and i915 state tracking structures. */
15469void intel_modeset_setup_hw_state(struct drm_device *dev,
15470 bool force_restore)
15471{
15472 struct drm_i915_private *dev_priv = dev->dev_private;
37ade417
ACO
15473 struct drm_crtc *crtc;
15474 struct drm_crtc_state *crtc_state;
30e984df 15475 struct intel_encoder *encoder;
37ade417
ACO
15476 struct drm_atomic_state *state;
15477 struct intel_shared_dpll_config shared_dplls[I915_NUM_PLLS];
35c95375 15478 int i;
30e984df 15479
37ade417
ACO
15480 state = intel_modeset_readout_hw_state(dev);
15481 if (IS_ERR(state)) {
15482 DRM_ERROR("Failed to read out hw state\n");
15483 return;
babea61d
JB
15484 }
15485
37ade417
ACO
15486 drm_atomic_helper_swap_state(dev, state);
15487
15488 /* swap sw/hw dpll state */
15489 intel_atomic_duplicate_dpll_state(dev_priv, shared_dplls);
15490 intel_shared_dpll_commit(state);
15491 memcpy(to_intel_atomic_state(state)->shared_dpll,
15492 shared_dplls, sizeof(*shared_dplls) * dev_priv->num_shared_dpll);
15493
24929352 15494 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15495 for_each_intel_encoder(dev, encoder) {
24929352
DV
15496 intel_sanitize_encoder(encoder);
15497 }
15498
37ade417
ACO
15499 for_each_crtc_in_state(state, crtc, crtc_state, i) {
15500 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
15501
15502 /* prevent unnneeded restores with force_restore */
15503 crtc_state->active_changed =
15504 crtc_state->mode_changed =
15505 crtc_state->planes_changed = false;
15506
15507 if (crtc->enabled) {
15508 intel_mode_from_pipe_config(&crtc->state->mode,
15509 to_intel_crtc_state(crtc->state));
15510
15511 drm_mode_copy(&crtc->mode, &crtc->state->mode);
15512 drm_mode_copy(&crtc->hwmode,
15513 &crtc->state->adjusted_mode);
15514 }
15515
15516 intel_sanitize_crtc(intel_crtc);
15517
15518 /*
15519 * sanitize_crtc may have forced an update of crtc->state,
15520 * so reload in intel_dump_pipe_config
15521 */
15522 intel_dump_pipe_config(intel_crtc,
15523 to_intel_crtc_state(crtc->state),
6e3c9717 15524 "[setup_hw_state]");
24929352 15525 }
9a935856 15526
d29b2f9d
ACO
15527 intel_modeset_update_connector_atomic_state(dev);
15528
35c95375
DV
15529 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15530 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15531
15532 if (!pll->on || pll->active)
15533 continue;
15534
15535 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15536
15537 pll->disable(dev_priv, pll);
15538 pll->on = false;
15539 }
15540
3078999f
PB
15541 if (IS_GEN9(dev))
15542 skl_wm_get_hw_state(dev);
15543 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
15544 ilk_wm_get_hw_state(dev);
15545
45e2b5f6 15546 if (force_restore) {
37ade417 15547 int ret;
7d0bc1ea 15548
37ade417 15549 i915_redisable_vga(dev);
f30da187 15550
37ade417
ACO
15551 ret = intel_set_mode(state);
15552 if (ret) {
15553 DRM_ERROR("Failed to restore previous mode\n");
15554 drm_atomic_state_free(state);
45e2b5f6
DV
15555 }
15556 } else {
37ade417 15557 drm_atomic_state_free(state);
45e2b5f6 15558 }
8af6cf88
DV
15559
15560 intel_modeset_check_state(dev);
2c7111db
CW
15561}
15562
15563void intel_modeset_gem_init(struct drm_device *dev)
15564{
92122789 15565 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 15566 struct drm_crtc *c;
2ff8fde1 15567 struct drm_i915_gem_object *obj;
e0d6149b 15568 int ret;
484b41dd 15569
ae48434c
ID
15570 mutex_lock(&dev->struct_mutex);
15571 intel_init_gt_powersave(dev);
15572 mutex_unlock(&dev->struct_mutex);
15573
92122789
JB
15574 /*
15575 * There may be no VBT; and if the BIOS enabled SSC we can
15576 * just keep using it to avoid unnecessary flicker. Whereas if the
15577 * BIOS isn't using it, don't assume it will work even if the VBT
15578 * indicates as much.
15579 */
15580 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15581 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15582 DREF_SSC1_ENABLE);
15583
1833b134 15584 intel_modeset_init_hw(dev);
02e792fb
DV
15585
15586 intel_setup_overlay(dev);
484b41dd
JB
15587
15588 /*
15589 * Make sure any fbs we allocated at startup are properly
15590 * pinned & fenced. When we do the allocation it's too early
15591 * for this.
15592 */
70e1e0ec 15593 for_each_crtc(dev, c) {
2ff8fde1
MR
15594 obj = intel_fb_obj(c->primary->fb);
15595 if (obj == NULL)
484b41dd
JB
15596 continue;
15597
e0d6149b
TU
15598 mutex_lock(&dev->struct_mutex);
15599 ret = intel_pin_and_fence_fb_obj(c->primary,
15600 c->primary->fb,
15601 c->primary->state,
15602 NULL);
15603 mutex_unlock(&dev->struct_mutex);
15604 if (ret) {
484b41dd
JB
15605 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15606 to_intel_crtc(c)->pipe);
66e514c1
DA
15607 drm_framebuffer_unreference(c->primary->fb);
15608 c->primary->fb = NULL;
36750f28 15609 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 15610 update_state_fb(c->primary);
36750f28 15611 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15612 }
15613 }
0962c3c9
VS
15614
15615 intel_backlight_register(dev);
79e53945
JB
15616}
15617
4932e2c3
ID
15618void intel_connector_unregister(struct intel_connector *intel_connector)
15619{
15620 struct drm_connector *connector = &intel_connector->base;
15621
15622 intel_panel_destroy_backlight(connector);
34ea3d38 15623 drm_connector_unregister(connector);
4932e2c3
ID
15624}
15625
79e53945
JB
15626void intel_modeset_cleanup(struct drm_device *dev)
15627{
652c393a 15628 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 15629 struct drm_connector *connector;
652c393a 15630
2eb5252e
ID
15631 intel_disable_gt_powersave(dev);
15632
0962c3c9
VS
15633 intel_backlight_unregister(dev);
15634
fd0c0642
DV
15635 /*
15636 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15637 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15638 * experience fancy races otherwise.
15639 */
2aeb7d3a 15640 intel_irq_uninstall(dev_priv);
eb21b92b 15641
fd0c0642
DV
15642 /*
15643 * Due to the hpd irq storm handling the hotplug work can re-arm the
15644 * poll handlers. Hence disable polling after hpd handling is shut down.
15645 */
f87ea761 15646 drm_kms_helper_poll_fini(dev);
fd0c0642 15647
652c393a
JB
15648 mutex_lock(&dev->struct_mutex);
15649
723bfd70
JB
15650 intel_unregister_dsm_handler();
15651
7ff0ebcc 15652 intel_fbc_disable(dev);
e70236a8 15653
69341a5e
KH
15654 mutex_unlock(&dev->struct_mutex);
15655
1630fe75
CW
15656 /* flush any delayed tasks or pending work */
15657 flush_scheduled_work();
15658
db31af1d
JN
15659 /* destroy the backlight and sysfs files before encoders/connectors */
15660 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
15661 struct intel_connector *intel_connector;
15662
15663 intel_connector = to_intel_connector(connector);
15664 intel_connector->unregister(intel_connector);
db31af1d 15665 }
d9255d57 15666
79e53945 15667 drm_mode_config_cleanup(dev);
4d7bb011
DV
15668
15669 intel_cleanup_overlay(dev);
ae48434c
ID
15670
15671 mutex_lock(&dev->struct_mutex);
15672 intel_cleanup_gt_powersave(dev);
15673 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15674}
15675
f1c79df3
ZW
15676/*
15677 * Return which encoder is currently attached for connector.
15678 */
df0e9248 15679struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15680{
df0e9248
CW
15681 return &intel_attached_encoder(connector)->base;
15682}
f1c79df3 15683
df0e9248
CW
15684void intel_connector_attach_encoder(struct intel_connector *connector,
15685 struct intel_encoder *encoder)
15686{
15687 connector->encoder = encoder;
15688 drm_mode_connector_attach_encoder(&connector->base,
15689 &encoder->base);
79e53945 15690}
28d52043
DA
15691
15692/*
15693 * set vga decode state - true == enable VGA decode
15694 */
15695int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15696{
15697 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15698 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15699 u16 gmch_ctrl;
15700
75fa041d
CW
15701 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15702 DRM_ERROR("failed to read control word\n");
15703 return -EIO;
15704 }
15705
c0cc8a55
CW
15706 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15707 return 0;
15708
28d52043
DA
15709 if (state)
15710 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15711 else
15712 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15713
15714 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15715 DRM_ERROR("failed to write control word\n");
15716 return -EIO;
15717 }
15718
28d52043
DA
15719 return 0;
15720}
c4a1d9e4 15721
c4a1d9e4 15722struct intel_display_error_state {
ff57f1b0
PZ
15723
15724 u32 power_well_driver;
15725
63b66e5b
CW
15726 int num_transcoders;
15727
c4a1d9e4
CW
15728 struct intel_cursor_error_state {
15729 u32 control;
15730 u32 position;
15731 u32 base;
15732 u32 size;
52331309 15733 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15734
15735 struct intel_pipe_error_state {
ddf9c536 15736 bool power_domain_on;
c4a1d9e4 15737 u32 source;
f301b1e1 15738 u32 stat;
52331309 15739 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15740
15741 struct intel_plane_error_state {
15742 u32 control;
15743 u32 stride;
15744 u32 size;
15745 u32 pos;
15746 u32 addr;
15747 u32 surface;
15748 u32 tile_offset;
52331309 15749 } plane[I915_MAX_PIPES];
63b66e5b
CW
15750
15751 struct intel_transcoder_error_state {
ddf9c536 15752 bool power_domain_on;
63b66e5b
CW
15753 enum transcoder cpu_transcoder;
15754
15755 u32 conf;
15756
15757 u32 htotal;
15758 u32 hblank;
15759 u32 hsync;
15760 u32 vtotal;
15761 u32 vblank;
15762 u32 vsync;
15763 } transcoder[4];
c4a1d9e4
CW
15764};
15765
15766struct intel_display_error_state *
15767intel_display_capture_error_state(struct drm_device *dev)
15768{
fbee40df 15769 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 15770 struct intel_display_error_state *error;
63b66e5b
CW
15771 int transcoders[] = {
15772 TRANSCODER_A,
15773 TRANSCODER_B,
15774 TRANSCODER_C,
15775 TRANSCODER_EDP,
15776 };
c4a1d9e4
CW
15777 int i;
15778
63b66e5b
CW
15779 if (INTEL_INFO(dev)->num_pipes == 0)
15780 return NULL;
15781
9d1cb914 15782 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15783 if (error == NULL)
15784 return NULL;
15785
190be112 15786 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
15787 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15788
055e393f 15789 for_each_pipe(dev_priv, i) {
ddf9c536 15790 error->pipe[i].power_domain_on =
f458ebbc
DV
15791 __intel_display_power_is_enabled(dev_priv,
15792 POWER_DOMAIN_PIPE(i));
ddf9c536 15793 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15794 continue;
15795
5efb3e28
VS
15796 error->cursor[i].control = I915_READ(CURCNTR(i));
15797 error->cursor[i].position = I915_READ(CURPOS(i));
15798 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15799
15800 error->plane[i].control = I915_READ(DSPCNTR(i));
15801 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 15802 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 15803 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15804 error->plane[i].pos = I915_READ(DSPPOS(i));
15805 }
ca291363
PZ
15806 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15807 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
15808 if (INTEL_INFO(dev)->gen >= 4) {
15809 error->plane[i].surface = I915_READ(DSPSURF(i));
15810 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15811 }
15812
c4a1d9e4 15813 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15814
3abfce77 15815 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 15816 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15817 }
15818
15819 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15820 if (HAS_DDI(dev_priv->dev))
15821 error->num_transcoders++; /* Account for eDP. */
15822
15823 for (i = 0; i < error->num_transcoders; i++) {
15824 enum transcoder cpu_transcoder = transcoders[i];
15825
ddf9c536 15826 error->transcoder[i].power_domain_on =
f458ebbc 15827 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15828 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15829 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15830 continue;
15831
63b66e5b
CW
15832 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15833
15834 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15835 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15836 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15837 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15838 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15839 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15840 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15841 }
15842
15843 return error;
15844}
15845
edc3d884
MK
15846#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15847
c4a1d9e4 15848void
edc3d884 15849intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15850 struct drm_device *dev,
15851 struct intel_display_error_state *error)
15852{
055e393f 15853 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15854 int i;
15855
63b66e5b
CW
15856 if (!error)
15857 return;
15858
edc3d884 15859 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15860 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15861 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15862 error->power_well_driver);
055e393f 15863 for_each_pipe(dev_priv, i) {
edc3d884 15864 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
15865 err_printf(m, " Power: %s\n",
15866 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 15867 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15868 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15869
15870 err_printf(m, "Plane [%d]:\n", i);
15871 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15872 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15873 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15874 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15875 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15876 }
4b71a570 15877 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15878 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15879 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15880 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15881 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15882 }
15883
edc3d884
MK
15884 err_printf(m, "Cursor [%d]:\n", i);
15885 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15886 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15887 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15888 }
63b66e5b
CW
15889
15890 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 15891 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 15892 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
15893 err_printf(m, " Power: %s\n",
15894 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
15895 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15896 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15897 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15898 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15899 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15900 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15901 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15902 }
c4a1d9e4 15903}
e2fcdaa9
VS
15904
15905void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15906{
15907 struct intel_crtc *crtc;
15908
15909 for_each_intel_crtc(dev, crtc) {
15910 struct intel_unpin_work *work;
e2fcdaa9 15911
5e2d7afc 15912 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
15913
15914 work = crtc->unpin_work;
15915
15916 if (work && work->event &&
15917 work->event->base.file_priv == file) {
15918 kfree(work->event);
15919 work->event = NULL;
15920 }
15921
5e2d7afc 15922 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
15923 }
15924}