drm/i915: Don't use staged config to calculate mode_changed flags
[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 86 struct drm_atomic_state *state);
eb1bfe80
JB
87static int intel_framebuffer_init(struct drm_device *dev,
88 struct intel_framebuffer *ifb,
89 struct drm_mode_fb_cmd2 *mode_cmd,
90 struct drm_i915_gem_object *obj);
5b18e57c
DV
91static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
92static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 93static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
94 struct intel_link_m_n *m_n,
95 struct intel_link_m_n *m2_n2);
29407aab 96static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
97static void haswell_set_pipeconf(struct drm_crtc *crtc);
98static void intel_set_pipe_csc(struct drm_crtc *crtc);
d288f65f 99static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 100 const struct intel_crtc_state *pipe_config);
d288f65f 101static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 102 const struct intel_crtc_state *pipe_config);
ea2c67bb
MR
103static void intel_begin_crtc_commit(struct drm_crtc *crtc);
104static void intel_finish_crtc_commit(struct drm_crtc *crtc);
549e2bfb
CK
105static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
106 struct intel_crtc_state *crtc_state);
5ab7b0b7
ID
107static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
108 int num_connectors);
ce22dba9
ML
109static void intel_crtc_enable_planes(struct drm_crtc *crtc);
110static void intel_crtc_disable_planes(struct drm_crtc *crtc);
e7457a9a 111
0e32b39c
DA
112static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
113{
114 if (!connector->mst_port)
115 return connector->encoder;
116 else
117 return &connector->mst_port->mst_encoders[pipe]->base;
118}
119
79e53945 120typedef struct {
0206e353 121 int min, max;
79e53945
JB
122} intel_range_t;
123
124typedef struct {
0206e353
AJ
125 int dot_limit;
126 int p2_slow, p2_fast;
79e53945
JB
127} intel_p2_t;
128
d4906093
ML
129typedef struct intel_limit intel_limit_t;
130struct intel_limit {
0206e353
AJ
131 intel_range_t dot, vco, n, m, m1, m2, p, p1;
132 intel_p2_t p2;
d4906093 133};
79e53945 134
d2acd215
DV
135int
136intel_pch_rawclk(struct drm_device *dev)
137{
138 struct drm_i915_private *dev_priv = dev->dev_private;
139
140 WARN_ON(!HAS_PCH_SPLIT(dev));
141
142 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
143}
144
021357ac
CW
145static inline u32 /* units of 100MHz */
146intel_fdi_link_freq(struct drm_device *dev)
147{
8b99e68c
CW
148 if (IS_GEN5(dev)) {
149 struct drm_i915_private *dev_priv = dev->dev_private;
150 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
151 } else
152 return 27;
021357ac
CW
153}
154
5d536e28 155static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 156 .dot = { .min = 25000, .max = 350000 },
9c333719 157 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 158 .n = { .min = 2, .max = 16 },
0206e353
AJ
159 .m = { .min = 96, .max = 140 },
160 .m1 = { .min = 18, .max = 26 },
161 .m2 = { .min = 6, .max = 16 },
162 .p = { .min = 4, .max = 128 },
163 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
164 .p2 = { .dot_limit = 165000,
165 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
166};
167
5d536e28
DV
168static const intel_limit_t intel_limits_i8xx_dvo = {
169 .dot = { .min = 25000, .max = 350000 },
9c333719 170 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 171 .n = { .min = 2, .max = 16 },
5d536e28
DV
172 .m = { .min = 96, .max = 140 },
173 .m1 = { .min = 18, .max = 26 },
174 .m2 = { .min = 6, .max = 16 },
175 .p = { .min = 4, .max = 128 },
176 .p1 = { .min = 2, .max = 33 },
177 .p2 = { .dot_limit = 165000,
178 .p2_slow = 4, .p2_fast = 4 },
179};
180
e4b36699 181static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 182 .dot = { .min = 25000, .max = 350000 },
9c333719 183 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 184 .n = { .min = 2, .max = 16 },
0206e353
AJ
185 .m = { .min = 96, .max = 140 },
186 .m1 = { .min = 18, .max = 26 },
187 .m2 = { .min = 6, .max = 16 },
188 .p = { .min = 4, .max = 128 },
189 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
190 .p2 = { .dot_limit = 165000,
191 .p2_slow = 14, .p2_fast = 7 },
e4b36699 192};
273e27ca 193
e4b36699 194static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
195 .dot = { .min = 20000, .max = 400000 },
196 .vco = { .min = 1400000, .max = 2800000 },
197 .n = { .min = 1, .max = 6 },
198 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
199 .m1 = { .min = 8, .max = 18 },
200 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
201 .p = { .min = 5, .max = 80 },
202 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
203 .p2 = { .dot_limit = 200000,
204 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
205};
206
207static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
208 .dot = { .min = 20000, .max = 400000 },
209 .vco = { .min = 1400000, .max = 2800000 },
210 .n = { .min = 1, .max = 6 },
211 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
212 .m1 = { .min = 8, .max = 18 },
213 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
214 .p = { .min = 7, .max = 98 },
215 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
216 .p2 = { .dot_limit = 112000,
217 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
218};
219
273e27ca 220
e4b36699 221static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
222 .dot = { .min = 25000, .max = 270000 },
223 .vco = { .min = 1750000, .max = 3500000},
224 .n = { .min = 1, .max = 4 },
225 .m = { .min = 104, .max = 138 },
226 .m1 = { .min = 17, .max = 23 },
227 .m2 = { .min = 5, .max = 11 },
228 .p = { .min = 10, .max = 30 },
229 .p1 = { .min = 1, .max = 3},
230 .p2 = { .dot_limit = 270000,
231 .p2_slow = 10,
232 .p2_fast = 10
044c7c41 233 },
e4b36699
KP
234};
235
236static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
237 .dot = { .min = 22000, .max = 400000 },
238 .vco = { .min = 1750000, .max = 3500000},
239 .n = { .min = 1, .max = 4 },
240 .m = { .min = 104, .max = 138 },
241 .m1 = { .min = 16, .max = 23 },
242 .m2 = { .min = 5, .max = 11 },
243 .p = { .min = 5, .max = 80 },
244 .p1 = { .min = 1, .max = 8},
245 .p2 = { .dot_limit = 165000,
246 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
247};
248
249static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
250 .dot = { .min = 20000, .max = 115000 },
251 .vco = { .min = 1750000, .max = 3500000 },
252 .n = { .min = 1, .max = 3 },
253 .m = { .min = 104, .max = 138 },
254 .m1 = { .min = 17, .max = 23 },
255 .m2 = { .min = 5, .max = 11 },
256 .p = { .min = 28, .max = 112 },
257 .p1 = { .min = 2, .max = 8 },
258 .p2 = { .dot_limit = 0,
259 .p2_slow = 14, .p2_fast = 14
044c7c41 260 },
e4b36699
KP
261};
262
263static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
264 .dot = { .min = 80000, .max = 224000 },
265 .vco = { .min = 1750000, .max = 3500000 },
266 .n = { .min = 1, .max = 3 },
267 .m = { .min = 104, .max = 138 },
268 .m1 = { .min = 17, .max = 23 },
269 .m2 = { .min = 5, .max = 11 },
270 .p = { .min = 14, .max = 42 },
271 .p1 = { .min = 2, .max = 6 },
272 .p2 = { .dot_limit = 0,
273 .p2_slow = 7, .p2_fast = 7
044c7c41 274 },
e4b36699
KP
275};
276
f2b115e6 277static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
278 .dot = { .min = 20000, .max = 400000},
279 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 280 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
281 .n = { .min = 3, .max = 6 },
282 .m = { .min = 2, .max = 256 },
273e27ca 283 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
284 .m1 = { .min = 0, .max = 0 },
285 .m2 = { .min = 0, .max = 254 },
286 .p = { .min = 5, .max = 80 },
287 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
288 .p2 = { .dot_limit = 200000,
289 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
290};
291
f2b115e6 292static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
293 .dot = { .min = 20000, .max = 400000 },
294 .vco = { .min = 1700000, .max = 3500000 },
295 .n = { .min = 3, .max = 6 },
296 .m = { .min = 2, .max = 256 },
297 .m1 = { .min = 0, .max = 0 },
298 .m2 = { .min = 0, .max = 254 },
299 .p = { .min = 7, .max = 112 },
300 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
301 .p2 = { .dot_limit = 112000,
302 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
303};
304
273e27ca
EA
305/* Ironlake / Sandybridge
306 *
307 * We calculate clock using (register_value + 2) for N/M1/M2, so here
308 * the range value for them is (actual_value - 2).
309 */
b91ad0ec 310static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
311 .dot = { .min = 25000, .max = 350000 },
312 .vco = { .min = 1760000, .max = 3510000 },
313 .n = { .min = 1, .max = 5 },
314 .m = { .min = 79, .max = 127 },
315 .m1 = { .min = 12, .max = 22 },
316 .m2 = { .min = 5, .max = 9 },
317 .p = { .min = 5, .max = 80 },
318 .p1 = { .min = 1, .max = 8 },
319 .p2 = { .dot_limit = 225000,
320 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
321};
322
b91ad0ec 323static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
324 .dot = { .min = 25000, .max = 350000 },
325 .vco = { .min = 1760000, .max = 3510000 },
326 .n = { .min = 1, .max = 3 },
327 .m = { .min = 79, .max = 118 },
328 .m1 = { .min = 12, .max = 22 },
329 .m2 = { .min = 5, .max = 9 },
330 .p = { .min = 28, .max = 112 },
331 .p1 = { .min = 2, .max = 8 },
332 .p2 = { .dot_limit = 225000,
333 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
334};
335
336static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
337 .dot = { .min = 25000, .max = 350000 },
338 .vco = { .min = 1760000, .max = 3510000 },
339 .n = { .min = 1, .max = 3 },
340 .m = { .min = 79, .max = 127 },
341 .m1 = { .min = 12, .max = 22 },
342 .m2 = { .min = 5, .max = 9 },
343 .p = { .min = 14, .max = 56 },
344 .p1 = { .min = 2, .max = 8 },
345 .p2 = { .dot_limit = 225000,
346 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
347};
348
273e27ca 349/* LVDS 100mhz refclk limits. */
b91ad0ec 350static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
351 .dot = { .min = 25000, .max = 350000 },
352 .vco = { .min = 1760000, .max = 3510000 },
353 .n = { .min = 1, .max = 2 },
354 .m = { .min = 79, .max = 126 },
355 .m1 = { .min = 12, .max = 22 },
356 .m2 = { .min = 5, .max = 9 },
357 .p = { .min = 28, .max = 112 },
0206e353 358 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
359 .p2 = { .dot_limit = 225000,
360 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
361};
362
363static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
364 .dot = { .min = 25000, .max = 350000 },
365 .vco = { .min = 1760000, .max = 3510000 },
366 .n = { .min = 1, .max = 3 },
367 .m = { .min = 79, .max = 126 },
368 .m1 = { .min = 12, .max = 22 },
369 .m2 = { .min = 5, .max = 9 },
370 .p = { .min = 14, .max = 42 },
0206e353 371 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
372 .p2 = { .dot_limit = 225000,
373 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
374};
375
dc730512 376static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
377 /*
378 * These are the data rate limits (measured in fast clocks)
379 * since those are the strictest limits we have. The fast
380 * clock and actual rate limits are more relaxed, so checking
381 * them would make no difference.
382 */
383 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 384 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 385 .n = { .min = 1, .max = 7 },
a0c4da24
JB
386 .m1 = { .min = 2, .max = 3 },
387 .m2 = { .min = 11, .max = 156 },
b99ab663 388 .p1 = { .min = 2, .max = 3 },
5fdc9c49 389 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
390};
391
ef9348c8
CML
392static const intel_limit_t intel_limits_chv = {
393 /*
394 * These are the data rate limits (measured in fast clocks)
395 * since those are the strictest limits we have. The fast
396 * clock and actual rate limits are more relaxed, so checking
397 * them would make no difference.
398 */
399 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 400 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
401 .n = { .min = 1, .max = 1 },
402 .m1 = { .min = 2, .max = 2 },
403 .m2 = { .min = 24 << 22, .max = 175 << 22 },
404 .p1 = { .min = 2, .max = 4 },
405 .p2 = { .p2_slow = 1, .p2_fast = 14 },
406};
407
5ab7b0b7
ID
408static const intel_limit_t intel_limits_bxt = {
409 /* FIXME: find real dot limits */
410 .dot = { .min = 0, .max = INT_MAX },
411 .vco = { .min = 4800000, .max = 6480000 },
412 .n = { .min = 1, .max = 1 },
413 .m1 = { .min = 2, .max = 2 },
414 /* FIXME: find real m2 limits */
415 .m2 = { .min = 2 << 22, .max = 255 << 22 },
416 .p1 = { .min = 2, .max = 4 },
417 .p2 = { .p2_slow = 1, .p2_fast = 20 },
418};
419
6b4bf1c4
VS
420static void vlv_clock(int refclk, intel_clock_t *clock)
421{
422 clock->m = clock->m1 * clock->m2;
423 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
424 if (WARN_ON(clock->n == 0 || clock->p == 0))
425 return;
fb03ac01
VS
426 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
427 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
428}
429
e0638cdf
PZ
430/**
431 * Returns whether any output on the specified pipe is of the specified type
432 */
4093561b 433bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 434{
409ee761 435 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
436 struct intel_encoder *encoder;
437
409ee761 438 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
439 if (encoder->type == type)
440 return true;
441
442 return false;
443}
444
d0737e1d
ACO
445/**
446 * Returns whether any output on the specified pipe will have the specified
447 * type after a staged modeset is complete, i.e., the same as
448 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
449 * encoder->crtc.
450 */
a93e255f
ACO
451static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
452 int type)
d0737e1d 453{
a93e255f 454 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 455 struct drm_connector *connector;
a93e255f 456 struct drm_connector_state *connector_state;
d0737e1d 457 struct intel_encoder *encoder;
a93e255f
ACO
458 int i, num_connectors = 0;
459
da3ced29 460 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
461 if (connector_state->crtc != crtc_state->base.crtc)
462 continue;
463
464 num_connectors++;
d0737e1d 465
a93e255f
ACO
466 encoder = to_intel_encoder(connector_state->best_encoder);
467 if (encoder->type == type)
d0737e1d 468 return true;
a93e255f
ACO
469 }
470
471 WARN_ON(num_connectors == 0);
d0737e1d
ACO
472
473 return false;
474}
475
a93e255f
ACO
476static const intel_limit_t *
477intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
2c07245f 478{
a93e255f 479 struct drm_device *dev = crtc_state->base.crtc->dev;
2c07245f 480 const intel_limit_t *limit;
b91ad0ec 481
a93e255f 482 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 483 if (intel_is_dual_link_lvds(dev)) {
1b894b59 484 if (refclk == 100000)
b91ad0ec
ZW
485 limit = &intel_limits_ironlake_dual_lvds_100m;
486 else
487 limit = &intel_limits_ironlake_dual_lvds;
488 } else {
1b894b59 489 if (refclk == 100000)
b91ad0ec
ZW
490 limit = &intel_limits_ironlake_single_lvds_100m;
491 else
492 limit = &intel_limits_ironlake_single_lvds;
493 }
c6bb3538 494 } else
b91ad0ec 495 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
496
497 return limit;
498}
499
a93e255f
ACO
500static const intel_limit_t *
501intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 502{
a93e255f 503 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
504 const intel_limit_t *limit;
505
a93e255f 506 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 507 if (intel_is_dual_link_lvds(dev))
e4b36699 508 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 509 else
e4b36699 510 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
511 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
512 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 513 limit = &intel_limits_g4x_hdmi;
a93e255f 514 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 515 limit = &intel_limits_g4x_sdvo;
044c7c41 516 } else /* The option is for other outputs */
e4b36699 517 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
518
519 return limit;
520}
521
a93e255f
ACO
522static const intel_limit_t *
523intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 524{
a93e255f 525 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
526 const intel_limit_t *limit;
527
5ab7b0b7
ID
528 if (IS_BROXTON(dev))
529 limit = &intel_limits_bxt;
530 else if (HAS_PCH_SPLIT(dev))
a93e255f 531 limit = intel_ironlake_limit(crtc_state, refclk);
2c07245f 532 else if (IS_G4X(dev)) {
a93e255f 533 limit = intel_g4x_limit(crtc_state);
f2b115e6 534 } else if (IS_PINEVIEW(dev)) {
a93e255f 535 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 536 limit = &intel_limits_pineview_lvds;
2177832f 537 else
f2b115e6 538 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
539 } else if (IS_CHERRYVIEW(dev)) {
540 limit = &intel_limits_chv;
a0c4da24 541 } else if (IS_VALLEYVIEW(dev)) {
dc730512 542 limit = &intel_limits_vlv;
a6c45cf0 543 } else if (!IS_GEN2(dev)) {
a93e255f 544 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
545 limit = &intel_limits_i9xx_lvds;
546 else
547 limit = &intel_limits_i9xx_sdvo;
79e53945 548 } else {
a93e255f 549 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 550 limit = &intel_limits_i8xx_lvds;
a93e255f 551 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 552 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
553 else
554 limit = &intel_limits_i8xx_dac;
79e53945
JB
555 }
556 return limit;
557}
558
f2b115e6
AJ
559/* m1 is reserved as 0 in Pineview, n is a ring counter */
560static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 561{
2177832f
SL
562 clock->m = clock->m2 + 2;
563 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
564 if (WARN_ON(clock->n == 0 || clock->p == 0))
565 return;
fb03ac01
VS
566 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
567 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
568}
569
7429e9d4
DV
570static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
571{
572 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
573}
574
ac58c3f0 575static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 576{
7429e9d4 577 clock->m = i9xx_dpll_compute_m(clock);
79e53945 578 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
579 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
580 return;
fb03ac01
VS
581 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
582 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
583}
584
ef9348c8
CML
585static void chv_clock(int refclk, intel_clock_t *clock)
586{
587 clock->m = clock->m1 * clock->m2;
588 clock->p = clock->p1 * clock->p2;
589 if (WARN_ON(clock->n == 0 || clock->p == 0))
590 return;
591 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
592 clock->n << 22);
593 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
594}
595
7c04d1d9 596#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
597/**
598 * Returns whether the given set of divisors are valid for a given refclk with
599 * the given connectors.
600 */
601
1b894b59
CW
602static bool intel_PLL_is_valid(struct drm_device *dev,
603 const intel_limit_t *limit,
604 const intel_clock_t *clock)
79e53945 605{
f01b7962
VS
606 if (clock->n < limit->n.min || limit->n.max < clock->n)
607 INTELPllInvalid("n out of range\n");
79e53945 608 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 609 INTELPllInvalid("p1 out of range\n");
79e53945 610 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 611 INTELPllInvalid("m2 out of range\n");
79e53945 612 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 613 INTELPllInvalid("m1 out of range\n");
f01b7962 614
5ab7b0b7 615 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
616 if (clock->m1 <= clock->m2)
617 INTELPllInvalid("m1 <= m2\n");
618
5ab7b0b7 619 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
620 if (clock->p < limit->p.min || limit->p.max < clock->p)
621 INTELPllInvalid("p out of range\n");
622 if (clock->m < limit->m.min || limit->m.max < clock->m)
623 INTELPllInvalid("m out of range\n");
624 }
625
79e53945 626 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 627 INTELPllInvalid("vco out of range\n");
79e53945
JB
628 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
629 * connector, etc., rather than just a single range.
630 */
631 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 632 INTELPllInvalid("dot out of range\n");
79e53945
JB
633
634 return true;
635}
636
d4906093 637static bool
a93e255f
ACO
638i9xx_find_best_dpll(const intel_limit_t *limit,
639 struct intel_crtc_state *crtc_state,
cec2f356
SP
640 int target, int refclk, intel_clock_t *match_clock,
641 intel_clock_t *best_clock)
79e53945 642{
a93e255f 643 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 644 struct drm_device *dev = crtc->base.dev;
79e53945 645 intel_clock_t clock;
79e53945
JB
646 int err = target;
647
a93e255f 648 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 649 /*
a210b028
DV
650 * For LVDS just rely on its current settings for dual-channel.
651 * We haven't figured out how to reliably set up different
652 * single/dual channel state, if we even can.
79e53945 653 */
1974cad0 654 if (intel_is_dual_link_lvds(dev))
79e53945
JB
655 clock.p2 = limit->p2.p2_fast;
656 else
657 clock.p2 = limit->p2.p2_slow;
658 } else {
659 if (target < limit->p2.dot_limit)
660 clock.p2 = limit->p2.p2_slow;
661 else
662 clock.p2 = limit->p2.p2_fast;
663 }
664
0206e353 665 memset(best_clock, 0, sizeof(*best_clock));
79e53945 666
42158660
ZY
667 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
668 clock.m1++) {
669 for (clock.m2 = limit->m2.min;
670 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 671 if (clock.m2 >= clock.m1)
42158660
ZY
672 break;
673 for (clock.n = limit->n.min;
674 clock.n <= limit->n.max; clock.n++) {
675 for (clock.p1 = limit->p1.min;
676 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
677 int this_err;
678
ac58c3f0
DV
679 i9xx_clock(refclk, &clock);
680 if (!intel_PLL_is_valid(dev, limit,
681 &clock))
682 continue;
683 if (match_clock &&
684 clock.p != match_clock->p)
685 continue;
686
687 this_err = abs(clock.dot - target);
688 if (this_err < err) {
689 *best_clock = clock;
690 err = this_err;
691 }
692 }
693 }
694 }
695 }
696
697 return (err != target);
698}
699
700static bool
a93e255f
ACO
701pnv_find_best_dpll(const intel_limit_t *limit,
702 struct intel_crtc_state *crtc_state,
ee9300bb
DV
703 int target, int refclk, intel_clock_t *match_clock,
704 intel_clock_t *best_clock)
79e53945 705{
a93e255f 706 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 707 struct drm_device *dev = crtc->base.dev;
79e53945 708 intel_clock_t clock;
79e53945
JB
709 int err = target;
710
a93e255f 711 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 712 /*
a210b028
DV
713 * For LVDS just rely on its current settings for dual-channel.
714 * We haven't figured out how to reliably set up different
715 * single/dual channel state, if we even can.
79e53945 716 */
1974cad0 717 if (intel_is_dual_link_lvds(dev))
79e53945
JB
718 clock.p2 = limit->p2.p2_fast;
719 else
720 clock.p2 = limit->p2.p2_slow;
721 } else {
722 if (target < limit->p2.dot_limit)
723 clock.p2 = limit->p2.p2_slow;
724 else
725 clock.p2 = limit->p2.p2_fast;
726 }
727
0206e353 728 memset(best_clock, 0, sizeof(*best_clock));
79e53945 729
42158660
ZY
730 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
731 clock.m1++) {
732 for (clock.m2 = limit->m2.min;
733 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
734 for (clock.n = limit->n.min;
735 clock.n <= limit->n.max; clock.n++) {
736 for (clock.p1 = limit->p1.min;
737 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
738 int this_err;
739
ac58c3f0 740 pineview_clock(refclk, &clock);
1b894b59
CW
741 if (!intel_PLL_is_valid(dev, limit,
742 &clock))
79e53945 743 continue;
cec2f356
SP
744 if (match_clock &&
745 clock.p != match_clock->p)
746 continue;
79e53945
JB
747
748 this_err = abs(clock.dot - target);
749 if (this_err < err) {
750 *best_clock = clock;
751 err = this_err;
752 }
753 }
754 }
755 }
756 }
757
758 return (err != target);
759}
760
d4906093 761static bool
a93e255f
ACO
762g4x_find_best_dpll(const intel_limit_t *limit,
763 struct intel_crtc_state *crtc_state,
ee9300bb
DV
764 int target, int refclk, intel_clock_t *match_clock,
765 intel_clock_t *best_clock)
d4906093 766{
a93e255f 767 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 768 struct drm_device *dev = crtc->base.dev;
d4906093
ML
769 intel_clock_t clock;
770 int max_n;
771 bool found;
6ba770dc
AJ
772 /* approximately equals target * 0.00585 */
773 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
774 found = false;
775
a93e255f 776 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 777 if (intel_is_dual_link_lvds(dev))
d4906093
ML
778 clock.p2 = limit->p2.p2_fast;
779 else
780 clock.p2 = limit->p2.p2_slow;
781 } else {
782 if (target < limit->p2.dot_limit)
783 clock.p2 = limit->p2.p2_slow;
784 else
785 clock.p2 = limit->p2.p2_fast;
786 }
787
788 memset(best_clock, 0, sizeof(*best_clock));
789 max_n = limit->n.max;
f77f13e2 790 /* based on hardware requirement, prefer smaller n to precision */
d4906093 791 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 792 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
793 for (clock.m1 = limit->m1.max;
794 clock.m1 >= limit->m1.min; clock.m1--) {
795 for (clock.m2 = limit->m2.max;
796 clock.m2 >= limit->m2.min; clock.m2--) {
797 for (clock.p1 = limit->p1.max;
798 clock.p1 >= limit->p1.min; clock.p1--) {
799 int this_err;
800
ac58c3f0 801 i9xx_clock(refclk, &clock);
1b894b59
CW
802 if (!intel_PLL_is_valid(dev, limit,
803 &clock))
d4906093 804 continue;
1b894b59
CW
805
806 this_err = abs(clock.dot - target);
d4906093
ML
807 if (this_err < err_most) {
808 *best_clock = clock;
809 err_most = this_err;
810 max_n = clock.n;
811 found = true;
812 }
813 }
814 }
815 }
816 }
2c07245f
ZW
817 return found;
818}
819
d5dd62bd
ID
820/*
821 * Check if the calculated PLL configuration is more optimal compared to the
822 * best configuration and error found so far. Return the calculated error.
823 */
824static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
825 const intel_clock_t *calculated_clock,
826 const intel_clock_t *best_clock,
827 unsigned int best_error_ppm,
828 unsigned int *error_ppm)
829{
9ca3ba01
ID
830 /*
831 * For CHV ignore the error and consider only the P value.
832 * Prefer a bigger P value based on HW requirements.
833 */
834 if (IS_CHERRYVIEW(dev)) {
835 *error_ppm = 0;
836
837 return calculated_clock->p > best_clock->p;
838 }
839
24be4e46
ID
840 if (WARN_ON_ONCE(!target_freq))
841 return false;
842
d5dd62bd
ID
843 *error_ppm = div_u64(1000000ULL *
844 abs(target_freq - calculated_clock->dot),
845 target_freq);
846 /*
847 * Prefer a better P value over a better (smaller) error if the error
848 * is small. Ensure this preference for future configurations too by
849 * setting the error to 0.
850 */
851 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
852 *error_ppm = 0;
853
854 return true;
855 }
856
857 return *error_ppm + 10 < best_error_ppm;
858}
859
a0c4da24 860static bool
a93e255f
ACO
861vlv_find_best_dpll(const intel_limit_t *limit,
862 struct intel_crtc_state *crtc_state,
ee9300bb
DV
863 int target, int refclk, intel_clock_t *match_clock,
864 intel_clock_t *best_clock)
a0c4da24 865{
a93e255f 866 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 867 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 868 intel_clock_t clock;
69e4f900 869 unsigned int bestppm = 1000000;
27e639bf
VS
870 /* min update 19.2 MHz */
871 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 872 bool found = false;
a0c4da24 873
6b4bf1c4
VS
874 target *= 5; /* fast clock */
875
876 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
877
878 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 879 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 880 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 881 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 882 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 883 clock.p = clock.p1 * clock.p2;
a0c4da24 884 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 885 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 886 unsigned int ppm;
69e4f900 887
6b4bf1c4
VS
888 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
889 refclk * clock.m1);
890
891 vlv_clock(refclk, &clock);
43b0ac53 892
f01b7962
VS
893 if (!intel_PLL_is_valid(dev, limit,
894 &clock))
43b0ac53
VS
895 continue;
896
d5dd62bd
ID
897 if (!vlv_PLL_is_optimal(dev, target,
898 &clock,
899 best_clock,
900 bestppm, &ppm))
901 continue;
6b4bf1c4 902
d5dd62bd
ID
903 *best_clock = clock;
904 bestppm = ppm;
905 found = true;
a0c4da24
JB
906 }
907 }
908 }
909 }
a0c4da24 910
49e497ef 911 return found;
a0c4da24 912}
a4fc5ed6 913
ef9348c8 914static bool
a93e255f
ACO
915chv_find_best_dpll(const intel_limit_t *limit,
916 struct intel_crtc_state *crtc_state,
ef9348c8
CML
917 int target, int refclk, intel_clock_t *match_clock,
918 intel_clock_t *best_clock)
919{
a93e255f 920 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 921 struct drm_device *dev = crtc->base.dev;
9ca3ba01 922 unsigned int best_error_ppm;
ef9348c8
CML
923 intel_clock_t clock;
924 uint64_t m2;
925 int found = false;
926
927 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 928 best_error_ppm = 1000000;
ef9348c8
CML
929
930 /*
931 * Based on hardware doc, the n always set to 1, and m1 always
932 * set to 2. If requires to support 200Mhz refclk, we need to
933 * revisit this because n may not 1 anymore.
934 */
935 clock.n = 1, clock.m1 = 2;
936 target *= 5; /* fast clock */
937
938 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
939 for (clock.p2 = limit->p2.p2_fast;
940 clock.p2 >= limit->p2.p2_slow;
941 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 942 unsigned int error_ppm;
ef9348c8
CML
943
944 clock.p = clock.p1 * clock.p2;
945
946 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
947 clock.n) << 22, refclk * clock.m1);
948
949 if (m2 > INT_MAX/clock.m1)
950 continue;
951
952 clock.m2 = m2;
953
954 chv_clock(refclk, &clock);
955
956 if (!intel_PLL_is_valid(dev, limit, &clock))
957 continue;
958
9ca3ba01
ID
959 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
960 best_error_ppm, &error_ppm))
961 continue;
962
963 *best_clock = clock;
964 best_error_ppm = error_ppm;
965 found = true;
ef9348c8
CML
966 }
967 }
968
969 return found;
970}
971
5ab7b0b7
ID
972bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
973 intel_clock_t *best_clock)
974{
975 int refclk = i9xx_get_refclk(crtc_state, 0);
976
977 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
978 target_clock, refclk, NULL, best_clock);
979}
980
20ddf665
VS
981bool intel_crtc_active(struct drm_crtc *crtc)
982{
983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
984
985 /* Be paranoid as we can arrive here with only partial
986 * state retrieved from the hardware during setup.
987 *
241bfc38 988 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
989 * as Haswell has gained clock readout/fastboot support.
990 *
66e514c1 991 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 992 * properly reconstruct framebuffers.
c3d1f436
MR
993 *
994 * FIXME: The intel_crtc->active here should be switched to
995 * crtc->state->active once we have proper CRTC states wired up
996 * for atomic.
20ddf665 997 */
c3d1f436 998 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 999 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
1000}
1001
a5c961d1
PZ
1002enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1003 enum pipe pipe)
1004{
1005 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1007
6e3c9717 1008 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1009}
1010
fbf49ea2
VS
1011static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1012{
1013 struct drm_i915_private *dev_priv = dev->dev_private;
1014 u32 reg = PIPEDSL(pipe);
1015 u32 line1, line2;
1016 u32 line_mask;
1017
1018 if (IS_GEN2(dev))
1019 line_mask = DSL_LINEMASK_GEN2;
1020 else
1021 line_mask = DSL_LINEMASK_GEN3;
1022
1023 line1 = I915_READ(reg) & line_mask;
1024 mdelay(5);
1025 line2 = I915_READ(reg) & line_mask;
1026
1027 return line1 == line2;
1028}
1029
ab7ad7f6
KP
1030/*
1031 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1032 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1033 *
1034 * After disabling a pipe, we can't wait for vblank in the usual way,
1035 * spinning on the vblank interrupt status bit, since we won't actually
1036 * see an interrupt when the pipe is disabled.
1037 *
ab7ad7f6
KP
1038 * On Gen4 and above:
1039 * wait for the pipe register state bit to turn off
1040 *
1041 * Otherwise:
1042 * wait for the display line value to settle (it usually
1043 * ends up stopping at the start of the next frame).
58e10eb9 1044 *
9d0498a2 1045 */
575f7ab7 1046static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1047{
575f7ab7 1048 struct drm_device *dev = crtc->base.dev;
9d0498a2 1049 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1050 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1051 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1052
1053 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 1054 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1055
1056 /* Wait for the Pipe State to go off */
58e10eb9
CW
1057 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1058 100))
284637d9 1059 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1060 } else {
ab7ad7f6 1061 /* Wait for the display line to settle */
fbf49ea2 1062 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1063 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1064 }
79e53945
JB
1065}
1066
b0ea7d37
DL
1067/*
1068 * ibx_digital_port_connected - is the specified port connected?
1069 * @dev_priv: i915 private structure
1070 * @port: the port to test
1071 *
1072 * Returns true if @port is connected, false otherwise.
1073 */
1074bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1075 struct intel_digital_port *port)
1076{
1077 u32 bit;
1078
c36346e3 1079 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1080 switch (port->port) {
c36346e3
DL
1081 case PORT_B:
1082 bit = SDE_PORTB_HOTPLUG;
1083 break;
1084 case PORT_C:
1085 bit = SDE_PORTC_HOTPLUG;
1086 break;
1087 case PORT_D:
1088 bit = SDE_PORTD_HOTPLUG;
1089 break;
1090 default:
1091 return true;
1092 }
1093 } else {
eba905b2 1094 switch (port->port) {
c36346e3
DL
1095 case PORT_B:
1096 bit = SDE_PORTB_HOTPLUG_CPT;
1097 break;
1098 case PORT_C:
1099 bit = SDE_PORTC_HOTPLUG_CPT;
1100 break;
1101 case PORT_D:
1102 bit = SDE_PORTD_HOTPLUG_CPT;
1103 break;
1104 default:
1105 return true;
1106 }
b0ea7d37
DL
1107 }
1108
1109 return I915_READ(SDEISR) & bit;
1110}
1111
b24e7179
JB
1112static const char *state_string(bool enabled)
1113{
1114 return enabled ? "on" : "off";
1115}
1116
1117/* Only for pre-ILK configs */
55607e8a
DV
1118void assert_pll(struct drm_i915_private *dev_priv,
1119 enum pipe pipe, bool state)
b24e7179
JB
1120{
1121 int reg;
1122 u32 val;
1123 bool cur_state;
1124
1125 reg = DPLL(pipe);
1126 val = I915_READ(reg);
1127 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1128 I915_STATE_WARN(cur_state != state,
b24e7179
JB
1129 "PLL state assertion failure (expected %s, current %s)\n",
1130 state_string(state), state_string(cur_state));
1131}
b24e7179 1132
23538ef1
JN
1133/* XXX: the dsi pll is shared between MIPI DSI ports */
1134static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1135{
1136 u32 val;
1137 bool cur_state;
1138
1139 mutex_lock(&dev_priv->dpio_lock);
1140 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1141 mutex_unlock(&dev_priv->dpio_lock);
1142
1143 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1144 I915_STATE_WARN(cur_state != state,
23538ef1
JN
1145 "DSI PLL state assertion failure (expected %s, current %s)\n",
1146 state_string(state), state_string(cur_state));
1147}
1148#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1149#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1150
55607e8a 1151struct intel_shared_dpll *
e2b78267
DV
1152intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1153{
1154 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1155
6e3c9717 1156 if (crtc->config->shared_dpll < 0)
e2b78267
DV
1157 return NULL;
1158
6e3c9717 1159 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
e2b78267
DV
1160}
1161
040484af 1162/* For ILK+ */
55607e8a
DV
1163void assert_shared_dpll(struct drm_i915_private *dev_priv,
1164 struct intel_shared_dpll *pll,
1165 bool state)
040484af 1166{
040484af 1167 bool cur_state;
5358901f 1168 struct intel_dpll_hw_state hw_state;
040484af 1169
92b27b08 1170 if (WARN (!pll,
46edb027 1171 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1172 return;
ee7b9f93 1173
5358901f 1174 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
e2c719b7 1175 I915_STATE_WARN(cur_state != state,
5358901f
DV
1176 "%s assertion failure (expected %s, current %s)\n",
1177 pll->name, state_string(state), state_string(cur_state));
040484af 1178}
040484af
JB
1179
1180static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1181 enum pipe pipe, bool state)
1182{
1183 int reg;
1184 u32 val;
1185 bool cur_state;
ad80a810
PZ
1186 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1187 pipe);
040484af 1188
affa9354
PZ
1189 if (HAS_DDI(dev_priv->dev)) {
1190 /* DDI does not have a specific FDI_TX register */
ad80a810 1191 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1192 val = I915_READ(reg);
ad80a810 1193 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1194 } else {
1195 reg = FDI_TX_CTL(pipe);
1196 val = I915_READ(reg);
1197 cur_state = !!(val & FDI_TX_ENABLE);
1198 }
e2c719b7 1199 I915_STATE_WARN(cur_state != state,
040484af
JB
1200 "FDI TX state assertion failure (expected %s, current %s)\n",
1201 state_string(state), state_string(cur_state));
1202}
1203#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1204#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1205
1206static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1207 enum pipe pipe, bool state)
1208{
1209 int reg;
1210 u32 val;
1211 bool cur_state;
1212
d63fa0dc
PZ
1213 reg = FDI_RX_CTL(pipe);
1214 val = I915_READ(reg);
1215 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1216 I915_STATE_WARN(cur_state != state,
040484af
JB
1217 "FDI RX state assertion failure (expected %s, current %s)\n",
1218 state_string(state), state_string(cur_state));
1219}
1220#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1221#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1222
1223static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1224 enum pipe pipe)
1225{
1226 int reg;
1227 u32 val;
1228
1229 /* ILK FDI PLL is always enabled */
3d13ef2e 1230 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1231 return;
1232
bf507ef7 1233 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1234 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1235 return;
1236
040484af
JB
1237 reg = FDI_TX_CTL(pipe);
1238 val = I915_READ(reg);
e2c719b7 1239 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1240}
1241
55607e8a
DV
1242void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1243 enum pipe pipe, bool state)
040484af
JB
1244{
1245 int reg;
1246 u32 val;
55607e8a 1247 bool cur_state;
040484af
JB
1248
1249 reg = FDI_RX_CTL(pipe);
1250 val = I915_READ(reg);
55607e8a 1251 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1252 I915_STATE_WARN(cur_state != state,
55607e8a
DV
1253 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1254 state_string(state), state_string(cur_state));
040484af
JB
1255}
1256
b680c37a
DV
1257void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1258 enum pipe pipe)
ea0760cf 1259{
bedd4dba
JN
1260 struct drm_device *dev = dev_priv->dev;
1261 int pp_reg;
ea0760cf
JB
1262 u32 val;
1263 enum pipe panel_pipe = PIPE_A;
0de3b485 1264 bool locked = true;
ea0760cf 1265
bedd4dba
JN
1266 if (WARN_ON(HAS_DDI(dev)))
1267 return;
1268
1269 if (HAS_PCH_SPLIT(dev)) {
1270 u32 port_sel;
1271
ea0760cf 1272 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1273 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1274
1275 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1276 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1277 panel_pipe = PIPE_B;
1278 /* XXX: else fix for eDP */
1279 } else if (IS_VALLEYVIEW(dev)) {
1280 /* presumably write lock depends on pipe, not port select */
1281 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1282 panel_pipe = pipe;
ea0760cf
JB
1283 } else {
1284 pp_reg = PP_CONTROL;
bedd4dba
JN
1285 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1286 panel_pipe = PIPE_B;
ea0760cf
JB
1287 }
1288
1289 val = I915_READ(pp_reg);
1290 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1291 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1292 locked = false;
1293
e2c719b7 1294 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1295 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1296 pipe_name(pipe));
ea0760cf
JB
1297}
1298
93ce0ba6
JN
1299static void assert_cursor(struct drm_i915_private *dev_priv,
1300 enum pipe pipe, bool state)
1301{
1302 struct drm_device *dev = dev_priv->dev;
1303 bool cur_state;
1304
d9d82081 1305 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1306 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1307 else
5efb3e28 1308 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1309
e2c719b7 1310 I915_STATE_WARN(cur_state != state,
93ce0ba6
JN
1311 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1312 pipe_name(pipe), state_string(state), state_string(cur_state));
1313}
1314#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1315#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1316
b840d907
JB
1317void assert_pipe(struct drm_i915_private *dev_priv,
1318 enum pipe pipe, bool state)
b24e7179
JB
1319{
1320 int reg;
1321 u32 val;
63d7bbe9 1322 bool cur_state;
702e7a56
PZ
1323 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1324 pipe);
b24e7179 1325
b6b5d049
VS
1326 /* if we need the pipe quirk it must be always on */
1327 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1328 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1329 state = true;
1330
f458ebbc 1331 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1332 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1333 cur_state = false;
1334 } else {
1335 reg = PIPECONF(cpu_transcoder);
1336 val = I915_READ(reg);
1337 cur_state = !!(val & PIPECONF_ENABLE);
1338 }
1339
e2c719b7 1340 I915_STATE_WARN(cur_state != state,
63d7bbe9 1341 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1342 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1343}
1344
931872fc
CW
1345static void assert_plane(struct drm_i915_private *dev_priv,
1346 enum plane plane, bool state)
b24e7179
JB
1347{
1348 int reg;
1349 u32 val;
931872fc 1350 bool cur_state;
b24e7179
JB
1351
1352 reg = DSPCNTR(plane);
1353 val = I915_READ(reg);
931872fc 1354 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1355 I915_STATE_WARN(cur_state != state,
931872fc
CW
1356 "plane %c assertion failure (expected %s, current %s)\n",
1357 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1358}
1359
931872fc
CW
1360#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1361#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1362
b24e7179
JB
1363static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1364 enum pipe pipe)
1365{
653e1026 1366 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1367 int reg, i;
1368 u32 val;
1369 int cur_pipe;
1370
653e1026
VS
1371 /* Primary planes are fixed to pipes on gen4+ */
1372 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1373 reg = DSPCNTR(pipe);
1374 val = I915_READ(reg);
e2c719b7 1375 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1376 "plane %c assertion failure, should be disabled but not\n",
1377 plane_name(pipe));
19ec1358 1378 return;
28c05794 1379 }
19ec1358 1380
b24e7179 1381 /* Need to check both planes against the pipe */
055e393f 1382 for_each_pipe(dev_priv, i) {
b24e7179
JB
1383 reg = DSPCNTR(i);
1384 val = I915_READ(reg);
1385 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1386 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1387 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1388 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1389 plane_name(i), pipe_name(pipe));
b24e7179
JB
1390 }
1391}
1392
19332d7a
JB
1393static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1394 enum pipe pipe)
1395{
20674eef 1396 struct drm_device *dev = dev_priv->dev;
1fe47785 1397 int reg, sprite;
19332d7a
JB
1398 u32 val;
1399
7feb8b88 1400 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1401 for_each_sprite(dev_priv, pipe, sprite) {
7feb8b88 1402 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1403 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1404 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1405 sprite, pipe_name(pipe));
1406 }
1407 } else if (IS_VALLEYVIEW(dev)) {
3bdcfc0c 1408 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 1409 reg = SPCNTR(pipe, sprite);
20674eef 1410 val = I915_READ(reg);
e2c719b7 1411 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1412 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1413 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1414 }
1415 } else if (INTEL_INFO(dev)->gen >= 7) {
1416 reg = SPRCTL(pipe);
19332d7a 1417 val = I915_READ(reg);
e2c719b7 1418 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1419 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1420 plane_name(pipe), pipe_name(pipe));
1421 } else if (INTEL_INFO(dev)->gen >= 5) {
1422 reg = DVSCNTR(pipe);
19332d7a 1423 val = I915_READ(reg);
e2c719b7 1424 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1425 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1426 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1427 }
1428}
1429
08c71e5e
VS
1430static void assert_vblank_disabled(struct drm_crtc *crtc)
1431{
e2c719b7 1432 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1433 drm_crtc_vblank_put(crtc);
1434}
1435
89eff4be 1436static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1437{
1438 u32 val;
1439 bool enabled;
1440
e2c719b7 1441 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1442
92f2584a
JB
1443 val = I915_READ(PCH_DREF_CONTROL);
1444 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1445 DREF_SUPERSPREAD_SOURCE_MASK));
e2c719b7 1446 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
92f2584a
JB
1447}
1448
ab9412ba
DV
1449static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1450 enum pipe pipe)
92f2584a
JB
1451{
1452 int reg;
1453 u32 val;
1454 bool enabled;
1455
ab9412ba 1456 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1457 val = I915_READ(reg);
1458 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1459 I915_STATE_WARN(enabled,
9db4a9c7
JB
1460 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1461 pipe_name(pipe));
92f2584a
JB
1462}
1463
4e634389
KP
1464static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1465 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1466{
1467 if ((val & DP_PORT_EN) == 0)
1468 return false;
1469
1470 if (HAS_PCH_CPT(dev_priv->dev)) {
1471 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1472 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1473 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1474 return false;
44f37d1f
CML
1475 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1476 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1477 return false;
f0575e92
KP
1478 } else {
1479 if ((val & DP_PIPE_MASK) != (pipe << 30))
1480 return false;
1481 }
1482 return true;
1483}
1484
1519b995
KP
1485static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1486 enum pipe pipe, u32 val)
1487{
dc0fa718 1488 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1489 return false;
1490
1491 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1492 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1493 return false;
44f37d1f
CML
1494 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1495 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1496 return false;
1519b995 1497 } else {
dc0fa718 1498 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1499 return false;
1500 }
1501 return true;
1502}
1503
1504static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1505 enum pipe pipe, u32 val)
1506{
1507 if ((val & LVDS_PORT_EN) == 0)
1508 return false;
1509
1510 if (HAS_PCH_CPT(dev_priv->dev)) {
1511 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1512 return false;
1513 } else {
1514 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1515 return false;
1516 }
1517 return true;
1518}
1519
1520static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1521 enum pipe pipe, u32 val)
1522{
1523 if ((val & ADPA_DAC_ENABLE) == 0)
1524 return false;
1525 if (HAS_PCH_CPT(dev_priv->dev)) {
1526 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1527 return false;
1528 } else {
1529 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1530 return false;
1531 }
1532 return true;
1533}
1534
291906f1 1535static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1536 enum pipe pipe, int reg, u32 port_sel)
291906f1 1537{
47a05eca 1538 u32 val = I915_READ(reg);
e2c719b7 1539 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1540 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1541 reg, pipe_name(pipe));
de9a35ab 1542
e2c719b7 1543 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1544 && (val & DP_PIPEB_SELECT),
de9a35ab 1545 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1546}
1547
1548static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1549 enum pipe pipe, int reg)
1550{
47a05eca 1551 u32 val = I915_READ(reg);
e2c719b7 1552 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1553 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1554 reg, pipe_name(pipe));
de9a35ab 1555
e2c719b7 1556 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1557 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1558 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1559}
1560
1561static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1562 enum pipe pipe)
1563{
1564 int reg;
1565 u32 val;
291906f1 1566
f0575e92
KP
1567 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1568 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1569 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1570
1571 reg = PCH_ADPA;
1572 val = I915_READ(reg);
e2c719b7 1573 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1574 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1575 pipe_name(pipe));
291906f1
JB
1576
1577 reg = PCH_LVDS;
1578 val = I915_READ(reg);
e2c719b7 1579 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1580 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1581 pipe_name(pipe));
291906f1 1582
e2debe91
PZ
1583 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1584 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1585 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1586}
1587
40e9cf64
JB
1588static void intel_init_dpio(struct drm_device *dev)
1589{
1590 struct drm_i915_private *dev_priv = dev->dev_private;
1591
1592 if (!IS_VALLEYVIEW(dev))
1593 return;
1594
a09caddd
CML
1595 /*
1596 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1597 * CHV x1 PHY (DP/HDMI D)
1598 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1599 */
1600 if (IS_CHERRYVIEW(dev)) {
1601 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1602 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1603 } else {
1604 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1605 }
5382f5f3
JB
1606}
1607
d288f65f 1608static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1609 const struct intel_crtc_state *pipe_config)
87442f73 1610{
426115cf
DV
1611 struct drm_device *dev = crtc->base.dev;
1612 struct drm_i915_private *dev_priv = dev->dev_private;
1613 int reg = DPLL(crtc->pipe);
d288f65f 1614 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1615
426115cf 1616 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1617
1618 /* No really, not for ILK+ */
1619 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1620
1621 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1622 if (IS_MOBILE(dev_priv->dev))
426115cf 1623 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1624
426115cf
DV
1625 I915_WRITE(reg, dpll);
1626 POSTING_READ(reg);
1627 udelay(150);
1628
1629 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1630 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1631
d288f65f 1632 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1633 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1634
1635 /* We do this three times for luck */
426115cf 1636 I915_WRITE(reg, dpll);
87442f73
DV
1637 POSTING_READ(reg);
1638 udelay(150); /* wait for warmup */
426115cf 1639 I915_WRITE(reg, dpll);
87442f73
DV
1640 POSTING_READ(reg);
1641 udelay(150); /* wait for warmup */
426115cf 1642 I915_WRITE(reg, dpll);
87442f73
DV
1643 POSTING_READ(reg);
1644 udelay(150); /* wait for warmup */
1645}
1646
d288f65f 1647static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1648 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1649{
1650 struct drm_device *dev = crtc->base.dev;
1651 struct drm_i915_private *dev_priv = dev->dev_private;
1652 int pipe = crtc->pipe;
1653 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1654 u32 tmp;
1655
1656 assert_pipe_disabled(dev_priv, crtc->pipe);
1657
1658 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1659
1660 mutex_lock(&dev_priv->dpio_lock);
1661
1662 /* Enable back the 10bit clock to display controller */
1663 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1664 tmp |= DPIO_DCLKP_EN;
1665 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1666
1667 /*
1668 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1669 */
1670 udelay(1);
1671
1672 /* Enable PLL */
d288f65f 1673 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1674
1675 /* Check PLL is locked */
a11b0703 1676 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1677 DRM_ERROR("PLL %d failed to lock\n", pipe);
1678
a11b0703 1679 /* not sure when this should be written */
d288f65f 1680 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703
VS
1681 POSTING_READ(DPLL_MD(pipe));
1682
9d556c99
CML
1683 mutex_unlock(&dev_priv->dpio_lock);
1684}
1685
1c4e0274
VS
1686static int intel_num_dvo_pipes(struct drm_device *dev)
1687{
1688 struct intel_crtc *crtc;
1689 int count = 0;
1690
1691 for_each_intel_crtc(dev, crtc)
1692 count += crtc->active &&
409ee761 1693 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1694
1695 return count;
1696}
1697
66e3d5c0 1698static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1699{
66e3d5c0
DV
1700 struct drm_device *dev = crtc->base.dev;
1701 struct drm_i915_private *dev_priv = dev->dev_private;
1702 int reg = DPLL(crtc->pipe);
6e3c9717 1703 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1704
66e3d5c0 1705 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1706
63d7bbe9 1707 /* No really, not for ILK+ */
3d13ef2e 1708 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1709
1710 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1711 if (IS_MOBILE(dev) && !IS_I830(dev))
1712 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1713
1c4e0274
VS
1714 /* Enable DVO 2x clock on both PLLs if necessary */
1715 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1716 /*
1717 * It appears to be important that we don't enable this
1718 * for the current pipe before otherwise configuring the
1719 * PLL. No idea how this should be handled if multiple
1720 * DVO outputs are enabled simultaneosly.
1721 */
1722 dpll |= DPLL_DVO_2X_MODE;
1723 I915_WRITE(DPLL(!crtc->pipe),
1724 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1725 }
66e3d5c0
DV
1726
1727 /* Wait for the clocks to stabilize. */
1728 POSTING_READ(reg);
1729 udelay(150);
1730
1731 if (INTEL_INFO(dev)->gen >= 4) {
1732 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1733 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1734 } else {
1735 /* The pixel multiplier can only be updated once the
1736 * DPLL is enabled and the clocks are stable.
1737 *
1738 * So write it again.
1739 */
1740 I915_WRITE(reg, dpll);
1741 }
63d7bbe9
JB
1742
1743 /* We do this three times for luck */
66e3d5c0 1744 I915_WRITE(reg, dpll);
63d7bbe9
JB
1745 POSTING_READ(reg);
1746 udelay(150); /* wait for warmup */
66e3d5c0 1747 I915_WRITE(reg, dpll);
63d7bbe9
JB
1748 POSTING_READ(reg);
1749 udelay(150); /* wait for warmup */
66e3d5c0 1750 I915_WRITE(reg, dpll);
63d7bbe9
JB
1751 POSTING_READ(reg);
1752 udelay(150); /* wait for warmup */
1753}
1754
1755/**
50b44a44 1756 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1757 * @dev_priv: i915 private structure
1758 * @pipe: pipe PLL to disable
1759 *
1760 * Disable the PLL for @pipe, making sure the pipe is off first.
1761 *
1762 * Note! This is for pre-ILK only.
1763 */
1c4e0274 1764static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1765{
1c4e0274
VS
1766 struct drm_device *dev = crtc->base.dev;
1767 struct drm_i915_private *dev_priv = dev->dev_private;
1768 enum pipe pipe = crtc->pipe;
1769
1770 /* Disable DVO 2x clock on both PLLs if necessary */
1771 if (IS_I830(dev) &&
409ee761 1772 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
1c4e0274
VS
1773 intel_num_dvo_pipes(dev) == 1) {
1774 I915_WRITE(DPLL(PIPE_B),
1775 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1776 I915_WRITE(DPLL(PIPE_A),
1777 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1778 }
1779
b6b5d049
VS
1780 /* Don't disable pipe or pipe PLLs if needed */
1781 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1782 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1783 return;
1784
1785 /* Make sure the pipe isn't still relying on us */
1786 assert_pipe_disabled(dev_priv, pipe);
1787
50b44a44
DV
1788 I915_WRITE(DPLL(pipe), 0);
1789 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1790}
1791
f6071166
JB
1792static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1793{
1794 u32 val = 0;
1795
1796 /* Make sure the pipe isn't still relying on us */
1797 assert_pipe_disabled(dev_priv, pipe);
1798
e5cbfbfb
ID
1799 /*
1800 * Leave integrated clock source and reference clock enabled for pipe B.
1801 * The latter is needed for VGA hotplug / manual detection.
1802 */
f6071166 1803 if (pipe == PIPE_B)
e5cbfbfb 1804 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1805 I915_WRITE(DPLL(pipe), val);
1806 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1807
1808}
1809
1810static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1811{
d752048d 1812 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1813 u32 val;
1814
a11b0703
VS
1815 /* Make sure the pipe isn't still relying on us */
1816 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1817
a11b0703 1818 /* Set PLL en = 0 */
d17ec4ce 1819 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1820 if (pipe != PIPE_A)
1821 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1822 I915_WRITE(DPLL(pipe), val);
1823 POSTING_READ(DPLL(pipe));
d752048d
VS
1824
1825 mutex_lock(&dev_priv->dpio_lock);
1826
1827 /* Disable 10bit clock to display controller */
1828 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1829 val &= ~DPIO_DCLKP_EN;
1830 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1831
61407f6d
VS
1832 /* disable left/right clock distribution */
1833 if (pipe != PIPE_B) {
1834 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1835 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1836 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1837 } else {
1838 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1839 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1840 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1841 }
1842
d752048d 1843 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1844}
1845
e4607fcf
CML
1846void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1847 struct intel_digital_port *dport)
89b667f8
JB
1848{
1849 u32 port_mask;
00fc31b7 1850 int dpll_reg;
89b667f8 1851
e4607fcf
CML
1852 switch (dport->port) {
1853 case PORT_B:
89b667f8 1854 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1855 dpll_reg = DPLL(0);
e4607fcf
CML
1856 break;
1857 case PORT_C:
89b667f8 1858 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7
CML
1859 dpll_reg = DPLL(0);
1860 break;
1861 case PORT_D:
1862 port_mask = DPLL_PORTD_READY_MASK;
1863 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1864 break;
1865 default:
1866 BUG();
1867 }
89b667f8 1868
00fc31b7 1869 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
89b667f8 1870 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
00fc31b7 1871 port_name(dport->port), I915_READ(dpll_reg));
89b667f8
JB
1872}
1873
b14b1055
DV
1874static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1875{
1876 struct drm_device *dev = crtc->base.dev;
1877 struct drm_i915_private *dev_priv = dev->dev_private;
1878 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1879
be19f0ff
CW
1880 if (WARN_ON(pll == NULL))
1881 return;
1882
3e369b76 1883 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1884 if (pll->active == 0) {
1885 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1886 WARN_ON(pll->on);
1887 assert_shared_dpll_disabled(dev_priv, pll);
1888
1889 pll->mode_set(dev_priv, pll);
1890 }
1891}
1892
92f2584a 1893/**
85b3894f 1894 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1895 * @dev_priv: i915 private structure
1896 * @pipe: pipe PLL to enable
1897 *
1898 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1899 * drives the transcoder clock.
1900 */
85b3894f 1901static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1902{
3d13ef2e
DL
1903 struct drm_device *dev = crtc->base.dev;
1904 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1905 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1906
87a875bb 1907 if (WARN_ON(pll == NULL))
48da64a8
CW
1908 return;
1909
3e369b76 1910 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1911 return;
ee7b9f93 1912
74dd6928 1913 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1914 pll->name, pll->active, pll->on,
e2b78267 1915 crtc->base.base.id);
92f2584a 1916
cdbd2316
DV
1917 if (pll->active++) {
1918 WARN_ON(!pll->on);
e9d6944e 1919 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1920 return;
1921 }
f4a091c7 1922 WARN_ON(pll->on);
ee7b9f93 1923
bd2bb1b9
PZ
1924 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1925
46edb027 1926 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1927 pll->enable(dev_priv, pll);
ee7b9f93 1928 pll->on = true;
92f2584a
JB
1929}
1930
f6daaec2 1931static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1932{
3d13ef2e
DL
1933 struct drm_device *dev = crtc->base.dev;
1934 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1935 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1936
92f2584a 1937 /* PCH only available on ILK+ */
3d13ef2e 1938 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1939 if (WARN_ON(pll == NULL))
ee7b9f93 1940 return;
92f2584a 1941
3e369b76 1942 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1943 return;
7a419866 1944
46edb027
DV
1945 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1946 pll->name, pll->active, pll->on,
e2b78267 1947 crtc->base.base.id);
7a419866 1948
48da64a8 1949 if (WARN_ON(pll->active == 0)) {
e9d6944e 1950 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1951 return;
1952 }
1953
e9d6944e 1954 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1955 WARN_ON(!pll->on);
cdbd2316 1956 if (--pll->active)
7a419866 1957 return;
ee7b9f93 1958
46edb027 1959 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1960 pll->disable(dev_priv, pll);
ee7b9f93 1961 pll->on = false;
bd2bb1b9
PZ
1962
1963 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1964}
1965
b8a4f404
PZ
1966static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1967 enum pipe pipe)
040484af 1968{
23670b32 1969 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1970 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1971 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1972 uint32_t reg, val, pipeconf_val;
040484af
JB
1973
1974 /* PCH only available on ILK+ */
55522f37 1975 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1976
1977 /* Make sure PCH DPLL is enabled */
e72f9fbf 1978 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1979 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1980
1981 /* FDI must be feeding us bits for PCH ports */
1982 assert_fdi_tx_enabled(dev_priv, pipe);
1983 assert_fdi_rx_enabled(dev_priv, pipe);
1984
23670b32
DV
1985 if (HAS_PCH_CPT(dev)) {
1986 /* Workaround: Set the timing override bit before enabling the
1987 * pch transcoder. */
1988 reg = TRANS_CHICKEN2(pipe);
1989 val = I915_READ(reg);
1990 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1991 I915_WRITE(reg, val);
59c859d6 1992 }
23670b32 1993
ab9412ba 1994 reg = PCH_TRANSCONF(pipe);
040484af 1995 val = I915_READ(reg);
5f7f726d 1996 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1997
1998 if (HAS_PCH_IBX(dev_priv->dev)) {
1999 /*
2000 * make the BPC in transcoder be consistent with
2001 * that in pipeconf reg.
2002 */
dfd07d72
DV
2003 val &= ~PIPECONF_BPC_MASK;
2004 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 2005 }
5f7f726d
PZ
2006
2007 val &= ~TRANS_INTERLACE_MASK;
2008 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 2009 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 2010 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
2011 val |= TRANS_LEGACY_INTERLACED_ILK;
2012 else
2013 val |= TRANS_INTERLACED;
5f7f726d
PZ
2014 else
2015 val |= TRANS_PROGRESSIVE;
2016
040484af
JB
2017 I915_WRITE(reg, val | TRANS_ENABLE);
2018 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 2019 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
2020}
2021
8fb033d7 2022static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 2023 enum transcoder cpu_transcoder)
040484af 2024{
8fb033d7 2025 u32 val, pipeconf_val;
8fb033d7
PZ
2026
2027 /* PCH only available on ILK+ */
55522f37 2028 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 2029
8fb033d7 2030 /* FDI must be feeding us bits for PCH ports */
1a240d4d 2031 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 2032 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 2033
223a6fdf
PZ
2034 /* Workaround: set timing override bit. */
2035 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2036 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
2037 I915_WRITE(_TRANSA_CHICKEN2, val);
2038
25f3ef11 2039 val = TRANS_ENABLE;
937bb610 2040 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 2041
9a76b1c6
PZ
2042 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2043 PIPECONF_INTERLACED_ILK)
a35f2679 2044 val |= TRANS_INTERLACED;
8fb033d7
PZ
2045 else
2046 val |= TRANS_PROGRESSIVE;
2047
ab9412ba
DV
2048 I915_WRITE(LPT_TRANSCONF, val);
2049 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 2050 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
2051}
2052
b8a4f404
PZ
2053static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2054 enum pipe pipe)
040484af 2055{
23670b32
DV
2056 struct drm_device *dev = dev_priv->dev;
2057 uint32_t reg, val;
040484af
JB
2058
2059 /* FDI relies on the transcoder */
2060 assert_fdi_tx_disabled(dev_priv, pipe);
2061 assert_fdi_rx_disabled(dev_priv, pipe);
2062
291906f1
JB
2063 /* Ports must be off as well */
2064 assert_pch_ports_disabled(dev_priv, pipe);
2065
ab9412ba 2066 reg = PCH_TRANSCONF(pipe);
040484af
JB
2067 val = I915_READ(reg);
2068 val &= ~TRANS_ENABLE;
2069 I915_WRITE(reg, val);
2070 /* wait for PCH transcoder off, transcoder state */
2071 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 2072 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
2073
2074 if (!HAS_PCH_IBX(dev)) {
2075 /* Workaround: Clear the timing override chicken bit again. */
2076 reg = TRANS_CHICKEN2(pipe);
2077 val = I915_READ(reg);
2078 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2079 I915_WRITE(reg, val);
2080 }
040484af
JB
2081}
2082
ab4d966c 2083static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 2084{
8fb033d7
PZ
2085 u32 val;
2086
ab9412ba 2087 val = I915_READ(LPT_TRANSCONF);
8fb033d7 2088 val &= ~TRANS_ENABLE;
ab9412ba 2089 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2090 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2091 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2092 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2093
2094 /* Workaround: clear timing override bit. */
2095 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2096 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2097 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2098}
2099
b24e7179 2100/**
309cfea8 2101 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2102 * @crtc: crtc responsible for the pipe
b24e7179 2103 *
0372264a 2104 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2105 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2106 */
e1fdc473 2107static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2108{
0372264a
PZ
2109 struct drm_device *dev = crtc->base.dev;
2110 struct drm_i915_private *dev_priv = dev->dev_private;
2111 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2112 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2113 pipe);
1a240d4d 2114 enum pipe pch_transcoder;
b24e7179
JB
2115 int reg;
2116 u32 val;
2117
58c6eaa2 2118 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2119 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2120 assert_sprites_disabled(dev_priv, pipe);
2121
681e5811 2122 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2123 pch_transcoder = TRANSCODER_A;
2124 else
2125 pch_transcoder = pipe;
2126
b24e7179
JB
2127 /*
2128 * A pipe without a PLL won't actually be able to drive bits from
2129 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2130 * need the check.
2131 */
50360403 2132 if (HAS_GMCH_DISPLAY(dev_priv->dev))
409ee761 2133 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2134 assert_dsi_pll_enabled(dev_priv);
2135 else
2136 assert_pll_enabled(dev_priv, pipe);
040484af 2137 else {
6e3c9717 2138 if (crtc->config->has_pch_encoder) {
040484af 2139 /* if driving the PCH, we need FDI enabled */
cc391bbb 2140 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2141 assert_fdi_tx_pll_enabled(dev_priv,
2142 (enum pipe) cpu_transcoder);
040484af
JB
2143 }
2144 /* FIXME: assert CPU port conditions for SNB+ */
2145 }
b24e7179 2146
702e7a56 2147 reg = PIPECONF(cpu_transcoder);
b24e7179 2148 val = I915_READ(reg);
7ad25d48 2149 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2150 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2151 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2152 return;
7ad25d48 2153 }
00d70b15
CW
2154
2155 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2156 POSTING_READ(reg);
b24e7179
JB
2157}
2158
2159/**
309cfea8 2160 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2161 * @crtc: crtc whose pipes is to be disabled
b24e7179 2162 *
575f7ab7
VS
2163 * Disable the pipe of @crtc, making sure that various hardware
2164 * specific requirements are met, if applicable, e.g. plane
2165 * disabled, panel fitter off, etc.
b24e7179
JB
2166 *
2167 * Will wait until the pipe has shut down before returning.
2168 */
575f7ab7 2169static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2170{
575f7ab7 2171 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2172 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2173 enum pipe pipe = crtc->pipe;
b24e7179
JB
2174 int reg;
2175 u32 val;
2176
2177 /*
2178 * Make sure planes won't keep trying to pump pixels to us,
2179 * or we might hang the display.
2180 */
2181 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2182 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2183 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2184
702e7a56 2185 reg = PIPECONF(cpu_transcoder);
b24e7179 2186 val = I915_READ(reg);
00d70b15
CW
2187 if ((val & PIPECONF_ENABLE) == 0)
2188 return;
2189
67adc644
VS
2190 /*
2191 * Double wide has implications for planes
2192 * so best keep it disabled when not needed.
2193 */
6e3c9717 2194 if (crtc->config->double_wide)
67adc644
VS
2195 val &= ~PIPECONF_DOUBLE_WIDE;
2196
2197 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2198 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2199 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2200 val &= ~PIPECONF_ENABLE;
2201
2202 I915_WRITE(reg, val);
2203 if ((val & PIPECONF_ENABLE) == 0)
2204 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2205}
2206
d74362c9
KP
2207/*
2208 * Plane regs are double buffered, going from enabled->disabled needs a
2209 * trigger in order to latch. The display address reg provides this.
2210 */
1dba99f4
VS
2211void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2212 enum plane plane)
d74362c9 2213{
3d13ef2e
DL
2214 struct drm_device *dev = dev_priv->dev;
2215 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2216
2217 I915_WRITE(reg, I915_READ(reg));
2218 POSTING_READ(reg);
d74362c9
KP
2219}
2220
b24e7179 2221/**
262ca2b0 2222 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2223 * @plane: plane to be enabled
2224 * @crtc: crtc for the plane
b24e7179 2225 *
fdd508a6 2226 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2227 */
fdd508a6
VS
2228static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2229 struct drm_crtc *crtc)
b24e7179 2230{
fdd508a6
VS
2231 struct drm_device *dev = plane->dev;
2232 struct drm_i915_private *dev_priv = dev->dev_private;
2233 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2234
2235 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2236 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b70709a6 2237 to_intel_plane_state(plane->state)->visible = true;
939c2fe8 2238
fdd508a6
VS
2239 dev_priv->display.update_primary_plane(crtc, plane->fb,
2240 crtc->x, crtc->y);
b24e7179
JB
2241}
2242
693db184
CW
2243static bool need_vtd_wa(struct drm_device *dev)
2244{
2245#ifdef CONFIG_INTEL_IOMMU
2246 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2247 return true;
2248#endif
2249 return false;
2250}
2251
50470bb0 2252unsigned int
6761dd31
TU
2253intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2254 uint64_t fb_format_modifier)
a57ce0b2 2255{
6761dd31
TU
2256 unsigned int tile_height;
2257 uint32_t pixel_bytes;
a57ce0b2 2258
b5d0e9bf
DL
2259 switch (fb_format_modifier) {
2260 case DRM_FORMAT_MOD_NONE:
2261 tile_height = 1;
2262 break;
2263 case I915_FORMAT_MOD_X_TILED:
2264 tile_height = IS_GEN2(dev) ? 16 : 8;
2265 break;
2266 case I915_FORMAT_MOD_Y_TILED:
2267 tile_height = 32;
2268 break;
2269 case I915_FORMAT_MOD_Yf_TILED:
6761dd31
TU
2270 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2271 switch (pixel_bytes) {
b5d0e9bf 2272 default:
6761dd31 2273 case 1:
b5d0e9bf
DL
2274 tile_height = 64;
2275 break;
6761dd31
TU
2276 case 2:
2277 case 4:
b5d0e9bf
DL
2278 tile_height = 32;
2279 break;
6761dd31 2280 case 8:
b5d0e9bf
DL
2281 tile_height = 16;
2282 break;
6761dd31 2283 case 16:
b5d0e9bf
DL
2284 WARN_ONCE(1,
2285 "128-bit pixels are not supported for display!");
2286 tile_height = 16;
2287 break;
2288 }
2289 break;
2290 default:
2291 MISSING_CASE(fb_format_modifier);
2292 tile_height = 1;
2293 break;
2294 }
091df6cb 2295
6761dd31
TU
2296 return tile_height;
2297}
2298
2299unsigned int
2300intel_fb_align_height(struct drm_device *dev, unsigned int height,
2301 uint32_t pixel_format, uint64_t fb_format_modifier)
2302{
2303 return ALIGN(height, intel_tile_height(dev, pixel_format,
2304 fb_format_modifier));
a57ce0b2
JB
2305}
2306
f64b98cd
TU
2307static int
2308intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2309 const struct drm_plane_state *plane_state)
2310{
50470bb0 2311 struct intel_rotation_info *info = &view->rotation_info;
50470bb0 2312
f64b98cd
TU
2313 *view = i915_ggtt_view_normal;
2314
50470bb0
TU
2315 if (!plane_state)
2316 return 0;
2317
121920fa 2318 if (!intel_rotation_90_or_270(plane_state->rotation))
50470bb0
TU
2319 return 0;
2320
9abc4648 2321 *view = i915_ggtt_view_rotated;
50470bb0
TU
2322
2323 info->height = fb->height;
2324 info->pixel_format = fb->pixel_format;
2325 info->pitch = fb->pitches[0];
2326 info->fb_modifier = fb->modifier[0];
2327
f64b98cd
TU
2328 return 0;
2329}
2330
127bd2ac 2331int
850c4cdc
TU
2332intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2333 struct drm_framebuffer *fb,
82bc3b2d 2334 const struct drm_plane_state *plane_state,
a4872ba6 2335 struct intel_engine_cs *pipelined)
6b95a207 2336{
850c4cdc 2337 struct drm_device *dev = fb->dev;
ce453d81 2338 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2339 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2340 struct i915_ggtt_view view;
6b95a207
KH
2341 u32 alignment;
2342 int ret;
2343
ebcdd39e
MR
2344 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2345
7b911adc
TU
2346 switch (fb->modifier[0]) {
2347 case DRM_FORMAT_MOD_NONE:
1fada4cc
DL
2348 if (INTEL_INFO(dev)->gen >= 9)
2349 alignment = 256 * 1024;
2350 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
534843da 2351 alignment = 128 * 1024;
a6c45cf0 2352 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2353 alignment = 4 * 1024;
2354 else
2355 alignment = 64 * 1024;
6b95a207 2356 break;
7b911adc 2357 case I915_FORMAT_MOD_X_TILED:
1fada4cc
DL
2358 if (INTEL_INFO(dev)->gen >= 9)
2359 alignment = 256 * 1024;
2360 else {
2361 /* pin() will align the object as required by fence */
2362 alignment = 0;
2363 }
6b95a207 2364 break;
7b911adc 2365 case I915_FORMAT_MOD_Y_TILED:
1327b9a1
DL
2366 case I915_FORMAT_MOD_Yf_TILED:
2367 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2368 "Y tiling bo slipped through, driver bug!\n"))
2369 return -EINVAL;
2370 alignment = 1 * 1024 * 1024;
2371 break;
6b95a207 2372 default:
7b911adc
TU
2373 MISSING_CASE(fb->modifier[0]);
2374 return -EINVAL;
6b95a207
KH
2375 }
2376
f64b98cd
TU
2377 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2378 if (ret)
2379 return ret;
2380
693db184
CW
2381 /* Note that the w/a also requires 64 PTE of padding following the
2382 * bo. We currently fill all unused PTE with the shadow page and so
2383 * we should always have valid PTE following the scanout preventing
2384 * the VT-d warning.
2385 */
2386 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2387 alignment = 256 * 1024;
2388
d6dd6843
PZ
2389 /*
2390 * Global gtt pte registers are special registers which actually forward
2391 * writes to a chunk of system memory. Which means that there is no risk
2392 * that the register values disappear as soon as we call
2393 * intel_runtime_pm_put(), so it is correct to wrap only the
2394 * pin/unpin/fence and not more.
2395 */
2396 intel_runtime_pm_get(dev_priv);
2397
ce453d81 2398 dev_priv->mm.interruptible = false;
e6617330 2399 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
f64b98cd 2400 &view);
48b956c5 2401 if (ret)
ce453d81 2402 goto err_interruptible;
6b95a207
KH
2403
2404 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2405 * fence, whereas 965+ only requires a fence if using
2406 * framebuffer compression. For simplicity, we always install
2407 * a fence as the cost is not that onerous.
2408 */
06d98131 2409 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2410 if (ret)
2411 goto err_unpin;
1690e1eb 2412
9a5a53b3 2413 i915_gem_object_pin_fence(obj);
6b95a207 2414
ce453d81 2415 dev_priv->mm.interruptible = true;
d6dd6843 2416 intel_runtime_pm_put(dev_priv);
6b95a207 2417 return 0;
48b956c5
CW
2418
2419err_unpin:
f64b98cd 2420 i915_gem_object_unpin_from_display_plane(obj, &view);
ce453d81
CW
2421err_interruptible:
2422 dev_priv->mm.interruptible = true;
d6dd6843 2423 intel_runtime_pm_put(dev_priv);
48b956c5 2424 return ret;
6b95a207
KH
2425}
2426
82bc3b2d
TU
2427static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2428 const struct drm_plane_state *plane_state)
1690e1eb 2429{
82bc3b2d 2430 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd
TU
2431 struct i915_ggtt_view view;
2432 int ret;
82bc3b2d 2433
ebcdd39e
MR
2434 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2435
f64b98cd
TU
2436 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2437 WARN_ONCE(ret, "Couldn't get view from plane state!");
2438
1690e1eb 2439 i915_gem_object_unpin_fence(obj);
f64b98cd 2440 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2441}
2442
c2c75131
DV
2443/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2444 * is assumed to be a power-of-two. */
bc752862
CW
2445unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2446 unsigned int tiling_mode,
2447 unsigned int cpp,
2448 unsigned int pitch)
c2c75131 2449{
bc752862
CW
2450 if (tiling_mode != I915_TILING_NONE) {
2451 unsigned int tile_rows, tiles;
c2c75131 2452
bc752862
CW
2453 tile_rows = *y / 8;
2454 *y %= 8;
c2c75131 2455
bc752862
CW
2456 tiles = *x / (512/cpp);
2457 *x %= 512/cpp;
2458
2459 return tile_rows * pitch * 8 + tiles * 4096;
2460 } else {
2461 unsigned int offset;
2462
2463 offset = *y * pitch + *x * cpp;
2464 *y = 0;
2465 *x = (offset & 4095) / cpp;
2466 return offset & -4096;
2467 }
c2c75131
DV
2468}
2469
b35d63fa 2470static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2471{
2472 switch (format) {
2473 case DISPPLANE_8BPP:
2474 return DRM_FORMAT_C8;
2475 case DISPPLANE_BGRX555:
2476 return DRM_FORMAT_XRGB1555;
2477 case DISPPLANE_BGRX565:
2478 return DRM_FORMAT_RGB565;
2479 default:
2480 case DISPPLANE_BGRX888:
2481 return DRM_FORMAT_XRGB8888;
2482 case DISPPLANE_RGBX888:
2483 return DRM_FORMAT_XBGR8888;
2484 case DISPPLANE_BGRX101010:
2485 return DRM_FORMAT_XRGB2101010;
2486 case DISPPLANE_RGBX101010:
2487 return DRM_FORMAT_XBGR2101010;
2488 }
2489}
2490
bc8d7dff
DL
2491static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2492{
2493 switch (format) {
2494 case PLANE_CTL_FORMAT_RGB_565:
2495 return DRM_FORMAT_RGB565;
2496 default:
2497 case PLANE_CTL_FORMAT_XRGB_8888:
2498 if (rgb_order) {
2499 if (alpha)
2500 return DRM_FORMAT_ABGR8888;
2501 else
2502 return DRM_FORMAT_XBGR8888;
2503 } else {
2504 if (alpha)
2505 return DRM_FORMAT_ARGB8888;
2506 else
2507 return DRM_FORMAT_XRGB8888;
2508 }
2509 case PLANE_CTL_FORMAT_XRGB_2101010:
2510 if (rgb_order)
2511 return DRM_FORMAT_XBGR2101010;
2512 else
2513 return DRM_FORMAT_XRGB2101010;
2514 }
2515}
2516
5724dbd1 2517static bool
f6936e29
DV
2518intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2519 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2520{
2521 struct drm_device *dev = crtc->base.dev;
2522 struct drm_i915_gem_object *obj = NULL;
2523 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2524 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2525 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2526 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2527 PAGE_SIZE);
2528
2529 size_aligned -= base_aligned;
46f297fb 2530
ff2652ea
CW
2531 if (plane_config->size == 0)
2532 return false;
2533
f37b5c2b
DV
2534 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2535 base_aligned,
2536 base_aligned,
2537 size_aligned);
46f297fb 2538 if (!obj)
484b41dd 2539 return false;
46f297fb 2540
49af449b
DL
2541 obj->tiling_mode = plane_config->tiling;
2542 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2543 obj->stride = fb->pitches[0];
46f297fb 2544
6bf129df
DL
2545 mode_cmd.pixel_format = fb->pixel_format;
2546 mode_cmd.width = fb->width;
2547 mode_cmd.height = fb->height;
2548 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2549 mode_cmd.modifier[0] = fb->modifier[0];
2550 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb
JB
2551
2552 mutex_lock(&dev->struct_mutex);
6bf129df 2553 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2554 &mode_cmd, obj)) {
46f297fb
JB
2555 DRM_DEBUG_KMS("intel fb init failed\n");
2556 goto out_unref_obj;
2557 }
46f297fb 2558 mutex_unlock(&dev->struct_mutex);
484b41dd 2559
f6936e29 2560 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2561 return true;
46f297fb
JB
2562
2563out_unref_obj:
2564 drm_gem_object_unreference(&obj->base);
2565 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2566 return false;
2567}
2568
afd65eb4
MR
2569/* Update plane->state->fb to match plane->fb after driver-internal updates */
2570static void
2571update_state_fb(struct drm_plane *plane)
2572{
2573 if (plane->fb == plane->state->fb)
2574 return;
2575
2576 if (plane->state->fb)
2577 drm_framebuffer_unreference(plane->state->fb);
2578 plane->state->fb = plane->fb;
2579 if (plane->state->fb)
2580 drm_framebuffer_reference(plane->state->fb);
2581}
2582
5724dbd1 2583static void
f6936e29
DV
2584intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2585 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2586{
2587 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2588 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2589 struct drm_crtc *c;
2590 struct intel_crtc *i;
2ff8fde1 2591 struct drm_i915_gem_object *obj;
88595ac9
DV
2592 struct drm_plane *primary = intel_crtc->base.primary;
2593 struct drm_framebuffer *fb;
484b41dd 2594
2d14030b 2595 if (!plane_config->fb)
484b41dd
JB
2596 return;
2597
f6936e29 2598 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2599 fb = &plane_config->fb->base;
2600 goto valid_fb;
f55548b5 2601 }
484b41dd 2602
2d14030b 2603 kfree(plane_config->fb);
484b41dd
JB
2604
2605 /*
2606 * Failed to alloc the obj, check to see if we should share
2607 * an fb with another CRTC instead
2608 */
70e1e0ec 2609 for_each_crtc(dev, c) {
484b41dd
JB
2610 i = to_intel_crtc(c);
2611
2612 if (c == &intel_crtc->base)
2613 continue;
2614
2ff8fde1
MR
2615 if (!i->active)
2616 continue;
2617
88595ac9
DV
2618 fb = c->primary->fb;
2619 if (!fb)
484b41dd
JB
2620 continue;
2621
88595ac9 2622 obj = intel_fb_obj(fb);
2ff8fde1 2623 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2624 drm_framebuffer_reference(fb);
2625 goto valid_fb;
484b41dd
JB
2626 }
2627 }
88595ac9
DV
2628
2629 return;
2630
2631valid_fb:
2632 obj = intel_fb_obj(fb);
2633 if (obj->tiling_mode != I915_TILING_NONE)
2634 dev_priv->preserve_bios_swizzle = true;
2635
2636 primary->fb = fb;
2637 primary->state->crtc = &intel_crtc->base;
2638 primary->crtc = &intel_crtc->base;
2639 update_state_fb(primary);
2640 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
46f297fb
JB
2641}
2642
29b9bde6
DV
2643static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2644 struct drm_framebuffer *fb,
2645 int x, int y)
81255565
JB
2646{
2647 struct drm_device *dev = crtc->dev;
2648 struct drm_i915_private *dev_priv = dev->dev_private;
2649 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2650 struct drm_plane *primary = crtc->primary;
2651 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2652 struct drm_i915_gem_object *obj;
81255565 2653 int plane = intel_crtc->plane;
e506a0c6 2654 unsigned long linear_offset;
81255565 2655 u32 dspcntr;
f45651ba 2656 u32 reg = DSPCNTR(plane);
48404c1e 2657 int pixel_size;
f45651ba 2658
b70709a6 2659 if (!visible || !fb) {
fdd508a6
VS
2660 I915_WRITE(reg, 0);
2661 if (INTEL_INFO(dev)->gen >= 4)
2662 I915_WRITE(DSPSURF(plane), 0);
2663 else
2664 I915_WRITE(DSPADDR(plane), 0);
2665 POSTING_READ(reg);
2666 return;
2667 }
2668
c9ba6fad
VS
2669 obj = intel_fb_obj(fb);
2670 if (WARN_ON(obj == NULL))
2671 return;
2672
2673 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2674
f45651ba
VS
2675 dspcntr = DISPPLANE_GAMMA_ENABLE;
2676
fdd508a6 2677 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2678
2679 if (INTEL_INFO(dev)->gen < 4) {
2680 if (intel_crtc->pipe == PIPE_B)
2681 dspcntr |= DISPPLANE_SEL_PIPE_B;
2682
2683 /* pipesrc and dspsize control the size that is scaled from,
2684 * which should always be the user's requested size.
2685 */
2686 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2687 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2688 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2689 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2690 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2691 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2692 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2693 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2694 I915_WRITE(PRIMPOS(plane), 0);
2695 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2696 }
81255565 2697
57779d06
VS
2698 switch (fb->pixel_format) {
2699 case DRM_FORMAT_C8:
81255565
JB
2700 dspcntr |= DISPPLANE_8BPP;
2701 break;
57779d06
VS
2702 case DRM_FORMAT_XRGB1555:
2703 case DRM_FORMAT_ARGB1555:
2704 dspcntr |= DISPPLANE_BGRX555;
81255565 2705 break;
57779d06
VS
2706 case DRM_FORMAT_RGB565:
2707 dspcntr |= DISPPLANE_BGRX565;
2708 break;
2709 case DRM_FORMAT_XRGB8888:
2710 case DRM_FORMAT_ARGB8888:
2711 dspcntr |= DISPPLANE_BGRX888;
2712 break;
2713 case DRM_FORMAT_XBGR8888:
2714 case DRM_FORMAT_ABGR8888:
2715 dspcntr |= DISPPLANE_RGBX888;
2716 break;
2717 case DRM_FORMAT_XRGB2101010:
2718 case DRM_FORMAT_ARGB2101010:
2719 dspcntr |= DISPPLANE_BGRX101010;
2720 break;
2721 case DRM_FORMAT_XBGR2101010:
2722 case DRM_FORMAT_ABGR2101010:
2723 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2724 break;
2725 default:
baba133a 2726 BUG();
81255565 2727 }
57779d06 2728
f45651ba
VS
2729 if (INTEL_INFO(dev)->gen >= 4 &&
2730 obj->tiling_mode != I915_TILING_NONE)
2731 dspcntr |= DISPPLANE_TILED;
81255565 2732
de1aa629
VS
2733 if (IS_G4X(dev))
2734 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2735
b9897127 2736 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2737
c2c75131
DV
2738 if (INTEL_INFO(dev)->gen >= 4) {
2739 intel_crtc->dspaddr_offset =
bc752862 2740 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2741 pixel_size,
bc752862 2742 fb->pitches[0]);
c2c75131
DV
2743 linear_offset -= intel_crtc->dspaddr_offset;
2744 } else {
e506a0c6 2745 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2746 }
e506a0c6 2747
8e7d688b 2748 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2749 dspcntr |= DISPPLANE_ROTATE_180;
2750
6e3c9717
ACO
2751 x += (intel_crtc->config->pipe_src_w - 1);
2752 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2753
2754 /* Finding the last pixel of the last line of the display
2755 data and adding to linear_offset*/
2756 linear_offset +=
6e3c9717
ACO
2757 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2758 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2759 }
2760
2761 I915_WRITE(reg, dspcntr);
2762
01f2c773 2763 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2764 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2765 I915_WRITE(DSPSURF(plane),
2766 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2767 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2768 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2769 } else
f343c5f6 2770 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2771 POSTING_READ(reg);
17638cd6
JB
2772}
2773
29b9bde6
DV
2774static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2775 struct drm_framebuffer *fb,
2776 int x, int y)
17638cd6
JB
2777{
2778 struct drm_device *dev = crtc->dev;
2779 struct drm_i915_private *dev_priv = dev->dev_private;
2780 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2781 struct drm_plane *primary = crtc->primary;
2782 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2783 struct drm_i915_gem_object *obj;
17638cd6 2784 int plane = intel_crtc->plane;
e506a0c6 2785 unsigned long linear_offset;
17638cd6 2786 u32 dspcntr;
f45651ba 2787 u32 reg = DSPCNTR(plane);
48404c1e 2788 int pixel_size;
f45651ba 2789
b70709a6 2790 if (!visible || !fb) {
fdd508a6
VS
2791 I915_WRITE(reg, 0);
2792 I915_WRITE(DSPSURF(plane), 0);
2793 POSTING_READ(reg);
2794 return;
2795 }
2796
c9ba6fad
VS
2797 obj = intel_fb_obj(fb);
2798 if (WARN_ON(obj == NULL))
2799 return;
2800
2801 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2802
f45651ba
VS
2803 dspcntr = DISPPLANE_GAMMA_ENABLE;
2804
fdd508a6 2805 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2806
2807 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2808 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2809
57779d06
VS
2810 switch (fb->pixel_format) {
2811 case DRM_FORMAT_C8:
17638cd6
JB
2812 dspcntr |= DISPPLANE_8BPP;
2813 break;
57779d06
VS
2814 case DRM_FORMAT_RGB565:
2815 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2816 break;
57779d06
VS
2817 case DRM_FORMAT_XRGB8888:
2818 case DRM_FORMAT_ARGB8888:
2819 dspcntr |= DISPPLANE_BGRX888;
2820 break;
2821 case DRM_FORMAT_XBGR8888:
2822 case DRM_FORMAT_ABGR8888:
2823 dspcntr |= DISPPLANE_RGBX888;
2824 break;
2825 case DRM_FORMAT_XRGB2101010:
2826 case DRM_FORMAT_ARGB2101010:
2827 dspcntr |= DISPPLANE_BGRX101010;
2828 break;
2829 case DRM_FORMAT_XBGR2101010:
2830 case DRM_FORMAT_ABGR2101010:
2831 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2832 break;
2833 default:
baba133a 2834 BUG();
17638cd6
JB
2835 }
2836
2837 if (obj->tiling_mode != I915_TILING_NONE)
2838 dspcntr |= DISPPLANE_TILED;
17638cd6 2839
f45651ba 2840 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2841 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2842
b9897127 2843 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2844 intel_crtc->dspaddr_offset =
bc752862 2845 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2846 pixel_size,
bc752862 2847 fb->pitches[0]);
c2c75131 2848 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2849 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2850 dspcntr |= DISPPLANE_ROTATE_180;
2851
2852 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2853 x += (intel_crtc->config->pipe_src_w - 1);
2854 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2855
2856 /* Finding the last pixel of the last line of the display
2857 data and adding to linear_offset*/
2858 linear_offset +=
6e3c9717
ACO
2859 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2860 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2861 }
2862 }
2863
2864 I915_WRITE(reg, dspcntr);
17638cd6 2865
01f2c773 2866 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2867 I915_WRITE(DSPSURF(plane),
2868 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2869 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2870 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2871 } else {
2872 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2873 I915_WRITE(DSPLINOFF(plane), linear_offset);
2874 }
17638cd6 2875 POSTING_READ(reg);
17638cd6
JB
2876}
2877
b321803d
DL
2878u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2879 uint32_t pixel_format)
2880{
2881 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2882
2883 /*
2884 * The stride is either expressed as a multiple of 64 bytes
2885 * chunks for linear buffers or in number of tiles for tiled
2886 * buffers.
2887 */
2888 switch (fb_modifier) {
2889 case DRM_FORMAT_MOD_NONE:
2890 return 64;
2891 case I915_FORMAT_MOD_X_TILED:
2892 if (INTEL_INFO(dev)->gen == 2)
2893 return 128;
2894 return 512;
2895 case I915_FORMAT_MOD_Y_TILED:
2896 /* No need to check for old gens and Y tiling since this is
2897 * about the display engine and those will be blocked before
2898 * we get here.
2899 */
2900 return 128;
2901 case I915_FORMAT_MOD_Yf_TILED:
2902 if (bits_per_pixel == 8)
2903 return 64;
2904 else
2905 return 128;
2906 default:
2907 MISSING_CASE(fb_modifier);
2908 return 64;
2909 }
2910}
2911
121920fa
TU
2912unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2913 struct drm_i915_gem_object *obj)
2914{
9abc4648 2915 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
121920fa
TU
2916
2917 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
9abc4648 2918 view = &i915_ggtt_view_rotated;
121920fa
TU
2919
2920 return i915_gem_obj_ggtt_offset_view(obj, view);
2921}
2922
a1b2278e
CK
2923/*
2924 * This function detaches (aka. unbinds) unused scalers in hardware
2925 */
2926void skl_detach_scalers(struct intel_crtc *intel_crtc)
2927{
2928 struct drm_device *dev;
2929 struct drm_i915_private *dev_priv;
2930 struct intel_crtc_scaler_state *scaler_state;
2931 int i;
2932
2933 if (!intel_crtc || !intel_crtc->config)
2934 return;
2935
2936 dev = intel_crtc->base.dev;
2937 dev_priv = dev->dev_private;
2938 scaler_state = &intel_crtc->config->scaler_state;
2939
2940 /* loop through and disable scalers that aren't in use */
2941 for (i = 0; i < intel_crtc->num_scalers; i++) {
2942 if (!scaler_state->scalers[i].in_use) {
2943 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2944 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2945 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2946 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2947 intel_crtc->base.base.id, intel_crtc->pipe, i);
2948 }
2949 }
2950}
2951
6156a456 2952u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2953{
6156a456
CK
2954 u32 plane_ctl_format = 0;
2955 switch (pixel_format) {
70d21f0e 2956 case DRM_FORMAT_RGB565:
6156a456 2957 plane_ctl_format = PLANE_CTL_FORMAT_RGB_565;
f75fb42a 2958 break;
70d21f0e 2959 case DRM_FORMAT_XBGR8888:
6156a456
CK
2960 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
2961 break;
2962 case DRM_FORMAT_XRGB8888:
2963 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888;
70d21f0e 2964 break;
6156a456
CK
2965 /*
2966 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2967 * to be already pre-multiplied. We need to add a knob (or a different
2968 * DRM_FORMAT) for user-space to configure that.
2969 */
f75fb42a 2970 case DRM_FORMAT_ABGR8888:
6156a456
CK
2971 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
2972 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2973 break;
2974 case DRM_FORMAT_ARGB8888:
2975 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 |
2976 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
f75fb42a 2977 break;
70d21f0e 2978 case DRM_FORMAT_XRGB2101010:
6156a456 2979 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e
DL
2980 break;
2981 case DRM_FORMAT_XBGR2101010:
6156a456
CK
2982 plane_ctl_format = PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
2983 break;
2984 case DRM_FORMAT_YUYV:
2985 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
2986 break;
2987 case DRM_FORMAT_YVYU:
2988 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
2989 break;
2990 case DRM_FORMAT_UYVY:
2991 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
2992 break;
2993 case DRM_FORMAT_VYUY:
2994 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e
DL
2995 break;
2996 default:
2997 BUG();
2998 }
6156a456
CK
2999 return plane_ctl_format;
3000}
70d21f0e 3001
6156a456
CK
3002u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3003{
3004 u32 plane_ctl_tiling = 0;
3005 switch (fb_modifier) {
30af77c4 3006 case DRM_FORMAT_MOD_NONE:
70d21f0e 3007 break;
30af77c4 3008 case I915_FORMAT_MOD_X_TILED:
6156a456 3009 plane_ctl_tiling = PLANE_CTL_TILED_X;
b321803d
DL
3010 break;
3011 case I915_FORMAT_MOD_Y_TILED:
6156a456 3012 plane_ctl_tiling = PLANE_CTL_TILED_Y;
b321803d
DL
3013 break;
3014 case I915_FORMAT_MOD_Yf_TILED:
6156a456 3015 plane_ctl_tiling = PLANE_CTL_TILED_YF;
70d21f0e
DL
3016 break;
3017 default:
6156a456 3018 MISSING_CASE(fb_modifier);
70d21f0e 3019 }
6156a456
CK
3020 return plane_ctl_tiling;
3021}
70d21f0e 3022
6156a456
CK
3023u32 skl_plane_ctl_rotation(unsigned int rotation)
3024{
3025 u32 plane_ctl_rotation = 0;
3b7a5119 3026 switch (rotation) {
6156a456
CK
3027 case BIT(DRM_ROTATE_0):
3028 break;
3b7a5119 3029 case BIT(DRM_ROTATE_90):
6156a456 3030 plane_ctl_rotation = PLANE_CTL_ROTATE_90;
3b7a5119 3031 break;
3b7a5119 3032 case BIT(DRM_ROTATE_180):
6156a456 3033 plane_ctl_rotation = PLANE_CTL_ROTATE_180;
3b7a5119 3034 break;
3b7a5119 3035 case BIT(DRM_ROTATE_270):
6156a456 3036 plane_ctl_rotation = PLANE_CTL_ROTATE_270;
3b7a5119 3037 break;
6156a456
CK
3038 default:
3039 MISSING_CASE(rotation);
3040 }
3041
3042 return plane_ctl_rotation;
3043}
3044
3045static void skylake_update_primary_plane(struct drm_crtc *crtc,
3046 struct drm_framebuffer *fb,
3047 int x, int y)
3048{
3049 struct drm_device *dev = crtc->dev;
3050 struct drm_i915_private *dev_priv = dev->dev_private;
3051 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
3052 struct drm_plane *plane = crtc->primary;
3053 bool visible = to_intel_plane_state(plane->state)->visible;
6156a456
CK
3054 struct drm_i915_gem_object *obj;
3055 int pipe = intel_crtc->pipe;
3056 u32 plane_ctl, stride_div, stride;
3057 u32 tile_height, plane_offset, plane_size;
3058 unsigned int rotation;
3059 int x_offset, y_offset;
3060 unsigned long surf_addr;
6156a456
CK
3061 struct intel_crtc_state *crtc_state = intel_crtc->config;
3062 struct intel_plane_state *plane_state;
3063 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3064 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3065 int scaler_id = -1;
3066
6156a456
CK
3067 plane_state = to_intel_plane_state(plane->state);
3068
b70709a6 3069 if (!visible || !fb) {
6156a456
CK
3070 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3071 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3072 POSTING_READ(PLANE_CTL(pipe, 0));
3073 return;
3b7a5119 3074 }
70d21f0e 3075
6156a456
CK
3076 plane_ctl = PLANE_CTL_ENABLE |
3077 PLANE_CTL_PIPE_GAMMA_ENABLE |
3078 PLANE_CTL_PIPE_CSC_ENABLE;
3079
3080 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3081 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3082 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3083
3084 rotation = plane->state->rotation;
3085 plane_ctl |= skl_plane_ctl_rotation(rotation);
3086
b321803d
DL
3087 obj = intel_fb_obj(fb);
3088 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3089 fb->pixel_format);
3b7a5119
SJ
3090 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3091
6156a456
CK
3092 /*
3093 * FIXME: intel_plane_state->src, dst aren't set when transitional
3094 * update_plane helpers are called from legacy paths.
3095 * Once full atomic crtc is available, below check can be avoided.
3096 */
3097 if (drm_rect_width(&plane_state->src)) {
3098 scaler_id = plane_state->scaler_id;
3099 src_x = plane_state->src.x1 >> 16;
3100 src_y = plane_state->src.y1 >> 16;
3101 src_w = drm_rect_width(&plane_state->src) >> 16;
3102 src_h = drm_rect_height(&plane_state->src) >> 16;
3103 dst_x = plane_state->dst.x1;
3104 dst_y = plane_state->dst.y1;
3105 dst_w = drm_rect_width(&plane_state->dst);
3106 dst_h = drm_rect_height(&plane_state->dst);
3107
3108 WARN_ON(x != src_x || y != src_y);
3109 } else {
3110 src_w = intel_crtc->config->pipe_src_w;
3111 src_h = intel_crtc->config->pipe_src_h;
3112 }
3113
3b7a5119
SJ
3114 if (intel_rotation_90_or_270(rotation)) {
3115 /* stride = Surface height in tiles */
3116 tile_height = intel_tile_height(dev, fb->bits_per_pixel,
3117 fb->modifier[0]);
3118 stride = DIV_ROUND_UP(fb->height, tile_height);
6156a456 3119 x_offset = stride * tile_height - y - src_h;
3b7a5119 3120 y_offset = x;
6156a456 3121 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3122 } else {
3123 stride = fb->pitches[0] / stride_div;
3124 x_offset = x;
3125 y_offset = y;
6156a456 3126 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3127 }
3128 plane_offset = y_offset << 16 | x_offset;
b321803d 3129
70d21f0e 3130 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3131 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3132 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3133 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3134
3135 if (scaler_id >= 0) {
3136 uint32_t ps_ctrl = 0;
3137
3138 WARN_ON(!dst_w || !dst_h);
3139 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3140 crtc_state->scaler_state.scalers[scaler_id].mode;
3141 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3142 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3143 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3144 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3145 I915_WRITE(PLANE_POS(pipe, 0), 0);
3146 } else {
3147 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3148 }
3149
121920fa 3150 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3151
3152 POSTING_READ(PLANE_SURF(pipe, 0));
3153}
3154
17638cd6
JB
3155/* Assume fb object is pinned & idle & fenced and just update base pointers */
3156static int
3157intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3158 int x, int y, enum mode_set_atomic state)
3159{
3160 struct drm_device *dev = crtc->dev;
3161 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 3162
6b8e6ed0
CW
3163 if (dev_priv->display.disable_fbc)
3164 dev_priv->display.disable_fbc(dev);
81255565 3165
29b9bde6
DV
3166 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3167
3168 return 0;
81255565
JB
3169}
3170
7514747d 3171static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3172{
96a02917
VS
3173 struct drm_crtc *crtc;
3174
70e1e0ec 3175 for_each_crtc(dev, crtc) {
96a02917
VS
3176 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3177 enum plane plane = intel_crtc->plane;
3178
3179 intel_prepare_page_flip(dev, plane);
3180 intel_finish_page_flip_plane(dev, plane);
3181 }
7514747d
VS
3182}
3183
3184static void intel_update_primary_planes(struct drm_device *dev)
3185{
3186 struct drm_i915_private *dev_priv = dev->dev_private;
3187 struct drm_crtc *crtc;
96a02917 3188
70e1e0ec 3189 for_each_crtc(dev, crtc) {
96a02917
VS
3190 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3191
51fd371b 3192 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
3193 /*
3194 * FIXME: Once we have proper support for primary planes (and
3195 * disabling them without disabling the entire crtc) allow again
66e514c1 3196 * a NULL crtc->primary->fb.
947fdaad 3197 */
f4510a27 3198 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 3199 dev_priv->display.update_primary_plane(crtc,
66e514c1 3200 crtc->primary->fb,
262ca2b0
MR
3201 crtc->x,
3202 crtc->y);
51fd371b 3203 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
3204 }
3205}
3206
ce22dba9
ML
3207void intel_crtc_reset(struct intel_crtc *crtc)
3208{
3209 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3210
3211 if (!crtc->active)
3212 return;
3213
3214 intel_crtc_disable_planes(&crtc->base);
3215 dev_priv->display.crtc_disable(&crtc->base);
3216 dev_priv->display.crtc_enable(&crtc->base);
3217 intel_crtc_enable_planes(&crtc->base);
3218}
3219
7514747d
VS
3220void intel_prepare_reset(struct drm_device *dev)
3221{
f98ce92f
VS
3222 struct drm_i915_private *dev_priv = to_i915(dev);
3223 struct intel_crtc *crtc;
3224
7514747d
VS
3225 /* no reset support for gen2 */
3226 if (IS_GEN2(dev))
3227 return;
3228
3229 /* reset doesn't touch the display */
3230 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3231 return;
3232
3233 drm_modeset_lock_all(dev);
f98ce92f
VS
3234
3235 /*
3236 * Disabling the crtcs gracefully seems nicer. Also the
3237 * g33 docs say we should at least disable all the planes.
3238 */
3239 for_each_intel_crtc(dev, crtc) {
ce22dba9
ML
3240 if (!crtc->active)
3241 continue;
3242
3243 intel_crtc_disable_planes(&crtc->base);
3244 dev_priv->display.crtc_disable(&crtc->base);
f98ce92f 3245 }
7514747d
VS
3246}
3247
3248void intel_finish_reset(struct drm_device *dev)
3249{
3250 struct drm_i915_private *dev_priv = to_i915(dev);
3251
3252 /*
3253 * Flips in the rings will be nuked by the reset,
3254 * so complete all pending flips so that user space
3255 * will get its events and not get stuck.
3256 */
3257 intel_complete_page_flips(dev);
3258
3259 /* no reset support for gen2 */
3260 if (IS_GEN2(dev))
3261 return;
3262
3263 /* reset doesn't touch the display */
3264 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3265 /*
3266 * Flips in the rings have been nuked by the reset,
3267 * so update the base address of all primary
3268 * planes to the the last fb to make sure we're
3269 * showing the correct fb after a reset.
3270 */
3271 intel_update_primary_planes(dev);
3272 return;
3273 }
3274
3275 /*
3276 * The display has been reset as well,
3277 * so need a full re-initialization.
3278 */
3279 intel_runtime_pm_disable_interrupts(dev_priv);
3280 intel_runtime_pm_enable_interrupts(dev_priv);
3281
3282 intel_modeset_init_hw(dev);
3283
3284 spin_lock_irq(&dev_priv->irq_lock);
3285 if (dev_priv->display.hpd_irq_setup)
3286 dev_priv->display.hpd_irq_setup(dev);
3287 spin_unlock_irq(&dev_priv->irq_lock);
3288
3289 intel_modeset_setup_hw_state(dev, true);
3290
3291 intel_hpd_init(dev_priv);
3292
3293 drm_modeset_unlock_all(dev);
3294}
3295
14667a4b
CW
3296static int
3297intel_finish_fb(struct drm_framebuffer *old_fb)
3298{
2ff8fde1 3299 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
14667a4b
CW
3300 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3301 bool was_interruptible = dev_priv->mm.interruptible;
3302 int ret;
3303
14667a4b
CW
3304 /* Big Hammer, we also need to ensure that any pending
3305 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3306 * current scanout is retired before unpinning the old
3307 * framebuffer.
3308 *
3309 * This should only fail upon a hung GPU, in which case we
3310 * can safely continue.
3311 */
3312 dev_priv->mm.interruptible = false;
3313 ret = i915_gem_object_finish_gpu(obj);
3314 dev_priv->mm.interruptible = was_interruptible;
3315
3316 return ret;
3317}
3318
7d5e3799
CW
3319static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3320{
3321 struct drm_device *dev = crtc->dev;
3322 struct drm_i915_private *dev_priv = dev->dev_private;
3323 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3324 bool pending;
3325
3326 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3327 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3328 return false;
3329
5e2d7afc 3330 spin_lock_irq(&dev->event_lock);
7d5e3799 3331 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3332 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3333
3334 return pending;
3335}
3336
e30e8f75
GP
3337static void intel_update_pipe_size(struct intel_crtc *crtc)
3338{
3339 struct drm_device *dev = crtc->base.dev;
3340 struct drm_i915_private *dev_priv = dev->dev_private;
3341 const struct drm_display_mode *adjusted_mode;
3342
3343 if (!i915.fastboot)
3344 return;
3345
3346 /*
3347 * Update pipe size and adjust fitter if needed: the reason for this is
3348 * that in compute_mode_changes we check the native mode (not the pfit
3349 * mode) to see if we can flip rather than do a full mode set. In the
3350 * fastboot case, we'll flip, but if we don't update the pipesrc and
3351 * pfit state, we'll end up with a big fb scanned out into the wrong
3352 * sized surface.
3353 *
3354 * To fix this properly, we need to hoist the checks up into
3355 * compute_mode_changes (or above), check the actual pfit state and
3356 * whether the platform allows pfit disable with pipe active, and only
3357 * then update the pipesrc and pfit state, even on the flip path.
3358 */
3359
6e3c9717 3360 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
3361
3362 I915_WRITE(PIPESRC(crtc->pipe),
3363 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3364 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 3365 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
3366 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3367 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3368 I915_WRITE(PF_CTL(crtc->pipe), 0);
3369 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3370 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3371 }
6e3c9717
ACO
3372 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3373 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3374}
3375
5e84e1a4
ZW
3376static void intel_fdi_normal_train(struct drm_crtc *crtc)
3377{
3378 struct drm_device *dev = crtc->dev;
3379 struct drm_i915_private *dev_priv = dev->dev_private;
3380 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3381 int pipe = intel_crtc->pipe;
3382 u32 reg, temp;
3383
3384 /* enable normal train */
3385 reg = FDI_TX_CTL(pipe);
3386 temp = I915_READ(reg);
61e499bf 3387 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3388 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3389 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3390 } else {
3391 temp &= ~FDI_LINK_TRAIN_NONE;
3392 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3393 }
5e84e1a4
ZW
3394 I915_WRITE(reg, temp);
3395
3396 reg = FDI_RX_CTL(pipe);
3397 temp = I915_READ(reg);
3398 if (HAS_PCH_CPT(dev)) {
3399 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3400 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3401 } else {
3402 temp &= ~FDI_LINK_TRAIN_NONE;
3403 temp |= FDI_LINK_TRAIN_NONE;
3404 }
3405 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3406
3407 /* wait one idle pattern time */
3408 POSTING_READ(reg);
3409 udelay(1000);
357555c0
JB
3410
3411 /* IVB wants error correction enabled */
3412 if (IS_IVYBRIDGE(dev))
3413 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3414 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3415}
3416
8db9d77b
ZW
3417/* The FDI link training functions for ILK/Ibexpeak. */
3418static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3419{
3420 struct drm_device *dev = crtc->dev;
3421 struct drm_i915_private *dev_priv = dev->dev_private;
3422 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3423 int pipe = intel_crtc->pipe;
5eddb70b 3424 u32 reg, temp, tries;
8db9d77b 3425
1c8562f6 3426 /* FDI needs bits from pipe first */
0fc932b8 3427 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3428
e1a44743
AJ
3429 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3430 for train result */
5eddb70b
CW
3431 reg = FDI_RX_IMR(pipe);
3432 temp = I915_READ(reg);
e1a44743
AJ
3433 temp &= ~FDI_RX_SYMBOL_LOCK;
3434 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3435 I915_WRITE(reg, temp);
3436 I915_READ(reg);
e1a44743
AJ
3437 udelay(150);
3438
8db9d77b 3439 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3440 reg = FDI_TX_CTL(pipe);
3441 temp = I915_READ(reg);
627eb5a3 3442 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3443 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3444 temp &= ~FDI_LINK_TRAIN_NONE;
3445 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3446 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3447
5eddb70b
CW
3448 reg = FDI_RX_CTL(pipe);
3449 temp = I915_READ(reg);
8db9d77b
ZW
3450 temp &= ~FDI_LINK_TRAIN_NONE;
3451 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3452 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3453
3454 POSTING_READ(reg);
8db9d77b
ZW
3455 udelay(150);
3456
5b2adf89 3457 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3458 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3459 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3460 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3461
5eddb70b 3462 reg = FDI_RX_IIR(pipe);
e1a44743 3463 for (tries = 0; tries < 5; tries++) {
5eddb70b 3464 temp = I915_READ(reg);
8db9d77b
ZW
3465 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3466
3467 if ((temp & FDI_RX_BIT_LOCK)) {
3468 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3469 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3470 break;
3471 }
8db9d77b 3472 }
e1a44743 3473 if (tries == 5)
5eddb70b 3474 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3475
3476 /* Train 2 */
5eddb70b
CW
3477 reg = FDI_TX_CTL(pipe);
3478 temp = I915_READ(reg);
8db9d77b
ZW
3479 temp &= ~FDI_LINK_TRAIN_NONE;
3480 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3481 I915_WRITE(reg, temp);
8db9d77b 3482
5eddb70b
CW
3483 reg = FDI_RX_CTL(pipe);
3484 temp = I915_READ(reg);
8db9d77b
ZW
3485 temp &= ~FDI_LINK_TRAIN_NONE;
3486 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3487 I915_WRITE(reg, temp);
8db9d77b 3488
5eddb70b
CW
3489 POSTING_READ(reg);
3490 udelay(150);
8db9d77b 3491
5eddb70b 3492 reg = FDI_RX_IIR(pipe);
e1a44743 3493 for (tries = 0; tries < 5; tries++) {
5eddb70b 3494 temp = I915_READ(reg);
8db9d77b
ZW
3495 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3496
3497 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3498 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3499 DRM_DEBUG_KMS("FDI train 2 done.\n");
3500 break;
3501 }
8db9d77b 3502 }
e1a44743 3503 if (tries == 5)
5eddb70b 3504 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3505
3506 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3507
8db9d77b
ZW
3508}
3509
0206e353 3510static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3511 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3512 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3513 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3514 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3515};
3516
3517/* The FDI link training functions for SNB/Cougarpoint. */
3518static void gen6_fdi_link_train(struct drm_crtc *crtc)
3519{
3520 struct drm_device *dev = crtc->dev;
3521 struct drm_i915_private *dev_priv = dev->dev_private;
3522 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3523 int pipe = intel_crtc->pipe;
fa37d39e 3524 u32 reg, temp, i, retry;
8db9d77b 3525
e1a44743
AJ
3526 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3527 for train result */
5eddb70b
CW
3528 reg = FDI_RX_IMR(pipe);
3529 temp = I915_READ(reg);
e1a44743
AJ
3530 temp &= ~FDI_RX_SYMBOL_LOCK;
3531 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3532 I915_WRITE(reg, temp);
3533
3534 POSTING_READ(reg);
e1a44743
AJ
3535 udelay(150);
3536
8db9d77b 3537 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3538 reg = FDI_TX_CTL(pipe);
3539 temp = I915_READ(reg);
627eb5a3 3540 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3541 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3542 temp &= ~FDI_LINK_TRAIN_NONE;
3543 temp |= FDI_LINK_TRAIN_PATTERN_1;
3544 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3545 /* SNB-B */
3546 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3547 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3548
d74cf324
DV
3549 I915_WRITE(FDI_RX_MISC(pipe),
3550 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3551
5eddb70b
CW
3552 reg = FDI_RX_CTL(pipe);
3553 temp = I915_READ(reg);
8db9d77b
ZW
3554 if (HAS_PCH_CPT(dev)) {
3555 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3556 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3557 } else {
3558 temp &= ~FDI_LINK_TRAIN_NONE;
3559 temp |= FDI_LINK_TRAIN_PATTERN_1;
3560 }
5eddb70b
CW
3561 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3562
3563 POSTING_READ(reg);
8db9d77b
ZW
3564 udelay(150);
3565
0206e353 3566 for (i = 0; i < 4; i++) {
5eddb70b
CW
3567 reg = FDI_TX_CTL(pipe);
3568 temp = I915_READ(reg);
8db9d77b
ZW
3569 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3570 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3571 I915_WRITE(reg, temp);
3572
3573 POSTING_READ(reg);
8db9d77b
ZW
3574 udelay(500);
3575
fa37d39e
SP
3576 for (retry = 0; retry < 5; retry++) {
3577 reg = FDI_RX_IIR(pipe);
3578 temp = I915_READ(reg);
3579 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3580 if (temp & FDI_RX_BIT_LOCK) {
3581 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3582 DRM_DEBUG_KMS("FDI train 1 done.\n");
3583 break;
3584 }
3585 udelay(50);
8db9d77b 3586 }
fa37d39e
SP
3587 if (retry < 5)
3588 break;
8db9d77b
ZW
3589 }
3590 if (i == 4)
5eddb70b 3591 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3592
3593 /* Train 2 */
5eddb70b
CW
3594 reg = FDI_TX_CTL(pipe);
3595 temp = I915_READ(reg);
8db9d77b
ZW
3596 temp &= ~FDI_LINK_TRAIN_NONE;
3597 temp |= FDI_LINK_TRAIN_PATTERN_2;
3598 if (IS_GEN6(dev)) {
3599 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3600 /* SNB-B */
3601 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3602 }
5eddb70b 3603 I915_WRITE(reg, temp);
8db9d77b 3604
5eddb70b
CW
3605 reg = FDI_RX_CTL(pipe);
3606 temp = I915_READ(reg);
8db9d77b
ZW
3607 if (HAS_PCH_CPT(dev)) {
3608 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3609 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3610 } else {
3611 temp &= ~FDI_LINK_TRAIN_NONE;
3612 temp |= FDI_LINK_TRAIN_PATTERN_2;
3613 }
5eddb70b
CW
3614 I915_WRITE(reg, temp);
3615
3616 POSTING_READ(reg);
8db9d77b
ZW
3617 udelay(150);
3618
0206e353 3619 for (i = 0; i < 4; i++) {
5eddb70b
CW
3620 reg = FDI_TX_CTL(pipe);
3621 temp = I915_READ(reg);
8db9d77b
ZW
3622 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3623 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3624 I915_WRITE(reg, temp);
3625
3626 POSTING_READ(reg);
8db9d77b
ZW
3627 udelay(500);
3628
fa37d39e
SP
3629 for (retry = 0; retry < 5; retry++) {
3630 reg = FDI_RX_IIR(pipe);
3631 temp = I915_READ(reg);
3632 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3633 if (temp & FDI_RX_SYMBOL_LOCK) {
3634 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3635 DRM_DEBUG_KMS("FDI train 2 done.\n");
3636 break;
3637 }
3638 udelay(50);
8db9d77b 3639 }
fa37d39e
SP
3640 if (retry < 5)
3641 break;
8db9d77b
ZW
3642 }
3643 if (i == 4)
5eddb70b 3644 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3645
3646 DRM_DEBUG_KMS("FDI train done.\n");
3647}
3648
357555c0
JB
3649/* Manual link training for Ivy Bridge A0 parts */
3650static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3651{
3652 struct drm_device *dev = crtc->dev;
3653 struct drm_i915_private *dev_priv = dev->dev_private;
3654 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3655 int pipe = intel_crtc->pipe;
139ccd3f 3656 u32 reg, temp, i, j;
357555c0
JB
3657
3658 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3659 for train result */
3660 reg = FDI_RX_IMR(pipe);
3661 temp = I915_READ(reg);
3662 temp &= ~FDI_RX_SYMBOL_LOCK;
3663 temp &= ~FDI_RX_BIT_LOCK;
3664 I915_WRITE(reg, temp);
3665
3666 POSTING_READ(reg);
3667 udelay(150);
3668
01a415fd
DV
3669 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3670 I915_READ(FDI_RX_IIR(pipe)));
3671
139ccd3f
JB
3672 /* Try each vswing and preemphasis setting twice before moving on */
3673 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3674 /* disable first in case we need to retry */
3675 reg = FDI_TX_CTL(pipe);
3676 temp = I915_READ(reg);
3677 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3678 temp &= ~FDI_TX_ENABLE;
3679 I915_WRITE(reg, temp);
357555c0 3680
139ccd3f
JB
3681 reg = FDI_RX_CTL(pipe);
3682 temp = I915_READ(reg);
3683 temp &= ~FDI_LINK_TRAIN_AUTO;
3684 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3685 temp &= ~FDI_RX_ENABLE;
3686 I915_WRITE(reg, temp);
357555c0 3687
139ccd3f 3688 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3689 reg = FDI_TX_CTL(pipe);
3690 temp = I915_READ(reg);
139ccd3f 3691 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3692 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3693 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3694 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3695 temp |= snb_b_fdi_train_param[j/2];
3696 temp |= FDI_COMPOSITE_SYNC;
3697 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3698
139ccd3f
JB
3699 I915_WRITE(FDI_RX_MISC(pipe),
3700 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3701
139ccd3f 3702 reg = FDI_RX_CTL(pipe);
357555c0 3703 temp = I915_READ(reg);
139ccd3f
JB
3704 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3705 temp |= FDI_COMPOSITE_SYNC;
3706 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3707
139ccd3f
JB
3708 POSTING_READ(reg);
3709 udelay(1); /* should be 0.5us */
357555c0 3710
139ccd3f
JB
3711 for (i = 0; i < 4; i++) {
3712 reg = FDI_RX_IIR(pipe);
3713 temp = I915_READ(reg);
3714 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3715
139ccd3f
JB
3716 if (temp & FDI_RX_BIT_LOCK ||
3717 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3718 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3719 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3720 i);
3721 break;
3722 }
3723 udelay(1); /* should be 0.5us */
3724 }
3725 if (i == 4) {
3726 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3727 continue;
3728 }
357555c0 3729
139ccd3f 3730 /* Train 2 */
357555c0
JB
3731 reg = FDI_TX_CTL(pipe);
3732 temp = I915_READ(reg);
139ccd3f
JB
3733 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3734 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3735 I915_WRITE(reg, temp);
3736
3737 reg = FDI_RX_CTL(pipe);
3738 temp = I915_READ(reg);
3739 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3740 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3741 I915_WRITE(reg, temp);
3742
3743 POSTING_READ(reg);
139ccd3f 3744 udelay(2); /* should be 1.5us */
357555c0 3745
139ccd3f
JB
3746 for (i = 0; i < 4; i++) {
3747 reg = FDI_RX_IIR(pipe);
3748 temp = I915_READ(reg);
3749 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3750
139ccd3f
JB
3751 if (temp & FDI_RX_SYMBOL_LOCK ||
3752 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3753 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3754 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3755 i);
3756 goto train_done;
3757 }
3758 udelay(2); /* should be 1.5us */
357555c0 3759 }
139ccd3f
JB
3760 if (i == 4)
3761 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3762 }
357555c0 3763
139ccd3f 3764train_done:
357555c0
JB
3765 DRM_DEBUG_KMS("FDI train done.\n");
3766}
3767
88cefb6c 3768static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3769{
88cefb6c 3770 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3771 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3772 int pipe = intel_crtc->pipe;
5eddb70b 3773 u32 reg, temp;
79e53945 3774
c64e311e 3775
c98e9dcf 3776 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3777 reg = FDI_RX_CTL(pipe);
3778 temp = I915_READ(reg);
627eb5a3 3779 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3780 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3781 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3782 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3783
3784 POSTING_READ(reg);
c98e9dcf
JB
3785 udelay(200);
3786
3787 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3788 temp = I915_READ(reg);
3789 I915_WRITE(reg, temp | FDI_PCDCLK);
3790
3791 POSTING_READ(reg);
c98e9dcf
JB
3792 udelay(200);
3793
20749730
PZ
3794 /* Enable CPU FDI TX PLL, always on for Ironlake */
3795 reg = FDI_TX_CTL(pipe);
3796 temp = I915_READ(reg);
3797 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3798 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3799
20749730
PZ
3800 POSTING_READ(reg);
3801 udelay(100);
6be4a607 3802 }
0e23b99d
JB
3803}
3804
88cefb6c
DV
3805static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3806{
3807 struct drm_device *dev = intel_crtc->base.dev;
3808 struct drm_i915_private *dev_priv = dev->dev_private;
3809 int pipe = intel_crtc->pipe;
3810 u32 reg, temp;
3811
3812 /* Switch from PCDclk to Rawclk */
3813 reg = FDI_RX_CTL(pipe);
3814 temp = I915_READ(reg);
3815 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3816
3817 /* Disable CPU FDI TX PLL */
3818 reg = FDI_TX_CTL(pipe);
3819 temp = I915_READ(reg);
3820 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3821
3822 POSTING_READ(reg);
3823 udelay(100);
3824
3825 reg = FDI_RX_CTL(pipe);
3826 temp = I915_READ(reg);
3827 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3828
3829 /* Wait for the clocks to turn off. */
3830 POSTING_READ(reg);
3831 udelay(100);
3832}
3833
0fc932b8
JB
3834static void ironlake_fdi_disable(struct drm_crtc *crtc)
3835{
3836 struct drm_device *dev = crtc->dev;
3837 struct drm_i915_private *dev_priv = dev->dev_private;
3838 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3839 int pipe = intel_crtc->pipe;
3840 u32 reg, temp;
3841
3842 /* disable CPU FDI tx and PCH FDI rx */
3843 reg = FDI_TX_CTL(pipe);
3844 temp = I915_READ(reg);
3845 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3846 POSTING_READ(reg);
3847
3848 reg = FDI_RX_CTL(pipe);
3849 temp = I915_READ(reg);
3850 temp &= ~(0x7 << 16);
dfd07d72 3851 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3852 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3853
3854 POSTING_READ(reg);
3855 udelay(100);
3856
3857 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3858 if (HAS_PCH_IBX(dev))
6f06ce18 3859 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3860
3861 /* still set train pattern 1 */
3862 reg = FDI_TX_CTL(pipe);
3863 temp = I915_READ(reg);
3864 temp &= ~FDI_LINK_TRAIN_NONE;
3865 temp |= FDI_LINK_TRAIN_PATTERN_1;
3866 I915_WRITE(reg, temp);
3867
3868 reg = FDI_RX_CTL(pipe);
3869 temp = I915_READ(reg);
3870 if (HAS_PCH_CPT(dev)) {
3871 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3872 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3873 } else {
3874 temp &= ~FDI_LINK_TRAIN_NONE;
3875 temp |= FDI_LINK_TRAIN_PATTERN_1;
3876 }
3877 /* BPC in FDI rx is consistent with that in PIPECONF */
3878 temp &= ~(0x07 << 16);
dfd07d72 3879 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3880 I915_WRITE(reg, temp);
3881
3882 POSTING_READ(reg);
3883 udelay(100);
3884}
3885
5dce5b93
CW
3886bool intel_has_pending_fb_unpin(struct drm_device *dev)
3887{
3888 struct intel_crtc *crtc;
3889
3890 /* Note that we don't need to be called with mode_config.lock here
3891 * as our list of CRTC objects is static for the lifetime of the
3892 * device and so cannot disappear as we iterate. Similarly, we can
3893 * happily treat the predicates as racy, atomic checks as userspace
3894 * cannot claim and pin a new fb without at least acquring the
3895 * struct_mutex and so serialising with us.
3896 */
d3fcc808 3897 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3898 if (atomic_read(&crtc->unpin_work_count) == 0)
3899 continue;
3900
3901 if (crtc->unpin_work)
3902 intel_wait_for_vblank(dev, crtc->pipe);
3903
3904 return true;
3905 }
3906
3907 return false;
3908}
3909
d6bbafa1
CW
3910static void page_flip_completed(struct intel_crtc *intel_crtc)
3911{
3912 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3913 struct intel_unpin_work *work = intel_crtc->unpin_work;
3914
3915 /* ensure that the unpin work is consistent wrt ->pending. */
3916 smp_rmb();
3917 intel_crtc->unpin_work = NULL;
3918
3919 if (work->event)
3920 drm_send_vblank_event(intel_crtc->base.dev,
3921 intel_crtc->pipe,
3922 work->event);
3923
3924 drm_crtc_vblank_put(&intel_crtc->base);
3925
3926 wake_up_all(&dev_priv->pending_flip_queue);
3927 queue_work(dev_priv->wq, &work->work);
3928
3929 trace_i915_flip_complete(intel_crtc->plane,
3930 work->pending_flip_obj);
3931}
3932
46a55d30 3933void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3934{
0f91128d 3935 struct drm_device *dev = crtc->dev;
5bb61643 3936 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3937
2c10d571 3938 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3939 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3940 !intel_crtc_has_pending_flip(crtc),
3941 60*HZ) == 0)) {
3942 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3943
5e2d7afc 3944 spin_lock_irq(&dev->event_lock);
9c787942
CW
3945 if (intel_crtc->unpin_work) {
3946 WARN_ONCE(1, "Removing stuck page flip\n");
3947 page_flip_completed(intel_crtc);
3948 }
5e2d7afc 3949 spin_unlock_irq(&dev->event_lock);
9c787942 3950 }
5bb61643 3951
975d568a
CW
3952 if (crtc->primary->fb) {
3953 mutex_lock(&dev->struct_mutex);
3954 intel_finish_fb(crtc->primary->fb);
3955 mutex_unlock(&dev->struct_mutex);
3956 }
e6c3a2a6
CW
3957}
3958
e615efe4
ED
3959/* Program iCLKIP clock to the desired frequency */
3960static void lpt_program_iclkip(struct drm_crtc *crtc)
3961{
3962 struct drm_device *dev = crtc->dev;
3963 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3964 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3965 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3966 u32 temp;
3967
09153000
DV
3968 mutex_lock(&dev_priv->dpio_lock);
3969
e615efe4
ED
3970 /* It is necessary to ungate the pixclk gate prior to programming
3971 * the divisors, and gate it back when it is done.
3972 */
3973 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3974
3975 /* Disable SSCCTL */
3976 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3977 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3978 SBI_SSCCTL_DISABLE,
3979 SBI_ICLK);
e615efe4
ED
3980
3981 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3982 if (clock == 20000) {
e615efe4
ED
3983 auxdiv = 1;
3984 divsel = 0x41;
3985 phaseinc = 0x20;
3986 } else {
3987 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3988 * but the adjusted_mode->crtc_clock in in KHz. To get the
3989 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3990 * convert the virtual clock precision to KHz here for higher
3991 * precision.
3992 */
3993 u32 iclk_virtual_root_freq = 172800 * 1000;
3994 u32 iclk_pi_range = 64;
3995 u32 desired_divisor, msb_divisor_value, pi_value;
3996
12d7ceed 3997 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3998 msb_divisor_value = desired_divisor / iclk_pi_range;
3999 pi_value = desired_divisor % iclk_pi_range;
4000
4001 auxdiv = 0;
4002 divsel = msb_divisor_value - 2;
4003 phaseinc = pi_value;
4004 }
4005
4006 /* This should not happen with any sane values */
4007 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4008 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4009 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4010 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4011
4012 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 4013 clock,
e615efe4
ED
4014 auxdiv,
4015 divsel,
4016 phasedir,
4017 phaseinc);
4018
4019 /* Program SSCDIVINTPHASE6 */
988d6ee8 4020 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4021 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4022 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4023 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4024 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4025 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4026 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4027 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4028
4029 /* Program SSCAUXDIV */
988d6ee8 4030 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4031 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4032 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4033 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4034
4035 /* Enable modulator and associated divider */
988d6ee8 4036 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4037 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4038 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
4039
4040 /* Wait for initialization time */
4041 udelay(24);
4042
4043 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
4044
4045 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
4046}
4047
275f01b2
DV
4048static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4049 enum pipe pch_transcoder)
4050{
4051 struct drm_device *dev = crtc->base.dev;
4052 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4053 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4054
4055 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4056 I915_READ(HTOTAL(cpu_transcoder)));
4057 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4058 I915_READ(HBLANK(cpu_transcoder)));
4059 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4060 I915_READ(HSYNC(cpu_transcoder)));
4061
4062 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4063 I915_READ(VTOTAL(cpu_transcoder)));
4064 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4065 I915_READ(VBLANK(cpu_transcoder)));
4066 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4067 I915_READ(VSYNC(cpu_transcoder)));
4068 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4069 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4070}
4071
003632d9 4072static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4073{
4074 struct drm_i915_private *dev_priv = dev->dev_private;
4075 uint32_t temp;
4076
4077 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4078 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4079 return;
4080
4081 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4082 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4083
003632d9
ACO
4084 temp &= ~FDI_BC_BIFURCATION_SELECT;
4085 if (enable)
4086 temp |= FDI_BC_BIFURCATION_SELECT;
4087
4088 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4089 I915_WRITE(SOUTH_CHICKEN1, temp);
4090 POSTING_READ(SOUTH_CHICKEN1);
4091}
4092
4093static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4094{
4095 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4096
4097 switch (intel_crtc->pipe) {
4098 case PIPE_A:
4099 break;
4100 case PIPE_B:
6e3c9717 4101 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4102 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4103 else
003632d9 4104 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4105
4106 break;
4107 case PIPE_C:
003632d9 4108 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4109
4110 break;
4111 default:
4112 BUG();
4113 }
4114}
4115
f67a559d
JB
4116/*
4117 * Enable PCH resources required for PCH ports:
4118 * - PCH PLLs
4119 * - FDI training & RX/TX
4120 * - update transcoder timings
4121 * - DP transcoding bits
4122 * - transcoder
4123 */
4124static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4125{
4126 struct drm_device *dev = crtc->dev;
4127 struct drm_i915_private *dev_priv = dev->dev_private;
4128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4129 int pipe = intel_crtc->pipe;
ee7b9f93 4130 u32 reg, temp;
2c07245f 4131
ab9412ba 4132 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4133
1fbc0d78
DV
4134 if (IS_IVYBRIDGE(dev))
4135 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4136
cd986abb
DV
4137 /* Write the TU size bits before fdi link training, so that error
4138 * detection works. */
4139 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4140 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4141
c98e9dcf 4142 /* For PCH output, training FDI link */
674cf967 4143 dev_priv->display.fdi_link_train(crtc);
2c07245f 4144
3ad8a208
DV
4145 /* We need to program the right clock selection before writing the pixel
4146 * mutliplier into the DPLL. */
303b81e0 4147 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4148 u32 sel;
4b645f14 4149
c98e9dcf 4150 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4151 temp |= TRANS_DPLL_ENABLE(pipe);
4152 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 4153 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
4154 temp |= sel;
4155 else
4156 temp &= ~sel;
c98e9dcf 4157 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4158 }
5eddb70b 4159
3ad8a208
DV
4160 /* XXX: pch pll's can be enabled any time before we enable the PCH
4161 * transcoder, and we actually should do this to not upset any PCH
4162 * transcoder that already use the clock when we share it.
4163 *
4164 * Note that enable_shared_dpll tries to do the right thing, but
4165 * get_shared_dpll unconditionally resets the pll - we need that to have
4166 * the right LVDS enable sequence. */
85b3894f 4167 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4168
d9b6cb56
JB
4169 /* set transcoder timing, panel must allow it */
4170 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4171 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4172
303b81e0 4173 intel_fdi_normal_train(crtc);
5e84e1a4 4174
c98e9dcf 4175 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4176 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 4177 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
4178 reg = TRANS_DP_CTL(pipe);
4179 temp = I915_READ(reg);
4180 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4181 TRANS_DP_SYNC_MASK |
4182 TRANS_DP_BPC_MASK);
5eddb70b
CW
4183 temp |= (TRANS_DP_OUTPUT_ENABLE |
4184 TRANS_DP_ENH_FRAMING);
9325c9f0 4185 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
4186
4187 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4188 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 4189 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4190 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4191
4192 switch (intel_trans_dp_port_sel(crtc)) {
4193 case PCH_DP_B:
5eddb70b 4194 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
4195 break;
4196 case PCH_DP_C:
5eddb70b 4197 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
4198 break;
4199 case PCH_DP_D:
5eddb70b 4200 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4201 break;
4202 default:
e95d41e1 4203 BUG();
32f9d658 4204 }
2c07245f 4205
5eddb70b 4206 I915_WRITE(reg, temp);
6be4a607 4207 }
b52eb4dc 4208
b8a4f404 4209 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4210}
4211
1507e5bd
PZ
4212static void lpt_pch_enable(struct drm_crtc *crtc)
4213{
4214 struct drm_device *dev = crtc->dev;
4215 struct drm_i915_private *dev_priv = dev->dev_private;
4216 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4217 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4218
ab9412ba 4219 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4220
8c52b5e8 4221 lpt_program_iclkip(crtc);
1507e5bd 4222
0540e488 4223 /* Set transcoder timing. */
275f01b2 4224 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4225
937bb610 4226 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4227}
4228
716c2e55 4229void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 4230{
e2b78267 4231 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
4232
4233 if (pll == NULL)
4234 return;
4235
3e369b76 4236 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
1e6f2ddc 4237 WARN(1, "bad %s crtc mask\n", pll->name);
ee7b9f93
JB
4238 return;
4239 }
4240
3e369b76
ACO
4241 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4242 if (pll->config.crtc_mask == 0) {
f4a091c7
DV
4243 WARN_ON(pll->on);
4244 WARN_ON(pll->active);
4245 }
4246
6e3c9717 4247 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
4248}
4249
190f68c5
ACO
4250struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4251 struct intel_crtc_state *crtc_state)
ee7b9f93 4252{
e2b78267 4253 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 4254 struct intel_shared_dpll *pll;
e2b78267 4255 enum intel_dpll_id i;
ee7b9f93 4256
98b6bd99
DV
4257 if (HAS_PCH_IBX(dev_priv->dev)) {
4258 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 4259 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 4260 pll = &dev_priv->shared_dplls[i];
98b6bd99 4261
46edb027
DV
4262 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4263 crtc->base.base.id, pll->name);
98b6bd99 4264
8bd31e67 4265 WARN_ON(pll->new_config->crtc_mask);
f2a69f44 4266
98b6bd99
DV
4267 goto found;
4268 }
4269
bcddf610
S
4270 if (IS_BROXTON(dev_priv->dev)) {
4271 /* PLL is attached to port in bxt */
4272 struct intel_encoder *encoder;
4273 struct intel_digital_port *intel_dig_port;
4274
4275 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4276 if (WARN_ON(!encoder))
4277 return NULL;
4278
4279 intel_dig_port = enc_to_dig_port(&encoder->base);
4280 /* 1:1 mapping between ports and PLLs */
4281 i = (enum intel_dpll_id)intel_dig_port->port;
4282 pll = &dev_priv->shared_dplls[i];
4283 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4284 crtc->base.base.id, pll->name);
4285 WARN_ON(pll->new_config->crtc_mask);
4286
4287 goto found;
4288 }
4289
e72f9fbf
DV
4290 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4291 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
4292
4293 /* Only want to check enabled timings first */
8bd31e67 4294 if (pll->new_config->crtc_mask == 0)
ee7b9f93
JB
4295 continue;
4296
190f68c5 4297 if (memcmp(&crtc_state->dpll_hw_state,
8bd31e67
ACO
4298 &pll->new_config->hw_state,
4299 sizeof(pll->new_config->hw_state)) == 0) {
4300 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 4301 crtc->base.base.id, pll->name,
8bd31e67
ACO
4302 pll->new_config->crtc_mask,
4303 pll->active);
ee7b9f93
JB
4304 goto found;
4305 }
4306 }
4307
4308 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
4309 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4310 pll = &dev_priv->shared_dplls[i];
8bd31e67 4311 if (pll->new_config->crtc_mask == 0) {
46edb027
DV
4312 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4313 crtc->base.base.id, pll->name);
ee7b9f93
JB
4314 goto found;
4315 }
4316 }
4317
4318 return NULL;
4319
4320found:
8bd31e67 4321 if (pll->new_config->crtc_mask == 0)
190f68c5 4322 pll->new_config->hw_state = crtc_state->dpll_hw_state;
f2a69f44 4323
190f68c5 4324 crtc_state->shared_dpll = i;
46edb027
DV
4325 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4326 pipe_name(crtc->pipe));
ee7b9f93 4327
8bd31e67 4328 pll->new_config->crtc_mask |= 1 << crtc->pipe;
e04c7350 4329
ee7b9f93
JB
4330 return pll;
4331}
4332
8bd31e67
ACO
4333/**
4334 * intel_shared_dpll_start_config - start a new PLL staged config
4335 * @dev_priv: DRM device
4336 * @clear_pipes: mask of pipes that will have their PLLs freed
4337 *
4338 * Starts a new PLL staged config, copying the current config but
4339 * releasing the references of pipes specified in clear_pipes.
4340 */
4341static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4342 unsigned clear_pipes)
4343{
4344 struct intel_shared_dpll *pll;
4345 enum intel_dpll_id i;
4346
4347 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4348 pll = &dev_priv->shared_dplls[i];
4349
4350 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4351 GFP_KERNEL);
4352 if (!pll->new_config)
4353 goto cleanup;
4354
4355 pll->new_config->crtc_mask &= ~clear_pipes;
4356 }
4357
4358 return 0;
4359
4360cleanup:
4361 while (--i >= 0) {
4362 pll = &dev_priv->shared_dplls[i];
f354d733 4363 kfree(pll->new_config);
8bd31e67
ACO
4364 pll->new_config = NULL;
4365 }
4366
4367 return -ENOMEM;
4368}
4369
4370static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4371{
4372 struct intel_shared_dpll *pll;
4373 enum intel_dpll_id i;
4374
4375 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4376 pll = &dev_priv->shared_dplls[i];
4377
4378 WARN_ON(pll->new_config == &pll->config);
4379
4380 pll->config = *pll->new_config;
4381 kfree(pll->new_config);
4382 pll->new_config = NULL;
4383 }
4384}
4385
4386static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4387{
4388 struct intel_shared_dpll *pll;
4389 enum intel_dpll_id i;
4390
4391 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4392 pll = &dev_priv->shared_dplls[i];
4393
4394 WARN_ON(pll->new_config == &pll->config);
4395
4396 kfree(pll->new_config);
4397 pll->new_config = NULL;
4398 }
4399}
4400
a1520318 4401static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4402{
4403 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4404 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4405 u32 temp;
4406
4407 temp = I915_READ(dslreg);
4408 udelay(500);
4409 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4410 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4411 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4412 }
4413}
4414
a1b2278e
CK
4415/**
4416 * skl_update_scaler_users - Stages update to crtc's scaler state
4417 * @intel_crtc: crtc
4418 * @crtc_state: crtc_state
4419 * @plane: plane (NULL indicates crtc is requesting update)
4420 * @plane_state: plane's state
4421 * @force_detach: request unconditional detachment of scaler
4422 *
4423 * This function updates scaler state for requested plane or crtc.
4424 * To request scaler usage update for a plane, caller shall pass plane pointer.
4425 * To request scaler usage update for crtc, caller shall pass plane pointer
4426 * as NULL.
4427 *
4428 * Return
4429 * 0 - scaler_usage updated successfully
4430 * error - requested scaling cannot be supported or other error condition
4431 */
4432int
4433skl_update_scaler_users(
4434 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4435 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4436 int force_detach)
4437{
4438 int need_scaling;
4439 int idx;
4440 int src_w, src_h, dst_w, dst_h;
4441 int *scaler_id;
4442 struct drm_framebuffer *fb;
4443 struct intel_crtc_scaler_state *scaler_state;
6156a456 4444 unsigned int rotation;
a1b2278e
CK
4445
4446 if (!intel_crtc || !crtc_state)
4447 return 0;
4448
4449 scaler_state = &crtc_state->scaler_state;
4450
4451 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4452 fb = intel_plane ? plane_state->base.fb : NULL;
4453
4454 if (intel_plane) {
4455 src_w = drm_rect_width(&plane_state->src) >> 16;
4456 src_h = drm_rect_height(&plane_state->src) >> 16;
4457 dst_w = drm_rect_width(&plane_state->dst);
4458 dst_h = drm_rect_height(&plane_state->dst);
4459 scaler_id = &plane_state->scaler_id;
6156a456 4460 rotation = plane_state->base.rotation;
a1b2278e
CK
4461 } else {
4462 struct drm_display_mode *adjusted_mode =
4463 &crtc_state->base.adjusted_mode;
4464 src_w = crtc_state->pipe_src_w;
4465 src_h = crtc_state->pipe_src_h;
4466 dst_w = adjusted_mode->hdisplay;
4467 dst_h = adjusted_mode->vdisplay;
4468 scaler_id = &scaler_state->scaler_id;
6156a456 4469 rotation = DRM_ROTATE_0;
a1b2278e 4470 }
6156a456
CK
4471
4472 need_scaling = intel_rotation_90_or_270(rotation) ?
4473 (src_h != dst_w || src_w != dst_h):
4474 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4475
4476 /*
4477 * if plane is being disabled or scaler is no more required or force detach
4478 * - free scaler binded to this plane/crtc
4479 * - in order to do this, update crtc->scaler_usage
4480 *
4481 * Here scaler state in crtc_state is set free so that
4482 * scaler can be assigned to other user. Actual register
4483 * update to free the scaler is done in plane/panel-fit programming.
4484 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4485 */
4486 if (force_detach || !need_scaling || (intel_plane &&
4487 (!fb || !plane_state->visible))) {
4488 if (*scaler_id >= 0) {
4489 scaler_state->scaler_users &= ~(1 << idx);
4490 scaler_state->scalers[*scaler_id].in_use = 0;
4491
4492 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4493 "crtc_state = %p scaler_users = 0x%x\n",
4494 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4495 intel_plane ? intel_plane->base.base.id :
4496 intel_crtc->base.base.id, crtc_state,
4497 scaler_state->scaler_users);
4498 *scaler_id = -1;
4499 }
4500 return 0;
4501 }
4502
4503 /* range checks */
4504 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4505 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4506
4507 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4508 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4509 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4510 "size is out of scaler range\n",
4511 intel_plane ? "PLANE" : "CRTC",
4512 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4513 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4514 return -EINVAL;
4515 }
4516
4517 /* check colorkey */
4518 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4519 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4520 intel_plane->base.base.id);
4521 return -EINVAL;
4522 }
4523
4524 /* Check src format */
4525 if (intel_plane) {
4526 switch (fb->pixel_format) {
4527 case DRM_FORMAT_RGB565:
4528 case DRM_FORMAT_XBGR8888:
4529 case DRM_FORMAT_XRGB8888:
4530 case DRM_FORMAT_ABGR8888:
4531 case DRM_FORMAT_ARGB8888:
4532 case DRM_FORMAT_XRGB2101010:
4533 case DRM_FORMAT_ARGB2101010:
4534 case DRM_FORMAT_XBGR2101010:
4535 case DRM_FORMAT_ABGR2101010:
4536 case DRM_FORMAT_YUYV:
4537 case DRM_FORMAT_YVYU:
4538 case DRM_FORMAT_UYVY:
4539 case DRM_FORMAT_VYUY:
4540 break;
4541 default:
4542 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4543 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4544 return -EINVAL;
4545 }
4546 }
4547
4548 /* mark this plane as a scaler user in crtc_state */
4549 scaler_state->scaler_users |= (1 << idx);
4550 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4551 "crtc_state = %p scaler_users = 0x%x\n",
4552 intel_plane ? "PLANE" : "CRTC",
4553 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4554 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4555 return 0;
4556}
4557
4558static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
bd2e244f
JB
4559{
4560 struct drm_device *dev = crtc->base.dev;
4561 struct drm_i915_private *dev_priv = dev->dev_private;
4562 int pipe = crtc->pipe;
a1b2278e
CK
4563 struct intel_crtc_scaler_state *scaler_state =
4564 &crtc->config->scaler_state;
4565
4566 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4567
4568 /* To update pfit, first update scaler state */
4569 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4570 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4571 skl_detach_scalers(crtc);
4572 if (!enable)
4573 return;
bd2e244f 4574
6e3c9717 4575 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4576 int id;
4577
4578 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4579 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4580 return;
4581 }
4582
4583 id = scaler_state->scaler_id;
4584 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4585 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4586 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4587 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4588
4589 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4590 }
4591}
4592
b074cec8
JB
4593static void ironlake_pfit_enable(struct intel_crtc *crtc)
4594{
4595 struct drm_device *dev = crtc->base.dev;
4596 struct drm_i915_private *dev_priv = dev->dev_private;
4597 int pipe = crtc->pipe;
4598
6e3c9717 4599 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4600 /* Force use of hard-coded filter coefficients
4601 * as some pre-programmed values are broken,
4602 * e.g. x201.
4603 */
4604 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4605 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4606 PF_PIPE_SEL_IVB(pipe));
4607 else
4608 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4609 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4610 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4611 }
4612}
4613
4a3b8769 4614static void intel_enable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4615{
4616 struct drm_device *dev = crtc->dev;
4617 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4618 struct drm_plane *plane;
bb53d4ae
VS
4619 struct intel_plane *intel_plane;
4620
af2b653b
MR
4621 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4622 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
4623 if (intel_plane->pipe == pipe)
4624 intel_plane_restore(&intel_plane->base);
af2b653b 4625 }
bb53d4ae
VS
4626}
4627
20bc8673 4628void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4629{
cea165c3
VS
4630 struct drm_device *dev = crtc->base.dev;
4631 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4632
6e3c9717 4633 if (!crtc->config->ips_enabled)
d77e4531
PZ
4634 return;
4635
cea165c3
VS
4636 /* We can only enable IPS after we enable a plane and wait for a vblank */
4637 intel_wait_for_vblank(dev, crtc->pipe);
4638
d77e4531 4639 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4640 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4641 mutex_lock(&dev_priv->rps.hw_lock);
4642 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4643 mutex_unlock(&dev_priv->rps.hw_lock);
4644 /* Quoting Art Runyan: "its not safe to expect any particular
4645 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4646 * mailbox." Moreover, the mailbox may return a bogus state,
4647 * so we need to just enable it and continue on.
2a114cc1
BW
4648 */
4649 } else {
4650 I915_WRITE(IPS_CTL, IPS_ENABLE);
4651 /* The bit only becomes 1 in the next vblank, so this wait here
4652 * is essentially intel_wait_for_vblank. If we don't have this
4653 * and don't wait for vblanks until the end of crtc_enable, then
4654 * the HW state readout code will complain that the expected
4655 * IPS_CTL value is not the one we read. */
4656 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4657 DRM_ERROR("Timed out waiting for IPS enable\n");
4658 }
d77e4531
PZ
4659}
4660
20bc8673 4661void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4662{
4663 struct drm_device *dev = crtc->base.dev;
4664 struct drm_i915_private *dev_priv = dev->dev_private;
4665
6e3c9717 4666 if (!crtc->config->ips_enabled)
d77e4531
PZ
4667 return;
4668
4669 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4670 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4671 mutex_lock(&dev_priv->rps.hw_lock);
4672 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4673 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4674 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4675 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4676 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4677 } else {
2a114cc1 4678 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4679 POSTING_READ(IPS_CTL);
4680 }
d77e4531
PZ
4681
4682 /* We need to wait for a vblank before we can disable the plane. */
4683 intel_wait_for_vblank(dev, crtc->pipe);
4684}
4685
4686/** Loads the palette/gamma unit for the CRTC with the prepared values */
4687static void intel_crtc_load_lut(struct drm_crtc *crtc)
4688{
4689 struct drm_device *dev = crtc->dev;
4690 struct drm_i915_private *dev_priv = dev->dev_private;
4691 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4692 enum pipe pipe = intel_crtc->pipe;
4693 int palreg = PALETTE(pipe);
4694 int i;
4695 bool reenable_ips = false;
4696
4697 /* The clocks have to be on to load the palette. */
83d65738 4698 if (!crtc->state->enable || !intel_crtc->active)
d77e4531
PZ
4699 return;
4700
50360403 4701 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
409ee761 4702 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4703 assert_dsi_pll_enabled(dev_priv);
4704 else
4705 assert_pll_enabled(dev_priv, pipe);
4706 }
4707
4708 /* use legacy palette for Ironlake */
7a1db49a 4709 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4710 palreg = LGC_PALETTE(pipe);
4711
4712 /* Workaround : Do not read or write the pipe palette/gamma data while
4713 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4714 */
6e3c9717 4715 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4716 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4717 GAMMA_MODE_MODE_SPLIT)) {
4718 hsw_disable_ips(intel_crtc);
4719 reenable_ips = true;
4720 }
4721
4722 for (i = 0; i < 256; i++) {
4723 I915_WRITE(palreg + 4 * i,
4724 (intel_crtc->lut_r[i] << 16) |
4725 (intel_crtc->lut_g[i] << 8) |
4726 intel_crtc->lut_b[i]);
4727 }
4728
4729 if (reenable_ips)
4730 hsw_enable_ips(intel_crtc);
4731}
4732
7cac945f 4733static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4734{
7cac945f 4735 if (intel_crtc->overlay) {
d3eedb1a
VS
4736 struct drm_device *dev = intel_crtc->base.dev;
4737 struct drm_i915_private *dev_priv = dev->dev_private;
4738
4739 mutex_lock(&dev->struct_mutex);
4740 dev_priv->mm.interruptible = false;
4741 (void) intel_overlay_switch_off(intel_crtc->overlay);
4742 dev_priv->mm.interruptible = true;
4743 mutex_unlock(&dev->struct_mutex);
4744 }
4745
4746 /* Let userspace switch the overlay on again. In most cases userspace
4747 * has to recompute where to put it anyway.
4748 */
4749}
4750
87d4300a
ML
4751/**
4752 * intel_post_enable_primary - Perform operations after enabling primary plane
4753 * @crtc: the CRTC whose primary plane was just enabled
4754 *
4755 * Performs potentially sleeping operations that must be done after the primary
4756 * plane is enabled, such as updating FBC and IPS. Note that this may be
4757 * called due to an explicit primary plane update, or due to an implicit
4758 * re-enable that is caused when a sprite plane is updated to no longer
4759 * completely hide the primary plane.
4760 */
4761static void
4762intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4763{
4764 struct drm_device *dev = crtc->dev;
87d4300a 4765 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4767 int pipe = intel_crtc->pipe;
a5c4d7bc 4768
87d4300a
ML
4769 /*
4770 * BDW signals flip done immediately if the plane
4771 * is disabled, even if the plane enable is already
4772 * armed to occur at the next vblank :(
4773 */
4774 if (IS_BROADWELL(dev))
4775 intel_wait_for_vblank(dev, pipe);
a5c4d7bc 4776
87d4300a
ML
4777 /*
4778 * FIXME IPS should be fine as long as one plane is
4779 * enabled, but in practice it seems to have problems
4780 * when going from primary only to sprite only and vice
4781 * versa.
4782 */
a5c4d7bc
VS
4783 hsw_enable_ips(intel_crtc);
4784
4785 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4786 intel_fbc_update(dev);
a5c4d7bc 4787 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4788
4789 /*
87d4300a
ML
4790 * Gen2 reports pipe underruns whenever all planes are disabled.
4791 * So don't enable underrun reporting before at least some planes
4792 * are enabled.
4793 * FIXME: Need to fix the logic to work when we turn off all planes
4794 * but leave the pipe running.
f99d7069 4795 */
87d4300a
ML
4796 if (IS_GEN2(dev))
4797 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4798
4799 /* Underruns don't raise interrupts, so check manually. */
4800 if (HAS_GMCH_DISPLAY(dev))
4801 i9xx_check_fifo_underruns(dev_priv);
a5c4d7bc
VS
4802}
4803
87d4300a
ML
4804/**
4805 * intel_pre_disable_primary - Perform operations before disabling primary plane
4806 * @crtc: the CRTC whose primary plane is to be disabled
4807 *
4808 * Performs potentially sleeping operations that must be done before the
4809 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4810 * be called due to an explicit primary plane update, or due to an implicit
4811 * disable that is caused when a sprite plane completely hides the primary
4812 * plane.
4813 */
4814static void
4815intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4816{
4817 struct drm_device *dev = crtc->dev;
4818 struct drm_i915_private *dev_priv = dev->dev_private;
4819 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4820 int pipe = intel_crtc->pipe;
a5c4d7bc 4821
87d4300a
ML
4822 /*
4823 * Gen2 reports pipe underruns whenever all planes are disabled.
4824 * So diasble underrun reporting before all the planes get disabled.
4825 * FIXME: Need to fix the logic to work when we turn off all planes
4826 * but leave the pipe running.
4827 */
4828 if (IS_GEN2(dev))
4829 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4830
87d4300a
ML
4831 /*
4832 * Vblank time updates from the shadow to live plane control register
4833 * are blocked if the memory self-refresh mode is active at that
4834 * moment. So to make sure the plane gets truly disabled, disable
4835 * first the self-refresh mode. The self-refresh enable bit in turn
4836 * will be checked/applied by the HW only at the next frame start
4837 * event which is after the vblank start event, so we need to have a
4838 * wait-for-vblank between disabling the plane and the pipe.
4839 */
4840 if (HAS_GMCH_DISPLAY(dev))
4841 intel_set_memory_cxsr(dev_priv, false);
4842
4843 mutex_lock(&dev->struct_mutex);
e35fef21 4844 if (dev_priv->fbc.crtc == intel_crtc)
7ff0ebcc 4845 intel_fbc_disable(dev);
87d4300a 4846 mutex_unlock(&dev->struct_mutex);
a5c4d7bc 4847
87d4300a
ML
4848 /*
4849 * FIXME IPS should be fine as long as one plane is
4850 * enabled, but in practice it seems to have problems
4851 * when going from primary only to sprite only and vice
4852 * versa.
4853 */
a5c4d7bc 4854 hsw_disable_ips(intel_crtc);
87d4300a
ML
4855}
4856
4857static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4858{
87d4300a
ML
4859 intel_enable_primary_hw_plane(crtc->primary, crtc);
4860 intel_enable_sprite_planes(crtc);
4861 intel_crtc_update_cursor(crtc, true);
87d4300a
ML
4862
4863 intel_post_enable_primary(crtc);
4864}
4865
4866static void intel_crtc_disable_planes(struct drm_crtc *crtc)
4867{
4868 struct drm_device *dev = crtc->dev;
4869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4870 struct intel_plane *intel_plane;
4871 int pipe = intel_crtc->pipe;
4872
4873 intel_crtc_wait_for_pending_flips(crtc);
4874
4875 intel_pre_disable_primary(crtc);
a5c4d7bc 4876
7cac945f 4877 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8
ML
4878 for_each_intel_plane(dev, intel_plane) {
4879 if (intel_plane->pipe == pipe) {
4880 struct drm_crtc *from = intel_plane->base.crtc;
4881
4882 intel_plane->disable_plane(&intel_plane->base,
4883 from ?: crtc, true);
4884 }
4885 }
f98551ae 4886
f99d7069
DV
4887 /*
4888 * FIXME: Once we grow proper nuclear flip support out of this we need
4889 * to compute the mask of flip planes precisely. For the time being
4890 * consider this a flip to a NULL plane.
4891 */
4892 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4893}
4894
f67a559d
JB
4895static void ironlake_crtc_enable(struct drm_crtc *crtc)
4896{
4897 struct drm_device *dev = crtc->dev;
4898 struct drm_i915_private *dev_priv = dev->dev_private;
4899 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4900 struct intel_encoder *encoder;
f67a559d 4901 int pipe = intel_crtc->pipe;
f67a559d 4902
83d65738 4903 WARN_ON(!crtc->state->enable);
08a48469 4904
f67a559d
JB
4905 if (intel_crtc->active)
4906 return;
4907
6e3c9717 4908 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4909 intel_prepare_shared_dpll(intel_crtc);
4910
6e3c9717 4911 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4912 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4913
4914 intel_set_pipe_timings(intel_crtc);
4915
6e3c9717 4916 if (intel_crtc->config->has_pch_encoder) {
29407aab 4917 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4918 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4919 }
4920
4921 ironlake_set_pipeconf(crtc);
4922
f67a559d 4923 intel_crtc->active = true;
8664281b 4924
a72e4c9f
DV
4925 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4926 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4927
f6736a1a 4928 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4929 if (encoder->pre_enable)
4930 encoder->pre_enable(encoder);
f67a559d 4931
6e3c9717 4932 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4933 /* Note: FDI PLL enabling _must_ be done before we enable the
4934 * cpu pipes, hence this is separate from all the other fdi/pch
4935 * enabling. */
88cefb6c 4936 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4937 } else {
4938 assert_fdi_tx_disabled(dev_priv, pipe);
4939 assert_fdi_rx_disabled(dev_priv, pipe);
4940 }
f67a559d 4941
b074cec8 4942 ironlake_pfit_enable(intel_crtc);
f67a559d 4943
9c54c0dd
JB
4944 /*
4945 * On ILK+ LUT must be loaded before the pipe is running but with
4946 * clocks enabled
4947 */
4948 intel_crtc_load_lut(crtc);
4949
f37fcc2a 4950 intel_update_watermarks(crtc);
e1fdc473 4951 intel_enable_pipe(intel_crtc);
f67a559d 4952
6e3c9717 4953 if (intel_crtc->config->has_pch_encoder)
f67a559d 4954 ironlake_pch_enable(crtc);
c98e9dcf 4955
f9b61ff6
DV
4956 assert_vblank_disabled(crtc);
4957 drm_crtc_vblank_on(crtc);
4958
fa5c73b1
DV
4959 for_each_encoder_on_crtc(dev, crtc, encoder)
4960 encoder->enable(encoder);
61b77ddd
DV
4961
4962 if (HAS_PCH_CPT(dev))
a1520318 4963 cpt_verify_modeset(dev, intel_crtc->pipe);
6be4a607
JB
4964}
4965
42db64ef
PZ
4966/* IPS only exists on ULT machines and is tied to pipe A. */
4967static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4968{
f5adf94e 4969 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4970}
4971
e4916946
PZ
4972/*
4973 * This implements the workaround described in the "notes" section of the mode
4974 * set sequence documentation. When going from no pipes or single pipe to
4975 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4976 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4977 */
4978static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4979{
4980 struct drm_device *dev = crtc->base.dev;
4981 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4982
4983 /* We want to get the other_active_crtc only if there's only 1 other
4984 * active crtc. */
d3fcc808 4985 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4986 if (!crtc_it->active || crtc_it == crtc)
4987 continue;
4988
4989 if (other_active_crtc)
4990 return;
4991
4992 other_active_crtc = crtc_it;
4993 }
4994 if (!other_active_crtc)
4995 return;
4996
4997 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4998 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4999}
5000
4f771f10
PZ
5001static void haswell_crtc_enable(struct drm_crtc *crtc)
5002{
5003 struct drm_device *dev = crtc->dev;
5004 struct drm_i915_private *dev_priv = dev->dev_private;
5005 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5006 struct intel_encoder *encoder;
5007 int pipe = intel_crtc->pipe;
4f771f10 5008
83d65738 5009 WARN_ON(!crtc->state->enable);
4f771f10
PZ
5010
5011 if (intel_crtc->active)
5012 return;
5013
df8ad70c
DV
5014 if (intel_crtc_to_shared_dpll(intel_crtc))
5015 intel_enable_shared_dpll(intel_crtc);
5016
6e3c9717 5017 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5018 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
5019
5020 intel_set_pipe_timings(intel_crtc);
5021
6e3c9717
ACO
5022 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5023 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5024 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5025 }
5026
6e3c9717 5027 if (intel_crtc->config->has_pch_encoder) {
229fca97 5028 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5029 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5030 }
5031
5032 haswell_set_pipeconf(crtc);
5033
5034 intel_set_pipe_csc(crtc);
5035
4f771f10 5036 intel_crtc->active = true;
8664281b 5037
a72e4c9f 5038 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
5039 for_each_encoder_on_crtc(dev, crtc, encoder)
5040 if (encoder->pre_enable)
5041 encoder->pre_enable(encoder);
5042
6e3c9717 5043 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
5044 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5045 true);
4fe9467d
ID
5046 dev_priv->display.fdi_link_train(crtc);
5047 }
5048
1f544388 5049 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5050
ff6d9f55 5051 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5052 skylake_pfit_update(intel_crtc, 1);
ff6d9f55 5053 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5054 ironlake_pfit_enable(intel_crtc);
ff6d9f55
JB
5055 else
5056 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10
PZ
5057
5058 /*
5059 * On ILK+ LUT must be loaded before the pipe is running but with
5060 * clocks enabled
5061 */
5062 intel_crtc_load_lut(crtc);
5063
1f544388 5064 intel_ddi_set_pipe_settings(crtc);
8228c251 5065 intel_ddi_enable_transcoder_func(crtc);
4f771f10 5066
f37fcc2a 5067 intel_update_watermarks(crtc);
e1fdc473 5068 intel_enable_pipe(intel_crtc);
42db64ef 5069
6e3c9717 5070 if (intel_crtc->config->has_pch_encoder)
1507e5bd 5071 lpt_pch_enable(crtc);
4f771f10 5072
6e3c9717 5073 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
5074 intel_ddi_set_vc_payload_alloc(crtc, true);
5075
f9b61ff6
DV
5076 assert_vblank_disabled(crtc);
5077 drm_crtc_vblank_on(crtc);
5078
8807e55b 5079 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 5080 encoder->enable(encoder);
8807e55b
JN
5081 intel_opregion_notify_encoder(encoder, true);
5082 }
4f771f10 5083
e4916946
PZ
5084 /* If we change the relative order between pipe/planes enabling, we need
5085 * to change the workaround. */
5086 haswell_mode_set_planes_workaround(intel_crtc);
4f771f10
PZ
5087}
5088
3f8dce3a
DV
5089static void ironlake_pfit_disable(struct intel_crtc *crtc)
5090{
5091 struct drm_device *dev = crtc->base.dev;
5092 struct drm_i915_private *dev_priv = dev->dev_private;
5093 int pipe = crtc->pipe;
5094
5095 /* To avoid upsetting the power well on haswell only disable the pfit if
5096 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 5097 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5098 I915_WRITE(PF_CTL(pipe), 0);
5099 I915_WRITE(PF_WIN_POS(pipe), 0);
5100 I915_WRITE(PF_WIN_SZ(pipe), 0);
5101 }
5102}
5103
6be4a607
JB
5104static void ironlake_crtc_disable(struct drm_crtc *crtc)
5105{
5106 struct drm_device *dev = crtc->dev;
5107 struct drm_i915_private *dev_priv = dev->dev_private;
5108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5109 struct intel_encoder *encoder;
6be4a607 5110 int pipe = intel_crtc->pipe;
5eddb70b 5111 u32 reg, temp;
b52eb4dc 5112
f7abfe8b
CW
5113 if (!intel_crtc->active)
5114 return;
5115
ea9d758d
DV
5116 for_each_encoder_on_crtc(dev, crtc, encoder)
5117 encoder->disable(encoder);
5118
f9b61ff6
DV
5119 drm_crtc_vblank_off(crtc);
5120 assert_vblank_disabled(crtc);
5121
6e3c9717 5122 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 5123 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 5124
575f7ab7 5125 intel_disable_pipe(intel_crtc);
32f9d658 5126
3f8dce3a 5127 ironlake_pfit_disable(intel_crtc);
2c07245f 5128
bf49ec8c
DV
5129 for_each_encoder_on_crtc(dev, crtc, encoder)
5130 if (encoder->post_disable)
5131 encoder->post_disable(encoder);
2c07245f 5132
6e3c9717 5133 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5134 ironlake_fdi_disable(crtc);
913d8d11 5135
d925c59a 5136 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5137
d925c59a
DV
5138 if (HAS_PCH_CPT(dev)) {
5139 /* disable TRANS_DP_CTL */
5140 reg = TRANS_DP_CTL(pipe);
5141 temp = I915_READ(reg);
5142 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5143 TRANS_DP_PORT_SEL_MASK);
5144 temp |= TRANS_DP_PORT_SEL_NONE;
5145 I915_WRITE(reg, temp);
5146
5147 /* disable DPLL_SEL */
5148 temp = I915_READ(PCH_DPLL_SEL);
11887397 5149 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5150 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5151 }
e3421a18 5152
d925c59a 5153 /* disable PCH DPLL */
e72f9fbf 5154 intel_disable_shared_dpll(intel_crtc);
8db9d77b 5155
d925c59a
DV
5156 ironlake_fdi_pll_disable(intel_crtc);
5157 }
6b383a7f 5158
f7abfe8b 5159 intel_crtc->active = false;
46ba614c 5160 intel_update_watermarks(crtc);
d1ebd816
BW
5161
5162 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5163 intel_fbc_update(dev);
d1ebd816 5164 mutex_unlock(&dev->struct_mutex);
6be4a607 5165}
1b3c7a47 5166
4f771f10 5167static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5168{
4f771f10
PZ
5169 struct drm_device *dev = crtc->dev;
5170 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5172 struct intel_encoder *encoder;
6e3c9717 5173 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5174
4f771f10
PZ
5175 if (!intel_crtc->active)
5176 return;
5177
8807e55b
JN
5178 for_each_encoder_on_crtc(dev, crtc, encoder) {
5179 intel_opregion_notify_encoder(encoder, false);
4f771f10 5180 encoder->disable(encoder);
8807e55b 5181 }
4f771f10 5182
f9b61ff6
DV
5183 drm_crtc_vblank_off(crtc);
5184 assert_vblank_disabled(crtc);
5185
6e3c9717 5186 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
5187 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5188 false);
575f7ab7 5189 intel_disable_pipe(intel_crtc);
4f771f10 5190
6e3c9717 5191 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5192 intel_ddi_set_vc_payload_alloc(crtc, false);
5193
ad80a810 5194 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5195
ff6d9f55 5196 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5197 skylake_pfit_update(intel_crtc, 0);
ff6d9f55 5198 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5199 ironlake_pfit_disable(intel_crtc);
ff6d9f55
JB
5200 else
5201 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10 5202
1f544388 5203 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5204
6e3c9717 5205 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 5206 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 5207 intel_ddi_fdi_disable(crtc);
83616634 5208 }
4f771f10 5209
97b040aa
ID
5210 for_each_encoder_on_crtc(dev, crtc, encoder)
5211 if (encoder->post_disable)
5212 encoder->post_disable(encoder);
5213
4f771f10 5214 intel_crtc->active = false;
46ba614c 5215 intel_update_watermarks(crtc);
4f771f10
PZ
5216
5217 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5218 intel_fbc_update(dev);
4f771f10 5219 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
5220
5221 if (intel_crtc_to_shared_dpll(intel_crtc))
5222 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
5223}
5224
ee7b9f93
JB
5225static void ironlake_crtc_off(struct drm_crtc *crtc)
5226{
5227 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 5228 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
5229}
5230
6441ab5f 5231
2dd24552
JB
5232static void i9xx_pfit_enable(struct intel_crtc *crtc)
5233{
5234 struct drm_device *dev = crtc->base.dev;
5235 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5236 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5237
681a8504 5238 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5239 return;
5240
2dd24552 5241 /*
c0b03411
DV
5242 * The panel fitter should only be adjusted whilst the pipe is disabled,
5243 * according to register description and PRM.
2dd24552 5244 */
c0b03411
DV
5245 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5246 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5247
b074cec8
JB
5248 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5249 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5250
5251 /* Border color in case we don't scale up to the full screen. Black by
5252 * default, change to something else for debugging. */
5253 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5254}
5255
d05410f9
DA
5256static enum intel_display_power_domain port_to_power_domain(enum port port)
5257{
5258 switch (port) {
5259 case PORT_A:
5260 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5261 case PORT_B:
5262 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5263 case PORT_C:
5264 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5265 case PORT_D:
5266 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5267 default:
5268 WARN_ON_ONCE(1);
5269 return POWER_DOMAIN_PORT_OTHER;
5270 }
5271}
5272
77d22dca
ID
5273#define for_each_power_domain(domain, mask) \
5274 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5275 if ((1 << (domain)) & (mask))
5276
319be8ae
ID
5277enum intel_display_power_domain
5278intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5279{
5280 struct drm_device *dev = intel_encoder->base.dev;
5281 struct intel_digital_port *intel_dig_port;
5282
5283 switch (intel_encoder->type) {
5284 case INTEL_OUTPUT_UNKNOWN:
5285 /* Only DDI platforms should ever use this output type */
5286 WARN_ON_ONCE(!HAS_DDI(dev));
5287 case INTEL_OUTPUT_DISPLAYPORT:
5288 case INTEL_OUTPUT_HDMI:
5289 case INTEL_OUTPUT_EDP:
5290 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5291 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5292 case INTEL_OUTPUT_DP_MST:
5293 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5294 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5295 case INTEL_OUTPUT_ANALOG:
5296 return POWER_DOMAIN_PORT_CRT;
5297 case INTEL_OUTPUT_DSI:
5298 return POWER_DOMAIN_PORT_DSI;
5299 default:
5300 return POWER_DOMAIN_PORT_OTHER;
5301 }
5302}
5303
5304static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5305{
319be8ae
ID
5306 struct drm_device *dev = crtc->dev;
5307 struct intel_encoder *intel_encoder;
5308 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5309 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
5310 unsigned long mask;
5311 enum transcoder transcoder;
5312
5313 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5314
5315 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5316 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
5317 if (intel_crtc->config->pch_pfit.enabled ||
5318 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
5319 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5320
319be8ae
ID
5321 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5322 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5323
77d22dca
ID
5324 return mask;
5325}
5326
679dacd4 5327static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
77d22dca 5328{
679dacd4 5329 struct drm_device *dev = state->dev;
77d22dca
ID
5330 struct drm_i915_private *dev_priv = dev->dev_private;
5331 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5332 struct intel_crtc *crtc;
5333
5334 /*
5335 * First get all needed power domains, then put all unneeded, to avoid
5336 * any unnecessary toggling of the power wells.
5337 */
d3fcc808 5338 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5339 enum intel_display_power_domain domain;
5340
83d65738 5341 if (!crtc->base.state->enable)
77d22dca
ID
5342 continue;
5343
319be8ae 5344 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
5345
5346 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5347 intel_display_power_get(dev_priv, domain);
5348 }
5349
50f6e502 5350 if (dev_priv->display.modeset_global_resources)
679dacd4 5351 dev_priv->display.modeset_global_resources(state);
50f6e502 5352
d3fcc808 5353 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5354 enum intel_display_power_domain domain;
5355
5356 for_each_power_domain(domain, crtc->enabled_power_domains)
5357 intel_display_power_put(dev_priv, domain);
5358
5359 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5360 }
5361
5362 intel_display_set_init_power(dev_priv, false);
5363}
5364
f8437dd1
VK
5365void broxton_set_cdclk(struct drm_device *dev, int frequency)
5366{
5367 struct drm_i915_private *dev_priv = dev->dev_private;
5368 uint32_t divider;
5369 uint32_t ratio;
5370 uint32_t current_freq;
5371 int ret;
5372
5373 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5374 switch (frequency) {
5375 case 144000:
5376 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5377 ratio = BXT_DE_PLL_RATIO(60);
5378 break;
5379 case 288000:
5380 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5381 ratio = BXT_DE_PLL_RATIO(60);
5382 break;
5383 case 384000:
5384 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5385 ratio = BXT_DE_PLL_RATIO(60);
5386 break;
5387 case 576000:
5388 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5389 ratio = BXT_DE_PLL_RATIO(60);
5390 break;
5391 case 624000:
5392 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5393 ratio = BXT_DE_PLL_RATIO(65);
5394 break;
5395 case 19200:
5396 /*
5397 * Bypass frequency with DE PLL disabled. Init ratio, divider
5398 * to suppress GCC warning.
5399 */
5400 ratio = 0;
5401 divider = 0;
5402 break;
5403 default:
5404 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5405
5406 return;
5407 }
5408
5409 mutex_lock(&dev_priv->rps.hw_lock);
5410 /* Inform power controller of upcoming frequency change */
5411 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5412 0x80000000);
5413 mutex_unlock(&dev_priv->rps.hw_lock);
5414
5415 if (ret) {
5416 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5417 ret, frequency);
5418 return;
5419 }
5420
5421 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5422 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5423 current_freq = current_freq * 500 + 1000;
5424
5425 /*
5426 * DE PLL has to be disabled when
5427 * - setting to 19.2MHz (bypass, PLL isn't used)
5428 * - before setting to 624MHz (PLL needs toggling)
5429 * - before setting to any frequency from 624MHz (PLL needs toggling)
5430 */
5431 if (frequency == 19200 || frequency == 624000 ||
5432 current_freq == 624000) {
5433 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5434 /* Timeout 200us */
5435 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5436 1))
5437 DRM_ERROR("timout waiting for DE PLL unlock\n");
5438 }
5439
5440 if (frequency != 19200) {
5441 uint32_t val;
5442
5443 val = I915_READ(BXT_DE_PLL_CTL);
5444 val &= ~BXT_DE_PLL_RATIO_MASK;
5445 val |= ratio;
5446 I915_WRITE(BXT_DE_PLL_CTL, val);
5447
5448 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5449 /* Timeout 200us */
5450 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5451 DRM_ERROR("timeout waiting for DE PLL lock\n");
5452
5453 val = I915_READ(CDCLK_CTL);
5454 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5455 val |= divider;
5456 /*
5457 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5458 * enable otherwise.
5459 */
5460 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5461 if (frequency >= 500000)
5462 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5463
5464 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5465 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5466 val |= (frequency - 1000) / 500;
5467 I915_WRITE(CDCLK_CTL, val);
5468 }
5469
5470 mutex_lock(&dev_priv->rps.hw_lock);
5471 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5472 DIV_ROUND_UP(frequency, 25000));
5473 mutex_unlock(&dev_priv->rps.hw_lock);
5474
5475 if (ret) {
5476 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5477 ret, frequency);
5478 return;
5479 }
5480
5481 dev_priv->cdclk_freq = frequency;
5482}
5483
5484void broxton_init_cdclk(struct drm_device *dev)
5485{
5486 struct drm_i915_private *dev_priv = dev->dev_private;
5487 uint32_t val;
5488
5489 /*
5490 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5491 * or else the reset will hang because there is no PCH to respond.
5492 * Move the handshake programming to initialization sequence.
5493 * Previously was left up to BIOS.
5494 */
5495 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5496 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5497 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5498
5499 /* Enable PG1 for cdclk */
5500 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5501
5502 /* check if cd clock is enabled */
5503 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5504 DRM_DEBUG_KMS("Display already initialized\n");
5505 return;
5506 }
5507
5508 /*
5509 * FIXME:
5510 * - The initial CDCLK needs to be read from VBT.
5511 * Need to make this change after VBT has changes for BXT.
5512 * - check if setting the max (or any) cdclk freq is really necessary
5513 * here, it belongs to modeset time
5514 */
5515 broxton_set_cdclk(dev, 624000);
5516
5517 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5518 POSTING_READ(DBUF_CTL);
5519
f8437dd1
VK
5520 udelay(10);
5521
5522 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5523 DRM_ERROR("DBuf power enable timeout!\n");
5524}
5525
5526void broxton_uninit_cdclk(struct drm_device *dev)
5527{
5528 struct drm_i915_private *dev_priv = dev->dev_private;
5529
5530 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5531 POSTING_READ(DBUF_CTL);
5532
f8437dd1
VK
5533 udelay(10);
5534
5535 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5536 DRM_ERROR("DBuf power disable timeout!\n");
5537
5538 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5539 broxton_set_cdclk(dev, 19200);
5540
5541 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5542}
5543
dfcab17e 5544/* returns HPLL frequency in kHz */
f8bf63fd 5545static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 5546{
586f49dc 5547 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 5548
586f49dc
JB
5549 /* Obtain SKU information */
5550 mutex_lock(&dev_priv->dpio_lock);
5551 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5552 CCK_FUSE_HPLL_FREQ_MASK;
5553 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 5554
dfcab17e 5555 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
5556}
5557
f8bf63fd
VS
5558static void vlv_update_cdclk(struct drm_device *dev)
5559{
5560 struct drm_i915_private *dev_priv = dev->dev_private;
5561
164dfd28 5562 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
43dc52c3 5563 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
164dfd28 5564 dev_priv->cdclk_freq);
f8bf63fd
VS
5565
5566 /*
5567 * Program the gmbus_freq based on the cdclk frequency.
5568 * BSpec erroneously claims we should aim for 4MHz, but
5569 * in fact 1MHz is the correct frequency.
5570 */
164dfd28 5571 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
f8bf63fd
VS
5572}
5573
30a970c6
JB
5574/* Adjust CDclk dividers to allow high res or save power if possible */
5575static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5576{
5577 struct drm_i915_private *dev_priv = dev->dev_private;
5578 u32 val, cmd;
5579
164dfd28
VK
5580 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5581 != dev_priv->cdclk_freq);
d60c4473 5582
dfcab17e 5583 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5584 cmd = 2;
dfcab17e 5585 else if (cdclk == 266667)
30a970c6
JB
5586 cmd = 1;
5587 else
5588 cmd = 0;
5589
5590 mutex_lock(&dev_priv->rps.hw_lock);
5591 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5592 val &= ~DSPFREQGUAR_MASK;
5593 val |= (cmd << DSPFREQGUAR_SHIFT);
5594 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5595 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5596 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5597 50)) {
5598 DRM_ERROR("timed out waiting for CDclk change\n");
5599 }
5600 mutex_unlock(&dev_priv->rps.hw_lock);
5601
dfcab17e 5602 if (cdclk == 400000) {
6bcda4f0 5603 u32 divider;
30a970c6 5604
6bcda4f0 5605 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6
JB
5606
5607 mutex_lock(&dev_priv->dpio_lock);
5608 /* adjust cdclk divider */
5609 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 5610 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
5611 val |= divider;
5612 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5613
5614 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5615 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5616 50))
5617 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5618 mutex_unlock(&dev_priv->dpio_lock);
5619 }
5620
5621 mutex_lock(&dev_priv->dpio_lock);
5622 /* adjust self-refresh exit latency value */
5623 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5624 val &= ~0x7f;
5625
5626 /*
5627 * For high bandwidth configs, we set a higher latency in the bunit
5628 * so that the core display fetch happens in time to avoid underruns.
5629 */
dfcab17e 5630 if (cdclk == 400000)
30a970c6
JB
5631 val |= 4500 / 250; /* 4.5 usec */
5632 else
5633 val |= 3000 / 250; /* 3.0 usec */
5634 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5635 mutex_unlock(&dev_priv->dpio_lock);
5636
f8bf63fd 5637 vlv_update_cdclk(dev);
30a970c6
JB
5638}
5639
383c5a6a
VS
5640static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5641{
5642 struct drm_i915_private *dev_priv = dev->dev_private;
5643 u32 val, cmd;
5644
164dfd28
VK
5645 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5646 != dev_priv->cdclk_freq);
383c5a6a
VS
5647
5648 switch (cdclk) {
383c5a6a
VS
5649 case 333333:
5650 case 320000:
383c5a6a 5651 case 266667:
383c5a6a 5652 case 200000:
383c5a6a
VS
5653 break;
5654 default:
5f77eeb0 5655 MISSING_CASE(cdclk);
383c5a6a
VS
5656 return;
5657 }
5658
9d0d3fda
VS
5659 /*
5660 * Specs are full of misinformation, but testing on actual
5661 * hardware has shown that we just need to write the desired
5662 * CCK divider into the Punit register.
5663 */
5664 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5665
383c5a6a
VS
5666 mutex_lock(&dev_priv->rps.hw_lock);
5667 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5668 val &= ~DSPFREQGUAR_MASK_CHV;
5669 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5670 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5671 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5672 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5673 50)) {
5674 DRM_ERROR("timed out waiting for CDclk change\n");
5675 }
5676 mutex_unlock(&dev_priv->rps.hw_lock);
5677
5678 vlv_update_cdclk(dev);
5679}
5680
30a970c6
JB
5681static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5682 int max_pixclk)
5683{
6bcda4f0 5684 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5685 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5686
30a970c6
JB
5687 /*
5688 * Really only a few cases to deal with, as only 4 CDclks are supported:
5689 * 200MHz
5690 * 267MHz
29dc7ef3 5691 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5692 * 400MHz (VLV only)
5693 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5694 * of the lower bin and adjust if needed.
e37c67a1
VS
5695 *
5696 * We seem to get an unstable or solid color picture at 200MHz.
5697 * Not sure what's wrong. For now use 200MHz only when all pipes
5698 * are off.
30a970c6 5699 */
6cca3195
VS
5700 if (!IS_CHERRYVIEW(dev_priv) &&
5701 max_pixclk > freq_320*limit/100)
dfcab17e 5702 return 400000;
6cca3195 5703 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5704 return freq_320;
e37c67a1 5705 else if (max_pixclk > 0)
dfcab17e 5706 return 266667;
e37c67a1
VS
5707 else
5708 return 200000;
30a970c6
JB
5709}
5710
f8437dd1
VK
5711static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5712 int max_pixclk)
5713{
5714 /*
5715 * FIXME:
5716 * - remove the guardband, it's not needed on BXT
5717 * - set 19.2MHz bypass frequency if there are no active pipes
5718 */
5719 if (max_pixclk > 576000*9/10)
5720 return 624000;
5721 else if (max_pixclk > 384000*9/10)
5722 return 576000;
5723 else if (max_pixclk > 288000*9/10)
5724 return 384000;
5725 else if (max_pixclk > 144000*9/10)
5726 return 288000;
5727 else
5728 return 144000;
5729}
5730
2f2d7aa1 5731/* compute the max pixel clock for new configuration */
304603f4 5732static int intel_mode_max_pixclk(struct drm_atomic_state *state)
30a970c6 5733{
304603f4 5734 struct drm_device *dev = state->dev;
30a970c6 5735 struct intel_crtc *intel_crtc;
304603f4 5736 struct intel_crtc_state *crtc_state;
30a970c6
JB
5737 int max_pixclk = 0;
5738
d3fcc808 5739 for_each_intel_crtc(dev, intel_crtc) {
304603f4
ACO
5740 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5741 if (IS_ERR(crtc_state))
5742 return PTR_ERR(crtc_state);
5743
5744 if (!crtc_state->base.enable)
5745 continue;
5746
5747 max_pixclk = max(max_pixclk,
5748 crtc_state->base.adjusted_mode.crtc_clock);
30a970c6
JB
5749 }
5750
5751 return max_pixclk;
5752}
5753
0a9ab303 5754static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
30a970c6 5755{
304603f4 5756 struct drm_i915_private *dev_priv = to_i915(state->dev);
0a9ab303
ACO
5757 struct drm_crtc *crtc;
5758 struct drm_crtc_state *crtc_state;
304603f4 5759 int max_pixclk = intel_mode_max_pixclk(state);
0a9ab303 5760 int cdclk, i;
30a970c6 5761
304603f4
ACO
5762 if (max_pixclk < 0)
5763 return max_pixclk;
30a970c6 5764
f8437dd1
VK
5765 if (IS_VALLEYVIEW(dev_priv))
5766 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5767 else
5768 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5769
5770 if (cdclk == dev_priv->cdclk_freq)
304603f4 5771 return 0;
30a970c6 5772
0a9ab303
ACO
5773 /* add all active pipes to the state */
5774 for_each_crtc(state->dev, crtc) {
5775 if (!crtc->state->enable)
5776 continue;
5777
5778 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5779 if (IS_ERR(crtc_state))
5780 return PTR_ERR(crtc_state);
5781 }
5782
2f2d7aa1 5783 /* disable/enable all currently active pipes while we change cdclk */
0a9ab303
ACO
5784 for_each_crtc_in_state(state, crtc, crtc_state, i)
5785 if (crtc_state->enable)
5786 crtc_state->mode_changed = true;
304603f4
ACO
5787
5788 return 0;
30a970c6
JB
5789}
5790
1e69cd74
VS
5791static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5792{
5793 unsigned int credits, default_credits;
5794
5795 if (IS_CHERRYVIEW(dev_priv))
5796 default_credits = PFI_CREDIT(12);
5797 else
5798 default_credits = PFI_CREDIT(8);
5799
164dfd28 5800 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
1e69cd74
VS
5801 /* CHV suggested value is 31 or 63 */
5802 if (IS_CHERRYVIEW(dev_priv))
5803 credits = PFI_CREDIT_31;
5804 else
5805 credits = PFI_CREDIT(15);
5806 } else {
5807 credits = default_credits;
5808 }
5809
5810 /*
5811 * WA - write default credits before re-programming
5812 * FIXME: should we also set the resend bit here?
5813 */
5814 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5815 default_credits);
5816
5817 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5818 credits | PFI_CREDIT_RESEND);
5819
5820 /*
5821 * FIXME is this guaranteed to clear
5822 * immediately or should we poll for it?
5823 */
5824 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5825}
5826
679dacd4 5827static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
30a970c6 5828{
679dacd4 5829 struct drm_device *dev = state->dev;
30a970c6 5830 struct drm_i915_private *dev_priv = dev->dev_private;
304603f4
ACO
5831 int max_pixclk = intel_mode_max_pixclk(state);
5832 int req_cdclk;
5833
5834 /* The only reason this can fail is if we fail to add the crtc_state
5835 * to the atomic state. But that can't happen since the call to
5836 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5837 * can't have failed otherwise the mode set would be aborted) added all
5838 * the states already. */
5839 if (WARN_ON(max_pixclk < 0))
5840 return;
30a970c6 5841
304603f4 5842 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
30a970c6 5843
164dfd28 5844 if (req_cdclk != dev_priv->cdclk_freq) {
738c05c0
ID
5845 /*
5846 * FIXME: We can end up here with all power domains off, yet
5847 * with a CDCLK frequency other than the minimum. To account
5848 * for this take the PIPE-A power domain, which covers the HW
5849 * blocks needed for the following programming. This can be
5850 * removed once it's guaranteed that we get here either with
5851 * the minimum CDCLK set, or the required power domains
5852 * enabled.
5853 */
5854 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5855
383c5a6a
VS
5856 if (IS_CHERRYVIEW(dev))
5857 cherryview_set_cdclk(dev, req_cdclk);
5858 else
5859 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 5860
1e69cd74
VS
5861 vlv_program_pfi_credits(dev_priv);
5862
738c05c0 5863 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
383c5a6a 5864 }
30a970c6
JB
5865}
5866
89b667f8
JB
5867static void valleyview_crtc_enable(struct drm_crtc *crtc)
5868{
5869 struct drm_device *dev = crtc->dev;
a72e4c9f 5870 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
5871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5872 struct intel_encoder *encoder;
5873 int pipe = intel_crtc->pipe;
23538ef1 5874 bool is_dsi;
89b667f8 5875
83d65738 5876 WARN_ON(!crtc->state->enable);
89b667f8
JB
5877
5878 if (intel_crtc->active)
5879 return;
5880
409ee761 5881 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 5882
1ae0d137
VS
5883 if (!is_dsi) {
5884 if (IS_CHERRYVIEW(dev))
6e3c9717 5885 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5886 else
6e3c9717 5887 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5888 }
5b18e57c 5889
6e3c9717 5890 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5891 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5892
5893 intel_set_pipe_timings(intel_crtc);
5894
c14b0485
VS
5895 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5896 struct drm_i915_private *dev_priv = dev->dev_private;
5897
5898 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5899 I915_WRITE(CHV_CANVAS(pipe), 0);
5900 }
5901
5b18e57c
DV
5902 i9xx_set_pipeconf(intel_crtc);
5903
89b667f8 5904 intel_crtc->active = true;
89b667f8 5905
a72e4c9f 5906 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5907
89b667f8
JB
5908 for_each_encoder_on_crtc(dev, crtc, encoder)
5909 if (encoder->pre_pll_enable)
5910 encoder->pre_pll_enable(encoder);
5911
9d556c99
CML
5912 if (!is_dsi) {
5913 if (IS_CHERRYVIEW(dev))
6e3c9717 5914 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5915 else
6e3c9717 5916 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5917 }
89b667f8
JB
5918
5919 for_each_encoder_on_crtc(dev, crtc, encoder)
5920 if (encoder->pre_enable)
5921 encoder->pre_enable(encoder);
5922
2dd24552
JB
5923 i9xx_pfit_enable(intel_crtc);
5924
63cbb074
VS
5925 intel_crtc_load_lut(crtc);
5926
f37fcc2a 5927 intel_update_watermarks(crtc);
e1fdc473 5928 intel_enable_pipe(intel_crtc);
be6a6f8e 5929
4b3a9526
VS
5930 assert_vblank_disabled(crtc);
5931 drm_crtc_vblank_on(crtc);
5932
f9b61ff6
DV
5933 for_each_encoder_on_crtc(dev, crtc, encoder)
5934 encoder->enable(encoder);
89b667f8
JB
5935}
5936
f13c2ef3
DV
5937static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5938{
5939 struct drm_device *dev = crtc->base.dev;
5940 struct drm_i915_private *dev_priv = dev->dev_private;
5941
6e3c9717
ACO
5942 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5943 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
5944}
5945
0b8765c6 5946static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
5947{
5948 struct drm_device *dev = crtc->dev;
a72e4c9f 5949 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 5950 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5951 struct intel_encoder *encoder;
79e53945 5952 int pipe = intel_crtc->pipe;
79e53945 5953
83d65738 5954 WARN_ON(!crtc->state->enable);
08a48469 5955
f7abfe8b
CW
5956 if (intel_crtc->active)
5957 return;
5958
f13c2ef3
DV
5959 i9xx_set_pll_dividers(intel_crtc);
5960
6e3c9717 5961 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5962 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5963
5964 intel_set_pipe_timings(intel_crtc);
5965
5b18e57c
DV
5966 i9xx_set_pipeconf(intel_crtc);
5967
f7abfe8b 5968 intel_crtc->active = true;
6b383a7f 5969
4a3436e8 5970 if (!IS_GEN2(dev))
a72e4c9f 5971 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5972
9d6d9f19
MK
5973 for_each_encoder_on_crtc(dev, crtc, encoder)
5974 if (encoder->pre_enable)
5975 encoder->pre_enable(encoder);
5976
f6736a1a
DV
5977 i9xx_enable_pll(intel_crtc);
5978
2dd24552
JB
5979 i9xx_pfit_enable(intel_crtc);
5980
63cbb074
VS
5981 intel_crtc_load_lut(crtc);
5982
f37fcc2a 5983 intel_update_watermarks(crtc);
e1fdc473 5984 intel_enable_pipe(intel_crtc);
be6a6f8e 5985
4b3a9526
VS
5986 assert_vblank_disabled(crtc);
5987 drm_crtc_vblank_on(crtc);
5988
f9b61ff6
DV
5989 for_each_encoder_on_crtc(dev, crtc, encoder)
5990 encoder->enable(encoder);
0b8765c6 5991}
79e53945 5992
87476d63
DV
5993static void i9xx_pfit_disable(struct intel_crtc *crtc)
5994{
5995 struct drm_device *dev = crtc->base.dev;
5996 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 5997
6e3c9717 5998 if (!crtc->config->gmch_pfit.control)
328d8e82 5999 return;
87476d63 6000
328d8e82 6001 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 6002
328d8e82
DV
6003 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6004 I915_READ(PFIT_CONTROL));
6005 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
6006}
6007
0b8765c6
JB
6008static void i9xx_crtc_disable(struct drm_crtc *crtc)
6009{
6010 struct drm_device *dev = crtc->dev;
6011 struct drm_i915_private *dev_priv = dev->dev_private;
6012 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 6013 struct intel_encoder *encoder;
0b8765c6 6014 int pipe = intel_crtc->pipe;
ef9c3aee 6015
f7abfe8b
CW
6016 if (!intel_crtc->active)
6017 return;
6018
6304cd91
VS
6019 /*
6020 * On gen2 planes are double buffered but the pipe isn't, so we must
6021 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6022 * We also need to wait on all gmch platforms because of the
6023 * self-refresh mode constraint explained above.
6304cd91 6024 */
564ed191 6025 intel_wait_for_vblank(dev, pipe);
6304cd91 6026
4b3a9526
VS
6027 for_each_encoder_on_crtc(dev, crtc, encoder)
6028 encoder->disable(encoder);
6029
f9b61ff6
DV
6030 drm_crtc_vblank_off(crtc);
6031 assert_vblank_disabled(crtc);
6032
575f7ab7 6033 intel_disable_pipe(intel_crtc);
24a1f16d 6034
87476d63 6035 i9xx_pfit_disable(intel_crtc);
24a1f16d 6036
89b667f8
JB
6037 for_each_encoder_on_crtc(dev, crtc, encoder)
6038 if (encoder->post_disable)
6039 encoder->post_disable(encoder);
6040
409ee761 6041 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6042 if (IS_CHERRYVIEW(dev))
6043 chv_disable_pll(dev_priv, pipe);
6044 else if (IS_VALLEYVIEW(dev))
6045 vlv_disable_pll(dev_priv, pipe);
6046 else
1c4e0274 6047 i9xx_disable_pll(intel_crtc);
076ed3b2 6048 }
0b8765c6 6049
4a3436e8 6050 if (!IS_GEN2(dev))
a72e4c9f 6051 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 6052
f7abfe8b 6053 intel_crtc->active = false;
46ba614c 6054 intel_update_watermarks(crtc);
f37fcc2a 6055
efa9624e 6056 mutex_lock(&dev->struct_mutex);
7ff0ebcc 6057 intel_fbc_update(dev);
efa9624e 6058 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
6059}
6060
ee7b9f93
JB
6061static void i9xx_crtc_off(struct drm_crtc *crtc)
6062{
6063}
6064
b04c5bd6
BF
6065/* Master function to enable/disable CRTC and corresponding power wells */
6066void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
6067{
6068 struct drm_device *dev = crtc->dev;
6069 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 6070 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
6071 enum intel_display_power_domain domain;
6072 unsigned long domains;
976f8a20 6073
0e572fe7
DV
6074 if (enable) {
6075 if (!intel_crtc->active) {
e1e9fb84
DV
6076 domains = get_crtc_power_domains(crtc);
6077 for_each_power_domain(domain, domains)
6078 intel_display_power_get(dev_priv, domain);
6079 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
6080
6081 dev_priv->display.crtc_enable(crtc);
ce22dba9 6082 intel_crtc_enable_planes(crtc);
0e572fe7
DV
6083 }
6084 } else {
6085 if (intel_crtc->active) {
ce22dba9 6086 intel_crtc_disable_planes(crtc);
0e572fe7
DV
6087 dev_priv->display.crtc_disable(crtc);
6088
e1e9fb84
DV
6089 domains = intel_crtc->enabled_power_domains;
6090 for_each_power_domain(domain, domains)
6091 intel_display_power_put(dev_priv, domain);
6092 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
6093 }
6094 }
b04c5bd6
BF
6095}
6096
6097/**
6098 * Sets the power management mode of the pipe and plane.
6099 */
6100void intel_crtc_update_dpms(struct drm_crtc *crtc)
6101{
6102 struct drm_device *dev = crtc->dev;
6103 struct intel_encoder *intel_encoder;
6104 bool enable = false;
6105
6106 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6107 enable |= intel_encoder->connectors_active;
6108
6109 intel_crtc_control(crtc, enable);
976f8a20
DV
6110}
6111
cdd59983
CW
6112static void intel_crtc_disable(struct drm_crtc *crtc)
6113{
cdd59983 6114 struct drm_device *dev = crtc->dev;
976f8a20 6115 struct drm_connector *connector;
ee7b9f93 6116 struct drm_i915_private *dev_priv = dev->dev_private;
cdd59983 6117
976f8a20 6118 /* crtc should still be enabled when we disable it. */
83d65738 6119 WARN_ON(!crtc->state->enable);
976f8a20 6120
ce22dba9 6121 intel_crtc_disable_planes(crtc);
976f8a20 6122 dev_priv->display.crtc_disable(crtc);
ee7b9f93
JB
6123 dev_priv->display.off(crtc);
6124
70a101f8 6125 drm_plane_helper_disable(crtc->primary);
976f8a20
DV
6126
6127 /* Update computed state. */
6128 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6129 if (!connector->encoder || !connector->encoder->crtc)
6130 continue;
6131
6132 if (connector->encoder->crtc != crtc)
6133 continue;
6134
6135 connector->dpms = DRM_MODE_DPMS_OFF;
6136 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
6137 }
6138}
6139
ea5b213a 6140void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6141{
4ef69c7a 6142 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6143
ea5b213a
CW
6144 drm_encoder_cleanup(encoder);
6145 kfree(intel_encoder);
7e7d76c3
JB
6146}
6147
9237329d 6148/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
6149 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6150 * state of the entire output pipe. */
9237329d 6151static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 6152{
5ab432ef
DV
6153 if (mode == DRM_MODE_DPMS_ON) {
6154 encoder->connectors_active = true;
6155
b2cabb0e 6156 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
6157 } else {
6158 encoder->connectors_active = false;
6159
b2cabb0e 6160 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 6161 }
79e53945
JB
6162}
6163
0a91ca29
DV
6164/* Cross check the actual hw state with our own modeset state tracking (and it's
6165 * internal consistency). */
b980514c 6166static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6167{
0a91ca29
DV
6168 if (connector->get_hw_state(connector)) {
6169 struct intel_encoder *encoder = connector->encoder;
6170 struct drm_crtc *crtc;
6171 bool encoder_enabled;
6172 enum pipe pipe;
6173
6174 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6175 connector->base.base.id,
c23cc417 6176 connector->base.name);
0a91ca29 6177
0e32b39c
DA
6178 /* there is no real hw state for MST connectors */
6179 if (connector->mst_port)
6180 return;
6181
e2c719b7 6182 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 6183 "wrong connector dpms state\n");
e2c719b7 6184 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 6185 "active connector not linked to encoder\n");
0a91ca29 6186
36cd7444 6187 if (encoder) {
e2c719b7 6188 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
6189 "encoder->connectors_active not set\n");
6190
6191 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
6192 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6193 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 6194 return;
0a91ca29 6195
36cd7444 6196 crtc = encoder->base.crtc;
0a91ca29 6197
83d65738
MR
6198 I915_STATE_WARN(!crtc->state->enable,
6199 "crtc not enabled\n");
e2c719b7
RC
6200 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6201 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
6202 "encoder active on the wrong pipe\n");
6203 }
0a91ca29 6204 }
79e53945
JB
6205}
6206
08d9bc92
ACO
6207int intel_connector_init(struct intel_connector *connector)
6208{
6209 struct drm_connector_state *connector_state;
6210
6211 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6212 if (!connector_state)
6213 return -ENOMEM;
6214
6215 connector->base.state = connector_state;
6216 return 0;
6217}
6218
6219struct intel_connector *intel_connector_alloc(void)
6220{
6221 struct intel_connector *connector;
6222
6223 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6224 if (!connector)
6225 return NULL;
6226
6227 if (intel_connector_init(connector) < 0) {
6228 kfree(connector);
6229 return NULL;
6230 }
6231
6232 return connector;
6233}
6234
5ab432ef
DV
6235/* Even simpler default implementation, if there's really no special case to
6236 * consider. */
6237void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 6238{
5ab432ef
DV
6239 /* All the simple cases only support two dpms states. */
6240 if (mode != DRM_MODE_DPMS_ON)
6241 mode = DRM_MODE_DPMS_OFF;
d4270e57 6242
5ab432ef
DV
6243 if (mode == connector->dpms)
6244 return;
6245
6246 connector->dpms = mode;
6247
6248 /* Only need to change hw state when actually enabled */
c9976dcf
CW
6249 if (connector->encoder)
6250 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 6251
b980514c 6252 intel_modeset_check_state(connector->dev);
79e53945
JB
6253}
6254
f0947c37
DV
6255/* Simple connector->get_hw_state implementation for encoders that support only
6256 * one connector and no cloning and hence the encoder state determines the state
6257 * of the connector. */
6258bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6259{
24929352 6260 enum pipe pipe = 0;
f0947c37 6261 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6262
f0947c37 6263 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6264}
6265
6d293983 6266static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6267{
6d293983
ACO
6268 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6269 return crtc_state->fdi_lanes;
d272ddfa
VS
6270
6271 return 0;
6272}
6273
6d293983 6274static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6275 struct intel_crtc_state *pipe_config)
1857e1da 6276{
6d293983
ACO
6277 struct drm_atomic_state *state = pipe_config->base.state;
6278 struct intel_crtc *other_crtc;
6279 struct intel_crtc_state *other_crtc_state;
6280
1857e1da
DV
6281 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6282 pipe_name(pipe), pipe_config->fdi_lanes);
6283 if (pipe_config->fdi_lanes > 4) {
6284 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6285 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6286 return -EINVAL;
1857e1da
DV
6287 }
6288
bafb6553 6289 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6290 if (pipe_config->fdi_lanes > 2) {
6291 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6292 pipe_config->fdi_lanes);
6d293983 6293 return -EINVAL;
1857e1da 6294 } else {
6d293983 6295 return 0;
1857e1da
DV
6296 }
6297 }
6298
6299 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6300 return 0;
1857e1da
DV
6301
6302 /* Ivybridge 3 pipe is really complicated */
6303 switch (pipe) {
6304 case PIPE_A:
6d293983 6305 return 0;
1857e1da 6306 case PIPE_B:
6d293983
ACO
6307 if (pipe_config->fdi_lanes <= 2)
6308 return 0;
6309
6310 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6311 other_crtc_state =
6312 intel_atomic_get_crtc_state(state, other_crtc);
6313 if (IS_ERR(other_crtc_state))
6314 return PTR_ERR(other_crtc_state);
6315
6316 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6317 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6318 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6319 return -EINVAL;
1857e1da 6320 }
6d293983 6321 return 0;
1857e1da 6322 case PIPE_C:
251cc67c
VS
6323 if (pipe_config->fdi_lanes > 2) {
6324 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6325 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6326 return -EINVAL;
251cc67c 6327 }
6d293983
ACO
6328
6329 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6330 other_crtc_state =
6331 intel_atomic_get_crtc_state(state, other_crtc);
6332 if (IS_ERR(other_crtc_state))
6333 return PTR_ERR(other_crtc_state);
6334
6335 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6336 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6337 return -EINVAL;
1857e1da 6338 }
6d293983 6339 return 0;
1857e1da
DV
6340 default:
6341 BUG();
6342 }
6343}
6344
e29c22c0
DV
6345#define RETRY 1
6346static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6347 struct intel_crtc_state *pipe_config)
877d48d5 6348{
1857e1da 6349 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 6350 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6351 int lane, link_bw, fdi_dotclock, ret;
6352 bool needs_recompute = false;
877d48d5 6353
e29c22c0 6354retry:
877d48d5
DV
6355 /* FDI is a binary signal running at ~2.7GHz, encoding
6356 * each output octet as 10 bits. The actual frequency
6357 * is stored as a divider into a 100MHz clock, and the
6358 * mode pixel clock is stored in units of 1KHz.
6359 * Hence the bw of each lane in terms of the mode signal
6360 * is:
6361 */
6362 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6363
241bfc38 6364 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6365
2bd89a07 6366 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6367 pipe_config->pipe_bpp);
6368
6369 pipe_config->fdi_lanes = lane;
6370
2bd89a07 6371 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6372 link_bw, &pipe_config->fdi_m_n);
1857e1da 6373
6d293983
ACO
6374 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6375 intel_crtc->pipe, pipe_config);
6376 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6377 pipe_config->pipe_bpp -= 2*3;
6378 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6379 pipe_config->pipe_bpp);
6380 needs_recompute = true;
6381 pipe_config->bw_constrained = true;
6382
6383 goto retry;
6384 }
6385
6386 if (needs_recompute)
6387 return RETRY;
6388
6d293983 6389 return ret;
877d48d5
DV
6390}
6391
42db64ef 6392static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6393 struct intel_crtc_state *pipe_config)
42db64ef 6394{
d330a953 6395 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 6396 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 6397 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
6398}
6399
a43f6e0f 6400static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6401 struct intel_crtc_state *pipe_config)
79e53945 6402{
a43f6e0f 6403 struct drm_device *dev = crtc->base.dev;
8bd31e67 6404 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 6405 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
d03c93d4 6406 int ret;
89749350 6407
ad3a4479 6408 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6409 if (INTEL_INFO(dev)->gen < 4) {
cf532bb2
VS
6410 int clock_limit =
6411 dev_priv->display.get_display_clock_speed(dev);
6412
6413 /*
6414 * Enable pixel doubling when the dot clock
6415 * is > 90% of the (display) core speed.
6416 *
b397c96b
VS
6417 * GDG double wide on either pipe,
6418 * otherwise pipe A only.
cf532bb2 6419 */
b397c96b 6420 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 6421 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 6422 clock_limit *= 2;
cf532bb2 6423 pipe_config->double_wide = true;
ad3a4479
VS
6424 }
6425
241bfc38 6426 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 6427 return -EINVAL;
2c07245f 6428 }
89749350 6429
1d1d0e27
VS
6430 /*
6431 * Pipe horizontal size must be even in:
6432 * - DVO ganged mode
6433 * - LVDS dual channel mode
6434 * - Double wide pipe
6435 */
a93e255f 6436 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6437 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6438 pipe_config->pipe_src_w &= ~1;
6439
8693a824
DL
6440 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6441 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6442 */
6443 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6444 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 6445 return -EINVAL;
44f46b42 6446
f5adf94e 6447 if (HAS_IPS(dev))
a43f6e0f
DV
6448 hsw_compute_ips_config(crtc, pipe_config);
6449
877d48d5 6450 if (pipe_config->has_pch_encoder)
a43f6e0f 6451 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6452
d03c93d4
CK
6453 /* FIXME: remove below call once atomic mode set is place and all crtc
6454 * related checks called from atomic_crtc_check function */
6455 ret = 0;
6456 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6457 crtc, pipe_config->base.state);
6458 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6459
6460 return ret;
79e53945
JB
6461}
6462
1652d19e
VS
6463static int skylake_get_display_clock_speed(struct drm_device *dev)
6464{
6465 struct drm_i915_private *dev_priv = to_i915(dev);
6466 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6467 uint32_t cdctl = I915_READ(CDCLK_CTL);
6468 uint32_t linkrate;
6469
6470 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6471 WARN(1, "LCPLL1 not enabled\n");
6472 return 24000; /* 24MHz is the cd freq with NSSC ref */
6473 }
6474
6475 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6476 return 540000;
6477
6478 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6479 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6480
71cd8423
DL
6481 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6482 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6483 /* vco 8640 */
6484 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6485 case CDCLK_FREQ_450_432:
6486 return 432000;
6487 case CDCLK_FREQ_337_308:
6488 return 308570;
6489 case CDCLK_FREQ_675_617:
6490 return 617140;
6491 default:
6492 WARN(1, "Unknown cd freq selection\n");
6493 }
6494 } else {
6495 /* vco 8100 */
6496 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6497 case CDCLK_FREQ_450_432:
6498 return 450000;
6499 case CDCLK_FREQ_337_308:
6500 return 337500;
6501 case CDCLK_FREQ_675_617:
6502 return 675000;
6503 default:
6504 WARN(1, "Unknown cd freq selection\n");
6505 }
6506 }
6507
6508 /* error case, do as if DPLL0 isn't enabled */
6509 return 24000;
6510}
6511
6512static int broadwell_get_display_clock_speed(struct drm_device *dev)
6513{
6514 struct drm_i915_private *dev_priv = dev->dev_private;
6515 uint32_t lcpll = I915_READ(LCPLL_CTL);
6516 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6517
6518 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6519 return 800000;
6520 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6521 return 450000;
6522 else if (freq == LCPLL_CLK_FREQ_450)
6523 return 450000;
6524 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6525 return 540000;
6526 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6527 return 337500;
6528 else
6529 return 675000;
6530}
6531
6532static int haswell_get_display_clock_speed(struct drm_device *dev)
6533{
6534 struct drm_i915_private *dev_priv = dev->dev_private;
6535 uint32_t lcpll = I915_READ(LCPLL_CTL);
6536 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6537
6538 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6539 return 800000;
6540 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6541 return 450000;
6542 else if (freq == LCPLL_CLK_FREQ_450)
6543 return 450000;
6544 else if (IS_HSW_ULT(dev))
6545 return 337500;
6546 else
6547 return 540000;
79e53945
JB
6548}
6549
25eb05fc
JB
6550static int valleyview_get_display_clock_speed(struct drm_device *dev)
6551{
d197b7d3 6552 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
6553 u32 val;
6554 int divider;
6555
6bcda4f0
VS
6556 if (dev_priv->hpll_freq == 0)
6557 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6558
d197b7d3
VS
6559 mutex_lock(&dev_priv->dpio_lock);
6560 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6561 mutex_unlock(&dev_priv->dpio_lock);
6562
6563 divider = val & DISPLAY_FREQUENCY_VALUES;
6564
7d007f40
VS
6565 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6566 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6567 "cdclk change in progress\n");
6568
6bcda4f0 6569 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
6570}
6571
b37a6434
VS
6572static int ilk_get_display_clock_speed(struct drm_device *dev)
6573{
6574 return 450000;
6575}
6576
e70236a8
JB
6577static int i945_get_display_clock_speed(struct drm_device *dev)
6578{
6579 return 400000;
6580}
79e53945 6581
e70236a8 6582static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6583{
e907f170 6584 return 333333;
e70236a8 6585}
79e53945 6586
e70236a8
JB
6587static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6588{
6589 return 200000;
6590}
79e53945 6591
257a7ffc
DV
6592static int pnv_get_display_clock_speed(struct drm_device *dev)
6593{
6594 u16 gcfgc = 0;
6595
6596 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6597
6598 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6599 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6600 return 266667;
257a7ffc 6601 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6602 return 333333;
257a7ffc 6603 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6604 return 444444;
257a7ffc
DV
6605 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6606 return 200000;
6607 default:
6608 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6609 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6610 return 133333;
257a7ffc 6611 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6612 return 166667;
257a7ffc
DV
6613 }
6614}
6615
e70236a8
JB
6616static int i915gm_get_display_clock_speed(struct drm_device *dev)
6617{
6618 u16 gcfgc = 0;
79e53945 6619
e70236a8
JB
6620 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6621
6622 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6623 return 133333;
e70236a8
JB
6624 else {
6625 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6626 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6627 return 333333;
e70236a8
JB
6628 default:
6629 case GC_DISPLAY_CLOCK_190_200_MHZ:
6630 return 190000;
79e53945 6631 }
e70236a8
JB
6632 }
6633}
6634
6635static int i865_get_display_clock_speed(struct drm_device *dev)
6636{
e907f170 6637 return 266667;
e70236a8
JB
6638}
6639
6640static int i855_get_display_clock_speed(struct drm_device *dev)
6641{
6642 u16 hpllcc = 0;
6643 /* Assume that the hardware is in the high speed state. This
6644 * should be the default.
6645 */
6646 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6647 case GC_CLOCK_133_200:
6648 case GC_CLOCK_100_200:
6649 return 200000;
6650 case GC_CLOCK_166_250:
6651 return 250000;
6652 case GC_CLOCK_100_133:
e907f170 6653 return 133333;
e70236a8 6654 }
79e53945 6655
e70236a8
JB
6656 /* Shouldn't happen */
6657 return 0;
6658}
79e53945 6659
e70236a8
JB
6660static int i830_get_display_clock_speed(struct drm_device *dev)
6661{
e907f170 6662 return 133333;
79e53945
JB
6663}
6664
2c07245f 6665static void
a65851af 6666intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6667{
a65851af
VS
6668 while (*num > DATA_LINK_M_N_MASK ||
6669 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6670 *num >>= 1;
6671 *den >>= 1;
6672 }
6673}
6674
a65851af
VS
6675static void compute_m_n(unsigned int m, unsigned int n,
6676 uint32_t *ret_m, uint32_t *ret_n)
6677{
6678 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6679 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6680 intel_reduce_m_n_ratio(ret_m, ret_n);
6681}
6682
e69d0bc1
DV
6683void
6684intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6685 int pixel_clock, int link_clock,
6686 struct intel_link_m_n *m_n)
2c07245f 6687{
e69d0bc1 6688 m_n->tu = 64;
a65851af
VS
6689
6690 compute_m_n(bits_per_pixel * pixel_clock,
6691 link_clock * nlanes * 8,
6692 &m_n->gmch_m, &m_n->gmch_n);
6693
6694 compute_m_n(pixel_clock, link_clock,
6695 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
6696}
6697
a7615030
CW
6698static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6699{
d330a953
JN
6700 if (i915.panel_use_ssc >= 0)
6701 return i915.panel_use_ssc != 0;
41aa3448 6702 return dev_priv->vbt.lvds_use_ssc
435793df 6703 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6704}
6705
a93e255f
ACO
6706static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6707 int num_connectors)
c65d77d8 6708{
a93e255f 6709 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
6710 struct drm_i915_private *dev_priv = dev->dev_private;
6711 int refclk;
6712
a93e255f
ACO
6713 WARN_ON(!crtc_state->base.state);
6714
5ab7b0b7 6715 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 6716 refclk = 100000;
a93e255f 6717 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 6718 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
6719 refclk = dev_priv->vbt.lvds_ssc_freq;
6720 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
6721 } else if (!IS_GEN2(dev)) {
6722 refclk = 96000;
6723 } else {
6724 refclk = 48000;
6725 }
6726
6727 return refclk;
6728}
6729
7429e9d4 6730static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6731{
7df00d7a 6732 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6733}
f47709a9 6734
7429e9d4
DV
6735static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6736{
6737 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6738}
6739
f47709a9 6740static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6741 struct intel_crtc_state *crtc_state,
a7516a05
JB
6742 intel_clock_t *reduced_clock)
6743{
f47709a9 6744 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
6745 u32 fp, fp2 = 0;
6746
6747 if (IS_PINEVIEW(dev)) {
190f68c5 6748 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6749 if (reduced_clock)
7429e9d4 6750 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6751 } else {
190f68c5 6752 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6753 if (reduced_clock)
7429e9d4 6754 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6755 }
6756
190f68c5 6757 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6758
f47709a9 6759 crtc->lowfreq_avail = false;
a93e255f 6760 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6761 reduced_clock) {
190f68c5 6762 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 6763 crtc->lowfreq_avail = true;
a7516a05 6764 } else {
190f68c5 6765 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6766 }
6767}
6768
5e69f97f
CML
6769static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6770 pipe)
89b667f8
JB
6771{
6772 u32 reg_val;
6773
6774 /*
6775 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6776 * and set it to a reasonable value instead.
6777 */
ab3c759a 6778 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6779 reg_val &= 0xffffff00;
6780 reg_val |= 0x00000030;
ab3c759a 6781 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6782
ab3c759a 6783 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6784 reg_val &= 0x8cffffff;
6785 reg_val = 0x8c000000;
ab3c759a 6786 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6787
ab3c759a 6788 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6789 reg_val &= 0xffffff00;
ab3c759a 6790 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6791
ab3c759a 6792 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6793 reg_val &= 0x00ffffff;
6794 reg_val |= 0xb0000000;
ab3c759a 6795 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6796}
6797
b551842d
DV
6798static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6799 struct intel_link_m_n *m_n)
6800{
6801 struct drm_device *dev = crtc->base.dev;
6802 struct drm_i915_private *dev_priv = dev->dev_private;
6803 int pipe = crtc->pipe;
6804
e3b95f1e
DV
6805 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6806 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6807 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6808 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6809}
6810
6811static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6812 struct intel_link_m_n *m_n,
6813 struct intel_link_m_n *m2_n2)
b551842d
DV
6814{
6815 struct drm_device *dev = crtc->base.dev;
6816 struct drm_i915_private *dev_priv = dev->dev_private;
6817 int pipe = crtc->pipe;
6e3c9717 6818 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
6819
6820 if (INTEL_INFO(dev)->gen >= 5) {
6821 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6822 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6823 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6824 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6825 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6826 * for gen < 8) and if DRRS is supported (to make sure the
6827 * registers are not unnecessarily accessed).
6828 */
44395bfe 6829 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 6830 crtc->config->has_drrs) {
f769cd24
VK
6831 I915_WRITE(PIPE_DATA_M2(transcoder),
6832 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6833 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6834 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6835 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6836 }
b551842d 6837 } else {
e3b95f1e
DV
6838 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6839 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6840 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6841 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6842 }
6843}
6844
fe3cd48d 6845void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6846{
fe3cd48d
R
6847 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6848
6849 if (m_n == M1_N1) {
6850 dp_m_n = &crtc->config->dp_m_n;
6851 dp_m2_n2 = &crtc->config->dp_m2_n2;
6852 } else if (m_n == M2_N2) {
6853
6854 /*
6855 * M2_N2 registers are not supported. Hence m2_n2 divider value
6856 * needs to be programmed into M1_N1.
6857 */
6858 dp_m_n = &crtc->config->dp_m2_n2;
6859 } else {
6860 DRM_ERROR("Unsupported divider value\n");
6861 return;
6862 }
6863
6e3c9717
ACO
6864 if (crtc->config->has_pch_encoder)
6865 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6866 else
fe3cd48d 6867 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6868}
6869
d288f65f 6870static void vlv_update_pll(struct intel_crtc *crtc,
5cec258b 6871 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
6872{
6873 u32 dpll, dpll_md;
6874
6875 /*
6876 * Enable DPIO clock input. We should never disable the reference
6877 * clock for pipe B, since VGA hotplug / manual detection depends
6878 * on it.
6879 */
6880 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6881 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6882 /* We should never disable this, set it here for state tracking */
6883 if (crtc->pipe == PIPE_B)
6884 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6885 dpll |= DPLL_VCO_ENABLE;
d288f65f 6886 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 6887
d288f65f 6888 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 6889 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 6890 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
6891}
6892
d288f65f 6893static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6894 const struct intel_crtc_state *pipe_config)
a0c4da24 6895{
f47709a9 6896 struct drm_device *dev = crtc->base.dev;
a0c4da24 6897 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 6898 int pipe = crtc->pipe;
bdd4b6a6 6899 u32 mdiv;
a0c4da24 6900 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6901 u32 coreclk, reg_val;
a0c4da24 6902
09153000
DV
6903 mutex_lock(&dev_priv->dpio_lock);
6904
d288f65f
VS
6905 bestn = pipe_config->dpll.n;
6906 bestm1 = pipe_config->dpll.m1;
6907 bestm2 = pipe_config->dpll.m2;
6908 bestp1 = pipe_config->dpll.p1;
6909 bestp2 = pipe_config->dpll.p2;
a0c4da24 6910
89b667f8
JB
6911 /* See eDP HDMI DPIO driver vbios notes doc */
6912
6913 /* PLL B needs special handling */
bdd4b6a6 6914 if (pipe == PIPE_B)
5e69f97f 6915 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6916
6917 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6918 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6919
6920 /* Disable target IRef on PLL */
ab3c759a 6921 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6922 reg_val &= 0x00ffffff;
ab3c759a 6923 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6924
6925 /* Disable fast lock */
ab3c759a 6926 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6927
6928 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6929 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6930 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6931 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6932 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6933
6934 /*
6935 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6936 * but we don't support that).
6937 * Note: don't use the DAC post divider as it seems unstable.
6938 */
6939 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6940 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6941
a0c4da24 6942 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6943 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6944
89b667f8 6945 /* Set HBR and RBR LPF coefficients */
d288f65f 6946 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
6947 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6948 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 6949 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6950 0x009f0003);
89b667f8 6951 else
ab3c759a 6952 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6953 0x00d0000f);
6954
681a8504 6955 if (pipe_config->has_dp_encoder) {
89b667f8 6956 /* Use SSC source */
bdd4b6a6 6957 if (pipe == PIPE_A)
ab3c759a 6958 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6959 0x0df40000);
6960 else
ab3c759a 6961 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6962 0x0df70000);
6963 } else { /* HDMI or VGA */
6964 /* Use bend source */
bdd4b6a6 6965 if (pipe == PIPE_A)
ab3c759a 6966 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6967 0x0df70000);
6968 else
ab3c759a 6969 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6970 0x0df40000);
6971 }
a0c4da24 6972
ab3c759a 6973 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6974 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
6975 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6976 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 6977 coreclk |= 0x01000000;
ab3c759a 6978 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6979
ab3c759a 6980 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 6981 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
6982}
6983
d288f65f 6984static void chv_update_pll(struct intel_crtc *crtc,
5cec258b 6985 struct intel_crtc_state *pipe_config)
1ae0d137 6986{
d288f65f 6987 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
6988 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6989 DPLL_VCO_ENABLE;
6990 if (crtc->pipe != PIPE_A)
d288f65f 6991 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 6992
d288f65f
VS
6993 pipe_config->dpll_hw_state.dpll_md =
6994 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
6995}
6996
d288f65f 6997static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6998 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6999{
7000 struct drm_device *dev = crtc->base.dev;
7001 struct drm_i915_private *dev_priv = dev->dev_private;
7002 int pipe = crtc->pipe;
7003 int dpll_reg = DPLL(crtc->pipe);
7004 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 7005 u32 loopfilter, tribuf_calcntr;
9d556c99 7006 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 7007 u32 dpio_val;
9cbe40c1 7008 int vco;
9d556c99 7009
d288f65f
VS
7010 bestn = pipe_config->dpll.n;
7011 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7012 bestm1 = pipe_config->dpll.m1;
7013 bestm2 = pipe_config->dpll.m2 >> 22;
7014 bestp1 = pipe_config->dpll.p1;
7015 bestp2 = pipe_config->dpll.p2;
9cbe40c1 7016 vco = pipe_config->dpll.vco;
a945ce7e 7017 dpio_val = 0;
9cbe40c1 7018 loopfilter = 0;
9d556c99
CML
7019
7020 /*
7021 * Enable Refclk and SSC
7022 */
a11b0703 7023 I915_WRITE(dpll_reg,
d288f65f 7024 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703
VS
7025
7026 mutex_lock(&dev_priv->dpio_lock);
9d556c99 7027
9d556c99
CML
7028 /* p1 and p2 divider */
7029 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7030 5 << DPIO_CHV_S1_DIV_SHIFT |
7031 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7032 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7033 1 << DPIO_CHV_K_DIV_SHIFT);
7034
7035 /* Feedback post-divider - m2 */
7036 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7037
7038 /* Feedback refclk divider - n and m1 */
7039 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7040 DPIO_CHV_M1_DIV_BY_2 |
7041 1 << DPIO_CHV_N_DIV_SHIFT);
7042
7043 /* M2 fraction division */
a945ce7e
VP
7044 if (bestm2_frac)
7045 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7046
7047 /* M2 fraction division enable */
a945ce7e
VP
7048 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7049 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7050 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7051 if (bestm2_frac)
7052 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7053 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7054
de3a0fde
VP
7055 /* Program digital lock detect threshold */
7056 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7057 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7058 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7059 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7060 if (!bestm2_frac)
7061 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7062 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7063
9d556c99 7064 /* Loop filter */
9cbe40c1
VP
7065 if (vco == 5400000) {
7066 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7067 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7068 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7069 tribuf_calcntr = 0x9;
7070 } else if (vco <= 6200000) {
7071 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7072 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7073 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7074 tribuf_calcntr = 0x9;
7075 } else if (vco <= 6480000) {
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 = 0x8;
7080 } else {
7081 /* Not supported. Apply the same limits as in the max case */
7082 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7083 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7084 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7085 tribuf_calcntr = 0;
7086 }
9d556c99
CML
7087 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7088
968040b2 7089 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7090 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7091 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7092 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7093
9d556c99
CML
7094 /* AFC Recal */
7095 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7096 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7097 DPIO_AFC_RECAL);
7098
7099 mutex_unlock(&dev_priv->dpio_lock);
7100}
7101
d288f65f
VS
7102/**
7103 * vlv_force_pll_on - forcibly enable just the PLL
7104 * @dev_priv: i915 private structure
7105 * @pipe: pipe PLL to enable
7106 * @dpll: PLL configuration
7107 *
7108 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7109 * in cases where we need the PLL enabled even when @pipe is not going to
7110 * be enabled.
7111 */
7112void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7113 const struct dpll *dpll)
7114{
7115 struct intel_crtc *crtc =
7116 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 7117 struct intel_crtc_state pipe_config = {
a93e255f 7118 .base.crtc = &crtc->base,
d288f65f
VS
7119 .pixel_multiplier = 1,
7120 .dpll = *dpll,
7121 };
7122
7123 if (IS_CHERRYVIEW(dev)) {
7124 chv_update_pll(crtc, &pipe_config);
7125 chv_prepare_pll(crtc, &pipe_config);
7126 chv_enable_pll(crtc, &pipe_config);
7127 } else {
7128 vlv_update_pll(crtc, &pipe_config);
7129 vlv_prepare_pll(crtc, &pipe_config);
7130 vlv_enable_pll(crtc, &pipe_config);
7131 }
7132}
7133
7134/**
7135 * vlv_force_pll_off - forcibly disable just the PLL
7136 * @dev_priv: i915 private structure
7137 * @pipe: pipe PLL to disable
7138 *
7139 * Disable the PLL for @pipe. To be used in cases where we need
7140 * the PLL enabled even when @pipe is not going to be enabled.
7141 */
7142void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7143{
7144 if (IS_CHERRYVIEW(dev))
7145 chv_disable_pll(to_i915(dev), pipe);
7146 else
7147 vlv_disable_pll(to_i915(dev), pipe);
7148}
7149
f47709a9 7150static void i9xx_update_pll(struct intel_crtc *crtc,
190f68c5 7151 struct intel_crtc_state *crtc_state,
f47709a9 7152 intel_clock_t *reduced_clock,
eb1cbe48
DV
7153 int num_connectors)
7154{
f47709a9 7155 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7156 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7157 u32 dpll;
7158 bool is_sdvo;
190f68c5 7159 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7160
190f68c5 7161 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7162
a93e255f
ACO
7163 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7164 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7165
7166 dpll = DPLL_VGA_MODE_DIS;
7167
a93e255f 7168 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7169 dpll |= DPLLB_MODE_LVDS;
7170 else
7171 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7172
ef1b460d 7173 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7174 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7175 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7176 }
198a037f
DV
7177
7178 if (is_sdvo)
4a33e48d 7179 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7180
190f68c5 7181 if (crtc_state->has_dp_encoder)
4a33e48d 7182 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7183
7184 /* compute bitmask from p1 value */
7185 if (IS_PINEVIEW(dev))
7186 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7187 else {
7188 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7189 if (IS_G4X(dev) && reduced_clock)
7190 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7191 }
7192 switch (clock->p2) {
7193 case 5:
7194 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7195 break;
7196 case 7:
7197 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7198 break;
7199 case 10:
7200 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7201 break;
7202 case 14:
7203 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7204 break;
7205 }
7206 if (INTEL_INFO(dev)->gen >= 4)
7207 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7208
190f68c5 7209 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7210 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7211 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7212 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7213 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7214 else
7215 dpll |= PLL_REF_INPUT_DREFCLK;
7216
7217 dpll |= DPLL_VCO_ENABLE;
190f68c5 7218 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7219
eb1cbe48 7220 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7221 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7222 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7223 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7224 }
7225}
7226
f47709a9 7227static void i8xx_update_pll(struct intel_crtc *crtc,
190f68c5 7228 struct intel_crtc_state *crtc_state,
f47709a9 7229 intel_clock_t *reduced_clock,
eb1cbe48
DV
7230 int num_connectors)
7231{
f47709a9 7232 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7233 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7234 u32 dpll;
190f68c5 7235 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7236
190f68c5 7237 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7238
eb1cbe48
DV
7239 dpll = DPLL_VGA_MODE_DIS;
7240
a93e255f 7241 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7242 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7243 } else {
7244 if (clock->p1 == 2)
7245 dpll |= PLL_P1_DIVIDE_BY_TWO;
7246 else
7247 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7248 if (clock->p2 == 4)
7249 dpll |= PLL_P2_DIVIDE_BY_4;
7250 }
7251
a93e255f 7252 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7253 dpll |= DPLL_DVO_2X_MODE;
7254
a93e255f 7255 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7256 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7257 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7258 else
7259 dpll |= PLL_REF_INPUT_DREFCLK;
7260
7261 dpll |= DPLL_VCO_ENABLE;
190f68c5 7262 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7263}
7264
8a654f3b 7265static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7266{
7267 struct drm_device *dev = intel_crtc->base.dev;
7268 struct drm_i915_private *dev_priv = dev->dev_private;
7269 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7270 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 7271 struct drm_display_mode *adjusted_mode =
6e3c9717 7272 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7273 uint32_t crtc_vtotal, crtc_vblank_end;
7274 int vsyncshift = 0;
4d8a62ea
DV
7275
7276 /* We need to be careful not to changed the adjusted mode, for otherwise
7277 * the hw state checker will get angry at the mismatch. */
7278 crtc_vtotal = adjusted_mode->crtc_vtotal;
7279 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7280
609aeaca 7281 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7282 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7283 crtc_vtotal -= 1;
7284 crtc_vblank_end -= 1;
609aeaca 7285
409ee761 7286 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7287 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7288 else
7289 vsyncshift = adjusted_mode->crtc_hsync_start -
7290 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7291 if (vsyncshift < 0)
7292 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7293 }
7294
7295 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7296 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7297
fe2b8f9d 7298 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7299 (adjusted_mode->crtc_hdisplay - 1) |
7300 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7301 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7302 (adjusted_mode->crtc_hblank_start - 1) |
7303 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7304 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7305 (adjusted_mode->crtc_hsync_start - 1) |
7306 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7307
fe2b8f9d 7308 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7309 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7310 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7311 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7312 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7313 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7314 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7315 (adjusted_mode->crtc_vsync_start - 1) |
7316 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7317
b5e508d4
PZ
7318 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7319 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7320 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7321 * bits. */
7322 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7323 (pipe == PIPE_B || pipe == PIPE_C))
7324 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7325
b0e77b9c
PZ
7326 /* pipesrc controls the size that is scaled from, which should
7327 * always be the user's requested size.
7328 */
7329 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7330 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7331 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7332}
7333
1bd1bd80 7334static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7335 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7336{
7337 struct drm_device *dev = crtc->base.dev;
7338 struct drm_i915_private *dev_priv = dev->dev_private;
7339 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7340 uint32_t tmp;
7341
7342 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7343 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7344 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7345 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7346 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7347 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7348 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7349 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7350 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7351
7352 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7353 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7354 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7355 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7356 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7357 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7358 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7359 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7360 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7361
7362 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7363 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7364 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7365 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7366 }
7367
7368 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7369 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7370 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7371
2d112de7
ACO
7372 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7373 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7374}
7375
f6a83288 7376void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7377 struct intel_crtc_state *pipe_config)
babea61d 7378{
2d112de7
ACO
7379 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7380 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7381 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7382 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7383
2d112de7
ACO
7384 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7385 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7386 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7387 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7388
2d112de7 7389 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 7390
2d112de7
ACO
7391 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7392 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
7393}
7394
84b046f3
DV
7395static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7396{
7397 struct drm_device *dev = intel_crtc->base.dev;
7398 struct drm_i915_private *dev_priv = dev->dev_private;
7399 uint32_t pipeconf;
7400
9f11a9e4 7401 pipeconf = 0;
84b046f3 7402
b6b5d049
VS
7403 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7404 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7405 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7406
6e3c9717 7407 if (intel_crtc->config->double_wide)
cf532bb2 7408 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7409
ff9ce46e
DV
7410 /* only g4x and later have fancy bpc/dither controls */
7411 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 7412 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7413 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7414 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7415 PIPECONF_DITHER_TYPE_SP;
84b046f3 7416
6e3c9717 7417 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7418 case 18:
7419 pipeconf |= PIPECONF_6BPC;
7420 break;
7421 case 24:
7422 pipeconf |= PIPECONF_8BPC;
7423 break;
7424 case 30:
7425 pipeconf |= PIPECONF_10BPC;
7426 break;
7427 default:
7428 /* Case prevented by intel_choose_pipe_bpp_dither. */
7429 BUG();
84b046f3
DV
7430 }
7431 }
7432
7433 if (HAS_PIPE_CXSR(dev)) {
7434 if (intel_crtc->lowfreq_avail) {
7435 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7436 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7437 } else {
7438 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7439 }
7440 }
7441
6e3c9717 7442 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7443 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7444 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7445 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7446 else
7447 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7448 } else
84b046f3
DV
7449 pipeconf |= PIPECONF_PROGRESSIVE;
7450
6e3c9717 7451 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 7452 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7453
84b046f3
DV
7454 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7455 POSTING_READ(PIPECONF(intel_crtc->pipe));
7456}
7457
190f68c5
ACO
7458static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7459 struct intel_crtc_state *crtc_state)
79e53945 7460{
c7653199 7461 struct drm_device *dev = crtc->base.dev;
79e53945 7462 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7463 int refclk, num_connectors = 0;
652c393a 7464 intel_clock_t clock, reduced_clock;
a16af721 7465 bool ok, has_reduced_clock = false;
e9fd1c02 7466 bool is_lvds = false, is_dsi = false;
5eddb70b 7467 struct intel_encoder *encoder;
d4906093 7468 const intel_limit_t *limit;
55bb9992 7469 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 7470 struct drm_connector *connector;
55bb9992
ACO
7471 struct drm_connector_state *connector_state;
7472 int i;
79e53945 7473
da3ced29 7474 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
7475 if (connector_state->crtc != &crtc->base)
7476 continue;
7477
7478 encoder = to_intel_encoder(connector_state->best_encoder);
7479
5eddb70b 7480 switch (encoder->type) {
79e53945
JB
7481 case INTEL_OUTPUT_LVDS:
7482 is_lvds = true;
7483 break;
e9fd1c02
JN
7484 case INTEL_OUTPUT_DSI:
7485 is_dsi = true;
7486 break;
6847d71b
PZ
7487 default:
7488 break;
79e53945 7489 }
43565a06 7490
c751ce4f 7491 num_connectors++;
79e53945
JB
7492 }
7493
f2335330 7494 if (is_dsi)
5b18e57c 7495 return 0;
f2335330 7496
190f68c5 7497 if (!crtc_state->clock_set) {
a93e255f 7498 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7499
e9fd1c02
JN
7500 /*
7501 * Returns a set of divisors for the desired target clock with
7502 * the given refclk, or FALSE. The returned values represent
7503 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7504 * 2) / p1 / p2.
7505 */
a93e255f
ACO
7506 limit = intel_limit(crtc_state, refclk);
7507 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7508 crtc_state->port_clock,
e9fd1c02 7509 refclk, NULL, &clock);
f2335330 7510 if (!ok) {
e9fd1c02
JN
7511 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7512 return -EINVAL;
7513 }
79e53945 7514
f2335330
JN
7515 if (is_lvds && dev_priv->lvds_downclock_avail) {
7516 /*
7517 * Ensure we match the reduced clock's P to the target
7518 * clock. If the clocks don't match, we can't switch
7519 * the display clock by using the FP0/FP1. In such case
7520 * we will disable the LVDS downclock feature.
7521 */
7522 has_reduced_clock =
a93e255f 7523 dev_priv->display.find_dpll(limit, crtc_state,
f2335330
JN
7524 dev_priv->lvds_downclock,
7525 refclk, &clock,
7526 &reduced_clock);
7527 }
7528 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7529 crtc_state->dpll.n = clock.n;
7530 crtc_state->dpll.m1 = clock.m1;
7531 crtc_state->dpll.m2 = clock.m2;
7532 crtc_state->dpll.p1 = clock.p1;
7533 crtc_state->dpll.p2 = clock.p2;
f47709a9 7534 }
7026d4ac 7535
e9fd1c02 7536 if (IS_GEN2(dev)) {
190f68c5 7537 i8xx_update_pll(crtc, crtc_state,
2a8f64ca
VP
7538 has_reduced_clock ? &reduced_clock : NULL,
7539 num_connectors);
9d556c99 7540 } else if (IS_CHERRYVIEW(dev)) {
190f68c5 7541 chv_update_pll(crtc, crtc_state);
e9fd1c02 7542 } else if (IS_VALLEYVIEW(dev)) {
190f68c5 7543 vlv_update_pll(crtc, crtc_state);
e9fd1c02 7544 } else {
190f68c5 7545 i9xx_update_pll(crtc, crtc_state,
eb1cbe48 7546 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 7547 num_connectors);
e9fd1c02 7548 }
79e53945 7549
c8f7a0db 7550 return 0;
f564048e
EA
7551}
7552
2fa2fe9a 7553static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7554 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7555{
7556 struct drm_device *dev = crtc->base.dev;
7557 struct drm_i915_private *dev_priv = dev->dev_private;
7558 uint32_t tmp;
7559
dc9e7dec
VS
7560 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7561 return;
7562
2fa2fe9a 7563 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7564 if (!(tmp & PFIT_ENABLE))
7565 return;
2fa2fe9a 7566
06922821 7567 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7568 if (INTEL_INFO(dev)->gen < 4) {
7569 if (crtc->pipe != PIPE_B)
7570 return;
2fa2fe9a
DV
7571 } else {
7572 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7573 return;
7574 }
7575
06922821 7576 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7577 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7578 if (INTEL_INFO(dev)->gen < 5)
7579 pipe_config->gmch_pfit.lvds_border_bits =
7580 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7581}
7582
acbec814 7583static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7584 struct intel_crtc_state *pipe_config)
acbec814
JB
7585{
7586 struct drm_device *dev = crtc->base.dev;
7587 struct drm_i915_private *dev_priv = dev->dev_private;
7588 int pipe = pipe_config->cpu_transcoder;
7589 intel_clock_t clock;
7590 u32 mdiv;
662c6ecb 7591 int refclk = 100000;
acbec814 7592
f573de5a
SK
7593 /* In case of MIPI DPLL will not even be used */
7594 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7595 return;
7596
acbec814 7597 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 7598 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
7599 mutex_unlock(&dev_priv->dpio_lock);
7600
7601 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7602 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7603 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7604 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7605 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7606
f646628b 7607 vlv_clock(refclk, &clock);
acbec814 7608
f646628b
VS
7609 /* clock.dot is the fast clock */
7610 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
7611}
7612
5724dbd1
DL
7613static void
7614i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7615 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7616{
7617 struct drm_device *dev = crtc->base.dev;
7618 struct drm_i915_private *dev_priv = dev->dev_private;
7619 u32 val, base, offset;
7620 int pipe = crtc->pipe, plane = crtc->plane;
7621 int fourcc, pixel_format;
6761dd31 7622 unsigned int aligned_height;
b113d5ee 7623 struct drm_framebuffer *fb;
1b842c89 7624 struct intel_framebuffer *intel_fb;
1ad292b5 7625
42a7b088
DL
7626 val = I915_READ(DSPCNTR(plane));
7627 if (!(val & DISPLAY_PLANE_ENABLE))
7628 return;
7629
d9806c9f 7630 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7631 if (!intel_fb) {
1ad292b5
JB
7632 DRM_DEBUG_KMS("failed to alloc fb\n");
7633 return;
7634 }
7635
1b842c89
DL
7636 fb = &intel_fb->base;
7637
18c5247e
DV
7638 if (INTEL_INFO(dev)->gen >= 4) {
7639 if (val & DISPPLANE_TILED) {
49af449b 7640 plane_config->tiling = I915_TILING_X;
18c5247e
DV
7641 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7642 }
7643 }
1ad292b5
JB
7644
7645 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7646 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
7647 fb->pixel_format = fourcc;
7648 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
7649
7650 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 7651 if (plane_config->tiling)
1ad292b5
JB
7652 offset = I915_READ(DSPTILEOFF(plane));
7653 else
7654 offset = I915_READ(DSPLINOFF(plane));
7655 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7656 } else {
7657 base = I915_READ(DSPADDR(plane));
7658 }
7659 plane_config->base = base;
7660
7661 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7662 fb->width = ((val >> 16) & 0xfff) + 1;
7663 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7664
7665 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7666 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7667
b113d5ee 7668 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
7669 fb->pixel_format,
7670 fb->modifier[0]);
1ad292b5 7671
f37b5c2b 7672 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7673
2844a921
DL
7674 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7675 pipe_name(pipe), plane, fb->width, fb->height,
7676 fb->bits_per_pixel, base, fb->pitches[0],
7677 plane_config->size);
1ad292b5 7678
2d14030b 7679 plane_config->fb = intel_fb;
1ad292b5
JB
7680}
7681
70b23a98 7682static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7683 struct intel_crtc_state *pipe_config)
70b23a98
VS
7684{
7685 struct drm_device *dev = crtc->base.dev;
7686 struct drm_i915_private *dev_priv = dev->dev_private;
7687 int pipe = pipe_config->cpu_transcoder;
7688 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7689 intel_clock_t clock;
7690 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7691 int refclk = 100000;
7692
7693 mutex_lock(&dev_priv->dpio_lock);
7694 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7695 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7696 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7697 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7698 mutex_unlock(&dev_priv->dpio_lock);
7699
7700 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7701 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7702 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7703 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7704 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7705
7706 chv_clock(refclk, &clock);
7707
7708 /* clock.dot is the fast clock */
7709 pipe_config->port_clock = clock.dot / 5;
7710}
7711
0e8ffe1b 7712static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7713 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
7714{
7715 struct drm_device *dev = crtc->base.dev;
7716 struct drm_i915_private *dev_priv = dev->dev_private;
7717 uint32_t tmp;
7718
f458ebbc
DV
7719 if (!intel_display_power_is_enabled(dev_priv,
7720 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
7721 return false;
7722
e143a21c 7723 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7724 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7725
0e8ffe1b
DV
7726 tmp = I915_READ(PIPECONF(crtc->pipe));
7727 if (!(tmp & PIPECONF_ENABLE))
7728 return false;
7729
42571aef
VS
7730 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7731 switch (tmp & PIPECONF_BPC_MASK) {
7732 case PIPECONF_6BPC:
7733 pipe_config->pipe_bpp = 18;
7734 break;
7735 case PIPECONF_8BPC:
7736 pipe_config->pipe_bpp = 24;
7737 break;
7738 case PIPECONF_10BPC:
7739 pipe_config->pipe_bpp = 30;
7740 break;
7741 default:
7742 break;
7743 }
7744 }
7745
b5a9fa09
DV
7746 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7747 pipe_config->limited_color_range = true;
7748
282740f7
VS
7749 if (INTEL_INFO(dev)->gen < 4)
7750 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7751
1bd1bd80
DV
7752 intel_get_pipe_timings(crtc, pipe_config);
7753
2fa2fe9a
DV
7754 i9xx_get_pfit_config(crtc, pipe_config);
7755
6c49f241
DV
7756 if (INTEL_INFO(dev)->gen >= 4) {
7757 tmp = I915_READ(DPLL_MD(crtc->pipe));
7758 pipe_config->pixel_multiplier =
7759 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7760 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 7761 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
7762 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7763 tmp = I915_READ(DPLL(crtc->pipe));
7764 pipe_config->pixel_multiplier =
7765 ((tmp & SDVO_MULTIPLIER_MASK)
7766 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7767 } else {
7768 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7769 * port and will be fixed up in the encoder->get_config
7770 * function. */
7771 pipe_config->pixel_multiplier = 1;
7772 }
8bcc2795
DV
7773 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7774 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
7775 /*
7776 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7777 * on 830. Filter it out here so that we don't
7778 * report errors due to that.
7779 */
7780 if (IS_I830(dev))
7781 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7782
8bcc2795
DV
7783 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7784 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7785 } else {
7786 /* Mask out read-only status bits. */
7787 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7788 DPLL_PORTC_READY_MASK |
7789 DPLL_PORTB_READY_MASK);
8bcc2795 7790 }
6c49f241 7791
70b23a98
VS
7792 if (IS_CHERRYVIEW(dev))
7793 chv_crtc_clock_get(crtc, pipe_config);
7794 else if (IS_VALLEYVIEW(dev))
acbec814
JB
7795 vlv_crtc_clock_get(crtc, pipe_config);
7796 else
7797 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7798
0e8ffe1b
DV
7799 return true;
7800}
7801
dde86e2d 7802static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
7803{
7804 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 7805 struct intel_encoder *encoder;
74cfd7ac 7806 u32 val, final;
13d83a67 7807 bool has_lvds = false;
199e5d79 7808 bool has_cpu_edp = false;
199e5d79 7809 bool has_panel = false;
99eb6a01
KP
7810 bool has_ck505 = false;
7811 bool can_ssc = false;
13d83a67
JB
7812
7813 /* We need to take the global config into account */
b2784e15 7814 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
7815 switch (encoder->type) {
7816 case INTEL_OUTPUT_LVDS:
7817 has_panel = true;
7818 has_lvds = true;
7819 break;
7820 case INTEL_OUTPUT_EDP:
7821 has_panel = true;
2de6905f 7822 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7823 has_cpu_edp = true;
7824 break;
6847d71b
PZ
7825 default:
7826 break;
13d83a67
JB
7827 }
7828 }
7829
99eb6a01 7830 if (HAS_PCH_IBX(dev)) {
41aa3448 7831 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7832 can_ssc = has_ck505;
7833 } else {
7834 has_ck505 = false;
7835 can_ssc = true;
7836 }
7837
2de6905f
ID
7838 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7839 has_panel, has_lvds, has_ck505);
13d83a67
JB
7840
7841 /* Ironlake: try to setup display ref clock before DPLL
7842 * enabling. This is only under driver's control after
7843 * PCH B stepping, previous chipset stepping should be
7844 * ignoring this setting.
7845 */
74cfd7ac
CW
7846 val = I915_READ(PCH_DREF_CONTROL);
7847
7848 /* As we must carefully and slowly disable/enable each source in turn,
7849 * compute the final state we want first and check if we need to
7850 * make any changes at all.
7851 */
7852 final = val;
7853 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7854 if (has_ck505)
7855 final |= DREF_NONSPREAD_CK505_ENABLE;
7856 else
7857 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7858
7859 final &= ~DREF_SSC_SOURCE_MASK;
7860 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7861 final &= ~DREF_SSC1_ENABLE;
7862
7863 if (has_panel) {
7864 final |= DREF_SSC_SOURCE_ENABLE;
7865
7866 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7867 final |= DREF_SSC1_ENABLE;
7868
7869 if (has_cpu_edp) {
7870 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7871 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7872 else
7873 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7874 } else
7875 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7876 } else {
7877 final |= DREF_SSC_SOURCE_DISABLE;
7878 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7879 }
7880
7881 if (final == val)
7882 return;
7883
13d83a67 7884 /* Always enable nonspread source */
74cfd7ac 7885 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7886
99eb6a01 7887 if (has_ck505)
74cfd7ac 7888 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7889 else
74cfd7ac 7890 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7891
199e5d79 7892 if (has_panel) {
74cfd7ac
CW
7893 val &= ~DREF_SSC_SOURCE_MASK;
7894 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7895
199e5d79 7896 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7897 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7898 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7899 val |= DREF_SSC1_ENABLE;
e77166b5 7900 } else
74cfd7ac 7901 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7902
7903 /* Get SSC going before enabling the outputs */
74cfd7ac 7904 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7905 POSTING_READ(PCH_DREF_CONTROL);
7906 udelay(200);
7907
74cfd7ac 7908 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7909
7910 /* Enable CPU source on CPU attached eDP */
199e5d79 7911 if (has_cpu_edp) {
99eb6a01 7912 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7913 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7914 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7915 } else
74cfd7ac 7916 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7917 } else
74cfd7ac 7918 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7919
74cfd7ac 7920 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7921 POSTING_READ(PCH_DREF_CONTROL);
7922 udelay(200);
7923 } else {
7924 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7925
74cfd7ac 7926 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7927
7928 /* Turn off CPU output */
74cfd7ac 7929 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7930
74cfd7ac 7931 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7932 POSTING_READ(PCH_DREF_CONTROL);
7933 udelay(200);
7934
7935 /* Turn off the SSC source */
74cfd7ac
CW
7936 val &= ~DREF_SSC_SOURCE_MASK;
7937 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
7938
7939 /* Turn off SSC1 */
74cfd7ac 7940 val &= ~DREF_SSC1_ENABLE;
199e5d79 7941
74cfd7ac 7942 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
7943 POSTING_READ(PCH_DREF_CONTROL);
7944 udelay(200);
7945 }
74cfd7ac
CW
7946
7947 BUG_ON(val != final);
13d83a67
JB
7948}
7949
f31f2d55 7950static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7951{
f31f2d55 7952 uint32_t tmp;
dde86e2d 7953
0ff066a9
PZ
7954 tmp = I915_READ(SOUTH_CHICKEN2);
7955 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7956 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7957
0ff066a9
PZ
7958 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7959 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7960 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7961
0ff066a9
PZ
7962 tmp = I915_READ(SOUTH_CHICKEN2);
7963 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7964 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7965
0ff066a9
PZ
7966 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7967 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7968 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7969}
7970
7971/* WaMPhyProgramming:hsw */
7972static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7973{
7974 uint32_t tmp;
dde86e2d
PZ
7975
7976 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7977 tmp &= ~(0xFF << 24);
7978 tmp |= (0x12 << 24);
7979 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7980
dde86e2d
PZ
7981 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7982 tmp |= (1 << 11);
7983 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7984
7985 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7986 tmp |= (1 << 11);
7987 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7988
dde86e2d
PZ
7989 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7990 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7991 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7992
7993 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7994 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7995 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7996
0ff066a9
PZ
7997 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7998 tmp &= ~(7 << 13);
7999 tmp |= (5 << 13);
8000 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 8001
0ff066a9
PZ
8002 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8003 tmp &= ~(7 << 13);
8004 tmp |= (5 << 13);
8005 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
8006
8007 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8008 tmp &= ~0xFF;
8009 tmp |= 0x1C;
8010 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8011
8012 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8013 tmp &= ~0xFF;
8014 tmp |= 0x1C;
8015 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8016
8017 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8018 tmp &= ~(0xFF << 16);
8019 tmp |= (0x1C << 16);
8020 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8021
8022 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8023 tmp &= ~(0xFF << 16);
8024 tmp |= (0x1C << 16);
8025 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8026
0ff066a9
PZ
8027 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8028 tmp |= (1 << 27);
8029 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8030
0ff066a9
PZ
8031 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8032 tmp |= (1 << 27);
8033 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8034
0ff066a9
PZ
8035 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8036 tmp &= ~(0xF << 28);
8037 tmp |= (4 << 28);
8038 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8039
0ff066a9
PZ
8040 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8041 tmp &= ~(0xF << 28);
8042 tmp |= (4 << 28);
8043 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8044}
8045
2fa86a1f
PZ
8046/* Implements 3 different sequences from BSpec chapter "Display iCLK
8047 * Programming" based on the parameters passed:
8048 * - Sequence to enable CLKOUT_DP
8049 * - Sequence to enable CLKOUT_DP without spread
8050 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8051 */
8052static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8053 bool with_fdi)
f31f2d55
PZ
8054{
8055 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8056 uint32_t reg, tmp;
8057
8058 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8059 with_spread = true;
8060 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8061 with_fdi, "LP PCH doesn't have FDI\n"))
8062 with_fdi = false;
f31f2d55
PZ
8063
8064 mutex_lock(&dev_priv->dpio_lock);
8065
8066 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8067 tmp &= ~SBI_SSCCTL_DISABLE;
8068 tmp |= SBI_SSCCTL_PATHALT;
8069 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8070
8071 udelay(24);
8072
2fa86a1f
PZ
8073 if (with_spread) {
8074 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8075 tmp &= ~SBI_SSCCTL_PATHALT;
8076 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8077
2fa86a1f
PZ
8078 if (with_fdi) {
8079 lpt_reset_fdi_mphy(dev_priv);
8080 lpt_program_fdi_mphy(dev_priv);
8081 }
8082 }
dde86e2d 8083
2fa86a1f
PZ
8084 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8085 SBI_GEN0 : SBI_DBUFF0;
8086 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8087 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8088 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
8089
8090 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
8091}
8092
47701c3b
PZ
8093/* Sequence to disable CLKOUT_DP */
8094static void lpt_disable_clkout_dp(struct drm_device *dev)
8095{
8096 struct drm_i915_private *dev_priv = dev->dev_private;
8097 uint32_t reg, tmp;
8098
8099 mutex_lock(&dev_priv->dpio_lock);
8100
8101 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8102 SBI_GEN0 : SBI_DBUFF0;
8103 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8104 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8105 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8106
8107 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8108 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8109 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8110 tmp |= SBI_SSCCTL_PATHALT;
8111 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8112 udelay(32);
8113 }
8114 tmp |= SBI_SSCCTL_DISABLE;
8115 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8116 }
8117
8118 mutex_unlock(&dev_priv->dpio_lock);
8119}
8120
bf8fa3d3
PZ
8121static void lpt_init_pch_refclk(struct drm_device *dev)
8122{
bf8fa3d3
PZ
8123 struct intel_encoder *encoder;
8124 bool has_vga = false;
8125
b2784e15 8126 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8127 switch (encoder->type) {
8128 case INTEL_OUTPUT_ANALOG:
8129 has_vga = true;
8130 break;
6847d71b
PZ
8131 default:
8132 break;
bf8fa3d3
PZ
8133 }
8134 }
8135
47701c3b
PZ
8136 if (has_vga)
8137 lpt_enable_clkout_dp(dev, true, true);
8138 else
8139 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
8140}
8141
dde86e2d
PZ
8142/*
8143 * Initialize reference clocks when the driver loads
8144 */
8145void intel_init_pch_refclk(struct drm_device *dev)
8146{
8147 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8148 ironlake_init_pch_refclk(dev);
8149 else if (HAS_PCH_LPT(dev))
8150 lpt_init_pch_refclk(dev);
8151}
8152
55bb9992 8153static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8154{
55bb9992 8155 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8156 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8157 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8158 struct drm_connector *connector;
55bb9992 8159 struct drm_connector_state *connector_state;
d9d444cb 8160 struct intel_encoder *encoder;
55bb9992 8161 int num_connectors = 0, i;
d9d444cb
JB
8162 bool is_lvds = false;
8163
da3ced29 8164 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
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 8417 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8418 struct drm_connector *connector;
55bb9992
ACO
8419 struct drm_connector_state *connector_state;
8420 struct intel_encoder *encoder;
de13a2e3 8421 uint32_t dpll;
55bb9992 8422 int factor, num_connectors = 0, i;
09ede541 8423 bool is_lvds = false, is_sdvo = false;
79e53945 8424
da3ced29 8425 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8426 if (connector_state->crtc != crtc_state->base.crtc)
8427 continue;
8428
8429 encoder = to_intel_encoder(connector_state->best_encoder);
8430
8431 switch (encoder->type) {
79e53945
JB
8432 case INTEL_OUTPUT_LVDS:
8433 is_lvds = true;
8434 break;
8435 case INTEL_OUTPUT_SDVO:
7d57382e 8436 case INTEL_OUTPUT_HDMI:
79e53945 8437 is_sdvo = true;
79e53945 8438 break;
6847d71b
PZ
8439 default:
8440 break;
79e53945 8441 }
43565a06 8442
c751ce4f 8443 num_connectors++;
79e53945 8444 }
79e53945 8445
c1858123 8446 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8447 factor = 21;
8448 if (is_lvds) {
8449 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8450 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8451 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8452 factor = 25;
190f68c5 8453 } else if (crtc_state->sdvo_tv_clock)
8febb297 8454 factor = 20;
c1858123 8455
190f68c5 8456 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8457 *fp |= FP_CB_TUNE;
2c07245f 8458
9a7c7890
DV
8459 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8460 *fp2 |= FP_CB_TUNE;
8461
5eddb70b 8462 dpll = 0;
2c07245f 8463
a07d6787
EA
8464 if (is_lvds)
8465 dpll |= DPLLB_MODE_LVDS;
8466 else
8467 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8468
190f68c5 8469 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8470 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8471
8472 if (is_sdvo)
4a33e48d 8473 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8474 if (crtc_state->has_dp_encoder)
4a33e48d 8475 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8476
a07d6787 8477 /* compute bitmask from p1 value */
190f68c5 8478 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8479 /* also FPA1 */
190f68c5 8480 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8481
190f68c5 8482 switch (crtc_state->dpll.p2) {
a07d6787
EA
8483 case 5:
8484 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8485 break;
8486 case 7:
8487 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8488 break;
8489 case 10:
8490 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8491 break;
8492 case 14:
8493 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8494 break;
79e53945
JB
8495 }
8496
b4c09f3b 8497 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8498 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8499 else
8500 dpll |= PLL_REF_INPUT_DREFCLK;
8501
959e16d6 8502 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8503}
8504
190f68c5
ACO
8505static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8506 struct intel_crtc_state *crtc_state)
de13a2e3 8507{
c7653199 8508 struct drm_device *dev = crtc->base.dev;
de13a2e3 8509 intel_clock_t clock, reduced_clock;
cbbab5bd 8510 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8511 bool ok, has_reduced_clock = false;
8b47047b 8512 bool is_lvds = false;
e2b78267 8513 struct intel_shared_dpll *pll;
de13a2e3 8514
409ee761 8515 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 8516
5dc5298b
PZ
8517 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8518 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8519
190f68c5 8520 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8521 &has_reduced_clock, &reduced_clock);
190f68c5 8522 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8523 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8524 return -EINVAL;
79e53945 8525 }
f47709a9 8526 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8527 if (!crtc_state->clock_set) {
8528 crtc_state->dpll.n = clock.n;
8529 crtc_state->dpll.m1 = clock.m1;
8530 crtc_state->dpll.m2 = clock.m2;
8531 crtc_state->dpll.p1 = clock.p1;
8532 crtc_state->dpll.p2 = clock.p2;
f47709a9 8533 }
79e53945 8534
5dc5298b 8535 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8536 if (crtc_state->has_pch_encoder) {
8537 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8538 if (has_reduced_clock)
7429e9d4 8539 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8540
190f68c5 8541 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8542 &fp, &reduced_clock,
8543 has_reduced_clock ? &fp2 : NULL);
8544
190f68c5
ACO
8545 crtc_state->dpll_hw_state.dpll = dpll;
8546 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8547 if (has_reduced_clock)
190f68c5 8548 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8549 else
190f68c5 8550 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8551
190f68c5 8552 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 8553 if (pll == NULL) {
84f44ce7 8554 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8555 pipe_name(crtc->pipe));
4b645f14
JB
8556 return -EINVAL;
8557 }
3fb37703 8558 }
79e53945 8559
ab585dea 8560 if (is_lvds && has_reduced_clock)
c7653199 8561 crtc->lowfreq_avail = true;
bcd644e0 8562 else
c7653199 8563 crtc->lowfreq_avail = false;
e2b78267 8564
c8f7a0db 8565 return 0;
79e53945
JB
8566}
8567
eb14cb74
VS
8568static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8569 struct intel_link_m_n *m_n)
8570{
8571 struct drm_device *dev = crtc->base.dev;
8572 struct drm_i915_private *dev_priv = dev->dev_private;
8573 enum pipe pipe = crtc->pipe;
8574
8575 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8576 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8577 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8578 & ~TU_SIZE_MASK;
8579 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8580 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8581 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8582}
8583
8584static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8585 enum transcoder transcoder,
b95af8be
VK
8586 struct intel_link_m_n *m_n,
8587 struct intel_link_m_n *m2_n2)
72419203
DV
8588{
8589 struct drm_device *dev = crtc->base.dev;
8590 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8591 enum pipe pipe = crtc->pipe;
72419203 8592
eb14cb74
VS
8593 if (INTEL_INFO(dev)->gen >= 5) {
8594 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8595 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8596 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8597 & ~TU_SIZE_MASK;
8598 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8599 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8600 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8601 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8602 * gen < 8) and if DRRS is supported (to make sure the
8603 * registers are not unnecessarily read).
8604 */
8605 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8606 crtc->config->has_drrs) {
b95af8be
VK
8607 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8608 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8609 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8610 & ~TU_SIZE_MASK;
8611 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8612 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8613 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8614 }
eb14cb74
VS
8615 } else {
8616 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8617 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8618 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8619 & ~TU_SIZE_MASK;
8620 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8621 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8622 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8623 }
8624}
8625
8626void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8627 struct intel_crtc_state *pipe_config)
eb14cb74 8628{
681a8504 8629 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8630 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8631 else
8632 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8633 &pipe_config->dp_m_n,
8634 &pipe_config->dp_m2_n2);
eb14cb74 8635}
72419203 8636
eb14cb74 8637static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8638 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8639{
8640 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8641 &pipe_config->fdi_m_n, NULL);
72419203
DV
8642}
8643
bd2e244f 8644static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8645 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8646{
8647 struct drm_device *dev = crtc->base.dev;
8648 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
8649 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8650 uint32_t ps_ctrl = 0;
8651 int id = -1;
8652 int i;
bd2e244f 8653
a1b2278e
CK
8654 /* find scaler attached to this pipe */
8655 for (i = 0; i < crtc->num_scalers; i++) {
8656 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8657 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8658 id = i;
8659 pipe_config->pch_pfit.enabled = true;
8660 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8661 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8662 break;
8663 }
8664 }
bd2e244f 8665
a1b2278e
CK
8666 scaler_state->scaler_id = id;
8667 if (id >= 0) {
8668 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8669 } else {
8670 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8671 }
8672}
8673
5724dbd1
DL
8674static void
8675skylake_get_initial_plane_config(struct intel_crtc *crtc,
8676 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8677{
8678 struct drm_device *dev = crtc->base.dev;
8679 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 8680 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8681 int pipe = crtc->pipe;
8682 int fourcc, pixel_format;
6761dd31 8683 unsigned int aligned_height;
bc8d7dff 8684 struct drm_framebuffer *fb;
1b842c89 8685 struct intel_framebuffer *intel_fb;
bc8d7dff 8686
d9806c9f 8687 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8688 if (!intel_fb) {
bc8d7dff
DL
8689 DRM_DEBUG_KMS("failed to alloc fb\n");
8690 return;
8691 }
8692
1b842c89
DL
8693 fb = &intel_fb->base;
8694
bc8d7dff 8695 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8696 if (!(val & PLANE_CTL_ENABLE))
8697 goto error;
8698
bc8d7dff
DL
8699 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8700 fourcc = skl_format_to_fourcc(pixel_format,
8701 val & PLANE_CTL_ORDER_RGBX,
8702 val & PLANE_CTL_ALPHA_MASK);
8703 fb->pixel_format = fourcc;
8704 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8705
40f46283
DL
8706 tiling = val & PLANE_CTL_TILED_MASK;
8707 switch (tiling) {
8708 case PLANE_CTL_TILED_LINEAR:
8709 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8710 break;
8711 case PLANE_CTL_TILED_X:
8712 plane_config->tiling = I915_TILING_X;
8713 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8714 break;
8715 case PLANE_CTL_TILED_Y:
8716 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8717 break;
8718 case PLANE_CTL_TILED_YF:
8719 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8720 break;
8721 default:
8722 MISSING_CASE(tiling);
8723 goto error;
8724 }
8725
bc8d7dff
DL
8726 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8727 plane_config->base = base;
8728
8729 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8730
8731 val = I915_READ(PLANE_SIZE(pipe, 0));
8732 fb->height = ((val >> 16) & 0xfff) + 1;
8733 fb->width = ((val >> 0) & 0x1fff) + 1;
8734
8735 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
8736 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8737 fb->pixel_format);
bc8d7dff
DL
8738 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8739
8740 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8741 fb->pixel_format,
8742 fb->modifier[0]);
bc8d7dff 8743
f37b5c2b 8744 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
8745
8746 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8747 pipe_name(pipe), fb->width, fb->height,
8748 fb->bits_per_pixel, base, fb->pitches[0],
8749 plane_config->size);
8750
2d14030b 8751 plane_config->fb = intel_fb;
bc8d7dff
DL
8752 return;
8753
8754error:
8755 kfree(fb);
8756}
8757
2fa2fe9a 8758static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8759 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8760{
8761 struct drm_device *dev = crtc->base.dev;
8762 struct drm_i915_private *dev_priv = dev->dev_private;
8763 uint32_t tmp;
8764
8765 tmp = I915_READ(PF_CTL(crtc->pipe));
8766
8767 if (tmp & PF_ENABLE) {
fd4daa9c 8768 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
8769 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8770 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
8771
8772 /* We currently do not free assignements of panel fitters on
8773 * ivb/hsw (since we don't use the higher upscaling modes which
8774 * differentiates them) so just WARN about this case for now. */
8775 if (IS_GEN7(dev)) {
8776 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8777 PF_PIPE_SEL_IVB(crtc->pipe));
8778 }
2fa2fe9a 8779 }
79e53945
JB
8780}
8781
5724dbd1
DL
8782static void
8783ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8784 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
8785{
8786 struct drm_device *dev = crtc->base.dev;
8787 struct drm_i915_private *dev_priv = dev->dev_private;
8788 u32 val, base, offset;
aeee5a49 8789 int pipe = crtc->pipe;
4c6baa59 8790 int fourcc, pixel_format;
6761dd31 8791 unsigned int aligned_height;
b113d5ee 8792 struct drm_framebuffer *fb;
1b842c89 8793 struct intel_framebuffer *intel_fb;
4c6baa59 8794
42a7b088
DL
8795 val = I915_READ(DSPCNTR(pipe));
8796 if (!(val & DISPLAY_PLANE_ENABLE))
8797 return;
8798
d9806c9f 8799 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8800 if (!intel_fb) {
4c6baa59
JB
8801 DRM_DEBUG_KMS("failed to alloc fb\n");
8802 return;
8803 }
8804
1b842c89
DL
8805 fb = &intel_fb->base;
8806
18c5247e
DV
8807 if (INTEL_INFO(dev)->gen >= 4) {
8808 if (val & DISPPLANE_TILED) {
49af449b 8809 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8810 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8811 }
8812 }
4c6baa59
JB
8813
8814 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8815 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8816 fb->pixel_format = fourcc;
8817 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 8818
aeee5a49 8819 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 8820 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 8821 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8822 } else {
49af449b 8823 if (plane_config->tiling)
aeee5a49 8824 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8825 else
aeee5a49 8826 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8827 }
8828 plane_config->base = base;
8829
8830 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8831 fb->width = ((val >> 16) & 0xfff) + 1;
8832 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8833
8834 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8835 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8836
b113d5ee 8837 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8838 fb->pixel_format,
8839 fb->modifier[0]);
4c6baa59 8840
f37b5c2b 8841 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8842
2844a921
DL
8843 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8844 pipe_name(pipe), fb->width, fb->height,
8845 fb->bits_per_pixel, base, fb->pitches[0],
8846 plane_config->size);
b113d5ee 8847
2d14030b 8848 plane_config->fb = intel_fb;
4c6baa59
JB
8849}
8850
0e8ffe1b 8851static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8852 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8853{
8854 struct drm_device *dev = crtc->base.dev;
8855 struct drm_i915_private *dev_priv = dev->dev_private;
8856 uint32_t tmp;
8857
f458ebbc
DV
8858 if (!intel_display_power_is_enabled(dev_priv,
8859 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
8860 return false;
8861
e143a21c 8862 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8863 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8864
0e8ffe1b
DV
8865 tmp = I915_READ(PIPECONF(crtc->pipe));
8866 if (!(tmp & PIPECONF_ENABLE))
8867 return false;
8868
42571aef
VS
8869 switch (tmp & PIPECONF_BPC_MASK) {
8870 case PIPECONF_6BPC:
8871 pipe_config->pipe_bpp = 18;
8872 break;
8873 case PIPECONF_8BPC:
8874 pipe_config->pipe_bpp = 24;
8875 break;
8876 case PIPECONF_10BPC:
8877 pipe_config->pipe_bpp = 30;
8878 break;
8879 case PIPECONF_12BPC:
8880 pipe_config->pipe_bpp = 36;
8881 break;
8882 default:
8883 break;
8884 }
8885
b5a9fa09
DV
8886 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8887 pipe_config->limited_color_range = true;
8888
ab9412ba 8889 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
8890 struct intel_shared_dpll *pll;
8891
88adfff1
DV
8892 pipe_config->has_pch_encoder = true;
8893
627eb5a3
DV
8894 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8895 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8896 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8897
8898 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8899
c0d43d62 8900 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
8901 pipe_config->shared_dpll =
8902 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8903 } else {
8904 tmp = I915_READ(PCH_DPLL_SEL);
8905 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8906 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8907 else
8908 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8909 }
66e985c0
DV
8910
8911 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8912
8913 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8914 &pipe_config->dpll_hw_state));
c93f54cf
DV
8915
8916 tmp = pipe_config->dpll_hw_state.dpll;
8917 pipe_config->pixel_multiplier =
8918 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8919 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8920
8921 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8922 } else {
8923 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8924 }
8925
1bd1bd80
DV
8926 intel_get_pipe_timings(crtc, pipe_config);
8927
2fa2fe9a
DV
8928 ironlake_get_pfit_config(crtc, pipe_config);
8929
0e8ffe1b
DV
8930 return true;
8931}
8932
be256dc7
PZ
8933static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8934{
8935 struct drm_device *dev = dev_priv->dev;
be256dc7 8936 struct intel_crtc *crtc;
be256dc7 8937
d3fcc808 8938 for_each_intel_crtc(dev, crtc)
e2c719b7 8939 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8940 pipe_name(crtc->pipe));
8941
e2c719b7
RC
8942 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8943 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8944 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8945 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8946 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8947 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8948 "CPU PWM1 enabled\n");
c5107b87 8949 if (IS_HASWELL(dev))
e2c719b7 8950 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8951 "CPU PWM2 enabled\n");
e2c719b7 8952 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8953 "PCH PWM1 enabled\n");
e2c719b7 8954 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8955 "Utility pin enabled\n");
e2c719b7 8956 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8957
9926ada1
PZ
8958 /*
8959 * In theory we can still leave IRQs enabled, as long as only the HPD
8960 * interrupts remain enabled. We used to check for that, but since it's
8961 * gen-specific and since we only disable LCPLL after we fully disable
8962 * the interrupts, the check below should be enough.
8963 */
e2c719b7 8964 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8965}
8966
9ccd5aeb
PZ
8967static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8968{
8969 struct drm_device *dev = dev_priv->dev;
8970
8971 if (IS_HASWELL(dev))
8972 return I915_READ(D_COMP_HSW);
8973 else
8974 return I915_READ(D_COMP_BDW);
8975}
8976
3c4c9b81
PZ
8977static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8978{
8979 struct drm_device *dev = dev_priv->dev;
8980
8981 if (IS_HASWELL(dev)) {
8982 mutex_lock(&dev_priv->rps.hw_lock);
8983 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8984 val))
f475dadf 8985 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
8986 mutex_unlock(&dev_priv->rps.hw_lock);
8987 } else {
9ccd5aeb
PZ
8988 I915_WRITE(D_COMP_BDW, val);
8989 POSTING_READ(D_COMP_BDW);
3c4c9b81 8990 }
be256dc7
PZ
8991}
8992
8993/*
8994 * This function implements pieces of two sequences from BSpec:
8995 * - Sequence for display software to disable LCPLL
8996 * - Sequence for display software to allow package C8+
8997 * The steps implemented here are just the steps that actually touch the LCPLL
8998 * register. Callers should take care of disabling all the display engine
8999 * functions, doing the mode unset, fixing interrupts, etc.
9000 */
6ff58d53
PZ
9001static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9002 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
9003{
9004 uint32_t val;
9005
9006 assert_can_disable_lcpll(dev_priv);
9007
9008 val = I915_READ(LCPLL_CTL);
9009
9010 if (switch_to_fclk) {
9011 val |= LCPLL_CD_SOURCE_FCLK;
9012 I915_WRITE(LCPLL_CTL, val);
9013
9014 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9015 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9016 DRM_ERROR("Switching to FCLK failed\n");
9017
9018 val = I915_READ(LCPLL_CTL);
9019 }
9020
9021 val |= LCPLL_PLL_DISABLE;
9022 I915_WRITE(LCPLL_CTL, val);
9023 POSTING_READ(LCPLL_CTL);
9024
9025 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9026 DRM_ERROR("LCPLL still locked\n");
9027
9ccd5aeb 9028 val = hsw_read_dcomp(dev_priv);
be256dc7 9029 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9030 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9031 ndelay(100);
9032
9ccd5aeb
PZ
9033 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9034 1))
be256dc7
PZ
9035 DRM_ERROR("D_COMP RCOMP still in progress\n");
9036
9037 if (allow_power_down) {
9038 val = I915_READ(LCPLL_CTL);
9039 val |= LCPLL_POWER_DOWN_ALLOW;
9040 I915_WRITE(LCPLL_CTL, val);
9041 POSTING_READ(LCPLL_CTL);
9042 }
9043}
9044
9045/*
9046 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9047 * source.
9048 */
6ff58d53 9049static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9050{
9051 uint32_t val;
9052
9053 val = I915_READ(LCPLL_CTL);
9054
9055 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9056 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9057 return;
9058
a8a8bd54
PZ
9059 /*
9060 * Make sure we're not on PC8 state before disabling PC8, otherwise
9061 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9062 */
59bad947 9063 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9064
be256dc7
PZ
9065 if (val & LCPLL_POWER_DOWN_ALLOW) {
9066 val &= ~LCPLL_POWER_DOWN_ALLOW;
9067 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9068 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9069 }
9070
9ccd5aeb 9071 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9072 val |= D_COMP_COMP_FORCE;
9073 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9074 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9075
9076 val = I915_READ(LCPLL_CTL);
9077 val &= ~LCPLL_PLL_DISABLE;
9078 I915_WRITE(LCPLL_CTL, val);
9079
9080 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9081 DRM_ERROR("LCPLL not locked yet\n");
9082
9083 if (val & LCPLL_CD_SOURCE_FCLK) {
9084 val = I915_READ(LCPLL_CTL);
9085 val &= ~LCPLL_CD_SOURCE_FCLK;
9086 I915_WRITE(LCPLL_CTL, val);
9087
9088 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9089 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9090 DRM_ERROR("Switching back to LCPLL failed\n");
9091 }
215733fa 9092
59bad947 9093 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
be256dc7
PZ
9094}
9095
765dab67
PZ
9096/*
9097 * Package states C8 and deeper are really deep PC states that can only be
9098 * reached when all the devices on the system allow it, so even if the graphics
9099 * device allows PC8+, it doesn't mean the system will actually get to these
9100 * states. Our driver only allows PC8+ when going into runtime PM.
9101 *
9102 * The requirements for PC8+ are that all the outputs are disabled, the power
9103 * well is disabled and most interrupts are disabled, and these are also
9104 * requirements for runtime PM. When these conditions are met, we manually do
9105 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9106 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9107 * hang the machine.
9108 *
9109 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9110 * the state of some registers, so when we come back from PC8+ we need to
9111 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9112 * need to take care of the registers kept by RC6. Notice that this happens even
9113 * if we don't put the device in PCI D3 state (which is what currently happens
9114 * because of the runtime PM support).
9115 *
9116 * For more, read "Display Sequences for Package C8" on the hardware
9117 * documentation.
9118 */
a14cb6fc 9119void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9120{
c67a470b
PZ
9121 struct drm_device *dev = dev_priv->dev;
9122 uint32_t val;
9123
c67a470b
PZ
9124 DRM_DEBUG_KMS("Enabling package C8+\n");
9125
c67a470b
PZ
9126 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9127 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9128 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9129 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9130 }
9131
9132 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9133 hsw_disable_lcpll(dev_priv, true, true);
9134}
9135
a14cb6fc 9136void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9137{
9138 struct drm_device *dev = dev_priv->dev;
9139 uint32_t val;
9140
c67a470b
PZ
9141 DRM_DEBUG_KMS("Disabling package C8+\n");
9142
9143 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9144 lpt_init_pch_refclk(dev);
9145
9146 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9147 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9148 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9149 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9150 }
9151
9152 intel_prepare_ddi(dev);
c67a470b
PZ
9153}
9154
f8437dd1
VK
9155static void broxton_modeset_global_resources(struct drm_atomic_state *state)
9156{
9157 struct drm_device *dev = state->dev;
9158 struct drm_i915_private *dev_priv = dev->dev_private;
9159 int max_pixclk = intel_mode_max_pixclk(state);
9160 int req_cdclk;
9161
9162 /* see the comment in valleyview_modeset_global_resources */
9163 if (WARN_ON(max_pixclk < 0))
9164 return;
9165
9166 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9167
9168 if (req_cdclk != dev_priv->cdclk_freq)
9169 broxton_set_cdclk(dev, req_cdclk);
9170}
9171
190f68c5
ACO
9172static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9173 struct intel_crtc_state *crtc_state)
09b4ddf9 9174{
190f68c5 9175 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 9176 return -EINVAL;
716c2e55 9177
c7653199 9178 crtc->lowfreq_avail = false;
644cef34 9179
c8f7a0db 9180 return 0;
79e53945
JB
9181}
9182
3760b59c
S
9183static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9184 enum port port,
9185 struct intel_crtc_state *pipe_config)
9186{
9187 switch (port) {
9188 case PORT_A:
9189 pipe_config->ddi_pll_sel = SKL_DPLL0;
9190 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9191 break;
9192 case PORT_B:
9193 pipe_config->ddi_pll_sel = SKL_DPLL1;
9194 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9195 break;
9196 case PORT_C:
9197 pipe_config->ddi_pll_sel = SKL_DPLL2;
9198 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9199 break;
9200 default:
9201 DRM_ERROR("Incorrect port type\n");
9202 }
9203}
9204
96b7dfb7
S
9205static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9206 enum port port,
5cec258b 9207 struct intel_crtc_state *pipe_config)
96b7dfb7 9208{
3148ade7 9209 u32 temp, dpll_ctl1;
96b7dfb7
S
9210
9211 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9212 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9213
9214 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
9215 case SKL_DPLL0:
9216 /*
9217 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9218 * of the shared DPLL framework and thus needs to be read out
9219 * separately
9220 */
9221 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9222 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9223 break;
96b7dfb7
S
9224 case SKL_DPLL1:
9225 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9226 break;
9227 case SKL_DPLL2:
9228 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9229 break;
9230 case SKL_DPLL3:
9231 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9232 break;
96b7dfb7
S
9233 }
9234}
9235
7d2c8175
DL
9236static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9237 enum port port,
5cec258b 9238 struct intel_crtc_state *pipe_config)
7d2c8175
DL
9239{
9240 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9241
9242 switch (pipe_config->ddi_pll_sel) {
9243 case PORT_CLK_SEL_WRPLL1:
9244 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9245 break;
9246 case PORT_CLK_SEL_WRPLL2:
9247 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9248 break;
9249 }
9250}
9251
26804afd 9252static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9253 struct intel_crtc_state *pipe_config)
26804afd
DV
9254{
9255 struct drm_device *dev = crtc->base.dev;
9256 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9257 struct intel_shared_dpll *pll;
26804afd
DV
9258 enum port port;
9259 uint32_t tmp;
9260
9261 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9262
9263 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9264
96b7dfb7
S
9265 if (IS_SKYLAKE(dev))
9266 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9267 else if (IS_BROXTON(dev))
9268 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9269 else
9270 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9271
d452c5b6
DV
9272 if (pipe_config->shared_dpll >= 0) {
9273 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9274
9275 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9276 &pipe_config->dpll_hw_state));
9277 }
9278
26804afd
DV
9279 /*
9280 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9281 * DDI E. So just check whether this pipe is wired to DDI E and whether
9282 * the PCH transcoder is on.
9283 */
ca370455
DL
9284 if (INTEL_INFO(dev)->gen < 9 &&
9285 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9286 pipe_config->has_pch_encoder = true;
9287
9288 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9289 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9290 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9291
9292 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9293 }
9294}
9295
0e8ffe1b 9296static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9297 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9298{
9299 struct drm_device *dev = crtc->base.dev;
9300 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 9301 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
9302 uint32_t tmp;
9303
f458ebbc 9304 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
9305 POWER_DOMAIN_PIPE(crtc->pipe)))
9306 return false;
9307
e143a21c 9308 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
9309 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9310
eccb140b
DV
9311 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9312 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9313 enum pipe trans_edp_pipe;
9314 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9315 default:
9316 WARN(1, "unknown pipe linked to edp transcoder\n");
9317 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9318 case TRANS_DDI_EDP_INPUT_A_ON:
9319 trans_edp_pipe = PIPE_A;
9320 break;
9321 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9322 trans_edp_pipe = PIPE_B;
9323 break;
9324 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9325 trans_edp_pipe = PIPE_C;
9326 break;
9327 }
9328
9329 if (trans_edp_pipe == crtc->pipe)
9330 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9331 }
9332
f458ebbc 9333 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 9334 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
9335 return false;
9336
eccb140b 9337 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
9338 if (!(tmp & PIPECONF_ENABLE))
9339 return false;
9340
26804afd 9341 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9342
1bd1bd80
DV
9343 intel_get_pipe_timings(crtc, pipe_config);
9344
a1b2278e
CK
9345 if (INTEL_INFO(dev)->gen >= 9) {
9346 skl_init_scalers(dev, crtc, pipe_config);
9347 }
9348
2fa2fe9a 9349 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
bd2e244f 9350 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
ff6d9f55 9351 if (INTEL_INFO(dev)->gen == 9)
bd2e244f 9352 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9353 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 9354 ironlake_get_pfit_config(crtc, pipe_config);
ff6d9f55
JB
9355 else
9356 MISSING_CASE(INTEL_INFO(dev)->gen);
9357
a1b2278e
CK
9358 } else {
9359 pipe_config->scaler_state.scaler_id = -1;
9360 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f 9361 }
88adfff1 9362
e59150dc
JB
9363 if (IS_HASWELL(dev))
9364 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9365 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9366
ebb69c95
CT
9367 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9368 pipe_config->pixel_multiplier =
9369 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9370 } else {
9371 pipe_config->pixel_multiplier = 1;
9372 }
6c49f241 9373
0e8ffe1b
DV
9374 return true;
9375}
9376
560b85bb
CW
9377static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9378{
9379 struct drm_device *dev = crtc->dev;
9380 struct drm_i915_private *dev_priv = dev->dev_private;
9381 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9382 uint32_t cntl = 0, size = 0;
560b85bb 9383
dc41c154 9384 if (base) {
3dd512fb
MR
9385 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9386 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
9387 unsigned int stride = roundup_pow_of_two(width) * 4;
9388
9389 switch (stride) {
9390 default:
9391 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9392 width, stride);
9393 stride = 256;
9394 /* fallthrough */
9395 case 256:
9396 case 512:
9397 case 1024:
9398 case 2048:
9399 break;
4b0e333e
CW
9400 }
9401
dc41c154
VS
9402 cntl |= CURSOR_ENABLE |
9403 CURSOR_GAMMA_ENABLE |
9404 CURSOR_FORMAT_ARGB |
9405 CURSOR_STRIDE(stride);
9406
9407 size = (height << 12) | width;
4b0e333e 9408 }
560b85bb 9409
dc41c154
VS
9410 if (intel_crtc->cursor_cntl != 0 &&
9411 (intel_crtc->cursor_base != base ||
9412 intel_crtc->cursor_size != size ||
9413 intel_crtc->cursor_cntl != cntl)) {
9414 /* On these chipsets we can only modify the base/size/stride
9415 * whilst the cursor is disabled.
9416 */
9417 I915_WRITE(_CURACNTR, 0);
4b0e333e 9418 POSTING_READ(_CURACNTR);
dc41c154 9419 intel_crtc->cursor_cntl = 0;
4b0e333e 9420 }
560b85bb 9421
99d1f387 9422 if (intel_crtc->cursor_base != base) {
9db4a9c7 9423 I915_WRITE(_CURABASE, base);
99d1f387
VS
9424 intel_crtc->cursor_base = base;
9425 }
4726e0b0 9426
dc41c154
VS
9427 if (intel_crtc->cursor_size != size) {
9428 I915_WRITE(CURSIZE, size);
9429 intel_crtc->cursor_size = size;
4b0e333e 9430 }
560b85bb 9431
4b0e333e 9432 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
9433 I915_WRITE(_CURACNTR, cntl);
9434 POSTING_READ(_CURACNTR);
4b0e333e 9435 intel_crtc->cursor_cntl = cntl;
560b85bb 9436 }
560b85bb
CW
9437}
9438
560b85bb 9439static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
9440{
9441 struct drm_device *dev = crtc->dev;
9442 struct drm_i915_private *dev_priv = dev->dev_private;
9443 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9444 int pipe = intel_crtc->pipe;
4b0e333e
CW
9445 uint32_t cntl;
9446
9447 cntl = 0;
9448 if (base) {
9449 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 9450 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
9451 case 64:
9452 cntl |= CURSOR_MODE_64_ARGB_AX;
9453 break;
9454 case 128:
9455 cntl |= CURSOR_MODE_128_ARGB_AX;
9456 break;
9457 case 256:
9458 cntl |= CURSOR_MODE_256_ARGB_AX;
9459 break;
9460 default:
3dd512fb 9461 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 9462 return;
65a21cd6 9463 }
4b0e333e 9464 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
9465
9466 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9467 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 9468 }
65a21cd6 9469
8e7d688b 9470 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
9471 cntl |= CURSOR_ROTATE_180;
9472
4b0e333e
CW
9473 if (intel_crtc->cursor_cntl != cntl) {
9474 I915_WRITE(CURCNTR(pipe), cntl);
9475 POSTING_READ(CURCNTR(pipe));
9476 intel_crtc->cursor_cntl = cntl;
65a21cd6 9477 }
4b0e333e 9478
65a21cd6 9479 /* and commit changes on next vblank */
5efb3e28
VS
9480 I915_WRITE(CURBASE(pipe), base);
9481 POSTING_READ(CURBASE(pipe));
99d1f387
VS
9482
9483 intel_crtc->cursor_base = base;
65a21cd6
JB
9484}
9485
cda4b7d3 9486/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
9487static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9488 bool on)
cda4b7d3
CW
9489{
9490 struct drm_device *dev = crtc->dev;
9491 struct drm_i915_private *dev_priv = dev->dev_private;
9492 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9493 int pipe = intel_crtc->pipe;
3d7d6510
MR
9494 int x = crtc->cursor_x;
9495 int y = crtc->cursor_y;
d6e4db15 9496 u32 base = 0, pos = 0;
cda4b7d3 9497
d6e4db15 9498 if (on)
cda4b7d3 9499 base = intel_crtc->cursor_addr;
cda4b7d3 9500
6e3c9717 9501 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
9502 base = 0;
9503
6e3c9717 9504 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
9505 base = 0;
9506
9507 if (x < 0) {
3dd512fb 9508 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
cda4b7d3
CW
9509 base = 0;
9510
9511 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9512 x = -x;
9513 }
9514 pos |= x << CURSOR_X_SHIFT;
9515
9516 if (y < 0) {
3dd512fb 9517 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
cda4b7d3
CW
9518 base = 0;
9519
9520 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9521 y = -y;
9522 }
9523 pos |= y << CURSOR_Y_SHIFT;
9524
4b0e333e 9525 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
9526 return;
9527
5efb3e28
VS
9528 I915_WRITE(CURPOS(pipe), pos);
9529
4398ad45
VS
9530 /* ILK+ do this automagically */
9531 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 9532 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
3dd512fb
MR
9533 base += (intel_crtc->base.cursor->state->crtc_h *
9534 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
4398ad45
VS
9535 }
9536
8ac54669 9537 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
9538 i845_update_cursor(crtc, base);
9539 else
9540 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
9541}
9542
dc41c154
VS
9543static bool cursor_size_ok(struct drm_device *dev,
9544 uint32_t width, uint32_t height)
9545{
9546 if (width == 0 || height == 0)
9547 return false;
9548
9549 /*
9550 * 845g/865g are special in that they are only limited by
9551 * the width of their cursors, the height is arbitrary up to
9552 * the precision of the register. Everything else requires
9553 * square cursors, limited to a few power-of-two sizes.
9554 */
9555 if (IS_845G(dev) || IS_I865G(dev)) {
9556 if ((width & 63) != 0)
9557 return false;
9558
9559 if (width > (IS_845G(dev) ? 64 : 512))
9560 return false;
9561
9562 if (height > 1023)
9563 return false;
9564 } else {
9565 switch (width | height) {
9566 case 256:
9567 case 128:
9568 if (IS_GEN2(dev))
9569 return false;
9570 case 64:
9571 break;
9572 default:
9573 return false;
9574 }
9575 }
9576
9577 return true;
9578}
9579
79e53945 9580static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 9581 u16 *blue, uint32_t start, uint32_t size)
79e53945 9582{
7203425a 9583 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 9584 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 9585
7203425a 9586 for (i = start; i < end; i++) {
79e53945
JB
9587 intel_crtc->lut_r[i] = red[i] >> 8;
9588 intel_crtc->lut_g[i] = green[i] >> 8;
9589 intel_crtc->lut_b[i] = blue[i] >> 8;
9590 }
9591
9592 intel_crtc_load_lut(crtc);
9593}
9594
79e53945
JB
9595/* VESA 640x480x72Hz mode to set on the pipe */
9596static struct drm_display_mode load_detect_mode = {
9597 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9598 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9599};
9600
a8bb6818
DV
9601struct drm_framebuffer *
9602__intel_framebuffer_create(struct drm_device *dev,
9603 struct drm_mode_fb_cmd2 *mode_cmd,
9604 struct drm_i915_gem_object *obj)
d2dff872
CW
9605{
9606 struct intel_framebuffer *intel_fb;
9607 int ret;
9608
9609 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9610 if (!intel_fb) {
6ccb81f2 9611 drm_gem_object_unreference(&obj->base);
d2dff872
CW
9612 return ERR_PTR(-ENOMEM);
9613 }
9614
9615 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
9616 if (ret)
9617 goto err;
d2dff872
CW
9618
9619 return &intel_fb->base;
dd4916c5 9620err:
6ccb81f2 9621 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
9622 kfree(intel_fb);
9623
9624 return ERR_PTR(ret);
d2dff872
CW
9625}
9626
b5ea642a 9627static struct drm_framebuffer *
a8bb6818
DV
9628intel_framebuffer_create(struct drm_device *dev,
9629 struct drm_mode_fb_cmd2 *mode_cmd,
9630 struct drm_i915_gem_object *obj)
9631{
9632 struct drm_framebuffer *fb;
9633 int ret;
9634
9635 ret = i915_mutex_lock_interruptible(dev);
9636 if (ret)
9637 return ERR_PTR(ret);
9638 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9639 mutex_unlock(&dev->struct_mutex);
9640
9641 return fb;
9642}
9643
d2dff872
CW
9644static u32
9645intel_framebuffer_pitch_for_width(int width, int bpp)
9646{
9647 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9648 return ALIGN(pitch, 64);
9649}
9650
9651static u32
9652intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9653{
9654 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 9655 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
9656}
9657
9658static struct drm_framebuffer *
9659intel_framebuffer_create_for_mode(struct drm_device *dev,
9660 struct drm_display_mode *mode,
9661 int depth, int bpp)
9662{
9663 struct drm_i915_gem_object *obj;
0fed39bd 9664 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
9665
9666 obj = i915_gem_alloc_object(dev,
9667 intel_framebuffer_size_for_mode(mode, bpp));
9668 if (obj == NULL)
9669 return ERR_PTR(-ENOMEM);
9670
9671 mode_cmd.width = mode->hdisplay;
9672 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
9673 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9674 bpp);
5ca0c34a 9675 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
9676
9677 return intel_framebuffer_create(dev, &mode_cmd, obj);
9678}
9679
9680static struct drm_framebuffer *
9681mode_fits_in_fbdev(struct drm_device *dev,
9682 struct drm_display_mode *mode)
9683{
4520f53a 9684#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
9685 struct drm_i915_private *dev_priv = dev->dev_private;
9686 struct drm_i915_gem_object *obj;
9687 struct drm_framebuffer *fb;
9688
4c0e5528 9689 if (!dev_priv->fbdev)
d2dff872
CW
9690 return NULL;
9691
4c0e5528 9692 if (!dev_priv->fbdev->fb)
d2dff872
CW
9693 return NULL;
9694
4c0e5528
DV
9695 obj = dev_priv->fbdev->fb->obj;
9696 BUG_ON(!obj);
9697
8bcd4553 9698 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
9699 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9700 fb->bits_per_pixel))
d2dff872
CW
9701 return NULL;
9702
01f2c773 9703 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
9704 return NULL;
9705
9706 return fb;
4520f53a
DV
9707#else
9708 return NULL;
9709#endif
d2dff872
CW
9710}
9711
d3a40d1b
ACO
9712static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9713 struct drm_crtc *crtc,
9714 struct drm_display_mode *mode,
9715 struct drm_framebuffer *fb,
9716 int x, int y)
9717{
9718 struct drm_plane_state *plane_state;
9719 int hdisplay, vdisplay;
9720 int ret;
9721
9722 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9723 if (IS_ERR(plane_state))
9724 return PTR_ERR(plane_state);
9725
9726 if (mode)
9727 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
9728 else
9729 hdisplay = vdisplay = 0;
9730
9731 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9732 if (ret)
9733 return ret;
9734 drm_atomic_set_fb_for_plane(plane_state, fb);
9735 plane_state->crtc_x = 0;
9736 plane_state->crtc_y = 0;
9737 plane_state->crtc_w = hdisplay;
9738 plane_state->crtc_h = vdisplay;
9739 plane_state->src_x = x << 16;
9740 plane_state->src_y = y << 16;
9741 plane_state->src_w = hdisplay << 16;
9742 plane_state->src_h = vdisplay << 16;
9743
9744 return 0;
9745}
9746
d2434ab7 9747bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 9748 struct drm_display_mode *mode,
51fd371b
RC
9749 struct intel_load_detect_pipe *old,
9750 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
9751{
9752 struct intel_crtc *intel_crtc;
d2434ab7
DV
9753 struct intel_encoder *intel_encoder =
9754 intel_attached_encoder(connector);
79e53945 9755 struct drm_crtc *possible_crtc;
4ef69c7a 9756 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
9757 struct drm_crtc *crtc = NULL;
9758 struct drm_device *dev = encoder->dev;
94352cf9 9759 struct drm_framebuffer *fb;
51fd371b 9760 struct drm_mode_config *config = &dev->mode_config;
83a57153 9761 struct drm_atomic_state *state = NULL;
944b0c76 9762 struct drm_connector_state *connector_state;
4be07317 9763 struct intel_crtc_state *crtc_state;
51fd371b 9764 int ret, i = -1;
79e53945 9765
d2dff872 9766 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9767 connector->base.id, connector->name,
8e329a03 9768 encoder->base.id, encoder->name);
d2dff872 9769
51fd371b
RC
9770retry:
9771 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9772 if (ret)
9773 goto fail_unlock;
6e9f798d 9774
79e53945
JB
9775 /*
9776 * Algorithm gets a little messy:
7a5e4805 9777 *
79e53945
JB
9778 * - if the connector already has an assigned crtc, use it (but make
9779 * sure it's on first)
7a5e4805 9780 *
79e53945
JB
9781 * - try to find the first unused crtc that can drive this connector,
9782 * and use that if we find one
79e53945
JB
9783 */
9784
9785 /* See if we already have a CRTC for this connector */
9786 if (encoder->crtc) {
9787 crtc = encoder->crtc;
8261b191 9788
51fd371b 9789 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de
DV
9790 if (ret)
9791 goto fail_unlock;
9792 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b
RC
9793 if (ret)
9794 goto fail_unlock;
7b24056b 9795
24218aac 9796 old->dpms_mode = connector->dpms;
8261b191
CW
9797 old->load_detect_temp = false;
9798
9799 /* Make sure the crtc and connector are running */
24218aac
DV
9800 if (connector->dpms != DRM_MODE_DPMS_ON)
9801 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 9802
7173188d 9803 return true;
79e53945
JB
9804 }
9805
9806 /* Find an unused one (if possible) */
70e1e0ec 9807 for_each_crtc(dev, possible_crtc) {
79e53945
JB
9808 i++;
9809 if (!(encoder->possible_crtcs & (1 << i)))
9810 continue;
83d65738 9811 if (possible_crtc->state->enable)
a459249c
VS
9812 continue;
9813 /* This can occur when applying the pipe A quirk on resume. */
9814 if (to_intel_crtc(possible_crtc)->new_enabled)
9815 continue;
9816
9817 crtc = possible_crtc;
9818 break;
79e53945
JB
9819 }
9820
9821 /*
9822 * If we didn't find an unused CRTC, don't use any.
9823 */
9824 if (!crtc) {
7173188d 9825 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 9826 goto fail_unlock;
79e53945
JB
9827 }
9828
51fd371b
RC
9829 ret = drm_modeset_lock(&crtc->mutex, ctx);
9830 if (ret)
4d02e2de
DV
9831 goto fail_unlock;
9832 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9833 if (ret)
51fd371b 9834 goto fail_unlock;
fc303101
DV
9835 intel_encoder->new_crtc = to_intel_crtc(crtc);
9836 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
9837
9838 intel_crtc = to_intel_crtc(crtc);
412b61d8 9839 intel_crtc->new_enabled = true;
24218aac 9840 old->dpms_mode = connector->dpms;
8261b191 9841 old->load_detect_temp = true;
d2dff872 9842 old->release_fb = NULL;
79e53945 9843
83a57153
ACO
9844 state = drm_atomic_state_alloc(dev);
9845 if (!state)
9846 return false;
9847
9848 state->acquire_ctx = ctx;
9849
944b0c76
ACO
9850 connector_state = drm_atomic_get_connector_state(state, connector);
9851 if (IS_ERR(connector_state)) {
9852 ret = PTR_ERR(connector_state);
9853 goto fail;
9854 }
9855
9856 connector_state->crtc = crtc;
9857 connector_state->best_encoder = &intel_encoder->base;
9858
4be07317
ACO
9859 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9860 if (IS_ERR(crtc_state)) {
9861 ret = PTR_ERR(crtc_state);
9862 goto fail;
9863 }
9864
9865 crtc_state->base.enable = true;
9866
6492711d
CW
9867 if (!mode)
9868 mode = &load_detect_mode;
79e53945 9869
d2dff872
CW
9870 /* We need a framebuffer large enough to accommodate all accesses
9871 * that the plane may generate whilst we perform load detection.
9872 * We can not rely on the fbcon either being present (we get called
9873 * during its initialisation to detect all boot displays, or it may
9874 * not even exist) or that it is large enough to satisfy the
9875 * requested mode.
9876 */
94352cf9
DV
9877 fb = mode_fits_in_fbdev(dev, mode);
9878 if (fb == NULL) {
d2dff872 9879 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
9880 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9881 old->release_fb = fb;
d2dff872
CW
9882 } else
9883 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 9884 if (IS_ERR(fb)) {
d2dff872 9885 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 9886 goto fail;
79e53945 9887 }
79e53945 9888
d3a40d1b
ACO
9889 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9890 if (ret)
9891 goto fail;
9892
cf6d0d76 9893 if (intel_set_mode(crtc, mode, state)) {
6492711d 9894 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
9895 if (old->release_fb)
9896 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 9897 goto fail;
79e53945 9898 }
9128b040 9899 crtc->primary->crtc = crtc;
7173188d 9900
79e53945 9901 /* let the connector get through one full cycle before testing */
9d0498a2 9902 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 9903 return true;
412b61d8
VS
9904
9905 fail:
83d65738 9906 intel_crtc->new_enabled = crtc->state->enable;
51fd371b 9907fail_unlock:
e5d958ef
ACO
9908 drm_atomic_state_free(state);
9909 state = NULL;
83a57153 9910
51fd371b
RC
9911 if (ret == -EDEADLK) {
9912 drm_modeset_backoff(ctx);
9913 goto retry;
9914 }
9915
412b61d8 9916 return false;
79e53945
JB
9917}
9918
d2434ab7 9919void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
9920 struct intel_load_detect_pipe *old,
9921 struct drm_modeset_acquire_ctx *ctx)
79e53945 9922{
83a57153 9923 struct drm_device *dev = connector->dev;
d2434ab7
DV
9924 struct intel_encoder *intel_encoder =
9925 intel_attached_encoder(connector);
4ef69c7a 9926 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 9927 struct drm_crtc *crtc = encoder->crtc;
412b61d8 9928 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83a57153 9929 struct drm_atomic_state *state;
944b0c76 9930 struct drm_connector_state *connector_state;
4be07317 9931 struct intel_crtc_state *crtc_state;
d3a40d1b 9932 int ret;
79e53945 9933
d2dff872 9934 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9935 connector->base.id, connector->name,
8e329a03 9936 encoder->base.id, encoder->name);
d2dff872 9937
8261b191 9938 if (old->load_detect_temp) {
83a57153 9939 state = drm_atomic_state_alloc(dev);
944b0c76
ACO
9940 if (!state)
9941 goto fail;
83a57153
ACO
9942
9943 state->acquire_ctx = ctx;
9944
944b0c76
ACO
9945 connector_state = drm_atomic_get_connector_state(state, connector);
9946 if (IS_ERR(connector_state))
9947 goto fail;
9948
4be07317
ACO
9949 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9950 if (IS_ERR(crtc_state))
9951 goto fail;
9952
fc303101
DV
9953 to_intel_connector(connector)->new_encoder = NULL;
9954 intel_encoder->new_crtc = NULL;
412b61d8 9955 intel_crtc->new_enabled = false;
944b0c76
ACO
9956
9957 connector_state->best_encoder = NULL;
9958 connector_state->crtc = NULL;
9959
4be07317
ACO
9960 crtc_state->base.enable = false;
9961
d3a40d1b
ACO
9962 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
9963 0, 0);
9964 if (ret)
9965 goto fail;
9966
cf6d0d76 9967 intel_set_mode(crtc, NULL, state);
83a57153
ACO
9968
9969 drm_atomic_state_free(state);
d2dff872 9970
36206361
DV
9971 if (old->release_fb) {
9972 drm_framebuffer_unregister_private(old->release_fb);
9973 drm_framebuffer_unreference(old->release_fb);
9974 }
d2dff872 9975
0622a53c 9976 return;
79e53945
JB
9977 }
9978
c751ce4f 9979 /* Switch crtc and encoder back off if necessary */
24218aac
DV
9980 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9981 connector->funcs->dpms(connector, old->dpms_mode);
944b0c76
ACO
9982
9983 return;
9984fail:
9985 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9986 drm_atomic_state_free(state);
79e53945
JB
9987}
9988
da4a1efa 9989static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 9990 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
9991{
9992 struct drm_i915_private *dev_priv = dev->dev_private;
9993 u32 dpll = pipe_config->dpll_hw_state.dpll;
9994
9995 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 9996 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
9997 else if (HAS_PCH_SPLIT(dev))
9998 return 120000;
9999 else if (!IS_GEN2(dev))
10000 return 96000;
10001 else
10002 return 48000;
10003}
10004
79e53945 10005/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 10006static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 10007 struct intel_crtc_state *pipe_config)
79e53945 10008{
f1f644dc 10009 struct drm_device *dev = crtc->base.dev;
79e53945 10010 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10011 int pipe = pipe_config->cpu_transcoder;
293623f7 10012 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10013 u32 fp;
10014 intel_clock_t clock;
da4a1efa 10015 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10016
10017 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10018 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10019 else
293623f7 10020 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10021
10022 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10023 if (IS_PINEVIEW(dev)) {
10024 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10025 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10026 } else {
10027 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10028 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10029 }
10030
a6c45cf0 10031 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10032 if (IS_PINEVIEW(dev))
10033 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10034 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10035 else
10036 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10037 DPLL_FPA01_P1_POST_DIV_SHIFT);
10038
10039 switch (dpll & DPLL_MODE_MASK) {
10040 case DPLLB_MODE_DAC_SERIAL:
10041 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10042 5 : 10;
10043 break;
10044 case DPLLB_MODE_LVDS:
10045 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10046 7 : 14;
10047 break;
10048 default:
28c97730 10049 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10050 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10051 return;
79e53945
JB
10052 }
10053
ac58c3f0 10054 if (IS_PINEVIEW(dev))
da4a1efa 10055 pineview_clock(refclk, &clock);
ac58c3f0 10056 else
da4a1efa 10057 i9xx_clock(refclk, &clock);
79e53945 10058 } else {
0fb58223 10059 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10060 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10061
10062 if (is_lvds) {
10063 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10064 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10065
10066 if (lvds & LVDS_CLKB_POWER_UP)
10067 clock.p2 = 7;
10068 else
10069 clock.p2 = 14;
79e53945
JB
10070 } else {
10071 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10072 clock.p1 = 2;
10073 else {
10074 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10075 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10076 }
10077 if (dpll & PLL_P2_DIVIDE_BY_4)
10078 clock.p2 = 4;
10079 else
10080 clock.p2 = 2;
79e53945 10081 }
da4a1efa
VS
10082
10083 i9xx_clock(refclk, &clock);
79e53945
JB
10084 }
10085
18442d08
VS
10086 /*
10087 * This value includes pixel_multiplier. We will use
241bfc38 10088 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10089 * encoder's get_config() function.
10090 */
10091 pipe_config->port_clock = clock.dot;
f1f644dc
JB
10092}
10093
6878da05
VS
10094int intel_dotclock_calculate(int link_freq,
10095 const struct intel_link_m_n *m_n)
f1f644dc 10096{
f1f644dc
JB
10097 /*
10098 * The calculation for the data clock is:
1041a02f 10099 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10100 * But we want to avoid losing precison if possible, so:
1041a02f 10101 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10102 *
10103 * and the link clock is simpler:
1041a02f 10104 * link_clock = (m * link_clock) / n
f1f644dc
JB
10105 */
10106
6878da05
VS
10107 if (!m_n->link_n)
10108 return 0;
f1f644dc 10109
6878da05
VS
10110 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10111}
f1f644dc 10112
18442d08 10113static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10114 struct intel_crtc_state *pipe_config)
6878da05
VS
10115{
10116 struct drm_device *dev = crtc->base.dev;
79e53945 10117
18442d08
VS
10118 /* read out port_clock from the DPLL */
10119 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10120
f1f644dc 10121 /*
18442d08 10122 * This value does not include pixel_multiplier.
241bfc38 10123 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
10124 * agree once we know their relationship in the encoder's
10125 * get_config() function.
79e53945 10126 */
2d112de7 10127 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
10128 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10129 &pipe_config->fdi_m_n);
79e53945
JB
10130}
10131
10132/** Returns the currently programmed mode of the given pipe. */
10133struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10134 struct drm_crtc *crtc)
10135{
548f245b 10136 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10137 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10138 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10139 struct drm_display_mode *mode;
5cec258b 10140 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
10141 int htot = I915_READ(HTOTAL(cpu_transcoder));
10142 int hsync = I915_READ(HSYNC(cpu_transcoder));
10143 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10144 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10145 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10146
10147 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10148 if (!mode)
10149 return NULL;
10150
f1f644dc
JB
10151 /*
10152 * Construct a pipe_config sufficient for getting the clock info
10153 * back out of crtc_clock_get.
10154 *
10155 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10156 * to use a real value here instead.
10157 */
293623f7 10158 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 10159 pipe_config.pixel_multiplier = 1;
293623f7
VS
10160 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10161 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10162 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
10163 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10164
773ae034 10165 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
10166 mode->hdisplay = (htot & 0xffff) + 1;
10167 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10168 mode->hsync_start = (hsync & 0xffff) + 1;
10169 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10170 mode->vdisplay = (vtot & 0xffff) + 1;
10171 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10172 mode->vsync_start = (vsync & 0xffff) + 1;
10173 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10174
10175 drm_mode_set_name(mode);
79e53945
JB
10176
10177 return mode;
10178}
10179
652c393a
JB
10180static void intel_decrease_pllclock(struct drm_crtc *crtc)
10181{
10182 struct drm_device *dev = crtc->dev;
fbee40df 10183 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 10185
baff296c 10186 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
10187 return;
10188
10189 if (!dev_priv->lvds_downclock_avail)
10190 return;
10191
10192 /*
10193 * Since this is called by a timer, we should never get here in
10194 * the manual case.
10195 */
10196 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
10197 int pipe = intel_crtc->pipe;
10198 int dpll_reg = DPLL(pipe);
10199 int dpll;
f6e5b160 10200
44d98a61 10201 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 10202
8ac5a6d5 10203 assert_panel_unlocked(dev_priv, pipe);
652c393a 10204
dc257cf1 10205 dpll = I915_READ(dpll_reg);
652c393a
JB
10206 dpll |= DISPLAY_RATE_SELECT_FPA1;
10207 I915_WRITE(dpll_reg, dpll);
9d0498a2 10208 intel_wait_for_vblank(dev, pipe);
652c393a
JB
10209 dpll = I915_READ(dpll_reg);
10210 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 10211 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
10212 }
10213
10214}
10215
f047e395
CW
10216void intel_mark_busy(struct drm_device *dev)
10217{
c67a470b
PZ
10218 struct drm_i915_private *dev_priv = dev->dev_private;
10219
f62a0076
CW
10220 if (dev_priv->mm.busy)
10221 return;
10222
43694d69 10223 intel_runtime_pm_get(dev_priv);
c67a470b 10224 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10225 if (INTEL_INFO(dev)->gen >= 6)
10226 gen6_rps_busy(dev_priv);
f62a0076 10227 dev_priv->mm.busy = true;
f047e395
CW
10228}
10229
10230void intel_mark_idle(struct drm_device *dev)
652c393a 10231{
c67a470b 10232 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10233 struct drm_crtc *crtc;
652c393a 10234
f62a0076
CW
10235 if (!dev_priv->mm.busy)
10236 return;
10237
10238 dev_priv->mm.busy = false;
10239
70e1e0ec 10240 for_each_crtc(dev, crtc) {
f4510a27 10241 if (!crtc->primary->fb)
652c393a
JB
10242 continue;
10243
725a5b54 10244 intel_decrease_pllclock(crtc);
652c393a 10245 }
b29c19b6 10246
3d13ef2e 10247 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10248 gen6_rps_idle(dev->dev_private);
bb4cdd53 10249
43694d69 10250 intel_runtime_pm_put(dev_priv);
652c393a
JB
10251}
10252
f5de6e07
ACO
10253static void intel_crtc_set_state(struct intel_crtc *crtc,
10254 struct intel_crtc_state *crtc_state)
10255{
10256 kfree(crtc->config);
10257 crtc->config = crtc_state;
16f3f658 10258 crtc->base.state = &crtc_state->base;
f5de6e07
ACO
10259}
10260
79e53945
JB
10261static void intel_crtc_destroy(struct drm_crtc *crtc)
10262{
10263 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10264 struct drm_device *dev = crtc->dev;
10265 struct intel_unpin_work *work;
67e77c5a 10266
5e2d7afc 10267 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10268 work = intel_crtc->unpin_work;
10269 intel_crtc->unpin_work = NULL;
5e2d7afc 10270 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10271
10272 if (work) {
10273 cancel_work_sync(&work->work);
10274 kfree(work);
10275 }
79e53945 10276
f5de6e07 10277 intel_crtc_set_state(intel_crtc, NULL);
79e53945 10278 drm_crtc_cleanup(crtc);
67e77c5a 10279
79e53945
JB
10280 kfree(intel_crtc);
10281}
10282
6b95a207
KH
10283static void intel_unpin_work_fn(struct work_struct *__work)
10284{
10285 struct intel_unpin_work *work =
10286 container_of(__work, struct intel_unpin_work, work);
b4a98e57 10287 struct drm_device *dev = work->crtc->dev;
f99d7069 10288 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 10289
b4a98e57 10290 mutex_lock(&dev->struct_mutex);
82bc3b2d 10291 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
05394f39 10292 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10293
7ff0ebcc 10294 intel_fbc_update(dev);
f06cc1b9
JH
10295
10296 if (work->flip_queued_req)
146d84f0 10297 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10298 mutex_unlock(&dev->struct_mutex);
10299
f99d7069 10300 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
89ed88ba 10301 drm_framebuffer_unreference(work->old_fb);
f99d7069 10302
b4a98e57
CW
10303 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10304 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10305
6b95a207
KH
10306 kfree(work);
10307}
10308
1afe3e9d 10309static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10310 struct drm_crtc *crtc)
6b95a207 10311{
6b95a207
KH
10312 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10313 struct intel_unpin_work *work;
6b95a207
KH
10314 unsigned long flags;
10315
10316 /* Ignore early vblank irqs */
10317 if (intel_crtc == NULL)
10318 return;
10319
f326038a
DV
10320 /*
10321 * This is called both by irq handlers and the reset code (to complete
10322 * lost pageflips) so needs the full irqsave spinlocks.
10323 */
6b95a207
KH
10324 spin_lock_irqsave(&dev->event_lock, flags);
10325 work = intel_crtc->unpin_work;
e7d841ca
CW
10326
10327 /* Ensure we don't miss a work->pending update ... */
10328 smp_rmb();
10329
10330 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10331 spin_unlock_irqrestore(&dev->event_lock, flags);
10332 return;
10333 }
10334
d6bbafa1 10335 page_flip_completed(intel_crtc);
0af7e4df 10336
6b95a207 10337 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10338}
10339
1afe3e9d
JB
10340void intel_finish_page_flip(struct drm_device *dev, int pipe)
10341{
fbee40df 10342 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10343 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10344
49b14a5c 10345 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10346}
10347
10348void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10349{
fbee40df 10350 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10351 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10352
49b14a5c 10353 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10354}
10355
75f7f3ec
VS
10356/* Is 'a' after or equal to 'b'? */
10357static bool g4x_flip_count_after_eq(u32 a, u32 b)
10358{
10359 return !((a - b) & 0x80000000);
10360}
10361
10362static bool page_flip_finished(struct intel_crtc *crtc)
10363{
10364 struct drm_device *dev = crtc->base.dev;
10365 struct drm_i915_private *dev_priv = dev->dev_private;
10366
bdfa7542
VS
10367 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10368 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10369 return true;
10370
75f7f3ec
VS
10371 /*
10372 * The relevant registers doen't exist on pre-ctg.
10373 * As the flip done interrupt doesn't trigger for mmio
10374 * flips on gmch platforms, a flip count check isn't
10375 * really needed there. But since ctg has the registers,
10376 * include it in the check anyway.
10377 */
10378 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10379 return true;
10380
10381 /*
10382 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10383 * used the same base address. In that case the mmio flip might
10384 * have completed, but the CS hasn't even executed the flip yet.
10385 *
10386 * A flip count check isn't enough as the CS might have updated
10387 * the base address just after start of vblank, but before we
10388 * managed to process the interrupt. This means we'd complete the
10389 * CS flip too soon.
10390 *
10391 * Combining both checks should get us a good enough result. It may
10392 * still happen that the CS flip has been executed, but has not
10393 * yet actually completed. But in case the base address is the same
10394 * anyway, we don't really care.
10395 */
10396 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10397 crtc->unpin_work->gtt_offset &&
10398 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10399 crtc->unpin_work->flip_count);
10400}
10401
6b95a207
KH
10402void intel_prepare_page_flip(struct drm_device *dev, int plane)
10403{
fbee40df 10404 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10405 struct intel_crtc *intel_crtc =
10406 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10407 unsigned long flags;
10408
f326038a
DV
10409
10410 /*
10411 * This is called both by irq handlers and the reset code (to complete
10412 * lost pageflips) so needs the full irqsave spinlocks.
10413 *
10414 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10415 * generate a page-flip completion irq, i.e. every modeset
10416 * is also accompanied by a spurious intel_prepare_page_flip().
10417 */
6b95a207 10418 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10419 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10420 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10421 spin_unlock_irqrestore(&dev->event_lock, flags);
10422}
10423
eba905b2 10424static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
10425{
10426 /* Ensure that the work item is consistent when activating it ... */
10427 smp_wmb();
10428 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10429 /* and that it is marked active as soon as the irq could fire. */
10430 smp_wmb();
10431}
10432
8c9f3aaf
JB
10433static int intel_gen2_queue_flip(struct drm_device *dev,
10434 struct drm_crtc *crtc,
10435 struct drm_framebuffer *fb,
ed8d1975 10436 struct drm_i915_gem_object *obj,
a4872ba6 10437 struct intel_engine_cs *ring,
ed8d1975 10438 uint32_t flags)
8c9f3aaf 10439{
8c9f3aaf 10440 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10441 u32 flip_mask;
10442 int ret;
10443
6d90c952 10444 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10445 if (ret)
4fa62c89 10446 return ret;
8c9f3aaf
JB
10447
10448 /* Can't queue multiple flips, so wait for the previous
10449 * one to finish before executing the next.
10450 */
10451 if (intel_crtc->plane)
10452 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10453 else
10454 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10455 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10456 intel_ring_emit(ring, MI_NOOP);
10457 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10458 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10459 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10460 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 10461 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
10462
10463 intel_mark_page_flip_active(intel_crtc);
09246732 10464 __intel_ring_advance(ring);
83d4092b 10465 return 0;
8c9f3aaf
JB
10466}
10467
10468static int intel_gen3_queue_flip(struct drm_device *dev,
10469 struct drm_crtc *crtc,
10470 struct drm_framebuffer *fb,
ed8d1975 10471 struct drm_i915_gem_object *obj,
a4872ba6 10472 struct intel_engine_cs *ring,
ed8d1975 10473 uint32_t flags)
8c9f3aaf 10474{
8c9f3aaf 10475 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10476 u32 flip_mask;
10477 int ret;
10478
6d90c952 10479 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10480 if (ret)
4fa62c89 10481 return ret;
8c9f3aaf
JB
10482
10483 if (intel_crtc->plane)
10484 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10485 else
10486 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10487 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10488 intel_ring_emit(ring, MI_NOOP);
10489 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10490 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10491 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10492 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
10493 intel_ring_emit(ring, MI_NOOP);
10494
e7d841ca 10495 intel_mark_page_flip_active(intel_crtc);
09246732 10496 __intel_ring_advance(ring);
83d4092b 10497 return 0;
8c9f3aaf
JB
10498}
10499
10500static int intel_gen4_queue_flip(struct drm_device *dev,
10501 struct drm_crtc *crtc,
10502 struct drm_framebuffer *fb,
ed8d1975 10503 struct drm_i915_gem_object *obj,
a4872ba6 10504 struct intel_engine_cs *ring,
ed8d1975 10505 uint32_t flags)
8c9f3aaf
JB
10506{
10507 struct drm_i915_private *dev_priv = dev->dev_private;
10508 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10509 uint32_t pf, pipesrc;
10510 int ret;
10511
6d90c952 10512 ret = intel_ring_begin(ring, 4);
8c9f3aaf 10513 if (ret)
4fa62c89 10514 return ret;
8c9f3aaf
JB
10515
10516 /* i965+ uses the linear or tiled offsets from the
10517 * Display Registers (which do not change across a page-flip)
10518 * so we need only reprogram the base address.
10519 */
6d90c952
DV
10520 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10521 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10522 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10523 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 10524 obj->tiling_mode);
8c9f3aaf
JB
10525
10526 /* XXX Enabling the panel-fitter across page-flip is so far
10527 * untested on non-native modes, so ignore it for now.
10528 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10529 */
10530 pf = 0;
10531 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10532 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10533
10534 intel_mark_page_flip_active(intel_crtc);
09246732 10535 __intel_ring_advance(ring);
83d4092b 10536 return 0;
8c9f3aaf
JB
10537}
10538
10539static int intel_gen6_queue_flip(struct drm_device *dev,
10540 struct drm_crtc *crtc,
10541 struct drm_framebuffer *fb,
ed8d1975 10542 struct drm_i915_gem_object *obj,
a4872ba6 10543 struct intel_engine_cs *ring,
ed8d1975 10544 uint32_t flags)
8c9f3aaf
JB
10545{
10546 struct drm_i915_private *dev_priv = dev->dev_private;
10547 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10548 uint32_t pf, pipesrc;
10549 int ret;
10550
6d90c952 10551 ret = intel_ring_begin(ring, 4);
8c9f3aaf 10552 if (ret)
4fa62c89 10553 return ret;
8c9f3aaf 10554
6d90c952
DV
10555 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10556 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10557 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 10558 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 10559
dc257cf1
DV
10560 /* Contrary to the suggestions in the documentation,
10561 * "Enable Panel Fitter" does not seem to be required when page
10562 * flipping with a non-native mode, and worse causes a normal
10563 * modeset to fail.
10564 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10565 */
10566 pf = 0;
8c9f3aaf 10567 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10568 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10569
10570 intel_mark_page_flip_active(intel_crtc);
09246732 10571 __intel_ring_advance(ring);
83d4092b 10572 return 0;
8c9f3aaf
JB
10573}
10574
7c9017e5
JB
10575static int intel_gen7_queue_flip(struct drm_device *dev,
10576 struct drm_crtc *crtc,
10577 struct drm_framebuffer *fb,
ed8d1975 10578 struct drm_i915_gem_object *obj,
a4872ba6 10579 struct intel_engine_cs *ring,
ed8d1975 10580 uint32_t flags)
7c9017e5 10581{
7c9017e5 10582 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 10583 uint32_t plane_bit = 0;
ffe74d75
CW
10584 int len, ret;
10585
eba905b2 10586 switch (intel_crtc->plane) {
cb05d8de
DV
10587 case PLANE_A:
10588 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10589 break;
10590 case PLANE_B:
10591 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10592 break;
10593 case PLANE_C:
10594 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10595 break;
10596 default:
10597 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 10598 return -ENODEV;
cb05d8de
DV
10599 }
10600
ffe74d75 10601 len = 4;
f476828a 10602 if (ring->id == RCS) {
ffe74d75 10603 len += 6;
f476828a
DL
10604 /*
10605 * On Gen 8, SRM is now taking an extra dword to accommodate
10606 * 48bits addresses, and we need a NOOP for the batch size to
10607 * stay even.
10608 */
10609 if (IS_GEN8(dev))
10610 len += 2;
10611 }
ffe74d75 10612
f66fab8e
VS
10613 /*
10614 * BSpec MI_DISPLAY_FLIP for IVB:
10615 * "The full packet must be contained within the same cache line."
10616 *
10617 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10618 * cacheline, if we ever start emitting more commands before
10619 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10620 * then do the cacheline alignment, and finally emit the
10621 * MI_DISPLAY_FLIP.
10622 */
10623 ret = intel_ring_cacheline_align(ring);
10624 if (ret)
4fa62c89 10625 return ret;
f66fab8e 10626
ffe74d75 10627 ret = intel_ring_begin(ring, len);
7c9017e5 10628 if (ret)
4fa62c89 10629 return ret;
7c9017e5 10630
ffe74d75
CW
10631 /* Unmask the flip-done completion message. Note that the bspec says that
10632 * we should do this for both the BCS and RCS, and that we must not unmask
10633 * more than one flip event at any time (or ensure that one flip message
10634 * can be sent by waiting for flip-done prior to queueing new flips).
10635 * Experimentation says that BCS works despite DERRMR masking all
10636 * flip-done completion events and that unmasking all planes at once
10637 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10638 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10639 */
10640 if (ring->id == RCS) {
10641 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10642 intel_ring_emit(ring, DERRMR);
10643 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10644 DERRMR_PIPEB_PRI_FLIP_DONE |
10645 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
10646 if (IS_GEN8(dev))
10647 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10648 MI_SRM_LRM_GLOBAL_GTT);
10649 else
10650 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10651 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
10652 intel_ring_emit(ring, DERRMR);
10653 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
10654 if (IS_GEN8(dev)) {
10655 intel_ring_emit(ring, 0);
10656 intel_ring_emit(ring, MI_NOOP);
10657 }
ffe74d75
CW
10658 }
10659
cb05d8de 10660 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 10661 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 10662 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 10663 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
10664
10665 intel_mark_page_flip_active(intel_crtc);
09246732 10666 __intel_ring_advance(ring);
83d4092b 10667 return 0;
7c9017e5
JB
10668}
10669
84c33a64
SG
10670static bool use_mmio_flip(struct intel_engine_cs *ring,
10671 struct drm_i915_gem_object *obj)
10672{
10673 /*
10674 * This is not being used for older platforms, because
10675 * non-availability of flip done interrupt forces us to use
10676 * CS flips. Older platforms derive flip done using some clever
10677 * tricks involving the flip_pending status bits and vblank irqs.
10678 * So using MMIO flips there would disrupt this mechanism.
10679 */
10680
8e09bf83
CW
10681 if (ring == NULL)
10682 return true;
10683
84c33a64
SG
10684 if (INTEL_INFO(ring->dev)->gen < 5)
10685 return false;
10686
10687 if (i915.use_mmio_flip < 0)
10688 return false;
10689 else if (i915.use_mmio_flip > 0)
10690 return true;
14bf993e
OM
10691 else if (i915.enable_execlists)
10692 return true;
84c33a64 10693 else
41c52415 10694 return ring != i915_gem_request_get_ring(obj->last_read_req);
84c33a64
SG
10695}
10696
ff944564
DL
10697static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10698{
10699 struct drm_device *dev = intel_crtc->base.dev;
10700 struct drm_i915_private *dev_priv = dev->dev_private;
10701 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564
DL
10702 const enum pipe pipe = intel_crtc->pipe;
10703 u32 ctl, stride;
10704
10705 ctl = I915_READ(PLANE_CTL(pipe, 0));
10706 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
10707 switch (fb->modifier[0]) {
10708 case DRM_FORMAT_MOD_NONE:
10709 break;
10710 case I915_FORMAT_MOD_X_TILED:
ff944564 10711 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
10712 break;
10713 case I915_FORMAT_MOD_Y_TILED:
10714 ctl |= PLANE_CTL_TILED_Y;
10715 break;
10716 case I915_FORMAT_MOD_Yf_TILED:
10717 ctl |= PLANE_CTL_TILED_YF;
10718 break;
10719 default:
10720 MISSING_CASE(fb->modifier[0]);
10721 }
ff944564
DL
10722
10723 /*
10724 * The stride is either expressed as a multiple of 64 bytes chunks for
10725 * linear buffers or in number of tiles for tiled buffers.
10726 */
2ebef630
TU
10727 stride = fb->pitches[0] /
10728 intel_fb_stride_alignment(dev, fb->modifier[0],
10729 fb->pixel_format);
ff944564
DL
10730
10731 /*
10732 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10733 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10734 */
10735 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10736 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10737
10738 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10739 POSTING_READ(PLANE_SURF(pipe, 0));
10740}
10741
10742static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
10743{
10744 struct drm_device *dev = intel_crtc->base.dev;
10745 struct drm_i915_private *dev_priv = dev->dev_private;
10746 struct intel_framebuffer *intel_fb =
10747 to_intel_framebuffer(intel_crtc->base.primary->fb);
10748 struct drm_i915_gem_object *obj = intel_fb->obj;
10749 u32 dspcntr;
10750 u32 reg;
10751
84c33a64
SG
10752 reg = DSPCNTR(intel_crtc->plane);
10753 dspcntr = I915_READ(reg);
10754
c5d97472
DL
10755 if (obj->tiling_mode != I915_TILING_NONE)
10756 dspcntr |= DISPPLANE_TILED;
10757 else
10758 dspcntr &= ~DISPPLANE_TILED;
10759
84c33a64
SG
10760 I915_WRITE(reg, dspcntr);
10761
10762 I915_WRITE(DSPSURF(intel_crtc->plane),
10763 intel_crtc->unpin_work->gtt_offset);
10764 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 10765
ff944564
DL
10766}
10767
10768/*
10769 * XXX: This is the temporary way to update the plane registers until we get
10770 * around to using the usual plane update functions for MMIO flips
10771 */
10772static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10773{
10774 struct drm_device *dev = intel_crtc->base.dev;
10775 bool atomic_update;
10776 u32 start_vbl_count;
10777
10778 intel_mark_page_flip_active(intel_crtc);
10779
10780 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10781
10782 if (INTEL_INFO(dev)->gen >= 9)
10783 skl_do_mmio_flip(intel_crtc);
10784 else
10785 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10786 ilk_do_mmio_flip(intel_crtc);
10787
9362c7c5
ACO
10788 if (atomic_update)
10789 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
10790}
10791
9362c7c5 10792static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 10793{
cc8c4cc2 10794 struct intel_crtc *crtc =
9362c7c5 10795 container_of(work, struct intel_crtc, mmio_flip.work);
cc8c4cc2 10796 struct intel_mmio_flip *mmio_flip;
84c33a64 10797
cc8c4cc2
JH
10798 mmio_flip = &crtc->mmio_flip;
10799 if (mmio_flip->req)
9c654818
JH
10800 WARN_ON(__i915_wait_request(mmio_flip->req,
10801 crtc->reset_counter,
10802 false, NULL, NULL) != 0);
84c33a64 10803
cc8c4cc2
JH
10804 intel_do_mmio_flip(crtc);
10805 if (mmio_flip->req) {
10806 mutex_lock(&crtc->base.dev->struct_mutex);
146d84f0 10807 i915_gem_request_assign(&mmio_flip->req, NULL);
cc8c4cc2
JH
10808 mutex_unlock(&crtc->base.dev->struct_mutex);
10809 }
84c33a64
SG
10810}
10811
10812static int intel_queue_mmio_flip(struct drm_device *dev,
10813 struct drm_crtc *crtc,
10814 struct drm_framebuffer *fb,
10815 struct drm_i915_gem_object *obj,
10816 struct intel_engine_cs *ring,
10817 uint32_t flags)
10818{
84c33a64 10819 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
84c33a64 10820
cc8c4cc2
JH
10821 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10822 obj->last_write_req);
536f5b5e
ACO
10823
10824 schedule_work(&intel_crtc->mmio_flip.work);
84c33a64 10825
84c33a64
SG
10826 return 0;
10827}
10828
8c9f3aaf
JB
10829static int intel_default_queue_flip(struct drm_device *dev,
10830 struct drm_crtc *crtc,
10831 struct drm_framebuffer *fb,
ed8d1975 10832 struct drm_i915_gem_object *obj,
a4872ba6 10833 struct intel_engine_cs *ring,
ed8d1975 10834 uint32_t flags)
8c9f3aaf
JB
10835{
10836 return -ENODEV;
10837}
10838
d6bbafa1
CW
10839static bool __intel_pageflip_stall_check(struct drm_device *dev,
10840 struct drm_crtc *crtc)
10841{
10842 struct drm_i915_private *dev_priv = dev->dev_private;
10843 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10844 struct intel_unpin_work *work = intel_crtc->unpin_work;
10845 u32 addr;
10846
10847 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10848 return true;
10849
10850 if (!work->enable_stall_check)
10851 return false;
10852
10853 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
10854 if (work->flip_queued_req &&
10855 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
10856 return false;
10857
1e3feefd 10858 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
10859 }
10860
1e3feefd 10861 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
10862 return false;
10863
10864 /* Potential stall - if we see that the flip has happened,
10865 * assume a missed interrupt. */
10866 if (INTEL_INFO(dev)->gen >= 4)
10867 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10868 else
10869 addr = I915_READ(DSPADDR(intel_crtc->plane));
10870
10871 /* There is a potential issue here with a false positive after a flip
10872 * to the same address. We could address this by checking for a
10873 * non-incrementing frame counter.
10874 */
10875 return addr == work->gtt_offset;
10876}
10877
10878void intel_check_page_flip(struct drm_device *dev, int pipe)
10879{
10880 struct drm_i915_private *dev_priv = dev->dev_private;
10881 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 10883 struct intel_unpin_work *work;
f326038a 10884
6c51d46f 10885 WARN_ON(!in_interrupt());
d6bbafa1
CW
10886
10887 if (crtc == NULL)
10888 return;
10889
f326038a 10890 spin_lock(&dev->event_lock);
6ad790c0
CW
10891 work = intel_crtc->unpin_work;
10892 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 10893 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 10894 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 10895 page_flip_completed(intel_crtc);
6ad790c0 10896 work = NULL;
d6bbafa1 10897 }
6ad790c0
CW
10898 if (work != NULL &&
10899 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10900 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 10901 spin_unlock(&dev->event_lock);
d6bbafa1
CW
10902}
10903
6b95a207
KH
10904static int intel_crtc_page_flip(struct drm_crtc *crtc,
10905 struct drm_framebuffer *fb,
ed8d1975
KP
10906 struct drm_pending_vblank_event *event,
10907 uint32_t page_flip_flags)
6b95a207
KH
10908{
10909 struct drm_device *dev = crtc->dev;
10910 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 10911 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 10912 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 10913 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 10914 struct drm_plane *primary = crtc->primary;
a071fa00 10915 enum pipe pipe = intel_crtc->pipe;
6b95a207 10916 struct intel_unpin_work *work;
a4872ba6 10917 struct intel_engine_cs *ring;
cf5d8a46 10918 bool mmio_flip;
52e68630 10919 int ret;
6b95a207 10920
2ff8fde1
MR
10921 /*
10922 * drm_mode_page_flip_ioctl() should already catch this, but double
10923 * check to be safe. In the future we may enable pageflipping from
10924 * a disabled primary plane.
10925 */
10926 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10927 return -EBUSY;
10928
e6a595d2 10929 /* Can't change pixel format via MI display flips. */
f4510a27 10930 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
10931 return -EINVAL;
10932
10933 /*
10934 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10935 * Note that pitch changes could also affect these register.
10936 */
10937 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
10938 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10939 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
10940 return -EINVAL;
10941
f900db47
CW
10942 if (i915_terminally_wedged(&dev_priv->gpu_error))
10943 goto out_hang;
10944
b14c5679 10945 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
10946 if (work == NULL)
10947 return -ENOMEM;
10948
6b95a207 10949 work->event = event;
b4a98e57 10950 work->crtc = crtc;
ab8d6675 10951 work->old_fb = old_fb;
6b95a207
KH
10952 INIT_WORK(&work->work, intel_unpin_work_fn);
10953
87b6b101 10954 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
10955 if (ret)
10956 goto free_work;
10957
6b95a207 10958 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 10959 spin_lock_irq(&dev->event_lock);
6b95a207 10960 if (intel_crtc->unpin_work) {
d6bbafa1
CW
10961 /* Before declaring the flip queue wedged, check if
10962 * the hardware completed the operation behind our backs.
10963 */
10964 if (__intel_pageflip_stall_check(dev, crtc)) {
10965 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10966 page_flip_completed(intel_crtc);
10967 } else {
10968 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 10969 spin_unlock_irq(&dev->event_lock);
468f0b44 10970
d6bbafa1
CW
10971 drm_crtc_vblank_put(crtc);
10972 kfree(work);
10973 return -EBUSY;
10974 }
6b95a207
KH
10975 }
10976 intel_crtc->unpin_work = work;
5e2d7afc 10977 spin_unlock_irq(&dev->event_lock);
6b95a207 10978
b4a98e57
CW
10979 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10980 flush_workqueue(dev_priv->wq);
10981
75dfca80 10982 /* Reference the objects for the scheduled work. */
ab8d6675 10983 drm_framebuffer_reference(work->old_fb);
05394f39 10984 drm_gem_object_reference(&obj->base);
6b95a207 10985
f4510a27 10986 crtc->primary->fb = fb;
afd65eb4 10987 update_state_fb(crtc->primary);
1ed1f968 10988
e1f99ce6 10989 work->pending_flip_obj = obj;
e1f99ce6 10990
89ed88ba
CW
10991 ret = i915_mutex_lock_interruptible(dev);
10992 if (ret)
10993 goto cleanup;
10994
b4a98e57 10995 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 10996 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 10997
75f7f3ec 10998 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 10999 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 11000
4fa62c89
VS
11001 if (IS_VALLEYVIEW(dev)) {
11002 ring = &dev_priv->ring[BCS];
ab8d6675 11003 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
11004 /* vlv: DISPLAY_FLIP fails to change tiling */
11005 ring = NULL;
48bf5b2d 11006 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 11007 ring = &dev_priv->ring[BCS];
4fa62c89 11008 } else if (INTEL_INFO(dev)->gen >= 7) {
41c52415 11009 ring = i915_gem_request_get_ring(obj->last_read_req);
4fa62c89
VS
11010 if (ring == NULL || ring->id != RCS)
11011 ring = &dev_priv->ring[BCS];
11012 } else {
11013 ring = &dev_priv->ring[RCS];
11014 }
11015
cf5d8a46
CW
11016 mmio_flip = use_mmio_flip(ring, obj);
11017
11018 /* When using CS flips, we want to emit semaphores between rings.
11019 * However, when using mmio flips we will create a task to do the
11020 * synchronisation, so all we want here is to pin the framebuffer
11021 * into the display plane and skip any waits.
11022 */
82bc3b2d 11023 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
cf5d8a46
CW
11024 crtc->primary->state,
11025 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
8c9f3aaf
JB
11026 if (ret)
11027 goto cleanup_pending;
6b95a207 11028
121920fa
TU
11029 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11030 + intel_crtc->dspaddr_offset;
4fa62c89 11031
cf5d8a46 11032 if (mmio_flip) {
84c33a64
SG
11033 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11034 page_flip_flags);
d6bbafa1
CW
11035 if (ret)
11036 goto cleanup_unpin;
11037
f06cc1b9
JH
11038 i915_gem_request_assign(&work->flip_queued_req,
11039 obj->last_write_req);
d6bbafa1 11040 } else {
84c33a64 11041 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
11042 page_flip_flags);
11043 if (ret)
11044 goto cleanup_unpin;
11045
f06cc1b9
JH
11046 i915_gem_request_assign(&work->flip_queued_req,
11047 intel_ring_get_request(ring));
d6bbafa1
CW
11048 }
11049
1e3feefd 11050 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11051 work->enable_stall_check = true;
4fa62c89 11052
ab8d6675 11053 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a071fa00
DV
11054 INTEL_FRONTBUFFER_PRIMARY(pipe));
11055
7ff0ebcc 11056 intel_fbc_disable(dev);
f99d7069 11057 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
11058 mutex_unlock(&dev->struct_mutex);
11059
e5510fac
JB
11060 trace_i915_flip_request(intel_crtc->plane, obj);
11061
6b95a207 11062 return 0;
96b099fd 11063
4fa62c89 11064cleanup_unpin:
82bc3b2d 11065 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 11066cleanup_pending:
b4a98e57 11067 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11068 mutex_unlock(&dev->struct_mutex);
11069cleanup:
f4510a27 11070 crtc->primary->fb = old_fb;
afd65eb4 11071 update_state_fb(crtc->primary);
89ed88ba
CW
11072
11073 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11074 drm_framebuffer_unreference(work->old_fb);
96b099fd 11075
5e2d7afc 11076 spin_lock_irq(&dev->event_lock);
96b099fd 11077 intel_crtc->unpin_work = NULL;
5e2d7afc 11078 spin_unlock_irq(&dev->event_lock);
96b099fd 11079
87b6b101 11080 drm_crtc_vblank_put(crtc);
7317c75e 11081free_work:
96b099fd
CW
11082 kfree(work);
11083
f900db47
CW
11084 if (ret == -EIO) {
11085out_hang:
53a366b9 11086 ret = intel_plane_restore(primary);
f0d3dad3 11087 if (ret == 0 && event) {
5e2d7afc 11088 spin_lock_irq(&dev->event_lock);
a071fa00 11089 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11090 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11091 }
f900db47 11092 }
96b099fd 11093 return ret;
6b95a207
KH
11094}
11095
65b38e0d 11096static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11097 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11098 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11099 .atomic_begin = intel_begin_crtc_commit,
11100 .atomic_flush = intel_finish_crtc_commit,
f6e5b160
CW
11101};
11102
9a935856
DV
11103/**
11104 * intel_modeset_update_staged_output_state
11105 *
11106 * Updates the staged output configuration state, e.g. after we've read out the
11107 * current hw state.
11108 */
11109static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 11110{
7668851f 11111 struct intel_crtc *crtc;
9a935856
DV
11112 struct intel_encoder *encoder;
11113 struct intel_connector *connector;
f6e5b160 11114
3a3371ff 11115 for_each_intel_connector(dev, connector) {
9a935856
DV
11116 connector->new_encoder =
11117 to_intel_encoder(connector->base.encoder);
11118 }
f6e5b160 11119
b2784e15 11120 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11121 encoder->new_crtc =
11122 to_intel_crtc(encoder->base.crtc);
11123 }
7668851f 11124
d3fcc808 11125 for_each_intel_crtc(dev, crtc) {
83d65738 11126 crtc->new_enabled = crtc->base.state->enable;
7668851f 11127 }
f6e5b160
CW
11128}
11129
d29b2f9d
ACO
11130/* Transitional helper to copy current connector/encoder state to
11131 * connector->state. This is needed so that code that is partially
11132 * converted to atomic does the right thing.
11133 */
11134static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11135{
11136 struct intel_connector *connector;
11137
11138 for_each_intel_connector(dev, connector) {
11139 if (connector->base.encoder) {
11140 connector->base.state->best_encoder =
11141 connector->base.encoder;
11142 connector->base.state->crtc =
11143 connector->base.encoder->crtc;
11144 } else {
11145 connector->base.state->best_encoder = NULL;
11146 connector->base.state->crtc = NULL;
11147 }
11148 }
11149}
11150
9a935856
DV
11151/**
11152 * intel_modeset_commit_output_state
11153 *
11154 * This function copies the stage display pipe configuration to the real one.
11155 */
11156static void intel_modeset_commit_output_state(struct drm_device *dev)
11157{
7668851f 11158 struct intel_crtc *crtc;
9a935856
DV
11159 struct intel_encoder *encoder;
11160 struct intel_connector *connector;
f6e5b160 11161
3a3371ff 11162 for_each_intel_connector(dev, connector) {
9a935856
DV
11163 connector->base.encoder = &connector->new_encoder->base;
11164 }
f6e5b160 11165
b2784e15 11166 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11167 encoder->base.crtc = &encoder->new_crtc->base;
11168 }
7668851f 11169
d3fcc808 11170 for_each_intel_crtc(dev, crtc) {
83d65738 11171 crtc->base.state->enable = crtc->new_enabled;
7668851f
VS
11172 crtc->base.enabled = crtc->new_enabled;
11173 }
d29b2f9d
ACO
11174
11175 intel_modeset_update_connector_atomic_state(dev);
9a935856
DV
11176}
11177
050f7aeb 11178static void
eba905b2 11179connected_sink_compute_bpp(struct intel_connector *connector,
5cec258b 11180 struct intel_crtc_state *pipe_config)
050f7aeb
DV
11181{
11182 int bpp = pipe_config->pipe_bpp;
11183
11184 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11185 connector->base.base.id,
c23cc417 11186 connector->base.name);
050f7aeb
DV
11187
11188 /* Don't use an invalid EDID bpc value */
11189 if (connector->base.display_info.bpc &&
11190 connector->base.display_info.bpc * 3 < bpp) {
11191 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11192 bpp, connector->base.display_info.bpc*3);
11193 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11194 }
11195
11196 /* Clamp bpp to 8 on screens without EDID 1.4 */
11197 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11198 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11199 bpp);
11200 pipe_config->pipe_bpp = 24;
11201 }
11202}
11203
4e53c2e0 11204static int
050f7aeb 11205compute_baseline_pipe_bpp(struct intel_crtc *crtc,
5cec258b 11206 struct intel_crtc_state *pipe_config)
4e53c2e0 11207{
050f7aeb 11208 struct drm_device *dev = crtc->base.dev;
1486017f 11209 struct drm_atomic_state *state;
da3ced29
ACO
11210 struct drm_connector *connector;
11211 struct drm_connector_state *connector_state;
1486017f 11212 int bpp, i;
4e53c2e0 11213
d328c9d7 11214 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
4e53c2e0 11215 bpp = 10*3;
d328c9d7
DV
11216 else if (INTEL_INFO(dev)->gen >= 5)
11217 bpp = 12*3;
11218 else
11219 bpp = 8*3;
11220
4e53c2e0 11221
4e53c2e0
DV
11222 pipe_config->pipe_bpp = bpp;
11223
1486017f
ACO
11224 state = pipe_config->base.state;
11225
4e53c2e0 11226 /* Clamp display bpp to EDID value */
da3ced29
ACO
11227 for_each_connector_in_state(state, connector, connector_state, i) {
11228 if (connector_state->crtc != &crtc->base)
4e53c2e0
DV
11229 continue;
11230
da3ced29
ACO
11231 connected_sink_compute_bpp(to_intel_connector(connector),
11232 pipe_config);
4e53c2e0
DV
11233 }
11234
11235 return bpp;
11236}
11237
644db711
DV
11238static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11239{
11240 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11241 "type: 0x%x flags: 0x%x\n",
1342830c 11242 mode->crtc_clock,
644db711
DV
11243 mode->crtc_hdisplay, mode->crtc_hsync_start,
11244 mode->crtc_hsync_end, mode->crtc_htotal,
11245 mode->crtc_vdisplay, mode->crtc_vsync_start,
11246 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11247}
11248
c0b03411 11249static void intel_dump_pipe_config(struct intel_crtc *crtc,
5cec258b 11250 struct intel_crtc_state *pipe_config,
c0b03411
DV
11251 const char *context)
11252{
6a60cd87
CK
11253 struct drm_device *dev = crtc->base.dev;
11254 struct drm_plane *plane;
11255 struct intel_plane *intel_plane;
11256 struct intel_plane_state *state;
11257 struct drm_framebuffer *fb;
11258
11259 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11260 context, pipe_config, pipe_name(crtc->pipe));
c0b03411
DV
11261
11262 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11263 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11264 pipe_config->pipe_bpp, pipe_config->dither);
11265 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11266 pipe_config->has_pch_encoder,
11267 pipe_config->fdi_lanes,
11268 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11269 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11270 pipe_config->fdi_m_n.tu);
eb14cb74
VS
11271 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11272 pipe_config->has_dp_encoder,
11273 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11274 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11275 pipe_config->dp_m_n.tu);
b95af8be
VK
11276
11277 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11278 pipe_config->has_dp_encoder,
11279 pipe_config->dp_m2_n2.gmch_m,
11280 pipe_config->dp_m2_n2.gmch_n,
11281 pipe_config->dp_m2_n2.link_m,
11282 pipe_config->dp_m2_n2.link_n,
11283 pipe_config->dp_m2_n2.tu);
11284
55072d19
DV
11285 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11286 pipe_config->has_audio,
11287 pipe_config->has_infoframe);
11288
c0b03411 11289 DRM_DEBUG_KMS("requested mode:\n");
2d112de7 11290 drm_mode_debug_printmodeline(&pipe_config->base.mode);
c0b03411 11291 DRM_DEBUG_KMS("adjusted mode:\n");
2d112de7
ACO
11292 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11293 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
d71b8d4a 11294 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
11295 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11296 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
6a60cd87
CK
11297 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
11298 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
11299 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
c0b03411
DV
11300 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11301 pipe_config->gmch_pfit.control,
11302 pipe_config->gmch_pfit.pgm_ratios,
11303 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 11304 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 11305 pipe_config->pch_pfit.pos,
fd4daa9c
CW
11306 pipe_config->pch_pfit.size,
11307 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 11308 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 11309 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87
CK
11310
11311 DRM_DEBUG_KMS("planes on this crtc\n");
11312 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11313 intel_plane = to_intel_plane(plane);
11314 if (intel_plane->pipe != crtc->pipe)
11315 continue;
11316
11317 state = to_intel_plane_state(plane->state);
11318 fb = state->base.fb;
11319 if (!fb) {
11320 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11321 "disabled, scaler_id = %d\n",
11322 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11323 plane->base.id, intel_plane->pipe,
11324 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11325 drm_plane_index(plane), state->scaler_id);
11326 continue;
11327 }
11328
11329 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11330 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11331 plane->base.id, intel_plane->pipe,
11332 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11333 drm_plane_index(plane));
11334 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11335 fb->base.id, fb->width, fb->height, fb->pixel_format);
11336 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11337 state->scaler_id,
11338 state->src.x1 >> 16, state->src.y1 >> 16,
11339 drm_rect_width(&state->src) >> 16,
11340 drm_rect_height(&state->src) >> 16,
11341 state->dst.x1, state->dst.y1,
11342 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11343 }
c0b03411
DV
11344}
11345
bc079e8b
VS
11346static bool encoders_cloneable(const struct intel_encoder *a,
11347 const struct intel_encoder *b)
accfc0c5 11348{
bc079e8b
VS
11349 /* masks could be asymmetric, so check both ways */
11350 return a == b || (a->cloneable & (1 << b->type) &&
11351 b->cloneable & (1 << a->type));
11352}
11353
98a221da
ACO
11354static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11355 struct intel_crtc *crtc,
bc079e8b
VS
11356 struct intel_encoder *encoder)
11357{
bc079e8b 11358 struct intel_encoder *source_encoder;
da3ced29 11359 struct drm_connector *connector;
98a221da
ACO
11360 struct drm_connector_state *connector_state;
11361 int i;
bc079e8b 11362
da3ced29 11363 for_each_connector_in_state(state, connector, connector_state, i) {
98a221da 11364 if (connector_state->crtc != &crtc->base)
bc079e8b
VS
11365 continue;
11366
98a221da
ACO
11367 source_encoder =
11368 to_intel_encoder(connector_state->best_encoder);
bc079e8b
VS
11369 if (!encoders_cloneable(encoder, source_encoder))
11370 return false;
11371 }
11372
11373 return true;
11374}
11375
98a221da
ACO
11376static bool check_encoder_cloning(struct drm_atomic_state *state,
11377 struct intel_crtc *crtc)
bc079e8b 11378{
accfc0c5 11379 struct intel_encoder *encoder;
da3ced29 11380 struct drm_connector *connector;
98a221da
ACO
11381 struct drm_connector_state *connector_state;
11382 int i;
accfc0c5 11383
da3ced29 11384 for_each_connector_in_state(state, connector, connector_state, i) {
98a221da
ACO
11385 if (connector_state->crtc != &crtc->base)
11386 continue;
11387
11388 encoder = to_intel_encoder(connector_state->best_encoder);
11389 if (!check_single_encoder_cloning(state, crtc, encoder))
bc079e8b 11390 return false;
accfc0c5
DV
11391 }
11392
bc079e8b 11393 return true;
accfc0c5
DV
11394}
11395
5448a00d 11396static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11397{
5448a00d
ACO
11398 struct drm_device *dev = state->dev;
11399 struct intel_encoder *encoder;
da3ced29 11400 struct drm_connector *connector;
5448a00d 11401 struct drm_connector_state *connector_state;
00f0b378 11402 unsigned int used_ports = 0;
5448a00d 11403 int i;
00f0b378
VS
11404
11405 /*
11406 * Walk the connector list instead of the encoder
11407 * list to detect the problem on ddi platforms
11408 * where there's just one encoder per digital port.
11409 */
da3ced29 11410 for_each_connector_in_state(state, connector, connector_state, i) {
5448a00d 11411 if (!connector_state->best_encoder)
00f0b378
VS
11412 continue;
11413
5448a00d
ACO
11414 encoder = to_intel_encoder(connector_state->best_encoder);
11415
11416 WARN_ON(!connector_state->crtc);
00f0b378
VS
11417
11418 switch (encoder->type) {
11419 unsigned int port_mask;
11420 case INTEL_OUTPUT_UNKNOWN:
11421 if (WARN_ON(!HAS_DDI(dev)))
11422 break;
11423 case INTEL_OUTPUT_DISPLAYPORT:
11424 case INTEL_OUTPUT_HDMI:
11425 case INTEL_OUTPUT_EDP:
11426 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11427
11428 /* the same port mustn't appear more than once */
11429 if (used_ports & port_mask)
11430 return false;
11431
11432 used_ports |= port_mask;
11433 default:
11434 break;
11435 }
11436 }
11437
11438 return true;
11439}
11440
83a57153
ACO
11441static void
11442clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11443{
11444 struct drm_crtc_state tmp_state;
663a3640 11445 struct intel_crtc_scaler_state scaler_state;
83a57153 11446
663a3640 11447 /* Clear only the intel specific part of the crtc state excluding scalers */
83a57153 11448 tmp_state = crtc_state->base;
663a3640 11449 scaler_state = crtc_state->scaler_state;
83a57153
ACO
11450 memset(crtc_state, 0, sizeof *crtc_state);
11451 crtc_state->base = tmp_state;
663a3640 11452 crtc_state->scaler_state = scaler_state;
83a57153
ACO
11453}
11454
548ee15b 11455static int
b8cecdf5 11456intel_modeset_pipe_config(struct drm_crtc *crtc,
83a57153 11457 struct drm_display_mode *mode,
548ee15b
ACO
11458 struct drm_atomic_state *state,
11459 struct intel_crtc_state *pipe_config)
ee7b9f93 11460{
7758a113 11461 struct intel_encoder *encoder;
da3ced29 11462 struct drm_connector *connector;
0b901879 11463 struct drm_connector_state *connector_state;
d328c9d7 11464 int base_bpp, ret = -EINVAL;
0b901879 11465 int i;
e29c22c0 11466 bool retry = true;
ee7b9f93 11467
98a221da 11468 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
accfc0c5 11469 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
548ee15b 11470 return -EINVAL;
accfc0c5
DV
11471 }
11472
5448a00d 11473 if (!check_digital_port_conflicts(state)) {
00f0b378 11474 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
548ee15b 11475 return -EINVAL;
00f0b378
VS
11476 }
11477
83a57153 11478 clear_intel_crtc_state(pipe_config);
7758a113 11479
07878248 11480 pipe_config->base.crtc = crtc;
2d112de7
ACO
11481 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
11482 drm_mode_copy(&pipe_config->base.mode, mode);
37327abd 11483
e143a21c
DV
11484 pipe_config->cpu_transcoder =
11485 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 11486 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 11487
2960bc9c
ID
11488 /*
11489 * Sanitize sync polarity flags based on requested ones. If neither
11490 * positive or negative polarity is requested, treat this as meaning
11491 * negative polarity.
11492 */
2d112de7 11493 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11494 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 11495 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 11496
2d112de7 11497 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11498 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 11499 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 11500
050f7aeb
DV
11501 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11502 * plane pixel format and any sink constraints into account. Returns the
11503 * source plane bpp so that dithering can be selected on mismatches
11504 * after encoders and crtc also have had their say. */
d328c9d7
DV
11505 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11506 pipe_config);
11507 if (base_bpp < 0)
4e53c2e0
DV
11508 goto fail;
11509
e41a56be
VS
11510 /*
11511 * Determine the real pipe dimensions. Note that stereo modes can
11512 * increase the actual pipe size due to the frame doubling and
11513 * insertion of additional space for blanks between the frame. This
11514 * is stored in the crtc timings. We use the requested mode to do this
11515 * computation to clearly distinguish it from the adjusted mode, which
11516 * can be changed by the connectors in the below retry loop.
11517 */
2d112de7 11518 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
11519 &pipe_config->pipe_src_w,
11520 &pipe_config->pipe_src_h);
e41a56be 11521
e29c22c0 11522encoder_retry:
ef1b460d 11523 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 11524 pipe_config->port_clock = 0;
ef1b460d 11525 pipe_config->pixel_multiplier = 1;
ff9a6750 11526
135c81b8 11527 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
11528 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11529 CRTC_STEREO_DOUBLE);
135c81b8 11530
7758a113
DV
11531 /* Pass our mode to the connectors and the CRTC to give them a chance to
11532 * adjust it according to limitations or connector properties, and also
11533 * a chance to reject the mode entirely.
47f1c6c9 11534 */
da3ced29 11535 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 11536 if (connector_state->crtc != crtc)
7758a113 11537 continue;
7ae89233 11538
0b901879
ACO
11539 encoder = to_intel_encoder(connector_state->best_encoder);
11540
efea6e8e
DV
11541 if (!(encoder->compute_config(encoder, pipe_config))) {
11542 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
11543 goto fail;
11544 }
ee7b9f93 11545 }
47f1c6c9 11546
ff9a6750
DV
11547 /* Set default port clock if not overwritten by the encoder. Needs to be
11548 * done afterwards in case the encoder adjusts the mode. */
11549 if (!pipe_config->port_clock)
2d112de7 11550 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 11551 * pipe_config->pixel_multiplier;
ff9a6750 11552
a43f6e0f 11553 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 11554 if (ret < 0) {
7758a113
DV
11555 DRM_DEBUG_KMS("CRTC fixup failed\n");
11556 goto fail;
ee7b9f93 11557 }
e29c22c0
DV
11558
11559 if (ret == RETRY) {
11560 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11561 ret = -EINVAL;
11562 goto fail;
11563 }
11564
11565 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11566 retry = false;
11567 goto encoder_retry;
11568 }
11569
d328c9d7 11570 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
4e53c2e0 11571 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 11572 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 11573
548ee15b 11574 return 0;
7758a113 11575fail:
548ee15b 11576 return ret;
ee7b9f93 11577}
47f1c6c9 11578
ea9d758d 11579static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 11580{
ea9d758d 11581 struct drm_encoder *encoder;
f6e5b160 11582 struct drm_device *dev = crtc->dev;
f6e5b160 11583
ea9d758d
DV
11584 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11585 if (encoder->crtc == crtc)
11586 return true;
11587
11588 return false;
11589}
11590
0a9ab303
ACO
11591static bool
11592needs_modeset(struct drm_crtc_state *state)
11593{
11594 return state->mode_changed || state->active_changed;
11595}
11596
ea9d758d 11597static void
0a9ab303 11598intel_modeset_update_state(struct drm_atomic_state *state)
ea9d758d 11599{
0a9ab303 11600 struct drm_device *dev = state->dev;
ba41c0de 11601 struct drm_i915_private *dev_priv = dev->dev_private;
ea9d758d 11602 struct intel_encoder *intel_encoder;
0a9ab303
ACO
11603 struct drm_crtc *crtc;
11604 struct drm_crtc_state *crtc_state;
ea9d758d 11605 struct drm_connector *connector;
0a9ab303 11606 int i;
ea9d758d 11607
ba41c0de
DV
11608 intel_shared_dpll_commit(dev_priv);
11609
b2784e15 11610 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
11611 if (!intel_encoder->base.crtc)
11612 continue;
11613
0a9ab303
ACO
11614 for_each_crtc_in_state(state, crtc, crtc_state, i)
11615 if (crtc == intel_encoder->base.crtc)
11616 break;
11617
11618 if (crtc != intel_encoder->base.crtc)
11619 continue;
ea9d758d 11620
0a9ab303 11621 if (crtc_state->enable && needs_modeset(crtc_state))
ea9d758d
DV
11622 intel_encoder->connectors_active = false;
11623 }
11624
11625 intel_modeset_commit_output_state(dev);
11626
7668851f 11627 /* Double check state. */
0a9ab303
ACO
11628 for_each_crtc(dev, crtc) {
11629 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
ea9d758d
DV
11630 }
11631
11632 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11633 if (!connector->encoder || !connector->encoder->crtc)
11634 continue;
11635
0a9ab303
ACO
11636 for_each_crtc_in_state(state, crtc, crtc_state, i)
11637 if (crtc == connector->encoder->crtc)
11638 break;
11639
11640 if (crtc != connector->encoder->crtc)
11641 continue;
ea9d758d 11642
0a9ab303 11643 if (crtc_state->enable && needs_modeset(crtc_state)) {
68d34720
DV
11644 struct drm_property *dpms_property =
11645 dev->mode_config.dpms_property;
11646
ea9d758d 11647 connector->dpms = DRM_MODE_DPMS_ON;
662595df 11648 drm_object_property_set_value(&connector->base,
68d34720
DV
11649 dpms_property,
11650 DRM_MODE_DPMS_ON);
ea9d758d
DV
11651
11652 intel_encoder = to_intel_encoder(connector->encoder);
11653 intel_encoder->connectors_active = true;
11654 }
11655 }
11656
11657}
11658
3bd26263 11659static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 11660{
3bd26263 11661 int diff;
f1f644dc
JB
11662
11663 if (clock1 == clock2)
11664 return true;
11665
11666 if (!clock1 || !clock2)
11667 return false;
11668
11669 diff = abs(clock1 - clock2);
11670
11671 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11672 return true;
11673
11674 return false;
11675}
11676
25c5b266
DV
11677#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11678 list_for_each_entry((intel_crtc), \
11679 &(dev)->mode_config.crtc_list, \
11680 base.head) \
0973f18f 11681 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 11682
0e8ffe1b 11683static bool
2fa2fe9a 11684intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
11685 struct intel_crtc_state *current_config,
11686 struct intel_crtc_state *pipe_config)
0e8ffe1b 11687{
66e985c0
DV
11688#define PIPE_CONF_CHECK_X(name) \
11689 if (current_config->name != pipe_config->name) { \
11690 DRM_ERROR("mismatch in " #name " " \
11691 "(expected 0x%08x, found 0x%08x)\n", \
11692 current_config->name, \
11693 pipe_config->name); \
11694 return false; \
11695 }
11696
08a24034
DV
11697#define PIPE_CONF_CHECK_I(name) \
11698 if (current_config->name != pipe_config->name) { \
11699 DRM_ERROR("mismatch in " #name " " \
11700 "(expected %i, found %i)\n", \
11701 current_config->name, \
11702 pipe_config->name); \
11703 return false; \
88adfff1
DV
11704 }
11705
b95af8be
VK
11706/* This is required for BDW+ where there is only one set of registers for
11707 * switching between high and low RR.
11708 * This macro can be used whenever a comparison has to be made between one
11709 * hw state and multiple sw state variables.
11710 */
11711#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11712 if ((current_config->name != pipe_config->name) && \
11713 (current_config->alt_name != pipe_config->name)) { \
11714 DRM_ERROR("mismatch in " #name " " \
11715 "(expected %i or %i, found %i)\n", \
11716 current_config->name, \
11717 current_config->alt_name, \
11718 pipe_config->name); \
11719 return false; \
11720 }
11721
1bd1bd80
DV
11722#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11723 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 11724 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
11725 "(expected %i, found %i)\n", \
11726 current_config->name & (mask), \
11727 pipe_config->name & (mask)); \
11728 return false; \
11729 }
11730
5e550656
VS
11731#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11732 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11733 DRM_ERROR("mismatch in " #name " " \
11734 "(expected %i, found %i)\n", \
11735 current_config->name, \
11736 pipe_config->name); \
11737 return false; \
11738 }
11739
bb760063
DV
11740#define PIPE_CONF_QUIRK(quirk) \
11741 ((current_config->quirks | pipe_config->quirks) & (quirk))
11742
eccb140b
DV
11743 PIPE_CONF_CHECK_I(cpu_transcoder);
11744
08a24034
DV
11745 PIPE_CONF_CHECK_I(has_pch_encoder);
11746 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
11747 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11748 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11749 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11750 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11751 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 11752
eb14cb74 11753 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
11754
11755 if (INTEL_INFO(dev)->gen < 8) {
11756 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11757 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11758 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11759 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11760 PIPE_CONF_CHECK_I(dp_m_n.tu);
11761
11762 if (current_config->has_drrs) {
11763 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11764 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11765 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11766 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11767 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11768 }
11769 } else {
11770 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11771 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11772 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11773 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11774 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11775 }
eb14cb74 11776
2d112de7
ACO
11777 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11778 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11779 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11780 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11781 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11782 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11783
2d112de7
ACO
11784 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11785 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11786 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11787 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11788 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11789 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11790
c93f54cf 11791 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 11792 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
11793 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11794 IS_VALLEYVIEW(dev))
11795 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 11796 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 11797
9ed109a7
DV
11798 PIPE_CONF_CHECK_I(has_audio);
11799
2d112de7 11800 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
11801 DRM_MODE_FLAG_INTERLACE);
11802
bb760063 11803 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11804 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11805 DRM_MODE_FLAG_PHSYNC);
2d112de7 11806 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11807 DRM_MODE_FLAG_NHSYNC);
2d112de7 11808 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11809 DRM_MODE_FLAG_PVSYNC);
2d112de7 11810 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
11811 DRM_MODE_FLAG_NVSYNC);
11812 }
045ac3b5 11813
37327abd
VS
11814 PIPE_CONF_CHECK_I(pipe_src_w);
11815 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 11816
9953599b
DV
11817 /*
11818 * FIXME: BIOS likes to set up a cloned config with lvds+external
11819 * screen. Since we don't yet re-compute the pipe config when moving
11820 * just the lvds port away to another pipe the sw tracking won't match.
11821 *
11822 * Proper atomic modesets with recomputed global state will fix this.
11823 * Until then just don't check gmch state for inherited modes.
11824 */
11825 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11826 PIPE_CONF_CHECK_I(gmch_pfit.control);
11827 /* pfit ratios are autocomputed by the hw on gen4+ */
11828 if (INTEL_INFO(dev)->gen < 4)
11829 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11830 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11831 }
11832
fd4daa9c
CW
11833 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11834 if (current_config->pch_pfit.enabled) {
11835 PIPE_CONF_CHECK_I(pch_pfit.pos);
11836 PIPE_CONF_CHECK_I(pch_pfit.size);
11837 }
2fa2fe9a 11838
a1b2278e
CK
11839 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11840
e59150dc
JB
11841 /* BDW+ don't expose a synchronous way to read the state */
11842 if (IS_HASWELL(dev))
11843 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 11844
282740f7
VS
11845 PIPE_CONF_CHECK_I(double_wide);
11846
26804afd
DV
11847 PIPE_CONF_CHECK_X(ddi_pll_sel);
11848
c0d43d62 11849 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 11850 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11851 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
11852 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11853 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11854 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
11855 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11856 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11857 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 11858
42571aef
VS
11859 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11860 PIPE_CONF_CHECK_I(pipe_bpp);
11861
2d112de7 11862 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 11863 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 11864
66e985c0 11865#undef PIPE_CONF_CHECK_X
08a24034 11866#undef PIPE_CONF_CHECK_I
b95af8be 11867#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 11868#undef PIPE_CONF_CHECK_FLAGS
5e550656 11869#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 11870#undef PIPE_CONF_QUIRK
88adfff1 11871
0e8ffe1b
DV
11872 return true;
11873}
11874
08db6652
DL
11875static void check_wm_state(struct drm_device *dev)
11876{
11877 struct drm_i915_private *dev_priv = dev->dev_private;
11878 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11879 struct intel_crtc *intel_crtc;
11880 int plane;
11881
11882 if (INTEL_INFO(dev)->gen < 9)
11883 return;
11884
11885 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11886 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11887
11888 for_each_intel_crtc(dev, intel_crtc) {
11889 struct skl_ddb_entry *hw_entry, *sw_entry;
11890 const enum pipe pipe = intel_crtc->pipe;
11891
11892 if (!intel_crtc->active)
11893 continue;
11894
11895 /* planes */
dd740780 11896 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
11897 hw_entry = &hw_ddb.plane[pipe][plane];
11898 sw_entry = &sw_ddb->plane[pipe][plane];
11899
11900 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11901 continue;
11902
11903 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11904 "(expected (%u,%u), found (%u,%u))\n",
11905 pipe_name(pipe), plane + 1,
11906 sw_entry->start, sw_entry->end,
11907 hw_entry->start, hw_entry->end);
11908 }
11909
11910 /* cursor */
11911 hw_entry = &hw_ddb.cursor[pipe];
11912 sw_entry = &sw_ddb->cursor[pipe];
11913
11914 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11915 continue;
11916
11917 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11918 "(expected (%u,%u), found (%u,%u))\n",
11919 pipe_name(pipe),
11920 sw_entry->start, sw_entry->end,
11921 hw_entry->start, hw_entry->end);
11922 }
11923}
11924
91d1b4bd
DV
11925static void
11926check_connector_state(struct drm_device *dev)
8af6cf88 11927{
8af6cf88
DV
11928 struct intel_connector *connector;
11929
3a3371ff 11930 for_each_intel_connector(dev, connector) {
8af6cf88
DV
11931 /* This also checks the encoder/connector hw state with the
11932 * ->get_hw_state callbacks. */
11933 intel_connector_check_state(connector);
11934
e2c719b7 11935 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
8af6cf88
DV
11936 "connector's staged encoder doesn't match current encoder\n");
11937 }
91d1b4bd
DV
11938}
11939
11940static void
11941check_encoder_state(struct drm_device *dev)
11942{
11943 struct intel_encoder *encoder;
11944 struct intel_connector *connector;
8af6cf88 11945
b2784e15 11946 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
11947 bool enabled = false;
11948 bool active = false;
11949 enum pipe pipe, tracked_pipe;
11950
11951 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11952 encoder->base.base.id,
8e329a03 11953 encoder->base.name);
8af6cf88 11954
e2c719b7 11955 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
8af6cf88 11956 "encoder's stage crtc doesn't match current crtc\n");
e2c719b7 11957 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
11958 "encoder's active_connectors set, but no crtc\n");
11959
3a3371ff 11960 for_each_intel_connector(dev, connector) {
8af6cf88
DV
11961 if (connector->base.encoder != &encoder->base)
11962 continue;
11963 enabled = true;
11964 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11965 active = true;
11966 }
0e32b39c
DA
11967 /*
11968 * for MST connectors if we unplug the connector is gone
11969 * away but the encoder is still connected to a crtc
11970 * until a modeset happens in response to the hotplug.
11971 */
11972 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
11973 continue;
11974
e2c719b7 11975 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
11976 "encoder's enabled state mismatch "
11977 "(expected %i, found %i)\n",
11978 !!encoder->base.crtc, enabled);
e2c719b7 11979 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
11980 "active encoder with no crtc\n");
11981
e2c719b7 11982 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
11983 "encoder's computed active state doesn't match tracked active state "
11984 "(expected %i, found %i)\n", active, encoder->connectors_active);
11985
11986 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 11987 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
11988 "encoder's hw state doesn't match sw tracking "
11989 "(expected %i, found %i)\n",
11990 encoder->connectors_active, active);
11991
11992 if (!encoder->base.crtc)
11993 continue;
11994
11995 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 11996 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
11997 "active encoder's pipe doesn't match"
11998 "(expected %i, found %i)\n",
11999 tracked_pipe, pipe);
12000
12001 }
91d1b4bd
DV
12002}
12003
12004static void
12005check_crtc_state(struct drm_device *dev)
12006{
fbee40df 12007 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12008 struct intel_crtc *crtc;
12009 struct intel_encoder *encoder;
5cec258b 12010 struct intel_crtc_state pipe_config;
8af6cf88 12011
d3fcc808 12012 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
12013 bool enabled = false;
12014 bool active = false;
12015
045ac3b5
JB
12016 memset(&pipe_config, 0, sizeof(pipe_config));
12017
8af6cf88
DV
12018 DRM_DEBUG_KMS("[CRTC:%d]\n",
12019 crtc->base.base.id);
12020
83d65738 12021 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
8af6cf88
DV
12022 "active crtc, but not enabled in sw tracking\n");
12023
b2784e15 12024 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12025 if (encoder->base.crtc != &crtc->base)
12026 continue;
12027 enabled = true;
12028 if (encoder->connectors_active)
12029 active = true;
12030 }
6c49f241 12031
e2c719b7 12032 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
12033 "crtc's computed active state doesn't match tracked active state "
12034 "(expected %i, found %i)\n", active, crtc->active);
83d65738 12035 I915_STATE_WARN(enabled != crtc->base.state->enable,
8af6cf88 12036 "crtc's computed enabled state doesn't match tracked enabled state "
83d65738
MR
12037 "(expected %i, found %i)\n", enabled,
12038 crtc->base.state->enable);
8af6cf88 12039
0e8ffe1b
DV
12040 active = dev_priv->display.get_pipe_config(crtc,
12041 &pipe_config);
d62cf62a 12042
b6b5d049
VS
12043 /* hw state is inconsistent with the pipe quirk */
12044 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12045 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
12046 active = crtc->active;
12047
b2784e15 12048 for_each_intel_encoder(dev, encoder) {
3eaba51c 12049 enum pipe pipe;
6c49f241
DV
12050 if (encoder->base.crtc != &crtc->base)
12051 continue;
1d37b689 12052 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
12053 encoder->get_config(encoder, &pipe_config);
12054 }
12055
e2c719b7 12056 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
12057 "crtc active state doesn't match with hw state "
12058 "(expected %i, found %i)\n", crtc->active, active);
12059
c0b03411 12060 if (active &&
6e3c9717 12061 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 12062 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
12063 intel_dump_pipe_config(crtc, &pipe_config,
12064 "[hw state]");
6e3c9717 12065 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
12066 "[sw state]");
12067 }
8af6cf88
DV
12068 }
12069}
12070
91d1b4bd
DV
12071static void
12072check_shared_dpll_state(struct drm_device *dev)
12073{
fbee40df 12074 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12075 struct intel_crtc *crtc;
12076 struct intel_dpll_hw_state dpll_hw_state;
12077 int i;
5358901f
DV
12078
12079 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12080 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12081 int enabled_crtcs = 0, active_crtcs = 0;
12082 bool active;
12083
12084 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12085
12086 DRM_DEBUG_KMS("%s\n", pll->name);
12087
12088 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12089
e2c719b7 12090 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12091 "more active pll users than references: %i vs %i\n",
3e369b76 12092 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12093 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12094 "pll in active use but not on in sw tracking\n");
e2c719b7 12095 I915_STATE_WARN(pll->on && !pll->active,
35c95375 12096 "pll in on but not on in use in sw tracking\n");
e2c719b7 12097 I915_STATE_WARN(pll->on != active,
5358901f
DV
12098 "pll on state mismatch (expected %i, found %i)\n",
12099 pll->on, active);
12100
d3fcc808 12101 for_each_intel_crtc(dev, crtc) {
83d65738 12102 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
12103 enabled_crtcs++;
12104 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12105 active_crtcs++;
12106 }
e2c719b7 12107 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
12108 "pll active crtcs mismatch (expected %i, found %i)\n",
12109 pll->active, active_crtcs);
e2c719b7 12110 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 12111 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 12112 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 12113
e2c719b7 12114 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12115 sizeof(dpll_hw_state)),
12116 "pll hw state mismatch\n");
5358901f 12117 }
8af6cf88
DV
12118}
12119
91d1b4bd
DV
12120void
12121intel_modeset_check_state(struct drm_device *dev)
12122{
08db6652 12123 check_wm_state(dev);
91d1b4bd
DV
12124 check_connector_state(dev);
12125 check_encoder_state(dev);
12126 check_crtc_state(dev);
12127 check_shared_dpll_state(dev);
12128}
12129
5cec258b 12130void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
12131 int dotclock)
12132{
12133 /*
12134 * FDI already provided one idea for the dotclock.
12135 * Yell if the encoder disagrees.
12136 */
2d112de7 12137 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 12138 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 12139 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
12140}
12141
80715b2f
VS
12142static void update_scanline_offset(struct intel_crtc *crtc)
12143{
12144 struct drm_device *dev = crtc->base.dev;
12145
12146 /*
12147 * The scanline counter increments at the leading edge of hsync.
12148 *
12149 * On most platforms it starts counting from vtotal-1 on the
12150 * first active line. That means the scanline counter value is
12151 * always one less than what we would expect. Ie. just after
12152 * start of vblank, which also occurs at start of hsync (on the
12153 * last active line), the scanline counter will read vblank_start-1.
12154 *
12155 * On gen2 the scanline counter starts counting from 1 instead
12156 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12157 * to keep the value positive), instead of adding one.
12158 *
12159 * On HSW+ the behaviour of the scanline counter depends on the output
12160 * type. For DP ports it behaves like most other platforms, but on HDMI
12161 * there's an extra 1 line difference. So we need to add two instead of
12162 * one to the value.
12163 */
12164 if (IS_GEN2(dev)) {
6e3c9717 12165 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12166 int vtotal;
12167
12168 vtotal = mode->crtc_vtotal;
12169 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12170 vtotal /= 2;
12171
12172 crtc->scanline_offset = vtotal - 1;
12173 } else if (HAS_DDI(dev) &&
409ee761 12174 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12175 crtc->scanline_offset = 2;
12176 } else
12177 crtc->scanline_offset = 1;
12178}
12179
0a9ab303
ACO
12180static void
12181intel_atomic_modeset_compute_changed_flags(struct drm_atomic_state *state,
12182 struct drm_crtc *modeset_crtc)
12183{
12184 struct drm_crtc_state *crtc_state;
12185 struct drm_crtc *crtc;
12186 int i;
12187
12188 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12189 if (crtc_state->enable != crtc->state->enable)
12190 crtc_state->mode_changed = true;
12191
12192 /* FIXME: Do we need to always set mode_changed for
12193 * modeset_crtc if it is enabled? modeset_affect_pipes()
12194 * did that. */
12195 }
12196}
12197
5cec258b 12198static struct intel_crtc_state *
7f27126e
JB
12199intel_modeset_compute_config(struct drm_crtc *crtc,
12200 struct drm_display_mode *mode,
0a9ab303 12201 struct drm_atomic_state *state)
7f27126e 12202{
548ee15b 12203 struct intel_crtc_state *pipe_config;
0b901879
ACO
12204 int ret = 0;
12205
12206 ret = drm_atomic_add_affected_connectors(state, crtc);
12207 if (ret)
12208 return ERR_PTR(ret);
7f27126e 12209
0a9ab303 12210 intel_atomic_modeset_compute_changed_flags(state, crtc);
7f27126e 12211
7f27126e
JB
12212 /*
12213 * Note this needs changes when we start tracking multiple modes
12214 * and crtcs. At that point we'll need to compute the whole config
12215 * (i.e. one pipe_config for each crtc) rather than just the one
12216 * for this crtc.
12217 */
548ee15b
ACO
12218 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12219 if (IS_ERR(pipe_config))
12220 return pipe_config;
83a57153 12221
4fed33f6 12222 if (!pipe_config->base.enable)
548ee15b 12223 return pipe_config;
7f27126e 12224
548ee15b
ACO
12225 ret = intel_modeset_pipe_config(crtc, mode, state, pipe_config);
12226 if (ret)
12227 return ERR_PTR(ret);
12228
8d8c9b51
ACO
12229 /* Check things that can only be changed through modeset */
12230 if (pipe_config->has_audio !=
12231 to_intel_crtc(crtc)->config->has_audio)
12232 pipe_config->base.mode_changed = true;
12233
12234 /*
12235 * Note we have an issue here with infoframes: current code
12236 * only updates them on the full mode set path per hw
12237 * requirements. So here we should be checking for any
12238 * required changes and forcing a mode set.
12239 */
12240
548ee15b 12241 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,"[modeset]");
db7542dd 12242
548ee15b 12243 return pipe_config;
7f27126e
JB
12244}
12245
0a9ab303 12246static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
ed6739ef 12247{
225da59b 12248 struct drm_device *dev = state->dev;
ed6739ef 12249 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303 12250 unsigned clear_pipes = 0;
ed6739ef 12251 struct intel_crtc *intel_crtc;
0a9ab303
ACO
12252 struct intel_crtc_state *intel_crtc_state;
12253 struct drm_crtc *crtc;
12254 struct drm_crtc_state *crtc_state;
ed6739ef 12255 int ret = 0;
0a9ab303 12256 int i;
ed6739ef
ACO
12257
12258 if (!dev_priv->display.crtc_compute_clock)
12259 return 0;
12260
0a9ab303
ACO
12261 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12262 intel_crtc = to_intel_crtc(crtc);
12263
12264 if (needs_modeset(crtc_state))
12265 clear_pipes |= 1 << intel_crtc->pipe;
12266 }
12267
ed6739ef
ACO
12268 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12269 if (ret)
12270 goto done;
12271
0a9ab303
ACO
12272 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12273 if (!needs_modeset(crtc_state) || !crtc_state->enable)
225da59b
ACO
12274 continue;
12275
0a9ab303
ACO
12276 intel_crtc = to_intel_crtc(crtc);
12277 intel_crtc_state = to_intel_crtc_state(crtc_state);
12278
ed6739ef 12279 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
0a9ab303 12280 intel_crtc_state);
ed6739ef
ACO
12281 if (ret) {
12282 intel_shared_dpll_abort_config(dev_priv);
12283 goto done;
12284 }
12285 }
12286
12287done:
12288 return ret;
12289}
12290
054518dd
ACO
12291/* Code that should eventually be part of atomic_check() */
12292static int __intel_set_mode_checks(struct drm_atomic_state *state)
12293{
12294 struct drm_device *dev = state->dev;
12295 int ret;
12296
12297 /*
12298 * See if the config requires any additional preparation, e.g.
12299 * to adjust global state with pipes off. We need to do this
12300 * here so we can get the modeset_pipe updated config for the new
12301 * mode set on this crtc. For other crtcs we need to use the
12302 * adjusted_mode bits in the crtc directly.
12303 */
12304 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
12305 ret = valleyview_modeset_global_pipes(state);
12306 if (ret)
12307 return ret;
12308 }
12309
12310 ret = __intel_set_mode_setup_plls(state);
12311 if (ret)
12312 return ret;
12313
12314 return 0;
12315}
12316
0a9ab303 12317static int __intel_set_mode(struct drm_crtc *modeset_crtc,
f30da187 12318 struct drm_display_mode *mode,
0a9ab303 12319 struct intel_crtc_state *pipe_config)
a6778b3c 12320{
0a9ab303 12321 struct drm_device *dev = modeset_crtc->dev;
fbee40df 12322 struct drm_i915_private *dev_priv = dev->dev_private;
304603f4 12323 struct drm_atomic_state *state = pipe_config->base.state;
83a57153 12324 struct intel_crtc_state *crtc_state_copy = NULL;
25c5b266 12325 struct intel_crtc *intel_crtc;
0a9ab303
ACO
12326 struct drm_crtc *crtc;
12327 struct drm_crtc_state *crtc_state;
d3a40d1b
ACO
12328 struct drm_plane *plane;
12329 struct drm_plane_state *plane_state;
c0c36b94 12330 int ret = 0;
0a9ab303 12331 int i;
a6778b3c 12332
054518dd
ACO
12333 ret = __intel_set_mode_checks(state);
12334 if (ret < 0)
12335 return ret;
12336
83a57153 12337 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
9eb45f22
ACO
12338 if (!crtc_state_copy)
12339 return -ENOMEM;
83a57153 12340
0a9ab303
ACO
12341 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12342 if (!needs_modeset(crtc_state))
12343 continue;
460da916 12344
0a9ab303
ACO
12345 if (!crtc_state->enable) {
12346 intel_crtc_disable(crtc);
12347 } else if (crtc->state->enable) {
12348 intel_crtc_disable_planes(crtc);
12349 dev_priv->display.crtc_disable(crtc);
ce22dba9 12350 }
ea9d758d 12351 }
a6778b3c 12352
6c4c86f5
DV
12353 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12354 * to set it here already despite that we pass it down the callchain.
7f27126e
JB
12355 *
12356 * Note we'll need to fix this up when we start tracking multiple
12357 * pipes; here we assume a single modeset_pipe and only track the
12358 * single crtc and mode.
f6e5b160 12359 */
0a9ab303
ACO
12360 if (pipe_config->base.enable && needs_modeset(&pipe_config->base)) {
12361 modeset_crtc->mode = *mode;
b8cecdf5
DV
12362 /* mode_set/enable/disable functions rely on a correct pipe
12363 * config. */
0a9ab303 12364 intel_crtc_set_state(to_intel_crtc(modeset_crtc), pipe_config);
c326c0a9
VS
12365
12366 /*
12367 * Calculate and store various constants which
12368 * are later needed by vblank and swap-completion
12369 * timestamping. They are derived from true hwmode.
12370 */
0a9ab303 12371 drm_calc_timestamping_constants(modeset_crtc,
2d112de7 12372 &pipe_config->base.adjusted_mode);
b8cecdf5 12373 }
7758a113 12374
ea9d758d
DV
12375 /* Only after disabling all output pipelines that will be changed can we
12376 * update the the output configuration. */
0a9ab303 12377 intel_modeset_update_state(state);
f6e5b160 12378
304603f4 12379 modeset_update_crtc_power_domains(state);
47fab737 12380
d3a40d1b
ACO
12381 for_each_plane_in_state(state, plane, plane_state, i) {
12382 if (WARN_ON(plane != modeset_crtc->primary))
12383 continue;
0a9ab303 12384
d3a40d1b
ACO
12385 /* Primary plane is disabled in intel_crtc_disable() */
12386 if (!pipe_config->base.enable)
12387 continue;
0a9ab303 12388
d3a40d1b
ACO
12389 ret = drm_plane_helper_update(plane, plane_state->crtc,
12390 plane_state->fb,
12391 plane_state->crtc_x,
12392 plane_state->crtc_y,
12393 plane_state->crtc_w,
12394 plane_state->crtc_h,
12395 plane_state->src_x,
12396 plane_state->src_y,
12397 plane_state->src_w,
12398 plane_state->src_h);
9eb45f22 12399 WARN_ON(ret != 0);
a6778b3c
DV
12400 }
12401
12402 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
0a9ab303
ACO
12403 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12404 if (!needs_modeset(crtc_state) || !crtc_state->enable)
12405 continue;
12406
12407 update_scanline_offset(to_intel_crtc(crtc));
80715b2f 12408
0a9ab303
ACO
12409 dev_priv->display.crtc_enable(crtc);
12410 intel_crtc_enable_planes(crtc);
80715b2f 12411 }
a6778b3c 12412
a6778b3c 12413 /* FIXME: add subpixel order */
83a57153 12414
9eb45f22
ACO
12415 intel_crtc = to_intel_crtc(modeset_crtc);
12416
12417 /* The pipe_config will be freed with the atomic state, so
12418 * make a copy. */
12419 memcpy(crtc_state_copy, intel_crtc->config, sizeof *crtc_state_copy);
12420 intel_crtc->config = crtc_state_copy;
12421 intel_crtc->base.state = &crtc_state_copy->base;
12422
12423 return 0;
f6e5b160
CW
12424}
12425
0a9ab303
ACO
12426static int intel_set_mode_with_config(struct drm_crtc *crtc,
12427 struct drm_display_mode *mode,
0a9ab303 12428 struct intel_crtc_state *pipe_config)
f30da187
DV
12429{
12430 int ret;
12431
cf6d0d76 12432 ret = __intel_set_mode(crtc, mode, pipe_config);
f30da187
DV
12433
12434 if (ret == 0)
12435 intel_modeset_check_state(crtc->dev);
12436
12437 return ret;
12438}
12439
7f27126e
JB
12440static int intel_set_mode(struct drm_crtc *crtc,
12441 struct drm_display_mode *mode,
83a57153 12442 struct drm_atomic_state *state)
7f27126e 12443{
5cec258b 12444 struct intel_crtc_state *pipe_config;
83a57153 12445 int ret = 0;
7f27126e 12446
0a9ab303 12447 pipe_config = intel_modeset_compute_config(crtc, mode, state);
83a57153
ACO
12448 if (IS_ERR(pipe_config)) {
12449 ret = PTR_ERR(pipe_config);
12450 goto out;
12451 }
12452
cf6d0d76 12453 ret = intel_set_mode_with_config(crtc, mode, pipe_config);
83a57153
ACO
12454 if (ret)
12455 goto out;
7f27126e 12456
83a57153
ACO
12457out:
12458 return ret;
7f27126e
JB
12459}
12460
c0c36b94
CW
12461void intel_crtc_restore_mode(struct drm_crtc *crtc)
12462{
83a57153
ACO
12463 struct drm_device *dev = crtc->dev;
12464 struct drm_atomic_state *state;
4be07317 12465 struct intel_crtc *intel_crtc;
83a57153
ACO
12466 struct intel_encoder *encoder;
12467 struct intel_connector *connector;
12468 struct drm_connector_state *connector_state;
4be07317 12469 struct intel_crtc_state *crtc_state;
83a57153
ACO
12470
12471 state = drm_atomic_state_alloc(dev);
12472 if (!state) {
12473 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12474 crtc->base.id);
12475 return;
12476 }
12477
12478 state->acquire_ctx = dev->mode_config.acquire_ctx;
12479
12480 /* The force restore path in the HW readout code relies on the staged
12481 * config still keeping the user requested config while the actual
12482 * state has been overwritten by the configuration read from HW. We
12483 * need to copy the staged config to the atomic state, otherwise the
12484 * mode set will just reapply the state the HW is already in. */
12485 for_each_intel_encoder(dev, encoder) {
12486 if (&encoder->new_crtc->base != crtc)
12487 continue;
12488
12489 for_each_intel_connector(dev, connector) {
12490 if (connector->new_encoder != encoder)
12491 continue;
12492
12493 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12494 if (IS_ERR(connector_state)) {
12495 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12496 connector->base.base.id,
12497 connector->base.name,
12498 PTR_ERR(connector_state));
12499 continue;
12500 }
12501
12502 connector_state->crtc = crtc;
12503 connector_state->best_encoder = &encoder->base;
12504 }
12505 }
12506
4be07317
ACO
12507 for_each_intel_crtc(dev, intel_crtc) {
12508 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
12509 continue;
12510
12511 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
12512 if (IS_ERR(crtc_state)) {
12513 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
12514 intel_crtc->base.base.id,
12515 PTR_ERR(crtc_state));
12516 continue;
12517 }
12518
12519 crtc_state->base.enable = intel_crtc->new_enabled;
12520 }
12521
d3a40d1b
ACO
12522 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
12523 crtc->primary->fb, crtc->x, crtc->y);
12524
cf6d0d76 12525 intel_set_mode(crtc, &crtc->mode, state);
83a57153
ACO
12526
12527 drm_atomic_state_free(state);
c0c36b94
CW
12528}
12529
25c5b266
DV
12530#undef for_each_intel_crtc_masked
12531
d9e55608
DV
12532static void intel_set_config_free(struct intel_set_config *config)
12533{
12534 if (!config)
12535 return;
12536
1aa4b628
DV
12537 kfree(config->save_connector_encoders);
12538 kfree(config->save_encoder_crtcs);
7668851f 12539 kfree(config->save_crtc_enabled);
d9e55608
DV
12540 kfree(config);
12541}
12542
85f9eb71
DV
12543static int intel_set_config_save_state(struct drm_device *dev,
12544 struct intel_set_config *config)
12545{
7668851f 12546 struct drm_crtc *crtc;
85f9eb71
DV
12547 struct drm_encoder *encoder;
12548 struct drm_connector *connector;
12549 int count;
12550
7668851f
VS
12551 config->save_crtc_enabled =
12552 kcalloc(dev->mode_config.num_crtc,
12553 sizeof(bool), GFP_KERNEL);
12554 if (!config->save_crtc_enabled)
12555 return -ENOMEM;
12556
1aa4b628
DV
12557 config->save_encoder_crtcs =
12558 kcalloc(dev->mode_config.num_encoder,
12559 sizeof(struct drm_crtc *), GFP_KERNEL);
12560 if (!config->save_encoder_crtcs)
85f9eb71
DV
12561 return -ENOMEM;
12562
1aa4b628
DV
12563 config->save_connector_encoders =
12564 kcalloc(dev->mode_config.num_connector,
12565 sizeof(struct drm_encoder *), GFP_KERNEL);
12566 if (!config->save_connector_encoders)
85f9eb71
DV
12567 return -ENOMEM;
12568
12569 /* Copy data. Note that driver private data is not affected.
12570 * Should anything bad happen only the expected state is
12571 * restored, not the drivers personal bookkeeping.
12572 */
7668851f 12573 count = 0;
70e1e0ec 12574 for_each_crtc(dev, crtc) {
83d65738 12575 config->save_crtc_enabled[count++] = crtc->state->enable;
7668851f
VS
12576 }
12577
85f9eb71
DV
12578 count = 0;
12579 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 12580 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
12581 }
12582
12583 count = 0;
12584 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 12585 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
12586 }
12587
12588 return 0;
12589}
12590
12591static void intel_set_config_restore_state(struct drm_device *dev,
12592 struct intel_set_config *config)
12593{
7668851f 12594 struct intel_crtc *crtc;
9a935856
DV
12595 struct intel_encoder *encoder;
12596 struct intel_connector *connector;
85f9eb71
DV
12597 int count;
12598
7668851f 12599 count = 0;
d3fcc808 12600 for_each_intel_crtc(dev, crtc) {
7668851f
VS
12601 crtc->new_enabled = config->save_crtc_enabled[count++];
12602 }
12603
85f9eb71 12604 count = 0;
b2784e15 12605 for_each_intel_encoder(dev, encoder) {
9a935856
DV
12606 encoder->new_crtc =
12607 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
12608 }
12609
12610 count = 0;
3a3371ff 12611 for_each_intel_connector(dev, connector) {
9a935856
DV
12612 connector->new_encoder =
12613 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
12614 }
12615}
12616
e3de42b6 12617static bool
2e57f47d 12618is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
12619{
12620 int i;
12621
2e57f47d
CW
12622 if (set->num_connectors == 0)
12623 return false;
12624
12625 if (WARN_ON(set->connectors == NULL))
12626 return false;
12627
12628 for (i = 0; i < set->num_connectors; i++)
12629 if (set->connectors[i]->encoder &&
12630 set->connectors[i]->encoder->crtc == set->crtc &&
12631 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
12632 return true;
12633
12634 return false;
12635}
12636
5e2b584e
DV
12637static void
12638intel_set_config_compute_mode_changes(struct drm_mode_set *set,
8d8c9b51 12639 struct intel_crtc_state *pipe_config)
5e2b584e 12640{
41227c8c
ACO
12641 struct drm_atomic_state *state;
12642 struct drm_connector *connector;
12643 struct drm_connector_state *connector_state;
12644 struct drm_crtc *crtc;
12645 struct drm_crtc_state *crtc_state;
12646 int i;
5e2b584e
DV
12647
12648 /* We should be able to check here if the fb has the same properties
12649 * and then just flip_or_move it */
2e57f47d 12650 if (is_crtc_connector_off(set)) {
8d8c9b51 12651 pipe_config->base.mode_changed = true;
f4510a27 12652 } else if (set->crtc->primary->fb != set->fb) {
3b150f08
MR
12653 /*
12654 * If we have no fb, we can only flip as long as the crtc is
12655 * active, otherwise we need a full mode set. The crtc may
12656 * be active if we've only disabled the primary plane, or
12657 * in fastboot situations.
12658 */
f4510a27 12659 if (set->crtc->primary->fb == NULL) {
319d9827
JB
12660 struct intel_crtc *intel_crtc =
12661 to_intel_crtc(set->crtc);
12662
3b150f08 12663 if (intel_crtc->active) {
319d9827 12664 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
8d8c9b51 12665 pipe_config->base.planes_changed = true;
319d9827
JB
12666 } else {
12667 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
8d8c9b51 12668 pipe_config->base.mode_changed = true;
319d9827 12669 }
5e2b584e 12670 } else if (set->fb == NULL) {
8d8c9b51 12671 pipe_config->base.mode_changed = true;
72f4901e 12672 } else if (set->fb->pixel_format !=
f4510a27 12673 set->crtc->primary->fb->pixel_format) {
8d8c9b51 12674 pipe_config->base.mode_changed = true;
e3de42b6 12675 } else {
8d8c9b51 12676 pipe_config->base.planes_changed = true;
e3de42b6 12677 }
5e2b584e
DV
12678 }
12679
835c5873 12680 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
8d8c9b51 12681 pipe_config->base.planes_changed = true;
5e2b584e
DV
12682
12683 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12684 DRM_DEBUG_KMS("modes are different, full mode set\n");
12685 drm_mode_debug_printmodeline(&set->crtc->mode);
12686 drm_mode_debug_printmodeline(set->mode);
8d8c9b51 12687 pipe_config->base.mode_changed = true;
5e2b584e 12688 }
a1d95703 12689
41227c8c 12690 state = pipe_config->base.state;
462a425a 12691
41227c8c
ACO
12692 for_each_connector_in_state(state, connector, connector_state, i) {
12693 if (connector_state->best_encoder !=
12694 connector->state->best_encoder) {
12695 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12696 connector->base.id,
12697 connector->name);
12698 pipe_config->base.mode_changed = true;
12699 }
462a425a 12700
41227c8c
ACO
12701 if (connector_state->crtc != connector->state->crtc) {
12702 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] crtc changed, full mode switch\n",
12703 connector->base.id,
12704 connector->name);
12705 pipe_config->base.mode_changed = true;
12706 }
462a425a
ACO
12707 }
12708
41227c8c
ACO
12709 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12710 if (crtc_state->enable == crtc->state->enable)
462a425a
ACO
12711 continue;
12712
12713 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
41227c8c
ACO
12714 crtc->base.id,
12715 crtc_state->enable ? "en" : "dis");
8d8c9b51 12716 pipe_config->base.mode_changed = true;
462a425a
ACO
12717 }
12718
a1d95703 12719 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
8d8c9b51
ACO
12720 set->crtc->base.id, pipe_config->base.mode_changed,
12721 pipe_config->base.planes_changed);
5e2b584e
DV
12722}
12723
2e431051 12724static int
9a935856
DV
12725intel_modeset_stage_output_state(struct drm_device *dev,
12726 struct drm_mode_set *set,
944b0c76 12727 struct drm_atomic_state *state)
50f56119 12728{
9a935856 12729 struct intel_connector *connector;
944b0c76 12730 struct drm_connector_state *connector_state;
9a935856 12731 struct intel_encoder *encoder;
7668851f 12732 struct intel_crtc *crtc;
4be07317 12733 struct intel_crtc_state *crtc_state;
f3f08572 12734 int ro;
50f56119 12735
9abdda74 12736 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
12737 * of connectors. For paranoia, double-check this. */
12738 WARN_ON(!set->fb && (set->num_connectors != 0));
12739 WARN_ON(set->fb && (set->num_connectors == 0));
12740
3a3371ff 12741 for_each_intel_connector(dev, connector) {
9a935856
DV
12742 /* Otherwise traverse passed in connector list and get encoders
12743 * for them. */
50f56119 12744 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 12745 if (set->connectors[ro] == &connector->base) {
0e32b39c 12746 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
50f56119
DV
12747 break;
12748 }
12749 }
12750
9a935856
DV
12751 /* If we disable the crtc, disable all its connectors. Also, if
12752 * the connector is on the changing crtc but not on the new
12753 * connector list, disable it. */
12754 if ((!set->fb || ro == set->num_connectors) &&
12755 connector->base.encoder &&
12756 connector->base.encoder->crtc == set->crtc) {
12757 connector->new_encoder = NULL;
12758
12759 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12760 connector->base.base.id,
c23cc417 12761 connector->base.name);
9a935856 12762 }
50f56119 12763 }
9a935856 12764 /* connector->new_encoder is now updated for all connectors. */
50f56119 12765
9a935856 12766 /* Update crtc of enabled connectors. */
3a3371ff 12767 for_each_intel_connector(dev, connector) {
7668851f
VS
12768 struct drm_crtc *new_crtc;
12769
9a935856 12770 if (!connector->new_encoder)
50f56119
DV
12771 continue;
12772
9a935856 12773 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
12774
12775 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 12776 if (set->connectors[ro] == &connector->base)
50f56119
DV
12777 new_crtc = set->crtc;
12778 }
12779
12780 /* Make sure the new CRTC will work with the encoder */
14509916
TR
12781 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12782 new_crtc)) {
5e2b584e 12783 return -EINVAL;
50f56119 12784 }
0e32b39c 12785 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
9a935856 12786
944b0c76
ACO
12787 connector_state =
12788 drm_atomic_get_connector_state(state, &connector->base);
12789 if (IS_ERR(connector_state))
12790 return PTR_ERR(connector_state);
12791
12792 connector_state->crtc = new_crtc;
12793 connector_state->best_encoder = &connector->new_encoder->base;
12794
9a935856
DV
12795 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12796 connector->base.base.id,
c23cc417 12797 connector->base.name,
9a935856
DV
12798 new_crtc->base.id);
12799 }
12800
12801 /* Check for any encoders that needs to be disabled. */
b2784e15 12802 for_each_intel_encoder(dev, encoder) {
5a65f358 12803 int num_connectors = 0;
3a3371ff 12804 for_each_intel_connector(dev, connector) {
9a935856
DV
12805 if (connector->new_encoder == encoder) {
12806 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 12807 num_connectors++;
9a935856
DV
12808 }
12809 }
5a65f358
PZ
12810
12811 if (num_connectors == 0)
12812 encoder->new_crtc = NULL;
12813 else if (num_connectors > 1)
12814 return -EINVAL;
50f56119 12815 }
9a935856 12816 /* Now we've also updated encoder->new_crtc for all encoders. */
3a3371ff 12817 for_each_intel_connector(dev, connector) {
944b0c76
ACO
12818 connector_state =
12819 drm_atomic_get_connector_state(state, &connector->base);
9d918c15
ACO
12820 if (IS_ERR(connector_state))
12821 return PTR_ERR(connector_state);
944b0c76
ACO
12822
12823 if (connector->new_encoder) {
0e32b39c
DA
12824 if (connector->new_encoder != connector->encoder)
12825 connector->encoder = connector->new_encoder;
944b0c76
ACO
12826 } else {
12827 connector_state->crtc = NULL;
f61cccf3 12828 connector_state->best_encoder = NULL;
944b0c76 12829 }
0e32b39c 12830 }
d3fcc808 12831 for_each_intel_crtc(dev, crtc) {
7668851f
VS
12832 crtc->new_enabled = false;
12833
b2784e15 12834 for_each_intel_encoder(dev, encoder) {
7668851f
VS
12835 if (encoder->new_crtc == crtc) {
12836 crtc->new_enabled = true;
12837 break;
12838 }
12839 }
4be07317
ACO
12840
12841 if (crtc->new_enabled != crtc->base.state->enable) {
12842 crtc_state = intel_atomic_get_crtc_state(state, crtc);
12843 if (IS_ERR(crtc_state))
12844 return PTR_ERR(crtc_state);
12845
12846 crtc_state->base.enable = crtc->new_enabled;
12847 }
7668851f
VS
12848 }
12849
2e431051
DV
12850 return 0;
12851}
12852
12853static int intel_crtc_set_config(struct drm_mode_set *set)
12854{
12855 struct drm_device *dev;
83a57153 12856 struct drm_atomic_state *state = NULL;
2e431051 12857 struct intel_set_config *config;
5cec258b 12858 struct intel_crtc_state *pipe_config;
2e431051 12859 int ret;
2e431051 12860
8d3e375e
DV
12861 BUG_ON(!set);
12862 BUG_ON(!set->crtc);
12863 BUG_ON(!set->crtc->helper_private);
2e431051 12864
7e53f3a4
DV
12865 /* Enforce sane interface api - has been abused by the fb helper. */
12866 BUG_ON(!set->mode && set->fb);
12867 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 12868
2e431051
DV
12869 if (set->fb) {
12870 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12871 set->crtc->base.id, set->fb->base.id,
12872 (int)set->num_connectors, set->x, set->y);
12873 } else {
12874 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
12875 }
12876
12877 dev = set->crtc->dev;
12878
12879 ret = -ENOMEM;
12880 config = kzalloc(sizeof(*config), GFP_KERNEL);
12881 if (!config)
12882 goto out_config;
12883
12884 ret = intel_set_config_save_state(dev, config);
12885 if (ret)
12886 goto out_config;
12887
83a57153
ACO
12888 state = drm_atomic_state_alloc(dev);
12889 if (!state) {
12890 ret = -ENOMEM;
12891 goto out_config;
12892 }
12893
12894 state->acquire_ctx = dev->mode_config.acquire_ctx;
12895
462a425a 12896 ret = intel_modeset_stage_output_state(dev, set, state);
2e431051
DV
12897 if (ret)
12898 goto fail;
12899
d3a40d1b
ACO
12900 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
12901 set->fb, set->x, set->y);
12902 if (ret)
12903 goto fail;
12904
50f52756 12905 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
0a9ab303 12906 state);
20664591 12907 if (IS_ERR(pipe_config)) {
6ac0483b 12908 ret = PTR_ERR(pipe_config);
50f52756 12909 goto fail;
20664591 12910 }
50f52756 12911
8d8c9b51
ACO
12912 /* Compute whether we need a full modeset, only an fb base update or no
12913 * change at all. In the future we might also check whether only the
12914 * mode changed, e.g. for LVDS where we only change the panel fitter in
12915 * such cases. */
12916 intel_set_config_compute_mode_changes(set, pipe_config);
12917
1f9954d0
JB
12918 intel_update_pipe_size(to_intel_crtc(set->crtc));
12919
8d8c9b51 12920 if (pipe_config->base.mode_changed) {
0a9ab303 12921 ret = intel_set_mode_with_config(set->crtc, set->mode,
0a9ab303 12922 pipe_config);
8d8c9b51 12923 } else if (pipe_config->base.planes_changed) {
3b150f08 12924 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
455a6808 12925 struct drm_plane *primary = set->crtc->primary;
b70709a6
ML
12926 struct intel_plane_state *plane_state =
12927 to_intel_plane_state(primary->state);
12928 bool was_visible = plane_state->visible;
455a6808 12929 int vdisplay, hdisplay;
3b150f08 12930
455a6808 12931 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
70a101f8
MR
12932 ret = drm_plane_helper_update(primary, set->crtc, set->fb,
12933 0, 0, hdisplay, vdisplay,
12934 set->x << 16, set->y << 16,
12935 hdisplay << 16, vdisplay << 16);
3b150f08
MR
12936
12937 /*
12938 * We need to make sure the primary plane is re-enabled if it
12939 * has previously been turned off.
12940 */
b70709a6
ML
12941 plane_state = to_intel_plane_state(primary->state);
12942 if (ret == 0 && !was_visible && plane_state->visible) {
3b150f08 12943 WARN_ON(!intel_crtc->active);
87d4300a 12944 intel_post_enable_primary(set->crtc);
3b150f08
MR
12945 }
12946
7ca51a3a
JB
12947 /*
12948 * In the fastboot case this may be our only check of the
12949 * state after boot. It would be better to only do it on
12950 * the first update, but we don't have a nice way of doing that
12951 * (and really, set_config isn't used much for high freq page
12952 * flipping, so increasing its cost here shouldn't be a big
12953 * deal).
12954 */
d330a953 12955 if (i915.fastboot && ret == 0)
7ca51a3a 12956 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
12957 }
12958
2d05eae1 12959 if (ret) {
bf67dfeb
DV
12960 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12961 set->crtc->base.id, ret);
50f56119 12962fail:
2d05eae1 12963 intel_set_config_restore_state(dev, config);
2d05eae1 12964 }
50f56119 12965
d9e55608 12966out_config:
e5d958ef 12967 drm_atomic_state_free(state);
83a57153 12968
d9e55608 12969 intel_set_config_free(config);
50f56119
DV
12970 return ret;
12971}
f6e5b160
CW
12972
12973static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 12974 .gamma_set = intel_crtc_gamma_set,
50f56119 12975 .set_config = intel_crtc_set_config,
f6e5b160
CW
12976 .destroy = intel_crtc_destroy,
12977 .page_flip = intel_crtc_page_flip,
1356837e
MR
12978 .atomic_duplicate_state = intel_crtc_duplicate_state,
12979 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
12980};
12981
5358901f
DV
12982static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12983 struct intel_shared_dpll *pll,
12984 struct intel_dpll_hw_state *hw_state)
ee7b9f93 12985{
5358901f 12986 uint32_t val;
ee7b9f93 12987
f458ebbc 12988 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
12989 return false;
12990
5358901f 12991 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
12992 hw_state->dpll = val;
12993 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12994 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
12995
12996 return val & DPLL_VCO_ENABLE;
12997}
12998
15bdd4cf
DV
12999static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13000 struct intel_shared_dpll *pll)
13001{
3e369b76
ACO
13002 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13003 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
13004}
13005
e7b903d2
DV
13006static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13007 struct intel_shared_dpll *pll)
13008{
e7b903d2 13009 /* PCH refclock must be enabled first */
89eff4be 13010 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 13011
3e369b76 13012 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
13013
13014 /* Wait for the clocks to stabilize. */
13015 POSTING_READ(PCH_DPLL(pll->id));
13016 udelay(150);
13017
13018 /* The pixel multiplier can only be updated once the
13019 * DPLL is enabled and the clocks are stable.
13020 *
13021 * So write it again.
13022 */
3e369b76 13023 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 13024 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13025 udelay(200);
13026}
13027
13028static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13029 struct intel_shared_dpll *pll)
13030{
13031 struct drm_device *dev = dev_priv->dev;
13032 struct intel_crtc *crtc;
e7b903d2
DV
13033
13034 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 13035 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
13036 if (intel_crtc_to_shared_dpll(crtc) == pll)
13037 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
13038 }
13039
15bdd4cf
DV
13040 I915_WRITE(PCH_DPLL(pll->id), 0);
13041 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
13042 udelay(200);
13043}
13044
46edb027
DV
13045static char *ibx_pch_dpll_names[] = {
13046 "PCH DPLL A",
13047 "PCH DPLL B",
13048};
13049
7c74ade1 13050static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 13051{
e7b903d2 13052 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
13053 int i;
13054
7c74ade1 13055 dev_priv->num_shared_dpll = 2;
ee7b9f93 13056
e72f9fbf 13057 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
13058 dev_priv->shared_dplls[i].id = i;
13059 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 13060 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
13061 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13062 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
13063 dev_priv->shared_dplls[i].get_hw_state =
13064 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
13065 }
13066}
13067
7c74ade1
DV
13068static void intel_shared_dpll_init(struct drm_device *dev)
13069{
e7b903d2 13070 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 13071
9cd86933
DV
13072 if (HAS_DDI(dev))
13073 intel_ddi_pll_init(dev);
13074 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
13075 ibx_pch_dpll_init(dev);
13076 else
13077 dev_priv->num_shared_dpll = 0;
13078
13079 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
13080}
13081
1fc0a8f7
TU
13082/**
13083 * intel_wm_need_update - Check whether watermarks need updating
13084 * @plane: drm plane
13085 * @state: new plane state
13086 *
13087 * Check current plane state versus the new one to determine whether
13088 * watermarks need to be recalculated.
13089 *
13090 * Returns true or false.
13091 */
13092bool intel_wm_need_update(struct drm_plane *plane,
13093 struct drm_plane_state *state)
13094{
13095 /* Update watermarks on tiling changes. */
13096 if (!plane->state->fb || !state->fb ||
13097 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13098 plane->state->rotation != state->rotation)
13099 return true;
13100
13101 return false;
13102}
13103
6beb8c23
MR
13104/**
13105 * intel_prepare_plane_fb - Prepare fb for usage on plane
13106 * @plane: drm plane to prepare for
13107 * @fb: framebuffer to prepare for presentation
13108 *
13109 * Prepares a framebuffer for usage on a display plane. Generally this
13110 * involves pinning the underlying object and updating the frontbuffer tracking
13111 * bits. Some older platforms need special physical address handling for
13112 * cursor planes.
13113 *
13114 * Returns 0 on success, negative error code on failure.
13115 */
13116int
13117intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee
TU
13118 struct drm_framebuffer *fb,
13119 const struct drm_plane_state *new_state)
465c120c
MR
13120{
13121 struct drm_device *dev = plane->dev;
6beb8c23
MR
13122 struct intel_plane *intel_plane = to_intel_plane(plane);
13123 enum pipe pipe = intel_plane->pipe;
13124 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13125 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13126 unsigned frontbuffer_bits = 0;
13127 int ret = 0;
465c120c 13128
ea2c67bb 13129 if (!obj)
465c120c
MR
13130 return 0;
13131
6beb8c23
MR
13132 switch (plane->type) {
13133 case DRM_PLANE_TYPE_PRIMARY:
13134 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13135 break;
13136 case DRM_PLANE_TYPE_CURSOR:
13137 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13138 break;
13139 case DRM_PLANE_TYPE_OVERLAY:
13140 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13141 break;
13142 }
465c120c 13143
6beb8c23 13144 mutex_lock(&dev->struct_mutex);
465c120c 13145
6beb8c23
MR
13146 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13147 INTEL_INFO(dev)->cursor_needs_physical) {
13148 int align = IS_I830(dev) ? 16 * 1024 : 256;
13149 ret = i915_gem_object_attach_phys(obj, align);
13150 if (ret)
13151 DRM_DEBUG_KMS("failed to attach phys object\n");
13152 } else {
82bc3b2d 13153 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
6beb8c23 13154 }
465c120c 13155
6beb8c23
MR
13156 if (ret == 0)
13157 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
fdd508a6 13158
4c34574f 13159 mutex_unlock(&dev->struct_mutex);
465c120c 13160
6beb8c23
MR
13161 return ret;
13162}
13163
38f3ce3a
MR
13164/**
13165 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13166 * @plane: drm plane to clean up for
13167 * @fb: old framebuffer that was on plane
13168 *
13169 * Cleans up a framebuffer that has just been removed from a plane.
13170 */
13171void
13172intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee
TU
13173 struct drm_framebuffer *fb,
13174 const struct drm_plane_state *old_state)
38f3ce3a
MR
13175{
13176 struct drm_device *dev = plane->dev;
13177 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13178
13179 if (WARN_ON(!obj))
13180 return;
13181
13182 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13183 !INTEL_INFO(dev)->cursor_needs_physical) {
13184 mutex_lock(&dev->struct_mutex);
82bc3b2d 13185 intel_unpin_fb_obj(fb, old_state);
38f3ce3a
MR
13186 mutex_unlock(&dev->struct_mutex);
13187 }
465c120c
MR
13188}
13189
6156a456
CK
13190int
13191skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13192{
13193 int max_scale;
13194 struct drm_device *dev;
13195 struct drm_i915_private *dev_priv;
13196 int crtc_clock, cdclk;
13197
13198 if (!intel_crtc || !crtc_state)
13199 return DRM_PLANE_HELPER_NO_SCALING;
13200
13201 dev = intel_crtc->base.dev;
13202 dev_priv = dev->dev_private;
13203 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13204 cdclk = dev_priv->display.get_display_clock_speed(dev);
13205
13206 if (!crtc_clock || !cdclk)
13207 return DRM_PLANE_HELPER_NO_SCALING;
13208
13209 /*
13210 * skl max scale is lower of:
13211 * close to 3 but not 3, -1 is for that purpose
13212 * or
13213 * cdclk/crtc_clock
13214 */
13215 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13216
13217 return max_scale;
13218}
13219
465c120c 13220static int
3c692a41
GP
13221intel_check_primary_plane(struct drm_plane *plane,
13222 struct intel_plane_state *state)
13223{
32b7eeec
MR
13224 struct drm_device *dev = plane->dev;
13225 struct drm_i915_private *dev_priv = dev->dev_private;
2b875c22 13226 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13227 struct intel_crtc *intel_crtc;
6156a456 13228 struct intel_crtc_state *crtc_state;
2b875c22 13229 struct drm_framebuffer *fb = state->base.fb;
3c692a41
GP
13230 struct drm_rect *dest = &state->dst;
13231 struct drm_rect *src = &state->src;
13232 const struct drm_rect *clip = &state->clip;
d8106366 13233 bool can_position = false;
6156a456
CK
13234 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13235 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
465c120c
MR
13236 int ret;
13237
ea2c67bb
MR
13238 crtc = crtc ? crtc : plane->crtc;
13239 intel_crtc = to_intel_crtc(crtc);
6156a456
CK
13240 crtc_state = state->base.state ?
13241 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
ea2c67bb 13242
6156a456
CK
13243 if (INTEL_INFO(dev)->gen >= 9) {
13244 min_scale = 1;
13245 max_scale = skl_max_scale(intel_crtc, crtc_state);
d8106366 13246 can_position = true;
6156a456 13247 }
d8106366 13248
c59cb179
MR
13249 ret = drm_plane_helper_check_update(plane, crtc, fb,
13250 src, dest, clip,
6156a456
CK
13251 min_scale,
13252 max_scale,
d8106366
SJ
13253 can_position, true,
13254 &state->visible);
c59cb179
MR
13255 if (ret)
13256 return ret;
465c120c 13257
32b7eeec 13258 if (intel_crtc->active) {
b70709a6
ML
13259 struct intel_plane_state *old_state =
13260 to_intel_plane_state(plane->state);
13261
32b7eeec
MR
13262 intel_crtc->atomic.wait_for_flips = true;
13263
13264 /*
13265 * FBC does not work on some platforms for rotated
13266 * planes, so disable it when rotation is not 0 and
13267 * update it when rotation is set back to 0.
13268 *
13269 * FIXME: This is redundant with the fbc update done in
13270 * the primary plane enable function except that that
13271 * one is done too late. We eventually need to unify
13272 * this.
13273 */
b70709a6 13274 if (state->visible &&
32b7eeec 13275 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
e35fef21 13276 dev_priv->fbc.crtc == intel_crtc &&
8e7d688b 13277 state->base.rotation != BIT(DRM_ROTATE_0)) {
32b7eeec
MR
13278 intel_crtc->atomic.disable_fbc = true;
13279 }
13280
b70709a6 13281 if (state->visible && !old_state->visible) {
32b7eeec
MR
13282 /*
13283 * BDW signals flip done immediately if the plane
13284 * is disabled, even if the plane enable is already
13285 * armed to occur at the next vblank :(
13286 */
b70709a6 13287 if (IS_BROADWELL(dev))
32b7eeec
MR
13288 intel_crtc->atomic.wait_vblank = true;
13289 }
13290
13291 intel_crtc->atomic.fb_bits |=
13292 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13293
13294 intel_crtc->atomic.update_fbc = true;
0fda6568 13295
1fc0a8f7 13296 if (intel_wm_need_update(plane, &state->base))
0fda6568 13297 intel_crtc->atomic.update_wm = true;
ccc759dc
GP
13298 }
13299
6156a456
CK
13300 if (INTEL_INFO(dev)->gen >= 9) {
13301 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13302 to_intel_plane(plane), state, 0);
13303 if (ret)
13304 return ret;
13305 }
13306
14af293f
GP
13307 return 0;
13308}
13309
13310static void
13311intel_commit_primary_plane(struct drm_plane *plane,
13312 struct intel_plane_state *state)
13313{
2b875c22
MR
13314 struct drm_crtc *crtc = state->base.crtc;
13315 struct drm_framebuffer *fb = state->base.fb;
13316 struct drm_device *dev = plane->dev;
14af293f 13317 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 13318 struct intel_crtc *intel_crtc;
14af293f
GP
13319 struct drm_rect *src = &state->src;
13320
ea2c67bb
MR
13321 crtc = crtc ? crtc : plane->crtc;
13322 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
13323
13324 plane->fb = fb;
9dc806fc
MR
13325 crtc->x = src->x1 >> 16;
13326 crtc->y = src->y1 >> 16;
ccc759dc 13327
ccc759dc 13328 if (intel_crtc->active) {
27321ae8 13329 if (state->visible)
ccc759dc
GP
13330 /* FIXME: kill this fastboot hack */
13331 intel_update_pipe_size(intel_crtc);
465c120c 13332
27321ae8
ML
13333 dev_priv->display.update_primary_plane(crtc, plane->fb,
13334 crtc->x, crtc->y);
ccc759dc 13335 }
465c120c
MR
13336}
13337
a8ad0d8e
ML
13338static void
13339intel_disable_primary_plane(struct drm_plane *plane,
13340 struct drm_crtc *crtc,
13341 bool force)
13342{
13343 struct drm_device *dev = plane->dev;
13344 struct drm_i915_private *dev_priv = dev->dev_private;
13345
a8ad0d8e
ML
13346 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13347}
13348
32b7eeec 13349static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 13350{
32b7eeec 13351 struct drm_device *dev = crtc->dev;
140fd38d 13352 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 13353 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb
MR
13354 struct intel_plane *intel_plane;
13355 struct drm_plane *p;
13356 unsigned fb_bits = 0;
13357
13358 /* Track fb's for any planes being disabled */
13359 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13360 intel_plane = to_intel_plane(p);
13361
13362 if (intel_crtc->atomic.disabled_planes &
13363 (1 << drm_plane_index(p))) {
13364 switch (p->type) {
13365 case DRM_PLANE_TYPE_PRIMARY:
13366 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13367 break;
13368 case DRM_PLANE_TYPE_CURSOR:
13369 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13370 break;
13371 case DRM_PLANE_TYPE_OVERLAY:
13372 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13373 break;
13374 }
3c692a41 13375
ea2c67bb
MR
13376 mutex_lock(&dev->struct_mutex);
13377 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13378 mutex_unlock(&dev->struct_mutex);
13379 }
13380 }
3c692a41 13381
32b7eeec
MR
13382 if (intel_crtc->atomic.wait_for_flips)
13383 intel_crtc_wait_for_pending_flips(crtc);
3c692a41 13384
32b7eeec
MR
13385 if (intel_crtc->atomic.disable_fbc)
13386 intel_fbc_disable(dev);
3c692a41 13387
32b7eeec
MR
13388 if (intel_crtc->atomic.pre_disable_primary)
13389 intel_pre_disable_primary(crtc);
3c692a41 13390
32b7eeec
MR
13391 if (intel_crtc->atomic.update_wm)
13392 intel_update_watermarks(crtc);
3c692a41 13393
32b7eeec 13394 intel_runtime_pm_get(dev_priv);
3c692a41 13395
c34c9ee4
MR
13396 /* Perform vblank evasion around commit operation */
13397 if (intel_crtc->active)
13398 intel_crtc->atomic.evade =
13399 intel_pipe_update_start(intel_crtc,
13400 &intel_crtc->atomic.start_vbl_count);
32b7eeec
MR
13401}
13402
13403static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13404{
13405 struct drm_device *dev = crtc->dev;
13406 struct drm_i915_private *dev_priv = dev->dev_private;
13407 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13408 struct drm_plane *p;
13409
c34c9ee4
MR
13410 if (intel_crtc->atomic.evade)
13411 intel_pipe_update_end(intel_crtc,
13412 intel_crtc->atomic.start_vbl_count);
3c692a41 13413
140fd38d 13414 intel_runtime_pm_put(dev_priv);
3c692a41 13415
32b7eeec
MR
13416 if (intel_crtc->atomic.wait_vblank)
13417 intel_wait_for_vblank(dev, intel_crtc->pipe);
13418
13419 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13420
13421 if (intel_crtc->atomic.update_fbc) {
ccc759dc 13422 mutex_lock(&dev->struct_mutex);
7ff0ebcc 13423 intel_fbc_update(dev);
ccc759dc 13424 mutex_unlock(&dev->struct_mutex);
38f3ce3a 13425 }
3c692a41 13426
32b7eeec
MR
13427 if (intel_crtc->atomic.post_enable_primary)
13428 intel_post_enable_primary(crtc);
3c692a41 13429
32b7eeec
MR
13430 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13431 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13432 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13433 false, false);
13434
13435 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
3c692a41
GP
13436}
13437
cf4c7c12 13438/**
4a3b8769
MR
13439 * intel_plane_destroy - destroy a plane
13440 * @plane: plane to destroy
cf4c7c12 13441 *
4a3b8769
MR
13442 * Common destruction function for all types of planes (primary, cursor,
13443 * sprite).
cf4c7c12 13444 */
4a3b8769 13445void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13446{
13447 struct intel_plane *intel_plane = to_intel_plane(plane);
13448 drm_plane_cleanup(plane);
13449 kfree(intel_plane);
13450}
13451
65a3fea0 13452const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13453 .update_plane = drm_atomic_helper_update_plane,
13454 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13455 .destroy = intel_plane_destroy,
c196e1d6 13456 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13457 .atomic_get_property = intel_plane_atomic_get_property,
13458 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13459 .atomic_duplicate_state = intel_plane_duplicate_state,
13460 .atomic_destroy_state = intel_plane_destroy_state,
13461
465c120c
MR
13462};
13463
13464static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13465 int pipe)
13466{
13467 struct intel_plane *primary;
8e7d688b 13468 struct intel_plane_state *state;
465c120c
MR
13469 const uint32_t *intel_primary_formats;
13470 int num_formats;
13471
13472 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13473 if (primary == NULL)
13474 return NULL;
13475
8e7d688b
MR
13476 state = intel_create_plane_state(&primary->base);
13477 if (!state) {
ea2c67bb
MR
13478 kfree(primary);
13479 return NULL;
13480 }
8e7d688b 13481 primary->base.state = &state->base;
ea2c67bb 13482
465c120c
MR
13483 primary->can_scale = false;
13484 primary->max_downscale = 1;
6156a456
CK
13485 if (INTEL_INFO(dev)->gen >= 9) {
13486 primary->can_scale = true;
13487 }
549e2bfb 13488 state->scaler_id = -1;
465c120c
MR
13489 primary->pipe = pipe;
13490 primary->plane = pipe;
c59cb179
MR
13491 primary->check_plane = intel_check_primary_plane;
13492 primary->commit_plane = intel_commit_primary_plane;
a8ad0d8e 13493 primary->disable_plane = intel_disable_primary_plane;
08e221fb 13494 primary->ckey.flags = I915_SET_COLORKEY_NONE;
465c120c
MR
13495 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13496 primary->plane = !pipe;
13497
13498 if (INTEL_INFO(dev)->gen <= 3) {
13499 intel_primary_formats = intel_primary_formats_gen2;
13500 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
13501 } else {
13502 intel_primary_formats = intel_primary_formats_gen4;
13503 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
13504 }
13505
13506 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13507 &intel_plane_funcs,
465c120c
MR
13508 intel_primary_formats, num_formats,
13509 DRM_PLANE_TYPE_PRIMARY);
48404c1e 13510
3b7a5119
SJ
13511 if (INTEL_INFO(dev)->gen >= 4)
13512 intel_create_rotation_property(dev, primary);
48404c1e 13513
ea2c67bb
MR
13514 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13515
465c120c
MR
13516 return &primary->base;
13517}
13518
3b7a5119
SJ
13519void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13520{
13521 if (!dev->mode_config.rotation_property) {
13522 unsigned long flags = BIT(DRM_ROTATE_0) |
13523 BIT(DRM_ROTATE_180);
13524
13525 if (INTEL_INFO(dev)->gen >= 9)
13526 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13527
13528 dev->mode_config.rotation_property =
13529 drm_mode_create_rotation_property(dev, flags);
13530 }
13531 if (dev->mode_config.rotation_property)
13532 drm_object_attach_property(&plane->base.base,
13533 dev->mode_config.rotation_property,
13534 plane->base.state->rotation);
13535}
13536
3d7d6510 13537static int
852e787c
GP
13538intel_check_cursor_plane(struct drm_plane *plane,
13539 struct intel_plane_state *state)
3d7d6510 13540{
2b875c22 13541 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13542 struct drm_device *dev = plane->dev;
2b875c22 13543 struct drm_framebuffer *fb = state->base.fb;
852e787c
GP
13544 struct drm_rect *dest = &state->dst;
13545 struct drm_rect *src = &state->src;
13546 const struct drm_rect *clip = &state->clip;
757f9a3e 13547 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ea2c67bb 13548 struct intel_crtc *intel_crtc;
757f9a3e
GP
13549 unsigned stride;
13550 int ret;
3d7d6510 13551
ea2c67bb
MR
13552 crtc = crtc ? crtc : plane->crtc;
13553 intel_crtc = to_intel_crtc(crtc);
13554
757f9a3e 13555 ret = drm_plane_helper_check_update(plane, crtc, fb,
852e787c 13556 src, dest, clip,
3d7d6510
MR
13557 DRM_PLANE_HELPER_NO_SCALING,
13558 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13559 true, true, &state->visible);
757f9a3e
GP
13560 if (ret)
13561 return ret;
13562
13563
13564 /* if we want to turn off the cursor ignore width and height */
13565 if (!obj)
32b7eeec 13566 goto finish;
757f9a3e 13567
757f9a3e 13568 /* Check for which cursor types we support */
ea2c67bb
MR
13569 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13570 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13571 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13572 return -EINVAL;
13573 }
13574
ea2c67bb
MR
13575 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13576 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13577 DRM_DEBUG_KMS("buffer is too small\n");
13578 return -ENOMEM;
13579 }
13580
3a656b54 13581 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e
GP
13582 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13583 ret = -EINVAL;
13584 }
757f9a3e 13585
32b7eeec
MR
13586finish:
13587 if (intel_crtc->active) {
3749f463 13588 if (plane->state->crtc_w != state->base.crtc_w)
32b7eeec
MR
13589 intel_crtc->atomic.update_wm = true;
13590
13591 intel_crtc->atomic.fb_bits |=
13592 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13593 }
13594
757f9a3e 13595 return ret;
852e787c 13596}
3d7d6510 13597
a8ad0d8e
ML
13598static void
13599intel_disable_cursor_plane(struct drm_plane *plane,
13600 struct drm_crtc *crtc,
13601 bool force)
13602{
13603 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13604
13605 if (!force) {
13606 plane->fb = NULL;
13607 intel_crtc->cursor_bo = NULL;
13608 intel_crtc->cursor_addr = 0;
13609 }
13610
13611 intel_crtc_update_cursor(crtc, false);
13612}
13613
f4a2cf29 13614static void
852e787c
GP
13615intel_commit_cursor_plane(struct drm_plane *plane,
13616 struct intel_plane_state *state)
13617{
2b875c22 13618 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
13619 struct drm_device *dev = plane->dev;
13620 struct intel_crtc *intel_crtc;
2b875c22 13621 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13622 uint32_t addr;
852e787c 13623
ea2c67bb
MR
13624 crtc = crtc ? crtc : plane->crtc;
13625 intel_crtc = to_intel_crtc(crtc);
13626
2b875c22 13627 plane->fb = state->base.fb;
ea2c67bb
MR
13628 crtc->cursor_x = state->base.crtc_x;
13629 crtc->cursor_y = state->base.crtc_y;
13630
a912f12f
GP
13631 if (intel_crtc->cursor_bo == obj)
13632 goto update;
4ed91096 13633
f4a2cf29 13634 if (!obj)
a912f12f 13635 addr = 0;
f4a2cf29 13636 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 13637 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 13638 else
a912f12f 13639 addr = obj->phys_handle->busaddr;
852e787c 13640
a912f12f
GP
13641 intel_crtc->cursor_addr = addr;
13642 intel_crtc->cursor_bo = obj;
13643update:
852e787c 13644
32b7eeec 13645 if (intel_crtc->active)
a912f12f 13646 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
13647}
13648
3d7d6510
MR
13649static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13650 int pipe)
13651{
13652 struct intel_plane *cursor;
8e7d688b 13653 struct intel_plane_state *state;
3d7d6510
MR
13654
13655 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13656 if (cursor == NULL)
13657 return NULL;
13658
8e7d688b
MR
13659 state = intel_create_plane_state(&cursor->base);
13660 if (!state) {
ea2c67bb
MR
13661 kfree(cursor);
13662 return NULL;
13663 }
8e7d688b 13664 cursor->base.state = &state->base;
ea2c67bb 13665
3d7d6510
MR
13666 cursor->can_scale = false;
13667 cursor->max_downscale = 1;
13668 cursor->pipe = pipe;
13669 cursor->plane = pipe;
549e2bfb 13670 state->scaler_id = -1;
c59cb179
MR
13671 cursor->check_plane = intel_check_cursor_plane;
13672 cursor->commit_plane = intel_commit_cursor_plane;
a8ad0d8e 13673 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
13674
13675 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 13676 &intel_plane_funcs,
3d7d6510
MR
13677 intel_cursor_formats,
13678 ARRAY_SIZE(intel_cursor_formats),
13679 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
13680
13681 if (INTEL_INFO(dev)->gen >= 4) {
13682 if (!dev->mode_config.rotation_property)
13683 dev->mode_config.rotation_property =
13684 drm_mode_create_rotation_property(dev,
13685 BIT(DRM_ROTATE_0) |
13686 BIT(DRM_ROTATE_180));
13687 if (dev->mode_config.rotation_property)
13688 drm_object_attach_property(&cursor->base.base,
13689 dev->mode_config.rotation_property,
8e7d688b 13690 state->base.rotation);
4398ad45
VS
13691 }
13692
ea2c67bb
MR
13693 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13694
3d7d6510
MR
13695 return &cursor->base;
13696}
13697
549e2bfb
CK
13698static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13699 struct intel_crtc_state *crtc_state)
13700{
13701 int i;
13702 struct intel_scaler *intel_scaler;
13703 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13704
13705 for (i = 0; i < intel_crtc->num_scalers; i++) {
13706 intel_scaler = &scaler_state->scalers[i];
13707 intel_scaler->in_use = 0;
13708 intel_scaler->id = i;
13709
13710 intel_scaler->mode = PS_SCALER_MODE_DYN;
13711 }
13712
13713 scaler_state->scaler_id = -1;
13714}
13715
b358d0a6 13716static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 13717{
fbee40df 13718 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 13719 struct intel_crtc *intel_crtc;
f5de6e07 13720 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
13721 struct drm_plane *primary = NULL;
13722 struct drm_plane *cursor = NULL;
465c120c 13723 int i, ret;
79e53945 13724
955382f3 13725 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
13726 if (intel_crtc == NULL)
13727 return;
13728
f5de6e07
ACO
13729 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13730 if (!crtc_state)
13731 goto fail;
13732 intel_crtc_set_state(intel_crtc, crtc_state);
07878248 13733 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13734
549e2bfb
CK
13735 /* initialize shared scalers */
13736 if (INTEL_INFO(dev)->gen >= 9) {
13737 if (pipe == PIPE_C)
13738 intel_crtc->num_scalers = 1;
13739 else
13740 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13741
13742 skl_init_scalers(dev, intel_crtc, crtc_state);
13743 }
13744
465c120c 13745 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
13746 if (!primary)
13747 goto fail;
13748
13749 cursor = intel_cursor_plane_create(dev, pipe);
13750 if (!cursor)
13751 goto fail;
13752
465c120c 13753 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
13754 cursor, &intel_crtc_funcs);
13755 if (ret)
13756 goto fail;
79e53945
JB
13757
13758 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
13759 for (i = 0; i < 256; i++) {
13760 intel_crtc->lut_r[i] = i;
13761 intel_crtc->lut_g[i] = i;
13762 intel_crtc->lut_b[i] = i;
13763 }
13764
1f1c2e24
VS
13765 /*
13766 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 13767 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 13768 */
80824003
JB
13769 intel_crtc->pipe = pipe;
13770 intel_crtc->plane = pipe;
3a77c4c4 13771 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 13772 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 13773 intel_crtc->plane = !pipe;
80824003
JB
13774 }
13775
4b0e333e
CW
13776 intel_crtc->cursor_base = ~0;
13777 intel_crtc->cursor_cntl = ~0;
dc41c154 13778 intel_crtc->cursor_size = ~0;
8d7849db 13779
22fd0fab
JB
13780 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13781 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13782 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13783 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13784
9362c7c5
ACO
13785 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13786
79e53945 13787 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
13788
13789 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
13790 return;
13791
13792fail:
13793 if (primary)
13794 drm_plane_cleanup(primary);
13795 if (cursor)
13796 drm_plane_cleanup(cursor);
f5de6e07 13797 kfree(crtc_state);
3d7d6510 13798 kfree(intel_crtc);
79e53945
JB
13799}
13800
752aa88a
JB
13801enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13802{
13803 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 13804 struct drm_device *dev = connector->base.dev;
752aa88a 13805
51fd371b 13806 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 13807
d3babd3f 13808 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
13809 return INVALID_PIPE;
13810
13811 return to_intel_crtc(encoder->crtc)->pipe;
13812}
13813
08d7b3d1 13814int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 13815 struct drm_file *file)
08d7b3d1 13816{
08d7b3d1 13817 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 13818 struct drm_crtc *drmmode_crtc;
c05422d5 13819 struct intel_crtc *crtc;
08d7b3d1 13820
7707e653 13821 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 13822
7707e653 13823 if (!drmmode_crtc) {
08d7b3d1 13824 DRM_ERROR("no such CRTC id\n");
3f2c2057 13825 return -ENOENT;
08d7b3d1
CW
13826 }
13827
7707e653 13828 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 13829 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 13830
c05422d5 13831 return 0;
08d7b3d1
CW
13832}
13833
66a9278e 13834static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 13835{
66a9278e
DV
13836 struct drm_device *dev = encoder->base.dev;
13837 struct intel_encoder *source_encoder;
79e53945 13838 int index_mask = 0;
79e53945
JB
13839 int entry = 0;
13840
b2784e15 13841 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 13842 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
13843 index_mask |= (1 << entry);
13844
79e53945
JB
13845 entry++;
13846 }
4ef69c7a 13847
79e53945
JB
13848 return index_mask;
13849}
13850
4d302442
CW
13851static bool has_edp_a(struct drm_device *dev)
13852{
13853 struct drm_i915_private *dev_priv = dev->dev_private;
13854
13855 if (!IS_MOBILE(dev))
13856 return false;
13857
13858 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13859 return false;
13860
e3589908 13861 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
13862 return false;
13863
13864 return true;
13865}
13866
84b4e042
JB
13867static bool intel_crt_present(struct drm_device *dev)
13868{
13869 struct drm_i915_private *dev_priv = dev->dev_private;
13870
884497ed
DL
13871 if (INTEL_INFO(dev)->gen >= 9)
13872 return false;
13873
cf404ce4 13874 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
13875 return false;
13876
13877 if (IS_CHERRYVIEW(dev))
13878 return false;
13879
13880 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13881 return false;
13882
13883 return true;
13884}
13885
79e53945
JB
13886static void intel_setup_outputs(struct drm_device *dev)
13887{
725e30ad 13888 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 13889 struct intel_encoder *encoder;
cb0953d7 13890 bool dpd_is_edp = false;
79e53945 13891
c9093354 13892 intel_lvds_init(dev);
79e53945 13893
84b4e042 13894 if (intel_crt_present(dev))
79935fca 13895 intel_crt_init(dev);
cb0953d7 13896
c776eb2e
VK
13897 if (IS_BROXTON(dev)) {
13898 /*
13899 * FIXME: Broxton doesn't support port detection via the
13900 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13901 * detect the ports.
13902 */
13903 intel_ddi_init(dev, PORT_A);
13904 intel_ddi_init(dev, PORT_B);
13905 intel_ddi_init(dev, PORT_C);
13906 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
13907 int found;
13908
de31facd
JB
13909 /*
13910 * Haswell uses DDI functions to detect digital outputs.
13911 * On SKL pre-D0 the strap isn't connected, so we assume
13912 * it's there.
13913 */
0e72a5b5 13914 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
de31facd
JB
13915 /* WaIgnoreDDIAStrap: skl */
13916 if (found ||
13917 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
0e72a5b5
ED
13918 intel_ddi_init(dev, PORT_A);
13919
13920 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13921 * register */
13922 found = I915_READ(SFUSE_STRAP);
13923
13924 if (found & SFUSE_STRAP_DDIB_DETECTED)
13925 intel_ddi_init(dev, PORT_B);
13926 if (found & SFUSE_STRAP_DDIC_DETECTED)
13927 intel_ddi_init(dev, PORT_C);
13928 if (found & SFUSE_STRAP_DDID_DETECTED)
13929 intel_ddi_init(dev, PORT_D);
13930 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 13931 int found;
5d8a7752 13932 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
13933
13934 if (has_edp_a(dev))
13935 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 13936
dc0fa718 13937 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 13938 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 13939 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 13940 if (!found)
e2debe91 13941 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 13942 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 13943 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
13944 }
13945
dc0fa718 13946 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 13947 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 13948
dc0fa718 13949 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 13950 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 13951
5eb08b69 13952 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 13953 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 13954
270b3042 13955 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 13956 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 13957 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
13958 /*
13959 * The DP_DETECTED bit is the latched state of the DDC
13960 * SDA pin at boot. However since eDP doesn't require DDC
13961 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13962 * eDP ports may have been muxed to an alternate function.
13963 * Thus we can't rely on the DP_DETECTED bit alone to detect
13964 * eDP ports. Consult the VBT as well as DP_DETECTED to
13965 * detect eDP ports.
13966 */
d2182a66
VS
13967 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13968 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
13969 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13970 PORT_B);
e17ac6db
VS
13971 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13972 intel_dp_is_edp(dev, PORT_B))
13973 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 13974
d2182a66
VS
13975 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13976 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
13977 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13978 PORT_C);
e17ac6db
VS
13979 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13980 intel_dp_is_edp(dev, PORT_C))
13981 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 13982
9418c1f1 13983 if (IS_CHERRYVIEW(dev)) {
e17ac6db 13984 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
13985 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13986 PORT_D);
e17ac6db
VS
13987 /* eDP not supported on port D, so don't check VBT */
13988 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13989 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
13990 }
13991
3cfca973 13992 intel_dsi_init(dev);
103a196f 13993 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 13994 bool found = false;
7d57382e 13995
e2debe91 13996 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 13997 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 13998 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
13999 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14000 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 14001 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 14002 }
27185ae1 14003
e7281eab 14004 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14005 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 14006 }
13520b05
KH
14007
14008 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 14009
e2debe91 14010 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 14011 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 14012 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 14013 }
27185ae1 14014
e2debe91 14015 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 14016
b01f2c3a
JB
14017 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14018 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 14019 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 14020 }
e7281eab 14021 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 14022 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 14023 }
27185ae1 14024
b01f2c3a 14025 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 14026 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 14027 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 14028 } else if (IS_GEN2(dev))
79e53945
JB
14029 intel_dvo_init(dev);
14030
103a196f 14031 if (SUPPORTS_TV(dev))
79e53945
JB
14032 intel_tv_init(dev);
14033
0bc12bcb 14034 intel_psr_init(dev);
7c8f8a70 14035
b2784e15 14036 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
14037 encoder->base.possible_crtcs = encoder->crtc_mask;
14038 encoder->base.possible_clones =
66a9278e 14039 intel_encoder_clones(encoder);
79e53945 14040 }
47356eb6 14041
dde86e2d 14042 intel_init_pch_refclk(dev);
270b3042
DV
14043
14044 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
14045}
14046
14047static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14048{
60a5ca01 14049 struct drm_device *dev = fb->dev;
79e53945 14050 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 14051
ef2d633e 14052 drm_framebuffer_cleanup(fb);
60a5ca01 14053 mutex_lock(&dev->struct_mutex);
ef2d633e 14054 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
14055 drm_gem_object_unreference(&intel_fb->obj->base);
14056 mutex_unlock(&dev->struct_mutex);
79e53945
JB
14057 kfree(intel_fb);
14058}
14059
14060static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 14061 struct drm_file *file,
79e53945
JB
14062 unsigned int *handle)
14063{
14064 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 14065 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 14066
05394f39 14067 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
14068}
14069
14070static const struct drm_framebuffer_funcs intel_fb_funcs = {
14071 .destroy = intel_user_framebuffer_destroy,
14072 .create_handle = intel_user_framebuffer_create_handle,
14073};
14074
b321803d
DL
14075static
14076u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14077 uint32_t pixel_format)
14078{
14079 u32 gen = INTEL_INFO(dev)->gen;
14080
14081 if (gen >= 9) {
14082 /* "The stride in bytes must not exceed the of the size of 8K
14083 * pixels and 32K bytes."
14084 */
14085 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14086 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14087 return 32*1024;
14088 } else if (gen >= 4) {
14089 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14090 return 16*1024;
14091 else
14092 return 32*1024;
14093 } else if (gen >= 3) {
14094 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14095 return 8*1024;
14096 else
14097 return 16*1024;
14098 } else {
14099 /* XXX DSPC is limited to 4k tiled */
14100 return 8*1024;
14101 }
14102}
14103
b5ea642a
DV
14104static int intel_framebuffer_init(struct drm_device *dev,
14105 struct intel_framebuffer *intel_fb,
14106 struct drm_mode_fb_cmd2 *mode_cmd,
14107 struct drm_i915_gem_object *obj)
79e53945 14108{
6761dd31 14109 unsigned int aligned_height;
79e53945 14110 int ret;
b321803d 14111 u32 pitch_limit, stride_alignment;
79e53945 14112
dd4916c5
DV
14113 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14114
2a80eada
DV
14115 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14116 /* Enforce that fb modifier and tiling mode match, but only for
14117 * X-tiled. This is needed for FBC. */
14118 if (!!(obj->tiling_mode == I915_TILING_X) !=
14119 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14120 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14121 return -EINVAL;
14122 }
14123 } else {
14124 if (obj->tiling_mode == I915_TILING_X)
14125 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14126 else if (obj->tiling_mode == I915_TILING_Y) {
14127 DRM_DEBUG("No Y tiling for legacy addfb\n");
14128 return -EINVAL;
14129 }
14130 }
14131
9a8f0a12
TU
14132 /* Passed in modifier sanity checking. */
14133 switch (mode_cmd->modifier[0]) {
14134 case I915_FORMAT_MOD_Y_TILED:
14135 case I915_FORMAT_MOD_Yf_TILED:
14136 if (INTEL_INFO(dev)->gen < 9) {
14137 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14138 mode_cmd->modifier[0]);
14139 return -EINVAL;
14140 }
14141 case DRM_FORMAT_MOD_NONE:
14142 case I915_FORMAT_MOD_X_TILED:
14143 break;
14144 default:
c0f40428
JB
14145 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14146 mode_cmd->modifier[0]);
57cd6508 14147 return -EINVAL;
c16ed4be 14148 }
57cd6508 14149
b321803d
DL
14150 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14151 mode_cmd->pixel_format);
14152 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14153 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14154 mode_cmd->pitches[0], stride_alignment);
57cd6508 14155 return -EINVAL;
c16ed4be 14156 }
57cd6508 14157
b321803d
DL
14158 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14159 mode_cmd->pixel_format);
a35cdaa0 14160 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
14161 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14162 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 14163 "tiled" : "linear",
a35cdaa0 14164 mode_cmd->pitches[0], pitch_limit);
5d7bd705 14165 return -EINVAL;
c16ed4be 14166 }
5d7bd705 14167
2a80eada 14168 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
14169 mode_cmd->pitches[0] != obj->stride) {
14170 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14171 mode_cmd->pitches[0], obj->stride);
5d7bd705 14172 return -EINVAL;
c16ed4be 14173 }
5d7bd705 14174
57779d06 14175 /* Reject formats not supported by any plane early. */
308e5bcb 14176 switch (mode_cmd->pixel_format) {
57779d06 14177 case DRM_FORMAT_C8:
04b3924d
VS
14178 case DRM_FORMAT_RGB565:
14179 case DRM_FORMAT_XRGB8888:
14180 case DRM_FORMAT_ARGB8888:
57779d06
VS
14181 break;
14182 case DRM_FORMAT_XRGB1555:
14183 case DRM_FORMAT_ARGB1555:
c16ed4be 14184 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
14185 DRM_DEBUG("unsupported pixel format: %s\n",
14186 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14187 return -EINVAL;
c16ed4be 14188 }
57779d06
VS
14189 break;
14190 case DRM_FORMAT_XBGR8888:
14191 case DRM_FORMAT_ABGR8888:
04b3924d
VS
14192 case DRM_FORMAT_XRGB2101010:
14193 case DRM_FORMAT_ARGB2101010:
57779d06
VS
14194 case DRM_FORMAT_XBGR2101010:
14195 case DRM_FORMAT_ABGR2101010:
c16ed4be 14196 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
14197 DRM_DEBUG("unsupported pixel format: %s\n",
14198 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14199 return -EINVAL;
c16ed4be 14200 }
b5626747 14201 break;
04b3924d
VS
14202 case DRM_FORMAT_YUYV:
14203 case DRM_FORMAT_UYVY:
14204 case DRM_FORMAT_YVYU:
14205 case DRM_FORMAT_VYUY:
c16ed4be 14206 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14207 DRM_DEBUG("unsupported pixel format: %s\n",
14208 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14209 return -EINVAL;
c16ed4be 14210 }
57cd6508
CW
14211 break;
14212 default:
4ee62c76
VS
14213 DRM_DEBUG("unsupported pixel format: %s\n",
14214 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14215 return -EINVAL;
14216 }
14217
90f9a336
VS
14218 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14219 if (mode_cmd->offsets[0] != 0)
14220 return -EINVAL;
14221
ec2c981e 14222 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14223 mode_cmd->pixel_format,
14224 mode_cmd->modifier[0]);
53155c0a
DV
14225 /* FIXME drm helper for size checks (especially planar formats)? */
14226 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14227 return -EINVAL;
14228
c7d73f6a
DV
14229 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14230 intel_fb->obj = obj;
80075d49 14231 intel_fb->obj->framebuffer_references++;
c7d73f6a 14232
79e53945
JB
14233 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14234 if (ret) {
14235 DRM_ERROR("framebuffer init failed %d\n", ret);
14236 return ret;
14237 }
14238
79e53945
JB
14239 return 0;
14240}
14241
79e53945
JB
14242static struct drm_framebuffer *
14243intel_user_framebuffer_create(struct drm_device *dev,
14244 struct drm_file *filp,
308e5bcb 14245 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14246{
05394f39 14247 struct drm_i915_gem_object *obj;
79e53945 14248
308e5bcb
JB
14249 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14250 mode_cmd->handles[0]));
c8725226 14251 if (&obj->base == NULL)
cce13ff7 14252 return ERR_PTR(-ENOENT);
79e53945 14253
d2dff872 14254 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
14255}
14256
4520f53a 14257#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 14258static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14259{
14260}
14261#endif
14262
79e53945 14263static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14264 .fb_create = intel_user_framebuffer_create,
0632fef6 14265 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14266 .atomic_check = intel_atomic_check,
14267 .atomic_commit = intel_atomic_commit,
79e53945
JB
14268};
14269
e70236a8
JB
14270/* Set up chip specific display functions */
14271static void intel_init_display(struct drm_device *dev)
14272{
14273 struct drm_i915_private *dev_priv = dev->dev_private;
14274
ee9300bb
DV
14275 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14276 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14277 else if (IS_CHERRYVIEW(dev))
14278 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14279 else if (IS_VALLEYVIEW(dev))
14280 dev_priv->display.find_dpll = vlv_find_best_dpll;
14281 else if (IS_PINEVIEW(dev))
14282 dev_priv->display.find_dpll = pnv_find_best_dpll;
14283 else
14284 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14285
bc8d7dff
DL
14286 if (INTEL_INFO(dev)->gen >= 9) {
14287 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14288 dev_priv->display.get_initial_plane_config =
14289 skylake_get_initial_plane_config;
bc8d7dff
DL
14290 dev_priv->display.crtc_compute_clock =
14291 haswell_crtc_compute_clock;
14292 dev_priv->display.crtc_enable = haswell_crtc_enable;
14293 dev_priv->display.crtc_disable = haswell_crtc_disable;
14294 dev_priv->display.off = ironlake_crtc_off;
14295 dev_priv->display.update_primary_plane =
14296 skylake_update_primary_plane;
14297 } else if (HAS_DDI(dev)) {
0e8ffe1b 14298 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14299 dev_priv->display.get_initial_plane_config =
14300 ironlake_get_initial_plane_config;
797d0259
ACO
14301 dev_priv->display.crtc_compute_clock =
14302 haswell_crtc_compute_clock;
4f771f10
PZ
14303 dev_priv->display.crtc_enable = haswell_crtc_enable;
14304 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 14305 dev_priv->display.off = ironlake_crtc_off;
bc8d7dff
DL
14306 dev_priv->display.update_primary_plane =
14307 ironlake_update_primary_plane;
09b4ddf9 14308 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14309 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14310 dev_priv->display.get_initial_plane_config =
14311 ironlake_get_initial_plane_config;
3fb37703
ACO
14312 dev_priv->display.crtc_compute_clock =
14313 ironlake_crtc_compute_clock;
76e5a89c
DV
14314 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14315 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 14316 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
14317 dev_priv->display.update_primary_plane =
14318 ironlake_update_primary_plane;
89b667f8
JB
14319 } else if (IS_VALLEYVIEW(dev)) {
14320 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14321 dev_priv->display.get_initial_plane_config =
14322 i9xx_get_initial_plane_config;
d6dfee7a 14323 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14324 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14325 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14326 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
14327 dev_priv->display.update_primary_plane =
14328 i9xx_update_primary_plane;
f564048e 14329 } else {
0e8ffe1b 14330 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14331 dev_priv->display.get_initial_plane_config =
14332 i9xx_get_initial_plane_config;
d6dfee7a 14333 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14334 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14335 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 14336 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
14337 dev_priv->display.update_primary_plane =
14338 i9xx_update_primary_plane;
f564048e 14339 }
e70236a8 14340
e70236a8 14341 /* Returns the core display clock speed */
1652d19e
VS
14342 if (IS_SKYLAKE(dev))
14343 dev_priv->display.get_display_clock_speed =
14344 skylake_get_display_clock_speed;
14345 else if (IS_BROADWELL(dev))
14346 dev_priv->display.get_display_clock_speed =
14347 broadwell_get_display_clock_speed;
14348 else if (IS_HASWELL(dev))
14349 dev_priv->display.get_display_clock_speed =
14350 haswell_get_display_clock_speed;
14351 else if (IS_VALLEYVIEW(dev))
25eb05fc
JB
14352 dev_priv->display.get_display_clock_speed =
14353 valleyview_get_display_clock_speed;
b37a6434
VS
14354 else if (IS_GEN5(dev))
14355 dev_priv->display.get_display_clock_speed =
14356 ilk_get_display_clock_speed;
a7c66cd8
VS
14357 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14358 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
14359 dev_priv->display.get_display_clock_speed =
14360 i945_get_display_clock_speed;
14361 else if (IS_I915G(dev))
14362 dev_priv->display.get_display_clock_speed =
14363 i915_get_display_clock_speed;
257a7ffc 14364 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14365 dev_priv->display.get_display_clock_speed =
14366 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
14367 else if (IS_PINEVIEW(dev))
14368 dev_priv->display.get_display_clock_speed =
14369 pnv_get_display_clock_speed;
e70236a8
JB
14370 else if (IS_I915GM(dev))
14371 dev_priv->display.get_display_clock_speed =
14372 i915gm_get_display_clock_speed;
14373 else if (IS_I865G(dev))
14374 dev_priv->display.get_display_clock_speed =
14375 i865_get_display_clock_speed;
f0f8a9ce 14376 else if (IS_I85X(dev))
e70236a8
JB
14377 dev_priv->display.get_display_clock_speed =
14378 i855_get_display_clock_speed;
14379 else /* 852, 830 */
14380 dev_priv->display.get_display_clock_speed =
14381 i830_get_display_clock_speed;
14382
7c10a2b5 14383 if (IS_GEN5(dev)) {
3bb11b53 14384 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14385 } else if (IS_GEN6(dev)) {
14386 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14387 } else if (IS_IVYBRIDGE(dev)) {
14388 /* FIXME: detect B0+ stepping and use auto training */
14389 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14390 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14391 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
30a970c6
JB
14392 } else if (IS_VALLEYVIEW(dev)) {
14393 dev_priv->display.modeset_global_resources =
14394 valleyview_modeset_global_resources;
f8437dd1
VK
14395 } else if (IS_BROXTON(dev)) {
14396 dev_priv->display.modeset_global_resources =
14397 broxton_modeset_global_resources;
e70236a8 14398 }
8c9f3aaf 14399
8c9f3aaf
JB
14400 switch (INTEL_INFO(dev)->gen) {
14401 case 2:
14402 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14403 break;
14404
14405 case 3:
14406 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14407 break;
14408
14409 case 4:
14410 case 5:
14411 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14412 break;
14413
14414 case 6:
14415 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14416 break;
7c9017e5 14417 case 7:
4e0bbc31 14418 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14419 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14420 break;
830c81db 14421 case 9:
ba343e02
TU
14422 /* Drop through - unsupported since execlist only. */
14423 default:
14424 /* Default just returns -ENODEV to indicate unsupported */
14425 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14426 }
7bd688cd
JN
14427
14428 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
14429
14430 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
14431}
14432
b690e96c
JB
14433/*
14434 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14435 * resume, or other times. This quirk makes sure that's the case for
14436 * affected systems.
14437 */
0206e353 14438static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14439{
14440 struct drm_i915_private *dev_priv = dev->dev_private;
14441
14442 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14443 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14444}
14445
b6b5d049
VS
14446static void quirk_pipeb_force(struct drm_device *dev)
14447{
14448 struct drm_i915_private *dev_priv = dev->dev_private;
14449
14450 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14451 DRM_INFO("applying pipe b force quirk\n");
14452}
14453
435793df
KP
14454/*
14455 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14456 */
14457static void quirk_ssc_force_disable(struct drm_device *dev)
14458{
14459 struct drm_i915_private *dev_priv = dev->dev_private;
14460 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14461 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14462}
14463
4dca20ef 14464/*
5a15ab5b
CE
14465 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14466 * brightness value
4dca20ef
CE
14467 */
14468static void quirk_invert_brightness(struct drm_device *dev)
14469{
14470 struct drm_i915_private *dev_priv = dev->dev_private;
14471 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14472 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14473}
14474
9c72cc6f
SD
14475/* Some VBT's incorrectly indicate no backlight is present */
14476static void quirk_backlight_present(struct drm_device *dev)
14477{
14478 struct drm_i915_private *dev_priv = dev->dev_private;
14479 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14480 DRM_INFO("applying backlight present quirk\n");
14481}
14482
b690e96c
JB
14483struct intel_quirk {
14484 int device;
14485 int subsystem_vendor;
14486 int subsystem_device;
14487 void (*hook)(struct drm_device *dev);
14488};
14489
5f85f176
EE
14490/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14491struct intel_dmi_quirk {
14492 void (*hook)(struct drm_device *dev);
14493 const struct dmi_system_id (*dmi_id_list)[];
14494};
14495
14496static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14497{
14498 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14499 return 1;
14500}
14501
14502static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14503 {
14504 .dmi_id_list = &(const struct dmi_system_id[]) {
14505 {
14506 .callback = intel_dmi_reverse_brightness,
14507 .ident = "NCR Corporation",
14508 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14509 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14510 },
14511 },
14512 { } /* terminating entry */
14513 },
14514 .hook = quirk_invert_brightness,
14515 },
14516};
14517
c43b5634 14518static struct intel_quirk intel_quirks[] = {
b690e96c 14519 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 14520 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 14521
b690e96c
JB
14522 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14523 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14524
b690e96c
JB
14525 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14526 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14527
5f080c0f
VS
14528 /* 830 needs to leave pipe A & dpll A up */
14529 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14530
b6b5d049
VS
14531 /* 830 needs to leave pipe B & dpll B up */
14532 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14533
435793df
KP
14534 /* Lenovo U160 cannot use SSC on LVDS */
14535 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14536
14537 /* Sony Vaio Y cannot use SSC on LVDS */
14538 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14539
be505f64
AH
14540 /* Acer Aspire 5734Z must invert backlight brightness */
14541 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14542
14543 /* Acer/eMachines G725 */
14544 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14545
14546 /* Acer/eMachines e725 */
14547 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14548
14549 /* Acer/Packard Bell NCL20 */
14550 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14551
14552 /* Acer Aspire 4736Z */
14553 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14554
14555 /* Acer Aspire 5336 */
14556 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14557
14558 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14559 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14560
dfb3d47b
SD
14561 /* Acer C720 Chromebook (Core i3 4005U) */
14562 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14563
b2a9601c 14564 /* Apple Macbook 2,1 (Core 2 T7400) */
14565 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14566
d4967d8c
SD
14567 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14568 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14569
14570 /* HP Chromebook 14 (Celeron 2955U) */
14571 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14572
14573 /* Dell Chromebook 11 */
14574 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14575};
14576
14577static void intel_init_quirks(struct drm_device *dev)
14578{
14579 struct pci_dev *d = dev->pdev;
14580 int i;
14581
14582 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14583 struct intel_quirk *q = &intel_quirks[i];
14584
14585 if (d->device == q->device &&
14586 (d->subsystem_vendor == q->subsystem_vendor ||
14587 q->subsystem_vendor == PCI_ANY_ID) &&
14588 (d->subsystem_device == q->subsystem_device ||
14589 q->subsystem_device == PCI_ANY_ID))
14590 q->hook(dev);
14591 }
5f85f176
EE
14592 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14593 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14594 intel_dmi_quirks[i].hook(dev);
14595 }
b690e96c
JB
14596}
14597
9cce37f4
JB
14598/* Disable the VGA plane that we never use */
14599static void i915_disable_vga(struct drm_device *dev)
14600{
14601 struct drm_i915_private *dev_priv = dev->dev_private;
14602 u8 sr1;
766aa1c4 14603 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 14604
2b37c616 14605 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 14606 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14607 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14608 sr1 = inb(VGA_SR_DATA);
14609 outb(sr1 | 1<<5, VGA_SR_DATA);
14610 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14611 udelay(300);
14612
01f5a626 14613 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14614 POSTING_READ(vga_reg);
14615}
14616
f817586c
DV
14617void intel_modeset_init_hw(struct drm_device *dev)
14618{
a8f78b58
ED
14619 intel_prepare_ddi(dev);
14620
f8bf63fd
VS
14621 if (IS_VALLEYVIEW(dev))
14622 vlv_update_cdclk(dev);
14623
f817586c
DV
14624 intel_init_clock_gating(dev);
14625
8090c6b9 14626 intel_enable_gt_powersave(dev);
f817586c
DV
14627}
14628
79e53945
JB
14629void intel_modeset_init(struct drm_device *dev)
14630{
652c393a 14631 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 14632 int sprite, ret;
8cc87b75 14633 enum pipe pipe;
46f297fb 14634 struct intel_crtc *crtc;
79e53945
JB
14635
14636 drm_mode_config_init(dev);
14637
14638 dev->mode_config.min_width = 0;
14639 dev->mode_config.min_height = 0;
14640
019d96cb
DA
14641 dev->mode_config.preferred_depth = 24;
14642 dev->mode_config.prefer_shadow = 1;
14643
25bab385
TU
14644 dev->mode_config.allow_fb_modifiers = true;
14645
e6ecefaa 14646 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14647
b690e96c
JB
14648 intel_init_quirks(dev);
14649
1fa61106
ED
14650 intel_init_pm(dev);
14651
e3c74757
BW
14652 if (INTEL_INFO(dev)->num_pipes == 0)
14653 return;
14654
e70236a8 14655 intel_init_display(dev);
7c10a2b5 14656 intel_init_audio(dev);
e70236a8 14657
a6c45cf0
CW
14658 if (IS_GEN2(dev)) {
14659 dev->mode_config.max_width = 2048;
14660 dev->mode_config.max_height = 2048;
14661 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
14662 dev->mode_config.max_width = 4096;
14663 dev->mode_config.max_height = 4096;
79e53945 14664 } else {
a6c45cf0
CW
14665 dev->mode_config.max_width = 8192;
14666 dev->mode_config.max_height = 8192;
79e53945 14667 }
068be561 14668
dc41c154
VS
14669 if (IS_845G(dev) || IS_I865G(dev)) {
14670 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14671 dev->mode_config.cursor_height = 1023;
14672 } else if (IS_GEN2(dev)) {
068be561
DL
14673 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14674 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14675 } else {
14676 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14677 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14678 }
14679
5d4545ae 14680 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 14681
28c97730 14682 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
14683 INTEL_INFO(dev)->num_pipes,
14684 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 14685
055e393f 14686 for_each_pipe(dev_priv, pipe) {
8cc87b75 14687 intel_crtc_init(dev, pipe);
3bdcfc0c 14688 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 14689 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 14690 if (ret)
06da8da2 14691 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 14692 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 14693 }
79e53945
JB
14694 }
14695
f42bb70d
JB
14696 intel_init_dpio(dev);
14697
e72f9fbf 14698 intel_shared_dpll_init(dev);
ee7b9f93 14699
9cce37f4
JB
14700 /* Just disable it once at startup */
14701 i915_disable_vga(dev);
79e53945 14702 intel_setup_outputs(dev);
11be49eb
CW
14703
14704 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 14705 intel_fbc_disable(dev);
fa9fa083 14706
6e9f798d 14707 drm_modeset_lock_all(dev);
fa9fa083 14708 intel_modeset_setup_hw_state(dev, false);
6e9f798d 14709 drm_modeset_unlock_all(dev);
46f297fb 14710
d3fcc808 14711 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
14712 if (!crtc->active)
14713 continue;
14714
46f297fb 14715 /*
46f297fb
JB
14716 * Note that reserving the BIOS fb up front prevents us
14717 * from stuffing other stolen allocations like the ring
14718 * on top. This prevents some ugliness at boot time, and
14719 * can even allow for smooth boot transitions if the BIOS
14720 * fb is large enough for the active pipe configuration.
14721 */
5724dbd1
DL
14722 if (dev_priv->display.get_initial_plane_config) {
14723 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
14724 &crtc->plane_config);
14725 /*
14726 * If the fb is shared between multiple heads, we'll
14727 * just get the first one.
14728 */
f6936e29 14729 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
46f297fb 14730 }
46f297fb 14731 }
2c7111db
CW
14732}
14733
7fad798e
DV
14734static void intel_enable_pipe_a(struct drm_device *dev)
14735{
14736 struct intel_connector *connector;
14737 struct drm_connector *crt = NULL;
14738 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 14739 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
14740
14741 /* We can't just switch on the pipe A, we need to set things up with a
14742 * proper mode and output configuration. As a gross hack, enable pipe A
14743 * by enabling the load detect pipe once. */
3a3371ff 14744 for_each_intel_connector(dev, connector) {
7fad798e
DV
14745 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14746 crt = &connector->base;
14747 break;
14748 }
14749 }
14750
14751 if (!crt)
14752 return;
14753
208bf9fd 14754 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 14755 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
14756}
14757
fa555837
DV
14758static bool
14759intel_check_plane_mapping(struct intel_crtc *crtc)
14760{
7eb552ae
BW
14761 struct drm_device *dev = crtc->base.dev;
14762 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
14763 u32 reg, val;
14764
7eb552ae 14765 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
14766 return true;
14767
14768 reg = DSPCNTR(!crtc->plane);
14769 val = I915_READ(reg);
14770
14771 if ((val & DISPLAY_PLANE_ENABLE) &&
14772 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14773 return false;
14774
14775 return true;
14776}
14777
24929352
DV
14778static void intel_sanitize_crtc(struct intel_crtc *crtc)
14779{
14780 struct drm_device *dev = crtc->base.dev;
14781 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 14782 u32 reg;
24929352 14783
24929352 14784 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 14785 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
14786 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14787
d3eaf884 14788 /* restore vblank interrupts to correct state */
9625604c 14789 drm_crtc_vblank_reset(&crtc->base);
d297e103
VS
14790 if (crtc->active) {
14791 update_scanline_offset(crtc);
9625604c
DV
14792 drm_crtc_vblank_on(&crtc->base);
14793 }
d3eaf884 14794
24929352 14795 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
14796 * disable the crtc (and hence change the state) if it is wrong. Note
14797 * that gen4+ has a fixed plane -> pipe mapping. */
14798 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
14799 struct intel_connector *connector;
14800 bool plane;
14801
24929352
DV
14802 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14803 crtc->base.base.id);
14804
14805 /* Pipe has the wrong plane attached and the plane is active.
14806 * Temporarily change the plane mapping and disable everything
14807 * ... */
14808 plane = crtc->plane;
b70709a6 14809 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 14810 crtc->plane = !plane;
ce22dba9 14811 intel_crtc_disable_planes(&crtc->base);
24929352
DV
14812 dev_priv->display.crtc_disable(&crtc->base);
14813 crtc->plane = plane;
14814
14815 /* ... and break all links. */
3a3371ff 14816 for_each_intel_connector(dev, connector) {
24929352
DV
14817 if (connector->encoder->base.crtc != &crtc->base)
14818 continue;
14819
7f1950fb
EE
14820 connector->base.dpms = DRM_MODE_DPMS_OFF;
14821 connector->base.encoder = NULL;
24929352 14822 }
7f1950fb
EE
14823 /* multiple connectors may have the same encoder:
14824 * handle them and break crtc link separately */
3a3371ff 14825 for_each_intel_connector(dev, connector)
7f1950fb
EE
14826 if (connector->encoder->base.crtc == &crtc->base) {
14827 connector->encoder->base.crtc = NULL;
14828 connector->encoder->connectors_active = false;
14829 }
24929352
DV
14830
14831 WARN_ON(crtc->active);
83d65738 14832 crtc->base.state->enable = false;
24929352
DV
14833 crtc->base.enabled = false;
14834 }
24929352 14835
7fad798e
DV
14836 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14837 crtc->pipe == PIPE_A && !crtc->active) {
14838 /* BIOS forgot to enable pipe A, this mostly happens after
14839 * resume. Force-enable the pipe to fix this, the update_dpms
14840 * call below we restore the pipe to the right state, but leave
14841 * the required bits on. */
14842 intel_enable_pipe_a(dev);
14843 }
14844
24929352
DV
14845 /* Adjust the state of the output pipe according to whether we
14846 * have active connectors/encoders. */
14847 intel_crtc_update_dpms(&crtc->base);
14848
83d65738 14849 if (crtc->active != crtc->base.state->enable) {
24929352
DV
14850 struct intel_encoder *encoder;
14851
14852 /* This can happen either due to bugs in the get_hw_state
14853 * functions or because the pipe is force-enabled due to the
14854 * pipe A quirk. */
14855 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14856 crtc->base.base.id,
83d65738 14857 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
14858 crtc->active ? "enabled" : "disabled");
14859
83d65738 14860 crtc->base.state->enable = crtc->active;
24929352
DV
14861 crtc->base.enabled = crtc->active;
14862
14863 /* Because we only establish the connector -> encoder ->
14864 * crtc links if something is active, this means the
14865 * crtc is now deactivated. Break the links. connector
14866 * -> encoder links are only establish when things are
14867 * actually up, hence no need to break them. */
14868 WARN_ON(crtc->active);
14869
14870 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14871 WARN_ON(encoder->connectors_active);
14872 encoder->base.crtc = NULL;
14873 }
14874 }
c5ab3bc0 14875
a3ed6aad 14876 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
14877 /*
14878 * We start out with underrun reporting disabled to avoid races.
14879 * For correct bookkeeping mark this on active crtcs.
14880 *
c5ab3bc0
DV
14881 * Also on gmch platforms we dont have any hardware bits to
14882 * disable the underrun reporting. Which means we need to start
14883 * out with underrun reporting disabled also on inactive pipes,
14884 * since otherwise we'll complain about the garbage we read when
14885 * e.g. coming up after runtime pm.
14886 *
4cc31489
DV
14887 * No protection against concurrent access is required - at
14888 * worst a fifo underrun happens which also sets this to false.
14889 */
14890 crtc->cpu_fifo_underrun_disabled = true;
14891 crtc->pch_fifo_underrun_disabled = true;
14892 }
24929352
DV
14893}
14894
14895static void intel_sanitize_encoder(struct intel_encoder *encoder)
14896{
14897 struct intel_connector *connector;
14898 struct drm_device *dev = encoder->base.dev;
14899
14900 /* We need to check both for a crtc link (meaning that the
14901 * encoder is active and trying to read from a pipe) and the
14902 * pipe itself being active. */
14903 bool has_active_crtc = encoder->base.crtc &&
14904 to_intel_crtc(encoder->base.crtc)->active;
14905
14906 if (encoder->connectors_active && !has_active_crtc) {
14907 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14908 encoder->base.base.id,
8e329a03 14909 encoder->base.name);
24929352
DV
14910
14911 /* Connector is active, but has no active pipe. This is
14912 * fallout from our resume register restoring. Disable
14913 * the encoder manually again. */
14914 if (encoder->base.crtc) {
14915 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14916 encoder->base.base.id,
8e329a03 14917 encoder->base.name);
24929352 14918 encoder->disable(encoder);
a62d1497
VS
14919 if (encoder->post_disable)
14920 encoder->post_disable(encoder);
24929352 14921 }
7f1950fb
EE
14922 encoder->base.crtc = NULL;
14923 encoder->connectors_active = false;
24929352
DV
14924
14925 /* Inconsistent output/port/pipe state happens presumably due to
14926 * a bug in one of the get_hw_state functions. Or someplace else
14927 * in our code, like the register restore mess on resume. Clamp
14928 * things to off as a safer default. */
3a3371ff 14929 for_each_intel_connector(dev, connector) {
24929352
DV
14930 if (connector->encoder != encoder)
14931 continue;
7f1950fb
EE
14932 connector->base.dpms = DRM_MODE_DPMS_OFF;
14933 connector->base.encoder = NULL;
24929352
DV
14934 }
14935 }
14936 /* Enabled encoders without active connectors will be fixed in
14937 * the crtc fixup. */
14938}
14939
04098753 14940void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
14941{
14942 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 14943 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 14944
04098753
ID
14945 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14946 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14947 i915_disable_vga(dev);
14948 }
14949}
14950
14951void i915_redisable_vga(struct drm_device *dev)
14952{
14953 struct drm_i915_private *dev_priv = dev->dev_private;
14954
8dc8a27c
PZ
14955 /* This function can be called both from intel_modeset_setup_hw_state or
14956 * at a very early point in our resume sequence, where the power well
14957 * structures are not yet restored. Since this function is at a very
14958 * paranoid "someone might have enabled VGA while we were not looking"
14959 * level, just check if the power well is enabled instead of trying to
14960 * follow the "don't touch the power well if we don't need it" policy
14961 * the rest of the driver uses. */
f458ebbc 14962 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
14963 return;
14964
04098753 14965 i915_redisable_vga_power_on(dev);
0fde901f
KM
14966}
14967
98ec7739
VS
14968static bool primary_get_hw_state(struct intel_crtc *crtc)
14969{
14970 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14971
14972 if (!crtc->active)
14973 return false;
14974
14975 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14976}
14977
30e984df 14978static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
14979{
14980 struct drm_i915_private *dev_priv = dev->dev_private;
14981 enum pipe pipe;
24929352
DV
14982 struct intel_crtc *crtc;
14983 struct intel_encoder *encoder;
14984 struct intel_connector *connector;
5358901f 14985 int i;
24929352 14986
d3fcc808 14987 for_each_intel_crtc(dev, crtc) {
b70709a6
ML
14988 struct drm_plane *primary = crtc->base.primary;
14989 struct intel_plane_state *plane_state;
14990
6e3c9717 14991 memset(crtc->config, 0, sizeof(*crtc->config));
3b117c8f 14992
6e3c9717 14993 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
9953599b 14994
0e8ffe1b 14995 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 14996 crtc->config);
24929352 14997
83d65738 14998 crtc->base.state->enable = crtc->active;
24929352 14999 crtc->base.enabled = crtc->active;
b70709a6
ML
15000
15001 plane_state = to_intel_plane_state(primary->state);
15002 plane_state->visible = primary_get_hw_state(crtc);
24929352
DV
15003
15004 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15005 crtc->base.base.id,
15006 crtc->active ? "enabled" : "disabled");
15007 }
15008
5358901f
DV
15009 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15010 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15011
3e369b76
ACO
15012 pll->on = pll->get_hw_state(dev_priv, pll,
15013 &pll->config.hw_state);
5358901f 15014 pll->active = 0;
3e369b76 15015 pll->config.crtc_mask = 0;
d3fcc808 15016 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 15017 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 15018 pll->active++;
3e369b76 15019 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 15020 }
5358901f 15021 }
5358901f 15022
1e6f2ddc 15023 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 15024 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 15025
3e369b76 15026 if (pll->config.crtc_mask)
bd2bb1b9 15027 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
15028 }
15029
b2784e15 15030 for_each_intel_encoder(dev, encoder) {
24929352
DV
15031 pipe = 0;
15032
15033 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
15034 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15035 encoder->base.crtc = &crtc->base;
6e3c9717 15036 encoder->get_config(encoder, crtc->config);
24929352
DV
15037 } else {
15038 encoder->base.crtc = NULL;
15039 }
15040
15041 encoder->connectors_active = false;
6f2bcceb 15042 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 15043 encoder->base.base.id,
8e329a03 15044 encoder->base.name,
24929352 15045 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 15046 pipe_name(pipe));
24929352
DV
15047 }
15048
3a3371ff 15049 for_each_intel_connector(dev, connector) {
24929352
DV
15050 if (connector->get_hw_state(connector)) {
15051 connector->base.dpms = DRM_MODE_DPMS_ON;
15052 connector->encoder->connectors_active = true;
15053 connector->base.encoder = &connector->encoder->base;
15054 } else {
15055 connector->base.dpms = DRM_MODE_DPMS_OFF;
15056 connector->base.encoder = NULL;
15057 }
15058 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15059 connector->base.base.id,
c23cc417 15060 connector->base.name,
24929352
DV
15061 connector->base.encoder ? "enabled" : "disabled");
15062 }
30e984df
DV
15063}
15064
15065/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15066 * and i915 state tracking structures. */
15067void intel_modeset_setup_hw_state(struct drm_device *dev,
15068 bool force_restore)
15069{
15070 struct drm_i915_private *dev_priv = dev->dev_private;
15071 enum pipe pipe;
30e984df
DV
15072 struct intel_crtc *crtc;
15073 struct intel_encoder *encoder;
35c95375 15074 int i;
30e984df
DV
15075
15076 intel_modeset_readout_hw_state(dev);
24929352 15077
babea61d
JB
15078 /*
15079 * Now that we have the config, copy it to each CRTC struct
15080 * Note that this could go away if we move to using crtc_config
15081 * checking everywhere.
15082 */
d3fcc808 15083 for_each_intel_crtc(dev, crtc) {
d330a953 15084 if (crtc->active && i915.fastboot) {
6e3c9717
ACO
15085 intel_mode_from_pipe_config(&crtc->base.mode,
15086 crtc->config);
babea61d
JB
15087 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15088 crtc->base.base.id);
15089 drm_mode_debug_printmodeline(&crtc->base.mode);
15090 }
15091 }
15092
24929352 15093 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 15094 for_each_intel_encoder(dev, encoder) {
24929352
DV
15095 intel_sanitize_encoder(encoder);
15096 }
15097
055e393f 15098 for_each_pipe(dev_priv, pipe) {
24929352
DV
15099 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15100 intel_sanitize_crtc(crtc);
6e3c9717
ACO
15101 intel_dump_pipe_config(crtc, crtc->config,
15102 "[setup_hw_state]");
24929352 15103 }
9a935856 15104
d29b2f9d
ACO
15105 intel_modeset_update_connector_atomic_state(dev);
15106
35c95375
DV
15107 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15108 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15109
15110 if (!pll->on || pll->active)
15111 continue;
15112
15113 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15114
15115 pll->disable(dev_priv, pll);
15116 pll->on = false;
15117 }
15118
3078999f
PB
15119 if (IS_GEN9(dev))
15120 skl_wm_get_hw_state(dev);
15121 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
15122 ilk_wm_get_hw_state(dev);
15123
45e2b5f6 15124 if (force_restore) {
7d0bc1ea
VS
15125 i915_redisable_vga(dev);
15126
f30da187
DV
15127 /*
15128 * We need to use raw interfaces for restoring state to avoid
15129 * checking (bogus) intermediate states.
15130 */
055e393f 15131 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
15132 struct drm_crtc *crtc =
15133 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187 15134
83a57153 15135 intel_crtc_restore_mode(crtc);
45e2b5f6
DV
15136 }
15137 } else {
15138 intel_modeset_update_staged_output_state(dev);
15139 }
8af6cf88
DV
15140
15141 intel_modeset_check_state(dev);
2c7111db
CW
15142}
15143
15144void intel_modeset_gem_init(struct drm_device *dev)
15145{
92122789 15146 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 15147 struct drm_crtc *c;
2ff8fde1 15148 struct drm_i915_gem_object *obj;
e0d6149b 15149 int ret;
484b41dd 15150
ae48434c
ID
15151 mutex_lock(&dev->struct_mutex);
15152 intel_init_gt_powersave(dev);
15153 mutex_unlock(&dev->struct_mutex);
15154
92122789
JB
15155 /*
15156 * There may be no VBT; and if the BIOS enabled SSC we can
15157 * just keep using it to avoid unnecessary flicker. Whereas if the
15158 * BIOS isn't using it, don't assume it will work even if the VBT
15159 * indicates as much.
15160 */
15161 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15162 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15163 DREF_SSC1_ENABLE);
15164
1833b134 15165 intel_modeset_init_hw(dev);
02e792fb
DV
15166
15167 intel_setup_overlay(dev);
484b41dd
JB
15168
15169 /*
15170 * Make sure any fbs we allocated at startup are properly
15171 * pinned & fenced. When we do the allocation it's too early
15172 * for this.
15173 */
70e1e0ec 15174 for_each_crtc(dev, c) {
2ff8fde1
MR
15175 obj = intel_fb_obj(c->primary->fb);
15176 if (obj == NULL)
484b41dd
JB
15177 continue;
15178
e0d6149b
TU
15179 mutex_lock(&dev->struct_mutex);
15180 ret = intel_pin_and_fence_fb_obj(c->primary,
15181 c->primary->fb,
15182 c->primary->state,
15183 NULL);
15184 mutex_unlock(&dev->struct_mutex);
15185 if (ret) {
484b41dd
JB
15186 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15187 to_intel_crtc(c)->pipe);
66e514c1
DA
15188 drm_framebuffer_unreference(c->primary->fb);
15189 c->primary->fb = NULL;
afd65eb4 15190 update_state_fb(c->primary);
484b41dd
JB
15191 }
15192 }
0962c3c9
VS
15193
15194 intel_backlight_register(dev);
79e53945
JB
15195}
15196
4932e2c3
ID
15197void intel_connector_unregister(struct intel_connector *intel_connector)
15198{
15199 struct drm_connector *connector = &intel_connector->base;
15200
15201 intel_panel_destroy_backlight(connector);
34ea3d38 15202 drm_connector_unregister(connector);
4932e2c3
ID
15203}
15204
79e53945
JB
15205void intel_modeset_cleanup(struct drm_device *dev)
15206{
652c393a 15207 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 15208 struct drm_connector *connector;
652c393a 15209
2eb5252e
ID
15210 intel_disable_gt_powersave(dev);
15211
0962c3c9
VS
15212 intel_backlight_unregister(dev);
15213
fd0c0642
DV
15214 /*
15215 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15216 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15217 * experience fancy races otherwise.
15218 */
2aeb7d3a 15219 intel_irq_uninstall(dev_priv);
eb21b92b 15220
fd0c0642
DV
15221 /*
15222 * Due to the hpd irq storm handling the hotplug work can re-arm the
15223 * poll handlers. Hence disable polling after hpd handling is shut down.
15224 */
f87ea761 15225 drm_kms_helper_poll_fini(dev);
fd0c0642 15226
652c393a
JB
15227 mutex_lock(&dev->struct_mutex);
15228
723bfd70
JB
15229 intel_unregister_dsm_handler();
15230
7ff0ebcc 15231 intel_fbc_disable(dev);
e70236a8 15232
69341a5e
KH
15233 mutex_unlock(&dev->struct_mutex);
15234
1630fe75
CW
15235 /* flush any delayed tasks or pending work */
15236 flush_scheduled_work();
15237
db31af1d
JN
15238 /* destroy the backlight and sysfs files before encoders/connectors */
15239 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
15240 struct intel_connector *intel_connector;
15241
15242 intel_connector = to_intel_connector(connector);
15243 intel_connector->unregister(intel_connector);
db31af1d 15244 }
d9255d57 15245
79e53945 15246 drm_mode_config_cleanup(dev);
4d7bb011
DV
15247
15248 intel_cleanup_overlay(dev);
ae48434c
ID
15249
15250 mutex_lock(&dev->struct_mutex);
15251 intel_cleanup_gt_powersave(dev);
15252 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15253}
15254
f1c79df3
ZW
15255/*
15256 * Return which encoder is currently attached for connector.
15257 */
df0e9248 15258struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15259{
df0e9248
CW
15260 return &intel_attached_encoder(connector)->base;
15261}
f1c79df3 15262
df0e9248
CW
15263void intel_connector_attach_encoder(struct intel_connector *connector,
15264 struct intel_encoder *encoder)
15265{
15266 connector->encoder = encoder;
15267 drm_mode_connector_attach_encoder(&connector->base,
15268 &encoder->base);
79e53945 15269}
28d52043
DA
15270
15271/*
15272 * set vga decode state - true == enable VGA decode
15273 */
15274int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15275{
15276 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15277 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15278 u16 gmch_ctrl;
15279
75fa041d
CW
15280 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15281 DRM_ERROR("failed to read control word\n");
15282 return -EIO;
15283 }
15284
c0cc8a55
CW
15285 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15286 return 0;
15287
28d52043
DA
15288 if (state)
15289 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15290 else
15291 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15292
15293 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15294 DRM_ERROR("failed to write control word\n");
15295 return -EIO;
15296 }
15297
28d52043
DA
15298 return 0;
15299}
c4a1d9e4 15300
c4a1d9e4 15301struct intel_display_error_state {
ff57f1b0
PZ
15302
15303 u32 power_well_driver;
15304
63b66e5b
CW
15305 int num_transcoders;
15306
c4a1d9e4
CW
15307 struct intel_cursor_error_state {
15308 u32 control;
15309 u32 position;
15310 u32 base;
15311 u32 size;
52331309 15312 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15313
15314 struct intel_pipe_error_state {
ddf9c536 15315 bool power_domain_on;
c4a1d9e4 15316 u32 source;
f301b1e1 15317 u32 stat;
52331309 15318 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15319
15320 struct intel_plane_error_state {
15321 u32 control;
15322 u32 stride;
15323 u32 size;
15324 u32 pos;
15325 u32 addr;
15326 u32 surface;
15327 u32 tile_offset;
52331309 15328 } plane[I915_MAX_PIPES];
63b66e5b
CW
15329
15330 struct intel_transcoder_error_state {
ddf9c536 15331 bool power_domain_on;
63b66e5b
CW
15332 enum transcoder cpu_transcoder;
15333
15334 u32 conf;
15335
15336 u32 htotal;
15337 u32 hblank;
15338 u32 hsync;
15339 u32 vtotal;
15340 u32 vblank;
15341 u32 vsync;
15342 } transcoder[4];
c4a1d9e4
CW
15343};
15344
15345struct intel_display_error_state *
15346intel_display_capture_error_state(struct drm_device *dev)
15347{
fbee40df 15348 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 15349 struct intel_display_error_state *error;
63b66e5b
CW
15350 int transcoders[] = {
15351 TRANSCODER_A,
15352 TRANSCODER_B,
15353 TRANSCODER_C,
15354 TRANSCODER_EDP,
15355 };
c4a1d9e4
CW
15356 int i;
15357
63b66e5b
CW
15358 if (INTEL_INFO(dev)->num_pipes == 0)
15359 return NULL;
15360
9d1cb914 15361 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15362 if (error == NULL)
15363 return NULL;
15364
190be112 15365 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
15366 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15367
055e393f 15368 for_each_pipe(dev_priv, i) {
ddf9c536 15369 error->pipe[i].power_domain_on =
f458ebbc
DV
15370 __intel_display_power_is_enabled(dev_priv,
15371 POWER_DOMAIN_PIPE(i));
ddf9c536 15372 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15373 continue;
15374
5efb3e28
VS
15375 error->cursor[i].control = I915_READ(CURCNTR(i));
15376 error->cursor[i].position = I915_READ(CURPOS(i));
15377 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15378
15379 error->plane[i].control = I915_READ(DSPCNTR(i));
15380 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 15381 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 15382 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15383 error->plane[i].pos = I915_READ(DSPPOS(i));
15384 }
ca291363
PZ
15385 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15386 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
15387 if (INTEL_INFO(dev)->gen >= 4) {
15388 error->plane[i].surface = I915_READ(DSPSURF(i));
15389 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15390 }
15391
c4a1d9e4 15392 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15393
3abfce77 15394 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 15395 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15396 }
15397
15398 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15399 if (HAS_DDI(dev_priv->dev))
15400 error->num_transcoders++; /* Account for eDP. */
15401
15402 for (i = 0; i < error->num_transcoders; i++) {
15403 enum transcoder cpu_transcoder = transcoders[i];
15404
ddf9c536 15405 error->transcoder[i].power_domain_on =
f458ebbc 15406 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15407 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15408 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15409 continue;
15410
63b66e5b
CW
15411 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15412
15413 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15414 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15415 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15416 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15417 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15418 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15419 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15420 }
15421
15422 return error;
15423}
15424
edc3d884
MK
15425#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15426
c4a1d9e4 15427void
edc3d884 15428intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15429 struct drm_device *dev,
15430 struct intel_display_error_state *error)
15431{
055e393f 15432 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15433 int i;
15434
63b66e5b
CW
15435 if (!error)
15436 return;
15437
edc3d884 15438 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15439 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15440 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15441 error->power_well_driver);
055e393f 15442 for_each_pipe(dev_priv, i) {
edc3d884 15443 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
15444 err_printf(m, " Power: %s\n",
15445 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 15446 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15447 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15448
15449 err_printf(m, "Plane [%d]:\n", i);
15450 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15451 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15452 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15453 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15454 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15455 }
4b71a570 15456 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15457 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15458 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15459 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15460 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15461 }
15462
edc3d884
MK
15463 err_printf(m, "Cursor [%d]:\n", i);
15464 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15465 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15466 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15467 }
63b66e5b
CW
15468
15469 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 15470 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 15471 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
15472 err_printf(m, " Power: %s\n",
15473 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
15474 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15475 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15476 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15477 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15478 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15479 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15480 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15481 }
c4a1d9e4 15482}
e2fcdaa9
VS
15483
15484void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15485{
15486 struct intel_crtc *crtc;
15487
15488 for_each_intel_crtc(dev, crtc) {
15489 struct intel_unpin_work *work;
e2fcdaa9 15490
5e2d7afc 15491 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
15492
15493 work = crtc->unpin_work;
15494
15495 if (work && work->event &&
15496 work->event->base.file_priv == file) {
15497 kfree(work->event);
15498 work->event = NULL;
15499 }
15500
5e2d7afc 15501 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
15502 }
15503}
This page took 3.199858 seconds and 5 git commands to generate.