]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Remove mostly unused variable in intel_rotate_fb_obj_pages
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
e5510fac 39#include "i915_trace.h"
319c1d42 40#include <drm/drm_atomic.h>
c196e1d6 41#include <drm/drm_atomic_helper.h>
760285e7
DH
42#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
465c120c
MR
44#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
c0f372b3 46#include <linux/dma_remapping.h>
79e53945 47
465c120c 48/* Primary plane formats for gen <= 3 */
568db4f2 49static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
50 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
465c120c 52 DRM_FORMAT_XRGB1555,
67fe7dc5 53 DRM_FORMAT_XRGB8888,
465c120c
MR
54};
55
56/* Primary plane formats for gen >= 4 */
568db4f2 57static const uint32_t i965_primary_formats[] = {
6c0fd451
DL
58 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
60 DRM_FORMAT_XRGB8888,
61 DRM_FORMAT_XBGR8888,
62 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
64};
65
66static const uint32_t skl_primary_formats[] = {
67fe7dc5
DL
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
465c120c 70 DRM_FORMAT_XBGR8888,
67fe7dc5 71 DRM_FORMAT_ARGB8888,
465c120c
MR
72 DRM_FORMAT_ABGR8888,
73 DRM_FORMAT_XRGB2101010,
465c120c 74 DRM_FORMAT_XBGR2101010,
465c120c
MR
75};
76
3d7d6510
MR
77/* Cursor formats */
78static const uint32_t intel_cursor_formats[] = {
79 DRM_FORMAT_ARGB8888,
80};
81
6b383a7f 82static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 83
f1f644dc 84static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 85 struct intel_crtc_state *pipe_config);
18442d08 86static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 87 struct intel_crtc_state *pipe_config);
f1f644dc 88
568c634a 89static int intel_set_mode(struct drm_atomic_state *state);
eb1bfe80
JB
90static int intel_framebuffer_init(struct drm_device *dev,
91 struct intel_framebuffer *ifb,
92 struct drm_mode_fb_cmd2 *mode_cmd,
93 struct drm_i915_gem_object *obj);
5b18e57c
DV
94static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
95static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 96static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
97 struct intel_link_m_n *m_n,
98 struct intel_link_m_n *m2_n2);
29407aab 99static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
100static void haswell_set_pipeconf(struct drm_crtc *crtc);
101static void intel_set_pipe_csc(struct drm_crtc *crtc);
d288f65f 102static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 103 const struct intel_crtc_state *pipe_config);
d288f65f 104static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 105 const struct intel_crtc_state *pipe_config);
ea2c67bb
MR
106static void intel_begin_crtc_commit(struct drm_crtc *crtc);
107static void intel_finish_crtc_commit(struct drm_crtc *crtc);
549e2bfb
CK
108static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
109 struct intel_crtc_state *crtc_state);
5ab7b0b7
ID
110static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
111 int num_connectors);
e7457a9a 112
0e32b39c
DA
113static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
114{
115 if (!connector->mst_port)
116 return connector->encoder;
117 else
118 return &connector->mst_port->mst_encoders[pipe]->base;
119}
120
79e53945 121typedef struct {
0206e353 122 int min, max;
79e53945
JB
123} intel_range_t;
124
125typedef struct {
0206e353
AJ
126 int dot_limit;
127 int p2_slow, p2_fast;
79e53945
JB
128} intel_p2_t;
129
d4906093
ML
130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
0206e353
AJ
132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
d4906093 134};
79e53945 135
d2acd215
DV
136int
137intel_pch_rawclk(struct drm_device *dev)
138{
139 struct drm_i915_private *dev_priv = dev->dev_private;
140
141 WARN_ON(!HAS_PCH_SPLIT(dev));
142
143 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
144}
145
021357ac
CW
146static inline u32 /* units of 100MHz */
147intel_fdi_link_freq(struct drm_device *dev)
148{
8b99e68c
CW
149 if (IS_GEN5(dev)) {
150 struct drm_i915_private *dev_priv = dev->dev_private;
151 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
152 } else
153 return 27;
021357ac
CW
154}
155
5d536e28 156static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 157 .dot = { .min = 25000, .max = 350000 },
9c333719 158 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 159 .n = { .min = 2, .max = 16 },
0206e353
AJ
160 .m = { .min = 96, .max = 140 },
161 .m1 = { .min = 18, .max = 26 },
162 .m2 = { .min = 6, .max = 16 },
163 .p = { .min = 4, .max = 128 },
164 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
165 .p2 = { .dot_limit = 165000,
166 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
167};
168
5d536e28
DV
169static const intel_limit_t intel_limits_i8xx_dvo = {
170 .dot = { .min = 25000, .max = 350000 },
9c333719 171 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 172 .n = { .min = 2, .max = 16 },
5d536e28
DV
173 .m = { .min = 96, .max = 140 },
174 .m1 = { .min = 18, .max = 26 },
175 .m2 = { .min = 6, .max = 16 },
176 .p = { .min = 4, .max = 128 },
177 .p1 = { .min = 2, .max = 33 },
178 .p2 = { .dot_limit = 165000,
179 .p2_slow = 4, .p2_fast = 4 },
180};
181
e4b36699 182static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 183 .dot = { .min = 25000, .max = 350000 },
9c333719 184 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 185 .n = { .min = 2, .max = 16 },
0206e353
AJ
186 .m = { .min = 96, .max = 140 },
187 .m1 = { .min = 18, .max = 26 },
188 .m2 = { .min = 6, .max = 16 },
189 .p = { .min = 4, .max = 128 },
190 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
191 .p2 = { .dot_limit = 165000,
192 .p2_slow = 14, .p2_fast = 7 },
e4b36699 193};
273e27ca 194
e4b36699 195static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
196 .dot = { .min = 20000, .max = 400000 },
197 .vco = { .min = 1400000, .max = 2800000 },
198 .n = { .min = 1, .max = 6 },
199 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
200 .m1 = { .min = 8, .max = 18 },
201 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
202 .p = { .min = 5, .max = 80 },
203 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
204 .p2 = { .dot_limit = 200000,
205 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
206};
207
208static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
209 .dot = { .min = 20000, .max = 400000 },
210 .vco = { .min = 1400000, .max = 2800000 },
211 .n = { .min = 1, .max = 6 },
212 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
213 .m1 = { .min = 8, .max = 18 },
214 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
215 .p = { .min = 7, .max = 98 },
216 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
217 .p2 = { .dot_limit = 112000,
218 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
219};
220
273e27ca 221
e4b36699 222static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
223 .dot = { .min = 25000, .max = 270000 },
224 .vco = { .min = 1750000, .max = 3500000},
225 .n = { .min = 1, .max = 4 },
226 .m = { .min = 104, .max = 138 },
227 .m1 = { .min = 17, .max = 23 },
228 .m2 = { .min = 5, .max = 11 },
229 .p = { .min = 10, .max = 30 },
230 .p1 = { .min = 1, .max = 3},
231 .p2 = { .dot_limit = 270000,
232 .p2_slow = 10,
233 .p2_fast = 10
044c7c41 234 },
e4b36699
KP
235};
236
237static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
238 .dot = { .min = 22000, .max = 400000 },
239 .vco = { .min = 1750000, .max = 3500000},
240 .n = { .min = 1, .max = 4 },
241 .m = { .min = 104, .max = 138 },
242 .m1 = { .min = 16, .max = 23 },
243 .m2 = { .min = 5, .max = 11 },
244 .p = { .min = 5, .max = 80 },
245 .p1 = { .min = 1, .max = 8},
246 .p2 = { .dot_limit = 165000,
247 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
248};
249
250static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
251 .dot = { .min = 20000, .max = 115000 },
252 .vco = { .min = 1750000, .max = 3500000 },
253 .n = { .min = 1, .max = 3 },
254 .m = { .min = 104, .max = 138 },
255 .m1 = { .min = 17, .max = 23 },
256 .m2 = { .min = 5, .max = 11 },
257 .p = { .min = 28, .max = 112 },
258 .p1 = { .min = 2, .max = 8 },
259 .p2 = { .dot_limit = 0,
260 .p2_slow = 14, .p2_fast = 14
044c7c41 261 },
e4b36699
KP
262};
263
264static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
265 .dot = { .min = 80000, .max = 224000 },
266 .vco = { .min = 1750000, .max = 3500000 },
267 .n = { .min = 1, .max = 3 },
268 .m = { .min = 104, .max = 138 },
269 .m1 = { .min = 17, .max = 23 },
270 .m2 = { .min = 5, .max = 11 },
271 .p = { .min = 14, .max = 42 },
272 .p1 = { .min = 2, .max = 6 },
273 .p2 = { .dot_limit = 0,
274 .p2_slow = 7, .p2_fast = 7
044c7c41 275 },
e4b36699
KP
276};
277
f2b115e6 278static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
279 .dot = { .min = 20000, .max = 400000},
280 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 281 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
282 .n = { .min = 3, .max = 6 },
283 .m = { .min = 2, .max = 256 },
273e27ca 284 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
285 .m1 = { .min = 0, .max = 0 },
286 .m2 = { .min = 0, .max = 254 },
287 .p = { .min = 5, .max = 80 },
288 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
289 .p2 = { .dot_limit = 200000,
290 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
291};
292
f2b115e6 293static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
294 .dot = { .min = 20000, .max = 400000 },
295 .vco = { .min = 1700000, .max = 3500000 },
296 .n = { .min = 3, .max = 6 },
297 .m = { .min = 2, .max = 256 },
298 .m1 = { .min = 0, .max = 0 },
299 .m2 = { .min = 0, .max = 254 },
300 .p = { .min = 7, .max = 112 },
301 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
302 .p2 = { .dot_limit = 112000,
303 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
304};
305
273e27ca
EA
306/* Ironlake / Sandybridge
307 *
308 * We calculate clock using (register_value + 2) for N/M1/M2, so here
309 * the range value for them is (actual_value - 2).
310 */
b91ad0ec 311static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
312 .dot = { .min = 25000, .max = 350000 },
313 .vco = { .min = 1760000, .max = 3510000 },
314 .n = { .min = 1, .max = 5 },
315 .m = { .min = 79, .max = 127 },
316 .m1 = { .min = 12, .max = 22 },
317 .m2 = { .min = 5, .max = 9 },
318 .p = { .min = 5, .max = 80 },
319 .p1 = { .min = 1, .max = 8 },
320 .p2 = { .dot_limit = 225000,
321 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
322};
323
b91ad0ec 324static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
325 .dot = { .min = 25000, .max = 350000 },
326 .vco = { .min = 1760000, .max = 3510000 },
327 .n = { .min = 1, .max = 3 },
328 .m = { .min = 79, .max = 118 },
329 .m1 = { .min = 12, .max = 22 },
330 .m2 = { .min = 5, .max = 9 },
331 .p = { .min = 28, .max = 112 },
332 .p1 = { .min = 2, .max = 8 },
333 .p2 = { .dot_limit = 225000,
334 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
335};
336
337static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
338 .dot = { .min = 25000, .max = 350000 },
339 .vco = { .min = 1760000, .max = 3510000 },
340 .n = { .min = 1, .max = 3 },
341 .m = { .min = 79, .max = 127 },
342 .m1 = { .min = 12, .max = 22 },
343 .m2 = { .min = 5, .max = 9 },
344 .p = { .min = 14, .max = 56 },
345 .p1 = { .min = 2, .max = 8 },
346 .p2 = { .dot_limit = 225000,
347 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
348};
349
273e27ca 350/* LVDS 100mhz refclk limits. */
b91ad0ec 351static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
352 .dot = { .min = 25000, .max = 350000 },
353 .vco = { .min = 1760000, .max = 3510000 },
354 .n = { .min = 1, .max = 2 },
355 .m = { .min = 79, .max = 126 },
356 .m1 = { .min = 12, .max = 22 },
357 .m2 = { .min = 5, .max = 9 },
358 .p = { .min = 28, .max = 112 },
0206e353 359 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
360 .p2 = { .dot_limit = 225000,
361 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
362};
363
364static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
365 .dot = { .min = 25000, .max = 350000 },
366 .vco = { .min = 1760000, .max = 3510000 },
367 .n = { .min = 1, .max = 3 },
368 .m = { .min = 79, .max = 126 },
369 .m1 = { .min = 12, .max = 22 },
370 .m2 = { .min = 5, .max = 9 },
371 .p = { .min = 14, .max = 42 },
0206e353 372 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
373 .p2 = { .dot_limit = 225000,
374 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
375};
376
dc730512 377static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
378 /*
379 * These are the data rate limits (measured in fast clocks)
380 * since those are the strictest limits we have. The fast
381 * clock and actual rate limits are more relaxed, so checking
382 * them would make no difference.
383 */
384 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 385 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 386 .n = { .min = 1, .max = 7 },
a0c4da24
JB
387 .m1 = { .min = 2, .max = 3 },
388 .m2 = { .min = 11, .max = 156 },
b99ab663 389 .p1 = { .min = 2, .max = 3 },
5fdc9c49 390 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
391};
392
ef9348c8
CML
393static const intel_limit_t intel_limits_chv = {
394 /*
395 * These are the data rate limits (measured in fast clocks)
396 * since those are the strictest limits we have. The fast
397 * clock and actual rate limits are more relaxed, so checking
398 * them would make no difference.
399 */
400 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 401 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
402 .n = { .min = 1, .max = 1 },
403 .m1 = { .min = 2, .max = 2 },
404 .m2 = { .min = 24 << 22, .max = 175 << 22 },
405 .p1 = { .min = 2, .max = 4 },
406 .p2 = { .p2_slow = 1, .p2_fast = 14 },
407};
408
5ab7b0b7
ID
409static const intel_limit_t intel_limits_bxt = {
410 /* FIXME: find real dot limits */
411 .dot = { .min = 0, .max = INT_MAX },
412 .vco = { .min = 4800000, .max = 6480000 },
413 .n = { .min = 1, .max = 1 },
414 .m1 = { .min = 2, .max = 2 },
415 /* FIXME: find real m2 limits */
416 .m2 = { .min = 2 << 22, .max = 255 << 22 },
417 .p1 = { .min = 2, .max = 4 },
418 .p2 = { .p2_slow = 1, .p2_fast = 20 },
419};
420
6b4bf1c4
VS
421static void vlv_clock(int refclk, intel_clock_t *clock)
422{
423 clock->m = clock->m1 * clock->m2;
424 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
425 if (WARN_ON(clock->n == 0 || clock->p == 0))
426 return;
fb03ac01
VS
427 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
428 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
429}
430
cdba954e
ACO
431static bool
432needs_modeset(struct drm_crtc_state *state)
433{
434 return state->mode_changed || state->active_changed;
435}
436
e0638cdf
PZ
437/**
438 * Returns whether any output on the specified pipe is of the specified type
439 */
4093561b 440bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 441{
409ee761 442 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
443 struct intel_encoder *encoder;
444
409ee761 445 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
446 if (encoder->type == type)
447 return true;
448
449 return false;
450}
451
d0737e1d
ACO
452/**
453 * Returns whether any output on the specified pipe will have the specified
454 * type after a staged modeset is complete, i.e., the same as
455 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
456 * encoder->crtc.
457 */
a93e255f
ACO
458static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
459 int type)
d0737e1d 460{
a93e255f 461 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 462 struct drm_connector *connector;
a93e255f 463 struct drm_connector_state *connector_state;
d0737e1d 464 struct intel_encoder *encoder;
a93e255f
ACO
465 int i, num_connectors = 0;
466
da3ced29 467 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
468 if (connector_state->crtc != crtc_state->base.crtc)
469 continue;
470
471 num_connectors++;
d0737e1d 472
a93e255f
ACO
473 encoder = to_intel_encoder(connector_state->best_encoder);
474 if (encoder->type == type)
d0737e1d 475 return true;
a93e255f
ACO
476 }
477
478 WARN_ON(num_connectors == 0);
d0737e1d
ACO
479
480 return false;
481}
482
a93e255f
ACO
483static const intel_limit_t *
484intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
2c07245f 485{
a93e255f 486 struct drm_device *dev = crtc_state->base.crtc->dev;
2c07245f 487 const intel_limit_t *limit;
b91ad0ec 488
a93e255f 489 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 490 if (intel_is_dual_link_lvds(dev)) {
1b894b59 491 if (refclk == 100000)
b91ad0ec
ZW
492 limit = &intel_limits_ironlake_dual_lvds_100m;
493 else
494 limit = &intel_limits_ironlake_dual_lvds;
495 } else {
1b894b59 496 if (refclk == 100000)
b91ad0ec
ZW
497 limit = &intel_limits_ironlake_single_lvds_100m;
498 else
499 limit = &intel_limits_ironlake_single_lvds;
500 }
c6bb3538 501 } else
b91ad0ec 502 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
503
504 return limit;
505}
506
a93e255f
ACO
507static const intel_limit_t *
508intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 509{
a93e255f 510 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
511 const intel_limit_t *limit;
512
a93e255f 513 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 514 if (intel_is_dual_link_lvds(dev))
e4b36699 515 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 516 else
e4b36699 517 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
518 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
519 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 520 limit = &intel_limits_g4x_hdmi;
a93e255f 521 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 522 limit = &intel_limits_g4x_sdvo;
044c7c41 523 } else /* The option is for other outputs */
e4b36699 524 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
525
526 return limit;
527}
528
a93e255f
ACO
529static const intel_limit_t *
530intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 531{
a93e255f 532 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
533 const intel_limit_t *limit;
534
5ab7b0b7
ID
535 if (IS_BROXTON(dev))
536 limit = &intel_limits_bxt;
537 else if (HAS_PCH_SPLIT(dev))
a93e255f 538 limit = intel_ironlake_limit(crtc_state, refclk);
2c07245f 539 else if (IS_G4X(dev)) {
a93e255f 540 limit = intel_g4x_limit(crtc_state);
f2b115e6 541 } else if (IS_PINEVIEW(dev)) {
a93e255f 542 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 543 limit = &intel_limits_pineview_lvds;
2177832f 544 else
f2b115e6 545 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
546 } else if (IS_CHERRYVIEW(dev)) {
547 limit = &intel_limits_chv;
a0c4da24 548 } else if (IS_VALLEYVIEW(dev)) {
dc730512 549 limit = &intel_limits_vlv;
a6c45cf0 550 } else if (!IS_GEN2(dev)) {
a93e255f 551 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
552 limit = &intel_limits_i9xx_lvds;
553 else
554 limit = &intel_limits_i9xx_sdvo;
79e53945 555 } else {
a93e255f 556 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 557 limit = &intel_limits_i8xx_lvds;
a93e255f 558 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 559 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
560 else
561 limit = &intel_limits_i8xx_dac;
79e53945
JB
562 }
563 return limit;
564}
565
f2b115e6
AJ
566/* m1 is reserved as 0 in Pineview, n is a ring counter */
567static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 568{
2177832f
SL
569 clock->m = clock->m2 + 2;
570 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
571 if (WARN_ON(clock->n == 0 || clock->p == 0))
572 return;
fb03ac01
VS
573 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
574 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
575}
576
7429e9d4
DV
577static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
578{
579 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
580}
581
ac58c3f0 582static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 583{
7429e9d4 584 clock->m = i9xx_dpll_compute_m(clock);
79e53945 585 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
586 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
587 return;
fb03ac01
VS
588 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
589 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
590}
591
ef9348c8
CML
592static void chv_clock(int refclk, intel_clock_t *clock)
593{
594 clock->m = clock->m1 * clock->m2;
595 clock->p = clock->p1 * clock->p2;
596 if (WARN_ON(clock->n == 0 || clock->p == 0))
597 return;
598 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
599 clock->n << 22);
600 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
601}
602
7c04d1d9 603#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
604/**
605 * Returns whether the given set of divisors are valid for a given refclk with
606 * the given connectors.
607 */
608
1b894b59
CW
609static bool intel_PLL_is_valid(struct drm_device *dev,
610 const intel_limit_t *limit,
611 const intel_clock_t *clock)
79e53945 612{
f01b7962
VS
613 if (clock->n < limit->n.min || limit->n.max < clock->n)
614 INTELPllInvalid("n out of range\n");
79e53945 615 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 616 INTELPllInvalid("p1 out of range\n");
79e53945 617 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 618 INTELPllInvalid("m2 out of range\n");
79e53945 619 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 620 INTELPllInvalid("m1 out of range\n");
f01b7962 621
5ab7b0b7 622 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
623 if (clock->m1 <= clock->m2)
624 INTELPllInvalid("m1 <= m2\n");
625
5ab7b0b7 626 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
627 if (clock->p < limit->p.min || limit->p.max < clock->p)
628 INTELPllInvalid("p out of range\n");
629 if (clock->m < limit->m.min || limit->m.max < clock->m)
630 INTELPllInvalid("m out of range\n");
631 }
632
79e53945 633 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 634 INTELPllInvalid("vco out of range\n");
79e53945
JB
635 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
636 * connector, etc., rather than just a single range.
637 */
638 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 639 INTELPllInvalid("dot out of range\n");
79e53945
JB
640
641 return true;
642}
643
3b1429d9
VS
644static int
645i9xx_select_p2_div(const intel_limit_t *limit,
646 const struct intel_crtc_state *crtc_state,
647 int target)
79e53945 648{
3b1429d9 649 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 650
a93e255f 651 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 652 /*
a210b028
DV
653 * For LVDS just rely on its current settings for dual-channel.
654 * We haven't figured out how to reliably set up different
655 * single/dual channel state, if we even can.
79e53945 656 */
1974cad0 657 if (intel_is_dual_link_lvds(dev))
3b1429d9 658 return limit->p2.p2_fast;
79e53945 659 else
3b1429d9 660 return limit->p2.p2_slow;
79e53945
JB
661 } else {
662 if (target < limit->p2.dot_limit)
3b1429d9 663 return limit->p2.p2_slow;
79e53945 664 else
3b1429d9 665 return limit->p2.p2_fast;
79e53945 666 }
3b1429d9
VS
667}
668
669static bool
670i9xx_find_best_dpll(const intel_limit_t *limit,
671 struct intel_crtc_state *crtc_state,
672 int target, int refclk, intel_clock_t *match_clock,
673 intel_clock_t *best_clock)
674{
675 struct drm_device *dev = crtc_state->base.crtc->dev;
676 intel_clock_t clock;
677 int err = target;
79e53945 678
0206e353 679 memset(best_clock, 0, sizeof(*best_clock));
79e53945 680
3b1429d9
VS
681 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
682
42158660
ZY
683 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
684 clock.m1++) {
685 for (clock.m2 = limit->m2.min;
686 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 687 if (clock.m2 >= clock.m1)
42158660
ZY
688 break;
689 for (clock.n = limit->n.min;
690 clock.n <= limit->n.max; clock.n++) {
691 for (clock.p1 = limit->p1.min;
692 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
693 int this_err;
694
ac58c3f0
DV
695 i9xx_clock(refclk, &clock);
696 if (!intel_PLL_is_valid(dev, limit,
697 &clock))
698 continue;
699 if (match_clock &&
700 clock.p != match_clock->p)
701 continue;
702
703 this_err = abs(clock.dot - target);
704 if (this_err < err) {
705 *best_clock = clock;
706 err = this_err;
707 }
708 }
709 }
710 }
711 }
712
713 return (err != target);
714}
715
716static bool
a93e255f
ACO
717pnv_find_best_dpll(const intel_limit_t *limit,
718 struct intel_crtc_state *crtc_state,
ee9300bb
DV
719 int target, int refclk, intel_clock_t *match_clock,
720 intel_clock_t *best_clock)
79e53945 721{
3b1429d9 722 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945 723 intel_clock_t clock;
79e53945
JB
724 int err = target;
725
0206e353 726 memset(best_clock, 0, sizeof(*best_clock));
79e53945 727
3b1429d9
VS
728 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
729
42158660
ZY
730 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
731 clock.m1++) {
732 for (clock.m2 = limit->m2.min;
733 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
734 for (clock.n = limit->n.min;
735 clock.n <= limit->n.max; clock.n++) {
736 for (clock.p1 = limit->p1.min;
737 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
738 int this_err;
739
ac58c3f0 740 pineview_clock(refclk, &clock);
1b894b59
CW
741 if (!intel_PLL_is_valid(dev, limit,
742 &clock))
79e53945 743 continue;
cec2f356
SP
744 if (match_clock &&
745 clock.p != match_clock->p)
746 continue;
79e53945
JB
747
748 this_err = abs(clock.dot - target);
749 if (this_err < err) {
750 *best_clock = clock;
751 err = this_err;
752 }
753 }
754 }
755 }
756 }
757
758 return (err != target);
759}
760
d4906093 761static bool
a93e255f
ACO
762g4x_find_best_dpll(const intel_limit_t *limit,
763 struct intel_crtc_state *crtc_state,
ee9300bb
DV
764 int target, int refclk, intel_clock_t *match_clock,
765 intel_clock_t *best_clock)
d4906093 766{
3b1429d9 767 struct drm_device *dev = crtc_state->base.crtc->dev;
d4906093
ML
768 intel_clock_t clock;
769 int max_n;
3b1429d9 770 bool found = false;
6ba770dc
AJ
771 /* approximately equals target * 0.00585 */
772 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
773
774 memset(best_clock, 0, sizeof(*best_clock));
3b1429d9
VS
775
776 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
777
d4906093 778 max_n = limit->n.max;
f77f13e2 779 /* based on hardware requirement, prefer smaller n to precision */
d4906093 780 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 781 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
782 for (clock.m1 = limit->m1.max;
783 clock.m1 >= limit->m1.min; clock.m1--) {
784 for (clock.m2 = limit->m2.max;
785 clock.m2 >= limit->m2.min; clock.m2--) {
786 for (clock.p1 = limit->p1.max;
787 clock.p1 >= limit->p1.min; clock.p1--) {
788 int this_err;
789
ac58c3f0 790 i9xx_clock(refclk, &clock);
1b894b59
CW
791 if (!intel_PLL_is_valid(dev, limit,
792 &clock))
d4906093 793 continue;
1b894b59
CW
794
795 this_err = abs(clock.dot - target);
d4906093
ML
796 if (this_err < err_most) {
797 *best_clock = clock;
798 err_most = this_err;
799 max_n = clock.n;
800 found = true;
801 }
802 }
803 }
804 }
805 }
2c07245f
ZW
806 return found;
807}
808
d5dd62bd
ID
809/*
810 * Check if the calculated PLL configuration is more optimal compared to the
811 * best configuration and error found so far. Return the calculated error.
812 */
813static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
814 const intel_clock_t *calculated_clock,
815 const intel_clock_t *best_clock,
816 unsigned int best_error_ppm,
817 unsigned int *error_ppm)
818{
9ca3ba01
ID
819 /*
820 * For CHV ignore the error and consider only the P value.
821 * Prefer a bigger P value based on HW requirements.
822 */
823 if (IS_CHERRYVIEW(dev)) {
824 *error_ppm = 0;
825
826 return calculated_clock->p > best_clock->p;
827 }
828
24be4e46
ID
829 if (WARN_ON_ONCE(!target_freq))
830 return false;
831
d5dd62bd
ID
832 *error_ppm = div_u64(1000000ULL *
833 abs(target_freq - calculated_clock->dot),
834 target_freq);
835 /*
836 * Prefer a better P value over a better (smaller) error if the error
837 * is small. Ensure this preference for future configurations too by
838 * setting the error to 0.
839 */
840 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
841 *error_ppm = 0;
842
843 return true;
844 }
845
846 return *error_ppm + 10 < best_error_ppm;
847}
848
a0c4da24 849static bool
a93e255f
ACO
850vlv_find_best_dpll(const intel_limit_t *limit,
851 struct intel_crtc_state *crtc_state,
ee9300bb
DV
852 int target, int refclk, intel_clock_t *match_clock,
853 intel_clock_t *best_clock)
a0c4da24 854{
a93e255f 855 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 856 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 857 intel_clock_t clock;
69e4f900 858 unsigned int bestppm = 1000000;
27e639bf
VS
859 /* min update 19.2 MHz */
860 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 861 bool found = false;
a0c4da24 862
6b4bf1c4
VS
863 target *= 5; /* fast clock */
864
865 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
866
867 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 868 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 869 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 870 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 871 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 872 clock.p = clock.p1 * clock.p2;
a0c4da24 873 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 874 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 875 unsigned int ppm;
69e4f900 876
6b4bf1c4
VS
877 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
878 refclk * clock.m1);
879
880 vlv_clock(refclk, &clock);
43b0ac53 881
f01b7962
VS
882 if (!intel_PLL_is_valid(dev, limit,
883 &clock))
43b0ac53
VS
884 continue;
885
d5dd62bd
ID
886 if (!vlv_PLL_is_optimal(dev, target,
887 &clock,
888 best_clock,
889 bestppm, &ppm))
890 continue;
6b4bf1c4 891
d5dd62bd
ID
892 *best_clock = clock;
893 bestppm = ppm;
894 found = true;
a0c4da24
JB
895 }
896 }
897 }
898 }
a0c4da24 899
49e497ef 900 return found;
a0c4da24 901}
a4fc5ed6 902
ef9348c8 903static bool
a93e255f
ACO
904chv_find_best_dpll(const intel_limit_t *limit,
905 struct intel_crtc_state *crtc_state,
ef9348c8
CML
906 int target, int refclk, intel_clock_t *match_clock,
907 intel_clock_t *best_clock)
908{
a93e255f 909 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 910 struct drm_device *dev = crtc->base.dev;
9ca3ba01 911 unsigned int best_error_ppm;
ef9348c8
CML
912 intel_clock_t clock;
913 uint64_t m2;
914 int found = false;
915
916 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 917 best_error_ppm = 1000000;
ef9348c8
CML
918
919 /*
920 * Based on hardware doc, the n always set to 1, and m1 always
921 * set to 2. If requires to support 200Mhz refclk, we need to
922 * revisit this because n may not 1 anymore.
923 */
924 clock.n = 1, clock.m1 = 2;
925 target *= 5; /* fast clock */
926
927 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
928 for (clock.p2 = limit->p2.p2_fast;
929 clock.p2 >= limit->p2.p2_slow;
930 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 931 unsigned int error_ppm;
ef9348c8
CML
932
933 clock.p = clock.p1 * clock.p2;
934
935 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
936 clock.n) << 22, refclk * clock.m1);
937
938 if (m2 > INT_MAX/clock.m1)
939 continue;
940
941 clock.m2 = m2;
942
943 chv_clock(refclk, &clock);
944
945 if (!intel_PLL_is_valid(dev, limit, &clock))
946 continue;
947
9ca3ba01
ID
948 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
949 best_error_ppm, &error_ppm))
950 continue;
951
952 *best_clock = clock;
953 best_error_ppm = error_ppm;
954 found = true;
ef9348c8
CML
955 }
956 }
957
958 return found;
959}
960
5ab7b0b7
ID
961bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
962 intel_clock_t *best_clock)
963{
964 int refclk = i9xx_get_refclk(crtc_state, 0);
965
966 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
967 target_clock, refclk, NULL, best_clock);
968}
969
20ddf665
VS
970bool intel_crtc_active(struct drm_crtc *crtc)
971{
972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
973
974 /* Be paranoid as we can arrive here with only partial
975 * state retrieved from the hardware during setup.
976 *
241bfc38 977 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
978 * as Haswell has gained clock readout/fastboot support.
979 *
66e514c1 980 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 981 * properly reconstruct framebuffers.
c3d1f436
MR
982 *
983 * FIXME: The intel_crtc->active here should be switched to
984 * crtc->state->active once we have proper CRTC states wired up
985 * for atomic.
20ddf665 986 */
c3d1f436 987 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 988 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
989}
990
a5c961d1
PZ
991enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
992 enum pipe pipe)
993{
994 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
996
6e3c9717 997 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
998}
999
fbf49ea2
VS
1000static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1001{
1002 struct drm_i915_private *dev_priv = dev->dev_private;
1003 u32 reg = PIPEDSL(pipe);
1004 u32 line1, line2;
1005 u32 line_mask;
1006
1007 if (IS_GEN2(dev))
1008 line_mask = DSL_LINEMASK_GEN2;
1009 else
1010 line_mask = DSL_LINEMASK_GEN3;
1011
1012 line1 = I915_READ(reg) & line_mask;
1013 mdelay(5);
1014 line2 = I915_READ(reg) & line_mask;
1015
1016 return line1 == line2;
1017}
1018
ab7ad7f6
KP
1019/*
1020 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1021 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1022 *
1023 * After disabling a pipe, we can't wait for vblank in the usual way,
1024 * spinning on the vblank interrupt status bit, since we won't actually
1025 * see an interrupt when the pipe is disabled.
1026 *
ab7ad7f6
KP
1027 * On Gen4 and above:
1028 * wait for the pipe register state bit to turn off
1029 *
1030 * Otherwise:
1031 * wait for the display line value to settle (it usually
1032 * ends up stopping at the start of the next frame).
58e10eb9 1033 *
9d0498a2 1034 */
575f7ab7 1035static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1036{
575f7ab7 1037 struct drm_device *dev = crtc->base.dev;
9d0498a2 1038 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1039 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1040 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1041
1042 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 1043 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1044
1045 /* Wait for the Pipe State to go off */
58e10eb9
CW
1046 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1047 100))
284637d9 1048 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1049 } else {
ab7ad7f6 1050 /* Wait for the display line to settle */
fbf49ea2 1051 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1052 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1053 }
79e53945
JB
1054}
1055
b0ea7d37
DL
1056/*
1057 * ibx_digital_port_connected - is the specified port connected?
1058 * @dev_priv: i915 private structure
1059 * @port: the port to test
1060 *
1061 * Returns true if @port is connected, false otherwise.
1062 */
1063bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1064 struct intel_digital_port *port)
1065{
1066 u32 bit;
1067
c36346e3 1068 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1069 switch (port->port) {
c36346e3
DL
1070 case PORT_B:
1071 bit = SDE_PORTB_HOTPLUG;
1072 break;
1073 case PORT_C:
1074 bit = SDE_PORTC_HOTPLUG;
1075 break;
1076 case PORT_D:
1077 bit = SDE_PORTD_HOTPLUG;
1078 break;
1079 default:
1080 return true;
1081 }
1082 } else {
eba905b2 1083 switch (port->port) {
c36346e3
DL
1084 case PORT_B:
1085 bit = SDE_PORTB_HOTPLUG_CPT;
1086 break;
1087 case PORT_C:
1088 bit = SDE_PORTC_HOTPLUG_CPT;
1089 break;
1090 case PORT_D:
1091 bit = SDE_PORTD_HOTPLUG_CPT;
1092 break;
1093 default:
1094 return true;
1095 }
b0ea7d37
DL
1096 }
1097
1098 return I915_READ(SDEISR) & bit;
1099}
1100
b24e7179
JB
1101static const char *state_string(bool enabled)
1102{
1103 return enabled ? "on" : "off";
1104}
1105
1106/* Only for pre-ILK configs */
55607e8a
DV
1107void assert_pll(struct drm_i915_private *dev_priv,
1108 enum pipe pipe, bool state)
b24e7179
JB
1109{
1110 int reg;
1111 u32 val;
1112 bool cur_state;
1113
1114 reg = DPLL(pipe);
1115 val = I915_READ(reg);
1116 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1117 I915_STATE_WARN(cur_state != state,
b24e7179
JB
1118 "PLL state assertion failure (expected %s, current %s)\n",
1119 state_string(state), state_string(cur_state));
1120}
b24e7179 1121
23538ef1
JN
1122/* XXX: the dsi pll is shared between MIPI DSI ports */
1123static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1124{
1125 u32 val;
1126 bool cur_state;
1127
a580516d 1128 mutex_lock(&dev_priv->sb_lock);
23538ef1 1129 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
a580516d 1130 mutex_unlock(&dev_priv->sb_lock);
23538ef1
JN
1131
1132 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1133 I915_STATE_WARN(cur_state != state,
23538ef1
JN
1134 "DSI PLL state assertion failure (expected %s, current %s)\n",
1135 state_string(state), state_string(cur_state));
1136}
1137#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1138#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1139
55607e8a 1140struct intel_shared_dpll *
e2b78267
DV
1141intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1142{
1143 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1144
6e3c9717 1145 if (crtc->config->shared_dpll < 0)
e2b78267
DV
1146 return NULL;
1147
6e3c9717 1148 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
e2b78267
DV
1149}
1150
040484af 1151/* For ILK+ */
55607e8a
DV
1152void assert_shared_dpll(struct drm_i915_private *dev_priv,
1153 struct intel_shared_dpll *pll,
1154 bool state)
040484af 1155{
040484af 1156 bool cur_state;
5358901f 1157 struct intel_dpll_hw_state hw_state;
040484af 1158
92b27b08 1159 if (WARN (!pll,
46edb027 1160 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1161 return;
ee7b9f93 1162
5358901f 1163 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
e2c719b7 1164 I915_STATE_WARN(cur_state != state,
5358901f
DV
1165 "%s assertion failure (expected %s, current %s)\n",
1166 pll->name, state_string(state), state_string(cur_state));
040484af 1167}
040484af
JB
1168
1169static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1170 enum pipe pipe, bool state)
1171{
1172 int reg;
1173 u32 val;
1174 bool cur_state;
ad80a810
PZ
1175 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1176 pipe);
040484af 1177
affa9354
PZ
1178 if (HAS_DDI(dev_priv->dev)) {
1179 /* DDI does not have a specific FDI_TX register */
ad80a810 1180 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1181 val = I915_READ(reg);
ad80a810 1182 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1183 } else {
1184 reg = FDI_TX_CTL(pipe);
1185 val = I915_READ(reg);
1186 cur_state = !!(val & FDI_TX_ENABLE);
1187 }
e2c719b7 1188 I915_STATE_WARN(cur_state != state,
040484af
JB
1189 "FDI TX state assertion failure (expected %s, current %s)\n",
1190 state_string(state), state_string(cur_state));
1191}
1192#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1193#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1194
1195static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1196 enum pipe pipe, bool state)
1197{
1198 int reg;
1199 u32 val;
1200 bool cur_state;
1201
d63fa0dc
PZ
1202 reg = FDI_RX_CTL(pipe);
1203 val = I915_READ(reg);
1204 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1205 I915_STATE_WARN(cur_state != state,
040484af
JB
1206 "FDI RX state assertion failure (expected %s, current %s)\n",
1207 state_string(state), state_string(cur_state));
1208}
1209#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1210#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1211
1212static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1213 enum pipe pipe)
1214{
1215 int reg;
1216 u32 val;
1217
1218 /* ILK FDI PLL is always enabled */
3d13ef2e 1219 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1220 return;
1221
bf507ef7 1222 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1223 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1224 return;
1225
040484af
JB
1226 reg = FDI_TX_CTL(pipe);
1227 val = I915_READ(reg);
e2c719b7 1228 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1229}
1230
55607e8a
DV
1231void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1232 enum pipe pipe, bool state)
040484af
JB
1233{
1234 int reg;
1235 u32 val;
55607e8a 1236 bool cur_state;
040484af
JB
1237
1238 reg = FDI_RX_CTL(pipe);
1239 val = I915_READ(reg);
55607e8a 1240 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1241 I915_STATE_WARN(cur_state != state,
55607e8a
DV
1242 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1243 state_string(state), state_string(cur_state));
040484af
JB
1244}
1245
b680c37a
DV
1246void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1247 enum pipe pipe)
ea0760cf 1248{
bedd4dba
JN
1249 struct drm_device *dev = dev_priv->dev;
1250 int pp_reg;
ea0760cf
JB
1251 u32 val;
1252 enum pipe panel_pipe = PIPE_A;
0de3b485 1253 bool locked = true;
ea0760cf 1254
bedd4dba
JN
1255 if (WARN_ON(HAS_DDI(dev)))
1256 return;
1257
1258 if (HAS_PCH_SPLIT(dev)) {
1259 u32 port_sel;
1260
ea0760cf 1261 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1262 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1263
1264 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1265 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1266 panel_pipe = PIPE_B;
1267 /* XXX: else fix for eDP */
1268 } else if (IS_VALLEYVIEW(dev)) {
1269 /* presumably write lock depends on pipe, not port select */
1270 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1271 panel_pipe = pipe;
ea0760cf
JB
1272 } else {
1273 pp_reg = PP_CONTROL;
bedd4dba
JN
1274 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1275 panel_pipe = PIPE_B;
ea0760cf
JB
1276 }
1277
1278 val = I915_READ(pp_reg);
1279 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1280 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1281 locked = false;
1282
e2c719b7 1283 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1284 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1285 pipe_name(pipe));
ea0760cf
JB
1286}
1287
93ce0ba6
JN
1288static void assert_cursor(struct drm_i915_private *dev_priv,
1289 enum pipe pipe, bool state)
1290{
1291 struct drm_device *dev = dev_priv->dev;
1292 bool cur_state;
1293
d9d82081 1294 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1295 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1296 else
5efb3e28 1297 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1298
e2c719b7 1299 I915_STATE_WARN(cur_state != state,
93ce0ba6
JN
1300 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1301 pipe_name(pipe), state_string(state), state_string(cur_state));
1302}
1303#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1304#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1305
b840d907
JB
1306void assert_pipe(struct drm_i915_private *dev_priv,
1307 enum pipe pipe, bool state)
b24e7179
JB
1308{
1309 int reg;
1310 u32 val;
63d7bbe9 1311 bool cur_state;
702e7a56
PZ
1312 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1313 pipe);
b24e7179 1314
b6b5d049
VS
1315 /* if we need the pipe quirk it must be always on */
1316 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1317 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1318 state = true;
1319
f458ebbc 1320 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1321 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1322 cur_state = false;
1323 } else {
1324 reg = PIPECONF(cpu_transcoder);
1325 val = I915_READ(reg);
1326 cur_state = !!(val & PIPECONF_ENABLE);
1327 }
1328
e2c719b7 1329 I915_STATE_WARN(cur_state != state,
63d7bbe9 1330 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1331 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1332}
1333
931872fc
CW
1334static void assert_plane(struct drm_i915_private *dev_priv,
1335 enum plane plane, bool state)
b24e7179
JB
1336{
1337 int reg;
1338 u32 val;
931872fc 1339 bool cur_state;
b24e7179
JB
1340
1341 reg = DSPCNTR(plane);
1342 val = I915_READ(reg);
931872fc 1343 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1344 I915_STATE_WARN(cur_state != state,
931872fc
CW
1345 "plane %c assertion failure (expected %s, current %s)\n",
1346 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1347}
1348
931872fc
CW
1349#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1350#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1351
b24e7179
JB
1352static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1353 enum pipe pipe)
1354{
653e1026 1355 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1356 int reg, i;
1357 u32 val;
1358 int cur_pipe;
1359
653e1026
VS
1360 /* Primary planes are fixed to pipes on gen4+ */
1361 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1362 reg = DSPCNTR(pipe);
1363 val = I915_READ(reg);
e2c719b7 1364 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1365 "plane %c assertion failure, should be disabled but not\n",
1366 plane_name(pipe));
19ec1358 1367 return;
28c05794 1368 }
19ec1358 1369
b24e7179 1370 /* Need to check both planes against the pipe */
055e393f 1371 for_each_pipe(dev_priv, i) {
b24e7179
JB
1372 reg = DSPCNTR(i);
1373 val = I915_READ(reg);
1374 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1375 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1376 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1377 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1378 plane_name(i), pipe_name(pipe));
b24e7179
JB
1379 }
1380}
1381
19332d7a
JB
1382static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1383 enum pipe pipe)
1384{
20674eef 1385 struct drm_device *dev = dev_priv->dev;
1fe47785 1386 int reg, sprite;
19332d7a
JB
1387 u32 val;
1388
7feb8b88 1389 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1390 for_each_sprite(dev_priv, pipe, sprite) {
7feb8b88 1391 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1392 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1393 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1394 sprite, pipe_name(pipe));
1395 }
1396 } else if (IS_VALLEYVIEW(dev)) {
3bdcfc0c 1397 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 1398 reg = SPCNTR(pipe, sprite);
20674eef 1399 val = I915_READ(reg);
e2c719b7 1400 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1401 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1402 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1403 }
1404 } else if (INTEL_INFO(dev)->gen >= 7) {
1405 reg = SPRCTL(pipe);
19332d7a 1406 val = I915_READ(reg);
e2c719b7 1407 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1408 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1409 plane_name(pipe), pipe_name(pipe));
1410 } else if (INTEL_INFO(dev)->gen >= 5) {
1411 reg = DVSCNTR(pipe);
19332d7a 1412 val = I915_READ(reg);
e2c719b7 1413 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1414 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1415 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1416 }
1417}
1418
08c71e5e
VS
1419static void assert_vblank_disabled(struct drm_crtc *crtc)
1420{
e2c719b7 1421 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1422 drm_crtc_vblank_put(crtc);
1423}
1424
89eff4be 1425static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1426{
1427 u32 val;
1428 bool enabled;
1429
e2c719b7 1430 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1431
92f2584a
JB
1432 val = I915_READ(PCH_DREF_CONTROL);
1433 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1434 DREF_SUPERSPREAD_SOURCE_MASK));
e2c719b7 1435 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
92f2584a
JB
1436}
1437
ab9412ba
DV
1438static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1439 enum pipe pipe)
92f2584a
JB
1440{
1441 int reg;
1442 u32 val;
1443 bool enabled;
1444
ab9412ba 1445 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1446 val = I915_READ(reg);
1447 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1448 I915_STATE_WARN(enabled,
9db4a9c7
JB
1449 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1450 pipe_name(pipe));
92f2584a
JB
1451}
1452
4e634389
KP
1453static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1454 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1455{
1456 if ((val & DP_PORT_EN) == 0)
1457 return false;
1458
1459 if (HAS_PCH_CPT(dev_priv->dev)) {
1460 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1461 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1462 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1463 return false;
44f37d1f
CML
1464 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1465 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1466 return false;
f0575e92
KP
1467 } else {
1468 if ((val & DP_PIPE_MASK) != (pipe << 30))
1469 return false;
1470 }
1471 return true;
1472}
1473
1519b995
KP
1474static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1475 enum pipe pipe, u32 val)
1476{
dc0fa718 1477 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1478 return false;
1479
1480 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1481 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1482 return false;
44f37d1f
CML
1483 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1484 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1485 return false;
1519b995 1486 } else {
dc0fa718 1487 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1488 return false;
1489 }
1490 return true;
1491}
1492
1493static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1494 enum pipe pipe, u32 val)
1495{
1496 if ((val & LVDS_PORT_EN) == 0)
1497 return false;
1498
1499 if (HAS_PCH_CPT(dev_priv->dev)) {
1500 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1501 return false;
1502 } else {
1503 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1504 return false;
1505 }
1506 return true;
1507}
1508
1509static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1510 enum pipe pipe, u32 val)
1511{
1512 if ((val & ADPA_DAC_ENABLE) == 0)
1513 return false;
1514 if (HAS_PCH_CPT(dev_priv->dev)) {
1515 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1516 return false;
1517 } else {
1518 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1519 return false;
1520 }
1521 return true;
1522}
1523
291906f1 1524static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1525 enum pipe pipe, int reg, u32 port_sel)
291906f1 1526{
47a05eca 1527 u32 val = I915_READ(reg);
e2c719b7 1528 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1529 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1530 reg, pipe_name(pipe));
de9a35ab 1531
e2c719b7 1532 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1533 && (val & DP_PIPEB_SELECT),
de9a35ab 1534 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1535}
1536
1537static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1538 enum pipe pipe, int reg)
1539{
47a05eca 1540 u32 val = I915_READ(reg);
e2c719b7 1541 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1542 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1543 reg, pipe_name(pipe));
de9a35ab 1544
e2c719b7 1545 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1546 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1547 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1548}
1549
1550static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1551 enum pipe pipe)
1552{
1553 int reg;
1554 u32 val;
291906f1 1555
f0575e92
KP
1556 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1557 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1558 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1559
1560 reg = PCH_ADPA;
1561 val = I915_READ(reg);
e2c719b7 1562 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1563 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1564 pipe_name(pipe));
291906f1
JB
1565
1566 reg = PCH_LVDS;
1567 val = I915_READ(reg);
e2c719b7 1568 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1569 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1570 pipe_name(pipe));
291906f1 1571
e2debe91
PZ
1572 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1573 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1574 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1575}
1576
40e9cf64
JB
1577static void intel_init_dpio(struct drm_device *dev)
1578{
1579 struct drm_i915_private *dev_priv = dev->dev_private;
1580
1581 if (!IS_VALLEYVIEW(dev))
1582 return;
1583
a09caddd
CML
1584 /*
1585 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1586 * CHV x1 PHY (DP/HDMI D)
1587 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1588 */
1589 if (IS_CHERRYVIEW(dev)) {
1590 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1591 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1592 } else {
1593 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1594 }
5382f5f3
JB
1595}
1596
d288f65f 1597static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1598 const struct intel_crtc_state *pipe_config)
87442f73 1599{
426115cf
DV
1600 struct drm_device *dev = crtc->base.dev;
1601 struct drm_i915_private *dev_priv = dev->dev_private;
1602 int reg = DPLL(crtc->pipe);
d288f65f 1603 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1604
426115cf 1605 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1606
1607 /* No really, not for ILK+ */
1608 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1609
1610 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1611 if (IS_MOBILE(dev_priv->dev))
426115cf 1612 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1613
426115cf
DV
1614 I915_WRITE(reg, dpll);
1615 POSTING_READ(reg);
1616 udelay(150);
1617
1618 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1619 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1620
d288f65f 1621 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1622 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1623
1624 /* We do this three times for luck */
426115cf 1625 I915_WRITE(reg, dpll);
87442f73
DV
1626 POSTING_READ(reg);
1627 udelay(150); /* wait for warmup */
426115cf 1628 I915_WRITE(reg, dpll);
87442f73
DV
1629 POSTING_READ(reg);
1630 udelay(150); /* wait for warmup */
426115cf 1631 I915_WRITE(reg, dpll);
87442f73
DV
1632 POSTING_READ(reg);
1633 udelay(150); /* wait for warmup */
1634}
1635
d288f65f 1636static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1637 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1638{
1639 struct drm_device *dev = crtc->base.dev;
1640 struct drm_i915_private *dev_priv = dev->dev_private;
1641 int pipe = crtc->pipe;
1642 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1643 u32 tmp;
1644
1645 assert_pipe_disabled(dev_priv, crtc->pipe);
1646
1647 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1648
a580516d 1649 mutex_lock(&dev_priv->sb_lock);
9d556c99
CML
1650
1651 /* Enable back the 10bit clock to display controller */
1652 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1653 tmp |= DPIO_DCLKP_EN;
1654 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1655
54433e91
VS
1656 mutex_unlock(&dev_priv->sb_lock);
1657
9d556c99
CML
1658 /*
1659 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1660 */
1661 udelay(1);
1662
1663 /* Enable PLL */
d288f65f 1664 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1665
1666 /* Check PLL is locked */
a11b0703 1667 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1668 DRM_ERROR("PLL %d failed to lock\n", pipe);
1669
a11b0703 1670 /* not sure when this should be written */
d288f65f 1671 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703 1672 POSTING_READ(DPLL_MD(pipe));
9d556c99
CML
1673}
1674
1c4e0274
VS
1675static int intel_num_dvo_pipes(struct drm_device *dev)
1676{
1677 struct intel_crtc *crtc;
1678 int count = 0;
1679
1680 for_each_intel_crtc(dev, crtc)
3538b9df 1681 count += crtc->base.state->active &&
409ee761 1682 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1683
1684 return count;
1685}
1686
66e3d5c0 1687static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1688{
66e3d5c0
DV
1689 struct drm_device *dev = crtc->base.dev;
1690 struct drm_i915_private *dev_priv = dev->dev_private;
1691 int reg = DPLL(crtc->pipe);
6e3c9717 1692 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1693
66e3d5c0 1694 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1695
63d7bbe9 1696 /* No really, not for ILK+ */
3d13ef2e 1697 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1698
1699 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1700 if (IS_MOBILE(dev) && !IS_I830(dev))
1701 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1702
1c4e0274
VS
1703 /* Enable DVO 2x clock on both PLLs if necessary */
1704 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1705 /*
1706 * It appears to be important that we don't enable this
1707 * for the current pipe before otherwise configuring the
1708 * PLL. No idea how this should be handled if multiple
1709 * DVO outputs are enabled simultaneosly.
1710 */
1711 dpll |= DPLL_DVO_2X_MODE;
1712 I915_WRITE(DPLL(!crtc->pipe),
1713 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1714 }
66e3d5c0
DV
1715
1716 /* Wait for the clocks to stabilize. */
1717 POSTING_READ(reg);
1718 udelay(150);
1719
1720 if (INTEL_INFO(dev)->gen >= 4) {
1721 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1722 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1723 } else {
1724 /* The pixel multiplier can only be updated once the
1725 * DPLL is enabled and the clocks are stable.
1726 *
1727 * So write it again.
1728 */
1729 I915_WRITE(reg, dpll);
1730 }
63d7bbe9
JB
1731
1732 /* We do this three times for luck */
66e3d5c0 1733 I915_WRITE(reg, dpll);
63d7bbe9
JB
1734 POSTING_READ(reg);
1735 udelay(150); /* wait for warmup */
66e3d5c0 1736 I915_WRITE(reg, dpll);
63d7bbe9
JB
1737 POSTING_READ(reg);
1738 udelay(150); /* wait for warmup */
66e3d5c0 1739 I915_WRITE(reg, dpll);
63d7bbe9
JB
1740 POSTING_READ(reg);
1741 udelay(150); /* wait for warmup */
1742}
1743
1744/**
50b44a44 1745 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1746 * @dev_priv: i915 private structure
1747 * @pipe: pipe PLL to disable
1748 *
1749 * Disable the PLL for @pipe, making sure the pipe is off first.
1750 *
1751 * Note! This is for pre-ILK only.
1752 */
1c4e0274 1753static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1754{
1c4e0274
VS
1755 struct drm_device *dev = crtc->base.dev;
1756 struct drm_i915_private *dev_priv = dev->dev_private;
1757 enum pipe pipe = crtc->pipe;
1758
1759 /* Disable DVO 2x clock on both PLLs if necessary */
1760 if (IS_I830(dev) &&
409ee761 1761 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
3538b9df 1762 !intel_num_dvo_pipes(dev)) {
1c4e0274
VS
1763 I915_WRITE(DPLL(PIPE_B),
1764 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1765 I915_WRITE(DPLL(PIPE_A),
1766 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1767 }
1768
b6b5d049
VS
1769 /* Don't disable pipe or pipe PLLs if needed */
1770 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1771 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1772 return;
1773
1774 /* Make sure the pipe isn't still relying on us */
1775 assert_pipe_disabled(dev_priv, pipe);
1776
50b44a44
DV
1777 I915_WRITE(DPLL(pipe), 0);
1778 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1779}
1780
f6071166
JB
1781static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1782{
1783 u32 val = 0;
1784
1785 /* Make sure the pipe isn't still relying on us */
1786 assert_pipe_disabled(dev_priv, pipe);
1787
e5cbfbfb
ID
1788 /*
1789 * Leave integrated clock source and reference clock enabled for pipe B.
1790 * The latter is needed for VGA hotplug / manual detection.
1791 */
f6071166 1792 if (pipe == PIPE_B)
e5cbfbfb 1793 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1794 I915_WRITE(DPLL(pipe), val);
1795 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1796
1797}
1798
1799static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1800{
d752048d 1801 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1802 u32 val;
1803
a11b0703
VS
1804 /* Make sure the pipe isn't still relying on us */
1805 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1806
a11b0703 1807 /* Set PLL en = 0 */
d17ec4ce 1808 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1809 if (pipe != PIPE_A)
1810 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1811 I915_WRITE(DPLL(pipe), val);
1812 POSTING_READ(DPLL(pipe));
d752048d 1813
a580516d 1814 mutex_lock(&dev_priv->sb_lock);
d752048d
VS
1815
1816 /* Disable 10bit clock to display controller */
1817 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1818 val &= ~DPIO_DCLKP_EN;
1819 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1820
61407f6d
VS
1821 /* disable left/right clock distribution */
1822 if (pipe != PIPE_B) {
1823 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1824 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1825 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1826 } else {
1827 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1828 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1829 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1830 }
1831
a580516d 1832 mutex_unlock(&dev_priv->sb_lock);
f6071166
JB
1833}
1834
e4607fcf 1835void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1836 struct intel_digital_port *dport,
1837 unsigned int expected_mask)
89b667f8
JB
1838{
1839 u32 port_mask;
00fc31b7 1840 int dpll_reg;
89b667f8 1841
e4607fcf
CML
1842 switch (dport->port) {
1843 case PORT_B:
89b667f8 1844 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1845 dpll_reg = DPLL(0);
e4607fcf
CML
1846 break;
1847 case PORT_C:
89b667f8 1848 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1849 dpll_reg = DPLL(0);
9b6de0a1 1850 expected_mask <<= 4;
00fc31b7
CML
1851 break;
1852 case PORT_D:
1853 port_mask = DPLL_PORTD_READY_MASK;
1854 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1855 break;
1856 default:
1857 BUG();
1858 }
89b667f8 1859
9b6de0a1
VS
1860 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1861 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1862 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1863}
1864
b14b1055
DV
1865static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1866{
1867 struct drm_device *dev = crtc->base.dev;
1868 struct drm_i915_private *dev_priv = dev->dev_private;
1869 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1870
be19f0ff
CW
1871 if (WARN_ON(pll == NULL))
1872 return;
1873
3e369b76 1874 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1875 if (pll->active == 0) {
1876 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1877 WARN_ON(pll->on);
1878 assert_shared_dpll_disabled(dev_priv, pll);
1879
1880 pll->mode_set(dev_priv, pll);
1881 }
1882}
1883
92f2584a 1884/**
85b3894f 1885 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1886 * @dev_priv: i915 private structure
1887 * @pipe: pipe PLL to enable
1888 *
1889 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1890 * drives the transcoder clock.
1891 */
85b3894f 1892static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1893{
3d13ef2e
DL
1894 struct drm_device *dev = crtc->base.dev;
1895 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1896 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1897
87a875bb 1898 if (WARN_ON(pll == NULL))
48da64a8
CW
1899 return;
1900
3e369b76 1901 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1902 return;
ee7b9f93 1903
74dd6928 1904 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1905 pll->name, pll->active, pll->on,
e2b78267 1906 crtc->base.base.id);
92f2584a 1907
cdbd2316
DV
1908 if (pll->active++) {
1909 WARN_ON(!pll->on);
e9d6944e 1910 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1911 return;
1912 }
f4a091c7 1913 WARN_ON(pll->on);
ee7b9f93 1914
bd2bb1b9
PZ
1915 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1916
46edb027 1917 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1918 pll->enable(dev_priv, pll);
ee7b9f93 1919 pll->on = true;
92f2584a
JB
1920}
1921
f6daaec2 1922static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1923{
3d13ef2e
DL
1924 struct drm_device *dev = crtc->base.dev;
1925 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1926 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1927
92f2584a 1928 /* PCH only available on ILK+ */
3d13ef2e 1929 BUG_ON(INTEL_INFO(dev)->gen < 5);
eddfcbcd
ML
1930 if (pll == NULL)
1931 return;
92f2584a 1932
eddfcbcd 1933 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
48da64a8 1934 return;
7a419866 1935
46edb027
DV
1936 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1937 pll->name, pll->active, pll->on,
e2b78267 1938 crtc->base.base.id);
7a419866 1939
48da64a8 1940 if (WARN_ON(pll->active == 0)) {
e9d6944e 1941 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1942 return;
1943 }
1944
e9d6944e 1945 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1946 WARN_ON(!pll->on);
cdbd2316 1947 if (--pll->active)
7a419866 1948 return;
ee7b9f93 1949
46edb027 1950 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1951 pll->disable(dev_priv, pll);
ee7b9f93 1952 pll->on = false;
bd2bb1b9
PZ
1953
1954 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1955}
1956
b8a4f404
PZ
1957static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1958 enum pipe pipe)
040484af 1959{
23670b32 1960 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1961 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1962 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1963 uint32_t reg, val, pipeconf_val;
040484af
JB
1964
1965 /* PCH only available on ILK+ */
55522f37 1966 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1967
1968 /* Make sure PCH DPLL is enabled */
e72f9fbf 1969 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1970 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1971
1972 /* FDI must be feeding us bits for PCH ports */
1973 assert_fdi_tx_enabled(dev_priv, pipe);
1974 assert_fdi_rx_enabled(dev_priv, pipe);
1975
23670b32
DV
1976 if (HAS_PCH_CPT(dev)) {
1977 /* Workaround: Set the timing override bit before enabling the
1978 * pch transcoder. */
1979 reg = TRANS_CHICKEN2(pipe);
1980 val = I915_READ(reg);
1981 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1982 I915_WRITE(reg, val);
59c859d6 1983 }
23670b32 1984
ab9412ba 1985 reg = PCH_TRANSCONF(pipe);
040484af 1986 val = I915_READ(reg);
5f7f726d 1987 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1988
1989 if (HAS_PCH_IBX(dev_priv->dev)) {
1990 /*
c5de7c6f
VS
1991 * Make the BPC in transcoder be consistent with
1992 * that in pipeconf reg. For HDMI we must use 8bpc
1993 * here for both 8bpc and 12bpc.
e9bcff5c 1994 */
dfd07d72 1995 val &= ~PIPECONF_BPC_MASK;
c5de7c6f
VS
1996 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
1997 val |= PIPECONF_8BPC;
1998 else
1999 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 2000 }
5f7f726d
PZ
2001
2002 val &= ~TRANS_INTERLACE_MASK;
2003 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 2004 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 2005 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
2006 val |= TRANS_LEGACY_INTERLACED_ILK;
2007 else
2008 val |= TRANS_INTERLACED;
5f7f726d
PZ
2009 else
2010 val |= TRANS_PROGRESSIVE;
2011
040484af
JB
2012 I915_WRITE(reg, val | TRANS_ENABLE);
2013 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 2014 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
2015}
2016
8fb033d7 2017static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 2018 enum transcoder cpu_transcoder)
040484af 2019{
8fb033d7 2020 u32 val, pipeconf_val;
8fb033d7
PZ
2021
2022 /* PCH only available on ILK+ */
55522f37 2023 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 2024
8fb033d7 2025 /* FDI must be feeding us bits for PCH ports */
1a240d4d 2026 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 2027 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 2028
223a6fdf
PZ
2029 /* Workaround: set timing override bit. */
2030 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2031 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
2032 I915_WRITE(_TRANSA_CHICKEN2, val);
2033
25f3ef11 2034 val = TRANS_ENABLE;
937bb610 2035 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 2036
9a76b1c6
PZ
2037 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2038 PIPECONF_INTERLACED_ILK)
a35f2679 2039 val |= TRANS_INTERLACED;
8fb033d7
PZ
2040 else
2041 val |= TRANS_PROGRESSIVE;
2042
ab9412ba
DV
2043 I915_WRITE(LPT_TRANSCONF, val);
2044 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 2045 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
2046}
2047
b8a4f404
PZ
2048static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2049 enum pipe pipe)
040484af 2050{
23670b32
DV
2051 struct drm_device *dev = dev_priv->dev;
2052 uint32_t reg, val;
040484af
JB
2053
2054 /* FDI relies on the transcoder */
2055 assert_fdi_tx_disabled(dev_priv, pipe);
2056 assert_fdi_rx_disabled(dev_priv, pipe);
2057
291906f1
JB
2058 /* Ports must be off as well */
2059 assert_pch_ports_disabled(dev_priv, pipe);
2060
ab9412ba 2061 reg = PCH_TRANSCONF(pipe);
040484af
JB
2062 val = I915_READ(reg);
2063 val &= ~TRANS_ENABLE;
2064 I915_WRITE(reg, val);
2065 /* wait for PCH transcoder off, transcoder state */
2066 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 2067 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
2068
2069 if (!HAS_PCH_IBX(dev)) {
2070 /* Workaround: Clear the timing override chicken bit again. */
2071 reg = TRANS_CHICKEN2(pipe);
2072 val = I915_READ(reg);
2073 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2074 I915_WRITE(reg, val);
2075 }
040484af
JB
2076}
2077
ab4d966c 2078static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 2079{
8fb033d7
PZ
2080 u32 val;
2081
ab9412ba 2082 val = I915_READ(LPT_TRANSCONF);
8fb033d7 2083 val &= ~TRANS_ENABLE;
ab9412ba 2084 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2085 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2086 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2087 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2088
2089 /* Workaround: clear timing override bit. */
2090 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2091 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2092 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2093}
2094
b24e7179 2095/**
309cfea8 2096 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2097 * @crtc: crtc responsible for the pipe
b24e7179 2098 *
0372264a 2099 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2100 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2101 */
e1fdc473 2102static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2103{
0372264a
PZ
2104 struct drm_device *dev = crtc->base.dev;
2105 struct drm_i915_private *dev_priv = dev->dev_private;
2106 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2107 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2108 pipe);
1a240d4d 2109 enum pipe pch_transcoder;
b24e7179
JB
2110 int reg;
2111 u32 val;
2112
58c6eaa2 2113 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2114 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2115 assert_sprites_disabled(dev_priv, pipe);
2116
681e5811 2117 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2118 pch_transcoder = TRANSCODER_A;
2119 else
2120 pch_transcoder = pipe;
2121
b24e7179
JB
2122 /*
2123 * A pipe without a PLL won't actually be able to drive bits from
2124 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2125 * need the check.
2126 */
50360403 2127 if (HAS_GMCH_DISPLAY(dev_priv->dev))
409ee761 2128 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2129 assert_dsi_pll_enabled(dev_priv);
2130 else
2131 assert_pll_enabled(dev_priv, pipe);
040484af 2132 else {
6e3c9717 2133 if (crtc->config->has_pch_encoder) {
040484af 2134 /* if driving the PCH, we need FDI enabled */
cc391bbb 2135 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2136 assert_fdi_tx_pll_enabled(dev_priv,
2137 (enum pipe) cpu_transcoder);
040484af
JB
2138 }
2139 /* FIXME: assert CPU port conditions for SNB+ */
2140 }
b24e7179 2141
702e7a56 2142 reg = PIPECONF(cpu_transcoder);
b24e7179 2143 val = I915_READ(reg);
7ad25d48 2144 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2145 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2146 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2147 return;
7ad25d48 2148 }
00d70b15
CW
2149
2150 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2151 POSTING_READ(reg);
b24e7179
JB
2152}
2153
2154/**
309cfea8 2155 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2156 * @crtc: crtc whose pipes is to be disabled
b24e7179 2157 *
575f7ab7
VS
2158 * Disable the pipe of @crtc, making sure that various hardware
2159 * specific requirements are met, if applicable, e.g. plane
2160 * disabled, panel fitter off, etc.
b24e7179
JB
2161 *
2162 * Will wait until the pipe has shut down before returning.
2163 */
575f7ab7 2164static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2165{
575f7ab7 2166 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2167 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2168 enum pipe pipe = crtc->pipe;
b24e7179
JB
2169 int reg;
2170 u32 val;
2171
2172 /*
2173 * Make sure planes won't keep trying to pump pixels to us,
2174 * or we might hang the display.
2175 */
2176 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2177 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2178 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2179
702e7a56 2180 reg = PIPECONF(cpu_transcoder);
b24e7179 2181 val = I915_READ(reg);
00d70b15
CW
2182 if ((val & PIPECONF_ENABLE) == 0)
2183 return;
2184
67adc644
VS
2185 /*
2186 * Double wide has implications for planes
2187 * so best keep it disabled when not needed.
2188 */
6e3c9717 2189 if (crtc->config->double_wide)
67adc644
VS
2190 val &= ~PIPECONF_DOUBLE_WIDE;
2191
2192 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2193 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2194 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2195 val &= ~PIPECONF_ENABLE;
2196
2197 I915_WRITE(reg, val);
2198 if ((val & PIPECONF_ENABLE) == 0)
2199 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2200}
2201
693db184
CW
2202static bool need_vtd_wa(struct drm_device *dev)
2203{
2204#ifdef CONFIG_INTEL_IOMMU
2205 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2206 return true;
2207#endif
2208 return false;
2209}
2210
50470bb0 2211unsigned int
6761dd31
TU
2212intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2213 uint64_t fb_format_modifier)
a57ce0b2 2214{
6761dd31
TU
2215 unsigned int tile_height;
2216 uint32_t pixel_bytes;
a57ce0b2 2217
b5d0e9bf
DL
2218 switch (fb_format_modifier) {
2219 case DRM_FORMAT_MOD_NONE:
2220 tile_height = 1;
2221 break;
2222 case I915_FORMAT_MOD_X_TILED:
2223 tile_height = IS_GEN2(dev) ? 16 : 8;
2224 break;
2225 case I915_FORMAT_MOD_Y_TILED:
2226 tile_height = 32;
2227 break;
2228 case I915_FORMAT_MOD_Yf_TILED:
6761dd31
TU
2229 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2230 switch (pixel_bytes) {
b5d0e9bf 2231 default:
6761dd31 2232 case 1:
b5d0e9bf
DL
2233 tile_height = 64;
2234 break;
6761dd31
TU
2235 case 2:
2236 case 4:
b5d0e9bf
DL
2237 tile_height = 32;
2238 break;
6761dd31 2239 case 8:
b5d0e9bf
DL
2240 tile_height = 16;
2241 break;
6761dd31 2242 case 16:
b5d0e9bf
DL
2243 WARN_ONCE(1,
2244 "128-bit pixels are not supported for display!");
2245 tile_height = 16;
2246 break;
2247 }
2248 break;
2249 default:
2250 MISSING_CASE(fb_format_modifier);
2251 tile_height = 1;
2252 break;
2253 }
091df6cb 2254
6761dd31
TU
2255 return tile_height;
2256}
2257
2258unsigned int
2259intel_fb_align_height(struct drm_device *dev, unsigned int height,
2260 uint32_t pixel_format, uint64_t fb_format_modifier)
2261{
2262 return ALIGN(height, intel_tile_height(dev, pixel_format,
2263 fb_format_modifier));
a57ce0b2
JB
2264}
2265
f64b98cd
TU
2266static int
2267intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2268 const struct drm_plane_state *plane_state)
2269{
50470bb0 2270 struct intel_rotation_info *info = &view->rotation_info;
50470bb0 2271
f64b98cd
TU
2272 *view = i915_ggtt_view_normal;
2273
50470bb0
TU
2274 if (!plane_state)
2275 return 0;
2276
121920fa 2277 if (!intel_rotation_90_or_270(plane_state->rotation))
50470bb0
TU
2278 return 0;
2279
9abc4648 2280 *view = i915_ggtt_view_rotated;
50470bb0
TU
2281
2282 info->height = fb->height;
2283 info->pixel_format = fb->pixel_format;
2284 info->pitch = fb->pitches[0];
2285 info->fb_modifier = fb->modifier[0];
2286
f64b98cd
TU
2287 return 0;
2288}
2289
4e9a86b6
VS
2290static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2291{
2292 if (INTEL_INFO(dev_priv)->gen >= 9)
2293 return 256 * 1024;
985b8bb4
VS
2294 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2295 IS_VALLEYVIEW(dev_priv))
4e9a86b6
VS
2296 return 128 * 1024;
2297 else if (INTEL_INFO(dev_priv)->gen >= 4)
2298 return 4 * 1024;
2299 else
44c5905e 2300 return 0;
4e9a86b6
VS
2301}
2302
127bd2ac 2303int
850c4cdc
TU
2304intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2305 struct drm_framebuffer *fb,
82bc3b2d 2306 const struct drm_plane_state *plane_state,
91af127f
JH
2307 struct intel_engine_cs *pipelined,
2308 struct drm_i915_gem_request **pipelined_request)
6b95a207 2309{
850c4cdc 2310 struct drm_device *dev = fb->dev;
ce453d81 2311 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2312 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2313 struct i915_ggtt_view view;
6b95a207
KH
2314 u32 alignment;
2315 int ret;
2316
ebcdd39e
MR
2317 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2318
7b911adc
TU
2319 switch (fb->modifier[0]) {
2320 case DRM_FORMAT_MOD_NONE:
4e9a86b6 2321 alignment = intel_linear_alignment(dev_priv);
6b95a207 2322 break;
7b911adc 2323 case I915_FORMAT_MOD_X_TILED:
1fada4cc
DL
2324 if (INTEL_INFO(dev)->gen >= 9)
2325 alignment = 256 * 1024;
2326 else {
2327 /* pin() will align the object as required by fence */
2328 alignment = 0;
2329 }
6b95a207 2330 break;
7b911adc 2331 case I915_FORMAT_MOD_Y_TILED:
1327b9a1
DL
2332 case I915_FORMAT_MOD_Yf_TILED:
2333 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2334 "Y tiling bo slipped through, driver bug!\n"))
2335 return -EINVAL;
2336 alignment = 1 * 1024 * 1024;
2337 break;
6b95a207 2338 default:
7b911adc
TU
2339 MISSING_CASE(fb->modifier[0]);
2340 return -EINVAL;
6b95a207
KH
2341 }
2342
f64b98cd
TU
2343 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2344 if (ret)
2345 return ret;
2346
693db184
CW
2347 /* Note that the w/a also requires 64 PTE of padding following the
2348 * bo. We currently fill all unused PTE with the shadow page and so
2349 * we should always have valid PTE following the scanout preventing
2350 * the VT-d warning.
2351 */
2352 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2353 alignment = 256 * 1024;
2354
d6dd6843
PZ
2355 /*
2356 * Global gtt pte registers are special registers which actually forward
2357 * writes to a chunk of system memory. Which means that there is no risk
2358 * that the register values disappear as soon as we call
2359 * intel_runtime_pm_put(), so it is correct to wrap only the
2360 * pin/unpin/fence and not more.
2361 */
2362 intel_runtime_pm_get(dev_priv);
2363
ce453d81 2364 dev_priv->mm.interruptible = false;
e6617330 2365 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
91af127f 2366 pipelined_request, &view);
48b956c5 2367 if (ret)
ce453d81 2368 goto err_interruptible;
6b95a207
KH
2369
2370 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2371 * fence, whereas 965+ only requires a fence if using
2372 * framebuffer compression. For simplicity, we always install
2373 * a fence as the cost is not that onerous.
2374 */
06d98131 2375 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2376 if (ret)
2377 goto err_unpin;
1690e1eb 2378
9a5a53b3 2379 i915_gem_object_pin_fence(obj);
6b95a207 2380
ce453d81 2381 dev_priv->mm.interruptible = true;
d6dd6843 2382 intel_runtime_pm_put(dev_priv);
6b95a207 2383 return 0;
48b956c5
CW
2384
2385err_unpin:
f64b98cd 2386 i915_gem_object_unpin_from_display_plane(obj, &view);
ce453d81
CW
2387err_interruptible:
2388 dev_priv->mm.interruptible = true;
d6dd6843 2389 intel_runtime_pm_put(dev_priv);
48b956c5 2390 return ret;
6b95a207
KH
2391}
2392
82bc3b2d
TU
2393static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2394 const struct drm_plane_state *plane_state)
1690e1eb 2395{
82bc3b2d 2396 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd
TU
2397 struct i915_ggtt_view view;
2398 int ret;
82bc3b2d 2399
ebcdd39e
MR
2400 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2401
f64b98cd
TU
2402 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2403 WARN_ONCE(ret, "Couldn't get view from plane state!");
2404
1690e1eb 2405 i915_gem_object_unpin_fence(obj);
f64b98cd 2406 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2407}
2408
c2c75131
DV
2409/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2410 * is assumed to be a power-of-two. */
4e9a86b6
VS
2411unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2412 int *x, int *y,
bc752862
CW
2413 unsigned int tiling_mode,
2414 unsigned int cpp,
2415 unsigned int pitch)
c2c75131 2416{
bc752862
CW
2417 if (tiling_mode != I915_TILING_NONE) {
2418 unsigned int tile_rows, tiles;
c2c75131 2419
bc752862
CW
2420 tile_rows = *y / 8;
2421 *y %= 8;
c2c75131 2422
bc752862
CW
2423 tiles = *x / (512/cpp);
2424 *x %= 512/cpp;
2425
2426 return tile_rows * pitch * 8 + tiles * 4096;
2427 } else {
4e9a86b6 2428 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
bc752862
CW
2429 unsigned int offset;
2430
2431 offset = *y * pitch + *x * cpp;
4e9a86b6
VS
2432 *y = (offset & alignment) / pitch;
2433 *x = ((offset & alignment) - *y * pitch) / cpp;
2434 return offset & ~alignment;
bc752862 2435 }
c2c75131
DV
2436}
2437
b35d63fa 2438static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2439{
2440 switch (format) {
2441 case DISPPLANE_8BPP:
2442 return DRM_FORMAT_C8;
2443 case DISPPLANE_BGRX555:
2444 return DRM_FORMAT_XRGB1555;
2445 case DISPPLANE_BGRX565:
2446 return DRM_FORMAT_RGB565;
2447 default:
2448 case DISPPLANE_BGRX888:
2449 return DRM_FORMAT_XRGB8888;
2450 case DISPPLANE_RGBX888:
2451 return DRM_FORMAT_XBGR8888;
2452 case DISPPLANE_BGRX101010:
2453 return DRM_FORMAT_XRGB2101010;
2454 case DISPPLANE_RGBX101010:
2455 return DRM_FORMAT_XBGR2101010;
2456 }
2457}
2458
bc8d7dff
DL
2459static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2460{
2461 switch (format) {
2462 case PLANE_CTL_FORMAT_RGB_565:
2463 return DRM_FORMAT_RGB565;
2464 default:
2465 case PLANE_CTL_FORMAT_XRGB_8888:
2466 if (rgb_order) {
2467 if (alpha)
2468 return DRM_FORMAT_ABGR8888;
2469 else
2470 return DRM_FORMAT_XBGR8888;
2471 } else {
2472 if (alpha)
2473 return DRM_FORMAT_ARGB8888;
2474 else
2475 return DRM_FORMAT_XRGB8888;
2476 }
2477 case PLANE_CTL_FORMAT_XRGB_2101010:
2478 if (rgb_order)
2479 return DRM_FORMAT_XBGR2101010;
2480 else
2481 return DRM_FORMAT_XRGB2101010;
2482 }
2483}
2484
5724dbd1 2485static bool
f6936e29
DV
2486intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2487 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2488{
2489 struct drm_device *dev = crtc->base.dev;
2490 struct drm_i915_gem_object *obj = NULL;
2491 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2492 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2493 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2494 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2495 PAGE_SIZE);
2496
2497 size_aligned -= base_aligned;
46f297fb 2498
ff2652ea
CW
2499 if (plane_config->size == 0)
2500 return false;
2501
f37b5c2b
DV
2502 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2503 base_aligned,
2504 base_aligned,
2505 size_aligned);
46f297fb 2506 if (!obj)
484b41dd 2507 return false;
46f297fb 2508
49af449b
DL
2509 obj->tiling_mode = plane_config->tiling;
2510 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2511 obj->stride = fb->pitches[0];
46f297fb 2512
6bf129df
DL
2513 mode_cmd.pixel_format = fb->pixel_format;
2514 mode_cmd.width = fb->width;
2515 mode_cmd.height = fb->height;
2516 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2517 mode_cmd.modifier[0] = fb->modifier[0];
2518 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb
JB
2519
2520 mutex_lock(&dev->struct_mutex);
6bf129df 2521 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2522 &mode_cmd, obj)) {
46f297fb
JB
2523 DRM_DEBUG_KMS("intel fb init failed\n");
2524 goto out_unref_obj;
2525 }
46f297fb 2526 mutex_unlock(&dev->struct_mutex);
484b41dd 2527
f6936e29 2528 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2529 return true;
46f297fb
JB
2530
2531out_unref_obj:
2532 drm_gem_object_unreference(&obj->base);
2533 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2534 return false;
2535}
2536
afd65eb4
MR
2537/* Update plane->state->fb to match plane->fb after driver-internal updates */
2538static void
2539update_state_fb(struct drm_plane *plane)
2540{
2541 if (plane->fb == plane->state->fb)
2542 return;
2543
2544 if (plane->state->fb)
2545 drm_framebuffer_unreference(plane->state->fb);
2546 plane->state->fb = plane->fb;
2547 if (plane->state->fb)
2548 drm_framebuffer_reference(plane->state->fb);
2549}
2550
5724dbd1 2551static void
f6936e29
DV
2552intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2553 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2554{
2555 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2556 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2557 struct drm_crtc *c;
2558 struct intel_crtc *i;
2ff8fde1 2559 struct drm_i915_gem_object *obj;
88595ac9
DV
2560 struct drm_plane *primary = intel_crtc->base.primary;
2561 struct drm_framebuffer *fb;
484b41dd 2562
2d14030b 2563 if (!plane_config->fb)
484b41dd
JB
2564 return;
2565
f6936e29 2566 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2567 fb = &plane_config->fb->base;
2568 goto valid_fb;
f55548b5 2569 }
484b41dd 2570
2d14030b 2571 kfree(plane_config->fb);
484b41dd
JB
2572
2573 /*
2574 * Failed to alloc the obj, check to see if we should share
2575 * an fb with another CRTC instead
2576 */
70e1e0ec 2577 for_each_crtc(dev, c) {
484b41dd
JB
2578 i = to_intel_crtc(c);
2579
2580 if (c == &intel_crtc->base)
2581 continue;
2582
2ff8fde1
MR
2583 if (!i->active)
2584 continue;
2585
88595ac9
DV
2586 fb = c->primary->fb;
2587 if (!fb)
484b41dd
JB
2588 continue;
2589
88595ac9 2590 obj = intel_fb_obj(fb);
2ff8fde1 2591 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2592 drm_framebuffer_reference(fb);
2593 goto valid_fb;
484b41dd
JB
2594 }
2595 }
88595ac9
DV
2596
2597 return;
2598
2599valid_fb:
2600 obj = intel_fb_obj(fb);
2601 if (obj->tiling_mode != I915_TILING_NONE)
2602 dev_priv->preserve_bios_swizzle = true;
2603
2604 primary->fb = fb;
36750f28 2605 primary->crtc = primary->state->crtc = &intel_crtc->base;
88595ac9 2606 update_state_fb(primary);
36750f28 2607 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
88595ac9 2608 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
46f297fb
JB
2609}
2610
29b9bde6
DV
2611static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2612 struct drm_framebuffer *fb,
2613 int x, int y)
81255565
JB
2614{
2615 struct drm_device *dev = crtc->dev;
2616 struct drm_i915_private *dev_priv = dev->dev_private;
2617 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2618 struct drm_plane *primary = crtc->primary;
2619 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2620 struct drm_i915_gem_object *obj;
81255565 2621 int plane = intel_crtc->plane;
e506a0c6 2622 unsigned long linear_offset;
81255565 2623 u32 dspcntr;
f45651ba 2624 u32 reg = DSPCNTR(plane);
48404c1e 2625 int pixel_size;
f45651ba 2626
b70709a6 2627 if (!visible || !fb) {
fdd508a6
VS
2628 I915_WRITE(reg, 0);
2629 if (INTEL_INFO(dev)->gen >= 4)
2630 I915_WRITE(DSPSURF(plane), 0);
2631 else
2632 I915_WRITE(DSPADDR(plane), 0);
2633 POSTING_READ(reg);
2634 return;
2635 }
2636
c9ba6fad
VS
2637 obj = intel_fb_obj(fb);
2638 if (WARN_ON(obj == NULL))
2639 return;
2640
2641 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2642
f45651ba
VS
2643 dspcntr = DISPPLANE_GAMMA_ENABLE;
2644
fdd508a6 2645 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2646
2647 if (INTEL_INFO(dev)->gen < 4) {
2648 if (intel_crtc->pipe == PIPE_B)
2649 dspcntr |= DISPPLANE_SEL_PIPE_B;
2650
2651 /* pipesrc and dspsize control the size that is scaled from,
2652 * which should always be the user's requested size.
2653 */
2654 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2655 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2656 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2657 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2658 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2659 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2660 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2661 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2662 I915_WRITE(PRIMPOS(plane), 0);
2663 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2664 }
81255565 2665
57779d06
VS
2666 switch (fb->pixel_format) {
2667 case DRM_FORMAT_C8:
81255565
JB
2668 dspcntr |= DISPPLANE_8BPP;
2669 break;
57779d06 2670 case DRM_FORMAT_XRGB1555:
57779d06 2671 dspcntr |= DISPPLANE_BGRX555;
81255565 2672 break;
57779d06
VS
2673 case DRM_FORMAT_RGB565:
2674 dspcntr |= DISPPLANE_BGRX565;
2675 break;
2676 case DRM_FORMAT_XRGB8888:
57779d06
VS
2677 dspcntr |= DISPPLANE_BGRX888;
2678 break;
2679 case DRM_FORMAT_XBGR8888:
57779d06
VS
2680 dspcntr |= DISPPLANE_RGBX888;
2681 break;
2682 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2683 dspcntr |= DISPPLANE_BGRX101010;
2684 break;
2685 case DRM_FORMAT_XBGR2101010:
57779d06 2686 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2687 break;
2688 default:
baba133a 2689 BUG();
81255565 2690 }
57779d06 2691
f45651ba
VS
2692 if (INTEL_INFO(dev)->gen >= 4 &&
2693 obj->tiling_mode != I915_TILING_NONE)
2694 dspcntr |= DISPPLANE_TILED;
81255565 2695
de1aa629
VS
2696 if (IS_G4X(dev))
2697 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2698
b9897127 2699 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2700
c2c75131
DV
2701 if (INTEL_INFO(dev)->gen >= 4) {
2702 intel_crtc->dspaddr_offset =
4e9a86b6
VS
2703 intel_gen4_compute_page_offset(dev_priv,
2704 &x, &y, obj->tiling_mode,
b9897127 2705 pixel_size,
bc752862 2706 fb->pitches[0]);
c2c75131
DV
2707 linear_offset -= intel_crtc->dspaddr_offset;
2708 } else {
e506a0c6 2709 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2710 }
e506a0c6 2711
8e7d688b 2712 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2713 dspcntr |= DISPPLANE_ROTATE_180;
2714
6e3c9717
ACO
2715 x += (intel_crtc->config->pipe_src_w - 1);
2716 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2717
2718 /* Finding the last pixel of the last line of the display
2719 data and adding to linear_offset*/
2720 linear_offset +=
6e3c9717
ACO
2721 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2722 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2723 }
2724
2725 I915_WRITE(reg, dspcntr);
2726
01f2c773 2727 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2728 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2729 I915_WRITE(DSPSURF(plane),
2730 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2731 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2732 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2733 } else
f343c5f6 2734 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2735 POSTING_READ(reg);
17638cd6
JB
2736}
2737
29b9bde6
DV
2738static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2739 struct drm_framebuffer *fb,
2740 int x, int y)
17638cd6
JB
2741{
2742 struct drm_device *dev = crtc->dev;
2743 struct drm_i915_private *dev_priv = dev->dev_private;
2744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2745 struct drm_plane *primary = crtc->primary;
2746 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2747 struct drm_i915_gem_object *obj;
17638cd6 2748 int plane = intel_crtc->plane;
e506a0c6 2749 unsigned long linear_offset;
17638cd6 2750 u32 dspcntr;
f45651ba 2751 u32 reg = DSPCNTR(plane);
48404c1e 2752 int pixel_size;
f45651ba 2753
b70709a6 2754 if (!visible || !fb) {
fdd508a6
VS
2755 I915_WRITE(reg, 0);
2756 I915_WRITE(DSPSURF(plane), 0);
2757 POSTING_READ(reg);
2758 return;
2759 }
2760
c9ba6fad
VS
2761 obj = intel_fb_obj(fb);
2762 if (WARN_ON(obj == NULL))
2763 return;
2764
2765 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2766
f45651ba
VS
2767 dspcntr = DISPPLANE_GAMMA_ENABLE;
2768
fdd508a6 2769 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2770
2771 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2772 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2773
57779d06
VS
2774 switch (fb->pixel_format) {
2775 case DRM_FORMAT_C8:
17638cd6
JB
2776 dspcntr |= DISPPLANE_8BPP;
2777 break;
57779d06
VS
2778 case DRM_FORMAT_RGB565:
2779 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2780 break;
57779d06 2781 case DRM_FORMAT_XRGB8888:
57779d06
VS
2782 dspcntr |= DISPPLANE_BGRX888;
2783 break;
2784 case DRM_FORMAT_XBGR8888:
57779d06
VS
2785 dspcntr |= DISPPLANE_RGBX888;
2786 break;
2787 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2788 dspcntr |= DISPPLANE_BGRX101010;
2789 break;
2790 case DRM_FORMAT_XBGR2101010:
57779d06 2791 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2792 break;
2793 default:
baba133a 2794 BUG();
17638cd6
JB
2795 }
2796
2797 if (obj->tiling_mode != I915_TILING_NONE)
2798 dspcntr |= DISPPLANE_TILED;
17638cd6 2799
f45651ba 2800 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2801 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2802
b9897127 2803 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2804 intel_crtc->dspaddr_offset =
4e9a86b6
VS
2805 intel_gen4_compute_page_offset(dev_priv,
2806 &x, &y, obj->tiling_mode,
b9897127 2807 pixel_size,
bc752862 2808 fb->pitches[0]);
c2c75131 2809 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2810 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2811 dspcntr |= DISPPLANE_ROTATE_180;
2812
2813 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2814 x += (intel_crtc->config->pipe_src_w - 1);
2815 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2816
2817 /* Finding the last pixel of the last line of the display
2818 data and adding to linear_offset*/
2819 linear_offset +=
6e3c9717
ACO
2820 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2821 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2822 }
2823 }
2824
2825 I915_WRITE(reg, dspcntr);
17638cd6 2826
01f2c773 2827 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2828 I915_WRITE(DSPSURF(plane),
2829 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2830 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2831 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2832 } else {
2833 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2834 I915_WRITE(DSPLINOFF(plane), linear_offset);
2835 }
17638cd6 2836 POSTING_READ(reg);
17638cd6
JB
2837}
2838
b321803d
DL
2839u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2840 uint32_t pixel_format)
2841{
2842 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2843
2844 /*
2845 * The stride is either expressed as a multiple of 64 bytes
2846 * chunks for linear buffers or in number of tiles for tiled
2847 * buffers.
2848 */
2849 switch (fb_modifier) {
2850 case DRM_FORMAT_MOD_NONE:
2851 return 64;
2852 case I915_FORMAT_MOD_X_TILED:
2853 if (INTEL_INFO(dev)->gen == 2)
2854 return 128;
2855 return 512;
2856 case I915_FORMAT_MOD_Y_TILED:
2857 /* No need to check for old gens and Y tiling since this is
2858 * about the display engine and those will be blocked before
2859 * we get here.
2860 */
2861 return 128;
2862 case I915_FORMAT_MOD_Yf_TILED:
2863 if (bits_per_pixel == 8)
2864 return 64;
2865 else
2866 return 128;
2867 default:
2868 MISSING_CASE(fb_modifier);
2869 return 64;
2870 }
2871}
2872
121920fa
TU
2873unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2874 struct drm_i915_gem_object *obj)
2875{
9abc4648 2876 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
121920fa
TU
2877
2878 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
9abc4648 2879 view = &i915_ggtt_view_rotated;
121920fa
TU
2880
2881 return i915_gem_obj_ggtt_offset_view(obj, view);
2882}
2883
a1b2278e
CK
2884/*
2885 * This function detaches (aka. unbinds) unused scalers in hardware
2886 */
0583236e 2887static void skl_detach_scalers(struct intel_crtc *intel_crtc)
a1b2278e
CK
2888{
2889 struct drm_device *dev;
2890 struct drm_i915_private *dev_priv;
2891 struct intel_crtc_scaler_state *scaler_state;
2892 int i;
2893
a1b2278e
CK
2894 dev = intel_crtc->base.dev;
2895 dev_priv = dev->dev_private;
2896 scaler_state = &intel_crtc->config->scaler_state;
2897
2898 /* loop through and disable scalers that aren't in use */
2899 for (i = 0; i < intel_crtc->num_scalers; i++) {
2900 if (!scaler_state->scalers[i].in_use) {
2901 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2902 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2903 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2904 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2905 intel_crtc->base.base.id, intel_crtc->pipe, i);
2906 }
2907 }
2908}
2909
6156a456 2910u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2911{
6156a456 2912 switch (pixel_format) {
d161cf7a 2913 case DRM_FORMAT_C8:
c34ce3d1 2914 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2915 case DRM_FORMAT_RGB565:
c34ce3d1 2916 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2917 case DRM_FORMAT_XBGR8888:
c34ce3d1 2918 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2919 case DRM_FORMAT_XRGB8888:
c34ce3d1 2920 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2921 /*
2922 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2923 * to be already pre-multiplied. We need to add a knob (or a different
2924 * DRM_FORMAT) for user-space to configure that.
2925 */
f75fb42a 2926 case DRM_FORMAT_ABGR8888:
c34ce3d1 2927 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2928 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2929 case DRM_FORMAT_ARGB8888:
c34ce3d1 2930 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2931 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2932 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2933 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2934 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2935 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2936 case DRM_FORMAT_YUYV:
c34ce3d1 2937 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2938 case DRM_FORMAT_YVYU:
c34ce3d1 2939 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2940 case DRM_FORMAT_UYVY:
c34ce3d1 2941 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2942 case DRM_FORMAT_VYUY:
c34ce3d1 2943 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2944 default:
4249eeef 2945 MISSING_CASE(pixel_format);
70d21f0e 2946 }
8cfcba41 2947
c34ce3d1 2948 return 0;
6156a456 2949}
70d21f0e 2950
6156a456
CK
2951u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2952{
6156a456 2953 switch (fb_modifier) {
30af77c4 2954 case DRM_FORMAT_MOD_NONE:
70d21f0e 2955 break;
30af77c4 2956 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2957 return PLANE_CTL_TILED_X;
b321803d 2958 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2959 return PLANE_CTL_TILED_Y;
b321803d 2960 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2961 return PLANE_CTL_TILED_YF;
70d21f0e 2962 default:
6156a456 2963 MISSING_CASE(fb_modifier);
70d21f0e 2964 }
8cfcba41 2965
c34ce3d1 2966 return 0;
6156a456 2967}
70d21f0e 2968
6156a456
CK
2969u32 skl_plane_ctl_rotation(unsigned int rotation)
2970{
3b7a5119 2971 switch (rotation) {
6156a456
CK
2972 case BIT(DRM_ROTATE_0):
2973 break;
1e8df167
SJ
2974 /*
2975 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
2976 * while i915 HW rotation is clockwise, thats why this swapping.
2977 */
3b7a5119 2978 case BIT(DRM_ROTATE_90):
1e8df167 2979 return PLANE_CTL_ROTATE_270;
3b7a5119 2980 case BIT(DRM_ROTATE_180):
c34ce3d1 2981 return PLANE_CTL_ROTATE_180;
3b7a5119 2982 case BIT(DRM_ROTATE_270):
1e8df167 2983 return PLANE_CTL_ROTATE_90;
6156a456
CK
2984 default:
2985 MISSING_CASE(rotation);
2986 }
2987
c34ce3d1 2988 return 0;
6156a456
CK
2989}
2990
2991static void skylake_update_primary_plane(struct drm_crtc *crtc,
2992 struct drm_framebuffer *fb,
2993 int x, int y)
2994{
2995 struct drm_device *dev = crtc->dev;
2996 struct drm_i915_private *dev_priv = dev->dev_private;
2997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2998 struct drm_plane *plane = crtc->primary;
2999 bool visible = to_intel_plane_state(plane->state)->visible;
6156a456
CK
3000 struct drm_i915_gem_object *obj;
3001 int pipe = intel_crtc->pipe;
3002 u32 plane_ctl, stride_div, stride;
3003 u32 tile_height, plane_offset, plane_size;
3004 unsigned int rotation;
3005 int x_offset, y_offset;
3006 unsigned long surf_addr;
6156a456
CK
3007 struct intel_crtc_state *crtc_state = intel_crtc->config;
3008 struct intel_plane_state *plane_state;
3009 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3010 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3011 int scaler_id = -1;
3012
6156a456
CK
3013 plane_state = to_intel_plane_state(plane->state);
3014
b70709a6 3015 if (!visible || !fb) {
6156a456
CK
3016 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3017 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3018 POSTING_READ(PLANE_CTL(pipe, 0));
3019 return;
3b7a5119 3020 }
70d21f0e 3021
6156a456
CK
3022 plane_ctl = PLANE_CTL_ENABLE |
3023 PLANE_CTL_PIPE_GAMMA_ENABLE |
3024 PLANE_CTL_PIPE_CSC_ENABLE;
3025
3026 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3027 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3028 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3029
3030 rotation = plane->state->rotation;
3031 plane_ctl |= skl_plane_ctl_rotation(rotation);
3032
b321803d
DL
3033 obj = intel_fb_obj(fb);
3034 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3035 fb->pixel_format);
3b7a5119
SJ
3036 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3037
6156a456
CK
3038 /*
3039 * FIXME: intel_plane_state->src, dst aren't set when transitional
3040 * update_plane helpers are called from legacy paths.
3041 * Once full atomic crtc is available, below check can be avoided.
3042 */
3043 if (drm_rect_width(&plane_state->src)) {
3044 scaler_id = plane_state->scaler_id;
3045 src_x = plane_state->src.x1 >> 16;
3046 src_y = plane_state->src.y1 >> 16;
3047 src_w = drm_rect_width(&plane_state->src) >> 16;
3048 src_h = drm_rect_height(&plane_state->src) >> 16;
3049 dst_x = plane_state->dst.x1;
3050 dst_y = plane_state->dst.y1;
3051 dst_w = drm_rect_width(&plane_state->dst);
3052 dst_h = drm_rect_height(&plane_state->dst);
3053
3054 WARN_ON(x != src_x || y != src_y);
3055 } else {
3056 src_w = intel_crtc->config->pipe_src_w;
3057 src_h = intel_crtc->config->pipe_src_h;
3058 }
3059
3b7a5119
SJ
3060 if (intel_rotation_90_or_270(rotation)) {
3061 /* stride = Surface height in tiles */
2614f17d 3062 tile_height = intel_tile_height(dev, fb->pixel_format,
3b7a5119
SJ
3063 fb->modifier[0]);
3064 stride = DIV_ROUND_UP(fb->height, tile_height);
6156a456 3065 x_offset = stride * tile_height - y - src_h;
3b7a5119 3066 y_offset = x;
6156a456 3067 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3068 } else {
3069 stride = fb->pitches[0] / stride_div;
3070 x_offset = x;
3071 y_offset = y;
6156a456 3072 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3073 }
3074 plane_offset = y_offset << 16 | x_offset;
b321803d 3075
70d21f0e 3076 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3077 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3078 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3079 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3080
3081 if (scaler_id >= 0) {
3082 uint32_t ps_ctrl = 0;
3083
3084 WARN_ON(!dst_w || !dst_h);
3085 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3086 crtc_state->scaler_state.scalers[scaler_id].mode;
3087 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3088 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3089 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3090 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3091 I915_WRITE(PLANE_POS(pipe, 0), 0);
3092 } else {
3093 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3094 }
3095
121920fa 3096 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3097
3098 POSTING_READ(PLANE_SURF(pipe, 0));
3099}
3100
17638cd6
JB
3101/* Assume fb object is pinned & idle & fenced and just update base pointers */
3102static int
3103intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3104 int x, int y, enum mode_set_atomic state)
3105{
3106 struct drm_device *dev = crtc->dev;
3107 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 3108
6b8e6ed0
CW
3109 if (dev_priv->display.disable_fbc)
3110 dev_priv->display.disable_fbc(dev);
81255565 3111
29b9bde6
DV
3112 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3113
3114 return 0;
81255565
JB
3115}
3116
7514747d 3117static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3118{
96a02917
VS
3119 struct drm_crtc *crtc;
3120
70e1e0ec 3121 for_each_crtc(dev, crtc) {
96a02917
VS
3122 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3123 enum plane plane = intel_crtc->plane;
3124
3125 intel_prepare_page_flip(dev, plane);
3126 intel_finish_page_flip_plane(dev, plane);
3127 }
7514747d
VS
3128}
3129
3130static void intel_update_primary_planes(struct drm_device *dev)
3131{
3132 struct drm_i915_private *dev_priv = dev->dev_private;
3133 struct drm_crtc *crtc;
96a02917 3134
70e1e0ec 3135 for_each_crtc(dev, crtc) {
96a02917
VS
3136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3137
51fd371b 3138 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
3139 /*
3140 * FIXME: Once we have proper support for primary planes (and
3141 * disabling them without disabling the entire crtc) allow again
66e514c1 3142 * a NULL crtc->primary->fb.
947fdaad 3143 */
f4510a27 3144 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 3145 dev_priv->display.update_primary_plane(crtc,
66e514c1 3146 crtc->primary->fb,
262ca2b0
MR
3147 crtc->x,
3148 crtc->y);
51fd371b 3149 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
3150 }
3151}
3152
7514747d
VS
3153void intel_prepare_reset(struct drm_device *dev)
3154{
3155 /* no reset support for gen2 */
3156 if (IS_GEN2(dev))
3157 return;
3158
3159 /* reset doesn't touch the display */
3160 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3161 return;
3162
3163 drm_modeset_lock_all(dev);
f98ce92f
VS
3164 /*
3165 * Disabling the crtcs gracefully seems nicer. Also the
3166 * g33 docs say we should at least disable all the planes.
3167 */
6b72d486 3168 intel_display_suspend(dev);
7514747d
VS
3169}
3170
3171void intel_finish_reset(struct drm_device *dev)
3172{
3173 struct drm_i915_private *dev_priv = to_i915(dev);
3174
3175 /*
3176 * Flips in the rings will be nuked by the reset,
3177 * so complete all pending flips so that user space
3178 * will get its events and not get stuck.
3179 */
3180 intel_complete_page_flips(dev);
3181
3182 /* no reset support for gen2 */
3183 if (IS_GEN2(dev))
3184 return;
3185
3186 /* reset doesn't touch the display */
3187 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3188 /*
3189 * Flips in the rings have been nuked by the reset,
3190 * so update the base address of all primary
3191 * planes to the the last fb to make sure we're
3192 * showing the correct fb after a reset.
3193 */
3194 intel_update_primary_planes(dev);
3195 return;
3196 }
3197
3198 /*
3199 * The display has been reset as well,
3200 * so need a full re-initialization.
3201 */
3202 intel_runtime_pm_disable_interrupts(dev_priv);
3203 intel_runtime_pm_enable_interrupts(dev_priv);
3204
3205 intel_modeset_init_hw(dev);
3206
3207 spin_lock_irq(&dev_priv->irq_lock);
3208 if (dev_priv->display.hpd_irq_setup)
3209 dev_priv->display.hpd_irq_setup(dev);
3210 spin_unlock_irq(&dev_priv->irq_lock);
3211
3212 intel_modeset_setup_hw_state(dev, true);
3213
3214 intel_hpd_init(dev_priv);
3215
3216 drm_modeset_unlock_all(dev);
3217}
3218
2e2f351d 3219static void
14667a4b
CW
3220intel_finish_fb(struct drm_framebuffer *old_fb)
3221{
2ff8fde1 3222 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
2e2f351d 3223 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
14667a4b
CW
3224 bool was_interruptible = dev_priv->mm.interruptible;
3225 int ret;
3226
14667a4b
CW
3227 /* Big Hammer, we also need to ensure that any pending
3228 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3229 * current scanout is retired before unpinning the old
2e2f351d
CW
3230 * framebuffer. Note that we rely on userspace rendering
3231 * into the buffer attached to the pipe they are waiting
3232 * on. If not, userspace generates a GPU hang with IPEHR
3233 * point to the MI_WAIT_FOR_EVENT.
14667a4b
CW
3234 *
3235 * This should only fail upon a hung GPU, in which case we
3236 * can safely continue.
3237 */
3238 dev_priv->mm.interruptible = false;
2e2f351d 3239 ret = i915_gem_object_wait_rendering(obj, true);
14667a4b
CW
3240 dev_priv->mm.interruptible = was_interruptible;
3241
2e2f351d 3242 WARN_ON(ret);
14667a4b
CW
3243}
3244
7d5e3799
CW
3245static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3246{
3247 struct drm_device *dev = crtc->dev;
3248 struct drm_i915_private *dev_priv = dev->dev_private;
3249 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3250 bool pending;
3251
3252 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3253 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3254 return false;
3255
5e2d7afc 3256 spin_lock_irq(&dev->event_lock);
7d5e3799 3257 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3258 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3259
3260 return pending;
3261}
3262
e30e8f75
GP
3263static void intel_update_pipe_size(struct intel_crtc *crtc)
3264{
3265 struct drm_device *dev = crtc->base.dev;
3266 struct drm_i915_private *dev_priv = dev->dev_private;
3267 const struct drm_display_mode *adjusted_mode;
3268
3269 if (!i915.fastboot)
3270 return;
3271
3272 /*
3273 * Update pipe size and adjust fitter if needed: the reason for this is
3274 * that in compute_mode_changes we check the native mode (not the pfit
3275 * mode) to see if we can flip rather than do a full mode set. In the
3276 * fastboot case, we'll flip, but if we don't update the pipesrc and
3277 * pfit state, we'll end up with a big fb scanned out into the wrong
3278 * sized surface.
3279 *
3280 * To fix this properly, we need to hoist the checks up into
3281 * compute_mode_changes (or above), check the actual pfit state and
3282 * whether the platform allows pfit disable with pipe active, and only
3283 * then update the pipesrc and pfit state, even on the flip path.
3284 */
3285
6e3c9717 3286 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
3287
3288 I915_WRITE(PIPESRC(crtc->pipe),
3289 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3290 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 3291 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
3292 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3293 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3294 I915_WRITE(PF_CTL(crtc->pipe), 0);
3295 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3296 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3297 }
6e3c9717
ACO
3298 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3299 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3300}
3301
5e84e1a4
ZW
3302static void intel_fdi_normal_train(struct drm_crtc *crtc)
3303{
3304 struct drm_device *dev = crtc->dev;
3305 struct drm_i915_private *dev_priv = dev->dev_private;
3306 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3307 int pipe = intel_crtc->pipe;
3308 u32 reg, temp;
3309
3310 /* enable normal train */
3311 reg = FDI_TX_CTL(pipe);
3312 temp = I915_READ(reg);
61e499bf 3313 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3314 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3315 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3316 } else {
3317 temp &= ~FDI_LINK_TRAIN_NONE;
3318 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3319 }
5e84e1a4
ZW
3320 I915_WRITE(reg, temp);
3321
3322 reg = FDI_RX_CTL(pipe);
3323 temp = I915_READ(reg);
3324 if (HAS_PCH_CPT(dev)) {
3325 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3326 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3327 } else {
3328 temp &= ~FDI_LINK_TRAIN_NONE;
3329 temp |= FDI_LINK_TRAIN_NONE;
3330 }
3331 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3332
3333 /* wait one idle pattern time */
3334 POSTING_READ(reg);
3335 udelay(1000);
357555c0
JB
3336
3337 /* IVB wants error correction enabled */
3338 if (IS_IVYBRIDGE(dev))
3339 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3340 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3341}
3342
8db9d77b
ZW
3343/* The FDI link training functions for ILK/Ibexpeak. */
3344static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3345{
3346 struct drm_device *dev = crtc->dev;
3347 struct drm_i915_private *dev_priv = dev->dev_private;
3348 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3349 int pipe = intel_crtc->pipe;
5eddb70b 3350 u32 reg, temp, tries;
8db9d77b 3351
1c8562f6 3352 /* FDI needs bits from pipe first */
0fc932b8 3353 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3354
e1a44743
AJ
3355 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3356 for train result */
5eddb70b
CW
3357 reg = FDI_RX_IMR(pipe);
3358 temp = I915_READ(reg);
e1a44743
AJ
3359 temp &= ~FDI_RX_SYMBOL_LOCK;
3360 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3361 I915_WRITE(reg, temp);
3362 I915_READ(reg);
e1a44743
AJ
3363 udelay(150);
3364
8db9d77b 3365 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3366 reg = FDI_TX_CTL(pipe);
3367 temp = I915_READ(reg);
627eb5a3 3368 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3369 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3370 temp &= ~FDI_LINK_TRAIN_NONE;
3371 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3372 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3373
5eddb70b
CW
3374 reg = FDI_RX_CTL(pipe);
3375 temp = I915_READ(reg);
8db9d77b
ZW
3376 temp &= ~FDI_LINK_TRAIN_NONE;
3377 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3378 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3379
3380 POSTING_READ(reg);
8db9d77b
ZW
3381 udelay(150);
3382
5b2adf89 3383 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3384 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3385 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3386 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3387
5eddb70b 3388 reg = FDI_RX_IIR(pipe);
e1a44743 3389 for (tries = 0; tries < 5; tries++) {
5eddb70b 3390 temp = I915_READ(reg);
8db9d77b
ZW
3391 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3392
3393 if ((temp & FDI_RX_BIT_LOCK)) {
3394 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3395 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3396 break;
3397 }
8db9d77b 3398 }
e1a44743 3399 if (tries == 5)
5eddb70b 3400 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3401
3402 /* Train 2 */
5eddb70b
CW
3403 reg = FDI_TX_CTL(pipe);
3404 temp = I915_READ(reg);
8db9d77b
ZW
3405 temp &= ~FDI_LINK_TRAIN_NONE;
3406 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3407 I915_WRITE(reg, temp);
8db9d77b 3408
5eddb70b
CW
3409 reg = FDI_RX_CTL(pipe);
3410 temp = I915_READ(reg);
8db9d77b
ZW
3411 temp &= ~FDI_LINK_TRAIN_NONE;
3412 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3413 I915_WRITE(reg, temp);
8db9d77b 3414
5eddb70b
CW
3415 POSTING_READ(reg);
3416 udelay(150);
8db9d77b 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_SYMBOL_LOCK) {
5eddb70b 3424 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3425 DRM_DEBUG_KMS("FDI train 2 done.\n");
3426 break;
3427 }
8db9d77b 3428 }
e1a44743 3429 if (tries == 5)
5eddb70b 3430 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3431
3432 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3433
8db9d77b
ZW
3434}
3435
0206e353 3436static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3437 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3438 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3439 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3440 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3441};
3442
3443/* The FDI link training functions for SNB/Cougarpoint. */
3444static void gen6_fdi_link_train(struct drm_crtc *crtc)
3445{
3446 struct drm_device *dev = crtc->dev;
3447 struct drm_i915_private *dev_priv = dev->dev_private;
3448 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3449 int pipe = intel_crtc->pipe;
fa37d39e 3450 u32 reg, temp, i, retry;
8db9d77b 3451
e1a44743
AJ
3452 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3453 for train result */
5eddb70b
CW
3454 reg = FDI_RX_IMR(pipe);
3455 temp = I915_READ(reg);
e1a44743
AJ
3456 temp &= ~FDI_RX_SYMBOL_LOCK;
3457 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3458 I915_WRITE(reg, temp);
3459
3460 POSTING_READ(reg);
e1a44743
AJ
3461 udelay(150);
3462
8db9d77b 3463 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3464 reg = FDI_TX_CTL(pipe);
3465 temp = I915_READ(reg);
627eb5a3 3466 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3467 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3468 temp &= ~FDI_LINK_TRAIN_NONE;
3469 temp |= FDI_LINK_TRAIN_PATTERN_1;
3470 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3471 /* SNB-B */
3472 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3473 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3474
d74cf324
DV
3475 I915_WRITE(FDI_RX_MISC(pipe),
3476 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3477
5eddb70b
CW
3478 reg = FDI_RX_CTL(pipe);
3479 temp = I915_READ(reg);
8db9d77b
ZW
3480 if (HAS_PCH_CPT(dev)) {
3481 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3482 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3483 } else {
3484 temp &= ~FDI_LINK_TRAIN_NONE;
3485 temp |= FDI_LINK_TRAIN_PATTERN_1;
3486 }
5eddb70b
CW
3487 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3488
3489 POSTING_READ(reg);
8db9d77b
ZW
3490 udelay(150);
3491
0206e353 3492 for (i = 0; i < 4; i++) {
5eddb70b
CW
3493 reg = FDI_TX_CTL(pipe);
3494 temp = I915_READ(reg);
8db9d77b
ZW
3495 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3496 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3497 I915_WRITE(reg, temp);
3498
3499 POSTING_READ(reg);
8db9d77b
ZW
3500 udelay(500);
3501
fa37d39e
SP
3502 for (retry = 0; retry < 5; retry++) {
3503 reg = FDI_RX_IIR(pipe);
3504 temp = I915_READ(reg);
3505 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3506 if (temp & FDI_RX_BIT_LOCK) {
3507 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3508 DRM_DEBUG_KMS("FDI train 1 done.\n");
3509 break;
3510 }
3511 udelay(50);
8db9d77b 3512 }
fa37d39e
SP
3513 if (retry < 5)
3514 break;
8db9d77b
ZW
3515 }
3516 if (i == 4)
5eddb70b 3517 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3518
3519 /* Train 2 */
5eddb70b
CW
3520 reg = FDI_TX_CTL(pipe);
3521 temp = I915_READ(reg);
8db9d77b
ZW
3522 temp &= ~FDI_LINK_TRAIN_NONE;
3523 temp |= FDI_LINK_TRAIN_PATTERN_2;
3524 if (IS_GEN6(dev)) {
3525 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3526 /* SNB-B */
3527 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3528 }
5eddb70b 3529 I915_WRITE(reg, temp);
8db9d77b 3530
5eddb70b
CW
3531 reg = FDI_RX_CTL(pipe);
3532 temp = I915_READ(reg);
8db9d77b
ZW
3533 if (HAS_PCH_CPT(dev)) {
3534 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3535 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3536 } else {
3537 temp &= ~FDI_LINK_TRAIN_NONE;
3538 temp |= FDI_LINK_TRAIN_PATTERN_2;
3539 }
5eddb70b
CW
3540 I915_WRITE(reg, temp);
3541
3542 POSTING_READ(reg);
8db9d77b
ZW
3543 udelay(150);
3544
0206e353 3545 for (i = 0; i < 4; i++) {
5eddb70b
CW
3546 reg = FDI_TX_CTL(pipe);
3547 temp = I915_READ(reg);
8db9d77b
ZW
3548 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3549 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3550 I915_WRITE(reg, temp);
3551
3552 POSTING_READ(reg);
8db9d77b
ZW
3553 udelay(500);
3554
fa37d39e
SP
3555 for (retry = 0; retry < 5; retry++) {
3556 reg = FDI_RX_IIR(pipe);
3557 temp = I915_READ(reg);
3558 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3559 if (temp & FDI_RX_SYMBOL_LOCK) {
3560 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3561 DRM_DEBUG_KMS("FDI train 2 done.\n");
3562 break;
3563 }
3564 udelay(50);
8db9d77b 3565 }
fa37d39e
SP
3566 if (retry < 5)
3567 break;
8db9d77b
ZW
3568 }
3569 if (i == 4)
5eddb70b 3570 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3571
3572 DRM_DEBUG_KMS("FDI train done.\n");
3573}
3574
357555c0
JB
3575/* Manual link training for Ivy Bridge A0 parts */
3576static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3577{
3578 struct drm_device *dev = crtc->dev;
3579 struct drm_i915_private *dev_priv = dev->dev_private;
3580 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3581 int pipe = intel_crtc->pipe;
139ccd3f 3582 u32 reg, temp, i, j;
357555c0
JB
3583
3584 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3585 for train result */
3586 reg = FDI_RX_IMR(pipe);
3587 temp = I915_READ(reg);
3588 temp &= ~FDI_RX_SYMBOL_LOCK;
3589 temp &= ~FDI_RX_BIT_LOCK;
3590 I915_WRITE(reg, temp);
3591
3592 POSTING_READ(reg);
3593 udelay(150);
3594
01a415fd
DV
3595 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3596 I915_READ(FDI_RX_IIR(pipe)));
3597
139ccd3f
JB
3598 /* Try each vswing and preemphasis setting twice before moving on */
3599 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3600 /* disable first in case we need to retry */
3601 reg = FDI_TX_CTL(pipe);
3602 temp = I915_READ(reg);
3603 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3604 temp &= ~FDI_TX_ENABLE;
3605 I915_WRITE(reg, temp);
357555c0 3606
139ccd3f
JB
3607 reg = FDI_RX_CTL(pipe);
3608 temp = I915_READ(reg);
3609 temp &= ~FDI_LINK_TRAIN_AUTO;
3610 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3611 temp &= ~FDI_RX_ENABLE;
3612 I915_WRITE(reg, temp);
357555c0 3613
139ccd3f 3614 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3615 reg = FDI_TX_CTL(pipe);
3616 temp = I915_READ(reg);
139ccd3f 3617 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3618 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3619 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3620 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3621 temp |= snb_b_fdi_train_param[j/2];
3622 temp |= FDI_COMPOSITE_SYNC;
3623 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3624
139ccd3f
JB
3625 I915_WRITE(FDI_RX_MISC(pipe),
3626 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3627
139ccd3f 3628 reg = FDI_RX_CTL(pipe);
357555c0 3629 temp = I915_READ(reg);
139ccd3f
JB
3630 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3631 temp |= FDI_COMPOSITE_SYNC;
3632 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3633
139ccd3f
JB
3634 POSTING_READ(reg);
3635 udelay(1); /* should be 0.5us */
357555c0 3636
139ccd3f
JB
3637 for (i = 0; i < 4; i++) {
3638 reg = FDI_RX_IIR(pipe);
3639 temp = I915_READ(reg);
3640 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3641
139ccd3f
JB
3642 if (temp & FDI_RX_BIT_LOCK ||
3643 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3644 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3645 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3646 i);
3647 break;
3648 }
3649 udelay(1); /* should be 0.5us */
3650 }
3651 if (i == 4) {
3652 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3653 continue;
3654 }
357555c0 3655
139ccd3f 3656 /* Train 2 */
357555c0
JB
3657 reg = FDI_TX_CTL(pipe);
3658 temp = I915_READ(reg);
139ccd3f
JB
3659 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3660 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3661 I915_WRITE(reg, temp);
3662
3663 reg = FDI_RX_CTL(pipe);
3664 temp = I915_READ(reg);
3665 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3666 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3667 I915_WRITE(reg, temp);
3668
3669 POSTING_READ(reg);
139ccd3f 3670 udelay(2); /* should be 1.5us */
357555c0 3671
139ccd3f
JB
3672 for (i = 0; i < 4; i++) {
3673 reg = FDI_RX_IIR(pipe);
3674 temp = I915_READ(reg);
3675 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3676
139ccd3f
JB
3677 if (temp & FDI_RX_SYMBOL_LOCK ||
3678 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3679 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3680 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3681 i);
3682 goto train_done;
3683 }
3684 udelay(2); /* should be 1.5us */
357555c0 3685 }
139ccd3f
JB
3686 if (i == 4)
3687 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3688 }
357555c0 3689
139ccd3f 3690train_done:
357555c0
JB
3691 DRM_DEBUG_KMS("FDI train done.\n");
3692}
3693
88cefb6c 3694static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3695{
88cefb6c 3696 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3697 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3698 int pipe = intel_crtc->pipe;
5eddb70b 3699 u32 reg, temp;
79e53945 3700
c64e311e 3701
c98e9dcf 3702 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3703 reg = FDI_RX_CTL(pipe);
3704 temp = I915_READ(reg);
627eb5a3 3705 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3706 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3707 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3708 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3709
3710 POSTING_READ(reg);
c98e9dcf
JB
3711 udelay(200);
3712
3713 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3714 temp = I915_READ(reg);
3715 I915_WRITE(reg, temp | FDI_PCDCLK);
3716
3717 POSTING_READ(reg);
c98e9dcf
JB
3718 udelay(200);
3719
20749730
PZ
3720 /* Enable CPU FDI TX PLL, always on for Ironlake */
3721 reg = FDI_TX_CTL(pipe);
3722 temp = I915_READ(reg);
3723 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3724 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3725
20749730
PZ
3726 POSTING_READ(reg);
3727 udelay(100);
6be4a607 3728 }
0e23b99d
JB
3729}
3730
88cefb6c
DV
3731static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3732{
3733 struct drm_device *dev = intel_crtc->base.dev;
3734 struct drm_i915_private *dev_priv = dev->dev_private;
3735 int pipe = intel_crtc->pipe;
3736 u32 reg, temp;
3737
3738 /* Switch from PCDclk to Rawclk */
3739 reg = FDI_RX_CTL(pipe);
3740 temp = I915_READ(reg);
3741 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3742
3743 /* Disable CPU FDI TX PLL */
3744 reg = FDI_TX_CTL(pipe);
3745 temp = I915_READ(reg);
3746 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3747
3748 POSTING_READ(reg);
3749 udelay(100);
3750
3751 reg = FDI_RX_CTL(pipe);
3752 temp = I915_READ(reg);
3753 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3754
3755 /* Wait for the clocks to turn off. */
3756 POSTING_READ(reg);
3757 udelay(100);
3758}
3759
0fc932b8
JB
3760static void ironlake_fdi_disable(struct drm_crtc *crtc)
3761{
3762 struct drm_device *dev = crtc->dev;
3763 struct drm_i915_private *dev_priv = dev->dev_private;
3764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3765 int pipe = intel_crtc->pipe;
3766 u32 reg, temp;
3767
3768 /* disable CPU FDI tx and PCH FDI rx */
3769 reg = FDI_TX_CTL(pipe);
3770 temp = I915_READ(reg);
3771 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3772 POSTING_READ(reg);
3773
3774 reg = FDI_RX_CTL(pipe);
3775 temp = I915_READ(reg);
3776 temp &= ~(0x7 << 16);
dfd07d72 3777 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3778 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3779
3780 POSTING_READ(reg);
3781 udelay(100);
3782
3783 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3784 if (HAS_PCH_IBX(dev))
6f06ce18 3785 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3786
3787 /* still set train pattern 1 */
3788 reg = FDI_TX_CTL(pipe);
3789 temp = I915_READ(reg);
3790 temp &= ~FDI_LINK_TRAIN_NONE;
3791 temp |= FDI_LINK_TRAIN_PATTERN_1;
3792 I915_WRITE(reg, temp);
3793
3794 reg = FDI_RX_CTL(pipe);
3795 temp = I915_READ(reg);
3796 if (HAS_PCH_CPT(dev)) {
3797 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3798 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3799 } else {
3800 temp &= ~FDI_LINK_TRAIN_NONE;
3801 temp |= FDI_LINK_TRAIN_PATTERN_1;
3802 }
3803 /* BPC in FDI rx is consistent with that in PIPECONF */
3804 temp &= ~(0x07 << 16);
dfd07d72 3805 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3806 I915_WRITE(reg, temp);
3807
3808 POSTING_READ(reg);
3809 udelay(100);
3810}
3811
5dce5b93
CW
3812bool intel_has_pending_fb_unpin(struct drm_device *dev)
3813{
3814 struct intel_crtc *crtc;
3815
3816 /* Note that we don't need to be called with mode_config.lock here
3817 * as our list of CRTC objects is static for the lifetime of the
3818 * device and so cannot disappear as we iterate. Similarly, we can
3819 * happily treat the predicates as racy, atomic checks as userspace
3820 * cannot claim and pin a new fb without at least acquring the
3821 * struct_mutex and so serialising with us.
3822 */
d3fcc808 3823 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3824 if (atomic_read(&crtc->unpin_work_count) == 0)
3825 continue;
3826
3827 if (crtc->unpin_work)
3828 intel_wait_for_vblank(dev, crtc->pipe);
3829
3830 return true;
3831 }
3832
3833 return false;
3834}
3835
d6bbafa1
CW
3836static void page_flip_completed(struct intel_crtc *intel_crtc)
3837{
3838 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3839 struct intel_unpin_work *work = intel_crtc->unpin_work;
3840
3841 /* ensure that the unpin work is consistent wrt ->pending. */
3842 smp_rmb();
3843 intel_crtc->unpin_work = NULL;
3844
3845 if (work->event)
3846 drm_send_vblank_event(intel_crtc->base.dev,
3847 intel_crtc->pipe,
3848 work->event);
3849
3850 drm_crtc_vblank_put(&intel_crtc->base);
3851
3852 wake_up_all(&dev_priv->pending_flip_queue);
3853 queue_work(dev_priv->wq, &work->work);
3854
3855 trace_i915_flip_complete(intel_crtc->plane,
3856 work->pending_flip_obj);
3857}
3858
46a55d30 3859void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3860{
0f91128d 3861 struct drm_device *dev = crtc->dev;
5bb61643 3862 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3863
2c10d571 3864 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3865 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3866 !intel_crtc_has_pending_flip(crtc),
3867 60*HZ) == 0)) {
3868 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3869
5e2d7afc 3870 spin_lock_irq(&dev->event_lock);
9c787942
CW
3871 if (intel_crtc->unpin_work) {
3872 WARN_ONCE(1, "Removing stuck page flip\n");
3873 page_flip_completed(intel_crtc);
3874 }
5e2d7afc 3875 spin_unlock_irq(&dev->event_lock);
9c787942 3876 }
5bb61643 3877
975d568a
CW
3878 if (crtc->primary->fb) {
3879 mutex_lock(&dev->struct_mutex);
3880 intel_finish_fb(crtc->primary->fb);
3881 mutex_unlock(&dev->struct_mutex);
3882 }
e6c3a2a6
CW
3883}
3884
e615efe4
ED
3885/* Program iCLKIP clock to the desired frequency */
3886static void lpt_program_iclkip(struct drm_crtc *crtc)
3887{
3888 struct drm_device *dev = crtc->dev;
3889 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3890 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3891 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3892 u32 temp;
3893
a580516d 3894 mutex_lock(&dev_priv->sb_lock);
09153000 3895
e615efe4
ED
3896 /* It is necessary to ungate the pixclk gate prior to programming
3897 * the divisors, and gate it back when it is done.
3898 */
3899 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3900
3901 /* Disable SSCCTL */
3902 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3903 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3904 SBI_SSCCTL_DISABLE,
3905 SBI_ICLK);
e615efe4
ED
3906
3907 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3908 if (clock == 20000) {
e615efe4
ED
3909 auxdiv = 1;
3910 divsel = 0x41;
3911 phaseinc = 0x20;
3912 } else {
3913 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3914 * but the adjusted_mode->crtc_clock in in KHz. To get the
3915 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3916 * convert the virtual clock precision to KHz here for higher
3917 * precision.
3918 */
3919 u32 iclk_virtual_root_freq = 172800 * 1000;
3920 u32 iclk_pi_range = 64;
3921 u32 desired_divisor, msb_divisor_value, pi_value;
3922
12d7ceed 3923 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3924 msb_divisor_value = desired_divisor / iclk_pi_range;
3925 pi_value = desired_divisor % iclk_pi_range;
3926
3927 auxdiv = 0;
3928 divsel = msb_divisor_value - 2;
3929 phaseinc = pi_value;
3930 }
3931
3932 /* This should not happen with any sane values */
3933 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3934 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3935 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3936 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3937
3938 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3939 clock,
e615efe4
ED
3940 auxdiv,
3941 divsel,
3942 phasedir,
3943 phaseinc);
3944
3945 /* Program SSCDIVINTPHASE6 */
988d6ee8 3946 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3947 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3948 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3949 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3950 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3951 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3952 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3953 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3954
3955 /* Program SSCAUXDIV */
988d6ee8 3956 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3957 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3958 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3959 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3960
3961 /* Enable modulator and associated divider */
988d6ee8 3962 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3963 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3964 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3965
3966 /* Wait for initialization time */
3967 udelay(24);
3968
3969 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000 3970
a580516d 3971 mutex_unlock(&dev_priv->sb_lock);
e615efe4
ED
3972}
3973
275f01b2
DV
3974static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3975 enum pipe pch_transcoder)
3976{
3977 struct drm_device *dev = crtc->base.dev;
3978 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3979 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
3980
3981 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3982 I915_READ(HTOTAL(cpu_transcoder)));
3983 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3984 I915_READ(HBLANK(cpu_transcoder)));
3985 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3986 I915_READ(HSYNC(cpu_transcoder)));
3987
3988 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3989 I915_READ(VTOTAL(cpu_transcoder)));
3990 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3991 I915_READ(VBLANK(cpu_transcoder)));
3992 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3993 I915_READ(VSYNC(cpu_transcoder)));
3994 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3995 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3996}
3997
003632d9 3998static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
3999{
4000 struct drm_i915_private *dev_priv = dev->dev_private;
4001 uint32_t temp;
4002
4003 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4004 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4005 return;
4006
4007 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4008 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4009
003632d9
ACO
4010 temp &= ~FDI_BC_BIFURCATION_SELECT;
4011 if (enable)
4012 temp |= FDI_BC_BIFURCATION_SELECT;
4013
4014 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4015 I915_WRITE(SOUTH_CHICKEN1, temp);
4016 POSTING_READ(SOUTH_CHICKEN1);
4017}
4018
4019static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4020{
4021 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4022
4023 switch (intel_crtc->pipe) {
4024 case PIPE_A:
4025 break;
4026 case PIPE_B:
6e3c9717 4027 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4028 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4029 else
003632d9 4030 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4031
4032 break;
4033 case PIPE_C:
003632d9 4034 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4035
4036 break;
4037 default:
4038 BUG();
4039 }
4040}
4041
f67a559d
JB
4042/*
4043 * Enable PCH resources required for PCH ports:
4044 * - PCH PLLs
4045 * - FDI training & RX/TX
4046 * - update transcoder timings
4047 * - DP transcoding bits
4048 * - transcoder
4049 */
4050static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4051{
4052 struct drm_device *dev = crtc->dev;
4053 struct drm_i915_private *dev_priv = dev->dev_private;
4054 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4055 int pipe = intel_crtc->pipe;
ee7b9f93 4056 u32 reg, temp;
2c07245f 4057
ab9412ba 4058 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4059
1fbc0d78
DV
4060 if (IS_IVYBRIDGE(dev))
4061 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4062
cd986abb
DV
4063 /* Write the TU size bits before fdi link training, so that error
4064 * detection works. */
4065 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4066 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4067
c98e9dcf 4068 /* For PCH output, training FDI link */
674cf967 4069 dev_priv->display.fdi_link_train(crtc);
2c07245f 4070
3ad8a208
DV
4071 /* We need to program the right clock selection before writing the pixel
4072 * mutliplier into the DPLL. */
303b81e0 4073 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4074 u32 sel;
4b645f14 4075
c98e9dcf 4076 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4077 temp |= TRANS_DPLL_ENABLE(pipe);
4078 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 4079 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
4080 temp |= sel;
4081 else
4082 temp &= ~sel;
c98e9dcf 4083 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4084 }
5eddb70b 4085
3ad8a208
DV
4086 /* XXX: pch pll's can be enabled any time before we enable the PCH
4087 * transcoder, and we actually should do this to not upset any PCH
4088 * transcoder that already use the clock when we share it.
4089 *
4090 * Note that enable_shared_dpll tries to do the right thing, but
4091 * get_shared_dpll unconditionally resets the pll - we need that to have
4092 * the right LVDS enable sequence. */
85b3894f 4093 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4094
d9b6cb56
JB
4095 /* set transcoder timing, panel must allow it */
4096 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4097 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4098
303b81e0 4099 intel_fdi_normal_train(crtc);
5e84e1a4 4100
c98e9dcf 4101 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4102 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 4103 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
4104 reg = TRANS_DP_CTL(pipe);
4105 temp = I915_READ(reg);
4106 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4107 TRANS_DP_SYNC_MASK |
4108 TRANS_DP_BPC_MASK);
e3ef4479 4109 temp |= TRANS_DP_OUTPUT_ENABLE;
9325c9f0 4110 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
4111
4112 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4113 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 4114 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4115 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4116
4117 switch (intel_trans_dp_port_sel(crtc)) {
4118 case PCH_DP_B:
5eddb70b 4119 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
4120 break;
4121 case PCH_DP_C:
5eddb70b 4122 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
4123 break;
4124 case PCH_DP_D:
5eddb70b 4125 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4126 break;
4127 default:
e95d41e1 4128 BUG();
32f9d658 4129 }
2c07245f 4130
5eddb70b 4131 I915_WRITE(reg, temp);
6be4a607 4132 }
b52eb4dc 4133
b8a4f404 4134 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4135}
4136
1507e5bd
PZ
4137static void lpt_pch_enable(struct drm_crtc *crtc)
4138{
4139 struct drm_device *dev = crtc->dev;
4140 struct drm_i915_private *dev_priv = dev->dev_private;
4141 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4142 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4143
ab9412ba 4144 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4145
8c52b5e8 4146 lpt_program_iclkip(crtc);
1507e5bd 4147
0540e488 4148 /* Set transcoder timing. */
275f01b2 4149 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4150
937bb610 4151 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4152}
4153
190f68c5
ACO
4154struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4155 struct intel_crtc_state *crtc_state)
ee7b9f93 4156{
e2b78267 4157 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 4158 struct intel_shared_dpll *pll;
de419ab6 4159 struct intel_shared_dpll_config *shared_dpll;
e2b78267 4160 enum intel_dpll_id i;
ee7b9f93 4161
de419ab6
ML
4162 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4163
98b6bd99
DV
4164 if (HAS_PCH_IBX(dev_priv->dev)) {
4165 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 4166 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 4167 pll = &dev_priv->shared_dplls[i];
98b6bd99 4168
46edb027
DV
4169 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4170 crtc->base.base.id, pll->name);
98b6bd99 4171
de419ab6 4172 WARN_ON(shared_dpll[i].crtc_mask);
f2a69f44 4173
98b6bd99
DV
4174 goto found;
4175 }
4176
bcddf610
S
4177 if (IS_BROXTON(dev_priv->dev)) {
4178 /* PLL is attached to port in bxt */
4179 struct intel_encoder *encoder;
4180 struct intel_digital_port *intel_dig_port;
4181
4182 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4183 if (WARN_ON(!encoder))
4184 return NULL;
4185
4186 intel_dig_port = enc_to_dig_port(&encoder->base);
4187 /* 1:1 mapping between ports and PLLs */
4188 i = (enum intel_dpll_id)intel_dig_port->port;
4189 pll = &dev_priv->shared_dplls[i];
4190 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4191 crtc->base.base.id, pll->name);
de419ab6 4192 WARN_ON(shared_dpll[i].crtc_mask);
bcddf610
S
4193
4194 goto found;
4195 }
4196
e72f9fbf
DV
4197 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4198 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
4199
4200 /* Only want to check enabled timings first */
de419ab6 4201 if (shared_dpll[i].crtc_mask == 0)
ee7b9f93
JB
4202 continue;
4203
190f68c5 4204 if (memcmp(&crtc_state->dpll_hw_state,
de419ab6
ML
4205 &shared_dpll[i].hw_state,
4206 sizeof(crtc_state->dpll_hw_state)) == 0) {
8bd31e67 4207 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 4208 crtc->base.base.id, pll->name,
de419ab6 4209 shared_dpll[i].crtc_mask,
8bd31e67 4210 pll->active);
ee7b9f93
JB
4211 goto found;
4212 }
4213 }
4214
4215 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
4216 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4217 pll = &dev_priv->shared_dplls[i];
de419ab6 4218 if (shared_dpll[i].crtc_mask == 0) {
46edb027
DV
4219 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4220 crtc->base.base.id, pll->name);
ee7b9f93
JB
4221 goto found;
4222 }
4223 }
4224
4225 return NULL;
4226
4227found:
de419ab6
ML
4228 if (shared_dpll[i].crtc_mask == 0)
4229 shared_dpll[i].hw_state =
4230 crtc_state->dpll_hw_state;
f2a69f44 4231
190f68c5 4232 crtc_state->shared_dpll = i;
46edb027
DV
4233 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4234 pipe_name(crtc->pipe));
ee7b9f93 4235
de419ab6 4236 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
e04c7350 4237
ee7b9f93
JB
4238 return pll;
4239}
4240
de419ab6 4241static void intel_shared_dpll_commit(struct drm_atomic_state *state)
8bd31e67 4242{
de419ab6
ML
4243 struct drm_i915_private *dev_priv = to_i915(state->dev);
4244 struct intel_shared_dpll_config *shared_dpll;
8bd31e67
ACO
4245 struct intel_shared_dpll *pll;
4246 enum intel_dpll_id i;
4247
de419ab6
ML
4248 if (!to_intel_atomic_state(state)->dpll_set)
4249 return;
8bd31e67 4250
de419ab6 4251 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
8bd31e67
ACO
4252 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4253 pll = &dev_priv->shared_dplls[i];
de419ab6 4254 pll->config = shared_dpll[i];
8bd31e67
ACO
4255 }
4256}
4257
a1520318 4258static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4259{
4260 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4261 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4262 u32 temp;
4263
4264 temp = I915_READ(dslreg);
4265 udelay(500);
4266 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4267 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4268 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4269 }
4270}
4271
86adf9d7
ML
4272static int
4273skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4274 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4275 int src_w, int src_h, int dst_w, int dst_h)
a1b2278e 4276{
86adf9d7
ML
4277 struct intel_crtc_scaler_state *scaler_state =
4278 &crtc_state->scaler_state;
4279 struct intel_crtc *intel_crtc =
4280 to_intel_crtc(crtc_state->base.crtc);
a1b2278e 4281 int need_scaling;
6156a456
CK
4282
4283 need_scaling = intel_rotation_90_or_270(rotation) ?
4284 (src_h != dst_w || src_w != dst_h):
4285 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4286
4287 /*
4288 * if plane is being disabled or scaler is no more required or force detach
4289 * - free scaler binded to this plane/crtc
4290 * - in order to do this, update crtc->scaler_usage
4291 *
4292 * Here scaler state in crtc_state is set free so that
4293 * scaler can be assigned to other user. Actual register
4294 * update to free the scaler is done in plane/panel-fit programming.
4295 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4296 */
86adf9d7 4297 if (force_detach || !need_scaling) {
a1b2278e 4298 if (*scaler_id >= 0) {
86adf9d7 4299 scaler_state->scaler_users &= ~(1 << scaler_user);
a1b2278e
CK
4300 scaler_state->scalers[*scaler_id].in_use = 0;
4301
86adf9d7
ML
4302 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4303 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4304 intel_crtc->pipe, scaler_user, *scaler_id,
a1b2278e
CK
4305 scaler_state->scaler_users);
4306 *scaler_id = -1;
4307 }
4308 return 0;
4309 }
4310
4311 /* range checks */
4312 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4313 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4314
4315 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4316 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
86adf9d7 4317 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
a1b2278e 4318 "size is out of scaler range\n",
86adf9d7 4319 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
a1b2278e
CK
4320 return -EINVAL;
4321 }
4322
86adf9d7
ML
4323 /* mark this plane as a scaler user in crtc_state */
4324 scaler_state->scaler_users |= (1 << scaler_user);
4325 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4326 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4327 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4328 scaler_state->scaler_users);
4329
4330 return 0;
4331}
4332
4333/**
4334 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4335 *
4336 * @state: crtc's scaler state
4337 * @force_detach: whether to forcibly disable scaler
4338 *
4339 * Return
4340 * 0 - scaler_usage updated successfully
4341 * error - requested scaling cannot be supported or other error condition
4342 */
4343int skl_update_scaler_crtc(struct intel_crtc_state *state, int force_detach)
4344{
4345 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4346 struct drm_display_mode *adjusted_mode =
4347 &state->base.adjusted_mode;
4348
4349 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4350 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4351
4352 return skl_update_scaler(state, force_detach, SKL_CRTC_INDEX,
4353 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4354 state->pipe_src_w, state->pipe_src_h,
8c6cda29 4355 adjusted_mode->hdisplay, adjusted_mode->vdisplay);
86adf9d7
ML
4356}
4357
4358/**
4359 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4360 *
4361 * @state: crtc's scaler state
86adf9d7
ML
4362 * @plane_state: atomic plane state to update
4363 *
4364 * Return
4365 * 0 - scaler_usage updated successfully
4366 * error - requested scaling cannot be supported or other error condition
4367 */
da20eabd
ML
4368static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4369 struct intel_plane_state *plane_state)
86adf9d7
ML
4370{
4371
4372 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
da20eabd
ML
4373 struct intel_plane *intel_plane =
4374 to_intel_plane(plane_state->base.plane);
86adf9d7
ML
4375 struct drm_framebuffer *fb = plane_state->base.fb;
4376 int ret;
4377
4378 bool force_detach = !fb || !plane_state->visible;
4379
4380 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4381 intel_plane->base.base.id, intel_crtc->pipe,
4382 drm_plane_index(&intel_plane->base));
4383
4384 ret = skl_update_scaler(crtc_state, force_detach,
4385 drm_plane_index(&intel_plane->base),
4386 &plane_state->scaler_id,
4387 plane_state->base.rotation,
4388 drm_rect_width(&plane_state->src) >> 16,
4389 drm_rect_height(&plane_state->src) >> 16,
4390 drm_rect_width(&plane_state->dst),
4391 drm_rect_height(&plane_state->dst));
4392
4393 if (ret || plane_state->scaler_id < 0)
4394 return ret;
4395
a1b2278e 4396 /* check colorkey */
818ed961 4397 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
86adf9d7 4398 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
818ed961 4399 intel_plane->base.base.id);
a1b2278e
CK
4400 return -EINVAL;
4401 }
4402
4403 /* Check src format */
86adf9d7
ML
4404 switch (fb->pixel_format) {
4405 case DRM_FORMAT_RGB565:
4406 case DRM_FORMAT_XBGR8888:
4407 case DRM_FORMAT_XRGB8888:
4408 case DRM_FORMAT_ABGR8888:
4409 case DRM_FORMAT_ARGB8888:
4410 case DRM_FORMAT_XRGB2101010:
4411 case DRM_FORMAT_XBGR2101010:
4412 case DRM_FORMAT_YUYV:
4413 case DRM_FORMAT_YVYU:
4414 case DRM_FORMAT_UYVY:
4415 case DRM_FORMAT_VYUY:
4416 break;
4417 default:
4418 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4419 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4420 return -EINVAL;
a1b2278e
CK
4421 }
4422
a1b2278e
CK
4423 return 0;
4424}
4425
4426static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
bd2e244f
JB
4427{
4428 struct drm_device *dev = crtc->base.dev;
4429 struct drm_i915_private *dev_priv = dev->dev_private;
4430 int pipe = crtc->pipe;
a1b2278e
CK
4431 struct intel_crtc_scaler_state *scaler_state =
4432 &crtc->config->scaler_state;
4433
4434 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4435
4436 /* To update pfit, first update scaler state */
86adf9d7 4437 skl_update_scaler_crtc(crtc->config, !enable);
a1b2278e
CK
4438 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4439 skl_detach_scalers(crtc);
4440 if (!enable)
4441 return;
bd2e244f 4442
6e3c9717 4443 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4444 int id;
4445
4446 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4447 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4448 return;
4449 }
4450
4451 id = scaler_state->scaler_id;
4452 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4453 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4454 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4455 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4456
4457 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4458 }
4459}
4460
b074cec8
JB
4461static void ironlake_pfit_enable(struct intel_crtc *crtc)
4462{
4463 struct drm_device *dev = crtc->base.dev;
4464 struct drm_i915_private *dev_priv = dev->dev_private;
4465 int pipe = crtc->pipe;
4466
6e3c9717 4467 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4468 /* Force use of hard-coded filter coefficients
4469 * as some pre-programmed values are broken,
4470 * e.g. x201.
4471 */
4472 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4473 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4474 PF_PIPE_SEL_IVB(pipe));
4475 else
4476 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4477 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4478 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4479 }
4480}
4481
20bc8673 4482void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4483{
cea165c3
VS
4484 struct drm_device *dev = crtc->base.dev;
4485 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4486
6e3c9717 4487 if (!crtc->config->ips_enabled)
d77e4531
PZ
4488 return;
4489
cea165c3
VS
4490 /* We can only enable IPS after we enable a plane and wait for a vblank */
4491 intel_wait_for_vblank(dev, crtc->pipe);
4492
d77e4531 4493 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4494 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4495 mutex_lock(&dev_priv->rps.hw_lock);
4496 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4497 mutex_unlock(&dev_priv->rps.hw_lock);
4498 /* Quoting Art Runyan: "its not safe to expect any particular
4499 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4500 * mailbox." Moreover, the mailbox may return a bogus state,
4501 * so we need to just enable it and continue on.
2a114cc1
BW
4502 */
4503 } else {
4504 I915_WRITE(IPS_CTL, IPS_ENABLE);
4505 /* The bit only becomes 1 in the next vblank, so this wait here
4506 * is essentially intel_wait_for_vblank. If we don't have this
4507 * and don't wait for vblanks until the end of crtc_enable, then
4508 * the HW state readout code will complain that the expected
4509 * IPS_CTL value is not the one we read. */
4510 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4511 DRM_ERROR("Timed out waiting for IPS enable\n");
4512 }
d77e4531
PZ
4513}
4514
20bc8673 4515void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4516{
4517 struct drm_device *dev = crtc->base.dev;
4518 struct drm_i915_private *dev_priv = dev->dev_private;
4519
6e3c9717 4520 if (!crtc->config->ips_enabled)
d77e4531
PZ
4521 return;
4522
4523 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4524 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4525 mutex_lock(&dev_priv->rps.hw_lock);
4526 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4527 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4528 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4529 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4530 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4531 } else {
2a114cc1 4532 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4533 POSTING_READ(IPS_CTL);
4534 }
d77e4531
PZ
4535
4536 /* We need to wait for a vblank before we can disable the plane. */
4537 intel_wait_for_vblank(dev, crtc->pipe);
4538}
4539
4540/** Loads the palette/gamma unit for the CRTC with the prepared values */
4541static void intel_crtc_load_lut(struct drm_crtc *crtc)
4542{
4543 struct drm_device *dev = crtc->dev;
4544 struct drm_i915_private *dev_priv = dev->dev_private;
4545 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4546 enum pipe pipe = intel_crtc->pipe;
4547 int palreg = PALETTE(pipe);
4548 int i;
4549 bool reenable_ips = false;
4550
4551 /* The clocks have to be on to load the palette. */
53d9f4e9 4552 if (!crtc->state->active)
d77e4531
PZ
4553 return;
4554
50360403 4555 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
409ee761 4556 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4557 assert_dsi_pll_enabled(dev_priv);
4558 else
4559 assert_pll_enabled(dev_priv, pipe);
4560 }
4561
4562 /* use legacy palette for Ironlake */
7a1db49a 4563 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4564 palreg = LGC_PALETTE(pipe);
4565
4566 /* Workaround : Do not read or write the pipe palette/gamma data while
4567 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4568 */
6e3c9717 4569 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4570 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4571 GAMMA_MODE_MODE_SPLIT)) {
4572 hsw_disable_ips(intel_crtc);
4573 reenable_ips = true;
4574 }
4575
4576 for (i = 0; i < 256; i++) {
4577 I915_WRITE(palreg + 4 * i,
4578 (intel_crtc->lut_r[i] << 16) |
4579 (intel_crtc->lut_g[i] << 8) |
4580 intel_crtc->lut_b[i]);
4581 }
4582
4583 if (reenable_ips)
4584 hsw_enable_ips(intel_crtc);
4585}
4586
7cac945f 4587static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4588{
7cac945f 4589 if (intel_crtc->overlay) {
d3eedb1a
VS
4590 struct drm_device *dev = intel_crtc->base.dev;
4591 struct drm_i915_private *dev_priv = dev->dev_private;
4592
4593 mutex_lock(&dev->struct_mutex);
4594 dev_priv->mm.interruptible = false;
4595 (void) intel_overlay_switch_off(intel_crtc->overlay);
4596 dev_priv->mm.interruptible = true;
4597 mutex_unlock(&dev->struct_mutex);
4598 }
4599
4600 /* Let userspace switch the overlay on again. In most cases userspace
4601 * has to recompute where to put it anyway.
4602 */
4603}
4604
87d4300a
ML
4605/**
4606 * intel_post_enable_primary - Perform operations after enabling primary plane
4607 * @crtc: the CRTC whose primary plane was just enabled
4608 *
4609 * Performs potentially sleeping operations that must be done after the primary
4610 * plane is enabled, such as updating FBC and IPS. Note that this may be
4611 * called due to an explicit primary plane update, or due to an implicit
4612 * re-enable that is caused when a sprite plane is updated to no longer
4613 * completely hide the primary plane.
4614 */
4615static void
4616intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4617{
4618 struct drm_device *dev = crtc->dev;
87d4300a 4619 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4620 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4621 int pipe = intel_crtc->pipe;
a5c4d7bc 4622
87d4300a
ML
4623 /*
4624 * BDW signals flip done immediately if the plane
4625 * is disabled, even if the plane enable is already
4626 * armed to occur at the next vblank :(
4627 */
4628 if (IS_BROADWELL(dev))
4629 intel_wait_for_vblank(dev, pipe);
a5c4d7bc 4630
87d4300a
ML
4631 /*
4632 * FIXME IPS should be fine as long as one plane is
4633 * enabled, but in practice it seems to have problems
4634 * when going from primary only to sprite only and vice
4635 * versa.
4636 */
a5c4d7bc
VS
4637 hsw_enable_ips(intel_crtc);
4638
f99d7069 4639 /*
87d4300a
ML
4640 * Gen2 reports pipe underruns whenever all planes are disabled.
4641 * So don't enable underrun reporting before at least some planes
4642 * are enabled.
4643 * FIXME: Need to fix the logic to work when we turn off all planes
4644 * but leave the pipe running.
f99d7069 4645 */
87d4300a
ML
4646 if (IS_GEN2(dev))
4647 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4648
4649 /* Underruns don't raise interrupts, so check manually. */
4650 if (HAS_GMCH_DISPLAY(dev))
4651 i9xx_check_fifo_underruns(dev_priv);
a5c4d7bc
VS
4652}
4653
87d4300a
ML
4654/**
4655 * intel_pre_disable_primary - Perform operations before disabling primary plane
4656 * @crtc: the CRTC whose primary plane is to be disabled
4657 *
4658 * Performs potentially sleeping operations that must be done before the
4659 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4660 * be called due to an explicit primary plane update, or due to an implicit
4661 * disable that is caused when a sprite plane completely hides the primary
4662 * plane.
4663 */
4664static void
4665intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4666{
4667 struct drm_device *dev = crtc->dev;
4668 struct drm_i915_private *dev_priv = dev->dev_private;
4669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4670 int pipe = intel_crtc->pipe;
a5c4d7bc 4671
87d4300a
ML
4672 /*
4673 * Gen2 reports pipe underruns whenever all planes are disabled.
4674 * So diasble underrun reporting before all the planes get disabled.
4675 * FIXME: Need to fix the logic to work when we turn off all planes
4676 * but leave the pipe running.
4677 */
4678 if (IS_GEN2(dev))
4679 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4680
87d4300a
ML
4681 /*
4682 * Vblank time updates from the shadow to live plane control register
4683 * are blocked if the memory self-refresh mode is active at that
4684 * moment. So to make sure the plane gets truly disabled, disable
4685 * first the self-refresh mode. The self-refresh enable bit in turn
4686 * will be checked/applied by the HW only at the next frame start
4687 * event which is after the vblank start event, so we need to have a
4688 * wait-for-vblank between disabling the plane and the pipe.
4689 */
4690 if (HAS_GMCH_DISPLAY(dev))
4691 intel_set_memory_cxsr(dev_priv, false);
4692
87d4300a
ML
4693 /*
4694 * FIXME IPS should be fine as long as one plane is
4695 * enabled, but in practice it seems to have problems
4696 * when going from primary only to sprite only and vice
4697 * versa.
4698 */
a5c4d7bc 4699 hsw_disable_ips(intel_crtc);
87d4300a
ML
4700}
4701
ac21b225
ML
4702static void intel_post_plane_update(struct intel_crtc *crtc)
4703{
4704 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4705 struct drm_device *dev = crtc->base.dev;
4706 struct drm_plane *plane;
4707
4708 if (atomic->wait_vblank)
4709 intel_wait_for_vblank(dev, crtc->pipe);
4710
4711 intel_frontbuffer_flip(dev, atomic->fb_bits);
4712
4713 if (atomic->update_fbc) {
4714 mutex_lock(&dev->struct_mutex);
4715 intel_fbc_update(dev);
4716 mutex_unlock(&dev->struct_mutex);
4717 }
4718
4719 if (atomic->post_enable_primary)
4720 intel_post_enable_primary(&crtc->base);
4721
4722 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4723 intel_update_sprite_watermarks(plane, &crtc->base,
4724 0, 0, 0, false, false);
4725
4726 memset(atomic, 0, sizeof(*atomic));
4727}
4728
4729static void intel_pre_plane_update(struct intel_crtc *crtc)
4730{
4731 struct drm_device *dev = crtc->base.dev;
eddfcbcd 4732 struct drm_i915_private *dev_priv = dev->dev_private;
ac21b225
ML
4733 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4734 struct drm_plane *p;
4735
4736 /* Track fb's for any planes being disabled */
4737
4738 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4739 struct intel_plane *plane = to_intel_plane(p);
4740 unsigned fb_bits = 0;
4741
4742 switch (p->type) {
4743 case DRM_PLANE_TYPE_PRIMARY:
4744 fb_bits = INTEL_FRONTBUFFER_PRIMARY(plane->pipe);
4745 break;
4746 case DRM_PLANE_TYPE_CURSOR:
4747 fb_bits = INTEL_FRONTBUFFER_CURSOR(plane->pipe);
4748 break;
4749 case DRM_PLANE_TYPE_OVERLAY:
4750 fb_bits = INTEL_FRONTBUFFER_SPRITE(plane->pipe);
4751 break;
4752 }
4753
4754 mutex_lock(&dev->struct_mutex);
4755 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL, fb_bits);
4756 mutex_unlock(&dev->struct_mutex);
4757 }
4758
4759 if (atomic->wait_for_flips)
4760 intel_crtc_wait_for_pending_flips(&crtc->base);
4761
eddfcbcd
ML
4762 if (atomic->disable_fbc &&
4763 dev_priv->fbc.crtc == crtc) {
4764 mutex_lock(&dev->struct_mutex);
4765 if (dev_priv->fbc.crtc == crtc)
4766 intel_fbc_disable(dev);
4767 mutex_unlock(&dev->struct_mutex);
4768 }
ac21b225
ML
4769
4770 if (atomic->pre_disable_primary)
4771 intel_pre_disable_primary(&crtc->base);
4772}
4773
d032ffa0 4774static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
87d4300a
ML
4775{
4776 struct drm_device *dev = crtc->dev;
4777 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
d032ffa0 4778 struct drm_plane *p;
87d4300a
ML
4779 int pipe = intel_crtc->pipe;
4780
7cac945f 4781 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8 4782
d032ffa0
ML
4783 drm_for_each_plane_mask(p, dev, plane_mask)
4784 to_intel_plane(p)->disable_plane(p, crtc);
f98551ae 4785
f99d7069
DV
4786 /*
4787 * FIXME: Once we grow proper nuclear flip support out of this we need
4788 * to compute the mask of flip planes precisely. For the time being
4789 * consider this a flip to a NULL plane.
4790 */
4791 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4792}
4793
f67a559d
JB
4794static void ironlake_crtc_enable(struct drm_crtc *crtc)
4795{
4796 struct drm_device *dev = crtc->dev;
4797 struct drm_i915_private *dev_priv = dev->dev_private;
4798 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4799 struct intel_encoder *encoder;
f67a559d 4800 int pipe = intel_crtc->pipe;
f67a559d 4801
53d9f4e9 4802 if (WARN_ON(intel_crtc->active))
f67a559d
JB
4803 return;
4804
6e3c9717 4805 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4806 intel_prepare_shared_dpll(intel_crtc);
4807
6e3c9717 4808 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4809 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4810
4811 intel_set_pipe_timings(intel_crtc);
4812
6e3c9717 4813 if (intel_crtc->config->has_pch_encoder) {
29407aab 4814 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4815 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4816 }
4817
4818 ironlake_set_pipeconf(crtc);
4819
f67a559d 4820 intel_crtc->active = true;
8664281b 4821
a72e4c9f
DV
4822 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4823 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4824
f6736a1a 4825 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4826 if (encoder->pre_enable)
4827 encoder->pre_enable(encoder);
f67a559d 4828
6e3c9717 4829 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4830 /* Note: FDI PLL enabling _must_ be done before we enable the
4831 * cpu pipes, hence this is separate from all the other fdi/pch
4832 * enabling. */
88cefb6c 4833 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4834 } else {
4835 assert_fdi_tx_disabled(dev_priv, pipe);
4836 assert_fdi_rx_disabled(dev_priv, pipe);
4837 }
f67a559d 4838
b074cec8 4839 ironlake_pfit_enable(intel_crtc);
f67a559d 4840
9c54c0dd
JB
4841 /*
4842 * On ILK+ LUT must be loaded before the pipe is running but with
4843 * clocks enabled
4844 */
4845 intel_crtc_load_lut(crtc);
4846
f37fcc2a 4847 intel_update_watermarks(crtc);
e1fdc473 4848 intel_enable_pipe(intel_crtc);
f67a559d 4849
6e3c9717 4850 if (intel_crtc->config->has_pch_encoder)
f67a559d 4851 ironlake_pch_enable(crtc);
c98e9dcf 4852
f9b61ff6
DV
4853 assert_vblank_disabled(crtc);
4854 drm_crtc_vblank_on(crtc);
4855
fa5c73b1
DV
4856 for_each_encoder_on_crtc(dev, crtc, encoder)
4857 encoder->enable(encoder);
61b77ddd
DV
4858
4859 if (HAS_PCH_CPT(dev))
a1520318 4860 cpt_verify_modeset(dev, intel_crtc->pipe);
6be4a607
JB
4861}
4862
42db64ef
PZ
4863/* IPS only exists on ULT machines and is tied to pipe A. */
4864static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4865{
f5adf94e 4866 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4867}
4868
4f771f10
PZ
4869static void haswell_crtc_enable(struct drm_crtc *crtc)
4870{
4871 struct drm_device *dev = crtc->dev;
4872 struct drm_i915_private *dev_priv = dev->dev_private;
4873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4874 struct intel_encoder *encoder;
99d736a2
ML
4875 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4876 struct intel_crtc_state *pipe_config =
4877 to_intel_crtc_state(crtc->state);
4f771f10 4878
53d9f4e9 4879 if (WARN_ON(intel_crtc->active))
4f771f10
PZ
4880 return;
4881
df8ad70c
DV
4882 if (intel_crtc_to_shared_dpll(intel_crtc))
4883 intel_enable_shared_dpll(intel_crtc);
4884
6e3c9717 4885 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4886 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
4887
4888 intel_set_pipe_timings(intel_crtc);
4889
6e3c9717
ACO
4890 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4891 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4892 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
4893 }
4894
6e3c9717 4895 if (intel_crtc->config->has_pch_encoder) {
229fca97 4896 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4897 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
4898 }
4899
4900 haswell_set_pipeconf(crtc);
4901
4902 intel_set_pipe_csc(crtc);
4903
4f771f10 4904 intel_crtc->active = true;
8664281b 4905
a72e4c9f 4906 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
4907 for_each_encoder_on_crtc(dev, crtc, encoder)
4908 if (encoder->pre_enable)
4909 encoder->pre_enable(encoder);
4910
6e3c9717 4911 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
4912 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4913 true);
4fe9467d
ID
4914 dev_priv->display.fdi_link_train(crtc);
4915 }
4916
1f544388 4917 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 4918
ff6d9f55 4919 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 4920 skylake_pfit_update(intel_crtc, 1);
ff6d9f55 4921 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 4922 ironlake_pfit_enable(intel_crtc);
ff6d9f55
JB
4923 else
4924 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10
PZ
4925
4926 /*
4927 * On ILK+ LUT must be loaded before the pipe is running but with
4928 * clocks enabled
4929 */
4930 intel_crtc_load_lut(crtc);
4931
1f544388 4932 intel_ddi_set_pipe_settings(crtc);
8228c251 4933 intel_ddi_enable_transcoder_func(crtc);
4f771f10 4934
f37fcc2a 4935 intel_update_watermarks(crtc);
e1fdc473 4936 intel_enable_pipe(intel_crtc);
42db64ef 4937
6e3c9717 4938 if (intel_crtc->config->has_pch_encoder)
1507e5bd 4939 lpt_pch_enable(crtc);
4f771f10 4940
6e3c9717 4941 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
4942 intel_ddi_set_vc_payload_alloc(crtc, true);
4943
f9b61ff6
DV
4944 assert_vblank_disabled(crtc);
4945 drm_crtc_vblank_on(crtc);
4946
8807e55b 4947 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 4948 encoder->enable(encoder);
8807e55b
JN
4949 intel_opregion_notify_encoder(encoder, true);
4950 }
4f771f10 4951
e4916946
PZ
4952 /* If we change the relative order between pipe/planes enabling, we need
4953 * to change the workaround. */
99d736a2
ML
4954 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4955 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4956 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4957 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4958 }
4f771f10
PZ
4959}
4960
3f8dce3a
DV
4961static void ironlake_pfit_disable(struct intel_crtc *crtc)
4962{
4963 struct drm_device *dev = crtc->base.dev;
4964 struct drm_i915_private *dev_priv = dev->dev_private;
4965 int pipe = crtc->pipe;
4966
4967 /* To avoid upsetting the power well on haswell only disable the pfit if
4968 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 4969 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
4970 I915_WRITE(PF_CTL(pipe), 0);
4971 I915_WRITE(PF_WIN_POS(pipe), 0);
4972 I915_WRITE(PF_WIN_SZ(pipe), 0);
4973 }
4974}
4975
6be4a607
JB
4976static void ironlake_crtc_disable(struct drm_crtc *crtc)
4977{
4978 struct drm_device *dev = crtc->dev;
4979 struct drm_i915_private *dev_priv = dev->dev_private;
4980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4981 struct intel_encoder *encoder;
6be4a607 4982 int pipe = intel_crtc->pipe;
5eddb70b 4983 u32 reg, temp;
b52eb4dc 4984
ea9d758d
DV
4985 for_each_encoder_on_crtc(dev, crtc, encoder)
4986 encoder->disable(encoder);
4987
f9b61ff6
DV
4988 drm_crtc_vblank_off(crtc);
4989 assert_vblank_disabled(crtc);
4990
6e3c9717 4991 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 4992 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 4993
575f7ab7 4994 intel_disable_pipe(intel_crtc);
32f9d658 4995
3f8dce3a 4996 ironlake_pfit_disable(intel_crtc);
2c07245f 4997
5a74f70a
VS
4998 if (intel_crtc->config->has_pch_encoder)
4999 ironlake_fdi_disable(crtc);
5000
bf49ec8c
DV
5001 for_each_encoder_on_crtc(dev, crtc, encoder)
5002 if (encoder->post_disable)
5003 encoder->post_disable(encoder);
2c07245f 5004
6e3c9717 5005 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5006 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5007
d925c59a
DV
5008 if (HAS_PCH_CPT(dev)) {
5009 /* disable TRANS_DP_CTL */
5010 reg = TRANS_DP_CTL(pipe);
5011 temp = I915_READ(reg);
5012 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5013 TRANS_DP_PORT_SEL_MASK);
5014 temp |= TRANS_DP_PORT_SEL_NONE;
5015 I915_WRITE(reg, temp);
5016
5017 /* disable DPLL_SEL */
5018 temp = I915_READ(PCH_DPLL_SEL);
11887397 5019 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5020 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5021 }
e3421a18 5022
d925c59a
DV
5023 ironlake_fdi_pll_disable(intel_crtc);
5024 }
6be4a607 5025}
1b3c7a47 5026
4f771f10 5027static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5028{
4f771f10
PZ
5029 struct drm_device *dev = crtc->dev;
5030 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5032 struct intel_encoder *encoder;
6e3c9717 5033 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5034
8807e55b
JN
5035 for_each_encoder_on_crtc(dev, crtc, encoder) {
5036 intel_opregion_notify_encoder(encoder, false);
4f771f10 5037 encoder->disable(encoder);
8807e55b 5038 }
4f771f10 5039
f9b61ff6
DV
5040 drm_crtc_vblank_off(crtc);
5041 assert_vblank_disabled(crtc);
5042
6e3c9717 5043 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
5044 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5045 false);
575f7ab7 5046 intel_disable_pipe(intel_crtc);
4f771f10 5047
6e3c9717 5048 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5049 intel_ddi_set_vc_payload_alloc(crtc, false);
5050
ad80a810 5051 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5052
ff6d9f55 5053 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5054 skylake_pfit_update(intel_crtc, 0);
ff6d9f55 5055 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5056 ironlake_pfit_disable(intel_crtc);
ff6d9f55
JB
5057 else
5058 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10 5059
1f544388 5060 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5061
6e3c9717 5062 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 5063 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 5064 intel_ddi_fdi_disable(crtc);
83616634 5065 }
4f771f10 5066
97b040aa
ID
5067 for_each_encoder_on_crtc(dev, crtc, encoder)
5068 if (encoder->post_disable)
5069 encoder->post_disable(encoder);
4f771f10
PZ
5070}
5071
2dd24552
JB
5072static void i9xx_pfit_enable(struct intel_crtc *crtc)
5073{
5074 struct drm_device *dev = crtc->base.dev;
5075 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5076 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5077
681a8504 5078 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5079 return;
5080
2dd24552 5081 /*
c0b03411
DV
5082 * The panel fitter should only be adjusted whilst the pipe is disabled,
5083 * according to register description and PRM.
2dd24552 5084 */
c0b03411
DV
5085 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5086 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5087
b074cec8
JB
5088 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5089 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5090
5091 /* Border color in case we don't scale up to the full screen. Black by
5092 * default, change to something else for debugging. */
5093 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5094}
5095
d05410f9
DA
5096static enum intel_display_power_domain port_to_power_domain(enum port port)
5097{
5098 switch (port) {
5099 case PORT_A:
5100 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5101 case PORT_B:
5102 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5103 case PORT_C:
5104 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5105 case PORT_D:
5106 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5107 default:
5108 WARN_ON_ONCE(1);
5109 return POWER_DOMAIN_PORT_OTHER;
5110 }
5111}
5112
77d22dca
ID
5113#define for_each_power_domain(domain, mask) \
5114 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5115 if ((1 << (domain)) & (mask))
5116
319be8ae
ID
5117enum intel_display_power_domain
5118intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5119{
5120 struct drm_device *dev = intel_encoder->base.dev;
5121 struct intel_digital_port *intel_dig_port;
5122
5123 switch (intel_encoder->type) {
5124 case INTEL_OUTPUT_UNKNOWN:
5125 /* Only DDI platforms should ever use this output type */
5126 WARN_ON_ONCE(!HAS_DDI(dev));
5127 case INTEL_OUTPUT_DISPLAYPORT:
5128 case INTEL_OUTPUT_HDMI:
5129 case INTEL_OUTPUT_EDP:
5130 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5131 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5132 case INTEL_OUTPUT_DP_MST:
5133 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5134 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5135 case INTEL_OUTPUT_ANALOG:
5136 return POWER_DOMAIN_PORT_CRT;
5137 case INTEL_OUTPUT_DSI:
5138 return POWER_DOMAIN_PORT_DSI;
5139 default:
5140 return POWER_DOMAIN_PORT_OTHER;
5141 }
5142}
5143
5144static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5145{
319be8ae
ID
5146 struct drm_device *dev = crtc->dev;
5147 struct intel_encoder *intel_encoder;
5148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5149 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
5150 unsigned long mask;
5151 enum transcoder transcoder;
5152
5153 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5154
5155 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5156 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
5157 if (intel_crtc->config->pch_pfit.enabled ||
5158 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
5159 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5160
319be8ae
ID
5161 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5162 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5163
77d22dca
ID
5164 return mask;
5165}
5166
679dacd4 5167static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
77d22dca 5168{
679dacd4 5169 struct drm_device *dev = state->dev;
77d22dca
ID
5170 struct drm_i915_private *dev_priv = dev->dev_private;
5171 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5172 struct intel_crtc *crtc;
5173
5174 /*
5175 * First get all needed power domains, then put all unneeded, to avoid
5176 * any unnecessary toggling of the power wells.
5177 */
d3fcc808 5178 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5179 enum intel_display_power_domain domain;
5180
83d65738 5181 if (!crtc->base.state->enable)
77d22dca
ID
5182 continue;
5183
319be8ae 5184 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
5185
5186 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5187 intel_display_power_get(dev_priv, domain);
5188 }
5189
27c329ed
ML
5190 if (dev_priv->display.modeset_commit_cdclk) {
5191 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5192
5193 if (cdclk != dev_priv->cdclk_freq &&
5194 !WARN_ON(!state->allow_modeset))
5195 dev_priv->display.modeset_commit_cdclk(state);
5196 }
50f6e502 5197
d3fcc808 5198 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5199 enum intel_display_power_domain domain;
5200
5201 for_each_power_domain(domain, crtc->enabled_power_domains)
5202 intel_display_power_put(dev_priv, domain);
5203
5204 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5205 }
5206
5207 intel_display_set_init_power(dev_priv, false);
5208}
5209
560a7ae4
DL
5210static void intel_update_max_cdclk(struct drm_device *dev)
5211{
5212 struct drm_i915_private *dev_priv = dev->dev_private;
5213
5214 if (IS_SKYLAKE(dev)) {
5215 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5216
5217 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5218 dev_priv->max_cdclk_freq = 675000;
5219 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5220 dev_priv->max_cdclk_freq = 540000;
5221 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5222 dev_priv->max_cdclk_freq = 450000;
5223 else
5224 dev_priv->max_cdclk_freq = 337500;
5225 } else if (IS_BROADWELL(dev)) {
5226 /*
5227 * FIXME with extra cooling we can allow
5228 * 540 MHz for ULX and 675 Mhz for ULT.
5229 * How can we know if extra cooling is
5230 * available? PCI ID, VTB, something else?
5231 */
5232 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5233 dev_priv->max_cdclk_freq = 450000;
5234 else if (IS_BDW_ULX(dev))
5235 dev_priv->max_cdclk_freq = 450000;
5236 else if (IS_BDW_ULT(dev))
5237 dev_priv->max_cdclk_freq = 540000;
5238 else
5239 dev_priv->max_cdclk_freq = 675000;
0904deaf
MK
5240 } else if (IS_CHERRYVIEW(dev)) {
5241 dev_priv->max_cdclk_freq = 320000;
560a7ae4
DL
5242 } else if (IS_VALLEYVIEW(dev)) {
5243 dev_priv->max_cdclk_freq = 400000;
5244 } else {
5245 /* otherwise assume cdclk is fixed */
5246 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5247 }
5248
5249 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5250 dev_priv->max_cdclk_freq);
5251}
5252
5253static void intel_update_cdclk(struct drm_device *dev)
5254{
5255 struct drm_i915_private *dev_priv = dev->dev_private;
5256
5257 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5258 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5259 dev_priv->cdclk_freq);
5260
5261 /*
5262 * Program the gmbus_freq based on the cdclk frequency.
5263 * BSpec erroneously claims we should aim for 4MHz, but
5264 * in fact 1MHz is the correct frequency.
5265 */
5266 if (IS_VALLEYVIEW(dev)) {
5267 /*
5268 * Program the gmbus_freq based on the cdclk frequency.
5269 * BSpec erroneously claims we should aim for 4MHz, but
5270 * in fact 1MHz is the correct frequency.
5271 */
5272 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5273 }
5274
5275 if (dev_priv->max_cdclk_freq == 0)
5276 intel_update_max_cdclk(dev);
5277}
5278
70d0c574 5279static void broxton_set_cdclk(struct drm_device *dev, int frequency)
f8437dd1
VK
5280{
5281 struct drm_i915_private *dev_priv = dev->dev_private;
5282 uint32_t divider;
5283 uint32_t ratio;
5284 uint32_t current_freq;
5285 int ret;
5286
5287 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5288 switch (frequency) {
5289 case 144000:
5290 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5291 ratio = BXT_DE_PLL_RATIO(60);
5292 break;
5293 case 288000:
5294 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5295 ratio = BXT_DE_PLL_RATIO(60);
5296 break;
5297 case 384000:
5298 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5299 ratio = BXT_DE_PLL_RATIO(60);
5300 break;
5301 case 576000:
5302 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5303 ratio = BXT_DE_PLL_RATIO(60);
5304 break;
5305 case 624000:
5306 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5307 ratio = BXT_DE_PLL_RATIO(65);
5308 break;
5309 case 19200:
5310 /*
5311 * Bypass frequency with DE PLL disabled. Init ratio, divider
5312 * to suppress GCC warning.
5313 */
5314 ratio = 0;
5315 divider = 0;
5316 break;
5317 default:
5318 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5319
5320 return;
5321 }
5322
5323 mutex_lock(&dev_priv->rps.hw_lock);
5324 /* Inform power controller of upcoming frequency change */
5325 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5326 0x80000000);
5327 mutex_unlock(&dev_priv->rps.hw_lock);
5328
5329 if (ret) {
5330 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5331 ret, frequency);
5332 return;
5333 }
5334
5335 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5336 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5337 current_freq = current_freq * 500 + 1000;
5338
5339 /*
5340 * DE PLL has to be disabled when
5341 * - setting to 19.2MHz (bypass, PLL isn't used)
5342 * - before setting to 624MHz (PLL needs toggling)
5343 * - before setting to any frequency from 624MHz (PLL needs toggling)
5344 */
5345 if (frequency == 19200 || frequency == 624000 ||
5346 current_freq == 624000) {
5347 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5348 /* Timeout 200us */
5349 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5350 1))
5351 DRM_ERROR("timout waiting for DE PLL unlock\n");
5352 }
5353
5354 if (frequency != 19200) {
5355 uint32_t val;
5356
5357 val = I915_READ(BXT_DE_PLL_CTL);
5358 val &= ~BXT_DE_PLL_RATIO_MASK;
5359 val |= ratio;
5360 I915_WRITE(BXT_DE_PLL_CTL, val);
5361
5362 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5363 /* Timeout 200us */
5364 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5365 DRM_ERROR("timeout waiting for DE PLL lock\n");
5366
5367 val = I915_READ(CDCLK_CTL);
5368 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5369 val |= divider;
5370 /*
5371 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5372 * enable otherwise.
5373 */
5374 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5375 if (frequency >= 500000)
5376 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5377
5378 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5379 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5380 val |= (frequency - 1000) / 500;
5381 I915_WRITE(CDCLK_CTL, val);
5382 }
5383
5384 mutex_lock(&dev_priv->rps.hw_lock);
5385 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5386 DIV_ROUND_UP(frequency, 25000));
5387 mutex_unlock(&dev_priv->rps.hw_lock);
5388
5389 if (ret) {
5390 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5391 ret, frequency);
5392 return;
5393 }
5394
a47871bd 5395 intel_update_cdclk(dev);
f8437dd1
VK
5396}
5397
5398void broxton_init_cdclk(struct drm_device *dev)
5399{
5400 struct drm_i915_private *dev_priv = dev->dev_private;
5401 uint32_t val;
5402
5403 /*
5404 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5405 * or else the reset will hang because there is no PCH to respond.
5406 * Move the handshake programming to initialization sequence.
5407 * Previously was left up to BIOS.
5408 */
5409 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5410 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5411 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5412
5413 /* Enable PG1 for cdclk */
5414 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5415
5416 /* check if cd clock is enabled */
5417 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5418 DRM_DEBUG_KMS("Display already initialized\n");
5419 return;
5420 }
5421
5422 /*
5423 * FIXME:
5424 * - The initial CDCLK needs to be read from VBT.
5425 * Need to make this change after VBT has changes for BXT.
5426 * - check if setting the max (or any) cdclk freq is really necessary
5427 * here, it belongs to modeset time
5428 */
5429 broxton_set_cdclk(dev, 624000);
5430
5431 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5432 POSTING_READ(DBUF_CTL);
5433
f8437dd1
VK
5434 udelay(10);
5435
5436 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5437 DRM_ERROR("DBuf power enable timeout!\n");
5438}
5439
5440void broxton_uninit_cdclk(struct drm_device *dev)
5441{
5442 struct drm_i915_private *dev_priv = dev->dev_private;
5443
5444 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5445 POSTING_READ(DBUF_CTL);
5446
f8437dd1
VK
5447 udelay(10);
5448
5449 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5450 DRM_ERROR("DBuf power disable timeout!\n");
5451
5452 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5453 broxton_set_cdclk(dev, 19200);
5454
5455 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5456}
5457
5d96d8af
DL
5458static const struct skl_cdclk_entry {
5459 unsigned int freq;
5460 unsigned int vco;
5461} skl_cdclk_frequencies[] = {
5462 { .freq = 308570, .vco = 8640 },
5463 { .freq = 337500, .vco = 8100 },
5464 { .freq = 432000, .vco = 8640 },
5465 { .freq = 450000, .vco = 8100 },
5466 { .freq = 540000, .vco = 8100 },
5467 { .freq = 617140, .vco = 8640 },
5468 { .freq = 675000, .vco = 8100 },
5469};
5470
5471static unsigned int skl_cdclk_decimal(unsigned int freq)
5472{
5473 return (freq - 1000) / 500;
5474}
5475
5476static unsigned int skl_cdclk_get_vco(unsigned int freq)
5477{
5478 unsigned int i;
5479
5480 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5481 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5482
5483 if (e->freq == freq)
5484 return e->vco;
5485 }
5486
5487 return 8100;
5488}
5489
5490static void
5491skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5492{
5493 unsigned int min_freq;
5494 u32 val;
5495
5496 /* select the minimum CDCLK before enabling DPLL 0 */
5497 val = I915_READ(CDCLK_CTL);
5498 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5499 val |= CDCLK_FREQ_337_308;
5500
5501 if (required_vco == 8640)
5502 min_freq = 308570;
5503 else
5504 min_freq = 337500;
5505
5506 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5507
5508 I915_WRITE(CDCLK_CTL, val);
5509 POSTING_READ(CDCLK_CTL);
5510
5511 /*
5512 * We always enable DPLL0 with the lowest link rate possible, but still
5513 * taking into account the VCO required to operate the eDP panel at the
5514 * desired frequency. The usual DP link rates operate with a VCO of
5515 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5516 * The modeset code is responsible for the selection of the exact link
5517 * rate later on, with the constraint of choosing a frequency that
5518 * works with required_vco.
5519 */
5520 val = I915_READ(DPLL_CTRL1);
5521
5522 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5523 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5524 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5525 if (required_vco == 8640)
5526 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5527 SKL_DPLL0);
5528 else
5529 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5530 SKL_DPLL0);
5531
5532 I915_WRITE(DPLL_CTRL1, val);
5533 POSTING_READ(DPLL_CTRL1);
5534
5535 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5536
5537 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5538 DRM_ERROR("DPLL0 not locked\n");
5539}
5540
5541static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5542{
5543 int ret;
5544 u32 val;
5545
5546 /* inform PCU we want to change CDCLK */
5547 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5548 mutex_lock(&dev_priv->rps.hw_lock);
5549 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5550 mutex_unlock(&dev_priv->rps.hw_lock);
5551
5552 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5553}
5554
5555static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5556{
5557 unsigned int i;
5558
5559 for (i = 0; i < 15; i++) {
5560 if (skl_cdclk_pcu_ready(dev_priv))
5561 return true;
5562 udelay(10);
5563 }
5564
5565 return false;
5566}
5567
5568static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5569{
560a7ae4 5570 struct drm_device *dev = dev_priv->dev;
5d96d8af
DL
5571 u32 freq_select, pcu_ack;
5572
5573 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5574
5575 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5576 DRM_ERROR("failed to inform PCU about cdclk change\n");
5577 return;
5578 }
5579
5580 /* set CDCLK_CTL */
5581 switch(freq) {
5582 case 450000:
5583 case 432000:
5584 freq_select = CDCLK_FREQ_450_432;
5585 pcu_ack = 1;
5586 break;
5587 case 540000:
5588 freq_select = CDCLK_FREQ_540;
5589 pcu_ack = 2;
5590 break;
5591 case 308570:
5592 case 337500:
5593 default:
5594 freq_select = CDCLK_FREQ_337_308;
5595 pcu_ack = 0;
5596 break;
5597 case 617140:
5598 case 675000:
5599 freq_select = CDCLK_FREQ_675_617;
5600 pcu_ack = 3;
5601 break;
5602 }
5603
5604 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5605 POSTING_READ(CDCLK_CTL);
5606
5607 /* inform PCU of the change */
5608 mutex_lock(&dev_priv->rps.hw_lock);
5609 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5610 mutex_unlock(&dev_priv->rps.hw_lock);
560a7ae4
DL
5611
5612 intel_update_cdclk(dev);
5d96d8af
DL
5613}
5614
5615void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5616{
5617 /* disable DBUF power */
5618 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5619 POSTING_READ(DBUF_CTL);
5620
5621 udelay(10);
5622
5623 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5624 DRM_ERROR("DBuf power disable timeout\n");
5625
5626 /* disable DPLL0 */
5627 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5628 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5629 DRM_ERROR("Couldn't disable DPLL0\n");
5630
5631 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5632}
5633
5634void skl_init_cdclk(struct drm_i915_private *dev_priv)
5635{
5636 u32 val;
5637 unsigned int required_vco;
5638
5639 /* enable PCH reset handshake */
5640 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5641 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5642
5643 /* enable PG1 and Misc I/O */
5644 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5645
5646 /* DPLL0 already enabed !? */
5647 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5648 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5649 return;
5650 }
5651
5652 /* enable DPLL0 */
5653 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5654 skl_dpll0_enable(dev_priv, required_vco);
5655
5656 /* set CDCLK to the frequency the BIOS chose */
5657 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5658
5659 /* enable DBUF power */
5660 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5661 POSTING_READ(DBUF_CTL);
5662
5663 udelay(10);
5664
5665 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5666 DRM_ERROR("DBuf power enable timeout\n");
5667}
5668
dfcab17e 5669/* returns HPLL frequency in kHz */
f8bf63fd 5670static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 5671{
586f49dc 5672 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 5673
586f49dc 5674 /* Obtain SKU information */
a580516d 5675 mutex_lock(&dev_priv->sb_lock);
586f49dc
JB
5676 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5677 CCK_FUSE_HPLL_FREQ_MASK;
a580516d 5678 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5679
dfcab17e 5680 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
5681}
5682
5683/* Adjust CDclk dividers to allow high res or save power if possible */
5684static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5685{
5686 struct drm_i915_private *dev_priv = dev->dev_private;
5687 u32 val, cmd;
5688
164dfd28
VK
5689 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5690 != dev_priv->cdclk_freq);
d60c4473 5691
dfcab17e 5692 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5693 cmd = 2;
dfcab17e 5694 else if (cdclk == 266667)
30a970c6
JB
5695 cmd = 1;
5696 else
5697 cmd = 0;
5698
5699 mutex_lock(&dev_priv->rps.hw_lock);
5700 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5701 val &= ~DSPFREQGUAR_MASK;
5702 val |= (cmd << DSPFREQGUAR_SHIFT);
5703 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5704 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5705 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5706 50)) {
5707 DRM_ERROR("timed out waiting for CDclk change\n");
5708 }
5709 mutex_unlock(&dev_priv->rps.hw_lock);
5710
54433e91
VS
5711 mutex_lock(&dev_priv->sb_lock);
5712
dfcab17e 5713 if (cdclk == 400000) {
6bcda4f0 5714 u32 divider;
30a970c6 5715
6bcda4f0 5716 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6 5717
30a970c6
JB
5718 /* adjust cdclk divider */
5719 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 5720 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
5721 val |= divider;
5722 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5723
5724 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5725 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5726 50))
5727 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5728 }
5729
30a970c6
JB
5730 /* adjust self-refresh exit latency value */
5731 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5732 val &= ~0x7f;
5733
5734 /*
5735 * For high bandwidth configs, we set a higher latency in the bunit
5736 * so that the core display fetch happens in time to avoid underruns.
5737 */
dfcab17e 5738 if (cdclk == 400000)
30a970c6
JB
5739 val |= 4500 / 250; /* 4.5 usec */
5740 else
5741 val |= 3000 / 250; /* 3.0 usec */
5742 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
54433e91 5743
a580516d 5744 mutex_unlock(&dev_priv->sb_lock);
30a970c6 5745
b6283055 5746 intel_update_cdclk(dev);
30a970c6
JB
5747}
5748
383c5a6a
VS
5749static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5750{
5751 struct drm_i915_private *dev_priv = dev->dev_private;
5752 u32 val, cmd;
5753
164dfd28
VK
5754 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5755 != dev_priv->cdclk_freq);
383c5a6a
VS
5756
5757 switch (cdclk) {
383c5a6a
VS
5758 case 333333:
5759 case 320000:
383c5a6a 5760 case 266667:
383c5a6a 5761 case 200000:
383c5a6a
VS
5762 break;
5763 default:
5f77eeb0 5764 MISSING_CASE(cdclk);
383c5a6a
VS
5765 return;
5766 }
5767
9d0d3fda
VS
5768 /*
5769 * Specs are full of misinformation, but testing on actual
5770 * hardware has shown that we just need to write the desired
5771 * CCK divider into the Punit register.
5772 */
5773 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5774
383c5a6a
VS
5775 mutex_lock(&dev_priv->rps.hw_lock);
5776 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5777 val &= ~DSPFREQGUAR_MASK_CHV;
5778 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5779 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5780 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5781 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5782 50)) {
5783 DRM_ERROR("timed out waiting for CDclk change\n");
5784 }
5785 mutex_unlock(&dev_priv->rps.hw_lock);
5786
b6283055 5787 intel_update_cdclk(dev);
383c5a6a
VS
5788}
5789
30a970c6
JB
5790static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5791 int max_pixclk)
5792{
6bcda4f0 5793 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5794 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5795
30a970c6
JB
5796 /*
5797 * Really only a few cases to deal with, as only 4 CDclks are supported:
5798 * 200MHz
5799 * 267MHz
29dc7ef3 5800 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5801 * 400MHz (VLV only)
5802 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5803 * of the lower bin and adjust if needed.
e37c67a1
VS
5804 *
5805 * We seem to get an unstable or solid color picture at 200MHz.
5806 * Not sure what's wrong. For now use 200MHz only when all pipes
5807 * are off.
30a970c6 5808 */
6cca3195
VS
5809 if (!IS_CHERRYVIEW(dev_priv) &&
5810 max_pixclk > freq_320*limit/100)
dfcab17e 5811 return 400000;
6cca3195 5812 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5813 return freq_320;
e37c67a1 5814 else if (max_pixclk > 0)
dfcab17e 5815 return 266667;
e37c67a1
VS
5816 else
5817 return 200000;
30a970c6
JB
5818}
5819
f8437dd1
VK
5820static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5821 int max_pixclk)
5822{
5823 /*
5824 * FIXME:
5825 * - remove the guardband, it's not needed on BXT
5826 * - set 19.2MHz bypass frequency if there are no active pipes
5827 */
5828 if (max_pixclk > 576000*9/10)
5829 return 624000;
5830 else if (max_pixclk > 384000*9/10)
5831 return 576000;
5832 else if (max_pixclk > 288000*9/10)
5833 return 384000;
5834 else if (max_pixclk > 144000*9/10)
5835 return 288000;
5836 else
5837 return 144000;
5838}
5839
a821fc46
ACO
5840/* Compute the max pixel clock for new configuration. Uses atomic state if
5841 * that's non-NULL, look at current state otherwise. */
5842static int intel_mode_max_pixclk(struct drm_device *dev,
5843 struct drm_atomic_state *state)
30a970c6 5844{
30a970c6 5845 struct intel_crtc *intel_crtc;
304603f4 5846 struct intel_crtc_state *crtc_state;
30a970c6
JB
5847 int max_pixclk = 0;
5848
d3fcc808 5849 for_each_intel_crtc(dev, intel_crtc) {
27c329ed 5850 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
304603f4
ACO
5851 if (IS_ERR(crtc_state))
5852 return PTR_ERR(crtc_state);
5853
5854 if (!crtc_state->base.enable)
5855 continue;
5856
5857 max_pixclk = max(max_pixclk,
5858 crtc_state->base.adjusted_mode.crtc_clock);
30a970c6
JB
5859 }
5860
5861 return max_pixclk;
5862}
5863
27c329ed 5864static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
30a970c6 5865{
27c329ed
ML
5866 struct drm_device *dev = state->dev;
5867 struct drm_i915_private *dev_priv = dev->dev_private;
5868 int max_pixclk = intel_mode_max_pixclk(dev, state);
30a970c6 5869
304603f4
ACO
5870 if (max_pixclk < 0)
5871 return max_pixclk;
30a970c6 5872
27c329ed
ML
5873 to_intel_atomic_state(state)->cdclk =
5874 valleyview_calc_cdclk(dev_priv, max_pixclk);
0a9ab303 5875
27c329ed
ML
5876 return 0;
5877}
304603f4 5878
27c329ed
ML
5879static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5880{
5881 struct drm_device *dev = state->dev;
5882 struct drm_i915_private *dev_priv = dev->dev_private;
5883 int max_pixclk = intel_mode_max_pixclk(dev, state);
85a96e7a 5884
27c329ed
ML
5885 if (max_pixclk < 0)
5886 return max_pixclk;
85a96e7a 5887
27c329ed
ML
5888 to_intel_atomic_state(state)->cdclk =
5889 broxton_calc_cdclk(dev_priv, max_pixclk);
85a96e7a 5890
27c329ed 5891 return 0;
30a970c6
JB
5892}
5893
1e69cd74
VS
5894static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5895{
5896 unsigned int credits, default_credits;
5897
5898 if (IS_CHERRYVIEW(dev_priv))
5899 default_credits = PFI_CREDIT(12);
5900 else
5901 default_credits = PFI_CREDIT(8);
5902
164dfd28 5903 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
1e69cd74
VS
5904 /* CHV suggested value is 31 or 63 */
5905 if (IS_CHERRYVIEW(dev_priv))
fcc0008f 5906 credits = PFI_CREDIT_63;
1e69cd74
VS
5907 else
5908 credits = PFI_CREDIT(15);
5909 } else {
5910 credits = default_credits;
5911 }
5912
5913 /*
5914 * WA - write default credits before re-programming
5915 * FIXME: should we also set the resend bit here?
5916 */
5917 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5918 default_credits);
5919
5920 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5921 credits | PFI_CREDIT_RESEND);
5922
5923 /*
5924 * FIXME is this guaranteed to clear
5925 * immediately or should we poll for it?
5926 */
5927 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5928}
5929
27c329ed 5930static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
30a970c6 5931{
a821fc46 5932 struct drm_device *dev = old_state->dev;
27c329ed 5933 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
30a970c6 5934 struct drm_i915_private *dev_priv = dev->dev_private;
30a970c6 5935
27c329ed
ML
5936 /*
5937 * FIXME: We can end up here with all power domains off, yet
5938 * with a CDCLK frequency other than the minimum. To account
5939 * for this take the PIPE-A power domain, which covers the HW
5940 * blocks needed for the following programming. This can be
5941 * removed once it's guaranteed that we get here either with
5942 * the minimum CDCLK set, or the required power domains
5943 * enabled.
5944 */
5945 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
738c05c0 5946
27c329ed
ML
5947 if (IS_CHERRYVIEW(dev))
5948 cherryview_set_cdclk(dev, req_cdclk);
5949 else
5950 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 5951
27c329ed 5952 vlv_program_pfi_credits(dev_priv);
1e69cd74 5953
27c329ed 5954 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
30a970c6
JB
5955}
5956
89b667f8
JB
5957static void valleyview_crtc_enable(struct drm_crtc *crtc)
5958{
5959 struct drm_device *dev = crtc->dev;
a72e4c9f 5960 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
5961 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5962 struct intel_encoder *encoder;
5963 int pipe = intel_crtc->pipe;
23538ef1 5964 bool is_dsi;
89b667f8 5965
53d9f4e9 5966 if (WARN_ON(intel_crtc->active))
89b667f8
JB
5967 return;
5968
409ee761 5969 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 5970
1ae0d137
VS
5971 if (!is_dsi) {
5972 if (IS_CHERRYVIEW(dev))
6e3c9717 5973 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5974 else
6e3c9717 5975 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5976 }
5b18e57c 5977
6e3c9717 5978 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5979 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5980
5981 intel_set_pipe_timings(intel_crtc);
5982
c14b0485
VS
5983 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5984 struct drm_i915_private *dev_priv = dev->dev_private;
5985
5986 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5987 I915_WRITE(CHV_CANVAS(pipe), 0);
5988 }
5989
5b18e57c
DV
5990 i9xx_set_pipeconf(intel_crtc);
5991
89b667f8 5992 intel_crtc->active = true;
89b667f8 5993
a72e4c9f 5994 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5995
89b667f8
JB
5996 for_each_encoder_on_crtc(dev, crtc, encoder)
5997 if (encoder->pre_pll_enable)
5998 encoder->pre_pll_enable(encoder);
5999
9d556c99
CML
6000 if (!is_dsi) {
6001 if (IS_CHERRYVIEW(dev))
6e3c9717 6002 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6003 else
6e3c9717 6004 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 6005 }
89b667f8
JB
6006
6007 for_each_encoder_on_crtc(dev, crtc, encoder)
6008 if (encoder->pre_enable)
6009 encoder->pre_enable(encoder);
6010
2dd24552
JB
6011 i9xx_pfit_enable(intel_crtc);
6012
63cbb074
VS
6013 intel_crtc_load_lut(crtc);
6014
f37fcc2a 6015 intel_update_watermarks(crtc);
e1fdc473 6016 intel_enable_pipe(intel_crtc);
be6a6f8e 6017
4b3a9526
VS
6018 assert_vblank_disabled(crtc);
6019 drm_crtc_vblank_on(crtc);
6020
f9b61ff6
DV
6021 for_each_encoder_on_crtc(dev, crtc, encoder)
6022 encoder->enable(encoder);
89b667f8
JB
6023}
6024
f13c2ef3
DV
6025static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6026{
6027 struct drm_device *dev = crtc->base.dev;
6028 struct drm_i915_private *dev_priv = dev->dev_private;
6029
6e3c9717
ACO
6030 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6031 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
6032}
6033
0b8765c6 6034static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
6035{
6036 struct drm_device *dev = crtc->dev;
a72e4c9f 6037 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 6038 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6039 struct intel_encoder *encoder;
79e53945 6040 int pipe = intel_crtc->pipe;
79e53945 6041
53d9f4e9 6042 if (WARN_ON(intel_crtc->active))
f7abfe8b
CW
6043 return;
6044
f13c2ef3
DV
6045 i9xx_set_pll_dividers(intel_crtc);
6046
6e3c9717 6047 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 6048 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
6049
6050 intel_set_pipe_timings(intel_crtc);
6051
5b18e57c
DV
6052 i9xx_set_pipeconf(intel_crtc);
6053
f7abfe8b 6054 intel_crtc->active = true;
6b383a7f 6055
4a3436e8 6056 if (!IS_GEN2(dev))
a72e4c9f 6057 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 6058
9d6d9f19
MK
6059 for_each_encoder_on_crtc(dev, crtc, encoder)
6060 if (encoder->pre_enable)
6061 encoder->pre_enable(encoder);
6062
f6736a1a
DV
6063 i9xx_enable_pll(intel_crtc);
6064
2dd24552
JB
6065 i9xx_pfit_enable(intel_crtc);
6066
63cbb074
VS
6067 intel_crtc_load_lut(crtc);
6068
f37fcc2a 6069 intel_update_watermarks(crtc);
e1fdc473 6070 intel_enable_pipe(intel_crtc);
be6a6f8e 6071
4b3a9526
VS
6072 assert_vblank_disabled(crtc);
6073 drm_crtc_vblank_on(crtc);
6074
f9b61ff6
DV
6075 for_each_encoder_on_crtc(dev, crtc, encoder)
6076 encoder->enable(encoder);
0b8765c6 6077}
79e53945 6078
87476d63
DV
6079static void i9xx_pfit_disable(struct intel_crtc *crtc)
6080{
6081 struct drm_device *dev = crtc->base.dev;
6082 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 6083
6e3c9717 6084 if (!crtc->config->gmch_pfit.control)
328d8e82 6085 return;
87476d63 6086
328d8e82 6087 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6088
328d8e82
DV
6089 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6090 I915_READ(PFIT_CONTROL));
6091 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6092}
6093
0b8765c6
JB
6094static void i9xx_crtc_disable(struct drm_crtc *crtc)
6095{
6096 struct drm_device *dev = crtc->dev;
6097 struct drm_i915_private *dev_priv = dev->dev_private;
6098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6099 struct intel_encoder *encoder;
0b8765c6 6100 int pipe = intel_crtc->pipe;
ef9c3aee 6101
6304cd91
VS
6102 /*
6103 * On gen2 planes are double buffered but the pipe isn't, so we must
6104 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6105 * We also need to wait on all gmch platforms because of the
6106 * self-refresh mode constraint explained above.
6304cd91 6107 */
564ed191 6108 intel_wait_for_vblank(dev, pipe);
6304cd91 6109
4b3a9526
VS
6110 for_each_encoder_on_crtc(dev, crtc, encoder)
6111 encoder->disable(encoder);
6112
f9b61ff6
DV
6113 drm_crtc_vblank_off(crtc);
6114 assert_vblank_disabled(crtc);
6115
575f7ab7 6116 intel_disable_pipe(intel_crtc);
24a1f16d 6117
87476d63 6118 i9xx_pfit_disable(intel_crtc);
24a1f16d 6119
89b667f8
JB
6120 for_each_encoder_on_crtc(dev, crtc, encoder)
6121 if (encoder->post_disable)
6122 encoder->post_disable(encoder);
6123
409ee761 6124 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6125 if (IS_CHERRYVIEW(dev))
6126 chv_disable_pll(dev_priv, pipe);
6127 else if (IS_VALLEYVIEW(dev))
6128 vlv_disable_pll(dev_priv, pipe);
6129 else
1c4e0274 6130 i9xx_disable_pll(intel_crtc);
076ed3b2 6131 }
0b8765c6 6132
4a3436e8 6133 if (!IS_GEN2(dev))
a72e4c9f 6134 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
0b8765c6
JB
6135}
6136
b17d48e2
ML
6137static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
6138{
6139 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6140 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
6141 enum intel_display_power_domain domain;
6142 unsigned long domains;
6143
6144 if (!intel_crtc->active)
6145 return;
6146
a539205a
ML
6147 if (to_intel_plane_state(crtc->primary->state)->visible) {
6148 intel_crtc_wait_for_pending_flips(crtc);
6149 intel_pre_disable_primary(crtc);
6150 }
6151
d032ffa0 6152 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
b17d48e2
ML
6153 dev_priv->display.crtc_disable(crtc);
6154
6155 domains = intel_crtc->enabled_power_domains;
6156 for_each_power_domain(domain, domains)
6157 intel_display_power_put(dev_priv, domain);
6158 intel_crtc->enabled_power_domains = 0;
6159}
6160
6b72d486
ML
6161/*
6162 * turn all crtc's off, but do not adjust state
6163 * This has to be paired with a call to intel_modeset_setup_hw_state.
6164 */
9716c691 6165void intel_display_suspend(struct drm_device *dev)
ee7b9f93 6166{
6b72d486
ML
6167 struct drm_crtc *crtc;
6168
b17d48e2
ML
6169 for_each_crtc(dev, crtc)
6170 intel_crtc_disable_noatomic(crtc);
ee7b9f93
JB
6171}
6172
b04c5bd6 6173/* Master function to enable/disable CRTC and corresponding power wells */
5da76e94 6174int intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
6175{
6176 struct drm_device *dev = crtc->dev;
5da76e94
ML
6177 struct drm_mode_config *config = &dev->mode_config;
6178 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
0e572fe7 6179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5da76e94
ML
6180 struct intel_crtc_state *pipe_config;
6181 struct drm_atomic_state *state;
6182 int ret;
976f8a20 6183
1b509259 6184 if (enable == intel_crtc->active)
5da76e94 6185 return 0;
0e572fe7 6186
1b509259 6187 if (enable && !crtc->state->enable)
5da76e94 6188 return 0;
1b509259 6189
5da76e94
ML
6190 /* this function should be called with drm_modeset_lock_all for now */
6191 if (WARN_ON(!ctx))
6192 return -EIO;
6193 lockdep_assert_held(&ctx->ww_ctx);
1b509259 6194
5da76e94
ML
6195 state = drm_atomic_state_alloc(dev);
6196 if (WARN_ON(!state))
6197 return -ENOMEM;
1b509259 6198
5da76e94
ML
6199 state->acquire_ctx = ctx;
6200 state->allow_modeset = true;
6201
6202 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6203 if (IS_ERR(pipe_config)) {
6204 ret = PTR_ERR(pipe_config);
6205 goto err;
0e572fe7 6206 }
5da76e94
ML
6207 pipe_config->base.active = enable;
6208
6209 ret = intel_set_mode(state);
6210 if (!ret)
6211 return ret;
6212
6213err:
6214 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6215 drm_atomic_state_free(state);
6216 return ret;
b04c5bd6
BF
6217}
6218
6219/**
6220 * Sets the power management mode of the pipe and plane.
6221 */
6222void intel_crtc_update_dpms(struct drm_crtc *crtc)
6223{
6224 struct drm_device *dev = crtc->dev;
6225 struct intel_encoder *intel_encoder;
6226 bool enable = false;
6227
6228 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6229 enable |= intel_encoder->connectors_active;
6230
6231 intel_crtc_control(crtc, enable);
cdd59983
CW
6232}
6233
ea5b213a 6234void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6235{
4ef69c7a 6236 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6237
ea5b213a
CW
6238 drm_encoder_cleanup(encoder);
6239 kfree(intel_encoder);
7e7d76c3
JB
6240}
6241
9237329d 6242/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
6243 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6244 * state of the entire output pipe. */
9237329d 6245static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 6246{
5ab432ef
DV
6247 if (mode == DRM_MODE_DPMS_ON) {
6248 encoder->connectors_active = true;
6249
b2cabb0e 6250 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
6251 } else {
6252 encoder->connectors_active = false;
6253
b2cabb0e 6254 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 6255 }
79e53945
JB
6256}
6257
0a91ca29
DV
6258/* Cross check the actual hw state with our own modeset state tracking (and it's
6259 * internal consistency). */
b980514c 6260static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6261{
0a91ca29
DV
6262 if (connector->get_hw_state(connector)) {
6263 struct intel_encoder *encoder = connector->encoder;
6264 struct drm_crtc *crtc;
6265 bool encoder_enabled;
6266 enum pipe pipe;
6267
6268 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6269 connector->base.base.id,
c23cc417 6270 connector->base.name);
0a91ca29 6271
0e32b39c
DA
6272 /* there is no real hw state for MST connectors */
6273 if (connector->mst_port)
6274 return;
6275
e2c719b7 6276 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 6277 "wrong connector dpms state\n");
e2c719b7 6278 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 6279 "active connector not linked to encoder\n");
0a91ca29 6280
36cd7444 6281 if (encoder) {
e2c719b7 6282 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
6283 "encoder->connectors_active not set\n");
6284
6285 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
6286 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6287 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 6288 return;
0a91ca29 6289
36cd7444 6290 crtc = encoder->base.crtc;
0a91ca29 6291
83d65738
MR
6292 I915_STATE_WARN(!crtc->state->enable,
6293 "crtc not enabled\n");
e2c719b7
RC
6294 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6295 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
6296 "encoder active on the wrong pipe\n");
6297 }
0a91ca29 6298 }
79e53945
JB
6299}
6300
08d9bc92
ACO
6301int intel_connector_init(struct intel_connector *connector)
6302{
6303 struct drm_connector_state *connector_state;
6304
6305 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6306 if (!connector_state)
6307 return -ENOMEM;
6308
6309 connector->base.state = connector_state;
6310 return 0;
6311}
6312
6313struct intel_connector *intel_connector_alloc(void)
6314{
6315 struct intel_connector *connector;
6316
6317 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6318 if (!connector)
6319 return NULL;
6320
6321 if (intel_connector_init(connector) < 0) {
6322 kfree(connector);
6323 return NULL;
6324 }
6325
6326 return connector;
6327}
6328
5ab432ef
DV
6329/* Even simpler default implementation, if there's really no special case to
6330 * consider. */
6331void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 6332{
5ab432ef
DV
6333 /* All the simple cases only support two dpms states. */
6334 if (mode != DRM_MODE_DPMS_ON)
6335 mode = DRM_MODE_DPMS_OFF;
d4270e57 6336
5ab432ef
DV
6337 if (mode == connector->dpms)
6338 return;
6339
6340 connector->dpms = mode;
6341
6342 /* Only need to change hw state when actually enabled */
c9976dcf
CW
6343 if (connector->encoder)
6344 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 6345
b980514c 6346 intel_modeset_check_state(connector->dev);
79e53945
JB
6347}
6348
f0947c37
DV
6349/* Simple connector->get_hw_state implementation for encoders that support only
6350 * one connector and no cloning and hence the encoder state determines the state
6351 * of the connector. */
6352bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6353{
24929352 6354 enum pipe pipe = 0;
f0947c37 6355 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6356
f0947c37 6357 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6358}
6359
6d293983 6360static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6361{
6d293983
ACO
6362 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6363 return crtc_state->fdi_lanes;
d272ddfa
VS
6364
6365 return 0;
6366}
6367
6d293983 6368static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6369 struct intel_crtc_state *pipe_config)
1857e1da 6370{
6d293983
ACO
6371 struct drm_atomic_state *state = pipe_config->base.state;
6372 struct intel_crtc *other_crtc;
6373 struct intel_crtc_state *other_crtc_state;
6374
1857e1da
DV
6375 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6376 pipe_name(pipe), pipe_config->fdi_lanes);
6377 if (pipe_config->fdi_lanes > 4) {
6378 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6379 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6380 return -EINVAL;
1857e1da
DV
6381 }
6382
bafb6553 6383 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6384 if (pipe_config->fdi_lanes > 2) {
6385 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6386 pipe_config->fdi_lanes);
6d293983 6387 return -EINVAL;
1857e1da 6388 } else {
6d293983 6389 return 0;
1857e1da
DV
6390 }
6391 }
6392
6393 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6394 return 0;
1857e1da
DV
6395
6396 /* Ivybridge 3 pipe is really complicated */
6397 switch (pipe) {
6398 case PIPE_A:
6d293983 6399 return 0;
1857e1da 6400 case PIPE_B:
6d293983
ACO
6401 if (pipe_config->fdi_lanes <= 2)
6402 return 0;
6403
6404 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6405 other_crtc_state =
6406 intel_atomic_get_crtc_state(state, other_crtc);
6407 if (IS_ERR(other_crtc_state))
6408 return PTR_ERR(other_crtc_state);
6409
6410 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6411 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6412 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6413 return -EINVAL;
1857e1da 6414 }
6d293983 6415 return 0;
1857e1da 6416 case PIPE_C:
251cc67c
VS
6417 if (pipe_config->fdi_lanes > 2) {
6418 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6419 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6420 return -EINVAL;
251cc67c 6421 }
6d293983
ACO
6422
6423 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6424 other_crtc_state =
6425 intel_atomic_get_crtc_state(state, other_crtc);
6426 if (IS_ERR(other_crtc_state))
6427 return PTR_ERR(other_crtc_state);
6428
6429 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6430 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6431 return -EINVAL;
1857e1da 6432 }
6d293983 6433 return 0;
1857e1da
DV
6434 default:
6435 BUG();
6436 }
6437}
6438
e29c22c0
DV
6439#define RETRY 1
6440static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6441 struct intel_crtc_state *pipe_config)
877d48d5 6442{
1857e1da 6443 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 6444 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6445 int lane, link_bw, fdi_dotclock, ret;
6446 bool needs_recompute = false;
877d48d5 6447
e29c22c0 6448retry:
877d48d5
DV
6449 /* FDI is a binary signal running at ~2.7GHz, encoding
6450 * each output octet as 10 bits. The actual frequency
6451 * is stored as a divider into a 100MHz clock, and the
6452 * mode pixel clock is stored in units of 1KHz.
6453 * Hence the bw of each lane in terms of the mode signal
6454 * is:
6455 */
6456 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6457
241bfc38 6458 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6459
2bd89a07 6460 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6461 pipe_config->pipe_bpp);
6462
6463 pipe_config->fdi_lanes = lane;
6464
2bd89a07 6465 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6466 link_bw, &pipe_config->fdi_m_n);
1857e1da 6467
6d293983
ACO
6468 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6469 intel_crtc->pipe, pipe_config);
6470 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6471 pipe_config->pipe_bpp -= 2*3;
6472 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6473 pipe_config->pipe_bpp);
6474 needs_recompute = true;
6475 pipe_config->bw_constrained = true;
6476
6477 goto retry;
6478 }
6479
6480 if (needs_recompute)
6481 return RETRY;
6482
6d293983 6483 return ret;
877d48d5
DV
6484}
6485
8cfb3407
VS
6486static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6487 struct intel_crtc_state *pipe_config)
6488{
6489 if (pipe_config->pipe_bpp > 24)
6490 return false;
6491
6492 /* HSW can handle pixel rate up to cdclk? */
6493 if (IS_HASWELL(dev_priv->dev))
6494 return true;
6495
6496 /*
b432e5cf
VS
6497 * We compare against max which means we must take
6498 * the increased cdclk requirement into account when
6499 * calculating the new cdclk.
6500 *
6501 * Should measure whether using a lower cdclk w/o IPS
8cfb3407
VS
6502 */
6503 return ilk_pipe_pixel_rate(pipe_config) <=
6504 dev_priv->max_cdclk_freq * 95 / 100;
6505}
6506
42db64ef 6507static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6508 struct intel_crtc_state *pipe_config)
42db64ef 6509{
8cfb3407
VS
6510 struct drm_device *dev = crtc->base.dev;
6511 struct drm_i915_private *dev_priv = dev->dev_private;
6512
d330a953 6513 pipe_config->ips_enabled = i915.enable_ips &&
8cfb3407
VS
6514 hsw_crtc_supports_ips(crtc) &&
6515 pipe_config_supports_ips(dev_priv, pipe_config);
42db64ef
PZ
6516}
6517
a43f6e0f 6518static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6519 struct intel_crtc_state *pipe_config)
79e53945 6520{
a43f6e0f 6521 struct drm_device *dev = crtc->base.dev;
8bd31e67 6522 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 6523 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
89749350 6524
ad3a4479 6525 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6526 if (INTEL_INFO(dev)->gen < 4) {
44913155 6527 int clock_limit = dev_priv->max_cdclk_freq;
cf532bb2
VS
6528
6529 /*
6530 * Enable pixel doubling when the dot clock
6531 * is > 90% of the (display) core speed.
6532 *
b397c96b
VS
6533 * GDG double wide on either pipe,
6534 * otherwise pipe A only.
cf532bb2 6535 */
b397c96b 6536 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 6537 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 6538 clock_limit *= 2;
cf532bb2 6539 pipe_config->double_wide = true;
ad3a4479
VS
6540 }
6541
241bfc38 6542 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 6543 return -EINVAL;
2c07245f 6544 }
89749350 6545
1d1d0e27
VS
6546 /*
6547 * Pipe horizontal size must be even in:
6548 * - DVO ganged mode
6549 * - LVDS dual channel mode
6550 * - Double wide pipe
6551 */
a93e255f 6552 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6553 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6554 pipe_config->pipe_src_w &= ~1;
6555
8693a824
DL
6556 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6557 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6558 */
6559 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6560 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 6561 return -EINVAL;
44f46b42 6562
f5adf94e 6563 if (HAS_IPS(dev))
a43f6e0f
DV
6564 hsw_compute_ips_config(crtc, pipe_config);
6565
877d48d5 6566 if (pipe_config->has_pch_encoder)
a43f6e0f 6567 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6568
cf5a15be 6569 return 0;
79e53945
JB
6570}
6571
1652d19e
VS
6572static int skylake_get_display_clock_speed(struct drm_device *dev)
6573{
6574 struct drm_i915_private *dev_priv = to_i915(dev);
6575 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6576 uint32_t cdctl = I915_READ(CDCLK_CTL);
6577 uint32_t linkrate;
6578
414355a7 6579 if (!(lcpll1 & LCPLL_PLL_ENABLE))
1652d19e 6580 return 24000; /* 24MHz is the cd freq with NSSC ref */
1652d19e
VS
6581
6582 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6583 return 540000;
6584
6585 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6586 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6587
71cd8423
DL
6588 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6589 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6590 /* vco 8640 */
6591 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6592 case CDCLK_FREQ_450_432:
6593 return 432000;
6594 case CDCLK_FREQ_337_308:
6595 return 308570;
6596 case CDCLK_FREQ_675_617:
6597 return 617140;
6598 default:
6599 WARN(1, "Unknown cd freq selection\n");
6600 }
6601 } else {
6602 /* vco 8100 */
6603 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6604 case CDCLK_FREQ_450_432:
6605 return 450000;
6606 case CDCLK_FREQ_337_308:
6607 return 337500;
6608 case CDCLK_FREQ_675_617:
6609 return 675000;
6610 default:
6611 WARN(1, "Unknown cd freq selection\n");
6612 }
6613 }
6614
6615 /* error case, do as if DPLL0 isn't enabled */
6616 return 24000;
6617}
6618
6619static int broadwell_get_display_clock_speed(struct drm_device *dev)
6620{
6621 struct drm_i915_private *dev_priv = dev->dev_private;
6622 uint32_t lcpll = I915_READ(LCPLL_CTL);
6623 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6624
6625 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6626 return 800000;
6627 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6628 return 450000;
6629 else if (freq == LCPLL_CLK_FREQ_450)
6630 return 450000;
6631 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6632 return 540000;
6633 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6634 return 337500;
6635 else
6636 return 675000;
6637}
6638
6639static int haswell_get_display_clock_speed(struct drm_device *dev)
6640{
6641 struct drm_i915_private *dev_priv = dev->dev_private;
6642 uint32_t lcpll = I915_READ(LCPLL_CTL);
6643 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6644
6645 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6646 return 800000;
6647 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6648 return 450000;
6649 else if (freq == LCPLL_CLK_FREQ_450)
6650 return 450000;
6651 else if (IS_HSW_ULT(dev))
6652 return 337500;
6653 else
6654 return 540000;
79e53945
JB
6655}
6656
25eb05fc
JB
6657static int valleyview_get_display_clock_speed(struct drm_device *dev)
6658{
d197b7d3 6659 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
6660 u32 val;
6661 int divider;
6662
6bcda4f0
VS
6663 if (dev_priv->hpll_freq == 0)
6664 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6665
a580516d 6666 mutex_lock(&dev_priv->sb_lock);
d197b7d3 6667 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
a580516d 6668 mutex_unlock(&dev_priv->sb_lock);
d197b7d3
VS
6669
6670 divider = val & DISPLAY_FREQUENCY_VALUES;
6671
7d007f40
VS
6672 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6673 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6674 "cdclk change in progress\n");
6675
6bcda4f0 6676 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
6677}
6678
b37a6434
VS
6679static int ilk_get_display_clock_speed(struct drm_device *dev)
6680{
6681 return 450000;
6682}
6683
e70236a8
JB
6684static int i945_get_display_clock_speed(struct drm_device *dev)
6685{
6686 return 400000;
6687}
79e53945 6688
e70236a8 6689static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6690{
e907f170 6691 return 333333;
e70236a8 6692}
79e53945 6693
e70236a8
JB
6694static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6695{
6696 return 200000;
6697}
79e53945 6698
257a7ffc
DV
6699static int pnv_get_display_clock_speed(struct drm_device *dev)
6700{
6701 u16 gcfgc = 0;
6702
6703 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6704
6705 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6706 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6707 return 266667;
257a7ffc 6708 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6709 return 333333;
257a7ffc 6710 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6711 return 444444;
257a7ffc
DV
6712 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6713 return 200000;
6714 default:
6715 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6716 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6717 return 133333;
257a7ffc 6718 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6719 return 166667;
257a7ffc
DV
6720 }
6721}
6722
e70236a8
JB
6723static int i915gm_get_display_clock_speed(struct drm_device *dev)
6724{
6725 u16 gcfgc = 0;
79e53945 6726
e70236a8
JB
6727 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6728
6729 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6730 return 133333;
e70236a8
JB
6731 else {
6732 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6733 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6734 return 333333;
e70236a8
JB
6735 default:
6736 case GC_DISPLAY_CLOCK_190_200_MHZ:
6737 return 190000;
79e53945 6738 }
e70236a8
JB
6739 }
6740}
6741
6742static int i865_get_display_clock_speed(struct drm_device *dev)
6743{
e907f170 6744 return 266667;
e70236a8
JB
6745}
6746
1b1d2716 6747static int i85x_get_display_clock_speed(struct drm_device *dev)
e70236a8
JB
6748{
6749 u16 hpllcc = 0;
1b1d2716 6750
65cd2b3f
VS
6751 /*
6752 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6753 * encoding is different :(
6754 * FIXME is this the right way to detect 852GM/852GMV?
6755 */
6756 if (dev->pdev->revision == 0x1)
6757 return 133333;
6758
1b1d2716
VS
6759 pci_bus_read_config_word(dev->pdev->bus,
6760 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6761
e70236a8
JB
6762 /* Assume that the hardware is in the high speed state. This
6763 * should be the default.
6764 */
6765 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6766 case GC_CLOCK_133_200:
1b1d2716 6767 case GC_CLOCK_133_200_2:
e70236a8
JB
6768 case GC_CLOCK_100_200:
6769 return 200000;
6770 case GC_CLOCK_166_250:
6771 return 250000;
6772 case GC_CLOCK_100_133:
e907f170 6773 return 133333;
1b1d2716
VS
6774 case GC_CLOCK_133_266:
6775 case GC_CLOCK_133_266_2:
6776 case GC_CLOCK_166_266:
6777 return 266667;
e70236a8 6778 }
79e53945 6779
e70236a8
JB
6780 /* Shouldn't happen */
6781 return 0;
6782}
79e53945 6783
e70236a8
JB
6784static int i830_get_display_clock_speed(struct drm_device *dev)
6785{
e907f170 6786 return 133333;
79e53945
JB
6787}
6788
34edce2f
VS
6789static unsigned int intel_hpll_vco(struct drm_device *dev)
6790{
6791 struct drm_i915_private *dev_priv = dev->dev_private;
6792 static const unsigned int blb_vco[8] = {
6793 [0] = 3200000,
6794 [1] = 4000000,
6795 [2] = 5333333,
6796 [3] = 4800000,
6797 [4] = 6400000,
6798 };
6799 static const unsigned int pnv_vco[8] = {
6800 [0] = 3200000,
6801 [1] = 4000000,
6802 [2] = 5333333,
6803 [3] = 4800000,
6804 [4] = 2666667,
6805 };
6806 static const unsigned int cl_vco[8] = {
6807 [0] = 3200000,
6808 [1] = 4000000,
6809 [2] = 5333333,
6810 [3] = 6400000,
6811 [4] = 3333333,
6812 [5] = 3566667,
6813 [6] = 4266667,
6814 };
6815 static const unsigned int elk_vco[8] = {
6816 [0] = 3200000,
6817 [1] = 4000000,
6818 [2] = 5333333,
6819 [3] = 4800000,
6820 };
6821 static const unsigned int ctg_vco[8] = {
6822 [0] = 3200000,
6823 [1] = 4000000,
6824 [2] = 5333333,
6825 [3] = 6400000,
6826 [4] = 2666667,
6827 [5] = 4266667,
6828 };
6829 const unsigned int *vco_table;
6830 unsigned int vco;
6831 uint8_t tmp = 0;
6832
6833 /* FIXME other chipsets? */
6834 if (IS_GM45(dev))
6835 vco_table = ctg_vco;
6836 else if (IS_G4X(dev))
6837 vco_table = elk_vco;
6838 else if (IS_CRESTLINE(dev))
6839 vco_table = cl_vco;
6840 else if (IS_PINEVIEW(dev))
6841 vco_table = pnv_vco;
6842 else if (IS_G33(dev))
6843 vco_table = blb_vco;
6844 else
6845 return 0;
6846
6847 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6848
6849 vco = vco_table[tmp & 0x7];
6850 if (vco == 0)
6851 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6852 else
6853 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6854
6855 return vco;
6856}
6857
6858static int gm45_get_display_clock_speed(struct drm_device *dev)
6859{
6860 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6861 uint16_t tmp = 0;
6862
6863 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6864
6865 cdclk_sel = (tmp >> 12) & 0x1;
6866
6867 switch (vco) {
6868 case 2666667:
6869 case 4000000:
6870 case 5333333:
6871 return cdclk_sel ? 333333 : 222222;
6872 case 3200000:
6873 return cdclk_sel ? 320000 : 228571;
6874 default:
6875 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6876 return 222222;
6877 }
6878}
6879
6880static int i965gm_get_display_clock_speed(struct drm_device *dev)
6881{
6882 static const uint8_t div_3200[] = { 16, 10, 8 };
6883 static const uint8_t div_4000[] = { 20, 12, 10 };
6884 static const uint8_t div_5333[] = { 24, 16, 14 };
6885 const uint8_t *div_table;
6886 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6887 uint16_t tmp = 0;
6888
6889 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6890
6891 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6892
6893 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6894 goto fail;
6895
6896 switch (vco) {
6897 case 3200000:
6898 div_table = div_3200;
6899 break;
6900 case 4000000:
6901 div_table = div_4000;
6902 break;
6903 case 5333333:
6904 div_table = div_5333;
6905 break;
6906 default:
6907 goto fail;
6908 }
6909
6910 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6911
caf4e252 6912fail:
34edce2f
VS
6913 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6914 return 200000;
6915}
6916
6917static int g33_get_display_clock_speed(struct drm_device *dev)
6918{
6919 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6920 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6921 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6922 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6923 const uint8_t *div_table;
6924 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6925 uint16_t tmp = 0;
6926
6927 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6928
6929 cdclk_sel = (tmp >> 4) & 0x7;
6930
6931 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6932 goto fail;
6933
6934 switch (vco) {
6935 case 3200000:
6936 div_table = div_3200;
6937 break;
6938 case 4000000:
6939 div_table = div_4000;
6940 break;
6941 case 4800000:
6942 div_table = div_4800;
6943 break;
6944 case 5333333:
6945 div_table = div_5333;
6946 break;
6947 default:
6948 goto fail;
6949 }
6950
6951 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6952
caf4e252 6953fail:
34edce2f
VS
6954 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
6955 return 190476;
6956}
6957
2c07245f 6958static void
a65851af 6959intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6960{
a65851af
VS
6961 while (*num > DATA_LINK_M_N_MASK ||
6962 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6963 *num >>= 1;
6964 *den >>= 1;
6965 }
6966}
6967
a65851af
VS
6968static void compute_m_n(unsigned int m, unsigned int n,
6969 uint32_t *ret_m, uint32_t *ret_n)
6970{
6971 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6972 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6973 intel_reduce_m_n_ratio(ret_m, ret_n);
6974}
6975
e69d0bc1
DV
6976void
6977intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6978 int pixel_clock, int link_clock,
6979 struct intel_link_m_n *m_n)
2c07245f 6980{
e69d0bc1 6981 m_n->tu = 64;
a65851af
VS
6982
6983 compute_m_n(bits_per_pixel * pixel_clock,
6984 link_clock * nlanes * 8,
6985 &m_n->gmch_m, &m_n->gmch_n);
6986
6987 compute_m_n(pixel_clock, link_clock,
6988 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
6989}
6990
a7615030
CW
6991static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6992{
d330a953
JN
6993 if (i915.panel_use_ssc >= 0)
6994 return i915.panel_use_ssc != 0;
41aa3448 6995 return dev_priv->vbt.lvds_use_ssc
435793df 6996 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6997}
6998
a93e255f
ACO
6999static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7000 int num_connectors)
c65d77d8 7001{
a93e255f 7002 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
7003 struct drm_i915_private *dev_priv = dev->dev_private;
7004 int refclk;
7005
a93e255f
ACO
7006 WARN_ON(!crtc_state->base.state);
7007
5ab7b0b7 7008 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 7009 refclk = 100000;
a93e255f 7010 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 7011 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
7012 refclk = dev_priv->vbt.lvds_ssc_freq;
7013 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
7014 } else if (!IS_GEN2(dev)) {
7015 refclk = 96000;
7016 } else {
7017 refclk = 48000;
7018 }
7019
7020 return refclk;
7021}
7022
7429e9d4 7023static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 7024{
7df00d7a 7025 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 7026}
f47709a9 7027
7429e9d4
DV
7028static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7029{
7030 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
7031}
7032
f47709a9 7033static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 7034 struct intel_crtc_state *crtc_state,
a7516a05
JB
7035 intel_clock_t *reduced_clock)
7036{
f47709a9 7037 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
7038 u32 fp, fp2 = 0;
7039
7040 if (IS_PINEVIEW(dev)) {
190f68c5 7041 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7042 if (reduced_clock)
7429e9d4 7043 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 7044 } else {
190f68c5 7045 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 7046 if (reduced_clock)
7429e9d4 7047 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
7048 }
7049
190f68c5 7050 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 7051
f47709a9 7052 crtc->lowfreq_avail = false;
a93e255f 7053 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 7054 reduced_clock) {
190f68c5 7055 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 7056 crtc->lowfreq_avail = true;
a7516a05 7057 } else {
190f68c5 7058 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
7059 }
7060}
7061
5e69f97f
CML
7062static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7063 pipe)
89b667f8
JB
7064{
7065 u32 reg_val;
7066
7067 /*
7068 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7069 * and set it to a reasonable value instead.
7070 */
ab3c759a 7071 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
7072 reg_val &= 0xffffff00;
7073 reg_val |= 0x00000030;
ab3c759a 7074 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7075
ab3c759a 7076 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7077 reg_val &= 0x8cffffff;
7078 reg_val = 0x8c000000;
ab3c759a 7079 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 7080
ab3c759a 7081 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 7082 reg_val &= 0xffffff00;
ab3c759a 7083 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 7084
ab3c759a 7085 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
7086 reg_val &= 0x00ffffff;
7087 reg_val |= 0xb0000000;
ab3c759a 7088 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
7089}
7090
b551842d
DV
7091static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7092 struct intel_link_m_n *m_n)
7093{
7094 struct drm_device *dev = crtc->base.dev;
7095 struct drm_i915_private *dev_priv = dev->dev_private;
7096 int pipe = crtc->pipe;
7097
e3b95f1e
DV
7098 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7099 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7100 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7101 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
7102}
7103
7104static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
7105 struct intel_link_m_n *m_n,
7106 struct intel_link_m_n *m2_n2)
b551842d
DV
7107{
7108 struct drm_device *dev = crtc->base.dev;
7109 struct drm_i915_private *dev_priv = dev->dev_private;
7110 int pipe = crtc->pipe;
6e3c9717 7111 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
7112
7113 if (INTEL_INFO(dev)->gen >= 5) {
7114 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7115 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7116 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7117 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
7118 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7119 * for gen < 8) and if DRRS is supported (to make sure the
7120 * registers are not unnecessarily accessed).
7121 */
44395bfe 7122 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 7123 crtc->config->has_drrs) {
f769cd24
VK
7124 I915_WRITE(PIPE_DATA_M2(transcoder),
7125 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7126 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7127 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7128 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7129 }
b551842d 7130 } else {
e3b95f1e
DV
7131 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7132 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7133 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7134 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
7135 }
7136}
7137
fe3cd48d 7138void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 7139{
fe3cd48d
R
7140 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7141
7142 if (m_n == M1_N1) {
7143 dp_m_n = &crtc->config->dp_m_n;
7144 dp_m2_n2 = &crtc->config->dp_m2_n2;
7145 } else if (m_n == M2_N2) {
7146
7147 /*
7148 * M2_N2 registers are not supported. Hence m2_n2 divider value
7149 * needs to be programmed into M1_N1.
7150 */
7151 dp_m_n = &crtc->config->dp_m2_n2;
7152 } else {
7153 DRM_ERROR("Unsupported divider value\n");
7154 return;
7155 }
7156
6e3c9717
ACO
7157 if (crtc->config->has_pch_encoder)
7158 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 7159 else
fe3cd48d 7160 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
7161}
7162
251ac862
DV
7163static void vlv_compute_dpll(struct intel_crtc *crtc,
7164 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
7165{
7166 u32 dpll, dpll_md;
7167
7168 /*
7169 * Enable DPIO clock input. We should never disable the reference
7170 * clock for pipe B, since VGA hotplug / manual detection depends
7171 * on it.
7172 */
7173 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7174 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7175 /* We should never disable this, set it here for state tracking */
7176 if (crtc->pipe == PIPE_B)
7177 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7178 dpll |= DPLL_VCO_ENABLE;
d288f65f 7179 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 7180
d288f65f 7181 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 7182 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 7183 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
7184}
7185
d288f65f 7186static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7187 const struct intel_crtc_state *pipe_config)
a0c4da24 7188{
f47709a9 7189 struct drm_device *dev = crtc->base.dev;
a0c4da24 7190 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 7191 int pipe = crtc->pipe;
bdd4b6a6 7192 u32 mdiv;
a0c4da24 7193 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 7194 u32 coreclk, reg_val;
a0c4da24 7195
a580516d 7196 mutex_lock(&dev_priv->sb_lock);
09153000 7197
d288f65f
VS
7198 bestn = pipe_config->dpll.n;
7199 bestm1 = pipe_config->dpll.m1;
7200 bestm2 = pipe_config->dpll.m2;
7201 bestp1 = pipe_config->dpll.p1;
7202 bestp2 = pipe_config->dpll.p2;
a0c4da24 7203
89b667f8
JB
7204 /* See eDP HDMI DPIO driver vbios notes doc */
7205
7206 /* PLL B needs special handling */
bdd4b6a6 7207 if (pipe == PIPE_B)
5e69f97f 7208 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
7209
7210 /* Set up Tx target for periodic Rcomp update */
ab3c759a 7211 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
7212
7213 /* Disable target IRef on PLL */
ab3c759a 7214 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 7215 reg_val &= 0x00ffffff;
ab3c759a 7216 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
7217
7218 /* Disable fast lock */
ab3c759a 7219 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
7220
7221 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
7222 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7223 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7224 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 7225 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
7226
7227 /*
7228 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7229 * but we don't support that).
7230 * Note: don't use the DAC post divider as it seems unstable.
7231 */
7232 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 7233 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7234
a0c4da24 7235 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 7236 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 7237
89b667f8 7238 /* Set HBR and RBR LPF coefficients */
d288f65f 7239 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
7240 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7241 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 7242 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 7243 0x009f0003);
89b667f8 7244 else
ab3c759a 7245 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
7246 0x00d0000f);
7247
681a8504 7248 if (pipe_config->has_dp_encoder) {
89b667f8 7249 /* Use SSC source */
bdd4b6a6 7250 if (pipe == PIPE_A)
ab3c759a 7251 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7252 0x0df40000);
7253 else
ab3c759a 7254 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7255 0x0df70000);
7256 } else { /* HDMI or VGA */
7257 /* Use bend source */
bdd4b6a6 7258 if (pipe == PIPE_A)
ab3c759a 7259 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7260 0x0df70000);
7261 else
ab3c759a 7262 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
7263 0x0df40000);
7264 }
a0c4da24 7265
ab3c759a 7266 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 7267 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
7268 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7269 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 7270 coreclk |= 0x01000000;
ab3c759a 7271 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 7272
ab3c759a 7273 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a580516d 7274 mutex_unlock(&dev_priv->sb_lock);
a0c4da24
JB
7275}
7276
251ac862
DV
7277static void chv_compute_dpll(struct intel_crtc *crtc,
7278 struct intel_crtc_state *pipe_config)
1ae0d137 7279{
d288f65f 7280 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
7281 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7282 DPLL_VCO_ENABLE;
7283 if (crtc->pipe != PIPE_A)
d288f65f 7284 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 7285
d288f65f
VS
7286 pipe_config->dpll_hw_state.dpll_md =
7287 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
7288}
7289
d288f65f 7290static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 7291 const struct intel_crtc_state *pipe_config)
9d556c99
CML
7292{
7293 struct drm_device *dev = crtc->base.dev;
7294 struct drm_i915_private *dev_priv = dev->dev_private;
7295 int pipe = crtc->pipe;
7296 int dpll_reg = DPLL(crtc->pipe);
7297 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7298 u32 loopfilter, tribuf_calcntr;
9d556c99 7299 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7300 u32 dpio_val;
9cbe40c1 7301 int vco;
9d556c99 7302
d288f65f
VS
7303 bestn = pipe_config->dpll.n;
7304 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7305 bestm1 = pipe_config->dpll.m1;
7306 bestm2 = pipe_config->dpll.m2 >> 22;
7307 bestp1 = pipe_config->dpll.p1;
7308 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7309 vco = pipe_config->dpll.vco;
a945ce7e 7310 dpio_val = 0;
9cbe40c1 7311 loopfilter = 0;
9d556c99
CML
7312
7313 /*
7314 * Enable Refclk and SSC
7315 */
a11b0703 7316 I915_WRITE(dpll_reg,
d288f65f 7317 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703 7318
a580516d 7319 mutex_lock(&dev_priv->sb_lock);
9d556c99 7320
9d556c99
CML
7321 /* p1 and p2 divider */
7322 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7323 5 << DPIO_CHV_S1_DIV_SHIFT |
7324 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7325 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7326 1 << DPIO_CHV_K_DIV_SHIFT);
7327
7328 /* Feedback post-divider - m2 */
7329 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7330
7331 /* Feedback refclk divider - n and m1 */
7332 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7333 DPIO_CHV_M1_DIV_BY_2 |
7334 1 << DPIO_CHV_N_DIV_SHIFT);
7335
7336 /* M2 fraction division */
a945ce7e
VP
7337 if (bestm2_frac)
7338 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7339
7340 /* M2 fraction division enable */
a945ce7e
VP
7341 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7342 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7343 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7344 if (bestm2_frac)
7345 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7346 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7347
de3a0fde
VP
7348 /* Program digital lock detect threshold */
7349 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7350 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7351 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7352 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7353 if (!bestm2_frac)
7354 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7355 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7356
9d556c99 7357 /* Loop filter */
9cbe40c1
VP
7358 if (vco == 5400000) {
7359 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7360 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7361 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7362 tribuf_calcntr = 0x9;
7363 } else if (vco <= 6200000) {
7364 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7365 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7366 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7367 tribuf_calcntr = 0x9;
7368 } else if (vco <= 6480000) {
7369 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7370 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7371 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7372 tribuf_calcntr = 0x8;
7373 } else {
7374 /* Not supported. Apply the same limits as in the max case */
7375 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7376 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7377 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7378 tribuf_calcntr = 0;
7379 }
9d556c99
CML
7380 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7381
968040b2 7382 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7383 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7384 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7385 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7386
9d556c99
CML
7387 /* AFC Recal */
7388 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7389 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7390 DPIO_AFC_RECAL);
7391
a580516d 7392 mutex_unlock(&dev_priv->sb_lock);
9d556c99
CML
7393}
7394
d288f65f
VS
7395/**
7396 * vlv_force_pll_on - forcibly enable just the PLL
7397 * @dev_priv: i915 private structure
7398 * @pipe: pipe PLL to enable
7399 * @dpll: PLL configuration
7400 *
7401 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7402 * in cases where we need the PLL enabled even when @pipe is not going to
7403 * be enabled.
7404 */
7405void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7406 const struct dpll *dpll)
7407{
7408 struct intel_crtc *crtc =
7409 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 7410 struct intel_crtc_state pipe_config = {
a93e255f 7411 .base.crtc = &crtc->base,
d288f65f
VS
7412 .pixel_multiplier = 1,
7413 .dpll = *dpll,
7414 };
7415
7416 if (IS_CHERRYVIEW(dev)) {
251ac862 7417 chv_compute_dpll(crtc, &pipe_config);
d288f65f
VS
7418 chv_prepare_pll(crtc, &pipe_config);
7419 chv_enable_pll(crtc, &pipe_config);
7420 } else {
251ac862 7421 vlv_compute_dpll(crtc, &pipe_config);
d288f65f
VS
7422 vlv_prepare_pll(crtc, &pipe_config);
7423 vlv_enable_pll(crtc, &pipe_config);
7424 }
7425}
7426
7427/**
7428 * vlv_force_pll_off - forcibly disable just the PLL
7429 * @dev_priv: i915 private structure
7430 * @pipe: pipe PLL to disable
7431 *
7432 * Disable the PLL for @pipe. To be used in cases where we need
7433 * the PLL enabled even when @pipe is not going to be enabled.
7434 */
7435void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7436{
7437 if (IS_CHERRYVIEW(dev))
7438 chv_disable_pll(to_i915(dev), pipe);
7439 else
7440 vlv_disable_pll(to_i915(dev), pipe);
7441}
7442
251ac862
DV
7443static void i9xx_compute_dpll(struct intel_crtc *crtc,
7444 struct intel_crtc_state *crtc_state,
7445 intel_clock_t *reduced_clock,
7446 int num_connectors)
eb1cbe48 7447{
f47709a9 7448 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7449 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7450 u32 dpll;
7451 bool is_sdvo;
190f68c5 7452 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7453
190f68c5 7454 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7455
a93e255f
ACO
7456 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7457 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7458
7459 dpll = DPLL_VGA_MODE_DIS;
7460
a93e255f 7461 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7462 dpll |= DPLLB_MODE_LVDS;
7463 else
7464 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7465
ef1b460d 7466 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7467 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7468 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7469 }
198a037f
DV
7470
7471 if (is_sdvo)
4a33e48d 7472 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7473
190f68c5 7474 if (crtc_state->has_dp_encoder)
4a33e48d 7475 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7476
7477 /* compute bitmask from p1 value */
7478 if (IS_PINEVIEW(dev))
7479 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7480 else {
7481 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7482 if (IS_G4X(dev) && reduced_clock)
7483 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7484 }
7485 switch (clock->p2) {
7486 case 5:
7487 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7488 break;
7489 case 7:
7490 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7491 break;
7492 case 10:
7493 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7494 break;
7495 case 14:
7496 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7497 break;
7498 }
7499 if (INTEL_INFO(dev)->gen >= 4)
7500 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7501
190f68c5 7502 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7503 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7504 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7505 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7506 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7507 else
7508 dpll |= PLL_REF_INPUT_DREFCLK;
7509
7510 dpll |= DPLL_VCO_ENABLE;
190f68c5 7511 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7512
eb1cbe48 7513 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7514 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7515 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7516 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7517 }
7518}
7519
251ac862
DV
7520static void i8xx_compute_dpll(struct intel_crtc *crtc,
7521 struct intel_crtc_state *crtc_state,
7522 intel_clock_t *reduced_clock,
7523 int num_connectors)
eb1cbe48 7524{
f47709a9 7525 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7526 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7527 u32 dpll;
190f68c5 7528 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7529
190f68c5 7530 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7531
eb1cbe48
DV
7532 dpll = DPLL_VGA_MODE_DIS;
7533
a93e255f 7534 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7535 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7536 } else {
7537 if (clock->p1 == 2)
7538 dpll |= PLL_P1_DIVIDE_BY_TWO;
7539 else
7540 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7541 if (clock->p2 == 4)
7542 dpll |= PLL_P2_DIVIDE_BY_4;
7543 }
7544
a93e255f 7545 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7546 dpll |= DPLL_DVO_2X_MODE;
7547
a93e255f 7548 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7549 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7550 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7551 else
7552 dpll |= PLL_REF_INPUT_DREFCLK;
7553
7554 dpll |= DPLL_VCO_ENABLE;
190f68c5 7555 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7556}
7557
8a654f3b 7558static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7559{
7560 struct drm_device *dev = intel_crtc->base.dev;
7561 struct drm_i915_private *dev_priv = dev->dev_private;
7562 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7563 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 7564 struct drm_display_mode *adjusted_mode =
6e3c9717 7565 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7566 uint32_t crtc_vtotal, crtc_vblank_end;
7567 int vsyncshift = 0;
4d8a62ea
DV
7568
7569 /* We need to be careful not to changed the adjusted mode, for otherwise
7570 * the hw state checker will get angry at the mismatch. */
7571 crtc_vtotal = adjusted_mode->crtc_vtotal;
7572 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7573
609aeaca 7574 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7575 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7576 crtc_vtotal -= 1;
7577 crtc_vblank_end -= 1;
609aeaca 7578
409ee761 7579 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7580 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7581 else
7582 vsyncshift = adjusted_mode->crtc_hsync_start -
7583 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7584 if (vsyncshift < 0)
7585 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7586 }
7587
7588 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7589 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7590
fe2b8f9d 7591 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7592 (adjusted_mode->crtc_hdisplay - 1) |
7593 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7594 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7595 (adjusted_mode->crtc_hblank_start - 1) |
7596 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7597 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7598 (adjusted_mode->crtc_hsync_start - 1) |
7599 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7600
fe2b8f9d 7601 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7602 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7603 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7604 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7605 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7606 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7607 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7608 (adjusted_mode->crtc_vsync_start - 1) |
7609 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7610
b5e508d4
PZ
7611 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7612 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7613 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7614 * bits. */
7615 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7616 (pipe == PIPE_B || pipe == PIPE_C))
7617 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7618
b0e77b9c
PZ
7619 /* pipesrc controls the size that is scaled from, which should
7620 * always be the user's requested size.
7621 */
7622 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7623 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7624 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7625}
7626
1bd1bd80 7627static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7628 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7629{
7630 struct drm_device *dev = crtc->base.dev;
7631 struct drm_i915_private *dev_priv = dev->dev_private;
7632 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7633 uint32_t tmp;
7634
7635 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7636 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7637 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7638 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7639 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7640 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7641 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7642 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7643 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7644
7645 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7646 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7647 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7648 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7649 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7650 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7651 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7652 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7653 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7654
7655 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7656 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7657 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7658 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7659 }
7660
7661 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7662 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7663 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7664
2d112de7
ACO
7665 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7666 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7667}
7668
f6a83288 7669void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7670 struct intel_crtc_state *pipe_config)
babea61d 7671{
2d112de7
ACO
7672 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7673 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7674 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7675 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7676
2d112de7
ACO
7677 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7678 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7679 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7680 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7681
2d112de7 7682 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 7683
2d112de7
ACO
7684 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7685 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
7686}
7687
84b046f3
DV
7688static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7689{
7690 struct drm_device *dev = intel_crtc->base.dev;
7691 struct drm_i915_private *dev_priv = dev->dev_private;
7692 uint32_t pipeconf;
7693
9f11a9e4 7694 pipeconf = 0;
84b046f3 7695
b6b5d049
VS
7696 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7697 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7698 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7699
6e3c9717 7700 if (intel_crtc->config->double_wide)
cf532bb2 7701 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7702
ff9ce46e
DV
7703 /* only g4x and later have fancy bpc/dither controls */
7704 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 7705 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7706 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7707 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7708 PIPECONF_DITHER_TYPE_SP;
84b046f3 7709
6e3c9717 7710 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7711 case 18:
7712 pipeconf |= PIPECONF_6BPC;
7713 break;
7714 case 24:
7715 pipeconf |= PIPECONF_8BPC;
7716 break;
7717 case 30:
7718 pipeconf |= PIPECONF_10BPC;
7719 break;
7720 default:
7721 /* Case prevented by intel_choose_pipe_bpp_dither. */
7722 BUG();
84b046f3
DV
7723 }
7724 }
7725
7726 if (HAS_PIPE_CXSR(dev)) {
7727 if (intel_crtc->lowfreq_avail) {
7728 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7729 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7730 } else {
7731 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7732 }
7733 }
7734
6e3c9717 7735 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7736 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7737 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7738 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7739 else
7740 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7741 } else
84b046f3
DV
7742 pipeconf |= PIPECONF_PROGRESSIVE;
7743
6e3c9717 7744 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 7745 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7746
84b046f3
DV
7747 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7748 POSTING_READ(PIPECONF(intel_crtc->pipe));
7749}
7750
190f68c5
ACO
7751static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7752 struct intel_crtc_state *crtc_state)
79e53945 7753{
c7653199 7754 struct drm_device *dev = crtc->base.dev;
79e53945 7755 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7756 int refclk, num_connectors = 0;
c329a4ec
DV
7757 intel_clock_t clock;
7758 bool ok;
7759 bool is_dsi = false;
5eddb70b 7760 struct intel_encoder *encoder;
d4906093 7761 const intel_limit_t *limit;
55bb9992 7762 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 7763 struct drm_connector *connector;
55bb9992
ACO
7764 struct drm_connector_state *connector_state;
7765 int i;
79e53945 7766
dd3cd74a
ACO
7767 memset(&crtc_state->dpll_hw_state, 0,
7768 sizeof(crtc_state->dpll_hw_state));
7769
da3ced29 7770 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
7771 if (connector_state->crtc != &crtc->base)
7772 continue;
7773
7774 encoder = to_intel_encoder(connector_state->best_encoder);
7775
5eddb70b 7776 switch (encoder->type) {
e9fd1c02
JN
7777 case INTEL_OUTPUT_DSI:
7778 is_dsi = true;
7779 break;
6847d71b
PZ
7780 default:
7781 break;
79e53945 7782 }
43565a06 7783
c751ce4f 7784 num_connectors++;
79e53945
JB
7785 }
7786
f2335330 7787 if (is_dsi)
5b18e57c 7788 return 0;
f2335330 7789
190f68c5 7790 if (!crtc_state->clock_set) {
a93e255f 7791 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7792
e9fd1c02
JN
7793 /*
7794 * Returns a set of divisors for the desired target clock with
7795 * the given refclk, or FALSE. The returned values represent
7796 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7797 * 2) / p1 / p2.
7798 */
a93e255f
ACO
7799 limit = intel_limit(crtc_state, refclk);
7800 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7801 crtc_state->port_clock,
e9fd1c02 7802 refclk, NULL, &clock);
f2335330 7803 if (!ok) {
e9fd1c02
JN
7804 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7805 return -EINVAL;
7806 }
79e53945 7807
f2335330 7808 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7809 crtc_state->dpll.n = clock.n;
7810 crtc_state->dpll.m1 = clock.m1;
7811 crtc_state->dpll.m2 = clock.m2;
7812 crtc_state->dpll.p1 = clock.p1;
7813 crtc_state->dpll.p2 = clock.p2;
f47709a9 7814 }
7026d4ac 7815
e9fd1c02 7816 if (IS_GEN2(dev)) {
c329a4ec 7817 i8xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7818 num_connectors);
9d556c99 7819 } else if (IS_CHERRYVIEW(dev)) {
251ac862 7820 chv_compute_dpll(crtc, crtc_state);
e9fd1c02 7821 } else if (IS_VALLEYVIEW(dev)) {
251ac862 7822 vlv_compute_dpll(crtc, crtc_state);
e9fd1c02 7823 } else {
c329a4ec 7824 i9xx_compute_dpll(crtc, crtc_state, NULL,
251ac862 7825 num_connectors);
e9fd1c02 7826 }
79e53945 7827
c8f7a0db 7828 return 0;
f564048e
EA
7829}
7830
2fa2fe9a 7831static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7832 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7833{
7834 struct drm_device *dev = crtc->base.dev;
7835 struct drm_i915_private *dev_priv = dev->dev_private;
7836 uint32_t tmp;
7837
dc9e7dec
VS
7838 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7839 return;
7840
2fa2fe9a 7841 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7842 if (!(tmp & PFIT_ENABLE))
7843 return;
2fa2fe9a 7844
06922821 7845 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7846 if (INTEL_INFO(dev)->gen < 4) {
7847 if (crtc->pipe != PIPE_B)
7848 return;
2fa2fe9a
DV
7849 } else {
7850 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7851 return;
7852 }
7853
06922821 7854 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7855 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7856 if (INTEL_INFO(dev)->gen < 5)
7857 pipe_config->gmch_pfit.lvds_border_bits =
7858 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7859}
7860
acbec814 7861static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7862 struct intel_crtc_state *pipe_config)
acbec814
JB
7863{
7864 struct drm_device *dev = crtc->base.dev;
7865 struct drm_i915_private *dev_priv = dev->dev_private;
7866 int pipe = pipe_config->cpu_transcoder;
7867 intel_clock_t clock;
7868 u32 mdiv;
662c6ecb 7869 int refclk = 100000;
acbec814 7870
f573de5a
SK
7871 /* In case of MIPI DPLL will not even be used */
7872 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7873 return;
7874
a580516d 7875 mutex_lock(&dev_priv->sb_lock);
ab3c759a 7876 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
a580516d 7877 mutex_unlock(&dev_priv->sb_lock);
acbec814
JB
7878
7879 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7880 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7881 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7882 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7883 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7884
f646628b 7885 vlv_clock(refclk, &clock);
acbec814 7886
f646628b
VS
7887 /* clock.dot is the fast clock */
7888 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
7889}
7890
5724dbd1
DL
7891static void
7892i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7893 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7894{
7895 struct drm_device *dev = crtc->base.dev;
7896 struct drm_i915_private *dev_priv = dev->dev_private;
7897 u32 val, base, offset;
7898 int pipe = crtc->pipe, plane = crtc->plane;
7899 int fourcc, pixel_format;
6761dd31 7900 unsigned int aligned_height;
b113d5ee 7901 struct drm_framebuffer *fb;
1b842c89 7902 struct intel_framebuffer *intel_fb;
1ad292b5 7903
42a7b088
DL
7904 val = I915_READ(DSPCNTR(plane));
7905 if (!(val & DISPLAY_PLANE_ENABLE))
7906 return;
7907
d9806c9f 7908 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7909 if (!intel_fb) {
1ad292b5
JB
7910 DRM_DEBUG_KMS("failed to alloc fb\n");
7911 return;
7912 }
7913
1b842c89
DL
7914 fb = &intel_fb->base;
7915
18c5247e
DV
7916 if (INTEL_INFO(dev)->gen >= 4) {
7917 if (val & DISPPLANE_TILED) {
49af449b 7918 plane_config->tiling = I915_TILING_X;
18c5247e
DV
7919 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7920 }
7921 }
1ad292b5
JB
7922
7923 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7924 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
7925 fb->pixel_format = fourcc;
7926 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
7927
7928 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 7929 if (plane_config->tiling)
1ad292b5
JB
7930 offset = I915_READ(DSPTILEOFF(plane));
7931 else
7932 offset = I915_READ(DSPLINOFF(plane));
7933 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7934 } else {
7935 base = I915_READ(DSPADDR(plane));
7936 }
7937 plane_config->base = base;
7938
7939 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7940 fb->width = ((val >> 16) & 0xfff) + 1;
7941 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7942
7943 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7944 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7945
b113d5ee 7946 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
7947 fb->pixel_format,
7948 fb->modifier[0]);
1ad292b5 7949
f37b5c2b 7950 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7951
2844a921
DL
7952 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7953 pipe_name(pipe), plane, fb->width, fb->height,
7954 fb->bits_per_pixel, base, fb->pitches[0],
7955 plane_config->size);
1ad292b5 7956
2d14030b 7957 plane_config->fb = intel_fb;
1ad292b5
JB
7958}
7959
70b23a98 7960static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7961 struct intel_crtc_state *pipe_config)
70b23a98
VS
7962{
7963 struct drm_device *dev = crtc->base.dev;
7964 struct drm_i915_private *dev_priv = dev->dev_private;
7965 int pipe = pipe_config->cpu_transcoder;
7966 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7967 intel_clock_t clock;
7968 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7969 int refclk = 100000;
7970
a580516d 7971 mutex_lock(&dev_priv->sb_lock);
70b23a98
VS
7972 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7973 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7974 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7975 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
a580516d 7976 mutex_unlock(&dev_priv->sb_lock);
70b23a98
VS
7977
7978 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7979 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7980 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7981 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7982 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7983
7984 chv_clock(refclk, &clock);
7985
7986 /* clock.dot is the fast clock */
7987 pipe_config->port_clock = clock.dot / 5;
7988}
7989
0e8ffe1b 7990static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7991 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
7992{
7993 struct drm_device *dev = crtc->base.dev;
7994 struct drm_i915_private *dev_priv = dev->dev_private;
7995 uint32_t tmp;
7996
f458ebbc
DV
7997 if (!intel_display_power_is_enabled(dev_priv,
7998 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
7999 return false;
8000
e143a21c 8001 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8002 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8003
0e8ffe1b
DV
8004 tmp = I915_READ(PIPECONF(crtc->pipe));
8005 if (!(tmp & PIPECONF_ENABLE))
8006 return false;
8007
42571aef
VS
8008 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8009 switch (tmp & PIPECONF_BPC_MASK) {
8010 case PIPECONF_6BPC:
8011 pipe_config->pipe_bpp = 18;
8012 break;
8013 case PIPECONF_8BPC:
8014 pipe_config->pipe_bpp = 24;
8015 break;
8016 case PIPECONF_10BPC:
8017 pipe_config->pipe_bpp = 30;
8018 break;
8019 default:
8020 break;
8021 }
8022 }
8023
b5a9fa09
DV
8024 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8025 pipe_config->limited_color_range = true;
8026
282740f7
VS
8027 if (INTEL_INFO(dev)->gen < 4)
8028 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8029
1bd1bd80
DV
8030 intel_get_pipe_timings(crtc, pipe_config);
8031
2fa2fe9a
DV
8032 i9xx_get_pfit_config(crtc, pipe_config);
8033
6c49f241
DV
8034 if (INTEL_INFO(dev)->gen >= 4) {
8035 tmp = I915_READ(DPLL_MD(crtc->pipe));
8036 pipe_config->pixel_multiplier =
8037 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8038 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 8039 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
8040 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8041 tmp = I915_READ(DPLL(crtc->pipe));
8042 pipe_config->pixel_multiplier =
8043 ((tmp & SDVO_MULTIPLIER_MASK)
8044 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8045 } else {
8046 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8047 * port and will be fixed up in the encoder->get_config
8048 * function. */
8049 pipe_config->pixel_multiplier = 1;
8050 }
8bcc2795
DV
8051 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8052 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
8053 /*
8054 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8055 * on 830. Filter it out here so that we don't
8056 * report errors due to that.
8057 */
8058 if (IS_I830(dev))
8059 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8060
8bcc2795
DV
8061 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8062 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
8063 } else {
8064 /* Mask out read-only status bits. */
8065 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8066 DPLL_PORTC_READY_MASK |
8067 DPLL_PORTB_READY_MASK);
8bcc2795 8068 }
6c49f241 8069
70b23a98
VS
8070 if (IS_CHERRYVIEW(dev))
8071 chv_crtc_clock_get(crtc, pipe_config);
8072 else if (IS_VALLEYVIEW(dev))
acbec814
JB
8073 vlv_crtc_clock_get(crtc, pipe_config);
8074 else
8075 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 8076
0e8ffe1b
DV
8077 return true;
8078}
8079
dde86e2d 8080static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
8081{
8082 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 8083 struct intel_encoder *encoder;
74cfd7ac 8084 u32 val, final;
13d83a67 8085 bool has_lvds = false;
199e5d79 8086 bool has_cpu_edp = false;
199e5d79 8087 bool has_panel = false;
99eb6a01
KP
8088 bool has_ck505 = false;
8089 bool can_ssc = false;
13d83a67
JB
8090
8091 /* We need to take the global config into account */
b2784e15 8092 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
8093 switch (encoder->type) {
8094 case INTEL_OUTPUT_LVDS:
8095 has_panel = true;
8096 has_lvds = true;
8097 break;
8098 case INTEL_OUTPUT_EDP:
8099 has_panel = true;
2de6905f 8100 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
8101 has_cpu_edp = true;
8102 break;
6847d71b
PZ
8103 default:
8104 break;
13d83a67
JB
8105 }
8106 }
8107
99eb6a01 8108 if (HAS_PCH_IBX(dev)) {
41aa3448 8109 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
8110 can_ssc = has_ck505;
8111 } else {
8112 has_ck505 = false;
8113 can_ssc = true;
8114 }
8115
2de6905f
ID
8116 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8117 has_panel, has_lvds, has_ck505);
13d83a67
JB
8118
8119 /* Ironlake: try to setup display ref clock before DPLL
8120 * enabling. This is only under driver's control after
8121 * PCH B stepping, previous chipset stepping should be
8122 * ignoring this setting.
8123 */
74cfd7ac
CW
8124 val = I915_READ(PCH_DREF_CONTROL);
8125
8126 /* As we must carefully and slowly disable/enable each source in turn,
8127 * compute the final state we want first and check if we need to
8128 * make any changes at all.
8129 */
8130 final = val;
8131 final &= ~DREF_NONSPREAD_SOURCE_MASK;
8132 if (has_ck505)
8133 final |= DREF_NONSPREAD_CK505_ENABLE;
8134 else
8135 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8136
8137 final &= ~DREF_SSC_SOURCE_MASK;
8138 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8139 final &= ~DREF_SSC1_ENABLE;
8140
8141 if (has_panel) {
8142 final |= DREF_SSC_SOURCE_ENABLE;
8143
8144 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8145 final |= DREF_SSC1_ENABLE;
8146
8147 if (has_cpu_edp) {
8148 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8149 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8150 else
8151 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8152 } else
8153 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8154 } else {
8155 final |= DREF_SSC_SOURCE_DISABLE;
8156 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8157 }
8158
8159 if (final == val)
8160 return;
8161
13d83a67 8162 /* Always enable nonspread source */
74cfd7ac 8163 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 8164
99eb6a01 8165 if (has_ck505)
74cfd7ac 8166 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 8167 else
74cfd7ac 8168 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 8169
199e5d79 8170 if (has_panel) {
74cfd7ac
CW
8171 val &= ~DREF_SSC_SOURCE_MASK;
8172 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 8173
199e5d79 8174 /* SSC must be turned on before enabling the CPU output */
99eb6a01 8175 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8176 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 8177 val |= DREF_SSC1_ENABLE;
e77166b5 8178 } else
74cfd7ac 8179 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
8180
8181 /* Get SSC going before enabling the outputs */
74cfd7ac 8182 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8183 POSTING_READ(PCH_DREF_CONTROL);
8184 udelay(200);
8185
74cfd7ac 8186 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
8187
8188 /* Enable CPU source on CPU attached eDP */
199e5d79 8189 if (has_cpu_edp) {
99eb6a01 8190 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 8191 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 8192 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 8193 } else
74cfd7ac 8194 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 8195 } else
74cfd7ac 8196 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8197
74cfd7ac 8198 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8199 POSTING_READ(PCH_DREF_CONTROL);
8200 udelay(200);
8201 } else {
8202 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8203
74cfd7ac 8204 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
8205
8206 /* Turn off CPU output */
74cfd7ac 8207 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 8208
74cfd7ac 8209 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
8210 POSTING_READ(PCH_DREF_CONTROL);
8211 udelay(200);
8212
8213 /* Turn off the SSC source */
74cfd7ac
CW
8214 val &= ~DREF_SSC_SOURCE_MASK;
8215 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
8216
8217 /* Turn off SSC1 */
74cfd7ac 8218 val &= ~DREF_SSC1_ENABLE;
199e5d79 8219
74cfd7ac 8220 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
8221 POSTING_READ(PCH_DREF_CONTROL);
8222 udelay(200);
8223 }
74cfd7ac
CW
8224
8225 BUG_ON(val != final);
13d83a67
JB
8226}
8227
f31f2d55 8228static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 8229{
f31f2d55 8230 uint32_t tmp;
dde86e2d 8231
0ff066a9
PZ
8232 tmp = I915_READ(SOUTH_CHICKEN2);
8233 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8234 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8235
0ff066a9
PZ
8236 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8237 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8238 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 8239
0ff066a9
PZ
8240 tmp = I915_READ(SOUTH_CHICKEN2);
8241 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8242 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 8243
0ff066a9
PZ
8244 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8245 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8246 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
8247}
8248
8249/* WaMPhyProgramming:hsw */
8250static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8251{
8252 uint32_t tmp;
dde86e2d
PZ
8253
8254 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8255 tmp &= ~(0xFF << 24);
8256 tmp |= (0x12 << 24);
8257 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8258
dde86e2d
PZ
8259 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8260 tmp |= (1 << 11);
8261 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8262
8263 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8264 tmp |= (1 << 11);
8265 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8266
dde86e2d
PZ
8267 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8268 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8269 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8270
8271 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8272 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8273 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8274
0ff066a9
PZ
8275 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8276 tmp &= ~(7 << 13);
8277 tmp |= (5 << 13);
8278 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8279
0ff066a9
PZ
8280 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8281 tmp &= ~(7 << 13);
8282 tmp |= (5 << 13);
8283 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8284
8285 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8286 tmp &= ~0xFF;
8287 tmp |= 0x1C;
8288 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8289
8290 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8291 tmp &= ~0xFF;
8292 tmp |= 0x1C;
8293 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8294
8295 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8296 tmp &= ~(0xFF << 16);
8297 tmp |= (0x1C << 16);
8298 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8299
8300 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8301 tmp &= ~(0xFF << 16);
8302 tmp |= (0x1C << 16);
8303 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8304
0ff066a9
PZ
8305 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8306 tmp |= (1 << 27);
8307 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8308
0ff066a9
PZ
8309 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8310 tmp |= (1 << 27);
8311 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8312
0ff066a9
PZ
8313 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8314 tmp &= ~(0xF << 28);
8315 tmp |= (4 << 28);
8316 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8317
0ff066a9
PZ
8318 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8319 tmp &= ~(0xF << 28);
8320 tmp |= (4 << 28);
8321 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8322}
8323
2fa86a1f
PZ
8324/* Implements 3 different sequences from BSpec chapter "Display iCLK
8325 * Programming" based on the parameters passed:
8326 * - Sequence to enable CLKOUT_DP
8327 * - Sequence to enable CLKOUT_DP without spread
8328 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8329 */
8330static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8331 bool with_fdi)
f31f2d55
PZ
8332{
8333 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8334 uint32_t reg, tmp;
8335
8336 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8337 with_spread = true;
8338 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8339 with_fdi, "LP PCH doesn't have FDI\n"))
8340 with_fdi = false;
f31f2d55 8341
a580516d 8342 mutex_lock(&dev_priv->sb_lock);
f31f2d55
PZ
8343
8344 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8345 tmp &= ~SBI_SSCCTL_DISABLE;
8346 tmp |= SBI_SSCCTL_PATHALT;
8347 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8348
8349 udelay(24);
8350
2fa86a1f
PZ
8351 if (with_spread) {
8352 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8353 tmp &= ~SBI_SSCCTL_PATHALT;
8354 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8355
2fa86a1f
PZ
8356 if (with_fdi) {
8357 lpt_reset_fdi_mphy(dev_priv);
8358 lpt_program_fdi_mphy(dev_priv);
8359 }
8360 }
dde86e2d 8361
2fa86a1f
PZ
8362 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8363 SBI_GEN0 : SBI_DBUFF0;
8364 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8365 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8366 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246 8367
a580516d 8368 mutex_unlock(&dev_priv->sb_lock);
dde86e2d
PZ
8369}
8370
47701c3b
PZ
8371/* Sequence to disable CLKOUT_DP */
8372static void lpt_disable_clkout_dp(struct drm_device *dev)
8373{
8374 struct drm_i915_private *dev_priv = dev->dev_private;
8375 uint32_t reg, tmp;
8376
a580516d 8377 mutex_lock(&dev_priv->sb_lock);
47701c3b
PZ
8378
8379 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8380 SBI_GEN0 : SBI_DBUFF0;
8381 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8382 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8383 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8384
8385 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8386 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8387 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8388 tmp |= SBI_SSCCTL_PATHALT;
8389 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8390 udelay(32);
8391 }
8392 tmp |= SBI_SSCCTL_DISABLE;
8393 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8394 }
8395
a580516d 8396 mutex_unlock(&dev_priv->sb_lock);
47701c3b
PZ
8397}
8398
bf8fa3d3
PZ
8399static void lpt_init_pch_refclk(struct drm_device *dev)
8400{
bf8fa3d3
PZ
8401 struct intel_encoder *encoder;
8402 bool has_vga = false;
8403
b2784e15 8404 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8405 switch (encoder->type) {
8406 case INTEL_OUTPUT_ANALOG:
8407 has_vga = true;
8408 break;
6847d71b
PZ
8409 default:
8410 break;
bf8fa3d3
PZ
8411 }
8412 }
8413
47701c3b
PZ
8414 if (has_vga)
8415 lpt_enable_clkout_dp(dev, true, true);
8416 else
8417 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
8418}
8419
dde86e2d
PZ
8420/*
8421 * Initialize reference clocks when the driver loads
8422 */
8423void intel_init_pch_refclk(struct drm_device *dev)
8424{
8425 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8426 ironlake_init_pch_refclk(dev);
8427 else if (HAS_PCH_LPT(dev))
8428 lpt_init_pch_refclk(dev);
8429}
8430
55bb9992 8431static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8432{
55bb9992 8433 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8434 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8435 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8436 struct drm_connector *connector;
55bb9992 8437 struct drm_connector_state *connector_state;
d9d444cb 8438 struct intel_encoder *encoder;
55bb9992 8439 int num_connectors = 0, i;
d9d444cb
JB
8440 bool is_lvds = false;
8441
da3ced29 8442 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8443 if (connector_state->crtc != crtc_state->base.crtc)
8444 continue;
8445
8446 encoder = to_intel_encoder(connector_state->best_encoder);
8447
d9d444cb
JB
8448 switch (encoder->type) {
8449 case INTEL_OUTPUT_LVDS:
8450 is_lvds = true;
8451 break;
6847d71b
PZ
8452 default:
8453 break;
d9d444cb
JB
8454 }
8455 num_connectors++;
8456 }
8457
8458 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8459 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8460 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8461 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8462 }
8463
8464 return 120000;
8465}
8466
6ff93609 8467static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8468{
c8203565 8469 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8470 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8471 int pipe = intel_crtc->pipe;
c8203565
PZ
8472 uint32_t val;
8473
78114071 8474 val = 0;
c8203565 8475
6e3c9717 8476 switch (intel_crtc->config->pipe_bpp) {
c8203565 8477 case 18:
dfd07d72 8478 val |= PIPECONF_6BPC;
c8203565
PZ
8479 break;
8480 case 24:
dfd07d72 8481 val |= PIPECONF_8BPC;
c8203565
PZ
8482 break;
8483 case 30:
dfd07d72 8484 val |= PIPECONF_10BPC;
c8203565
PZ
8485 break;
8486 case 36:
dfd07d72 8487 val |= PIPECONF_12BPC;
c8203565
PZ
8488 break;
8489 default:
cc769b62
PZ
8490 /* Case prevented by intel_choose_pipe_bpp_dither. */
8491 BUG();
c8203565
PZ
8492 }
8493
6e3c9717 8494 if (intel_crtc->config->dither)
c8203565
PZ
8495 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8496
6e3c9717 8497 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8498 val |= PIPECONF_INTERLACED_ILK;
8499 else
8500 val |= PIPECONF_PROGRESSIVE;
8501
6e3c9717 8502 if (intel_crtc->config->limited_color_range)
3685a8f3 8503 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8504
c8203565
PZ
8505 I915_WRITE(PIPECONF(pipe), val);
8506 POSTING_READ(PIPECONF(pipe));
8507}
8508
86d3efce
VS
8509/*
8510 * Set up the pipe CSC unit.
8511 *
8512 * Currently only full range RGB to limited range RGB conversion
8513 * is supported, but eventually this should handle various
8514 * RGB<->YCbCr scenarios as well.
8515 */
50f3b016 8516static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8517{
8518 struct drm_device *dev = crtc->dev;
8519 struct drm_i915_private *dev_priv = dev->dev_private;
8520 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8521 int pipe = intel_crtc->pipe;
8522 uint16_t coeff = 0x7800; /* 1.0 */
8523
8524 /*
8525 * TODO: Check what kind of values actually come out of the pipe
8526 * with these coeff/postoff values and adjust to get the best
8527 * accuracy. Perhaps we even need to take the bpc value into
8528 * consideration.
8529 */
8530
6e3c9717 8531 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8532 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8533
8534 /*
8535 * GY/GU and RY/RU should be the other way around according
8536 * to BSpec, but reality doesn't agree. Just set them up in
8537 * a way that results in the correct picture.
8538 */
8539 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8540 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8541
8542 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8543 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8544
8545 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8546 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8547
8548 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8549 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8550 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8551
8552 if (INTEL_INFO(dev)->gen > 6) {
8553 uint16_t postoff = 0;
8554
6e3c9717 8555 if (intel_crtc->config->limited_color_range)
32cf0cb0 8556 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8557
8558 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8559 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8560 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8561
8562 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8563 } else {
8564 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8565
6e3c9717 8566 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8567 mode |= CSC_BLACK_SCREEN_OFFSET;
8568
8569 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8570 }
8571}
8572
6ff93609 8573static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8574{
756f85cf
PZ
8575 struct drm_device *dev = crtc->dev;
8576 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 8577 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8578 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8579 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
8580 uint32_t val;
8581
3eff4faa 8582 val = 0;
ee2b0b38 8583
6e3c9717 8584 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
8585 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8586
6e3c9717 8587 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8588 val |= PIPECONF_INTERLACED_ILK;
8589 else
8590 val |= PIPECONF_PROGRESSIVE;
8591
702e7a56
PZ
8592 I915_WRITE(PIPECONF(cpu_transcoder), val);
8593 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
8594
8595 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8596 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 8597
3cdf122c 8598 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
8599 val = 0;
8600
6e3c9717 8601 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8602 case 18:
8603 val |= PIPEMISC_DITHER_6_BPC;
8604 break;
8605 case 24:
8606 val |= PIPEMISC_DITHER_8_BPC;
8607 break;
8608 case 30:
8609 val |= PIPEMISC_DITHER_10_BPC;
8610 break;
8611 case 36:
8612 val |= PIPEMISC_DITHER_12_BPC;
8613 break;
8614 default:
8615 /* Case prevented by pipe_config_set_bpp. */
8616 BUG();
8617 }
8618
6e3c9717 8619 if (intel_crtc->config->dither)
756f85cf
PZ
8620 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8621
8622 I915_WRITE(PIPEMISC(pipe), val);
8623 }
ee2b0b38
PZ
8624}
8625
6591c6e4 8626static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8627 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8628 intel_clock_t *clock,
8629 bool *has_reduced_clock,
8630 intel_clock_t *reduced_clock)
8631{
8632 struct drm_device *dev = crtc->dev;
8633 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8634 int refclk;
d4906093 8635 const intel_limit_t *limit;
c329a4ec 8636 bool ret;
79e53945 8637
55bb9992 8638 refclk = ironlake_get_refclk(crtc_state);
79e53945 8639
d4906093
ML
8640 /*
8641 * Returns a set of divisors for the desired target clock with the given
8642 * refclk, or FALSE. The returned values represent the clock equation:
8643 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8644 */
a93e255f
ACO
8645 limit = intel_limit(crtc_state, refclk);
8646 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8647 crtc_state->port_clock,
ee9300bb 8648 refclk, NULL, clock);
6591c6e4
PZ
8649 if (!ret)
8650 return false;
cda4b7d3 8651
6591c6e4
PZ
8652 return true;
8653}
8654
d4b1931c
PZ
8655int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8656{
8657 /*
8658 * Account for spread spectrum to avoid
8659 * oversubscribing the link. Max center spread
8660 * is 2.5%; use 5% for safety's sake.
8661 */
8662 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8663 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8664}
8665
7429e9d4 8666static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8667{
7429e9d4 8668 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8669}
8670
de13a2e3 8671static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8672 struct intel_crtc_state *crtc_state,
7429e9d4 8673 u32 *fp,
9a7c7890 8674 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8675{
de13a2e3 8676 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8677 struct drm_device *dev = crtc->dev;
8678 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8679 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8680 struct drm_connector *connector;
55bb9992
ACO
8681 struct drm_connector_state *connector_state;
8682 struct intel_encoder *encoder;
de13a2e3 8683 uint32_t dpll;
55bb9992 8684 int factor, num_connectors = 0, i;
09ede541 8685 bool is_lvds = false, is_sdvo = false;
79e53945 8686
da3ced29 8687 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8688 if (connector_state->crtc != crtc_state->base.crtc)
8689 continue;
8690
8691 encoder = to_intel_encoder(connector_state->best_encoder);
8692
8693 switch (encoder->type) {
79e53945
JB
8694 case INTEL_OUTPUT_LVDS:
8695 is_lvds = true;
8696 break;
8697 case INTEL_OUTPUT_SDVO:
7d57382e 8698 case INTEL_OUTPUT_HDMI:
79e53945 8699 is_sdvo = true;
79e53945 8700 break;
6847d71b
PZ
8701 default:
8702 break;
79e53945 8703 }
43565a06 8704
c751ce4f 8705 num_connectors++;
79e53945 8706 }
79e53945 8707
c1858123 8708 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8709 factor = 21;
8710 if (is_lvds) {
8711 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8712 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8713 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8714 factor = 25;
190f68c5 8715 } else if (crtc_state->sdvo_tv_clock)
8febb297 8716 factor = 20;
c1858123 8717
190f68c5 8718 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8719 *fp |= FP_CB_TUNE;
2c07245f 8720
9a7c7890
DV
8721 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8722 *fp2 |= FP_CB_TUNE;
8723
5eddb70b 8724 dpll = 0;
2c07245f 8725
a07d6787
EA
8726 if (is_lvds)
8727 dpll |= DPLLB_MODE_LVDS;
8728 else
8729 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8730
190f68c5 8731 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8732 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8733
8734 if (is_sdvo)
4a33e48d 8735 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8736 if (crtc_state->has_dp_encoder)
4a33e48d 8737 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8738
a07d6787 8739 /* compute bitmask from p1 value */
190f68c5 8740 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8741 /* also FPA1 */
190f68c5 8742 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8743
190f68c5 8744 switch (crtc_state->dpll.p2) {
a07d6787
EA
8745 case 5:
8746 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8747 break;
8748 case 7:
8749 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8750 break;
8751 case 10:
8752 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8753 break;
8754 case 14:
8755 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8756 break;
79e53945
JB
8757 }
8758
b4c09f3b 8759 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8760 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8761 else
8762 dpll |= PLL_REF_INPUT_DREFCLK;
8763
959e16d6 8764 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8765}
8766
190f68c5
ACO
8767static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8768 struct intel_crtc_state *crtc_state)
de13a2e3 8769{
c7653199 8770 struct drm_device *dev = crtc->base.dev;
de13a2e3 8771 intel_clock_t clock, reduced_clock;
cbbab5bd 8772 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8773 bool ok, has_reduced_clock = false;
8b47047b 8774 bool is_lvds = false;
e2b78267 8775 struct intel_shared_dpll *pll;
de13a2e3 8776
dd3cd74a
ACO
8777 memset(&crtc_state->dpll_hw_state, 0,
8778 sizeof(crtc_state->dpll_hw_state));
8779
409ee761 8780 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 8781
5dc5298b
PZ
8782 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8783 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8784
190f68c5 8785 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8786 &has_reduced_clock, &reduced_clock);
190f68c5 8787 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8788 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8789 return -EINVAL;
79e53945 8790 }
f47709a9 8791 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8792 if (!crtc_state->clock_set) {
8793 crtc_state->dpll.n = clock.n;
8794 crtc_state->dpll.m1 = clock.m1;
8795 crtc_state->dpll.m2 = clock.m2;
8796 crtc_state->dpll.p1 = clock.p1;
8797 crtc_state->dpll.p2 = clock.p2;
f47709a9 8798 }
79e53945 8799
5dc5298b 8800 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8801 if (crtc_state->has_pch_encoder) {
8802 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8803 if (has_reduced_clock)
7429e9d4 8804 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8805
190f68c5 8806 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8807 &fp, &reduced_clock,
8808 has_reduced_clock ? &fp2 : NULL);
8809
190f68c5
ACO
8810 crtc_state->dpll_hw_state.dpll = dpll;
8811 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8812 if (has_reduced_clock)
190f68c5 8813 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8814 else
190f68c5 8815 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8816
190f68c5 8817 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 8818 if (pll == NULL) {
84f44ce7 8819 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8820 pipe_name(crtc->pipe));
4b645f14
JB
8821 return -EINVAL;
8822 }
3fb37703 8823 }
79e53945 8824
ab585dea 8825 if (is_lvds && has_reduced_clock)
c7653199 8826 crtc->lowfreq_avail = true;
bcd644e0 8827 else
c7653199 8828 crtc->lowfreq_avail = false;
e2b78267 8829
c8f7a0db 8830 return 0;
79e53945
JB
8831}
8832
eb14cb74
VS
8833static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8834 struct intel_link_m_n *m_n)
8835{
8836 struct drm_device *dev = crtc->base.dev;
8837 struct drm_i915_private *dev_priv = dev->dev_private;
8838 enum pipe pipe = crtc->pipe;
8839
8840 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8841 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8842 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8843 & ~TU_SIZE_MASK;
8844 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8845 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8846 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8847}
8848
8849static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8850 enum transcoder transcoder,
b95af8be
VK
8851 struct intel_link_m_n *m_n,
8852 struct intel_link_m_n *m2_n2)
72419203
DV
8853{
8854 struct drm_device *dev = crtc->base.dev;
8855 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8856 enum pipe pipe = crtc->pipe;
72419203 8857
eb14cb74
VS
8858 if (INTEL_INFO(dev)->gen >= 5) {
8859 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8860 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8861 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8862 & ~TU_SIZE_MASK;
8863 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8864 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8865 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8866 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8867 * gen < 8) and if DRRS is supported (to make sure the
8868 * registers are not unnecessarily read).
8869 */
8870 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8871 crtc->config->has_drrs) {
b95af8be
VK
8872 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8873 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8874 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8875 & ~TU_SIZE_MASK;
8876 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8877 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8878 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8879 }
eb14cb74
VS
8880 } else {
8881 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8882 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8883 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8884 & ~TU_SIZE_MASK;
8885 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8886 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8887 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8888 }
8889}
8890
8891void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8892 struct intel_crtc_state *pipe_config)
eb14cb74 8893{
681a8504 8894 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8895 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8896 else
8897 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8898 &pipe_config->dp_m_n,
8899 &pipe_config->dp_m2_n2);
eb14cb74 8900}
72419203 8901
eb14cb74 8902static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8903 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8904{
8905 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8906 &pipe_config->fdi_m_n, NULL);
72419203
DV
8907}
8908
bd2e244f 8909static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8910 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8911{
8912 struct drm_device *dev = crtc->base.dev;
8913 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
8914 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8915 uint32_t ps_ctrl = 0;
8916 int id = -1;
8917 int i;
bd2e244f 8918
a1b2278e
CK
8919 /* find scaler attached to this pipe */
8920 for (i = 0; i < crtc->num_scalers; i++) {
8921 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8922 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8923 id = i;
8924 pipe_config->pch_pfit.enabled = true;
8925 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8926 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8927 break;
8928 }
8929 }
bd2e244f 8930
a1b2278e
CK
8931 scaler_state->scaler_id = id;
8932 if (id >= 0) {
8933 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8934 } else {
8935 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8936 }
8937}
8938
5724dbd1
DL
8939static void
8940skylake_get_initial_plane_config(struct intel_crtc *crtc,
8941 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8942{
8943 struct drm_device *dev = crtc->base.dev;
8944 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 8945 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8946 int pipe = crtc->pipe;
8947 int fourcc, pixel_format;
6761dd31 8948 unsigned int aligned_height;
bc8d7dff 8949 struct drm_framebuffer *fb;
1b842c89 8950 struct intel_framebuffer *intel_fb;
bc8d7dff 8951
d9806c9f 8952 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8953 if (!intel_fb) {
bc8d7dff
DL
8954 DRM_DEBUG_KMS("failed to alloc fb\n");
8955 return;
8956 }
8957
1b842c89
DL
8958 fb = &intel_fb->base;
8959
bc8d7dff 8960 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8961 if (!(val & PLANE_CTL_ENABLE))
8962 goto error;
8963
bc8d7dff
DL
8964 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8965 fourcc = skl_format_to_fourcc(pixel_format,
8966 val & PLANE_CTL_ORDER_RGBX,
8967 val & PLANE_CTL_ALPHA_MASK);
8968 fb->pixel_format = fourcc;
8969 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8970
40f46283
DL
8971 tiling = val & PLANE_CTL_TILED_MASK;
8972 switch (tiling) {
8973 case PLANE_CTL_TILED_LINEAR:
8974 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8975 break;
8976 case PLANE_CTL_TILED_X:
8977 plane_config->tiling = I915_TILING_X;
8978 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8979 break;
8980 case PLANE_CTL_TILED_Y:
8981 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8982 break;
8983 case PLANE_CTL_TILED_YF:
8984 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8985 break;
8986 default:
8987 MISSING_CASE(tiling);
8988 goto error;
8989 }
8990
bc8d7dff
DL
8991 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8992 plane_config->base = base;
8993
8994 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8995
8996 val = I915_READ(PLANE_SIZE(pipe, 0));
8997 fb->height = ((val >> 16) & 0xfff) + 1;
8998 fb->width = ((val >> 0) & 0x1fff) + 1;
8999
9000 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
9001 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9002 fb->pixel_format);
bc8d7dff
DL
9003 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9004
9005 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9006 fb->pixel_format,
9007 fb->modifier[0]);
bc8d7dff 9008
f37b5c2b 9009 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
9010
9011 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9012 pipe_name(pipe), fb->width, fb->height,
9013 fb->bits_per_pixel, base, fb->pitches[0],
9014 plane_config->size);
9015
2d14030b 9016 plane_config->fb = intel_fb;
bc8d7dff
DL
9017 return;
9018
9019error:
9020 kfree(fb);
9021}
9022
2fa2fe9a 9023static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 9024 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
9025{
9026 struct drm_device *dev = crtc->base.dev;
9027 struct drm_i915_private *dev_priv = dev->dev_private;
9028 uint32_t tmp;
9029
9030 tmp = I915_READ(PF_CTL(crtc->pipe));
9031
9032 if (tmp & PF_ENABLE) {
fd4daa9c 9033 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
9034 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9035 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
9036
9037 /* We currently do not free assignements of panel fitters on
9038 * ivb/hsw (since we don't use the higher upscaling modes which
9039 * differentiates them) so just WARN about this case for now. */
9040 if (IS_GEN7(dev)) {
9041 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9042 PF_PIPE_SEL_IVB(crtc->pipe));
9043 }
2fa2fe9a 9044 }
79e53945
JB
9045}
9046
5724dbd1
DL
9047static void
9048ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9049 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
9050{
9051 struct drm_device *dev = crtc->base.dev;
9052 struct drm_i915_private *dev_priv = dev->dev_private;
9053 u32 val, base, offset;
aeee5a49 9054 int pipe = crtc->pipe;
4c6baa59 9055 int fourcc, pixel_format;
6761dd31 9056 unsigned int aligned_height;
b113d5ee 9057 struct drm_framebuffer *fb;
1b842c89 9058 struct intel_framebuffer *intel_fb;
4c6baa59 9059
42a7b088
DL
9060 val = I915_READ(DSPCNTR(pipe));
9061 if (!(val & DISPLAY_PLANE_ENABLE))
9062 return;
9063
d9806c9f 9064 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 9065 if (!intel_fb) {
4c6baa59
JB
9066 DRM_DEBUG_KMS("failed to alloc fb\n");
9067 return;
9068 }
9069
1b842c89
DL
9070 fb = &intel_fb->base;
9071
18c5247e
DV
9072 if (INTEL_INFO(dev)->gen >= 4) {
9073 if (val & DISPPLANE_TILED) {
49af449b 9074 plane_config->tiling = I915_TILING_X;
18c5247e
DV
9075 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9076 }
9077 }
4c6baa59
JB
9078
9079 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 9080 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
9081 fb->pixel_format = fourcc;
9082 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 9083
aeee5a49 9084 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 9085 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 9086 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 9087 } else {
49af449b 9088 if (plane_config->tiling)
aeee5a49 9089 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 9090 else
aeee5a49 9091 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
9092 }
9093 plane_config->base = base;
9094
9095 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
9096 fb->width = ((val >> 16) & 0xfff) + 1;
9097 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
9098
9099 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 9100 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 9101
b113d5ee 9102 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
9103 fb->pixel_format,
9104 fb->modifier[0]);
4c6baa59 9105
f37b5c2b 9106 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 9107
2844a921
DL
9108 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9109 pipe_name(pipe), fb->width, fb->height,
9110 fb->bits_per_pixel, base, fb->pitches[0],
9111 plane_config->size);
b113d5ee 9112
2d14030b 9113 plane_config->fb = intel_fb;
4c6baa59
JB
9114}
9115
0e8ffe1b 9116static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9117 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9118{
9119 struct drm_device *dev = crtc->base.dev;
9120 struct drm_i915_private *dev_priv = dev->dev_private;
9121 uint32_t tmp;
9122
f458ebbc
DV
9123 if (!intel_display_power_is_enabled(dev_priv,
9124 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
9125 return false;
9126
e143a21c 9127 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 9128 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 9129
0e8ffe1b
DV
9130 tmp = I915_READ(PIPECONF(crtc->pipe));
9131 if (!(tmp & PIPECONF_ENABLE))
9132 return false;
9133
42571aef
VS
9134 switch (tmp & PIPECONF_BPC_MASK) {
9135 case PIPECONF_6BPC:
9136 pipe_config->pipe_bpp = 18;
9137 break;
9138 case PIPECONF_8BPC:
9139 pipe_config->pipe_bpp = 24;
9140 break;
9141 case PIPECONF_10BPC:
9142 pipe_config->pipe_bpp = 30;
9143 break;
9144 case PIPECONF_12BPC:
9145 pipe_config->pipe_bpp = 36;
9146 break;
9147 default:
9148 break;
9149 }
9150
b5a9fa09
DV
9151 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9152 pipe_config->limited_color_range = true;
9153
ab9412ba 9154 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
9155 struct intel_shared_dpll *pll;
9156
88adfff1
DV
9157 pipe_config->has_pch_encoder = true;
9158
627eb5a3
DV
9159 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9160 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9161 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
9162
9163 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 9164
c0d43d62 9165 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
9166 pipe_config->shared_dpll =
9167 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
9168 } else {
9169 tmp = I915_READ(PCH_DPLL_SEL);
9170 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9171 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9172 else
9173 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9174 }
66e985c0
DV
9175
9176 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9177
9178 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9179 &pipe_config->dpll_hw_state));
c93f54cf
DV
9180
9181 tmp = pipe_config->dpll_hw_state.dpll;
9182 pipe_config->pixel_multiplier =
9183 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9184 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
9185
9186 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
9187 } else {
9188 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
9189 }
9190
1bd1bd80
DV
9191 intel_get_pipe_timings(crtc, pipe_config);
9192
2fa2fe9a
DV
9193 ironlake_get_pfit_config(crtc, pipe_config);
9194
0e8ffe1b
DV
9195 return true;
9196}
9197
be256dc7
PZ
9198static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9199{
9200 struct drm_device *dev = dev_priv->dev;
be256dc7 9201 struct intel_crtc *crtc;
be256dc7 9202
d3fcc808 9203 for_each_intel_crtc(dev, crtc)
e2c719b7 9204 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
9205 pipe_name(crtc->pipe));
9206
e2c719b7
RC
9207 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9208 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9209 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9210 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9211 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9212 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 9213 "CPU PWM1 enabled\n");
c5107b87 9214 if (IS_HASWELL(dev))
e2c719b7 9215 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 9216 "CPU PWM2 enabled\n");
e2c719b7 9217 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 9218 "PCH PWM1 enabled\n");
e2c719b7 9219 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 9220 "Utility pin enabled\n");
e2c719b7 9221 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 9222
9926ada1
PZ
9223 /*
9224 * In theory we can still leave IRQs enabled, as long as only the HPD
9225 * interrupts remain enabled. We used to check for that, but since it's
9226 * gen-specific and since we only disable LCPLL after we fully disable
9227 * the interrupts, the check below should be enough.
9228 */
e2c719b7 9229 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
9230}
9231
9ccd5aeb
PZ
9232static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9233{
9234 struct drm_device *dev = dev_priv->dev;
9235
9236 if (IS_HASWELL(dev))
9237 return I915_READ(D_COMP_HSW);
9238 else
9239 return I915_READ(D_COMP_BDW);
9240}
9241
3c4c9b81
PZ
9242static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9243{
9244 struct drm_device *dev = dev_priv->dev;
9245
9246 if (IS_HASWELL(dev)) {
9247 mutex_lock(&dev_priv->rps.hw_lock);
9248 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9249 val))
f475dadf 9250 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
9251 mutex_unlock(&dev_priv->rps.hw_lock);
9252 } else {
9ccd5aeb
PZ
9253 I915_WRITE(D_COMP_BDW, val);
9254 POSTING_READ(D_COMP_BDW);
3c4c9b81 9255 }
be256dc7
PZ
9256}
9257
9258/*
9259 * This function implements pieces of two sequences from BSpec:
9260 * - Sequence for display software to disable LCPLL
9261 * - Sequence for display software to allow package C8+
9262 * The steps implemented here are just the steps that actually touch the LCPLL
9263 * register. Callers should take care of disabling all the display engine
9264 * functions, doing the mode unset, fixing interrupts, etc.
9265 */
6ff58d53
PZ
9266static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9267 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9268{
9269 uint32_t val;
9270
9271 assert_can_disable_lcpll(dev_priv);
9272
9273 val = I915_READ(LCPLL_CTL);
9274
9275 if (switch_to_fclk) {
9276 val |= LCPLL_CD_SOURCE_FCLK;
9277 I915_WRITE(LCPLL_CTL, val);
9278
9279 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9280 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9281 DRM_ERROR("Switching to FCLK failed\n");
9282
9283 val = I915_READ(LCPLL_CTL);
9284 }
9285
9286 val |= LCPLL_PLL_DISABLE;
9287 I915_WRITE(LCPLL_CTL, val);
9288 POSTING_READ(LCPLL_CTL);
9289
9290 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9291 DRM_ERROR("LCPLL still locked\n");
9292
9ccd5aeb 9293 val = hsw_read_dcomp(dev_priv);
be256dc7 9294 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9295 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9296 ndelay(100);
9297
9ccd5aeb
PZ
9298 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9299 1))
be256dc7
PZ
9300 DRM_ERROR("D_COMP RCOMP still in progress\n");
9301
9302 if (allow_power_down) {
9303 val = I915_READ(LCPLL_CTL);
9304 val |= LCPLL_POWER_DOWN_ALLOW;
9305 I915_WRITE(LCPLL_CTL, val);
9306 POSTING_READ(LCPLL_CTL);
9307 }
9308}
9309
9310/*
9311 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9312 * source.
9313 */
6ff58d53 9314static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9315{
9316 uint32_t val;
9317
9318 val = I915_READ(LCPLL_CTL);
9319
9320 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9321 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9322 return;
9323
a8a8bd54
PZ
9324 /*
9325 * Make sure we're not on PC8 state before disabling PC8, otherwise
9326 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9327 */
59bad947 9328 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9329
be256dc7
PZ
9330 if (val & LCPLL_POWER_DOWN_ALLOW) {
9331 val &= ~LCPLL_POWER_DOWN_ALLOW;
9332 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9333 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9334 }
9335
9ccd5aeb 9336 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9337 val |= D_COMP_COMP_FORCE;
9338 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9339 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9340
9341 val = I915_READ(LCPLL_CTL);
9342 val &= ~LCPLL_PLL_DISABLE;
9343 I915_WRITE(LCPLL_CTL, val);
9344
9345 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9346 DRM_ERROR("LCPLL not locked yet\n");
9347
9348 if (val & LCPLL_CD_SOURCE_FCLK) {
9349 val = I915_READ(LCPLL_CTL);
9350 val &= ~LCPLL_CD_SOURCE_FCLK;
9351 I915_WRITE(LCPLL_CTL, val);
9352
9353 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9354 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9355 DRM_ERROR("Switching back to LCPLL failed\n");
9356 }
215733fa 9357
59bad947 9358 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
b6283055 9359 intel_update_cdclk(dev_priv->dev);
be256dc7
PZ
9360}
9361
765dab67
PZ
9362/*
9363 * Package states C8 and deeper are really deep PC states that can only be
9364 * reached when all the devices on the system allow it, so even if the graphics
9365 * device allows PC8+, it doesn't mean the system will actually get to these
9366 * states. Our driver only allows PC8+ when going into runtime PM.
9367 *
9368 * The requirements for PC8+ are that all the outputs are disabled, the power
9369 * well is disabled and most interrupts are disabled, and these are also
9370 * requirements for runtime PM. When these conditions are met, we manually do
9371 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9372 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9373 * hang the machine.
9374 *
9375 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9376 * the state of some registers, so when we come back from PC8+ we need to
9377 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9378 * need to take care of the registers kept by RC6. Notice that this happens even
9379 * if we don't put the device in PCI D3 state (which is what currently happens
9380 * because of the runtime PM support).
9381 *
9382 * For more, read "Display Sequences for Package C8" on the hardware
9383 * documentation.
9384 */
a14cb6fc 9385void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9386{
c67a470b
PZ
9387 struct drm_device *dev = dev_priv->dev;
9388 uint32_t val;
9389
c67a470b
PZ
9390 DRM_DEBUG_KMS("Enabling package C8+\n");
9391
c67a470b
PZ
9392 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9393 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9394 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9395 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9396 }
9397
9398 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9399 hsw_disable_lcpll(dev_priv, true, true);
9400}
9401
a14cb6fc 9402void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9403{
9404 struct drm_device *dev = dev_priv->dev;
9405 uint32_t val;
9406
c67a470b
PZ
9407 DRM_DEBUG_KMS("Disabling package C8+\n");
9408
9409 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9410 lpt_init_pch_refclk(dev);
9411
9412 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9413 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9414 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9415 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9416 }
9417
9418 intel_prepare_ddi(dev);
c67a470b
PZ
9419}
9420
27c329ed 9421static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
f8437dd1 9422{
a821fc46 9423 struct drm_device *dev = old_state->dev;
27c329ed 9424 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
f8437dd1 9425
27c329ed 9426 broxton_set_cdclk(dev, req_cdclk);
f8437dd1
VK
9427}
9428
b432e5cf 9429/* compute the max rate for new configuration */
27c329ed 9430static int ilk_max_pixel_rate(struct drm_atomic_state *state)
b432e5cf 9431{
b432e5cf 9432 struct intel_crtc *intel_crtc;
27c329ed 9433 struct intel_crtc_state *crtc_state;
b432e5cf 9434 int max_pixel_rate = 0;
b432e5cf 9435
27c329ed
ML
9436 for_each_intel_crtc(state->dev, intel_crtc) {
9437 int pixel_rate;
9438
9439 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9440 if (IS_ERR(crtc_state))
9441 return PTR_ERR(crtc_state);
9442
9443 if (!crtc_state->base.enable)
b432e5cf
VS
9444 continue;
9445
27c329ed 9446 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
b432e5cf
VS
9447
9448 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
27c329ed 9449 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
b432e5cf
VS
9450 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9451
9452 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9453 }
9454
9455 return max_pixel_rate;
9456}
9457
9458static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9459{
9460 struct drm_i915_private *dev_priv = dev->dev_private;
9461 uint32_t val, data;
9462 int ret;
9463
9464 if (WARN((I915_READ(LCPLL_CTL) &
9465 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9466 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9467 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9468 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9469 "trying to change cdclk frequency with cdclk not enabled\n"))
9470 return;
9471
9472 mutex_lock(&dev_priv->rps.hw_lock);
9473 ret = sandybridge_pcode_write(dev_priv,
9474 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9475 mutex_unlock(&dev_priv->rps.hw_lock);
9476 if (ret) {
9477 DRM_ERROR("failed to inform pcode about cdclk change\n");
9478 return;
9479 }
9480
9481 val = I915_READ(LCPLL_CTL);
9482 val |= LCPLL_CD_SOURCE_FCLK;
9483 I915_WRITE(LCPLL_CTL, val);
9484
9485 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9486 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9487 DRM_ERROR("Switching to FCLK failed\n");
9488
9489 val = I915_READ(LCPLL_CTL);
9490 val &= ~LCPLL_CLK_FREQ_MASK;
9491
9492 switch (cdclk) {
9493 case 450000:
9494 val |= LCPLL_CLK_FREQ_450;
9495 data = 0;
9496 break;
9497 case 540000:
9498 val |= LCPLL_CLK_FREQ_54O_BDW;
9499 data = 1;
9500 break;
9501 case 337500:
9502 val |= LCPLL_CLK_FREQ_337_5_BDW;
9503 data = 2;
9504 break;
9505 case 675000:
9506 val |= LCPLL_CLK_FREQ_675_BDW;
9507 data = 3;
9508 break;
9509 default:
9510 WARN(1, "invalid cdclk frequency\n");
9511 return;
9512 }
9513
9514 I915_WRITE(LCPLL_CTL, val);
9515
9516 val = I915_READ(LCPLL_CTL);
9517 val &= ~LCPLL_CD_SOURCE_FCLK;
9518 I915_WRITE(LCPLL_CTL, val);
9519
9520 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9521 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9522 DRM_ERROR("Switching back to LCPLL failed\n");
9523
9524 mutex_lock(&dev_priv->rps.hw_lock);
9525 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9526 mutex_unlock(&dev_priv->rps.hw_lock);
9527
9528 intel_update_cdclk(dev);
9529
9530 WARN(cdclk != dev_priv->cdclk_freq,
9531 "cdclk requested %d kHz but got %d kHz\n",
9532 cdclk, dev_priv->cdclk_freq);
9533}
9534
27c329ed 9535static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
b432e5cf 9536{
27c329ed
ML
9537 struct drm_i915_private *dev_priv = to_i915(state->dev);
9538 int max_pixclk = ilk_max_pixel_rate(state);
b432e5cf
VS
9539 int cdclk;
9540
9541 /*
9542 * FIXME should also account for plane ratio
9543 * once 64bpp pixel formats are supported.
9544 */
27c329ed 9545 if (max_pixclk > 540000)
b432e5cf 9546 cdclk = 675000;
27c329ed 9547 else if (max_pixclk > 450000)
b432e5cf 9548 cdclk = 540000;
27c329ed 9549 else if (max_pixclk > 337500)
b432e5cf
VS
9550 cdclk = 450000;
9551 else
9552 cdclk = 337500;
9553
9554 /*
9555 * FIXME move the cdclk caclulation to
9556 * compute_config() so we can fail gracegully.
9557 */
9558 if (cdclk > dev_priv->max_cdclk_freq) {
9559 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9560 cdclk, dev_priv->max_cdclk_freq);
9561 cdclk = dev_priv->max_cdclk_freq;
9562 }
9563
27c329ed 9564 to_intel_atomic_state(state)->cdclk = cdclk;
b432e5cf
VS
9565
9566 return 0;
9567}
9568
27c329ed 9569static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
b432e5cf 9570{
27c329ed
ML
9571 struct drm_device *dev = old_state->dev;
9572 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
b432e5cf 9573
27c329ed 9574 broadwell_set_cdclk(dev, req_cdclk);
b432e5cf
VS
9575}
9576
190f68c5
ACO
9577static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9578 struct intel_crtc_state *crtc_state)
09b4ddf9 9579{
190f68c5 9580 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 9581 return -EINVAL;
716c2e55 9582
c7653199 9583 crtc->lowfreq_avail = false;
644cef34 9584
c8f7a0db 9585 return 0;
79e53945
JB
9586}
9587
3760b59c
S
9588static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9589 enum port port,
9590 struct intel_crtc_state *pipe_config)
9591{
9592 switch (port) {
9593 case PORT_A:
9594 pipe_config->ddi_pll_sel = SKL_DPLL0;
9595 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9596 break;
9597 case PORT_B:
9598 pipe_config->ddi_pll_sel = SKL_DPLL1;
9599 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9600 break;
9601 case PORT_C:
9602 pipe_config->ddi_pll_sel = SKL_DPLL2;
9603 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9604 break;
9605 default:
9606 DRM_ERROR("Incorrect port type\n");
9607 }
9608}
9609
96b7dfb7
S
9610static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9611 enum port port,
5cec258b 9612 struct intel_crtc_state *pipe_config)
96b7dfb7 9613{
3148ade7 9614 u32 temp, dpll_ctl1;
96b7dfb7
S
9615
9616 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9617 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9618
9619 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
9620 case SKL_DPLL0:
9621 /*
9622 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9623 * of the shared DPLL framework and thus needs to be read out
9624 * separately
9625 */
9626 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9627 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9628 break;
96b7dfb7
S
9629 case SKL_DPLL1:
9630 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9631 break;
9632 case SKL_DPLL2:
9633 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9634 break;
9635 case SKL_DPLL3:
9636 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9637 break;
96b7dfb7
S
9638 }
9639}
9640
7d2c8175
DL
9641static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9642 enum port port,
5cec258b 9643 struct intel_crtc_state *pipe_config)
7d2c8175
DL
9644{
9645 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9646
9647 switch (pipe_config->ddi_pll_sel) {
9648 case PORT_CLK_SEL_WRPLL1:
9649 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9650 break;
9651 case PORT_CLK_SEL_WRPLL2:
9652 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9653 break;
9654 }
9655}
9656
26804afd 9657static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9658 struct intel_crtc_state *pipe_config)
26804afd
DV
9659{
9660 struct drm_device *dev = crtc->base.dev;
9661 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9662 struct intel_shared_dpll *pll;
26804afd
DV
9663 enum port port;
9664 uint32_t tmp;
9665
9666 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9667
9668 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9669
96b7dfb7
S
9670 if (IS_SKYLAKE(dev))
9671 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9672 else if (IS_BROXTON(dev))
9673 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9674 else
9675 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9676
d452c5b6
DV
9677 if (pipe_config->shared_dpll >= 0) {
9678 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9679
9680 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9681 &pipe_config->dpll_hw_state));
9682 }
9683
26804afd
DV
9684 /*
9685 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9686 * DDI E. So just check whether this pipe is wired to DDI E and whether
9687 * the PCH transcoder is on.
9688 */
ca370455
DL
9689 if (INTEL_INFO(dev)->gen < 9 &&
9690 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9691 pipe_config->has_pch_encoder = true;
9692
9693 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9694 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9695 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9696
9697 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9698 }
9699}
9700
0e8ffe1b 9701static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9702 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9703{
9704 struct drm_device *dev = crtc->base.dev;
9705 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 9706 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
9707 uint32_t tmp;
9708
f458ebbc 9709 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
9710 POWER_DOMAIN_PIPE(crtc->pipe)))
9711 return false;
9712
e143a21c 9713 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
9714 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9715
eccb140b
DV
9716 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9717 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9718 enum pipe trans_edp_pipe;
9719 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9720 default:
9721 WARN(1, "unknown pipe linked to edp transcoder\n");
9722 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9723 case TRANS_DDI_EDP_INPUT_A_ON:
9724 trans_edp_pipe = PIPE_A;
9725 break;
9726 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9727 trans_edp_pipe = PIPE_B;
9728 break;
9729 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9730 trans_edp_pipe = PIPE_C;
9731 break;
9732 }
9733
9734 if (trans_edp_pipe == crtc->pipe)
9735 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9736 }
9737
f458ebbc 9738 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 9739 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
9740 return false;
9741
eccb140b 9742 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
9743 if (!(tmp & PIPECONF_ENABLE))
9744 return false;
9745
26804afd 9746 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9747
1bd1bd80
DV
9748 intel_get_pipe_timings(crtc, pipe_config);
9749
a1b2278e
CK
9750 if (INTEL_INFO(dev)->gen >= 9) {
9751 skl_init_scalers(dev, crtc, pipe_config);
9752 }
9753
2fa2fe9a 9754 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
af99ceda
CK
9755
9756 if (INTEL_INFO(dev)->gen >= 9) {
9757 pipe_config->scaler_state.scaler_id = -1;
9758 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9759 }
9760
bd2e244f 9761 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
ff6d9f55 9762 if (INTEL_INFO(dev)->gen == 9)
bd2e244f 9763 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9764 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 9765 ironlake_get_pfit_config(crtc, pipe_config);
ff6d9f55
JB
9766 else
9767 MISSING_CASE(INTEL_INFO(dev)->gen);
bd2e244f 9768 }
88adfff1 9769
e59150dc
JB
9770 if (IS_HASWELL(dev))
9771 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9772 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9773
ebb69c95
CT
9774 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9775 pipe_config->pixel_multiplier =
9776 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9777 } else {
9778 pipe_config->pixel_multiplier = 1;
9779 }
6c49f241 9780
0e8ffe1b
DV
9781 return true;
9782}
9783
560b85bb
CW
9784static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9785{
9786 struct drm_device *dev = crtc->dev;
9787 struct drm_i915_private *dev_priv = dev->dev_private;
9788 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9789 uint32_t cntl = 0, size = 0;
560b85bb 9790
dc41c154 9791 if (base) {
3dd512fb
MR
9792 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9793 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
9794 unsigned int stride = roundup_pow_of_two(width) * 4;
9795
9796 switch (stride) {
9797 default:
9798 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9799 width, stride);
9800 stride = 256;
9801 /* fallthrough */
9802 case 256:
9803 case 512:
9804 case 1024:
9805 case 2048:
9806 break;
4b0e333e
CW
9807 }
9808
dc41c154
VS
9809 cntl |= CURSOR_ENABLE |
9810 CURSOR_GAMMA_ENABLE |
9811 CURSOR_FORMAT_ARGB |
9812 CURSOR_STRIDE(stride);
9813
9814 size = (height << 12) | width;
4b0e333e 9815 }
560b85bb 9816
dc41c154
VS
9817 if (intel_crtc->cursor_cntl != 0 &&
9818 (intel_crtc->cursor_base != base ||
9819 intel_crtc->cursor_size != size ||
9820 intel_crtc->cursor_cntl != cntl)) {
9821 /* On these chipsets we can only modify the base/size/stride
9822 * whilst the cursor is disabled.
9823 */
9824 I915_WRITE(_CURACNTR, 0);
4b0e333e 9825 POSTING_READ(_CURACNTR);
dc41c154 9826 intel_crtc->cursor_cntl = 0;
4b0e333e 9827 }
560b85bb 9828
99d1f387 9829 if (intel_crtc->cursor_base != base) {
9db4a9c7 9830 I915_WRITE(_CURABASE, base);
99d1f387
VS
9831 intel_crtc->cursor_base = base;
9832 }
4726e0b0 9833
dc41c154
VS
9834 if (intel_crtc->cursor_size != size) {
9835 I915_WRITE(CURSIZE, size);
9836 intel_crtc->cursor_size = size;
4b0e333e 9837 }
560b85bb 9838
4b0e333e 9839 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
9840 I915_WRITE(_CURACNTR, cntl);
9841 POSTING_READ(_CURACNTR);
4b0e333e 9842 intel_crtc->cursor_cntl = cntl;
560b85bb 9843 }
560b85bb
CW
9844}
9845
560b85bb 9846static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
9847{
9848 struct drm_device *dev = crtc->dev;
9849 struct drm_i915_private *dev_priv = dev->dev_private;
9850 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9851 int pipe = intel_crtc->pipe;
4b0e333e
CW
9852 uint32_t cntl;
9853
9854 cntl = 0;
9855 if (base) {
9856 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 9857 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
9858 case 64:
9859 cntl |= CURSOR_MODE_64_ARGB_AX;
9860 break;
9861 case 128:
9862 cntl |= CURSOR_MODE_128_ARGB_AX;
9863 break;
9864 case 256:
9865 cntl |= CURSOR_MODE_256_ARGB_AX;
9866 break;
9867 default:
3dd512fb 9868 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 9869 return;
65a21cd6 9870 }
4b0e333e 9871 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
9872
9873 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9874 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 9875 }
65a21cd6 9876
8e7d688b 9877 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
9878 cntl |= CURSOR_ROTATE_180;
9879
4b0e333e
CW
9880 if (intel_crtc->cursor_cntl != cntl) {
9881 I915_WRITE(CURCNTR(pipe), cntl);
9882 POSTING_READ(CURCNTR(pipe));
9883 intel_crtc->cursor_cntl = cntl;
65a21cd6 9884 }
4b0e333e 9885
65a21cd6 9886 /* and commit changes on next vblank */
5efb3e28
VS
9887 I915_WRITE(CURBASE(pipe), base);
9888 POSTING_READ(CURBASE(pipe));
99d1f387
VS
9889
9890 intel_crtc->cursor_base = base;
65a21cd6
JB
9891}
9892
cda4b7d3 9893/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
9894static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9895 bool on)
cda4b7d3
CW
9896{
9897 struct drm_device *dev = crtc->dev;
9898 struct drm_i915_private *dev_priv = dev->dev_private;
9899 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9900 int pipe = intel_crtc->pipe;
3d7d6510
MR
9901 int x = crtc->cursor_x;
9902 int y = crtc->cursor_y;
d6e4db15 9903 u32 base = 0, pos = 0;
cda4b7d3 9904
d6e4db15 9905 if (on)
cda4b7d3 9906 base = intel_crtc->cursor_addr;
cda4b7d3 9907
6e3c9717 9908 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
9909 base = 0;
9910
6e3c9717 9911 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
9912 base = 0;
9913
9914 if (x < 0) {
3dd512fb 9915 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
cda4b7d3
CW
9916 base = 0;
9917
9918 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9919 x = -x;
9920 }
9921 pos |= x << CURSOR_X_SHIFT;
9922
9923 if (y < 0) {
3dd512fb 9924 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
cda4b7d3
CW
9925 base = 0;
9926
9927 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9928 y = -y;
9929 }
9930 pos |= y << CURSOR_Y_SHIFT;
9931
4b0e333e 9932 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
9933 return;
9934
5efb3e28
VS
9935 I915_WRITE(CURPOS(pipe), pos);
9936
4398ad45
VS
9937 /* ILK+ do this automagically */
9938 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 9939 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
3dd512fb
MR
9940 base += (intel_crtc->base.cursor->state->crtc_h *
9941 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
4398ad45
VS
9942 }
9943
8ac54669 9944 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
9945 i845_update_cursor(crtc, base);
9946 else
9947 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
9948}
9949
dc41c154
VS
9950static bool cursor_size_ok(struct drm_device *dev,
9951 uint32_t width, uint32_t height)
9952{
9953 if (width == 0 || height == 0)
9954 return false;
9955
9956 /*
9957 * 845g/865g are special in that they are only limited by
9958 * the width of their cursors, the height is arbitrary up to
9959 * the precision of the register. Everything else requires
9960 * square cursors, limited to a few power-of-two sizes.
9961 */
9962 if (IS_845G(dev) || IS_I865G(dev)) {
9963 if ((width & 63) != 0)
9964 return false;
9965
9966 if (width > (IS_845G(dev) ? 64 : 512))
9967 return false;
9968
9969 if (height > 1023)
9970 return false;
9971 } else {
9972 switch (width | height) {
9973 case 256:
9974 case 128:
9975 if (IS_GEN2(dev))
9976 return false;
9977 case 64:
9978 break;
9979 default:
9980 return false;
9981 }
9982 }
9983
9984 return true;
9985}
9986
79e53945 9987static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 9988 u16 *blue, uint32_t start, uint32_t size)
79e53945 9989{
7203425a 9990 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 9991 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 9992
7203425a 9993 for (i = start; i < end; i++) {
79e53945
JB
9994 intel_crtc->lut_r[i] = red[i] >> 8;
9995 intel_crtc->lut_g[i] = green[i] >> 8;
9996 intel_crtc->lut_b[i] = blue[i] >> 8;
9997 }
9998
9999 intel_crtc_load_lut(crtc);
10000}
10001
79e53945
JB
10002/* VESA 640x480x72Hz mode to set on the pipe */
10003static struct drm_display_mode load_detect_mode = {
10004 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10005 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10006};
10007
a8bb6818
DV
10008struct drm_framebuffer *
10009__intel_framebuffer_create(struct drm_device *dev,
10010 struct drm_mode_fb_cmd2 *mode_cmd,
10011 struct drm_i915_gem_object *obj)
d2dff872
CW
10012{
10013 struct intel_framebuffer *intel_fb;
10014 int ret;
10015
10016 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10017 if (!intel_fb) {
6ccb81f2 10018 drm_gem_object_unreference(&obj->base);
d2dff872
CW
10019 return ERR_PTR(-ENOMEM);
10020 }
10021
10022 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
10023 if (ret)
10024 goto err;
d2dff872
CW
10025
10026 return &intel_fb->base;
dd4916c5 10027err:
6ccb81f2 10028 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
10029 kfree(intel_fb);
10030
10031 return ERR_PTR(ret);
d2dff872
CW
10032}
10033
b5ea642a 10034static struct drm_framebuffer *
a8bb6818
DV
10035intel_framebuffer_create(struct drm_device *dev,
10036 struct drm_mode_fb_cmd2 *mode_cmd,
10037 struct drm_i915_gem_object *obj)
10038{
10039 struct drm_framebuffer *fb;
10040 int ret;
10041
10042 ret = i915_mutex_lock_interruptible(dev);
10043 if (ret)
10044 return ERR_PTR(ret);
10045 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10046 mutex_unlock(&dev->struct_mutex);
10047
10048 return fb;
10049}
10050
d2dff872
CW
10051static u32
10052intel_framebuffer_pitch_for_width(int width, int bpp)
10053{
10054 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10055 return ALIGN(pitch, 64);
10056}
10057
10058static u32
10059intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10060{
10061 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 10062 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
10063}
10064
10065static struct drm_framebuffer *
10066intel_framebuffer_create_for_mode(struct drm_device *dev,
10067 struct drm_display_mode *mode,
10068 int depth, int bpp)
10069{
10070 struct drm_i915_gem_object *obj;
0fed39bd 10071 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
10072
10073 obj = i915_gem_alloc_object(dev,
10074 intel_framebuffer_size_for_mode(mode, bpp));
10075 if (obj == NULL)
10076 return ERR_PTR(-ENOMEM);
10077
10078 mode_cmd.width = mode->hdisplay;
10079 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
10080 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10081 bpp);
5ca0c34a 10082 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
10083
10084 return intel_framebuffer_create(dev, &mode_cmd, obj);
10085}
10086
10087static struct drm_framebuffer *
10088mode_fits_in_fbdev(struct drm_device *dev,
10089 struct drm_display_mode *mode)
10090{
4520f53a 10091#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
10092 struct drm_i915_private *dev_priv = dev->dev_private;
10093 struct drm_i915_gem_object *obj;
10094 struct drm_framebuffer *fb;
10095
4c0e5528 10096 if (!dev_priv->fbdev)
d2dff872
CW
10097 return NULL;
10098
4c0e5528 10099 if (!dev_priv->fbdev->fb)
d2dff872
CW
10100 return NULL;
10101
4c0e5528
DV
10102 obj = dev_priv->fbdev->fb->obj;
10103 BUG_ON(!obj);
10104
8bcd4553 10105 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
10106 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10107 fb->bits_per_pixel))
d2dff872
CW
10108 return NULL;
10109
01f2c773 10110 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
10111 return NULL;
10112
10113 return fb;
4520f53a
DV
10114#else
10115 return NULL;
10116#endif
d2dff872
CW
10117}
10118
d3a40d1b
ACO
10119static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10120 struct drm_crtc *crtc,
10121 struct drm_display_mode *mode,
10122 struct drm_framebuffer *fb,
10123 int x, int y)
10124{
10125 struct drm_plane_state *plane_state;
10126 int hdisplay, vdisplay;
10127 int ret;
10128
10129 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10130 if (IS_ERR(plane_state))
10131 return PTR_ERR(plane_state);
10132
10133 if (mode)
10134 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10135 else
10136 hdisplay = vdisplay = 0;
10137
10138 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10139 if (ret)
10140 return ret;
10141 drm_atomic_set_fb_for_plane(plane_state, fb);
10142 plane_state->crtc_x = 0;
10143 plane_state->crtc_y = 0;
10144 plane_state->crtc_w = hdisplay;
10145 plane_state->crtc_h = vdisplay;
10146 plane_state->src_x = x << 16;
10147 plane_state->src_y = y << 16;
10148 plane_state->src_w = hdisplay << 16;
10149 plane_state->src_h = vdisplay << 16;
10150
10151 return 0;
10152}
10153
d2434ab7 10154bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 10155 struct drm_display_mode *mode,
51fd371b
RC
10156 struct intel_load_detect_pipe *old,
10157 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
10158{
10159 struct intel_crtc *intel_crtc;
d2434ab7
DV
10160 struct intel_encoder *intel_encoder =
10161 intel_attached_encoder(connector);
79e53945 10162 struct drm_crtc *possible_crtc;
4ef69c7a 10163 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
10164 struct drm_crtc *crtc = NULL;
10165 struct drm_device *dev = encoder->dev;
94352cf9 10166 struct drm_framebuffer *fb;
51fd371b 10167 struct drm_mode_config *config = &dev->mode_config;
83a57153 10168 struct drm_atomic_state *state = NULL;
944b0c76 10169 struct drm_connector_state *connector_state;
4be07317 10170 struct intel_crtc_state *crtc_state;
51fd371b 10171 int ret, i = -1;
79e53945 10172
d2dff872 10173 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10174 connector->base.id, connector->name,
8e329a03 10175 encoder->base.id, encoder->name);
d2dff872 10176
51fd371b
RC
10177retry:
10178 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10179 if (ret)
10180 goto fail_unlock;
6e9f798d 10181
79e53945
JB
10182 /*
10183 * Algorithm gets a little messy:
7a5e4805 10184 *
79e53945
JB
10185 * - if the connector already has an assigned crtc, use it (but make
10186 * sure it's on first)
7a5e4805 10187 *
79e53945
JB
10188 * - try to find the first unused crtc that can drive this connector,
10189 * and use that if we find one
79e53945
JB
10190 */
10191
10192 /* See if we already have a CRTC for this connector */
10193 if (encoder->crtc) {
10194 crtc = encoder->crtc;
8261b191 10195
51fd371b 10196 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de
DV
10197 if (ret)
10198 goto fail_unlock;
10199 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b
RC
10200 if (ret)
10201 goto fail_unlock;
7b24056b 10202
24218aac 10203 old->dpms_mode = connector->dpms;
8261b191
CW
10204 old->load_detect_temp = false;
10205
10206 /* Make sure the crtc and connector are running */
24218aac
DV
10207 if (connector->dpms != DRM_MODE_DPMS_ON)
10208 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 10209
7173188d 10210 return true;
79e53945
JB
10211 }
10212
10213 /* Find an unused one (if possible) */
70e1e0ec 10214 for_each_crtc(dev, possible_crtc) {
79e53945
JB
10215 i++;
10216 if (!(encoder->possible_crtcs & (1 << i)))
10217 continue;
83d65738 10218 if (possible_crtc->state->enable)
a459249c
VS
10219 continue;
10220 /* This can occur when applying the pipe A quirk on resume. */
10221 if (to_intel_crtc(possible_crtc)->new_enabled)
10222 continue;
10223
10224 crtc = possible_crtc;
10225 break;
79e53945
JB
10226 }
10227
10228 /*
10229 * If we didn't find an unused CRTC, don't use any.
10230 */
10231 if (!crtc) {
7173188d 10232 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 10233 goto fail_unlock;
79e53945
JB
10234 }
10235
51fd371b
RC
10236 ret = drm_modeset_lock(&crtc->mutex, ctx);
10237 if (ret)
4d02e2de
DV
10238 goto fail_unlock;
10239 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10240 if (ret)
51fd371b 10241 goto fail_unlock;
fc303101
DV
10242 intel_encoder->new_crtc = to_intel_crtc(crtc);
10243 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
10244
10245 intel_crtc = to_intel_crtc(crtc);
412b61d8 10246 intel_crtc->new_enabled = true;
24218aac 10247 old->dpms_mode = connector->dpms;
8261b191 10248 old->load_detect_temp = true;
d2dff872 10249 old->release_fb = NULL;
79e53945 10250
83a57153
ACO
10251 state = drm_atomic_state_alloc(dev);
10252 if (!state)
10253 return false;
10254
10255 state->acquire_ctx = ctx;
10256
944b0c76
ACO
10257 connector_state = drm_atomic_get_connector_state(state, connector);
10258 if (IS_ERR(connector_state)) {
10259 ret = PTR_ERR(connector_state);
10260 goto fail;
10261 }
10262
10263 connector_state->crtc = crtc;
10264 connector_state->best_encoder = &intel_encoder->base;
10265
4be07317
ACO
10266 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10267 if (IS_ERR(crtc_state)) {
10268 ret = PTR_ERR(crtc_state);
10269 goto fail;
10270 }
10271
49d6fa21 10272 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 10273
6492711d
CW
10274 if (!mode)
10275 mode = &load_detect_mode;
79e53945 10276
d2dff872
CW
10277 /* We need a framebuffer large enough to accommodate all accesses
10278 * that the plane may generate whilst we perform load detection.
10279 * We can not rely on the fbcon either being present (we get called
10280 * during its initialisation to detect all boot displays, or it may
10281 * not even exist) or that it is large enough to satisfy the
10282 * requested mode.
10283 */
94352cf9
DV
10284 fb = mode_fits_in_fbdev(dev, mode);
10285 if (fb == NULL) {
d2dff872 10286 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
10287 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10288 old->release_fb = fb;
d2dff872
CW
10289 } else
10290 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 10291 if (IS_ERR(fb)) {
d2dff872 10292 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 10293 goto fail;
79e53945 10294 }
79e53945 10295
d3a40d1b
ACO
10296 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10297 if (ret)
10298 goto fail;
10299
8c7b5ccb
ACO
10300 drm_mode_copy(&crtc_state->base.mode, mode);
10301
568c634a 10302 if (intel_set_mode(state)) {
6492711d 10303 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
10304 if (old->release_fb)
10305 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 10306 goto fail;
79e53945 10307 }
9128b040 10308 crtc->primary->crtc = crtc;
7173188d 10309
79e53945 10310 /* let the connector get through one full cycle before testing */
9d0498a2 10311 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 10312 return true;
412b61d8
VS
10313
10314 fail:
83d65738 10315 intel_crtc->new_enabled = crtc->state->enable;
51fd371b 10316fail_unlock:
e5d958ef
ACO
10317 drm_atomic_state_free(state);
10318 state = NULL;
83a57153 10319
51fd371b
RC
10320 if (ret == -EDEADLK) {
10321 drm_modeset_backoff(ctx);
10322 goto retry;
10323 }
10324
412b61d8 10325 return false;
79e53945
JB
10326}
10327
d2434ab7 10328void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
10329 struct intel_load_detect_pipe *old,
10330 struct drm_modeset_acquire_ctx *ctx)
79e53945 10331{
83a57153 10332 struct drm_device *dev = connector->dev;
d2434ab7
DV
10333 struct intel_encoder *intel_encoder =
10334 intel_attached_encoder(connector);
4ef69c7a 10335 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 10336 struct drm_crtc *crtc = encoder->crtc;
412b61d8 10337 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83a57153 10338 struct drm_atomic_state *state;
944b0c76 10339 struct drm_connector_state *connector_state;
4be07317 10340 struct intel_crtc_state *crtc_state;
d3a40d1b 10341 int ret;
79e53945 10342
d2dff872 10343 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 10344 connector->base.id, connector->name,
8e329a03 10345 encoder->base.id, encoder->name);
d2dff872 10346
8261b191 10347 if (old->load_detect_temp) {
83a57153 10348 state = drm_atomic_state_alloc(dev);
944b0c76
ACO
10349 if (!state)
10350 goto fail;
83a57153
ACO
10351
10352 state->acquire_ctx = ctx;
10353
944b0c76
ACO
10354 connector_state = drm_atomic_get_connector_state(state, connector);
10355 if (IS_ERR(connector_state))
10356 goto fail;
10357
4be07317
ACO
10358 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10359 if (IS_ERR(crtc_state))
10360 goto fail;
10361
fc303101
DV
10362 to_intel_connector(connector)->new_encoder = NULL;
10363 intel_encoder->new_crtc = NULL;
412b61d8 10364 intel_crtc->new_enabled = false;
944b0c76
ACO
10365
10366 connector_state->best_encoder = NULL;
10367 connector_state->crtc = NULL;
10368
49d6fa21 10369 crtc_state->base.enable = crtc_state->base.active = false;
4be07317 10370
d3a40d1b
ACO
10371 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10372 0, 0);
10373 if (ret)
10374 goto fail;
10375
568c634a 10376 ret = intel_set_mode(state);
2bfb4627
ACO
10377 if (ret)
10378 goto fail;
d2dff872 10379
36206361
DV
10380 if (old->release_fb) {
10381 drm_framebuffer_unregister_private(old->release_fb);
10382 drm_framebuffer_unreference(old->release_fb);
10383 }
d2dff872 10384
0622a53c 10385 return;
79e53945
JB
10386 }
10387
c751ce4f 10388 /* Switch crtc and encoder back off if necessary */
24218aac
DV
10389 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10390 connector->funcs->dpms(connector, old->dpms_mode);
944b0c76
ACO
10391
10392 return;
10393fail:
10394 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10395 drm_atomic_state_free(state);
79e53945
JB
10396}
10397
da4a1efa 10398static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 10399 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
10400{
10401 struct drm_i915_private *dev_priv = dev->dev_private;
10402 u32 dpll = pipe_config->dpll_hw_state.dpll;
10403
10404 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 10405 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
10406 else if (HAS_PCH_SPLIT(dev))
10407 return 120000;
10408 else if (!IS_GEN2(dev))
10409 return 96000;
10410 else
10411 return 48000;
10412}
10413
79e53945 10414/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10415static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10416 struct intel_crtc_state *pipe_config)
79e53945 10417{
f1f644dc 10418 struct drm_device *dev = crtc->base.dev;
79e53945 10419 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10420 int pipe = pipe_config->cpu_transcoder;
293623f7 10421 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10422 u32 fp;
10423 intel_clock_t clock;
da4a1efa 10424 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10425
10426 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10427 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10428 else
293623f7 10429 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10430
10431 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10432 if (IS_PINEVIEW(dev)) {
10433 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10434 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10435 } else {
10436 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10437 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10438 }
10439
a6c45cf0 10440 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10441 if (IS_PINEVIEW(dev))
10442 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10443 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10444 else
10445 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10446 DPLL_FPA01_P1_POST_DIV_SHIFT);
10447
10448 switch (dpll & DPLL_MODE_MASK) {
10449 case DPLLB_MODE_DAC_SERIAL:
10450 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10451 5 : 10;
10452 break;
10453 case DPLLB_MODE_LVDS:
10454 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10455 7 : 14;
10456 break;
10457 default:
28c97730 10458 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10459 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10460 return;
79e53945
JB
10461 }
10462
ac58c3f0 10463 if (IS_PINEVIEW(dev))
da4a1efa 10464 pineview_clock(refclk, &clock);
ac58c3f0 10465 else
da4a1efa 10466 i9xx_clock(refclk, &clock);
79e53945 10467 } else {
0fb58223 10468 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10469 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10470
10471 if (is_lvds) {
10472 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10473 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10474
10475 if (lvds & LVDS_CLKB_POWER_UP)
10476 clock.p2 = 7;
10477 else
10478 clock.p2 = 14;
79e53945
JB
10479 } else {
10480 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10481 clock.p1 = 2;
10482 else {
10483 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10484 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10485 }
10486 if (dpll & PLL_P2_DIVIDE_BY_4)
10487 clock.p2 = 4;
10488 else
10489 clock.p2 = 2;
79e53945 10490 }
da4a1efa
VS
10491
10492 i9xx_clock(refclk, &clock);
79e53945
JB
10493 }
10494
18442d08
VS
10495 /*
10496 * This value includes pixel_multiplier. We will use
241bfc38 10497 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10498 * encoder's get_config() function.
10499 */
10500 pipe_config->port_clock = clock.dot;
f1f644dc
JB
10501}
10502
6878da05
VS
10503int intel_dotclock_calculate(int link_freq,
10504 const struct intel_link_m_n *m_n)
f1f644dc 10505{
f1f644dc
JB
10506 /*
10507 * The calculation for the data clock is:
1041a02f 10508 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10509 * But we want to avoid losing precison if possible, so:
1041a02f 10510 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10511 *
10512 * and the link clock is simpler:
1041a02f 10513 * link_clock = (m * link_clock) / n
f1f644dc
JB
10514 */
10515
6878da05
VS
10516 if (!m_n->link_n)
10517 return 0;
f1f644dc 10518
6878da05
VS
10519 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10520}
f1f644dc 10521
18442d08 10522static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10523 struct intel_crtc_state *pipe_config)
6878da05
VS
10524{
10525 struct drm_device *dev = crtc->base.dev;
79e53945 10526
18442d08
VS
10527 /* read out port_clock from the DPLL */
10528 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10529
f1f644dc 10530 /*
18442d08 10531 * This value does not include pixel_multiplier.
241bfc38 10532 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
10533 * agree once we know their relationship in the encoder's
10534 * get_config() function.
79e53945 10535 */
2d112de7 10536 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
10537 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10538 &pipe_config->fdi_m_n);
79e53945
JB
10539}
10540
10541/** Returns the currently programmed mode of the given pipe. */
10542struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10543 struct drm_crtc *crtc)
10544{
548f245b 10545 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10546 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10547 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10548 struct drm_display_mode *mode;
5cec258b 10549 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
10550 int htot = I915_READ(HTOTAL(cpu_transcoder));
10551 int hsync = I915_READ(HSYNC(cpu_transcoder));
10552 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10553 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10554 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10555
10556 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10557 if (!mode)
10558 return NULL;
10559
f1f644dc
JB
10560 /*
10561 * Construct a pipe_config sufficient for getting the clock info
10562 * back out of crtc_clock_get.
10563 *
10564 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10565 * to use a real value here instead.
10566 */
293623f7 10567 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 10568 pipe_config.pixel_multiplier = 1;
293623f7
VS
10569 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10570 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10571 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
10572 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10573
773ae034 10574 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
10575 mode->hdisplay = (htot & 0xffff) + 1;
10576 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10577 mode->hsync_start = (hsync & 0xffff) + 1;
10578 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10579 mode->vdisplay = (vtot & 0xffff) + 1;
10580 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10581 mode->vsync_start = (vsync & 0xffff) + 1;
10582 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10583
10584 drm_mode_set_name(mode);
79e53945
JB
10585
10586 return mode;
10587}
10588
f047e395
CW
10589void intel_mark_busy(struct drm_device *dev)
10590{
c67a470b
PZ
10591 struct drm_i915_private *dev_priv = dev->dev_private;
10592
f62a0076
CW
10593 if (dev_priv->mm.busy)
10594 return;
10595
43694d69 10596 intel_runtime_pm_get(dev_priv);
c67a470b 10597 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10598 if (INTEL_INFO(dev)->gen >= 6)
10599 gen6_rps_busy(dev_priv);
f62a0076 10600 dev_priv->mm.busy = true;
f047e395
CW
10601}
10602
10603void intel_mark_idle(struct drm_device *dev)
652c393a 10604{
c67a470b 10605 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10606
f62a0076
CW
10607 if (!dev_priv->mm.busy)
10608 return;
10609
10610 dev_priv->mm.busy = false;
10611
3d13ef2e 10612 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10613 gen6_rps_idle(dev->dev_private);
bb4cdd53 10614
43694d69 10615 intel_runtime_pm_put(dev_priv);
652c393a
JB
10616}
10617
79e53945
JB
10618static void intel_crtc_destroy(struct drm_crtc *crtc)
10619{
10620 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10621 struct drm_device *dev = crtc->dev;
10622 struct intel_unpin_work *work;
67e77c5a 10623
5e2d7afc 10624 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10625 work = intel_crtc->unpin_work;
10626 intel_crtc->unpin_work = NULL;
5e2d7afc 10627 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10628
10629 if (work) {
10630 cancel_work_sync(&work->work);
10631 kfree(work);
10632 }
79e53945
JB
10633
10634 drm_crtc_cleanup(crtc);
67e77c5a 10635
79e53945
JB
10636 kfree(intel_crtc);
10637}
10638
6b95a207
KH
10639static void intel_unpin_work_fn(struct work_struct *__work)
10640{
10641 struct intel_unpin_work *work =
10642 container_of(__work, struct intel_unpin_work, work);
b4a98e57 10643 struct drm_device *dev = work->crtc->dev;
f99d7069 10644 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 10645
b4a98e57 10646 mutex_lock(&dev->struct_mutex);
82bc3b2d 10647 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
05394f39 10648 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10649
7ff0ebcc 10650 intel_fbc_update(dev);
f06cc1b9
JH
10651
10652 if (work->flip_queued_req)
146d84f0 10653 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10654 mutex_unlock(&dev->struct_mutex);
10655
f99d7069 10656 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
89ed88ba 10657 drm_framebuffer_unreference(work->old_fb);
f99d7069 10658
b4a98e57
CW
10659 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10660 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10661
6b95a207
KH
10662 kfree(work);
10663}
10664
1afe3e9d 10665static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10666 struct drm_crtc *crtc)
6b95a207 10667{
6b95a207
KH
10668 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10669 struct intel_unpin_work *work;
6b95a207
KH
10670 unsigned long flags;
10671
10672 /* Ignore early vblank irqs */
10673 if (intel_crtc == NULL)
10674 return;
10675
f326038a
DV
10676 /*
10677 * This is called both by irq handlers and the reset code (to complete
10678 * lost pageflips) so needs the full irqsave spinlocks.
10679 */
6b95a207
KH
10680 spin_lock_irqsave(&dev->event_lock, flags);
10681 work = intel_crtc->unpin_work;
e7d841ca
CW
10682
10683 /* Ensure we don't miss a work->pending update ... */
10684 smp_rmb();
10685
10686 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10687 spin_unlock_irqrestore(&dev->event_lock, flags);
10688 return;
10689 }
10690
d6bbafa1 10691 page_flip_completed(intel_crtc);
0af7e4df 10692
6b95a207 10693 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10694}
10695
1afe3e9d
JB
10696void intel_finish_page_flip(struct drm_device *dev, int pipe)
10697{
fbee40df 10698 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10699 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10700
49b14a5c 10701 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10702}
10703
10704void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10705{
fbee40df 10706 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10707 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10708
49b14a5c 10709 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10710}
10711
75f7f3ec
VS
10712/* Is 'a' after or equal to 'b'? */
10713static bool g4x_flip_count_after_eq(u32 a, u32 b)
10714{
10715 return !((a - b) & 0x80000000);
10716}
10717
10718static bool page_flip_finished(struct intel_crtc *crtc)
10719{
10720 struct drm_device *dev = crtc->base.dev;
10721 struct drm_i915_private *dev_priv = dev->dev_private;
10722
bdfa7542
VS
10723 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10724 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10725 return true;
10726
75f7f3ec
VS
10727 /*
10728 * The relevant registers doen't exist on pre-ctg.
10729 * As the flip done interrupt doesn't trigger for mmio
10730 * flips on gmch platforms, a flip count check isn't
10731 * really needed there. But since ctg has the registers,
10732 * include it in the check anyway.
10733 */
10734 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10735 return true;
10736
10737 /*
10738 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10739 * used the same base address. In that case the mmio flip might
10740 * have completed, but the CS hasn't even executed the flip yet.
10741 *
10742 * A flip count check isn't enough as the CS might have updated
10743 * the base address just after start of vblank, but before we
10744 * managed to process the interrupt. This means we'd complete the
10745 * CS flip too soon.
10746 *
10747 * Combining both checks should get us a good enough result. It may
10748 * still happen that the CS flip has been executed, but has not
10749 * yet actually completed. But in case the base address is the same
10750 * anyway, we don't really care.
10751 */
10752 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10753 crtc->unpin_work->gtt_offset &&
10754 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10755 crtc->unpin_work->flip_count);
10756}
10757
6b95a207
KH
10758void intel_prepare_page_flip(struct drm_device *dev, int plane)
10759{
fbee40df 10760 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10761 struct intel_crtc *intel_crtc =
10762 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10763 unsigned long flags;
10764
f326038a
DV
10765
10766 /*
10767 * This is called both by irq handlers and the reset code (to complete
10768 * lost pageflips) so needs the full irqsave spinlocks.
10769 *
10770 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10771 * generate a page-flip completion irq, i.e. every modeset
10772 * is also accompanied by a spurious intel_prepare_page_flip().
10773 */
6b95a207 10774 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10775 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10776 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10777 spin_unlock_irqrestore(&dev->event_lock, flags);
10778}
10779
eba905b2 10780static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
10781{
10782 /* Ensure that the work item is consistent when activating it ... */
10783 smp_wmb();
10784 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10785 /* and that it is marked active as soon as the irq could fire. */
10786 smp_wmb();
10787}
10788
8c9f3aaf
JB
10789static int intel_gen2_queue_flip(struct drm_device *dev,
10790 struct drm_crtc *crtc,
10791 struct drm_framebuffer *fb,
ed8d1975 10792 struct drm_i915_gem_object *obj,
6258fbe2 10793 struct drm_i915_gem_request *req,
ed8d1975 10794 uint32_t flags)
8c9f3aaf 10795{
6258fbe2 10796 struct intel_engine_cs *ring = req->ring;
8c9f3aaf 10797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10798 u32 flip_mask;
10799 int ret;
10800
5fb9de1a 10801 ret = intel_ring_begin(req, 6);
8c9f3aaf 10802 if (ret)
4fa62c89 10803 return ret;
8c9f3aaf
JB
10804
10805 /* Can't queue multiple flips, so wait for the previous
10806 * one to finish before executing the next.
10807 */
10808 if (intel_crtc->plane)
10809 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10810 else
10811 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10812 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10813 intel_ring_emit(ring, MI_NOOP);
10814 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10815 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10816 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10817 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 10818 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
10819
10820 intel_mark_page_flip_active(intel_crtc);
83d4092b 10821 return 0;
8c9f3aaf
JB
10822}
10823
10824static int intel_gen3_queue_flip(struct drm_device *dev,
10825 struct drm_crtc *crtc,
10826 struct drm_framebuffer *fb,
ed8d1975 10827 struct drm_i915_gem_object *obj,
6258fbe2 10828 struct drm_i915_gem_request *req,
ed8d1975 10829 uint32_t flags)
8c9f3aaf 10830{
6258fbe2 10831 struct intel_engine_cs *ring = req->ring;
8c9f3aaf 10832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10833 u32 flip_mask;
10834 int ret;
10835
5fb9de1a 10836 ret = intel_ring_begin(req, 6);
8c9f3aaf 10837 if (ret)
4fa62c89 10838 return ret;
8c9f3aaf
JB
10839
10840 if (intel_crtc->plane)
10841 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10842 else
10843 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10844 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10845 intel_ring_emit(ring, MI_NOOP);
10846 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10847 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10848 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10849 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
10850 intel_ring_emit(ring, MI_NOOP);
10851
e7d841ca 10852 intel_mark_page_flip_active(intel_crtc);
83d4092b 10853 return 0;
8c9f3aaf
JB
10854}
10855
10856static int intel_gen4_queue_flip(struct drm_device *dev,
10857 struct drm_crtc *crtc,
10858 struct drm_framebuffer *fb,
ed8d1975 10859 struct drm_i915_gem_object *obj,
6258fbe2 10860 struct drm_i915_gem_request *req,
ed8d1975 10861 uint32_t flags)
8c9f3aaf 10862{
6258fbe2 10863 struct intel_engine_cs *ring = req->ring;
8c9f3aaf
JB
10864 struct drm_i915_private *dev_priv = dev->dev_private;
10865 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10866 uint32_t pf, pipesrc;
10867 int ret;
10868
5fb9de1a 10869 ret = intel_ring_begin(req, 4);
8c9f3aaf 10870 if (ret)
4fa62c89 10871 return ret;
8c9f3aaf
JB
10872
10873 /* i965+ uses the linear or tiled offsets from the
10874 * Display Registers (which do not change across a page-flip)
10875 * so we need only reprogram the base address.
10876 */
6d90c952
DV
10877 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10878 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10879 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10880 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 10881 obj->tiling_mode);
8c9f3aaf
JB
10882
10883 /* XXX Enabling the panel-fitter across page-flip is so far
10884 * untested on non-native modes, so ignore it for now.
10885 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10886 */
10887 pf = 0;
10888 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10889 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10890
10891 intel_mark_page_flip_active(intel_crtc);
83d4092b 10892 return 0;
8c9f3aaf
JB
10893}
10894
10895static int intel_gen6_queue_flip(struct drm_device *dev,
10896 struct drm_crtc *crtc,
10897 struct drm_framebuffer *fb,
ed8d1975 10898 struct drm_i915_gem_object *obj,
6258fbe2 10899 struct drm_i915_gem_request *req,
ed8d1975 10900 uint32_t flags)
8c9f3aaf 10901{
6258fbe2 10902 struct intel_engine_cs *ring = req->ring;
8c9f3aaf
JB
10903 struct drm_i915_private *dev_priv = dev->dev_private;
10904 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10905 uint32_t pf, pipesrc;
10906 int ret;
10907
5fb9de1a 10908 ret = intel_ring_begin(req, 4);
8c9f3aaf 10909 if (ret)
4fa62c89 10910 return ret;
8c9f3aaf 10911
6d90c952
DV
10912 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10913 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10914 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 10915 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 10916
dc257cf1
DV
10917 /* Contrary to the suggestions in the documentation,
10918 * "Enable Panel Fitter" does not seem to be required when page
10919 * flipping with a non-native mode, and worse causes a normal
10920 * modeset to fail.
10921 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10922 */
10923 pf = 0;
8c9f3aaf 10924 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10925 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10926
10927 intel_mark_page_flip_active(intel_crtc);
83d4092b 10928 return 0;
8c9f3aaf
JB
10929}
10930
7c9017e5
JB
10931static int intel_gen7_queue_flip(struct drm_device *dev,
10932 struct drm_crtc *crtc,
10933 struct drm_framebuffer *fb,
ed8d1975 10934 struct drm_i915_gem_object *obj,
6258fbe2 10935 struct drm_i915_gem_request *req,
ed8d1975 10936 uint32_t flags)
7c9017e5 10937{
6258fbe2 10938 struct intel_engine_cs *ring = req->ring;
7c9017e5 10939 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 10940 uint32_t plane_bit = 0;
ffe74d75
CW
10941 int len, ret;
10942
eba905b2 10943 switch (intel_crtc->plane) {
cb05d8de
DV
10944 case PLANE_A:
10945 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10946 break;
10947 case PLANE_B:
10948 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10949 break;
10950 case PLANE_C:
10951 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10952 break;
10953 default:
10954 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 10955 return -ENODEV;
cb05d8de
DV
10956 }
10957
ffe74d75 10958 len = 4;
f476828a 10959 if (ring->id == RCS) {
ffe74d75 10960 len += 6;
f476828a
DL
10961 /*
10962 * On Gen 8, SRM is now taking an extra dword to accommodate
10963 * 48bits addresses, and we need a NOOP for the batch size to
10964 * stay even.
10965 */
10966 if (IS_GEN8(dev))
10967 len += 2;
10968 }
ffe74d75 10969
f66fab8e
VS
10970 /*
10971 * BSpec MI_DISPLAY_FLIP for IVB:
10972 * "The full packet must be contained within the same cache line."
10973 *
10974 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10975 * cacheline, if we ever start emitting more commands before
10976 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10977 * then do the cacheline alignment, and finally emit the
10978 * MI_DISPLAY_FLIP.
10979 */
bba09b12 10980 ret = intel_ring_cacheline_align(req);
f66fab8e 10981 if (ret)
4fa62c89 10982 return ret;
f66fab8e 10983
5fb9de1a 10984 ret = intel_ring_begin(req, len);
7c9017e5 10985 if (ret)
4fa62c89 10986 return ret;
7c9017e5 10987
ffe74d75
CW
10988 /* Unmask the flip-done completion message. Note that the bspec says that
10989 * we should do this for both the BCS and RCS, and that we must not unmask
10990 * more than one flip event at any time (or ensure that one flip message
10991 * can be sent by waiting for flip-done prior to queueing new flips).
10992 * Experimentation says that BCS works despite DERRMR masking all
10993 * flip-done completion events and that unmasking all planes at once
10994 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10995 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10996 */
10997 if (ring->id == RCS) {
10998 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10999 intel_ring_emit(ring, DERRMR);
11000 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11001 DERRMR_PIPEB_PRI_FLIP_DONE |
11002 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
11003 if (IS_GEN8(dev))
11004 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11005 MI_SRM_LRM_GLOBAL_GTT);
11006 else
11007 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11008 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
11009 intel_ring_emit(ring, DERRMR);
11010 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
11011 if (IS_GEN8(dev)) {
11012 intel_ring_emit(ring, 0);
11013 intel_ring_emit(ring, MI_NOOP);
11014 }
ffe74d75
CW
11015 }
11016
cb05d8de 11017 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 11018 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 11019 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 11020 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
11021
11022 intel_mark_page_flip_active(intel_crtc);
83d4092b 11023 return 0;
7c9017e5
JB
11024}
11025
84c33a64
SG
11026static bool use_mmio_flip(struct intel_engine_cs *ring,
11027 struct drm_i915_gem_object *obj)
11028{
11029 /*
11030 * This is not being used for older platforms, because
11031 * non-availability of flip done interrupt forces us to use
11032 * CS flips. Older platforms derive flip done using some clever
11033 * tricks involving the flip_pending status bits and vblank irqs.
11034 * So using MMIO flips there would disrupt this mechanism.
11035 */
11036
8e09bf83
CW
11037 if (ring == NULL)
11038 return true;
11039
84c33a64
SG
11040 if (INTEL_INFO(ring->dev)->gen < 5)
11041 return false;
11042
11043 if (i915.use_mmio_flip < 0)
11044 return false;
11045 else if (i915.use_mmio_flip > 0)
11046 return true;
14bf993e
OM
11047 else if (i915.enable_execlists)
11048 return true;
84c33a64 11049 else
b4716185 11050 return ring != i915_gem_request_get_ring(obj->last_write_req);
84c33a64
SG
11051}
11052
ff944564
DL
11053static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11054{
11055 struct drm_device *dev = intel_crtc->base.dev;
11056 struct drm_i915_private *dev_priv = dev->dev_private;
11057 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564
DL
11058 const enum pipe pipe = intel_crtc->pipe;
11059 u32 ctl, stride;
11060
11061 ctl = I915_READ(PLANE_CTL(pipe, 0));
11062 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
11063 switch (fb->modifier[0]) {
11064 case DRM_FORMAT_MOD_NONE:
11065 break;
11066 case I915_FORMAT_MOD_X_TILED:
ff944564 11067 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
11068 break;
11069 case I915_FORMAT_MOD_Y_TILED:
11070 ctl |= PLANE_CTL_TILED_Y;
11071 break;
11072 case I915_FORMAT_MOD_Yf_TILED:
11073 ctl |= PLANE_CTL_TILED_YF;
11074 break;
11075 default:
11076 MISSING_CASE(fb->modifier[0]);
11077 }
ff944564
DL
11078
11079 /*
11080 * The stride is either expressed as a multiple of 64 bytes chunks for
11081 * linear buffers or in number of tiles for tiled buffers.
11082 */
2ebef630
TU
11083 stride = fb->pitches[0] /
11084 intel_fb_stride_alignment(dev, fb->modifier[0],
11085 fb->pixel_format);
ff944564
DL
11086
11087 /*
11088 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11089 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11090 */
11091 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11092 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11093
11094 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11095 POSTING_READ(PLANE_SURF(pipe, 0));
11096}
11097
11098static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
11099{
11100 struct drm_device *dev = intel_crtc->base.dev;
11101 struct drm_i915_private *dev_priv = dev->dev_private;
11102 struct intel_framebuffer *intel_fb =
11103 to_intel_framebuffer(intel_crtc->base.primary->fb);
11104 struct drm_i915_gem_object *obj = intel_fb->obj;
11105 u32 dspcntr;
11106 u32 reg;
11107
84c33a64
SG
11108 reg = DSPCNTR(intel_crtc->plane);
11109 dspcntr = I915_READ(reg);
11110
c5d97472
DL
11111 if (obj->tiling_mode != I915_TILING_NONE)
11112 dspcntr |= DISPPLANE_TILED;
11113 else
11114 dspcntr &= ~DISPPLANE_TILED;
11115
84c33a64
SG
11116 I915_WRITE(reg, dspcntr);
11117
11118 I915_WRITE(DSPSURF(intel_crtc->plane),
11119 intel_crtc->unpin_work->gtt_offset);
11120 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 11121
ff944564
DL
11122}
11123
11124/*
11125 * XXX: This is the temporary way to update the plane registers until we get
11126 * around to using the usual plane update functions for MMIO flips
11127 */
11128static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11129{
11130 struct drm_device *dev = intel_crtc->base.dev;
11131 bool atomic_update;
11132 u32 start_vbl_count;
11133
11134 intel_mark_page_flip_active(intel_crtc);
11135
11136 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11137
11138 if (INTEL_INFO(dev)->gen >= 9)
11139 skl_do_mmio_flip(intel_crtc);
11140 else
11141 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11142 ilk_do_mmio_flip(intel_crtc);
11143
9362c7c5
ACO
11144 if (atomic_update)
11145 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
11146}
11147
9362c7c5 11148static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 11149{
b2cfe0ab
CW
11150 struct intel_mmio_flip *mmio_flip =
11151 container_of(work, struct intel_mmio_flip, work);
84c33a64 11152
eed29a5b
DV
11153 if (mmio_flip->req)
11154 WARN_ON(__i915_wait_request(mmio_flip->req,
b2cfe0ab 11155 mmio_flip->crtc->reset_counter,
bcafc4e3
CW
11156 false, NULL,
11157 &mmio_flip->i915->rps.mmioflips));
84c33a64 11158
b2cfe0ab
CW
11159 intel_do_mmio_flip(mmio_flip->crtc);
11160
eed29a5b 11161 i915_gem_request_unreference__unlocked(mmio_flip->req);
b2cfe0ab 11162 kfree(mmio_flip);
84c33a64
SG
11163}
11164
11165static int intel_queue_mmio_flip(struct drm_device *dev,
11166 struct drm_crtc *crtc,
11167 struct drm_framebuffer *fb,
11168 struct drm_i915_gem_object *obj,
11169 struct intel_engine_cs *ring,
11170 uint32_t flags)
11171{
b2cfe0ab
CW
11172 struct intel_mmio_flip *mmio_flip;
11173
11174 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11175 if (mmio_flip == NULL)
11176 return -ENOMEM;
84c33a64 11177
bcafc4e3 11178 mmio_flip->i915 = to_i915(dev);
eed29a5b 11179 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
b2cfe0ab 11180 mmio_flip->crtc = to_intel_crtc(crtc);
536f5b5e 11181
b2cfe0ab
CW
11182 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11183 schedule_work(&mmio_flip->work);
84c33a64 11184
84c33a64
SG
11185 return 0;
11186}
11187
8c9f3aaf
JB
11188static int intel_default_queue_flip(struct drm_device *dev,
11189 struct drm_crtc *crtc,
11190 struct drm_framebuffer *fb,
ed8d1975 11191 struct drm_i915_gem_object *obj,
6258fbe2 11192 struct drm_i915_gem_request *req,
ed8d1975 11193 uint32_t flags)
8c9f3aaf
JB
11194{
11195 return -ENODEV;
11196}
11197
d6bbafa1
CW
11198static bool __intel_pageflip_stall_check(struct drm_device *dev,
11199 struct drm_crtc *crtc)
11200{
11201 struct drm_i915_private *dev_priv = dev->dev_private;
11202 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11203 struct intel_unpin_work *work = intel_crtc->unpin_work;
11204 u32 addr;
11205
11206 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11207 return true;
11208
11209 if (!work->enable_stall_check)
11210 return false;
11211
11212 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
11213 if (work->flip_queued_req &&
11214 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
11215 return false;
11216
1e3feefd 11217 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
11218 }
11219
1e3feefd 11220 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
11221 return false;
11222
11223 /* Potential stall - if we see that the flip has happened,
11224 * assume a missed interrupt. */
11225 if (INTEL_INFO(dev)->gen >= 4)
11226 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11227 else
11228 addr = I915_READ(DSPADDR(intel_crtc->plane));
11229
11230 /* There is a potential issue here with a false positive after a flip
11231 * to the same address. We could address this by checking for a
11232 * non-incrementing frame counter.
11233 */
11234 return addr == work->gtt_offset;
11235}
11236
11237void intel_check_page_flip(struct drm_device *dev, int pipe)
11238{
11239 struct drm_i915_private *dev_priv = dev->dev_private;
11240 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 11242 struct intel_unpin_work *work;
f326038a 11243
6c51d46f 11244 WARN_ON(!in_interrupt());
d6bbafa1
CW
11245
11246 if (crtc == NULL)
11247 return;
11248
f326038a 11249 spin_lock(&dev->event_lock);
6ad790c0
CW
11250 work = intel_crtc->unpin_work;
11251 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 11252 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 11253 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 11254 page_flip_completed(intel_crtc);
6ad790c0 11255 work = NULL;
d6bbafa1 11256 }
6ad790c0
CW
11257 if (work != NULL &&
11258 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11259 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 11260 spin_unlock(&dev->event_lock);
d6bbafa1
CW
11261}
11262
6b95a207
KH
11263static int intel_crtc_page_flip(struct drm_crtc *crtc,
11264 struct drm_framebuffer *fb,
ed8d1975
KP
11265 struct drm_pending_vblank_event *event,
11266 uint32_t page_flip_flags)
6b95a207
KH
11267{
11268 struct drm_device *dev = crtc->dev;
11269 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 11270 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 11271 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 11272 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 11273 struct drm_plane *primary = crtc->primary;
a071fa00 11274 enum pipe pipe = intel_crtc->pipe;
6b95a207 11275 struct intel_unpin_work *work;
a4872ba6 11276 struct intel_engine_cs *ring;
cf5d8a46 11277 bool mmio_flip;
91af127f 11278 struct drm_i915_gem_request *request = NULL;
52e68630 11279 int ret;
6b95a207 11280
2ff8fde1
MR
11281 /*
11282 * drm_mode_page_flip_ioctl() should already catch this, but double
11283 * check to be safe. In the future we may enable pageflipping from
11284 * a disabled primary plane.
11285 */
11286 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11287 return -EBUSY;
11288
e6a595d2 11289 /* Can't change pixel format via MI display flips. */
f4510a27 11290 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
11291 return -EINVAL;
11292
11293 /*
11294 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11295 * Note that pitch changes could also affect these register.
11296 */
11297 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
11298 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11299 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
11300 return -EINVAL;
11301
f900db47
CW
11302 if (i915_terminally_wedged(&dev_priv->gpu_error))
11303 goto out_hang;
11304
b14c5679 11305 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
11306 if (work == NULL)
11307 return -ENOMEM;
11308
6b95a207 11309 work->event = event;
b4a98e57 11310 work->crtc = crtc;
ab8d6675 11311 work->old_fb = old_fb;
6b95a207
KH
11312 INIT_WORK(&work->work, intel_unpin_work_fn);
11313
87b6b101 11314 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
11315 if (ret)
11316 goto free_work;
11317
6b95a207 11318 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 11319 spin_lock_irq(&dev->event_lock);
6b95a207 11320 if (intel_crtc->unpin_work) {
d6bbafa1
CW
11321 /* Before declaring the flip queue wedged, check if
11322 * the hardware completed the operation behind our backs.
11323 */
11324 if (__intel_pageflip_stall_check(dev, crtc)) {
11325 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11326 page_flip_completed(intel_crtc);
11327 } else {
11328 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 11329 spin_unlock_irq(&dev->event_lock);
468f0b44 11330
d6bbafa1
CW
11331 drm_crtc_vblank_put(crtc);
11332 kfree(work);
11333 return -EBUSY;
11334 }
6b95a207
KH
11335 }
11336 intel_crtc->unpin_work = work;
5e2d7afc 11337 spin_unlock_irq(&dev->event_lock);
6b95a207 11338
b4a98e57
CW
11339 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11340 flush_workqueue(dev_priv->wq);
11341
75dfca80 11342 /* Reference the objects for the scheduled work. */
ab8d6675 11343 drm_framebuffer_reference(work->old_fb);
05394f39 11344 drm_gem_object_reference(&obj->base);
6b95a207 11345
f4510a27 11346 crtc->primary->fb = fb;
afd65eb4 11347 update_state_fb(crtc->primary);
1ed1f968 11348
e1f99ce6 11349 work->pending_flip_obj = obj;
e1f99ce6 11350
89ed88ba
CW
11351 ret = i915_mutex_lock_interruptible(dev);
11352 if (ret)
11353 goto cleanup;
11354
b4a98e57 11355 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 11356 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 11357
75f7f3ec 11358 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 11359 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 11360
4fa62c89
VS
11361 if (IS_VALLEYVIEW(dev)) {
11362 ring = &dev_priv->ring[BCS];
ab8d6675 11363 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
11364 /* vlv: DISPLAY_FLIP fails to change tiling */
11365 ring = NULL;
48bf5b2d 11366 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 11367 ring = &dev_priv->ring[BCS];
4fa62c89 11368 } else if (INTEL_INFO(dev)->gen >= 7) {
b4716185 11369 ring = i915_gem_request_get_ring(obj->last_write_req);
4fa62c89
VS
11370 if (ring == NULL || ring->id != RCS)
11371 ring = &dev_priv->ring[BCS];
11372 } else {
11373 ring = &dev_priv->ring[RCS];
11374 }
11375
cf5d8a46
CW
11376 mmio_flip = use_mmio_flip(ring, obj);
11377
11378 /* When using CS flips, we want to emit semaphores between rings.
11379 * However, when using mmio flips we will create a task to do the
11380 * synchronisation, so all we want here is to pin the framebuffer
11381 * into the display plane and skip any waits.
11382 */
82bc3b2d 11383 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
cf5d8a46 11384 crtc->primary->state,
91af127f 11385 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
8c9f3aaf
JB
11386 if (ret)
11387 goto cleanup_pending;
6b95a207 11388
121920fa
TU
11389 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11390 + intel_crtc->dspaddr_offset;
4fa62c89 11391
cf5d8a46 11392 if (mmio_flip) {
84c33a64
SG
11393 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11394 page_flip_flags);
d6bbafa1
CW
11395 if (ret)
11396 goto cleanup_unpin;
11397
f06cc1b9
JH
11398 i915_gem_request_assign(&work->flip_queued_req,
11399 obj->last_write_req);
d6bbafa1 11400 } else {
6258fbe2
JH
11401 if (!request) {
11402 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11403 if (ret)
11404 goto cleanup_unpin;
11405 }
11406
11407 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
d6bbafa1
CW
11408 page_flip_flags);
11409 if (ret)
11410 goto cleanup_unpin;
11411
6258fbe2 11412 i915_gem_request_assign(&work->flip_queued_req, request);
d6bbafa1
CW
11413 }
11414
91af127f 11415 if (request)
75289874 11416 i915_add_request_no_flush(request);
91af127f 11417
1e3feefd 11418 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11419 work->enable_stall_check = true;
4fa62c89 11420
ab8d6675 11421 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a071fa00
DV
11422 INTEL_FRONTBUFFER_PRIMARY(pipe));
11423
7ff0ebcc 11424 intel_fbc_disable(dev);
f99d7069 11425 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
11426 mutex_unlock(&dev->struct_mutex);
11427
e5510fac
JB
11428 trace_i915_flip_request(intel_crtc->plane, obj);
11429
6b95a207 11430 return 0;
96b099fd 11431
4fa62c89 11432cleanup_unpin:
82bc3b2d 11433 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 11434cleanup_pending:
91af127f
JH
11435 if (request)
11436 i915_gem_request_cancel(request);
b4a98e57 11437 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11438 mutex_unlock(&dev->struct_mutex);
11439cleanup:
f4510a27 11440 crtc->primary->fb = old_fb;
afd65eb4 11441 update_state_fb(crtc->primary);
89ed88ba
CW
11442
11443 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11444 drm_framebuffer_unreference(work->old_fb);
96b099fd 11445
5e2d7afc 11446 spin_lock_irq(&dev->event_lock);
96b099fd 11447 intel_crtc->unpin_work = NULL;
5e2d7afc 11448 spin_unlock_irq(&dev->event_lock);
96b099fd 11449
87b6b101 11450 drm_crtc_vblank_put(crtc);
7317c75e 11451free_work:
96b099fd
CW
11452 kfree(work);
11453
f900db47 11454 if (ret == -EIO) {
02e0efb5
ML
11455 struct drm_atomic_state *state;
11456 struct drm_plane_state *plane_state;
11457
f900db47 11458out_hang:
02e0efb5
ML
11459 state = drm_atomic_state_alloc(dev);
11460 if (!state)
11461 return -ENOMEM;
11462 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11463
11464retry:
11465 plane_state = drm_atomic_get_plane_state(state, primary);
11466 ret = PTR_ERR_OR_ZERO(plane_state);
11467 if (!ret) {
11468 drm_atomic_set_fb_for_plane(plane_state, fb);
11469
11470 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11471 if (!ret)
11472 ret = drm_atomic_commit(state);
11473 }
11474
11475 if (ret == -EDEADLK) {
11476 drm_modeset_backoff(state->acquire_ctx);
11477 drm_atomic_state_clear(state);
11478 goto retry;
11479 }
11480
11481 if (ret)
11482 drm_atomic_state_free(state);
11483
f0d3dad3 11484 if (ret == 0 && event) {
5e2d7afc 11485 spin_lock_irq(&dev->event_lock);
a071fa00 11486 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11487 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11488 }
f900db47 11489 }
96b099fd 11490 return ret;
6b95a207
KH
11491}
11492
da20eabd
ML
11493
11494/**
11495 * intel_wm_need_update - Check whether watermarks need updating
11496 * @plane: drm plane
11497 * @state: new plane state
11498 *
11499 * Check current plane state versus the new one to determine whether
11500 * watermarks need to be recalculated.
11501 *
11502 * Returns true or false.
11503 */
11504static bool intel_wm_need_update(struct drm_plane *plane,
11505 struct drm_plane_state *state)
11506{
11507 /* Update watermarks on tiling changes. */
11508 if (!plane->state->fb || !state->fb ||
11509 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11510 plane->state->rotation != state->rotation)
11511 return true;
11512
11513 if (plane->state->crtc_w != state->crtc_w)
11514 return true;
11515
11516 return false;
11517}
11518
11519int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11520 struct drm_plane_state *plane_state)
11521{
11522 struct drm_crtc *crtc = crtc_state->crtc;
11523 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11524 struct drm_plane *plane = plane_state->plane;
11525 struct drm_device *dev = crtc->dev;
11526 struct drm_i915_private *dev_priv = dev->dev_private;
11527 struct intel_plane_state *old_plane_state =
11528 to_intel_plane_state(plane->state);
11529 int idx = intel_crtc->base.base.id, ret;
11530 int i = drm_plane_index(plane);
11531 bool mode_changed = needs_modeset(crtc_state);
11532 bool was_crtc_enabled = crtc->state->active;
11533 bool is_crtc_enabled = crtc_state->active;
11534
11535 bool turn_off, turn_on, visible, was_visible;
11536 struct drm_framebuffer *fb = plane_state->fb;
11537
11538 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11539 plane->type != DRM_PLANE_TYPE_CURSOR) {
11540 ret = skl_update_scaler_plane(
11541 to_intel_crtc_state(crtc_state),
11542 to_intel_plane_state(plane_state));
11543 if (ret)
11544 return ret;
11545 }
11546
11547 /*
11548 * Disabling a plane is always okay; we just need to update
11549 * fb tracking in a special way since cleanup_fb() won't
11550 * get called by the plane helpers.
11551 */
11552 if (old_plane_state->base.fb && !fb)
11553 intel_crtc->atomic.disabled_planes |= 1 << i;
11554
da20eabd
ML
11555 was_visible = old_plane_state->visible;
11556 visible = to_intel_plane_state(plane_state)->visible;
11557
11558 if (!was_crtc_enabled && WARN_ON(was_visible))
11559 was_visible = false;
11560
11561 if (!is_crtc_enabled && WARN_ON(visible))
11562 visible = false;
11563
11564 if (!was_visible && !visible)
11565 return 0;
11566
11567 turn_off = was_visible && (!visible || mode_changed);
11568 turn_on = visible && (!was_visible || mode_changed);
11569
11570 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11571 plane->base.id, fb ? fb->base.id : -1);
11572
11573 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11574 plane->base.id, was_visible, visible,
11575 turn_off, turn_on, mode_changed);
11576
11577 if (intel_wm_need_update(plane, plane_state))
11578 intel_crtc->atomic.update_wm = true;
11579
11580 switch (plane->type) {
11581 case DRM_PLANE_TYPE_PRIMARY:
11582 if (visible)
11583 intel_crtc->atomic.fb_bits |=
11584 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
11585
11586 intel_crtc->atomic.wait_for_flips = true;
11587 intel_crtc->atomic.pre_disable_primary = turn_off;
11588 intel_crtc->atomic.post_enable_primary = turn_on;
11589
11590 if (turn_off)
11591 intel_crtc->atomic.disable_fbc = true;
11592
11593 /*
11594 * FBC does not work on some platforms for rotated
11595 * planes, so disable it when rotation is not 0 and
11596 * update it when rotation is set back to 0.
11597 *
11598 * FIXME: This is redundant with the fbc update done in
11599 * the primary plane enable function except that that
11600 * one is done too late. We eventually need to unify
11601 * this.
11602 */
11603
11604 if (visible &&
11605 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11606 dev_priv->fbc.crtc == intel_crtc &&
11607 plane_state->rotation != BIT(DRM_ROTATE_0))
11608 intel_crtc->atomic.disable_fbc = true;
11609
11610 /*
11611 * BDW signals flip done immediately if the plane
11612 * is disabled, even if the plane enable is already
11613 * armed to occur at the next vblank :(
11614 */
11615 if (turn_on && IS_BROADWELL(dev))
11616 intel_crtc->atomic.wait_vblank = true;
11617
11618 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11619 break;
11620 case DRM_PLANE_TYPE_CURSOR:
11621 if (visible)
11622 intel_crtc->atomic.fb_bits |=
11623 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
11624 break;
11625 case DRM_PLANE_TYPE_OVERLAY:
11626 /*
11627 * 'prepare' is never called when plane is being disabled, so
11628 * we need to handle frontbuffer tracking as a special case
11629 */
11630 if (visible)
11631 intel_crtc->atomic.fb_bits |=
11632 INTEL_FRONTBUFFER_SPRITE(intel_crtc->pipe);
11633
d032ffa0 11634 if (turn_off && !mode_changed) {
da20eabd
ML
11635 intel_crtc->atomic.wait_vblank = true;
11636 intel_crtc->atomic.update_sprite_watermarks |=
11637 1 << i;
11638 }
11639 break;
11640 }
11641 return 0;
11642}
11643
6d3a1ce7
ML
11644static bool encoders_cloneable(const struct intel_encoder *a,
11645 const struct intel_encoder *b)
11646{
11647 /* masks could be asymmetric, so check both ways */
11648 return a == b || (a->cloneable & (1 << b->type) &&
11649 b->cloneable & (1 << a->type));
11650}
11651
11652static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11653 struct intel_crtc *crtc,
11654 struct intel_encoder *encoder)
11655{
11656 struct intel_encoder *source_encoder;
11657 struct drm_connector *connector;
11658 struct drm_connector_state *connector_state;
11659 int i;
11660
11661 for_each_connector_in_state(state, connector, connector_state, i) {
11662 if (connector_state->crtc != &crtc->base)
11663 continue;
11664
11665 source_encoder =
11666 to_intel_encoder(connector_state->best_encoder);
11667 if (!encoders_cloneable(encoder, source_encoder))
11668 return false;
11669 }
11670
11671 return true;
11672}
11673
11674static bool check_encoder_cloning(struct drm_atomic_state *state,
11675 struct intel_crtc *crtc)
11676{
11677 struct intel_encoder *encoder;
11678 struct drm_connector *connector;
11679 struct drm_connector_state *connector_state;
11680 int i;
11681
11682 for_each_connector_in_state(state, connector, connector_state, i) {
11683 if (connector_state->crtc != &crtc->base)
11684 continue;
11685
11686 encoder = to_intel_encoder(connector_state->best_encoder);
11687 if (!check_single_encoder_cloning(state, crtc, encoder))
11688 return false;
11689 }
11690
11691 return true;
11692}
11693
d032ffa0
ML
11694static void intel_crtc_check_initial_planes(struct drm_crtc *crtc,
11695 struct drm_crtc_state *crtc_state)
11696{
11697 struct intel_crtc_state *pipe_config =
11698 to_intel_crtc_state(crtc_state);
11699 struct drm_plane *p;
11700 unsigned visible_mask = 0;
11701
11702 drm_for_each_plane_mask(p, crtc->dev, crtc_state->plane_mask) {
11703 struct drm_plane_state *plane_state =
11704 drm_atomic_get_existing_plane_state(crtc_state->state, p);
11705
11706 if (WARN_ON(!plane_state))
11707 continue;
11708
11709 if (!plane_state->fb)
11710 crtc_state->plane_mask &=
11711 ~(1 << drm_plane_index(p));
11712 else if (to_intel_plane_state(plane_state)->visible)
11713 visible_mask |= 1 << drm_plane_index(p);
11714 }
11715
11716 if (!visible_mask)
11717 return;
11718
11719 pipe_config->quirks &= ~PIPE_CONFIG_QUIRK_INITIAL_PLANES;
11720}
11721
6d3a1ce7
ML
11722static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11723 struct drm_crtc_state *crtc_state)
11724{
cf5a15be 11725 struct drm_device *dev = crtc->dev;
ad421372 11726 struct drm_i915_private *dev_priv = dev->dev_private;
6d3a1ce7 11727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cf5a15be
ML
11728 struct intel_crtc_state *pipe_config =
11729 to_intel_crtc_state(crtc_state);
6d3a1ce7 11730 struct drm_atomic_state *state = crtc_state->state;
ad421372 11731 int ret, idx = crtc->base.id;
6d3a1ce7
ML
11732 bool mode_changed = needs_modeset(crtc_state);
11733
11734 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11735 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11736 return -EINVAL;
11737 }
11738
11739 I915_STATE_WARN(crtc->state->active != intel_crtc->active,
11740 "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
11741 idx, crtc->state->active, intel_crtc->active);
11742
d032ffa0
ML
11743 /* plane mask is fixed up after all initial planes are calculated */
11744 if (pipe_config->quirks & PIPE_CONFIG_QUIRK_INITIAL_PLANES)
11745 intel_crtc_check_initial_planes(crtc, crtc_state);
11746
eddfcbcd
ML
11747 if (mode_changed)
11748 intel_crtc->atomic.update_wm = !crtc_state->active;
11749
ad421372
ML
11750 if (mode_changed && crtc_state->enable &&
11751 dev_priv->display.crtc_compute_clock &&
11752 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11753 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11754 pipe_config);
11755 if (ret)
11756 return ret;
11757 }
11758
cf5a15be 11759 return intel_atomic_setup_scalers(dev, intel_crtc, pipe_config);
6d3a1ce7
ML
11760}
11761
65b38e0d 11762static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11763 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11764 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11765 .atomic_begin = intel_begin_crtc_commit,
11766 .atomic_flush = intel_finish_crtc_commit,
6d3a1ce7 11767 .atomic_check = intel_crtc_atomic_check,
f6e5b160
CW
11768};
11769
9a935856
DV
11770/**
11771 * intel_modeset_update_staged_output_state
11772 *
11773 * Updates the staged output configuration state, e.g. after we've read out the
11774 * current hw state.
11775 */
11776static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 11777{
7668851f 11778 struct intel_crtc *crtc;
9a935856
DV
11779 struct intel_encoder *encoder;
11780 struct intel_connector *connector;
f6e5b160 11781
3a3371ff 11782 for_each_intel_connector(dev, connector) {
9a935856
DV
11783 connector->new_encoder =
11784 to_intel_encoder(connector->base.encoder);
11785 }
f6e5b160 11786
b2784e15 11787 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11788 encoder->new_crtc =
11789 to_intel_crtc(encoder->base.crtc);
11790 }
7668851f 11791
d3fcc808 11792 for_each_intel_crtc(dev, crtc) {
83d65738 11793 crtc->new_enabled = crtc->base.state->enable;
7668851f 11794 }
f6e5b160
CW
11795}
11796
d29b2f9d
ACO
11797/* Transitional helper to copy current connector/encoder state to
11798 * connector->state. This is needed so that code that is partially
11799 * converted to atomic does the right thing.
11800 */
11801static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11802{
11803 struct intel_connector *connector;
11804
11805 for_each_intel_connector(dev, connector) {
11806 if (connector->base.encoder) {
11807 connector->base.state->best_encoder =
11808 connector->base.encoder;
11809 connector->base.state->crtc =
11810 connector->base.encoder->crtc;
11811 } else {
11812 connector->base.state->best_encoder = NULL;
11813 connector->base.state->crtc = NULL;
11814 }
11815 }
11816}
11817
050f7aeb 11818static void
eba905b2 11819connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11820 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11821{
11822 int bpp = pipe_config->pipe_bpp;
11823
11824 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11825 connector->base.base.id,
c23cc417 11826 connector->base.name);
050f7aeb
DV
11827
11828 /* Don't use an invalid EDID bpc value */
11829 if (connector->base.display_info.bpc &&
11830 connector->base.display_info.bpc * 3 < bpp) {
11831 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11832 bpp, connector->base.display_info.bpc*3);
11833 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11834 }
11835
11836 /* Clamp bpp to 8 on screens without EDID 1.4 */
11837 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11838 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11839 bpp);
11840 pipe_config->pipe_bpp = 24;
11841 }
11842}
11843
4e53c2e0 11844static int
050f7aeb 11845compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11846 struct intel_crtc_state *pipe_config)
4e53c2e0 11847{
050f7aeb 11848 struct drm_device *dev = crtc->base.dev;
1486017f 11849 struct drm_atomic_state *state;
da3ced29
ACO
11850 struct drm_connector *connector;
11851 struct drm_connector_state *connector_state;
1486017f 11852 int bpp, i;
4e53c2e0 11853
d328c9d7 11854 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
4e53c2e0 11855 bpp = 10*3;
d328c9d7
DV
11856 else if (INTEL_INFO(dev)->gen >= 5)
11857 bpp = 12*3;
11858 else
11859 bpp = 8*3;
11860
4e53c2e0 11861
4e53c2e0
DV
11862 pipe_config->pipe_bpp = bpp;
11863
1486017f
ACO
11864 state = pipe_config->base.state;
11865
4e53c2e0 11866 /* Clamp display bpp to EDID value */
da3ced29
ACO
11867 for_each_connector_in_state(state, connector, connector_state, i) {
11868 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11869 continue;
11870
da3ced29
ACO
11871 connected_sink_compute_bpp(to_intel_connector(connector),
11872 pipe_config);
4e53c2e0
DV
11873 }
11874
11875 return bpp;
11876}
11877
644db711
DV
11878static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11879{
11880 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11881 "type: 0x%x flags: 0x%x\n",
1342830c 11882 mode->crtc_clock,
644db711
DV
11883 mode->crtc_hdisplay, mode->crtc_hsync_start,
11884 mode->crtc_hsync_end, mode->crtc_htotal,
11885 mode->crtc_vdisplay, mode->crtc_vsync_start,
11886 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11887}
11888
c0b03411 11889static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11890 struct intel_crtc_state *pipe_config,
c0b03411
DV
11891 const char *context)
11892{
6a60cd87
CK
11893 struct drm_device *dev = crtc->base.dev;
11894 struct drm_plane *plane;
11895 struct intel_plane *intel_plane;
11896 struct intel_plane_state *state;
11897 struct drm_framebuffer *fb;
11898
11899 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11900 context, pipe_config, pipe_name(crtc->pipe));
c0b03411
DV
11901
11902 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11903 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11904 pipe_config->pipe_bpp, pipe_config->dither);
11905 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11906 pipe_config->has_pch_encoder,
11907 pipe_config->fdi_lanes,
11908 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11909 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11910 pipe_config->fdi_m_n.tu);
eb14cb74
VS
11911 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11912 pipe_config->has_dp_encoder,
11913 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11914 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11915 pipe_config->dp_m_n.tu);
b95af8be
VK
11916
11917 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11918 pipe_config->has_dp_encoder,
11919 pipe_config->dp_m2_n2.gmch_m,
11920 pipe_config->dp_m2_n2.gmch_n,
11921 pipe_config->dp_m2_n2.link_m,
11922 pipe_config->dp_m2_n2.link_n,
11923 pipe_config->dp_m2_n2.tu);
11924
55072d19
DV
11925 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11926 pipe_config->has_audio,
11927 pipe_config->has_infoframe);
11928
c0b03411 11929 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11930 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11931 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11932 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11933 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 11934 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
11935 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11936 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
0ec463d3
TU
11937 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11938 crtc->num_scalers,
11939 pipe_config->scaler_state.scaler_users,
11940 pipe_config->scaler_state.scaler_id);
c0b03411
DV
11941 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11942 pipe_config->gmch_pfit.control,
11943 pipe_config->gmch_pfit.pgm_ratios,
11944 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 11945 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 11946 pipe_config->pch_pfit.pos,
fd4daa9c
CW
11947 pipe_config->pch_pfit.size,
11948 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 11949 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 11950 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 11951
415ff0f6
TU
11952 if (IS_BROXTON(dev)) {
11953 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11954 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11955 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11956 pipe_config->ddi_pll_sel,
11957 pipe_config->dpll_hw_state.ebb0,
11958 pipe_config->dpll_hw_state.pll0,
11959 pipe_config->dpll_hw_state.pll1,
11960 pipe_config->dpll_hw_state.pll2,
11961 pipe_config->dpll_hw_state.pll3,
11962 pipe_config->dpll_hw_state.pll6,
11963 pipe_config->dpll_hw_state.pll8,
11964 pipe_config->dpll_hw_state.pcsdw12);
11965 } else if (IS_SKYLAKE(dev)) {
11966 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11967 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11968 pipe_config->ddi_pll_sel,
11969 pipe_config->dpll_hw_state.ctrl1,
11970 pipe_config->dpll_hw_state.cfgcr1,
11971 pipe_config->dpll_hw_state.cfgcr2);
11972 } else if (HAS_DDI(dev)) {
11973 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11974 pipe_config->ddi_pll_sel,
11975 pipe_config->dpll_hw_state.wrpll);
11976 } else {
11977 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11978 "fp0: 0x%x, fp1: 0x%x\n",
11979 pipe_config->dpll_hw_state.dpll,
11980 pipe_config->dpll_hw_state.dpll_md,
11981 pipe_config->dpll_hw_state.fp0,
11982 pipe_config->dpll_hw_state.fp1);
11983 }
11984
6a60cd87
CK
11985 DRM_DEBUG_KMS("planes on this crtc\n");
11986 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11987 intel_plane = to_intel_plane(plane);
11988 if (intel_plane->pipe != crtc->pipe)
11989 continue;
11990
11991 state = to_intel_plane_state(plane->state);
11992 fb = state->base.fb;
11993 if (!fb) {
11994 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11995 "disabled, scaler_id = %d\n",
11996 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11997 plane->base.id, intel_plane->pipe,
11998 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11999 drm_plane_index(plane), state->scaler_id);
12000 continue;
12001 }
12002
12003 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12004 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12005 plane->base.id, intel_plane->pipe,
12006 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12007 drm_plane_index(plane));
12008 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12009 fb->base.id, fb->width, fb->height, fb->pixel_format);
12010 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12011 state->scaler_id,
12012 state->src.x1 >> 16, state->src.y1 >> 16,
12013 drm_rect_width(&state->src) >> 16,
12014 drm_rect_height(&state->src) >> 16,
12015 state->dst.x1, state->dst.y1,
12016 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12017 }
c0b03411
DV
12018}
12019
5448a00d 12020static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 12021{
5448a00d
ACO
12022 struct drm_device *dev = state->dev;
12023 struct intel_encoder *encoder;
da3ced29 12024 struct drm_connector *connector;
5448a00d 12025 struct drm_connector_state *connector_state;
00f0b378 12026 unsigned int used_ports = 0;
5448a00d 12027 int i;
00f0b378
VS
12028
12029 /*
12030 * Walk the connector list instead of the encoder
12031 * list to detect the problem on ddi platforms
12032 * where there's just one encoder per digital port.
12033 */
da3ced29 12034 for_each_connector_in_state(state, connector, connector_state, i) {
5448a00d 12035 if (!connector_state->best_encoder)
00f0b378
VS
12036 continue;
12037
5448a00d
ACO
12038 encoder = to_intel_encoder(connector_state->best_encoder);
12039
12040 WARN_ON(!connector_state->crtc);
00f0b378
VS
12041
12042 switch (encoder->type) {
12043 unsigned int port_mask;
12044 case INTEL_OUTPUT_UNKNOWN:
12045 if (WARN_ON(!HAS_DDI(dev)))
12046 break;
12047 case INTEL_OUTPUT_DISPLAYPORT:
12048 case INTEL_OUTPUT_HDMI:
12049 case INTEL_OUTPUT_EDP:
12050 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12051
12052 /* the same port mustn't appear more than once */
12053 if (used_ports & port_mask)
12054 return false;
12055
12056 used_ports |= port_mask;
12057 default:
12058 break;
12059 }
12060 }
12061
12062 return true;
12063}
12064
83a57153
ACO
12065static void
12066clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12067{
12068 struct drm_crtc_state tmp_state;
663a3640 12069 struct intel_crtc_scaler_state scaler_state;
4978cc93
ACO
12070 struct intel_dpll_hw_state dpll_hw_state;
12071 enum intel_dpll_id shared_dpll;
8504c74c 12072 uint32_t ddi_pll_sel;
83a57153 12073
7546a384
ACO
12074 /* FIXME: before the switch to atomic started, a new pipe_config was
12075 * kzalloc'd. Code that depends on any field being zero should be
12076 * fixed, so that the crtc_state can be safely duplicated. For now,
12077 * only fields that are know to not cause problems are preserved. */
12078
83a57153 12079 tmp_state = crtc_state->base;
663a3640 12080 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
12081 shared_dpll = crtc_state->shared_dpll;
12082 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 12083 ddi_pll_sel = crtc_state->ddi_pll_sel;
4978cc93 12084
83a57153 12085 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 12086
83a57153 12087 crtc_state->base = tmp_state;
663a3640 12088 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
12089 crtc_state->shared_dpll = shared_dpll;
12090 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 12091 crtc_state->ddi_pll_sel = ddi_pll_sel;
83a57153
ACO
12092}
12093
548ee15b 12094static int
b8cecdf5 12095intel_modeset_pipe_config(struct drm_crtc *crtc,
b359283a 12096 struct intel_crtc_state *pipe_config)
ee7b9f93 12097{
b359283a 12098 struct drm_atomic_state *state = pipe_config->base.state;
7758a113 12099 struct intel_encoder *encoder;
da3ced29 12100 struct drm_connector *connector;
0b901879 12101 struct drm_connector_state *connector_state;
d328c9d7 12102 int base_bpp, ret = -EINVAL;
0b901879 12103 int i;
e29c22c0 12104 bool retry = true;
ee7b9f93 12105
83a57153 12106 clear_intel_crtc_state(pipe_config);
7758a113 12107
e143a21c
DV
12108 pipe_config->cpu_transcoder =
12109 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 12110
2960bc9c
ID
12111 /*
12112 * Sanitize sync polarity flags based on requested ones. If neither
12113 * positive or negative polarity is requested, treat this as meaning
12114 * negative polarity.
12115 */
2d112de7 12116 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12117 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 12118 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 12119
2d112de7 12120 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 12121 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 12122 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 12123
050f7aeb
DV
12124 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12125 * plane pixel format and any sink constraints into account. Returns the
12126 * source plane bpp so that dithering can be selected on mismatches
12127 * after encoders and crtc also have had their say. */
d328c9d7
DV
12128 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12129 pipe_config);
12130 if (base_bpp < 0)
4e53c2e0
DV
12131 goto fail;
12132
e41a56be
VS
12133 /*
12134 * Determine the real pipe dimensions. Note that stereo modes can
12135 * increase the actual pipe size due to the frame doubling and
12136 * insertion of additional space for blanks between the frame. This
12137 * is stored in the crtc timings. We use the requested mode to do this
12138 * computation to clearly distinguish it from the adjusted mode, which
12139 * can be changed by the connectors in the below retry loop.
12140 */
2d112de7 12141 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
12142 &pipe_config->pipe_src_w,
12143 &pipe_config->pipe_src_h);
e41a56be 12144
e29c22c0 12145encoder_retry:
ef1b460d 12146 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 12147 pipe_config->port_clock = 0;
ef1b460d 12148 pipe_config->pixel_multiplier = 1;
ff9a6750 12149
135c81b8 12150 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
12151 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12152 CRTC_STEREO_DOUBLE);
135c81b8 12153
7758a113
DV
12154 /* Pass our mode to the connectors and the CRTC to give them a chance to
12155 * adjust it according to limitations or connector properties, and also
12156 * a chance to reject the mode entirely.
47f1c6c9 12157 */
da3ced29 12158 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 12159 if (connector_state->crtc != crtc)
7758a113 12160 continue;
7ae89233 12161
0b901879
ACO
12162 encoder = to_intel_encoder(connector_state->best_encoder);
12163
efea6e8e
DV
12164 if (!(encoder->compute_config(encoder, pipe_config))) {
12165 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
12166 goto fail;
12167 }
ee7b9f93 12168 }
47f1c6c9 12169
ff9a6750
DV
12170 /* Set default port clock if not overwritten by the encoder. Needs to be
12171 * done afterwards in case the encoder adjusts the mode. */
12172 if (!pipe_config->port_clock)
2d112de7 12173 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 12174 * pipe_config->pixel_multiplier;
ff9a6750 12175
a43f6e0f 12176 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 12177 if (ret < 0) {
7758a113
DV
12178 DRM_DEBUG_KMS("CRTC fixup failed\n");
12179 goto fail;
ee7b9f93 12180 }
e29c22c0
DV
12181
12182 if (ret == RETRY) {
12183 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12184 ret = -EINVAL;
12185 goto fail;
12186 }
12187
12188 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12189 retry = false;
12190 goto encoder_retry;
12191 }
12192
d328c9d7 12193 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
4e53c2e0 12194 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 12195 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 12196
cdba954e
ACO
12197 /* Check if we need to force a modeset */
12198 if (pipe_config->has_audio !=
85a96e7a 12199 to_intel_crtc_state(crtc->state)->has_audio) {
cdba954e 12200 pipe_config->base.mode_changed = true;
85a96e7a
ML
12201 ret = drm_atomic_add_affected_planes(state, crtc);
12202 }
cdba954e
ACO
12203
12204 /*
12205 * Note we have an issue here with infoframes: current code
12206 * only updates them on the full mode set path per hw
12207 * requirements. So here we should be checking for any
12208 * required changes and forcing a mode set.
12209 */
7758a113 12210fail:
548ee15b 12211 return ret;
ee7b9f93 12212}
47f1c6c9 12213
ea9d758d 12214static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 12215{
ea9d758d 12216 struct drm_encoder *encoder;
f6e5b160 12217 struct drm_device *dev = crtc->dev;
f6e5b160 12218
ea9d758d
DV
12219 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12220 if (encoder->crtc == crtc)
12221 return true;
12222
12223 return false;
12224}
12225
12226static void
0a9ab303 12227intel_modeset_update_state(struct drm_atomic_state *state)
ea9d758d 12228{
0a9ab303 12229 struct drm_device *dev = state->dev;
ea9d758d 12230 struct intel_encoder *intel_encoder;
0a9ab303
ACO
12231 struct drm_crtc *crtc;
12232 struct drm_crtc_state *crtc_state;
ea9d758d
DV
12233 struct drm_connector *connector;
12234
de419ab6 12235 intel_shared_dpll_commit(state);
ba41c0de 12236
b2784e15 12237 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
12238 if (!intel_encoder->base.crtc)
12239 continue;
12240
69024de8
ML
12241 crtc = intel_encoder->base.crtc;
12242 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12243 if (!crtc_state || !needs_modeset(crtc->state))
12244 continue;
ea9d758d 12245
69024de8 12246 intel_encoder->connectors_active = false;
ea9d758d
DV
12247 }
12248
3cb480bc 12249 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
f7217905 12250 intel_modeset_update_staged_output_state(state->dev);
ea9d758d 12251
7668851f 12252 /* Double check state. */
0a9ab303
ACO
12253 for_each_crtc(dev, crtc) {
12254 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
3cb480bc
ML
12255
12256 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
fc467a22
ML
12257
12258 /* Update hwmode for vblank functions */
12259 if (crtc->state->active)
12260 crtc->hwmode = crtc->state->adjusted_mode;
12261 else
12262 crtc->hwmode.crtc_clock = 0;
ea9d758d
DV
12263 }
12264
12265 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12266 if (!connector->encoder || !connector->encoder->crtc)
12267 continue;
12268
69024de8
ML
12269 crtc = connector->encoder->crtc;
12270 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12271 if (!crtc_state || !needs_modeset(crtc->state))
12272 continue;
ea9d758d 12273
53d9f4e9 12274 if (crtc->state->active) {
69024de8
ML
12275 struct drm_property *dpms_property =
12276 dev->mode_config.dpms_property;
68d34720 12277
69024de8
ML
12278 connector->dpms = DRM_MODE_DPMS_ON;
12279 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
ea9d758d 12280
69024de8
ML
12281 intel_encoder = to_intel_encoder(connector->encoder);
12282 intel_encoder->connectors_active = true;
12283 } else
12284 connector->dpms = DRM_MODE_DPMS_OFF;
ea9d758d 12285 }
ea9d758d
DV
12286}
12287
3bd26263 12288static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 12289{
3bd26263 12290 int diff;
f1f644dc
JB
12291
12292 if (clock1 == clock2)
12293 return true;
12294
12295 if (!clock1 || !clock2)
12296 return false;
12297
12298 diff = abs(clock1 - clock2);
12299
12300 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12301 return true;
12302
12303 return false;
12304}
12305
25c5b266
DV
12306#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12307 list_for_each_entry((intel_crtc), \
12308 &(dev)->mode_config.crtc_list, \
12309 base.head) \
0973f18f 12310 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 12311
0e8ffe1b 12312static bool
2fa2fe9a 12313intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
12314 struct intel_crtc_state *current_config,
12315 struct intel_crtc_state *pipe_config)
0e8ffe1b 12316{
66e985c0
DV
12317#define PIPE_CONF_CHECK_X(name) \
12318 if (current_config->name != pipe_config->name) { \
12319 DRM_ERROR("mismatch in " #name " " \
12320 "(expected 0x%08x, found 0x%08x)\n", \
12321 current_config->name, \
12322 pipe_config->name); \
12323 return false; \
12324 }
12325
08a24034
DV
12326#define PIPE_CONF_CHECK_I(name) \
12327 if (current_config->name != pipe_config->name) { \
12328 DRM_ERROR("mismatch in " #name " " \
12329 "(expected %i, found %i)\n", \
12330 current_config->name, \
12331 pipe_config->name); \
12332 return false; \
88adfff1
DV
12333 }
12334
b95af8be
VK
12335/* This is required for BDW+ where there is only one set of registers for
12336 * switching between high and low RR.
12337 * This macro can be used whenever a comparison has to be made between one
12338 * hw state and multiple sw state variables.
12339 */
12340#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12341 if ((current_config->name != pipe_config->name) && \
12342 (current_config->alt_name != pipe_config->name)) { \
12343 DRM_ERROR("mismatch in " #name " " \
12344 "(expected %i or %i, found %i)\n", \
12345 current_config->name, \
12346 current_config->alt_name, \
12347 pipe_config->name); \
12348 return false; \
12349 }
12350
1bd1bd80
DV
12351#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12352 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 12353 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
12354 "(expected %i, found %i)\n", \
12355 current_config->name & (mask), \
12356 pipe_config->name & (mask)); \
12357 return false; \
12358 }
12359
5e550656
VS
12360#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12361 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12362 DRM_ERROR("mismatch in " #name " " \
12363 "(expected %i, found %i)\n", \
12364 current_config->name, \
12365 pipe_config->name); \
12366 return false; \
12367 }
12368
bb760063
DV
12369#define PIPE_CONF_QUIRK(quirk) \
12370 ((current_config->quirks | pipe_config->quirks) & (quirk))
12371
eccb140b
DV
12372 PIPE_CONF_CHECK_I(cpu_transcoder);
12373
08a24034
DV
12374 PIPE_CONF_CHECK_I(has_pch_encoder);
12375 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
12376 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12377 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12378 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12379 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12380 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 12381
eb14cb74 12382 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
12383
12384 if (INTEL_INFO(dev)->gen < 8) {
12385 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12386 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12387 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12388 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12389 PIPE_CONF_CHECK_I(dp_m_n.tu);
12390
12391 if (current_config->has_drrs) {
12392 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12393 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12394 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12395 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12396 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12397 }
12398 } else {
12399 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12400 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12401 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12402 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12403 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12404 }
eb14cb74 12405
2d112de7
ACO
12406 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12407 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12408 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12409 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12410 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12411 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 12412
2d112de7
ACO
12413 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12414 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12415 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12416 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12417 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12418 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 12419
c93f54cf 12420 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 12421 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
12422 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12423 IS_VALLEYVIEW(dev))
12424 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 12425 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 12426
9ed109a7
DV
12427 PIPE_CONF_CHECK_I(has_audio);
12428
2d112de7 12429 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
12430 DRM_MODE_FLAG_INTERLACE);
12431
bb760063 12432 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 12433 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12434 DRM_MODE_FLAG_PHSYNC);
2d112de7 12435 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12436 DRM_MODE_FLAG_NHSYNC);
2d112de7 12437 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 12438 DRM_MODE_FLAG_PVSYNC);
2d112de7 12439 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
12440 DRM_MODE_FLAG_NVSYNC);
12441 }
045ac3b5 12442
37327abd
VS
12443 PIPE_CONF_CHECK_I(pipe_src_w);
12444 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 12445
9953599b
DV
12446 /*
12447 * FIXME: BIOS likes to set up a cloned config with lvds+external
12448 * screen. Since we don't yet re-compute the pipe config when moving
12449 * just the lvds port away to another pipe the sw tracking won't match.
12450 *
12451 * Proper atomic modesets with recomputed global state will fix this.
12452 * Until then just don't check gmch state for inherited modes.
12453 */
12454 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12455 PIPE_CONF_CHECK_I(gmch_pfit.control);
12456 /* pfit ratios are autocomputed by the hw on gen4+ */
12457 if (INTEL_INFO(dev)->gen < 4)
12458 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12459 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12460 }
12461
fd4daa9c
CW
12462 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12463 if (current_config->pch_pfit.enabled) {
12464 PIPE_CONF_CHECK_I(pch_pfit.pos);
12465 PIPE_CONF_CHECK_I(pch_pfit.size);
12466 }
2fa2fe9a 12467
a1b2278e
CK
12468 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12469
e59150dc
JB
12470 /* BDW+ don't expose a synchronous way to read the state */
12471 if (IS_HASWELL(dev))
12472 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 12473
282740f7
VS
12474 PIPE_CONF_CHECK_I(double_wide);
12475
26804afd
DV
12476 PIPE_CONF_CHECK_X(ddi_pll_sel);
12477
c0d43d62 12478 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 12479 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 12480 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
12481 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12482 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 12483 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
12484 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12485 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12486 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 12487
42571aef
VS
12488 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12489 PIPE_CONF_CHECK_I(pipe_bpp);
12490
2d112de7 12491 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 12492 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 12493
66e985c0 12494#undef PIPE_CONF_CHECK_X
08a24034 12495#undef PIPE_CONF_CHECK_I
b95af8be 12496#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 12497#undef PIPE_CONF_CHECK_FLAGS
5e550656 12498#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 12499#undef PIPE_CONF_QUIRK
88adfff1 12500
0e8ffe1b
DV
12501 return true;
12502}
12503
08db6652
DL
12504static void check_wm_state(struct drm_device *dev)
12505{
12506 struct drm_i915_private *dev_priv = dev->dev_private;
12507 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12508 struct intel_crtc *intel_crtc;
12509 int plane;
12510
12511 if (INTEL_INFO(dev)->gen < 9)
12512 return;
12513
12514 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12515 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12516
12517 for_each_intel_crtc(dev, intel_crtc) {
12518 struct skl_ddb_entry *hw_entry, *sw_entry;
12519 const enum pipe pipe = intel_crtc->pipe;
12520
12521 if (!intel_crtc->active)
12522 continue;
12523
12524 /* planes */
dd740780 12525 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
12526 hw_entry = &hw_ddb.plane[pipe][plane];
12527 sw_entry = &sw_ddb->plane[pipe][plane];
12528
12529 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12530 continue;
12531
12532 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12533 "(expected (%u,%u), found (%u,%u))\n",
12534 pipe_name(pipe), plane + 1,
12535 sw_entry->start, sw_entry->end,
12536 hw_entry->start, hw_entry->end);
12537 }
12538
12539 /* cursor */
12540 hw_entry = &hw_ddb.cursor[pipe];
12541 sw_entry = &sw_ddb->cursor[pipe];
12542
12543 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12544 continue;
12545
12546 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12547 "(expected (%u,%u), found (%u,%u))\n",
12548 pipe_name(pipe),
12549 sw_entry->start, sw_entry->end,
12550 hw_entry->start, hw_entry->end);
12551 }
12552}
12553
91d1b4bd
DV
12554static void
12555check_connector_state(struct drm_device *dev)
8af6cf88 12556{
8af6cf88
DV
12557 struct intel_connector *connector;
12558
3a3371ff 12559 for_each_intel_connector(dev, connector) {
8af6cf88
DV
12560 /* This also checks the encoder/connector hw state with the
12561 * ->get_hw_state callbacks. */
12562 intel_connector_check_state(connector);
12563
e2c719b7 12564 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
8af6cf88
DV
12565 "connector's staged encoder doesn't match current encoder\n");
12566 }
91d1b4bd
DV
12567}
12568
12569static void
12570check_encoder_state(struct drm_device *dev)
12571{
12572 struct intel_encoder *encoder;
12573 struct intel_connector *connector;
8af6cf88 12574
b2784e15 12575 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12576 bool enabled = false;
12577 bool active = false;
12578 enum pipe pipe, tracked_pipe;
12579
12580 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12581 encoder->base.base.id,
8e329a03 12582 encoder->base.name);
8af6cf88 12583
e2c719b7 12584 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
8af6cf88 12585 "encoder's stage crtc doesn't match current crtc\n");
e2c719b7 12586 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
12587 "encoder's active_connectors set, but no crtc\n");
12588
3a3371ff 12589 for_each_intel_connector(dev, connector) {
8af6cf88
DV
12590 if (connector->base.encoder != &encoder->base)
12591 continue;
12592 enabled = true;
12593 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12594 active = true;
12595 }
0e32b39c
DA
12596 /*
12597 * for MST connectors if we unplug the connector is gone
12598 * away but the encoder is still connected to a crtc
12599 * until a modeset happens in response to the hotplug.
12600 */
12601 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12602 continue;
12603
e2c719b7 12604 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12605 "encoder's enabled state mismatch "
12606 "(expected %i, found %i)\n",
12607 !!encoder->base.crtc, enabled);
e2c719b7 12608 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
12609 "active encoder with no crtc\n");
12610
e2c719b7 12611 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
12612 "encoder's computed active state doesn't match tracked active state "
12613 "(expected %i, found %i)\n", active, encoder->connectors_active);
12614
12615 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 12616 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
12617 "encoder's hw state doesn't match sw tracking "
12618 "(expected %i, found %i)\n",
12619 encoder->connectors_active, active);
12620
12621 if (!encoder->base.crtc)
12622 continue;
12623
12624 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 12625 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
12626 "active encoder's pipe doesn't match"
12627 "(expected %i, found %i)\n",
12628 tracked_pipe, pipe);
12629
12630 }
91d1b4bd
DV
12631}
12632
12633static void
12634check_crtc_state(struct drm_device *dev)
12635{
fbee40df 12636 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12637 struct intel_crtc *crtc;
12638 struct intel_encoder *encoder;
5cec258b 12639 struct intel_crtc_state pipe_config;
8af6cf88 12640
d3fcc808 12641 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
12642 bool enabled = false;
12643 bool active = false;
12644
045ac3b5
JB
12645 memset(&pipe_config, 0, sizeof(pipe_config));
12646
8af6cf88
DV
12647 DRM_DEBUG_KMS("[CRTC:%d]\n",
12648 crtc->base.base.id);
12649
83d65738 12650 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
8af6cf88
DV
12651 "active crtc, but not enabled in sw tracking\n");
12652
b2784e15 12653 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12654 if (encoder->base.crtc != &crtc->base)
12655 continue;
12656 enabled = true;
12657 if (encoder->connectors_active)
12658 active = true;
12659 }
6c49f241 12660
e2c719b7 12661 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
12662 "crtc's computed active state doesn't match tracked active state "
12663 "(expected %i, found %i)\n", active, crtc->active);
83d65738 12664 I915_STATE_WARN(enabled != crtc->base.state->enable,
8af6cf88 12665 "crtc's computed enabled state doesn't match tracked enabled state "
83d65738
MR
12666 "(expected %i, found %i)\n", enabled,
12667 crtc->base.state->enable);
8af6cf88 12668
0e8ffe1b
DV
12669 active = dev_priv->display.get_pipe_config(crtc,
12670 &pipe_config);
d62cf62a 12671
b6b5d049
VS
12672 /* hw state is inconsistent with the pipe quirk */
12673 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12674 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
12675 active = crtc->active;
12676
b2784e15 12677 for_each_intel_encoder(dev, encoder) {
3eaba51c 12678 enum pipe pipe;
6c49f241
DV
12679 if (encoder->base.crtc != &crtc->base)
12680 continue;
1d37b689 12681 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
12682 encoder->get_config(encoder, &pipe_config);
12683 }
12684
e2c719b7 12685 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
12686 "crtc active state doesn't match with hw state "
12687 "(expected %i, found %i)\n", crtc->active, active);
12688
53d9f4e9
ML
12689 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12690 "transitional active state does not match atomic hw state "
12691 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12692
c0b03411 12693 if (active &&
6e3c9717 12694 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 12695 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
12696 intel_dump_pipe_config(crtc, &pipe_config,
12697 "[hw state]");
6e3c9717 12698 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
12699 "[sw state]");
12700 }
8af6cf88
DV
12701 }
12702}
12703
91d1b4bd
DV
12704static void
12705check_shared_dpll_state(struct drm_device *dev)
12706{
fbee40df 12707 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12708 struct intel_crtc *crtc;
12709 struct intel_dpll_hw_state dpll_hw_state;
12710 int i;
5358901f
DV
12711
12712 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12713 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12714 int enabled_crtcs = 0, active_crtcs = 0;
12715 bool active;
12716
12717 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12718
12719 DRM_DEBUG_KMS("%s\n", pll->name);
12720
12721 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12722
e2c719b7 12723 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12724 "more active pll users than references: %i vs %i\n",
3e369b76 12725 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12726 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12727 "pll in active use but not on in sw tracking\n");
e2c719b7 12728 I915_STATE_WARN(pll->on && !pll->active,
35c95375 12729 "pll in on but not on in use in sw tracking\n");
e2c719b7 12730 I915_STATE_WARN(pll->on != active,
5358901f
DV
12731 "pll on state mismatch (expected %i, found %i)\n",
12732 pll->on, active);
12733
d3fcc808 12734 for_each_intel_crtc(dev, crtc) {
83d65738 12735 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
12736 enabled_crtcs++;
12737 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12738 active_crtcs++;
12739 }
e2c719b7 12740 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
12741 "pll active crtcs mismatch (expected %i, found %i)\n",
12742 pll->active, active_crtcs);
e2c719b7 12743 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 12744 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 12745 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 12746
e2c719b7 12747 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12748 sizeof(dpll_hw_state)),
12749 "pll hw state mismatch\n");
5358901f 12750 }
8af6cf88
DV
12751}
12752
91d1b4bd
DV
12753void
12754intel_modeset_check_state(struct drm_device *dev)
12755{
08db6652 12756 check_wm_state(dev);
91d1b4bd
DV
12757 check_connector_state(dev);
12758 check_encoder_state(dev);
12759 check_crtc_state(dev);
12760 check_shared_dpll_state(dev);
12761}
12762
5cec258b 12763void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
12764 int dotclock)
12765{
12766 /*
12767 * FDI already provided one idea for the dotclock.
12768 * Yell if the encoder disagrees.
12769 */
2d112de7 12770 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 12771 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 12772 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
12773}
12774
80715b2f
VS
12775static void update_scanline_offset(struct intel_crtc *crtc)
12776{
12777 struct drm_device *dev = crtc->base.dev;
12778
12779 /*
12780 * The scanline counter increments at the leading edge of hsync.
12781 *
12782 * On most platforms it starts counting from vtotal-1 on the
12783 * first active line. That means the scanline counter value is
12784 * always one less than what we would expect. Ie. just after
12785 * start of vblank, which also occurs at start of hsync (on the
12786 * last active line), the scanline counter will read vblank_start-1.
12787 *
12788 * On gen2 the scanline counter starts counting from 1 instead
12789 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12790 * to keep the value positive), instead of adding one.
12791 *
12792 * On HSW+ the behaviour of the scanline counter depends on the output
12793 * type. For DP ports it behaves like most other platforms, but on HDMI
12794 * there's an extra 1 line difference. So we need to add two instead of
12795 * one to the value.
12796 */
12797 if (IS_GEN2(dev)) {
6e3c9717 12798 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12799 int vtotal;
12800
12801 vtotal = mode->crtc_vtotal;
12802 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12803 vtotal /= 2;
12804
12805 crtc->scanline_offset = vtotal - 1;
12806 } else if (HAS_DDI(dev) &&
409ee761 12807 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12808 crtc->scanline_offset = 2;
12809 } else
12810 crtc->scanline_offset = 1;
12811}
12812
ad421372 12813static void intel_modeset_clear_plls(struct drm_atomic_state *state)
ed6739ef 12814{
225da59b 12815 struct drm_device *dev = state->dev;
ed6739ef 12816 struct drm_i915_private *dev_priv = to_i915(dev);
ad421372 12817 struct intel_shared_dpll_config *shared_dpll = NULL;
ed6739ef 12818 struct intel_crtc *intel_crtc;
0a9ab303
ACO
12819 struct intel_crtc_state *intel_crtc_state;
12820 struct drm_crtc *crtc;
12821 struct drm_crtc_state *crtc_state;
0a9ab303 12822 int i;
ed6739ef
ACO
12823
12824 if (!dev_priv->display.crtc_compute_clock)
ad421372 12825 return;
ed6739ef 12826
0a9ab303 12827 for_each_crtc_in_state(state, crtc, crtc_state, i) {
ad421372
ML
12828 int dpll;
12829
0a9ab303 12830 intel_crtc = to_intel_crtc(crtc);
4978cc93 12831 intel_crtc_state = to_intel_crtc_state(crtc_state);
ad421372 12832 dpll = intel_crtc_state->shared_dpll;
0a9ab303 12833
ad421372 12834 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
225da59b
ACO
12835 continue;
12836
ad421372 12837 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
0a9ab303 12838
ad421372
ML
12839 if (!shared_dpll)
12840 shared_dpll = intel_atomic_get_shared_dpll_state(state);
ed6739ef 12841
ad421372
ML
12842 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
12843 }
ed6739ef
ACO
12844}
12845
99d736a2
ML
12846/*
12847 * This implements the workaround described in the "notes" section of the mode
12848 * set sequence documentation. When going from no pipes or single pipe to
12849 * multiple pipes, and planes are enabled after the pipe, we need to wait at
12850 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
12851 */
12852static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
12853{
12854 struct drm_crtc_state *crtc_state;
12855 struct intel_crtc *intel_crtc;
12856 struct drm_crtc *crtc;
12857 struct intel_crtc_state *first_crtc_state = NULL;
12858 struct intel_crtc_state *other_crtc_state = NULL;
12859 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
12860 int i;
12861
12862 /* look at all crtc's that are going to be enabled in during modeset */
12863 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12864 intel_crtc = to_intel_crtc(crtc);
12865
12866 if (!crtc_state->active || !needs_modeset(crtc_state))
12867 continue;
12868
12869 if (first_crtc_state) {
12870 other_crtc_state = to_intel_crtc_state(crtc_state);
12871 break;
12872 } else {
12873 first_crtc_state = to_intel_crtc_state(crtc_state);
12874 first_pipe = intel_crtc->pipe;
12875 }
12876 }
12877
12878 /* No workaround needed? */
12879 if (!first_crtc_state)
12880 return 0;
12881
12882 /* w/a possibly needed, check how many crtc's are already enabled. */
12883 for_each_intel_crtc(state->dev, intel_crtc) {
12884 struct intel_crtc_state *pipe_config;
12885
12886 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12887 if (IS_ERR(pipe_config))
12888 return PTR_ERR(pipe_config);
12889
12890 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
12891
12892 if (!pipe_config->base.active ||
12893 needs_modeset(&pipe_config->base))
12894 continue;
12895
12896 /* 2 or more enabled crtcs means no need for w/a */
12897 if (enabled_pipe != INVALID_PIPE)
12898 return 0;
12899
12900 enabled_pipe = intel_crtc->pipe;
12901 }
12902
12903 if (enabled_pipe != INVALID_PIPE)
12904 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
12905 else if (other_crtc_state)
12906 other_crtc_state->hsw_workaround_pipe = first_pipe;
12907
12908 return 0;
12909}
12910
27c329ed
ML
12911static int intel_modeset_all_pipes(struct drm_atomic_state *state)
12912{
12913 struct drm_crtc *crtc;
12914 struct drm_crtc_state *crtc_state;
12915 int ret = 0;
12916
12917 /* add all active pipes to the state */
12918 for_each_crtc(state->dev, crtc) {
12919 crtc_state = drm_atomic_get_crtc_state(state, crtc);
12920 if (IS_ERR(crtc_state))
12921 return PTR_ERR(crtc_state);
12922
12923 if (!crtc_state->active || needs_modeset(crtc_state))
12924 continue;
12925
12926 crtc_state->mode_changed = true;
12927
12928 ret = drm_atomic_add_affected_connectors(state, crtc);
12929 if (ret)
12930 break;
12931
12932 ret = drm_atomic_add_affected_planes(state, crtc);
12933 if (ret)
12934 break;
12935 }
12936
12937 return ret;
12938}
12939
12940
054518dd 12941/* Code that should eventually be part of atomic_check() */
c347a676 12942static int intel_modeset_checks(struct drm_atomic_state *state)
054518dd
ACO
12943{
12944 struct drm_device *dev = state->dev;
27c329ed 12945 struct drm_i915_private *dev_priv = dev->dev_private;
054518dd
ACO
12946 int ret;
12947
b359283a
ML
12948 if (!check_digital_port_conflicts(state)) {
12949 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
12950 return -EINVAL;
12951 }
12952
054518dd
ACO
12953 /*
12954 * See if the config requires any additional preparation, e.g.
12955 * to adjust global state with pipes off. We need to do this
12956 * here so we can get the modeset_pipe updated config for the new
12957 * mode set on this crtc. For other crtcs we need to use the
12958 * adjusted_mode bits in the crtc directly.
12959 */
27c329ed
ML
12960 if (dev_priv->display.modeset_calc_cdclk) {
12961 unsigned int cdclk;
b432e5cf 12962
27c329ed
ML
12963 ret = dev_priv->display.modeset_calc_cdclk(state);
12964
12965 cdclk = to_intel_atomic_state(state)->cdclk;
12966 if (!ret && cdclk != dev_priv->cdclk_freq)
12967 ret = intel_modeset_all_pipes(state);
12968
12969 if (ret < 0)
054518dd 12970 return ret;
27c329ed
ML
12971 } else
12972 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
054518dd 12973
ad421372 12974 intel_modeset_clear_plls(state);
054518dd 12975
99d736a2 12976 if (IS_HASWELL(dev))
ad421372 12977 return haswell_mode_set_planes_workaround(state);
99d736a2 12978
ad421372 12979 return 0;
c347a676
ACO
12980}
12981
12982static int
12983intel_modeset_compute_config(struct drm_atomic_state *state)
12984{
12985 struct drm_crtc *crtc;
12986 struct drm_crtc_state *crtc_state;
12987 int ret, i;
61333b60 12988 bool any_ms = false;
c347a676
ACO
12989
12990 ret = drm_atomic_helper_check_modeset(state->dev, state);
054518dd
ACO
12991 if (ret)
12992 return ret;
12993
c347a676 12994 for_each_crtc_in_state(state, crtc, crtc_state, i) {
61333b60
ML
12995 if (!crtc_state->enable) {
12996 if (needs_modeset(crtc_state))
12997 any_ms = true;
c347a676 12998 continue;
61333b60 12999 }
c347a676 13000
d032ffa0
ML
13001 if (to_intel_crtc_state(crtc_state)->quirks &
13002 PIPE_CONFIG_QUIRK_INITIAL_PLANES) {
13003 ret = drm_atomic_add_affected_planes(state, crtc);
13004 if (ret)
13005 return ret;
13006
13007 /*
13008 * We ought to handle i915.fastboot here.
13009 * If no modeset is required and the primary plane has
13010 * a fb, update the members of crtc_state as needed,
13011 * and run the necessary updates during vblank evasion.
13012 */
13013 }
13014
b359283a
ML
13015 if (!needs_modeset(crtc_state)) {
13016 ret = drm_atomic_add_affected_connectors(state, crtc);
13017 if (ret)
13018 return ret;
13019 }
13020
13021 ret = intel_modeset_pipe_config(crtc,
13022 to_intel_crtc_state(crtc_state));
c347a676
ACO
13023 if (ret)
13024 return ret;
13025
61333b60
ML
13026 if (needs_modeset(crtc_state))
13027 any_ms = true;
13028
c347a676
ACO
13029 intel_dump_pipe_config(to_intel_crtc(crtc),
13030 to_intel_crtc_state(crtc_state),
13031 "[modeset]");
13032 }
13033
61333b60
ML
13034 if (any_ms) {
13035 ret = intel_modeset_checks(state);
13036
13037 if (ret)
13038 return ret;
27c329ed
ML
13039 } else
13040 to_intel_atomic_state(state)->cdclk =
13041 to_i915(state->dev)->cdclk_freq;
c347a676
ACO
13042
13043 return drm_atomic_helper_check_planes(state->dev, state);
054518dd
ACO
13044}
13045
c72d969b 13046static int __intel_set_mode(struct drm_atomic_state *state)
a6778b3c 13047{
c72d969b 13048 struct drm_device *dev = state->dev;
fbee40df 13049 struct drm_i915_private *dev_priv = dev->dev_private;
0a9ab303
ACO
13050 struct drm_crtc *crtc;
13051 struct drm_crtc_state *crtc_state;
c0c36b94 13052 int ret = 0;
0a9ab303 13053 int i;
61333b60 13054 bool any_ms = false;
a6778b3c 13055
d4afb8cc
ACO
13056 ret = drm_atomic_helper_prepare_planes(dev, state);
13057 if (ret)
13058 return ret;
13059
1c5e19f8
ML
13060 drm_atomic_helper_swap_state(dev, state);
13061
0a9ab303 13062 for_each_crtc_in_state(state, crtc, crtc_state, i) {
a539205a
ML
13063 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13064
61333b60
ML
13065 if (!needs_modeset(crtc->state))
13066 continue;
13067
13068 any_ms = true;
a539205a 13069 intel_pre_plane_update(intel_crtc);
460da916 13070
a539205a
ML
13071 if (crtc_state->active) {
13072 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13073 dev_priv->display.crtc_disable(crtc);
eddfcbcd
ML
13074 intel_crtc->active = false;
13075 intel_disable_shared_dpll(intel_crtc);
a539205a 13076 }
b8cecdf5 13077 }
7758a113 13078
ea9d758d
DV
13079 /* Only after disabling all output pipelines that will be changed can we
13080 * update the the output configuration. */
0a9ab303 13081 intel_modeset_update_state(state);
f6e5b160 13082
a821fc46
ACO
13083 /* The state has been swaped above, so state actually contains the
13084 * old state now. */
61333b60
ML
13085 if (any_ms)
13086 modeset_update_crtc_power_domains(state);
47fab737 13087
a6778b3c 13088 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
0a9ab303 13089 for_each_crtc_in_state(state, crtc, crtc_state, i) {
a539205a
ML
13090 if (needs_modeset(crtc->state) && crtc->state->active) {
13091 update_scanline_offset(to_intel_crtc(crtc));
13092 dev_priv->display.crtc_enable(crtc);
13093 }
80715b2f 13094
a539205a 13095 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
80715b2f 13096 }
a6778b3c 13097
a6778b3c 13098 /* FIXME: add subpixel order */
83a57153 13099
d4afb8cc
ACO
13100 drm_atomic_helper_cleanup_planes(dev, state);
13101
2bfb4627
ACO
13102 drm_atomic_state_free(state);
13103
9eb45f22 13104 return 0;
f6e5b160
CW
13105}
13106
568c634a 13107static int intel_set_mode_checked(struct drm_atomic_state *state)
f30da187 13108{
568c634a 13109 struct drm_device *dev = state->dev;
f30da187
DV
13110 int ret;
13111
568c634a 13112 ret = __intel_set_mode(state);
f30da187 13113 if (ret == 0)
568c634a 13114 intel_modeset_check_state(dev);
f30da187
DV
13115
13116 return ret;
13117}
13118
568c634a 13119static int intel_set_mode(struct drm_atomic_state *state)
7f27126e 13120{
568c634a 13121 int ret;
83a57153 13122
568c634a 13123 ret = intel_modeset_compute_config(state);
83a57153 13124 if (ret)
568c634a 13125 return ret;
7f27126e 13126
568c634a 13127 return intel_set_mode_checked(state);
7f27126e
JB
13128}
13129
c0c36b94
CW
13130void intel_crtc_restore_mode(struct drm_crtc *crtc)
13131{
83a57153
ACO
13132 struct drm_device *dev = crtc->dev;
13133 struct drm_atomic_state *state;
13134 struct intel_encoder *encoder;
13135 struct intel_connector *connector;
13136 struct drm_connector_state *connector_state;
4be07317 13137 struct intel_crtc_state *crtc_state;
2bfb4627 13138 int ret;
83a57153
ACO
13139
13140 state = drm_atomic_state_alloc(dev);
13141 if (!state) {
13142 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13143 crtc->base.id);
13144 return;
13145 }
13146
13147 state->acquire_ctx = dev->mode_config.acquire_ctx;
13148
13149 /* The force restore path in the HW readout code relies on the staged
13150 * config still keeping the user requested config while the actual
13151 * state has been overwritten by the configuration read from HW. We
13152 * need to copy the staged config to the atomic state, otherwise the
13153 * mode set will just reapply the state the HW is already in. */
13154 for_each_intel_encoder(dev, encoder) {
13155 if (&encoder->new_crtc->base != crtc)
13156 continue;
13157
13158 for_each_intel_connector(dev, connector) {
13159 if (connector->new_encoder != encoder)
13160 continue;
13161
13162 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13163 if (IS_ERR(connector_state)) {
13164 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13165 connector->base.base.id,
13166 connector->base.name,
13167 PTR_ERR(connector_state));
13168 continue;
13169 }
13170
13171 connector_state->crtc = crtc;
13172 connector_state->best_encoder = &encoder->base;
13173 }
13174 }
13175
4ed9fb37
ACO
13176 crtc_state = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
13177 if (IS_ERR(crtc_state)) {
13178 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13179 crtc->base.id, PTR_ERR(crtc_state));
13180 drm_atomic_state_free(state);
13181 return;
13182 }
4be07317 13183
4ed9fb37
ACO
13184 crtc_state->base.active = crtc_state->base.enable =
13185 to_intel_crtc(crtc)->new_enabled;
8c7b5ccb 13186
4ed9fb37 13187 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
4be07317 13188
d3a40d1b
ACO
13189 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13190 crtc->primary->fb, crtc->x, crtc->y);
13191
568c634a 13192 ret = intel_set_mode(state);
2bfb4627
ACO
13193 if (ret)
13194 drm_atomic_state_free(state);
c0c36b94
CW
13195}
13196
25c5b266
DV
13197#undef for_each_intel_crtc_masked
13198
b7885264
ACO
13199static bool intel_connector_in_mode_set(struct intel_connector *connector,
13200 struct drm_mode_set *set)
13201{
13202 int ro;
13203
13204 for (ro = 0; ro < set->num_connectors; ro++)
13205 if (set->connectors[ro] == &connector->base)
13206 return true;
13207
13208 return false;
13209}
13210
2e431051 13211static int
9a935856
DV
13212intel_modeset_stage_output_state(struct drm_device *dev,
13213 struct drm_mode_set *set,
944b0c76 13214 struct drm_atomic_state *state)
50f56119 13215{
9a935856 13216 struct intel_connector *connector;
d5432a9d 13217 struct drm_connector *drm_connector;
944b0c76 13218 struct drm_connector_state *connector_state;
d5432a9d
ACO
13219 struct drm_crtc *crtc;
13220 struct drm_crtc_state *crtc_state;
13221 int i, ret;
50f56119 13222
9abdda74 13223 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
13224 * of connectors. For paranoia, double-check this. */
13225 WARN_ON(!set->fb && (set->num_connectors != 0));
13226 WARN_ON(set->fb && (set->num_connectors == 0));
13227
3a3371ff 13228 for_each_intel_connector(dev, connector) {
b7885264
ACO
13229 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13230
d5432a9d
ACO
13231 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13232 continue;
13233
13234 connector_state =
13235 drm_atomic_get_connector_state(state, &connector->base);
13236 if (IS_ERR(connector_state))
13237 return PTR_ERR(connector_state);
13238
b7885264
ACO
13239 if (in_mode_set) {
13240 int pipe = to_intel_crtc(set->crtc)->pipe;
d5432a9d
ACO
13241 connector_state->best_encoder =
13242 &intel_find_encoder(connector, pipe)->base;
50f56119
DV
13243 }
13244
d5432a9d 13245 if (connector->base.state->crtc != set->crtc)
b7885264
ACO
13246 continue;
13247
9a935856
DV
13248 /* If we disable the crtc, disable all its connectors. Also, if
13249 * the connector is on the changing crtc but not on the new
13250 * connector list, disable it. */
b7885264 13251 if (!set->fb || !in_mode_set) {
d5432a9d 13252 connector_state->best_encoder = NULL;
9a935856
DV
13253
13254 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13255 connector->base.base.id,
c23cc417 13256 connector->base.name);
9a935856 13257 }
50f56119 13258 }
9a935856 13259 /* connector->new_encoder is now updated for all connectors. */
50f56119 13260
d5432a9d
ACO
13261 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13262 connector = to_intel_connector(drm_connector);
13263
13264 if (!connector_state->best_encoder) {
13265 ret = drm_atomic_set_crtc_for_connector(connector_state,
13266 NULL);
13267 if (ret)
13268 return ret;
7668851f 13269
50f56119 13270 continue;
d5432a9d 13271 }
50f56119 13272
d5432a9d
ACO
13273 if (intel_connector_in_mode_set(connector, set)) {
13274 struct drm_crtc *crtc = connector->base.state->crtc;
13275
13276 /* If this connector was in a previous crtc, add it
13277 * to the state. We might need to disable it. */
13278 if (crtc) {
13279 crtc_state =
13280 drm_atomic_get_crtc_state(state, crtc);
13281 if (IS_ERR(crtc_state))
13282 return PTR_ERR(crtc_state);
13283 }
13284
13285 ret = drm_atomic_set_crtc_for_connector(connector_state,
13286 set->crtc);
13287 if (ret)
13288 return ret;
13289 }
50f56119
DV
13290
13291 /* Make sure the new CRTC will work with the encoder */
d5432a9d
ACO
13292 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13293 connector_state->crtc)) {
5e2b584e 13294 return -EINVAL;
50f56119 13295 }
944b0c76 13296
9a935856
DV
13297 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13298 connector->base.base.id,
c23cc417 13299 connector->base.name,
d5432a9d 13300 connector_state->crtc->base.id);
944b0c76 13301
d5432a9d
ACO
13302 if (connector_state->best_encoder != &connector->encoder->base)
13303 connector->encoder =
13304 to_intel_encoder(connector_state->best_encoder);
0e32b39c 13305 }
7668851f 13306
d5432a9d 13307 for_each_crtc_in_state(state, crtc, crtc_state, i) {
49d6fa21
ML
13308 bool has_connectors;
13309
d5432a9d
ACO
13310 ret = drm_atomic_add_affected_connectors(state, crtc);
13311 if (ret)
13312 return ret;
4be07317 13313
49d6fa21
ML
13314 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13315 if (has_connectors != crtc_state->enable)
13316 crtc_state->enable =
13317 crtc_state->active = has_connectors;
7668851f
VS
13318 }
13319
8c7b5ccb
ACO
13320 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13321 set->fb, set->x, set->y);
13322 if (ret)
13323 return ret;
13324
13325 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13326 if (IS_ERR(crtc_state))
13327 return PTR_ERR(crtc_state);
13328
ce52299c
MR
13329 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
13330 if (ret)
13331 return ret;
8c7b5ccb
ACO
13332
13333 if (set->num_connectors)
13334 crtc_state->active = true;
13335
2e431051
DV
13336 return 0;
13337}
13338
13339static int intel_crtc_set_config(struct drm_mode_set *set)
13340{
13341 struct drm_device *dev;
83a57153 13342 struct drm_atomic_state *state = NULL;
2e431051 13343 int ret;
2e431051 13344
8d3e375e
DV
13345 BUG_ON(!set);
13346 BUG_ON(!set->crtc);
13347 BUG_ON(!set->crtc->helper_private);
2e431051 13348
7e53f3a4
DV
13349 /* Enforce sane interface api - has been abused by the fb helper. */
13350 BUG_ON(!set->mode && set->fb);
13351 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 13352
2e431051
DV
13353 if (set->fb) {
13354 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13355 set->crtc->base.id, set->fb->base.id,
13356 (int)set->num_connectors, set->x, set->y);
13357 } else {
13358 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
13359 }
13360
13361 dev = set->crtc->dev;
13362
83a57153 13363 state = drm_atomic_state_alloc(dev);
7cbf41d6
ACO
13364 if (!state)
13365 return -ENOMEM;
83a57153
ACO
13366
13367 state->acquire_ctx = dev->mode_config.acquire_ctx;
13368
462a425a 13369 ret = intel_modeset_stage_output_state(dev, set, state);
2e431051 13370 if (ret)
7cbf41d6 13371 goto out;
2e431051 13372
568c634a
ACO
13373 ret = intel_modeset_compute_config(state);
13374 if (ret)
7cbf41d6 13375 goto out;
50f52756 13376
1f9954d0
JB
13377 intel_update_pipe_size(to_intel_crtc(set->crtc));
13378
568c634a 13379 ret = intel_set_mode_checked(state);
2d05eae1 13380 if (ret) {
bf67dfeb
DV
13381 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13382 set->crtc->base.id, ret);
2d05eae1 13383 }
50f56119 13384
7cbf41d6 13385out:
2bfb4627
ACO
13386 if (ret)
13387 drm_atomic_state_free(state);
50f56119
DV
13388 return ret;
13389}
f6e5b160
CW
13390
13391static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13392 .gamma_set = intel_crtc_gamma_set,
50f56119 13393 .set_config = intel_crtc_set_config,
f6e5b160
CW
13394 .destroy = intel_crtc_destroy,
13395 .page_flip = intel_crtc_page_flip,
1356837e
MR
13396 .atomic_duplicate_state = intel_crtc_duplicate_state,
13397 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13398};
13399
5358901f
DV
13400static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13401 struct intel_shared_dpll *pll,
13402 struct intel_dpll_hw_state *hw_state)
ee7b9f93 13403{
5358901f 13404 uint32_t val;
ee7b9f93 13405
f458ebbc 13406 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
13407 return false;
13408
5358901f 13409 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
13410 hw_state->dpll = val;
13411 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13412 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
13413
13414 return val & DPLL_VCO_ENABLE;
13415}
13416
15bdd4cf
DV
13417static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13418 struct intel_shared_dpll *pll)
13419{
3e369b76
ACO
13420 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13421 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
13422}
13423
e7b903d2
DV
13424static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13425 struct intel_shared_dpll *pll)
13426{
e7b903d2 13427 /* PCH refclock must be enabled first */
89eff4be 13428 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 13429
3e369b76 13430 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
13431
13432 /* Wait for the clocks to stabilize. */
13433 POSTING_READ(PCH_DPLL(pll->id));
13434 udelay(150);
13435
13436 /* The pixel multiplier can only be updated once the
13437 * DPLL is enabled and the clocks are stable.
13438 *
13439 * So write it again.
13440 */
3e369b76 13441 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 13442 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13443 udelay(200);
13444}
13445
13446static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13447 struct intel_shared_dpll *pll)
13448{
13449 struct drm_device *dev = dev_priv->dev;
13450 struct intel_crtc *crtc;
e7b903d2
DV
13451
13452 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 13453 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
13454 if (intel_crtc_to_shared_dpll(crtc) == pll)
13455 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
13456 }
13457
15bdd4cf
DV
13458 I915_WRITE(PCH_DPLL(pll->id), 0);
13459 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13460 udelay(200);
13461}
13462
46edb027
DV
13463static char *ibx_pch_dpll_names[] = {
13464 "PCH DPLL A",
13465 "PCH DPLL B",
13466};
13467
7c74ade1 13468static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 13469{
e7b903d2 13470 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
13471 int i;
13472
7c74ade1 13473 dev_priv->num_shared_dpll = 2;
ee7b9f93 13474
e72f9fbf 13475 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
13476 dev_priv->shared_dplls[i].id = i;
13477 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 13478 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
13479 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13480 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
13481 dev_priv->shared_dplls[i].get_hw_state =
13482 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
13483 }
13484}
13485
7c74ade1
DV
13486static void intel_shared_dpll_init(struct drm_device *dev)
13487{
e7b903d2 13488 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 13489
b6283055
VS
13490 intel_update_cdclk(dev);
13491
9cd86933
DV
13492 if (HAS_DDI(dev))
13493 intel_ddi_pll_init(dev);
13494 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
13495 ibx_pch_dpll_init(dev);
13496 else
13497 dev_priv->num_shared_dpll = 0;
13498
13499 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
13500}
13501
6beb8c23
MR
13502/**
13503 * intel_prepare_plane_fb - Prepare fb for usage on plane
13504 * @plane: drm plane to prepare for
13505 * @fb: framebuffer to prepare for presentation
13506 *
13507 * Prepares a framebuffer for usage on a display plane. Generally this
13508 * involves pinning the underlying object and updating the frontbuffer tracking
13509 * bits. Some older platforms need special physical address handling for
13510 * cursor planes.
13511 *
13512 * Returns 0 on success, negative error code on failure.
13513 */
13514int
13515intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee
TU
13516 struct drm_framebuffer *fb,
13517 const struct drm_plane_state *new_state)
465c120c
MR
13518{
13519 struct drm_device *dev = plane->dev;
6beb8c23
MR
13520 struct intel_plane *intel_plane = to_intel_plane(plane);
13521 enum pipe pipe = intel_plane->pipe;
13522 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13523 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13524 unsigned frontbuffer_bits = 0;
13525 int ret = 0;
465c120c 13526
ea2c67bb 13527 if (!obj)
465c120c
MR
13528 return 0;
13529
6beb8c23
MR
13530 switch (plane->type) {
13531 case DRM_PLANE_TYPE_PRIMARY:
13532 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13533 break;
13534 case DRM_PLANE_TYPE_CURSOR:
13535 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13536 break;
13537 case DRM_PLANE_TYPE_OVERLAY:
13538 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13539 break;
13540 }
465c120c 13541
6beb8c23 13542 mutex_lock(&dev->struct_mutex);
465c120c 13543
6beb8c23
MR
13544 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13545 INTEL_INFO(dev)->cursor_needs_physical) {
13546 int align = IS_I830(dev) ? 16 * 1024 : 256;
13547 ret = i915_gem_object_attach_phys(obj, align);
13548 if (ret)
13549 DRM_DEBUG_KMS("failed to attach phys object\n");
13550 } else {
91af127f 13551 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
6beb8c23 13552 }
465c120c 13553
6beb8c23
MR
13554 if (ret == 0)
13555 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
fdd508a6 13556
4c34574f 13557 mutex_unlock(&dev->struct_mutex);
465c120c 13558
6beb8c23
MR
13559 return ret;
13560}
13561
38f3ce3a
MR
13562/**
13563 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13564 * @plane: drm plane to clean up for
13565 * @fb: old framebuffer that was on plane
13566 *
13567 * Cleans up a framebuffer that has just been removed from a plane.
13568 */
13569void
13570intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee
TU
13571 struct drm_framebuffer *fb,
13572 const struct drm_plane_state *old_state)
38f3ce3a
MR
13573{
13574 struct drm_device *dev = plane->dev;
13575 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13576
13577 if (WARN_ON(!obj))
13578 return;
13579
13580 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13581 !INTEL_INFO(dev)->cursor_needs_physical) {
13582 mutex_lock(&dev->struct_mutex);
82bc3b2d 13583 intel_unpin_fb_obj(fb, old_state);
38f3ce3a
MR
13584 mutex_unlock(&dev->struct_mutex);
13585 }
465c120c
MR
13586}
13587
6156a456
CK
13588int
13589skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13590{
13591 int max_scale;
13592 struct drm_device *dev;
13593 struct drm_i915_private *dev_priv;
13594 int crtc_clock, cdclk;
13595
13596 if (!intel_crtc || !crtc_state)
13597 return DRM_PLANE_HELPER_NO_SCALING;
13598
13599 dev = intel_crtc->base.dev;
13600 dev_priv = dev->dev_private;
13601 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
27c329ed 13602 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
6156a456
CK
13603
13604 if (!crtc_clock || !cdclk)
13605 return DRM_PLANE_HELPER_NO_SCALING;
13606
13607 /*
13608 * skl max scale is lower of:
13609 * close to 3 but not 3, -1 is for that purpose
13610 * or
13611 * cdclk/crtc_clock
13612 */
13613 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13614
13615 return max_scale;
13616}
13617
465c120c 13618static int
3c692a41 13619intel_check_primary_plane(struct drm_plane *plane,
061e4b8d 13620 struct intel_crtc_state *crtc_state,
3c692a41
GP
13621 struct intel_plane_state *state)
13622{
2b875c22
MR
13623 struct drm_crtc *crtc = state->base.crtc;
13624 struct drm_framebuffer *fb = state->base.fb;
6156a456 13625 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
061e4b8d
ML
13626 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13627 bool can_position = false;
465c120c 13628
061e4b8d
ML
13629 /* use scaler when colorkey is not required */
13630 if (INTEL_INFO(plane->dev)->gen >= 9 &&
818ed961 13631 state->ckey.flags == I915_SET_COLORKEY_NONE) {
061e4b8d
ML
13632 min_scale = 1;
13633 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
d8106366 13634 can_position = true;
6156a456 13635 }
d8106366 13636
061e4b8d
ML
13637 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13638 &state->dst, &state->clip,
da20eabd
ML
13639 min_scale, max_scale,
13640 can_position, true,
13641 &state->visible);
14af293f
GP
13642}
13643
13644static void
13645intel_commit_primary_plane(struct drm_plane *plane,
13646 struct intel_plane_state *state)
13647{
2b875c22
MR
13648 struct drm_crtc *crtc = state->base.crtc;
13649 struct drm_framebuffer *fb = state->base.fb;
13650 struct drm_device *dev = plane->dev;
14af293f 13651 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 13652 struct intel_crtc *intel_crtc;
14af293f
GP
13653 struct drm_rect *src = &state->src;
13654
ea2c67bb
MR
13655 crtc = crtc ? crtc : plane->crtc;
13656 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
13657
13658 plane->fb = fb;
9dc806fc
MR
13659 crtc->x = src->x1 >> 16;
13660 crtc->y = src->y1 >> 16;
ccc759dc 13661
a539205a 13662 if (!crtc->state->active)
302d19ac 13663 return;
465c120c 13664
302d19ac
ML
13665 if (state->visible)
13666 /* FIXME: kill this fastboot hack */
13667 intel_update_pipe_size(intel_crtc);
13668
13669 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
465c120c
MR
13670}
13671
a8ad0d8e
ML
13672static void
13673intel_disable_primary_plane(struct drm_plane *plane,
7fabf5ef 13674 struct drm_crtc *crtc)
a8ad0d8e
ML
13675{
13676 struct drm_device *dev = plane->dev;
13677 struct drm_i915_private *dev_priv = dev->dev_private;
13678
a8ad0d8e
ML
13679 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13680}
13681
32b7eeec 13682static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 13683{
32b7eeec 13684 struct drm_device *dev = crtc->dev;
140fd38d 13685 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 13686 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3c692a41 13687
a539205a
ML
13688 if (!needs_modeset(crtc->state))
13689 intel_pre_plane_update(intel_crtc);
3c692a41 13690
32b7eeec
MR
13691 if (intel_crtc->atomic.update_wm)
13692 intel_update_watermarks(crtc);
3c692a41 13693
32b7eeec 13694 intel_runtime_pm_get(dev_priv);
3c692a41 13695
c34c9ee4 13696 /* Perform vblank evasion around commit operation */
a539205a 13697 if (crtc->state->active)
c34c9ee4
MR
13698 intel_crtc->atomic.evade =
13699 intel_pipe_update_start(intel_crtc,
13700 &intel_crtc->atomic.start_vbl_count);
0583236e
ML
13701
13702 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13703 skl_detach_scalers(intel_crtc);
32b7eeec
MR
13704}
13705
13706static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13707{
13708 struct drm_device *dev = crtc->dev;
13709 struct drm_i915_private *dev_priv = dev->dev_private;
13710 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
32b7eeec 13711
c34c9ee4
MR
13712 if (intel_crtc->atomic.evade)
13713 intel_pipe_update_end(intel_crtc,
13714 intel_crtc->atomic.start_vbl_count);
3c692a41 13715
140fd38d 13716 intel_runtime_pm_put(dev_priv);
3c692a41 13717
ac21b225 13718 intel_post_plane_update(intel_crtc);
3c692a41
GP
13719}
13720
cf4c7c12 13721/**
4a3b8769
MR
13722 * intel_plane_destroy - destroy a plane
13723 * @plane: plane to destroy
cf4c7c12 13724 *
4a3b8769
MR
13725 * Common destruction function for all types of planes (primary, cursor,
13726 * sprite).
cf4c7c12 13727 */
4a3b8769 13728void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13729{
13730 struct intel_plane *intel_plane = to_intel_plane(plane);
13731 drm_plane_cleanup(plane);
13732 kfree(intel_plane);
13733}
13734
65a3fea0 13735const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13736 .update_plane = drm_atomic_helper_update_plane,
13737 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13738 .destroy = intel_plane_destroy,
c196e1d6 13739 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13740 .atomic_get_property = intel_plane_atomic_get_property,
13741 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13742 .atomic_duplicate_state = intel_plane_duplicate_state,
13743 .atomic_destroy_state = intel_plane_destroy_state,
13744
465c120c
MR
13745};
13746
13747static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13748 int pipe)
13749{
13750 struct intel_plane *primary;
8e7d688b 13751 struct intel_plane_state *state;
465c120c
MR
13752 const uint32_t *intel_primary_formats;
13753 int num_formats;
13754
13755 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13756 if (primary == NULL)
13757 return NULL;
13758
8e7d688b
MR
13759 state = intel_create_plane_state(&primary->base);
13760 if (!state) {
ea2c67bb
MR
13761 kfree(primary);
13762 return NULL;
13763 }
8e7d688b 13764 primary->base.state = &state->base;
ea2c67bb 13765
465c120c
MR
13766 primary->can_scale = false;
13767 primary->max_downscale = 1;
6156a456
CK
13768 if (INTEL_INFO(dev)->gen >= 9) {
13769 primary->can_scale = true;
af99ceda 13770 state->scaler_id = -1;
6156a456 13771 }
465c120c
MR
13772 primary->pipe = pipe;
13773 primary->plane = pipe;
c59cb179
MR
13774 primary->check_plane = intel_check_primary_plane;
13775 primary->commit_plane = intel_commit_primary_plane;
a8ad0d8e 13776 primary->disable_plane = intel_disable_primary_plane;
465c120c
MR
13777 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13778 primary->plane = !pipe;
13779
6c0fd451
DL
13780 if (INTEL_INFO(dev)->gen >= 9) {
13781 intel_primary_formats = skl_primary_formats;
13782 num_formats = ARRAY_SIZE(skl_primary_formats);
13783 } else if (INTEL_INFO(dev)->gen >= 4) {
568db4f2
DL
13784 intel_primary_formats = i965_primary_formats;
13785 num_formats = ARRAY_SIZE(i965_primary_formats);
6c0fd451
DL
13786 } else {
13787 intel_primary_formats = i8xx_primary_formats;
13788 num_formats = ARRAY_SIZE(i8xx_primary_formats);
465c120c
MR
13789 }
13790
13791 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13792 &intel_plane_funcs,
465c120c
MR
13793 intel_primary_formats, num_formats,
13794 DRM_PLANE_TYPE_PRIMARY);
48404c1e 13795
3b7a5119
SJ
13796 if (INTEL_INFO(dev)->gen >= 4)
13797 intel_create_rotation_property(dev, primary);
48404c1e 13798
ea2c67bb
MR
13799 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13800
465c120c
MR
13801 return &primary->base;
13802}
13803
3b7a5119
SJ
13804void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13805{
13806 if (!dev->mode_config.rotation_property) {
13807 unsigned long flags = BIT(DRM_ROTATE_0) |
13808 BIT(DRM_ROTATE_180);
13809
13810 if (INTEL_INFO(dev)->gen >= 9)
13811 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13812
13813 dev->mode_config.rotation_property =
13814 drm_mode_create_rotation_property(dev, flags);
13815 }
13816 if (dev->mode_config.rotation_property)
13817 drm_object_attach_property(&plane->base.base,
13818 dev->mode_config.rotation_property,
13819 plane->base.state->rotation);
13820}
13821
3d7d6510 13822static int
852e787c 13823intel_check_cursor_plane(struct drm_plane *plane,
061e4b8d 13824 struct intel_crtc_state *crtc_state,
852e787c 13825 struct intel_plane_state *state)
3d7d6510 13826{
061e4b8d 13827 struct drm_crtc *crtc = crtc_state->base.crtc;
2b875c22 13828 struct drm_framebuffer *fb = state->base.fb;
757f9a3e 13829 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
757f9a3e
GP
13830 unsigned stride;
13831 int ret;
3d7d6510 13832
061e4b8d
ML
13833 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13834 &state->dst, &state->clip,
3d7d6510
MR
13835 DRM_PLANE_HELPER_NO_SCALING,
13836 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13837 true, true, &state->visible);
757f9a3e
GP
13838 if (ret)
13839 return ret;
13840
757f9a3e
GP
13841 /* if we want to turn off the cursor ignore width and height */
13842 if (!obj)
da20eabd 13843 return 0;
757f9a3e 13844
757f9a3e 13845 /* Check for which cursor types we support */
061e4b8d 13846 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
ea2c67bb
MR
13847 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13848 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13849 return -EINVAL;
13850 }
13851
ea2c67bb
MR
13852 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13853 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13854 DRM_DEBUG_KMS("buffer is too small\n");
13855 return -ENOMEM;
13856 }
13857
3a656b54 13858 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e 13859 DRM_DEBUG_KMS("cursor cannot be tiled\n");
da20eabd 13860 return -EINVAL;
32b7eeec
MR
13861 }
13862
da20eabd 13863 return 0;
852e787c 13864}
3d7d6510 13865
a8ad0d8e
ML
13866static void
13867intel_disable_cursor_plane(struct drm_plane *plane,
7fabf5ef 13868 struct drm_crtc *crtc)
a8ad0d8e 13869{
a8ad0d8e
ML
13870 intel_crtc_update_cursor(crtc, false);
13871}
13872
f4a2cf29 13873static void
852e787c
GP
13874intel_commit_cursor_plane(struct drm_plane *plane,
13875 struct intel_plane_state *state)
13876{
2b875c22 13877 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
13878 struct drm_device *dev = plane->dev;
13879 struct intel_crtc *intel_crtc;
2b875c22 13880 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13881 uint32_t addr;
852e787c 13882
ea2c67bb
MR
13883 crtc = crtc ? crtc : plane->crtc;
13884 intel_crtc = to_intel_crtc(crtc);
13885
2b875c22 13886 plane->fb = state->base.fb;
ea2c67bb
MR
13887 crtc->cursor_x = state->base.crtc_x;
13888 crtc->cursor_y = state->base.crtc_y;
13889
a912f12f
GP
13890 if (intel_crtc->cursor_bo == obj)
13891 goto update;
4ed91096 13892
f4a2cf29 13893 if (!obj)
a912f12f 13894 addr = 0;
f4a2cf29 13895 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 13896 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 13897 else
a912f12f 13898 addr = obj->phys_handle->busaddr;
852e787c 13899
a912f12f
GP
13900 intel_crtc->cursor_addr = addr;
13901 intel_crtc->cursor_bo = obj;
852e787c 13902
302d19ac 13903update:
a539205a 13904 if (crtc->state->active)
a912f12f 13905 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
13906}
13907
3d7d6510
MR
13908static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13909 int pipe)
13910{
13911 struct intel_plane *cursor;
8e7d688b 13912 struct intel_plane_state *state;
3d7d6510
MR
13913
13914 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13915 if (cursor == NULL)
13916 return NULL;
13917
8e7d688b
MR
13918 state = intel_create_plane_state(&cursor->base);
13919 if (!state) {
ea2c67bb
MR
13920 kfree(cursor);
13921 return NULL;
13922 }
8e7d688b 13923 cursor->base.state = &state->base;
ea2c67bb 13924
3d7d6510
MR
13925 cursor->can_scale = false;
13926 cursor->max_downscale = 1;
13927 cursor->pipe = pipe;
13928 cursor->plane = pipe;
c59cb179
MR
13929 cursor->check_plane = intel_check_cursor_plane;
13930 cursor->commit_plane = intel_commit_cursor_plane;
a8ad0d8e 13931 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
13932
13933 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 13934 &intel_plane_funcs,
3d7d6510
MR
13935 intel_cursor_formats,
13936 ARRAY_SIZE(intel_cursor_formats),
13937 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
13938
13939 if (INTEL_INFO(dev)->gen >= 4) {
13940 if (!dev->mode_config.rotation_property)
13941 dev->mode_config.rotation_property =
13942 drm_mode_create_rotation_property(dev,
13943 BIT(DRM_ROTATE_0) |
13944 BIT(DRM_ROTATE_180));
13945 if (dev->mode_config.rotation_property)
13946 drm_object_attach_property(&cursor->base.base,
13947 dev->mode_config.rotation_property,
8e7d688b 13948 state->base.rotation);
4398ad45
VS
13949 }
13950
af99ceda
CK
13951 if (INTEL_INFO(dev)->gen >=9)
13952 state->scaler_id = -1;
13953
ea2c67bb
MR
13954 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13955
3d7d6510
MR
13956 return &cursor->base;
13957}
13958
549e2bfb
CK
13959static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13960 struct intel_crtc_state *crtc_state)
13961{
13962 int i;
13963 struct intel_scaler *intel_scaler;
13964 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13965
13966 for (i = 0; i < intel_crtc->num_scalers; i++) {
13967 intel_scaler = &scaler_state->scalers[i];
13968 intel_scaler->in_use = 0;
549e2bfb
CK
13969 intel_scaler->mode = PS_SCALER_MODE_DYN;
13970 }
13971
13972 scaler_state->scaler_id = -1;
13973}
13974
b358d0a6 13975static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 13976{
fbee40df 13977 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 13978 struct intel_crtc *intel_crtc;
f5de6e07 13979 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
13980 struct drm_plane *primary = NULL;
13981 struct drm_plane *cursor = NULL;
465c120c 13982 int i, ret;
79e53945 13983
955382f3 13984 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
13985 if (intel_crtc == NULL)
13986 return;
13987
f5de6e07
ACO
13988 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13989 if (!crtc_state)
13990 goto fail;
550acefd
ACO
13991 intel_crtc->config = crtc_state;
13992 intel_crtc->base.state = &crtc_state->base;
07878248 13993 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13994
549e2bfb
CK
13995 /* initialize shared scalers */
13996 if (INTEL_INFO(dev)->gen >= 9) {
13997 if (pipe == PIPE_C)
13998 intel_crtc->num_scalers = 1;
13999 else
14000 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14001
14002 skl_init_scalers(dev, intel_crtc, crtc_state);
14003 }
14004
465c120c 14005 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
14006 if (!primary)
14007 goto fail;
14008
14009 cursor = intel_cursor_plane_create(dev, pipe);
14010 if (!cursor)
14011 goto fail;
14012
465c120c 14013 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
14014 cursor, &intel_crtc_funcs);
14015 if (ret)
14016 goto fail;
79e53945
JB
14017
14018 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
14019 for (i = 0; i < 256; i++) {
14020 intel_crtc->lut_r[i] = i;
14021 intel_crtc->lut_g[i] = i;
14022 intel_crtc->lut_b[i] = i;
14023 }
14024
1f1c2e24
VS
14025 /*
14026 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 14027 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 14028 */
80824003
JB
14029 intel_crtc->pipe = pipe;
14030 intel_crtc->plane = pipe;
3a77c4c4 14031 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 14032 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 14033 intel_crtc->plane = !pipe;
80824003
JB
14034 }
14035
4b0e333e
CW
14036 intel_crtc->cursor_base = ~0;
14037 intel_crtc->cursor_cntl = ~0;
dc41c154 14038 intel_crtc->cursor_size = ~0;
8d7849db 14039
22fd0fab
JB
14040 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14041 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14042 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14043 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14044
79e53945 14045 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
14046
14047 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
14048 return;
14049
14050fail:
14051 if (primary)
14052 drm_plane_cleanup(primary);
14053 if (cursor)
14054 drm_plane_cleanup(cursor);
f5de6e07 14055 kfree(crtc_state);
3d7d6510 14056 kfree(intel_crtc);
79e53945
JB
14057}
14058
752aa88a
JB
14059enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14060{
14061 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 14062 struct drm_device *dev = connector->base.dev;
752aa88a 14063
51fd371b 14064 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 14065
d3babd3f 14066 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
14067 return INVALID_PIPE;
14068
14069 return to_intel_crtc(encoder->crtc)->pipe;
14070}
14071
08d7b3d1 14072int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 14073 struct drm_file *file)
08d7b3d1 14074{
08d7b3d1 14075 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 14076 struct drm_crtc *drmmode_crtc;
c05422d5 14077 struct intel_crtc *crtc;
08d7b3d1 14078
7707e653 14079 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 14080
7707e653 14081 if (!drmmode_crtc) {
08d7b3d1 14082 DRM_ERROR("no such CRTC id\n");
3f2c2057 14083 return -ENOENT;
08d7b3d1
CW
14084 }
14085
7707e653 14086 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 14087 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 14088
c05422d5 14089 return 0;
08d7b3d1
CW
14090}
14091
66a9278e 14092static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 14093{
66a9278e
DV
14094 struct drm_device *dev = encoder->base.dev;
14095 struct intel_encoder *source_encoder;
79e53945 14096 int index_mask = 0;
79e53945
JB
14097 int entry = 0;
14098
b2784e15 14099 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 14100 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
14101 index_mask |= (1 << entry);
14102
79e53945
JB
14103 entry++;
14104 }
4ef69c7a 14105
79e53945
JB
14106 return index_mask;
14107}
14108
4d302442
CW
14109static bool has_edp_a(struct drm_device *dev)
14110{
14111 struct drm_i915_private *dev_priv = dev->dev_private;
14112
14113 if (!IS_MOBILE(dev))
14114 return false;
14115
14116 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14117 return false;
14118
e3589908 14119 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
14120 return false;
14121
14122 return true;
14123}
14124
84b4e042
JB
14125static bool intel_crt_present(struct drm_device *dev)
14126{
14127 struct drm_i915_private *dev_priv = dev->dev_private;
14128
884497ed
DL
14129 if (INTEL_INFO(dev)->gen >= 9)
14130 return false;
14131
cf404ce4 14132 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
14133 return false;
14134
14135 if (IS_CHERRYVIEW(dev))
14136 return false;
14137
14138 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14139 return false;
14140
14141 return true;
14142}
14143
79e53945
JB
14144static void intel_setup_outputs(struct drm_device *dev)
14145{
725e30ad 14146 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 14147 struct intel_encoder *encoder;
cb0953d7 14148 bool dpd_is_edp = false;
79e53945 14149
c9093354 14150 intel_lvds_init(dev);
79e53945 14151
84b4e042 14152 if (intel_crt_present(dev))
79935fca 14153 intel_crt_init(dev);
cb0953d7 14154
c776eb2e
VK
14155 if (IS_BROXTON(dev)) {
14156 /*
14157 * FIXME: Broxton doesn't support port detection via the
14158 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14159 * detect the ports.
14160 */
14161 intel_ddi_init(dev, PORT_A);
14162 intel_ddi_init(dev, PORT_B);
14163 intel_ddi_init(dev, PORT_C);
14164 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
14165 int found;
14166
de31facd
JB
14167 /*
14168 * Haswell uses DDI functions to detect digital outputs.
14169 * On SKL pre-D0 the strap isn't connected, so we assume
14170 * it's there.
14171 */
0e72a5b5 14172 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
de31facd
JB
14173 /* WaIgnoreDDIAStrap: skl */
14174 if (found ||
14175 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
0e72a5b5
ED
14176 intel_ddi_init(dev, PORT_A);
14177
14178 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14179 * register */
14180 found = I915_READ(SFUSE_STRAP);
14181
14182 if (found & SFUSE_STRAP_DDIB_DETECTED)
14183 intel_ddi_init(dev, PORT_B);
14184 if (found & SFUSE_STRAP_DDIC_DETECTED)
14185 intel_ddi_init(dev, PORT_C);
14186 if (found & SFUSE_STRAP_DDID_DETECTED)
14187 intel_ddi_init(dev, PORT_D);
14188 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 14189 int found;
5d8a7752 14190 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
14191
14192 if (has_edp_a(dev))
14193 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 14194
dc0fa718 14195 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 14196 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 14197 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 14198 if (!found)
e2debe91 14199 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 14200 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 14201 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
14202 }
14203
dc0fa718 14204 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 14205 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 14206
dc0fa718 14207 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 14208 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 14209
5eb08b69 14210 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 14211 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 14212
270b3042 14213 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 14214 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 14215 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
14216 /*
14217 * The DP_DETECTED bit is the latched state of the DDC
14218 * SDA pin at boot. However since eDP doesn't require DDC
14219 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14220 * eDP ports may have been muxed to an alternate function.
14221 * Thus we can't rely on the DP_DETECTED bit alone to detect
14222 * eDP ports. Consult the VBT as well as DP_DETECTED to
14223 * detect eDP ports.
14224 */
d2182a66
VS
14225 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14226 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
14227 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14228 PORT_B);
e17ac6db
VS
14229 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14230 intel_dp_is_edp(dev, PORT_B))
14231 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 14232
d2182a66
VS
14233 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14234 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
14235 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14236 PORT_C);
e17ac6db
VS
14237 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14238 intel_dp_is_edp(dev, PORT_C))
14239 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 14240
9418c1f1 14241 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14242 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
14243 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14244 PORT_D);
e17ac6db
VS
14245 /* eDP not supported on port D, so don't check VBT */
14246 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14247 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
14248 }
14249
3cfca973 14250 intel_dsi_init(dev);
103a196f 14251 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 14252 bool found = false;
7d57382e 14253
e2debe91 14254 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14255 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 14256 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
14257 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14258 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14259 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14260 }
27185ae1 14261
e7281eab 14262 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14263 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14264 }
13520b05
KH
14265
14266 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14267
e2debe91 14268 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14269 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 14270 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 14271 }
27185ae1 14272
e2debe91 14273 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14274
b01f2c3a
JB
14275 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14276 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14277 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14278 }
e7281eab 14279 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14280 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14281 }
27185ae1 14282
b01f2c3a 14283 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 14284 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14285 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14286 } else if (IS_GEN2(dev))
79e53945
JB
14287 intel_dvo_init(dev);
14288
103a196f 14289 if (SUPPORTS_TV(dev))
79e53945
JB
14290 intel_tv_init(dev);
14291
0bc12bcb 14292 intel_psr_init(dev);
7c8f8a70 14293
b2784e15 14294 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14295 encoder->base.possible_crtcs = encoder->crtc_mask;
14296 encoder->base.possible_clones =
66a9278e 14297 intel_encoder_clones(encoder);
79e53945 14298 }
47356eb6 14299
dde86e2d 14300 intel_init_pch_refclk(dev);
270b3042
DV
14301
14302 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14303}
14304
14305static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14306{
60a5ca01 14307 struct drm_device *dev = fb->dev;
79e53945 14308 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14309
ef2d633e 14310 drm_framebuffer_cleanup(fb);
60a5ca01 14311 mutex_lock(&dev->struct_mutex);
ef2d633e 14312 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14313 drm_gem_object_unreference(&intel_fb->obj->base);
14314 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14315 kfree(intel_fb);
14316}
14317
14318static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14319 struct drm_file *file,
79e53945
JB
14320 unsigned int *handle)
14321{
14322 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14323 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14324
05394f39 14325 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14326}
14327
14328static const struct drm_framebuffer_funcs intel_fb_funcs = {
14329 .destroy = intel_user_framebuffer_destroy,
14330 .create_handle = intel_user_framebuffer_create_handle,
14331};
14332
b321803d
DL
14333static
14334u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14335 uint32_t pixel_format)
14336{
14337 u32 gen = INTEL_INFO(dev)->gen;
14338
14339 if (gen >= 9) {
14340 /* "The stride in bytes must not exceed the of the size of 8K
14341 * pixels and 32K bytes."
14342 */
14343 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14344 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14345 return 32*1024;
14346 } else if (gen >= 4) {
14347 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14348 return 16*1024;
14349 else
14350 return 32*1024;
14351 } else if (gen >= 3) {
14352 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14353 return 8*1024;
14354 else
14355 return 16*1024;
14356 } else {
14357 /* XXX DSPC is limited to 4k tiled */
14358 return 8*1024;
14359 }
14360}
14361
b5ea642a
DV
14362static int intel_framebuffer_init(struct drm_device *dev,
14363 struct intel_framebuffer *intel_fb,
14364 struct drm_mode_fb_cmd2 *mode_cmd,
14365 struct drm_i915_gem_object *obj)
79e53945 14366{
6761dd31 14367 unsigned int aligned_height;
79e53945 14368 int ret;
b321803d 14369 u32 pitch_limit, stride_alignment;
79e53945 14370
dd4916c5
DV
14371 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14372
2a80eada
DV
14373 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14374 /* Enforce that fb modifier and tiling mode match, but only for
14375 * X-tiled. This is needed for FBC. */
14376 if (!!(obj->tiling_mode == I915_TILING_X) !=
14377 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14378 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14379 return -EINVAL;
14380 }
14381 } else {
14382 if (obj->tiling_mode == I915_TILING_X)
14383 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14384 else if (obj->tiling_mode == I915_TILING_Y) {
14385 DRM_DEBUG("No Y tiling for legacy addfb\n");
14386 return -EINVAL;
14387 }
14388 }
14389
9a8f0a12
TU
14390 /* Passed in modifier sanity checking. */
14391 switch (mode_cmd->modifier[0]) {
14392 case I915_FORMAT_MOD_Y_TILED:
14393 case I915_FORMAT_MOD_Yf_TILED:
14394 if (INTEL_INFO(dev)->gen < 9) {
14395 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14396 mode_cmd->modifier[0]);
14397 return -EINVAL;
14398 }
14399 case DRM_FORMAT_MOD_NONE:
14400 case I915_FORMAT_MOD_X_TILED:
14401 break;
14402 default:
c0f40428
JB
14403 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14404 mode_cmd->modifier[0]);
57cd6508 14405 return -EINVAL;
c16ed4be 14406 }
57cd6508 14407
b321803d
DL
14408 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14409 mode_cmd->pixel_format);
14410 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14411 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14412 mode_cmd->pitches[0], stride_alignment);
57cd6508 14413 return -EINVAL;
c16ed4be 14414 }
57cd6508 14415
b321803d
DL
14416 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14417 mode_cmd->pixel_format);
a35cdaa0 14418 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14419 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14420 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14421 "tiled" : "linear",
a35cdaa0 14422 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14423 return -EINVAL;
c16ed4be 14424 }
5d7bd705 14425
2a80eada 14426 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14427 mode_cmd->pitches[0] != obj->stride) {
14428 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14429 mode_cmd->pitches[0], obj->stride);
5d7bd705 14430 return -EINVAL;
c16ed4be 14431 }
5d7bd705 14432
57779d06 14433 /* Reject formats not supported by any plane early. */
308e5bcb 14434 switch (mode_cmd->pixel_format) {
57779d06 14435 case DRM_FORMAT_C8:
04b3924d
VS
14436 case DRM_FORMAT_RGB565:
14437 case DRM_FORMAT_XRGB8888:
14438 case DRM_FORMAT_ARGB8888:
57779d06
VS
14439 break;
14440 case DRM_FORMAT_XRGB1555:
c16ed4be 14441 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14442 DRM_DEBUG("unsupported pixel format: %s\n",
14443 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14444 return -EINVAL;
c16ed4be 14445 }
57779d06 14446 break;
57779d06 14447 case DRM_FORMAT_ABGR8888:
6c0fd451
DL
14448 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14449 DRM_DEBUG("unsupported pixel format: %s\n",
14450 drm_get_format_name(mode_cmd->pixel_format));
14451 return -EINVAL;
14452 }
14453 break;
14454 case DRM_FORMAT_XBGR8888:
04b3924d 14455 case DRM_FORMAT_XRGB2101010:
57779d06 14456 case DRM_FORMAT_XBGR2101010:
c16ed4be 14457 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14458 DRM_DEBUG("unsupported pixel format: %s\n",
14459 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14460 return -EINVAL;
c16ed4be 14461 }
b5626747 14462 break;
7531208b
DL
14463 case DRM_FORMAT_ABGR2101010:
14464 if (!IS_VALLEYVIEW(dev)) {
14465 DRM_DEBUG("unsupported pixel format: %s\n",
14466 drm_get_format_name(mode_cmd->pixel_format));
14467 return -EINVAL;
14468 }
14469 break;
04b3924d
VS
14470 case DRM_FORMAT_YUYV:
14471 case DRM_FORMAT_UYVY:
14472 case DRM_FORMAT_YVYU:
14473 case DRM_FORMAT_VYUY:
c16ed4be 14474 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14475 DRM_DEBUG("unsupported pixel format: %s\n",
14476 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14477 return -EINVAL;
c16ed4be 14478 }
57cd6508
CW
14479 break;
14480 default:
4ee62c76
VS
14481 DRM_DEBUG("unsupported pixel format: %s\n",
14482 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14483 return -EINVAL;
14484 }
14485
90f9a336
VS
14486 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14487 if (mode_cmd->offsets[0] != 0)
14488 return -EINVAL;
14489
ec2c981e 14490 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14491 mode_cmd->pixel_format,
14492 mode_cmd->modifier[0]);
53155c0a
DV
14493 /* FIXME drm helper for size checks (especially planar formats)? */
14494 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14495 return -EINVAL;
14496
c7d73f6a
DV
14497 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14498 intel_fb->obj = obj;
80075d49 14499 intel_fb->obj->framebuffer_references++;
c7d73f6a 14500
79e53945
JB
14501 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14502 if (ret) {
14503 DRM_ERROR("framebuffer init failed %d\n", ret);
14504 return ret;
14505 }
14506
79e53945
JB
14507 return 0;
14508}
14509
79e53945
JB
14510static struct drm_framebuffer *
14511intel_user_framebuffer_create(struct drm_device *dev,
14512 struct drm_file *filp,
308e5bcb 14513 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14514{
05394f39 14515 struct drm_i915_gem_object *obj;
79e53945 14516
308e5bcb
JB
14517 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14518 mode_cmd->handles[0]));
c8725226 14519 if (&obj->base == NULL)
cce13ff7 14520 return ERR_PTR(-ENOENT);
79e53945 14521
d2dff872 14522 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
14523}
14524
4520f53a 14525#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 14526static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14527{
14528}
14529#endif
14530
79e53945 14531static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14532 .fb_create = intel_user_framebuffer_create,
0632fef6 14533 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14534 .atomic_check = intel_atomic_check,
14535 .atomic_commit = intel_atomic_commit,
de419ab6
ML
14536 .atomic_state_alloc = intel_atomic_state_alloc,
14537 .atomic_state_clear = intel_atomic_state_clear,
79e53945
JB
14538};
14539
e70236a8
JB
14540/* Set up chip specific display functions */
14541static void intel_init_display(struct drm_device *dev)
14542{
14543 struct drm_i915_private *dev_priv = dev->dev_private;
14544
ee9300bb
DV
14545 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14546 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14547 else if (IS_CHERRYVIEW(dev))
14548 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14549 else if (IS_VALLEYVIEW(dev))
14550 dev_priv->display.find_dpll = vlv_find_best_dpll;
14551 else if (IS_PINEVIEW(dev))
14552 dev_priv->display.find_dpll = pnv_find_best_dpll;
14553 else
14554 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14555
bc8d7dff
DL
14556 if (INTEL_INFO(dev)->gen >= 9) {
14557 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14558 dev_priv->display.get_initial_plane_config =
14559 skylake_get_initial_plane_config;
bc8d7dff
DL
14560 dev_priv->display.crtc_compute_clock =
14561 haswell_crtc_compute_clock;
14562 dev_priv->display.crtc_enable = haswell_crtc_enable;
14563 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14564 dev_priv->display.update_primary_plane =
14565 skylake_update_primary_plane;
14566 } else if (HAS_DDI(dev)) {
0e8ffe1b 14567 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14568 dev_priv->display.get_initial_plane_config =
14569 ironlake_get_initial_plane_config;
797d0259
ACO
14570 dev_priv->display.crtc_compute_clock =
14571 haswell_crtc_compute_clock;
4f771f10
PZ
14572 dev_priv->display.crtc_enable = haswell_crtc_enable;
14573 dev_priv->display.crtc_disable = haswell_crtc_disable;
bc8d7dff
DL
14574 dev_priv->display.update_primary_plane =
14575 ironlake_update_primary_plane;
09b4ddf9 14576 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14577 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14578 dev_priv->display.get_initial_plane_config =
14579 ironlake_get_initial_plane_config;
3fb37703
ACO
14580 dev_priv->display.crtc_compute_clock =
14581 ironlake_crtc_compute_clock;
76e5a89c
DV
14582 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14583 dev_priv->display.crtc_disable = ironlake_crtc_disable;
262ca2b0
MR
14584 dev_priv->display.update_primary_plane =
14585 ironlake_update_primary_plane;
89b667f8
JB
14586 } else if (IS_VALLEYVIEW(dev)) {
14587 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14588 dev_priv->display.get_initial_plane_config =
14589 i9xx_get_initial_plane_config;
d6dfee7a 14590 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14591 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14592 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14593 dev_priv->display.update_primary_plane =
14594 i9xx_update_primary_plane;
f564048e 14595 } else {
0e8ffe1b 14596 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14597 dev_priv->display.get_initial_plane_config =
14598 i9xx_get_initial_plane_config;
d6dfee7a 14599 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14600 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14601 dev_priv->display.crtc_disable = i9xx_crtc_disable;
262ca2b0
MR
14602 dev_priv->display.update_primary_plane =
14603 i9xx_update_primary_plane;
f564048e 14604 }
e70236a8 14605
e70236a8 14606 /* Returns the core display clock speed */
1652d19e
VS
14607 if (IS_SKYLAKE(dev))
14608 dev_priv->display.get_display_clock_speed =
14609 skylake_get_display_clock_speed;
14610 else if (IS_BROADWELL(dev))
14611 dev_priv->display.get_display_clock_speed =
14612 broadwell_get_display_clock_speed;
14613 else if (IS_HASWELL(dev))
14614 dev_priv->display.get_display_clock_speed =
14615 haswell_get_display_clock_speed;
14616 else if (IS_VALLEYVIEW(dev))
25eb05fc
JB
14617 dev_priv->display.get_display_clock_speed =
14618 valleyview_get_display_clock_speed;
b37a6434
VS
14619 else if (IS_GEN5(dev))
14620 dev_priv->display.get_display_clock_speed =
14621 ilk_get_display_clock_speed;
a7c66cd8 14622 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
34edce2f 14623 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
e70236a8
JB
14624 dev_priv->display.get_display_clock_speed =
14625 i945_get_display_clock_speed;
34edce2f
VS
14626 else if (IS_GM45(dev))
14627 dev_priv->display.get_display_clock_speed =
14628 gm45_get_display_clock_speed;
14629 else if (IS_CRESTLINE(dev))
14630 dev_priv->display.get_display_clock_speed =
14631 i965gm_get_display_clock_speed;
14632 else if (IS_PINEVIEW(dev))
14633 dev_priv->display.get_display_clock_speed =
14634 pnv_get_display_clock_speed;
14635 else if (IS_G33(dev) || IS_G4X(dev))
14636 dev_priv->display.get_display_clock_speed =
14637 g33_get_display_clock_speed;
e70236a8
JB
14638 else if (IS_I915G(dev))
14639 dev_priv->display.get_display_clock_speed =
14640 i915_get_display_clock_speed;
257a7ffc 14641 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14642 dev_priv->display.get_display_clock_speed =
14643 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
14644 else if (IS_PINEVIEW(dev))
14645 dev_priv->display.get_display_clock_speed =
14646 pnv_get_display_clock_speed;
e70236a8
JB
14647 else if (IS_I915GM(dev))
14648 dev_priv->display.get_display_clock_speed =
14649 i915gm_get_display_clock_speed;
14650 else if (IS_I865G(dev))
14651 dev_priv->display.get_display_clock_speed =
14652 i865_get_display_clock_speed;
f0f8a9ce 14653 else if (IS_I85X(dev))
e70236a8 14654 dev_priv->display.get_display_clock_speed =
1b1d2716 14655 i85x_get_display_clock_speed;
623e01e5
VS
14656 else { /* 830 */
14657 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
e70236a8
JB
14658 dev_priv->display.get_display_clock_speed =
14659 i830_get_display_clock_speed;
623e01e5 14660 }
e70236a8 14661
7c10a2b5 14662 if (IS_GEN5(dev)) {
3bb11b53 14663 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14664 } else if (IS_GEN6(dev)) {
14665 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14666 } else if (IS_IVYBRIDGE(dev)) {
14667 /* FIXME: detect B0+ stepping and use auto training */
14668 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14669 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14670 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
27c329ed
ML
14671 if (IS_BROADWELL(dev)) {
14672 dev_priv->display.modeset_commit_cdclk =
14673 broadwell_modeset_commit_cdclk;
14674 dev_priv->display.modeset_calc_cdclk =
14675 broadwell_modeset_calc_cdclk;
14676 }
30a970c6 14677 } else if (IS_VALLEYVIEW(dev)) {
27c329ed
ML
14678 dev_priv->display.modeset_commit_cdclk =
14679 valleyview_modeset_commit_cdclk;
14680 dev_priv->display.modeset_calc_cdclk =
14681 valleyview_modeset_calc_cdclk;
f8437dd1 14682 } else if (IS_BROXTON(dev)) {
27c329ed
ML
14683 dev_priv->display.modeset_commit_cdclk =
14684 broxton_modeset_commit_cdclk;
14685 dev_priv->display.modeset_calc_cdclk =
14686 broxton_modeset_calc_cdclk;
e70236a8 14687 }
8c9f3aaf 14688
8c9f3aaf
JB
14689 switch (INTEL_INFO(dev)->gen) {
14690 case 2:
14691 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14692 break;
14693
14694 case 3:
14695 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14696 break;
14697
14698 case 4:
14699 case 5:
14700 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14701 break;
14702
14703 case 6:
14704 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14705 break;
7c9017e5 14706 case 7:
4e0bbc31 14707 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14708 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14709 break;
830c81db 14710 case 9:
ba343e02
TU
14711 /* Drop through - unsupported since execlist only. */
14712 default:
14713 /* Default just returns -ENODEV to indicate unsupported */
14714 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14715 }
7bd688cd
JN
14716
14717 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
14718
14719 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
14720}
14721
b690e96c
JB
14722/*
14723 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14724 * resume, or other times. This quirk makes sure that's the case for
14725 * affected systems.
14726 */
0206e353 14727static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14728{
14729 struct drm_i915_private *dev_priv = dev->dev_private;
14730
14731 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14732 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14733}
14734
b6b5d049
VS
14735static void quirk_pipeb_force(struct drm_device *dev)
14736{
14737 struct drm_i915_private *dev_priv = dev->dev_private;
14738
14739 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14740 DRM_INFO("applying pipe b force quirk\n");
14741}
14742
435793df
KP
14743/*
14744 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14745 */
14746static void quirk_ssc_force_disable(struct drm_device *dev)
14747{
14748 struct drm_i915_private *dev_priv = dev->dev_private;
14749 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14750 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14751}
14752
4dca20ef 14753/*
5a15ab5b
CE
14754 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14755 * brightness value
4dca20ef
CE
14756 */
14757static void quirk_invert_brightness(struct drm_device *dev)
14758{
14759 struct drm_i915_private *dev_priv = dev->dev_private;
14760 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14761 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14762}
14763
9c72cc6f
SD
14764/* Some VBT's incorrectly indicate no backlight is present */
14765static void quirk_backlight_present(struct drm_device *dev)
14766{
14767 struct drm_i915_private *dev_priv = dev->dev_private;
14768 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14769 DRM_INFO("applying backlight present quirk\n");
14770}
14771
b690e96c
JB
14772struct intel_quirk {
14773 int device;
14774 int subsystem_vendor;
14775 int subsystem_device;
14776 void (*hook)(struct drm_device *dev);
14777};
14778
5f85f176
EE
14779/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14780struct intel_dmi_quirk {
14781 void (*hook)(struct drm_device *dev);
14782 const struct dmi_system_id (*dmi_id_list)[];
14783};
14784
14785static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14786{
14787 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14788 return 1;
14789}
14790
14791static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14792 {
14793 .dmi_id_list = &(const struct dmi_system_id[]) {
14794 {
14795 .callback = intel_dmi_reverse_brightness,
14796 .ident = "NCR Corporation",
14797 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14798 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14799 },
14800 },
14801 { } /* terminating entry */
14802 },
14803 .hook = quirk_invert_brightness,
14804 },
14805};
14806
c43b5634 14807static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14808 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14809 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14810
b690e96c
JB
14811 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14812 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14813
5f080c0f
VS
14814 /* 830 needs to leave pipe A & dpll A up */
14815 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14816
b6b5d049
VS
14817 /* 830 needs to leave pipe B & dpll B up */
14818 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14819
435793df
KP
14820 /* Lenovo U160 cannot use SSC on LVDS */
14821 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14822
14823 /* Sony Vaio Y cannot use SSC on LVDS */
14824 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14825
be505f64
AH
14826 /* Acer Aspire 5734Z must invert backlight brightness */
14827 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14828
14829 /* Acer/eMachines G725 */
14830 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14831
14832 /* Acer/eMachines e725 */
14833 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14834
14835 /* Acer/Packard Bell NCL20 */
14836 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14837
14838 /* Acer Aspire 4736Z */
14839 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14840
14841 /* Acer Aspire 5336 */
14842 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14843
14844 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14845 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14846
dfb3d47b
SD
14847 /* Acer C720 Chromebook (Core i3 4005U) */
14848 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14849
b2a9601c 14850 /* Apple Macbook 2,1 (Core 2 T7400) */
14851 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14852
d4967d8c
SD
14853 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14854 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14855
14856 /* HP Chromebook 14 (Celeron 2955U) */
14857 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14858
14859 /* Dell Chromebook 11 */
14860 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14861};
14862
14863static void intel_init_quirks(struct drm_device *dev)
14864{
14865 struct pci_dev *d = dev->pdev;
14866 int i;
14867
14868 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14869 struct intel_quirk *q = &intel_quirks[i];
14870
14871 if (d->device == q->device &&
14872 (d->subsystem_vendor == q->subsystem_vendor ||
14873 q->subsystem_vendor == PCI_ANY_ID) &&
14874 (d->subsystem_device == q->subsystem_device ||
14875 q->subsystem_device == PCI_ANY_ID))
14876 q->hook(dev);
14877 }
5f85f176
EE
14878 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14879 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14880 intel_dmi_quirks[i].hook(dev);
14881 }
b690e96c
JB
14882}
14883
9cce37f4
JB
14884/* Disable the VGA plane that we never use */
14885static void i915_disable_vga(struct drm_device *dev)
14886{
14887 struct drm_i915_private *dev_priv = dev->dev_private;
14888 u8 sr1;
766aa1c4 14889 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 14890
2b37c616 14891 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 14892 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14893 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14894 sr1 = inb(VGA_SR_DATA);
14895 outb(sr1 | 1<<5, VGA_SR_DATA);
14896 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14897 udelay(300);
14898
01f5a626 14899 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14900 POSTING_READ(vga_reg);
14901}
14902
f817586c
DV
14903void intel_modeset_init_hw(struct drm_device *dev)
14904{
b6283055 14905 intel_update_cdclk(dev);
a8f78b58 14906 intel_prepare_ddi(dev);
f817586c 14907 intel_init_clock_gating(dev);
8090c6b9 14908 intel_enable_gt_powersave(dev);
f817586c
DV
14909}
14910
79e53945
JB
14911void intel_modeset_init(struct drm_device *dev)
14912{
652c393a 14913 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 14914 int sprite, ret;
8cc87b75 14915 enum pipe pipe;
46f297fb 14916 struct intel_crtc *crtc;
79e53945
JB
14917
14918 drm_mode_config_init(dev);
14919
14920 dev->mode_config.min_width = 0;
14921 dev->mode_config.min_height = 0;
14922
019d96cb
DA
14923 dev->mode_config.preferred_depth = 24;
14924 dev->mode_config.prefer_shadow = 1;
14925
25bab385
TU
14926 dev->mode_config.allow_fb_modifiers = true;
14927
e6ecefaa 14928 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14929
b690e96c
JB
14930 intel_init_quirks(dev);
14931
1fa61106
ED
14932 intel_init_pm(dev);
14933
e3c74757
BW
14934 if (INTEL_INFO(dev)->num_pipes == 0)
14935 return;
14936
e70236a8 14937 intel_init_display(dev);
7c10a2b5 14938 intel_init_audio(dev);
e70236a8 14939
a6c45cf0
CW
14940 if (IS_GEN2(dev)) {
14941 dev->mode_config.max_width = 2048;
14942 dev->mode_config.max_height = 2048;
14943 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
14944 dev->mode_config.max_width = 4096;
14945 dev->mode_config.max_height = 4096;
79e53945 14946 } else {
a6c45cf0
CW
14947 dev->mode_config.max_width = 8192;
14948 dev->mode_config.max_height = 8192;
79e53945 14949 }
068be561 14950
dc41c154
VS
14951 if (IS_845G(dev) || IS_I865G(dev)) {
14952 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14953 dev->mode_config.cursor_height = 1023;
14954 } else if (IS_GEN2(dev)) {
068be561
DL
14955 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14956 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14957 } else {
14958 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14959 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14960 }
14961
5d4545ae 14962 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 14963
28c97730 14964 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
14965 INTEL_INFO(dev)->num_pipes,
14966 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 14967
055e393f 14968 for_each_pipe(dev_priv, pipe) {
8cc87b75 14969 intel_crtc_init(dev, pipe);
3bdcfc0c 14970 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 14971 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 14972 if (ret)
06da8da2 14973 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 14974 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 14975 }
79e53945
JB
14976 }
14977
f42bb70d
JB
14978 intel_init_dpio(dev);
14979
e72f9fbf 14980 intel_shared_dpll_init(dev);
ee7b9f93 14981
9cce37f4
JB
14982 /* Just disable it once at startup */
14983 i915_disable_vga(dev);
79e53945 14984 intel_setup_outputs(dev);
11be49eb
CW
14985
14986 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 14987 intel_fbc_disable(dev);
fa9fa083 14988
6e9f798d 14989 drm_modeset_lock_all(dev);
fa9fa083 14990 intel_modeset_setup_hw_state(dev, false);
6e9f798d 14991 drm_modeset_unlock_all(dev);
46f297fb 14992
d3fcc808 14993 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
14994 if (!crtc->active)
14995 continue;
14996
46f297fb 14997 /*
46f297fb
JB
14998 * Note that reserving the BIOS fb up front prevents us
14999 * from stuffing other stolen allocations like the ring
15000 * on top. This prevents some ugliness at boot time, and
15001 * can even allow for smooth boot transitions if the BIOS
15002 * fb is large enough for the active pipe configuration.
15003 */
5724dbd1
DL
15004 if (dev_priv->display.get_initial_plane_config) {
15005 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
15006 &crtc->plane_config);
15007 /*
15008 * If the fb is shared between multiple heads, we'll
15009 * just get the first one.
15010 */
f6936e29 15011 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
46f297fb 15012 }
46f297fb 15013 }
2c7111db
CW
15014}
15015
7fad798e
DV
15016static void intel_enable_pipe_a(struct drm_device *dev)
15017{
15018 struct intel_connector *connector;
15019 struct drm_connector *crt = NULL;
15020 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 15021 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
15022
15023 /* We can't just switch on the pipe A, we need to set things up with a
15024 * proper mode and output configuration. As a gross hack, enable pipe A
15025 * by enabling the load detect pipe once. */
3a3371ff 15026 for_each_intel_connector(dev, connector) {
7fad798e
DV
15027 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15028 crt = &connector->base;
15029 break;
15030 }
15031 }
15032
15033 if (!crt)
15034 return;
15035
208bf9fd 15036 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 15037 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
15038}
15039
fa555837
DV
15040static bool
15041intel_check_plane_mapping(struct intel_crtc *crtc)
15042{
7eb552ae
BW
15043 struct drm_device *dev = crtc->base.dev;
15044 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
15045 u32 reg, val;
15046
7eb552ae 15047 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
15048 return true;
15049
15050 reg = DSPCNTR(!crtc->plane);
15051 val = I915_READ(reg);
15052
15053 if ((val & DISPLAY_PLANE_ENABLE) &&
15054 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15055 return false;
15056
15057 return true;
15058}
15059
24929352
DV
15060static void intel_sanitize_crtc(struct intel_crtc *crtc)
15061{
15062 struct drm_device *dev = crtc->base.dev;
15063 struct drm_i915_private *dev_priv = dev->dev_private;
b17d48e2 15064 struct intel_encoder *encoder;
fa555837 15065 u32 reg;
b17d48e2 15066 bool enable;
24929352 15067
24929352 15068 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 15069 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
15070 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15071
d3eaf884 15072 /* restore vblank interrupts to correct state */
9625604c 15073 drm_crtc_vblank_reset(&crtc->base);
d297e103
VS
15074 if (crtc->active) {
15075 update_scanline_offset(crtc);
9625604c
DV
15076 drm_crtc_vblank_on(&crtc->base);
15077 }
d3eaf884 15078
24929352 15079 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
15080 * disable the crtc (and hence change the state) if it is wrong. Note
15081 * that gen4+ has a fixed plane -> pipe mapping. */
15082 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
15083 bool plane;
15084
24929352
DV
15085 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15086 crtc->base.base.id);
15087
15088 /* Pipe has the wrong plane attached and the plane is active.
15089 * Temporarily change the plane mapping and disable everything
15090 * ... */
15091 plane = crtc->plane;
b70709a6 15092 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 15093 crtc->plane = !plane;
b17d48e2 15094 intel_crtc_disable_noatomic(&crtc->base);
24929352 15095 crtc->plane = plane;
24929352 15096 }
24929352 15097
7fad798e
DV
15098 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15099 crtc->pipe == PIPE_A && !crtc->active) {
15100 /* BIOS forgot to enable pipe A, this mostly happens after
15101 * resume. Force-enable the pipe to fix this, the update_dpms
15102 * call below we restore the pipe to the right state, but leave
15103 * the required bits on. */
15104 intel_enable_pipe_a(dev);
15105 }
15106
24929352
DV
15107 /* Adjust the state of the output pipe according to whether we
15108 * have active connectors/encoders. */
b17d48e2
ML
15109 enable = false;
15110 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15111 enable |= encoder->connectors_active;
24929352 15112
b17d48e2
ML
15113 if (!enable)
15114 intel_crtc_disable_noatomic(&crtc->base);
24929352 15115
53d9f4e9 15116 if (crtc->active != crtc->base.state->active) {
24929352
DV
15117
15118 /* This can happen either due to bugs in the get_hw_state
b17d48e2
ML
15119 * functions or because of calls to intel_crtc_disable_noatomic,
15120 * or because the pipe is force-enabled due to the
24929352
DV
15121 * pipe A quirk. */
15122 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15123 crtc->base.base.id,
83d65738 15124 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
15125 crtc->active ? "enabled" : "disabled");
15126
83d65738 15127 crtc->base.state->enable = crtc->active;
49d6fa21 15128 crtc->base.state->active = crtc->active;
24929352
DV
15129 crtc->base.enabled = crtc->active;
15130
15131 /* Because we only establish the connector -> encoder ->
15132 * crtc links if something is active, this means the
15133 * crtc is now deactivated. Break the links. connector
15134 * -> encoder links are only establish when things are
15135 * actually up, hence no need to break them. */
15136 WARN_ON(crtc->active);
15137
15138 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15139 WARN_ON(encoder->connectors_active);
15140 encoder->base.crtc = NULL;
15141 }
15142 }
c5ab3bc0 15143
a3ed6aad 15144 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
15145 /*
15146 * We start out with underrun reporting disabled to avoid races.
15147 * For correct bookkeeping mark this on active crtcs.
15148 *
c5ab3bc0
DV
15149 * Also on gmch platforms we dont have any hardware bits to
15150 * disable the underrun reporting. Which means we need to start
15151 * out with underrun reporting disabled also on inactive pipes,
15152 * since otherwise we'll complain about the garbage we read when
15153 * e.g. coming up after runtime pm.
15154 *
4cc31489
DV
15155 * No protection against concurrent access is required - at
15156 * worst a fifo underrun happens which also sets this to false.
15157 */
15158 crtc->cpu_fifo_underrun_disabled = true;
15159 crtc->pch_fifo_underrun_disabled = true;
15160 }
24929352
DV
15161}
15162
15163static void intel_sanitize_encoder(struct intel_encoder *encoder)
15164{
15165 struct intel_connector *connector;
15166 struct drm_device *dev = encoder->base.dev;
15167
15168 /* We need to check both for a crtc link (meaning that the
15169 * encoder is active and trying to read from a pipe) and the
15170 * pipe itself being active. */
15171 bool has_active_crtc = encoder->base.crtc &&
15172 to_intel_crtc(encoder->base.crtc)->active;
15173
15174 if (encoder->connectors_active && !has_active_crtc) {
15175 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15176 encoder->base.base.id,
8e329a03 15177 encoder->base.name);
24929352
DV
15178
15179 /* Connector is active, but has no active pipe. This is
15180 * fallout from our resume register restoring. Disable
15181 * the encoder manually again. */
15182 if (encoder->base.crtc) {
15183 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15184 encoder->base.base.id,
8e329a03 15185 encoder->base.name);
24929352 15186 encoder->disable(encoder);
a62d1497
VS
15187 if (encoder->post_disable)
15188 encoder->post_disable(encoder);
24929352 15189 }
7f1950fb
EE
15190 encoder->base.crtc = NULL;
15191 encoder->connectors_active = false;
24929352
DV
15192
15193 /* Inconsistent output/port/pipe state happens presumably due to
15194 * a bug in one of the get_hw_state functions. Or someplace else
15195 * in our code, like the register restore mess on resume. Clamp
15196 * things to off as a safer default. */
3a3371ff 15197 for_each_intel_connector(dev, connector) {
24929352
DV
15198 if (connector->encoder != encoder)
15199 continue;
7f1950fb
EE
15200 connector->base.dpms = DRM_MODE_DPMS_OFF;
15201 connector->base.encoder = NULL;
24929352
DV
15202 }
15203 }
15204 /* Enabled encoders without active connectors will be fixed in
15205 * the crtc fixup. */
15206}
15207
04098753 15208void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
15209{
15210 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 15211 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 15212
04098753
ID
15213 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15214 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15215 i915_disable_vga(dev);
15216 }
15217}
15218
15219void i915_redisable_vga(struct drm_device *dev)
15220{
15221 struct drm_i915_private *dev_priv = dev->dev_private;
15222
8dc8a27c
PZ
15223 /* This function can be called both from intel_modeset_setup_hw_state or
15224 * at a very early point in our resume sequence, where the power well
15225 * structures are not yet restored. Since this function is at a very
15226 * paranoid "someone might have enabled VGA while we were not looking"
15227 * level, just check if the power well is enabled instead of trying to
15228 * follow the "don't touch the power well if we don't need it" policy
15229 * the rest of the driver uses. */
f458ebbc 15230 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15231 return;
15232
04098753 15233 i915_redisable_vga_power_on(dev);
0fde901f
KM
15234}
15235
98ec7739
VS
15236static bool primary_get_hw_state(struct intel_crtc *crtc)
15237{
15238 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15239
d032ffa0
ML
15240 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15241}
15242
15243static void readout_plane_state(struct intel_crtc *crtc,
15244 struct intel_crtc_state *crtc_state)
15245{
15246 struct intel_plane *p;
15247 struct drm_plane_state *drm_plane_state;
15248 bool active = crtc_state->base.active;
15249
15250 if (active) {
15251 crtc_state->quirks |= PIPE_CONFIG_QUIRK_INITIAL_PLANES;
15252
15253 /* apply to previous sw state too */
15254 to_intel_crtc_state(crtc->base.state)->quirks |=
15255 PIPE_CONFIG_QUIRK_INITIAL_PLANES;
15256 }
98ec7739 15257
d032ffa0
ML
15258 for_each_intel_plane(crtc->base.dev, p) {
15259 bool visible = active;
15260
15261 if (crtc->pipe != p->pipe)
15262 continue;
15263
15264 drm_plane_state = p->base.state;
15265 if (active && p->base.type == DRM_PLANE_TYPE_PRIMARY) {
15266 visible = primary_get_hw_state(crtc);
15267 to_intel_plane_state(drm_plane_state)->visible = visible;
15268 } else {
15269 /*
15270 * unknown state, assume it's off to force a transition
15271 * to on when calculating state changes.
15272 */
15273 to_intel_plane_state(drm_plane_state)->visible = false;
15274 }
15275
15276 if (visible) {
15277 crtc_state->base.plane_mask |=
15278 1 << drm_plane_index(&p->base);
15279 } else if (crtc_state->base.state) {
15280 /* Make this unconditional for atomic hw readout. */
15281 crtc_state->base.plane_mask &=
15282 ~(1 << drm_plane_index(&p->base));
15283 }
15284 }
98ec7739
VS
15285}
15286
30e984df 15287static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15288{
15289 struct drm_i915_private *dev_priv = dev->dev_private;
15290 enum pipe pipe;
24929352
DV
15291 struct intel_crtc *crtc;
15292 struct intel_encoder *encoder;
15293 struct intel_connector *connector;
5358901f 15294 int i;
24929352 15295
d3fcc808 15296 for_each_intel_crtc(dev, crtc) {
6e3c9717 15297 memset(crtc->config, 0, sizeof(*crtc->config));
f7217905 15298 crtc->config->base.crtc = &crtc->base;
3b117c8f 15299
6e3c9717 15300 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
9953599b 15301
0e8ffe1b 15302 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 15303 crtc->config);
24929352 15304
83d65738 15305 crtc->base.state->enable = crtc->active;
49d6fa21 15306 crtc->base.state->active = crtc->active;
24929352 15307 crtc->base.enabled = crtc->active;
b8b7fade 15308 crtc->base.hwmode = crtc->config->base.adjusted_mode;
b70709a6 15309
d032ffa0 15310 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
24929352
DV
15311
15312 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15313 crtc->base.base.id,
15314 crtc->active ? "enabled" : "disabled");
15315 }
15316
5358901f
DV
15317 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15318 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15319
3e369b76
ACO
15320 pll->on = pll->get_hw_state(dev_priv, pll,
15321 &pll->config.hw_state);
5358901f 15322 pll->active = 0;
3e369b76 15323 pll->config.crtc_mask = 0;
d3fcc808 15324 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 15325 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 15326 pll->active++;
3e369b76 15327 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 15328 }
5358901f 15329 }
5358901f 15330
1e6f2ddc 15331 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15332 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 15333
3e369b76 15334 if (pll->config.crtc_mask)
bd2bb1b9 15335 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
15336 }
15337
b2784e15 15338 for_each_intel_encoder(dev, encoder) {
24929352
DV
15339 pipe = 0;
15340
15341 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15342 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15343 encoder->base.crtc = &crtc->base;
6e3c9717 15344 encoder->get_config(encoder, crtc->config);
24929352
DV
15345 } else {
15346 encoder->base.crtc = NULL;
15347 }
15348
15349 encoder->connectors_active = false;
6f2bcceb 15350 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15351 encoder->base.base.id,
8e329a03 15352 encoder->base.name,
24929352 15353 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15354 pipe_name(pipe));
24929352
DV
15355 }
15356
3a3371ff 15357 for_each_intel_connector(dev, connector) {
24929352
DV
15358 if (connector->get_hw_state(connector)) {
15359 connector->base.dpms = DRM_MODE_DPMS_ON;
15360 connector->encoder->connectors_active = true;
15361 connector->base.encoder = &connector->encoder->base;
15362 } else {
15363 connector->base.dpms = DRM_MODE_DPMS_OFF;
15364 connector->base.encoder = NULL;
15365 }
15366 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15367 connector->base.base.id,
c23cc417 15368 connector->base.name,
24929352
DV
15369 connector->base.encoder ? "enabled" : "disabled");
15370 }
30e984df
DV
15371}
15372
15373/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15374 * and i915 state tracking structures. */
15375void intel_modeset_setup_hw_state(struct drm_device *dev,
15376 bool force_restore)
15377{
15378 struct drm_i915_private *dev_priv = dev->dev_private;
15379 enum pipe pipe;
30e984df
DV
15380 struct intel_crtc *crtc;
15381 struct intel_encoder *encoder;
35c95375 15382 int i;
30e984df
DV
15383
15384 intel_modeset_readout_hw_state(dev);
24929352 15385
babea61d
JB
15386 /*
15387 * Now that we have the config, copy it to each CRTC struct
15388 * Note that this could go away if we move to using crtc_config
15389 * checking everywhere.
15390 */
d3fcc808 15391 for_each_intel_crtc(dev, crtc) {
d330a953 15392 if (crtc->active && i915.fastboot) {
6e3c9717
ACO
15393 intel_mode_from_pipe_config(&crtc->base.mode,
15394 crtc->config);
babea61d
JB
15395 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15396 crtc->base.base.id);
15397 drm_mode_debug_printmodeline(&crtc->base.mode);
15398 }
15399 }
15400
24929352 15401 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15402 for_each_intel_encoder(dev, encoder) {
24929352
DV
15403 intel_sanitize_encoder(encoder);
15404 }
15405
055e393f 15406 for_each_pipe(dev_priv, pipe) {
24929352
DV
15407 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15408 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15409 intel_dump_pipe_config(crtc, crtc->config,
15410 "[setup_hw_state]");
24929352 15411 }
9a935856 15412
d29b2f9d
ACO
15413 intel_modeset_update_connector_atomic_state(dev);
15414
35c95375
DV
15415 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15416 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15417
15418 if (!pll->on || pll->active)
15419 continue;
15420
15421 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15422
15423 pll->disable(dev_priv, pll);
15424 pll->on = false;
15425 }
15426
3078999f
PB
15427 if (IS_GEN9(dev))
15428 skl_wm_get_hw_state(dev);
15429 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
15430 ilk_wm_get_hw_state(dev);
15431
45e2b5f6 15432 if (force_restore) {
7d0bc1ea
VS
15433 i915_redisable_vga(dev);
15434
f30da187
DV
15435 /*
15436 * We need to use raw interfaces for restoring state to avoid
15437 * checking (bogus) intermediate states.
15438 */
055e393f 15439 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
15440 struct drm_crtc *crtc =
15441 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187 15442
83a57153 15443 intel_crtc_restore_mode(crtc);
45e2b5f6
DV
15444 }
15445 } else {
15446 intel_modeset_update_staged_output_state(dev);
15447 }
8af6cf88
DV
15448
15449 intel_modeset_check_state(dev);
2c7111db
CW
15450}
15451
15452void intel_modeset_gem_init(struct drm_device *dev)
15453{
92122789 15454 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 15455 struct drm_crtc *c;
2ff8fde1 15456 struct drm_i915_gem_object *obj;
e0d6149b 15457 int ret;
484b41dd 15458
ae48434c
ID
15459 mutex_lock(&dev->struct_mutex);
15460 intel_init_gt_powersave(dev);
15461 mutex_unlock(&dev->struct_mutex);
15462
92122789
JB
15463 /*
15464 * There may be no VBT; and if the BIOS enabled SSC we can
15465 * just keep using it to avoid unnecessary flicker. Whereas if the
15466 * BIOS isn't using it, don't assume it will work even if the VBT
15467 * indicates as much.
15468 */
15469 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15470 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15471 DREF_SSC1_ENABLE);
15472
1833b134 15473 intel_modeset_init_hw(dev);
02e792fb
DV
15474
15475 intel_setup_overlay(dev);
484b41dd
JB
15476
15477 /*
15478 * Make sure any fbs we allocated at startup are properly
15479 * pinned & fenced. When we do the allocation it's too early
15480 * for this.
15481 */
70e1e0ec 15482 for_each_crtc(dev, c) {
2ff8fde1
MR
15483 obj = intel_fb_obj(c->primary->fb);
15484 if (obj == NULL)
484b41dd
JB
15485 continue;
15486
e0d6149b
TU
15487 mutex_lock(&dev->struct_mutex);
15488 ret = intel_pin_and_fence_fb_obj(c->primary,
15489 c->primary->fb,
15490 c->primary->state,
91af127f 15491 NULL, NULL);
e0d6149b
TU
15492 mutex_unlock(&dev->struct_mutex);
15493 if (ret) {
484b41dd
JB
15494 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15495 to_intel_crtc(c)->pipe);
66e514c1
DA
15496 drm_framebuffer_unreference(c->primary->fb);
15497 c->primary->fb = NULL;
36750f28 15498 c->primary->crtc = c->primary->state->crtc = NULL;
afd65eb4 15499 update_state_fb(c->primary);
36750f28 15500 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
484b41dd
JB
15501 }
15502 }
0962c3c9
VS
15503
15504 intel_backlight_register(dev);
79e53945
JB
15505}
15506
4932e2c3
ID
15507void intel_connector_unregister(struct intel_connector *intel_connector)
15508{
15509 struct drm_connector *connector = &intel_connector->base;
15510
15511 intel_panel_destroy_backlight(connector);
34ea3d38 15512 drm_connector_unregister(connector);
4932e2c3
ID
15513}
15514
79e53945
JB
15515void intel_modeset_cleanup(struct drm_device *dev)
15516{
652c393a 15517 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 15518 struct drm_connector *connector;
652c393a 15519
2eb5252e
ID
15520 intel_disable_gt_powersave(dev);
15521
0962c3c9
VS
15522 intel_backlight_unregister(dev);
15523
fd0c0642
DV
15524 /*
15525 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15526 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15527 * experience fancy races otherwise.
15528 */
2aeb7d3a 15529 intel_irq_uninstall(dev_priv);
eb21b92b 15530
fd0c0642
DV
15531 /*
15532 * Due to the hpd irq storm handling the hotplug work can re-arm the
15533 * poll handlers. Hence disable polling after hpd handling is shut down.
15534 */
f87ea761 15535 drm_kms_helper_poll_fini(dev);
fd0c0642 15536
652c393a
JB
15537 mutex_lock(&dev->struct_mutex);
15538
723bfd70
JB
15539 intel_unregister_dsm_handler();
15540
7ff0ebcc 15541 intel_fbc_disable(dev);
e70236a8 15542
69341a5e
KH
15543 mutex_unlock(&dev->struct_mutex);
15544
1630fe75
CW
15545 /* flush any delayed tasks or pending work */
15546 flush_scheduled_work();
15547
db31af1d
JN
15548 /* destroy the backlight and sysfs files before encoders/connectors */
15549 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
15550 struct intel_connector *intel_connector;
15551
15552 intel_connector = to_intel_connector(connector);
15553 intel_connector->unregister(intel_connector);
db31af1d 15554 }
d9255d57 15555
79e53945 15556 drm_mode_config_cleanup(dev);
4d7bb011
DV
15557
15558 intel_cleanup_overlay(dev);
ae48434c
ID
15559
15560 mutex_lock(&dev->struct_mutex);
15561 intel_cleanup_gt_powersave(dev);
15562 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15563}
15564
f1c79df3
ZW
15565/*
15566 * Return which encoder is currently attached for connector.
15567 */
df0e9248 15568struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15569{
df0e9248
CW
15570 return &intel_attached_encoder(connector)->base;
15571}
f1c79df3 15572
df0e9248
CW
15573void intel_connector_attach_encoder(struct intel_connector *connector,
15574 struct intel_encoder *encoder)
15575{
15576 connector->encoder = encoder;
15577 drm_mode_connector_attach_encoder(&connector->base,
15578 &encoder->base);
79e53945 15579}
28d52043
DA
15580
15581/*
15582 * set vga decode state - true == enable VGA decode
15583 */
15584int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15585{
15586 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15587 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15588 u16 gmch_ctrl;
15589
75fa041d
CW
15590 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15591 DRM_ERROR("failed to read control word\n");
15592 return -EIO;
15593 }
15594
c0cc8a55
CW
15595 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15596 return 0;
15597
28d52043
DA
15598 if (state)
15599 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15600 else
15601 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15602
15603 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15604 DRM_ERROR("failed to write control word\n");
15605 return -EIO;
15606 }
15607
28d52043
DA
15608 return 0;
15609}
c4a1d9e4 15610
c4a1d9e4 15611struct intel_display_error_state {
ff57f1b0
PZ
15612
15613 u32 power_well_driver;
15614
63b66e5b
CW
15615 int num_transcoders;
15616
c4a1d9e4
CW
15617 struct intel_cursor_error_state {
15618 u32 control;
15619 u32 position;
15620 u32 base;
15621 u32 size;
52331309 15622 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15623
15624 struct intel_pipe_error_state {
ddf9c536 15625 bool power_domain_on;
c4a1d9e4 15626 u32 source;
f301b1e1 15627 u32 stat;
52331309 15628 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15629
15630 struct intel_plane_error_state {
15631 u32 control;
15632 u32 stride;
15633 u32 size;
15634 u32 pos;
15635 u32 addr;
15636 u32 surface;
15637 u32 tile_offset;
52331309 15638 } plane[I915_MAX_PIPES];
63b66e5b
CW
15639
15640 struct intel_transcoder_error_state {
ddf9c536 15641 bool power_domain_on;
63b66e5b
CW
15642 enum transcoder cpu_transcoder;
15643
15644 u32 conf;
15645
15646 u32 htotal;
15647 u32 hblank;
15648 u32 hsync;
15649 u32 vtotal;
15650 u32 vblank;
15651 u32 vsync;
15652 } transcoder[4];
c4a1d9e4
CW
15653};
15654
15655struct intel_display_error_state *
15656intel_display_capture_error_state(struct drm_device *dev)
15657{
fbee40df 15658 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 15659 struct intel_display_error_state *error;
63b66e5b
CW
15660 int transcoders[] = {
15661 TRANSCODER_A,
15662 TRANSCODER_B,
15663 TRANSCODER_C,
15664 TRANSCODER_EDP,
15665 };
c4a1d9e4
CW
15666 int i;
15667
63b66e5b
CW
15668 if (INTEL_INFO(dev)->num_pipes == 0)
15669 return NULL;
15670
9d1cb914 15671 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15672 if (error == NULL)
15673 return NULL;
15674
190be112 15675 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
15676 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15677
055e393f 15678 for_each_pipe(dev_priv, i) {
ddf9c536 15679 error->pipe[i].power_domain_on =
f458ebbc
DV
15680 __intel_display_power_is_enabled(dev_priv,
15681 POWER_DOMAIN_PIPE(i));
ddf9c536 15682 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15683 continue;
15684
5efb3e28
VS
15685 error->cursor[i].control = I915_READ(CURCNTR(i));
15686 error->cursor[i].position = I915_READ(CURPOS(i));
15687 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15688
15689 error->plane[i].control = I915_READ(DSPCNTR(i));
15690 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 15691 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 15692 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15693 error->plane[i].pos = I915_READ(DSPPOS(i));
15694 }
ca291363
PZ
15695 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15696 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
15697 if (INTEL_INFO(dev)->gen >= 4) {
15698 error->plane[i].surface = I915_READ(DSPSURF(i));
15699 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15700 }
15701
c4a1d9e4 15702 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15703
3abfce77 15704 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 15705 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15706 }
15707
15708 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15709 if (HAS_DDI(dev_priv->dev))
15710 error->num_transcoders++; /* Account for eDP. */
15711
15712 for (i = 0; i < error->num_transcoders; i++) {
15713 enum transcoder cpu_transcoder = transcoders[i];
15714
ddf9c536 15715 error->transcoder[i].power_domain_on =
f458ebbc 15716 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15717 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15718 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15719 continue;
15720
63b66e5b
CW
15721 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15722
15723 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15724 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15725 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15726 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15727 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15728 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15729 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15730 }
15731
15732 return error;
15733}
15734
edc3d884
MK
15735#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15736
c4a1d9e4 15737void
edc3d884 15738intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15739 struct drm_device *dev,
15740 struct intel_display_error_state *error)
15741{
055e393f 15742 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15743 int i;
15744
63b66e5b
CW
15745 if (!error)
15746 return;
15747
edc3d884 15748 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15749 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15750 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15751 error->power_well_driver);
055e393f 15752 for_each_pipe(dev_priv, i) {
edc3d884 15753 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
15754 err_printf(m, " Power: %s\n",
15755 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 15756 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15757 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15758
15759 err_printf(m, "Plane [%d]:\n", i);
15760 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15761 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15762 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15763 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15764 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15765 }
4b71a570 15766 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15767 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15768 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15769 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15770 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15771 }
15772
edc3d884
MK
15773 err_printf(m, "Cursor [%d]:\n", i);
15774 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15775 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15776 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15777 }
63b66e5b
CW
15778
15779 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 15780 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 15781 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
15782 err_printf(m, " Power: %s\n",
15783 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
15784 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15785 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15786 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15787 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15788 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15789 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15790 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15791 }
c4a1d9e4 15792}
e2fcdaa9
VS
15793
15794void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15795{
15796 struct intel_crtc *crtc;
15797
15798 for_each_intel_crtc(dev, crtc) {
15799 struct intel_unpin_work *work;
e2fcdaa9 15800
5e2d7afc 15801 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
15802
15803 work = crtc->unpin_work;
15804
15805 if (work && work->event &&
15806 work->event->base.file_priv == file) {
15807 kfree(work->event);
15808 work->event = NULL;
15809 }
15810
5e2d7afc 15811 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
15812 }
15813}