drm/i915: Call drm helpers when duplicating crtc and plane states
[deliverable/linux.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
e5510fac 39#include "i915_trace.h"
319c1d42 40#include <drm/drm_atomic.h>
c196e1d6 41#include <drm/drm_atomic_helper.h>
760285e7
DH
42#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
465c120c
MR
44#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
c0f372b3 46#include <linux/dma_remapping.h>
79e53945 47
465c120c
MR
48/* Primary plane formats supported by all gen */
49#define COMMON_PRIMARY_FORMATS \
50 DRM_FORMAT_C8, \
51 DRM_FORMAT_RGB565, \
52 DRM_FORMAT_XRGB8888, \
53 DRM_FORMAT_ARGB8888
54
55/* Primary plane formats for gen <= 3 */
56static const uint32_t intel_primary_formats_gen2[] = {
57 COMMON_PRIMARY_FORMATS,
58 DRM_FORMAT_XRGB1555,
59 DRM_FORMAT_ARGB1555,
60};
61
62/* Primary plane formats for gen >= 4 */
63static const uint32_t intel_primary_formats_gen4[] = {
64 COMMON_PRIMARY_FORMATS, \
65 DRM_FORMAT_XBGR8888,
66 DRM_FORMAT_ABGR8888,
67 DRM_FORMAT_XRGB2101010,
68 DRM_FORMAT_ARGB2101010,
69 DRM_FORMAT_XBGR2101010,
70 DRM_FORMAT_ABGR2101010,
71};
72
3d7d6510
MR
73/* Cursor formats */
74static const uint32_t intel_cursor_formats[] = {
75 DRM_FORMAT_ARGB8888,
76};
77
6b383a7f 78static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 79
f1f644dc 80static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 81 struct intel_crtc_state *pipe_config);
18442d08 82static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 83 struct intel_crtc_state *pipe_config);
f1f644dc 84
e7457a9a 85static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
83a57153
ACO
86 int x, int y, struct drm_framebuffer *old_fb,
87 struct drm_atomic_state *state);
eb1bfe80
JB
88static int intel_framebuffer_init(struct drm_device *dev,
89 struct intel_framebuffer *ifb,
90 struct drm_mode_fb_cmd2 *mode_cmd,
91 struct drm_i915_gem_object *obj);
5b18e57c
DV
92static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
93static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 94static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
95 struct intel_link_m_n *m_n,
96 struct intel_link_m_n *m2_n2);
29407aab 97static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
98static void haswell_set_pipeconf(struct drm_crtc *crtc);
99static void intel_set_pipe_csc(struct drm_crtc *crtc);
d288f65f 100static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 101 const struct intel_crtc_state *pipe_config);
d288f65f 102static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 103 const struct intel_crtc_state *pipe_config);
ea2c67bb
MR
104static void intel_begin_crtc_commit(struct drm_crtc *crtc);
105static void intel_finish_crtc_commit(struct drm_crtc *crtc);
549e2bfb
CK
106static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
107 struct intel_crtc_state *crtc_state);
5ab7b0b7
ID
108static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
109 int num_connectors);
ce22dba9
ML
110static void intel_crtc_enable_planes(struct drm_crtc *crtc);
111static void intel_crtc_disable_planes(struct drm_crtc *crtc);
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
e0638cdf
PZ
431/**
432 * Returns whether any output on the specified pipe is of the specified type
433 */
4093561b 434bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 435{
409ee761 436 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
437 struct intel_encoder *encoder;
438
409ee761 439 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
440 if (encoder->type == type)
441 return true;
442
443 return false;
444}
445
d0737e1d
ACO
446/**
447 * Returns whether any output on the specified pipe will have the specified
448 * type after a staged modeset is complete, i.e., the same as
449 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
450 * encoder->crtc.
451 */
a93e255f
ACO
452static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
453 int type)
d0737e1d 454{
a93e255f
ACO
455 struct drm_atomic_state *state = crtc_state->base.state;
456 struct drm_connector_state *connector_state;
d0737e1d 457 struct intel_encoder *encoder;
a93e255f
ACO
458 int i, num_connectors = 0;
459
460 for (i = 0; i < state->num_connector; i++) {
461 if (!state->connectors[i])
462 continue;
463
464 connector_state = state->connector_states[i];
465 if (connector_state->crtc != crtc_state->base.crtc)
466 continue;
467
468 num_connectors++;
d0737e1d 469
a93e255f
ACO
470 encoder = to_intel_encoder(connector_state->best_encoder);
471 if (encoder->type == type)
d0737e1d 472 return true;
a93e255f
ACO
473 }
474
475 WARN_ON(num_connectors == 0);
d0737e1d
ACO
476
477 return false;
478}
479
a93e255f
ACO
480static const intel_limit_t *
481intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
2c07245f 482{
a93e255f 483 struct drm_device *dev = crtc_state->base.crtc->dev;
2c07245f 484 const intel_limit_t *limit;
b91ad0ec 485
a93e255f 486 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 487 if (intel_is_dual_link_lvds(dev)) {
1b894b59 488 if (refclk == 100000)
b91ad0ec
ZW
489 limit = &intel_limits_ironlake_dual_lvds_100m;
490 else
491 limit = &intel_limits_ironlake_dual_lvds;
492 } else {
1b894b59 493 if (refclk == 100000)
b91ad0ec
ZW
494 limit = &intel_limits_ironlake_single_lvds_100m;
495 else
496 limit = &intel_limits_ironlake_single_lvds;
497 }
c6bb3538 498 } else
b91ad0ec 499 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
500
501 return limit;
502}
503
a93e255f
ACO
504static const intel_limit_t *
505intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 506{
a93e255f 507 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
508 const intel_limit_t *limit;
509
a93e255f 510 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 511 if (intel_is_dual_link_lvds(dev))
e4b36699 512 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 513 else
e4b36699 514 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
515 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
516 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 517 limit = &intel_limits_g4x_hdmi;
a93e255f 518 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 519 limit = &intel_limits_g4x_sdvo;
044c7c41 520 } else /* The option is for other outputs */
e4b36699 521 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
522
523 return limit;
524}
525
a93e255f
ACO
526static const intel_limit_t *
527intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 528{
a93e255f 529 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
530 const intel_limit_t *limit;
531
5ab7b0b7
ID
532 if (IS_BROXTON(dev))
533 limit = &intel_limits_bxt;
534 else if (HAS_PCH_SPLIT(dev))
a93e255f 535 limit = intel_ironlake_limit(crtc_state, refclk);
2c07245f 536 else if (IS_G4X(dev)) {
a93e255f 537 limit = intel_g4x_limit(crtc_state);
f2b115e6 538 } else if (IS_PINEVIEW(dev)) {
a93e255f 539 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 540 limit = &intel_limits_pineview_lvds;
2177832f 541 else
f2b115e6 542 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
543 } else if (IS_CHERRYVIEW(dev)) {
544 limit = &intel_limits_chv;
a0c4da24 545 } else if (IS_VALLEYVIEW(dev)) {
dc730512 546 limit = &intel_limits_vlv;
a6c45cf0 547 } else if (!IS_GEN2(dev)) {
a93e255f 548 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
549 limit = &intel_limits_i9xx_lvds;
550 else
551 limit = &intel_limits_i9xx_sdvo;
79e53945 552 } else {
a93e255f 553 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 554 limit = &intel_limits_i8xx_lvds;
a93e255f 555 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 556 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
557 else
558 limit = &intel_limits_i8xx_dac;
79e53945
JB
559 }
560 return limit;
561}
562
f2b115e6
AJ
563/* m1 is reserved as 0 in Pineview, n is a ring counter */
564static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 565{
2177832f
SL
566 clock->m = clock->m2 + 2;
567 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
568 if (WARN_ON(clock->n == 0 || clock->p == 0))
569 return;
fb03ac01
VS
570 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
571 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
572}
573
7429e9d4
DV
574static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
575{
576 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
577}
578
ac58c3f0 579static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 580{
7429e9d4 581 clock->m = i9xx_dpll_compute_m(clock);
79e53945 582 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
583 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
584 return;
fb03ac01
VS
585 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
586 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
587}
588
ef9348c8
CML
589static void chv_clock(int refclk, intel_clock_t *clock)
590{
591 clock->m = clock->m1 * clock->m2;
592 clock->p = clock->p1 * clock->p2;
593 if (WARN_ON(clock->n == 0 || clock->p == 0))
594 return;
595 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
596 clock->n << 22);
597 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
598}
599
7c04d1d9 600#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
601/**
602 * Returns whether the given set of divisors are valid for a given refclk with
603 * the given connectors.
604 */
605
1b894b59
CW
606static bool intel_PLL_is_valid(struct drm_device *dev,
607 const intel_limit_t *limit,
608 const intel_clock_t *clock)
79e53945 609{
f01b7962
VS
610 if (clock->n < limit->n.min || limit->n.max < clock->n)
611 INTELPllInvalid("n out of range\n");
79e53945 612 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 613 INTELPllInvalid("p1 out of range\n");
79e53945 614 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 615 INTELPllInvalid("m2 out of range\n");
79e53945 616 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 617 INTELPllInvalid("m1 out of range\n");
f01b7962 618
5ab7b0b7 619 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
620 if (clock->m1 <= clock->m2)
621 INTELPllInvalid("m1 <= m2\n");
622
5ab7b0b7 623 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
624 if (clock->p < limit->p.min || limit->p.max < clock->p)
625 INTELPllInvalid("p out of range\n");
626 if (clock->m < limit->m.min || limit->m.max < clock->m)
627 INTELPllInvalid("m out of range\n");
628 }
629
79e53945 630 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 631 INTELPllInvalid("vco out of range\n");
79e53945
JB
632 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
633 * connector, etc., rather than just a single range.
634 */
635 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 636 INTELPllInvalid("dot out of range\n");
79e53945
JB
637
638 return true;
639}
640
d4906093 641static bool
a93e255f
ACO
642i9xx_find_best_dpll(const intel_limit_t *limit,
643 struct intel_crtc_state *crtc_state,
cec2f356
SP
644 int target, int refclk, intel_clock_t *match_clock,
645 intel_clock_t *best_clock)
79e53945 646{
a93e255f 647 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 648 struct drm_device *dev = crtc->base.dev;
79e53945 649 intel_clock_t clock;
79e53945
JB
650 int err = target;
651
a93e255f 652 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 653 /*
a210b028
DV
654 * For LVDS just rely on its current settings for dual-channel.
655 * We haven't figured out how to reliably set up different
656 * single/dual channel state, if we even can.
79e53945 657 */
1974cad0 658 if (intel_is_dual_link_lvds(dev))
79e53945
JB
659 clock.p2 = limit->p2.p2_fast;
660 else
661 clock.p2 = limit->p2.p2_slow;
662 } else {
663 if (target < limit->p2.dot_limit)
664 clock.p2 = limit->p2.p2_slow;
665 else
666 clock.p2 = limit->p2.p2_fast;
667 }
668
0206e353 669 memset(best_clock, 0, sizeof(*best_clock));
79e53945 670
42158660
ZY
671 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
672 clock.m1++) {
673 for (clock.m2 = limit->m2.min;
674 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 675 if (clock.m2 >= clock.m1)
42158660
ZY
676 break;
677 for (clock.n = limit->n.min;
678 clock.n <= limit->n.max; clock.n++) {
679 for (clock.p1 = limit->p1.min;
680 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
681 int this_err;
682
ac58c3f0
DV
683 i9xx_clock(refclk, &clock);
684 if (!intel_PLL_is_valid(dev, limit,
685 &clock))
686 continue;
687 if (match_clock &&
688 clock.p != match_clock->p)
689 continue;
690
691 this_err = abs(clock.dot - target);
692 if (this_err < err) {
693 *best_clock = clock;
694 err = this_err;
695 }
696 }
697 }
698 }
699 }
700
701 return (err != target);
702}
703
704static bool
a93e255f
ACO
705pnv_find_best_dpll(const intel_limit_t *limit,
706 struct intel_crtc_state *crtc_state,
ee9300bb
DV
707 int target, int refclk, intel_clock_t *match_clock,
708 intel_clock_t *best_clock)
79e53945 709{
a93e255f 710 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 711 struct drm_device *dev = crtc->base.dev;
79e53945 712 intel_clock_t clock;
79e53945
JB
713 int err = target;
714
a93e255f 715 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 716 /*
a210b028
DV
717 * For LVDS just rely on its current settings for dual-channel.
718 * We haven't figured out how to reliably set up different
719 * single/dual channel state, if we even can.
79e53945 720 */
1974cad0 721 if (intel_is_dual_link_lvds(dev))
79e53945
JB
722 clock.p2 = limit->p2.p2_fast;
723 else
724 clock.p2 = limit->p2.p2_slow;
725 } else {
726 if (target < limit->p2.dot_limit)
727 clock.p2 = limit->p2.p2_slow;
728 else
729 clock.p2 = limit->p2.p2_fast;
730 }
731
0206e353 732 memset(best_clock, 0, sizeof(*best_clock));
79e53945 733
42158660
ZY
734 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
735 clock.m1++) {
736 for (clock.m2 = limit->m2.min;
737 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
738 for (clock.n = limit->n.min;
739 clock.n <= limit->n.max; clock.n++) {
740 for (clock.p1 = limit->p1.min;
741 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
742 int this_err;
743
ac58c3f0 744 pineview_clock(refclk, &clock);
1b894b59
CW
745 if (!intel_PLL_is_valid(dev, limit,
746 &clock))
79e53945 747 continue;
cec2f356
SP
748 if (match_clock &&
749 clock.p != match_clock->p)
750 continue;
79e53945
JB
751
752 this_err = abs(clock.dot - target);
753 if (this_err < err) {
754 *best_clock = clock;
755 err = this_err;
756 }
757 }
758 }
759 }
760 }
761
762 return (err != target);
763}
764
d4906093 765static bool
a93e255f
ACO
766g4x_find_best_dpll(const intel_limit_t *limit,
767 struct intel_crtc_state *crtc_state,
ee9300bb
DV
768 int target, int refclk, intel_clock_t *match_clock,
769 intel_clock_t *best_clock)
d4906093 770{
a93e255f 771 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 772 struct drm_device *dev = crtc->base.dev;
d4906093
ML
773 intel_clock_t clock;
774 int max_n;
775 bool found;
6ba770dc
AJ
776 /* approximately equals target * 0.00585 */
777 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
778 found = false;
779
a93e255f 780 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 781 if (intel_is_dual_link_lvds(dev))
d4906093
ML
782 clock.p2 = limit->p2.p2_fast;
783 else
784 clock.p2 = limit->p2.p2_slow;
785 } else {
786 if (target < limit->p2.dot_limit)
787 clock.p2 = limit->p2.p2_slow;
788 else
789 clock.p2 = limit->p2.p2_fast;
790 }
791
792 memset(best_clock, 0, sizeof(*best_clock));
793 max_n = limit->n.max;
f77f13e2 794 /* based on hardware requirement, prefer smaller n to precision */
d4906093 795 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 796 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
797 for (clock.m1 = limit->m1.max;
798 clock.m1 >= limit->m1.min; clock.m1--) {
799 for (clock.m2 = limit->m2.max;
800 clock.m2 >= limit->m2.min; clock.m2--) {
801 for (clock.p1 = limit->p1.max;
802 clock.p1 >= limit->p1.min; clock.p1--) {
803 int this_err;
804
ac58c3f0 805 i9xx_clock(refclk, &clock);
1b894b59
CW
806 if (!intel_PLL_is_valid(dev, limit,
807 &clock))
d4906093 808 continue;
1b894b59
CW
809
810 this_err = abs(clock.dot - target);
d4906093
ML
811 if (this_err < err_most) {
812 *best_clock = clock;
813 err_most = this_err;
814 max_n = clock.n;
815 found = true;
816 }
817 }
818 }
819 }
820 }
2c07245f
ZW
821 return found;
822}
823
d5dd62bd
ID
824/*
825 * Check if the calculated PLL configuration is more optimal compared to the
826 * best configuration and error found so far. Return the calculated error.
827 */
828static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
829 const intel_clock_t *calculated_clock,
830 const intel_clock_t *best_clock,
831 unsigned int best_error_ppm,
832 unsigned int *error_ppm)
833{
9ca3ba01
ID
834 /*
835 * For CHV ignore the error and consider only the P value.
836 * Prefer a bigger P value based on HW requirements.
837 */
838 if (IS_CHERRYVIEW(dev)) {
839 *error_ppm = 0;
840
841 return calculated_clock->p > best_clock->p;
842 }
843
24be4e46
ID
844 if (WARN_ON_ONCE(!target_freq))
845 return false;
846
d5dd62bd
ID
847 *error_ppm = div_u64(1000000ULL *
848 abs(target_freq - calculated_clock->dot),
849 target_freq);
850 /*
851 * Prefer a better P value over a better (smaller) error if the error
852 * is small. Ensure this preference for future configurations too by
853 * setting the error to 0.
854 */
855 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
856 *error_ppm = 0;
857
858 return true;
859 }
860
861 return *error_ppm + 10 < best_error_ppm;
862}
863
a0c4da24 864static bool
a93e255f
ACO
865vlv_find_best_dpll(const intel_limit_t *limit,
866 struct intel_crtc_state *crtc_state,
ee9300bb
DV
867 int target, int refclk, intel_clock_t *match_clock,
868 intel_clock_t *best_clock)
a0c4da24 869{
a93e255f 870 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 871 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 872 intel_clock_t clock;
69e4f900 873 unsigned int bestppm = 1000000;
27e639bf
VS
874 /* min update 19.2 MHz */
875 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 876 bool found = false;
a0c4da24 877
6b4bf1c4
VS
878 target *= 5; /* fast clock */
879
880 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
881
882 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 883 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 884 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 885 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 886 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 887 clock.p = clock.p1 * clock.p2;
a0c4da24 888 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 889 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 890 unsigned int ppm;
69e4f900 891
6b4bf1c4
VS
892 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
893 refclk * clock.m1);
894
895 vlv_clock(refclk, &clock);
43b0ac53 896
f01b7962
VS
897 if (!intel_PLL_is_valid(dev, limit,
898 &clock))
43b0ac53
VS
899 continue;
900
d5dd62bd
ID
901 if (!vlv_PLL_is_optimal(dev, target,
902 &clock,
903 best_clock,
904 bestppm, &ppm))
905 continue;
6b4bf1c4 906
d5dd62bd
ID
907 *best_clock = clock;
908 bestppm = ppm;
909 found = true;
a0c4da24
JB
910 }
911 }
912 }
913 }
a0c4da24 914
49e497ef 915 return found;
a0c4da24 916}
a4fc5ed6 917
ef9348c8 918static bool
a93e255f
ACO
919chv_find_best_dpll(const intel_limit_t *limit,
920 struct intel_crtc_state *crtc_state,
ef9348c8
CML
921 int target, int refclk, intel_clock_t *match_clock,
922 intel_clock_t *best_clock)
923{
a93e255f 924 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 925 struct drm_device *dev = crtc->base.dev;
9ca3ba01 926 unsigned int best_error_ppm;
ef9348c8
CML
927 intel_clock_t clock;
928 uint64_t m2;
929 int found = false;
930
931 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 932 best_error_ppm = 1000000;
ef9348c8
CML
933
934 /*
935 * Based on hardware doc, the n always set to 1, and m1 always
936 * set to 2. If requires to support 200Mhz refclk, we need to
937 * revisit this because n may not 1 anymore.
938 */
939 clock.n = 1, clock.m1 = 2;
940 target *= 5; /* fast clock */
941
942 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
943 for (clock.p2 = limit->p2.p2_fast;
944 clock.p2 >= limit->p2.p2_slow;
945 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 946 unsigned int error_ppm;
ef9348c8
CML
947
948 clock.p = clock.p1 * clock.p2;
949
950 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
951 clock.n) << 22, refclk * clock.m1);
952
953 if (m2 > INT_MAX/clock.m1)
954 continue;
955
956 clock.m2 = m2;
957
958 chv_clock(refclk, &clock);
959
960 if (!intel_PLL_is_valid(dev, limit, &clock))
961 continue;
962
9ca3ba01
ID
963 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
964 best_error_ppm, &error_ppm))
965 continue;
966
967 *best_clock = clock;
968 best_error_ppm = error_ppm;
969 found = true;
ef9348c8
CML
970 }
971 }
972
973 return found;
974}
975
5ab7b0b7
ID
976bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
977 intel_clock_t *best_clock)
978{
979 int refclk = i9xx_get_refclk(crtc_state, 0);
980
981 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
982 target_clock, refclk, NULL, best_clock);
983}
984
20ddf665
VS
985bool intel_crtc_active(struct drm_crtc *crtc)
986{
987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
988
989 /* Be paranoid as we can arrive here with only partial
990 * state retrieved from the hardware during setup.
991 *
241bfc38 992 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
993 * as Haswell has gained clock readout/fastboot support.
994 *
66e514c1 995 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 996 * properly reconstruct framebuffers.
c3d1f436
MR
997 *
998 * FIXME: The intel_crtc->active here should be switched to
999 * crtc->state->active once we have proper CRTC states wired up
1000 * for atomic.
20ddf665 1001 */
c3d1f436 1002 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 1003 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1004}
1005
a5c961d1
PZ
1006enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1007 enum pipe pipe)
1008{
1009 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1011
6e3c9717 1012 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1013}
1014
fbf49ea2
VS
1015static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1016{
1017 struct drm_i915_private *dev_priv = dev->dev_private;
1018 u32 reg = PIPEDSL(pipe);
1019 u32 line1, line2;
1020 u32 line_mask;
1021
1022 if (IS_GEN2(dev))
1023 line_mask = DSL_LINEMASK_GEN2;
1024 else
1025 line_mask = DSL_LINEMASK_GEN3;
1026
1027 line1 = I915_READ(reg) & line_mask;
1028 mdelay(5);
1029 line2 = I915_READ(reg) & line_mask;
1030
1031 return line1 == line2;
1032}
1033
ab7ad7f6
KP
1034/*
1035 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1036 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1037 *
1038 * After disabling a pipe, we can't wait for vblank in the usual way,
1039 * spinning on the vblank interrupt status bit, since we won't actually
1040 * see an interrupt when the pipe is disabled.
1041 *
ab7ad7f6
KP
1042 * On Gen4 and above:
1043 * wait for the pipe register state bit to turn off
1044 *
1045 * Otherwise:
1046 * wait for the display line value to settle (it usually
1047 * ends up stopping at the start of the next frame).
58e10eb9 1048 *
9d0498a2 1049 */
575f7ab7 1050static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1051{
575f7ab7 1052 struct drm_device *dev = crtc->base.dev;
9d0498a2 1053 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1054 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1055 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1056
1057 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 1058 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1059
1060 /* Wait for the Pipe State to go off */
58e10eb9
CW
1061 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1062 100))
284637d9 1063 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1064 } else {
ab7ad7f6 1065 /* Wait for the display line to settle */
fbf49ea2 1066 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1067 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1068 }
79e53945
JB
1069}
1070
b0ea7d37
DL
1071/*
1072 * ibx_digital_port_connected - is the specified port connected?
1073 * @dev_priv: i915 private structure
1074 * @port: the port to test
1075 *
1076 * Returns true if @port is connected, false otherwise.
1077 */
1078bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1079 struct intel_digital_port *port)
1080{
1081 u32 bit;
1082
c36346e3 1083 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1084 switch (port->port) {
c36346e3
DL
1085 case PORT_B:
1086 bit = SDE_PORTB_HOTPLUG;
1087 break;
1088 case PORT_C:
1089 bit = SDE_PORTC_HOTPLUG;
1090 break;
1091 case PORT_D:
1092 bit = SDE_PORTD_HOTPLUG;
1093 break;
1094 default:
1095 return true;
1096 }
1097 } else {
eba905b2 1098 switch (port->port) {
c36346e3
DL
1099 case PORT_B:
1100 bit = SDE_PORTB_HOTPLUG_CPT;
1101 break;
1102 case PORT_C:
1103 bit = SDE_PORTC_HOTPLUG_CPT;
1104 break;
1105 case PORT_D:
1106 bit = SDE_PORTD_HOTPLUG_CPT;
1107 break;
1108 default:
1109 return true;
1110 }
b0ea7d37
DL
1111 }
1112
1113 return I915_READ(SDEISR) & bit;
1114}
1115
b24e7179
JB
1116static const char *state_string(bool enabled)
1117{
1118 return enabled ? "on" : "off";
1119}
1120
1121/* Only for pre-ILK configs */
55607e8a
DV
1122void assert_pll(struct drm_i915_private *dev_priv,
1123 enum pipe pipe, bool state)
b24e7179
JB
1124{
1125 int reg;
1126 u32 val;
1127 bool cur_state;
1128
1129 reg = DPLL(pipe);
1130 val = I915_READ(reg);
1131 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1132 I915_STATE_WARN(cur_state != state,
b24e7179
JB
1133 "PLL state assertion failure (expected %s, current %s)\n",
1134 state_string(state), state_string(cur_state));
1135}
b24e7179 1136
23538ef1
JN
1137/* XXX: the dsi pll is shared between MIPI DSI ports */
1138static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1139{
1140 u32 val;
1141 bool cur_state;
1142
1143 mutex_lock(&dev_priv->dpio_lock);
1144 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1145 mutex_unlock(&dev_priv->dpio_lock);
1146
1147 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1148 I915_STATE_WARN(cur_state != state,
23538ef1
JN
1149 "DSI PLL state assertion failure (expected %s, current %s)\n",
1150 state_string(state), state_string(cur_state));
1151}
1152#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1153#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1154
55607e8a 1155struct intel_shared_dpll *
e2b78267
DV
1156intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1157{
1158 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1159
6e3c9717 1160 if (crtc->config->shared_dpll < 0)
e2b78267
DV
1161 return NULL;
1162
6e3c9717 1163 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
e2b78267
DV
1164}
1165
040484af 1166/* For ILK+ */
55607e8a
DV
1167void assert_shared_dpll(struct drm_i915_private *dev_priv,
1168 struct intel_shared_dpll *pll,
1169 bool state)
040484af 1170{
040484af 1171 bool cur_state;
5358901f 1172 struct intel_dpll_hw_state hw_state;
040484af 1173
92b27b08 1174 if (WARN (!pll,
46edb027 1175 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1176 return;
ee7b9f93 1177
5358901f 1178 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
e2c719b7 1179 I915_STATE_WARN(cur_state != state,
5358901f
DV
1180 "%s assertion failure (expected %s, current %s)\n",
1181 pll->name, state_string(state), state_string(cur_state));
040484af 1182}
040484af
JB
1183
1184static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1185 enum pipe pipe, bool state)
1186{
1187 int reg;
1188 u32 val;
1189 bool cur_state;
ad80a810
PZ
1190 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1191 pipe);
040484af 1192
affa9354
PZ
1193 if (HAS_DDI(dev_priv->dev)) {
1194 /* DDI does not have a specific FDI_TX register */
ad80a810 1195 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1196 val = I915_READ(reg);
ad80a810 1197 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1198 } else {
1199 reg = FDI_TX_CTL(pipe);
1200 val = I915_READ(reg);
1201 cur_state = !!(val & FDI_TX_ENABLE);
1202 }
e2c719b7 1203 I915_STATE_WARN(cur_state != state,
040484af
JB
1204 "FDI TX state assertion failure (expected %s, current %s)\n",
1205 state_string(state), state_string(cur_state));
1206}
1207#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1208#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1209
1210static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1211 enum pipe pipe, bool state)
1212{
1213 int reg;
1214 u32 val;
1215 bool cur_state;
1216
d63fa0dc
PZ
1217 reg = FDI_RX_CTL(pipe);
1218 val = I915_READ(reg);
1219 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1220 I915_STATE_WARN(cur_state != state,
040484af
JB
1221 "FDI RX state assertion failure (expected %s, current %s)\n",
1222 state_string(state), state_string(cur_state));
1223}
1224#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1225#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1226
1227static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1228 enum pipe pipe)
1229{
1230 int reg;
1231 u32 val;
1232
1233 /* ILK FDI PLL is always enabled */
3d13ef2e 1234 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1235 return;
1236
bf507ef7 1237 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1238 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1239 return;
1240
040484af
JB
1241 reg = FDI_TX_CTL(pipe);
1242 val = I915_READ(reg);
e2c719b7 1243 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1244}
1245
55607e8a
DV
1246void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1247 enum pipe pipe, bool state)
040484af
JB
1248{
1249 int reg;
1250 u32 val;
55607e8a 1251 bool cur_state;
040484af
JB
1252
1253 reg = FDI_RX_CTL(pipe);
1254 val = I915_READ(reg);
55607e8a 1255 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1256 I915_STATE_WARN(cur_state != state,
55607e8a
DV
1257 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1258 state_string(state), state_string(cur_state));
040484af
JB
1259}
1260
b680c37a
DV
1261void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1262 enum pipe pipe)
ea0760cf 1263{
bedd4dba
JN
1264 struct drm_device *dev = dev_priv->dev;
1265 int pp_reg;
ea0760cf
JB
1266 u32 val;
1267 enum pipe panel_pipe = PIPE_A;
0de3b485 1268 bool locked = true;
ea0760cf 1269
bedd4dba
JN
1270 if (WARN_ON(HAS_DDI(dev)))
1271 return;
1272
1273 if (HAS_PCH_SPLIT(dev)) {
1274 u32 port_sel;
1275
ea0760cf 1276 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1277 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1278
1279 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1280 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1281 panel_pipe = PIPE_B;
1282 /* XXX: else fix for eDP */
1283 } else if (IS_VALLEYVIEW(dev)) {
1284 /* presumably write lock depends on pipe, not port select */
1285 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1286 panel_pipe = pipe;
ea0760cf
JB
1287 } else {
1288 pp_reg = PP_CONTROL;
bedd4dba
JN
1289 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1290 panel_pipe = PIPE_B;
ea0760cf
JB
1291 }
1292
1293 val = I915_READ(pp_reg);
1294 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1295 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1296 locked = false;
1297
e2c719b7 1298 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1299 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1300 pipe_name(pipe));
ea0760cf
JB
1301}
1302
93ce0ba6
JN
1303static void assert_cursor(struct drm_i915_private *dev_priv,
1304 enum pipe pipe, bool state)
1305{
1306 struct drm_device *dev = dev_priv->dev;
1307 bool cur_state;
1308
d9d82081 1309 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1310 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1311 else
5efb3e28 1312 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1313
e2c719b7 1314 I915_STATE_WARN(cur_state != state,
93ce0ba6
JN
1315 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1316 pipe_name(pipe), state_string(state), state_string(cur_state));
1317}
1318#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1319#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1320
b840d907
JB
1321void assert_pipe(struct drm_i915_private *dev_priv,
1322 enum pipe pipe, bool state)
b24e7179
JB
1323{
1324 int reg;
1325 u32 val;
63d7bbe9 1326 bool cur_state;
702e7a56
PZ
1327 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1328 pipe);
b24e7179 1329
b6b5d049
VS
1330 /* if we need the pipe quirk it must be always on */
1331 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1332 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1333 state = true;
1334
f458ebbc 1335 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1336 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1337 cur_state = false;
1338 } else {
1339 reg = PIPECONF(cpu_transcoder);
1340 val = I915_READ(reg);
1341 cur_state = !!(val & PIPECONF_ENABLE);
1342 }
1343
e2c719b7 1344 I915_STATE_WARN(cur_state != state,
63d7bbe9 1345 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1346 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1347}
1348
931872fc
CW
1349static void assert_plane(struct drm_i915_private *dev_priv,
1350 enum plane plane, bool state)
b24e7179
JB
1351{
1352 int reg;
1353 u32 val;
931872fc 1354 bool cur_state;
b24e7179
JB
1355
1356 reg = DSPCNTR(plane);
1357 val = I915_READ(reg);
931872fc 1358 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1359 I915_STATE_WARN(cur_state != state,
931872fc
CW
1360 "plane %c assertion failure (expected %s, current %s)\n",
1361 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1362}
1363
931872fc
CW
1364#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1365#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1366
b24e7179
JB
1367static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1368 enum pipe pipe)
1369{
653e1026 1370 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1371 int reg, i;
1372 u32 val;
1373 int cur_pipe;
1374
653e1026
VS
1375 /* Primary planes are fixed to pipes on gen4+ */
1376 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1377 reg = DSPCNTR(pipe);
1378 val = I915_READ(reg);
e2c719b7 1379 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1380 "plane %c assertion failure, should be disabled but not\n",
1381 plane_name(pipe));
19ec1358 1382 return;
28c05794 1383 }
19ec1358 1384
b24e7179 1385 /* Need to check both planes against the pipe */
055e393f 1386 for_each_pipe(dev_priv, i) {
b24e7179
JB
1387 reg = DSPCNTR(i);
1388 val = I915_READ(reg);
1389 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1390 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1391 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1392 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1393 plane_name(i), pipe_name(pipe));
b24e7179
JB
1394 }
1395}
1396
19332d7a
JB
1397static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1398 enum pipe pipe)
1399{
20674eef 1400 struct drm_device *dev = dev_priv->dev;
1fe47785 1401 int reg, sprite;
19332d7a
JB
1402 u32 val;
1403
7feb8b88 1404 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1405 for_each_sprite(dev_priv, pipe, sprite) {
7feb8b88 1406 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1407 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1408 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1409 sprite, pipe_name(pipe));
1410 }
1411 } else if (IS_VALLEYVIEW(dev)) {
3bdcfc0c 1412 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 1413 reg = SPCNTR(pipe, sprite);
20674eef 1414 val = I915_READ(reg);
e2c719b7 1415 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1416 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1417 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1418 }
1419 } else if (INTEL_INFO(dev)->gen >= 7) {
1420 reg = SPRCTL(pipe);
19332d7a 1421 val = I915_READ(reg);
e2c719b7 1422 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1423 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1424 plane_name(pipe), pipe_name(pipe));
1425 } else if (INTEL_INFO(dev)->gen >= 5) {
1426 reg = DVSCNTR(pipe);
19332d7a 1427 val = I915_READ(reg);
e2c719b7 1428 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1429 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1430 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1431 }
1432}
1433
08c71e5e
VS
1434static void assert_vblank_disabled(struct drm_crtc *crtc)
1435{
e2c719b7 1436 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1437 drm_crtc_vblank_put(crtc);
1438}
1439
89eff4be 1440static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1441{
1442 u32 val;
1443 bool enabled;
1444
e2c719b7 1445 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1446
92f2584a
JB
1447 val = I915_READ(PCH_DREF_CONTROL);
1448 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1449 DREF_SUPERSPREAD_SOURCE_MASK));
e2c719b7 1450 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
92f2584a
JB
1451}
1452
ab9412ba
DV
1453static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1454 enum pipe pipe)
92f2584a
JB
1455{
1456 int reg;
1457 u32 val;
1458 bool enabled;
1459
ab9412ba 1460 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1461 val = I915_READ(reg);
1462 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1463 I915_STATE_WARN(enabled,
9db4a9c7
JB
1464 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1465 pipe_name(pipe));
92f2584a
JB
1466}
1467
4e634389
KP
1468static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1469 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1470{
1471 if ((val & DP_PORT_EN) == 0)
1472 return false;
1473
1474 if (HAS_PCH_CPT(dev_priv->dev)) {
1475 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1476 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1477 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1478 return false;
44f37d1f
CML
1479 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1480 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1481 return false;
f0575e92
KP
1482 } else {
1483 if ((val & DP_PIPE_MASK) != (pipe << 30))
1484 return false;
1485 }
1486 return true;
1487}
1488
1519b995
KP
1489static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1490 enum pipe pipe, u32 val)
1491{
dc0fa718 1492 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1493 return false;
1494
1495 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1496 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1497 return false;
44f37d1f
CML
1498 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1499 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1500 return false;
1519b995 1501 } else {
dc0fa718 1502 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1503 return false;
1504 }
1505 return true;
1506}
1507
1508static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1509 enum pipe pipe, u32 val)
1510{
1511 if ((val & LVDS_PORT_EN) == 0)
1512 return false;
1513
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 & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1519 return false;
1520 }
1521 return true;
1522}
1523
1524static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1525 enum pipe pipe, u32 val)
1526{
1527 if ((val & ADPA_DAC_ENABLE) == 0)
1528 return false;
1529 if (HAS_PCH_CPT(dev_priv->dev)) {
1530 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1531 return false;
1532 } else {
1533 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1534 return false;
1535 }
1536 return true;
1537}
1538
291906f1 1539static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1540 enum pipe pipe, int reg, u32 port_sel)
291906f1 1541{
47a05eca 1542 u32 val = I915_READ(reg);
e2c719b7 1543 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1544 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1545 reg, pipe_name(pipe));
de9a35ab 1546
e2c719b7 1547 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1548 && (val & DP_PIPEB_SELECT),
de9a35ab 1549 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1550}
1551
1552static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1553 enum pipe pipe, int reg)
1554{
47a05eca 1555 u32 val = I915_READ(reg);
e2c719b7 1556 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1557 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1558 reg, pipe_name(pipe));
de9a35ab 1559
e2c719b7 1560 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1561 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1562 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1563}
1564
1565static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1566 enum pipe pipe)
1567{
1568 int reg;
1569 u32 val;
291906f1 1570
f0575e92
KP
1571 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1572 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1573 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1574
1575 reg = PCH_ADPA;
1576 val = I915_READ(reg);
e2c719b7 1577 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1578 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1579 pipe_name(pipe));
291906f1
JB
1580
1581 reg = PCH_LVDS;
1582 val = I915_READ(reg);
e2c719b7 1583 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1584 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1585 pipe_name(pipe));
291906f1 1586
e2debe91
PZ
1587 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1588 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1589 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1590}
1591
40e9cf64
JB
1592static void intel_init_dpio(struct drm_device *dev)
1593{
1594 struct drm_i915_private *dev_priv = dev->dev_private;
1595
1596 if (!IS_VALLEYVIEW(dev))
1597 return;
1598
a09caddd
CML
1599 /*
1600 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1601 * CHV x1 PHY (DP/HDMI D)
1602 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1603 */
1604 if (IS_CHERRYVIEW(dev)) {
1605 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1606 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1607 } else {
1608 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1609 }
5382f5f3
JB
1610}
1611
d288f65f 1612static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1613 const struct intel_crtc_state *pipe_config)
87442f73 1614{
426115cf
DV
1615 struct drm_device *dev = crtc->base.dev;
1616 struct drm_i915_private *dev_priv = dev->dev_private;
1617 int reg = DPLL(crtc->pipe);
d288f65f 1618 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1619
426115cf 1620 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1621
1622 /* No really, not for ILK+ */
1623 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1624
1625 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1626 if (IS_MOBILE(dev_priv->dev))
426115cf 1627 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1628
426115cf
DV
1629 I915_WRITE(reg, dpll);
1630 POSTING_READ(reg);
1631 udelay(150);
1632
1633 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1634 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1635
d288f65f 1636 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1637 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1638
1639 /* We do this three times for luck */
426115cf 1640 I915_WRITE(reg, dpll);
87442f73
DV
1641 POSTING_READ(reg);
1642 udelay(150); /* wait for warmup */
426115cf 1643 I915_WRITE(reg, dpll);
87442f73
DV
1644 POSTING_READ(reg);
1645 udelay(150); /* wait for warmup */
426115cf 1646 I915_WRITE(reg, dpll);
87442f73
DV
1647 POSTING_READ(reg);
1648 udelay(150); /* wait for warmup */
1649}
1650
d288f65f 1651static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1652 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1653{
1654 struct drm_device *dev = crtc->base.dev;
1655 struct drm_i915_private *dev_priv = dev->dev_private;
1656 int pipe = crtc->pipe;
1657 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1658 u32 tmp;
1659
1660 assert_pipe_disabled(dev_priv, crtc->pipe);
1661
1662 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1663
1664 mutex_lock(&dev_priv->dpio_lock);
1665
1666 /* Enable back the 10bit clock to display controller */
1667 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1668 tmp |= DPIO_DCLKP_EN;
1669 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1670
1671 /*
1672 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1673 */
1674 udelay(1);
1675
1676 /* Enable PLL */
d288f65f 1677 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1678
1679 /* Check PLL is locked */
a11b0703 1680 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1681 DRM_ERROR("PLL %d failed to lock\n", pipe);
1682
a11b0703 1683 /* not sure when this should be written */
d288f65f 1684 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703
VS
1685 POSTING_READ(DPLL_MD(pipe));
1686
9d556c99
CML
1687 mutex_unlock(&dev_priv->dpio_lock);
1688}
1689
1c4e0274
VS
1690static int intel_num_dvo_pipes(struct drm_device *dev)
1691{
1692 struct intel_crtc *crtc;
1693 int count = 0;
1694
1695 for_each_intel_crtc(dev, crtc)
1696 count += crtc->active &&
409ee761 1697 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1698
1699 return count;
1700}
1701
66e3d5c0 1702static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1703{
66e3d5c0
DV
1704 struct drm_device *dev = crtc->base.dev;
1705 struct drm_i915_private *dev_priv = dev->dev_private;
1706 int reg = DPLL(crtc->pipe);
6e3c9717 1707 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1708
66e3d5c0 1709 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1710
63d7bbe9 1711 /* No really, not for ILK+ */
3d13ef2e 1712 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1713
1714 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1715 if (IS_MOBILE(dev) && !IS_I830(dev))
1716 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1717
1c4e0274
VS
1718 /* Enable DVO 2x clock on both PLLs if necessary */
1719 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1720 /*
1721 * It appears to be important that we don't enable this
1722 * for the current pipe before otherwise configuring the
1723 * PLL. No idea how this should be handled if multiple
1724 * DVO outputs are enabled simultaneosly.
1725 */
1726 dpll |= DPLL_DVO_2X_MODE;
1727 I915_WRITE(DPLL(!crtc->pipe),
1728 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1729 }
66e3d5c0
DV
1730
1731 /* Wait for the clocks to stabilize. */
1732 POSTING_READ(reg);
1733 udelay(150);
1734
1735 if (INTEL_INFO(dev)->gen >= 4) {
1736 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1737 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1738 } else {
1739 /* The pixel multiplier can only be updated once the
1740 * DPLL is enabled and the clocks are stable.
1741 *
1742 * So write it again.
1743 */
1744 I915_WRITE(reg, dpll);
1745 }
63d7bbe9
JB
1746
1747 /* We do this three times for luck */
66e3d5c0 1748 I915_WRITE(reg, dpll);
63d7bbe9
JB
1749 POSTING_READ(reg);
1750 udelay(150); /* wait for warmup */
66e3d5c0 1751 I915_WRITE(reg, dpll);
63d7bbe9
JB
1752 POSTING_READ(reg);
1753 udelay(150); /* wait for warmup */
66e3d5c0 1754 I915_WRITE(reg, dpll);
63d7bbe9
JB
1755 POSTING_READ(reg);
1756 udelay(150); /* wait for warmup */
1757}
1758
1759/**
50b44a44 1760 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1761 * @dev_priv: i915 private structure
1762 * @pipe: pipe PLL to disable
1763 *
1764 * Disable the PLL for @pipe, making sure the pipe is off first.
1765 *
1766 * Note! This is for pre-ILK only.
1767 */
1c4e0274 1768static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1769{
1c4e0274
VS
1770 struct drm_device *dev = crtc->base.dev;
1771 struct drm_i915_private *dev_priv = dev->dev_private;
1772 enum pipe pipe = crtc->pipe;
1773
1774 /* Disable DVO 2x clock on both PLLs if necessary */
1775 if (IS_I830(dev) &&
409ee761 1776 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
1c4e0274
VS
1777 intel_num_dvo_pipes(dev) == 1) {
1778 I915_WRITE(DPLL(PIPE_B),
1779 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1780 I915_WRITE(DPLL(PIPE_A),
1781 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1782 }
1783
b6b5d049
VS
1784 /* Don't disable pipe or pipe PLLs if needed */
1785 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1786 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1787 return;
1788
1789 /* Make sure the pipe isn't still relying on us */
1790 assert_pipe_disabled(dev_priv, pipe);
1791
50b44a44
DV
1792 I915_WRITE(DPLL(pipe), 0);
1793 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1794}
1795
f6071166
JB
1796static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1797{
1798 u32 val = 0;
1799
1800 /* Make sure the pipe isn't still relying on us */
1801 assert_pipe_disabled(dev_priv, pipe);
1802
e5cbfbfb
ID
1803 /*
1804 * Leave integrated clock source and reference clock enabled for pipe B.
1805 * The latter is needed for VGA hotplug / manual detection.
1806 */
f6071166 1807 if (pipe == PIPE_B)
e5cbfbfb 1808 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1809 I915_WRITE(DPLL(pipe), val);
1810 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1811
1812}
1813
1814static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1815{
d752048d 1816 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1817 u32 val;
1818
a11b0703
VS
1819 /* Make sure the pipe isn't still relying on us */
1820 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1821
a11b0703 1822 /* Set PLL en = 0 */
d17ec4ce 1823 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1824 if (pipe != PIPE_A)
1825 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1826 I915_WRITE(DPLL(pipe), val);
1827 POSTING_READ(DPLL(pipe));
d752048d
VS
1828
1829 mutex_lock(&dev_priv->dpio_lock);
1830
1831 /* Disable 10bit clock to display controller */
1832 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1833 val &= ~DPIO_DCLKP_EN;
1834 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1835
61407f6d
VS
1836 /* disable left/right clock distribution */
1837 if (pipe != PIPE_B) {
1838 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1839 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1840 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1841 } else {
1842 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1843 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1844 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1845 }
1846
d752048d 1847 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1848}
1849
e4607fcf
CML
1850void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1851 struct intel_digital_port *dport)
89b667f8
JB
1852{
1853 u32 port_mask;
00fc31b7 1854 int dpll_reg;
89b667f8 1855
e4607fcf
CML
1856 switch (dport->port) {
1857 case PORT_B:
89b667f8 1858 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1859 dpll_reg = DPLL(0);
e4607fcf
CML
1860 break;
1861 case PORT_C:
89b667f8 1862 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1863 dpll_reg = DPLL(0);
1864 break;
1865 case PORT_D:
1866 port_mask = DPLL_PORTD_READY_MASK;
1867 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1868 break;
1869 default:
1870 BUG();
1871 }
89b667f8 1872
00fc31b7 1873 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1874 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1875 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1876}
1877
b14b1055
DV
1878static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1879{
1880 struct drm_device *dev = crtc->base.dev;
1881 struct drm_i915_private *dev_priv = dev->dev_private;
1882 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1883
be19f0ff
CW
1884 if (WARN_ON(pll == NULL))
1885 return;
1886
3e369b76 1887 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1888 if (pll->active == 0) {
1889 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1890 WARN_ON(pll->on);
1891 assert_shared_dpll_disabled(dev_priv, pll);
1892
1893 pll->mode_set(dev_priv, pll);
1894 }
1895}
1896
92f2584a 1897/**
85b3894f 1898 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1899 * @dev_priv: i915 private structure
1900 * @pipe: pipe PLL to enable
1901 *
1902 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1903 * drives the transcoder clock.
1904 */
85b3894f 1905static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1906{
3d13ef2e
DL
1907 struct drm_device *dev = crtc->base.dev;
1908 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1909 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1910
87a875bb 1911 if (WARN_ON(pll == NULL))
48da64a8
CW
1912 return;
1913
3e369b76 1914 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1915 return;
ee7b9f93 1916
74dd6928 1917 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1918 pll->name, pll->active, pll->on,
e2b78267 1919 crtc->base.base.id);
92f2584a 1920
cdbd2316
DV
1921 if (pll->active++) {
1922 WARN_ON(!pll->on);
e9d6944e 1923 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1924 return;
1925 }
f4a091c7 1926 WARN_ON(pll->on);
ee7b9f93 1927
bd2bb1b9
PZ
1928 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1929
46edb027 1930 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1931 pll->enable(dev_priv, pll);
ee7b9f93 1932 pll->on = true;
92f2584a
JB
1933}
1934
f6daaec2 1935static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1936{
3d13ef2e
DL
1937 struct drm_device *dev = crtc->base.dev;
1938 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1939 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1940
92f2584a 1941 /* PCH only available on ILK+ */
3d13ef2e 1942 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1943 if (WARN_ON(pll == NULL))
ee7b9f93 1944 return;
92f2584a 1945
3e369b76 1946 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1947 return;
7a419866 1948
46edb027
DV
1949 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1950 pll->name, pll->active, pll->on,
e2b78267 1951 crtc->base.base.id);
7a419866 1952
48da64a8 1953 if (WARN_ON(pll->active == 0)) {
e9d6944e 1954 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1955 return;
1956 }
1957
e9d6944e 1958 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1959 WARN_ON(!pll->on);
cdbd2316 1960 if (--pll->active)
7a419866 1961 return;
ee7b9f93 1962
46edb027 1963 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1964 pll->disable(dev_priv, pll);
ee7b9f93 1965 pll->on = false;
bd2bb1b9
PZ
1966
1967 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1968}
1969
b8a4f404
PZ
1970static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1971 enum pipe pipe)
040484af 1972{
23670b32 1973 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1974 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1975 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1976 uint32_t reg, val, pipeconf_val;
040484af
JB
1977
1978 /* PCH only available on ILK+ */
55522f37 1979 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1980
1981 /* Make sure PCH DPLL is enabled */
e72f9fbf 1982 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1983 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1984
1985 /* FDI must be feeding us bits for PCH ports */
1986 assert_fdi_tx_enabled(dev_priv, pipe);
1987 assert_fdi_rx_enabled(dev_priv, pipe);
1988
23670b32
DV
1989 if (HAS_PCH_CPT(dev)) {
1990 /* Workaround: Set the timing override bit before enabling the
1991 * pch transcoder. */
1992 reg = TRANS_CHICKEN2(pipe);
1993 val = I915_READ(reg);
1994 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1995 I915_WRITE(reg, val);
59c859d6 1996 }
23670b32 1997
ab9412ba 1998 reg = PCH_TRANSCONF(pipe);
040484af 1999 val = I915_READ(reg);
5f7f726d 2000 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
2001
2002 if (HAS_PCH_IBX(dev_priv->dev)) {
2003 /*
2004 * make the BPC in transcoder be consistent with
2005 * that in pipeconf reg.
2006 */
dfd07d72
DV
2007 val &= ~PIPECONF_BPC_MASK;
2008 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 2009 }
5f7f726d
PZ
2010
2011 val &= ~TRANS_INTERLACE_MASK;
2012 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 2013 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 2014 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
2015 val |= TRANS_LEGACY_INTERLACED_ILK;
2016 else
2017 val |= TRANS_INTERLACED;
5f7f726d
PZ
2018 else
2019 val |= TRANS_PROGRESSIVE;
2020
040484af
JB
2021 I915_WRITE(reg, val | TRANS_ENABLE);
2022 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 2023 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
2024}
2025
8fb033d7 2026static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 2027 enum transcoder cpu_transcoder)
040484af 2028{
8fb033d7 2029 u32 val, pipeconf_val;
8fb033d7
PZ
2030
2031 /* PCH only available on ILK+ */
55522f37 2032 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 2033
8fb033d7 2034 /* FDI must be feeding us bits for PCH ports */
1a240d4d 2035 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 2036 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 2037
223a6fdf
PZ
2038 /* Workaround: set timing override bit. */
2039 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2040 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
2041 I915_WRITE(_TRANSA_CHICKEN2, val);
2042
25f3ef11 2043 val = TRANS_ENABLE;
937bb610 2044 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 2045
9a76b1c6
PZ
2046 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2047 PIPECONF_INTERLACED_ILK)
a35f2679 2048 val |= TRANS_INTERLACED;
8fb033d7
PZ
2049 else
2050 val |= TRANS_PROGRESSIVE;
2051
ab9412ba
DV
2052 I915_WRITE(LPT_TRANSCONF, val);
2053 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 2054 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
2055}
2056
b8a4f404
PZ
2057static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2058 enum pipe pipe)
040484af 2059{
23670b32
DV
2060 struct drm_device *dev = dev_priv->dev;
2061 uint32_t reg, val;
040484af
JB
2062
2063 /* FDI relies on the transcoder */
2064 assert_fdi_tx_disabled(dev_priv, pipe);
2065 assert_fdi_rx_disabled(dev_priv, pipe);
2066
291906f1
JB
2067 /* Ports must be off as well */
2068 assert_pch_ports_disabled(dev_priv, pipe);
2069
ab9412ba 2070 reg = PCH_TRANSCONF(pipe);
040484af
JB
2071 val = I915_READ(reg);
2072 val &= ~TRANS_ENABLE;
2073 I915_WRITE(reg, val);
2074 /* wait for PCH transcoder off, transcoder state */
2075 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 2076 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
2077
2078 if (!HAS_PCH_IBX(dev)) {
2079 /* Workaround: Clear the timing override chicken bit again. */
2080 reg = TRANS_CHICKEN2(pipe);
2081 val = I915_READ(reg);
2082 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2083 I915_WRITE(reg, val);
2084 }
040484af
JB
2085}
2086
ab4d966c 2087static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 2088{
8fb033d7
PZ
2089 u32 val;
2090
ab9412ba 2091 val = I915_READ(LPT_TRANSCONF);
8fb033d7 2092 val &= ~TRANS_ENABLE;
ab9412ba 2093 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2094 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2095 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2096 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2097
2098 /* Workaround: clear timing override bit. */
2099 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2100 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2101 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2102}
2103
b24e7179 2104/**
309cfea8 2105 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2106 * @crtc: crtc responsible for the pipe
b24e7179 2107 *
0372264a 2108 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2109 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2110 */
e1fdc473 2111static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2112{
0372264a
PZ
2113 struct drm_device *dev = crtc->base.dev;
2114 struct drm_i915_private *dev_priv = dev->dev_private;
2115 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2116 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2117 pipe);
1a240d4d 2118 enum pipe pch_transcoder;
b24e7179
JB
2119 int reg;
2120 u32 val;
2121
58c6eaa2 2122 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2123 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2124 assert_sprites_disabled(dev_priv, pipe);
2125
681e5811 2126 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2127 pch_transcoder = TRANSCODER_A;
2128 else
2129 pch_transcoder = pipe;
2130
b24e7179
JB
2131 /*
2132 * A pipe without a PLL won't actually be able to drive bits from
2133 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2134 * need the check.
2135 */
50360403 2136 if (HAS_GMCH_DISPLAY(dev_priv->dev))
409ee761 2137 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2138 assert_dsi_pll_enabled(dev_priv);
2139 else
2140 assert_pll_enabled(dev_priv, pipe);
040484af 2141 else {
6e3c9717 2142 if (crtc->config->has_pch_encoder) {
040484af 2143 /* if driving the PCH, we need FDI enabled */
cc391bbb 2144 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2145 assert_fdi_tx_pll_enabled(dev_priv,
2146 (enum pipe) cpu_transcoder);
040484af
JB
2147 }
2148 /* FIXME: assert CPU port conditions for SNB+ */
2149 }
b24e7179 2150
702e7a56 2151 reg = PIPECONF(cpu_transcoder);
b24e7179 2152 val = I915_READ(reg);
7ad25d48 2153 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2154 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2155 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2156 return;
7ad25d48 2157 }
00d70b15
CW
2158
2159 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2160 POSTING_READ(reg);
b24e7179
JB
2161}
2162
2163/**
309cfea8 2164 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2165 * @crtc: crtc whose pipes is to be disabled
b24e7179 2166 *
575f7ab7
VS
2167 * Disable the pipe of @crtc, making sure that various hardware
2168 * specific requirements are met, if applicable, e.g. plane
2169 * disabled, panel fitter off, etc.
b24e7179
JB
2170 *
2171 * Will wait until the pipe has shut down before returning.
2172 */
575f7ab7 2173static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2174{
575f7ab7 2175 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2176 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2177 enum pipe pipe = crtc->pipe;
b24e7179
JB
2178 int reg;
2179 u32 val;
2180
2181 /*
2182 * Make sure planes won't keep trying to pump pixels to us,
2183 * or we might hang the display.
2184 */
2185 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2186 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2187 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2188
702e7a56 2189 reg = PIPECONF(cpu_transcoder);
b24e7179 2190 val = I915_READ(reg);
00d70b15
CW
2191 if ((val & PIPECONF_ENABLE) == 0)
2192 return;
2193
67adc644
VS
2194 /*
2195 * Double wide has implications for planes
2196 * so best keep it disabled when not needed.
2197 */
6e3c9717 2198 if (crtc->config->double_wide)
67adc644
VS
2199 val &= ~PIPECONF_DOUBLE_WIDE;
2200
2201 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2202 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2203 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2204 val &= ~PIPECONF_ENABLE;
2205
2206 I915_WRITE(reg, val);
2207 if ((val & PIPECONF_ENABLE) == 0)
2208 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2209}
2210
d74362c9
KP
2211/*
2212 * Plane regs are double buffered, going from enabled->disabled needs a
2213 * trigger in order to latch. The display address reg provides this.
2214 */
1dba99f4
VS
2215void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2216 enum plane plane)
d74362c9 2217{
3d13ef2e
DL
2218 struct drm_device *dev = dev_priv->dev;
2219 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2220
2221 I915_WRITE(reg, I915_READ(reg));
2222 POSTING_READ(reg);
d74362c9
KP
2223}
2224
b24e7179 2225/**
262ca2b0 2226 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2227 * @plane: plane to be enabled
2228 * @crtc: crtc for the plane
b24e7179 2229 *
fdd508a6 2230 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2231 */
fdd508a6
VS
2232static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2233 struct drm_crtc *crtc)
b24e7179 2234{
fdd508a6
VS
2235 struct drm_device *dev = plane->dev;
2236 struct drm_i915_private *dev_priv = dev->dev_private;
2237 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2238
2239 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2240 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b70709a6 2241 to_intel_plane_state(plane->state)->visible = true;
939c2fe8 2242
fdd508a6
VS
2243 dev_priv->display.update_primary_plane(crtc, plane->fb,
2244 crtc->x, crtc->y);
b24e7179
JB
2245}
2246
693db184
CW
2247static bool need_vtd_wa(struct drm_device *dev)
2248{
2249#ifdef CONFIG_INTEL_IOMMU
2250 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2251 return true;
2252#endif
2253 return false;
2254}
2255
50470bb0 2256unsigned int
6761dd31
TU
2257intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2258 uint64_t fb_format_modifier)
a57ce0b2 2259{
6761dd31
TU
2260 unsigned int tile_height;
2261 uint32_t pixel_bytes;
a57ce0b2 2262
b5d0e9bf
DL
2263 switch (fb_format_modifier) {
2264 case DRM_FORMAT_MOD_NONE:
2265 tile_height = 1;
2266 break;
2267 case I915_FORMAT_MOD_X_TILED:
2268 tile_height = IS_GEN2(dev) ? 16 : 8;
2269 break;
2270 case I915_FORMAT_MOD_Y_TILED:
2271 tile_height = 32;
2272 break;
2273 case I915_FORMAT_MOD_Yf_TILED:
6761dd31
TU
2274 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2275 switch (pixel_bytes) {
b5d0e9bf 2276 default:
6761dd31 2277 case 1:
b5d0e9bf
DL
2278 tile_height = 64;
2279 break;
6761dd31
TU
2280 case 2:
2281 case 4:
b5d0e9bf
DL
2282 tile_height = 32;
2283 break;
6761dd31 2284 case 8:
b5d0e9bf
DL
2285 tile_height = 16;
2286 break;
6761dd31 2287 case 16:
b5d0e9bf
DL
2288 WARN_ONCE(1,
2289 "128-bit pixels are not supported for display!");
2290 tile_height = 16;
2291 break;
2292 }
2293 break;
2294 default:
2295 MISSING_CASE(fb_format_modifier);
2296 tile_height = 1;
2297 break;
2298 }
091df6cb 2299
6761dd31
TU
2300 return tile_height;
2301}
2302
2303unsigned int
2304intel_fb_align_height(struct drm_device *dev, unsigned int height,
2305 uint32_t pixel_format, uint64_t fb_format_modifier)
2306{
2307 return ALIGN(height, intel_tile_height(dev, pixel_format,
2308 fb_format_modifier));
a57ce0b2
JB
2309}
2310
f64b98cd
TU
2311static int
2312intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2313 const struct drm_plane_state *plane_state)
2314{
50470bb0 2315 struct intel_rotation_info *info = &view->rotation_info;
50470bb0 2316
f64b98cd
TU
2317 *view = i915_ggtt_view_normal;
2318
50470bb0
TU
2319 if (!plane_state)
2320 return 0;
2321
121920fa 2322 if (!intel_rotation_90_or_270(plane_state->rotation))
50470bb0
TU
2323 return 0;
2324
9abc4648 2325 *view = i915_ggtt_view_rotated;
50470bb0
TU
2326
2327 info->height = fb->height;
2328 info->pixel_format = fb->pixel_format;
2329 info->pitch = fb->pitches[0];
2330 info->fb_modifier = fb->modifier[0];
2331
f64b98cd
TU
2332 return 0;
2333}
2334
127bd2ac 2335int
850c4cdc
TU
2336intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2337 struct drm_framebuffer *fb,
82bc3b2d 2338 const struct drm_plane_state *plane_state,
a4872ba6 2339 struct intel_engine_cs *pipelined)
6b95a207 2340{
850c4cdc 2341 struct drm_device *dev = fb->dev;
ce453d81 2342 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2343 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2344 struct i915_ggtt_view view;
6b95a207
KH
2345 u32 alignment;
2346 int ret;
2347
ebcdd39e
MR
2348 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2349
7b911adc
TU
2350 switch (fb->modifier[0]) {
2351 case DRM_FORMAT_MOD_NONE:
1fada4cc
DL
2352 if (INTEL_INFO(dev)->gen >= 9)
2353 alignment = 256 * 1024;
2354 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
534843da 2355 alignment = 128 * 1024;
a6c45cf0 2356 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2357 alignment = 4 * 1024;
2358 else
2359 alignment = 64 * 1024;
6b95a207 2360 break;
7b911adc 2361 case I915_FORMAT_MOD_X_TILED:
1fada4cc
DL
2362 if (INTEL_INFO(dev)->gen >= 9)
2363 alignment = 256 * 1024;
2364 else {
2365 /* pin() will align the object as required by fence */
2366 alignment = 0;
2367 }
6b95a207 2368 break;
7b911adc 2369 case I915_FORMAT_MOD_Y_TILED:
1327b9a1
DL
2370 case I915_FORMAT_MOD_Yf_TILED:
2371 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2372 "Y tiling bo slipped through, driver bug!\n"))
2373 return -EINVAL;
2374 alignment = 1 * 1024 * 1024;
2375 break;
6b95a207 2376 default:
7b911adc
TU
2377 MISSING_CASE(fb->modifier[0]);
2378 return -EINVAL;
6b95a207
KH
2379 }
2380
f64b98cd
TU
2381 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2382 if (ret)
2383 return ret;
2384
693db184
CW
2385 /* Note that the w/a also requires 64 PTE of padding following the
2386 * bo. We currently fill all unused PTE with the shadow page and so
2387 * we should always have valid PTE following the scanout preventing
2388 * the VT-d warning.
2389 */
2390 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2391 alignment = 256 * 1024;
2392
d6dd6843
PZ
2393 /*
2394 * Global gtt pte registers are special registers which actually forward
2395 * writes to a chunk of system memory. Which means that there is no risk
2396 * that the register values disappear as soon as we call
2397 * intel_runtime_pm_put(), so it is correct to wrap only the
2398 * pin/unpin/fence and not more.
2399 */
2400 intel_runtime_pm_get(dev_priv);
2401
ce453d81 2402 dev_priv->mm.interruptible = false;
e6617330 2403 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
f64b98cd 2404 &view);
48b956c5 2405 if (ret)
ce453d81 2406 goto err_interruptible;
6b95a207
KH
2407
2408 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2409 * fence, whereas 965+ only requires a fence if using
2410 * framebuffer compression. For simplicity, we always install
2411 * a fence as the cost is not that onerous.
2412 */
06d98131 2413 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2414 if (ret)
2415 goto err_unpin;
1690e1eb 2416
9a5a53b3 2417 i915_gem_object_pin_fence(obj);
6b95a207 2418
ce453d81 2419 dev_priv->mm.interruptible = true;
d6dd6843 2420 intel_runtime_pm_put(dev_priv);
6b95a207 2421 return 0;
48b956c5
CW
2422
2423err_unpin:
f64b98cd 2424 i915_gem_object_unpin_from_display_plane(obj, &view);
ce453d81
CW
2425err_interruptible:
2426 dev_priv->mm.interruptible = true;
d6dd6843 2427 intel_runtime_pm_put(dev_priv);
48b956c5 2428 return ret;
6b95a207
KH
2429}
2430
82bc3b2d
TU
2431static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2432 const struct drm_plane_state *plane_state)
1690e1eb 2433{
82bc3b2d 2434 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd
TU
2435 struct i915_ggtt_view view;
2436 int ret;
82bc3b2d 2437
ebcdd39e
MR
2438 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2439
f64b98cd
TU
2440 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2441 WARN_ONCE(ret, "Couldn't get view from plane state!");
2442
1690e1eb 2443 i915_gem_object_unpin_fence(obj);
f64b98cd 2444 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2445}
2446
c2c75131
DV
2447/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2448 * is assumed to be a power-of-two. */
bc752862
CW
2449unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2450 unsigned int tiling_mode,
2451 unsigned int cpp,
2452 unsigned int pitch)
c2c75131 2453{
bc752862
CW
2454 if (tiling_mode != I915_TILING_NONE) {
2455 unsigned int tile_rows, tiles;
c2c75131 2456
bc752862
CW
2457 tile_rows = *y / 8;
2458 *y %= 8;
c2c75131 2459
bc752862
CW
2460 tiles = *x / (512/cpp);
2461 *x %= 512/cpp;
2462
2463 return tile_rows * pitch * 8 + tiles * 4096;
2464 } else {
2465 unsigned int offset;
2466
2467 offset = *y * pitch + *x * cpp;
2468 *y = 0;
2469 *x = (offset & 4095) / cpp;
2470 return offset & -4096;
2471 }
c2c75131
DV
2472}
2473
b35d63fa 2474static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2475{
2476 switch (format) {
2477 case DISPPLANE_8BPP:
2478 return DRM_FORMAT_C8;
2479 case DISPPLANE_BGRX555:
2480 return DRM_FORMAT_XRGB1555;
2481 case DISPPLANE_BGRX565:
2482 return DRM_FORMAT_RGB565;
2483 default:
2484 case DISPPLANE_BGRX888:
2485 return DRM_FORMAT_XRGB8888;
2486 case DISPPLANE_RGBX888:
2487 return DRM_FORMAT_XBGR8888;
2488 case DISPPLANE_BGRX101010:
2489 return DRM_FORMAT_XRGB2101010;
2490 case DISPPLANE_RGBX101010:
2491 return DRM_FORMAT_XBGR2101010;
2492 }
2493}
2494
bc8d7dff
DL
2495static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2496{
2497 switch (format) {
2498 case PLANE_CTL_FORMAT_RGB_565:
2499 return DRM_FORMAT_RGB565;
2500 default:
2501 case PLANE_CTL_FORMAT_XRGB_8888:
2502 if (rgb_order) {
2503 if (alpha)
2504 return DRM_FORMAT_ABGR8888;
2505 else
2506 return DRM_FORMAT_XBGR8888;
2507 } else {
2508 if (alpha)
2509 return DRM_FORMAT_ARGB8888;
2510 else
2511 return DRM_FORMAT_XRGB8888;
2512 }
2513 case PLANE_CTL_FORMAT_XRGB_2101010:
2514 if (rgb_order)
2515 return DRM_FORMAT_XBGR2101010;
2516 else
2517 return DRM_FORMAT_XRGB2101010;
2518 }
2519}
2520
5724dbd1 2521static bool
f6936e29
DV
2522intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2523 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2524{
2525 struct drm_device *dev = crtc->base.dev;
2526 struct drm_i915_gem_object *obj = NULL;
2527 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2528 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2529 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2530 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2531 PAGE_SIZE);
2532
2533 size_aligned -= base_aligned;
46f297fb 2534
ff2652ea
CW
2535 if (plane_config->size == 0)
2536 return false;
2537
f37b5c2b
DV
2538 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2539 base_aligned,
2540 base_aligned,
2541 size_aligned);
46f297fb 2542 if (!obj)
484b41dd 2543 return false;
46f297fb 2544
49af449b
DL
2545 obj->tiling_mode = plane_config->tiling;
2546 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2547 obj->stride = fb->pitches[0];
46f297fb 2548
6bf129df
DL
2549 mode_cmd.pixel_format = fb->pixel_format;
2550 mode_cmd.width = fb->width;
2551 mode_cmd.height = fb->height;
2552 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2553 mode_cmd.modifier[0] = fb->modifier[0];
2554 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb
JB
2555
2556 mutex_lock(&dev->struct_mutex);
6bf129df 2557 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2558 &mode_cmd, obj)) {
46f297fb
JB
2559 DRM_DEBUG_KMS("intel fb init failed\n");
2560 goto out_unref_obj;
2561 }
46f297fb 2562 mutex_unlock(&dev->struct_mutex);
484b41dd 2563
f6936e29 2564 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2565 return true;
46f297fb
JB
2566
2567out_unref_obj:
2568 drm_gem_object_unreference(&obj->base);
2569 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2570 return false;
2571}
2572
afd65eb4
MR
2573/* Update plane->state->fb to match plane->fb after driver-internal updates */
2574static void
2575update_state_fb(struct drm_plane *plane)
2576{
2577 if (plane->fb == plane->state->fb)
2578 return;
2579
2580 if (plane->state->fb)
2581 drm_framebuffer_unreference(plane->state->fb);
2582 plane->state->fb = plane->fb;
2583 if (plane->state->fb)
2584 drm_framebuffer_reference(plane->state->fb);
2585}
2586
5724dbd1 2587static void
f6936e29
DV
2588intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2589 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2590{
2591 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2592 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2593 struct drm_crtc *c;
2594 struct intel_crtc *i;
2ff8fde1 2595 struct drm_i915_gem_object *obj;
88595ac9
DV
2596 struct drm_plane *primary = intel_crtc->base.primary;
2597 struct drm_framebuffer *fb;
484b41dd 2598
2d14030b 2599 if (!plane_config->fb)
484b41dd
JB
2600 return;
2601
f6936e29 2602 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2603 fb = &plane_config->fb->base;
2604 goto valid_fb;
f55548b5 2605 }
484b41dd 2606
2d14030b 2607 kfree(plane_config->fb);
484b41dd
JB
2608
2609 /*
2610 * Failed to alloc the obj, check to see if we should share
2611 * an fb with another CRTC instead
2612 */
70e1e0ec 2613 for_each_crtc(dev, c) {
484b41dd
JB
2614 i = to_intel_crtc(c);
2615
2616 if (c == &intel_crtc->base)
2617 continue;
2618
2ff8fde1
MR
2619 if (!i->active)
2620 continue;
2621
88595ac9
DV
2622 fb = c->primary->fb;
2623 if (!fb)
484b41dd
JB
2624 continue;
2625
88595ac9 2626 obj = intel_fb_obj(fb);
2ff8fde1 2627 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2628 drm_framebuffer_reference(fb);
2629 goto valid_fb;
484b41dd
JB
2630 }
2631 }
88595ac9
DV
2632
2633 return;
2634
2635valid_fb:
2636 obj = intel_fb_obj(fb);
2637 if (obj->tiling_mode != I915_TILING_NONE)
2638 dev_priv->preserve_bios_swizzle = true;
2639
2640 primary->fb = fb;
2641 primary->state->crtc = &intel_crtc->base;
2642 primary->crtc = &intel_crtc->base;
2643 update_state_fb(primary);
2644 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
46f297fb
JB
2645}
2646
29b9bde6
DV
2647static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2648 struct drm_framebuffer *fb,
2649 int x, int y)
81255565
JB
2650{
2651 struct drm_device *dev = crtc->dev;
2652 struct drm_i915_private *dev_priv = dev->dev_private;
2653 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2654 struct drm_plane *primary = crtc->primary;
2655 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2656 struct drm_i915_gem_object *obj;
81255565 2657 int plane = intel_crtc->plane;
e506a0c6 2658 unsigned long linear_offset;
81255565 2659 u32 dspcntr;
f45651ba 2660 u32 reg = DSPCNTR(plane);
48404c1e 2661 int pixel_size;
f45651ba 2662
b70709a6 2663 if (!visible || !fb) {
fdd508a6
VS
2664 I915_WRITE(reg, 0);
2665 if (INTEL_INFO(dev)->gen >= 4)
2666 I915_WRITE(DSPSURF(plane), 0);
2667 else
2668 I915_WRITE(DSPADDR(plane), 0);
2669 POSTING_READ(reg);
2670 return;
2671 }
2672
c9ba6fad
VS
2673 obj = intel_fb_obj(fb);
2674 if (WARN_ON(obj == NULL))
2675 return;
2676
2677 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2678
f45651ba
VS
2679 dspcntr = DISPPLANE_GAMMA_ENABLE;
2680
fdd508a6 2681 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2682
2683 if (INTEL_INFO(dev)->gen < 4) {
2684 if (intel_crtc->pipe == PIPE_B)
2685 dspcntr |= DISPPLANE_SEL_PIPE_B;
2686
2687 /* pipesrc and dspsize control the size that is scaled from,
2688 * which should always be the user's requested size.
2689 */
2690 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2691 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2692 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2693 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2694 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2695 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2696 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2697 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2698 I915_WRITE(PRIMPOS(plane), 0);
2699 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2700 }
81255565 2701
57779d06
VS
2702 switch (fb->pixel_format) {
2703 case DRM_FORMAT_C8:
81255565
JB
2704 dspcntr |= DISPPLANE_8BPP;
2705 break;
57779d06
VS
2706 case DRM_FORMAT_XRGB1555:
2707 case DRM_FORMAT_ARGB1555:
2708 dspcntr |= DISPPLANE_BGRX555;
81255565 2709 break;
57779d06
VS
2710 case DRM_FORMAT_RGB565:
2711 dspcntr |= DISPPLANE_BGRX565;
2712 break;
2713 case DRM_FORMAT_XRGB8888:
2714 case DRM_FORMAT_ARGB8888:
2715 dspcntr |= DISPPLANE_BGRX888;
2716 break;
2717 case DRM_FORMAT_XBGR8888:
2718 case DRM_FORMAT_ABGR8888:
2719 dspcntr |= DISPPLANE_RGBX888;
2720 break;
2721 case DRM_FORMAT_XRGB2101010:
2722 case DRM_FORMAT_ARGB2101010:
2723 dspcntr |= DISPPLANE_BGRX101010;
2724 break;
2725 case DRM_FORMAT_XBGR2101010:
2726 case DRM_FORMAT_ABGR2101010:
2727 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2728 break;
2729 default:
baba133a 2730 BUG();
81255565 2731 }
57779d06 2732
f45651ba
VS
2733 if (INTEL_INFO(dev)->gen >= 4 &&
2734 obj->tiling_mode != I915_TILING_NONE)
2735 dspcntr |= DISPPLANE_TILED;
81255565 2736
de1aa629
VS
2737 if (IS_G4X(dev))
2738 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2739
b9897127 2740 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2741
c2c75131
DV
2742 if (INTEL_INFO(dev)->gen >= 4) {
2743 intel_crtc->dspaddr_offset =
bc752862 2744 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2745 pixel_size,
bc752862 2746 fb->pitches[0]);
c2c75131
DV
2747 linear_offset -= intel_crtc->dspaddr_offset;
2748 } else {
e506a0c6 2749 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2750 }
e506a0c6 2751
8e7d688b 2752 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2753 dspcntr |= DISPPLANE_ROTATE_180;
2754
6e3c9717
ACO
2755 x += (intel_crtc->config->pipe_src_w - 1);
2756 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2757
2758 /* Finding the last pixel of the last line of the display
2759 data and adding to linear_offset*/
2760 linear_offset +=
6e3c9717
ACO
2761 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2762 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2763 }
2764
2765 I915_WRITE(reg, dspcntr);
2766
01f2c773 2767 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2768 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2769 I915_WRITE(DSPSURF(plane),
2770 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2771 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2772 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2773 } else
f343c5f6 2774 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2775 POSTING_READ(reg);
17638cd6
JB
2776}
2777
29b9bde6
DV
2778static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2779 struct drm_framebuffer *fb,
2780 int x, int y)
17638cd6
JB
2781{
2782 struct drm_device *dev = crtc->dev;
2783 struct drm_i915_private *dev_priv = dev->dev_private;
2784 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2785 struct drm_plane *primary = crtc->primary;
2786 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2787 struct drm_i915_gem_object *obj;
17638cd6 2788 int plane = intel_crtc->plane;
e506a0c6 2789 unsigned long linear_offset;
17638cd6 2790 u32 dspcntr;
f45651ba 2791 u32 reg = DSPCNTR(plane);
48404c1e 2792 int pixel_size;
f45651ba 2793
b70709a6 2794 if (!visible || !fb) {
fdd508a6
VS
2795 I915_WRITE(reg, 0);
2796 I915_WRITE(DSPSURF(plane), 0);
2797 POSTING_READ(reg);
2798 return;
2799 }
2800
c9ba6fad
VS
2801 obj = intel_fb_obj(fb);
2802 if (WARN_ON(obj == NULL))
2803 return;
2804
2805 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2806
f45651ba
VS
2807 dspcntr = DISPPLANE_GAMMA_ENABLE;
2808
fdd508a6 2809 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2810
2811 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2812 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2813
57779d06
VS
2814 switch (fb->pixel_format) {
2815 case DRM_FORMAT_C8:
17638cd6
JB
2816 dspcntr |= DISPPLANE_8BPP;
2817 break;
57779d06
VS
2818 case DRM_FORMAT_RGB565:
2819 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2820 break;
57779d06
VS
2821 case DRM_FORMAT_XRGB8888:
2822 case DRM_FORMAT_ARGB8888:
2823 dspcntr |= DISPPLANE_BGRX888;
2824 break;
2825 case DRM_FORMAT_XBGR8888:
2826 case DRM_FORMAT_ABGR8888:
2827 dspcntr |= DISPPLANE_RGBX888;
2828 break;
2829 case DRM_FORMAT_XRGB2101010:
2830 case DRM_FORMAT_ARGB2101010:
2831 dspcntr |= DISPPLANE_BGRX101010;
2832 break;
2833 case DRM_FORMAT_XBGR2101010:
2834 case DRM_FORMAT_ABGR2101010:
2835 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2836 break;
2837 default:
baba133a 2838 BUG();
17638cd6
JB
2839 }
2840
2841 if (obj->tiling_mode != I915_TILING_NONE)
2842 dspcntr |= DISPPLANE_TILED;
17638cd6 2843
f45651ba 2844 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2845 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2846
b9897127 2847 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2848 intel_crtc->dspaddr_offset =
bc752862 2849 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2850 pixel_size,
bc752862 2851 fb->pitches[0]);
c2c75131 2852 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2853 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2854 dspcntr |= DISPPLANE_ROTATE_180;
2855
2856 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2857 x += (intel_crtc->config->pipe_src_w - 1);
2858 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2859
2860 /* Finding the last pixel of the last line of the display
2861 data and adding to linear_offset*/
2862 linear_offset +=
6e3c9717
ACO
2863 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2864 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2865 }
2866 }
2867
2868 I915_WRITE(reg, dspcntr);
17638cd6 2869
01f2c773 2870 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2871 I915_WRITE(DSPSURF(plane),
2872 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2873 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2874 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2875 } else {
2876 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2877 I915_WRITE(DSPLINOFF(plane), linear_offset);
2878 }
17638cd6 2879 POSTING_READ(reg);
17638cd6
JB
2880}
2881
b321803d
DL
2882u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2883 uint32_t pixel_format)
2884{
2885 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2886
2887 /*
2888 * The stride is either expressed as a multiple of 64 bytes
2889 * chunks for linear buffers or in number of tiles for tiled
2890 * buffers.
2891 */
2892 switch (fb_modifier) {
2893 case DRM_FORMAT_MOD_NONE:
2894 return 64;
2895 case I915_FORMAT_MOD_X_TILED:
2896 if (INTEL_INFO(dev)->gen == 2)
2897 return 128;
2898 return 512;
2899 case I915_FORMAT_MOD_Y_TILED:
2900 /* No need to check for old gens and Y tiling since this is
2901 * about the display engine and those will be blocked before
2902 * we get here.
2903 */
2904 return 128;
2905 case I915_FORMAT_MOD_Yf_TILED:
2906 if (bits_per_pixel == 8)
2907 return 64;
2908 else
2909 return 128;
2910 default:
2911 MISSING_CASE(fb_modifier);
2912 return 64;
2913 }
2914}
2915
121920fa
TU
2916unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2917 struct drm_i915_gem_object *obj)
2918{
9abc4648 2919 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
121920fa
TU
2920
2921 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
9abc4648 2922 view = &i915_ggtt_view_rotated;
121920fa
TU
2923
2924 return i915_gem_obj_ggtt_offset_view(obj, view);
2925}
2926
a1b2278e
CK
2927/*
2928 * This function detaches (aka. unbinds) unused scalers in hardware
2929 */
2930void skl_detach_scalers(struct intel_crtc *intel_crtc)
2931{
2932 struct drm_device *dev;
2933 struct drm_i915_private *dev_priv;
2934 struct intel_crtc_scaler_state *scaler_state;
2935 int i;
2936
2937 if (!intel_crtc || !intel_crtc->config)
2938 return;
2939
2940 dev = intel_crtc->base.dev;
2941 dev_priv = dev->dev_private;
2942 scaler_state = &intel_crtc->config->scaler_state;
2943
2944 /* loop through and disable scalers that aren't in use */
2945 for (i = 0; i < intel_crtc->num_scalers; i++) {
2946 if (!scaler_state->scalers[i].in_use) {
2947 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2948 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2949 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2950 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2951 intel_crtc->base.base.id, intel_crtc->pipe, i);
2952 }
2953 }
2954}
2955
6156a456 2956u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2957{
6156a456
CK
2958 u32 plane_ctl_format = 0;
2959 switch (pixel_format) {
70d21f0e 2960 case DRM_FORMAT_RGB565:
6156a456 2961 plane_ctl_format = PLANE_CTL_FORMAT_RGB_565;
f75fb42a 2962 break;
70d21f0e 2963 case DRM_FORMAT_XBGR8888:
6156a456
CK
2964 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
2965 break;
2966 case DRM_FORMAT_XRGB8888:
2967 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888;
70d21f0e 2968 break;
6156a456
CK
2969 /*
2970 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2971 * to be already pre-multiplied. We need to add a knob (or a different
2972 * DRM_FORMAT) for user-space to configure that.
2973 */
f75fb42a 2974 case DRM_FORMAT_ABGR8888:
6156a456
CK
2975 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
2976 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2977 break;
2978 case DRM_FORMAT_ARGB8888:
2979 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 |
2980 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
f75fb42a 2981 break;
70d21f0e 2982 case DRM_FORMAT_XRGB2101010:
6156a456 2983 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e
DL
2984 break;
2985 case DRM_FORMAT_XBGR2101010:
6156a456
CK
2986 plane_ctl_format = PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
2987 break;
2988 case DRM_FORMAT_YUYV:
2989 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
2990 break;
2991 case DRM_FORMAT_YVYU:
2992 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
2993 break;
2994 case DRM_FORMAT_UYVY:
2995 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
2996 break;
2997 case DRM_FORMAT_VYUY:
2998 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e
DL
2999 break;
3000 default:
3001 BUG();
3002 }
6156a456
CK
3003 return plane_ctl_format;
3004}
70d21f0e 3005
6156a456
CK
3006u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3007{
3008 u32 plane_ctl_tiling = 0;
3009 switch (fb_modifier) {
30af77c4 3010 case DRM_FORMAT_MOD_NONE:
70d21f0e 3011 break;
30af77c4 3012 case I915_FORMAT_MOD_X_TILED:
6156a456 3013 plane_ctl_tiling = PLANE_CTL_TILED_X;
b321803d
DL
3014 break;
3015 case I915_FORMAT_MOD_Y_TILED:
6156a456 3016 plane_ctl_tiling = PLANE_CTL_TILED_Y;
b321803d
DL
3017 break;
3018 case I915_FORMAT_MOD_Yf_TILED:
6156a456 3019 plane_ctl_tiling = PLANE_CTL_TILED_YF;
70d21f0e
DL
3020 break;
3021 default:
6156a456 3022 MISSING_CASE(fb_modifier);
70d21f0e 3023 }
6156a456
CK
3024 return plane_ctl_tiling;
3025}
70d21f0e 3026
6156a456
CK
3027u32 skl_plane_ctl_rotation(unsigned int rotation)
3028{
3029 u32 plane_ctl_rotation = 0;
3b7a5119 3030 switch (rotation) {
6156a456
CK
3031 case BIT(DRM_ROTATE_0):
3032 break;
3b7a5119 3033 case BIT(DRM_ROTATE_90):
6156a456 3034 plane_ctl_rotation = PLANE_CTL_ROTATE_90;
3b7a5119 3035 break;
3b7a5119 3036 case BIT(DRM_ROTATE_180):
6156a456 3037 plane_ctl_rotation = PLANE_CTL_ROTATE_180;
3b7a5119 3038 break;
3b7a5119 3039 case BIT(DRM_ROTATE_270):
6156a456 3040 plane_ctl_rotation = PLANE_CTL_ROTATE_270;
3b7a5119 3041 break;
6156a456
CK
3042 default:
3043 MISSING_CASE(rotation);
3044 }
3045
3046 return plane_ctl_rotation;
3047}
3048
3049static void skylake_update_primary_plane(struct drm_crtc *crtc,
3050 struct drm_framebuffer *fb,
3051 int x, int y)
3052{
3053 struct drm_device *dev = crtc->dev;
3054 struct drm_i915_private *dev_priv = dev->dev_private;
3055 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
3056 struct drm_plane *plane = crtc->primary;
3057 bool visible = to_intel_plane_state(plane->state)->visible;
6156a456
CK
3058 struct drm_i915_gem_object *obj;
3059 int pipe = intel_crtc->pipe;
3060 u32 plane_ctl, stride_div, stride;
3061 u32 tile_height, plane_offset, plane_size;
3062 unsigned int rotation;
3063 int x_offset, y_offset;
3064 unsigned long surf_addr;
6156a456
CK
3065 struct intel_crtc_state *crtc_state = intel_crtc->config;
3066 struct intel_plane_state *plane_state;
3067 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3068 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3069 int scaler_id = -1;
3070
6156a456
CK
3071 plane_state = to_intel_plane_state(plane->state);
3072
b70709a6 3073 if (!visible || !fb) {
6156a456
CK
3074 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3075 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3076 POSTING_READ(PLANE_CTL(pipe, 0));
3077 return;
3b7a5119 3078 }
70d21f0e 3079
6156a456
CK
3080 plane_ctl = PLANE_CTL_ENABLE |
3081 PLANE_CTL_PIPE_GAMMA_ENABLE |
3082 PLANE_CTL_PIPE_CSC_ENABLE;
3083
3084 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3085 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3086 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3087
3088 rotation = plane->state->rotation;
3089 plane_ctl |= skl_plane_ctl_rotation(rotation);
3090
b321803d
DL
3091 obj = intel_fb_obj(fb);
3092 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3093 fb->pixel_format);
3b7a5119
SJ
3094 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3095
6156a456
CK
3096 /*
3097 * FIXME: intel_plane_state->src, dst aren't set when transitional
3098 * update_plane helpers are called from legacy paths.
3099 * Once full atomic crtc is available, below check can be avoided.
3100 */
3101 if (drm_rect_width(&plane_state->src)) {
3102 scaler_id = plane_state->scaler_id;
3103 src_x = plane_state->src.x1 >> 16;
3104 src_y = plane_state->src.y1 >> 16;
3105 src_w = drm_rect_width(&plane_state->src) >> 16;
3106 src_h = drm_rect_height(&plane_state->src) >> 16;
3107 dst_x = plane_state->dst.x1;
3108 dst_y = plane_state->dst.y1;
3109 dst_w = drm_rect_width(&plane_state->dst);
3110 dst_h = drm_rect_height(&plane_state->dst);
3111
3112 WARN_ON(x != src_x || y != src_y);
3113 } else {
3114 src_w = intel_crtc->config->pipe_src_w;
3115 src_h = intel_crtc->config->pipe_src_h;
3116 }
3117
3b7a5119
SJ
3118 if (intel_rotation_90_or_270(rotation)) {
3119 /* stride = Surface height in tiles */
3120 tile_height = intel_tile_height(dev, fb->bits_per_pixel,
3121 fb->modifier[0]);
3122 stride = DIV_ROUND_UP(fb->height, tile_height);
6156a456 3123 x_offset = stride * tile_height - y - src_h;
3b7a5119 3124 y_offset = x;
6156a456 3125 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3126 } else {
3127 stride = fb->pitches[0] / stride_div;
3128 x_offset = x;
3129 y_offset = y;
6156a456 3130 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3131 }
3132 plane_offset = y_offset << 16 | x_offset;
b321803d 3133
70d21f0e 3134 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3135 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3136 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3137 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3138
3139 if (scaler_id >= 0) {
3140 uint32_t ps_ctrl = 0;
3141
3142 WARN_ON(!dst_w || !dst_h);
3143 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3144 crtc_state->scaler_state.scalers[scaler_id].mode;
3145 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3146 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3147 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3148 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3149 I915_WRITE(PLANE_POS(pipe, 0), 0);
3150 } else {
3151 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3152 }
3153
121920fa 3154 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3155
3156 POSTING_READ(PLANE_SURF(pipe, 0));
3157}
3158
17638cd6
JB
3159/* Assume fb object is pinned & idle & fenced and just update base pointers */
3160static int
3161intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3162 int x, int y, enum mode_set_atomic state)
3163{
3164 struct drm_device *dev = crtc->dev;
3165 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 3166
6b8e6ed0
CW
3167 if (dev_priv->display.disable_fbc)
3168 dev_priv->display.disable_fbc(dev);
81255565 3169
29b9bde6
DV
3170 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3171
3172 return 0;
81255565
JB
3173}
3174
7514747d 3175static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3176{
96a02917
VS
3177 struct drm_crtc *crtc;
3178
70e1e0ec 3179 for_each_crtc(dev, crtc) {
96a02917
VS
3180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3181 enum plane plane = intel_crtc->plane;
3182
3183 intel_prepare_page_flip(dev, plane);
3184 intel_finish_page_flip_plane(dev, plane);
3185 }
7514747d
VS
3186}
3187
3188static void intel_update_primary_planes(struct drm_device *dev)
3189{
3190 struct drm_i915_private *dev_priv = dev->dev_private;
3191 struct drm_crtc *crtc;
96a02917 3192
70e1e0ec 3193 for_each_crtc(dev, crtc) {
96a02917
VS
3194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3195
51fd371b 3196 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
3197 /*
3198 * FIXME: Once we have proper support for primary planes (and
3199 * disabling them without disabling the entire crtc) allow again
66e514c1 3200 * a NULL crtc->primary->fb.
947fdaad 3201 */
f4510a27 3202 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 3203 dev_priv->display.update_primary_plane(crtc,
66e514c1 3204 crtc->primary->fb,
262ca2b0
MR
3205 crtc->x,
3206 crtc->y);
51fd371b 3207 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
3208 }
3209}
3210
ce22dba9
ML
3211void intel_crtc_reset(struct intel_crtc *crtc)
3212{
3213 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3214
3215 if (!crtc->active)
3216 return;
3217
3218 intel_crtc_disable_planes(&crtc->base);
3219 dev_priv->display.crtc_disable(&crtc->base);
3220 dev_priv->display.crtc_enable(&crtc->base);
3221 intel_crtc_enable_planes(&crtc->base);
3222}
3223
7514747d
VS
3224void intel_prepare_reset(struct drm_device *dev)
3225{
f98ce92f
VS
3226 struct drm_i915_private *dev_priv = to_i915(dev);
3227 struct intel_crtc *crtc;
3228
7514747d
VS
3229 /* no reset support for gen2 */
3230 if (IS_GEN2(dev))
3231 return;
3232
3233 /* reset doesn't touch the display */
3234 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3235 return;
3236
3237 drm_modeset_lock_all(dev);
f98ce92f
VS
3238
3239 /*
3240 * Disabling the crtcs gracefully seems nicer. Also the
3241 * g33 docs say we should at least disable all the planes.
3242 */
3243 for_each_intel_crtc(dev, crtc) {
ce22dba9
ML
3244 if (!crtc->active)
3245 continue;
3246
3247 intel_crtc_disable_planes(&crtc->base);
3248 dev_priv->display.crtc_disable(&crtc->base);
f98ce92f 3249 }
7514747d
VS
3250}
3251
3252void intel_finish_reset(struct drm_device *dev)
3253{
3254 struct drm_i915_private *dev_priv = to_i915(dev);
3255
3256 /*
3257 * Flips in the rings will be nuked by the reset,
3258 * so complete all pending flips so that user space
3259 * will get its events and not get stuck.
3260 */
3261 intel_complete_page_flips(dev);
3262
3263 /* no reset support for gen2 */
3264 if (IS_GEN2(dev))
3265 return;
3266
3267 /* reset doesn't touch the display */
3268 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3269 /*
3270 * Flips in the rings have been nuked by the reset,
3271 * so update the base address of all primary
3272 * planes to the the last fb to make sure we're
3273 * showing the correct fb after a reset.
3274 */
3275 intel_update_primary_planes(dev);
3276 return;
3277 }
3278
3279 /*
3280 * The display has been reset as well,
3281 * so need a full re-initialization.
3282 */
3283 intel_runtime_pm_disable_interrupts(dev_priv);
3284 intel_runtime_pm_enable_interrupts(dev_priv);
3285
3286 intel_modeset_init_hw(dev);
3287
3288 spin_lock_irq(&dev_priv->irq_lock);
3289 if (dev_priv->display.hpd_irq_setup)
3290 dev_priv->display.hpd_irq_setup(dev);
3291 spin_unlock_irq(&dev_priv->irq_lock);
3292
3293 intel_modeset_setup_hw_state(dev, true);
3294
3295 intel_hpd_init(dev_priv);
3296
3297 drm_modeset_unlock_all(dev);
3298}
3299
14667a4b
CW
3300static int
3301intel_finish_fb(struct drm_framebuffer *old_fb)
3302{
2ff8fde1 3303 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
14667a4b
CW
3304 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3305 bool was_interruptible = dev_priv->mm.interruptible;
3306 int ret;
3307
14667a4b
CW
3308 /* Big Hammer, we also need to ensure that any pending
3309 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3310 * current scanout is retired before unpinning the old
3311 * framebuffer.
3312 *
3313 * This should only fail upon a hung GPU, in which case we
3314 * can safely continue.
3315 */
3316 dev_priv->mm.interruptible = false;
3317 ret = i915_gem_object_finish_gpu(obj);
3318 dev_priv->mm.interruptible = was_interruptible;
3319
3320 return ret;
3321}
3322
7d5e3799
CW
3323static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3324{
3325 struct drm_device *dev = crtc->dev;
3326 struct drm_i915_private *dev_priv = dev->dev_private;
3327 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3328 bool pending;
3329
3330 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3331 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3332 return false;
3333
5e2d7afc 3334 spin_lock_irq(&dev->event_lock);
7d5e3799 3335 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3336 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3337
3338 return pending;
3339}
3340
e30e8f75
GP
3341static void intel_update_pipe_size(struct intel_crtc *crtc)
3342{
3343 struct drm_device *dev = crtc->base.dev;
3344 struct drm_i915_private *dev_priv = dev->dev_private;
3345 const struct drm_display_mode *adjusted_mode;
3346
3347 if (!i915.fastboot)
3348 return;
3349
3350 /*
3351 * Update pipe size and adjust fitter if needed: the reason for this is
3352 * that in compute_mode_changes we check the native mode (not the pfit
3353 * mode) to see if we can flip rather than do a full mode set. In the
3354 * fastboot case, we'll flip, but if we don't update the pipesrc and
3355 * pfit state, we'll end up with a big fb scanned out into the wrong
3356 * sized surface.
3357 *
3358 * To fix this properly, we need to hoist the checks up into
3359 * compute_mode_changes (or above), check the actual pfit state and
3360 * whether the platform allows pfit disable with pipe active, and only
3361 * then update the pipesrc and pfit state, even on the flip path.
3362 */
3363
6e3c9717 3364 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
3365
3366 I915_WRITE(PIPESRC(crtc->pipe),
3367 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3368 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 3369 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
3370 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3371 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3372 I915_WRITE(PF_CTL(crtc->pipe), 0);
3373 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3374 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3375 }
6e3c9717
ACO
3376 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3377 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3378}
3379
5e84e1a4
ZW
3380static void intel_fdi_normal_train(struct drm_crtc *crtc)
3381{
3382 struct drm_device *dev = crtc->dev;
3383 struct drm_i915_private *dev_priv = dev->dev_private;
3384 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3385 int pipe = intel_crtc->pipe;
3386 u32 reg, temp;
3387
3388 /* enable normal train */
3389 reg = FDI_TX_CTL(pipe);
3390 temp = I915_READ(reg);
61e499bf 3391 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3392 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3393 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3394 } else {
3395 temp &= ~FDI_LINK_TRAIN_NONE;
3396 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3397 }
5e84e1a4
ZW
3398 I915_WRITE(reg, temp);
3399
3400 reg = FDI_RX_CTL(pipe);
3401 temp = I915_READ(reg);
3402 if (HAS_PCH_CPT(dev)) {
3403 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3404 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3405 } else {
3406 temp &= ~FDI_LINK_TRAIN_NONE;
3407 temp |= FDI_LINK_TRAIN_NONE;
3408 }
3409 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3410
3411 /* wait one idle pattern time */
3412 POSTING_READ(reg);
3413 udelay(1000);
357555c0
JB
3414
3415 /* IVB wants error correction enabled */
3416 if (IS_IVYBRIDGE(dev))
3417 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3418 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3419}
3420
8db9d77b
ZW
3421/* The FDI link training functions for ILK/Ibexpeak. */
3422static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3423{
3424 struct drm_device *dev = crtc->dev;
3425 struct drm_i915_private *dev_priv = dev->dev_private;
3426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3427 int pipe = intel_crtc->pipe;
5eddb70b 3428 u32 reg, temp, tries;
8db9d77b 3429
1c8562f6 3430 /* FDI needs bits from pipe first */
0fc932b8 3431 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3432
e1a44743
AJ
3433 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3434 for train result */
5eddb70b
CW
3435 reg = FDI_RX_IMR(pipe);
3436 temp = I915_READ(reg);
e1a44743
AJ
3437 temp &= ~FDI_RX_SYMBOL_LOCK;
3438 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3439 I915_WRITE(reg, temp);
3440 I915_READ(reg);
e1a44743
AJ
3441 udelay(150);
3442
8db9d77b 3443 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3444 reg = FDI_TX_CTL(pipe);
3445 temp = I915_READ(reg);
627eb5a3 3446 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3447 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3448 temp &= ~FDI_LINK_TRAIN_NONE;
3449 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3450 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3451
5eddb70b
CW
3452 reg = FDI_RX_CTL(pipe);
3453 temp = I915_READ(reg);
8db9d77b
ZW
3454 temp &= ~FDI_LINK_TRAIN_NONE;
3455 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3456 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3457
3458 POSTING_READ(reg);
8db9d77b
ZW
3459 udelay(150);
3460
5b2adf89 3461 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3462 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3463 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3464 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3465
5eddb70b 3466 reg = FDI_RX_IIR(pipe);
e1a44743 3467 for (tries = 0; tries < 5; tries++) {
5eddb70b 3468 temp = I915_READ(reg);
8db9d77b
ZW
3469 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3470
3471 if ((temp & FDI_RX_BIT_LOCK)) {
3472 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3473 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3474 break;
3475 }
8db9d77b 3476 }
e1a44743 3477 if (tries == 5)
5eddb70b 3478 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3479
3480 /* Train 2 */
5eddb70b
CW
3481 reg = FDI_TX_CTL(pipe);
3482 temp = I915_READ(reg);
8db9d77b
ZW
3483 temp &= ~FDI_LINK_TRAIN_NONE;
3484 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3485 I915_WRITE(reg, temp);
8db9d77b 3486
5eddb70b
CW
3487 reg = FDI_RX_CTL(pipe);
3488 temp = I915_READ(reg);
8db9d77b
ZW
3489 temp &= ~FDI_LINK_TRAIN_NONE;
3490 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3491 I915_WRITE(reg, temp);
8db9d77b 3492
5eddb70b
CW
3493 POSTING_READ(reg);
3494 udelay(150);
8db9d77b 3495
5eddb70b 3496 reg = FDI_RX_IIR(pipe);
e1a44743 3497 for (tries = 0; tries < 5; tries++) {
5eddb70b 3498 temp = I915_READ(reg);
8db9d77b
ZW
3499 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3500
3501 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3502 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3503 DRM_DEBUG_KMS("FDI train 2 done.\n");
3504 break;
3505 }
8db9d77b 3506 }
e1a44743 3507 if (tries == 5)
5eddb70b 3508 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3509
3510 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3511
8db9d77b
ZW
3512}
3513
0206e353 3514static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3515 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3516 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3517 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3518 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3519};
3520
3521/* The FDI link training functions for SNB/Cougarpoint. */
3522static void gen6_fdi_link_train(struct drm_crtc *crtc)
3523{
3524 struct drm_device *dev = crtc->dev;
3525 struct drm_i915_private *dev_priv = dev->dev_private;
3526 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3527 int pipe = intel_crtc->pipe;
fa37d39e 3528 u32 reg, temp, i, retry;
8db9d77b 3529
e1a44743
AJ
3530 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3531 for train result */
5eddb70b
CW
3532 reg = FDI_RX_IMR(pipe);
3533 temp = I915_READ(reg);
e1a44743
AJ
3534 temp &= ~FDI_RX_SYMBOL_LOCK;
3535 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3536 I915_WRITE(reg, temp);
3537
3538 POSTING_READ(reg);
e1a44743
AJ
3539 udelay(150);
3540
8db9d77b 3541 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3542 reg = FDI_TX_CTL(pipe);
3543 temp = I915_READ(reg);
627eb5a3 3544 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3545 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3546 temp &= ~FDI_LINK_TRAIN_NONE;
3547 temp |= FDI_LINK_TRAIN_PATTERN_1;
3548 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3549 /* SNB-B */
3550 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3551 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3552
d74cf324
DV
3553 I915_WRITE(FDI_RX_MISC(pipe),
3554 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3555
5eddb70b
CW
3556 reg = FDI_RX_CTL(pipe);
3557 temp = I915_READ(reg);
8db9d77b
ZW
3558 if (HAS_PCH_CPT(dev)) {
3559 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3560 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3561 } else {
3562 temp &= ~FDI_LINK_TRAIN_NONE;
3563 temp |= FDI_LINK_TRAIN_PATTERN_1;
3564 }
5eddb70b
CW
3565 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3566
3567 POSTING_READ(reg);
8db9d77b
ZW
3568 udelay(150);
3569
0206e353 3570 for (i = 0; i < 4; i++) {
5eddb70b
CW
3571 reg = FDI_TX_CTL(pipe);
3572 temp = I915_READ(reg);
8db9d77b
ZW
3573 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3574 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3575 I915_WRITE(reg, temp);
3576
3577 POSTING_READ(reg);
8db9d77b
ZW
3578 udelay(500);
3579
fa37d39e
SP
3580 for (retry = 0; retry < 5; retry++) {
3581 reg = FDI_RX_IIR(pipe);
3582 temp = I915_READ(reg);
3583 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3584 if (temp & FDI_RX_BIT_LOCK) {
3585 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3586 DRM_DEBUG_KMS("FDI train 1 done.\n");
3587 break;
3588 }
3589 udelay(50);
8db9d77b 3590 }
fa37d39e
SP
3591 if (retry < 5)
3592 break;
8db9d77b
ZW
3593 }
3594 if (i == 4)
5eddb70b 3595 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3596
3597 /* Train 2 */
5eddb70b
CW
3598 reg = FDI_TX_CTL(pipe);
3599 temp = I915_READ(reg);
8db9d77b
ZW
3600 temp &= ~FDI_LINK_TRAIN_NONE;
3601 temp |= FDI_LINK_TRAIN_PATTERN_2;
3602 if (IS_GEN6(dev)) {
3603 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3604 /* SNB-B */
3605 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3606 }
5eddb70b 3607 I915_WRITE(reg, temp);
8db9d77b 3608
5eddb70b
CW
3609 reg = FDI_RX_CTL(pipe);
3610 temp = I915_READ(reg);
8db9d77b
ZW
3611 if (HAS_PCH_CPT(dev)) {
3612 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3613 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3614 } else {
3615 temp &= ~FDI_LINK_TRAIN_NONE;
3616 temp |= FDI_LINK_TRAIN_PATTERN_2;
3617 }
5eddb70b
CW
3618 I915_WRITE(reg, temp);
3619
3620 POSTING_READ(reg);
8db9d77b
ZW
3621 udelay(150);
3622
0206e353 3623 for (i = 0; i < 4; i++) {
5eddb70b
CW
3624 reg = FDI_TX_CTL(pipe);
3625 temp = I915_READ(reg);
8db9d77b
ZW
3626 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3627 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3628 I915_WRITE(reg, temp);
3629
3630 POSTING_READ(reg);
8db9d77b
ZW
3631 udelay(500);
3632
fa37d39e
SP
3633 for (retry = 0; retry < 5; retry++) {
3634 reg = FDI_RX_IIR(pipe);
3635 temp = I915_READ(reg);
3636 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3637 if (temp & FDI_RX_SYMBOL_LOCK) {
3638 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3639 DRM_DEBUG_KMS("FDI train 2 done.\n");
3640 break;
3641 }
3642 udelay(50);
8db9d77b 3643 }
fa37d39e
SP
3644 if (retry < 5)
3645 break;
8db9d77b
ZW
3646 }
3647 if (i == 4)
5eddb70b 3648 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3649
3650 DRM_DEBUG_KMS("FDI train done.\n");
3651}
3652
357555c0
JB
3653/* Manual link training for Ivy Bridge A0 parts */
3654static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3655{
3656 struct drm_device *dev = crtc->dev;
3657 struct drm_i915_private *dev_priv = dev->dev_private;
3658 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3659 int pipe = intel_crtc->pipe;
139ccd3f 3660 u32 reg, temp, i, j;
357555c0
JB
3661
3662 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3663 for train result */
3664 reg = FDI_RX_IMR(pipe);
3665 temp = I915_READ(reg);
3666 temp &= ~FDI_RX_SYMBOL_LOCK;
3667 temp &= ~FDI_RX_BIT_LOCK;
3668 I915_WRITE(reg, temp);
3669
3670 POSTING_READ(reg);
3671 udelay(150);
3672
01a415fd
DV
3673 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3674 I915_READ(FDI_RX_IIR(pipe)));
3675
139ccd3f
JB
3676 /* Try each vswing and preemphasis setting twice before moving on */
3677 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3678 /* disable first in case we need to retry */
3679 reg = FDI_TX_CTL(pipe);
3680 temp = I915_READ(reg);
3681 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3682 temp &= ~FDI_TX_ENABLE;
3683 I915_WRITE(reg, temp);
357555c0 3684
139ccd3f
JB
3685 reg = FDI_RX_CTL(pipe);
3686 temp = I915_READ(reg);
3687 temp &= ~FDI_LINK_TRAIN_AUTO;
3688 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3689 temp &= ~FDI_RX_ENABLE;
3690 I915_WRITE(reg, temp);
357555c0 3691
139ccd3f 3692 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3693 reg = FDI_TX_CTL(pipe);
3694 temp = I915_READ(reg);
139ccd3f 3695 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3696 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3697 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3698 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3699 temp |= snb_b_fdi_train_param[j/2];
3700 temp |= FDI_COMPOSITE_SYNC;
3701 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3702
139ccd3f
JB
3703 I915_WRITE(FDI_RX_MISC(pipe),
3704 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3705
139ccd3f 3706 reg = FDI_RX_CTL(pipe);
357555c0 3707 temp = I915_READ(reg);
139ccd3f
JB
3708 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3709 temp |= FDI_COMPOSITE_SYNC;
3710 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3711
139ccd3f
JB
3712 POSTING_READ(reg);
3713 udelay(1); /* should be 0.5us */
357555c0 3714
139ccd3f
JB
3715 for (i = 0; i < 4; i++) {
3716 reg = FDI_RX_IIR(pipe);
3717 temp = I915_READ(reg);
3718 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3719
139ccd3f
JB
3720 if (temp & FDI_RX_BIT_LOCK ||
3721 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3722 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3723 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3724 i);
3725 break;
3726 }
3727 udelay(1); /* should be 0.5us */
3728 }
3729 if (i == 4) {
3730 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3731 continue;
3732 }
357555c0 3733
139ccd3f 3734 /* Train 2 */
357555c0
JB
3735 reg = FDI_TX_CTL(pipe);
3736 temp = I915_READ(reg);
139ccd3f
JB
3737 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3738 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3739 I915_WRITE(reg, temp);
3740
3741 reg = FDI_RX_CTL(pipe);
3742 temp = I915_READ(reg);
3743 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3744 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3745 I915_WRITE(reg, temp);
3746
3747 POSTING_READ(reg);
139ccd3f 3748 udelay(2); /* should be 1.5us */
357555c0 3749
139ccd3f
JB
3750 for (i = 0; i < 4; i++) {
3751 reg = FDI_RX_IIR(pipe);
3752 temp = I915_READ(reg);
3753 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3754
139ccd3f
JB
3755 if (temp & FDI_RX_SYMBOL_LOCK ||
3756 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3757 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3758 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3759 i);
3760 goto train_done;
3761 }
3762 udelay(2); /* should be 1.5us */
357555c0 3763 }
139ccd3f
JB
3764 if (i == 4)
3765 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3766 }
357555c0 3767
139ccd3f 3768train_done:
357555c0
JB
3769 DRM_DEBUG_KMS("FDI train done.\n");
3770}
3771
88cefb6c 3772static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3773{
88cefb6c 3774 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3775 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3776 int pipe = intel_crtc->pipe;
5eddb70b 3777 u32 reg, temp;
79e53945 3778
c64e311e 3779
c98e9dcf 3780 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3781 reg = FDI_RX_CTL(pipe);
3782 temp = I915_READ(reg);
627eb5a3 3783 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3784 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3785 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3786 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3787
3788 POSTING_READ(reg);
c98e9dcf
JB
3789 udelay(200);
3790
3791 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3792 temp = I915_READ(reg);
3793 I915_WRITE(reg, temp | FDI_PCDCLK);
3794
3795 POSTING_READ(reg);
c98e9dcf
JB
3796 udelay(200);
3797
20749730
PZ
3798 /* Enable CPU FDI TX PLL, always on for Ironlake */
3799 reg = FDI_TX_CTL(pipe);
3800 temp = I915_READ(reg);
3801 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3802 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3803
20749730
PZ
3804 POSTING_READ(reg);
3805 udelay(100);
6be4a607 3806 }
0e23b99d
JB
3807}
3808
88cefb6c
DV
3809static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3810{
3811 struct drm_device *dev = intel_crtc->base.dev;
3812 struct drm_i915_private *dev_priv = dev->dev_private;
3813 int pipe = intel_crtc->pipe;
3814 u32 reg, temp;
3815
3816 /* Switch from PCDclk to Rawclk */
3817 reg = FDI_RX_CTL(pipe);
3818 temp = I915_READ(reg);
3819 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3820
3821 /* Disable CPU FDI TX PLL */
3822 reg = FDI_TX_CTL(pipe);
3823 temp = I915_READ(reg);
3824 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3825
3826 POSTING_READ(reg);
3827 udelay(100);
3828
3829 reg = FDI_RX_CTL(pipe);
3830 temp = I915_READ(reg);
3831 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3832
3833 /* Wait for the clocks to turn off. */
3834 POSTING_READ(reg);
3835 udelay(100);
3836}
3837
0fc932b8
JB
3838static void ironlake_fdi_disable(struct drm_crtc *crtc)
3839{
3840 struct drm_device *dev = crtc->dev;
3841 struct drm_i915_private *dev_priv = dev->dev_private;
3842 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3843 int pipe = intel_crtc->pipe;
3844 u32 reg, temp;
3845
3846 /* disable CPU FDI tx and PCH FDI rx */
3847 reg = FDI_TX_CTL(pipe);
3848 temp = I915_READ(reg);
3849 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3850 POSTING_READ(reg);
3851
3852 reg = FDI_RX_CTL(pipe);
3853 temp = I915_READ(reg);
3854 temp &= ~(0x7 << 16);
dfd07d72 3855 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3856 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3857
3858 POSTING_READ(reg);
3859 udelay(100);
3860
3861 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3862 if (HAS_PCH_IBX(dev))
6f06ce18 3863 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3864
3865 /* still set train pattern 1 */
3866 reg = FDI_TX_CTL(pipe);
3867 temp = I915_READ(reg);
3868 temp &= ~FDI_LINK_TRAIN_NONE;
3869 temp |= FDI_LINK_TRAIN_PATTERN_1;
3870 I915_WRITE(reg, temp);
3871
3872 reg = FDI_RX_CTL(pipe);
3873 temp = I915_READ(reg);
3874 if (HAS_PCH_CPT(dev)) {
3875 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3876 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3877 } else {
3878 temp &= ~FDI_LINK_TRAIN_NONE;
3879 temp |= FDI_LINK_TRAIN_PATTERN_1;
3880 }
3881 /* BPC in FDI rx is consistent with that in PIPECONF */
3882 temp &= ~(0x07 << 16);
dfd07d72 3883 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3884 I915_WRITE(reg, temp);
3885
3886 POSTING_READ(reg);
3887 udelay(100);
3888}
3889
5dce5b93
CW
3890bool intel_has_pending_fb_unpin(struct drm_device *dev)
3891{
3892 struct intel_crtc *crtc;
3893
3894 /* Note that we don't need to be called with mode_config.lock here
3895 * as our list of CRTC objects is static for the lifetime of the
3896 * device and so cannot disappear as we iterate. Similarly, we can
3897 * happily treat the predicates as racy, atomic checks as userspace
3898 * cannot claim and pin a new fb without at least acquring the
3899 * struct_mutex and so serialising with us.
3900 */
d3fcc808 3901 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3902 if (atomic_read(&crtc->unpin_work_count) == 0)
3903 continue;
3904
3905 if (crtc->unpin_work)
3906 intel_wait_for_vblank(dev, crtc->pipe);
3907
3908 return true;
3909 }
3910
3911 return false;
3912}
3913
d6bbafa1
CW
3914static void page_flip_completed(struct intel_crtc *intel_crtc)
3915{
3916 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3917 struct intel_unpin_work *work = intel_crtc->unpin_work;
3918
3919 /* ensure that the unpin work is consistent wrt ->pending. */
3920 smp_rmb();
3921 intel_crtc->unpin_work = NULL;
3922
3923 if (work->event)
3924 drm_send_vblank_event(intel_crtc->base.dev,
3925 intel_crtc->pipe,
3926 work->event);
3927
3928 drm_crtc_vblank_put(&intel_crtc->base);
3929
3930 wake_up_all(&dev_priv->pending_flip_queue);
3931 queue_work(dev_priv->wq, &work->work);
3932
3933 trace_i915_flip_complete(intel_crtc->plane,
3934 work->pending_flip_obj);
3935}
3936
46a55d30 3937void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3938{
0f91128d 3939 struct drm_device *dev = crtc->dev;
5bb61643 3940 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3941
2c10d571 3942 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3943 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3944 !intel_crtc_has_pending_flip(crtc),
3945 60*HZ) == 0)) {
3946 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3947
5e2d7afc 3948 spin_lock_irq(&dev->event_lock);
9c787942
CW
3949 if (intel_crtc->unpin_work) {
3950 WARN_ONCE(1, "Removing stuck page flip\n");
3951 page_flip_completed(intel_crtc);
3952 }
5e2d7afc 3953 spin_unlock_irq(&dev->event_lock);
9c787942 3954 }
5bb61643 3955
975d568a
CW
3956 if (crtc->primary->fb) {
3957 mutex_lock(&dev->struct_mutex);
3958 intel_finish_fb(crtc->primary->fb);
3959 mutex_unlock(&dev->struct_mutex);
3960 }
e6c3a2a6
CW
3961}
3962
e615efe4
ED
3963/* Program iCLKIP clock to the desired frequency */
3964static void lpt_program_iclkip(struct drm_crtc *crtc)
3965{
3966 struct drm_device *dev = crtc->dev;
3967 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3968 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3969 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3970 u32 temp;
3971
09153000
DV
3972 mutex_lock(&dev_priv->dpio_lock);
3973
e615efe4
ED
3974 /* It is necessary to ungate the pixclk gate prior to programming
3975 * the divisors, and gate it back when it is done.
3976 */
3977 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3978
3979 /* Disable SSCCTL */
3980 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3981 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3982 SBI_SSCCTL_DISABLE,
3983 SBI_ICLK);
e615efe4
ED
3984
3985 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3986 if (clock == 20000) {
e615efe4
ED
3987 auxdiv = 1;
3988 divsel = 0x41;
3989 phaseinc = 0x20;
3990 } else {
3991 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3992 * but the adjusted_mode->crtc_clock in in KHz. To get the
3993 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3994 * convert the virtual clock precision to KHz here for higher
3995 * precision.
3996 */
3997 u32 iclk_virtual_root_freq = 172800 * 1000;
3998 u32 iclk_pi_range = 64;
3999 u32 desired_divisor, msb_divisor_value, pi_value;
4000
12d7ceed 4001 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
4002 msb_divisor_value = desired_divisor / iclk_pi_range;
4003 pi_value = desired_divisor % iclk_pi_range;
4004
4005 auxdiv = 0;
4006 divsel = msb_divisor_value - 2;
4007 phaseinc = pi_value;
4008 }
4009
4010 /* This should not happen with any sane values */
4011 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4012 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4013 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4014 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4015
4016 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4017 clock,
e615efe4
ED
4018 auxdiv,
4019 divsel,
4020 phasedir,
4021 phaseinc);
4022
4023 /* Program SSCDIVINTPHASE6 */
988d6ee8 4024 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4025 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4026 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4027 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4028 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4029 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4030 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4031 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4032
4033 /* Program SSCAUXDIV */
988d6ee8 4034 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4035 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4036 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4037 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4038
4039 /* Enable modulator and associated divider */
988d6ee8 4040 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4041 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4042 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
4043
4044 /* Wait for initialization time */
4045 udelay(24);
4046
4047 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
4048
4049 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
4050}
4051
275f01b2
DV
4052static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4053 enum pipe pch_transcoder)
4054{
4055 struct drm_device *dev = crtc->base.dev;
4056 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4057 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4058
4059 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4060 I915_READ(HTOTAL(cpu_transcoder)));
4061 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4062 I915_READ(HBLANK(cpu_transcoder)));
4063 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4064 I915_READ(HSYNC(cpu_transcoder)));
4065
4066 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4067 I915_READ(VTOTAL(cpu_transcoder)));
4068 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4069 I915_READ(VBLANK(cpu_transcoder)));
4070 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4071 I915_READ(VSYNC(cpu_transcoder)));
4072 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4073 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4074}
4075
003632d9 4076static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4077{
4078 struct drm_i915_private *dev_priv = dev->dev_private;
4079 uint32_t temp;
4080
4081 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4082 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4083 return;
4084
4085 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4086 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4087
003632d9
ACO
4088 temp &= ~FDI_BC_BIFURCATION_SELECT;
4089 if (enable)
4090 temp |= FDI_BC_BIFURCATION_SELECT;
4091
4092 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4093 I915_WRITE(SOUTH_CHICKEN1, temp);
4094 POSTING_READ(SOUTH_CHICKEN1);
4095}
4096
4097static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4098{
4099 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4100
4101 switch (intel_crtc->pipe) {
4102 case PIPE_A:
4103 break;
4104 case PIPE_B:
6e3c9717 4105 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4106 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4107 else
003632d9 4108 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4109
4110 break;
4111 case PIPE_C:
003632d9 4112 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4113
4114 break;
4115 default:
4116 BUG();
4117 }
4118}
4119
f67a559d
JB
4120/*
4121 * Enable PCH resources required for PCH ports:
4122 * - PCH PLLs
4123 * - FDI training & RX/TX
4124 * - update transcoder timings
4125 * - DP transcoding bits
4126 * - transcoder
4127 */
4128static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4129{
4130 struct drm_device *dev = crtc->dev;
4131 struct drm_i915_private *dev_priv = dev->dev_private;
4132 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4133 int pipe = intel_crtc->pipe;
ee7b9f93 4134 u32 reg, temp;
2c07245f 4135
ab9412ba 4136 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4137
1fbc0d78
DV
4138 if (IS_IVYBRIDGE(dev))
4139 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4140
cd986abb
DV
4141 /* Write the TU size bits before fdi link training, so that error
4142 * detection works. */
4143 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4144 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4145
c98e9dcf 4146 /* For PCH output, training FDI link */
674cf967 4147 dev_priv->display.fdi_link_train(crtc);
2c07245f 4148
3ad8a208
DV
4149 /* We need to program the right clock selection before writing the pixel
4150 * mutliplier into the DPLL. */
303b81e0 4151 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4152 u32 sel;
4b645f14 4153
c98e9dcf 4154 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4155 temp |= TRANS_DPLL_ENABLE(pipe);
4156 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 4157 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
4158 temp |= sel;
4159 else
4160 temp &= ~sel;
c98e9dcf 4161 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4162 }
5eddb70b 4163
3ad8a208
DV
4164 /* XXX: pch pll's can be enabled any time before we enable the PCH
4165 * transcoder, and we actually should do this to not upset any PCH
4166 * transcoder that already use the clock when we share it.
4167 *
4168 * Note that enable_shared_dpll tries to do the right thing, but
4169 * get_shared_dpll unconditionally resets the pll - we need that to have
4170 * the right LVDS enable sequence. */
85b3894f 4171 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4172
d9b6cb56
JB
4173 /* set transcoder timing, panel must allow it */
4174 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4175 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4176
303b81e0 4177 intel_fdi_normal_train(crtc);
5e84e1a4 4178
c98e9dcf 4179 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4180 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 4181 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
4182 reg = TRANS_DP_CTL(pipe);
4183 temp = I915_READ(reg);
4184 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4185 TRANS_DP_SYNC_MASK |
4186 TRANS_DP_BPC_MASK);
5eddb70b
CW
4187 temp |= (TRANS_DP_OUTPUT_ENABLE |
4188 TRANS_DP_ENH_FRAMING);
9325c9f0 4189 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
4190
4191 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4192 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 4193 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4194 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4195
4196 switch (intel_trans_dp_port_sel(crtc)) {
4197 case PCH_DP_B:
5eddb70b 4198 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
4199 break;
4200 case PCH_DP_C:
5eddb70b 4201 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
4202 break;
4203 case PCH_DP_D:
5eddb70b 4204 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4205 break;
4206 default:
e95d41e1 4207 BUG();
32f9d658 4208 }
2c07245f 4209
5eddb70b 4210 I915_WRITE(reg, temp);
6be4a607 4211 }
b52eb4dc 4212
b8a4f404 4213 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4214}
4215
1507e5bd
PZ
4216static void lpt_pch_enable(struct drm_crtc *crtc)
4217{
4218 struct drm_device *dev = crtc->dev;
4219 struct drm_i915_private *dev_priv = dev->dev_private;
4220 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4221 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4222
ab9412ba 4223 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4224
8c52b5e8 4225 lpt_program_iclkip(crtc);
1507e5bd 4226
0540e488 4227 /* Set transcoder timing. */
275f01b2 4228 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4229
937bb610 4230 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4231}
4232
716c2e55 4233void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 4234{
e2b78267 4235 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
4236
4237 if (pll == NULL)
4238 return;
4239
3e369b76 4240 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
1e6f2ddc 4241 WARN(1, "bad %s crtc mask\n", pll->name);
ee7b9f93
JB
4242 return;
4243 }
4244
3e369b76
ACO
4245 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4246 if (pll->config.crtc_mask == 0) {
f4a091c7
DV
4247 WARN_ON(pll->on);
4248 WARN_ON(pll->active);
4249 }
4250
6e3c9717 4251 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
4252}
4253
190f68c5
ACO
4254struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4255 struct intel_crtc_state *crtc_state)
ee7b9f93 4256{
e2b78267 4257 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 4258 struct intel_shared_dpll *pll;
e2b78267 4259 enum intel_dpll_id i;
ee7b9f93 4260
98b6bd99
DV
4261 if (HAS_PCH_IBX(dev_priv->dev)) {
4262 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 4263 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 4264 pll = &dev_priv->shared_dplls[i];
98b6bd99 4265
46edb027
DV
4266 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4267 crtc->base.base.id, pll->name);
98b6bd99 4268
8bd31e67 4269 WARN_ON(pll->new_config->crtc_mask);
f2a69f44 4270
98b6bd99
DV
4271 goto found;
4272 }
4273
bcddf610
S
4274 if (IS_BROXTON(dev_priv->dev)) {
4275 /* PLL is attached to port in bxt */
4276 struct intel_encoder *encoder;
4277 struct intel_digital_port *intel_dig_port;
4278
4279 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4280 if (WARN_ON(!encoder))
4281 return NULL;
4282
4283 intel_dig_port = enc_to_dig_port(&encoder->base);
4284 /* 1:1 mapping between ports and PLLs */
4285 i = (enum intel_dpll_id)intel_dig_port->port;
4286 pll = &dev_priv->shared_dplls[i];
4287 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4288 crtc->base.base.id, pll->name);
4289 WARN_ON(pll->new_config->crtc_mask);
4290
4291 goto found;
4292 }
4293
e72f9fbf
DV
4294 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4295 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
4296
4297 /* Only want to check enabled timings first */
8bd31e67 4298 if (pll->new_config->crtc_mask == 0)
ee7b9f93
JB
4299 continue;
4300
190f68c5 4301 if (memcmp(&crtc_state->dpll_hw_state,
8bd31e67
ACO
4302 &pll->new_config->hw_state,
4303 sizeof(pll->new_config->hw_state)) == 0) {
4304 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 4305 crtc->base.base.id, pll->name,
8bd31e67
ACO
4306 pll->new_config->crtc_mask,
4307 pll->active);
ee7b9f93
JB
4308 goto found;
4309 }
4310 }
4311
4312 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
4313 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4314 pll = &dev_priv->shared_dplls[i];
8bd31e67 4315 if (pll->new_config->crtc_mask == 0) {
46edb027
DV
4316 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4317 crtc->base.base.id, pll->name);
ee7b9f93
JB
4318 goto found;
4319 }
4320 }
4321
4322 return NULL;
4323
4324found:
8bd31e67 4325 if (pll->new_config->crtc_mask == 0)
190f68c5 4326 pll->new_config->hw_state = crtc_state->dpll_hw_state;
f2a69f44 4327
190f68c5 4328 crtc_state->shared_dpll = i;
46edb027
DV
4329 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4330 pipe_name(crtc->pipe));
ee7b9f93 4331
8bd31e67 4332 pll->new_config->crtc_mask |= 1 << crtc->pipe;
e04c7350 4333
ee7b9f93
JB
4334 return pll;
4335}
4336
8bd31e67
ACO
4337/**
4338 * intel_shared_dpll_start_config - start a new PLL staged config
4339 * @dev_priv: DRM device
4340 * @clear_pipes: mask of pipes that will have their PLLs freed
4341 *
4342 * Starts a new PLL staged config, copying the current config but
4343 * releasing the references of pipes specified in clear_pipes.
4344 */
4345static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4346 unsigned clear_pipes)
4347{
4348 struct intel_shared_dpll *pll;
4349 enum intel_dpll_id i;
4350
4351 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4352 pll = &dev_priv->shared_dplls[i];
4353
4354 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4355 GFP_KERNEL);
4356 if (!pll->new_config)
4357 goto cleanup;
4358
4359 pll->new_config->crtc_mask &= ~clear_pipes;
4360 }
4361
4362 return 0;
4363
4364cleanup:
4365 while (--i >= 0) {
4366 pll = &dev_priv->shared_dplls[i];
f354d733 4367 kfree(pll->new_config);
8bd31e67
ACO
4368 pll->new_config = NULL;
4369 }
4370
4371 return -ENOMEM;
4372}
4373
4374static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4375{
4376 struct intel_shared_dpll *pll;
4377 enum intel_dpll_id i;
4378
4379 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4380 pll = &dev_priv->shared_dplls[i];
4381
4382 WARN_ON(pll->new_config == &pll->config);
4383
4384 pll->config = *pll->new_config;
4385 kfree(pll->new_config);
4386 pll->new_config = NULL;
4387 }
4388}
4389
4390static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4391{
4392 struct intel_shared_dpll *pll;
4393 enum intel_dpll_id i;
4394
4395 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4396 pll = &dev_priv->shared_dplls[i];
4397
4398 WARN_ON(pll->new_config == &pll->config);
4399
4400 kfree(pll->new_config);
4401 pll->new_config = NULL;
4402 }
4403}
4404
a1520318 4405static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4406{
4407 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4408 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4409 u32 temp;
4410
4411 temp = I915_READ(dslreg);
4412 udelay(500);
4413 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4414 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4415 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4416 }
4417}
4418
a1b2278e
CK
4419/**
4420 * skl_update_scaler_users - Stages update to crtc's scaler state
4421 * @intel_crtc: crtc
4422 * @crtc_state: crtc_state
4423 * @plane: plane (NULL indicates crtc is requesting update)
4424 * @plane_state: plane's state
4425 * @force_detach: request unconditional detachment of scaler
4426 *
4427 * This function updates scaler state for requested plane or crtc.
4428 * To request scaler usage update for a plane, caller shall pass plane pointer.
4429 * To request scaler usage update for crtc, caller shall pass plane pointer
4430 * as NULL.
4431 *
4432 * Return
4433 * 0 - scaler_usage updated successfully
4434 * error - requested scaling cannot be supported or other error condition
4435 */
4436int
4437skl_update_scaler_users(
4438 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4439 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4440 int force_detach)
4441{
4442 int need_scaling;
4443 int idx;
4444 int src_w, src_h, dst_w, dst_h;
4445 int *scaler_id;
4446 struct drm_framebuffer *fb;
4447 struct intel_crtc_scaler_state *scaler_state;
6156a456 4448 unsigned int rotation;
a1b2278e
CK
4449
4450 if (!intel_crtc || !crtc_state)
4451 return 0;
4452
4453 scaler_state = &crtc_state->scaler_state;
4454
4455 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4456 fb = intel_plane ? plane_state->base.fb : NULL;
4457
4458 if (intel_plane) {
4459 src_w = drm_rect_width(&plane_state->src) >> 16;
4460 src_h = drm_rect_height(&plane_state->src) >> 16;
4461 dst_w = drm_rect_width(&plane_state->dst);
4462 dst_h = drm_rect_height(&plane_state->dst);
4463 scaler_id = &plane_state->scaler_id;
6156a456 4464 rotation = plane_state->base.rotation;
a1b2278e
CK
4465 } else {
4466 struct drm_display_mode *adjusted_mode =
4467 &crtc_state->base.adjusted_mode;
4468 src_w = crtc_state->pipe_src_w;
4469 src_h = crtc_state->pipe_src_h;
4470 dst_w = adjusted_mode->hdisplay;
4471 dst_h = adjusted_mode->vdisplay;
4472 scaler_id = &scaler_state->scaler_id;
6156a456 4473 rotation = DRM_ROTATE_0;
a1b2278e 4474 }
6156a456
CK
4475
4476 need_scaling = intel_rotation_90_or_270(rotation) ?
4477 (src_h != dst_w || src_w != dst_h):
4478 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4479
4480 /*
4481 * if plane is being disabled or scaler is no more required or force detach
4482 * - free scaler binded to this plane/crtc
4483 * - in order to do this, update crtc->scaler_usage
4484 *
4485 * Here scaler state in crtc_state is set free so that
4486 * scaler can be assigned to other user. Actual register
4487 * update to free the scaler is done in plane/panel-fit programming.
4488 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4489 */
4490 if (force_detach || !need_scaling || (intel_plane &&
4491 (!fb || !plane_state->visible))) {
4492 if (*scaler_id >= 0) {
4493 scaler_state->scaler_users &= ~(1 << idx);
4494 scaler_state->scalers[*scaler_id].in_use = 0;
4495
4496 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4497 "crtc_state = %p scaler_users = 0x%x\n",
4498 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4499 intel_plane ? intel_plane->base.base.id :
4500 intel_crtc->base.base.id, crtc_state,
4501 scaler_state->scaler_users);
4502 *scaler_id = -1;
4503 }
4504 return 0;
4505 }
4506
4507 /* range checks */
4508 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4509 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4510
4511 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4512 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4513 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4514 "size is out of scaler range\n",
4515 intel_plane ? "PLANE" : "CRTC",
4516 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4517 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4518 return -EINVAL;
4519 }
4520
4521 /* check colorkey */
4522 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4523 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4524 intel_plane->base.base.id);
4525 return -EINVAL;
4526 }
4527
4528 /* Check src format */
4529 if (intel_plane) {
4530 switch (fb->pixel_format) {
4531 case DRM_FORMAT_RGB565:
4532 case DRM_FORMAT_XBGR8888:
4533 case DRM_FORMAT_XRGB8888:
4534 case DRM_FORMAT_ABGR8888:
4535 case DRM_FORMAT_ARGB8888:
4536 case DRM_FORMAT_XRGB2101010:
4537 case DRM_FORMAT_ARGB2101010:
4538 case DRM_FORMAT_XBGR2101010:
4539 case DRM_FORMAT_ABGR2101010:
4540 case DRM_FORMAT_YUYV:
4541 case DRM_FORMAT_YVYU:
4542 case DRM_FORMAT_UYVY:
4543 case DRM_FORMAT_VYUY:
4544 break;
4545 default:
4546 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4547 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4548 return -EINVAL;
4549 }
4550 }
4551
4552 /* mark this plane as a scaler user in crtc_state */
4553 scaler_state->scaler_users |= (1 << idx);
4554 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4555 "crtc_state = %p scaler_users = 0x%x\n",
4556 intel_plane ? "PLANE" : "CRTC",
4557 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4558 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4559 return 0;
4560}
4561
4562static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
bd2e244f
JB
4563{
4564 struct drm_device *dev = crtc->base.dev;
4565 struct drm_i915_private *dev_priv = dev->dev_private;
4566 int pipe = crtc->pipe;
a1b2278e
CK
4567 struct intel_crtc_scaler_state *scaler_state =
4568 &crtc->config->scaler_state;
4569
4570 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4571
4572 /* To update pfit, first update scaler state */
4573 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4574 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4575 skl_detach_scalers(crtc);
4576 if (!enable)
4577 return;
bd2e244f 4578
6e3c9717 4579 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4580 int id;
4581
4582 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4583 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4584 return;
4585 }
4586
4587 id = scaler_state->scaler_id;
4588 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4589 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4590 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4591 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4592
4593 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4594 }
4595}
4596
b074cec8
JB
4597static void ironlake_pfit_enable(struct intel_crtc *crtc)
4598{
4599 struct drm_device *dev = crtc->base.dev;
4600 struct drm_i915_private *dev_priv = dev->dev_private;
4601 int pipe = crtc->pipe;
4602
6e3c9717 4603 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4604 /* Force use of hard-coded filter coefficients
4605 * as some pre-programmed values are broken,
4606 * e.g. x201.
4607 */
4608 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4609 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4610 PF_PIPE_SEL_IVB(pipe));
4611 else
4612 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4613 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4614 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4615 }
4616}
4617
4a3b8769 4618static void intel_enable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4619{
4620 struct drm_device *dev = crtc->dev;
4621 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4622 struct drm_plane *plane;
bb53d4ae
VS
4623 struct intel_plane *intel_plane;
4624
af2b653b
MR
4625 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4626 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
4627 if (intel_plane->pipe == pipe)
4628 intel_plane_restore(&intel_plane->base);
af2b653b 4629 }
bb53d4ae
VS
4630}
4631
20bc8673 4632void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4633{
cea165c3
VS
4634 struct drm_device *dev = crtc->base.dev;
4635 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4636
6e3c9717 4637 if (!crtc->config->ips_enabled)
d77e4531
PZ
4638 return;
4639
cea165c3
VS
4640 /* We can only enable IPS after we enable a plane and wait for a vblank */
4641 intel_wait_for_vblank(dev, crtc->pipe);
4642
d77e4531 4643 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4644 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4645 mutex_lock(&dev_priv->rps.hw_lock);
4646 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4647 mutex_unlock(&dev_priv->rps.hw_lock);
4648 /* Quoting Art Runyan: "its not safe to expect any particular
4649 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4650 * mailbox." Moreover, the mailbox may return a bogus state,
4651 * so we need to just enable it and continue on.
2a114cc1
BW
4652 */
4653 } else {
4654 I915_WRITE(IPS_CTL, IPS_ENABLE);
4655 /* The bit only becomes 1 in the next vblank, so this wait here
4656 * is essentially intel_wait_for_vblank. If we don't have this
4657 * and don't wait for vblanks until the end of crtc_enable, then
4658 * the HW state readout code will complain that the expected
4659 * IPS_CTL value is not the one we read. */
4660 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4661 DRM_ERROR("Timed out waiting for IPS enable\n");
4662 }
d77e4531
PZ
4663}
4664
20bc8673 4665void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4666{
4667 struct drm_device *dev = crtc->base.dev;
4668 struct drm_i915_private *dev_priv = dev->dev_private;
4669
6e3c9717 4670 if (!crtc->config->ips_enabled)
d77e4531
PZ
4671 return;
4672
4673 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4674 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4675 mutex_lock(&dev_priv->rps.hw_lock);
4676 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4677 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4678 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4679 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4680 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4681 } else {
2a114cc1 4682 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4683 POSTING_READ(IPS_CTL);
4684 }
d77e4531
PZ
4685
4686 /* We need to wait for a vblank before we can disable the plane. */
4687 intel_wait_for_vblank(dev, crtc->pipe);
4688}
4689
4690/** Loads the palette/gamma unit for the CRTC with the prepared values */
4691static void intel_crtc_load_lut(struct drm_crtc *crtc)
4692{
4693 struct drm_device *dev = crtc->dev;
4694 struct drm_i915_private *dev_priv = dev->dev_private;
4695 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4696 enum pipe pipe = intel_crtc->pipe;
4697 int palreg = PALETTE(pipe);
4698 int i;
4699 bool reenable_ips = false;
4700
4701 /* The clocks have to be on to load the palette. */
83d65738 4702 if (!crtc->state->enable || !intel_crtc->active)
d77e4531
PZ
4703 return;
4704
50360403 4705 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
409ee761 4706 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4707 assert_dsi_pll_enabled(dev_priv);
4708 else
4709 assert_pll_enabled(dev_priv, pipe);
4710 }
4711
4712 /* use legacy palette for Ironlake */
7a1db49a 4713 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4714 palreg = LGC_PALETTE(pipe);
4715
4716 /* Workaround : Do not read or write the pipe palette/gamma data while
4717 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4718 */
6e3c9717 4719 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4720 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4721 GAMMA_MODE_MODE_SPLIT)) {
4722 hsw_disable_ips(intel_crtc);
4723 reenable_ips = true;
4724 }
4725
4726 for (i = 0; i < 256; i++) {
4727 I915_WRITE(palreg + 4 * i,
4728 (intel_crtc->lut_r[i] << 16) |
4729 (intel_crtc->lut_g[i] << 8) |
4730 intel_crtc->lut_b[i]);
4731 }
4732
4733 if (reenable_ips)
4734 hsw_enable_ips(intel_crtc);
4735}
4736
7cac945f 4737static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4738{
7cac945f 4739 if (intel_crtc->overlay) {
d3eedb1a
VS
4740 struct drm_device *dev = intel_crtc->base.dev;
4741 struct drm_i915_private *dev_priv = dev->dev_private;
4742
4743 mutex_lock(&dev->struct_mutex);
4744 dev_priv->mm.interruptible = false;
4745 (void) intel_overlay_switch_off(intel_crtc->overlay);
4746 dev_priv->mm.interruptible = true;
4747 mutex_unlock(&dev->struct_mutex);
4748 }
4749
4750 /* Let userspace switch the overlay on again. In most cases userspace
4751 * has to recompute where to put it anyway.
4752 */
4753}
4754
87d4300a
ML
4755/**
4756 * intel_post_enable_primary - Perform operations after enabling primary plane
4757 * @crtc: the CRTC whose primary plane was just enabled
4758 *
4759 * Performs potentially sleeping operations that must be done after the primary
4760 * plane is enabled, such as updating FBC and IPS. Note that this may be
4761 * called due to an explicit primary plane update, or due to an implicit
4762 * re-enable that is caused when a sprite plane is updated to no longer
4763 * completely hide the primary plane.
4764 */
4765static void
4766intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4767{
4768 struct drm_device *dev = crtc->dev;
87d4300a 4769 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4771 int pipe = intel_crtc->pipe;
a5c4d7bc 4772
87d4300a
ML
4773 /*
4774 * BDW signals flip done immediately if the plane
4775 * is disabled, even if the plane enable is already
4776 * armed to occur at the next vblank :(
4777 */
4778 if (IS_BROADWELL(dev))
4779 intel_wait_for_vblank(dev, pipe);
a5c4d7bc 4780
87d4300a
ML
4781 /*
4782 * FIXME IPS should be fine as long as one plane is
4783 * enabled, but in practice it seems to have problems
4784 * when going from primary only to sprite only and vice
4785 * versa.
4786 */
a5c4d7bc
VS
4787 hsw_enable_ips(intel_crtc);
4788
4789 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4790 intel_fbc_update(dev);
a5c4d7bc 4791 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4792
4793 /*
87d4300a
ML
4794 * Gen2 reports pipe underruns whenever all planes are disabled.
4795 * So don't enable underrun reporting before at least some planes
4796 * are enabled.
4797 * FIXME: Need to fix the logic to work when we turn off all planes
4798 * but leave the pipe running.
f99d7069 4799 */
87d4300a
ML
4800 if (IS_GEN2(dev))
4801 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4802
4803 /* Underruns don't raise interrupts, so check manually. */
4804 if (HAS_GMCH_DISPLAY(dev))
4805 i9xx_check_fifo_underruns(dev_priv);
a5c4d7bc
VS
4806}
4807
87d4300a
ML
4808/**
4809 * intel_pre_disable_primary - Perform operations before disabling primary plane
4810 * @crtc: the CRTC whose primary plane is to be disabled
4811 *
4812 * Performs potentially sleeping operations that must be done before the
4813 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4814 * be called due to an explicit primary plane update, or due to an implicit
4815 * disable that is caused when a sprite plane completely hides the primary
4816 * plane.
4817 */
4818static void
4819intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4820{
4821 struct drm_device *dev = crtc->dev;
4822 struct drm_i915_private *dev_priv = dev->dev_private;
4823 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4824 int pipe = intel_crtc->pipe;
a5c4d7bc 4825
87d4300a
ML
4826 /*
4827 * Gen2 reports pipe underruns whenever all planes are disabled.
4828 * So diasble underrun reporting before all the planes get disabled.
4829 * FIXME: Need to fix the logic to work when we turn off all planes
4830 * but leave the pipe running.
4831 */
4832 if (IS_GEN2(dev))
4833 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4834
87d4300a
ML
4835 /*
4836 * Vblank time updates from the shadow to live plane control register
4837 * are blocked if the memory self-refresh mode is active at that
4838 * moment. So to make sure the plane gets truly disabled, disable
4839 * first the self-refresh mode. The self-refresh enable bit in turn
4840 * will be checked/applied by the HW only at the next frame start
4841 * event which is after the vblank start event, so we need to have a
4842 * wait-for-vblank between disabling the plane and the pipe.
4843 */
4844 if (HAS_GMCH_DISPLAY(dev))
4845 intel_set_memory_cxsr(dev_priv, false);
4846
4847 mutex_lock(&dev->struct_mutex);
e35fef21 4848 if (dev_priv->fbc.crtc == intel_crtc)
7ff0ebcc 4849 intel_fbc_disable(dev);
87d4300a 4850 mutex_unlock(&dev->struct_mutex);
a5c4d7bc 4851
87d4300a
ML
4852 /*
4853 * FIXME IPS should be fine as long as one plane is
4854 * enabled, but in practice it seems to have problems
4855 * when going from primary only to sprite only and vice
4856 * versa.
4857 */
a5c4d7bc 4858 hsw_disable_ips(intel_crtc);
87d4300a
ML
4859}
4860
4861static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4862{
87d4300a
ML
4863 intel_enable_primary_hw_plane(crtc->primary, crtc);
4864 intel_enable_sprite_planes(crtc);
4865 intel_crtc_update_cursor(crtc, true);
87d4300a
ML
4866
4867 intel_post_enable_primary(crtc);
4868}
4869
4870static void intel_crtc_disable_planes(struct drm_crtc *crtc)
4871{
4872 struct drm_device *dev = crtc->dev;
4873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4874 struct intel_plane *intel_plane;
4875 int pipe = intel_crtc->pipe;
4876
4877 intel_crtc_wait_for_pending_flips(crtc);
4878
4879 intel_pre_disable_primary(crtc);
a5c4d7bc 4880
7cac945f 4881 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8
ML
4882 for_each_intel_plane(dev, intel_plane) {
4883 if (intel_plane->pipe == pipe) {
4884 struct drm_crtc *from = intel_plane->base.crtc;
4885
4886 intel_plane->disable_plane(&intel_plane->base,
4887 from ?: crtc, true);
4888 }
4889 }
f98551ae 4890
f99d7069
DV
4891 /*
4892 * FIXME: Once we grow proper nuclear flip support out of this we need
4893 * to compute the mask of flip planes precisely. For the time being
4894 * consider this a flip to a NULL plane.
4895 */
4896 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4897}
4898
f67a559d
JB
4899static void ironlake_crtc_enable(struct drm_crtc *crtc)
4900{
4901 struct drm_device *dev = crtc->dev;
4902 struct drm_i915_private *dev_priv = dev->dev_private;
4903 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4904 struct intel_encoder *encoder;
f67a559d 4905 int pipe = intel_crtc->pipe;
f67a559d 4906
83d65738 4907 WARN_ON(!crtc->state->enable);
08a48469 4908
f67a559d
JB
4909 if (intel_crtc->active)
4910 return;
4911
6e3c9717 4912 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4913 intel_prepare_shared_dpll(intel_crtc);
4914
6e3c9717 4915 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4916 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4917
4918 intel_set_pipe_timings(intel_crtc);
4919
6e3c9717 4920 if (intel_crtc->config->has_pch_encoder) {
29407aab 4921 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4922 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4923 }
4924
4925 ironlake_set_pipeconf(crtc);
4926
f67a559d 4927 intel_crtc->active = true;
8664281b 4928
a72e4c9f
DV
4929 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4930 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4931
f6736a1a 4932 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4933 if (encoder->pre_enable)
4934 encoder->pre_enable(encoder);
f67a559d 4935
6e3c9717 4936 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4937 /* Note: FDI PLL enabling _must_ be done before we enable the
4938 * cpu pipes, hence this is separate from all the other fdi/pch
4939 * enabling. */
88cefb6c 4940 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4941 } else {
4942 assert_fdi_tx_disabled(dev_priv, pipe);
4943 assert_fdi_rx_disabled(dev_priv, pipe);
4944 }
f67a559d 4945
b074cec8 4946 ironlake_pfit_enable(intel_crtc);
f67a559d 4947
9c54c0dd
JB
4948 /*
4949 * On ILK+ LUT must be loaded before the pipe is running but with
4950 * clocks enabled
4951 */
4952 intel_crtc_load_lut(crtc);
4953
f37fcc2a 4954 intel_update_watermarks(crtc);
e1fdc473 4955 intel_enable_pipe(intel_crtc);
f67a559d 4956
6e3c9717 4957 if (intel_crtc->config->has_pch_encoder)
f67a559d 4958 ironlake_pch_enable(crtc);
c98e9dcf 4959
f9b61ff6
DV
4960 assert_vblank_disabled(crtc);
4961 drm_crtc_vblank_on(crtc);
4962
fa5c73b1
DV
4963 for_each_encoder_on_crtc(dev, crtc, encoder)
4964 encoder->enable(encoder);
61b77ddd
DV
4965
4966 if (HAS_PCH_CPT(dev))
a1520318 4967 cpt_verify_modeset(dev, intel_crtc->pipe);
6be4a607
JB
4968}
4969
42db64ef
PZ
4970/* IPS only exists on ULT machines and is tied to pipe A. */
4971static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4972{
f5adf94e 4973 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4974}
4975
e4916946
PZ
4976/*
4977 * This implements the workaround described in the "notes" section of the mode
4978 * set sequence documentation. When going from no pipes or single pipe to
4979 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4980 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4981 */
4982static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4983{
4984 struct drm_device *dev = crtc->base.dev;
4985 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4986
4987 /* We want to get the other_active_crtc only if there's only 1 other
4988 * active crtc. */
d3fcc808 4989 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4990 if (!crtc_it->active || crtc_it == crtc)
4991 continue;
4992
4993 if (other_active_crtc)
4994 return;
4995
4996 other_active_crtc = crtc_it;
4997 }
4998 if (!other_active_crtc)
4999 return;
5000
5001 intel_wait_for_vblank(dev, other_active_crtc->pipe);
5002 intel_wait_for_vblank(dev, other_active_crtc->pipe);
5003}
5004
4f771f10
PZ
5005static void haswell_crtc_enable(struct drm_crtc *crtc)
5006{
5007 struct drm_device *dev = crtc->dev;
5008 struct drm_i915_private *dev_priv = dev->dev_private;
5009 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5010 struct intel_encoder *encoder;
5011 int pipe = intel_crtc->pipe;
4f771f10 5012
83d65738 5013 WARN_ON(!crtc->state->enable);
4f771f10
PZ
5014
5015 if (intel_crtc->active)
5016 return;
5017
df8ad70c
DV
5018 if (intel_crtc_to_shared_dpll(intel_crtc))
5019 intel_enable_shared_dpll(intel_crtc);
5020
6e3c9717 5021 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5022 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
5023
5024 intel_set_pipe_timings(intel_crtc);
5025
6e3c9717
ACO
5026 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5027 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5028 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5029 }
5030
6e3c9717 5031 if (intel_crtc->config->has_pch_encoder) {
229fca97 5032 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5033 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5034 }
5035
5036 haswell_set_pipeconf(crtc);
5037
5038 intel_set_pipe_csc(crtc);
5039
4f771f10 5040 intel_crtc->active = true;
8664281b 5041
a72e4c9f 5042 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
5043 for_each_encoder_on_crtc(dev, crtc, encoder)
5044 if (encoder->pre_enable)
5045 encoder->pre_enable(encoder);
5046
6e3c9717 5047 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
5048 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5049 true);
4fe9467d
ID
5050 dev_priv->display.fdi_link_train(crtc);
5051 }
5052
1f544388 5053 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5054
ff6d9f55 5055 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5056 skylake_pfit_update(intel_crtc, 1);
ff6d9f55 5057 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5058 ironlake_pfit_enable(intel_crtc);
ff6d9f55
JB
5059 else
5060 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10
PZ
5061
5062 /*
5063 * On ILK+ LUT must be loaded before the pipe is running but with
5064 * clocks enabled
5065 */
5066 intel_crtc_load_lut(crtc);
5067
1f544388 5068 intel_ddi_set_pipe_settings(crtc);
8228c251 5069 intel_ddi_enable_transcoder_func(crtc);
4f771f10 5070
f37fcc2a 5071 intel_update_watermarks(crtc);
e1fdc473 5072 intel_enable_pipe(intel_crtc);
42db64ef 5073
6e3c9717 5074 if (intel_crtc->config->has_pch_encoder)
1507e5bd 5075 lpt_pch_enable(crtc);
4f771f10 5076
6e3c9717 5077 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
5078 intel_ddi_set_vc_payload_alloc(crtc, true);
5079
f9b61ff6
DV
5080 assert_vblank_disabled(crtc);
5081 drm_crtc_vblank_on(crtc);
5082
8807e55b 5083 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 5084 encoder->enable(encoder);
8807e55b
JN
5085 intel_opregion_notify_encoder(encoder, true);
5086 }
4f771f10 5087
e4916946
PZ
5088 /* If we change the relative order between pipe/planes enabling, we need
5089 * to change the workaround. */
5090 haswell_mode_set_planes_workaround(intel_crtc);
4f771f10
PZ
5091}
5092
3f8dce3a
DV
5093static void ironlake_pfit_disable(struct intel_crtc *crtc)
5094{
5095 struct drm_device *dev = crtc->base.dev;
5096 struct drm_i915_private *dev_priv = dev->dev_private;
5097 int pipe = crtc->pipe;
5098
5099 /* To avoid upsetting the power well on haswell only disable the pfit if
5100 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 5101 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5102 I915_WRITE(PF_CTL(pipe), 0);
5103 I915_WRITE(PF_WIN_POS(pipe), 0);
5104 I915_WRITE(PF_WIN_SZ(pipe), 0);
5105 }
5106}
5107
6be4a607
JB
5108static void ironlake_crtc_disable(struct drm_crtc *crtc)
5109{
5110 struct drm_device *dev = crtc->dev;
5111 struct drm_i915_private *dev_priv = dev->dev_private;
5112 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5113 struct intel_encoder *encoder;
6be4a607 5114 int pipe = intel_crtc->pipe;
5eddb70b 5115 u32 reg, temp;
b52eb4dc 5116
f7abfe8b
CW
5117 if (!intel_crtc->active)
5118 return;
5119
ea9d758d
DV
5120 for_each_encoder_on_crtc(dev, crtc, encoder)
5121 encoder->disable(encoder);
5122
f9b61ff6
DV
5123 drm_crtc_vblank_off(crtc);
5124 assert_vblank_disabled(crtc);
5125
6e3c9717 5126 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 5127 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 5128
575f7ab7 5129 intel_disable_pipe(intel_crtc);
32f9d658 5130
3f8dce3a 5131 ironlake_pfit_disable(intel_crtc);
2c07245f 5132
bf49ec8c
DV
5133 for_each_encoder_on_crtc(dev, crtc, encoder)
5134 if (encoder->post_disable)
5135 encoder->post_disable(encoder);
2c07245f 5136
6e3c9717 5137 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5138 ironlake_fdi_disable(crtc);
913d8d11 5139
d925c59a 5140 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5141
d925c59a
DV
5142 if (HAS_PCH_CPT(dev)) {
5143 /* disable TRANS_DP_CTL */
5144 reg = TRANS_DP_CTL(pipe);
5145 temp = I915_READ(reg);
5146 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5147 TRANS_DP_PORT_SEL_MASK);
5148 temp |= TRANS_DP_PORT_SEL_NONE;
5149 I915_WRITE(reg, temp);
5150
5151 /* disable DPLL_SEL */
5152 temp = I915_READ(PCH_DPLL_SEL);
11887397 5153 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5154 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5155 }
e3421a18 5156
d925c59a 5157 /* disable PCH DPLL */
e72f9fbf 5158 intel_disable_shared_dpll(intel_crtc);
8db9d77b 5159
d925c59a
DV
5160 ironlake_fdi_pll_disable(intel_crtc);
5161 }
6b383a7f 5162
f7abfe8b 5163 intel_crtc->active = false;
46ba614c 5164 intel_update_watermarks(crtc);
d1ebd816
BW
5165
5166 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5167 intel_fbc_update(dev);
d1ebd816 5168 mutex_unlock(&dev->struct_mutex);
6be4a607 5169}
1b3c7a47 5170
4f771f10 5171static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5172{
4f771f10
PZ
5173 struct drm_device *dev = crtc->dev;
5174 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5175 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5176 struct intel_encoder *encoder;
6e3c9717 5177 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5178
4f771f10
PZ
5179 if (!intel_crtc->active)
5180 return;
5181
8807e55b
JN
5182 for_each_encoder_on_crtc(dev, crtc, encoder) {
5183 intel_opregion_notify_encoder(encoder, false);
4f771f10 5184 encoder->disable(encoder);
8807e55b 5185 }
4f771f10 5186
f9b61ff6
DV
5187 drm_crtc_vblank_off(crtc);
5188 assert_vblank_disabled(crtc);
5189
6e3c9717 5190 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
5191 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5192 false);
575f7ab7 5193 intel_disable_pipe(intel_crtc);
4f771f10 5194
6e3c9717 5195 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5196 intel_ddi_set_vc_payload_alloc(crtc, false);
5197
ad80a810 5198 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5199
ff6d9f55 5200 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5201 skylake_pfit_update(intel_crtc, 0);
ff6d9f55 5202 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5203 ironlake_pfit_disable(intel_crtc);
ff6d9f55
JB
5204 else
5205 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10 5206
1f544388 5207 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5208
6e3c9717 5209 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 5210 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 5211 intel_ddi_fdi_disable(crtc);
83616634 5212 }
4f771f10 5213
97b040aa
ID
5214 for_each_encoder_on_crtc(dev, crtc, encoder)
5215 if (encoder->post_disable)
5216 encoder->post_disable(encoder);
5217
4f771f10 5218 intel_crtc->active = false;
46ba614c 5219 intel_update_watermarks(crtc);
4f771f10
PZ
5220
5221 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5222 intel_fbc_update(dev);
4f771f10 5223 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
5224
5225 if (intel_crtc_to_shared_dpll(intel_crtc))
5226 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
5227}
5228
ee7b9f93
JB
5229static void ironlake_crtc_off(struct drm_crtc *crtc)
5230{
5231 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 5232 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
5233}
5234
6441ab5f 5235
2dd24552
JB
5236static void i9xx_pfit_enable(struct intel_crtc *crtc)
5237{
5238 struct drm_device *dev = crtc->base.dev;
5239 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5240 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5241
681a8504 5242 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5243 return;
5244
2dd24552 5245 /*
c0b03411
DV
5246 * The panel fitter should only be adjusted whilst the pipe is disabled,
5247 * according to register description and PRM.
2dd24552 5248 */
c0b03411
DV
5249 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5250 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5251
b074cec8
JB
5252 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5253 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5254
5255 /* Border color in case we don't scale up to the full screen. Black by
5256 * default, change to something else for debugging. */
5257 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5258}
5259
d05410f9
DA
5260static enum intel_display_power_domain port_to_power_domain(enum port port)
5261{
5262 switch (port) {
5263 case PORT_A:
5264 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5265 case PORT_B:
5266 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5267 case PORT_C:
5268 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5269 case PORT_D:
5270 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5271 default:
5272 WARN_ON_ONCE(1);
5273 return POWER_DOMAIN_PORT_OTHER;
5274 }
5275}
5276
77d22dca
ID
5277#define for_each_power_domain(domain, mask) \
5278 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5279 if ((1 << (domain)) & (mask))
5280
319be8ae
ID
5281enum intel_display_power_domain
5282intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5283{
5284 struct drm_device *dev = intel_encoder->base.dev;
5285 struct intel_digital_port *intel_dig_port;
5286
5287 switch (intel_encoder->type) {
5288 case INTEL_OUTPUT_UNKNOWN:
5289 /* Only DDI platforms should ever use this output type */
5290 WARN_ON_ONCE(!HAS_DDI(dev));
5291 case INTEL_OUTPUT_DISPLAYPORT:
5292 case INTEL_OUTPUT_HDMI:
5293 case INTEL_OUTPUT_EDP:
5294 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5295 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5296 case INTEL_OUTPUT_DP_MST:
5297 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5298 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5299 case INTEL_OUTPUT_ANALOG:
5300 return POWER_DOMAIN_PORT_CRT;
5301 case INTEL_OUTPUT_DSI:
5302 return POWER_DOMAIN_PORT_DSI;
5303 default:
5304 return POWER_DOMAIN_PORT_OTHER;
5305 }
5306}
5307
5308static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5309{
319be8ae
ID
5310 struct drm_device *dev = crtc->dev;
5311 struct intel_encoder *intel_encoder;
5312 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5313 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
5314 unsigned long mask;
5315 enum transcoder transcoder;
5316
5317 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5318
5319 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5320 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
5321 if (intel_crtc->config->pch_pfit.enabled ||
5322 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
5323 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5324
319be8ae
ID
5325 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5326 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5327
77d22dca
ID
5328 return mask;
5329}
5330
679dacd4 5331static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
77d22dca 5332{
679dacd4 5333 struct drm_device *dev = state->dev;
77d22dca
ID
5334 struct drm_i915_private *dev_priv = dev->dev_private;
5335 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5336 struct intel_crtc *crtc;
5337
5338 /*
5339 * First get all needed power domains, then put all unneeded, to avoid
5340 * any unnecessary toggling of the power wells.
5341 */
d3fcc808 5342 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5343 enum intel_display_power_domain domain;
5344
83d65738 5345 if (!crtc->base.state->enable)
77d22dca
ID
5346 continue;
5347
319be8ae 5348 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
5349
5350 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5351 intel_display_power_get(dev_priv, domain);
5352 }
5353
50f6e502 5354 if (dev_priv->display.modeset_global_resources)
679dacd4 5355 dev_priv->display.modeset_global_resources(state);
50f6e502 5356
d3fcc808 5357 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5358 enum intel_display_power_domain domain;
5359
5360 for_each_power_domain(domain, crtc->enabled_power_domains)
5361 intel_display_power_put(dev_priv, domain);
5362
5363 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5364 }
5365
5366 intel_display_set_init_power(dev_priv, false);
5367}
5368
f8437dd1
VK
5369void broxton_set_cdclk(struct drm_device *dev, int frequency)
5370{
5371 struct drm_i915_private *dev_priv = dev->dev_private;
5372 uint32_t divider;
5373 uint32_t ratio;
5374 uint32_t current_freq;
5375 int ret;
5376
5377 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5378 switch (frequency) {
5379 case 144000:
5380 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5381 ratio = BXT_DE_PLL_RATIO(60);
5382 break;
5383 case 288000:
5384 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5385 ratio = BXT_DE_PLL_RATIO(60);
5386 break;
5387 case 384000:
5388 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5389 ratio = BXT_DE_PLL_RATIO(60);
5390 break;
5391 case 576000:
5392 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5393 ratio = BXT_DE_PLL_RATIO(60);
5394 break;
5395 case 624000:
5396 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5397 ratio = BXT_DE_PLL_RATIO(65);
5398 break;
5399 case 19200:
5400 /*
5401 * Bypass frequency with DE PLL disabled. Init ratio, divider
5402 * to suppress GCC warning.
5403 */
5404 ratio = 0;
5405 divider = 0;
5406 break;
5407 default:
5408 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5409
5410 return;
5411 }
5412
5413 mutex_lock(&dev_priv->rps.hw_lock);
5414 /* Inform power controller of upcoming frequency change */
5415 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5416 0x80000000);
5417 mutex_unlock(&dev_priv->rps.hw_lock);
5418
5419 if (ret) {
5420 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5421 ret, frequency);
5422 return;
5423 }
5424
5425 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5426 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5427 current_freq = current_freq * 500 + 1000;
5428
5429 /*
5430 * DE PLL has to be disabled when
5431 * - setting to 19.2MHz (bypass, PLL isn't used)
5432 * - before setting to 624MHz (PLL needs toggling)
5433 * - before setting to any frequency from 624MHz (PLL needs toggling)
5434 */
5435 if (frequency == 19200 || frequency == 624000 ||
5436 current_freq == 624000) {
5437 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5438 /* Timeout 200us */
5439 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5440 1))
5441 DRM_ERROR("timout waiting for DE PLL unlock\n");
5442 }
5443
5444 if (frequency != 19200) {
5445 uint32_t val;
5446
5447 val = I915_READ(BXT_DE_PLL_CTL);
5448 val &= ~BXT_DE_PLL_RATIO_MASK;
5449 val |= ratio;
5450 I915_WRITE(BXT_DE_PLL_CTL, val);
5451
5452 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5453 /* Timeout 200us */
5454 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5455 DRM_ERROR("timeout waiting for DE PLL lock\n");
5456
5457 val = I915_READ(CDCLK_CTL);
5458 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5459 val |= divider;
5460 /*
5461 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5462 * enable otherwise.
5463 */
5464 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5465 if (frequency >= 500000)
5466 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5467
5468 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5469 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5470 val |= (frequency - 1000) / 500;
5471 I915_WRITE(CDCLK_CTL, val);
5472 }
5473
5474 mutex_lock(&dev_priv->rps.hw_lock);
5475 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5476 DIV_ROUND_UP(frequency, 25000));
5477 mutex_unlock(&dev_priv->rps.hw_lock);
5478
5479 if (ret) {
5480 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5481 ret, frequency);
5482 return;
5483 }
5484
5485 dev_priv->cdclk_freq = frequency;
5486}
5487
5488void broxton_init_cdclk(struct drm_device *dev)
5489{
5490 struct drm_i915_private *dev_priv = dev->dev_private;
5491 uint32_t val;
5492
5493 /*
5494 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5495 * or else the reset will hang because there is no PCH to respond.
5496 * Move the handshake programming to initialization sequence.
5497 * Previously was left up to BIOS.
5498 */
5499 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5500 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5501 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5502
5503 /* Enable PG1 for cdclk */
5504 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5505
5506 /* check if cd clock is enabled */
5507 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5508 DRM_DEBUG_KMS("Display already initialized\n");
5509 return;
5510 }
5511
5512 /*
5513 * FIXME:
5514 * - The initial CDCLK needs to be read from VBT.
5515 * Need to make this change after VBT has changes for BXT.
5516 * - check if setting the max (or any) cdclk freq is really necessary
5517 * here, it belongs to modeset time
5518 */
5519 broxton_set_cdclk(dev, 624000);
5520
5521 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5522 POSTING_READ(DBUF_CTL);
5523
f8437dd1
VK
5524 udelay(10);
5525
5526 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5527 DRM_ERROR("DBuf power enable timeout!\n");
5528}
5529
5530void broxton_uninit_cdclk(struct drm_device *dev)
5531{
5532 struct drm_i915_private *dev_priv = dev->dev_private;
5533
5534 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5535 POSTING_READ(DBUF_CTL);
5536
f8437dd1
VK
5537 udelay(10);
5538
5539 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5540 DRM_ERROR("DBuf power disable timeout!\n");
5541
5542 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5543 broxton_set_cdclk(dev, 19200);
5544
5545 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5546}
5547
dfcab17e 5548/* returns HPLL frequency in kHz */
f8bf63fd 5549static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 5550{
586f49dc 5551 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 5552
586f49dc
JB
5553 /* Obtain SKU information */
5554 mutex_lock(&dev_priv->dpio_lock);
5555 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5556 CCK_FUSE_HPLL_FREQ_MASK;
5557 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 5558
dfcab17e 5559 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
5560}
5561
f8bf63fd
VS
5562static void vlv_update_cdclk(struct drm_device *dev)
5563{
5564 struct drm_i915_private *dev_priv = dev->dev_private;
5565
164dfd28 5566 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
43dc52c3 5567 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
164dfd28 5568 dev_priv->cdclk_freq);
f8bf63fd
VS
5569
5570 /*
5571 * Program the gmbus_freq based on the cdclk frequency.
5572 * BSpec erroneously claims we should aim for 4MHz, but
5573 * in fact 1MHz is the correct frequency.
5574 */
164dfd28 5575 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
f8bf63fd
VS
5576}
5577
30a970c6
JB
5578/* Adjust CDclk dividers to allow high res or save power if possible */
5579static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5580{
5581 struct drm_i915_private *dev_priv = dev->dev_private;
5582 u32 val, cmd;
5583
164dfd28
VK
5584 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5585 != dev_priv->cdclk_freq);
d60c4473 5586
dfcab17e 5587 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5588 cmd = 2;
dfcab17e 5589 else if (cdclk == 266667)
30a970c6
JB
5590 cmd = 1;
5591 else
5592 cmd = 0;
5593
5594 mutex_lock(&dev_priv->rps.hw_lock);
5595 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5596 val &= ~DSPFREQGUAR_MASK;
5597 val |= (cmd << DSPFREQGUAR_SHIFT);
5598 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5599 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5600 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5601 50)) {
5602 DRM_ERROR("timed out waiting for CDclk change\n");
5603 }
5604 mutex_unlock(&dev_priv->rps.hw_lock);
5605
dfcab17e 5606 if (cdclk == 400000) {
6bcda4f0 5607 u32 divider;
30a970c6 5608
6bcda4f0 5609 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6
JB
5610
5611 mutex_lock(&dev_priv->dpio_lock);
5612 /* adjust cdclk divider */
5613 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 5614 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
5615 val |= divider;
5616 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5617
5618 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5619 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5620 50))
5621 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5622 mutex_unlock(&dev_priv->dpio_lock);
5623 }
5624
5625 mutex_lock(&dev_priv->dpio_lock);
5626 /* adjust self-refresh exit latency value */
5627 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5628 val &= ~0x7f;
5629
5630 /*
5631 * For high bandwidth configs, we set a higher latency in the bunit
5632 * so that the core display fetch happens in time to avoid underruns.
5633 */
dfcab17e 5634 if (cdclk == 400000)
30a970c6
JB
5635 val |= 4500 / 250; /* 4.5 usec */
5636 else
5637 val |= 3000 / 250; /* 3.0 usec */
5638 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5639 mutex_unlock(&dev_priv->dpio_lock);
5640
f8bf63fd 5641 vlv_update_cdclk(dev);
30a970c6
JB
5642}
5643
383c5a6a
VS
5644static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5645{
5646 struct drm_i915_private *dev_priv = dev->dev_private;
5647 u32 val, cmd;
5648
164dfd28
VK
5649 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5650 != dev_priv->cdclk_freq);
383c5a6a
VS
5651
5652 switch (cdclk) {
383c5a6a
VS
5653 case 333333:
5654 case 320000:
383c5a6a 5655 case 266667:
383c5a6a 5656 case 200000:
383c5a6a
VS
5657 break;
5658 default:
5f77eeb0 5659 MISSING_CASE(cdclk);
383c5a6a
VS
5660 return;
5661 }
5662
9d0d3fda
VS
5663 /*
5664 * Specs are full of misinformation, but testing on actual
5665 * hardware has shown that we just need to write the desired
5666 * CCK divider into the Punit register.
5667 */
5668 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5669
383c5a6a
VS
5670 mutex_lock(&dev_priv->rps.hw_lock);
5671 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5672 val &= ~DSPFREQGUAR_MASK_CHV;
5673 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5674 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5675 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5676 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5677 50)) {
5678 DRM_ERROR("timed out waiting for CDclk change\n");
5679 }
5680 mutex_unlock(&dev_priv->rps.hw_lock);
5681
5682 vlv_update_cdclk(dev);
5683}
5684
30a970c6
JB
5685static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5686 int max_pixclk)
5687{
6bcda4f0 5688 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5689 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5690
30a970c6
JB
5691 /*
5692 * Really only a few cases to deal with, as only 4 CDclks are supported:
5693 * 200MHz
5694 * 267MHz
29dc7ef3 5695 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5696 * 400MHz (VLV only)
5697 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5698 * of the lower bin and adjust if needed.
e37c67a1
VS
5699 *
5700 * We seem to get an unstable or solid color picture at 200MHz.
5701 * Not sure what's wrong. For now use 200MHz only when all pipes
5702 * are off.
30a970c6 5703 */
6cca3195
VS
5704 if (!IS_CHERRYVIEW(dev_priv) &&
5705 max_pixclk > freq_320*limit/100)
dfcab17e 5706 return 400000;
6cca3195 5707 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5708 return freq_320;
e37c67a1 5709 else if (max_pixclk > 0)
dfcab17e 5710 return 266667;
e37c67a1
VS
5711 else
5712 return 200000;
30a970c6
JB
5713}
5714
f8437dd1
VK
5715static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5716 int max_pixclk)
5717{
5718 /*
5719 * FIXME:
5720 * - remove the guardband, it's not needed on BXT
5721 * - set 19.2MHz bypass frequency if there are no active pipes
5722 */
5723 if (max_pixclk > 576000*9/10)
5724 return 624000;
5725 else if (max_pixclk > 384000*9/10)
5726 return 576000;
5727 else if (max_pixclk > 288000*9/10)
5728 return 384000;
5729 else if (max_pixclk > 144000*9/10)
5730 return 288000;
5731 else
5732 return 144000;
5733}
5734
2f2d7aa1 5735/* compute the max pixel clock for new configuration */
304603f4 5736static int intel_mode_max_pixclk(struct drm_atomic_state *state)
30a970c6 5737{
304603f4 5738 struct drm_device *dev = state->dev;
30a970c6 5739 struct intel_crtc *intel_crtc;
304603f4 5740 struct intel_crtc_state *crtc_state;
30a970c6
JB
5741 int max_pixclk = 0;
5742
d3fcc808 5743 for_each_intel_crtc(dev, intel_crtc) {
304603f4
ACO
5744 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5745 if (IS_ERR(crtc_state))
5746 return PTR_ERR(crtc_state);
5747
5748 if (!crtc_state->base.enable)
5749 continue;
5750
5751 max_pixclk = max(max_pixclk,
5752 crtc_state->base.adjusted_mode.crtc_clock);
30a970c6
JB
5753 }
5754
5755 return max_pixclk;
5756}
5757
304603f4 5758static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
2f2d7aa1 5759 unsigned *prepare_pipes)
30a970c6 5760{
304603f4 5761 struct drm_i915_private *dev_priv = to_i915(state->dev);
30a970c6 5762 struct intel_crtc *intel_crtc;
304603f4 5763 int max_pixclk = intel_mode_max_pixclk(state);
f8437dd1 5764 int cdclk;
30a970c6 5765
304603f4
ACO
5766 if (max_pixclk < 0)
5767 return max_pixclk;
30a970c6 5768
f8437dd1
VK
5769 if (IS_VALLEYVIEW(dev_priv))
5770 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5771 else
5772 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5773
5774 if (cdclk == dev_priv->cdclk_freq)
304603f4 5775 return 0;
30a970c6 5776
2f2d7aa1 5777 /* disable/enable all currently active pipes while we change cdclk */
304603f4 5778 for_each_intel_crtc(state->dev, intel_crtc)
83d65738 5779 if (intel_crtc->base.state->enable)
30a970c6 5780 *prepare_pipes |= (1 << intel_crtc->pipe);
304603f4
ACO
5781
5782 return 0;
30a970c6
JB
5783}
5784
1e69cd74
VS
5785static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5786{
5787 unsigned int credits, default_credits;
5788
5789 if (IS_CHERRYVIEW(dev_priv))
5790 default_credits = PFI_CREDIT(12);
5791 else
5792 default_credits = PFI_CREDIT(8);
5793
164dfd28 5794 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
1e69cd74
VS
5795 /* CHV suggested value is 31 or 63 */
5796 if (IS_CHERRYVIEW(dev_priv))
5797 credits = PFI_CREDIT_31;
5798 else
5799 credits = PFI_CREDIT(15);
5800 } else {
5801 credits = default_credits;
5802 }
5803
5804 /*
5805 * WA - write default credits before re-programming
5806 * FIXME: should we also set the resend bit here?
5807 */
5808 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5809 default_credits);
5810
5811 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5812 credits | PFI_CREDIT_RESEND);
5813
5814 /*
5815 * FIXME is this guaranteed to clear
5816 * immediately or should we poll for it?
5817 */
5818 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5819}
5820
679dacd4 5821static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
30a970c6 5822{
679dacd4 5823 struct drm_device *dev = state->dev;
30a970c6 5824 struct drm_i915_private *dev_priv = dev->dev_private;
304603f4
ACO
5825 int max_pixclk = intel_mode_max_pixclk(state);
5826 int req_cdclk;
5827
5828 /* The only reason this can fail is if we fail to add the crtc_state
5829 * to the atomic state. But that can't happen since the call to
5830 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5831 * can't have failed otherwise the mode set would be aborted) added all
5832 * the states already. */
5833 if (WARN_ON(max_pixclk < 0))
5834 return;
30a970c6 5835
304603f4 5836 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
30a970c6 5837
164dfd28 5838 if (req_cdclk != dev_priv->cdclk_freq) {
738c05c0
ID
5839 /*
5840 * FIXME: We can end up here with all power domains off, yet
5841 * with a CDCLK frequency other than the minimum. To account
5842 * for this take the PIPE-A power domain, which covers the HW
5843 * blocks needed for the following programming. This can be
5844 * removed once it's guaranteed that we get here either with
5845 * the minimum CDCLK set, or the required power domains
5846 * enabled.
5847 */
5848 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5849
383c5a6a
VS
5850 if (IS_CHERRYVIEW(dev))
5851 cherryview_set_cdclk(dev, req_cdclk);
5852 else
5853 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 5854
1e69cd74
VS
5855 vlv_program_pfi_credits(dev_priv);
5856
738c05c0 5857 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
383c5a6a 5858 }
30a970c6
JB
5859}
5860
89b667f8
JB
5861static void valleyview_crtc_enable(struct drm_crtc *crtc)
5862{
5863 struct drm_device *dev = crtc->dev;
a72e4c9f 5864 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
5865 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5866 struct intel_encoder *encoder;
5867 int pipe = intel_crtc->pipe;
23538ef1 5868 bool is_dsi;
89b667f8 5869
83d65738 5870 WARN_ON(!crtc->state->enable);
89b667f8
JB
5871
5872 if (intel_crtc->active)
5873 return;
5874
409ee761 5875 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 5876
1ae0d137
VS
5877 if (!is_dsi) {
5878 if (IS_CHERRYVIEW(dev))
6e3c9717 5879 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5880 else
6e3c9717 5881 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5882 }
5b18e57c 5883
6e3c9717 5884 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5885 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5886
5887 intel_set_pipe_timings(intel_crtc);
5888
c14b0485
VS
5889 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5890 struct drm_i915_private *dev_priv = dev->dev_private;
5891
5892 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5893 I915_WRITE(CHV_CANVAS(pipe), 0);
5894 }
5895
5b18e57c
DV
5896 i9xx_set_pipeconf(intel_crtc);
5897
89b667f8 5898 intel_crtc->active = true;
89b667f8 5899
a72e4c9f 5900 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5901
89b667f8
JB
5902 for_each_encoder_on_crtc(dev, crtc, encoder)
5903 if (encoder->pre_pll_enable)
5904 encoder->pre_pll_enable(encoder);
5905
9d556c99
CML
5906 if (!is_dsi) {
5907 if (IS_CHERRYVIEW(dev))
6e3c9717 5908 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5909 else
6e3c9717 5910 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5911 }
89b667f8
JB
5912
5913 for_each_encoder_on_crtc(dev, crtc, encoder)
5914 if (encoder->pre_enable)
5915 encoder->pre_enable(encoder);
5916
2dd24552
JB
5917 i9xx_pfit_enable(intel_crtc);
5918
63cbb074
VS
5919 intel_crtc_load_lut(crtc);
5920
f37fcc2a 5921 intel_update_watermarks(crtc);
e1fdc473 5922 intel_enable_pipe(intel_crtc);
be6a6f8e 5923
4b3a9526
VS
5924 assert_vblank_disabled(crtc);
5925 drm_crtc_vblank_on(crtc);
5926
f9b61ff6
DV
5927 for_each_encoder_on_crtc(dev, crtc, encoder)
5928 encoder->enable(encoder);
89b667f8
JB
5929}
5930
f13c2ef3
DV
5931static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5932{
5933 struct drm_device *dev = crtc->base.dev;
5934 struct drm_i915_private *dev_priv = dev->dev_private;
5935
6e3c9717
ACO
5936 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5937 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
5938}
5939
0b8765c6 5940static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
5941{
5942 struct drm_device *dev = crtc->dev;
a72e4c9f 5943 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 5944 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5945 struct intel_encoder *encoder;
79e53945 5946 int pipe = intel_crtc->pipe;
79e53945 5947
83d65738 5948 WARN_ON(!crtc->state->enable);
08a48469 5949
f7abfe8b
CW
5950 if (intel_crtc->active)
5951 return;
5952
f13c2ef3
DV
5953 i9xx_set_pll_dividers(intel_crtc);
5954
6e3c9717 5955 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5956 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5957
5958 intel_set_pipe_timings(intel_crtc);
5959
5b18e57c
DV
5960 i9xx_set_pipeconf(intel_crtc);
5961
f7abfe8b 5962 intel_crtc->active = true;
6b383a7f 5963
4a3436e8 5964 if (!IS_GEN2(dev))
a72e4c9f 5965 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5966
9d6d9f19
MK
5967 for_each_encoder_on_crtc(dev, crtc, encoder)
5968 if (encoder->pre_enable)
5969 encoder->pre_enable(encoder);
5970
f6736a1a
DV
5971 i9xx_enable_pll(intel_crtc);
5972
2dd24552
JB
5973 i9xx_pfit_enable(intel_crtc);
5974
63cbb074
VS
5975 intel_crtc_load_lut(crtc);
5976
f37fcc2a 5977 intel_update_watermarks(crtc);
e1fdc473 5978 intel_enable_pipe(intel_crtc);
be6a6f8e 5979
4b3a9526
VS
5980 assert_vblank_disabled(crtc);
5981 drm_crtc_vblank_on(crtc);
5982
f9b61ff6
DV
5983 for_each_encoder_on_crtc(dev, crtc, encoder)
5984 encoder->enable(encoder);
0b8765c6 5985}
79e53945 5986
87476d63
DV
5987static void i9xx_pfit_disable(struct intel_crtc *crtc)
5988{
5989 struct drm_device *dev = crtc->base.dev;
5990 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 5991
6e3c9717 5992 if (!crtc->config->gmch_pfit.control)
328d8e82 5993 return;
87476d63 5994
328d8e82 5995 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 5996
328d8e82
DV
5997 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5998 I915_READ(PFIT_CONTROL));
5999 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6000}
6001
0b8765c6
JB
6002static void i9xx_crtc_disable(struct drm_crtc *crtc)
6003{
6004 struct drm_device *dev = crtc->dev;
6005 struct drm_i915_private *dev_priv = dev->dev_private;
6006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6007 struct intel_encoder *encoder;
0b8765c6 6008 int pipe = intel_crtc->pipe;
ef9c3aee 6009
f7abfe8b
CW
6010 if (!intel_crtc->active)
6011 return;
6012
6304cd91
VS
6013 /*
6014 * On gen2 planes are double buffered but the pipe isn't, so we must
6015 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6016 * We also need to wait on all gmch platforms because of the
6017 * self-refresh mode constraint explained above.
6304cd91 6018 */
564ed191 6019 intel_wait_for_vblank(dev, pipe);
6304cd91 6020
4b3a9526
VS
6021 for_each_encoder_on_crtc(dev, crtc, encoder)
6022 encoder->disable(encoder);
6023
f9b61ff6
DV
6024 drm_crtc_vblank_off(crtc);
6025 assert_vblank_disabled(crtc);
6026
575f7ab7 6027 intel_disable_pipe(intel_crtc);
24a1f16d 6028
87476d63 6029 i9xx_pfit_disable(intel_crtc);
24a1f16d 6030
89b667f8
JB
6031 for_each_encoder_on_crtc(dev, crtc, encoder)
6032 if (encoder->post_disable)
6033 encoder->post_disable(encoder);
6034
409ee761 6035 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6036 if (IS_CHERRYVIEW(dev))
6037 chv_disable_pll(dev_priv, pipe);
6038 else if (IS_VALLEYVIEW(dev))
6039 vlv_disable_pll(dev_priv, pipe);
6040 else
1c4e0274 6041 i9xx_disable_pll(intel_crtc);
076ed3b2 6042 }
0b8765c6 6043
4a3436e8 6044 if (!IS_GEN2(dev))
a72e4c9f 6045 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 6046
f7abfe8b 6047 intel_crtc->active = false;
46ba614c 6048 intel_update_watermarks(crtc);
f37fcc2a 6049
efa9624e 6050 mutex_lock(&dev->struct_mutex);
7ff0ebcc 6051 intel_fbc_update(dev);
efa9624e 6052 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
6053}
6054
ee7b9f93
JB
6055static void i9xx_crtc_off(struct drm_crtc *crtc)
6056{
6057}
6058
b04c5bd6
BF
6059/* Master function to enable/disable CRTC and corresponding power wells */
6060void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
6061{
6062 struct drm_device *dev = crtc->dev;
6063 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 6064 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
6065 enum intel_display_power_domain domain;
6066 unsigned long domains;
976f8a20 6067
0e572fe7
DV
6068 if (enable) {
6069 if (!intel_crtc->active) {
e1e9fb84
DV
6070 domains = get_crtc_power_domains(crtc);
6071 for_each_power_domain(domain, domains)
6072 intel_display_power_get(dev_priv, domain);
6073 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
6074
6075 dev_priv->display.crtc_enable(crtc);
ce22dba9 6076 intel_crtc_enable_planes(crtc);
0e572fe7
DV
6077 }
6078 } else {
6079 if (intel_crtc->active) {
ce22dba9 6080 intel_crtc_disable_planes(crtc);
0e572fe7
DV
6081 dev_priv->display.crtc_disable(crtc);
6082
e1e9fb84
DV
6083 domains = intel_crtc->enabled_power_domains;
6084 for_each_power_domain(domain, domains)
6085 intel_display_power_put(dev_priv, domain);
6086 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
6087 }
6088 }
b04c5bd6
BF
6089}
6090
6091/**
6092 * Sets the power management mode of the pipe and plane.
6093 */
6094void intel_crtc_update_dpms(struct drm_crtc *crtc)
6095{
6096 struct drm_device *dev = crtc->dev;
6097 struct intel_encoder *intel_encoder;
6098 bool enable = false;
6099
6100 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6101 enable |= intel_encoder->connectors_active;
6102
6103 intel_crtc_control(crtc, enable);
976f8a20
DV
6104}
6105
cdd59983
CW
6106static void intel_crtc_disable(struct drm_crtc *crtc)
6107{
cdd59983 6108 struct drm_device *dev = crtc->dev;
976f8a20 6109 struct drm_connector *connector;
ee7b9f93 6110 struct drm_i915_private *dev_priv = dev->dev_private;
cdd59983 6111
976f8a20 6112 /* crtc should still be enabled when we disable it. */
83d65738 6113 WARN_ON(!crtc->state->enable);
976f8a20 6114
ce22dba9 6115 intel_crtc_disable_planes(crtc);
976f8a20 6116 dev_priv->display.crtc_disable(crtc);
ee7b9f93
JB
6117 dev_priv->display.off(crtc);
6118
70a101f8 6119 drm_plane_helper_disable(crtc->primary);
976f8a20
DV
6120
6121 /* Update computed state. */
6122 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6123 if (!connector->encoder || !connector->encoder->crtc)
6124 continue;
6125
6126 if (connector->encoder->crtc != crtc)
6127 continue;
6128
6129 connector->dpms = DRM_MODE_DPMS_OFF;
6130 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
6131 }
6132}
6133
ea5b213a 6134void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6135{
4ef69c7a 6136 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6137
ea5b213a
CW
6138 drm_encoder_cleanup(encoder);
6139 kfree(intel_encoder);
7e7d76c3
JB
6140}
6141
9237329d 6142/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
6143 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6144 * state of the entire output pipe. */
9237329d 6145static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 6146{
5ab432ef
DV
6147 if (mode == DRM_MODE_DPMS_ON) {
6148 encoder->connectors_active = true;
6149
b2cabb0e 6150 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
6151 } else {
6152 encoder->connectors_active = false;
6153
b2cabb0e 6154 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 6155 }
79e53945
JB
6156}
6157
0a91ca29
DV
6158/* Cross check the actual hw state with our own modeset state tracking (and it's
6159 * internal consistency). */
b980514c 6160static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6161{
0a91ca29
DV
6162 if (connector->get_hw_state(connector)) {
6163 struct intel_encoder *encoder = connector->encoder;
6164 struct drm_crtc *crtc;
6165 bool encoder_enabled;
6166 enum pipe pipe;
6167
6168 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6169 connector->base.base.id,
c23cc417 6170 connector->base.name);
0a91ca29 6171
0e32b39c
DA
6172 /* there is no real hw state for MST connectors */
6173 if (connector->mst_port)
6174 return;
6175
e2c719b7 6176 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 6177 "wrong connector dpms state\n");
e2c719b7 6178 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 6179 "active connector not linked to encoder\n");
0a91ca29 6180
36cd7444 6181 if (encoder) {
e2c719b7 6182 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
6183 "encoder->connectors_active not set\n");
6184
6185 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
6186 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6187 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 6188 return;
0a91ca29 6189
36cd7444 6190 crtc = encoder->base.crtc;
0a91ca29 6191
83d65738
MR
6192 I915_STATE_WARN(!crtc->state->enable,
6193 "crtc not enabled\n");
e2c719b7
RC
6194 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6195 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
6196 "encoder active on the wrong pipe\n");
6197 }
0a91ca29 6198 }
79e53945
JB
6199}
6200
08d9bc92
ACO
6201int intel_connector_init(struct intel_connector *connector)
6202{
6203 struct drm_connector_state *connector_state;
6204
6205 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6206 if (!connector_state)
6207 return -ENOMEM;
6208
6209 connector->base.state = connector_state;
6210 return 0;
6211}
6212
6213struct intel_connector *intel_connector_alloc(void)
6214{
6215 struct intel_connector *connector;
6216
6217 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6218 if (!connector)
6219 return NULL;
6220
6221 if (intel_connector_init(connector) < 0) {
6222 kfree(connector);
6223 return NULL;
6224 }
6225
6226 return connector;
6227}
6228
5ab432ef
DV
6229/* Even simpler default implementation, if there's really no special case to
6230 * consider. */
6231void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 6232{
5ab432ef
DV
6233 /* All the simple cases only support two dpms states. */
6234 if (mode != DRM_MODE_DPMS_ON)
6235 mode = DRM_MODE_DPMS_OFF;
d4270e57 6236
5ab432ef
DV
6237 if (mode == connector->dpms)
6238 return;
6239
6240 connector->dpms = mode;
6241
6242 /* Only need to change hw state when actually enabled */
c9976dcf
CW
6243 if (connector->encoder)
6244 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 6245
b980514c 6246 intel_modeset_check_state(connector->dev);
79e53945
JB
6247}
6248
f0947c37
DV
6249/* Simple connector->get_hw_state implementation for encoders that support only
6250 * one connector and no cloning and hence the encoder state determines the state
6251 * of the connector. */
6252bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6253{
24929352 6254 enum pipe pipe = 0;
f0947c37 6255 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6256
f0947c37 6257 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6258}
6259
6d293983 6260static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6261{
6d293983
ACO
6262 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6263 return crtc_state->fdi_lanes;
d272ddfa
VS
6264
6265 return 0;
6266}
6267
6d293983 6268static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6269 struct intel_crtc_state *pipe_config)
1857e1da 6270{
6d293983
ACO
6271 struct drm_atomic_state *state = pipe_config->base.state;
6272 struct intel_crtc *other_crtc;
6273 struct intel_crtc_state *other_crtc_state;
6274
1857e1da
DV
6275 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6276 pipe_name(pipe), pipe_config->fdi_lanes);
6277 if (pipe_config->fdi_lanes > 4) {
6278 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6279 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6280 return -EINVAL;
1857e1da
DV
6281 }
6282
bafb6553 6283 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6284 if (pipe_config->fdi_lanes > 2) {
6285 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6286 pipe_config->fdi_lanes);
6d293983 6287 return -EINVAL;
1857e1da 6288 } else {
6d293983 6289 return 0;
1857e1da
DV
6290 }
6291 }
6292
6293 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6294 return 0;
1857e1da
DV
6295
6296 /* Ivybridge 3 pipe is really complicated */
6297 switch (pipe) {
6298 case PIPE_A:
6d293983 6299 return 0;
1857e1da 6300 case PIPE_B:
6d293983
ACO
6301 if (pipe_config->fdi_lanes <= 2)
6302 return 0;
6303
6304 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6305 other_crtc_state =
6306 intel_atomic_get_crtc_state(state, other_crtc);
6307 if (IS_ERR(other_crtc_state))
6308 return PTR_ERR(other_crtc_state);
6309
6310 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6311 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6312 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6313 return -EINVAL;
1857e1da 6314 }
6d293983 6315 return 0;
1857e1da 6316 case PIPE_C:
251cc67c
VS
6317 if (pipe_config->fdi_lanes > 2) {
6318 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6319 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6320 return -EINVAL;
251cc67c 6321 }
6d293983
ACO
6322
6323 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6324 other_crtc_state =
6325 intel_atomic_get_crtc_state(state, other_crtc);
6326 if (IS_ERR(other_crtc_state))
6327 return PTR_ERR(other_crtc_state);
6328
6329 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6330 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6331 return -EINVAL;
1857e1da 6332 }
6d293983 6333 return 0;
1857e1da
DV
6334 default:
6335 BUG();
6336 }
6337}
6338
e29c22c0
DV
6339#define RETRY 1
6340static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6341 struct intel_crtc_state *pipe_config)
877d48d5 6342{
1857e1da 6343 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 6344 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6345 int lane, link_bw, fdi_dotclock, ret;
6346 bool needs_recompute = false;
877d48d5 6347
e29c22c0 6348retry:
877d48d5
DV
6349 /* FDI is a binary signal running at ~2.7GHz, encoding
6350 * each output octet as 10 bits. The actual frequency
6351 * is stored as a divider into a 100MHz clock, and the
6352 * mode pixel clock is stored in units of 1KHz.
6353 * Hence the bw of each lane in terms of the mode signal
6354 * is:
6355 */
6356 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6357
241bfc38 6358 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6359
2bd89a07 6360 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6361 pipe_config->pipe_bpp);
6362
6363 pipe_config->fdi_lanes = lane;
6364
2bd89a07 6365 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6366 link_bw, &pipe_config->fdi_m_n);
1857e1da 6367
6d293983
ACO
6368 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6369 intel_crtc->pipe, pipe_config);
6370 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6371 pipe_config->pipe_bpp -= 2*3;
6372 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6373 pipe_config->pipe_bpp);
6374 needs_recompute = true;
6375 pipe_config->bw_constrained = true;
6376
6377 goto retry;
6378 }
6379
6380 if (needs_recompute)
6381 return RETRY;
6382
6d293983 6383 return ret;
877d48d5
DV
6384}
6385
42db64ef 6386static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6387 struct intel_crtc_state *pipe_config)
42db64ef 6388{
d330a953 6389 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 6390 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 6391 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
6392}
6393
a43f6e0f 6394static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6395 struct intel_crtc_state *pipe_config)
79e53945 6396{
a43f6e0f 6397 struct drm_device *dev = crtc->base.dev;
8bd31e67 6398 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 6399 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
d03c93d4 6400 int ret;
89749350 6401
ad3a4479 6402 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6403 if (INTEL_INFO(dev)->gen < 4) {
cf532bb2
VS
6404 int clock_limit =
6405 dev_priv->display.get_display_clock_speed(dev);
6406
6407 /*
6408 * Enable pixel doubling when the dot clock
6409 * is > 90% of the (display) core speed.
6410 *
b397c96b
VS
6411 * GDG double wide on either pipe,
6412 * otherwise pipe A only.
cf532bb2 6413 */
b397c96b 6414 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 6415 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 6416 clock_limit *= 2;
cf532bb2 6417 pipe_config->double_wide = true;
ad3a4479
VS
6418 }
6419
241bfc38 6420 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 6421 return -EINVAL;
2c07245f 6422 }
89749350 6423
1d1d0e27
VS
6424 /*
6425 * Pipe horizontal size must be even in:
6426 * - DVO ganged mode
6427 * - LVDS dual channel mode
6428 * - Double wide pipe
6429 */
a93e255f 6430 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6431 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6432 pipe_config->pipe_src_w &= ~1;
6433
8693a824
DL
6434 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6435 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6436 */
6437 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6438 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 6439 return -EINVAL;
44f46b42 6440
f5adf94e 6441 if (HAS_IPS(dev))
a43f6e0f
DV
6442 hsw_compute_ips_config(crtc, pipe_config);
6443
877d48d5 6444 if (pipe_config->has_pch_encoder)
a43f6e0f 6445 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6446
d03c93d4
CK
6447 /* FIXME: remove below call once atomic mode set is place and all crtc
6448 * related checks called from atomic_crtc_check function */
6449 ret = 0;
6450 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6451 crtc, pipe_config->base.state);
6452 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6453
6454 return ret;
79e53945
JB
6455}
6456
1652d19e
VS
6457static int skylake_get_display_clock_speed(struct drm_device *dev)
6458{
6459 struct drm_i915_private *dev_priv = to_i915(dev);
6460 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6461 uint32_t cdctl = I915_READ(CDCLK_CTL);
6462 uint32_t linkrate;
6463
6464 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6465 WARN(1, "LCPLL1 not enabled\n");
6466 return 24000; /* 24MHz is the cd freq with NSSC ref */
6467 }
6468
6469 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6470 return 540000;
6471
6472 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6473 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6474
71cd8423
DL
6475 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6476 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6477 /* vco 8640 */
6478 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6479 case CDCLK_FREQ_450_432:
6480 return 432000;
6481 case CDCLK_FREQ_337_308:
6482 return 308570;
6483 case CDCLK_FREQ_675_617:
6484 return 617140;
6485 default:
6486 WARN(1, "Unknown cd freq selection\n");
6487 }
6488 } else {
6489 /* vco 8100 */
6490 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6491 case CDCLK_FREQ_450_432:
6492 return 450000;
6493 case CDCLK_FREQ_337_308:
6494 return 337500;
6495 case CDCLK_FREQ_675_617:
6496 return 675000;
6497 default:
6498 WARN(1, "Unknown cd freq selection\n");
6499 }
6500 }
6501
6502 /* error case, do as if DPLL0 isn't enabled */
6503 return 24000;
6504}
6505
6506static int broadwell_get_display_clock_speed(struct drm_device *dev)
6507{
6508 struct drm_i915_private *dev_priv = dev->dev_private;
6509 uint32_t lcpll = I915_READ(LCPLL_CTL);
6510 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6511
6512 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6513 return 800000;
6514 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6515 return 450000;
6516 else if (freq == LCPLL_CLK_FREQ_450)
6517 return 450000;
6518 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6519 return 540000;
6520 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6521 return 337500;
6522 else
6523 return 675000;
6524}
6525
6526static int haswell_get_display_clock_speed(struct drm_device *dev)
6527{
6528 struct drm_i915_private *dev_priv = dev->dev_private;
6529 uint32_t lcpll = I915_READ(LCPLL_CTL);
6530 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6531
6532 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6533 return 800000;
6534 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6535 return 450000;
6536 else if (freq == LCPLL_CLK_FREQ_450)
6537 return 450000;
6538 else if (IS_HSW_ULT(dev))
6539 return 337500;
6540 else
6541 return 540000;
79e53945
JB
6542}
6543
25eb05fc
JB
6544static int valleyview_get_display_clock_speed(struct drm_device *dev)
6545{
d197b7d3 6546 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
6547 u32 val;
6548 int divider;
6549
6bcda4f0
VS
6550 if (dev_priv->hpll_freq == 0)
6551 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6552
d197b7d3
VS
6553 mutex_lock(&dev_priv->dpio_lock);
6554 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6555 mutex_unlock(&dev_priv->dpio_lock);
6556
6557 divider = val & DISPLAY_FREQUENCY_VALUES;
6558
7d007f40
VS
6559 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6560 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6561 "cdclk change in progress\n");
6562
6bcda4f0 6563 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
6564}
6565
b37a6434
VS
6566static int ilk_get_display_clock_speed(struct drm_device *dev)
6567{
6568 return 450000;
6569}
6570
e70236a8
JB
6571static int i945_get_display_clock_speed(struct drm_device *dev)
6572{
6573 return 400000;
6574}
79e53945 6575
e70236a8 6576static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6577{
e907f170 6578 return 333333;
e70236a8 6579}
79e53945 6580
e70236a8
JB
6581static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6582{
6583 return 200000;
6584}
79e53945 6585
257a7ffc
DV
6586static int pnv_get_display_clock_speed(struct drm_device *dev)
6587{
6588 u16 gcfgc = 0;
6589
6590 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6591
6592 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6593 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6594 return 266667;
257a7ffc 6595 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6596 return 333333;
257a7ffc 6597 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6598 return 444444;
257a7ffc
DV
6599 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6600 return 200000;
6601 default:
6602 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6603 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6604 return 133333;
257a7ffc 6605 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6606 return 166667;
257a7ffc
DV
6607 }
6608}
6609
e70236a8
JB
6610static int i915gm_get_display_clock_speed(struct drm_device *dev)
6611{
6612 u16 gcfgc = 0;
79e53945 6613
e70236a8
JB
6614 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6615
6616 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6617 return 133333;
e70236a8
JB
6618 else {
6619 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6620 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6621 return 333333;
e70236a8
JB
6622 default:
6623 case GC_DISPLAY_CLOCK_190_200_MHZ:
6624 return 190000;
79e53945 6625 }
e70236a8
JB
6626 }
6627}
6628
6629static int i865_get_display_clock_speed(struct drm_device *dev)
6630{
e907f170 6631 return 266667;
e70236a8
JB
6632}
6633
6634static int i855_get_display_clock_speed(struct drm_device *dev)
6635{
6636 u16 hpllcc = 0;
6637 /* Assume that the hardware is in the high speed state. This
6638 * should be the default.
6639 */
6640 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6641 case GC_CLOCK_133_200:
6642 case GC_CLOCK_100_200:
6643 return 200000;
6644 case GC_CLOCK_166_250:
6645 return 250000;
6646 case GC_CLOCK_100_133:
e907f170 6647 return 133333;
e70236a8 6648 }
79e53945 6649
e70236a8
JB
6650 /* Shouldn't happen */
6651 return 0;
6652}
79e53945 6653
e70236a8
JB
6654static int i830_get_display_clock_speed(struct drm_device *dev)
6655{
e907f170 6656 return 133333;
79e53945
JB
6657}
6658
2c07245f 6659static void
a65851af 6660intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6661{
a65851af
VS
6662 while (*num > DATA_LINK_M_N_MASK ||
6663 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6664 *num >>= 1;
6665 *den >>= 1;
6666 }
6667}
6668
a65851af
VS
6669static void compute_m_n(unsigned int m, unsigned int n,
6670 uint32_t *ret_m, uint32_t *ret_n)
6671{
6672 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6673 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6674 intel_reduce_m_n_ratio(ret_m, ret_n);
6675}
6676
e69d0bc1
DV
6677void
6678intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6679 int pixel_clock, int link_clock,
6680 struct intel_link_m_n *m_n)
2c07245f 6681{
e69d0bc1 6682 m_n->tu = 64;
a65851af
VS
6683
6684 compute_m_n(bits_per_pixel * pixel_clock,
6685 link_clock * nlanes * 8,
6686 &m_n->gmch_m, &m_n->gmch_n);
6687
6688 compute_m_n(pixel_clock, link_clock,
6689 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
6690}
6691
a7615030
CW
6692static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6693{
d330a953
JN
6694 if (i915.panel_use_ssc >= 0)
6695 return i915.panel_use_ssc != 0;
41aa3448 6696 return dev_priv->vbt.lvds_use_ssc
435793df 6697 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6698}
6699
a93e255f
ACO
6700static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6701 int num_connectors)
c65d77d8 6702{
a93e255f 6703 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
6704 struct drm_i915_private *dev_priv = dev->dev_private;
6705 int refclk;
6706
a93e255f
ACO
6707 WARN_ON(!crtc_state->base.state);
6708
5ab7b0b7 6709 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 6710 refclk = 100000;
a93e255f 6711 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 6712 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
6713 refclk = dev_priv->vbt.lvds_ssc_freq;
6714 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
6715 } else if (!IS_GEN2(dev)) {
6716 refclk = 96000;
6717 } else {
6718 refclk = 48000;
6719 }
6720
6721 return refclk;
6722}
6723
7429e9d4 6724static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6725{
7df00d7a 6726 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6727}
f47709a9 6728
7429e9d4
DV
6729static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6730{
6731 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6732}
6733
f47709a9 6734static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6735 struct intel_crtc_state *crtc_state,
a7516a05
JB
6736 intel_clock_t *reduced_clock)
6737{
f47709a9 6738 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
6739 u32 fp, fp2 = 0;
6740
6741 if (IS_PINEVIEW(dev)) {
190f68c5 6742 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6743 if (reduced_clock)
7429e9d4 6744 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6745 } else {
190f68c5 6746 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6747 if (reduced_clock)
7429e9d4 6748 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6749 }
6750
190f68c5 6751 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6752
f47709a9 6753 crtc->lowfreq_avail = false;
a93e255f 6754 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6755 reduced_clock) {
190f68c5 6756 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 6757 crtc->lowfreq_avail = true;
a7516a05 6758 } else {
190f68c5 6759 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6760 }
6761}
6762
5e69f97f
CML
6763static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6764 pipe)
89b667f8
JB
6765{
6766 u32 reg_val;
6767
6768 /*
6769 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6770 * and set it to a reasonable value instead.
6771 */
ab3c759a 6772 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6773 reg_val &= 0xffffff00;
6774 reg_val |= 0x00000030;
ab3c759a 6775 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6776
ab3c759a 6777 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6778 reg_val &= 0x8cffffff;
6779 reg_val = 0x8c000000;
ab3c759a 6780 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6781
ab3c759a 6782 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6783 reg_val &= 0xffffff00;
ab3c759a 6784 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6785
ab3c759a 6786 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6787 reg_val &= 0x00ffffff;
6788 reg_val |= 0xb0000000;
ab3c759a 6789 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6790}
6791
b551842d
DV
6792static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6793 struct intel_link_m_n *m_n)
6794{
6795 struct drm_device *dev = crtc->base.dev;
6796 struct drm_i915_private *dev_priv = dev->dev_private;
6797 int pipe = crtc->pipe;
6798
e3b95f1e
DV
6799 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6800 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6801 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6802 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6803}
6804
6805static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6806 struct intel_link_m_n *m_n,
6807 struct intel_link_m_n *m2_n2)
b551842d
DV
6808{
6809 struct drm_device *dev = crtc->base.dev;
6810 struct drm_i915_private *dev_priv = dev->dev_private;
6811 int pipe = crtc->pipe;
6e3c9717 6812 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
6813
6814 if (INTEL_INFO(dev)->gen >= 5) {
6815 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6816 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6817 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6818 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6819 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6820 * for gen < 8) and if DRRS is supported (to make sure the
6821 * registers are not unnecessarily accessed).
6822 */
44395bfe 6823 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 6824 crtc->config->has_drrs) {
f769cd24
VK
6825 I915_WRITE(PIPE_DATA_M2(transcoder),
6826 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6827 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6828 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6829 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6830 }
b551842d 6831 } else {
e3b95f1e
DV
6832 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6833 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6834 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6835 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6836 }
6837}
6838
fe3cd48d 6839void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6840{
fe3cd48d
R
6841 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6842
6843 if (m_n == M1_N1) {
6844 dp_m_n = &crtc->config->dp_m_n;
6845 dp_m2_n2 = &crtc->config->dp_m2_n2;
6846 } else if (m_n == M2_N2) {
6847
6848 /*
6849 * M2_N2 registers are not supported. Hence m2_n2 divider value
6850 * needs to be programmed into M1_N1.
6851 */
6852 dp_m_n = &crtc->config->dp_m2_n2;
6853 } else {
6854 DRM_ERROR("Unsupported divider value\n");
6855 return;
6856 }
6857
6e3c9717
ACO
6858 if (crtc->config->has_pch_encoder)
6859 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6860 else
fe3cd48d 6861 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6862}
6863
d288f65f 6864static void vlv_update_pll(struct intel_crtc *crtc,
5cec258b 6865 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
6866{
6867 u32 dpll, dpll_md;
6868
6869 /*
6870 * Enable DPIO clock input. We should never disable the reference
6871 * clock for pipe B, since VGA hotplug / manual detection depends
6872 * on it.
6873 */
6874 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6875 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6876 /* We should never disable this, set it here for state tracking */
6877 if (crtc->pipe == PIPE_B)
6878 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6879 dpll |= DPLL_VCO_ENABLE;
d288f65f 6880 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 6881
d288f65f 6882 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 6883 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 6884 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
6885}
6886
d288f65f 6887static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6888 const struct intel_crtc_state *pipe_config)
a0c4da24 6889{
f47709a9 6890 struct drm_device *dev = crtc->base.dev;
a0c4da24 6891 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 6892 int pipe = crtc->pipe;
bdd4b6a6 6893 u32 mdiv;
a0c4da24 6894 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6895 u32 coreclk, reg_val;
a0c4da24 6896
09153000
DV
6897 mutex_lock(&dev_priv->dpio_lock);
6898
d288f65f
VS
6899 bestn = pipe_config->dpll.n;
6900 bestm1 = pipe_config->dpll.m1;
6901 bestm2 = pipe_config->dpll.m2;
6902 bestp1 = pipe_config->dpll.p1;
6903 bestp2 = pipe_config->dpll.p2;
a0c4da24 6904
89b667f8
JB
6905 /* See eDP HDMI DPIO driver vbios notes doc */
6906
6907 /* PLL B needs special handling */
bdd4b6a6 6908 if (pipe == PIPE_B)
5e69f97f 6909 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6910
6911 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6912 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6913
6914 /* Disable target IRef on PLL */
ab3c759a 6915 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6916 reg_val &= 0x00ffffff;
ab3c759a 6917 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6918
6919 /* Disable fast lock */
ab3c759a 6920 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6921
6922 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6923 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6924 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6925 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6926 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6927
6928 /*
6929 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6930 * but we don't support that).
6931 * Note: don't use the DAC post divider as it seems unstable.
6932 */
6933 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6934 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6935
a0c4da24 6936 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6937 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6938
89b667f8 6939 /* Set HBR and RBR LPF coefficients */
d288f65f 6940 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
6941 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6942 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 6943 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6944 0x009f0003);
89b667f8 6945 else
ab3c759a 6946 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6947 0x00d0000f);
6948
681a8504 6949 if (pipe_config->has_dp_encoder) {
89b667f8 6950 /* Use SSC source */
bdd4b6a6 6951 if (pipe == PIPE_A)
ab3c759a 6952 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6953 0x0df40000);
6954 else
ab3c759a 6955 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6956 0x0df70000);
6957 } else { /* HDMI or VGA */
6958 /* Use bend source */
bdd4b6a6 6959 if (pipe == PIPE_A)
ab3c759a 6960 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6961 0x0df70000);
6962 else
ab3c759a 6963 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6964 0x0df40000);
6965 }
a0c4da24 6966
ab3c759a 6967 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6968 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
6969 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6970 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 6971 coreclk |= 0x01000000;
ab3c759a 6972 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6973
ab3c759a 6974 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 6975 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
6976}
6977
d288f65f 6978static void chv_update_pll(struct intel_crtc *crtc,
5cec258b 6979 struct intel_crtc_state *pipe_config)
1ae0d137 6980{
d288f65f 6981 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
6982 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6983 DPLL_VCO_ENABLE;
6984 if (crtc->pipe != PIPE_A)
d288f65f 6985 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 6986
d288f65f
VS
6987 pipe_config->dpll_hw_state.dpll_md =
6988 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
6989}
6990
d288f65f 6991static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6992 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6993{
6994 struct drm_device *dev = crtc->base.dev;
6995 struct drm_i915_private *dev_priv = dev->dev_private;
6996 int pipe = crtc->pipe;
6997 int dpll_reg = DPLL(crtc->pipe);
6998 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 6999 u32 loopfilter, tribuf_calcntr;
9d556c99 7000 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7001 u32 dpio_val;
9cbe40c1 7002 int vco;
9d556c99 7003
d288f65f
VS
7004 bestn = pipe_config->dpll.n;
7005 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7006 bestm1 = pipe_config->dpll.m1;
7007 bestm2 = pipe_config->dpll.m2 >> 22;
7008 bestp1 = pipe_config->dpll.p1;
7009 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7010 vco = pipe_config->dpll.vco;
a945ce7e 7011 dpio_val = 0;
9cbe40c1 7012 loopfilter = 0;
9d556c99
CML
7013
7014 /*
7015 * Enable Refclk and SSC
7016 */
a11b0703 7017 I915_WRITE(dpll_reg,
d288f65f 7018 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703
VS
7019
7020 mutex_lock(&dev_priv->dpio_lock);
9d556c99 7021
9d556c99
CML
7022 /* p1 and p2 divider */
7023 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7024 5 << DPIO_CHV_S1_DIV_SHIFT |
7025 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7026 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7027 1 << DPIO_CHV_K_DIV_SHIFT);
7028
7029 /* Feedback post-divider - m2 */
7030 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7031
7032 /* Feedback refclk divider - n and m1 */
7033 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7034 DPIO_CHV_M1_DIV_BY_2 |
7035 1 << DPIO_CHV_N_DIV_SHIFT);
7036
7037 /* M2 fraction division */
a945ce7e
VP
7038 if (bestm2_frac)
7039 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7040
7041 /* M2 fraction division enable */
a945ce7e
VP
7042 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7043 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7044 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7045 if (bestm2_frac)
7046 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7047 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7048
de3a0fde
VP
7049 /* Program digital lock detect threshold */
7050 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7051 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7052 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7053 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7054 if (!bestm2_frac)
7055 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7056 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7057
9d556c99 7058 /* Loop filter */
9cbe40c1
VP
7059 if (vco == 5400000) {
7060 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7061 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7062 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7063 tribuf_calcntr = 0x9;
7064 } else if (vco <= 6200000) {
7065 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7066 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7067 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7068 tribuf_calcntr = 0x9;
7069 } else if (vco <= 6480000) {
7070 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7071 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7072 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7073 tribuf_calcntr = 0x8;
7074 } else {
7075 /* Not supported. Apply the same limits as in the max case */
7076 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7077 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7078 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7079 tribuf_calcntr = 0;
7080 }
9d556c99
CML
7081 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7082
968040b2 7083 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7084 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7085 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7086 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7087
9d556c99
CML
7088 /* AFC Recal */
7089 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7090 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7091 DPIO_AFC_RECAL);
7092
7093 mutex_unlock(&dev_priv->dpio_lock);
7094}
7095
d288f65f
VS
7096/**
7097 * vlv_force_pll_on - forcibly enable just the PLL
7098 * @dev_priv: i915 private structure
7099 * @pipe: pipe PLL to enable
7100 * @dpll: PLL configuration
7101 *
7102 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7103 * in cases where we need the PLL enabled even when @pipe is not going to
7104 * be enabled.
7105 */
7106void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7107 const struct dpll *dpll)
7108{
7109 struct intel_crtc *crtc =
7110 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 7111 struct intel_crtc_state pipe_config = {
a93e255f 7112 .base.crtc = &crtc->base,
d288f65f
VS
7113 .pixel_multiplier = 1,
7114 .dpll = *dpll,
7115 };
7116
7117 if (IS_CHERRYVIEW(dev)) {
7118 chv_update_pll(crtc, &pipe_config);
7119 chv_prepare_pll(crtc, &pipe_config);
7120 chv_enable_pll(crtc, &pipe_config);
7121 } else {
7122 vlv_update_pll(crtc, &pipe_config);
7123 vlv_prepare_pll(crtc, &pipe_config);
7124 vlv_enable_pll(crtc, &pipe_config);
7125 }
7126}
7127
7128/**
7129 * vlv_force_pll_off - forcibly disable just the PLL
7130 * @dev_priv: i915 private structure
7131 * @pipe: pipe PLL to disable
7132 *
7133 * Disable the PLL for @pipe. To be used in cases where we need
7134 * the PLL enabled even when @pipe is not going to be enabled.
7135 */
7136void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7137{
7138 if (IS_CHERRYVIEW(dev))
7139 chv_disable_pll(to_i915(dev), pipe);
7140 else
7141 vlv_disable_pll(to_i915(dev), pipe);
7142}
7143
f47709a9 7144static void i9xx_update_pll(struct intel_crtc *crtc,
190f68c5 7145 struct intel_crtc_state *crtc_state,
f47709a9 7146 intel_clock_t *reduced_clock,
eb1cbe48
DV
7147 int num_connectors)
7148{
f47709a9 7149 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7150 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7151 u32 dpll;
7152 bool is_sdvo;
190f68c5 7153 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7154
190f68c5 7155 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7156
a93e255f
ACO
7157 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7158 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7159
7160 dpll = DPLL_VGA_MODE_DIS;
7161
a93e255f 7162 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7163 dpll |= DPLLB_MODE_LVDS;
7164 else
7165 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7166
ef1b460d 7167 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7168 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7169 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7170 }
198a037f
DV
7171
7172 if (is_sdvo)
4a33e48d 7173 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7174
190f68c5 7175 if (crtc_state->has_dp_encoder)
4a33e48d 7176 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7177
7178 /* compute bitmask from p1 value */
7179 if (IS_PINEVIEW(dev))
7180 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7181 else {
7182 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7183 if (IS_G4X(dev) && reduced_clock)
7184 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7185 }
7186 switch (clock->p2) {
7187 case 5:
7188 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7189 break;
7190 case 7:
7191 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7192 break;
7193 case 10:
7194 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7195 break;
7196 case 14:
7197 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7198 break;
7199 }
7200 if (INTEL_INFO(dev)->gen >= 4)
7201 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7202
190f68c5 7203 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7204 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7205 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7206 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7207 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7208 else
7209 dpll |= PLL_REF_INPUT_DREFCLK;
7210
7211 dpll |= DPLL_VCO_ENABLE;
190f68c5 7212 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7213
eb1cbe48 7214 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7215 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7216 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7217 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7218 }
7219}
7220
f47709a9 7221static void i8xx_update_pll(struct intel_crtc *crtc,
190f68c5 7222 struct intel_crtc_state *crtc_state,
f47709a9 7223 intel_clock_t *reduced_clock,
eb1cbe48
DV
7224 int num_connectors)
7225{
f47709a9 7226 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7227 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7228 u32 dpll;
190f68c5 7229 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7230
190f68c5 7231 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7232
eb1cbe48
DV
7233 dpll = DPLL_VGA_MODE_DIS;
7234
a93e255f 7235 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7236 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7237 } else {
7238 if (clock->p1 == 2)
7239 dpll |= PLL_P1_DIVIDE_BY_TWO;
7240 else
7241 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7242 if (clock->p2 == 4)
7243 dpll |= PLL_P2_DIVIDE_BY_4;
7244 }
7245
a93e255f 7246 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7247 dpll |= DPLL_DVO_2X_MODE;
7248
a93e255f 7249 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7250 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7251 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7252 else
7253 dpll |= PLL_REF_INPUT_DREFCLK;
7254
7255 dpll |= DPLL_VCO_ENABLE;
190f68c5 7256 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7257}
7258
8a654f3b 7259static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7260{
7261 struct drm_device *dev = intel_crtc->base.dev;
7262 struct drm_i915_private *dev_priv = dev->dev_private;
7263 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7264 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 7265 struct drm_display_mode *adjusted_mode =
6e3c9717 7266 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7267 uint32_t crtc_vtotal, crtc_vblank_end;
7268 int vsyncshift = 0;
4d8a62ea
DV
7269
7270 /* We need to be careful not to changed the adjusted mode, for otherwise
7271 * the hw state checker will get angry at the mismatch. */
7272 crtc_vtotal = adjusted_mode->crtc_vtotal;
7273 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7274
609aeaca 7275 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7276 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7277 crtc_vtotal -= 1;
7278 crtc_vblank_end -= 1;
609aeaca 7279
409ee761 7280 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7281 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7282 else
7283 vsyncshift = adjusted_mode->crtc_hsync_start -
7284 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7285 if (vsyncshift < 0)
7286 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7287 }
7288
7289 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7290 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7291
fe2b8f9d 7292 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7293 (adjusted_mode->crtc_hdisplay - 1) |
7294 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7295 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7296 (adjusted_mode->crtc_hblank_start - 1) |
7297 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7298 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7299 (adjusted_mode->crtc_hsync_start - 1) |
7300 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7301
fe2b8f9d 7302 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7303 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7304 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7305 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7306 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7307 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7308 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7309 (adjusted_mode->crtc_vsync_start - 1) |
7310 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7311
b5e508d4
PZ
7312 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7313 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7314 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7315 * bits. */
7316 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7317 (pipe == PIPE_B || pipe == PIPE_C))
7318 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7319
b0e77b9c
PZ
7320 /* pipesrc controls the size that is scaled from, which should
7321 * always be the user's requested size.
7322 */
7323 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7324 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7325 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7326}
7327
1bd1bd80 7328static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7329 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7330{
7331 struct drm_device *dev = crtc->base.dev;
7332 struct drm_i915_private *dev_priv = dev->dev_private;
7333 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7334 uint32_t tmp;
7335
7336 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7337 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7338 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7339 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7340 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7341 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7342 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7343 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7344 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7345
7346 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7347 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7348 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7349 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7350 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7351 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7352 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7353 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7354 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7355
7356 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7357 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7358 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7359 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7360 }
7361
7362 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7363 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7364 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7365
2d112de7
ACO
7366 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7367 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7368}
7369
f6a83288 7370void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7371 struct intel_crtc_state *pipe_config)
babea61d 7372{
2d112de7
ACO
7373 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7374 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7375 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7376 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7377
2d112de7
ACO
7378 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7379 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7380 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7381 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7382
2d112de7 7383 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 7384
2d112de7
ACO
7385 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7386 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
7387}
7388
84b046f3
DV
7389static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7390{
7391 struct drm_device *dev = intel_crtc->base.dev;
7392 struct drm_i915_private *dev_priv = dev->dev_private;
7393 uint32_t pipeconf;
7394
9f11a9e4 7395 pipeconf = 0;
84b046f3 7396
b6b5d049
VS
7397 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7398 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7399 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7400
6e3c9717 7401 if (intel_crtc->config->double_wide)
cf532bb2 7402 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7403
ff9ce46e
DV
7404 /* only g4x and later have fancy bpc/dither controls */
7405 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 7406 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7407 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7408 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7409 PIPECONF_DITHER_TYPE_SP;
84b046f3 7410
6e3c9717 7411 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7412 case 18:
7413 pipeconf |= PIPECONF_6BPC;
7414 break;
7415 case 24:
7416 pipeconf |= PIPECONF_8BPC;
7417 break;
7418 case 30:
7419 pipeconf |= PIPECONF_10BPC;
7420 break;
7421 default:
7422 /* Case prevented by intel_choose_pipe_bpp_dither. */
7423 BUG();
84b046f3
DV
7424 }
7425 }
7426
7427 if (HAS_PIPE_CXSR(dev)) {
7428 if (intel_crtc->lowfreq_avail) {
7429 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7430 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7431 } else {
7432 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7433 }
7434 }
7435
6e3c9717 7436 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7437 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7438 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7439 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7440 else
7441 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7442 } else
84b046f3
DV
7443 pipeconf |= PIPECONF_PROGRESSIVE;
7444
6e3c9717 7445 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 7446 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7447
84b046f3
DV
7448 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7449 POSTING_READ(PIPECONF(intel_crtc->pipe));
7450}
7451
190f68c5
ACO
7452static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7453 struct intel_crtc_state *crtc_state)
79e53945 7454{
c7653199 7455 struct drm_device *dev = crtc->base.dev;
79e53945 7456 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7457 int refclk, num_connectors = 0;
652c393a 7458 intel_clock_t clock, reduced_clock;
a16af721 7459 bool ok, has_reduced_clock = false;
e9fd1c02 7460 bool is_lvds = false, is_dsi = false;
5eddb70b 7461 struct intel_encoder *encoder;
d4906093 7462 const intel_limit_t *limit;
55bb9992
ACO
7463 struct drm_atomic_state *state = crtc_state->base.state;
7464 struct drm_connector_state *connector_state;
7465 int i;
79e53945 7466
55bb9992
ACO
7467 for (i = 0; i < state->num_connector; i++) {
7468 if (!state->connectors[i])
d0737e1d
ACO
7469 continue;
7470
55bb9992
ACO
7471 connector_state = state->connector_states[i];
7472 if (connector_state->crtc != &crtc->base)
7473 continue;
7474
7475 encoder = to_intel_encoder(connector_state->best_encoder);
7476
5eddb70b 7477 switch (encoder->type) {
79e53945
JB
7478 case INTEL_OUTPUT_LVDS:
7479 is_lvds = true;
7480 break;
e9fd1c02
JN
7481 case INTEL_OUTPUT_DSI:
7482 is_dsi = true;
7483 break;
6847d71b
PZ
7484 default:
7485 break;
79e53945 7486 }
43565a06 7487
c751ce4f 7488 num_connectors++;
79e53945
JB
7489 }
7490
f2335330 7491 if (is_dsi)
5b18e57c 7492 return 0;
f2335330 7493
190f68c5 7494 if (!crtc_state->clock_set) {
a93e255f 7495 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7496
e9fd1c02
JN
7497 /*
7498 * Returns a set of divisors for the desired target clock with
7499 * the given refclk, or FALSE. The returned values represent
7500 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7501 * 2) / p1 / p2.
7502 */
a93e255f
ACO
7503 limit = intel_limit(crtc_state, refclk);
7504 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7505 crtc_state->port_clock,
e9fd1c02 7506 refclk, NULL, &clock);
f2335330 7507 if (!ok) {
e9fd1c02
JN
7508 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7509 return -EINVAL;
7510 }
79e53945 7511
f2335330
JN
7512 if (is_lvds && dev_priv->lvds_downclock_avail) {
7513 /*
7514 * Ensure we match the reduced clock's P to the target
7515 * clock. If the clocks don't match, we can't switch
7516 * the display clock by using the FP0/FP1. In such case
7517 * we will disable the LVDS downclock feature.
7518 */
7519 has_reduced_clock =
a93e255f 7520 dev_priv->display.find_dpll(limit, crtc_state,
f2335330
JN
7521 dev_priv->lvds_downclock,
7522 refclk, &clock,
7523 &reduced_clock);
7524 }
7525 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7526 crtc_state->dpll.n = clock.n;
7527 crtc_state->dpll.m1 = clock.m1;
7528 crtc_state->dpll.m2 = clock.m2;
7529 crtc_state->dpll.p1 = clock.p1;
7530 crtc_state->dpll.p2 = clock.p2;
f47709a9 7531 }
7026d4ac 7532
e9fd1c02 7533 if (IS_GEN2(dev)) {
190f68c5 7534 i8xx_update_pll(crtc, crtc_state,
2a8f64ca
VP
7535 has_reduced_clock ? &reduced_clock : NULL,
7536 num_connectors);
9d556c99 7537 } else if (IS_CHERRYVIEW(dev)) {
190f68c5 7538 chv_update_pll(crtc, crtc_state);
e9fd1c02 7539 } else if (IS_VALLEYVIEW(dev)) {
190f68c5 7540 vlv_update_pll(crtc, crtc_state);
e9fd1c02 7541 } else {
190f68c5 7542 i9xx_update_pll(crtc, crtc_state,
eb1cbe48 7543 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 7544 num_connectors);
e9fd1c02 7545 }
79e53945 7546
c8f7a0db 7547 return 0;
f564048e
EA
7548}
7549
2fa2fe9a 7550static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7551 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7552{
7553 struct drm_device *dev = crtc->base.dev;
7554 struct drm_i915_private *dev_priv = dev->dev_private;
7555 uint32_t tmp;
7556
dc9e7dec
VS
7557 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7558 return;
7559
2fa2fe9a 7560 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7561 if (!(tmp & PFIT_ENABLE))
7562 return;
2fa2fe9a 7563
06922821 7564 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7565 if (INTEL_INFO(dev)->gen < 4) {
7566 if (crtc->pipe != PIPE_B)
7567 return;
2fa2fe9a
DV
7568 } else {
7569 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7570 return;
7571 }
7572
06922821 7573 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7574 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7575 if (INTEL_INFO(dev)->gen < 5)
7576 pipe_config->gmch_pfit.lvds_border_bits =
7577 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7578}
7579
acbec814 7580static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7581 struct intel_crtc_state *pipe_config)
acbec814
JB
7582{
7583 struct drm_device *dev = crtc->base.dev;
7584 struct drm_i915_private *dev_priv = dev->dev_private;
7585 int pipe = pipe_config->cpu_transcoder;
7586 intel_clock_t clock;
7587 u32 mdiv;
662c6ecb 7588 int refclk = 100000;
acbec814 7589
f573de5a
SK
7590 /* In case of MIPI DPLL will not even be used */
7591 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7592 return;
7593
acbec814 7594 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 7595 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
7596 mutex_unlock(&dev_priv->dpio_lock);
7597
7598 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7599 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7600 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7601 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7602 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7603
f646628b 7604 vlv_clock(refclk, &clock);
acbec814 7605
f646628b
VS
7606 /* clock.dot is the fast clock */
7607 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
7608}
7609
5724dbd1
DL
7610static void
7611i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7612 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7613{
7614 struct drm_device *dev = crtc->base.dev;
7615 struct drm_i915_private *dev_priv = dev->dev_private;
7616 u32 val, base, offset;
7617 int pipe = crtc->pipe, plane = crtc->plane;
7618 int fourcc, pixel_format;
6761dd31 7619 unsigned int aligned_height;
b113d5ee 7620 struct drm_framebuffer *fb;
1b842c89 7621 struct intel_framebuffer *intel_fb;
1ad292b5 7622
42a7b088
DL
7623 val = I915_READ(DSPCNTR(plane));
7624 if (!(val & DISPLAY_PLANE_ENABLE))
7625 return;
7626
d9806c9f 7627 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7628 if (!intel_fb) {
1ad292b5
JB
7629 DRM_DEBUG_KMS("failed to alloc fb\n");
7630 return;
7631 }
7632
1b842c89
DL
7633 fb = &intel_fb->base;
7634
18c5247e
DV
7635 if (INTEL_INFO(dev)->gen >= 4) {
7636 if (val & DISPPLANE_TILED) {
49af449b 7637 plane_config->tiling = I915_TILING_X;
18c5247e
DV
7638 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7639 }
7640 }
1ad292b5
JB
7641
7642 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7643 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
7644 fb->pixel_format = fourcc;
7645 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
7646
7647 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 7648 if (plane_config->tiling)
1ad292b5
JB
7649 offset = I915_READ(DSPTILEOFF(plane));
7650 else
7651 offset = I915_READ(DSPLINOFF(plane));
7652 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7653 } else {
7654 base = I915_READ(DSPADDR(plane));
7655 }
7656 plane_config->base = base;
7657
7658 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7659 fb->width = ((val >> 16) & 0xfff) + 1;
7660 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7661
7662 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7663 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7664
b113d5ee 7665 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
7666 fb->pixel_format,
7667 fb->modifier[0]);
1ad292b5 7668
f37b5c2b 7669 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7670
2844a921
DL
7671 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7672 pipe_name(pipe), plane, fb->width, fb->height,
7673 fb->bits_per_pixel, base, fb->pitches[0],
7674 plane_config->size);
1ad292b5 7675
2d14030b 7676 plane_config->fb = intel_fb;
1ad292b5
JB
7677}
7678
70b23a98 7679static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7680 struct intel_crtc_state *pipe_config)
70b23a98
VS
7681{
7682 struct drm_device *dev = crtc->base.dev;
7683 struct drm_i915_private *dev_priv = dev->dev_private;
7684 int pipe = pipe_config->cpu_transcoder;
7685 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7686 intel_clock_t clock;
7687 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7688 int refclk = 100000;
7689
7690 mutex_lock(&dev_priv->dpio_lock);
7691 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7692 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7693 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7694 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7695 mutex_unlock(&dev_priv->dpio_lock);
7696
7697 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7698 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7699 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7700 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7701 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7702
7703 chv_clock(refclk, &clock);
7704
7705 /* clock.dot is the fast clock */
7706 pipe_config->port_clock = clock.dot / 5;
7707}
7708
0e8ffe1b 7709static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7710 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
7711{
7712 struct drm_device *dev = crtc->base.dev;
7713 struct drm_i915_private *dev_priv = dev->dev_private;
7714 uint32_t tmp;
7715
f458ebbc
DV
7716 if (!intel_display_power_is_enabled(dev_priv,
7717 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
7718 return false;
7719
e143a21c 7720 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7721 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7722
0e8ffe1b
DV
7723 tmp = I915_READ(PIPECONF(crtc->pipe));
7724 if (!(tmp & PIPECONF_ENABLE))
7725 return false;
7726
42571aef
VS
7727 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7728 switch (tmp & PIPECONF_BPC_MASK) {
7729 case PIPECONF_6BPC:
7730 pipe_config->pipe_bpp = 18;
7731 break;
7732 case PIPECONF_8BPC:
7733 pipe_config->pipe_bpp = 24;
7734 break;
7735 case PIPECONF_10BPC:
7736 pipe_config->pipe_bpp = 30;
7737 break;
7738 default:
7739 break;
7740 }
7741 }
7742
b5a9fa09
DV
7743 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7744 pipe_config->limited_color_range = true;
7745
282740f7
VS
7746 if (INTEL_INFO(dev)->gen < 4)
7747 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7748
1bd1bd80
DV
7749 intel_get_pipe_timings(crtc, pipe_config);
7750
2fa2fe9a
DV
7751 i9xx_get_pfit_config(crtc, pipe_config);
7752
6c49f241
DV
7753 if (INTEL_INFO(dev)->gen >= 4) {
7754 tmp = I915_READ(DPLL_MD(crtc->pipe));
7755 pipe_config->pixel_multiplier =
7756 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7757 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 7758 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
7759 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7760 tmp = I915_READ(DPLL(crtc->pipe));
7761 pipe_config->pixel_multiplier =
7762 ((tmp & SDVO_MULTIPLIER_MASK)
7763 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7764 } else {
7765 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7766 * port and will be fixed up in the encoder->get_config
7767 * function. */
7768 pipe_config->pixel_multiplier = 1;
7769 }
8bcc2795
DV
7770 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7771 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
7772 /*
7773 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7774 * on 830. Filter it out here so that we don't
7775 * report errors due to that.
7776 */
7777 if (IS_I830(dev))
7778 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7779
8bcc2795
DV
7780 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7781 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7782 } else {
7783 /* Mask out read-only status bits. */
7784 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7785 DPLL_PORTC_READY_MASK |
7786 DPLL_PORTB_READY_MASK);
8bcc2795 7787 }
6c49f241 7788
70b23a98
VS
7789 if (IS_CHERRYVIEW(dev))
7790 chv_crtc_clock_get(crtc, pipe_config);
7791 else if (IS_VALLEYVIEW(dev))
acbec814
JB
7792 vlv_crtc_clock_get(crtc, pipe_config);
7793 else
7794 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7795
0e8ffe1b
DV
7796 return true;
7797}
7798
dde86e2d 7799static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
7800{
7801 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 7802 struct intel_encoder *encoder;
74cfd7ac 7803 u32 val, final;
13d83a67 7804 bool has_lvds = false;
199e5d79 7805 bool has_cpu_edp = false;
199e5d79 7806 bool has_panel = false;
99eb6a01
KP
7807 bool has_ck505 = false;
7808 bool can_ssc = false;
13d83a67
JB
7809
7810 /* We need to take the global config into account */
b2784e15 7811 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
7812 switch (encoder->type) {
7813 case INTEL_OUTPUT_LVDS:
7814 has_panel = true;
7815 has_lvds = true;
7816 break;
7817 case INTEL_OUTPUT_EDP:
7818 has_panel = true;
2de6905f 7819 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7820 has_cpu_edp = true;
7821 break;
6847d71b
PZ
7822 default:
7823 break;
13d83a67
JB
7824 }
7825 }
7826
99eb6a01 7827 if (HAS_PCH_IBX(dev)) {
41aa3448 7828 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7829 can_ssc = has_ck505;
7830 } else {
7831 has_ck505 = false;
7832 can_ssc = true;
7833 }
7834
2de6905f
ID
7835 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7836 has_panel, has_lvds, has_ck505);
13d83a67
JB
7837
7838 /* Ironlake: try to setup display ref clock before DPLL
7839 * enabling. This is only under driver's control after
7840 * PCH B stepping, previous chipset stepping should be
7841 * ignoring this setting.
7842 */
74cfd7ac
CW
7843 val = I915_READ(PCH_DREF_CONTROL);
7844
7845 /* As we must carefully and slowly disable/enable each source in turn,
7846 * compute the final state we want first and check if we need to
7847 * make any changes at all.
7848 */
7849 final = val;
7850 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7851 if (has_ck505)
7852 final |= DREF_NONSPREAD_CK505_ENABLE;
7853 else
7854 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7855
7856 final &= ~DREF_SSC_SOURCE_MASK;
7857 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7858 final &= ~DREF_SSC1_ENABLE;
7859
7860 if (has_panel) {
7861 final |= DREF_SSC_SOURCE_ENABLE;
7862
7863 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7864 final |= DREF_SSC1_ENABLE;
7865
7866 if (has_cpu_edp) {
7867 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7868 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7869 else
7870 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7871 } else
7872 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7873 } else {
7874 final |= DREF_SSC_SOURCE_DISABLE;
7875 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7876 }
7877
7878 if (final == val)
7879 return;
7880
13d83a67 7881 /* Always enable nonspread source */
74cfd7ac 7882 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7883
99eb6a01 7884 if (has_ck505)
74cfd7ac 7885 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7886 else
74cfd7ac 7887 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7888
199e5d79 7889 if (has_panel) {
74cfd7ac
CW
7890 val &= ~DREF_SSC_SOURCE_MASK;
7891 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7892
199e5d79 7893 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7894 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7895 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7896 val |= DREF_SSC1_ENABLE;
e77166b5 7897 } else
74cfd7ac 7898 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7899
7900 /* Get SSC going before enabling the outputs */
74cfd7ac 7901 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7902 POSTING_READ(PCH_DREF_CONTROL);
7903 udelay(200);
7904
74cfd7ac 7905 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7906
7907 /* Enable CPU source on CPU attached eDP */
199e5d79 7908 if (has_cpu_edp) {
99eb6a01 7909 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7910 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7911 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7912 } else
74cfd7ac 7913 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7914 } else
74cfd7ac 7915 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7916
74cfd7ac 7917 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7918 POSTING_READ(PCH_DREF_CONTROL);
7919 udelay(200);
7920 } else {
7921 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7922
74cfd7ac 7923 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7924
7925 /* Turn off CPU output */
74cfd7ac 7926 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7927
74cfd7ac 7928 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7929 POSTING_READ(PCH_DREF_CONTROL);
7930 udelay(200);
7931
7932 /* Turn off the SSC source */
74cfd7ac
CW
7933 val &= ~DREF_SSC_SOURCE_MASK;
7934 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
7935
7936 /* Turn off SSC1 */
74cfd7ac 7937 val &= ~DREF_SSC1_ENABLE;
199e5d79 7938
74cfd7ac 7939 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
7940 POSTING_READ(PCH_DREF_CONTROL);
7941 udelay(200);
7942 }
74cfd7ac
CW
7943
7944 BUG_ON(val != final);
13d83a67
JB
7945}
7946
f31f2d55 7947static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7948{
f31f2d55 7949 uint32_t tmp;
dde86e2d 7950
0ff066a9
PZ
7951 tmp = I915_READ(SOUTH_CHICKEN2);
7952 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7953 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7954
0ff066a9
PZ
7955 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7956 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7957 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7958
0ff066a9
PZ
7959 tmp = I915_READ(SOUTH_CHICKEN2);
7960 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7961 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7962
0ff066a9
PZ
7963 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7964 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7965 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7966}
7967
7968/* WaMPhyProgramming:hsw */
7969static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7970{
7971 uint32_t tmp;
dde86e2d
PZ
7972
7973 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7974 tmp &= ~(0xFF << 24);
7975 tmp |= (0x12 << 24);
7976 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7977
dde86e2d
PZ
7978 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7979 tmp |= (1 << 11);
7980 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7981
7982 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7983 tmp |= (1 << 11);
7984 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7985
dde86e2d
PZ
7986 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7987 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7988 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7989
7990 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7991 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7992 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7993
0ff066a9
PZ
7994 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7995 tmp &= ~(7 << 13);
7996 tmp |= (5 << 13);
7997 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 7998
0ff066a9
PZ
7999 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8000 tmp &= ~(7 << 13);
8001 tmp |= (5 << 13);
8002 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8003
8004 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8005 tmp &= ~0xFF;
8006 tmp |= 0x1C;
8007 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8008
8009 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8010 tmp &= ~0xFF;
8011 tmp |= 0x1C;
8012 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8013
8014 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8015 tmp &= ~(0xFF << 16);
8016 tmp |= (0x1C << 16);
8017 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8018
8019 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8020 tmp &= ~(0xFF << 16);
8021 tmp |= (0x1C << 16);
8022 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8023
0ff066a9
PZ
8024 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8025 tmp |= (1 << 27);
8026 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8027
0ff066a9
PZ
8028 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8029 tmp |= (1 << 27);
8030 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8031
0ff066a9
PZ
8032 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8033 tmp &= ~(0xF << 28);
8034 tmp |= (4 << 28);
8035 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8036
0ff066a9
PZ
8037 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8038 tmp &= ~(0xF << 28);
8039 tmp |= (4 << 28);
8040 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8041}
8042
2fa86a1f
PZ
8043/* Implements 3 different sequences from BSpec chapter "Display iCLK
8044 * Programming" based on the parameters passed:
8045 * - Sequence to enable CLKOUT_DP
8046 * - Sequence to enable CLKOUT_DP without spread
8047 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8048 */
8049static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8050 bool with_fdi)
f31f2d55
PZ
8051{
8052 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8053 uint32_t reg, tmp;
8054
8055 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8056 with_spread = true;
8057 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8058 with_fdi, "LP PCH doesn't have FDI\n"))
8059 with_fdi = false;
f31f2d55
PZ
8060
8061 mutex_lock(&dev_priv->dpio_lock);
8062
8063 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8064 tmp &= ~SBI_SSCCTL_DISABLE;
8065 tmp |= SBI_SSCCTL_PATHALT;
8066 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8067
8068 udelay(24);
8069
2fa86a1f
PZ
8070 if (with_spread) {
8071 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8072 tmp &= ~SBI_SSCCTL_PATHALT;
8073 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8074
2fa86a1f
PZ
8075 if (with_fdi) {
8076 lpt_reset_fdi_mphy(dev_priv);
8077 lpt_program_fdi_mphy(dev_priv);
8078 }
8079 }
dde86e2d 8080
2fa86a1f
PZ
8081 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8082 SBI_GEN0 : SBI_DBUFF0;
8083 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8084 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8085 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
8086
8087 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
8088}
8089
47701c3b
PZ
8090/* Sequence to disable CLKOUT_DP */
8091static void lpt_disable_clkout_dp(struct drm_device *dev)
8092{
8093 struct drm_i915_private *dev_priv = dev->dev_private;
8094 uint32_t reg, tmp;
8095
8096 mutex_lock(&dev_priv->dpio_lock);
8097
8098 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8099 SBI_GEN0 : SBI_DBUFF0;
8100 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8101 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8102 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8103
8104 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8105 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8106 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8107 tmp |= SBI_SSCCTL_PATHALT;
8108 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8109 udelay(32);
8110 }
8111 tmp |= SBI_SSCCTL_DISABLE;
8112 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8113 }
8114
8115 mutex_unlock(&dev_priv->dpio_lock);
8116}
8117
bf8fa3d3
PZ
8118static void lpt_init_pch_refclk(struct drm_device *dev)
8119{
bf8fa3d3
PZ
8120 struct intel_encoder *encoder;
8121 bool has_vga = false;
8122
b2784e15 8123 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8124 switch (encoder->type) {
8125 case INTEL_OUTPUT_ANALOG:
8126 has_vga = true;
8127 break;
6847d71b
PZ
8128 default:
8129 break;
bf8fa3d3
PZ
8130 }
8131 }
8132
47701c3b
PZ
8133 if (has_vga)
8134 lpt_enable_clkout_dp(dev, true, true);
8135 else
8136 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
8137}
8138
dde86e2d
PZ
8139/*
8140 * Initialize reference clocks when the driver loads
8141 */
8142void intel_init_pch_refclk(struct drm_device *dev)
8143{
8144 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8145 ironlake_init_pch_refclk(dev);
8146 else if (HAS_PCH_LPT(dev))
8147 lpt_init_pch_refclk(dev);
8148}
8149
55bb9992 8150static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8151{
55bb9992 8152 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8153 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992
ACO
8154 struct drm_atomic_state *state = crtc_state->base.state;
8155 struct drm_connector_state *connector_state;
d9d444cb 8156 struct intel_encoder *encoder;
55bb9992 8157 int num_connectors = 0, i;
d9d444cb
JB
8158 bool is_lvds = false;
8159
55bb9992
ACO
8160 for (i = 0; i < state->num_connector; i++) {
8161 if (!state->connectors[i])
d0737e1d
ACO
8162 continue;
8163
55bb9992
ACO
8164 connector_state = state->connector_states[i];
8165 if (connector_state->crtc != crtc_state->base.crtc)
8166 continue;
8167
8168 encoder = to_intel_encoder(connector_state->best_encoder);
8169
d9d444cb
JB
8170 switch (encoder->type) {
8171 case INTEL_OUTPUT_LVDS:
8172 is_lvds = true;
8173 break;
6847d71b
PZ
8174 default:
8175 break;
d9d444cb
JB
8176 }
8177 num_connectors++;
8178 }
8179
8180 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8181 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8182 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8183 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8184 }
8185
8186 return 120000;
8187}
8188
6ff93609 8189static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8190{
c8203565 8191 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8192 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8193 int pipe = intel_crtc->pipe;
c8203565
PZ
8194 uint32_t val;
8195
78114071 8196 val = 0;
c8203565 8197
6e3c9717 8198 switch (intel_crtc->config->pipe_bpp) {
c8203565 8199 case 18:
dfd07d72 8200 val |= PIPECONF_6BPC;
c8203565
PZ
8201 break;
8202 case 24:
dfd07d72 8203 val |= PIPECONF_8BPC;
c8203565
PZ
8204 break;
8205 case 30:
dfd07d72 8206 val |= PIPECONF_10BPC;
c8203565
PZ
8207 break;
8208 case 36:
dfd07d72 8209 val |= PIPECONF_12BPC;
c8203565
PZ
8210 break;
8211 default:
cc769b62
PZ
8212 /* Case prevented by intel_choose_pipe_bpp_dither. */
8213 BUG();
c8203565
PZ
8214 }
8215
6e3c9717 8216 if (intel_crtc->config->dither)
c8203565
PZ
8217 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8218
6e3c9717 8219 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8220 val |= PIPECONF_INTERLACED_ILK;
8221 else
8222 val |= PIPECONF_PROGRESSIVE;
8223
6e3c9717 8224 if (intel_crtc->config->limited_color_range)
3685a8f3 8225 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8226
c8203565
PZ
8227 I915_WRITE(PIPECONF(pipe), val);
8228 POSTING_READ(PIPECONF(pipe));
8229}
8230
86d3efce
VS
8231/*
8232 * Set up the pipe CSC unit.
8233 *
8234 * Currently only full range RGB to limited range RGB conversion
8235 * is supported, but eventually this should handle various
8236 * RGB<->YCbCr scenarios as well.
8237 */
50f3b016 8238static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8239{
8240 struct drm_device *dev = crtc->dev;
8241 struct drm_i915_private *dev_priv = dev->dev_private;
8242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8243 int pipe = intel_crtc->pipe;
8244 uint16_t coeff = 0x7800; /* 1.0 */
8245
8246 /*
8247 * TODO: Check what kind of values actually come out of the pipe
8248 * with these coeff/postoff values and adjust to get the best
8249 * accuracy. Perhaps we even need to take the bpc value into
8250 * consideration.
8251 */
8252
6e3c9717 8253 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8254 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8255
8256 /*
8257 * GY/GU and RY/RU should be the other way around according
8258 * to BSpec, but reality doesn't agree. Just set them up in
8259 * a way that results in the correct picture.
8260 */
8261 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8262 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8263
8264 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8265 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8266
8267 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8268 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8269
8270 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8271 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8272 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8273
8274 if (INTEL_INFO(dev)->gen > 6) {
8275 uint16_t postoff = 0;
8276
6e3c9717 8277 if (intel_crtc->config->limited_color_range)
32cf0cb0 8278 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8279
8280 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8281 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8282 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8283
8284 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8285 } else {
8286 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8287
6e3c9717 8288 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8289 mode |= CSC_BLACK_SCREEN_OFFSET;
8290
8291 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8292 }
8293}
8294
6ff93609 8295static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8296{
756f85cf
PZ
8297 struct drm_device *dev = crtc->dev;
8298 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 8299 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8300 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8301 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
8302 uint32_t val;
8303
3eff4faa 8304 val = 0;
ee2b0b38 8305
6e3c9717 8306 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
8307 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8308
6e3c9717 8309 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8310 val |= PIPECONF_INTERLACED_ILK;
8311 else
8312 val |= PIPECONF_PROGRESSIVE;
8313
702e7a56
PZ
8314 I915_WRITE(PIPECONF(cpu_transcoder), val);
8315 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
8316
8317 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8318 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 8319
3cdf122c 8320 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
8321 val = 0;
8322
6e3c9717 8323 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8324 case 18:
8325 val |= PIPEMISC_DITHER_6_BPC;
8326 break;
8327 case 24:
8328 val |= PIPEMISC_DITHER_8_BPC;
8329 break;
8330 case 30:
8331 val |= PIPEMISC_DITHER_10_BPC;
8332 break;
8333 case 36:
8334 val |= PIPEMISC_DITHER_12_BPC;
8335 break;
8336 default:
8337 /* Case prevented by pipe_config_set_bpp. */
8338 BUG();
8339 }
8340
6e3c9717 8341 if (intel_crtc->config->dither)
756f85cf
PZ
8342 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8343
8344 I915_WRITE(PIPEMISC(pipe), val);
8345 }
ee2b0b38
PZ
8346}
8347
6591c6e4 8348static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8349 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8350 intel_clock_t *clock,
8351 bool *has_reduced_clock,
8352 intel_clock_t *reduced_clock)
8353{
8354 struct drm_device *dev = crtc->dev;
8355 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8356 int refclk;
d4906093 8357 const intel_limit_t *limit;
a16af721 8358 bool ret, is_lvds = false;
79e53945 8359
a93e255f 8360 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
79e53945 8361
55bb9992 8362 refclk = ironlake_get_refclk(crtc_state);
79e53945 8363
d4906093
ML
8364 /*
8365 * Returns a set of divisors for the desired target clock with the given
8366 * refclk, or FALSE. The returned values represent the clock equation:
8367 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8368 */
a93e255f
ACO
8369 limit = intel_limit(crtc_state, refclk);
8370 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8371 crtc_state->port_clock,
ee9300bb 8372 refclk, NULL, clock);
6591c6e4
PZ
8373 if (!ret)
8374 return false;
cda4b7d3 8375
ddc9003c 8376 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
8377 /*
8378 * Ensure we match the reduced clock's P to the target clock.
8379 * If the clocks don't match, we can't switch the display clock
8380 * by using the FP0/FP1. In such case we will disable the LVDS
8381 * downclock feature.
8382 */
ee9300bb 8383 *has_reduced_clock =
a93e255f 8384 dev_priv->display.find_dpll(limit, crtc_state,
ee9300bb
DV
8385 dev_priv->lvds_downclock,
8386 refclk, clock,
8387 reduced_clock);
652c393a 8388 }
61e9653f 8389
6591c6e4
PZ
8390 return true;
8391}
8392
d4b1931c
PZ
8393int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8394{
8395 /*
8396 * Account for spread spectrum to avoid
8397 * oversubscribing the link. Max center spread
8398 * is 2.5%; use 5% for safety's sake.
8399 */
8400 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8401 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8402}
8403
7429e9d4 8404static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8405{
7429e9d4 8406 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8407}
8408
de13a2e3 8409static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8410 struct intel_crtc_state *crtc_state,
7429e9d4 8411 u32 *fp,
9a7c7890 8412 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8413{
de13a2e3 8414 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8415 struct drm_device *dev = crtc->dev;
8416 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992
ACO
8417 struct drm_atomic_state *state = crtc_state->base.state;
8418 struct drm_connector_state *connector_state;
8419 struct intel_encoder *encoder;
de13a2e3 8420 uint32_t dpll;
55bb9992 8421 int factor, num_connectors = 0, i;
09ede541 8422 bool is_lvds = false, is_sdvo = false;
79e53945 8423
55bb9992
ACO
8424 for (i = 0; i < state->num_connector; i++) {
8425 if (!state->connectors[i])
d0737e1d
ACO
8426 continue;
8427
55bb9992
ACO
8428 connector_state = state->connector_states[i];
8429 if (connector_state->crtc != crtc_state->base.crtc)
8430 continue;
8431
8432 encoder = to_intel_encoder(connector_state->best_encoder);
8433
8434 switch (encoder->type) {
79e53945
JB
8435 case INTEL_OUTPUT_LVDS:
8436 is_lvds = true;
8437 break;
8438 case INTEL_OUTPUT_SDVO:
7d57382e 8439 case INTEL_OUTPUT_HDMI:
79e53945 8440 is_sdvo = true;
79e53945 8441 break;
6847d71b
PZ
8442 default:
8443 break;
79e53945 8444 }
43565a06 8445
c751ce4f 8446 num_connectors++;
79e53945 8447 }
79e53945 8448
c1858123 8449 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8450 factor = 21;
8451 if (is_lvds) {
8452 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8453 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8454 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8455 factor = 25;
190f68c5 8456 } else if (crtc_state->sdvo_tv_clock)
8febb297 8457 factor = 20;
c1858123 8458
190f68c5 8459 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8460 *fp |= FP_CB_TUNE;
2c07245f 8461
9a7c7890
DV
8462 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8463 *fp2 |= FP_CB_TUNE;
8464
5eddb70b 8465 dpll = 0;
2c07245f 8466
a07d6787
EA
8467 if (is_lvds)
8468 dpll |= DPLLB_MODE_LVDS;
8469 else
8470 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8471
190f68c5 8472 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8473 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8474
8475 if (is_sdvo)
4a33e48d 8476 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8477 if (crtc_state->has_dp_encoder)
4a33e48d 8478 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8479
a07d6787 8480 /* compute bitmask from p1 value */
190f68c5 8481 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8482 /* also FPA1 */
190f68c5 8483 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8484
190f68c5 8485 switch (crtc_state->dpll.p2) {
a07d6787
EA
8486 case 5:
8487 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8488 break;
8489 case 7:
8490 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8491 break;
8492 case 10:
8493 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8494 break;
8495 case 14:
8496 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8497 break;
79e53945
JB
8498 }
8499
b4c09f3b 8500 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8501 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8502 else
8503 dpll |= PLL_REF_INPUT_DREFCLK;
8504
959e16d6 8505 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8506}
8507
190f68c5
ACO
8508static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8509 struct intel_crtc_state *crtc_state)
de13a2e3 8510{
c7653199 8511 struct drm_device *dev = crtc->base.dev;
de13a2e3 8512 intel_clock_t clock, reduced_clock;
cbbab5bd 8513 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8514 bool ok, has_reduced_clock = false;
8b47047b 8515 bool is_lvds = false;
e2b78267 8516 struct intel_shared_dpll *pll;
de13a2e3 8517
409ee761 8518 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 8519
5dc5298b
PZ
8520 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8521 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8522
190f68c5 8523 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8524 &has_reduced_clock, &reduced_clock);
190f68c5 8525 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8526 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8527 return -EINVAL;
79e53945 8528 }
f47709a9 8529 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8530 if (!crtc_state->clock_set) {
8531 crtc_state->dpll.n = clock.n;
8532 crtc_state->dpll.m1 = clock.m1;
8533 crtc_state->dpll.m2 = clock.m2;
8534 crtc_state->dpll.p1 = clock.p1;
8535 crtc_state->dpll.p2 = clock.p2;
f47709a9 8536 }
79e53945 8537
5dc5298b 8538 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8539 if (crtc_state->has_pch_encoder) {
8540 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8541 if (has_reduced_clock)
7429e9d4 8542 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8543
190f68c5 8544 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8545 &fp, &reduced_clock,
8546 has_reduced_clock ? &fp2 : NULL);
8547
190f68c5
ACO
8548 crtc_state->dpll_hw_state.dpll = dpll;
8549 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8550 if (has_reduced_clock)
190f68c5 8551 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8552 else
190f68c5 8553 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8554
190f68c5 8555 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 8556 if (pll == NULL) {
84f44ce7 8557 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8558 pipe_name(crtc->pipe));
4b645f14
JB
8559 return -EINVAL;
8560 }
3fb37703 8561 }
79e53945 8562
ab585dea 8563 if (is_lvds && has_reduced_clock)
c7653199 8564 crtc->lowfreq_avail = true;
bcd644e0 8565 else
c7653199 8566 crtc->lowfreq_avail = false;
e2b78267 8567
c8f7a0db 8568 return 0;
79e53945
JB
8569}
8570
eb14cb74
VS
8571static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8572 struct intel_link_m_n *m_n)
8573{
8574 struct drm_device *dev = crtc->base.dev;
8575 struct drm_i915_private *dev_priv = dev->dev_private;
8576 enum pipe pipe = crtc->pipe;
8577
8578 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8579 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8580 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8581 & ~TU_SIZE_MASK;
8582 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8583 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8584 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8585}
8586
8587static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8588 enum transcoder transcoder,
b95af8be
VK
8589 struct intel_link_m_n *m_n,
8590 struct intel_link_m_n *m2_n2)
72419203
DV
8591{
8592 struct drm_device *dev = crtc->base.dev;
8593 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8594 enum pipe pipe = crtc->pipe;
72419203 8595
eb14cb74
VS
8596 if (INTEL_INFO(dev)->gen >= 5) {
8597 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8598 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8599 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8600 & ~TU_SIZE_MASK;
8601 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8602 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8603 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8604 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8605 * gen < 8) and if DRRS is supported (to make sure the
8606 * registers are not unnecessarily read).
8607 */
8608 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8609 crtc->config->has_drrs) {
b95af8be
VK
8610 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8611 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8612 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8613 & ~TU_SIZE_MASK;
8614 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8615 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8616 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8617 }
eb14cb74
VS
8618 } else {
8619 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8620 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8621 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8622 & ~TU_SIZE_MASK;
8623 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8624 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8625 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8626 }
8627}
8628
8629void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8630 struct intel_crtc_state *pipe_config)
eb14cb74 8631{
681a8504 8632 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8633 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8634 else
8635 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8636 &pipe_config->dp_m_n,
8637 &pipe_config->dp_m2_n2);
eb14cb74 8638}
72419203 8639
eb14cb74 8640static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8641 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8642{
8643 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8644 &pipe_config->fdi_m_n, NULL);
72419203
DV
8645}
8646
bd2e244f 8647static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8648 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8649{
8650 struct drm_device *dev = crtc->base.dev;
8651 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
8652 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8653 uint32_t ps_ctrl = 0;
8654 int id = -1;
8655 int i;
bd2e244f 8656
a1b2278e
CK
8657 /* find scaler attached to this pipe */
8658 for (i = 0; i < crtc->num_scalers; i++) {
8659 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8660 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8661 id = i;
8662 pipe_config->pch_pfit.enabled = true;
8663 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8664 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8665 break;
8666 }
8667 }
bd2e244f 8668
a1b2278e
CK
8669 scaler_state->scaler_id = id;
8670 if (id >= 0) {
8671 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8672 } else {
8673 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8674 }
8675}
8676
5724dbd1
DL
8677static void
8678skylake_get_initial_plane_config(struct intel_crtc *crtc,
8679 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8680{
8681 struct drm_device *dev = crtc->base.dev;
8682 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 8683 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8684 int pipe = crtc->pipe;
8685 int fourcc, pixel_format;
6761dd31 8686 unsigned int aligned_height;
bc8d7dff 8687 struct drm_framebuffer *fb;
1b842c89 8688 struct intel_framebuffer *intel_fb;
bc8d7dff 8689
d9806c9f 8690 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8691 if (!intel_fb) {
bc8d7dff
DL
8692 DRM_DEBUG_KMS("failed to alloc fb\n");
8693 return;
8694 }
8695
1b842c89
DL
8696 fb = &intel_fb->base;
8697
bc8d7dff 8698 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8699 if (!(val & PLANE_CTL_ENABLE))
8700 goto error;
8701
bc8d7dff
DL
8702 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8703 fourcc = skl_format_to_fourcc(pixel_format,
8704 val & PLANE_CTL_ORDER_RGBX,
8705 val & PLANE_CTL_ALPHA_MASK);
8706 fb->pixel_format = fourcc;
8707 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8708
40f46283
DL
8709 tiling = val & PLANE_CTL_TILED_MASK;
8710 switch (tiling) {
8711 case PLANE_CTL_TILED_LINEAR:
8712 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8713 break;
8714 case PLANE_CTL_TILED_X:
8715 plane_config->tiling = I915_TILING_X;
8716 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8717 break;
8718 case PLANE_CTL_TILED_Y:
8719 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8720 break;
8721 case PLANE_CTL_TILED_YF:
8722 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8723 break;
8724 default:
8725 MISSING_CASE(tiling);
8726 goto error;
8727 }
8728
bc8d7dff
DL
8729 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8730 plane_config->base = base;
8731
8732 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8733
8734 val = I915_READ(PLANE_SIZE(pipe, 0));
8735 fb->height = ((val >> 16) & 0xfff) + 1;
8736 fb->width = ((val >> 0) & 0x1fff) + 1;
8737
8738 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
8739 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8740 fb->pixel_format);
bc8d7dff
DL
8741 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8742
8743 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8744 fb->pixel_format,
8745 fb->modifier[0]);
bc8d7dff 8746
f37b5c2b 8747 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
8748
8749 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8750 pipe_name(pipe), fb->width, fb->height,
8751 fb->bits_per_pixel, base, fb->pitches[0],
8752 plane_config->size);
8753
2d14030b 8754 plane_config->fb = intel_fb;
bc8d7dff
DL
8755 return;
8756
8757error:
8758 kfree(fb);
8759}
8760
2fa2fe9a 8761static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8762 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8763{
8764 struct drm_device *dev = crtc->base.dev;
8765 struct drm_i915_private *dev_priv = dev->dev_private;
8766 uint32_t tmp;
8767
8768 tmp = I915_READ(PF_CTL(crtc->pipe));
8769
8770 if (tmp & PF_ENABLE) {
fd4daa9c 8771 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
8772 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8773 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
8774
8775 /* We currently do not free assignements of panel fitters on
8776 * ivb/hsw (since we don't use the higher upscaling modes which
8777 * differentiates them) so just WARN about this case for now. */
8778 if (IS_GEN7(dev)) {
8779 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8780 PF_PIPE_SEL_IVB(crtc->pipe));
8781 }
2fa2fe9a 8782 }
79e53945
JB
8783}
8784
5724dbd1
DL
8785static void
8786ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8787 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
8788{
8789 struct drm_device *dev = crtc->base.dev;
8790 struct drm_i915_private *dev_priv = dev->dev_private;
8791 u32 val, base, offset;
aeee5a49 8792 int pipe = crtc->pipe;
4c6baa59 8793 int fourcc, pixel_format;
6761dd31 8794 unsigned int aligned_height;
b113d5ee 8795 struct drm_framebuffer *fb;
1b842c89 8796 struct intel_framebuffer *intel_fb;
4c6baa59 8797
42a7b088
DL
8798 val = I915_READ(DSPCNTR(pipe));
8799 if (!(val & DISPLAY_PLANE_ENABLE))
8800 return;
8801
d9806c9f 8802 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8803 if (!intel_fb) {
4c6baa59
JB
8804 DRM_DEBUG_KMS("failed to alloc fb\n");
8805 return;
8806 }
8807
1b842c89
DL
8808 fb = &intel_fb->base;
8809
18c5247e
DV
8810 if (INTEL_INFO(dev)->gen >= 4) {
8811 if (val & DISPPLANE_TILED) {
49af449b 8812 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8813 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8814 }
8815 }
4c6baa59
JB
8816
8817 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8818 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8819 fb->pixel_format = fourcc;
8820 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 8821
aeee5a49 8822 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 8823 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 8824 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8825 } else {
49af449b 8826 if (plane_config->tiling)
aeee5a49 8827 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8828 else
aeee5a49 8829 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8830 }
8831 plane_config->base = base;
8832
8833 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8834 fb->width = ((val >> 16) & 0xfff) + 1;
8835 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8836
8837 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8838 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8839
b113d5ee 8840 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8841 fb->pixel_format,
8842 fb->modifier[0]);
4c6baa59 8843
f37b5c2b 8844 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8845
2844a921
DL
8846 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8847 pipe_name(pipe), fb->width, fb->height,
8848 fb->bits_per_pixel, base, fb->pitches[0],
8849 plane_config->size);
b113d5ee 8850
2d14030b 8851 plane_config->fb = intel_fb;
4c6baa59
JB
8852}
8853
0e8ffe1b 8854static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8855 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8856{
8857 struct drm_device *dev = crtc->base.dev;
8858 struct drm_i915_private *dev_priv = dev->dev_private;
8859 uint32_t tmp;
8860
f458ebbc
DV
8861 if (!intel_display_power_is_enabled(dev_priv,
8862 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
8863 return false;
8864
e143a21c 8865 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8866 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8867
0e8ffe1b
DV
8868 tmp = I915_READ(PIPECONF(crtc->pipe));
8869 if (!(tmp & PIPECONF_ENABLE))
8870 return false;
8871
42571aef
VS
8872 switch (tmp & PIPECONF_BPC_MASK) {
8873 case PIPECONF_6BPC:
8874 pipe_config->pipe_bpp = 18;
8875 break;
8876 case PIPECONF_8BPC:
8877 pipe_config->pipe_bpp = 24;
8878 break;
8879 case PIPECONF_10BPC:
8880 pipe_config->pipe_bpp = 30;
8881 break;
8882 case PIPECONF_12BPC:
8883 pipe_config->pipe_bpp = 36;
8884 break;
8885 default:
8886 break;
8887 }
8888
b5a9fa09
DV
8889 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8890 pipe_config->limited_color_range = true;
8891
ab9412ba 8892 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
8893 struct intel_shared_dpll *pll;
8894
88adfff1
DV
8895 pipe_config->has_pch_encoder = true;
8896
627eb5a3
DV
8897 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8898 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8899 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8900
8901 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8902
c0d43d62 8903 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
8904 pipe_config->shared_dpll =
8905 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8906 } else {
8907 tmp = I915_READ(PCH_DPLL_SEL);
8908 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8909 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8910 else
8911 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8912 }
66e985c0
DV
8913
8914 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8915
8916 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8917 &pipe_config->dpll_hw_state));
c93f54cf
DV
8918
8919 tmp = pipe_config->dpll_hw_state.dpll;
8920 pipe_config->pixel_multiplier =
8921 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8922 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8923
8924 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8925 } else {
8926 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8927 }
8928
1bd1bd80
DV
8929 intel_get_pipe_timings(crtc, pipe_config);
8930
2fa2fe9a
DV
8931 ironlake_get_pfit_config(crtc, pipe_config);
8932
0e8ffe1b
DV
8933 return true;
8934}
8935
be256dc7
PZ
8936static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8937{
8938 struct drm_device *dev = dev_priv->dev;
be256dc7 8939 struct intel_crtc *crtc;
be256dc7 8940
d3fcc808 8941 for_each_intel_crtc(dev, crtc)
e2c719b7 8942 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8943 pipe_name(crtc->pipe));
8944
e2c719b7
RC
8945 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8946 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8947 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8948 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8949 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8950 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8951 "CPU PWM1 enabled\n");
c5107b87 8952 if (IS_HASWELL(dev))
e2c719b7 8953 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8954 "CPU PWM2 enabled\n");
e2c719b7 8955 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8956 "PCH PWM1 enabled\n");
e2c719b7 8957 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8958 "Utility pin enabled\n");
e2c719b7 8959 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8960
9926ada1
PZ
8961 /*
8962 * In theory we can still leave IRQs enabled, as long as only the HPD
8963 * interrupts remain enabled. We used to check for that, but since it's
8964 * gen-specific and since we only disable LCPLL after we fully disable
8965 * the interrupts, the check below should be enough.
8966 */
e2c719b7 8967 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8968}
8969
9ccd5aeb
PZ
8970static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8971{
8972 struct drm_device *dev = dev_priv->dev;
8973
8974 if (IS_HASWELL(dev))
8975 return I915_READ(D_COMP_HSW);
8976 else
8977 return I915_READ(D_COMP_BDW);
8978}
8979
3c4c9b81
PZ
8980static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8981{
8982 struct drm_device *dev = dev_priv->dev;
8983
8984 if (IS_HASWELL(dev)) {
8985 mutex_lock(&dev_priv->rps.hw_lock);
8986 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8987 val))
f475dadf 8988 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
8989 mutex_unlock(&dev_priv->rps.hw_lock);
8990 } else {
9ccd5aeb
PZ
8991 I915_WRITE(D_COMP_BDW, val);
8992 POSTING_READ(D_COMP_BDW);
3c4c9b81 8993 }
be256dc7
PZ
8994}
8995
8996/*
8997 * This function implements pieces of two sequences from BSpec:
8998 * - Sequence for display software to disable LCPLL
8999 * - Sequence for display software to allow package C8+
9000 * The steps implemented here are just the steps that actually touch the LCPLL
9001 * register. Callers should take care of disabling all the display engine
9002 * functions, doing the mode unset, fixing interrupts, etc.
9003 */
6ff58d53
PZ
9004static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9005 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9006{
9007 uint32_t val;
9008
9009 assert_can_disable_lcpll(dev_priv);
9010
9011 val = I915_READ(LCPLL_CTL);
9012
9013 if (switch_to_fclk) {
9014 val |= LCPLL_CD_SOURCE_FCLK;
9015 I915_WRITE(LCPLL_CTL, val);
9016
9017 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9018 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9019 DRM_ERROR("Switching to FCLK failed\n");
9020
9021 val = I915_READ(LCPLL_CTL);
9022 }
9023
9024 val |= LCPLL_PLL_DISABLE;
9025 I915_WRITE(LCPLL_CTL, val);
9026 POSTING_READ(LCPLL_CTL);
9027
9028 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9029 DRM_ERROR("LCPLL still locked\n");
9030
9ccd5aeb 9031 val = hsw_read_dcomp(dev_priv);
be256dc7 9032 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9033 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9034 ndelay(100);
9035
9ccd5aeb
PZ
9036 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9037 1))
be256dc7
PZ
9038 DRM_ERROR("D_COMP RCOMP still in progress\n");
9039
9040 if (allow_power_down) {
9041 val = I915_READ(LCPLL_CTL);
9042 val |= LCPLL_POWER_DOWN_ALLOW;
9043 I915_WRITE(LCPLL_CTL, val);
9044 POSTING_READ(LCPLL_CTL);
9045 }
9046}
9047
9048/*
9049 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9050 * source.
9051 */
6ff58d53 9052static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9053{
9054 uint32_t val;
9055
9056 val = I915_READ(LCPLL_CTL);
9057
9058 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9059 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9060 return;
9061
a8a8bd54
PZ
9062 /*
9063 * Make sure we're not on PC8 state before disabling PC8, otherwise
9064 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9065 */
59bad947 9066 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9067
be256dc7
PZ
9068 if (val & LCPLL_POWER_DOWN_ALLOW) {
9069 val &= ~LCPLL_POWER_DOWN_ALLOW;
9070 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9071 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9072 }
9073
9ccd5aeb 9074 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9075 val |= D_COMP_COMP_FORCE;
9076 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9077 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9078
9079 val = I915_READ(LCPLL_CTL);
9080 val &= ~LCPLL_PLL_DISABLE;
9081 I915_WRITE(LCPLL_CTL, val);
9082
9083 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9084 DRM_ERROR("LCPLL not locked yet\n");
9085
9086 if (val & LCPLL_CD_SOURCE_FCLK) {
9087 val = I915_READ(LCPLL_CTL);
9088 val &= ~LCPLL_CD_SOURCE_FCLK;
9089 I915_WRITE(LCPLL_CTL, val);
9090
9091 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9092 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9093 DRM_ERROR("Switching back to LCPLL failed\n");
9094 }
215733fa 9095
59bad947 9096 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
be256dc7
PZ
9097}
9098
765dab67
PZ
9099/*
9100 * Package states C8 and deeper are really deep PC states that can only be
9101 * reached when all the devices on the system allow it, so even if the graphics
9102 * device allows PC8+, it doesn't mean the system will actually get to these
9103 * states. Our driver only allows PC8+ when going into runtime PM.
9104 *
9105 * The requirements for PC8+ are that all the outputs are disabled, the power
9106 * well is disabled and most interrupts are disabled, and these are also
9107 * requirements for runtime PM. When these conditions are met, we manually do
9108 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9109 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9110 * hang the machine.
9111 *
9112 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9113 * the state of some registers, so when we come back from PC8+ we need to
9114 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9115 * need to take care of the registers kept by RC6. Notice that this happens even
9116 * if we don't put the device in PCI D3 state (which is what currently happens
9117 * because of the runtime PM support).
9118 *
9119 * For more, read "Display Sequences for Package C8" on the hardware
9120 * documentation.
9121 */
a14cb6fc 9122void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9123{
c67a470b
PZ
9124 struct drm_device *dev = dev_priv->dev;
9125 uint32_t val;
9126
c67a470b
PZ
9127 DRM_DEBUG_KMS("Enabling package C8+\n");
9128
c67a470b
PZ
9129 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9130 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9131 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9132 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9133 }
9134
9135 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9136 hsw_disable_lcpll(dev_priv, true, true);
9137}
9138
a14cb6fc 9139void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9140{
9141 struct drm_device *dev = dev_priv->dev;
9142 uint32_t val;
9143
c67a470b
PZ
9144 DRM_DEBUG_KMS("Disabling package C8+\n");
9145
9146 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9147 lpt_init_pch_refclk(dev);
9148
9149 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9150 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9151 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9152 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9153 }
9154
9155 intel_prepare_ddi(dev);
c67a470b
PZ
9156}
9157
f8437dd1
VK
9158static void broxton_modeset_global_resources(struct drm_atomic_state *state)
9159{
9160 struct drm_device *dev = state->dev;
9161 struct drm_i915_private *dev_priv = dev->dev_private;
9162 int max_pixclk = intel_mode_max_pixclk(state);
9163 int req_cdclk;
9164
9165 /* see the comment in valleyview_modeset_global_resources */
9166 if (WARN_ON(max_pixclk < 0))
9167 return;
9168
9169 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9170
9171 if (req_cdclk != dev_priv->cdclk_freq)
9172 broxton_set_cdclk(dev, req_cdclk);
9173}
9174
190f68c5
ACO
9175static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9176 struct intel_crtc_state *crtc_state)
09b4ddf9 9177{
190f68c5 9178 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 9179 return -EINVAL;
716c2e55 9180
c7653199 9181 crtc->lowfreq_avail = false;
644cef34 9182
c8f7a0db 9183 return 0;
79e53945
JB
9184}
9185
3760b59c
S
9186static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9187 enum port port,
9188 struct intel_crtc_state *pipe_config)
9189{
9190 switch (port) {
9191 case PORT_A:
9192 pipe_config->ddi_pll_sel = SKL_DPLL0;
9193 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9194 break;
9195 case PORT_B:
9196 pipe_config->ddi_pll_sel = SKL_DPLL1;
9197 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9198 break;
9199 case PORT_C:
9200 pipe_config->ddi_pll_sel = SKL_DPLL2;
9201 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9202 break;
9203 default:
9204 DRM_ERROR("Incorrect port type\n");
9205 }
9206}
9207
96b7dfb7
S
9208static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9209 enum port port,
5cec258b 9210 struct intel_crtc_state *pipe_config)
96b7dfb7 9211{
3148ade7 9212 u32 temp, dpll_ctl1;
96b7dfb7
S
9213
9214 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9215 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9216
9217 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
9218 case SKL_DPLL0:
9219 /*
9220 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9221 * of the shared DPLL framework and thus needs to be read out
9222 * separately
9223 */
9224 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9225 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9226 break;
96b7dfb7
S
9227 case SKL_DPLL1:
9228 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9229 break;
9230 case SKL_DPLL2:
9231 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9232 break;
9233 case SKL_DPLL3:
9234 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9235 break;
96b7dfb7
S
9236 }
9237}
9238
7d2c8175
DL
9239static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9240 enum port port,
5cec258b 9241 struct intel_crtc_state *pipe_config)
7d2c8175
DL
9242{
9243 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9244
9245 switch (pipe_config->ddi_pll_sel) {
9246 case PORT_CLK_SEL_WRPLL1:
9247 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9248 break;
9249 case PORT_CLK_SEL_WRPLL2:
9250 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9251 break;
9252 }
9253}
9254
26804afd 9255static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9256 struct intel_crtc_state *pipe_config)
26804afd
DV
9257{
9258 struct drm_device *dev = crtc->base.dev;
9259 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9260 struct intel_shared_dpll *pll;
26804afd
DV
9261 enum port port;
9262 uint32_t tmp;
9263
9264 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9265
9266 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9267
96b7dfb7
S
9268 if (IS_SKYLAKE(dev))
9269 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9270 else if (IS_BROXTON(dev))
9271 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9272 else
9273 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9274
d452c5b6
DV
9275 if (pipe_config->shared_dpll >= 0) {
9276 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9277
9278 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9279 &pipe_config->dpll_hw_state));
9280 }
9281
26804afd
DV
9282 /*
9283 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9284 * DDI E. So just check whether this pipe is wired to DDI E and whether
9285 * the PCH transcoder is on.
9286 */
ca370455
DL
9287 if (INTEL_INFO(dev)->gen < 9 &&
9288 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9289 pipe_config->has_pch_encoder = true;
9290
9291 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9292 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9293 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9294
9295 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9296 }
9297}
9298
0e8ffe1b 9299static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9300 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9301{
9302 struct drm_device *dev = crtc->base.dev;
9303 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 9304 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
9305 uint32_t tmp;
9306
f458ebbc 9307 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
9308 POWER_DOMAIN_PIPE(crtc->pipe)))
9309 return false;
9310
e143a21c 9311 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
9312 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9313
eccb140b
DV
9314 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9315 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9316 enum pipe trans_edp_pipe;
9317 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9318 default:
9319 WARN(1, "unknown pipe linked to edp transcoder\n");
9320 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9321 case TRANS_DDI_EDP_INPUT_A_ON:
9322 trans_edp_pipe = PIPE_A;
9323 break;
9324 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9325 trans_edp_pipe = PIPE_B;
9326 break;
9327 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9328 trans_edp_pipe = PIPE_C;
9329 break;
9330 }
9331
9332 if (trans_edp_pipe == crtc->pipe)
9333 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9334 }
9335
f458ebbc 9336 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 9337 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
9338 return false;
9339
eccb140b 9340 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
9341 if (!(tmp & PIPECONF_ENABLE))
9342 return false;
9343
26804afd 9344 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9345
1bd1bd80
DV
9346 intel_get_pipe_timings(crtc, pipe_config);
9347
a1b2278e
CK
9348 if (INTEL_INFO(dev)->gen >= 9) {
9349 skl_init_scalers(dev, crtc, pipe_config);
9350 }
9351
2fa2fe9a 9352 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
bd2e244f 9353 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
ff6d9f55 9354 if (INTEL_INFO(dev)->gen == 9)
bd2e244f 9355 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9356 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 9357 ironlake_get_pfit_config(crtc, pipe_config);
ff6d9f55
JB
9358 else
9359 MISSING_CASE(INTEL_INFO(dev)->gen);
9360
a1b2278e
CK
9361 } else {
9362 pipe_config->scaler_state.scaler_id = -1;
9363 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f 9364 }
88adfff1 9365
e59150dc
JB
9366 if (IS_HASWELL(dev))
9367 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9368 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9369
ebb69c95
CT
9370 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9371 pipe_config->pixel_multiplier =
9372 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9373 } else {
9374 pipe_config->pixel_multiplier = 1;
9375 }
6c49f241 9376
0e8ffe1b
DV
9377 return true;
9378}
9379
560b85bb
CW
9380static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9381{
9382 struct drm_device *dev = crtc->dev;
9383 struct drm_i915_private *dev_priv = dev->dev_private;
9384 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9385 uint32_t cntl = 0, size = 0;
560b85bb 9386
dc41c154 9387 if (base) {
3dd512fb
MR
9388 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9389 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
9390 unsigned int stride = roundup_pow_of_two(width) * 4;
9391
9392 switch (stride) {
9393 default:
9394 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9395 width, stride);
9396 stride = 256;
9397 /* fallthrough */
9398 case 256:
9399 case 512:
9400 case 1024:
9401 case 2048:
9402 break;
4b0e333e
CW
9403 }
9404
dc41c154
VS
9405 cntl |= CURSOR_ENABLE |
9406 CURSOR_GAMMA_ENABLE |
9407 CURSOR_FORMAT_ARGB |
9408 CURSOR_STRIDE(stride);
9409
9410 size = (height << 12) | width;
4b0e333e 9411 }
560b85bb 9412
dc41c154
VS
9413 if (intel_crtc->cursor_cntl != 0 &&
9414 (intel_crtc->cursor_base != base ||
9415 intel_crtc->cursor_size != size ||
9416 intel_crtc->cursor_cntl != cntl)) {
9417 /* On these chipsets we can only modify the base/size/stride
9418 * whilst the cursor is disabled.
9419 */
9420 I915_WRITE(_CURACNTR, 0);
4b0e333e 9421 POSTING_READ(_CURACNTR);
dc41c154 9422 intel_crtc->cursor_cntl = 0;
4b0e333e 9423 }
560b85bb 9424
99d1f387 9425 if (intel_crtc->cursor_base != base) {
9db4a9c7 9426 I915_WRITE(_CURABASE, base);
99d1f387
VS
9427 intel_crtc->cursor_base = base;
9428 }
4726e0b0 9429
dc41c154
VS
9430 if (intel_crtc->cursor_size != size) {
9431 I915_WRITE(CURSIZE, size);
9432 intel_crtc->cursor_size = size;
4b0e333e 9433 }
560b85bb 9434
4b0e333e 9435 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
9436 I915_WRITE(_CURACNTR, cntl);
9437 POSTING_READ(_CURACNTR);
4b0e333e 9438 intel_crtc->cursor_cntl = cntl;
560b85bb 9439 }
560b85bb
CW
9440}
9441
560b85bb 9442static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
9443{
9444 struct drm_device *dev = crtc->dev;
9445 struct drm_i915_private *dev_priv = dev->dev_private;
9446 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9447 int pipe = intel_crtc->pipe;
4b0e333e
CW
9448 uint32_t cntl;
9449
9450 cntl = 0;
9451 if (base) {
9452 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 9453 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
9454 case 64:
9455 cntl |= CURSOR_MODE_64_ARGB_AX;
9456 break;
9457 case 128:
9458 cntl |= CURSOR_MODE_128_ARGB_AX;
9459 break;
9460 case 256:
9461 cntl |= CURSOR_MODE_256_ARGB_AX;
9462 break;
9463 default:
3dd512fb 9464 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 9465 return;
65a21cd6 9466 }
4b0e333e 9467 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
9468
9469 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9470 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 9471 }
65a21cd6 9472
8e7d688b 9473 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
9474 cntl |= CURSOR_ROTATE_180;
9475
4b0e333e
CW
9476 if (intel_crtc->cursor_cntl != cntl) {
9477 I915_WRITE(CURCNTR(pipe), cntl);
9478 POSTING_READ(CURCNTR(pipe));
9479 intel_crtc->cursor_cntl = cntl;
65a21cd6 9480 }
4b0e333e 9481
65a21cd6 9482 /* and commit changes on next vblank */
5efb3e28
VS
9483 I915_WRITE(CURBASE(pipe), base);
9484 POSTING_READ(CURBASE(pipe));
99d1f387
VS
9485
9486 intel_crtc->cursor_base = base;
65a21cd6
JB
9487}
9488
cda4b7d3 9489/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
9490static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9491 bool on)
cda4b7d3
CW
9492{
9493 struct drm_device *dev = crtc->dev;
9494 struct drm_i915_private *dev_priv = dev->dev_private;
9495 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9496 int pipe = intel_crtc->pipe;
3d7d6510
MR
9497 int x = crtc->cursor_x;
9498 int y = crtc->cursor_y;
d6e4db15 9499 u32 base = 0, pos = 0;
cda4b7d3 9500
d6e4db15 9501 if (on)
cda4b7d3 9502 base = intel_crtc->cursor_addr;
cda4b7d3 9503
6e3c9717 9504 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
9505 base = 0;
9506
6e3c9717 9507 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
9508 base = 0;
9509
9510 if (x < 0) {
3dd512fb 9511 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
cda4b7d3
CW
9512 base = 0;
9513
9514 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9515 x = -x;
9516 }
9517 pos |= x << CURSOR_X_SHIFT;
9518
9519 if (y < 0) {
3dd512fb 9520 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
cda4b7d3
CW
9521 base = 0;
9522
9523 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9524 y = -y;
9525 }
9526 pos |= y << CURSOR_Y_SHIFT;
9527
4b0e333e 9528 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
9529 return;
9530
5efb3e28
VS
9531 I915_WRITE(CURPOS(pipe), pos);
9532
4398ad45
VS
9533 /* ILK+ do this automagically */
9534 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 9535 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
3dd512fb
MR
9536 base += (intel_crtc->base.cursor->state->crtc_h *
9537 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
4398ad45
VS
9538 }
9539
8ac54669 9540 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
9541 i845_update_cursor(crtc, base);
9542 else
9543 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
9544}
9545
dc41c154
VS
9546static bool cursor_size_ok(struct drm_device *dev,
9547 uint32_t width, uint32_t height)
9548{
9549 if (width == 0 || height == 0)
9550 return false;
9551
9552 /*
9553 * 845g/865g are special in that they are only limited by
9554 * the width of their cursors, the height is arbitrary up to
9555 * the precision of the register. Everything else requires
9556 * square cursors, limited to a few power-of-two sizes.
9557 */
9558 if (IS_845G(dev) || IS_I865G(dev)) {
9559 if ((width & 63) != 0)
9560 return false;
9561
9562 if (width > (IS_845G(dev) ? 64 : 512))
9563 return false;
9564
9565 if (height > 1023)
9566 return false;
9567 } else {
9568 switch (width | height) {
9569 case 256:
9570 case 128:
9571 if (IS_GEN2(dev))
9572 return false;
9573 case 64:
9574 break;
9575 default:
9576 return false;
9577 }
9578 }
9579
9580 return true;
9581}
9582
79e53945 9583static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 9584 u16 *blue, uint32_t start, uint32_t size)
79e53945 9585{
7203425a 9586 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 9587 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 9588
7203425a 9589 for (i = start; i < end; i++) {
79e53945
JB
9590 intel_crtc->lut_r[i] = red[i] >> 8;
9591 intel_crtc->lut_g[i] = green[i] >> 8;
9592 intel_crtc->lut_b[i] = blue[i] >> 8;
9593 }
9594
9595 intel_crtc_load_lut(crtc);
9596}
9597
79e53945
JB
9598/* VESA 640x480x72Hz mode to set on the pipe */
9599static struct drm_display_mode load_detect_mode = {
9600 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9601 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9602};
9603
a8bb6818
DV
9604struct drm_framebuffer *
9605__intel_framebuffer_create(struct drm_device *dev,
9606 struct drm_mode_fb_cmd2 *mode_cmd,
9607 struct drm_i915_gem_object *obj)
d2dff872
CW
9608{
9609 struct intel_framebuffer *intel_fb;
9610 int ret;
9611
9612 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9613 if (!intel_fb) {
6ccb81f2 9614 drm_gem_object_unreference(&obj->base);
d2dff872
CW
9615 return ERR_PTR(-ENOMEM);
9616 }
9617
9618 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
9619 if (ret)
9620 goto err;
d2dff872
CW
9621
9622 return &intel_fb->base;
dd4916c5 9623err:
6ccb81f2 9624 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
9625 kfree(intel_fb);
9626
9627 return ERR_PTR(ret);
d2dff872
CW
9628}
9629
b5ea642a 9630static struct drm_framebuffer *
a8bb6818
DV
9631intel_framebuffer_create(struct drm_device *dev,
9632 struct drm_mode_fb_cmd2 *mode_cmd,
9633 struct drm_i915_gem_object *obj)
9634{
9635 struct drm_framebuffer *fb;
9636 int ret;
9637
9638 ret = i915_mutex_lock_interruptible(dev);
9639 if (ret)
9640 return ERR_PTR(ret);
9641 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9642 mutex_unlock(&dev->struct_mutex);
9643
9644 return fb;
9645}
9646
d2dff872
CW
9647static u32
9648intel_framebuffer_pitch_for_width(int width, int bpp)
9649{
9650 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9651 return ALIGN(pitch, 64);
9652}
9653
9654static u32
9655intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9656{
9657 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 9658 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
9659}
9660
9661static struct drm_framebuffer *
9662intel_framebuffer_create_for_mode(struct drm_device *dev,
9663 struct drm_display_mode *mode,
9664 int depth, int bpp)
9665{
9666 struct drm_i915_gem_object *obj;
0fed39bd 9667 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
9668
9669 obj = i915_gem_alloc_object(dev,
9670 intel_framebuffer_size_for_mode(mode, bpp));
9671 if (obj == NULL)
9672 return ERR_PTR(-ENOMEM);
9673
9674 mode_cmd.width = mode->hdisplay;
9675 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
9676 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9677 bpp);
5ca0c34a 9678 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
9679
9680 return intel_framebuffer_create(dev, &mode_cmd, obj);
9681}
9682
9683static struct drm_framebuffer *
9684mode_fits_in_fbdev(struct drm_device *dev,
9685 struct drm_display_mode *mode)
9686{
4520f53a 9687#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
9688 struct drm_i915_private *dev_priv = dev->dev_private;
9689 struct drm_i915_gem_object *obj;
9690 struct drm_framebuffer *fb;
9691
4c0e5528 9692 if (!dev_priv->fbdev)
d2dff872
CW
9693 return NULL;
9694
4c0e5528 9695 if (!dev_priv->fbdev->fb)
d2dff872
CW
9696 return NULL;
9697
4c0e5528
DV
9698 obj = dev_priv->fbdev->fb->obj;
9699 BUG_ON(!obj);
9700
8bcd4553 9701 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
9702 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9703 fb->bits_per_pixel))
d2dff872
CW
9704 return NULL;
9705
01f2c773 9706 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
9707 return NULL;
9708
9709 return fb;
4520f53a
DV
9710#else
9711 return NULL;
9712#endif
d2dff872
CW
9713}
9714
d2434ab7 9715bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 9716 struct drm_display_mode *mode,
51fd371b
RC
9717 struct intel_load_detect_pipe *old,
9718 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
9719{
9720 struct intel_crtc *intel_crtc;
d2434ab7
DV
9721 struct intel_encoder *intel_encoder =
9722 intel_attached_encoder(connector);
79e53945 9723 struct drm_crtc *possible_crtc;
4ef69c7a 9724 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
9725 struct drm_crtc *crtc = NULL;
9726 struct drm_device *dev = encoder->dev;
94352cf9 9727 struct drm_framebuffer *fb;
51fd371b 9728 struct drm_mode_config *config = &dev->mode_config;
83a57153 9729 struct drm_atomic_state *state = NULL;
944b0c76 9730 struct drm_connector_state *connector_state;
51fd371b 9731 int ret, i = -1;
79e53945 9732
d2dff872 9733 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9734 connector->base.id, connector->name,
8e329a03 9735 encoder->base.id, encoder->name);
d2dff872 9736
51fd371b
RC
9737retry:
9738 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9739 if (ret)
9740 goto fail_unlock;
6e9f798d 9741
79e53945
JB
9742 /*
9743 * Algorithm gets a little messy:
7a5e4805 9744 *
79e53945
JB
9745 * - if the connector already has an assigned crtc, use it (but make
9746 * sure it's on first)
7a5e4805 9747 *
79e53945
JB
9748 * - try to find the first unused crtc that can drive this connector,
9749 * and use that if we find one
79e53945
JB
9750 */
9751
9752 /* See if we already have a CRTC for this connector */
9753 if (encoder->crtc) {
9754 crtc = encoder->crtc;
8261b191 9755
51fd371b 9756 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de
DV
9757 if (ret)
9758 goto fail_unlock;
9759 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b
RC
9760 if (ret)
9761 goto fail_unlock;
7b24056b 9762
24218aac 9763 old->dpms_mode = connector->dpms;
8261b191
CW
9764 old->load_detect_temp = false;
9765
9766 /* Make sure the crtc and connector are running */
24218aac
DV
9767 if (connector->dpms != DRM_MODE_DPMS_ON)
9768 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 9769
7173188d 9770 return true;
79e53945
JB
9771 }
9772
9773 /* Find an unused one (if possible) */
70e1e0ec 9774 for_each_crtc(dev, possible_crtc) {
79e53945
JB
9775 i++;
9776 if (!(encoder->possible_crtcs & (1 << i)))
9777 continue;
83d65738 9778 if (possible_crtc->state->enable)
a459249c
VS
9779 continue;
9780 /* This can occur when applying the pipe A quirk on resume. */
9781 if (to_intel_crtc(possible_crtc)->new_enabled)
9782 continue;
9783
9784 crtc = possible_crtc;
9785 break;
79e53945
JB
9786 }
9787
9788 /*
9789 * If we didn't find an unused CRTC, don't use any.
9790 */
9791 if (!crtc) {
7173188d 9792 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 9793 goto fail_unlock;
79e53945
JB
9794 }
9795
51fd371b
RC
9796 ret = drm_modeset_lock(&crtc->mutex, ctx);
9797 if (ret)
4d02e2de
DV
9798 goto fail_unlock;
9799 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9800 if (ret)
51fd371b 9801 goto fail_unlock;
fc303101
DV
9802 intel_encoder->new_crtc = to_intel_crtc(crtc);
9803 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
9804
9805 intel_crtc = to_intel_crtc(crtc);
412b61d8 9806 intel_crtc->new_enabled = true;
24218aac 9807 old->dpms_mode = connector->dpms;
8261b191 9808 old->load_detect_temp = true;
d2dff872 9809 old->release_fb = NULL;
79e53945 9810
83a57153
ACO
9811 state = drm_atomic_state_alloc(dev);
9812 if (!state)
9813 return false;
9814
9815 state->acquire_ctx = ctx;
9816
944b0c76
ACO
9817 connector_state = drm_atomic_get_connector_state(state, connector);
9818 if (IS_ERR(connector_state)) {
9819 ret = PTR_ERR(connector_state);
9820 goto fail;
9821 }
9822
9823 connector_state->crtc = crtc;
9824 connector_state->best_encoder = &intel_encoder->base;
9825
6492711d
CW
9826 if (!mode)
9827 mode = &load_detect_mode;
79e53945 9828
d2dff872
CW
9829 /* We need a framebuffer large enough to accommodate all accesses
9830 * that the plane may generate whilst we perform load detection.
9831 * We can not rely on the fbcon either being present (we get called
9832 * during its initialisation to detect all boot displays, or it may
9833 * not even exist) or that it is large enough to satisfy the
9834 * requested mode.
9835 */
94352cf9
DV
9836 fb = mode_fits_in_fbdev(dev, mode);
9837 if (fb == NULL) {
d2dff872 9838 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
9839 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9840 old->release_fb = fb;
d2dff872
CW
9841 } else
9842 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 9843 if (IS_ERR(fb)) {
d2dff872 9844 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 9845 goto fail;
79e53945 9846 }
79e53945 9847
83a57153 9848 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
6492711d 9849 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
9850 if (old->release_fb)
9851 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 9852 goto fail;
79e53945 9853 }
9128b040 9854 crtc->primary->crtc = crtc;
7173188d 9855
79e53945 9856 /* let the connector get through one full cycle before testing */
9d0498a2 9857 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 9858 return true;
412b61d8
VS
9859
9860 fail:
83d65738 9861 intel_crtc->new_enabled = crtc->state->enable;
51fd371b 9862fail_unlock:
e5d958ef
ACO
9863 drm_atomic_state_free(state);
9864 state = NULL;
83a57153 9865
51fd371b
RC
9866 if (ret == -EDEADLK) {
9867 drm_modeset_backoff(ctx);
9868 goto retry;
9869 }
9870
412b61d8 9871 return false;
79e53945
JB
9872}
9873
d2434ab7 9874void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
9875 struct intel_load_detect_pipe *old,
9876 struct drm_modeset_acquire_ctx *ctx)
79e53945 9877{
83a57153 9878 struct drm_device *dev = connector->dev;
d2434ab7
DV
9879 struct intel_encoder *intel_encoder =
9880 intel_attached_encoder(connector);
4ef69c7a 9881 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 9882 struct drm_crtc *crtc = encoder->crtc;
412b61d8 9883 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83a57153 9884 struct drm_atomic_state *state;
944b0c76 9885 struct drm_connector_state *connector_state;
79e53945 9886
d2dff872 9887 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9888 connector->base.id, connector->name,
8e329a03 9889 encoder->base.id, encoder->name);
d2dff872 9890
8261b191 9891 if (old->load_detect_temp) {
83a57153 9892 state = drm_atomic_state_alloc(dev);
944b0c76
ACO
9893 if (!state)
9894 goto fail;
83a57153
ACO
9895
9896 state->acquire_ctx = ctx;
9897
944b0c76
ACO
9898 connector_state = drm_atomic_get_connector_state(state, connector);
9899 if (IS_ERR(connector_state))
9900 goto fail;
9901
fc303101
DV
9902 to_intel_connector(connector)->new_encoder = NULL;
9903 intel_encoder->new_crtc = NULL;
412b61d8 9904 intel_crtc->new_enabled = false;
944b0c76
ACO
9905
9906 connector_state->best_encoder = NULL;
9907 connector_state->crtc = NULL;
9908
83a57153
ACO
9909 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9910
9911 drm_atomic_state_free(state);
d2dff872 9912
36206361
DV
9913 if (old->release_fb) {
9914 drm_framebuffer_unregister_private(old->release_fb);
9915 drm_framebuffer_unreference(old->release_fb);
9916 }
d2dff872 9917
0622a53c 9918 return;
79e53945
JB
9919 }
9920
c751ce4f 9921 /* Switch crtc and encoder back off if necessary */
24218aac
DV
9922 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9923 connector->funcs->dpms(connector, old->dpms_mode);
944b0c76
ACO
9924
9925 return;
9926fail:
9927 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9928 drm_atomic_state_free(state);
79e53945
JB
9929}
9930
da4a1efa 9931static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 9932 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
9933{
9934 struct drm_i915_private *dev_priv = dev->dev_private;
9935 u32 dpll = pipe_config->dpll_hw_state.dpll;
9936
9937 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 9938 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
9939 else if (HAS_PCH_SPLIT(dev))
9940 return 120000;
9941 else if (!IS_GEN2(dev))
9942 return 96000;
9943 else
9944 return 48000;
9945}
9946
79e53945 9947/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 9948static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9949 struct intel_crtc_state *pipe_config)
79e53945 9950{
f1f644dc 9951 struct drm_device *dev = crtc->base.dev;
79e53945 9952 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 9953 int pipe = pipe_config->cpu_transcoder;
293623f7 9954 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
9955 u32 fp;
9956 intel_clock_t clock;
da4a1efa 9957 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
9958
9959 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 9960 fp = pipe_config->dpll_hw_state.fp0;
79e53945 9961 else
293623f7 9962 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
9963
9964 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
9965 if (IS_PINEVIEW(dev)) {
9966 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9967 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
9968 } else {
9969 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9970 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9971 }
9972
a6c45cf0 9973 if (!IS_GEN2(dev)) {
f2b115e6
AJ
9974 if (IS_PINEVIEW(dev))
9975 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9976 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
9977 else
9978 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
9979 DPLL_FPA01_P1_POST_DIV_SHIFT);
9980
9981 switch (dpll & DPLL_MODE_MASK) {
9982 case DPLLB_MODE_DAC_SERIAL:
9983 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9984 5 : 10;
9985 break;
9986 case DPLLB_MODE_LVDS:
9987 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9988 7 : 14;
9989 break;
9990 default:
28c97730 9991 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 9992 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 9993 return;
79e53945
JB
9994 }
9995
ac58c3f0 9996 if (IS_PINEVIEW(dev))
da4a1efa 9997 pineview_clock(refclk, &clock);
ac58c3f0 9998 else
da4a1efa 9999 i9xx_clock(refclk, &clock);
79e53945 10000 } else {
0fb58223 10001 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10002 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10003
10004 if (is_lvds) {
10005 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10006 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10007
10008 if (lvds & LVDS_CLKB_POWER_UP)
10009 clock.p2 = 7;
10010 else
10011 clock.p2 = 14;
79e53945
JB
10012 } else {
10013 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10014 clock.p1 = 2;
10015 else {
10016 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10017 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10018 }
10019 if (dpll & PLL_P2_DIVIDE_BY_4)
10020 clock.p2 = 4;
10021 else
10022 clock.p2 = 2;
79e53945 10023 }
da4a1efa
VS
10024
10025 i9xx_clock(refclk, &clock);
79e53945
JB
10026 }
10027
18442d08
VS
10028 /*
10029 * This value includes pixel_multiplier. We will use
241bfc38 10030 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10031 * encoder's get_config() function.
10032 */
10033 pipe_config->port_clock = clock.dot;
f1f644dc
JB
10034}
10035
6878da05
VS
10036int intel_dotclock_calculate(int link_freq,
10037 const struct intel_link_m_n *m_n)
f1f644dc 10038{
f1f644dc
JB
10039 /*
10040 * The calculation for the data clock is:
1041a02f 10041 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10042 * But we want to avoid losing precison if possible, so:
1041a02f 10043 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10044 *
10045 * and the link clock is simpler:
1041a02f 10046 * link_clock = (m * link_clock) / n
f1f644dc
JB
10047 */
10048
6878da05
VS
10049 if (!m_n->link_n)
10050 return 0;
f1f644dc 10051
6878da05
VS
10052 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10053}
f1f644dc 10054
18442d08 10055static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10056 struct intel_crtc_state *pipe_config)
6878da05
VS
10057{
10058 struct drm_device *dev = crtc->base.dev;
79e53945 10059
18442d08
VS
10060 /* read out port_clock from the DPLL */
10061 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10062
f1f644dc 10063 /*
18442d08 10064 * This value does not include pixel_multiplier.
241bfc38 10065 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
10066 * agree once we know their relationship in the encoder's
10067 * get_config() function.
79e53945 10068 */
2d112de7 10069 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
10070 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10071 &pipe_config->fdi_m_n);
79e53945
JB
10072}
10073
10074/** Returns the currently programmed mode of the given pipe. */
10075struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10076 struct drm_crtc *crtc)
10077{
548f245b 10078 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10079 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10080 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10081 struct drm_display_mode *mode;
5cec258b 10082 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
10083 int htot = I915_READ(HTOTAL(cpu_transcoder));
10084 int hsync = I915_READ(HSYNC(cpu_transcoder));
10085 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10086 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10087 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10088
10089 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10090 if (!mode)
10091 return NULL;
10092
f1f644dc
JB
10093 /*
10094 * Construct a pipe_config sufficient for getting the clock info
10095 * back out of crtc_clock_get.
10096 *
10097 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10098 * to use a real value here instead.
10099 */
293623f7 10100 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 10101 pipe_config.pixel_multiplier = 1;
293623f7
VS
10102 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10103 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10104 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
10105 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10106
773ae034 10107 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
10108 mode->hdisplay = (htot & 0xffff) + 1;
10109 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10110 mode->hsync_start = (hsync & 0xffff) + 1;
10111 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10112 mode->vdisplay = (vtot & 0xffff) + 1;
10113 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10114 mode->vsync_start = (vsync & 0xffff) + 1;
10115 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10116
10117 drm_mode_set_name(mode);
79e53945
JB
10118
10119 return mode;
10120}
10121
652c393a
JB
10122static void intel_decrease_pllclock(struct drm_crtc *crtc)
10123{
10124 struct drm_device *dev = crtc->dev;
fbee40df 10125 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10126 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 10127
baff296c 10128 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
10129 return;
10130
10131 if (!dev_priv->lvds_downclock_avail)
10132 return;
10133
10134 /*
10135 * Since this is called by a timer, we should never get here in
10136 * the manual case.
10137 */
10138 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
10139 int pipe = intel_crtc->pipe;
10140 int dpll_reg = DPLL(pipe);
10141 int dpll;
f6e5b160 10142
44d98a61 10143 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 10144
8ac5a6d5 10145 assert_panel_unlocked(dev_priv, pipe);
652c393a 10146
dc257cf1 10147 dpll = I915_READ(dpll_reg);
652c393a
JB
10148 dpll |= DISPLAY_RATE_SELECT_FPA1;
10149 I915_WRITE(dpll_reg, dpll);
9d0498a2 10150 intel_wait_for_vblank(dev, pipe);
652c393a
JB
10151 dpll = I915_READ(dpll_reg);
10152 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 10153 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
10154 }
10155
10156}
10157
f047e395
CW
10158void intel_mark_busy(struct drm_device *dev)
10159{
c67a470b
PZ
10160 struct drm_i915_private *dev_priv = dev->dev_private;
10161
f62a0076
CW
10162 if (dev_priv->mm.busy)
10163 return;
10164
43694d69 10165 intel_runtime_pm_get(dev_priv);
c67a470b 10166 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10167 if (INTEL_INFO(dev)->gen >= 6)
10168 gen6_rps_busy(dev_priv);
f62a0076 10169 dev_priv->mm.busy = true;
f047e395
CW
10170}
10171
10172void intel_mark_idle(struct drm_device *dev)
652c393a 10173{
c67a470b 10174 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10175 struct drm_crtc *crtc;
652c393a 10176
f62a0076
CW
10177 if (!dev_priv->mm.busy)
10178 return;
10179
10180 dev_priv->mm.busy = false;
10181
70e1e0ec 10182 for_each_crtc(dev, crtc) {
f4510a27 10183 if (!crtc->primary->fb)
652c393a
JB
10184 continue;
10185
725a5b54 10186 intel_decrease_pllclock(crtc);
652c393a 10187 }
b29c19b6 10188
3d13ef2e 10189 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10190 gen6_rps_idle(dev->dev_private);
bb4cdd53 10191
43694d69 10192 intel_runtime_pm_put(dev_priv);
652c393a
JB
10193}
10194
f5de6e07
ACO
10195static void intel_crtc_set_state(struct intel_crtc *crtc,
10196 struct intel_crtc_state *crtc_state)
10197{
10198 kfree(crtc->config);
10199 crtc->config = crtc_state;
16f3f658 10200 crtc->base.state = &crtc_state->base;
f5de6e07
ACO
10201}
10202
79e53945
JB
10203static void intel_crtc_destroy(struct drm_crtc *crtc)
10204{
10205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10206 struct drm_device *dev = crtc->dev;
10207 struct intel_unpin_work *work;
67e77c5a 10208
5e2d7afc 10209 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10210 work = intel_crtc->unpin_work;
10211 intel_crtc->unpin_work = NULL;
5e2d7afc 10212 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10213
10214 if (work) {
10215 cancel_work_sync(&work->work);
10216 kfree(work);
10217 }
79e53945 10218
f5de6e07 10219 intel_crtc_set_state(intel_crtc, NULL);
79e53945 10220 drm_crtc_cleanup(crtc);
67e77c5a 10221
79e53945
JB
10222 kfree(intel_crtc);
10223}
10224
6b95a207
KH
10225static void intel_unpin_work_fn(struct work_struct *__work)
10226{
10227 struct intel_unpin_work *work =
10228 container_of(__work, struct intel_unpin_work, work);
b4a98e57 10229 struct drm_device *dev = work->crtc->dev;
f99d7069 10230 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 10231
b4a98e57 10232 mutex_lock(&dev->struct_mutex);
82bc3b2d 10233 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
05394f39 10234 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10235
7ff0ebcc 10236 intel_fbc_update(dev);
f06cc1b9
JH
10237
10238 if (work->flip_queued_req)
146d84f0 10239 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10240 mutex_unlock(&dev->struct_mutex);
10241
f99d7069 10242 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
89ed88ba 10243 drm_framebuffer_unreference(work->old_fb);
f99d7069 10244
b4a98e57
CW
10245 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10246 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10247
6b95a207
KH
10248 kfree(work);
10249}
10250
1afe3e9d 10251static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10252 struct drm_crtc *crtc)
6b95a207 10253{
6b95a207
KH
10254 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10255 struct intel_unpin_work *work;
6b95a207
KH
10256 unsigned long flags;
10257
10258 /* Ignore early vblank irqs */
10259 if (intel_crtc == NULL)
10260 return;
10261
f326038a
DV
10262 /*
10263 * This is called both by irq handlers and the reset code (to complete
10264 * lost pageflips) so needs the full irqsave spinlocks.
10265 */
6b95a207
KH
10266 spin_lock_irqsave(&dev->event_lock, flags);
10267 work = intel_crtc->unpin_work;
e7d841ca
CW
10268
10269 /* Ensure we don't miss a work->pending update ... */
10270 smp_rmb();
10271
10272 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10273 spin_unlock_irqrestore(&dev->event_lock, flags);
10274 return;
10275 }
10276
d6bbafa1 10277 page_flip_completed(intel_crtc);
0af7e4df 10278
6b95a207 10279 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10280}
10281
1afe3e9d
JB
10282void intel_finish_page_flip(struct drm_device *dev, int pipe)
10283{
fbee40df 10284 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10285 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10286
49b14a5c 10287 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10288}
10289
10290void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10291{
fbee40df 10292 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10293 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10294
49b14a5c 10295 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10296}
10297
75f7f3ec
VS
10298/* Is 'a' after or equal to 'b'? */
10299static bool g4x_flip_count_after_eq(u32 a, u32 b)
10300{
10301 return !((a - b) & 0x80000000);
10302}
10303
10304static bool page_flip_finished(struct intel_crtc *crtc)
10305{
10306 struct drm_device *dev = crtc->base.dev;
10307 struct drm_i915_private *dev_priv = dev->dev_private;
10308
bdfa7542
VS
10309 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10310 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10311 return true;
10312
75f7f3ec
VS
10313 /*
10314 * The relevant registers doen't exist on pre-ctg.
10315 * As the flip done interrupt doesn't trigger for mmio
10316 * flips on gmch platforms, a flip count check isn't
10317 * really needed there. But since ctg has the registers,
10318 * include it in the check anyway.
10319 */
10320 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10321 return true;
10322
10323 /*
10324 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10325 * used the same base address. In that case the mmio flip might
10326 * have completed, but the CS hasn't even executed the flip yet.
10327 *
10328 * A flip count check isn't enough as the CS might have updated
10329 * the base address just after start of vblank, but before we
10330 * managed to process the interrupt. This means we'd complete the
10331 * CS flip too soon.
10332 *
10333 * Combining both checks should get us a good enough result. It may
10334 * still happen that the CS flip has been executed, but has not
10335 * yet actually completed. But in case the base address is the same
10336 * anyway, we don't really care.
10337 */
10338 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10339 crtc->unpin_work->gtt_offset &&
10340 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10341 crtc->unpin_work->flip_count);
10342}
10343
6b95a207
KH
10344void intel_prepare_page_flip(struct drm_device *dev, int plane)
10345{
fbee40df 10346 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10347 struct intel_crtc *intel_crtc =
10348 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10349 unsigned long flags;
10350
f326038a
DV
10351
10352 /*
10353 * This is called both by irq handlers and the reset code (to complete
10354 * lost pageflips) so needs the full irqsave spinlocks.
10355 *
10356 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10357 * generate a page-flip completion irq, i.e. every modeset
10358 * is also accompanied by a spurious intel_prepare_page_flip().
10359 */
6b95a207 10360 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10361 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10362 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10363 spin_unlock_irqrestore(&dev->event_lock, flags);
10364}
10365
eba905b2 10366static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
10367{
10368 /* Ensure that the work item is consistent when activating it ... */
10369 smp_wmb();
10370 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10371 /* and that it is marked active as soon as the irq could fire. */
10372 smp_wmb();
10373}
10374
8c9f3aaf
JB
10375static int intel_gen2_queue_flip(struct drm_device *dev,
10376 struct drm_crtc *crtc,
10377 struct drm_framebuffer *fb,
ed8d1975 10378 struct drm_i915_gem_object *obj,
a4872ba6 10379 struct intel_engine_cs *ring,
ed8d1975 10380 uint32_t flags)
8c9f3aaf 10381{
8c9f3aaf 10382 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10383 u32 flip_mask;
10384 int ret;
10385
6d90c952 10386 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10387 if (ret)
4fa62c89 10388 return ret;
8c9f3aaf
JB
10389
10390 /* Can't queue multiple flips, so wait for the previous
10391 * one to finish before executing the next.
10392 */
10393 if (intel_crtc->plane)
10394 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10395 else
10396 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10397 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10398 intel_ring_emit(ring, MI_NOOP);
10399 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10400 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10401 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10402 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 10403 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
10404
10405 intel_mark_page_flip_active(intel_crtc);
09246732 10406 __intel_ring_advance(ring);
83d4092b 10407 return 0;
8c9f3aaf
JB
10408}
10409
10410static int intel_gen3_queue_flip(struct drm_device *dev,
10411 struct drm_crtc *crtc,
10412 struct drm_framebuffer *fb,
ed8d1975 10413 struct drm_i915_gem_object *obj,
a4872ba6 10414 struct intel_engine_cs *ring,
ed8d1975 10415 uint32_t flags)
8c9f3aaf 10416{
8c9f3aaf 10417 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10418 u32 flip_mask;
10419 int ret;
10420
6d90c952 10421 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10422 if (ret)
4fa62c89 10423 return ret;
8c9f3aaf
JB
10424
10425 if (intel_crtc->plane)
10426 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10427 else
10428 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10429 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10430 intel_ring_emit(ring, MI_NOOP);
10431 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10432 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10433 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10434 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
10435 intel_ring_emit(ring, MI_NOOP);
10436
e7d841ca 10437 intel_mark_page_flip_active(intel_crtc);
09246732 10438 __intel_ring_advance(ring);
83d4092b 10439 return 0;
8c9f3aaf
JB
10440}
10441
10442static int intel_gen4_queue_flip(struct drm_device *dev,
10443 struct drm_crtc *crtc,
10444 struct drm_framebuffer *fb,
ed8d1975 10445 struct drm_i915_gem_object *obj,
a4872ba6 10446 struct intel_engine_cs *ring,
ed8d1975 10447 uint32_t flags)
8c9f3aaf
JB
10448{
10449 struct drm_i915_private *dev_priv = dev->dev_private;
10450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10451 uint32_t pf, pipesrc;
10452 int ret;
10453
6d90c952 10454 ret = intel_ring_begin(ring, 4);
8c9f3aaf 10455 if (ret)
4fa62c89 10456 return ret;
8c9f3aaf
JB
10457
10458 /* i965+ uses the linear or tiled offsets from the
10459 * Display Registers (which do not change across a page-flip)
10460 * so we need only reprogram the base address.
10461 */
6d90c952
DV
10462 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10463 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10464 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10465 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 10466 obj->tiling_mode);
8c9f3aaf
JB
10467
10468 /* XXX Enabling the panel-fitter across page-flip is so far
10469 * untested on non-native modes, so ignore it for now.
10470 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10471 */
10472 pf = 0;
10473 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10474 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10475
10476 intel_mark_page_flip_active(intel_crtc);
09246732 10477 __intel_ring_advance(ring);
83d4092b 10478 return 0;
8c9f3aaf
JB
10479}
10480
10481static int intel_gen6_queue_flip(struct drm_device *dev,
10482 struct drm_crtc *crtc,
10483 struct drm_framebuffer *fb,
ed8d1975 10484 struct drm_i915_gem_object *obj,
a4872ba6 10485 struct intel_engine_cs *ring,
ed8d1975 10486 uint32_t flags)
8c9f3aaf
JB
10487{
10488 struct drm_i915_private *dev_priv = dev->dev_private;
10489 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10490 uint32_t pf, pipesrc;
10491 int ret;
10492
6d90c952 10493 ret = intel_ring_begin(ring, 4);
8c9f3aaf 10494 if (ret)
4fa62c89 10495 return ret;
8c9f3aaf 10496
6d90c952
DV
10497 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10498 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10499 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 10500 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 10501
dc257cf1
DV
10502 /* Contrary to the suggestions in the documentation,
10503 * "Enable Panel Fitter" does not seem to be required when page
10504 * flipping with a non-native mode, and worse causes a normal
10505 * modeset to fail.
10506 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10507 */
10508 pf = 0;
8c9f3aaf 10509 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10510 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10511
10512 intel_mark_page_flip_active(intel_crtc);
09246732 10513 __intel_ring_advance(ring);
83d4092b 10514 return 0;
8c9f3aaf
JB
10515}
10516
7c9017e5
JB
10517static int intel_gen7_queue_flip(struct drm_device *dev,
10518 struct drm_crtc *crtc,
10519 struct drm_framebuffer *fb,
ed8d1975 10520 struct drm_i915_gem_object *obj,
a4872ba6 10521 struct intel_engine_cs *ring,
ed8d1975 10522 uint32_t flags)
7c9017e5 10523{
7c9017e5 10524 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 10525 uint32_t plane_bit = 0;
ffe74d75
CW
10526 int len, ret;
10527
eba905b2 10528 switch (intel_crtc->plane) {
cb05d8de
DV
10529 case PLANE_A:
10530 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10531 break;
10532 case PLANE_B:
10533 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10534 break;
10535 case PLANE_C:
10536 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10537 break;
10538 default:
10539 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 10540 return -ENODEV;
cb05d8de
DV
10541 }
10542
ffe74d75 10543 len = 4;
f476828a 10544 if (ring->id == RCS) {
ffe74d75 10545 len += 6;
f476828a
DL
10546 /*
10547 * On Gen 8, SRM is now taking an extra dword to accommodate
10548 * 48bits addresses, and we need a NOOP for the batch size to
10549 * stay even.
10550 */
10551 if (IS_GEN8(dev))
10552 len += 2;
10553 }
ffe74d75 10554
f66fab8e
VS
10555 /*
10556 * BSpec MI_DISPLAY_FLIP for IVB:
10557 * "The full packet must be contained within the same cache line."
10558 *
10559 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10560 * cacheline, if we ever start emitting more commands before
10561 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10562 * then do the cacheline alignment, and finally emit the
10563 * MI_DISPLAY_FLIP.
10564 */
10565 ret = intel_ring_cacheline_align(ring);
10566 if (ret)
4fa62c89 10567 return ret;
f66fab8e 10568
ffe74d75 10569 ret = intel_ring_begin(ring, len);
7c9017e5 10570 if (ret)
4fa62c89 10571 return ret;
7c9017e5 10572
ffe74d75
CW
10573 /* Unmask the flip-done completion message. Note that the bspec says that
10574 * we should do this for both the BCS and RCS, and that we must not unmask
10575 * more than one flip event at any time (or ensure that one flip message
10576 * can be sent by waiting for flip-done prior to queueing new flips).
10577 * Experimentation says that BCS works despite DERRMR masking all
10578 * flip-done completion events and that unmasking all planes at once
10579 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10580 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10581 */
10582 if (ring->id == RCS) {
10583 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10584 intel_ring_emit(ring, DERRMR);
10585 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10586 DERRMR_PIPEB_PRI_FLIP_DONE |
10587 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
10588 if (IS_GEN8(dev))
10589 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10590 MI_SRM_LRM_GLOBAL_GTT);
10591 else
10592 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10593 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
10594 intel_ring_emit(ring, DERRMR);
10595 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
10596 if (IS_GEN8(dev)) {
10597 intel_ring_emit(ring, 0);
10598 intel_ring_emit(ring, MI_NOOP);
10599 }
ffe74d75
CW
10600 }
10601
cb05d8de 10602 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 10603 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 10604 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 10605 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
10606
10607 intel_mark_page_flip_active(intel_crtc);
09246732 10608 __intel_ring_advance(ring);
83d4092b 10609 return 0;
7c9017e5
JB
10610}
10611
84c33a64
SG
10612static bool use_mmio_flip(struct intel_engine_cs *ring,
10613 struct drm_i915_gem_object *obj)
10614{
10615 /*
10616 * This is not being used for older platforms, because
10617 * non-availability of flip done interrupt forces us to use
10618 * CS flips. Older platforms derive flip done using some clever
10619 * tricks involving the flip_pending status bits and vblank irqs.
10620 * So using MMIO flips there would disrupt this mechanism.
10621 */
10622
8e09bf83
CW
10623 if (ring == NULL)
10624 return true;
10625
84c33a64
SG
10626 if (INTEL_INFO(ring->dev)->gen < 5)
10627 return false;
10628
10629 if (i915.use_mmio_flip < 0)
10630 return false;
10631 else if (i915.use_mmio_flip > 0)
10632 return true;
14bf993e
OM
10633 else if (i915.enable_execlists)
10634 return true;
84c33a64 10635 else
41c52415 10636 return ring != i915_gem_request_get_ring(obj->last_read_req);
84c33a64
SG
10637}
10638
ff944564
DL
10639static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10640{
10641 struct drm_device *dev = intel_crtc->base.dev;
10642 struct drm_i915_private *dev_priv = dev->dev_private;
10643 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564
DL
10644 const enum pipe pipe = intel_crtc->pipe;
10645 u32 ctl, stride;
10646
10647 ctl = I915_READ(PLANE_CTL(pipe, 0));
10648 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
10649 switch (fb->modifier[0]) {
10650 case DRM_FORMAT_MOD_NONE:
10651 break;
10652 case I915_FORMAT_MOD_X_TILED:
ff944564 10653 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
10654 break;
10655 case I915_FORMAT_MOD_Y_TILED:
10656 ctl |= PLANE_CTL_TILED_Y;
10657 break;
10658 case I915_FORMAT_MOD_Yf_TILED:
10659 ctl |= PLANE_CTL_TILED_YF;
10660 break;
10661 default:
10662 MISSING_CASE(fb->modifier[0]);
10663 }
ff944564
DL
10664
10665 /*
10666 * The stride is either expressed as a multiple of 64 bytes chunks for
10667 * linear buffers or in number of tiles for tiled buffers.
10668 */
2ebef630
TU
10669 stride = fb->pitches[0] /
10670 intel_fb_stride_alignment(dev, fb->modifier[0],
10671 fb->pixel_format);
ff944564
DL
10672
10673 /*
10674 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10675 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10676 */
10677 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10678 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10679
10680 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10681 POSTING_READ(PLANE_SURF(pipe, 0));
10682}
10683
10684static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
10685{
10686 struct drm_device *dev = intel_crtc->base.dev;
10687 struct drm_i915_private *dev_priv = dev->dev_private;
10688 struct intel_framebuffer *intel_fb =
10689 to_intel_framebuffer(intel_crtc->base.primary->fb);
10690 struct drm_i915_gem_object *obj = intel_fb->obj;
10691 u32 dspcntr;
10692 u32 reg;
10693
84c33a64
SG
10694 reg = DSPCNTR(intel_crtc->plane);
10695 dspcntr = I915_READ(reg);
10696
c5d97472
DL
10697 if (obj->tiling_mode != I915_TILING_NONE)
10698 dspcntr |= DISPPLANE_TILED;
10699 else
10700 dspcntr &= ~DISPPLANE_TILED;
10701
84c33a64
SG
10702 I915_WRITE(reg, dspcntr);
10703
10704 I915_WRITE(DSPSURF(intel_crtc->plane),
10705 intel_crtc->unpin_work->gtt_offset);
10706 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 10707
ff944564
DL
10708}
10709
10710/*
10711 * XXX: This is the temporary way to update the plane registers until we get
10712 * around to using the usual plane update functions for MMIO flips
10713 */
10714static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10715{
10716 struct drm_device *dev = intel_crtc->base.dev;
10717 bool atomic_update;
10718 u32 start_vbl_count;
10719
10720 intel_mark_page_flip_active(intel_crtc);
10721
10722 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10723
10724 if (INTEL_INFO(dev)->gen >= 9)
10725 skl_do_mmio_flip(intel_crtc);
10726 else
10727 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10728 ilk_do_mmio_flip(intel_crtc);
10729
9362c7c5
ACO
10730 if (atomic_update)
10731 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
10732}
10733
9362c7c5 10734static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 10735{
cc8c4cc2 10736 struct intel_crtc *crtc =
9362c7c5 10737 container_of(work, struct intel_crtc, mmio_flip.work);
cc8c4cc2 10738 struct intel_mmio_flip *mmio_flip;
84c33a64 10739
cc8c4cc2
JH
10740 mmio_flip = &crtc->mmio_flip;
10741 if (mmio_flip->req)
9c654818
JH
10742 WARN_ON(__i915_wait_request(mmio_flip->req,
10743 crtc->reset_counter,
10744 false, NULL, NULL) != 0);
84c33a64 10745
cc8c4cc2
JH
10746 intel_do_mmio_flip(crtc);
10747 if (mmio_flip->req) {
10748 mutex_lock(&crtc->base.dev->struct_mutex);
146d84f0 10749 i915_gem_request_assign(&mmio_flip->req, NULL);
cc8c4cc2
JH
10750 mutex_unlock(&crtc->base.dev->struct_mutex);
10751 }
84c33a64
SG
10752}
10753
10754static int intel_queue_mmio_flip(struct drm_device *dev,
10755 struct drm_crtc *crtc,
10756 struct drm_framebuffer *fb,
10757 struct drm_i915_gem_object *obj,
10758 struct intel_engine_cs *ring,
10759 uint32_t flags)
10760{
84c33a64 10761 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
84c33a64 10762
cc8c4cc2
JH
10763 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10764 obj->last_write_req);
536f5b5e
ACO
10765
10766 schedule_work(&intel_crtc->mmio_flip.work);
84c33a64 10767
84c33a64
SG
10768 return 0;
10769}
10770
8c9f3aaf
JB
10771static int intel_default_queue_flip(struct drm_device *dev,
10772 struct drm_crtc *crtc,
10773 struct drm_framebuffer *fb,
ed8d1975 10774 struct drm_i915_gem_object *obj,
a4872ba6 10775 struct intel_engine_cs *ring,
ed8d1975 10776 uint32_t flags)
8c9f3aaf
JB
10777{
10778 return -ENODEV;
10779}
10780
d6bbafa1
CW
10781static bool __intel_pageflip_stall_check(struct drm_device *dev,
10782 struct drm_crtc *crtc)
10783{
10784 struct drm_i915_private *dev_priv = dev->dev_private;
10785 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10786 struct intel_unpin_work *work = intel_crtc->unpin_work;
10787 u32 addr;
10788
10789 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10790 return true;
10791
10792 if (!work->enable_stall_check)
10793 return false;
10794
10795 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
10796 if (work->flip_queued_req &&
10797 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
10798 return false;
10799
1e3feefd 10800 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
10801 }
10802
1e3feefd 10803 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
10804 return false;
10805
10806 /* Potential stall - if we see that the flip has happened,
10807 * assume a missed interrupt. */
10808 if (INTEL_INFO(dev)->gen >= 4)
10809 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10810 else
10811 addr = I915_READ(DSPADDR(intel_crtc->plane));
10812
10813 /* There is a potential issue here with a false positive after a flip
10814 * to the same address. We could address this by checking for a
10815 * non-incrementing frame counter.
10816 */
10817 return addr == work->gtt_offset;
10818}
10819
10820void intel_check_page_flip(struct drm_device *dev, int pipe)
10821{
10822 struct drm_i915_private *dev_priv = dev->dev_private;
10823 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10824 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 10825 struct intel_unpin_work *work;
f326038a 10826
6c51d46f 10827 WARN_ON(!in_interrupt());
d6bbafa1
CW
10828
10829 if (crtc == NULL)
10830 return;
10831
f326038a 10832 spin_lock(&dev->event_lock);
6ad790c0
CW
10833 work = intel_crtc->unpin_work;
10834 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 10835 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 10836 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 10837 page_flip_completed(intel_crtc);
6ad790c0 10838 work = NULL;
d6bbafa1 10839 }
6ad790c0
CW
10840 if (work != NULL &&
10841 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10842 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 10843 spin_unlock(&dev->event_lock);
d6bbafa1
CW
10844}
10845
6b95a207
KH
10846static int intel_crtc_page_flip(struct drm_crtc *crtc,
10847 struct drm_framebuffer *fb,
ed8d1975
KP
10848 struct drm_pending_vblank_event *event,
10849 uint32_t page_flip_flags)
6b95a207
KH
10850{
10851 struct drm_device *dev = crtc->dev;
10852 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 10853 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 10854 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 10855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 10856 struct drm_plane *primary = crtc->primary;
a071fa00 10857 enum pipe pipe = intel_crtc->pipe;
6b95a207 10858 struct intel_unpin_work *work;
a4872ba6 10859 struct intel_engine_cs *ring;
cf5d8a46 10860 bool mmio_flip;
52e68630 10861 int ret;
6b95a207 10862
2ff8fde1
MR
10863 /*
10864 * drm_mode_page_flip_ioctl() should already catch this, but double
10865 * check to be safe. In the future we may enable pageflipping from
10866 * a disabled primary plane.
10867 */
10868 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10869 return -EBUSY;
10870
e6a595d2 10871 /* Can't change pixel format via MI display flips. */
f4510a27 10872 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
10873 return -EINVAL;
10874
10875 /*
10876 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10877 * Note that pitch changes could also affect these register.
10878 */
10879 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
10880 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10881 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
10882 return -EINVAL;
10883
f900db47
CW
10884 if (i915_terminally_wedged(&dev_priv->gpu_error))
10885 goto out_hang;
10886
b14c5679 10887 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
10888 if (work == NULL)
10889 return -ENOMEM;
10890
6b95a207 10891 work->event = event;
b4a98e57 10892 work->crtc = crtc;
ab8d6675 10893 work->old_fb = old_fb;
6b95a207
KH
10894 INIT_WORK(&work->work, intel_unpin_work_fn);
10895
87b6b101 10896 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
10897 if (ret)
10898 goto free_work;
10899
6b95a207 10900 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 10901 spin_lock_irq(&dev->event_lock);
6b95a207 10902 if (intel_crtc->unpin_work) {
d6bbafa1
CW
10903 /* Before declaring the flip queue wedged, check if
10904 * the hardware completed the operation behind our backs.
10905 */
10906 if (__intel_pageflip_stall_check(dev, crtc)) {
10907 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10908 page_flip_completed(intel_crtc);
10909 } else {
10910 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 10911 spin_unlock_irq(&dev->event_lock);
468f0b44 10912
d6bbafa1
CW
10913 drm_crtc_vblank_put(crtc);
10914 kfree(work);
10915 return -EBUSY;
10916 }
6b95a207
KH
10917 }
10918 intel_crtc->unpin_work = work;
5e2d7afc 10919 spin_unlock_irq(&dev->event_lock);
6b95a207 10920
b4a98e57
CW
10921 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10922 flush_workqueue(dev_priv->wq);
10923
75dfca80 10924 /* Reference the objects for the scheduled work. */
ab8d6675 10925 drm_framebuffer_reference(work->old_fb);
05394f39 10926 drm_gem_object_reference(&obj->base);
6b95a207 10927
f4510a27 10928 crtc->primary->fb = fb;
afd65eb4 10929 update_state_fb(crtc->primary);
1ed1f968 10930
e1f99ce6 10931 work->pending_flip_obj = obj;
e1f99ce6 10932
89ed88ba
CW
10933 ret = i915_mutex_lock_interruptible(dev);
10934 if (ret)
10935 goto cleanup;
10936
b4a98e57 10937 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 10938 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 10939
75f7f3ec 10940 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 10941 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 10942
4fa62c89
VS
10943 if (IS_VALLEYVIEW(dev)) {
10944 ring = &dev_priv->ring[BCS];
ab8d6675 10945 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
10946 /* vlv: DISPLAY_FLIP fails to change tiling */
10947 ring = NULL;
48bf5b2d 10948 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 10949 ring = &dev_priv->ring[BCS];
4fa62c89 10950 } else if (INTEL_INFO(dev)->gen >= 7) {
41c52415 10951 ring = i915_gem_request_get_ring(obj->last_read_req);
4fa62c89
VS
10952 if (ring == NULL || ring->id != RCS)
10953 ring = &dev_priv->ring[BCS];
10954 } else {
10955 ring = &dev_priv->ring[RCS];
10956 }
10957
cf5d8a46
CW
10958 mmio_flip = use_mmio_flip(ring, obj);
10959
10960 /* When using CS flips, we want to emit semaphores between rings.
10961 * However, when using mmio flips we will create a task to do the
10962 * synchronisation, so all we want here is to pin the framebuffer
10963 * into the display plane and skip any waits.
10964 */
82bc3b2d 10965 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
cf5d8a46
CW
10966 crtc->primary->state,
10967 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
8c9f3aaf
JB
10968 if (ret)
10969 goto cleanup_pending;
6b95a207 10970
121920fa
TU
10971 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10972 + intel_crtc->dspaddr_offset;
4fa62c89 10973
cf5d8a46 10974 if (mmio_flip) {
84c33a64
SG
10975 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10976 page_flip_flags);
d6bbafa1
CW
10977 if (ret)
10978 goto cleanup_unpin;
10979
f06cc1b9
JH
10980 i915_gem_request_assign(&work->flip_queued_req,
10981 obj->last_write_req);
d6bbafa1 10982 } else {
84c33a64 10983 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
10984 page_flip_flags);
10985 if (ret)
10986 goto cleanup_unpin;
10987
f06cc1b9
JH
10988 i915_gem_request_assign(&work->flip_queued_req,
10989 intel_ring_get_request(ring));
d6bbafa1
CW
10990 }
10991
1e3feefd 10992 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 10993 work->enable_stall_check = true;
4fa62c89 10994
ab8d6675 10995 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a071fa00
DV
10996 INTEL_FRONTBUFFER_PRIMARY(pipe));
10997
7ff0ebcc 10998 intel_fbc_disable(dev);
f99d7069 10999 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
11000 mutex_unlock(&dev->struct_mutex);
11001
e5510fac
JB
11002 trace_i915_flip_request(intel_crtc->plane, obj);
11003
6b95a207 11004 return 0;
96b099fd 11005
4fa62c89 11006cleanup_unpin:
82bc3b2d 11007 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 11008cleanup_pending:
b4a98e57 11009 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11010 mutex_unlock(&dev->struct_mutex);
11011cleanup:
f4510a27 11012 crtc->primary->fb = old_fb;
afd65eb4 11013 update_state_fb(crtc->primary);
89ed88ba
CW
11014
11015 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11016 drm_framebuffer_unreference(work->old_fb);
96b099fd 11017
5e2d7afc 11018 spin_lock_irq(&dev->event_lock);
96b099fd 11019 intel_crtc->unpin_work = NULL;
5e2d7afc 11020 spin_unlock_irq(&dev->event_lock);
96b099fd 11021
87b6b101 11022 drm_crtc_vblank_put(crtc);
7317c75e 11023free_work:
96b099fd
CW
11024 kfree(work);
11025
f900db47
CW
11026 if (ret == -EIO) {
11027out_hang:
53a366b9 11028 ret = intel_plane_restore(primary);
f0d3dad3 11029 if (ret == 0 && event) {
5e2d7afc 11030 spin_lock_irq(&dev->event_lock);
a071fa00 11031 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11032 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11033 }
f900db47 11034 }
96b099fd 11035 return ret;
6b95a207
KH
11036}
11037
65b38e0d 11038static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11039 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11040 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11041 .atomic_begin = intel_begin_crtc_commit,
11042 .atomic_flush = intel_finish_crtc_commit,
f6e5b160
CW
11043};
11044
9a935856
DV
11045/**
11046 * intel_modeset_update_staged_output_state
11047 *
11048 * Updates the staged output configuration state, e.g. after we've read out the
11049 * current hw state.
11050 */
11051static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 11052{
7668851f 11053 struct intel_crtc *crtc;
9a935856
DV
11054 struct intel_encoder *encoder;
11055 struct intel_connector *connector;
f6e5b160 11056
3a3371ff 11057 for_each_intel_connector(dev, connector) {
9a935856
DV
11058 connector->new_encoder =
11059 to_intel_encoder(connector->base.encoder);
11060 }
f6e5b160 11061
b2784e15 11062 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11063 encoder->new_crtc =
11064 to_intel_crtc(encoder->base.crtc);
11065 }
7668851f 11066
d3fcc808 11067 for_each_intel_crtc(dev, crtc) {
83d65738 11068 crtc->new_enabled = crtc->base.state->enable;
7668851f 11069 }
f6e5b160
CW
11070}
11071
d29b2f9d
ACO
11072/* Transitional helper to copy current connector/encoder state to
11073 * connector->state. This is needed so that code that is partially
11074 * converted to atomic does the right thing.
11075 */
11076static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11077{
11078 struct intel_connector *connector;
11079
11080 for_each_intel_connector(dev, connector) {
11081 if (connector->base.encoder) {
11082 connector->base.state->best_encoder =
11083 connector->base.encoder;
11084 connector->base.state->crtc =
11085 connector->base.encoder->crtc;
11086 } else {
11087 connector->base.state->best_encoder = NULL;
11088 connector->base.state->crtc = NULL;
11089 }
11090 }
11091}
11092
9a935856
DV
11093/**
11094 * intel_modeset_commit_output_state
11095 *
11096 * This function copies the stage display pipe configuration to the real one.
11097 */
11098static void intel_modeset_commit_output_state(struct drm_device *dev)
11099{
7668851f 11100 struct intel_crtc *crtc;
9a935856
DV
11101 struct intel_encoder *encoder;
11102 struct intel_connector *connector;
f6e5b160 11103
3a3371ff 11104 for_each_intel_connector(dev, connector) {
9a935856
DV
11105 connector->base.encoder = &connector->new_encoder->base;
11106 }
f6e5b160 11107
b2784e15 11108 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11109 encoder->base.crtc = &encoder->new_crtc->base;
11110 }
7668851f 11111
d3fcc808 11112 for_each_intel_crtc(dev, crtc) {
83d65738 11113 crtc->base.state->enable = crtc->new_enabled;
7668851f
VS
11114 crtc->base.enabled = crtc->new_enabled;
11115 }
d29b2f9d
ACO
11116
11117 intel_modeset_update_connector_atomic_state(dev);
9a935856
DV
11118}
11119
050f7aeb 11120static void
eba905b2 11121connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11122 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11123{
11124 int bpp = pipe_config->pipe_bpp;
11125
11126 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11127 connector->base.base.id,
c23cc417 11128 connector->base.name);
050f7aeb
DV
11129
11130 /* Don't use an invalid EDID bpc value */
11131 if (connector->base.display_info.bpc &&
11132 connector->base.display_info.bpc * 3 < bpp) {
11133 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11134 bpp, connector->base.display_info.bpc*3);
11135 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11136 }
11137
11138 /* Clamp bpp to 8 on screens without EDID 1.4 */
11139 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11140 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11141 bpp);
11142 pipe_config->pipe_bpp = 24;
11143 }
11144}
11145
4e53c2e0 11146static int
050f7aeb 11147compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11148 struct intel_crtc_state *pipe_config)
4e53c2e0 11149{
050f7aeb 11150 struct drm_device *dev = crtc->base.dev;
1486017f 11151 struct drm_atomic_state *state;
050f7aeb 11152 struct intel_connector *connector;
1486017f 11153 int bpp, i;
4e53c2e0 11154
d328c9d7 11155 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
4e53c2e0 11156 bpp = 10*3;
d328c9d7
DV
11157 else if (INTEL_INFO(dev)->gen >= 5)
11158 bpp = 12*3;
11159 else
11160 bpp = 8*3;
11161
4e53c2e0 11162
4e53c2e0
DV
11163 pipe_config->pipe_bpp = bpp;
11164
1486017f
ACO
11165 state = pipe_config->base.state;
11166
4e53c2e0 11167 /* Clamp display bpp to EDID value */
1486017f
ACO
11168 for (i = 0; i < state->num_connector; i++) {
11169 if (!state->connectors[i])
11170 continue;
11171
11172 connector = to_intel_connector(state->connectors[i]);
11173 if (state->connector_states[i]->crtc != &crtc->base)
4e53c2e0
DV
11174 continue;
11175
050f7aeb 11176 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
11177 }
11178
11179 return bpp;
11180}
11181
644db711
DV
11182static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11183{
11184 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11185 "type: 0x%x flags: 0x%x\n",
1342830c 11186 mode->crtc_clock,
644db711
DV
11187 mode->crtc_hdisplay, mode->crtc_hsync_start,
11188 mode->crtc_hsync_end, mode->crtc_htotal,
11189 mode->crtc_vdisplay, mode->crtc_vsync_start,
11190 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11191}
11192
c0b03411 11193static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11194 struct intel_crtc_state *pipe_config,
c0b03411
DV
11195 const char *context)
11196{
6a60cd87
CK
11197 struct drm_device *dev = crtc->base.dev;
11198 struct drm_plane *plane;
11199 struct intel_plane *intel_plane;
11200 struct intel_plane_state *state;
11201 struct drm_framebuffer *fb;
11202
11203 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11204 context, pipe_config, pipe_name(crtc->pipe));
c0b03411
DV
11205
11206 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11207 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11208 pipe_config->pipe_bpp, pipe_config->dither);
11209 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11210 pipe_config->has_pch_encoder,
11211 pipe_config->fdi_lanes,
11212 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11213 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11214 pipe_config->fdi_m_n.tu);
eb14cb74
VS
11215 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11216 pipe_config->has_dp_encoder,
11217 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11218 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11219 pipe_config->dp_m_n.tu);
b95af8be
VK
11220
11221 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11222 pipe_config->has_dp_encoder,
11223 pipe_config->dp_m2_n2.gmch_m,
11224 pipe_config->dp_m2_n2.gmch_n,
11225 pipe_config->dp_m2_n2.link_m,
11226 pipe_config->dp_m2_n2.link_n,
11227 pipe_config->dp_m2_n2.tu);
11228
55072d19
DV
11229 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11230 pipe_config->has_audio,
11231 pipe_config->has_infoframe);
11232
c0b03411 11233 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11234 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11235 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11236 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11237 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 11238 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
11239 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11240 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
6a60cd87
CK
11241 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
11242 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
11243 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
c0b03411
DV
11244 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11245 pipe_config->gmch_pfit.control,
11246 pipe_config->gmch_pfit.pgm_ratios,
11247 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 11248 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 11249 pipe_config->pch_pfit.pos,
fd4daa9c
CW
11250 pipe_config->pch_pfit.size,
11251 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 11252 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 11253 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87
CK
11254
11255 DRM_DEBUG_KMS("planes on this crtc\n");
11256 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11257 intel_plane = to_intel_plane(plane);
11258 if (intel_plane->pipe != crtc->pipe)
11259 continue;
11260
11261 state = to_intel_plane_state(plane->state);
11262 fb = state->base.fb;
11263 if (!fb) {
11264 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11265 "disabled, scaler_id = %d\n",
11266 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11267 plane->base.id, intel_plane->pipe,
11268 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11269 drm_plane_index(plane), state->scaler_id);
11270 continue;
11271 }
11272
11273 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11274 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11275 plane->base.id, intel_plane->pipe,
11276 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11277 drm_plane_index(plane));
11278 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11279 fb->base.id, fb->width, fb->height, fb->pixel_format);
11280 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11281 state->scaler_id,
11282 state->src.x1 >> 16, state->src.y1 >> 16,
11283 drm_rect_width(&state->src) >> 16,
11284 drm_rect_height(&state->src) >> 16,
11285 state->dst.x1, state->dst.y1,
11286 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11287 }
c0b03411
DV
11288}
11289
bc079e8b
VS
11290static bool encoders_cloneable(const struct intel_encoder *a,
11291 const struct intel_encoder *b)
accfc0c5 11292{
bc079e8b
VS
11293 /* masks could be asymmetric, so check both ways */
11294 return a == b || (a->cloneable & (1 << b->type) &&
11295 b->cloneable & (1 << a->type));
11296}
11297
98a221da
ACO
11298static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11299 struct intel_crtc *crtc,
bc079e8b
VS
11300 struct intel_encoder *encoder)
11301{
bc079e8b 11302 struct intel_encoder *source_encoder;
98a221da
ACO
11303 struct drm_connector_state *connector_state;
11304 int i;
bc079e8b 11305
98a221da
ACO
11306 for (i = 0; i < state->num_connector; i++) {
11307 if (!state->connectors[i])
11308 continue;
bc079e8b 11309
98a221da
ACO
11310 connector_state = state->connector_states[i];
11311 if (connector_state->crtc != &crtc->base)
bc079e8b
VS
11312 continue;
11313
98a221da
ACO
11314 source_encoder =
11315 to_intel_encoder(connector_state->best_encoder);
bc079e8b
VS
11316 if (!encoders_cloneable(encoder, source_encoder))
11317 return false;
11318 }
11319
11320 return true;
11321}
11322
98a221da
ACO
11323static bool check_encoder_cloning(struct drm_atomic_state *state,
11324 struct intel_crtc *crtc)
bc079e8b 11325{
accfc0c5 11326 struct intel_encoder *encoder;
98a221da
ACO
11327 struct drm_connector_state *connector_state;
11328 int i;
accfc0c5 11329
98a221da
ACO
11330 for (i = 0; i < state->num_connector; i++) {
11331 if (!state->connectors[i])
accfc0c5
DV
11332 continue;
11333
98a221da
ACO
11334 connector_state = state->connector_states[i];
11335 if (connector_state->crtc != &crtc->base)
11336 continue;
11337
11338 encoder = to_intel_encoder(connector_state->best_encoder);
11339 if (!check_single_encoder_cloning(state, crtc, encoder))
bc079e8b 11340 return false;
accfc0c5
DV
11341 }
11342
bc079e8b 11343 return true;
accfc0c5
DV
11344}
11345
5448a00d 11346static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11347{
5448a00d
ACO
11348 struct drm_device *dev = state->dev;
11349 struct intel_encoder *encoder;
11350 struct drm_connector_state *connector_state;
00f0b378 11351 unsigned int used_ports = 0;
5448a00d 11352 int i;
00f0b378
VS
11353
11354 /*
11355 * Walk the connector list instead of the encoder
11356 * list to detect the problem on ddi platforms
11357 * where there's just one encoder per digital port.
11358 */
5448a00d
ACO
11359 for (i = 0; i < state->num_connector; i++) {
11360 if (!state->connectors[i])
11361 continue;
00f0b378 11362
5448a00d
ACO
11363 connector_state = state->connector_states[i];
11364 if (!connector_state->best_encoder)
00f0b378
VS
11365 continue;
11366
5448a00d
ACO
11367 encoder = to_intel_encoder(connector_state->best_encoder);
11368
11369 WARN_ON(!connector_state->crtc);
00f0b378
VS
11370
11371 switch (encoder->type) {
11372 unsigned int port_mask;
11373 case INTEL_OUTPUT_UNKNOWN:
11374 if (WARN_ON(!HAS_DDI(dev)))
11375 break;
11376 case INTEL_OUTPUT_DISPLAYPORT:
11377 case INTEL_OUTPUT_HDMI:
11378 case INTEL_OUTPUT_EDP:
11379 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11380
11381 /* the same port mustn't appear more than once */
11382 if (used_ports & port_mask)
11383 return false;
11384
11385 used_ports |= port_mask;
11386 default:
11387 break;
11388 }
11389 }
11390
11391 return true;
11392}
11393
83a57153
ACO
11394static void
11395clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11396{
11397 struct drm_crtc_state tmp_state;
663a3640 11398 struct intel_crtc_scaler_state scaler_state;
83a57153 11399
663a3640 11400 /* Clear only the intel specific part of the crtc state excluding scalers */
83a57153 11401 tmp_state = crtc_state->base;
663a3640 11402 scaler_state = crtc_state->scaler_state;
83a57153
ACO
11403 memset(crtc_state, 0, sizeof *crtc_state);
11404 crtc_state->base = tmp_state;
663a3640 11405 crtc_state->scaler_state = scaler_state;
83a57153
ACO
11406}
11407
5cec258b 11408static struct intel_crtc_state *
b8cecdf5 11409intel_modeset_pipe_config(struct drm_crtc *crtc,
83a57153
ACO
11410 struct drm_display_mode *mode,
11411 struct drm_atomic_state *state)
ee7b9f93 11412{
7758a113 11413 struct intel_encoder *encoder;
0b901879
ACO
11414 struct intel_connector *connector;
11415 struct drm_connector_state *connector_state;
5cec258b 11416 struct intel_crtc_state *pipe_config;
d328c9d7 11417 int base_bpp, ret = -EINVAL;
0b901879 11418 int i;
e29c22c0 11419 bool retry = true;
ee7b9f93 11420
98a221da 11421 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
accfc0c5
DV
11422 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11423 return ERR_PTR(-EINVAL);
11424 }
11425
5448a00d 11426 if (!check_digital_port_conflicts(state)) {
00f0b378
VS
11427 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
11428 return ERR_PTR(-EINVAL);
11429 }
11430
83a57153
ACO
11431 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
11432 if (IS_ERR(pipe_config))
11433 return pipe_config;
11434
11435 clear_intel_crtc_state(pipe_config);
7758a113 11436
07878248 11437 pipe_config->base.crtc = crtc;
2d112de7
ACO
11438 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
11439 drm_mode_copy(&pipe_config->base.mode, mode);
37327abd 11440
e143a21c
DV
11441 pipe_config->cpu_transcoder =
11442 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 11443 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 11444
2960bc9c
ID
11445 /*
11446 * Sanitize sync polarity flags based on requested ones. If neither
11447 * positive or negative polarity is requested, treat this as meaning
11448 * negative polarity.
11449 */
2d112de7 11450 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11451 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 11452 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 11453
2d112de7 11454 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11455 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 11456 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 11457
050f7aeb
DV
11458 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11459 * plane pixel format and any sink constraints into account. Returns the
11460 * source plane bpp so that dithering can be selected on mismatches
11461 * after encoders and crtc also have had their say. */
d328c9d7
DV
11462 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11463 pipe_config);
11464 if (base_bpp < 0)
4e53c2e0
DV
11465 goto fail;
11466
e41a56be
VS
11467 /*
11468 * Determine the real pipe dimensions. Note that stereo modes can
11469 * increase the actual pipe size due to the frame doubling and
11470 * insertion of additional space for blanks between the frame. This
11471 * is stored in the crtc timings. We use the requested mode to do this
11472 * computation to clearly distinguish it from the adjusted mode, which
11473 * can be changed by the connectors in the below retry loop.
11474 */
2d112de7 11475 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
11476 &pipe_config->pipe_src_w,
11477 &pipe_config->pipe_src_h);
e41a56be 11478
e29c22c0 11479encoder_retry:
ef1b460d 11480 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 11481 pipe_config->port_clock = 0;
ef1b460d 11482 pipe_config->pixel_multiplier = 1;
ff9a6750 11483
135c81b8 11484 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
11485 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11486 CRTC_STEREO_DOUBLE);
135c81b8 11487
7758a113
DV
11488 /* Pass our mode to the connectors and the CRTC to give them a chance to
11489 * adjust it according to limitations or connector properties, and also
11490 * a chance to reject the mode entirely.
47f1c6c9 11491 */
0b901879
ACO
11492 for (i = 0; i < state->num_connector; i++) {
11493 connector = to_intel_connector(state->connectors[i]);
11494 if (!connector)
11495 continue;
47f1c6c9 11496
0b901879
ACO
11497 connector_state = state->connector_states[i];
11498 if (connector_state->crtc != crtc)
7758a113 11499 continue;
7ae89233 11500
0b901879
ACO
11501 encoder = to_intel_encoder(connector_state->best_encoder);
11502
efea6e8e
DV
11503 if (!(encoder->compute_config(encoder, pipe_config))) {
11504 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
11505 goto fail;
11506 }
ee7b9f93 11507 }
47f1c6c9 11508
ff9a6750
DV
11509 /* Set default port clock if not overwritten by the encoder. Needs to be
11510 * done afterwards in case the encoder adjusts the mode. */
11511 if (!pipe_config->port_clock)
2d112de7 11512 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 11513 * pipe_config->pixel_multiplier;
ff9a6750 11514
a43f6e0f 11515 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 11516 if (ret < 0) {
7758a113
DV
11517 DRM_DEBUG_KMS("CRTC fixup failed\n");
11518 goto fail;
ee7b9f93 11519 }
e29c22c0
DV
11520
11521 if (ret == RETRY) {
11522 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11523 ret = -EINVAL;
11524 goto fail;
11525 }
11526
11527 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11528 retry = false;
11529 goto encoder_retry;
11530 }
11531
d328c9d7 11532 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
4e53c2e0 11533 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 11534 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 11535
b8cecdf5 11536 return pipe_config;
7758a113 11537fail:
e29c22c0 11538 return ERR_PTR(ret);
ee7b9f93 11539}
47f1c6c9 11540
e2e1ed41
DV
11541/* Computes which crtcs are affected and sets the relevant bits in the mask. For
11542 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
11543static void
11544intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
11545 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
11546{
11547 struct intel_crtc *intel_crtc;
e2e1ed41
DV
11548 struct drm_device *dev = crtc->dev;
11549 struct intel_encoder *encoder;
11550 struct intel_connector *connector;
11551 struct drm_crtc *tmp_crtc;
79e53945 11552
e2e1ed41 11553 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 11554
e2e1ed41
DV
11555 /* Check which crtcs have changed outputs connected to them, these need
11556 * to be part of the prepare_pipes mask. We don't (yet) support global
11557 * modeset across multiple crtcs, so modeset_pipes will only have one
11558 * bit set at most. */
3a3371ff 11559 for_each_intel_connector(dev, connector) {
e2e1ed41
DV
11560 if (connector->base.encoder == &connector->new_encoder->base)
11561 continue;
79e53945 11562
e2e1ed41
DV
11563 if (connector->base.encoder) {
11564 tmp_crtc = connector->base.encoder->crtc;
11565
11566 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11567 }
11568
11569 if (connector->new_encoder)
11570 *prepare_pipes |=
11571 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
11572 }
11573
b2784e15 11574 for_each_intel_encoder(dev, encoder) {
e2e1ed41
DV
11575 if (encoder->base.crtc == &encoder->new_crtc->base)
11576 continue;
11577
11578 if (encoder->base.crtc) {
11579 tmp_crtc = encoder->base.crtc;
11580
11581 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11582 }
11583
11584 if (encoder->new_crtc)
11585 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
11586 }
11587
7668851f 11588 /* Check for pipes that will be enabled/disabled ... */
d3fcc808 11589 for_each_intel_crtc(dev, intel_crtc) {
83d65738 11590 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
e2e1ed41 11591 continue;
7e7d76c3 11592
7668851f 11593 if (!intel_crtc->new_enabled)
e2e1ed41 11594 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
11595 else
11596 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
11597 }
11598
e2e1ed41
DV
11599
11600 /* set_mode is also used to update properties on life display pipes. */
11601 intel_crtc = to_intel_crtc(crtc);
7668851f 11602 if (intel_crtc->new_enabled)
e2e1ed41
DV
11603 *prepare_pipes |= 1 << intel_crtc->pipe;
11604
b6c5164d
DV
11605 /*
11606 * For simplicity do a full modeset on any pipe where the output routing
11607 * changed. We could be more clever, but that would require us to be
11608 * more careful with calling the relevant encoder->mode_set functions.
11609 */
e2e1ed41
DV
11610 if (*prepare_pipes)
11611 *modeset_pipes = *prepare_pipes;
11612
11613 /* ... and mask these out. */
11614 *modeset_pipes &= ~(*disable_pipes);
11615 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
11616
11617 /*
11618 * HACK: We don't (yet) fully support global modesets. intel_set_config
11619 * obies this rule, but the modeset restore mode of
11620 * intel_modeset_setup_hw_state does not.
11621 */
11622 *modeset_pipes &= 1 << intel_crtc->pipe;
11623 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
11624
11625 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
11626 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 11627}
79e53945 11628
ea9d758d 11629static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 11630{
ea9d758d 11631 struct drm_encoder *encoder;
f6e5b160 11632 struct drm_device *dev = crtc->dev;
f6e5b160 11633
ea9d758d
DV
11634 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11635 if (encoder->crtc == crtc)
11636 return true;
11637
11638 return false;
11639}
11640
11641static void
11642intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
11643{
ba41c0de 11644 struct drm_i915_private *dev_priv = dev->dev_private;
ea9d758d
DV
11645 struct intel_encoder *intel_encoder;
11646 struct intel_crtc *intel_crtc;
11647 struct drm_connector *connector;
11648
ba41c0de
DV
11649 intel_shared_dpll_commit(dev_priv);
11650
b2784e15 11651 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
11652 if (!intel_encoder->base.crtc)
11653 continue;
11654
11655 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
11656
11657 if (prepare_pipes & (1 << intel_crtc->pipe))
11658 intel_encoder->connectors_active = false;
11659 }
11660
11661 intel_modeset_commit_output_state(dev);
11662
7668851f 11663 /* Double check state. */
d3fcc808 11664 for_each_intel_crtc(dev, intel_crtc) {
83d65738 11665 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
ea9d758d
DV
11666 }
11667
11668 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11669 if (!connector->encoder || !connector->encoder->crtc)
11670 continue;
11671
11672 intel_crtc = to_intel_crtc(connector->encoder->crtc);
11673
11674 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
11675 struct drm_property *dpms_property =
11676 dev->mode_config.dpms_property;
11677
ea9d758d 11678 connector->dpms = DRM_MODE_DPMS_ON;
662595df 11679 drm_object_property_set_value(&connector->base,
68d34720
DV
11680 dpms_property,
11681 DRM_MODE_DPMS_ON);
ea9d758d
DV
11682
11683 intel_encoder = to_intel_encoder(connector->encoder);
11684 intel_encoder->connectors_active = true;
11685 }
11686 }
11687
11688}
11689
3bd26263 11690static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 11691{
3bd26263 11692 int diff;
f1f644dc
JB
11693
11694 if (clock1 == clock2)
11695 return true;
11696
11697 if (!clock1 || !clock2)
11698 return false;
11699
11700 diff = abs(clock1 - clock2);
11701
11702 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11703 return true;
11704
11705 return false;
11706}
11707
25c5b266
DV
11708#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11709 list_for_each_entry((intel_crtc), \
11710 &(dev)->mode_config.crtc_list, \
11711 base.head) \
0973f18f 11712 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 11713
0e8ffe1b 11714static bool
2fa2fe9a 11715intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
11716 struct intel_crtc_state *current_config,
11717 struct intel_crtc_state *pipe_config)
0e8ffe1b 11718{
66e985c0
DV
11719#define PIPE_CONF_CHECK_X(name) \
11720 if (current_config->name != pipe_config->name) { \
11721 DRM_ERROR("mismatch in " #name " " \
11722 "(expected 0x%08x, found 0x%08x)\n", \
11723 current_config->name, \
11724 pipe_config->name); \
11725 return false; \
11726 }
11727
08a24034
DV
11728#define PIPE_CONF_CHECK_I(name) \
11729 if (current_config->name != pipe_config->name) { \
11730 DRM_ERROR("mismatch in " #name " " \
11731 "(expected %i, found %i)\n", \
11732 current_config->name, \
11733 pipe_config->name); \
11734 return false; \
88adfff1
DV
11735 }
11736
b95af8be
VK
11737/* This is required for BDW+ where there is only one set of registers for
11738 * switching between high and low RR.
11739 * This macro can be used whenever a comparison has to be made between one
11740 * hw state and multiple sw state variables.
11741 */
11742#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11743 if ((current_config->name != pipe_config->name) && \
11744 (current_config->alt_name != pipe_config->name)) { \
11745 DRM_ERROR("mismatch in " #name " " \
11746 "(expected %i or %i, found %i)\n", \
11747 current_config->name, \
11748 current_config->alt_name, \
11749 pipe_config->name); \
11750 return false; \
11751 }
11752
1bd1bd80
DV
11753#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11754 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 11755 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
11756 "(expected %i, found %i)\n", \
11757 current_config->name & (mask), \
11758 pipe_config->name & (mask)); \
11759 return false; \
11760 }
11761
5e550656
VS
11762#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11763 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11764 DRM_ERROR("mismatch in " #name " " \
11765 "(expected %i, found %i)\n", \
11766 current_config->name, \
11767 pipe_config->name); \
11768 return false; \
11769 }
11770
bb760063
DV
11771#define PIPE_CONF_QUIRK(quirk) \
11772 ((current_config->quirks | pipe_config->quirks) & (quirk))
11773
eccb140b
DV
11774 PIPE_CONF_CHECK_I(cpu_transcoder);
11775
08a24034
DV
11776 PIPE_CONF_CHECK_I(has_pch_encoder);
11777 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
11778 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11779 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11780 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11781 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11782 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 11783
eb14cb74 11784 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
11785
11786 if (INTEL_INFO(dev)->gen < 8) {
11787 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11788 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11789 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11790 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11791 PIPE_CONF_CHECK_I(dp_m_n.tu);
11792
11793 if (current_config->has_drrs) {
11794 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11795 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11796 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11797 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11798 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11799 }
11800 } else {
11801 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11802 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11803 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11804 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11805 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11806 }
eb14cb74 11807
2d112de7
ACO
11808 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11809 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11810 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11811 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11812 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11813 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11814
2d112de7
ACO
11815 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11816 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11817 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11818 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11819 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11820 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11821
c93f54cf 11822 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 11823 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
11824 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11825 IS_VALLEYVIEW(dev))
11826 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 11827 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 11828
9ed109a7
DV
11829 PIPE_CONF_CHECK_I(has_audio);
11830
2d112de7 11831 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
11832 DRM_MODE_FLAG_INTERLACE);
11833
bb760063 11834 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11835 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11836 DRM_MODE_FLAG_PHSYNC);
2d112de7 11837 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11838 DRM_MODE_FLAG_NHSYNC);
2d112de7 11839 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11840 DRM_MODE_FLAG_PVSYNC);
2d112de7 11841 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
11842 DRM_MODE_FLAG_NVSYNC);
11843 }
045ac3b5 11844
37327abd
VS
11845 PIPE_CONF_CHECK_I(pipe_src_w);
11846 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 11847
9953599b
DV
11848 /*
11849 * FIXME: BIOS likes to set up a cloned config with lvds+external
11850 * screen. Since we don't yet re-compute the pipe config when moving
11851 * just the lvds port away to another pipe the sw tracking won't match.
11852 *
11853 * Proper atomic modesets with recomputed global state will fix this.
11854 * Until then just don't check gmch state for inherited modes.
11855 */
11856 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11857 PIPE_CONF_CHECK_I(gmch_pfit.control);
11858 /* pfit ratios are autocomputed by the hw on gen4+ */
11859 if (INTEL_INFO(dev)->gen < 4)
11860 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11861 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11862 }
11863
fd4daa9c
CW
11864 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11865 if (current_config->pch_pfit.enabled) {
11866 PIPE_CONF_CHECK_I(pch_pfit.pos);
11867 PIPE_CONF_CHECK_I(pch_pfit.size);
11868 }
2fa2fe9a 11869
a1b2278e
CK
11870 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11871
e59150dc
JB
11872 /* BDW+ don't expose a synchronous way to read the state */
11873 if (IS_HASWELL(dev))
11874 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 11875
282740f7
VS
11876 PIPE_CONF_CHECK_I(double_wide);
11877
26804afd
DV
11878 PIPE_CONF_CHECK_X(ddi_pll_sel);
11879
c0d43d62 11880 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 11881 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11882 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
11883 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11884 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11885 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
11886 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11887 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11888 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 11889
42571aef
VS
11890 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11891 PIPE_CONF_CHECK_I(pipe_bpp);
11892
2d112de7 11893 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 11894 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 11895
66e985c0 11896#undef PIPE_CONF_CHECK_X
08a24034 11897#undef PIPE_CONF_CHECK_I
b95af8be 11898#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 11899#undef PIPE_CONF_CHECK_FLAGS
5e550656 11900#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 11901#undef PIPE_CONF_QUIRK
88adfff1 11902
0e8ffe1b
DV
11903 return true;
11904}
11905
08db6652
DL
11906static void check_wm_state(struct drm_device *dev)
11907{
11908 struct drm_i915_private *dev_priv = dev->dev_private;
11909 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11910 struct intel_crtc *intel_crtc;
11911 int plane;
11912
11913 if (INTEL_INFO(dev)->gen < 9)
11914 return;
11915
11916 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11917 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11918
11919 for_each_intel_crtc(dev, intel_crtc) {
11920 struct skl_ddb_entry *hw_entry, *sw_entry;
11921 const enum pipe pipe = intel_crtc->pipe;
11922
11923 if (!intel_crtc->active)
11924 continue;
11925
11926 /* planes */
dd740780 11927 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
11928 hw_entry = &hw_ddb.plane[pipe][plane];
11929 sw_entry = &sw_ddb->plane[pipe][plane];
11930
11931 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11932 continue;
11933
11934 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11935 "(expected (%u,%u), found (%u,%u))\n",
11936 pipe_name(pipe), plane + 1,
11937 sw_entry->start, sw_entry->end,
11938 hw_entry->start, hw_entry->end);
11939 }
11940
11941 /* cursor */
11942 hw_entry = &hw_ddb.cursor[pipe];
11943 sw_entry = &sw_ddb->cursor[pipe];
11944
11945 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11946 continue;
11947
11948 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11949 "(expected (%u,%u), found (%u,%u))\n",
11950 pipe_name(pipe),
11951 sw_entry->start, sw_entry->end,
11952 hw_entry->start, hw_entry->end);
11953 }
11954}
11955
91d1b4bd
DV
11956static void
11957check_connector_state(struct drm_device *dev)
8af6cf88 11958{
8af6cf88
DV
11959 struct intel_connector *connector;
11960
3a3371ff 11961 for_each_intel_connector(dev, connector) {
8af6cf88
DV
11962 /* This also checks the encoder/connector hw state with the
11963 * ->get_hw_state callbacks. */
11964 intel_connector_check_state(connector);
11965
e2c719b7 11966 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
8af6cf88
DV
11967 "connector's staged encoder doesn't match current encoder\n");
11968 }
91d1b4bd
DV
11969}
11970
11971static void
11972check_encoder_state(struct drm_device *dev)
11973{
11974 struct intel_encoder *encoder;
11975 struct intel_connector *connector;
8af6cf88 11976
b2784e15 11977 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
11978 bool enabled = false;
11979 bool active = false;
11980 enum pipe pipe, tracked_pipe;
11981
11982 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11983 encoder->base.base.id,
8e329a03 11984 encoder->base.name);
8af6cf88 11985
e2c719b7 11986 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
8af6cf88 11987 "encoder's stage crtc doesn't match current crtc\n");
e2c719b7 11988 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
11989 "encoder's active_connectors set, but no crtc\n");
11990
3a3371ff 11991 for_each_intel_connector(dev, connector) {
8af6cf88
DV
11992 if (connector->base.encoder != &encoder->base)
11993 continue;
11994 enabled = true;
11995 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11996 active = true;
11997 }
0e32b39c
DA
11998 /*
11999 * for MST connectors if we unplug the connector is gone
12000 * away but the encoder is still connected to a crtc
12001 * until a modeset happens in response to the hotplug.
12002 */
12003 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12004 continue;
12005
e2c719b7 12006 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12007 "encoder's enabled state mismatch "
12008 "(expected %i, found %i)\n",
12009 !!encoder->base.crtc, enabled);
e2c719b7 12010 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
12011 "active encoder with no crtc\n");
12012
e2c719b7 12013 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
12014 "encoder's computed active state doesn't match tracked active state "
12015 "(expected %i, found %i)\n", active, encoder->connectors_active);
12016
12017 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 12018 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
12019 "encoder's hw state doesn't match sw tracking "
12020 "(expected %i, found %i)\n",
12021 encoder->connectors_active, active);
12022
12023 if (!encoder->base.crtc)
12024 continue;
12025
12026 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 12027 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
12028 "active encoder's pipe doesn't match"
12029 "(expected %i, found %i)\n",
12030 tracked_pipe, pipe);
12031
12032 }
91d1b4bd
DV
12033}
12034
12035static void
12036check_crtc_state(struct drm_device *dev)
12037{
fbee40df 12038 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12039 struct intel_crtc *crtc;
12040 struct intel_encoder *encoder;
5cec258b 12041 struct intel_crtc_state pipe_config;
8af6cf88 12042
d3fcc808 12043 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
12044 bool enabled = false;
12045 bool active = false;
12046
045ac3b5
JB
12047 memset(&pipe_config, 0, sizeof(pipe_config));
12048
8af6cf88
DV
12049 DRM_DEBUG_KMS("[CRTC:%d]\n",
12050 crtc->base.base.id);
12051
83d65738 12052 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
8af6cf88
DV
12053 "active crtc, but not enabled in sw tracking\n");
12054
b2784e15 12055 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12056 if (encoder->base.crtc != &crtc->base)
12057 continue;
12058 enabled = true;
12059 if (encoder->connectors_active)
12060 active = true;
12061 }
6c49f241 12062
e2c719b7 12063 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
12064 "crtc's computed active state doesn't match tracked active state "
12065 "(expected %i, found %i)\n", active, crtc->active);
83d65738 12066 I915_STATE_WARN(enabled != crtc->base.state->enable,
8af6cf88 12067 "crtc's computed enabled state doesn't match tracked enabled state "
83d65738
MR
12068 "(expected %i, found %i)\n", enabled,
12069 crtc->base.state->enable);
8af6cf88 12070
0e8ffe1b
DV
12071 active = dev_priv->display.get_pipe_config(crtc,
12072 &pipe_config);
d62cf62a 12073
b6b5d049
VS
12074 /* hw state is inconsistent with the pipe quirk */
12075 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12076 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
12077 active = crtc->active;
12078
b2784e15 12079 for_each_intel_encoder(dev, encoder) {
3eaba51c 12080 enum pipe pipe;
6c49f241
DV
12081 if (encoder->base.crtc != &crtc->base)
12082 continue;
1d37b689 12083 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
12084 encoder->get_config(encoder, &pipe_config);
12085 }
12086
e2c719b7 12087 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
12088 "crtc active state doesn't match with hw state "
12089 "(expected %i, found %i)\n", crtc->active, active);
12090
c0b03411 12091 if (active &&
6e3c9717 12092 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 12093 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
12094 intel_dump_pipe_config(crtc, &pipe_config,
12095 "[hw state]");
6e3c9717 12096 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
12097 "[sw state]");
12098 }
8af6cf88
DV
12099 }
12100}
12101
91d1b4bd
DV
12102static void
12103check_shared_dpll_state(struct drm_device *dev)
12104{
fbee40df 12105 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12106 struct intel_crtc *crtc;
12107 struct intel_dpll_hw_state dpll_hw_state;
12108 int i;
5358901f
DV
12109
12110 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12111 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12112 int enabled_crtcs = 0, active_crtcs = 0;
12113 bool active;
12114
12115 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12116
12117 DRM_DEBUG_KMS("%s\n", pll->name);
12118
12119 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12120
e2c719b7 12121 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12122 "more active pll users than references: %i vs %i\n",
3e369b76 12123 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12124 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12125 "pll in active use but not on in sw tracking\n");
e2c719b7 12126 I915_STATE_WARN(pll->on && !pll->active,
35c95375 12127 "pll in on but not on in use in sw tracking\n");
e2c719b7 12128 I915_STATE_WARN(pll->on != active,
5358901f
DV
12129 "pll on state mismatch (expected %i, found %i)\n",
12130 pll->on, active);
12131
d3fcc808 12132 for_each_intel_crtc(dev, crtc) {
83d65738 12133 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
12134 enabled_crtcs++;
12135 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12136 active_crtcs++;
12137 }
e2c719b7 12138 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
12139 "pll active crtcs mismatch (expected %i, found %i)\n",
12140 pll->active, active_crtcs);
e2c719b7 12141 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 12142 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 12143 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 12144
e2c719b7 12145 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12146 sizeof(dpll_hw_state)),
12147 "pll hw state mismatch\n");
5358901f 12148 }
8af6cf88
DV
12149}
12150
91d1b4bd
DV
12151void
12152intel_modeset_check_state(struct drm_device *dev)
12153{
08db6652 12154 check_wm_state(dev);
91d1b4bd
DV
12155 check_connector_state(dev);
12156 check_encoder_state(dev);
12157 check_crtc_state(dev);
12158 check_shared_dpll_state(dev);
12159}
12160
5cec258b 12161void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
12162 int dotclock)
12163{
12164 /*
12165 * FDI already provided one idea for the dotclock.
12166 * Yell if the encoder disagrees.
12167 */
2d112de7 12168 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 12169 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 12170 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
12171}
12172
80715b2f
VS
12173static void update_scanline_offset(struct intel_crtc *crtc)
12174{
12175 struct drm_device *dev = crtc->base.dev;
12176
12177 /*
12178 * The scanline counter increments at the leading edge of hsync.
12179 *
12180 * On most platforms it starts counting from vtotal-1 on the
12181 * first active line. That means the scanline counter value is
12182 * always one less than what we would expect. Ie. just after
12183 * start of vblank, which also occurs at start of hsync (on the
12184 * last active line), the scanline counter will read vblank_start-1.
12185 *
12186 * On gen2 the scanline counter starts counting from 1 instead
12187 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12188 * to keep the value positive), instead of adding one.
12189 *
12190 * On HSW+ the behaviour of the scanline counter depends on the output
12191 * type. For DP ports it behaves like most other platforms, but on HDMI
12192 * there's an extra 1 line difference. So we need to add two instead of
12193 * one to the value.
12194 */
12195 if (IS_GEN2(dev)) {
6e3c9717 12196 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12197 int vtotal;
12198
12199 vtotal = mode->crtc_vtotal;
12200 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12201 vtotal /= 2;
12202
12203 crtc->scanline_offset = vtotal - 1;
12204 } else if (HAS_DDI(dev) &&
409ee761 12205 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12206 crtc->scanline_offset = 2;
12207 } else
12208 crtc->scanline_offset = 1;
12209}
12210
5cec258b 12211static struct intel_crtc_state *
7f27126e
JB
12212intel_modeset_compute_config(struct drm_crtc *crtc,
12213 struct drm_display_mode *mode,
83a57153 12214 struct drm_atomic_state *state,
7f27126e
JB
12215 unsigned *modeset_pipes,
12216 unsigned *prepare_pipes,
12217 unsigned *disable_pipes)
12218{
db7542dd 12219 struct drm_device *dev = crtc->dev;
5cec258b 12220 struct intel_crtc_state *pipe_config = NULL;
db7542dd 12221 struct intel_crtc *intel_crtc;
0b901879
ACO
12222 int ret = 0;
12223
12224 ret = drm_atomic_add_affected_connectors(state, crtc);
12225 if (ret)
12226 return ERR_PTR(ret);
7f27126e
JB
12227
12228 intel_modeset_affected_pipes(crtc, modeset_pipes,
12229 prepare_pipes, disable_pipes);
12230
db7542dd
ACO
12231 for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
12232 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12233 if (IS_ERR(pipe_config))
12234 return pipe_config;
12235
12236 pipe_config->base.enable = false;
12237 }
7f27126e
JB
12238
12239 /*
12240 * Note this needs changes when we start tracking multiple modes
12241 * and crtcs. At that point we'll need to compute the whole config
12242 * (i.e. one pipe_config for each crtc) rather than just the one
12243 * for this crtc.
12244 */
db7542dd
ACO
12245 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
12246 /* FIXME: For now we still expect modeset_pipes has at most
12247 * one bit set. */
12248 if (WARN_ON(&intel_crtc->base != crtc))
12249 continue;
83a57153 12250
8805aa71 12251 pipe_config = intel_modeset_pipe_config(crtc, mode, state);
db7542dd
ACO
12252 if (IS_ERR(pipe_config))
12253 return pipe_config;
7f27126e 12254
304603f4
ACO
12255 pipe_config->base.enable = true;
12256
db7542dd
ACO
12257 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12258 "[modeset]");
12259 }
12260
12261 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
7f27126e
JB
12262}
12263
225da59b 12264static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
ed6739ef
ACO
12265 unsigned modeset_pipes,
12266 unsigned disable_pipes)
12267{
225da59b 12268 struct drm_device *dev = state->dev;
ed6739ef
ACO
12269 struct drm_i915_private *dev_priv = to_i915(dev);
12270 unsigned clear_pipes = modeset_pipes | disable_pipes;
12271 struct intel_crtc *intel_crtc;
12272 int ret = 0;
12273
12274 if (!dev_priv->display.crtc_compute_clock)
12275 return 0;
12276
12277 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12278 if (ret)
12279 goto done;
12280
12281 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
225da59b
ACO
12282 struct intel_crtc_state *crtc_state =
12283 intel_atomic_get_crtc_state(state, intel_crtc);
12284
12285 /* Modeset pipes should have a new state by now */
12286 if (WARN_ON(IS_ERR(crtc_state)))
12287 continue;
12288
ed6739ef 12289 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
225da59b 12290 crtc_state);
ed6739ef
ACO
12291 if (ret) {
12292 intel_shared_dpll_abort_config(dev_priv);
12293 goto done;
12294 }
12295 }
12296
12297done:
12298 return ret;
12299}
12300
f30da187
DV
12301static int __intel_set_mode(struct drm_crtc *crtc,
12302 struct drm_display_mode *mode,
7f27126e 12303 int x, int y, struct drm_framebuffer *fb,
5cec258b 12304 struct intel_crtc_state *pipe_config,
7f27126e
JB
12305 unsigned modeset_pipes,
12306 unsigned prepare_pipes,
12307 unsigned disable_pipes)
a6778b3c
DV
12308{
12309 struct drm_device *dev = crtc->dev;
fbee40df 12310 struct drm_i915_private *dev_priv = dev->dev_private;
4b4b9238 12311 struct drm_display_mode *saved_mode;
304603f4 12312 struct drm_atomic_state *state = pipe_config->base.state;
83a57153 12313 struct intel_crtc_state *crtc_state_copy = NULL;
25c5b266 12314 struct intel_crtc *intel_crtc;
c0c36b94 12315 int ret = 0;
a6778b3c 12316
4b4b9238 12317 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
12318 if (!saved_mode)
12319 return -ENOMEM;
a6778b3c 12320
83a57153
ACO
12321 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
12322 if (!crtc_state_copy) {
12323 ret = -ENOMEM;
12324 goto done;
12325 }
12326
3ac18232 12327 *saved_mode = crtc->mode;
a6778b3c 12328
30a970c6
JB
12329 /*
12330 * See if the config requires any additional preparation, e.g.
12331 * to adjust global state with pipes off. We need to do this
12332 * here so we can get the modeset_pipe updated config for the new
12333 * mode set on this crtc. For other crtcs we need to use the
12334 * adjusted_mode bits in the crtc directly.
12335 */
f8437dd1 12336 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
304603f4
ACO
12337 ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
12338 if (ret)
12339 goto done;
30a970c6 12340
c164f833
VS
12341 /* may have added more to prepare_pipes than we should */
12342 prepare_pipes &= ~disable_pipes;
12343 }
12344
225da59b 12345 ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
ed6739ef
ACO
12346 if (ret)
12347 goto done;
8bd31e67 12348
460da916
DV
12349 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
12350 intel_crtc_disable(&intel_crtc->base);
12351
ea9d758d 12352 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
ce22dba9
ML
12353 if (intel_crtc->base.state->enable) {
12354 intel_crtc_disable_planes(&intel_crtc->base);
ea9d758d 12355 dev_priv->display.crtc_disable(&intel_crtc->base);
ce22dba9 12356 }
ea9d758d 12357 }
a6778b3c 12358
6c4c86f5
DV
12359 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12360 * to set it here already despite that we pass it down the callchain.
7f27126e
JB
12361 *
12362 * Note we'll need to fix this up when we start tracking multiple
12363 * pipes; here we assume a single modeset_pipe and only track the
12364 * single crtc and mode.
f6e5b160 12365 */
b8cecdf5 12366 if (modeset_pipes) {
25c5b266 12367 crtc->mode = *mode;
b8cecdf5
DV
12368 /* mode_set/enable/disable functions rely on a correct pipe
12369 * config. */
f5de6e07 12370 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
c326c0a9
VS
12371
12372 /*
12373 * Calculate and store various constants which
12374 * are later needed by vblank and swap-completion
12375 * timestamping. They are derived from true hwmode.
12376 */
12377 drm_calc_timestamping_constants(crtc,
2d112de7 12378 &pipe_config->base.adjusted_mode);
b8cecdf5 12379 }
7758a113 12380
ea9d758d
DV
12381 /* Only after disabling all output pipelines that will be changed can we
12382 * update the the output configuration. */
12383 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 12384
304603f4 12385 modeset_update_crtc_power_domains(state);
47fab737 12386
25c5b266 12387 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
455a6808
GP
12388 struct drm_plane *primary = intel_crtc->base.primary;
12389 int vdisplay, hdisplay;
4c10794f 12390
455a6808 12391 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
70a101f8
MR
12392 ret = drm_plane_helper_update(primary, &intel_crtc->base,
12393 fb, 0, 0,
12394 hdisplay, vdisplay,
12395 x << 16, y << 16,
12396 hdisplay << 16, vdisplay << 16);
a6778b3c
DV
12397 }
12398
12399 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
80715b2f
VS
12400 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
12401 update_scanline_offset(intel_crtc);
12402
25c5b266 12403 dev_priv->display.crtc_enable(&intel_crtc->base);
ce22dba9 12404 intel_crtc_enable_planes(&intel_crtc->base);
80715b2f 12405 }
a6778b3c 12406
a6778b3c
DV
12407 /* FIXME: add subpixel order */
12408done:
83d65738 12409 if (ret && crtc->state->enable)
3ac18232 12410 crtc->mode = *saved_mode;
a6778b3c 12411
83a57153
ACO
12412 if (ret == 0 && pipe_config) {
12413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12414
12415 /* The pipe_config will be freed with the atomic state, so
12416 * make a copy. */
12417 memcpy(crtc_state_copy, intel_crtc->config,
12418 sizeof *crtc_state_copy);
12419 intel_crtc->config = crtc_state_copy;
12420 intel_crtc->base.state = &crtc_state_copy->base;
83a57153
ACO
12421 } else {
12422 kfree(crtc_state_copy);
12423 }
12424
3ac18232 12425 kfree(saved_mode);
a6778b3c 12426 return ret;
f6e5b160
CW
12427}
12428
7f27126e
JB
12429static int intel_set_mode_pipes(struct drm_crtc *crtc,
12430 struct drm_display_mode *mode,
12431 int x, int y, struct drm_framebuffer *fb,
5cec258b 12432 struct intel_crtc_state *pipe_config,
7f27126e
JB
12433 unsigned modeset_pipes,
12434 unsigned prepare_pipes,
12435 unsigned disable_pipes)
f30da187
DV
12436{
12437 int ret;
12438
7f27126e
JB
12439 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
12440 prepare_pipes, disable_pipes);
f30da187
DV
12441
12442 if (ret == 0)
12443 intel_modeset_check_state(crtc->dev);
12444
12445 return ret;
12446}
12447
7f27126e
JB
12448static int intel_set_mode(struct drm_crtc *crtc,
12449 struct drm_display_mode *mode,
83a57153
ACO
12450 int x, int y, struct drm_framebuffer *fb,
12451 struct drm_atomic_state *state)
7f27126e 12452{
5cec258b 12453 struct intel_crtc_state *pipe_config;
7f27126e 12454 unsigned modeset_pipes, prepare_pipes, disable_pipes;
83a57153 12455 int ret = 0;
7f27126e 12456
8805aa71 12457 pipe_config = intel_modeset_compute_config(crtc, mode, state,
7f27126e
JB
12458 &modeset_pipes,
12459 &prepare_pipes,
12460 &disable_pipes);
12461
83a57153
ACO
12462 if (IS_ERR(pipe_config)) {
12463 ret = PTR_ERR(pipe_config);
12464 goto out;
12465 }
12466
12467 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
12468 modeset_pipes, prepare_pipes,
12469 disable_pipes);
12470 if (ret)
12471 goto out;
7f27126e 12472
83a57153
ACO
12473out:
12474 return ret;
7f27126e
JB
12475}
12476
c0c36b94
CW
12477void intel_crtc_restore_mode(struct drm_crtc *crtc)
12478{
83a57153
ACO
12479 struct drm_device *dev = crtc->dev;
12480 struct drm_atomic_state *state;
12481 struct intel_encoder *encoder;
12482 struct intel_connector *connector;
12483 struct drm_connector_state *connector_state;
12484
12485 state = drm_atomic_state_alloc(dev);
12486 if (!state) {
12487 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12488 crtc->base.id);
12489 return;
12490 }
12491
12492 state->acquire_ctx = dev->mode_config.acquire_ctx;
12493
12494 /* The force restore path in the HW readout code relies on the staged
12495 * config still keeping the user requested config while the actual
12496 * state has been overwritten by the configuration read from HW. We
12497 * need to copy the staged config to the atomic state, otherwise the
12498 * mode set will just reapply the state the HW is already in. */
12499 for_each_intel_encoder(dev, encoder) {
12500 if (&encoder->new_crtc->base != crtc)
12501 continue;
12502
12503 for_each_intel_connector(dev, connector) {
12504 if (connector->new_encoder != encoder)
12505 continue;
12506
12507 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12508 if (IS_ERR(connector_state)) {
12509 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12510 connector->base.base.id,
12511 connector->base.name,
12512 PTR_ERR(connector_state));
12513 continue;
12514 }
12515
12516 connector_state->crtc = crtc;
12517 connector_state->best_encoder = &encoder->base;
12518 }
12519 }
12520
12521 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
12522 state);
12523
12524 drm_atomic_state_free(state);
c0c36b94
CW
12525}
12526
25c5b266
DV
12527#undef for_each_intel_crtc_masked
12528
d9e55608
DV
12529static void intel_set_config_free(struct intel_set_config *config)
12530{
12531 if (!config)
12532 return;
12533
1aa4b628
DV
12534 kfree(config->save_connector_encoders);
12535 kfree(config->save_encoder_crtcs);
7668851f 12536 kfree(config->save_crtc_enabled);
d9e55608
DV
12537 kfree(config);
12538}
12539
85f9eb71
DV
12540static int intel_set_config_save_state(struct drm_device *dev,
12541 struct intel_set_config *config)
12542{
7668851f 12543 struct drm_crtc *crtc;
85f9eb71
DV
12544 struct drm_encoder *encoder;
12545 struct drm_connector *connector;
12546 int count;
12547
7668851f
VS
12548 config->save_crtc_enabled =
12549 kcalloc(dev->mode_config.num_crtc,
12550 sizeof(bool), GFP_KERNEL);
12551 if (!config->save_crtc_enabled)
12552 return -ENOMEM;
12553
1aa4b628
DV
12554 config->save_encoder_crtcs =
12555 kcalloc(dev->mode_config.num_encoder,
12556 sizeof(struct drm_crtc *), GFP_KERNEL);
12557 if (!config->save_encoder_crtcs)
85f9eb71
DV
12558 return -ENOMEM;
12559
1aa4b628
DV
12560 config->save_connector_encoders =
12561 kcalloc(dev->mode_config.num_connector,
12562 sizeof(struct drm_encoder *), GFP_KERNEL);
12563 if (!config->save_connector_encoders)
85f9eb71
DV
12564 return -ENOMEM;
12565
12566 /* Copy data. Note that driver private data is not affected.
12567 * Should anything bad happen only the expected state is
12568 * restored, not the drivers personal bookkeeping.
12569 */
7668851f 12570 count = 0;
70e1e0ec 12571 for_each_crtc(dev, crtc) {
83d65738 12572 config->save_crtc_enabled[count++] = crtc->state->enable;
7668851f
VS
12573 }
12574
85f9eb71
DV
12575 count = 0;
12576 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 12577 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
12578 }
12579
12580 count = 0;
12581 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 12582 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
12583 }
12584
12585 return 0;
12586}
12587
12588static void intel_set_config_restore_state(struct drm_device *dev,
12589 struct intel_set_config *config)
12590{
7668851f 12591 struct intel_crtc *crtc;
9a935856
DV
12592 struct intel_encoder *encoder;
12593 struct intel_connector *connector;
85f9eb71
DV
12594 int count;
12595
7668851f 12596 count = 0;
d3fcc808 12597 for_each_intel_crtc(dev, crtc) {
7668851f
VS
12598 crtc->new_enabled = config->save_crtc_enabled[count++];
12599 }
12600
85f9eb71 12601 count = 0;
b2784e15 12602 for_each_intel_encoder(dev, encoder) {
9a935856
DV
12603 encoder->new_crtc =
12604 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
12605 }
12606
12607 count = 0;
3a3371ff 12608 for_each_intel_connector(dev, connector) {
9a935856
DV
12609 connector->new_encoder =
12610 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
12611 }
12612}
12613
e3de42b6 12614static bool
2e57f47d 12615is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
12616{
12617 int i;
12618
2e57f47d
CW
12619 if (set->num_connectors == 0)
12620 return false;
12621
12622 if (WARN_ON(set->connectors == NULL))
12623 return false;
12624
12625 for (i = 0; i < set->num_connectors; i++)
12626 if (set->connectors[i]->encoder &&
12627 set->connectors[i]->encoder->crtc == set->crtc &&
12628 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
12629 return true;
12630
12631 return false;
12632}
12633
5e2b584e
DV
12634static void
12635intel_set_config_compute_mode_changes(struct drm_mode_set *set,
12636 struct intel_set_config *config)
12637{
12638
12639 /* We should be able to check here if the fb has the same properties
12640 * and then just flip_or_move it */
2e57f47d
CW
12641 if (is_crtc_connector_off(set)) {
12642 config->mode_changed = true;
f4510a27 12643 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
12644 /*
12645 * If we have no fb, we can only flip as long as the crtc is
12646 * active, otherwise we need a full mode set. The crtc may
12647 * be active if we've only disabled the primary plane, or
12648 * in fastboot situations.
12649 */
f4510a27 12650 if (set->crtc->primary->fb == NULL) {
319d9827
JB
12651 struct intel_crtc *intel_crtc =
12652 to_intel_crtc(set->crtc);
12653
3b150f08 12654 if (intel_crtc->active) {
319d9827
JB
12655 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
12656 config->fb_changed = true;
12657 } else {
12658 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
12659 config->mode_changed = true;
12660 }
5e2b584e
DV
12661 } else if (set->fb == NULL) {
12662 config->mode_changed = true;
72f4901e 12663 } else if (set->fb->pixel_format !=
f4510a27 12664 set->crtc->primary->fb->pixel_format) {
5e2b584e 12665 config->mode_changed = true;
e3de42b6 12666 } else {
5e2b584e 12667 config->fb_changed = true;
e3de42b6 12668 }
5e2b584e
DV
12669 }
12670
835c5873 12671 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
12672 config->fb_changed = true;
12673
12674 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12675 DRM_DEBUG_KMS("modes are different, full mode set\n");
12676 drm_mode_debug_printmodeline(&set->crtc->mode);
12677 drm_mode_debug_printmodeline(set->mode);
12678 config->mode_changed = true;
12679 }
a1d95703
CW
12680
12681 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
12682 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
12683}
12684
2e431051 12685static int
9a935856
DV
12686intel_modeset_stage_output_state(struct drm_device *dev,
12687 struct drm_mode_set *set,
944b0c76
ACO
12688 struct intel_set_config *config,
12689 struct drm_atomic_state *state)
50f56119 12690{
9a935856 12691 struct intel_connector *connector;
944b0c76 12692 struct drm_connector_state *connector_state;
9a935856 12693 struct intel_encoder *encoder;
7668851f 12694 struct intel_crtc *crtc;
f3f08572 12695 int ro;
50f56119 12696
9abdda74 12697 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
12698 * of connectors. For paranoia, double-check this. */
12699 WARN_ON(!set->fb && (set->num_connectors != 0));
12700 WARN_ON(set->fb && (set->num_connectors == 0));
12701
3a3371ff 12702 for_each_intel_connector(dev, connector) {
9a935856
DV
12703 /* Otherwise traverse passed in connector list and get encoders
12704 * for them. */
50f56119 12705 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 12706 if (set->connectors[ro] == &connector->base) {
0e32b39c 12707 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
50f56119
DV
12708 break;
12709 }
12710 }
12711
9a935856
DV
12712 /* If we disable the crtc, disable all its connectors. Also, if
12713 * the connector is on the changing crtc but not on the new
12714 * connector list, disable it. */
12715 if ((!set->fb || ro == set->num_connectors) &&
12716 connector->base.encoder &&
12717 connector->base.encoder->crtc == set->crtc) {
12718 connector->new_encoder = NULL;
12719
12720 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12721 connector->base.base.id,
c23cc417 12722 connector->base.name);
9a935856
DV
12723 }
12724
12725
12726 if (&connector->new_encoder->base != connector->base.encoder) {
10634189
ACO
12727 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12728 connector->base.base.id,
12729 connector->base.name);
5e2b584e 12730 config->mode_changed = true;
50f56119
DV
12731 }
12732 }
9a935856 12733 /* connector->new_encoder is now updated for all connectors. */
50f56119 12734
9a935856 12735 /* Update crtc of enabled connectors. */
3a3371ff 12736 for_each_intel_connector(dev, connector) {
7668851f
VS
12737 struct drm_crtc *new_crtc;
12738
9a935856 12739 if (!connector->new_encoder)
50f56119
DV
12740 continue;
12741
9a935856 12742 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
12743
12744 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 12745 if (set->connectors[ro] == &connector->base)
50f56119
DV
12746 new_crtc = set->crtc;
12747 }
12748
12749 /* Make sure the new CRTC will work with the encoder */
14509916
TR
12750 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12751 new_crtc)) {
5e2b584e 12752 return -EINVAL;
50f56119 12753 }
0e32b39c 12754 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
9a935856 12755
944b0c76
ACO
12756 connector_state =
12757 drm_atomic_get_connector_state(state, &connector->base);
12758 if (IS_ERR(connector_state))
12759 return PTR_ERR(connector_state);
12760
12761 connector_state->crtc = new_crtc;
12762 connector_state->best_encoder = &connector->new_encoder->base;
12763
9a935856
DV
12764 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12765 connector->base.base.id,
c23cc417 12766 connector->base.name,
9a935856
DV
12767 new_crtc->base.id);
12768 }
12769
12770 /* Check for any encoders that needs to be disabled. */
b2784e15 12771 for_each_intel_encoder(dev, encoder) {
5a65f358 12772 int num_connectors = 0;
3a3371ff 12773 for_each_intel_connector(dev, connector) {
9a935856
DV
12774 if (connector->new_encoder == encoder) {
12775 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 12776 num_connectors++;
9a935856
DV
12777 }
12778 }
5a65f358
PZ
12779
12780 if (num_connectors == 0)
12781 encoder->new_crtc = NULL;
12782 else if (num_connectors > 1)
12783 return -EINVAL;
12784
9a935856
DV
12785 /* Only now check for crtc changes so we don't miss encoders
12786 * that will be disabled. */
12787 if (&encoder->new_crtc->base != encoder->base.crtc) {
10634189
ACO
12788 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12789 encoder->base.base.id,
12790 encoder->base.name);
5e2b584e 12791 config->mode_changed = true;
50f56119
DV
12792 }
12793 }
9a935856 12794 /* Now we've also updated encoder->new_crtc for all encoders. */
3a3371ff 12795 for_each_intel_connector(dev, connector) {
944b0c76
ACO
12796 connector_state =
12797 drm_atomic_get_connector_state(state, &connector->base);
9d918c15
ACO
12798 if (IS_ERR(connector_state))
12799 return PTR_ERR(connector_state);
944b0c76
ACO
12800
12801 if (connector->new_encoder) {
0e32b39c
DA
12802 if (connector->new_encoder != connector->encoder)
12803 connector->encoder = connector->new_encoder;
944b0c76
ACO
12804 } else {
12805 connector_state->crtc = NULL;
f61cccf3 12806 connector_state->best_encoder = NULL;
944b0c76 12807 }
0e32b39c 12808 }
d3fcc808 12809 for_each_intel_crtc(dev, crtc) {
7668851f
VS
12810 crtc->new_enabled = false;
12811
b2784e15 12812 for_each_intel_encoder(dev, encoder) {
7668851f
VS
12813 if (encoder->new_crtc == crtc) {
12814 crtc->new_enabled = true;
12815 break;
12816 }
12817 }
12818
83d65738 12819 if (crtc->new_enabled != crtc->base.state->enable) {
10634189
ACO
12820 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12821 crtc->base.base.id,
7668851f
VS
12822 crtc->new_enabled ? "en" : "dis");
12823 config->mode_changed = true;
12824 }
12825 }
12826
2e431051
DV
12827 return 0;
12828}
12829
7d00a1f5
VS
12830static void disable_crtc_nofb(struct intel_crtc *crtc)
12831{
12832 struct drm_device *dev = crtc->base.dev;
12833 struct intel_encoder *encoder;
12834 struct intel_connector *connector;
12835
12836 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12837 pipe_name(crtc->pipe));
12838
3a3371ff 12839 for_each_intel_connector(dev, connector) {
7d00a1f5
VS
12840 if (connector->new_encoder &&
12841 connector->new_encoder->new_crtc == crtc)
12842 connector->new_encoder = NULL;
12843 }
12844
b2784e15 12845 for_each_intel_encoder(dev, encoder) {
7d00a1f5
VS
12846 if (encoder->new_crtc == crtc)
12847 encoder->new_crtc = NULL;
12848 }
12849
12850 crtc->new_enabled = false;
12851}
12852
2e431051
DV
12853static int intel_crtc_set_config(struct drm_mode_set *set)
12854{
12855 struct drm_device *dev;
2e431051 12856 struct drm_mode_set save_set;
83a57153 12857 struct drm_atomic_state *state = NULL;
2e431051 12858 struct intel_set_config *config;
5cec258b 12859 struct intel_crtc_state *pipe_config;
50f52756 12860 unsigned modeset_pipes, prepare_pipes, disable_pipes;
2e431051 12861 int ret;
2e431051 12862
8d3e375e
DV
12863 BUG_ON(!set);
12864 BUG_ON(!set->crtc);
12865 BUG_ON(!set->crtc->helper_private);
2e431051 12866
7e53f3a4
DV
12867 /* Enforce sane interface api - has been abused by the fb helper. */
12868 BUG_ON(!set->mode && set->fb);
12869 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 12870
2e431051
DV
12871 if (set->fb) {
12872 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12873 set->crtc->base.id, set->fb->base.id,
12874 (int)set->num_connectors, set->x, set->y);
12875 } else {
12876 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
12877 }
12878
12879 dev = set->crtc->dev;
12880
12881 ret = -ENOMEM;
12882 config = kzalloc(sizeof(*config), GFP_KERNEL);
12883 if (!config)
12884 goto out_config;
12885
12886 ret = intel_set_config_save_state(dev, config);
12887 if (ret)
12888 goto out_config;
12889
12890 save_set.crtc = set->crtc;
12891 save_set.mode = &set->crtc->mode;
12892 save_set.x = set->crtc->x;
12893 save_set.y = set->crtc->y;
f4510a27 12894 save_set.fb = set->crtc->primary->fb;
2e431051
DV
12895
12896 /* Compute whether we need a full modeset, only an fb base update or no
12897 * change at all. In the future we might also check whether only the
12898 * mode changed, e.g. for LVDS where we only change the panel fitter in
12899 * such cases. */
12900 intel_set_config_compute_mode_changes(set, config);
12901
83a57153
ACO
12902 state = drm_atomic_state_alloc(dev);
12903 if (!state) {
12904 ret = -ENOMEM;
12905 goto out_config;
12906 }
12907
12908 state->acquire_ctx = dev->mode_config.acquire_ctx;
12909
944b0c76 12910 ret = intel_modeset_stage_output_state(dev, set, config, state);
2e431051
DV
12911 if (ret)
12912 goto fail;
12913
50f52756 12914 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
8805aa71 12915 state,
50f52756
JB
12916 &modeset_pipes,
12917 &prepare_pipes,
12918 &disable_pipes);
20664591 12919 if (IS_ERR(pipe_config)) {
6ac0483b 12920 ret = PTR_ERR(pipe_config);
50f52756 12921 goto fail;
20664591 12922 } else if (pipe_config) {
b9950a13 12923 if (pipe_config->has_audio !=
6e3c9717 12924 to_intel_crtc(set->crtc)->config->has_audio)
20664591
JB
12925 config->mode_changed = true;
12926
af15d2ce
JB
12927 /*
12928 * Note we have an issue here with infoframes: current code
12929 * only updates them on the full mode set path per hw
12930 * requirements. So here we should be checking for any
12931 * required changes and forcing a mode set.
12932 */
20664591 12933 }
50f52756 12934
1f9954d0
JB
12935 intel_update_pipe_size(to_intel_crtc(set->crtc));
12936
5e2b584e 12937 if (config->mode_changed) {
50f52756
JB
12938 ret = intel_set_mode_pipes(set->crtc, set->mode,
12939 set->x, set->y, set->fb, pipe_config,
12940 modeset_pipes, prepare_pipes,
12941 disable_pipes);
5e2b584e 12942 } else if (config->fb_changed) {
3b150f08 12943 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
455a6808 12944 struct drm_plane *primary = set->crtc->primary;
b70709a6
ML
12945 struct intel_plane_state *plane_state =
12946 to_intel_plane_state(primary->state);
12947 bool was_visible = plane_state->visible;
455a6808 12948 int vdisplay, hdisplay;
3b150f08 12949
455a6808 12950 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
70a101f8
MR
12951 ret = drm_plane_helper_update(primary, set->crtc, set->fb,
12952 0, 0, hdisplay, vdisplay,
12953 set->x << 16, set->y << 16,
12954 hdisplay << 16, vdisplay << 16);
3b150f08
MR
12955
12956 /*
12957 * We need to make sure the primary plane is re-enabled if it
12958 * has previously been turned off.
12959 */
b70709a6
ML
12960 plane_state = to_intel_plane_state(primary->state);
12961 if (ret == 0 && !was_visible && plane_state->visible) {
3b150f08 12962 WARN_ON(!intel_crtc->active);
87d4300a 12963 intel_post_enable_primary(set->crtc);
3b150f08
MR
12964 }
12965
7ca51a3a
JB
12966 /*
12967 * In the fastboot case this may be our only check of the
12968 * state after boot. It would be better to only do it on
12969 * the first update, but we don't have a nice way of doing that
12970 * (and really, set_config isn't used much for high freq page
12971 * flipping, so increasing its cost here shouldn't be a big
12972 * deal).
12973 */
d330a953 12974 if (i915.fastboot && ret == 0)
7ca51a3a 12975 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
12976 }
12977
2d05eae1 12978 if (ret) {
bf67dfeb
DV
12979 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12980 set->crtc->base.id, ret);
50f56119 12981fail:
2d05eae1 12982 intel_set_config_restore_state(dev, config);
50f56119 12983
83a57153
ACO
12984 drm_atomic_state_clear(state);
12985
7d00a1f5
VS
12986 /*
12987 * HACK: if the pipe was on, but we didn't have a framebuffer,
12988 * force the pipe off to avoid oopsing in the modeset code
12989 * due to fb==NULL. This should only happen during boot since
12990 * we don't yet reconstruct the FB from the hardware state.
12991 */
12992 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
12993 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
12994
2d05eae1
CW
12995 /* Try to restore the config */
12996 if (config->mode_changed &&
12997 intel_set_mode(save_set.crtc, save_set.mode,
83a57153
ACO
12998 save_set.x, save_set.y, save_set.fb,
12999 state))
2d05eae1
CW
13000 DRM_ERROR("failed to restore config after modeset failure\n");
13001 }
50f56119 13002
d9e55608 13003out_config:
e5d958ef 13004 drm_atomic_state_free(state);
83a57153 13005
d9e55608 13006 intel_set_config_free(config);
50f56119
DV
13007 return ret;
13008}
f6e5b160
CW
13009
13010static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 13011 .gamma_set = intel_crtc_gamma_set,
50f56119 13012 .set_config = intel_crtc_set_config,
f6e5b160
CW
13013 .destroy = intel_crtc_destroy,
13014 .page_flip = intel_crtc_page_flip,
1356837e
MR
13015 .atomic_duplicate_state = intel_crtc_duplicate_state,
13016 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
13017};
13018
5358901f
DV
13019static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13020 struct intel_shared_dpll *pll,
13021 struct intel_dpll_hw_state *hw_state)
ee7b9f93 13022{
5358901f 13023 uint32_t val;
ee7b9f93 13024
f458ebbc 13025 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
13026 return false;
13027
5358901f 13028 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
13029 hw_state->dpll = val;
13030 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13031 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
13032
13033 return val & DPLL_VCO_ENABLE;
13034}
13035
15bdd4cf
DV
13036static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13037 struct intel_shared_dpll *pll)
13038{
3e369b76
ACO
13039 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13040 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
13041}
13042
e7b903d2
DV
13043static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13044 struct intel_shared_dpll *pll)
13045{
e7b903d2 13046 /* PCH refclock must be enabled first */
89eff4be 13047 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 13048
3e369b76 13049 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
13050
13051 /* Wait for the clocks to stabilize. */
13052 POSTING_READ(PCH_DPLL(pll->id));
13053 udelay(150);
13054
13055 /* The pixel multiplier can only be updated once the
13056 * DPLL is enabled and the clocks are stable.
13057 *
13058 * So write it again.
13059 */
3e369b76 13060 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 13061 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13062 udelay(200);
13063}
13064
13065static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13066 struct intel_shared_dpll *pll)
13067{
13068 struct drm_device *dev = dev_priv->dev;
13069 struct intel_crtc *crtc;
e7b903d2
DV
13070
13071 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 13072 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
13073 if (intel_crtc_to_shared_dpll(crtc) == pll)
13074 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
13075 }
13076
15bdd4cf
DV
13077 I915_WRITE(PCH_DPLL(pll->id), 0);
13078 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13079 udelay(200);
13080}
13081
46edb027
DV
13082static char *ibx_pch_dpll_names[] = {
13083 "PCH DPLL A",
13084 "PCH DPLL B",
13085};
13086
7c74ade1 13087static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 13088{
e7b903d2 13089 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
13090 int i;
13091
7c74ade1 13092 dev_priv->num_shared_dpll = 2;
ee7b9f93 13093
e72f9fbf 13094 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
13095 dev_priv->shared_dplls[i].id = i;
13096 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 13097 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
13098 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13099 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
13100 dev_priv->shared_dplls[i].get_hw_state =
13101 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
13102 }
13103}
13104
7c74ade1
DV
13105static void intel_shared_dpll_init(struct drm_device *dev)
13106{
e7b903d2 13107 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 13108
9cd86933
DV
13109 if (HAS_DDI(dev))
13110 intel_ddi_pll_init(dev);
13111 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
13112 ibx_pch_dpll_init(dev);
13113 else
13114 dev_priv->num_shared_dpll = 0;
13115
13116 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
13117}
13118
1fc0a8f7
TU
13119/**
13120 * intel_wm_need_update - Check whether watermarks need updating
13121 * @plane: drm plane
13122 * @state: new plane state
13123 *
13124 * Check current plane state versus the new one to determine whether
13125 * watermarks need to be recalculated.
13126 *
13127 * Returns true or false.
13128 */
13129bool intel_wm_need_update(struct drm_plane *plane,
13130 struct drm_plane_state *state)
13131{
13132 /* Update watermarks on tiling changes. */
13133 if (!plane->state->fb || !state->fb ||
13134 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13135 plane->state->rotation != state->rotation)
13136 return true;
13137
13138 return false;
13139}
13140
6beb8c23
MR
13141/**
13142 * intel_prepare_plane_fb - Prepare fb for usage on plane
13143 * @plane: drm plane to prepare for
13144 * @fb: framebuffer to prepare for presentation
13145 *
13146 * Prepares a framebuffer for usage on a display plane. Generally this
13147 * involves pinning the underlying object and updating the frontbuffer tracking
13148 * bits. Some older platforms need special physical address handling for
13149 * cursor planes.
13150 *
13151 * Returns 0 on success, negative error code on failure.
13152 */
13153int
13154intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee
TU
13155 struct drm_framebuffer *fb,
13156 const struct drm_plane_state *new_state)
465c120c
MR
13157{
13158 struct drm_device *dev = plane->dev;
6beb8c23
MR
13159 struct intel_plane *intel_plane = to_intel_plane(plane);
13160 enum pipe pipe = intel_plane->pipe;
13161 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13162 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13163 unsigned frontbuffer_bits = 0;
13164 int ret = 0;
465c120c 13165
ea2c67bb 13166 if (!obj)
465c120c
MR
13167 return 0;
13168
6beb8c23
MR
13169 switch (plane->type) {
13170 case DRM_PLANE_TYPE_PRIMARY:
13171 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13172 break;
13173 case DRM_PLANE_TYPE_CURSOR:
13174 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13175 break;
13176 case DRM_PLANE_TYPE_OVERLAY:
13177 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13178 break;
13179 }
465c120c 13180
6beb8c23 13181 mutex_lock(&dev->struct_mutex);
465c120c 13182
6beb8c23
MR
13183 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13184 INTEL_INFO(dev)->cursor_needs_physical) {
13185 int align = IS_I830(dev) ? 16 * 1024 : 256;
13186 ret = i915_gem_object_attach_phys(obj, align);
13187 if (ret)
13188 DRM_DEBUG_KMS("failed to attach phys object\n");
13189 } else {
82bc3b2d 13190 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
6beb8c23 13191 }
465c120c 13192
6beb8c23
MR
13193 if (ret == 0)
13194 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
fdd508a6 13195
4c34574f 13196 mutex_unlock(&dev->struct_mutex);
465c120c 13197
6beb8c23
MR
13198 return ret;
13199}
13200
38f3ce3a
MR
13201/**
13202 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13203 * @plane: drm plane to clean up for
13204 * @fb: old framebuffer that was on plane
13205 *
13206 * Cleans up a framebuffer that has just been removed from a plane.
13207 */
13208void
13209intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee
TU
13210 struct drm_framebuffer *fb,
13211 const struct drm_plane_state *old_state)
38f3ce3a
MR
13212{
13213 struct drm_device *dev = plane->dev;
13214 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13215
13216 if (WARN_ON(!obj))
13217 return;
13218
13219 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13220 !INTEL_INFO(dev)->cursor_needs_physical) {
13221 mutex_lock(&dev->struct_mutex);
82bc3b2d 13222 intel_unpin_fb_obj(fb, old_state);
38f3ce3a
MR
13223 mutex_unlock(&dev->struct_mutex);
13224 }
465c120c
MR
13225}
13226
6156a456
CK
13227int
13228skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13229{
13230 int max_scale;
13231 struct drm_device *dev;
13232 struct drm_i915_private *dev_priv;
13233 int crtc_clock, cdclk;
13234
13235 if (!intel_crtc || !crtc_state)
13236 return DRM_PLANE_HELPER_NO_SCALING;
13237
13238 dev = intel_crtc->base.dev;
13239 dev_priv = dev->dev_private;
13240 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13241 cdclk = dev_priv->display.get_display_clock_speed(dev);
13242
13243 if (!crtc_clock || !cdclk)
13244 return DRM_PLANE_HELPER_NO_SCALING;
13245
13246 /*
13247 * skl max scale is lower of:
13248 * close to 3 but not 3, -1 is for that purpose
13249 * or
13250 * cdclk/crtc_clock
13251 */
13252 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13253
13254 return max_scale;
13255}
13256
465c120c 13257static int
3c692a41
GP
13258intel_check_primary_plane(struct drm_plane *plane,
13259 struct intel_plane_state *state)
13260{
32b7eeec
MR
13261 struct drm_device *dev = plane->dev;
13262 struct drm_i915_private *dev_priv = dev->dev_private;
2b875c22 13263 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13264 struct intel_crtc *intel_crtc;
6156a456 13265 struct intel_crtc_state *crtc_state;
2b875c22 13266 struct drm_framebuffer *fb = state->base.fb;
3c692a41
GP
13267 struct drm_rect *dest = &state->dst;
13268 struct drm_rect *src = &state->src;
13269 const struct drm_rect *clip = &state->clip;
d8106366 13270 bool can_position = false;
6156a456
CK
13271 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13272 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
465c120c
MR
13273 int ret;
13274
ea2c67bb
MR
13275 crtc = crtc ? crtc : plane->crtc;
13276 intel_crtc = to_intel_crtc(crtc);
6156a456
CK
13277 crtc_state = state->base.state ?
13278 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
ea2c67bb 13279
6156a456
CK
13280 if (INTEL_INFO(dev)->gen >= 9) {
13281 min_scale = 1;
13282 max_scale = skl_max_scale(intel_crtc, crtc_state);
d8106366 13283 can_position = true;
6156a456 13284 }
d8106366 13285
c59cb179
MR
13286 ret = drm_plane_helper_check_update(plane, crtc, fb,
13287 src, dest, clip,
6156a456
CK
13288 min_scale,
13289 max_scale,
d8106366
SJ
13290 can_position, true,
13291 &state->visible);
c59cb179
MR
13292 if (ret)
13293 return ret;
465c120c 13294
32b7eeec 13295 if (intel_crtc->active) {
b70709a6
ML
13296 struct intel_plane_state *old_state =
13297 to_intel_plane_state(plane->state);
13298
32b7eeec
MR
13299 intel_crtc->atomic.wait_for_flips = true;
13300
13301 /*
13302 * FBC does not work on some platforms for rotated
13303 * planes, so disable it when rotation is not 0 and
13304 * update it when rotation is set back to 0.
13305 *
13306 * FIXME: This is redundant with the fbc update done in
13307 * the primary plane enable function except that that
13308 * one is done too late. We eventually need to unify
13309 * this.
13310 */
b70709a6 13311 if (state->visible &&
32b7eeec 13312 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
e35fef21 13313 dev_priv->fbc.crtc == intel_crtc &&
8e7d688b 13314 state->base.rotation != BIT(DRM_ROTATE_0)) {
32b7eeec
MR
13315 intel_crtc->atomic.disable_fbc = true;
13316 }
13317
b70709a6 13318 if (state->visible && !old_state->visible) {
32b7eeec
MR
13319 /*
13320 * BDW signals flip done immediately if the plane
13321 * is disabled, even if the plane enable is already
13322 * armed to occur at the next vblank :(
13323 */
b70709a6 13324 if (IS_BROADWELL(dev))
32b7eeec
MR
13325 intel_crtc->atomic.wait_vblank = true;
13326 }
13327
13328 intel_crtc->atomic.fb_bits |=
13329 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13330
13331 intel_crtc->atomic.update_fbc = true;
0fda6568 13332
1fc0a8f7 13333 if (intel_wm_need_update(plane, &state->base))
0fda6568 13334 intel_crtc->atomic.update_wm = true;
ccc759dc
GP
13335 }
13336
6156a456
CK
13337 if (INTEL_INFO(dev)->gen >= 9) {
13338 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13339 to_intel_plane(plane), state, 0);
13340 if (ret)
13341 return ret;
13342 }
13343
14af293f
GP
13344 return 0;
13345}
13346
13347static void
13348intel_commit_primary_plane(struct drm_plane *plane,
13349 struct intel_plane_state *state)
13350{
2b875c22
MR
13351 struct drm_crtc *crtc = state->base.crtc;
13352 struct drm_framebuffer *fb = state->base.fb;
13353 struct drm_device *dev = plane->dev;
14af293f 13354 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 13355 struct intel_crtc *intel_crtc;
14af293f
GP
13356 struct drm_rect *src = &state->src;
13357
ea2c67bb
MR
13358 crtc = crtc ? crtc : plane->crtc;
13359 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
13360
13361 plane->fb = fb;
9dc806fc
MR
13362 crtc->x = src->x1 >> 16;
13363 crtc->y = src->y1 >> 16;
ccc759dc 13364
ccc759dc 13365 if (intel_crtc->active) {
27321ae8 13366 if (state->visible)
ccc759dc
GP
13367 /* FIXME: kill this fastboot hack */
13368 intel_update_pipe_size(intel_crtc);
465c120c 13369
27321ae8
ML
13370 dev_priv->display.update_primary_plane(crtc, plane->fb,
13371 crtc->x, crtc->y);
ccc759dc 13372 }
465c120c
MR
13373}
13374
a8ad0d8e
ML
13375static void
13376intel_disable_primary_plane(struct drm_plane *plane,
13377 struct drm_crtc *crtc,
13378 bool force)
13379{
13380 struct drm_device *dev = plane->dev;
13381 struct drm_i915_private *dev_priv = dev->dev_private;
13382
a8ad0d8e
ML
13383 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13384}
13385
32b7eeec 13386static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 13387{
32b7eeec 13388 struct drm_device *dev = crtc->dev;
140fd38d 13389 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 13390 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb
MR
13391 struct intel_plane *intel_plane;
13392 struct drm_plane *p;
13393 unsigned fb_bits = 0;
13394
13395 /* Track fb's for any planes being disabled */
13396 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13397 intel_plane = to_intel_plane(p);
13398
13399 if (intel_crtc->atomic.disabled_planes &
13400 (1 << drm_plane_index(p))) {
13401 switch (p->type) {
13402 case DRM_PLANE_TYPE_PRIMARY:
13403 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13404 break;
13405 case DRM_PLANE_TYPE_CURSOR:
13406 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13407 break;
13408 case DRM_PLANE_TYPE_OVERLAY:
13409 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13410 break;
13411 }
3c692a41 13412
ea2c67bb
MR
13413 mutex_lock(&dev->struct_mutex);
13414 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13415 mutex_unlock(&dev->struct_mutex);
13416 }
13417 }
3c692a41 13418
32b7eeec
MR
13419 if (intel_crtc->atomic.wait_for_flips)
13420 intel_crtc_wait_for_pending_flips(crtc);
3c692a41 13421
32b7eeec
MR
13422 if (intel_crtc->atomic.disable_fbc)
13423 intel_fbc_disable(dev);
3c692a41 13424
32b7eeec
MR
13425 if (intel_crtc->atomic.pre_disable_primary)
13426 intel_pre_disable_primary(crtc);
3c692a41 13427
32b7eeec
MR
13428 if (intel_crtc->atomic.update_wm)
13429 intel_update_watermarks(crtc);
3c692a41 13430
32b7eeec 13431 intel_runtime_pm_get(dev_priv);
3c692a41 13432
c34c9ee4
MR
13433 /* Perform vblank evasion around commit operation */
13434 if (intel_crtc->active)
13435 intel_crtc->atomic.evade =
13436 intel_pipe_update_start(intel_crtc,
13437 &intel_crtc->atomic.start_vbl_count);
32b7eeec
MR
13438}
13439
13440static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13441{
13442 struct drm_device *dev = crtc->dev;
13443 struct drm_i915_private *dev_priv = dev->dev_private;
13444 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13445 struct drm_plane *p;
13446
c34c9ee4
MR
13447 if (intel_crtc->atomic.evade)
13448 intel_pipe_update_end(intel_crtc,
13449 intel_crtc->atomic.start_vbl_count);
3c692a41 13450
140fd38d 13451 intel_runtime_pm_put(dev_priv);
3c692a41 13452
32b7eeec
MR
13453 if (intel_crtc->atomic.wait_vblank)
13454 intel_wait_for_vblank(dev, intel_crtc->pipe);
13455
13456 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13457
13458 if (intel_crtc->atomic.update_fbc) {
ccc759dc 13459 mutex_lock(&dev->struct_mutex);
7ff0ebcc 13460 intel_fbc_update(dev);
ccc759dc 13461 mutex_unlock(&dev->struct_mutex);
38f3ce3a 13462 }
3c692a41 13463
32b7eeec
MR
13464 if (intel_crtc->atomic.post_enable_primary)
13465 intel_post_enable_primary(crtc);
3c692a41 13466
32b7eeec
MR
13467 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13468 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13469 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13470 false, false);
13471
13472 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
3c692a41
GP
13473}
13474
cf4c7c12 13475/**
4a3b8769
MR
13476 * intel_plane_destroy - destroy a plane
13477 * @plane: plane to destroy
cf4c7c12 13478 *
4a3b8769
MR
13479 * Common destruction function for all types of planes (primary, cursor,
13480 * sprite).
cf4c7c12 13481 */
4a3b8769 13482void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13483{
13484 struct intel_plane *intel_plane = to_intel_plane(plane);
13485 drm_plane_cleanup(plane);
13486 kfree(intel_plane);
13487}
13488
65a3fea0 13489const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13490 .update_plane = drm_atomic_helper_update_plane,
13491 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13492 .destroy = intel_plane_destroy,
c196e1d6 13493 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13494 .atomic_get_property = intel_plane_atomic_get_property,
13495 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13496 .atomic_duplicate_state = intel_plane_duplicate_state,
13497 .atomic_destroy_state = intel_plane_destroy_state,
13498
465c120c
MR
13499};
13500
13501static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13502 int pipe)
13503{
13504 struct intel_plane *primary;
8e7d688b 13505 struct intel_plane_state *state;
465c120c
MR
13506 const uint32_t *intel_primary_formats;
13507 int num_formats;
13508
13509 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13510 if (primary == NULL)
13511 return NULL;
13512
8e7d688b
MR
13513 state = intel_create_plane_state(&primary->base);
13514 if (!state) {
ea2c67bb
MR
13515 kfree(primary);
13516 return NULL;
13517 }
8e7d688b 13518 primary->base.state = &state->base;
ea2c67bb 13519
465c120c
MR
13520 primary->can_scale = false;
13521 primary->max_downscale = 1;
6156a456
CK
13522 if (INTEL_INFO(dev)->gen >= 9) {
13523 primary->can_scale = true;
13524 }
549e2bfb 13525 state->scaler_id = -1;
465c120c
MR
13526 primary->pipe = pipe;
13527 primary->plane = pipe;
c59cb179
MR
13528 primary->check_plane = intel_check_primary_plane;
13529 primary->commit_plane = intel_commit_primary_plane;
a8ad0d8e 13530 primary->disable_plane = intel_disable_primary_plane;
08e221fb 13531 primary->ckey.flags = I915_SET_COLORKEY_NONE;
465c120c
MR
13532 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13533 primary->plane = !pipe;
13534
13535 if (INTEL_INFO(dev)->gen <= 3) {
13536 intel_primary_formats = intel_primary_formats_gen2;
13537 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
13538 } else {
13539 intel_primary_formats = intel_primary_formats_gen4;
13540 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
13541 }
13542
13543 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13544 &intel_plane_funcs,
465c120c
MR
13545 intel_primary_formats, num_formats,
13546 DRM_PLANE_TYPE_PRIMARY);
48404c1e 13547
3b7a5119
SJ
13548 if (INTEL_INFO(dev)->gen >= 4)
13549 intel_create_rotation_property(dev, primary);
48404c1e 13550
ea2c67bb
MR
13551 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13552
465c120c
MR
13553 return &primary->base;
13554}
13555
3b7a5119
SJ
13556void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13557{
13558 if (!dev->mode_config.rotation_property) {
13559 unsigned long flags = BIT(DRM_ROTATE_0) |
13560 BIT(DRM_ROTATE_180);
13561
13562 if (INTEL_INFO(dev)->gen >= 9)
13563 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13564
13565 dev->mode_config.rotation_property =
13566 drm_mode_create_rotation_property(dev, flags);
13567 }
13568 if (dev->mode_config.rotation_property)
13569 drm_object_attach_property(&plane->base.base,
13570 dev->mode_config.rotation_property,
13571 plane->base.state->rotation);
13572}
13573
3d7d6510 13574static int
852e787c
GP
13575intel_check_cursor_plane(struct drm_plane *plane,
13576 struct intel_plane_state *state)
3d7d6510 13577{
2b875c22 13578 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13579 struct drm_device *dev = plane->dev;
2b875c22 13580 struct drm_framebuffer *fb = state->base.fb;
852e787c
GP
13581 struct drm_rect *dest = &state->dst;
13582 struct drm_rect *src = &state->src;
13583 const struct drm_rect *clip = &state->clip;
757f9a3e 13584 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ea2c67bb 13585 struct intel_crtc *intel_crtc;
757f9a3e
GP
13586 unsigned stride;
13587 int ret;
3d7d6510 13588
ea2c67bb
MR
13589 crtc = crtc ? crtc : plane->crtc;
13590 intel_crtc = to_intel_crtc(crtc);
13591
757f9a3e 13592 ret = drm_plane_helper_check_update(plane, crtc, fb,
852e787c 13593 src, dest, clip,
3d7d6510
MR
13594 DRM_PLANE_HELPER_NO_SCALING,
13595 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13596 true, true, &state->visible);
757f9a3e
GP
13597 if (ret)
13598 return ret;
13599
13600
13601 /* if we want to turn off the cursor ignore width and height */
13602 if (!obj)
32b7eeec 13603 goto finish;
757f9a3e 13604
757f9a3e 13605 /* Check for which cursor types we support */
ea2c67bb
MR
13606 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13607 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13608 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13609 return -EINVAL;
13610 }
13611
ea2c67bb
MR
13612 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13613 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13614 DRM_DEBUG_KMS("buffer is too small\n");
13615 return -ENOMEM;
13616 }
13617
3a656b54 13618 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e
GP
13619 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13620 ret = -EINVAL;
13621 }
757f9a3e 13622
32b7eeec
MR
13623finish:
13624 if (intel_crtc->active) {
3749f463 13625 if (plane->state->crtc_w != state->base.crtc_w)
32b7eeec
MR
13626 intel_crtc->atomic.update_wm = true;
13627
13628 intel_crtc->atomic.fb_bits |=
13629 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13630 }
13631
757f9a3e 13632 return ret;
852e787c 13633}
3d7d6510 13634
a8ad0d8e
ML
13635static void
13636intel_disable_cursor_plane(struct drm_plane *plane,
13637 struct drm_crtc *crtc,
13638 bool force)
13639{
13640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13641
13642 if (!force) {
13643 plane->fb = NULL;
13644 intel_crtc->cursor_bo = NULL;
13645 intel_crtc->cursor_addr = 0;
13646 }
13647
13648 intel_crtc_update_cursor(crtc, false);
13649}
13650
f4a2cf29 13651static void
852e787c
GP
13652intel_commit_cursor_plane(struct drm_plane *plane,
13653 struct intel_plane_state *state)
13654{
2b875c22 13655 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
13656 struct drm_device *dev = plane->dev;
13657 struct intel_crtc *intel_crtc;
2b875c22 13658 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13659 uint32_t addr;
852e787c 13660
ea2c67bb
MR
13661 crtc = crtc ? crtc : plane->crtc;
13662 intel_crtc = to_intel_crtc(crtc);
13663
2b875c22 13664 plane->fb = state->base.fb;
ea2c67bb
MR
13665 crtc->cursor_x = state->base.crtc_x;
13666 crtc->cursor_y = state->base.crtc_y;
13667
a912f12f
GP
13668 if (intel_crtc->cursor_bo == obj)
13669 goto update;
4ed91096 13670
f4a2cf29 13671 if (!obj)
a912f12f 13672 addr = 0;
f4a2cf29 13673 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 13674 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 13675 else
a912f12f 13676 addr = obj->phys_handle->busaddr;
852e787c 13677
a912f12f
GP
13678 intel_crtc->cursor_addr = addr;
13679 intel_crtc->cursor_bo = obj;
13680update:
852e787c 13681
32b7eeec 13682 if (intel_crtc->active)
a912f12f 13683 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
13684}
13685
3d7d6510
MR
13686static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13687 int pipe)
13688{
13689 struct intel_plane *cursor;
8e7d688b 13690 struct intel_plane_state *state;
3d7d6510
MR
13691
13692 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13693 if (cursor == NULL)
13694 return NULL;
13695
8e7d688b
MR
13696 state = intel_create_plane_state(&cursor->base);
13697 if (!state) {
ea2c67bb
MR
13698 kfree(cursor);
13699 return NULL;
13700 }
8e7d688b 13701 cursor->base.state = &state->base;
ea2c67bb 13702
3d7d6510
MR
13703 cursor->can_scale = false;
13704 cursor->max_downscale = 1;
13705 cursor->pipe = pipe;
13706 cursor->plane = pipe;
549e2bfb 13707 state->scaler_id = -1;
c59cb179
MR
13708 cursor->check_plane = intel_check_cursor_plane;
13709 cursor->commit_plane = intel_commit_cursor_plane;
a8ad0d8e 13710 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
13711
13712 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 13713 &intel_plane_funcs,
3d7d6510
MR
13714 intel_cursor_formats,
13715 ARRAY_SIZE(intel_cursor_formats),
13716 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
13717
13718 if (INTEL_INFO(dev)->gen >= 4) {
13719 if (!dev->mode_config.rotation_property)
13720 dev->mode_config.rotation_property =
13721 drm_mode_create_rotation_property(dev,
13722 BIT(DRM_ROTATE_0) |
13723 BIT(DRM_ROTATE_180));
13724 if (dev->mode_config.rotation_property)
13725 drm_object_attach_property(&cursor->base.base,
13726 dev->mode_config.rotation_property,
8e7d688b 13727 state->base.rotation);
4398ad45
VS
13728 }
13729
ea2c67bb
MR
13730 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13731
3d7d6510
MR
13732 return &cursor->base;
13733}
13734
549e2bfb
CK
13735static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13736 struct intel_crtc_state *crtc_state)
13737{
13738 int i;
13739 struct intel_scaler *intel_scaler;
13740 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13741
13742 for (i = 0; i < intel_crtc->num_scalers; i++) {
13743 intel_scaler = &scaler_state->scalers[i];
13744 intel_scaler->in_use = 0;
13745 intel_scaler->id = i;
13746
13747 intel_scaler->mode = PS_SCALER_MODE_DYN;
13748 }
13749
13750 scaler_state->scaler_id = -1;
13751}
13752
b358d0a6 13753static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 13754{
fbee40df 13755 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 13756 struct intel_crtc *intel_crtc;
f5de6e07 13757 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
13758 struct drm_plane *primary = NULL;
13759 struct drm_plane *cursor = NULL;
465c120c 13760 int i, ret;
79e53945 13761
955382f3 13762 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
13763 if (intel_crtc == NULL)
13764 return;
13765
f5de6e07
ACO
13766 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13767 if (!crtc_state)
13768 goto fail;
13769 intel_crtc_set_state(intel_crtc, crtc_state);
07878248 13770 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13771
549e2bfb
CK
13772 /* initialize shared scalers */
13773 if (INTEL_INFO(dev)->gen >= 9) {
13774 if (pipe == PIPE_C)
13775 intel_crtc->num_scalers = 1;
13776 else
13777 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13778
13779 skl_init_scalers(dev, intel_crtc, crtc_state);
13780 }
13781
465c120c 13782 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
13783 if (!primary)
13784 goto fail;
13785
13786 cursor = intel_cursor_plane_create(dev, pipe);
13787 if (!cursor)
13788 goto fail;
13789
465c120c 13790 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
13791 cursor, &intel_crtc_funcs);
13792 if (ret)
13793 goto fail;
79e53945
JB
13794
13795 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
13796 for (i = 0; i < 256; i++) {
13797 intel_crtc->lut_r[i] = i;
13798 intel_crtc->lut_g[i] = i;
13799 intel_crtc->lut_b[i] = i;
13800 }
13801
1f1c2e24
VS
13802 /*
13803 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 13804 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 13805 */
80824003
JB
13806 intel_crtc->pipe = pipe;
13807 intel_crtc->plane = pipe;
3a77c4c4 13808 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 13809 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 13810 intel_crtc->plane = !pipe;
80824003
JB
13811 }
13812
4b0e333e
CW
13813 intel_crtc->cursor_base = ~0;
13814 intel_crtc->cursor_cntl = ~0;
dc41c154 13815 intel_crtc->cursor_size = ~0;
8d7849db 13816
22fd0fab
JB
13817 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13818 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13819 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13820 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13821
9362c7c5
ACO
13822 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13823
79e53945 13824 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
13825
13826 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
13827 return;
13828
13829fail:
13830 if (primary)
13831 drm_plane_cleanup(primary);
13832 if (cursor)
13833 drm_plane_cleanup(cursor);
f5de6e07 13834 kfree(crtc_state);
3d7d6510 13835 kfree(intel_crtc);
79e53945
JB
13836}
13837
752aa88a
JB
13838enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13839{
13840 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 13841 struct drm_device *dev = connector->base.dev;
752aa88a 13842
51fd371b 13843 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 13844
d3babd3f 13845 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
13846 return INVALID_PIPE;
13847
13848 return to_intel_crtc(encoder->crtc)->pipe;
13849}
13850
08d7b3d1 13851int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 13852 struct drm_file *file)
08d7b3d1 13853{
08d7b3d1 13854 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 13855 struct drm_crtc *drmmode_crtc;
c05422d5 13856 struct intel_crtc *crtc;
08d7b3d1 13857
7707e653 13858 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 13859
7707e653 13860 if (!drmmode_crtc) {
08d7b3d1 13861 DRM_ERROR("no such CRTC id\n");
3f2c2057 13862 return -ENOENT;
08d7b3d1
CW
13863 }
13864
7707e653 13865 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 13866 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 13867
c05422d5 13868 return 0;
08d7b3d1
CW
13869}
13870
66a9278e 13871static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 13872{
66a9278e
DV
13873 struct drm_device *dev = encoder->base.dev;
13874 struct intel_encoder *source_encoder;
79e53945 13875 int index_mask = 0;
79e53945
JB
13876 int entry = 0;
13877
b2784e15 13878 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 13879 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
13880 index_mask |= (1 << entry);
13881
79e53945
JB
13882 entry++;
13883 }
4ef69c7a 13884
79e53945
JB
13885 return index_mask;
13886}
13887
4d302442
CW
13888static bool has_edp_a(struct drm_device *dev)
13889{
13890 struct drm_i915_private *dev_priv = dev->dev_private;
13891
13892 if (!IS_MOBILE(dev))
13893 return false;
13894
13895 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13896 return false;
13897
e3589908 13898 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
13899 return false;
13900
13901 return true;
13902}
13903
84b4e042
JB
13904static bool intel_crt_present(struct drm_device *dev)
13905{
13906 struct drm_i915_private *dev_priv = dev->dev_private;
13907
884497ed
DL
13908 if (INTEL_INFO(dev)->gen >= 9)
13909 return false;
13910
cf404ce4 13911 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
13912 return false;
13913
13914 if (IS_CHERRYVIEW(dev))
13915 return false;
13916
13917 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13918 return false;
13919
13920 return true;
13921}
13922
79e53945
JB
13923static void intel_setup_outputs(struct drm_device *dev)
13924{
725e30ad 13925 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 13926 struct intel_encoder *encoder;
cb0953d7 13927 bool dpd_is_edp = false;
79e53945 13928
c9093354 13929 intel_lvds_init(dev);
79e53945 13930
84b4e042 13931 if (intel_crt_present(dev))
79935fca 13932 intel_crt_init(dev);
cb0953d7 13933
c776eb2e
VK
13934 if (IS_BROXTON(dev)) {
13935 /*
13936 * FIXME: Broxton doesn't support port detection via the
13937 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13938 * detect the ports.
13939 */
13940 intel_ddi_init(dev, PORT_A);
13941 intel_ddi_init(dev, PORT_B);
13942 intel_ddi_init(dev, PORT_C);
13943 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
13944 int found;
13945
de31facd
JB
13946 /*
13947 * Haswell uses DDI functions to detect digital outputs.
13948 * On SKL pre-D0 the strap isn't connected, so we assume
13949 * it's there.
13950 */
0e72a5b5 13951 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
de31facd
JB
13952 /* WaIgnoreDDIAStrap: skl */
13953 if (found ||
13954 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
0e72a5b5
ED
13955 intel_ddi_init(dev, PORT_A);
13956
13957 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13958 * register */
13959 found = I915_READ(SFUSE_STRAP);
13960
13961 if (found & SFUSE_STRAP_DDIB_DETECTED)
13962 intel_ddi_init(dev, PORT_B);
13963 if (found & SFUSE_STRAP_DDIC_DETECTED)
13964 intel_ddi_init(dev, PORT_C);
13965 if (found & SFUSE_STRAP_DDID_DETECTED)
13966 intel_ddi_init(dev, PORT_D);
13967 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 13968 int found;
5d8a7752 13969 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
13970
13971 if (has_edp_a(dev))
13972 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 13973
dc0fa718 13974 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 13975 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 13976 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 13977 if (!found)
e2debe91 13978 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 13979 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 13980 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
13981 }
13982
dc0fa718 13983 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 13984 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 13985
dc0fa718 13986 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 13987 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 13988
5eb08b69 13989 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 13990 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 13991
270b3042 13992 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 13993 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 13994 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
13995 /*
13996 * The DP_DETECTED bit is the latched state of the DDC
13997 * SDA pin at boot. However since eDP doesn't require DDC
13998 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13999 * eDP ports may have been muxed to an alternate function.
14000 * Thus we can't rely on the DP_DETECTED bit alone to detect
14001 * eDP ports. Consult the VBT as well as DP_DETECTED to
14002 * detect eDP ports.
14003 */
d2182a66
VS
14004 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14005 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
14006 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14007 PORT_B);
e17ac6db
VS
14008 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14009 intel_dp_is_edp(dev, PORT_B))
14010 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 14011
d2182a66
VS
14012 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14013 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
14014 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14015 PORT_C);
e17ac6db
VS
14016 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14017 intel_dp_is_edp(dev, PORT_C))
14018 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 14019
9418c1f1 14020 if (IS_CHERRYVIEW(dev)) {
e17ac6db 14021 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
14022 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14023 PORT_D);
e17ac6db
VS
14024 /* eDP not supported on port D, so don't check VBT */
14025 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14026 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
14027 }
14028
3cfca973 14029 intel_dsi_init(dev);
103a196f 14030 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 14031 bool found = false;
7d57382e 14032
e2debe91 14033 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14034 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 14035 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
14036 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14037 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14038 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14039 }
27185ae1 14040
e7281eab 14041 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14042 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14043 }
13520b05
KH
14044
14045 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14046
e2debe91 14047 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14048 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 14049 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 14050 }
27185ae1 14051
e2debe91 14052 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14053
b01f2c3a
JB
14054 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14055 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14056 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14057 }
e7281eab 14058 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14059 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14060 }
27185ae1 14061
b01f2c3a 14062 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 14063 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14064 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14065 } else if (IS_GEN2(dev))
79e53945
JB
14066 intel_dvo_init(dev);
14067
103a196f 14068 if (SUPPORTS_TV(dev))
79e53945
JB
14069 intel_tv_init(dev);
14070
0bc12bcb 14071 intel_psr_init(dev);
7c8f8a70 14072
b2784e15 14073 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14074 encoder->base.possible_crtcs = encoder->crtc_mask;
14075 encoder->base.possible_clones =
66a9278e 14076 intel_encoder_clones(encoder);
79e53945 14077 }
47356eb6 14078
dde86e2d 14079 intel_init_pch_refclk(dev);
270b3042
DV
14080
14081 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14082}
14083
14084static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14085{
60a5ca01 14086 struct drm_device *dev = fb->dev;
79e53945 14087 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14088
ef2d633e 14089 drm_framebuffer_cleanup(fb);
60a5ca01 14090 mutex_lock(&dev->struct_mutex);
ef2d633e 14091 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14092 drm_gem_object_unreference(&intel_fb->obj->base);
14093 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14094 kfree(intel_fb);
14095}
14096
14097static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14098 struct drm_file *file,
79e53945
JB
14099 unsigned int *handle)
14100{
14101 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14102 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14103
05394f39 14104 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14105}
14106
14107static const struct drm_framebuffer_funcs intel_fb_funcs = {
14108 .destroy = intel_user_framebuffer_destroy,
14109 .create_handle = intel_user_framebuffer_create_handle,
14110};
14111
b321803d
DL
14112static
14113u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14114 uint32_t pixel_format)
14115{
14116 u32 gen = INTEL_INFO(dev)->gen;
14117
14118 if (gen >= 9) {
14119 /* "The stride in bytes must not exceed the of the size of 8K
14120 * pixels and 32K bytes."
14121 */
14122 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14123 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14124 return 32*1024;
14125 } else if (gen >= 4) {
14126 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14127 return 16*1024;
14128 else
14129 return 32*1024;
14130 } else if (gen >= 3) {
14131 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14132 return 8*1024;
14133 else
14134 return 16*1024;
14135 } else {
14136 /* XXX DSPC is limited to 4k tiled */
14137 return 8*1024;
14138 }
14139}
14140
b5ea642a
DV
14141static int intel_framebuffer_init(struct drm_device *dev,
14142 struct intel_framebuffer *intel_fb,
14143 struct drm_mode_fb_cmd2 *mode_cmd,
14144 struct drm_i915_gem_object *obj)
79e53945 14145{
6761dd31 14146 unsigned int aligned_height;
79e53945 14147 int ret;
b321803d 14148 u32 pitch_limit, stride_alignment;
79e53945 14149
dd4916c5
DV
14150 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14151
2a80eada
DV
14152 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14153 /* Enforce that fb modifier and tiling mode match, but only for
14154 * X-tiled. This is needed for FBC. */
14155 if (!!(obj->tiling_mode == I915_TILING_X) !=
14156 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14157 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14158 return -EINVAL;
14159 }
14160 } else {
14161 if (obj->tiling_mode == I915_TILING_X)
14162 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14163 else if (obj->tiling_mode == I915_TILING_Y) {
14164 DRM_DEBUG("No Y tiling for legacy addfb\n");
14165 return -EINVAL;
14166 }
14167 }
14168
9a8f0a12
TU
14169 /* Passed in modifier sanity checking. */
14170 switch (mode_cmd->modifier[0]) {
14171 case I915_FORMAT_MOD_Y_TILED:
14172 case I915_FORMAT_MOD_Yf_TILED:
14173 if (INTEL_INFO(dev)->gen < 9) {
14174 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14175 mode_cmd->modifier[0]);
14176 return -EINVAL;
14177 }
14178 case DRM_FORMAT_MOD_NONE:
14179 case I915_FORMAT_MOD_X_TILED:
14180 break;
14181 default:
c0f40428
JB
14182 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14183 mode_cmd->modifier[0]);
57cd6508 14184 return -EINVAL;
c16ed4be 14185 }
57cd6508 14186
b321803d
DL
14187 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14188 mode_cmd->pixel_format);
14189 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14190 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14191 mode_cmd->pitches[0], stride_alignment);
57cd6508 14192 return -EINVAL;
c16ed4be 14193 }
57cd6508 14194
b321803d
DL
14195 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14196 mode_cmd->pixel_format);
a35cdaa0 14197 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14198 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14199 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14200 "tiled" : "linear",
a35cdaa0 14201 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14202 return -EINVAL;
c16ed4be 14203 }
5d7bd705 14204
2a80eada 14205 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14206 mode_cmd->pitches[0] != obj->stride) {
14207 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14208 mode_cmd->pitches[0], obj->stride);
5d7bd705 14209 return -EINVAL;
c16ed4be 14210 }
5d7bd705 14211
57779d06 14212 /* Reject formats not supported by any plane early. */
308e5bcb 14213 switch (mode_cmd->pixel_format) {
57779d06 14214 case DRM_FORMAT_C8:
04b3924d
VS
14215 case DRM_FORMAT_RGB565:
14216 case DRM_FORMAT_XRGB8888:
14217 case DRM_FORMAT_ARGB8888:
57779d06
VS
14218 break;
14219 case DRM_FORMAT_XRGB1555:
14220 case DRM_FORMAT_ARGB1555:
c16ed4be 14221 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14222 DRM_DEBUG("unsupported pixel format: %s\n",
14223 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14224 return -EINVAL;
c16ed4be 14225 }
57779d06
VS
14226 break;
14227 case DRM_FORMAT_XBGR8888:
14228 case DRM_FORMAT_ABGR8888:
04b3924d
VS
14229 case DRM_FORMAT_XRGB2101010:
14230 case DRM_FORMAT_ARGB2101010:
57779d06
VS
14231 case DRM_FORMAT_XBGR2101010:
14232 case DRM_FORMAT_ABGR2101010:
c16ed4be 14233 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14234 DRM_DEBUG("unsupported pixel format: %s\n",
14235 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14236 return -EINVAL;
c16ed4be 14237 }
b5626747 14238 break;
04b3924d
VS
14239 case DRM_FORMAT_YUYV:
14240 case DRM_FORMAT_UYVY:
14241 case DRM_FORMAT_YVYU:
14242 case DRM_FORMAT_VYUY:
c16ed4be 14243 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14244 DRM_DEBUG("unsupported pixel format: %s\n",
14245 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14246 return -EINVAL;
c16ed4be 14247 }
57cd6508
CW
14248 break;
14249 default:
4ee62c76
VS
14250 DRM_DEBUG("unsupported pixel format: %s\n",
14251 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14252 return -EINVAL;
14253 }
14254
90f9a336
VS
14255 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14256 if (mode_cmd->offsets[0] != 0)
14257 return -EINVAL;
14258
ec2c981e 14259 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14260 mode_cmd->pixel_format,
14261 mode_cmd->modifier[0]);
53155c0a
DV
14262 /* FIXME drm helper for size checks (especially planar formats)? */
14263 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14264 return -EINVAL;
14265
c7d73f6a
DV
14266 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14267 intel_fb->obj = obj;
80075d49 14268 intel_fb->obj->framebuffer_references++;
c7d73f6a 14269
79e53945
JB
14270 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14271 if (ret) {
14272 DRM_ERROR("framebuffer init failed %d\n", ret);
14273 return ret;
14274 }
14275
79e53945
JB
14276 return 0;
14277}
14278
79e53945
JB
14279static struct drm_framebuffer *
14280intel_user_framebuffer_create(struct drm_device *dev,
14281 struct drm_file *filp,
308e5bcb 14282 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14283{
05394f39 14284 struct drm_i915_gem_object *obj;
79e53945 14285
308e5bcb
JB
14286 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14287 mode_cmd->handles[0]));
c8725226 14288 if (&obj->base == NULL)
cce13ff7 14289 return ERR_PTR(-ENOENT);
79e53945 14290
d2dff872 14291 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
14292}
14293
4520f53a 14294#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 14295static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14296{
14297}
14298#endif
14299
79e53945 14300static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14301 .fb_create = intel_user_framebuffer_create,
0632fef6 14302 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14303 .atomic_check = intel_atomic_check,
14304 .atomic_commit = intel_atomic_commit,
79e53945
JB
14305};
14306
e70236a8
JB
14307/* Set up chip specific display functions */
14308static void intel_init_display(struct drm_device *dev)
14309{
14310 struct drm_i915_private *dev_priv = dev->dev_private;
14311
ee9300bb
DV
14312 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14313 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14314 else if (IS_CHERRYVIEW(dev))
14315 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14316 else if (IS_VALLEYVIEW(dev))
14317 dev_priv->display.find_dpll = vlv_find_best_dpll;
14318 else if (IS_PINEVIEW(dev))
14319 dev_priv->display.find_dpll = pnv_find_best_dpll;
14320 else
14321 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14322
bc8d7dff
DL
14323 if (INTEL_INFO(dev)->gen >= 9) {
14324 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14325 dev_priv->display.get_initial_plane_config =
14326 skylake_get_initial_plane_config;
bc8d7dff
DL
14327 dev_priv->display.crtc_compute_clock =
14328 haswell_crtc_compute_clock;
14329 dev_priv->display.crtc_enable = haswell_crtc_enable;
14330 dev_priv->display.crtc_disable = haswell_crtc_disable;
14331 dev_priv->display.off = ironlake_crtc_off;
14332 dev_priv->display.update_primary_plane =
14333 skylake_update_primary_plane;
14334 } else if (HAS_DDI(dev)) {
0e8ffe1b 14335 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14336 dev_priv->display.get_initial_plane_config =
14337 ironlake_get_initial_plane_config;
797d0259
ACO
14338 dev_priv->display.crtc_compute_clock =
14339 haswell_crtc_compute_clock;
4f771f10
PZ
14340 dev_priv->display.crtc_enable = haswell_crtc_enable;
14341 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 14342 dev_priv->display.off = ironlake_crtc_off;
bc8d7dff
DL
14343 dev_priv->display.update_primary_plane =
14344 ironlake_update_primary_plane;
09b4ddf9 14345 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14346 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14347 dev_priv->display.get_initial_plane_config =
14348 ironlake_get_initial_plane_config;
3fb37703
ACO
14349 dev_priv->display.crtc_compute_clock =
14350 ironlake_crtc_compute_clock;
76e5a89c
DV
14351 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14352 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 14353 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
14354 dev_priv->display.update_primary_plane =
14355 ironlake_update_primary_plane;
89b667f8
JB
14356 } else if (IS_VALLEYVIEW(dev)) {
14357 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14358 dev_priv->display.get_initial_plane_config =
14359 i9xx_get_initial_plane_config;
d6dfee7a 14360 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14361 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14362 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14363 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
14364 dev_priv->display.update_primary_plane =
14365 i9xx_update_primary_plane;
f564048e 14366 } else {
0e8ffe1b 14367 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14368 dev_priv->display.get_initial_plane_config =
14369 i9xx_get_initial_plane_config;
d6dfee7a 14370 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14371 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14372 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 14373 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
14374 dev_priv->display.update_primary_plane =
14375 i9xx_update_primary_plane;
f564048e 14376 }
e70236a8 14377
e70236a8 14378 /* Returns the core display clock speed */
1652d19e
VS
14379 if (IS_SKYLAKE(dev))
14380 dev_priv->display.get_display_clock_speed =
14381 skylake_get_display_clock_speed;
14382 else if (IS_BROADWELL(dev))
14383 dev_priv->display.get_display_clock_speed =
14384 broadwell_get_display_clock_speed;
14385 else if (IS_HASWELL(dev))
14386 dev_priv->display.get_display_clock_speed =
14387 haswell_get_display_clock_speed;
14388 else if (IS_VALLEYVIEW(dev))
25eb05fc
JB
14389 dev_priv->display.get_display_clock_speed =
14390 valleyview_get_display_clock_speed;
b37a6434
VS
14391 else if (IS_GEN5(dev))
14392 dev_priv->display.get_display_clock_speed =
14393 ilk_get_display_clock_speed;
a7c66cd8
VS
14394 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14395 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
14396 dev_priv->display.get_display_clock_speed =
14397 i945_get_display_clock_speed;
14398 else if (IS_I915G(dev))
14399 dev_priv->display.get_display_clock_speed =
14400 i915_get_display_clock_speed;
257a7ffc 14401 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14402 dev_priv->display.get_display_clock_speed =
14403 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
14404 else if (IS_PINEVIEW(dev))
14405 dev_priv->display.get_display_clock_speed =
14406 pnv_get_display_clock_speed;
e70236a8
JB
14407 else if (IS_I915GM(dev))
14408 dev_priv->display.get_display_clock_speed =
14409 i915gm_get_display_clock_speed;
14410 else if (IS_I865G(dev))
14411 dev_priv->display.get_display_clock_speed =
14412 i865_get_display_clock_speed;
f0f8a9ce 14413 else if (IS_I85X(dev))
e70236a8
JB
14414 dev_priv->display.get_display_clock_speed =
14415 i855_get_display_clock_speed;
14416 else /* 852, 830 */
14417 dev_priv->display.get_display_clock_speed =
14418 i830_get_display_clock_speed;
14419
7c10a2b5 14420 if (IS_GEN5(dev)) {
3bb11b53 14421 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14422 } else if (IS_GEN6(dev)) {
14423 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14424 } else if (IS_IVYBRIDGE(dev)) {
14425 /* FIXME: detect B0+ stepping and use auto training */
14426 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14427 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14428 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
30a970c6
JB
14429 } else if (IS_VALLEYVIEW(dev)) {
14430 dev_priv->display.modeset_global_resources =
14431 valleyview_modeset_global_resources;
f8437dd1
VK
14432 } else if (IS_BROXTON(dev)) {
14433 dev_priv->display.modeset_global_resources =
14434 broxton_modeset_global_resources;
e70236a8 14435 }
8c9f3aaf 14436
8c9f3aaf
JB
14437 switch (INTEL_INFO(dev)->gen) {
14438 case 2:
14439 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14440 break;
14441
14442 case 3:
14443 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14444 break;
14445
14446 case 4:
14447 case 5:
14448 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14449 break;
14450
14451 case 6:
14452 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14453 break;
7c9017e5 14454 case 7:
4e0bbc31 14455 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14456 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14457 break;
830c81db 14458 case 9:
ba343e02
TU
14459 /* Drop through - unsupported since execlist only. */
14460 default:
14461 /* Default just returns -ENODEV to indicate unsupported */
14462 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14463 }
7bd688cd
JN
14464
14465 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
14466
14467 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
14468}
14469
b690e96c
JB
14470/*
14471 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14472 * resume, or other times. This quirk makes sure that's the case for
14473 * affected systems.
14474 */
0206e353 14475static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14476{
14477 struct drm_i915_private *dev_priv = dev->dev_private;
14478
14479 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14480 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14481}
14482
b6b5d049
VS
14483static void quirk_pipeb_force(struct drm_device *dev)
14484{
14485 struct drm_i915_private *dev_priv = dev->dev_private;
14486
14487 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14488 DRM_INFO("applying pipe b force quirk\n");
14489}
14490
435793df
KP
14491/*
14492 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14493 */
14494static void quirk_ssc_force_disable(struct drm_device *dev)
14495{
14496 struct drm_i915_private *dev_priv = dev->dev_private;
14497 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14498 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14499}
14500
4dca20ef 14501/*
5a15ab5b
CE
14502 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14503 * brightness value
4dca20ef
CE
14504 */
14505static void quirk_invert_brightness(struct drm_device *dev)
14506{
14507 struct drm_i915_private *dev_priv = dev->dev_private;
14508 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14509 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14510}
14511
9c72cc6f
SD
14512/* Some VBT's incorrectly indicate no backlight is present */
14513static void quirk_backlight_present(struct drm_device *dev)
14514{
14515 struct drm_i915_private *dev_priv = dev->dev_private;
14516 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14517 DRM_INFO("applying backlight present quirk\n");
14518}
14519
b690e96c
JB
14520struct intel_quirk {
14521 int device;
14522 int subsystem_vendor;
14523 int subsystem_device;
14524 void (*hook)(struct drm_device *dev);
14525};
14526
5f85f176
EE
14527/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14528struct intel_dmi_quirk {
14529 void (*hook)(struct drm_device *dev);
14530 const struct dmi_system_id (*dmi_id_list)[];
14531};
14532
14533static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14534{
14535 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14536 return 1;
14537}
14538
14539static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14540 {
14541 .dmi_id_list = &(const struct dmi_system_id[]) {
14542 {
14543 .callback = intel_dmi_reverse_brightness,
14544 .ident = "NCR Corporation",
14545 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14546 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14547 },
14548 },
14549 { } /* terminating entry */
14550 },
14551 .hook = quirk_invert_brightness,
14552 },
14553};
14554
c43b5634 14555static struct intel_quirk intel_quirks[] = {
b690e96c 14556 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 14557 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 14558
b690e96c
JB
14559 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14560 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14561
b690e96c
JB
14562 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14563 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14564
5f080c0f
VS
14565 /* 830 needs to leave pipe A & dpll A up */
14566 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14567
b6b5d049
VS
14568 /* 830 needs to leave pipe B & dpll B up */
14569 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14570
435793df
KP
14571 /* Lenovo U160 cannot use SSC on LVDS */
14572 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14573
14574 /* Sony Vaio Y cannot use SSC on LVDS */
14575 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14576
be505f64
AH
14577 /* Acer Aspire 5734Z must invert backlight brightness */
14578 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14579
14580 /* Acer/eMachines G725 */
14581 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14582
14583 /* Acer/eMachines e725 */
14584 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14585
14586 /* Acer/Packard Bell NCL20 */
14587 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14588
14589 /* Acer Aspire 4736Z */
14590 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14591
14592 /* Acer Aspire 5336 */
14593 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14594
14595 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14596 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14597
dfb3d47b
SD
14598 /* Acer C720 Chromebook (Core i3 4005U) */
14599 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14600
b2a9601c 14601 /* Apple Macbook 2,1 (Core 2 T7400) */
14602 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14603
d4967d8c
SD
14604 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14605 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14606
14607 /* HP Chromebook 14 (Celeron 2955U) */
14608 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14609
14610 /* Dell Chromebook 11 */
14611 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14612};
14613
14614static void intel_init_quirks(struct drm_device *dev)
14615{
14616 struct pci_dev *d = dev->pdev;
14617 int i;
14618
14619 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14620 struct intel_quirk *q = &intel_quirks[i];
14621
14622 if (d->device == q->device &&
14623 (d->subsystem_vendor == q->subsystem_vendor ||
14624 q->subsystem_vendor == PCI_ANY_ID) &&
14625 (d->subsystem_device == q->subsystem_device ||
14626 q->subsystem_device == PCI_ANY_ID))
14627 q->hook(dev);
14628 }
5f85f176
EE
14629 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14630 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14631 intel_dmi_quirks[i].hook(dev);
14632 }
b690e96c
JB
14633}
14634
9cce37f4
JB
14635/* Disable the VGA plane that we never use */
14636static void i915_disable_vga(struct drm_device *dev)
14637{
14638 struct drm_i915_private *dev_priv = dev->dev_private;
14639 u8 sr1;
766aa1c4 14640 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 14641
2b37c616 14642 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 14643 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14644 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14645 sr1 = inb(VGA_SR_DATA);
14646 outb(sr1 | 1<<5, VGA_SR_DATA);
14647 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14648 udelay(300);
14649
01f5a626 14650 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14651 POSTING_READ(vga_reg);
14652}
14653
f817586c
DV
14654void intel_modeset_init_hw(struct drm_device *dev)
14655{
a8f78b58
ED
14656 intel_prepare_ddi(dev);
14657
f8bf63fd
VS
14658 if (IS_VALLEYVIEW(dev))
14659 vlv_update_cdclk(dev);
14660
f817586c
DV
14661 intel_init_clock_gating(dev);
14662
8090c6b9 14663 intel_enable_gt_powersave(dev);
f817586c
DV
14664}
14665
79e53945
JB
14666void intel_modeset_init(struct drm_device *dev)
14667{
652c393a 14668 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 14669 int sprite, ret;
8cc87b75 14670 enum pipe pipe;
46f297fb 14671 struct intel_crtc *crtc;
79e53945
JB
14672
14673 drm_mode_config_init(dev);
14674
14675 dev->mode_config.min_width = 0;
14676 dev->mode_config.min_height = 0;
14677
019d96cb
DA
14678 dev->mode_config.preferred_depth = 24;
14679 dev->mode_config.prefer_shadow = 1;
14680
25bab385
TU
14681 dev->mode_config.allow_fb_modifiers = true;
14682
e6ecefaa 14683 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14684
b690e96c
JB
14685 intel_init_quirks(dev);
14686
1fa61106
ED
14687 intel_init_pm(dev);
14688
e3c74757
BW
14689 if (INTEL_INFO(dev)->num_pipes == 0)
14690 return;
14691
e70236a8 14692 intel_init_display(dev);
7c10a2b5 14693 intel_init_audio(dev);
e70236a8 14694
a6c45cf0
CW
14695 if (IS_GEN2(dev)) {
14696 dev->mode_config.max_width = 2048;
14697 dev->mode_config.max_height = 2048;
14698 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
14699 dev->mode_config.max_width = 4096;
14700 dev->mode_config.max_height = 4096;
79e53945 14701 } else {
a6c45cf0
CW
14702 dev->mode_config.max_width = 8192;
14703 dev->mode_config.max_height = 8192;
79e53945 14704 }
068be561 14705
dc41c154
VS
14706 if (IS_845G(dev) || IS_I865G(dev)) {
14707 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14708 dev->mode_config.cursor_height = 1023;
14709 } else if (IS_GEN2(dev)) {
068be561
DL
14710 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14711 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14712 } else {
14713 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14714 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14715 }
14716
5d4545ae 14717 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 14718
28c97730 14719 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
14720 INTEL_INFO(dev)->num_pipes,
14721 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 14722
055e393f 14723 for_each_pipe(dev_priv, pipe) {
8cc87b75 14724 intel_crtc_init(dev, pipe);
3bdcfc0c 14725 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 14726 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 14727 if (ret)
06da8da2 14728 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 14729 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 14730 }
79e53945
JB
14731 }
14732
f42bb70d
JB
14733 intel_init_dpio(dev);
14734
e72f9fbf 14735 intel_shared_dpll_init(dev);
ee7b9f93 14736
9cce37f4
JB
14737 /* Just disable it once at startup */
14738 i915_disable_vga(dev);
79e53945 14739 intel_setup_outputs(dev);
11be49eb
CW
14740
14741 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 14742 intel_fbc_disable(dev);
fa9fa083 14743
6e9f798d 14744 drm_modeset_lock_all(dev);
fa9fa083 14745 intel_modeset_setup_hw_state(dev, false);
6e9f798d 14746 drm_modeset_unlock_all(dev);
46f297fb 14747
d3fcc808 14748 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
14749 if (!crtc->active)
14750 continue;
14751
46f297fb 14752 /*
46f297fb
JB
14753 * Note that reserving the BIOS fb up front prevents us
14754 * from stuffing other stolen allocations like the ring
14755 * on top. This prevents some ugliness at boot time, and
14756 * can even allow for smooth boot transitions if the BIOS
14757 * fb is large enough for the active pipe configuration.
14758 */
5724dbd1
DL
14759 if (dev_priv->display.get_initial_plane_config) {
14760 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
14761 &crtc->plane_config);
14762 /*
14763 * If the fb is shared between multiple heads, we'll
14764 * just get the first one.
14765 */
f6936e29 14766 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
46f297fb 14767 }
46f297fb 14768 }
2c7111db
CW
14769}
14770
7fad798e
DV
14771static void intel_enable_pipe_a(struct drm_device *dev)
14772{
14773 struct intel_connector *connector;
14774 struct drm_connector *crt = NULL;
14775 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 14776 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
14777
14778 /* We can't just switch on the pipe A, we need to set things up with a
14779 * proper mode and output configuration. As a gross hack, enable pipe A
14780 * by enabling the load detect pipe once. */
3a3371ff 14781 for_each_intel_connector(dev, connector) {
7fad798e
DV
14782 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14783 crt = &connector->base;
14784 break;
14785 }
14786 }
14787
14788 if (!crt)
14789 return;
14790
208bf9fd 14791 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 14792 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
14793}
14794
fa555837
DV
14795static bool
14796intel_check_plane_mapping(struct intel_crtc *crtc)
14797{
7eb552ae
BW
14798 struct drm_device *dev = crtc->base.dev;
14799 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
14800 u32 reg, val;
14801
7eb552ae 14802 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
14803 return true;
14804
14805 reg = DSPCNTR(!crtc->plane);
14806 val = I915_READ(reg);
14807
14808 if ((val & DISPLAY_PLANE_ENABLE) &&
14809 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14810 return false;
14811
14812 return true;
14813}
14814
24929352
DV
14815static void intel_sanitize_crtc(struct intel_crtc *crtc)
14816{
14817 struct drm_device *dev = crtc->base.dev;
14818 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 14819 u32 reg;
24929352 14820
24929352 14821 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 14822 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
14823 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14824
d3eaf884 14825 /* restore vblank interrupts to correct state */
9625604c 14826 drm_crtc_vblank_reset(&crtc->base);
d297e103
VS
14827 if (crtc->active) {
14828 update_scanline_offset(crtc);
9625604c
DV
14829 drm_crtc_vblank_on(&crtc->base);
14830 }
d3eaf884 14831
24929352 14832 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
14833 * disable the crtc (and hence change the state) if it is wrong. Note
14834 * that gen4+ has a fixed plane -> pipe mapping. */
14835 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
14836 struct intel_connector *connector;
14837 bool plane;
14838
24929352
DV
14839 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14840 crtc->base.base.id);
14841
14842 /* Pipe has the wrong plane attached and the plane is active.
14843 * Temporarily change the plane mapping and disable everything
14844 * ... */
14845 plane = crtc->plane;
b70709a6 14846 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 14847 crtc->plane = !plane;
ce22dba9 14848 intel_crtc_disable_planes(&crtc->base);
24929352
DV
14849 dev_priv->display.crtc_disable(&crtc->base);
14850 crtc->plane = plane;
14851
14852 /* ... and break all links. */
3a3371ff 14853 for_each_intel_connector(dev, connector) {
24929352
DV
14854 if (connector->encoder->base.crtc != &crtc->base)
14855 continue;
14856
7f1950fb
EE
14857 connector->base.dpms = DRM_MODE_DPMS_OFF;
14858 connector->base.encoder = NULL;
24929352 14859 }
7f1950fb
EE
14860 /* multiple connectors may have the same encoder:
14861 * handle them and break crtc link separately */
3a3371ff 14862 for_each_intel_connector(dev, connector)
7f1950fb
EE
14863 if (connector->encoder->base.crtc == &crtc->base) {
14864 connector->encoder->base.crtc = NULL;
14865 connector->encoder->connectors_active = false;
14866 }
24929352
DV
14867
14868 WARN_ON(crtc->active);
83d65738 14869 crtc->base.state->enable = false;
24929352
DV
14870 crtc->base.enabled = false;
14871 }
24929352 14872
7fad798e
DV
14873 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14874 crtc->pipe == PIPE_A && !crtc->active) {
14875 /* BIOS forgot to enable pipe A, this mostly happens after
14876 * resume. Force-enable the pipe to fix this, the update_dpms
14877 * call below we restore the pipe to the right state, but leave
14878 * the required bits on. */
14879 intel_enable_pipe_a(dev);
14880 }
14881
24929352
DV
14882 /* Adjust the state of the output pipe according to whether we
14883 * have active connectors/encoders. */
14884 intel_crtc_update_dpms(&crtc->base);
14885
83d65738 14886 if (crtc->active != crtc->base.state->enable) {
24929352
DV
14887 struct intel_encoder *encoder;
14888
14889 /* This can happen either due to bugs in the get_hw_state
14890 * functions or because the pipe is force-enabled due to the
14891 * pipe A quirk. */
14892 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14893 crtc->base.base.id,
83d65738 14894 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
14895 crtc->active ? "enabled" : "disabled");
14896
83d65738 14897 crtc->base.state->enable = crtc->active;
24929352
DV
14898 crtc->base.enabled = crtc->active;
14899
14900 /* Because we only establish the connector -> encoder ->
14901 * crtc links if something is active, this means the
14902 * crtc is now deactivated. Break the links. connector
14903 * -> encoder links are only establish when things are
14904 * actually up, hence no need to break them. */
14905 WARN_ON(crtc->active);
14906
14907 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14908 WARN_ON(encoder->connectors_active);
14909 encoder->base.crtc = NULL;
14910 }
14911 }
c5ab3bc0 14912
a3ed6aad 14913 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
14914 /*
14915 * We start out with underrun reporting disabled to avoid races.
14916 * For correct bookkeeping mark this on active crtcs.
14917 *
c5ab3bc0
DV
14918 * Also on gmch platforms we dont have any hardware bits to
14919 * disable the underrun reporting. Which means we need to start
14920 * out with underrun reporting disabled also on inactive pipes,
14921 * since otherwise we'll complain about the garbage we read when
14922 * e.g. coming up after runtime pm.
14923 *
4cc31489
DV
14924 * No protection against concurrent access is required - at
14925 * worst a fifo underrun happens which also sets this to false.
14926 */
14927 crtc->cpu_fifo_underrun_disabled = true;
14928 crtc->pch_fifo_underrun_disabled = true;
14929 }
24929352
DV
14930}
14931
14932static void intel_sanitize_encoder(struct intel_encoder *encoder)
14933{
14934 struct intel_connector *connector;
14935 struct drm_device *dev = encoder->base.dev;
14936
14937 /* We need to check both for a crtc link (meaning that the
14938 * encoder is active and trying to read from a pipe) and the
14939 * pipe itself being active. */
14940 bool has_active_crtc = encoder->base.crtc &&
14941 to_intel_crtc(encoder->base.crtc)->active;
14942
14943 if (encoder->connectors_active && !has_active_crtc) {
14944 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14945 encoder->base.base.id,
8e329a03 14946 encoder->base.name);
24929352
DV
14947
14948 /* Connector is active, but has no active pipe. This is
14949 * fallout from our resume register restoring. Disable
14950 * the encoder manually again. */
14951 if (encoder->base.crtc) {
14952 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14953 encoder->base.base.id,
8e329a03 14954 encoder->base.name);
24929352 14955 encoder->disable(encoder);
a62d1497
VS
14956 if (encoder->post_disable)
14957 encoder->post_disable(encoder);
24929352 14958 }
7f1950fb
EE
14959 encoder->base.crtc = NULL;
14960 encoder->connectors_active = false;
24929352
DV
14961
14962 /* Inconsistent output/port/pipe state happens presumably due to
14963 * a bug in one of the get_hw_state functions. Or someplace else
14964 * in our code, like the register restore mess on resume. Clamp
14965 * things to off as a safer default. */
3a3371ff 14966 for_each_intel_connector(dev, connector) {
24929352
DV
14967 if (connector->encoder != encoder)
14968 continue;
7f1950fb
EE
14969 connector->base.dpms = DRM_MODE_DPMS_OFF;
14970 connector->base.encoder = NULL;
24929352
DV
14971 }
14972 }
14973 /* Enabled encoders without active connectors will be fixed in
14974 * the crtc fixup. */
14975}
14976
04098753 14977void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
14978{
14979 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 14980 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 14981
04098753
ID
14982 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14983 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14984 i915_disable_vga(dev);
14985 }
14986}
14987
14988void i915_redisable_vga(struct drm_device *dev)
14989{
14990 struct drm_i915_private *dev_priv = dev->dev_private;
14991
8dc8a27c
PZ
14992 /* This function can be called both from intel_modeset_setup_hw_state or
14993 * at a very early point in our resume sequence, where the power well
14994 * structures are not yet restored. Since this function is at a very
14995 * paranoid "someone might have enabled VGA while we were not looking"
14996 * level, just check if the power well is enabled instead of trying to
14997 * follow the "don't touch the power well if we don't need it" policy
14998 * the rest of the driver uses. */
f458ebbc 14999 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
15000 return;
15001
04098753 15002 i915_redisable_vga_power_on(dev);
0fde901f
KM
15003}
15004
98ec7739
VS
15005static bool primary_get_hw_state(struct intel_crtc *crtc)
15006{
15007 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15008
15009 if (!crtc->active)
15010 return false;
15011
15012 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15013}
15014
30e984df 15015static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
15016{
15017 struct drm_i915_private *dev_priv = dev->dev_private;
15018 enum pipe pipe;
24929352
DV
15019 struct intel_crtc *crtc;
15020 struct intel_encoder *encoder;
15021 struct intel_connector *connector;
5358901f 15022 int i;
24929352 15023
d3fcc808 15024 for_each_intel_crtc(dev, crtc) {
b70709a6
ML
15025 struct drm_plane *primary = crtc->base.primary;
15026 struct intel_plane_state *plane_state;
15027
6e3c9717 15028 memset(crtc->config, 0, sizeof(*crtc->config));
3b117c8f 15029
6e3c9717 15030 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
9953599b 15031
0e8ffe1b 15032 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 15033 crtc->config);
24929352 15034
83d65738 15035 crtc->base.state->enable = crtc->active;
24929352 15036 crtc->base.enabled = crtc->active;
b70709a6
ML
15037
15038 plane_state = to_intel_plane_state(primary->state);
15039 plane_state->visible = primary_get_hw_state(crtc);
24929352
DV
15040
15041 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15042 crtc->base.base.id,
15043 crtc->active ? "enabled" : "disabled");
15044 }
15045
5358901f
DV
15046 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15047 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15048
3e369b76
ACO
15049 pll->on = pll->get_hw_state(dev_priv, pll,
15050 &pll->config.hw_state);
5358901f 15051 pll->active = 0;
3e369b76 15052 pll->config.crtc_mask = 0;
d3fcc808 15053 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 15054 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 15055 pll->active++;
3e369b76 15056 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 15057 }
5358901f 15058 }
5358901f 15059
1e6f2ddc 15060 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15061 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 15062
3e369b76 15063 if (pll->config.crtc_mask)
bd2bb1b9 15064 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
15065 }
15066
b2784e15 15067 for_each_intel_encoder(dev, encoder) {
24929352
DV
15068 pipe = 0;
15069
15070 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15071 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15072 encoder->base.crtc = &crtc->base;
6e3c9717 15073 encoder->get_config(encoder, crtc->config);
24929352
DV
15074 } else {
15075 encoder->base.crtc = NULL;
15076 }
15077
15078 encoder->connectors_active = false;
6f2bcceb 15079 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15080 encoder->base.base.id,
8e329a03 15081 encoder->base.name,
24929352 15082 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15083 pipe_name(pipe));
24929352
DV
15084 }
15085
3a3371ff 15086 for_each_intel_connector(dev, connector) {
24929352
DV
15087 if (connector->get_hw_state(connector)) {
15088 connector->base.dpms = DRM_MODE_DPMS_ON;
15089 connector->encoder->connectors_active = true;
15090 connector->base.encoder = &connector->encoder->base;
15091 } else {
15092 connector->base.dpms = DRM_MODE_DPMS_OFF;
15093 connector->base.encoder = NULL;
15094 }
15095 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15096 connector->base.base.id,
c23cc417 15097 connector->base.name,
24929352
DV
15098 connector->base.encoder ? "enabled" : "disabled");
15099 }
30e984df
DV
15100}
15101
15102/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15103 * and i915 state tracking structures. */
15104void intel_modeset_setup_hw_state(struct drm_device *dev,
15105 bool force_restore)
15106{
15107 struct drm_i915_private *dev_priv = dev->dev_private;
15108 enum pipe pipe;
30e984df
DV
15109 struct intel_crtc *crtc;
15110 struct intel_encoder *encoder;
35c95375 15111 int i;
30e984df
DV
15112
15113 intel_modeset_readout_hw_state(dev);
24929352 15114
babea61d
JB
15115 /*
15116 * Now that we have the config, copy it to each CRTC struct
15117 * Note that this could go away if we move to using crtc_config
15118 * checking everywhere.
15119 */
d3fcc808 15120 for_each_intel_crtc(dev, crtc) {
d330a953 15121 if (crtc->active && i915.fastboot) {
6e3c9717
ACO
15122 intel_mode_from_pipe_config(&crtc->base.mode,
15123 crtc->config);
babea61d
JB
15124 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15125 crtc->base.base.id);
15126 drm_mode_debug_printmodeline(&crtc->base.mode);
15127 }
15128 }
15129
24929352 15130 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15131 for_each_intel_encoder(dev, encoder) {
24929352
DV
15132 intel_sanitize_encoder(encoder);
15133 }
15134
055e393f 15135 for_each_pipe(dev_priv, pipe) {
24929352
DV
15136 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15137 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15138 intel_dump_pipe_config(crtc, crtc->config,
15139 "[setup_hw_state]");
24929352 15140 }
9a935856 15141
d29b2f9d
ACO
15142 intel_modeset_update_connector_atomic_state(dev);
15143
35c95375
DV
15144 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15145 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15146
15147 if (!pll->on || pll->active)
15148 continue;
15149
15150 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15151
15152 pll->disable(dev_priv, pll);
15153 pll->on = false;
15154 }
15155
3078999f
PB
15156 if (IS_GEN9(dev))
15157 skl_wm_get_hw_state(dev);
15158 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
15159 ilk_wm_get_hw_state(dev);
15160
45e2b5f6 15161 if (force_restore) {
7d0bc1ea
VS
15162 i915_redisable_vga(dev);
15163
f30da187
DV
15164 /*
15165 * We need to use raw interfaces for restoring state to avoid
15166 * checking (bogus) intermediate states.
15167 */
055e393f 15168 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
15169 struct drm_crtc *crtc =
15170 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187 15171
83a57153 15172 intel_crtc_restore_mode(crtc);
45e2b5f6
DV
15173 }
15174 } else {
15175 intel_modeset_update_staged_output_state(dev);
15176 }
8af6cf88
DV
15177
15178 intel_modeset_check_state(dev);
2c7111db
CW
15179}
15180
15181void intel_modeset_gem_init(struct drm_device *dev)
15182{
92122789 15183 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 15184 struct drm_crtc *c;
2ff8fde1 15185 struct drm_i915_gem_object *obj;
e0d6149b 15186 int ret;
484b41dd 15187
ae48434c
ID
15188 mutex_lock(&dev->struct_mutex);
15189 intel_init_gt_powersave(dev);
15190 mutex_unlock(&dev->struct_mutex);
15191
92122789
JB
15192 /*
15193 * There may be no VBT; and if the BIOS enabled SSC we can
15194 * just keep using it to avoid unnecessary flicker. Whereas if the
15195 * BIOS isn't using it, don't assume it will work even if the VBT
15196 * indicates as much.
15197 */
15198 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15199 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15200 DREF_SSC1_ENABLE);
15201
1833b134 15202 intel_modeset_init_hw(dev);
02e792fb
DV
15203
15204 intel_setup_overlay(dev);
484b41dd
JB
15205
15206 /*
15207 * Make sure any fbs we allocated at startup are properly
15208 * pinned & fenced. When we do the allocation it's too early
15209 * for this.
15210 */
70e1e0ec 15211 for_each_crtc(dev, c) {
2ff8fde1
MR
15212 obj = intel_fb_obj(c->primary->fb);
15213 if (obj == NULL)
484b41dd
JB
15214 continue;
15215
e0d6149b
TU
15216 mutex_lock(&dev->struct_mutex);
15217 ret = intel_pin_and_fence_fb_obj(c->primary,
15218 c->primary->fb,
15219 c->primary->state,
15220 NULL);
15221 mutex_unlock(&dev->struct_mutex);
15222 if (ret) {
484b41dd
JB
15223 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15224 to_intel_crtc(c)->pipe);
66e514c1
DA
15225 drm_framebuffer_unreference(c->primary->fb);
15226 c->primary->fb = NULL;
afd65eb4 15227 update_state_fb(c->primary);
484b41dd
JB
15228 }
15229 }
0962c3c9
VS
15230
15231 intel_backlight_register(dev);
79e53945
JB
15232}
15233
4932e2c3
ID
15234void intel_connector_unregister(struct intel_connector *intel_connector)
15235{
15236 struct drm_connector *connector = &intel_connector->base;
15237
15238 intel_panel_destroy_backlight(connector);
34ea3d38 15239 drm_connector_unregister(connector);
4932e2c3
ID
15240}
15241
79e53945
JB
15242void intel_modeset_cleanup(struct drm_device *dev)
15243{
652c393a 15244 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 15245 struct drm_connector *connector;
652c393a 15246
2eb5252e
ID
15247 intel_disable_gt_powersave(dev);
15248
0962c3c9
VS
15249 intel_backlight_unregister(dev);
15250
fd0c0642
DV
15251 /*
15252 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15253 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15254 * experience fancy races otherwise.
15255 */
2aeb7d3a 15256 intel_irq_uninstall(dev_priv);
eb21b92b 15257
fd0c0642
DV
15258 /*
15259 * Due to the hpd irq storm handling the hotplug work can re-arm the
15260 * poll handlers. Hence disable polling after hpd handling is shut down.
15261 */
f87ea761 15262 drm_kms_helper_poll_fini(dev);
fd0c0642 15263
652c393a
JB
15264 mutex_lock(&dev->struct_mutex);
15265
723bfd70
JB
15266 intel_unregister_dsm_handler();
15267
7ff0ebcc 15268 intel_fbc_disable(dev);
e70236a8 15269
69341a5e
KH
15270 mutex_unlock(&dev->struct_mutex);
15271
1630fe75
CW
15272 /* flush any delayed tasks or pending work */
15273 flush_scheduled_work();
15274
db31af1d
JN
15275 /* destroy the backlight and sysfs files before encoders/connectors */
15276 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
15277 struct intel_connector *intel_connector;
15278
15279 intel_connector = to_intel_connector(connector);
15280 intel_connector->unregister(intel_connector);
db31af1d 15281 }
d9255d57 15282
79e53945 15283 drm_mode_config_cleanup(dev);
4d7bb011
DV
15284
15285 intel_cleanup_overlay(dev);
ae48434c
ID
15286
15287 mutex_lock(&dev->struct_mutex);
15288 intel_cleanup_gt_powersave(dev);
15289 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15290}
15291
f1c79df3
ZW
15292/*
15293 * Return which encoder is currently attached for connector.
15294 */
df0e9248 15295struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15296{
df0e9248
CW
15297 return &intel_attached_encoder(connector)->base;
15298}
f1c79df3 15299
df0e9248
CW
15300void intel_connector_attach_encoder(struct intel_connector *connector,
15301 struct intel_encoder *encoder)
15302{
15303 connector->encoder = encoder;
15304 drm_mode_connector_attach_encoder(&connector->base,
15305 &encoder->base);
79e53945 15306}
28d52043
DA
15307
15308/*
15309 * set vga decode state - true == enable VGA decode
15310 */
15311int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15312{
15313 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15314 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15315 u16 gmch_ctrl;
15316
75fa041d
CW
15317 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15318 DRM_ERROR("failed to read control word\n");
15319 return -EIO;
15320 }
15321
c0cc8a55
CW
15322 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15323 return 0;
15324
28d52043
DA
15325 if (state)
15326 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15327 else
15328 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15329
15330 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15331 DRM_ERROR("failed to write control word\n");
15332 return -EIO;
15333 }
15334
28d52043
DA
15335 return 0;
15336}
c4a1d9e4 15337
c4a1d9e4 15338struct intel_display_error_state {
ff57f1b0
PZ
15339
15340 u32 power_well_driver;
15341
63b66e5b
CW
15342 int num_transcoders;
15343
c4a1d9e4
CW
15344 struct intel_cursor_error_state {
15345 u32 control;
15346 u32 position;
15347 u32 base;
15348 u32 size;
52331309 15349 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15350
15351 struct intel_pipe_error_state {
ddf9c536 15352 bool power_domain_on;
c4a1d9e4 15353 u32 source;
f301b1e1 15354 u32 stat;
52331309 15355 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15356
15357 struct intel_plane_error_state {
15358 u32 control;
15359 u32 stride;
15360 u32 size;
15361 u32 pos;
15362 u32 addr;
15363 u32 surface;
15364 u32 tile_offset;
52331309 15365 } plane[I915_MAX_PIPES];
63b66e5b
CW
15366
15367 struct intel_transcoder_error_state {
ddf9c536 15368 bool power_domain_on;
63b66e5b
CW
15369 enum transcoder cpu_transcoder;
15370
15371 u32 conf;
15372
15373 u32 htotal;
15374 u32 hblank;
15375 u32 hsync;
15376 u32 vtotal;
15377 u32 vblank;
15378 u32 vsync;
15379 } transcoder[4];
c4a1d9e4
CW
15380};
15381
15382struct intel_display_error_state *
15383intel_display_capture_error_state(struct drm_device *dev)
15384{
fbee40df 15385 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 15386 struct intel_display_error_state *error;
63b66e5b
CW
15387 int transcoders[] = {
15388 TRANSCODER_A,
15389 TRANSCODER_B,
15390 TRANSCODER_C,
15391 TRANSCODER_EDP,
15392 };
c4a1d9e4
CW
15393 int i;
15394
63b66e5b
CW
15395 if (INTEL_INFO(dev)->num_pipes == 0)
15396 return NULL;
15397
9d1cb914 15398 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15399 if (error == NULL)
15400 return NULL;
15401
190be112 15402 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
15403 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15404
055e393f 15405 for_each_pipe(dev_priv, i) {
ddf9c536 15406 error->pipe[i].power_domain_on =
f458ebbc
DV
15407 __intel_display_power_is_enabled(dev_priv,
15408 POWER_DOMAIN_PIPE(i));
ddf9c536 15409 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15410 continue;
15411
5efb3e28
VS
15412 error->cursor[i].control = I915_READ(CURCNTR(i));
15413 error->cursor[i].position = I915_READ(CURPOS(i));
15414 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15415
15416 error->plane[i].control = I915_READ(DSPCNTR(i));
15417 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 15418 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 15419 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15420 error->plane[i].pos = I915_READ(DSPPOS(i));
15421 }
ca291363
PZ
15422 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15423 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
15424 if (INTEL_INFO(dev)->gen >= 4) {
15425 error->plane[i].surface = I915_READ(DSPSURF(i));
15426 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15427 }
15428
c4a1d9e4 15429 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15430
3abfce77 15431 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 15432 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15433 }
15434
15435 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15436 if (HAS_DDI(dev_priv->dev))
15437 error->num_transcoders++; /* Account for eDP. */
15438
15439 for (i = 0; i < error->num_transcoders; i++) {
15440 enum transcoder cpu_transcoder = transcoders[i];
15441
ddf9c536 15442 error->transcoder[i].power_domain_on =
f458ebbc 15443 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15444 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15445 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15446 continue;
15447
63b66e5b
CW
15448 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15449
15450 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15451 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15452 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15453 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15454 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15455 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15456 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15457 }
15458
15459 return error;
15460}
15461
edc3d884
MK
15462#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15463
c4a1d9e4 15464void
edc3d884 15465intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15466 struct drm_device *dev,
15467 struct intel_display_error_state *error)
15468{
055e393f 15469 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15470 int i;
15471
63b66e5b
CW
15472 if (!error)
15473 return;
15474
edc3d884 15475 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15476 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15477 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15478 error->power_well_driver);
055e393f 15479 for_each_pipe(dev_priv, i) {
edc3d884 15480 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
15481 err_printf(m, " Power: %s\n",
15482 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 15483 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15484 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15485
15486 err_printf(m, "Plane [%d]:\n", i);
15487 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15488 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15489 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15490 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15491 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15492 }
4b71a570 15493 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15494 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15495 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15496 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15497 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15498 }
15499
edc3d884
MK
15500 err_printf(m, "Cursor [%d]:\n", i);
15501 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15502 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15503 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15504 }
63b66e5b
CW
15505
15506 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 15507 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 15508 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
15509 err_printf(m, " Power: %s\n",
15510 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
15511 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15512 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15513 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15514 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15515 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15516 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15517 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15518 }
c4a1d9e4 15519}
e2fcdaa9
VS
15520
15521void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15522{
15523 struct intel_crtc *crtc;
15524
15525 for_each_intel_crtc(dev, crtc) {
15526 struct intel_unpin_work *work;
e2fcdaa9 15527
5e2d7afc 15528 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
15529
15530 work = crtc->unpin_work;
15531
15532 if (work && work->event &&
15533 work->event->base.file_priv == file) {
15534 kfree(work->event);
15535 work->event = NULL;
15536 }
15537
5e2d7afc 15538 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
15539 }
15540}
This page took 2.924931 seconds and 5 git commands to generate.