]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915: Fix 'suspedn' typo
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_display.c
CommitLineData
79e53945
JB
1/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
618563e3 27#include <linux/dmi.h>
c1c7af60
JB
28#include <linux/module.h>
29#include <linux/input.h>
79e53945 30#include <linux/i2c.h>
7662c8bd 31#include <linux/kernel.h>
5a0e3ad6 32#include <linux/slab.h>
9cce37f4 33#include <linux/vgaarb.h>
e0dac65e 34#include <drm/drm_edid.h>
760285e7 35#include <drm/drmP.h>
79e53945 36#include "intel_drv.h"
760285e7 37#include <drm/i915_drm.h>
79e53945 38#include "i915_drv.h"
e5510fac 39#include "i915_trace.h"
319c1d42 40#include <drm/drm_atomic.h>
c196e1d6 41#include <drm/drm_atomic_helper.h>
760285e7
DH
42#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
465c120c
MR
44#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
c0f372b3 46#include <linux/dma_remapping.h>
79e53945 47
465c120c 48/* Primary plane formats for gen <= 3 */
568db4f2 49static const uint32_t i8xx_primary_formats[] = {
67fe7dc5
DL
50 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
465c120c 52 DRM_FORMAT_XRGB1555,
67fe7dc5
DL
53 DRM_FORMAT_XRGB8888,
54 DRM_FORMAT_ARGB8888,
465c120c
MR
55};
56
57/* Primary plane formats for gen >= 4 */
568db4f2 58static const uint32_t i965_primary_formats[] = {
67fe7dc5
DL
59 DRM_FORMAT_C8,
60 DRM_FORMAT_RGB565,
61 DRM_FORMAT_XRGB8888,
465c120c 62 DRM_FORMAT_XBGR8888,
67fe7dc5 63 DRM_FORMAT_ARGB8888,
465c120c
MR
64 DRM_FORMAT_ABGR8888,
65 DRM_FORMAT_XRGB2101010,
465c120c 66 DRM_FORMAT_XBGR2101010,
465c120c
MR
67};
68
3d7d6510
MR
69/* Cursor formats */
70static const uint32_t intel_cursor_formats[] = {
71 DRM_FORMAT_ARGB8888,
72};
73
6b383a7f 74static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 75
f1f644dc 76static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 77 struct intel_crtc_state *pipe_config);
18442d08 78static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 79 struct intel_crtc_state *pipe_config);
f1f644dc 80
8c7b5ccb 81static int intel_set_mode(struct drm_crtc *crtc,
83a57153 82 struct drm_atomic_state *state);
eb1bfe80
JB
83static int intel_framebuffer_init(struct drm_device *dev,
84 struct intel_framebuffer *ifb,
85 struct drm_mode_fb_cmd2 *mode_cmd,
86 struct drm_i915_gem_object *obj);
5b18e57c
DV
87static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
88static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
29407aab 89static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
90 struct intel_link_m_n *m_n,
91 struct intel_link_m_n *m2_n2);
29407aab 92static void ironlake_set_pipeconf(struct drm_crtc *crtc);
229fca97
DV
93static void haswell_set_pipeconf(struct drm_crtc *crtc);
94static void intel_set_pipe_csc(struct drm_crtc *crtc);
d288f65f 95static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 96 const struct intel_crtc_state *pipe_config);
d288f65f 97static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 98 const struct intel_crtc_state *pipe_config);
ea2c67bb
MR
99static void intel_begin_crtc_commit(struct drm_crtc *crtc);
100static void intel_finish_crtc_commit(struct drm_crtc *crtc);
549e2bfb
CK
101static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
102 struct intel_crtc_state *crtc_state);
5ab7b0b7
ID
103static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
104 int num_connectors);
ce22dba9
ML
105static void intel_crtc_enable_planes(struct drm_crtc *crtc);
106static void intel_crtc_disable_planes(struct drm_crtc *crtc);
e7457a9a 107
0e32b39c
DA
108static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
109{
110 if (!connector->mst_port)
111 return connector->encoder;
112 else
113 return &connector->mst_port->mst_encoders[pipe]->base;
114}
115
79e53945 116typedef struct {
0206e353 117 int min, max;
79e53945
JB
118} intel_range_t;
119
120typedef struct {
0206e353
AJ
121 int dot_limit;
122 int p2_slow, p2_fast;
79e53945
JB
123} intel_p2_t;
124
d4906093
ML
125typedef struct intel_limit intel_limit_t;
126struct intel_limit {
0206e353
AJ
127 intel_range_t dot, vco, n, m, m1, m2, p, p1;
128 intel_p2_t p2;
d4906093 129};
79e53945 130
d2acd215
DV
131int
132intel_pch_rawclk(struct drm_device *dev)
133{
134 struct drm_i915_private *dev_priv = dev->dev_private;
135
136 WARN_ON(!HAS_PCH_SPLIT(dev));
137
138 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
139}
140
021357ac
CW
141static inline u32 /* units of 100MHz */
142intel_fdi_link_freq(struct drm_device *dev)
143{
8b99e68c
CW
144 if (IS_GEN5(dev)) {
145 struct drm_i915_private *dev_priv = dev->dev_private;
146 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
147 } else
148 return 27;
021357ac
CW
149}
150
5d536e28 151static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 152 .dot = { .min = 25000, .max = 350000 },
9c333719 153 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 154 .n = { .min = 2, .max = 16 },
0206e353
AJ
155 .m = { .min = 96, .max = 140 },
156 .m1 = { .min = 18, .max = 26 },
157 .m2 = { .min = 6, .max = 16 },
158 .p = { .min = 4, .max = 128 },
159 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
160 .p2 = { .dot_limit = 165000,
161 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
162};
163
5d536e28
DV
164static const intel_limit_t intel_limits_i8xx_dvo = {
165 .dot = { .min = 25000, .max = 350000 },
9c333719 166 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 167 .n = { .min = 2, .max = 16 },
5d536e28
DV
168 .m = { .min = 96, .max = 140 },
169 .m1 = { .min = 18, .max = 26 },
170 .m2 = { .min = 6, .max = 16 },
171 .p = { .min = 4, .max = 128 },
172 .p1 = { .min = 2, .max = 33 },
173 .p2 = { .dot_limit = 165000,
174 .p2_slow = 4, .p2_fast = 4 },
175};
176
e4b36699 177static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 178 .dot = { .min = 25000, .max = 350000 },
9c333719 179 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 180 .n = { .min = 2, .max = 16 },
0206e353
AJ
181 .m = { .min = 96, .max = 140 },
182 .m1 = { .min = 18, .max = 26 },
183 .m2 = { .min = 6, .max = 16 },
184 .p = { .min = 4, .max = 128 },
185 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
186 .p2 = { .dot_limit = 165000,
187 .p2_slow = 14, .p2_fast = 7 },
e4b36699 188};
273e27ca 189
e4b36699 190static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
191 .dot = { .min = 20000, .max = 400000 },
192 .vco = { .min = 1400000, .max = 2800000 },
193 .n = { .min = 1, .max = 6 },
194 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
195 .m1 = { .min = 8, .max = 18 },
196 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
197 .p = { .min = 5, .max = 80 },
198 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
199 .p2 = { .dot_limit = 200000,
200 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
201};
202
203static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
204 .dot = { .min = 20000, .max = 400000 },
205 .vco = { .min = 1400000, .max = 2800000 },
206 .n = { .min = 1, .max = 6 },
207 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
208 .m1 = { .min = 8, .max = 18 },
209 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
210 .p = { .min = 7, .max = 98 },
211 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
212 .p2 = { .dot_limit = 112000,
213 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
214};
215
273e27ca 216
e4b36699 217static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
218 .dot = { .min = 25000, .max = 270000 },
219 .vco = { .min = 1750000, .max = 3500000},
220 .n = { .min = 1, .max = 4 },
221 .m = { .min = 104, .max = 138 },
222 .m1 = { .min = 17, .max = 23 },
223 .m2 = { .min = 5, .max = 11 },
224 .p = { .min = 10, .max = 30 },
225 .p1 = { .min = 1, .max = 3},
226 .p2 = { .dot_limit = 270000,
227 .p2_slow = 10,
228 .p2_fast = 10
044c7c41 229 },
e4b36699
KP
230};
231
232static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
233 .dot = { .min = 22000, .max = 400000 },
234 .vco = { .min = 1750000, .max = 3500000},
235 .n = { .min = 1, .max = 4 },
236 .m = { .min = 104, .max = 138 },
237 .m1 = { .min = 16, .max = 23 },
238 .m2 = { .min = 5, .max = 11 },
239 .p = { .min = 5, .max = 80 },
240 .p1 = { .min = 1, .max = 8},
241 .p2 = { .dot_limit = 165000,
242 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
243};
244
245static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
246 .dot = { .min = 20000, .max = 115000 },
247 .vco = { .min = 1750000, .max = 3500000 },
248 .n = { .min = 1, .max = 3 },
249 .m = { .min = 104, .max = 138 },
250 .m1 = { .min = 17, .max = 23 },
251 .m2 = { .min = 5, .max = 11 },
252 .p = { .min = 28, .max = 112 },
253 .p1 = { .min = 2, .max = 8 },
254 .p2 = { .dot_limit = 0,
255 .p2_slow = 14, .p2_fast = 14
044c7c41 256 },
e4b36699
KP
257};
258
259static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
260 .dot = { .min = 80000, .max = 224000 },
261 .vco = { .min = 1750000, .max = 3500000 },
262 .n = { .min = 1, .max = 3 },
263 .m = { .min = 104, .max = 138 },
264 .m1 = { .min = 17, .max = 23 },
265 .m2 = { .min = 5, .max = 11 },
266 .p = { .min = 14, .max = 42 },
267 .p1 = { .min = 2, .max = 6 },
268 .p2 = { .dot_limit = 0,
269 .p2_slow = 7, .p2_fast = 7
044c7c41 270 },
e4b36699
KP
271};
272
f2b115e6 273static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
274 .dot = { .min = 20000, .max = 400000},
275 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 276 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
277 .n = { .min = 3, .max = 6 },
278 .m = { .min = 2, .max = 256 },
273e27ca 279 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
280 .m1 = { .min = 0, .max = 0 },
281 .m2 = { .min = 0, .max = 254 },
282 .p = { .min = 5, .max = 80 },
283 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
284 .p2 = { .dot_limit = 200000,
285 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
286};
287
f2b115e6 288static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
289 .dot = { .min = 20000, .max = 400000 },
290 .vco = { .min = 1700000, .max = 3500000 },
291 .n = { .min = 3, .max = 6 },
292 .m = { .min = 2, .max = 256 },
293 .m1 = { .min = 0, .max = 0 },
294 .m2 = { .min = 0, .max = 254 },
295 .p = { .min = 7, .max = 112 },
296 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
297 .p2 = { .dot_limit = 112000,
298 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
299};
300
273e27ca
EA
301/* Ironlake / Sandybridge
302 *
303 * We calculate clock using (register_value + 2) for N/M1/M2, so here
304 * the range value for them is (actual_value - 2).
305 */
b91ad0ec 306static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
307 .dot = { .min = 25000, .max = 350000 },
308 .vco = { .min = 1760000, .max = 3510000 },
309 .n = { .min = 1, .max = 5 },
310 .m = { .min = 79, .max = 127 },
311 .m1 = { .min = 12, .max = 22 },
312 .m2 = { .min = 5, .max = 9 },
313 .p = { .min = 5, .max = 80 },
314 .p1 = { .min = 1, .max = 8 },
315 .p2 = { .dot_limit = 225000,
316 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
317};
318
b91ad0ec 319static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
320 .dot = { .min = 25000, .max = 350000 },
321 .vco = { .min = 1760000, .max = 3510000 },
322 .n = { .min = 1, .max = 3 },
323 .m = { .min = 79, .max = 118 },
324 .m1 = { .min = 12, .max = 22 },
325 .m2 = { .min = 5, .max = 9 },
326 .p = { .min = 28, .max = 112 },
327 .p1 = { .min = 2, .max = 8 },
328 .p2 = { .dot_limit = 225000,
329 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
330};
331
332static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
333 .dot = { .min = 25000, .max = 350000 },
334 .vco = { .min = 1760000, .max = 3510000 },
335 .n = { .min = 1, .max = 3 },
336 .m = { .min = 79, .max = 127 },
337 .m1 = { .min = 12, .max = 22 },
338 .m2 = { .min = 5, .max = 9 },
339 .p = { .min = 14, .max = 56 },
340 .p1 = { .min = 2, .max = 8 },
341 .p2 = { .dot_limit = 225000,
342 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
343};
344
273e27ca 345/* LVDS 100mhz refclk limits. */
b91ad0ec 346static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
347 .dot = { .min = 25000, .max = 350000 },
348 .vco = { .min = 1760000, .max = 3510000 },
349 .n = { .min = 1, .max = 2 },
350 .m = { .min = 79, .max = 126 },
351 .m1 = { .min = 12, .max = 22 },
352 .m2 = { .min = 5, .max = 9 },
353 .p = { .min = 28, .max = 112 },
0206e353 354 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
355 .p2 = { .dot_limit = 225000,
356 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
357};
358
359static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
360 .dot = { .min = 25000, .max = 350000 },
361 .vco = { .min = 1760000, .max = 3510000 },
362 .n = { .min = 1, .max = 3 },
363 .m = { .min = 79, .max = 126 },
364 .m1 = { .min = 12, .max = 22 },
365 .m2 = { .min = 5, .max = 9 },
366 .p = { .min = 14, .max = 42 },
0206e353 367 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
368 .p2 = { .dot_limit = 225000,
369 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
370};
371
dc730512 372static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
373 /*
374 * These are the data rate limits (measured in fast clocks)
375 * since those are the strictest limits we have. The fast
376 * clock and actual rate limits are more relaxed, so checking
377 * them would make no difference.
378 */
379 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 380 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 381 .n = { .min = 1, .max = 7 },
a0c4da24
JB
382 .m1 = { .min = 2, .max = 3 },
383 .m2 = { .min = 11, .max = 156 },
b99ab663 384 .p1 = { .min = 2, .max = 3 },
5fdc9c49 385 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
386};
387
ef9348c8
CML
388static const intel_limit_t intel_limits_chv = {
389 /*
390 * These are the data rate limits (measured in fast clocks)
391 * since those are the strictest limits we have. The fast
392 * clock and actual rate limits are more relaxed, so checking
393 * them would make no difference.
394 */
395 .dot = { .min = 25000 * 5, .max = 540000 * 5},
17fe1021 396 .vco = { .min = 4800000, .max = 6480000 },
ef9348c8
CML
397 .n = { .min = 1, .max = 1 },
398 .m1 = { .min = 2, .max = 2 },
399 .m2 = { .min = 24 << 22, .max = 175 << 22 },
400 .p1 = { .min = 2, .max = 4 },
401 .p2 = { .p2_slow = 1, .p2_fast = 14 },
402};
403
5ab7b0b7
ID
404static const intel_limit_t intel_limits_bxt = {
405 /* FIXME: find real dot limits */
406 .dot = { .min = 0, .max = INT_MAX },
407 .vco = { .min = 4800000, .max = 6480000 },
408 .n = { .min = 1, .max = 1 },
409 .m1 = { .min = 2, .max = 2 },
410 /* FIXME: find real m2 limits */
411 .m2 = { .min = 2 << 22, .max = 255 << 22 },
412 .p1 = { .min = 2, .max = 4 },
413 .p2 = { .p2_slow = 1, .p2_fast = 20 },
414};
415
6b4bf1c4
VS
416static void vlv_clock(int refclk, intel_clock_t *clock)
417{
418 clock->m = clock->m1 * clock->m2;
419 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
420 if (WARN_ON(clock->n == 0 || clock->p == 0))
421 return;
fb03ac01
VS
422 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
423 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
424}
425
e0638cdf
PZ
426/**
427 * Returns whether any output on the specified pipe is of the specified type
428 */
4093561b 429bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
e0638cdf 430{
409ee761 431 struct drm_device *dev = crtc->base.dev;
e0638cdf
PZ
432 struct intel_encoder *encoder;
433
409ee761 434 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
e0638cdf
PZ
435 if (encoder->type == type)
436 return true;
437
438 return false;
439}
440
d0737e1d
ACO
441/**
442 * Returns whether any output on the specified pipe will have the specified
443 * type after a staged modeset is complete, i.e., the same as
444 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
445 * encoder->crtc.
446 */
a93e255f
ACO
447static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
448 int type)
d0737e1d 449{
a93e255f 450 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 451 struct drm_connector *connector;
a93e255f 452 struct drm_connector_state *connector_state;
d0737e1d 453 struct intel_encoder *encoder;
a93e255f
ACO
454 int i, num_connectors = 0;
455
da3ced29 456 for_each_connector_in_state(state, connector, connector_state, i) {
a93e255f
ACO
457 if (connector_state->crtc != crtc_state->base.crtc)
458 continue;
459
460 num_connectors++;
d0737e1d 461
a93e255f
ACO
462 encoder = to_intel_encoder(connector_state->best_encoder);
463 if (encoder->type == type)
d0737e1d 464 return true;
a93e255f
ACO
465 }
466
467 WARN_ON(num_connectors == 0);
d0737e1d
ACO
468
469 return false;
470}
471
a93e255f
ACO
472static const intel_limit_t *
473intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
2c07245f 474{
a93e255f 475 struct drm_device *dev = crtc_state->base.crtc->dev;
2c07245f 476 const intel_limit_t *limit;
b91ad0ec 477
a93e255f 478 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 479 if (intel_is_dual_link_lvds(dev)) {
1b894b59 480 if (refclk == 100000)
b91ad0ec
ZW
481 limit = &intel_limits_ironlake_dual_lvds_100m;
482 else
483 limit = &intel_limits_ironlake_dual_lvds;
484 } else {
1b894b59 485 if (refclk == 100000)
b91ad0ec
ZW
486 limit = &intel_limits_ironlake_single_lvds_100m;
487 else
488 limit = &intel_limits_ironlake_single_lvds;
489 }
c6bb3538 490 } else
b91ad0ec 491 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
492
493 return limit;
494}
495
a93e255f
ACO
496static const intel_limit_t *
497intel_g4x_limit(struct intel_crtc_state *crtc_state)
044c7c41 498{
a93e255f 499 struct drm_device *dev = crtc_state->base.crtc->dev;
044c7c41
ML
500 const intel_limit_t *limit;
501
a93e255f 502 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 503 if (intel_is_dual_link_lvds(dev))
e4b36699 504 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 505 else
e4b36699 506 limit = &intel_limits_g4x_single_channel_lvds;
a93e255f
ACO
507 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
508 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
e4b36699 509 limit = &intel_limits_g4x_hdmi;
a93e255f 510 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
e4b36699 511 limit = &intel_limits_g4x_sdvo;
044c7c41 512 } else /* The option is for other outputs */
e4b36699 513 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
514
515 return limit;
516}
517
a93e255f
ACO
518static const intel_limit_t *
519intel_limit(struct intel_crtc_state *crtc_state, int refclk)
79e53945 520{
a93e255f 521 struct drm_device *dev = crtc_state->base.crtc->dev;
79e53945
JB
522 const intel_limit_t *limit;
523
5ab7b0b7
ID
524 if (IS_BROXTON(dev))
525 limit = &intel_limits_bxt;
526 else if (HAS_PCH_SPLIT(dev))
a93e255f 527 limit = intel_ironlake_limit(crtc_state, refclk);
2c07245f 528 else if (IS_G4X(dev)) {
a93e255f 529 limit = intel_g4x_limit(crtc_state);
f2b115e6 530 } else if (IS_PINEVIEW(dev)) {
a93e255f 531 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
f2b115e6 532 limit = &intel_limits_pineview_lvds;
2177832f 533 else
f2b115e6 534 limit = &intel_limits_pineview_sdvo;
ef9348c8
CML
535 } else if (IS_CHERRYVIEW(dev)) {
536 limit = &intel_limits_chv;
a0c4da24 537 } else if (IS_VALLEYVIEW(dev)) {
dc730512 538 limit = &intel_limits_vlv;
a6c45cf0 539 } else if (!IS_GEN2(dev)) {
a93e255f 540 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
a6c45cf0
CW
541 limit = &intel_limits_i9xx_lvds;
542 else
543 limit = &intel_limits_i9xx_sdvo;
79e53945 544 } else {
a93e255f 545 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
e4b36699 546 limit = &intel_limits_i8xx_lvds;
a93e255f 547 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
e4b36699 548 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
549 else
550 limit = &intel_limits_i8xx_dac;
79e53945
JB
551 }
552 return limit;
553}
554
f2b115e6
AJ
555/* m1 is reserved as 0 in Pineview, n is a ring counter */
556static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 557{
2177832f
SL
558 clock->m = clock->m2 + 2;
559 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
560 if (WARN_ON(clock->n == 0 || clock->p == 0))
561 return;
fb03ac01
VS
562 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
563 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
564}
565
7429e9d4
DV
566static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
567{
568 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
569}
570
ac58c3f0 571static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 572{
7429e9d4 573 clock->m = i9xx_dpll_compute_m(clock);
79e53945 574 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
575 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
576 return;
fb03ac01
VS
577 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
578 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
579}
580
ef9348c8
CML
581static void chv_clock(int refclk, intel_clock_t *clock)
582{
583 clock->m = clock->m1 * clock->m2;
584 clock->p = clock->p1 * clock->p2;
585 if (WARN_ON(clock->n == 0 || clock->p == 0))
586 return;
587 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
588 clock->n << 22);
589 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
590}
591
7c04d1d9 592#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
593/**
594 * Returns whether the given set of divisors are valid for a given refclk with
595 * the given connectors.
596 */
597
1b894b59
CW
598static bool intel_PLL_is_valid(struct drm_device *dev,
599 const intel_limit_t *limit,
600 const intel_clock_t *clock)
79e53945 601{
f01b7962
VS
602 if (clock->n < limit->n.min || limit->n.max < clock->n)
603 INTELPllInvalid("n out of range\n");
79e53945 604 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 605 INTELPllInvalid("p1 out of range\n");
79e53945 606 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 607 INTELPllInvalid("m2 out of range\n");
79e53945 608 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 609 INTELPllInvalid("m1 out of range\n");
f01b7962 610
5ab7b0b7 611 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
f01b7962
VS
612 if (clock->m1 <= clock->m2)
613 INTELPllInvalid("m1 <= m2\n");
614
5ab7b0b7 615 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
f01b7962
VS
616 if (clock->p < limit->p.min || limit->p.max < clock->p)
617 INTELPllInvalid("p out of range\n");
618 if (clock->m < limit->m.min || limit->m.max < clock->m)
619 INTELPllInvalid("m out of range\n");
620 }
621
79e53945 622 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 623 INTELPllInvalid("vco out of range\n");
79e53945
JB
624 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
625 * connector, etc., rather than just a single range.
626 */
627 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 628 INTELPllInvalid("dot out of range\n");
79e53945
JB
629
630 return true;
631}
632
d4906093 633static bool
a93e255f
ACO
634i9xx_find_best_dpll(const intel_limit_t *limit,
635 struct intel_crtc_state *crtc_state,
cec2f356
SP
636 int target, int refclk, intel_clock_t *match_clock,
637 intel_clock_t *best_clock)
79e53945 638{
a93e255f 639 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 640 struct drm_device *dev = crtc->base.dev;
79e53945 641 intel_clock_t clock;
79e53945
JB
642 int err = target;
643
a93e255f 644 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 645 /*
a210b028
DV
646 * For LVDS just rely on its current settings for dual-channel.
647 * We haven't figured out how to reliably set up different
648 * single/dual channel state, if we even can.
79e53945 649 */
1974cad0 650 if (intel_is_dual_link_lvds(dev))
79e53945
JB
651 clock.p2 = limit->p2.p2_fast;
652 else
653 clock.p2 = limit->p2.p2_slow;
654 } else {
655 if (target < limit->p2.dot_limit)
656 clock.p2 = limit->p2.p2_slow;
657 else
658 clock.p2 = limit->p2.p2_fast;
659 }
660
0206e353 661 memset(best_clock, 0, sizeof(*best_clock));
79e53945 662
42158660
ZY
663 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
664 clock.m1++) {
665 for (clock.m2 = limit->m2.min;
666 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 667 if (clock.m2 >= clock.m1)
42158660
ZY
668 break;
669 for (clock.n = limit->n.min;
670 clock.n <= limit->n.max; clock.n++) {
671 for (clock.p1 = limit->p1.min;
672 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
673 int this_err;
674
ac58c3f0
DV
675 i9xx_clock(refclk, &clock);
676 if (!intel_PLL_is_valid(dev, limit,
677 &clock))
678 continue;
679 if (match_clock &&
680 clock.p != match_clock->p)
681 continue;
682
683 this_err = abs(clock.dot - target);
684 if (this_err < err) {
685 *best_clock = clock;
686 err = this_err;
687 }
688 }
689 }
690 }
691 }
692
693 return (err != target);
694}
695
696static bool
a93e255f
ACO
697pnv_find_best_dpll(const intel_limit_t *limit,
698 struct intel_crtc_state *crtc_state,
ee9300bb
DV
699 int target, int refclk, intel_clock_t *match_clock,
700 intel_clock_t *best_clock)
79e53945 701{
a93e255f 702 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 703 struct drm_device *dev = crtc->base.dev;
79e53945 704 intel_clock_t clock;
79e53945
JB
705 int err = target;
706
a93e255f 707 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
79e53945 708 /*
a210b028
DV
709 * For LVDS just rely on its current settings for dual-channel.
710 * We haven't figured out how to reliably set up different
711 * single/dual channel state, if we even can.
79e53945 712 */
1974cad0 713 if (intel_is_dual_link_lvds(dev))
79e53945
JB
714 clock.p2 = limit->p2.p2_fast;
715 else
716 clock.p2 = limit->p2.p2_slow;
717 } else {
718 if (target < limit->p2.dot_limit)
719 clock.p2 = limit->p2.p2_slow;
720 else
721 clock.p2 = limit->p2.p2_fast;
722 }
723
0206e353 724 memset(best_clock, 0, sizeof(*best_clock));
79e53945 725
42158660
ZY
726 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
727 clock.m1++) {
728 for (clock.m2 = limit->m2.min;
729 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
730 for (clock.n = limit->n.min;
731 clock.n <= limit->n.max; clock.n++) {
732 for (clock.p1 = limit->p1.min;
733 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
734 int this_err;
735
ac58c3f0 736 pineview_clock(refclk, &clock);
1b894b59
CW
737 if (!intel_PLL_is_valid(dev, limit,
738 &clock))
79e53945 739 continue;
cec2f356
SP
740 if (match_clock &&
741 clock.p != match_clock->p)
742 continue;
79e53945
JB
743
744 this_err = abs(clock.dot - target);
745 if (this_err < err) {
746 *best_clock = clock;
747 err = this_err;
748 }
749 }
750 }
751 }
752 }
753
754 return (err != target);
755}
756
d4906093 757static bool
a93e255f
ACO
758g4x_find_best_dpll(const intel_limit_t *limit,
759 struct intel_crtc_state *crtc_state,
ee9300bb
DV
760 int target, int refclk, intel_clock_t *match_clock,
761 intel_clock_t *best_clock)
d4906093 762{
a93e255f 763 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 764 struct drm_device *dev = crtc->base.dev;
d4906093
ML
765 intel_clock_t clock;
766 int max_n;
767 bool found;
6ba770dc
AJ
768 /* approximately equals target * 0.00585 */
769 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
770 found = false;
771
a93e255f 772 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
1974cad0 773 if (intel_is_dual_link_lvds(dev))
d4906093
ML
774 clock.p2 = limit->p2.p2_fast;
775 else
776 clock.p2 = limit->p2.p2_slow;
777 } else {
778 if (target < limit->p2.dot_limit)
779 clock.p2 = limit->p2.p2_slow;
780 else
781 clock.p2 = limit->p2.p2_fast;
782 }
783
784 memset(best_clock, 0, sizeof(*best_clock));
785 max_n = limit->n.max;
f77f13e2 786 /* based on hardware requirement, prefer smaller n to precision */
d4906093 787 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 788 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
789 for (clock.m1 = limit->m1.max;
790 clock.m1 >= limit->m1.min; clock.m1--) {
791 for (clock.m2 = limit->m2.max;
792 clock.m2 >= limit->m2.min; clock.m2--) {
793 for (clock.p1 = limit->p1.max;
794 clock.p1 >= limit->p1.min; clock.p1--) {
795 int this_err;
796
ac58c3f0 797 i9xx_clock(refclk, &clock);
1b894b59
CW
798 if (!intel_PLL_is_valid(dev, limit,
799 &clock))
d4906093 800 continue;
1b894b59
CW
801
802 this_err = abs(clock.dot - target);
d4906093
ML
803 if (this_err < err_most) {
804 *best_clock = clock;
805 err_most = this_err;
806 max_n = clock.n;
807 found = true;
808 }
809 }
810 }
811 }
812 }
2c07245f
ZW
813 return found;
814}
815
d5dd62bd
ID
816/*
817 * Check if the calculated PLL configuration is more optimal compared to the
818 * best configuration and error found so far. Return the calculated error.
819 */
820static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
821 const intel_clock_t *calculated_clock,
822 const intel_clock_t *best_clock,
823 unsigned int best_error_ppm,
824 unsigned int *error_ppm)
825{
9ca3ba01
ID
826 /*
827 * For CHV ignore the error and consider only the P value.
828 * Prefer a bigger P value based on HW requirements.
829 */
830 if (IS_CHERRYVIEW(dev)) {
831 *error_ppm = 0;
832
833 return calculated_clock->p > best_clock->p;
834 }
835
24be4e46
ID
836 if (WARN_ON_ONCE(!target_freq))
837 return false;
838
d5dd62bd
ID
839 *error_ppm = div_u64(1000000ULL *
840 abs(target_freq - calculated_clock->dot),
841 target_freq);
842 /*
843 * Prefer a better P value over a better (smaller) error if the error
844 * is small. Ensure this preference for future configurations too by
845 * setting the error to 0.
846 */
847 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
848 *error_ppm = 0;
849
850 return true;
851 }
852
853 return *error_ppm + 10 < best_error_ppm;
854}
855
a0c4da24 856static bool
a93e255f
ACO
857vlv_find_best_dpll(const intel_limit_t *limit,
858 struct intel_crtc_state *crtc_state,
ee9300bb
DV
859 int target, int refclk, intel_clock_t *match_clock,
860 intel_clock_t *best_clock)
a0c4da24 861{
a93e255f 862 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 863 struct drm_device *dev = crtc->base.dev;
6b4bf1c4 864 intel_clock_t clock;
69e4f900 865 unsigned int bestppm = 1000000;
27e639bf
VS
866 /* min update 19.2 MHz */
867 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 868 bool found = false;
a0c4da24 869
6b4bf1c4
VS
870 target *= 5; /* fast clock */
871
872 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
873
874 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 875 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 876 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 877 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 878 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 879 clock.p = clock.p1 * clock.p2;
a0c4da24 880 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 881 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
d5dd62bd 882 unsigned int ppm;
69e4f900 883
6b4bf1c4
VS
884 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
885 refclk * clock.m1);
886
887 vlv_clock(refclk, &clock);
43b0ac53 888
f01b7962
VS
889 if (!intel_PLL_is_valid(dev, limit,
890 &clock))
43b0ac53
VS
891 continue;
892
d5dd62bd
ID
893 if (!vlv_PLL_is_optimal(dev, target,
894 &clock,
895 best_clock,
896 bestppm, &ppm))
897 continue;
6b4bf1c4 898
d5dd62bd
ID
899 *best_clock = clock;
900 bestppm = ppm;
901 found = true;
a0c4da24
JB
902 }
903 }
904 }
905 }
a0c4da24 906
49e497ef 907 return found;
a0c4da24 908}
a4fc5ed6 909
ef9348c8 910static bool
a93e255f
ACO
911chv_find_best_dpll(const intel_limit_t *limit,
912 struct intel_crtc_state *crtc_state,
ef9348c8
CML
913 int target, int refclk, intel_clock_t *match_clock,
914 intel_clock_t *best_clock)
915{
a93e255f 916 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
a919ff14 917 struct drm_device *dev = crtc->base.dev;
9ca3ba01 918 unsigned int best_error_ppm;
ef9348c8
CML
919 intel_clock_t clock;
920 uint64_t m2;
921 int found = false;
922
923 memset(best_clock, 0, sizeof(*best_clock));
9ca3ba01 924 best_error_ppm = 1000000;
ef9348c8
CML
925
926 /*
927 * Based on hardware doc, the n always set to 1, and m1 always
928 * set to 2. If requires to support 200Mhz refclk, we need to
929 * revisit this because n may not 1 anymore.
930 */
931 clock.n = 1, clock.m1 = 2;
932 target *= 5; /* fast clock */
933
934 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
935 for (clock.p2 = limit->p2.p2_fast;
936 clock.p2 >= limit->p2.p2_slow;
937 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
9ca3ba01 938 unsigned int error_ppm;
ef9348c8
CML
939
940 clock.p = clock.p1 * clock.p2;
941
942 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
943 clock.n) << 22, refclk * clock.m1);
944
945 if (m2 > INT_MAX/clock.m1)
946 continue;
947
948 clock.m2 = m2;
949
950 chv_clock(refclk, &clock);
951
952 if (!intel_PLL_is_valid(dev, limit, &clock))
953 continue;
954
9ca3ba01
ID
955 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
956 best_error_ppm, &error_ppm))
957 continue;
958
959 *best_clock = clock;
960 best_error_ppm = error_ppm;
961 found = true;
ef9348c8
CML
962 }
963 }
964
965 return found;
966}
967
5ab7b0b7
ID
968bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
969 intel_clock_t *best_clock)
970{
971 int refclk = i9xx_get_refclk(crtc_state, 0);
972
973 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
974 target_clock, refclk, NULL, best_clock);
975}
976
20ddf665
VS
977bool intel_crtc_active(struct drm_crtc *crtc)
978{
979 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
980
981 /* Be paranoid as we can arrive here with only partial
982 * state retrieved from the hardware during setup.
983 *
241bfc38 984 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
985 * as Haswell has gained clock readout/fastboot support.
986 *
66e514c1 987 * We can ditch the crtc->primary->fb check as soon as we can
20ddf665 988 * properly reconstruct framebuffers.
c3d1f436
MR
989 *
990 * FIXME: The intel_crtc->active here should be switched to
991 * crtc->state->active once we have proper CRTC states wired up
992 * for atomic.
20ddf665 993 */
c3d1f436 994 return intel_crtc->active && crtc->primary->state->fb &&
6e3c9717 995 intel_crtc->config->base.adjusted_mode.crtc_clock;
20ddf665
VS
996}
997
a5c961d1
PZ
998enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
999 enum pipe pipe)
1000{
1001 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1002 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1003
6e3c9717 1004 return intel_crtc->config->cpu_transcoder;
a5c961d1
PZ
1005}
1006
fbf49ea2
VS
1007static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1008{
1009 struct drm_i915_private *dev_priv = dev->dev_private;
1010 u32 reg = PIPEDSL(pipe);
1011 u32 line1, line2;
1012 u32 line_mask;
1013
1014 if (IS_GEN2(dev))
1015 line_mask = DSL_LINEMASK_GEN2;
1016 else
1017 line_mask = DSL_LINEMASK_GEN3;
1018
1019 line1 = I915_READ(reg) & line_mask;
1020 mdelay(5);
1021 line2 = I915_READ(reg) & line_mask;
1022
1023 return line1 == line2;
1024}
1025
ab7ad7f6
KP
1026/*
1027 * intel_wait_for_pipe_off - wait for pipe to turn off
575f7ab7 1028 * @crtc: crtc whose pipe to wait for
9d0498a2
JB
1029 *
1030 * After disabling a pipe, we can't wait for vblank in the usual way,
1031 * spinning on the vblank interrupt status bit, since we won't actually
1032 * see an interrupt when the pipe is disabled.
1033 *
ab7ad7f6
KP
1034 * On Gen4 and above:
1035 * wait for the pipe register state bit to turn off
1036 *
1037 * Otherwise:
1038 * wait for the display line value to settle (it usually
1039 * ends up stopping at the start of the next frame).
58e10eb9 1040 *
9d0498a2 1041 */
575f7ab7 1042static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
9d0498a2 1043{
575f7ab7 1044 struct drm_device *dev = crtc->base.dev;
9d0498a2 1045 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 1046 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 1047 enum pipe pipe = crtc->pipe;
ab7ad7f6
KP
1048
1049 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 1050 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
1051
1052 /* Wait for the Pipe State to go off */
58e10eb9
CW
1053 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1054 100))
284637d9 1055 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1056 } else {
ab7ad7f6 1057 /* Wait for the display line to settle */
fbf49ea2 1058 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 1059 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 1060 }
79e53945
JB
1061}
1062
b0ea7d37
DL
1063/*
1064 * ibx_digital_port_connected - is the specified port connected?
1065 * @dev_priv: i915 private structure
1066 * @port: the port to test
1067 *
1068 * Returns true if @port is connected, false otherwise.
1069 */
1070bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1071 struct intel_digital_port *port)
1072{
1073 u32 bit;
1074
c36346e3 1075 if (HAS_PCH_IBX(dev_priv->dev)) {
eba905b2 1076 switch (port->port) {
c36346e3
DL
1077 case PORT_B:
1078 bit = SDE_PORTB_HOTPLUG;
1079 break;
1080 case PORT_C:
1081 bit = SDE_PORTC_HOTPLUG;
1082 break;
1083 case PORT_D:
1084 bit = SDE_PORTD_HOTPLUG;
1085 break;
1086 default:
1087 return true;
1088 }
1089 } else {
eba905b2 1090 switch (port->port) {
c36346e3
DL
1091 case PORT_B:
1092 bit = SDE_PORTB_HOTPLUG_CPT;
1093 break;
1094 case PORT_C:
1095 bit = SDE_PORTC_HOTPLUG_CPT;
1096 break;
1097 case PORT_D:
1098 bit = SDE_PORTD_HOTPLUG_CPT;
1099 break;
1100 default:
1101 return true;
1102 }
b0ea7d37
DL
1103 }
1104
1105 return I915_READ(SDEISR) & bit;
1106}
1107
b24e7179
JB
1108static const char *state_string(bool enabled)
1109{
1110 return enabled ? "on" : "off";
1111}
1112
1113/* Only for pre-ILK configs */
55607e8a
DV
1114void assert_pll(struct drm_i915_private *dev_priv,
1115 enum pipe pipe, bool state)
b24e7179
JB
1116{
1117 int reg;
1118 u32 val;
1119 bool cur_state;
1120
1121 reg = DPLL(pipe);
1122 val = I915_READ(reg);
1123 cur_state = !!(val & DPLL_VCO_ENABLE);
e2c719b7 1124 I915_STATE_WARN(cur_state != state,
b24e7179
JB
1125 "PLL state assertion failure (expected %s, current %s)\n",
1126 state_string(state), state_string(cur_state));
1127}
b24e7179 1128
23538ef1
JN
1129/* XXX: the dsi pll is shared between MIPI DSI ports */
1130static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1131{
1132 u32 val;
1133 bool cur_state;
1134
1135 mutex_lock(&dev_priv->dpio_lock);
1136 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1137 mutex_unlock(&dev_priv->dpio_lock);
1138
1139 cur_state = val & DSI_PLL_VCO_EN;
e2c719b7 1140 I915_STATE_WARN(cur_state != state,
23538ef1
JN
1141 "DSI PLL state assertion failure (expected %s, current %s)\n",
1142 state_string(state), state_string(cur_state));
1143}
1144#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1145#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1146
55607e8a 1147struct intel_shared_dpll *
e2b78267
DV
1148intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
1149{
1150 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1151
6e3c9717 1152 if (crtc->config->shared_dpll < 0)
e2b78267
DV
1153 return NULL;
1154
6e3c9717 1155 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
e2b78267
DV
1156}
1157
040484af 1158/* For ILK+ */
55607e8a
DV
1159void assert_shared_dpll(struct drm_i915_private *dev_priv,
1160 struct intel_shared_dpll *pll,
1161 bool state)
040484af 1162{
040484af 1163 bool cur_state;
5358901f 1164 struct intel_dpll_hw_state hw_state;
040484af 1165
92b27b08 1166 if (WARN (!pll,
46edb027 1167 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 1168 return;
ee7b9f93 1169
5358901f 1170 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
e2c719b7 1171 I915_STATE_WARN(cur_state != state,
5358901f
DV
1172 "%s assertion failure (expected %s, current %s)\n",
1173 pll->name, state_string(state), state_string(cur_state));
040484af 1174}
040484af
JB
1175
1176static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1177 enum pipe pipe, bool state)
1178{
1179 int reg;
1180 u32 val;
1181 bool cur_state;
ad80a810
PZ
1182 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1183 pipe);
040484af 1184
affa9354
PZ
1185 if (HAS_DDI(dev_priv->dev)) {
1186 /* DDI does not have a specific FDI_TX register */
ad80a810 1187 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 1188 val = I915_READ(reg);
ad80a810 1189 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1190 } else {
1191 reg = FDI_TX_CTL(pipe);
1192 val = I915_READ(reg);
1193 cur_state = !!(val & FDI_TX_ENABLE);
1194 }
e2c719b7 1195 I915_STATE_WARN(cur_state != state,
040484af
JB
1196 "FDI TX state assertion failure (expected %s, current %s)\n",
1197 state_string(state), state_string(cur_state));
1198}
1199#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1200#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1201
1202static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1203 enum pipe pipe, bool state)
1204{
1205 int reg;
1206 u32 val;
1207 bool cur_state;
1208
d63fa0dc
PZ
1209 reg = FDI_RX_CTL(pipe);
1210 val = I915_READ(reg);
1211 cur_state = !!(val & FDI_RX_ENABLE);
e2c719b7 1212 I915_STATE_WARN(cur_state != state,
040484af
JB
1213 "FDI RX state assertion failure (expected %s, current %s)\n",
1214 state_string(state), state_string(cur_state));
1215}
1216#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1217#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1218
1219static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1220 enum pipe pipe)
1221{
1222 int reg;
1223 u32 val;
1224
1225 /* ILK FDI PLL is always enabled */
3d13ef2e 1226 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1227 return;
1228
bf507ef7 1229 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1230 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1231 return;
1232
040484af
JB
1233 reg = FDI_TX_CTL(pipe);
1234 val = I915_READ(reg);
e2c719b7 1235 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
040484af
JB
1236}
1237
55607e8a
DV
1238void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1239 enum pipe pipe, bool state)
040484af
JB
1240{
1241 int reg;
1242 u32 val;
55607e8a 1243 bool cur_state;
040484af
JB
1244
1245 reg = FDI_RX_CTL(pipe);
1246 val = I915_READ(reg);
55607e8a 1247 cur_state = !!(val & FDI_RX_PLL_ENABLE);
e2c719b7 1248 I915_STATE_WARN(cur_state != state,
55607e8a
DV
1249 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1250 state_string(state), state_string(cur_state));
040484af
JB
1251}
1252
b680c37a
DV
1253void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1254 enum pipe pipe)
ea0760cf 1255{
bedd4dba
JN
1256 struct drm_device *dev = dev_priv->dev;
1257 int pp_reg;
ea0760cf
JB
1258 u32 val;
1259 enum pipe panel_pipe = PIPE_A;
0de3b485 1260 bool locked = true;
ea0760cf 1261
bedd4dba
JN
1262 if (WARN_ON(HAS_DDI(dev)))
1263 return;
1264
1265 if (HAS_PCH_SPLIT(dev)) {
1266 u32 port_sel;
1267
ea0760cf 1268 pp_reg = PCH_PP_CONTROL;
bedd4dba
JN
1269 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1270
1271 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1272 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1273 panel_pipe = PIPE_B;
1274 /* XXX: else fix for eDP */
1275 } else if (IS_VALLEYVIEW(dev)) {
1276 /* presumably write lock depends on pipe, not port select */
1277 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1278 panel_pipe = pipe;
ea0760cf
JB
1279 } else {
1280 pp_reg = PP_CONTROL;
bedd4dba
JN
1281 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1282 panel_pipe = PIPE_B;
ea0760cf
JB
1283 }
1284
1285 val = I915_READ(pp_reg);
1286 if (!(val & PANEL_POWER_ON) ||
ec49ba2d 1287 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
ea0760cf
JB
1288 locked = false;
1289
e2c719b7 1290 I915_STATE_WARN(panel_pipe == pipe && locked,
ea0760cf 1291 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1292 pipe_name(pipe));
ea0760cf
JB
1293}
1294
93ce0ba6
JN
1295static void assert_cursor(struct drm_i915_private *dev_priv,
1296 enum pipe pipe, bool state)
1297{
1298 struct drm_device *dev = dev_priv->dev;
1299 bool cur_state;
1300
d9d82081 1301 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1302 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1303 else
5efb3e28 1304 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
93ce0ba6 1305
e2c719b7 1306 I915_STATE_WARN(cur_state != state,
93ce0ba6
JN
1307 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1308 pipe_name(pipe), state_string(state), state_string(cur_state));
1309}
1310#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1311#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1312
b840d907
JB
1313void assert_pipe(struct drm_i915_private *dev_priv,
1314 enum pipe pipe, bool state)
b24e7179
JB
1315{
1316 int reg;
1317 u32 val;
63d7bbe9 1318 bool cur_state;
702e7a56
PZ
1319 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1320 pipe);
b24e7179 1321
b6b5d049
VS
1322 /* if we need the pipe quirk it must be always on */
1323 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1324 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
8e636784
DV
1325 state = true;
1326
f458ebbc 1327 if (!intel_display_power_is_enabled(dev_priv,
b97186f0 1328 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1329 cur_state = false;
1330 } else {
1331 reg = PIPECONF(cpu_transcoder);
1332 val = I915_READ(reg);
1333 cur_state = !!(val & PIPECONF_ENABLE);
1334 }
1335
e2c719b7 1336 I915_STATE_WARN(cur_state != state,
63d7bbe9 1337 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1338 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1339}
1340
931872fc
CW
1341static void assert_plane(struct drm_i915_private *dev_priv,
1342 enum plane plane, bool state)
b24e7179
JB
1343{
1344 int reg;
1345 u32 val;
931872fc 1346 bool cur_state;
b24e7179
JB
1347
1348 reg = DSPCNTR(plane);
1349 val = I915_READ(reg);
931872fc 1350 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
e2c719b7 1351 I915_STATE_WARN(cur_state != state,
931872fc
CW
1352 "plane %c assertion failure (expected %s, current %s)\n",
1353 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1354}
1355
931872fc
CW
1356#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1357#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1358
b24e7179
JB
1359static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1360 enum pipe pipe)
1361{
653e1026 1362 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1363 int reg, i;
1364 u32 val;
1365 int cur_pipe;
1366
653e1026
VS
1367 /* Primary planes are fixed to pipes on gen4+ */
1368 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1369 reg = DSPCNTR(pipe);
1370 val = I915_READ(reg);
e2c719b7 1371 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1372 "plane %c assertion failure, should be disabled but not\n",
1373 plane_name(pipe));
19ec1358 1374 return;
28c05794 1375 }
19ec1358 1376
b24e7179 1377 /* Need to check both planes against the pipe */
055e393f 1378 for_each_pipe(dev_priv, i) {
b24e7179
JB
1379 reg = DSPCNTR(i);
1380 val = I915_READ(reg);
1381 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1382 DISPPLANE_SEL_PIPE_SHIFT;
e2c719b7 1383 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1384 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1385 plane_name(i), pipe_name(pipe));
b24e7179
JB
1386 }
1387}
1388
19332d7a
JB
1389static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1390 enum pipe pipe)
1391{
20674eef 1392 struct drm_device *dev = dev_priv->dev;
1fe47785 1393 int reg, sprite;
19332d7a
JB
1394 u32 val;
1395
7feb8b88 1396 if (INTEL_INFO(dev)->gen >= 9) {
3bdcfc0c 1397 for_each_sprite(dev_priv, pipe, sprite) {
7feb8b88 1398 val = I915_READ(PLANE_CTL(pipe, sprite));
e2c719b7 1399 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
7feb8b88
DL
1400 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1401 sprite, pipe_name(pipe));
1402 }
1403 } else if (IS_VALLEYVIEW(dev)) {
3bdcfc0c 1404 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 1405 reg = SPCNTR(pipe, sprite);
20674eef 1406 val = I915_READ(reg);
e2c719b7 1407 I915_STATE_WARN(val & SP_ENABLE,
20674eef 1408 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1409 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1410 }
1411 } else if (INTEL_INFO(dev)->gen >= 7) {
1412 reg = SPRCTL(pipe);
19332d7a 1413 val = I915_READ(reg);
e2c719b7 1414 I915_STATE_WARN(val & SPRITE_ENABLE,
06da8da2 1415 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1416 plane_name(pipe), pipe_name(pipe));
1417 } else if (INTEL_INFO(dev)->gen >= 5) {
1418 reg = DVSCNTR(pipe);
19332d7a 1419 val = I915_READ(reg);
e2c719b7 1420 I915_STATE_WARN(val & DVS_ENABLE,
06da8da2 1421 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1422 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1423 }
1424}
1425
08c71e5e
VS
1426static void assert_vblank_disabled(struct drm_crtc *crtc)
1427{
e2c719b7 1428 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
08c71e5e
VS
1429 drm_crtc_vblank_put(crtc);
1430}
1431
89eff4be 1432static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1433{
1434 u32 val;
1435 bool enabled;
1436
e2c719b7 1437 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1438
92f2584a
JB
1439 val = I915_READ(PCH_DREF_CONTROL);
1440 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1441 DREF_SUPERSPREAD_SOURCE_MASK));
e2c719b7 1442 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
92f2584a
JB
1443}
1444
ab9412ba
DV
1445static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1446 enum pipe pipe)
92f2584a
JB
1447{
1448 int reg;
1449 u32 val;
1450 bool enabled;
1451
ab9412ba 1452 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1453 val = I915_READ(reg);
1454 enabled = !!(val & TRANS_ENABLE);
e2c719b7 1455 I915_STATE_WARN(enabled,
9db4a9c7
JB
1456 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1457 pipe_name(pipe));
92f2584a
JB
1458}
1459
4e634389
KP
1460static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1461 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1462{
1463 if ((val & DP_PORT_EN) == 0)
1464 return false;
1465
1466 if (HAS_PCH_CPT(dev_priv->dev)) {
1467 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1468 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1469 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1470 return false;
44f37d1f
CML
1471 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1472 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1473 return false;
f0575e92
KP
1474 } else {
1475 if ((val & DP_PIPE_MASK) != (pipe << 30))
1476 return false;
1477 }
1478 return true;
1479}
1480
1519b995
KP
1481static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1482 enum pipe pipe, u32 val)
1483{
dc0fa718 1484 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1485 return false;
1486
1487 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1488 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995 1489 return false;
44f37d1f
CML
1490 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1491 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1492 return false;
1519b995 1493 } else {
dc0fa718 1494 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1495 return false;
1496 }
1497 return true;
1498}
1499
1500static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1501 enum pipe pipe, u32 val)
1502{
1503 if ((val & LVDS_PORT_EN) == 0)
1504 return false;
1505
1506 if (HAS_PCH_CPT(dev_priv->dev)) {
1507 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1508 return false;
1509 } else {
1510 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1511 return false;
1512 }
1513 return true;
1514}
1515
1516static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1517 enum pipe pipe, u32 val)
1518{
1519 if ((val & ADPA_DAC_ENABLE) == 0)
1520 return false;
1521 if (HAS_PCH_CPT(dev_priv->dev)) {
1522 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1523 return false;
1524 } else {
1525 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1526 return false;
1527 }
1528 return true;
1529}
1530
291906f1 1531static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1532 enum pipe pipe, int reg, u32 port_sel)
291906f1 1533{
47a05eca 1534 u32 val = I915_READ(reg);
e2c719b7 1535 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1536 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1537 reg, pipe_name(pipe));
de9a35ab 1538
e2c719b7 1539 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
75c5da27 1540 && (val & DP_PIPEB_SELECT),
de9a35ab 1541 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1542}
1543
1544static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1545 enum pipe pipe, int reg)
1546{
47a05eca 1547 u32 val = I915_READ(reg);
e2c719b7 1548 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1549 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1550 reg, pipe_name(pipe));
de9a35ab 1551
e2c719b7 1552 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1553 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1554 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1555}
1556
1557static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1558 enum pipe pipe)
1559{
1560 int reg;
1561 u32 val;
291906f1 1562
f0575e92
KP
1563 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1564 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1565 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1566
1567 reg = PCH_ADPA;
1568 val = I915_READ(reg);
e2c719b7 1569 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1570 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1571 pipe_name(pipe));
291906f1
JB
1572
1573 reg = PCH_LVDS;
1574 val = I915_READ(reg);
e2c719b7 1575 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1576 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1577 pipe_name(pipe));
291906f1 1578
e2debe91
PZ
1579 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1580 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1581 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1582}
1583
40e9cf64
JB
1584static void intel_init_dpio(struct drm_device *dev)
1585{
1586 struct drm_i915_private *dev_priv = dev->dev_private;
1587
1588 if (!IS_VALLEYVIEW(dev))
1589 return;
1590
a09caddd
CML
1591 /*
1592 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1593 * CHV x1 PHY (DP/HDMI D)
1594 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1595 */
1596 if (IS_CHERRYVIEW(dev)) {
1597 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1598 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1599 } else {
1600 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1601 }
5382f5f3
JB
1602}
1603
d288f65f 1604static void vlv_enable_pll(struct intel_crtc *crtc,
5cec258b 1605 const struct intel_crtc_state *pipe_config)
87442f73 1606{
426115cf
DV
1607 struct drm_device *dev = crtc->base.dev;
1608 struct drm_i915_private *dev_priv = dev->dev_private;
1609 int reg = DPLL(crtc->pipe);
d288f65f 1610 u32 dpll = pipe_config->dpll_hw_state.dpll;
87442f73 1611
426115cf 1612 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1613
1614 /* No really, not for ILK+ */
1615 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1616
1617 /* PLL is protected by panel, make sure we can write it */
6a9e7363 1618 if (IS_MOBILE(dev_priv->dev))
426115cf 1619 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1620
426115cf
DV
1621 I915_WRITE(reg, dpll);
1622 POSTING_READ(reg);
1623 udelay(150);
1624
1625 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1626 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1627
d288f65f 1628 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
426115cf 1629 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1630
1631 /* We do this three times for luck */
426115cf 1632 I915_WRITE(reg, dpll);
87442f73
DV
1633 POSTING_READ(reg);
1634 udelay(150); /* wait for warmup */
426115cf 1635 I915_WRITE(reg, dpll);
87442f73
DV
1636 POSTING_READ(reg);
1637 udelay(150); /* wait for warmup */
426115cf 1638 I915_WRITE(reg, dpll);
87442f73
DV
1639 POSTING_READ(reg);
1640 udelay(150); /* wait for warmup */
1641}
1642
d288f65f 1643static void chv_enable_pll(struct intel_crtc *crtc,
5cec258b 1644 const struct intel_crtc_state *pipe_config)
9d556c99
CML
1645{
1646 struct drm_device *dev = crtc->base.dev;
1647 struct drm_i915_private *dev_priv = dev->dev_private;
1648 int pipe = crtc->pipe;
1649 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9d556c99
CML
1650 u32 tmp;
1651
1652 assert_pipe_disabled(dev_priv, crtc->pipe);
1653
1654 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1655
1656 mutex_lock(&dev_priv->dpio_lock);
1657
1658 /* Enable back the 10bit clock to display controller */
1659 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1660 tmp |= DPIO_DCLKP_EN;
1661 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1662
1663 /*
1664 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1665 */
1666 udelay(1);
1667
1668 /* Enable PLL */
d288f65f 1669 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
9d556c99
CML
1670
1671 /* Check PLL is locked */
a11b0703 1672 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
9d556c99
CML
1673 DRM_ERROR("PLL %d failed to lock\n", pipe);
1674
a11b0703 1675 /* not sure when this should be written */
d288f65f 1676 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
a11b0703
VS
1677 POSTING_READ(DPLL_MD(pipe));
1678
9d556c99
CML
1679 mutex_unlock(&dev_priv->dpio_lock);
1680}
1681
1c4e0274
VS
1682static int intel_num_dvo_pipes(struct drm_device *dev)
1683{
1684 struct intel_crtc *crtc;
1685 int count = 0;
1686
1687 for_each_intel_crtc(dev, crtc)
1688 count += crtc->active &&
409ee761 1689 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
1c4e0274
VS
1690
1691 return count;
1692}
1693
66e3d5c0 1694static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1695{
66e3d5c0
DV
1696 struct drm_device *dev = crtc->base.dev;
1697 struct drm_i915_private *dev_priv = dev->dev_private;
1698 int reg = DPLL(crtc->pipe);
6e3c9717 1699 u32 dpll = crtc->config->dpll_hw_state.dpll;
63d7bbe9 1700
66e3d5c0 1701 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1702
63d7bbe9 1703 /* No really, not for ILK+ */
3d13ef2e 1704 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1705
1706 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1707 if (IS_MOBILE(dev) && !IS_I830(dev))
1708 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1709
1c4e0274
VS
1710 /* Enable DVO 2x clock on both PLLs if necessary */
1711 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1712 /*
1713 * It appears to be important that we don't enable this
1714 * for the current pipe before otherwise configuring the
1715 * PLL. No idea how this should be handled if multiple
1716 * DVO outputs are enabled simultaneosly.
1717 */
1718 dpll |= DPLL_DVO_2X_MODE;
1719 I915_WRITE(DPLL(!crtc->pipe),
1720 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1721 }
66e3d5c0
DV
1722
1723 /* Wait for the clocks to stabilize. */
1724 POSTING_READ(reg);
1725 udelay(150);
1726
1727 if (INTEL_INFO(dev)->gen >= 4) {
1728 I915_WRITE(DPLL_MD(crtc->pipe),
6e3c9717 1729 crtc->config->dpll_hw_state.dpll_md);
66e3d5c0
DV
1730 } else {
1731 /* The pixel multiplier can only be updated once the
1732 * DPLL is enabled and the clocks are stable.
1733 *
1734 * So write it again.
1735 */
1736 I915_WRITE(reg, dpll);
1737 }
63d7bbe9
JB
1738
1739 /* We do this three times for luck */
66e3d5c0 1740 I915_WRITE(reg, dpll);
63d7bbe9
JB
1741 POSTING_READ(reg);
1742 udelay(150); /* wait for warmup */
66e3d5c0 1743 I915_WRITE(reg, dpll);
63d7bbe9
JB
1744 POSTING_READ(reg);
1745 udelay(150); /* wait for warmup */
66e3d5c0 1746 I915_WRITE(reg, dpll);
63d7bbe9
JB
1747 POSTING_READ(reg);
1748 udelay(150); /* wait for warmup */
1749}
1750
1751/**
50b44a44 1752 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1753 * @dev_priv: i915 private structure
1754 * @pipe: pipe PLL to disable
1755 *
1756 * Disable the PLL for @pipe, making sure the pipe is off first.
1757 *
1758 * Note! This is for pre-ILK only.
1759 */
1c4e0274 1760static void i9xx_disable_pll(struct intel_crtc *crtc)
63d7bbe9 1761{
1c4e0274
VS
1762 struct drm_device *dev = crtc->base.dev;
1763 struct drm_i915_private *dev_priv = dev->dev_private;
1764 enum pipe pipe = crtc->pipe;
1765
1766 /* Disable DVO 2x clock on both PLLs if necessary */
1767 if (IS_I830(dev) &&
409ee761 1768 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
1c4e0274
VS
1769 intel_num_dvo_pipes(dev) == 1) {
1770 I915_WRITE(DPLL(PIPE_B),
1771 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1772 I915_WRITE(DPLL(PIPE_A),
1773 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1774 }
1775
b6b5d049
VS
1776 /* Don't disable pipe or pipe PLLs if needed */
1777 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1778 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
63d7bbe9
JB
1779 return;
1780
1781 /* Make sure the pipe isn't still relying on us */
1782 assert_pipe_disabled(dev_priv, pipe);
1783
50b44a44
DV
1784 I915_WRITE(DPLL(pipe), 0);
1785 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1786}
1787
f6071166
JB
1788static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1789{
1790 u32 val = 0;
1791
1792 /* Make sure the pipe isn't still relying on us */
1793 assert_pipe_disabled(dev_priv, pipe);
1794
e5cbfbfb
ID
1795 /*
1796 * Leave integrated clock source and reference clock enabled for pipe B.
1797 * The latter is needed for VGA hotplug / manual detection.
1798 */
f6071166 1799 if (pipe == PIPE_B)
e5cbfbfb 1800 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1801 I915_WRITE(DPLL(pipe), val);
1802 POSTING_READ(DPLL(pipe));
076ed3b2
CML
1803
1804}
1805
1806static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1807{
d752048d 1808 enum dpio_channel port = vlv_pipe_to_channel(pipe);
076ed3b2
CML
1809 u32 val;
1810
a11b0703
VS
1811 /* Make sure the pipe isn't still relying on us */
1812 assert_pipe_disabled(dev_priv, pipe);
076ed3b2 1813
a11b0703 1814 /* Set PLL en = 0 */
d17ec4ce 1815 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
a11b0703
VS
1816 if (pipe != PIPE_A)
1817 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1818 I915_WRITE(DPLL(pipe), val);
1819 POSTING_READ(DPLL(pipe));
d752048d
VS
1820
1821 mutex_lock(&dev_priv->dpio_lock);
1822
1823 /* Disable 10bit clock to display controller */
1824 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1825 val &= ~DPIO_DCLKP_EN;
1826 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1827
61407f6d
VS
1828 /* disable left/right clock distribution */
1829 if (pipe != PIPE_B) {
1830 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1831 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1832 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1833 } else {
1834 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1835 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1836 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1837 }
1838
d752048d 1839 mutex_unlock(&dev_priv->dpio_lock);
f6071166
JB
1840}
1841
e4607fcf 1842void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
9b6de0a1
VS
1843 struct intel_digital_port *dport,
1844 unsigned int expected_mask)
89b667f8
JB
1845{
1846 u32 port_mask;
00fc31b7 1847 int dpll_reg;
89b667f8 1848
e4607fcf
CML
1849 switch (dport->port) {
1850 case PORT_B:
89b667f8 1851 port_mask = DPLL_PORTB_READY_MASK;
00fc31b7 1852 dpll_reg = DPLL(0);
e4607fcf
CML
1853 break;
1854 case PORT_C:
89b667f8 1855 port_mask = DPLL_PORTC_READY_MASK;
00fc31b7 1856 dpll_reg = DPLL(0);
9b6de0a1 1857 expected_mask <<= 4;
00fc31b7
CML
1858 break;
1859 case PORT_D:
1860 port_mask = DPLL_PORTD_READY_MASK;
1861 dpll_reg = DPIO_PHY_STATUS;
e4607fcf
CML
1862 break;
1863 default:
1864 BUG();
1865 }
89b667f8 1866
9b6de0a1
VS
1867 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1868 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1869 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
89b667f8
JB
1870}
1871
b14b1055
DV
1872static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1873{
1874 struct drm_device *dev = crtc->base.dev;
1875 struct drm_i915_private *dev_priv = dev->dev_private;
1876 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1877
be19f0ff
CW
1878 if (WARN_ON(pll == NULL))
1879 return;
1880
3e369b76 1881 WARN_ON(!pll->config.crtc_mask);
b14b1055
DV
1882 if (pll->active == 0) {
1883 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1884 WARN_ON(pll->on);
1885 assert_shared_dpll_disabled(dev_priv, pll);
1886
1887 pll->mode_set(dev_priv, pll);
1888 }
1889}
1890
92f2584a 1891/**
85b3894f 1892 * intel_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1893 * @dev_priv: i915 private structure
1894 * @pipe: pipe PLL to enable
1895 *
1896 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1897 * drives the transcoder clock.
1898 */
85b3894f 1899static void intel_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1900{
3d13ef2e
DL
1901 struct drm_device *dev = crtc->base.dev;
1902 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1903 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1904
87a875bb 1905 if (WARN_ON(pll == NULL))
48da64a8
CW
1906 return;
1907
3e369b76 1908 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1909 return;
ee7b9f93 1910
74dd6928 1911 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
46edb027 1912 pll->name, pll->active, pll->on,
e2b78267 1913 crtc->base.base.id);
92f2584a 1914
cdbd2316
DV
1915 if (pll->active++) {
1916 WARN_ON(!pll->on);
e9d6944e 1917 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1918 return;
1919 }
f4a091c7 1920 WARN_ON(pll->on);
ee7b9f93 1921
bd2bb1b9
PZ
1922 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1923
46edb027 1924 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1925 pll->enable(dev_priv, pll);
ee7b9f93 1926 pll->on = true;
92f2584a
JB
1927}
1928
f6daaec2 1929static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1930{
3d13ef2e
DL
1931 struct drm_device *dev = crtc->base.dev;
1932 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1933 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1934
92f2584a 1935 /* PCH only available on ILK+ */
3d13ef2e 1936 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1937 if (WARN_ON(pll == NULL))
ee7b9f93 1938 return;
92f2584a 1939
3e369b76 1940 if (WARN_ON(pll->config.crtc_mask == 0))
48da64a8 1941 return;
7a419866 1942
46edb027
DV
1943 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1944 pll->name, pll->active, pll->on,
e2b78267 1945 crtc->base.base.id);
7a419866 1946
48da64a8 1947 if (WARN_ON(pll->active == 0)) {
e9d6944e 1948 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1949 return;
1950 }
1951
e9d6944e 1952 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1953 WARN_ON(!pll->on);
cdbd2316 1954 if (--pll->active)
7a419866 1955 return;
ee7b9f93 1956
46edb027 1957 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1958 pll->disable(dev_priv, pll);
ee7b9f93 1959 pll->on = false;
bd2bb1b9
PZ
1960
1961 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
92f2584a
JB
1962}
1963
b8a4f404
PZ
1964static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1965 enum pipe pipe)
040484af 1966{
23670b32 1967 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1968 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1970 uint32_t reg, val, pipeconf_val;
040484af
JB
1971
1972 /* PCH only available on ILK+ */
55522f37 1973 BUG_ON(!HAS_PCH_SPLIT(dev));
040484af
JB
1974
1975 /* Make sure PCH DPLL is enabled */
e72f9fbf 1976 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1977 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1978
1979 /* FDI must be feeding us bits for PCH ports */
1980 assert_fdi_tx_enabled(dev_priv, pipe);
1981 assert_fdi_rx_enabled(dev_priv, pipe);
1982
23670b32
DV
1983 if (HAS_PCH_CPT(dev)) {
1984 /* Workaround: Set the timing override bit before enabling the
1985 * pch transcoder. */
1986 reg = TRANS_CHICKEN2(pipe);
1987 val = I915_READ(reg);
1988 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1989 I915_WRITE(reg, val);
59c859d6 1990 }
23670b32 1991
ab9412ba 1992 reg = PCH_TRANSCONF(pipe);
040484af 1993 val = I915_READ(reg);
5f7f726d 1994 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1995
1996 if (HAS_PCH_IBX(dev_priv->dev)) {
1997 /*
1998 * make the BPC in transcoder be consistent with
1999 * that in pipeconf reg.
2000 */
dfd07d72
DV
2001 val &= ~PIPECONF_BPC_MASK;
2002 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 2003 }
5f7f726d
PZ
2004
2005 val &= ~TRANS_INTERLACE_MASK;
2006 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6 2007 if (HAS_PCH_IBX(dev_priv->dev) &&
409ee761 2008 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
7c26e5c6
PZ
2009 val |= TRANS_LEGACY_INTERLACED_ILK;
2010 else
2011 val |= TRANS_INTERLACED;
5f7f726d
PZ
2012 else
2013 val |= TRANS_PROGRESSIVE;
2014
040484af
JB
2015 I915_WRITE(reg, val | TRANS_ENABLE);
2016 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 2017 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
2018}
2019
8fb033d7 2020static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 2021 enum transcoder cpu_transcoder)
040484af 2022{
8fb033d7 2023 u32 val, pipeconf_val;
8fb033d7
PZ
2024
2025 /* PCH only available on ILK+ */
55522f37 2026 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
8fb033d7 2027
8fb033d7 2028 /* FDI must be feeding us bits for PCH ports */
1a240d4d 2029 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 2030 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 2031
223a6fdf
PZ
2032 /* Workaround: set timing override bit. */
2033 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2034 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
2035 I915_WRITE(_TRANSA_CHICKEN2, val);
2036
25f3ef11 2037 val = TRANS_ENABLE;
937bb610 2038 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 2039
9a76b1c6
PZ
2040 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2041 PIPECONF_INTERLACED_ILK)
a35f2679 2042 val |= TRANS_INTERLACED;
8fb033d7
PZ
2043 else
2044 val |= TRANS_PROGRESSIVE;
2045
ab9412ba
DV
2046 I915_WRITE(LPT_TRANSCONF, val);
2047 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 2048 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
2049}
2050
b8a4f404
PZ
2051static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2052 enum pipe pipe)
040484af 2053{
23670b32
DV
2054 struct drm_device *dev = dev_priv->dev;
2055 uint32_t reg, val;
040484af
JB
2056
2057 /* FDI relies on the transcoder */
2058 assert_fdi_tx_disabled(dev_priv, pipe);
2059 assert_fdi_rx_disabled(dev_priv, pipe);
2060
291906f1
JB
2061 /* Ports must be off as well */
2062 assert_pch_ports_disabled(dev_priv, pipe);
2063
ab9412ba 2064 reg = PCH_TRANSCONF(pipe);
040484af
JB
2065 val = I915_READ(reg);
2066 val &= ~TRANS_ENABLE;
2067 I915_WRITE(reg, val);
2068 /* wait for PCH transcoder off, transcoder state */
2069 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 2070 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
2071
2072 if (!HAS_PCH_IBX(dev)) {
2073 /* Workaround: Clear the timing override chicken bit again. */
2074 reg = TRANS_CHICKEN2(pipe);
2075 val = I915_READ(reg);
2076 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2077 I915_WRITE(reg, val);
2078 }
040484af
JB
2079}
2080
ab4d966c 2081static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 2082{
8fb033d7
PZ
2083 u32 val;
2084
ab9412ba 2085 val = I915_READ(LPT_TRANSCONF);
8fb033d7 2086 val &= ~TRANS_ENABLE;
ab9412ba 2087 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 2088 /* wait for PCH transcoder off, transcoder state */
ab9412ba 2089 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 2090 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
2091
2092 /* Workaround: clear timing override bit. */
2093 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 2094 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 2095 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
2096}
2097
b24e7179 2098/**
309cfea8 2099 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 2100 * @crtc: crtc responsible for the pipe
b24e7179 2101 *
0372264a 2102 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 2103 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 2104 */
e1fdc473 2105static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 2106{
0372264a
PZ
2107 struct drm_device *dev = crtc->base.dev;
2108 struct drm_i915_private *dev_priv = dev->dev_private;
2109 enum pipe pipe = crtc->pipe;
702e7a56
PZ
2110 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2111 pipe);
1a240d4d 2112 enum pipe pch_transcoder;
b24e7179
JB
2113 int reg;
2114 u32 val;
2115
58c6eaa2 2116 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2117 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
2118 assert_sprites_disabled(dev_priv, pipe);
2119
681e5811 2120 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
2121 pch_transcoder = TRANSCODER_A;
2122 else
2123 pch_transcoder = pipe;
2124
b24e7179
JB
2125 /*
2126 * A pipe without a PLL won't actually be able to drive bits from
2127 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2128 * need the check.
2129 */
50360403 2130 if (HAS_GMCH_DISPLAY(dev_priv->dev))
409ee761 2131 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
23538ef1
JN
2132 assert_dsi_pll_enabled(dev_priv);
2133 else
2134 assert_pll_enabled(dev_priv, pipe);
040484af 2135 else {
6e3c9717 2136 if (crtc->config->has_pch_encoder) {
040484af 2137 /* if driving the PCH, we need FDI enabled */
cc391bbb 2138 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
2139 assert_fdi_tx_pll_enabled(dev_priv,
2140 (enum pipe) cpu_transcoder);
040484af
JB
2141 }
2142 /* FIXME: assert CPU port conditions for SNB+ */
2143 }
b24e7179 2144
702e7a56 2145 reg = PIPECONF(cpu_transcoder);
b24e7179 2146 val = I915_READ(reg);
7ad25d48 2147 if (val & PIPECONF_ENABLE) {
b6b5d049
VS
2148 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2149 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
00d70b15 2150 return;
7ad25d48 2151 }
00d70b15
CW
2152
2153 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 2154 POSTING_READ(reg);
b24e7179
JB
2155}
2156
2157/**
309cfea8 2158 * intel_disable_pipe - disable a pipe, asserting requirements
575f7ab7 2159 * @crtc: crtc whose pipes is to be disabled
b24e7179 2160 *
575f7ab7
VS
2161 * Disable the pipe of @crtc, making sure that various hardware
2162 * specific requirements are met, if applicable, e.g. plane
2163 * disabled, panel fitter off, etc.
b24e7179
JB
2164 *
2165 * Will wait until the pipe has shut down before returning.
2166 */
575f7ab7 2167static void intel_disable_pipe(struct intel_crtc *crtc)
b24e7179 2168{
575f7ab7 2169 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
6e3c9717 2170 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
575f7ab7 2171 enum pipe pipe = crtc->pipe;
b24e7179
JB
2172 int reg;
2173 u32 val;
2174
2175 /*
2176 * Make sure planes won't keep trying to pump pixels to us,
2177 * or we might hang the display.
2178 */
2179 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 2180 assert_cursor_disabled(dev_priv, pipe);
19332d7a 2181 assert_sprites_disabled(dev_priv, pipe);
b24e7179 2182
702e7a56 2183 reg = PIPECONF(cpu_transcoder);
b24e7179 2184 val = I915_READ(reg);
00d70b15
CW
2185 if ((val & PIPECONF_ENABLE) == 0)
2186 return;
2187
67adc644
VS
2188 /*
2189 * Double wide has implications for planes
2190 * so best keep it disabled when not needed.
2191 */
6e3c9717 2192 if (crtc->config->double_wide)
67adc644
VS
2193 val &= ~PIPECONF_DOUBLE_WIDE;
2194
2195 /* Don't disable pipe or pipe PLLs if needed */
b6b5d049
VS
2196 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2197 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
67adc644
VS
2198 val &= ~PIPECONF_ENABLE;
2199
2200 I915_WRITE(reg, val);
2201 if ((val & PIPECONF_ENABLE) == 0)
2202 intel_wait_for_pipe_off(crtc);
b24e7179
JB
2203}
2204
d74362c9
KP
2205/*
2206 * Plane regs are double buffered, going from enabled->disabled needs a
2207 * trigger in order to latch. The display address reg provides this.
2208 */
1dba99f4
VS
2209void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2210 enum plane plane)
d74362c9 2211{
3d13ef2e
DL
2212 struct drm_device *dev = dev_priv->dev;
2213 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
2214
2215 I915_WRITE(reg, I915_READ(reg));
2216 POSTING_READ(reg);
d74362c9
KP
2217}
2218
b24e7179 2219/**
262ca2b0 2220 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
fdd508a6
VS
2221 * @plane: plane to be enabled
2222 * @crtc: crtc for the plane
b24e7179 2223 *
fdd508a6 2224 * Enable @plane on @crtc, making sure that the pipe is running first.
b24e7179 2225 */
fdd508a6
VS
2226static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2227 struct drm_crtc *crtc)
b24e7179 2228{
fdd508a6
VS
2229 struct drm_device *dev = plane->dev;
2230 struct drm_i915_private *dev_priv = dev->dev_private;
2231 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b24e7179
JB
2232
2233 /* If the pipe isn't enabled, we can't pump pixels and may hang */
fdd508a6 2234 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
b70709a6 2235 to_intel_plane_state(plane->state)->visible = true;
939c2fe8 2236
fdd508a6
VS
2237 dev_priv->display.update_primary_plane(crtc, plane->fb,
2238 crtc->x, crtc->y);
b24e7179
JB
2239}
2240
693db184
CW
2241static bool need_vtd_wa(struct drm_device *dev)
2242{
2243#ifdef CONFIG_INTEL_IOMMU
2244 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2245 return true;
2246#endif
2247 return false;
2248}
2249
50470bb0 2250unsigned int
6761dd31
TU
2251intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2252 uint64_t fb_format_modifier)
a57ce0b2 2253{
6761dd31
TU
2254 unsigned int tile_height;
2255 uint32_t pixel_bytes;
a57ce0b2 2256
b5d0e9bf
DL
2257 switch (fb_format_modifier) {
2258 case DRM_FORMAT_MOD_NONE:
2259 tile_height = 1;
2260 break;
2261 case I915_FORMAT_MOD_X_TILED:
2262 tile_height = IS_GEN2(dev) ? 16 : 8;
2263 break;
2264 case I915_FORMAT_MOD_Y_TILED:
2265 tile_height = 32;
2266 break;
2267 case I915_FORMAT_MOD_Yf_TILED:
6761dd31
TU
2268 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2269 switch (pixel_bytes) {
b5d0e9bf 2270 default:
6761dd31 2271 case 1:
b5d0e9bf
DL
2272 tile_height = 64;
2273 break;
6761dd31
TU
2274 case 2:
2275 case 4:
b5d0e9bf
DL
2276 tile_height = 32;
2277 break;
6761dd31 2278 case 8:
b5d0e9bf
DL
2279 tile_height = 16;
2280 break;
6761dd31 2281 case 16:
b5d0e9bf
DL
2282 WARN_ONCE(1,
2283 "128-bit pixels are not supported for display!");
2284 tile_height = 16;
2285 break;
2286 }
2287 break;
2288 default:
2289 MISSING_CASE(fb_format_modifier);
2290 tile_height = 1;
2291 break;
2292 }
091df6cb 2293
6761dd31
TU
2294 return tile_height;
2295}
2296
2297unsigned int
2298intel_fb_align_height(struct drm_device *dev, unsigned int height,
2299 uint32_t pixel_format, uint64_t fb_format_modifier)
2300{
2301 return ALIGN(height, intel_tile_height(dev, pixel_format,
2302 fb_format_modifier));
a57ce0b2
JB
2303}
2304
f64b98cd
TU
2305static int
2306intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2307 const struct drm_plane_state *plane_state)
2308{
50470bb0 2309 struct intel_rotation_info *info = &view->rotation_info;
50470bb0 2310
f64b98cd
TU
2311 *view = i915_ggtt_view_normal;
2312
50470bb0
TU
2313 if (!plane_state)
2314 return 0;
2315
121920fa 2316 if (!intel_rotation_90_or_270(plane_state->rotation))
50470bb0
TU
2317 return 0;
2318
9abc4648 2319 *view = i915_ggtt_view_rotated;
50470bb0
TU
2320
2321 info->height = fb->height;
2322 info->pixel_format = fb->pixel_format;
2323 info->pitch = fb->pitches[0];
2324 info->fb_modifier = fb->modifier[0];
2325
f64b98cd
TU
2326 return 0;
2327}
2328
127bd2ac 2329int
850c4cdc
TU
2330intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2331 struct drm_framebuffer *fb,
82bc3b2d 2332 const struct drm_plane_state *plane_state,
a4872ba6 2333 struct intel_engine_cs *pipelined)
6b95a207 2334{
850c4cdc 2335 struct drm_device *dev = fb->dev;
ce453d81 2336 struct drm_i915_private *dev_priv = dev->dev_private;
850c4cdc 2337 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd 2338 struct i915_ggtt_view view;
6b95a207
KH
2339 u32 alignment;
2340 int ret;
2341
ebcdd39e
MR
2342 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2343
7b911adc
TU
2344 switch (fb->modifier[0]) {
2345 case DRM_FORMAT_MOD_NONE:
1fada4cc
DL
2346 if (INTEL_INFO(dev)->gen >= 9)
2347 alignment = 256 * 1024;
2348 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
534843da 2349 alignment = 128 * 1024;
a6c45cf0 2350 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
2351 alignment = 4 * 1024;
2352 else
2353 alignment = 64 * 1024;
6b95a207 2354 break;
7b911adc 2355 case I915_FORMAT_MOD_X_TILED:
1fada4cc
DL
2356 if (INTEL_INFO(dev)->gen >= 9)
2357 alignment = 256 * 1024;
2358 else {
2359 /* pin() will align the object as required by fence */
2360 alignment = 0;
2361 }
6b95a207 2362 break;
7b911adc 2363 case I915_FORMAT_MOD_Y_TILED:
1327b9a1
DL
2364 case I915_FORMAT_MOD_Yf_TILED:
2365 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2366 "Y tiling bo slipped through, driver bug!\n"))
2367 return -EINVAL;
2368 alignment = 1 * 1024 * 1024;
2369 break;
6b95a207 2370 default:
7b911adc
TU
2371 MISSING_CASE(fb->modifier[0]);
2372 return -EINVAL;
6b95a207
KH
2373 }
2374
f64b98cd
TU
2375 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2376 if (ret)
2377 return ret;
2378
693db184
CW
2379 /* Note that the w/a also requires 64 PTE of padding following the
2380 * bo. We currently fill all unused PTE with the shadow page and so
2381 * we should always have valid PTE following the scanout preventing
2382 * the VT-d warning.
2383 */
2384 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2385 alignment = 256 * 1024;
2386
d6dd6843
PZ
2387 /*
2388 * Global gtt pte registers are special registers which actually forward
2389 * writes to a chunk of system memory. Which means that there is no risk
2390 * that the register values disappear as soon as we call
2391 * intel_runtime_pm_put(), so it is correct to wrap only the
2392 * pin/unpin/fence and not more.
2393 */
2394 intel_runtime_pm_get(dev_priv);
2395
ce453d81 2396 dev_priv->mm.interruptible = false;
e6617330 2397 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
f64b98cd 2398 &view);
48b956c5 2399 if (ret)
ce453d81 2400 goto err_interruptible;
6b95a207
KH
2401
2402 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2403 * fence, whereas 965+ only requires a fence if using
2404 * framebuffer compression. For simplicity, we always install
2405 * a fence as the cost is not that onerous.
2406 */
06d98131 2407 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2408 if (ret)
2409 goto err_unpin;
1690e1eb 2410
9a5a53b3 2411 i915_gem_object_pin_fence(obj);
6b95a207 2412
ce453d81 2413 dev_priv->mm.interruptible = true;
d6dd6843 2414 intel_runtime_pm_put(dev_priv);
6b95a207 2415 return 0;
48b956c5
CW
2416
2417err_unpin:
f64b98cd 2418 i915_gem_object_unpin_from_display_plane(obj, &view);
ce453d81
CW
2419err_interruptible:
2420 dev_priv->mm.interruptible = true;
d6dd6843 2421 intel_runtime_pm_put(dev_priv);
48b956c5 2422 return ret;
6b95a207
KH
2423}
2424
82bc3b2d
TU
2425static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2426 const struct drm_plane_state *plane_state)
1690e1eb 2427{
82bc3b2d 2428 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
f64b98cd
TU
2429 struct i915_ggtt_view view;
2430 int ret;
82bc3b2d 2431
ebcdd39e
MR
2432 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2433
f64b98cd
TU
2434 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2435 WARN_ONCE(ret, "Couldn't get view from plane state!");
2436
1690e1eb 2437 i915_gem_object_unpin_fence(obj);
f64b98cd 2438 i915_gem_object_unpin_from_display_plane(obj, &view);
1690e1eb
CW
2439}
2440
c2c75131
DV
2441/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2442 * is assumed to be a power-of-two. */
bc752862
CW
2443unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2444 unsigned int tiling_mode,
2445 unsigned int cpp,
2446 unsigned int pitch)
c2c75131 2447{
bc752862
CW
2448 if (tiling_mode != I915_TILING_NONE) {
2449 unsigned int tile_rows, tiles;
c2c75131 2450
bc752862
CW
2451 tile_rows = *y / 8;
2452 *y %= 8;
c2c75131 2453
bc752862
CW
2454 tiles = *x / (512/cpp);
2455 *x %= 512/cpp;
2456
2457 return tile_rows * pitch * 8 + tiles * 4096;
2458 } else {
2459 unsigned int offset;
2460
2461 offset = *y * pitch + *x * cpp;
2462 *y = 0;
2463 *x = (offset & 4095) / cpp;
2464 return offset & -4096;
2465 }
c2c75131
DV
2466}
2467
b35d63fa 2468static int i9xx_format_to_fourcc(int format)
46f297fb
JB
2469{
2470 switch (format) {
2471 case DISPPLANE_8BPP:
2472 return DRM_FORMAT_C8;
2473 case DISPPLANE_BGRX555:
2474 return DRM_FORMAT_XRGB1555;
2475 case DISPPLANE_BGRX565:
2476 return DRM_FORMAT_RGB565;
2477 default:
2478 case DISPPLANE_BGRX888:
2479 return DRM_FORMAT_XRGB8888;
2480 case DISPPLANE_RGBX888:
2481 return DRM_FORMAT_XBGR8888;
2482 case DISPPLANE_BGRX101010:
2483 return DRM_FORMAT_XRGB2101010;
2484 case DISPPLANE_RGBX101010:
2485 return DRM_FORMAT_XBGR2101010;
2486 }
2487}
2488
bc8d7dff
DL
2489static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2490{
2491 switch (format) {
2492 case PLANE_CTL_FORMAT_RGB_565:
2493 return DRM_FORMAT_RGB565;
2494 default:
2495 case PLANE_CTL_FORMAT_XRGB_8888:
2496 if (rgb_order) {
2497 if (alpha)
2498 return DRM_FORMAT_ABGR8888;
2499 else
2500 return DRM_FORMAT_XBGR8888;
2501 } else {
2502 if (alpha)
2503 return DRM_FORMAT_ARGB8888;
2504 else
2505 return DRM_FORMAT_XRGB8888;
2506 }
2507 case PLANE_CTL_FORMAT_XRGB_2101010:
2508 if (rgb_order)
2509 return DRM_FORMAT_XBGR2101010;
2510 else
2511 return DRM_FORMAT_XRGB2101010;
2512 }
2513}
2514
5724dbd1 2515static bool
f6936e29
DV
2516intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2517 struct intel_initial_plane_config *plane_config)
46f297fb
JB
2518{
2519 struct drm_device *dev = crtc->base.dev;
2520 struct drm_i915_gem_object *obj = NULL;
2521 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2d14030b 2522 struct drm_framebuffer *fb = &plane_config->fb->base;
f37b5c2b
DV
2523 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2524 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2525 PAGE_SIZE);
2526
2527 size_aligned -= base_aligned;
46f297fb 2528
ff2652ea
CW
2529 if (plane_config->size == 0)
2530 return false;
2531
f37b5c2b
DV
2532 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2533 base_aligned,
2534 base_aligned,
2535 size_aligned);
46f297fb 2536 if (!obj)
484b41dd 2537 return false;
46f297fb 2538
49af449b
DL
2539 obj->tiling_mode = plane_config->tiling;
2540 if (obj->tiling_mode == I915_TILING_X)
6bf129df 2541 obj->stride = fb->pitches[0];
46f297fb 2542
6bf129df
DL
2543 mode_cmd.pixel_format = fb->pixel_format;
2544 mode_cmd.width = fb->width;
2545 mode_cmd.height = fb->height;
2546 mode_cmd.pitches[0] = fb->pitches[0];
18c5247e
DV
2547 mode_cmd.modifier[0] = fb->modifier[0];
2548 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
46f297fb
JB
2549
2550 mutex_lock(&dev->struct_mutex);
6bf129df 2551 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
484b41dd 2552 &mode_cmd, obj)) {
46f297fb
JB
2553 DRM_DEBUG_KMS("intel fb init failed\n");
2554 goto out_unref_obj;
2555 }
46f297fb 2556 mutex_unlock(&dev->struct_mutex);
484b41dd 2557
f6936e29 2558 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
484b41dd 2559 return true;
46f297fb
JB
2560
2561out_unref_obj:
2562 drm_gem_object_unreference(&obj->base);
2563 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2564 return false;
2565}
2566
afd65eb4
MR
2567/* Update plane->state->fb to match plane->fb after driver-internal updates */
2568static void
2569update_state_fb(struct drm_plane *plane)
2570{
2571 if (plane->fb == plane->state->fb)
2572 return;
2573
2574 if (plane->state->fb)
2575 drm_framebuffer_unreference(plane->state->fb);
2576 plane->state->fb = plane->fb;
2577 if (plane->state->fb)
2578 drm_framebuffer_reference(plane->state->fb);
2579}
2580
5724dbd1 2581static void
f6936e29
DV
2582intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2583 struct intel_initial_plane_config *plane_config)
484b41dd
JB
2584{
2585 struct drm_device *dev = intel_crtc->base.dev;
d9ceb816 2586 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd
JB
2587 struct drm_crtc *c;
2588 struct intel_crtc *i;
2ff8fde1 2589 struct drm_i915_gem_object *obj;
88595ac9
DV
2590 struct drm_plane *primary = intel_crtc->base.primary;
2591 struct drm_framebuffer *fb;
484b41dd 2592
2d14030b 2593 if (!plane_config->fb)
484b41dd
JB
2594 return;
2595
f6936e29 2596 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
88595ac9
DV
2597 fb = &plane_config->fb->base;
2598 goto valid_fb;
f55548b5 2599 }
484b41dd 2600
2d14030b 2601 kfree(plane_config->fb);
484b41dd
JB
2602
2603 /*
2604 * Failed to alloc the obj, check to see if we should share
2605 * an fb with another CRTC instead
2606 */
70e1e0ec 2607 for_each_crtc(dev, c) {
484b41dd
JB
2608 i = to_intel_crtc(c);
2609
2610 if (c == &intel_crtc->base)
2611 continue;
2612
2ff8fde1
MR
2613 if (!i->active)
2614 continue;
2615
88595ac9
DV
2616 fb = c->primary->fb;
2617 if (!fb)
484b41dd
JB
2618 continue;
2619
88595ac9 2620 obj = intel_fb_obj(fb);
2ff8fde1 2621 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
88595ac9
DV
2622 drm_framebuffer_reference(fb);
2623 goto valid_fb;
484b41dd
JB
2624 }
2625 }
88595ac9
DV
2626
2627 return;
2628
2629valid_fb:
2630 obj = intel_fb_obj(fb);
2631 if (obj->tiling_mode != I915_TILING_NONE)
2632 dev_priv->preserve_bios_swizzle = true;
2633
2634 primary->fb = fb;
2635 primary->state->crtc = &intel_crtc->base;
2636 primary->crtc = &intel_crtc->base;
2637 update_state_fb(primary);
2638 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
46f297fb
JB
2639}
2640
29b9bde6
DV
2641static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2642 struct drm_framebuffer *fb,
2643 int x, int y)
81255565
JB
2644{
2645 struct drm_device *dev = crtc->dev;
2646 struct drm_i915_private *dev_priv = dev->dev_private;
2647 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2648 struct drm_plane *primary = crtc->primary;
2649 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2650 struct drm_i915_gem_object *obj;
81255565 2651 int plane = intel_crtc->plane;
e506a0c6 2652 unsigned long linear_offset;
81255565 2653 u32 dspcntr;
f45651ba 2654 u32 reg = DSPCNTR(plane);
48404c1e 2655 int pixel_size;
f45651ba 2656
b70709a6 2657 if (!visible || !fb) {
fdd508a6
VS
2658 I915_WRITE(reg, 0);
2659 if (INTEL_INFO(dev)->gen >= 4)
2660 I915_WRITE(DSPSURF(plane), 0);
2661 else
2662 I915_WRITE(DSPADDR(plane), 0);
2663 POSTING_READ(reg);
2664 return;
2665 }
2666
c9ba6fad
VS
2667 obj = intel_fb_obj(fb);
2668 if (WARN_ON(obj == NULL))
2669 return;
2670
2671 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2672
f45651ba
VS
2673 dspcntr = DISPPLANE_GAMMA_ENABLE;
2674
fdd508a6 2675 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2676
2677 if (INTEL_INFO(dev)->gen < 4) {
2678 if (intel_crtc->pipe == PIPE_B)
2679 dspcntr |= DISPPLANE_SEL_PIPE_B;
2680
2681 /* pipesrc and dspsize control the size that is scaled from,
2682 * which should always be the user's requested size.
2683 */
2684 I915_WRITE(DSPSIZE(plane),
6e3c9717
ACO
2685 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2686 (intel_crtc->config->pipe_src_w - 1));
f45651ba 2687 I915_WRITE(DSPPOS(plane), 0);
c14b0485
VS
2688 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2689 I915_WRITE(PRIMSIZE(plane),
6e3c9717
ACO
2690 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2691 (intel_crtc->config->pipe_src_w - 1));
c14b0485
VS
2692 I915_WRITE(PRIMPOS(plane), 0);
2693 I915_WRITE(PRIMCNSTALPHA(plane), 0);
f45651ba 2694 }
81255565 2695
57779d06
VS
2696 switch (fb->pixel_format) {
2697 case DRM_FORMAT_C8:
81255565
JB
2698 dspcntr |= DISPPLANE_8BPP;
2699 break;
57779d06 2700 case DRM_FORMAT_XRGB1555:
57779d06 2701 dspcntr |= DISPPLANE_BGRX555;
81255565 2702 break;
57779d06
VS
2703 case DRM_FORMAT_RGB565:
2704 dspcntr |= DISPPLANE_BGRX565;
2705 break;
2706 case DRM_FORMAT_XRGB8888:
2707 case DRM_FORMAT_ARGB8888:
2708 dspcntr |= DISPPLANE_BGRX888;
2709 break;
2710 case DRM_FORMAT_XBGR8888:
2711 case DRM_FORMAT_ABGR8888:
2712 dspcntr |= DISPPLANE_RGBX888;
2713 break;
2714 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2715 dspcntr |= DISPPLANE_BGRX101010;
2716 break;
2717 case DRM_FORMAT_XBGR2101010:
57779d06 2718 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2719 break;
2720 default:
baba133a 2721 BUG();
81255565 2722 }
57779d06 2723
f45651ba
VS
2724 if (INTEL_INFO(dev)->gen >= 4 &&
2725 obj->tiling_mode != I915_TILING_NONE)
2726 dspcntr |= DISPPLANE_TILED;
81255565 2727
de1aa629
VS
2728 if (IS_G4X(dev))
2729 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2730
b9897127 2731 linear_offset = y * fb->pitches[0] + x * pixel_size;
81255565 2732
c2c75131
DV
2733 if (INTEL_INFO(dev)->gen >= 4) {
2734 intel_crtc->dspaddr_offset =
bc752862 2735 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2736 pixel_size,
bc752862 2737 fb->pitches[0]);
c2c75131
DV
2738 linear_offset -= intel_crtc->dspaddr_offset;
2739 } else {
e506a0c6 2740 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2741 }
e506a0c6 2742
8e7d688b 2743 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2744 dspcntr |= DISPPLANE_ROTATE_180;
2745
6e3c9717
ACO
2746 x += (intel_crtc->config->pipe_src_w - 1);
2747 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2748
2749 /* Finding the last pixel of the last line of the display
2750 data and adding to linear_offset*/
2751 linear_offset +=
6e3c9717
ACO
2752 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2753 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2754 }
2755
2756 I915_WRITE(reg, dspcntr);
2757
01f2c773 2758 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2759 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2760 I915_WRITE(DSPSURF(plane),
2761 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2762 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2763 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2764 } else
f343c5f6 2765 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2766 POSTING_READ(reg);
17638cd6
JB
2767}
2768
29b9bde6
DV
2769static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2770 struct drm_framebuffer *fb,
2771 int x, int y)
17638cd6
JB
2772{
2773 struct drm_device *dev = crtc->dev;
2774 struct drm_i915_private *dev_priv = dev->dev_private;
2775 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
2776 struct drm_plane *primary = crtc->primary;
2777 bool visible = to_intel_plane_state(primary->state)->visible;
c9ba6fad 2778 struct drm_i915_gem_object *obj;
17638cd6 2779 int plane = intel_crtc->plane;
e506a0c6 2780 unsigned long linear_offset;
17638cd6 2781 u32 dspcntr;
f45651ba 2782 u32 reg = DSPCNTR(plane);
48404c1e 2783 int pixel_size;
f45651ba 2784
b70709a6 2785 if (!visible || !fb) {
fdd508a6
VS
2786 I915_WRITE(reg, 0);
2787 I915_WRITE(DSPSURF(plane), 0);
2788 POSTING_READ(reg);
2789 return;
2790 }
2791
c9ba6fad
VS
2792 obj = intel_fb_obj(fb);
2793 if (WARN_ON(obj == NULL))
2794 return;
2795
2796 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2797
f45651ba
VS
2798 dspcntr = DISPPLANE_GAMMA_ENABLE;
2799
fdd508a6 2800 dspcntr |= DISPLAY_PLANE_ENABLE;
f45651ba
VS
2801
2802 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2803 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
17638cd6 2804
57779d06
VS
2805 switch (fb->pixel_format) {
2806 case DRM_FORMAT_C8:
17638cd6
JB
2807 dspcntr |= DISPPLANE_8BPP;
2808 break;
57779d06
VS
2809 case DRM_FORMAT_RGB565:
2810 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2811 break;
57779d06
VS
2812 case DRM_FORMAT_XRGB8888:
2813 case DRM_FORMAT_ARGB8888:
2814 dspcntr |= DISPPLANE_BGRX888;
2815 break;
2816 case DRM_FORMAT_XBGR8888:
2817 case DRM_FORMAT_ABGR8888:
2818 dspcntr |= DISPPLANE_RGBX888;
2819 break;
2820 case DRM_FORMAT_XRGB2101010:
57779d06
VS
2821 dspcntr |= DISPPLANE_BGRX101010;
2822 break;
2823 case DRM_FORMAT_XBGR2101010:
57779d06 2824 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2825 break;
2826 default:
baba133a 2827 BUG();
17638cd6
JB
2828 }
2829
2830 if (obj->tiling_mode != I915_TILING_NONE)
2831 dspcntr |= DISPPLANE_TILED;
17638cd6 2832
f45651ba 2833 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
1f5d76db 2834 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6 2835
b9897127 2836 linear_offset = y * fb->pitches[0] + x * pixel_size;
c2c75131 2837 intel_crtc->dspaddr_offset =
bc752862 2838 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
b9897127 2839 pixel_size,
bc752862 2840 fb->pitches[0]);
c2c75131 2841 linear_offset -= intel_crtc->dspaddr_offset;
8e7d688b 2842 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
48404c1e
SJ
2843 dspcntr |= DISPPLANE_ROTATE_180;
2844
2845 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
6e3c9717
ACO
2846 x += (intel_crtc->config->pipe_src_w - 1);
2847 y += (intel_crtc->config->pipe_src_h - 1);
48404c1e
SJ
2848
2849 /* Finding the last pixel of the last line of the display
2850 data and adding to linear_offset*/
2851 linear_offset +=
6e3c9717
ACO
2852 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2853 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
48404c1e
SJ
2854 }
2855 }
2856
2857 I915_WRITE(reg, dspcntr);
17638cd6 2858
01f2c773 2859 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2860 I915_WRITE(DSPSURF(plane),
2861 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2862 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2863 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2864 } else {
2865 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2866 I915_WRITE(DSPLINOFF(plane), linear_offset);
2867 }
17638cd6 2868 POSTING_READ(reg);
17638cd6
JB
2869}
2870
b321803d
DL
2871u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2872 uint32_t pixel_format)
2873{
2874 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2875
2876 /*
2877 * The stride is either expressed as a multiple of 64 bytes
2878 * chunks for linear buffers or in number of tiles for tiled
2879 * buffers.
2880 */
2881 switch (fb_modifier) {
2882 case DRM_FORMAT_MOD_NONE:
2883 return 64;
2884 case I915_FORMAT_MOD_X_TILED:
2885 if (INTEL_INFO(dev)->gen == 2)
2886 return 128;
2887 return 512;
2888 case I915_FORMAT_MOD_Y_TILED:
2889 /* No need to check for old gens and Y tiling since this is
2890 * about the display engine and those will be blocked before
2891 * we get here.
2892 */
2893 return 128;
2894 case I915_FORMAT_MOD_Yf_TILED:
2895 if (bits_per_pixel == 8)
2896 return 64;
2897 else
2898 return 128;
2899 default:
2900 MISSING_CASE(fb_modifier);
2901 return 64;
2902 }
2903}
2904
121920fa
TU
2905unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2906 struct drm_i915_gem_object *obj)
2907{
9abc4648 2908 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
121920fa
TU
2909
2910 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
9abc4648 2911 view = &i915_ggtt_view_rotated;
121920fa
TU
2912
2913 return i915_gem_obj_ggtt_offset_view(obj, view);
2914}
2915
a1b2278e
CK
2916/*
2917 * This function detaches (aka. unbinds) unused scalers in hardware
2918 */
2919void skl_detach_scalers(struct intel_crtc *intel_crtc)
2920{
2921 struct drm_device *dev;
2922 struct drm_i915_private *dev_priv;
2923 struct intel_crtc_scaler_state *scaler_state;
2924 int i;
2925
2926 if (!intel_crtc || !intel_crtc->config)
2927 return;
2928
2929 dev = intel_crtc->base.dev;
2930 dev_priv = dev->dev_private;
2931 scaler_state = &intel_crtc->config->scaler_state;
2932
2933 /* loop through and disable scalers that aren't in use */
2934 for (i = 0; i < intel_crtc->num_scalers; i++) {
2935 if (!scaler_state->scalers[i].in_use) {
2936 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2937 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2938 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2939 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2940 intel_crtc->base.base.id, intel_crtc->pipe, i);
2941 }
2942 }
2943}
2944
6156a456 2945u32 skl_plane_ctl_format(uint32_t pixel_format)
70d21f0e 2946{
6156a456 2947 switch (pixel_format) {
d161cf7a 2948 case DRM_FORMAT_C8:
c34ce3d1 2949 return PLANE_CTL_FORMAT_INDEXED;
70d21f0e 2950 case DRM_FORMAT_RGB565:
c34ce3d1 2951 return PLANE_CTL_FORMAT_RGB_565;
70d21f0e 2952 case DRM_FORMAT_XBGR8888:
c34ce3d1 2953 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
6156a456 2954 case DRM_FORMAT_XRGB8888:
c34ce3d1 2955 return PLANE_CTL_FORMAT_XRGB_8888;
6156a456
CK
2956 /*
2957 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2958 * to be already pre-multiplied. We need to add a knob (or a different
2959 * DRM_FORMAT) for user-space to configure that.
2960 */
f75fb42a 2961 case DRM_FORMAT_ABGR8888:
c34ce3d1 2962 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
6156a456 2963 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
6156a456 2964 case DRM_FORMAT_ARGB8888:
c34ce3d1 2965 return PLANE_CTL_FORMAT_XRGB_8888 |
6156a456 2966 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
70d21f0e 2967 case DRM_FORMAT_XRGB2101010:
c34ce3d1 2968 return PLANE_CTL_FORMAT_XRGB_2101010;
70d21f0e 2969 case DRM_FORMAT_XBGR2101010:
c34ce3d1 2970 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
6156a456 2971 case DRM_FORMAT_YUYV:
c34ce3d1 2972 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
6156a456 2973 case DRM_FORMAT_YVYU:
c34ce3d1 2974 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
6156a456 2975 case DRM_FORMAT_UYVY:
c34ce3d1 2976 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
6156a456 2977 case DRM_FORMAT_VYUY:
c34ce3d1 2978 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
70d21f0e 2979 default:
4249eeef 2980 MISSING_CASE(pixel_format);
70d21f0e 2981 }
8cfcba41 2982
c34ce3d1 2983 return 0;
6156a456 2984}
70d21f0e 2985
6156a456
CK
2986u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2987{
6156a456 2988 switch (fb_modifier) {
30af77c4 2989 case DRM_FORMAT_MOD_NONE:
70d21f0e 2990 break;
30af77c4 2991 case I915_FORMAT_MOD_X_TILED:
c34ce3d1 2992 return PLANE_CTL_TILED_X;
b321803d 2993 case I915_FORMAT_MOD_Y_TILED:
c34ce3d1 2994 return PLANE_CTL_TILED_Y;
b321803d 2995 case I915_FORMAT_MOD_Yf_TILED:
c34ce3d1 2996 return PLANE_CTL_TILED_YF;
70d21f0e 2997 default:
6156a456 2998 MISSING_CASE(fb_modifier);
70d21f0e 2999 }
8cfcba41 3000
c34ce3d1 3001 return 0;
6156a456 3002}
70d21f0e 3003
6156a456
CK
3004u32 skl_plane_ctl_rotation(unsigned int rotation)
3005{
3b7a5119 3006 switch (rotation) {
6156a456
CK
3007 case BIT(DRM_ROTATE_0):
3008 break;
3b7a5119 3009 case BIT(DRM_ROTATE_90):
c34ce3d1 3010 return PLANE_CTL_ROTATE_90;
3b7a5119 3011 case BIT(DRM_ROTATE_180):
c34ce3d1 3012 return PLANE_CTL_ROTATE_180;
3b7a5119 3013 case BIT(DRM_ROTATE_270):
c34ce3d1 3014 return PLANE_CTL_ROTATE_270;
6156a456
CK
3015 default:
3016 MISSING_CASE(rotation);
3017 }
3018
c34ce3d1 3019 return 0;
6156a456
CK
3020}
3021
3022static void skylake_update_primary_plane(struct drm_crtc *crtc,
3023 struct drm_framebuffer *fb,
3024 int x, int y)
3025{
3026 struct drm_device *dev = crtc->dev;
3027 struct drm_i915_private *dev_priv = dev->dev_private;
3028 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b70709a6
ML
3029 struct drm_plane *plane = crtc->primary;
3030 bool visible = to_intel_plane_state(plane->state)->visible;
6156a456
CK
3031 struct drm_i915_gem_object *obj;
3032 int pipe = intel_crtc->pipe;
3033 u32 plane_ctl, stride_div, stride;
3034 u32 tile_height, plane_offset, plane_size;
3035 unsigned int rotation;
3036 int x_offset, y_offset;
3037 unsigned long surf_addr;
6156a456
CK
3038 struct intel_crtc_state *crtc_state = intel_crtc->config;
3039 struct intel_plane_state *plane_state;
3040 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3041 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3042 int scaler_id = -1;
3043
6156a456
CK
3044 plane_state = to_intel_plane_state(plane->state);
3045
b70709a6 3046 if (!visible || !fb) {
6156a456
CK
3047 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3048 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3049 POSTING_READ(PLANE_CTL(pipe, 0));
3050 return;
3b7a5119 3051 }
70d21f0e 3052
6156a456
CK
3053 plane_ctl = PLANE_CTL_ENABLE |
3054 PLANE_CTL_PIPE_GAMMA_ENABLE |
3055 PLANE_CTL_PIPE_CSC_ENABLE;
3056
3057 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3058 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
3059 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
3060
3061 rotation = plane->state->rotation;
3062 plane_ctl |= skl_plane_ctl_rotation(rotation);
3063
b321803d
DL
3064 obj = intel_fb_obj(fb);
3065 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3066 fb->pixel_format);
3b7a5119
SJ
3067 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3068
6156a456
CK
3069 /*
3070 * FIXME: intel_plane_state->src, dst aren't set when transitional
3071 * update_plane helpers are called from legacy paths.
3072 * Once full atomic crtc is available, below check can be avoided.
3073 */
3074 if (drm_rect_width(&plane_state->src)) {
3075 scaler_id = plane_state->scaler_id;
3076 src_x = plane_state->src.x1 >> 16;
3077 src_y = plane_state->src.y1 >> 16;
3078 src_w = drm_rect_width(&plane_state->src) >> 16;
3079 src_h = drm_rect_height(&plane_state->src) >> 16;
3080 dst_x = plane_state->dst.x1;
3081 dst_y = plane_state->dst.y1;
3082 dst_w = drm_rect_width(&plane_state->dst);
3083 dst_h = drm_rect_height(&plane_state->dst);
3084
3085 WARN_ON(x != src_x || y != src_y);
3086 } else {
3087 src_w = intel_crtc->config->pipe_src_w;
3088 src_h = intel_crtc->config->pipe_src_h;
3089 }
3090
3b7a5119
SJ
3091 if (intel_rotation_90_or_270(rotation)) {
3092 /* stride = Surface height in tiles */
2614f17d 3093 tile_height = intel_tile_height(dev, fb->pixel_format,
3b7a5119
SJ
3094 fb->modifier[0]);
3095 stride = DIV_ROUND_UP(fb->height, tile_height);
6156a456 3096 x_offset = stride * tile_height - y - src_h;
3b7a5119 3097 y_offset = x;
6156a456 3098 plane_size = (src_w - 1) << 16 | (src_h - 1);
3b7a5119
SJ
3099 } else {
3100 stride = fb->pitches[0] / stride_div;
3101 x_offset = x;
3102 y_offset = y;
6156a456 3103 plane_size = (src_h - 1) << 16 | (src_w - 1);
3b7a5119
SJ
3104 }
3105 plane_offset = y_offset << 16 | x_offset;
b321803d 3106
70d21f0e 3107 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
3b7a5119
SJ
3108 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3109 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3110 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
6156a456
CK
3111
3112 if (scaler_id >= 0) {
3113 uint32_t ps_ctrl = 0;
3114
3115 WARN_ON(!dst_w || !dst_h);
3116 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3117 crtc_state->scaler_state.scalers[scaler_id].mode;
3118 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3119 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3120 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3121 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3122 I915_WRITE(PLANE_POS(pipe, 0), 0);
3123 } else {
3124 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3125 }
3126
121920fa 3127 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
70d21f0e
DL
3128
3129 POSTING_READ(PLANE_SURF(pipe, 0));
3130}
3131
17638cd6
JB
3132/* Assume fb object is pinned & idle & fenced and just update base pointers */
3133static int
3134intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3135 int x, int y, enum mode_set_atomic state)
3136{
3137 struct drm_device *dev = crtc->dev;
3138 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 3139
6b8e6ed0
CW
3140 if (dev_priv->display.disable_fbc)
3141 dev_priv->display.disable_fbc(dev);
81255565 3142
29b9bde6
DV
3143 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3144
3145 return 0;
81255565
JB
3146}
3147
7514747d 3148static void intel_complete_page_flips(struct drm_device *dev)
96a02917 3149{
96a02917
VS
3150 struct drm_crtc *crtc;
3151
70e1e0ec 3152 for_each_crtc(dev, crtc) {
96a02917
VS
3153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3154 enum plane plane = intel_crtc->plane;
3155
3156 intel_prepare_page_flip(dev, plane);
3157 intel_finish_page_flip_plane(dev, plane);
3158 }
7514747d
VS
3159}
3160
3161static void intel_update_primary_planes(struct drm_device *dev)
3162{
3163 struct drm_i915_private *dev_priv = dev->dev_private;
3164 struct drm_crtc *crtc;
96a02917 3165
70e1e0ec 3166 for_each_crtc(dev, crtc) {
96a02917
VS
3167 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3168
51fd371b 3169 drm_modeset_lock(&crtc->mutex, NULL);
947fdaad
CW
3170 /*
3171 * FIXME: Once we have proper support for primary planes (and
3172 * disabling them without disabling the entire crtc) allow again
66e514c1 3173 * a NULL crtc->primary->fb.
947fdaad 3174 */
f4510a27 3175 if (intel_crtc->active && crtc->primary->fb)
262ca2b0 3176 dev_priv->display.update_primary_plane(crtc,
66e514c1 3177 crtc->primary->fb,
262ca2b0
MR
3178 crtc->x,
3179 crtc->y);
51fd371b 3180 drm_modeset_unlock(&crtc->mutex);
96a02917
VS
3181 }
3182}
3183
ce22dba9
ML
3184void intel_crtc_reset(struct intel_crtc *crtc)
3185{
3186 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3187
3188 if (!crtc->active)
3189 return;
3190
3191 intel_crtc_disable_planes(&crtc->base);
3192 dev_priv->display.crtc_disable(&crtc->base);
3193 dev_priv->display.crtc_enable(&crtc->base);
3194 intel_crtc_enable_planes(&crtc->base);
3195}
3196
7514747d
VS
3197void intel_prepare_reset(struct drm_device *dev)
3198{
f98ce92f
VS
3199 struct drm_i915_private *dev_priv = to_i915(dev);
3200 struct intel_crtc *crtc;
3201
7514747d
VS
3202 /* no reset support for gen2 */
3203 if (IS_GEN2(dev))
3204 return;
3205
3206 /* reset doesn't touch the display */
3207 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3208 return;
3209
3210 drm_modeset_lock_all(dev);
f98ce92f
VS
3211
3212 /*
3213 * Disabling the crtcs gracefully seems nicer. Also the
3214 * g33 docs say we should at least disable all the planes.
3215 */
3216 for_each_intel_crtc(dev, crtc) {
ce22dba9
ML
3217 if (!crtc->active)
3218 continue;
3219
3220 intel_crtc_disable_planes(&crtc->base);
3221 dev_priv->display.crtc_disable(&crtc->base);
f98ce92f 3222 }
7514747d
VS
3223}
3224
3225void intel_finish_reset(struct drm_device *dev)
3226{
3227 struct drm_i915_private *dev_priv = to_i915(dev);
3228
3229 /*
3230 * Flips in the rings will be nuked by the reset,
3231 * so complete all pending flips so that user space
3232 * will get its events and not get stuck.
3233 */
3234 intel_complete_page_flips(dev);
3235
3236 /* no reset support for gen2 */
3237 if (IS_GEN2(dev))
3238 return;
3239
3240 /* reset doesn't touch the display */
3241 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3242 /*
3243 * Flips in the rings have been nuked by the reset,
3244 * so update the base address of all primary
3245 * planes to the the last fb to make sure we're
3246 * showing the correct fb after a reset.
3247 */
3248 intel_update_primary_planes(dev);
3249 return;
3250 }
3251
3252 /*
3253 * The display has been reset as well,
3254 * so need a full re-initialization.
3255 */
3256 intel_runtime_pm_disable_interrupts(dev_priv);
3257 intel_runtime_pm_enable_interrupts(dev_priv);
3258
3259 intel_modeset_init_hw(dev);
3260
3261 spin_lock_irq(&dev_priv->irq_lock);
3262 if (dev_priv->display.hpd_irq_setup)
3263 dev_priv->display.hpd_irq_setup(dev);
3264 spin_unlock_irq(&dev_priv->irq_lock);
3265
3266 intel_modeset_setup_hw_state(dev, true);
3267
3268 intel_hpd_init(dev_priv);
3269
3270 drm_modeset_unlock_all(dev);
3271}
3272
2e2f351d 3273static void
14667a4b
CW
3274intel_finish_fb(struct drm_framebuffer *old_fb)
3275{
2ff8fde1 3276 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
2e2f351d 3277 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
14667a4b
CW
3278 bool was_interruptible = dev_priv->mm.interruptible;
3279 int ret;
3280
14667a4b
CW
3281 /* Big Hammer, we also need to ensure that any pending
3282 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3283 * current scanout is retired before unpinning the old
2e2f351d
CW
3284 * framebuffer. Note that we rely on userspace rendering
3285 * into the buffer attached to the pipe they are waiting
3286 * on. If not, userspace generates a GPU hang with IPEHR
3287 * point to the MI_WAIT_FOR_EVENT.
14667a4b
CW
3288 *
3289 * This should only fail upon a hung GPU, in which case we
3290 * can safely continue.
3291 */
3292 dev_priv->mm.interruptible = false;
2e2f351d 3293 ret = i915_gem_object_wait_rendering(obj, true);
14667a4b
CW
3294 dev_priv->mm.interruptible = was_interruptible;
3295
2e2f351d 3296 WARN_ON(ret);
14667a4b
CW
3297}
3298
7d5e3799
CW
3299static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3300{
3301 struct drm_device *dev = crtc->dev;
3302 struct drm_i915_private *dev_priv = dev->dev_private;
3303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7d5e3799
CW
3304 bool pending;
3305
3306 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3307 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3308 return false;
3309
5e2d7afc 3310 spin_lock_irq(&dev->event_lock);
7d5e3799 3311 pending = to_intel_crtc(crtc)->unpin_work != NULL;
5e2d7afc 3312 spin_unlock_irq(&dev->event_lock);
7d5e3799
CW
3313
3314 return pending;
3315}
3316
e30e8f75
GP
3317static void intel_update_pipe_size(struct intel_crtc *crtc)
3318{
3319 struct drm_device *dev = crtc->base.dev;
3320 struct drm_i915_private *dev_priv = dev->dev_private;
3321 const struct drm_display_mode *adjusted_mode;
3322
3323 if (!i915.fastboot)
3324 return;
3325
3326 /*
3327 * Update pipe size and adjust fitter if needed: the reason for this is
3328 * that in compute_mode_changes we check the native mode (not the pfit
3329 * mode) to see if we can flip rather than do a full mode set. In the
3330 * fastboot case, we'll flip, but if we don't update the pipesrc and
3331 * pfit state, we'll end up with a big fb scanned out into the wrong
3332 * sized surface.
3333 *
3334 * To fix this properly, we need to hoist the checks up into
3335 * compute_mode_changes (or above), check the actual pfit state and
3336 * whether the platform allows pfit disable with pipe active, and only
3337 * then update the pipesrc and pfit state, even on the flip path.
3338 */
3339
6e3c9717 3340 adjusted_mode = &crtc->config->base.adjusted_mode;
e30e8f75
GP
3341
3342 I915_WRITE(PIPESRC(crtc->pipe),
3343 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3344 (adjusted_mode->crtc_vdisplay - 1));
6e3c9717 3345 if (!crtc->config->pch_pfit.enabled &&
409ee761
ACO
3346 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3347 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
e30e8f75
GP
3348 I915_WRITE(PF_CTL(crtc->pipe), 0);
3349 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3350 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3351 }
6e3c9717
ACO
3352 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3353 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
e30e8f75
GP
3354}
3355
5e84e1a4
ZW
3356static void intel_fdi_normal_train(struct drm_crtc *crtc)
3357{
3358 struct drm_device *dev = crtc->dev;
3359 struct drm_i915_private *dev_priv = dev->dev_private;
3360 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3361 int pipe = intel_crtc->pipe;
3362 u32 reg, temp;
3363
3364 /* enable normal train */
3365 reg = FDI_TX_CTL(pipe);
3366 temp = I915_READ(reg);
61e499bf 3367 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
3368 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3369 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
3370 } else {
3371 temp &= ~FDI_LINK_TRAIN_NONE;
3372 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 3373 }
5e84e1a4
ZW
3374 I915_WRITE(reg, temp);
3375
3376 reg = FDI_RX_CTL(pipe);
3377 temp = I915_READ(reg);
3378 if (HAS_PCH_CPT(dev)) {
3379 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3380 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3381 } else {
3382 temp &= ~FDI_LINK_TRAIN_NONE;
3383 temp |= FDI_LINK_TRAIN_NONE;
3384 }
3385 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3386
3387 /* wait one idle pattern time */
3388 POSTING_READ(reg);
3389 udelay(1000);
357555c0
JB
3390
3391 /* IVB wants error correction enabled */
3392 if (IS_IVYBRIDGE(dev))
3393 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3394 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
3395}
3396
8db9d77b
ZW
3397/* The FDI link training functions for ILK/Ibexpeak. */
3398static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3399{
3400 struct drm_device *dev = crtc->dev;
3401 struct drm_i915_private *dev_priv = dev->dev_private;
3402 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3403 int pipe = intel_crtc->pipe;
5eddb70b 3404 u32 reg, temp, tries;
8db9d77b 3405
1c8562f6 3406 /* FDI needs bits from pipe first */
0fc932b8 3407 assert_pipe_enabled(dev_priv, pipe);
0fc932b8 3408
e1a44743
AJ
3409 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3410 for train result */
5eddb70b
CW
3411 reg = FDI_RX_IMR(pipe);
3412 temp = I915_READ(reg);
e1a44743
AJ
3413 temp &= ~FDI_RX_SYMBOL_LOCK;
3414 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3415 I915_WRITE(reg, temp);
3416 I915_READ(reg);
e1a44743
AJ
3417 udelay(150);
3418
8db9d77b 3419 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3420 reg = FDI_TX_CTL(pipe);
3421 temp = I915_READ(reg);
627eb5a3 3422 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3423 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3424 temp &= ~FDI_LINK_TRAIN_NONE;
3425 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 3426 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3427
5eddb70b
CW
3428 reg = FDI_RX_CTL(pipe);
3429 temp = I915_READ(reg);
8db9d77b
ZW
3430 temp &= ~FDI_LINK_TRAIN_NONE;
3431 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
3432 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3433
3434 POSTING_READ(reg);
8db9d77b
ZW
3435 udelay(150);
3436
5b2adf89 3437 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
3438 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3439 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3440 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 3441
5eddb70b 3442 reg = FDI_RX_IIR(pipe);
e1a44743 3443 for (tries = 0; tries < 5; tries++) {
5eddb70b 3444 temp = I915_READ(reg);
8db9d77b
ZW
3445 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3446
3447 if ((temp & FDI_RX_BIT_LOCK)) {
3448 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 3449 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
3450 break;
3451 }
8db9d77b 3452 }
e1a44743 3453 if (tries == 5)
5eddb70b 3454 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3455
3456 /* Train 2 */
5eddb70b
CW
3457 reg = FDI_TX_CTL(pipe);
3458 temp = I915_READ(reg);
8db9d77b
ZW
3459 temp &= ~FDI_LINK_TRAIN_NONE;
3460 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3461 I915_WRITE(reg, temp);
8db9d77b 3462
5eddb70b
CW
3463 reg = FDI_RX_CTL(pipe);
3464 temp = I915_READ(reg);
8db9d77b
ZW
3465 temp &= ~FDI_LINK_TRAIN_NONE;
3466 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 3467 I915_WRITE(reg, temp);
8db9d77b 3468
5eddb70b
CW
3469 POSTING_READ(reg);
3470 udelay(150);
8db9d77b 3471
5eddb70b 3472 reg = FDI_RX_IIR(pipe);
e1a44743 3473 for (tries = 0; tries < 5; tries++) {
5eddb70b 3474 temp = I915_READ(reg);
8db9d77b
ZW
3475 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3476
3477 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 3478 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
3479 DRM_DEBUG_KMS("FDI train 2 done.\n");
3480 break;
3481 }
8db9d77b 3482 }
e1a44743 3483 if (tries == 5)
5eddb70b 3484 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3485
3486 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 3487
8db9d77b
ZW
3488}
3489
0206e353 3490static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
3491 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3492 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3493 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3494 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3495};
3496
3497/* The FDI link training functions for SNB/Cougarpoint. */
3498static void gen6_fdi_link_train(struct drm_crtc *crtc)
3499{
3500 struct drm_device *dev = crtc->dev;
3501 struct drm_i915_private *dev_priv = dev->dev_private;
3502 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3503 int pipe = intel_crtc->pipe;
fa37d39e 3504 u32 reg, temp, i, retry;
8db9d77b 3505
e1a44743
AJ
3506 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3507 for train result */
5eddb70b
CW
3508 reg = FDI_RX_IMR(pipe);
3509 temp = I915_READ(reg);
e1a44743
AJ
3510 temp &= ~FDI_RX_SYMBOL_LOCK;
3511 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
3512 I915_WRITE(reg, temp);
3513
3514 POSTING_READ(reg);
e1a44743
AJ
3515 udelay(150);
3516
8db9d77b 3517 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
3518 reg = FDI_TX_CTL(pipe);
3519 temp = I915_READ(reg);
627eb5a3 3520 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3521 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
8db9d77b
ZW
3522 temp &= ~FDI_LINK_TRAIN_NONE;
3523 temp |= FDI_LINK_TRAIN_PATTERN_1;
3524 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3525 /* SNB-B */
3526 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 3527 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 3528
d74cf324
DV
3529 I915_WRITE(FDI_RX_MISC(pipe),
3530 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3531
5eddb70b
CW
3532 reg = FDI_RX_CTL(pipe);
3533 temp = I915_READ(reg);
8db9d77b
ZW
3534 if (HAS_PCH_CPT(dev)) {
3535 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3536 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3537 } else {
3538 temp &= ~FDI_LINK_TRAIN_NONE;
3539 temp |= FDI_LINK_TRAIN_PATTERN_1;
3540 }
5eddb70b
CW
3541 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3542
3543 POSTING_READ(reg);
8db9d77b
ZW
3544 udelay(150);
3545
0206e353 3546 for (i = 0; i < 4; i++) {
5eddb70b
CW
3547 reg = FDI_TX_CTL(pipe);
3548 temp = I915_READ(reg);
8db9d77b
ZW
3549 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3550 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3551 I915_WRITE(reg, temp);
3552
3553 POSTING_READ(reg);
8db9d77b
ZW
3554 udelay(500);
3555
fa37d39e
SP
3556 for (retry = 0; retry < 5; retry++) {
3557 reg = FDI_RX_IIR(pipe);
3558 temp = I915_READ(reg);
3559 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3560 if (temp & FDI_RX_BIT_LOCK) {
3561 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3562 DRM_DEBUG_KMS("FDI train 1 done.\n");
3563 break;
3564 }
3565 udelay(50);
8db9d77b 3566 }
fa37d39e
SP
3567 if (retry < 5)
3568 break;
8db9d77b
ZW
3569 }
3570 if (i == 4)
5eddb70b 3571 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
3572
3573 /* Train 2 */
5eddb70b
CW
3574 reg = FDI_TX_CTL(pipe);
3575 temp = I915_READ(reg);
8db9d77b
ZW
3576 temp &= ~FDI_LINK_TRAIN_NONE;
3577 temp |= FDI_LINK_TRAIN_PATTERN_2;
3578 if (IS_GEN6(dev)) {
3579 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3580 /* SNB-B */
3581 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3582 }
5eddb70b 3583 I915_WRITE(reg, temp);
8db9d77b 3584
5eddb70b
CW
3585 reg = FDI_RX_CTL(pipe);
3586 temp = I915_READ(reg);
8db9d77b
ZW
3587 if (HAS_PCH_CPT(dev)) {
3588 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3589 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3590 } else {
3591 temp &= ~FDI_LINK_TRAIN_NONE;
3592 temp |= FDI_LINK_TRAIN_PATTERN_2;
3593 }
5eddb70b
CW
3594 I915_WRITE(reg, temp);
3595
3596 POSTING_READ(reg);
8db9d77b
ZW
3597 udelay(150);
3598
0206e353 3599 for (i = 0; i < 4; i++) {
5eddb70b
CW
3600 reg = FDI_TX_CTL(pipe);
3601 temp = I915_READ(reg);
8db9d77b
ZW
3602 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3603 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
3604 I915_WRITE(reg, temp);
3605
3606 POSTING_READ(reg);
8db9d77b
ZW
3607 udelay(500);
3608
fa37d39e
SP
3609 for (retry = 0; retry < 5; retry++) {
3610 reg = FDI_RX_IIR(pipe);
3611 temp = I915_READ(reg);
3612 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3613 if (temp & FDI_RX_SYMBOL_LOCK) {
3614 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3615 DRM_DEBUG_KMS("FDI train 2 done.\n");
3616 break;
3617 }
3618 udelay(50);
8db9d77b 3619 }
fa37d39e
SP
3620 if (retry < 5)
3621 break;
8db9d77b
ZW
3622 }
3623 if (i == 4)
5eddb70b 3624 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
3625
3626 DRM_DEBUG_KMS("FDI train done.\n");
3627}
3628
357555c0
JB
3629/* Manual link training for Ivy Bridge A0 parts */
3630static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3631{
3632 struct drm_device *dev = crtc->dev;
3633 struct drm_i915_private *dev_priv = dev->dev_private;
3634 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3635 int pipe = intel_crtc->pipe;
139ccd3f 3636 u32 reg, temp, i, j;
357555c0
JB
3637
3638 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3639 for train result */
3640 reg = FDI_RX_IMR(pipe);
3641 temp = I915_READ(reg);
3642 temp &= ~FDI_RX_SYMBOL_LOCK;
3643 temp &= ~FDI_RX_BIT_LOCK;
3644 I915_WRITE(reg, temp);
3645
3646 POSTING_READ(reg);
3647 udelay(150);
3648
01a415fd
DV
3649 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3650 I915_READ(FDI_RX_IIR(pipe)));
3651
139ccd3f
JB
3652 /* Try each vswing and preemphasis setting twice before moving on */
3653 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3654 /* disable first in case we need to retry */
3655 reg = FDI_TX_CTL(pipe);
3656 temp = I915_READ(reg);
3657 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3658 temp &= ~FDI_TX_ENABLE;
3659 I915_WRITE(reg, temp);
357555c0 3660
139ccd3f
JB
3661 reg = FDI_RX_CTL(pipe);
3662 temp = I915_READ(reg);
3663 temp &= ~FDI_LINK_TRAIN_AUTO;
3664 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3665 temp &= ~FDI_RX_ENABLE;
3666 I915_WRITE(reg, temp);
357555c0 3667
139ccd3f 3668 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
3669 reg = FDI_TX_CTL(pipe);
3670 temp = I915_READ(reg);
139ccd3f 3671 temp &= ~FDI_DP_PORT_WIDTH_MASK;
6e3c9717 3672 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
139ccd3f 3673 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 3674 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
3675 temp |= snb_b_fdi_train_param[j/2];
3676 temp |= FDI_COMPOSITE_SYNC;
3677 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 3678
139ccd3f
JB
3679 I915_WRITE(FDI_RX_MISC(pipe),
3680 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 3681
139ccd3f 3682 reg = FDI_RX_CTL(pipe);
357555c0 3683 temp = I915_READ(reg);
139ccd3f
JB
3684 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3685 temp |= FDI_COMPOSITE_SYNC;
3686 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 3687
139ccd3f
JB
3688 POSTING_READ(reg);
3689 udelay(1); /* should be 0.5us */
357555c0 3690
139ccd3f
JB
3691 for (i = 0; i < 4; i++) {
3692 reg = FDI_RX_IIR(pipe);
3693 temp = I915_READ(reg);
3694 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3695
139ccd3f
JB
3696 if (temp & FDI_RX_BIT_LOCK ||
3697 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3698 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3699 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3700 i);
3701 break;
3702 }
3703 udelay(1); /* should be 0.5us */
3704 }
3705 if (i == 4) {
3706 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3707 continue;
3708 }
357555c0 3709
139ccd3f 3710 /* Train 2 */
357555c0
JB
3711 reg = FDI_TX_CTL(pipe);
3712 temp = I915_READ(reg);
139ccd3f
JB
3713 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3714 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3715 I915_WRITE(reg, temp);
3716
3717 reg = FDI_RX_CTL(pipe);
3718 temp = I915_READ(reg);
3719 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3720 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
3721 I915_WRITE(reg, temp);
3722
3723 POSTING_READ(reg);
139ccd3f 3724 udelay(2); /* should be 1.5us */
357555c0 3725
139ccd3f
JB
3726 for (i = 0; i < 4; i++) {
3727 reg = FDI_RX_IIR(pipe);
3728 temp = I915_READ(reg);
3729 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 3730
139ccd3f
JB
3731 if (temp & FDI_RX_SYMBOL_LOCK ||
3732 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3733 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3734 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3735 i);
3736 goto train_done;
3737 }
3738 udelay(2); /* should be 1.5us */
357555c0 3739 }
139ccd3f
JB
3740 if (i == 4)
3741 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 3742 }
357555c0 3743
139ccd3f 3744train_done:
357555c0
JB
3745 DRM_DEBUG_KMS("FDI train done.\n");
3746}
3747
88cefb6c 3748static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 3749{
88cefb6c 3750 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 3751 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 3752 int pipe = intel_crtc->pipe;
5eddb70b 3753 u32 reg, temp;
79e53945 3754
c64e311e 3755
c98e9dcf 3756 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
3757 reg = FDI_RX_CTL(pipe);
3758 temp = I915_READ(reg);
627eb5a3 3759 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
6e3c9717 3760 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
dfd07d72 3761 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
3762 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3763
3764 POSTING_READ(reg);
c98e9dcf
JB
3765 udelay(200);
3766
3767 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
3768 temp = I915_READ(reg);
3769 I915_WRITE(reg, temp | FDI_PCDCLK);
3770
3771 POSTING_READ(reg);
c98e9dcf
JB
3772 udelay(200);
3773
20749730
PZ
3774 /* Enable CPU FDI TX PLL, always on for Ironlake */
3775 reg = FDI_TX_CTL(pipe);
3776 temp = I915_READ(reg);
3777 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3778 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3779
20749730
PZ
3780 POSTING_READ(reg);
3781 udelay(100);
6be4a607 3782 }
0e23b99d
JB
3783}
3784
88cefb6c
DV
3785static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3786{
3787 struct drm_device *dev = intel_crtc->base.dev;
3788 struct drm_i915_private *dev_priv = dev->dev_private;
3789 int pipe = intel_crtc->pipe;
3790 u32 reg, temp;
3791
3792 /* Switch from PCDclk to Rawclk */
3793 reg = FDI_RX_CTL(pipe);
3794 temp = I915_READ(reg);
3795 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3796
3797 /* Disable CPU FDI TX PLL */
3798 reg = FDI_TX_CTL(pipe);
3799 temp = I915_READ(reg);
3800 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3801
3802 POSTING_READ(reg);
3803 udelay(100);
3804
3805 reg = FDI_RX_CTL(pipe);
3806 temp = I915_READ(reg);
3807 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3808
3809 /* Wait for the clocks to turn off. */
3810 POSTING_READ(reg);
3811 udelay(100);
3812}
3813
0fc932b8
JB
3814static void ironlake_fdi_disable(struct drm_crtc *crtc)
3815{
3816 struct drm_device *dev = crtc->dev;
3817 struct drm_i915_private *dev_priv = dev->dev_private;
3818 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3819 int pipe = intel_crtc->pipe;
3820 u32 reg, temp;
3821
3822 /* disable CPU FDI tx and PCH FDI rx */
3823 reg = FDI_TX_CTL(pipe);
3824 temp = I915_READ(reg);
3825 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3826 POSTING_READ(reg);
3827
3828 reg = FDI_RX_CTL(pipe);
3829 temp = I915_READ(reg);
3830 temp &= ~(0x7 << 16);
dfd07d72 3831 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3832 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3833
3834 POSTING_READ(reg);
3835 udelay(100);
3836
3837 /* Ironlake workaround, disable clock pointer after downing FDI */
eba905b2 3838 if (HAS_PCH_IBX(dev))
6f06ce18 3839 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
0fc932b8
JB
3840
3841 /* still set train pattern 1 */
3842 reg = FDI_TX_CTL(pipe);
3843 temp = I915_READ(reg);
3844 temp &= ~FDI_LINK_TRAIN_NONE;
3845 temp |= FDI_LINK_TRAIN_PATTERN_1;
3846 I915_WRITE(reg, temp);
3847
3848 reg = FDI_RX_CTL(pipe);
3849 temp = I915_READ(reg);
3850 if (HAS_PCH_CPT(dev)) {
3851 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3852 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3853 } else {
3854 temp &= ~FDI_LINK_TRAIN_NONE;
3855 temp |= FDI_LINK_TRAIN_PATTERN_1;
3856 }
3857 /* BPC in FDI rx is consistent with that in PIPECONF */
3858 temp &= ~(0x07 << 16);
dfd07d72 3859 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3860 I915_WRITE(reg, temp);
3861
3862 POSTING_READ(reg);
3863 udelay(100);
3864}
3865
5dce5b93
CW
3866bool intel_has_pending_fb_unpin(struct drm_device *dev)
3867{
3868 struct intel_crtc *crtc;
3869
3870 /* Note that we don't need to be called with mode_config.lock here
3871 * as our list of CRTC objects is static for the lifetime of the
3872 * device and so cannot disappear as we iterate. Similarly, we can
3873 * happily treat the predicates as racy, atomic checks as userspace
3874 * cannot claim and pin a new fb without at least acquring the
3875 * struct_mutex and so serialising with us.
3876 */
d3fcc808 3877 for_each_intel_crtc(dev, crtc) {
5dce5b93
CW
3878 if (atomic_read(&crtc->unpin_work_count) == 0)
3879 continue;
3880
3881 if (crtc->unpin_work)
3882 intel_wait_for_vblank(dev, crtc->pipe);
3883
3884 return true;
3885 }
3886
3887 return false;
3888}
3889
d6bbafa1
CW
3890static void page_flip_completed(struct intel_crtc *intel_crtc)
3891{
3892 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3893 struct intel_unpin_work *work = intel_crtc->unpin_work;
3894
3895 /* ensure that the unpin work is consistent wrt ->pending. */
3896 smp_rmb();
3897 intel_crtc->unpin_work = NULL;
3898
3899 if (work->event)
3900 drm_send_vblank_event(intel_crtc->base.dev,
3901 intel_crtc->pipe,
3902 work->event);
3903
3904 drm_crtc_vblank_put(&intel_crtc->base);
3905
3906 wake_up_all(&dev_priv->pending_flip_queue);
3907 queue_work(dev_priv->wq, &work->work);
3908
3909 trace_i915_flip_complete(intel_crtc->plane,
3910 work->pending_flip_obj);
3911}
3912
46a55d30 3913void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
e6c3a2a6 3914{
0f91128d 3915 struct drm_device *dev = crtc->dev;
5bb61643 3916 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6 3917
2c10d571 3918 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
9c787942
CW
3919 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3920 !intel_crtc_has_pending_flip(crtc),
3921 60*HZ) == 0)) {
3922 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2c10d571 3923
5e2d7afc 3924 spin_lock_irq(&dev->event_lock);
9c787942
CW
3925 if (intel_crtc->unpin_work) {
3926 WARN_ONCE(1, "Removing stuck page flip\n");
3927 page_flip_completed(intel_crtc);
3928 }
5e2d7afc 3929 spin_unlock_irq(&dev->event_lock);
9c787942 3930 }
5bb61643 3931
975d568a
CW
3932 if (crtc->primary->fb) {
3933 mutex_lock(&dev->struct_mutex);
3934 intel_finish_fb(crtc->primary->fb);
3935 mutex_unlock(&dev->struct_mutex);
3936 }
e6c3a2a6
CW
3937}
3938
e615efe4
ED
3939/* Program iCLKIP clock to the desired frequency */
3940static void lpt_program_iclkip(struct drm_crtc *crtc)
3941{
3942 struct drm_device *dev = crtc->dev;
3943 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 3944 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
e615efe4
ED
3945 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3946 u32 temp;
3947
09153000
DV
3948 mutex_lock(&dev_priv->dpio_lock);
3949
e615efe4
ED
3950 /* It is necessary to ungate the pixclk gate prior to programming
3951 * the divisors, and gate it back when it is done.
3952 */
3953 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3954
3955 /* Disable SSCCTL */
3956 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3957 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3958 SBI_SSCCTL_DISABLE,
3959 SBI_ICLK);
e615efe4
ED
3960
3961 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3962 if (clock == 20000) {
e615efe4
ED
3963 auxdiv = 1;
3964 divsel = 0x41;
3965 phaseinc = 0x20;
3966 } else {
3967 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3968 * but the adjusted_mode->crtc_clock in in KHz. To get the
3969 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3970 * convert the virtual clock precision to KHz here for higher
3971 * precision.
3972 */
3973 u32 iclk_virtual_root_freq = 172800 * 1000;
3974 u32 iclk_pi_range = 64;
3975 u32 desired_divisor, msb_divisor_value, pi_value;
3976
12d7ceed 3977 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3978 msb_divisor_value = desired_divisor / iclk_pi_range;
3979 pi_value = desired_divisor % iclk_pi_range;
3980
3981 auxdiv = 0;
3982 divsel = msb_divisor_value - 2;
3983 phaseinc = pi_value;
3984 }
3985
3986 /* This should not happen with any sane values */
3987 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3988 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3989 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3990 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3991
3992 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3993 clock,
e615efe4
ED
3994 auxdiv,
3995 divsel,
3996 phasedir,
3997 phaseinc);
3998
3999 /* Program SSCDIVINTPHASE6 */
988d6ee8 4000 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
4001 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4002 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4003 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4004 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4005 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4006 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 4007 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
4008
4009 /* Program SSCAUXDIV */
988d6ee8 4010 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
4011 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4012 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 4013 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
4014
4015 /* Enable modulator and associated divider */
988d6ee8 4016 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 4017 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 4018 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
4019
4020 /* Wait for initialization time */
4021 udelay(24);
4022
4023 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
4024
4025 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
4026}
4027
275f01b2
DV
4028static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4029 enum pipe pch_transcoder)
4030{
4031 struct drm_device *dev = crtc->base.dev;
4032 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 4033 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
275f01b2
DV
4034
4035 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4036 I915_READ(HTOTAL(cpu_transcoder)));
4037 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4038 I915_READ(HBLANK(cpu_transcoder)));
4039 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4040 I915_READ(HSYNC(cpu_transcoder)));
4041
4042 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4043 I915_READ(VTOTAL(cpu_transcoder)));
4044 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4045 I915_READ(VBLANK(cpu_transcoder)));
4046 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4047 I915_READ(VSYNC(cpu_transcoder)));
4048 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4049 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4050}
4051
003632d9 4052static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
1fbc0d78
DV
4053{
4054 struct drm_i915_private *dev_priv = dev->dev_private;
4055 uint32_t temp;
4056
4057 temp = I915_READ(SOUTH_CHICKEN1);
003632d9 4058 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
1fbc0d78
DV
4059 return;
4060
4061 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4062 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4063
003632d9
ACO
4064 temp &= ~FDI_BC_BIFURCATION_SELECT;
4065 if (enable)
4066 temp |= FDI_BC_BIFURCATION_SELECT;
4067
4068 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
1fbc0d78
DV
4069 I915_WRITE(SOUTH_CHICKEN1, temp);
4070 POSTING_READ(SOUTH_CHICKEN1);
4071}
4072
4073static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4074{
4075 struct drm_device *dev = intel_crtc->base.dev;
1fbc0d78
DV
4076
4077 switch (intel_crtc->pipe) {
4078 case PIPE_A:
4079 break;
4080 case PIPE_B:
6e3c9717 4081 if (intel_crtc->config->fdi_lanes > 2)
003632d9 4082 cpt_set_fdi_bc_bifurcation(dev, false);
1fbc0d78 4083 else
003632d9 4084 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4085
4086 break;
4087 case PIPE_C:
003632d9 4088 cpt_set_fdi_bc_bifurcation(dev, true);
1fbc0d78
DV
4089
4090 break;
4091 default:
4092 BUG();
4093 }
4094}
4095
f67a559d
JB
4096/*
4097 * Enable PCH resources required for PCH ports:
4098 * - PCH PLLs
4099 * - FDI training & RX/TX
4100 * - update transcoder timings
4101 * - DP transcoding bits
4102 * - transcoder
4103 */
4104static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
4105{
4106 struct drm_device *dev = crtc->dev;
4107 struct drm_i915_private *dev_priv = dev->dev_private;
4108 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4109 int pipe = intel_crtc->pipe;
ee7b9f93 4110 u32 reg, temp;
2c07245f 4111
ab9412ba 4112 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 4113
1fbc0d78
DV
4114 if (IS_IVYBRIDGE(dev))
4115 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4116
cd986abb
DV
4117 /* Write the TU size bits before fdi link training, so that error
4118 * detection works. */
4119 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4120 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4121
c98e9dcf 4122 /* For PCH output, training FDI link */
674cf967 4123 dev_priv->display.fdi_link_train(crtc);
2c07245f 4124
3ad8a208
DV
4125 /* We need to program the right clock selection before writing the pixel
4126 * mutliplier into the DPLL. */
303b81e0 4127 if (HAS_PCH_CPT(dev)) {
ee7b9f93 4128 u32 sel;
4b645f14 4129
c98e9dcf 4130 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
4131 temp |= TRANS_DPLL_ENABLE(pipe);
4132 sel = TRANS_DPLLB_SEL(pipe);
6e3c9717 4133 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
4134 temp |= sel;
4135 else
4136 temp &= ~sel;
c98e9dcf 4137 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 4138 }
5eddb70b 4139
3ad8a208
DV
4140 /* XXX: pch pll's can be enabled any time before we enable the PCH
4141 * transcoder, and we actually should do this to not upset any PCH
4142 * transcoder that already use the clock when we share it.
4143 *
4144 * Note that enable_shared_dpll tries to do the right thing, but
4145 * get_shared_dpll unconditionally resets the pll - we need that to have
4146 * the right LVDS enable sequence. */
85b3894f 4147 intel_enable_shared_dpll(intel_crtc);
3ad8a208 4148
d9b6cb56
JB
4149 /* set transcoder timing, panel must allow it */
4150 assert_panel_unlocked(dev_priv, pipe);
275f01b2 4151 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 4152
303b81e0 4153 intel_fdi_normal_train(crtc);
5e84e1a4 4154
c98e9dcf 4155 /* For PCH DP, enable TRANS_DP_CTL */
6e3c9717 4156 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
dfd07d72 4157 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
4158 reg = TRANS_DP_CTL(pipe);
4159 temp = I915_READ(reg);
4160 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
4161 TRANS_DP_SYNC_MASK |
4162 TRANS_DP_BPC_MASK);
5eddb70b
CW
4163 temp |= (TRANS_DP_OUTPUT_ENABLE |
4164 TRANS_DP_ENH_FRAMING);
9325c9f0 4165 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
4166
4167 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 4168 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 4169 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 4170 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
4171
4172 switch (intel_trans_dp_port_sel(crtc)) {
4173 case PCH_DP_B:
5eddb70b 4174 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
4175 break;
4176 case PCH_DP_C:
5eddb70b 4177 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
4178 break;
4179 case PCH_DP_D:
5eddb70b 4180 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
4181 break;
4182 default:
e95d41e1 4183 BUG();
32f9d658 4184 }
2c07245f 4185
5eddb70b 4186 I915_WRITE(reg, temp);
6be4a607 4187 }
b52eb4dc 4188
b8a4f404 4189 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
4190}
4191
1507e5bd
PZ
4192static void lpt_pch_enable(struct drm_crtc *crtc)
4193{
4194 struct drm_device *dev = crtc->dev;
4195 struct drm_i915_private *dev_priv = dev->dev_private;
4196 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 4197 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
1507e5bd 4198
ab9412ba 4199 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 4200
8c52b5e8 4201 lpt_program_iclkip(crtc);
1507e5bd 4202
0540e488 4203 /* Set transcoder timing. */
275f01b2 4204 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 4205
937bb610 4206 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
4207}
4208
716c2e55 4209void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 4210{
e2b78267 4211 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
4212
4213 if (pll == NULL)
4214 return;
4215
3e369b76 4216 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
1e6f2ddc 4217 WARN(1, "bad %s crtc mask\n", pll->name);
ee7b9f93
JB
4218 return;
4219 }
4220
3e369b76
ACO
4221 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4222 if (pll->config.crtc_mask == 0) {
f4a091c7
DV
4223 WARN_ON(pll->on);
4224 WARN_ON(pll->active);
4225 }
4226
6e3c9717 4227 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
4228}
4229
190f68c5
ACO
4230struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4231 struct intel_crtc_state *crtc_state)
ee7b9f93 4232{
e2b78267 4233 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8bd31e67 4234 struct intel_shared_dpll *pll;
e2b78267 4235 enum intel_dpll_id i;
ee7b9f93 4236
98b6bd99
DV
4237 if (HAS_PCH_IBX(dev_priv->dev)) {
4238 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 4239 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 4240 pll = &dev_priv->shared_dplls[i];
98b6bd99 4241
46edb027
DV
4242 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4243 crtc->base.base.id, pll->name);
98b6bd99 4244
8bd31e67 4245 WARN_ON(pll->new_config->crtc_mask);
f2a69f44 4246
98b6bd99
DV
4247 goto found;
4248 }
4249
bcddf610
S
4250 if (IS_BROXTON(dev_priv->dev)) {
4251 /* PLL is attached to port in bxt */
4252 struct intel_encoder *encoder;
4253 struct intel_digital_port *intel_dig_port;
4254
4255 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4256 if (WARN_ON(!encoder))
4257 return NULL;
4258
4259 intel_dig_port = enc_to_dig_port(&encoder->base);
4260 /* 1:1 mapping between ports and PLLs */
4261 i = (enum intel_dpll_id)intel_dig_port->port;
4262 pll = &dev_priv->shared_dplls[i];
4263 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4264 crtc->base.base.id, pll->name);
4265 WARN_ON(pll->new_config->crtc_mask);
4266
4267 goto found;
4268 }
4269
e72f9fbf
DV
4270 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4271 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
4272
4273 /* Only want to check enabled timings first */
8bd31e67 4274 if (pll->new_config->crtc_mask == 0)
ee7b9f93
JB
4275 continue;
4276
190f68c5 4277 if (memcmp(&crtc_state->dpll_hw_state,
8bd31e67
ACO
4278 &pll->new_config->hw_state,
4279 sizeof(pll->new_config->hw_state)) == 0) {
4280 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
1e6f2ddc 4281 crtc->base.base.id, pll->name,
8bd31e67
ACO
4282 pll->new_config->crtc_mask,
4283 pll->active);
ee7b9f93
JB
4284 goto found;
4285 }
4286 }
4287
4288 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
4289 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4290 pll = &dev_priv->shared_dplls[i];
8bd31e67 4291 if (pll->new_config->crtc_mask == 0) {
46edb027
DV
4292 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4293 crtc->base.base.id, pll->name);
ee7b9f93
JB
4294 goto found;
4295 }
4296 }
4297
4298 return NULL;
4299
4300found:
8bd31e67 4301 if (pll->new_config->crtc_mask == 0)
190f68c5 4302 pll->new_config->hw_state = crtc_state->dpll_hw_state;
f2a69f44 4303
190f68c5 4304 crtc_state->shared_dpll = i;
46edb027
DV
4305 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4306 pipe_name(crtc->pipe));
ee7b9f93 4307
8bd31e67 4308 pll->new_config->crtc_mask |= 1 << crtc->pipe;
e04c7350 4309
ee7b9f93
JB
4310 return pll;
4311}
4312
8bd31e67
ACO
4313/**
4314 * intel_shared_dpll_start_config - start a new PLL staged config
4315 * @dev_priv: DRM device
4316 * @clear_pipes: mask of pipes that will have their PLLs freed
4317 *
4318 * Starts a new PLL staged config, copying the current config but
4319 * releasing the references of pipes specified in clear_pipes.
4320 */
4321static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4322 unsigned clear_pipes)
4323{
4324 struct intel_shared_dpll *pll;
4325 enum intel_dpll_id i;
4326
4327 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4328 pll = &dev_priv->shared_dplls[i];
4329
4330 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4331 GFP_KERNEL);
4332 if (!pll->new_config)
4333 goto cleanup;
4334
4335 pll->new_config->crtc_mask &= ~clear_pipes;
4336 }
4337
4338 return 0;
4339
4340cleanup:
4341 while (--i >= 0) {
4342 pll = &dev_priv->shared_dplls[i];
f354d733 4343 kfree(pll->new_config);
8bd31e67
ACO
4344 pll->new_config = NULL;
4345 }
4346
4347 return -ENOMEM;
4348}
4349
4350static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4351{
4352 struct intel_shared_dpll *pll;
4353 enum intel_dpll_id i;
4354
4355 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4356 pll = &dev_priv->shared_dplls[i];
4357
4358 WARN_ON(pll->new_config == &pll->config);
4359
4360 pll->config = *pll->new_config;
4361 kfree(pll->new_config);
4362 pll->new_config = NULL;
4363 }
4364}
4365
4366static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4367{
4368 struct intel_shared_dpll *pll;
4369 enum intel_dpll_id i;
4370
4371 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4372 pll = &dev_priv->shared_dplls[i];
4373
4374 WARN_ON(pll->new_config == &pll->config);
4375
4376 kfree(pll->new_config);
4377 pll->new_config = NULL;
4378 }
4379}
4380
a1520318 4381static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
4382{
4383 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 4384 int dslreg = PIPEDSL(pipe);
d4270e57
JB
4385 u32 temp;
4386
4387 temp = I915_READ(dslreg);
4388 udelay(500);
4389 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 4390 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 4391 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
4392 }
4393}
4394
a1b2278e
CK
4395/**
4396 * skl_update_scaler_users - Stages update to crtc's scaler state
4397 * @intel_crtc: crtc
4398 * @crtc_state: crtc_state
4399 * @plane: plane (NULL indicates crtc is requesting update)
4400 * @plane_state: plane's state
4401 * @force_detach: request unconditional detachment of scaler
4402 *
4403 * This function updates scaler state for requested plane or crtc.
4404 * To request scaler usage update for a plane, caller shall pass plane pointer.
4405 * To request scaler usage update for crtc, caller shall pass plane pointer
4406 * as NULL.
4407 *
4408 * Return
4409 * 0 - scaler_usage updated successfully
4410 * error - requested scaling cannot be supported or other error condition
4411 */
4412int
4413skl_update_scaler_users(
4414 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4415 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4416 int force_detach)
4417{
4418 int need_scaling;
4419 int idx;
4420 int src_w, src_h, dst_w, dst_h;
4421 int *scaler_id;
4422 struct drm_framebuffer *fb;
4423 struct intel_crtc_scaler_state *scaler_state;
6156a456 4424 unsigned int rotation;
a1b2278e
CK
4425
4426 if (!intel_crtc || !crtc_state)
4427 return 0;
4428
4429 scaler_state = &crtc_state->scaler_state;
4430
4431 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4432 fb = intel_plane ? plane_state->base.fb : NULL;
4433
4434 if (intel_plane) {
4435 src_w = drm_rect_width(&plane_state->src) >> 16;
4436 src_h = drm_rect_height(&plane_state->src) >> 16;
4437 dst_w = drm_rect_width(&plane_state->dst);
4438 dst_h = drm_rect_height(&plane_state->dst);
4439 scaler_id = &plane_state->scaler_id;
6156a456 4440 rotation = plane_state->base.rotation;
a1b2278e
CK
4441 } else {
4442 struct drm_display_mode *adjusted_mode =
4443 &crtc_state->base.adjusted_mode;
4444 src_w = crtc_state->pipe_src_w;
4445 src_h = crtc_state->pipe_src_h;
4446 dst_w = adjusted_mode->hdisplay;
4447 dst_h = adjusted_mode->vdisplay;
4448 scaler_id = &scaler_state->scaler_id;
6156a456 4449 rotation = DRM_ROTATE_0;
a1b2278e 4450 }
6156a456
CK
4451
4452 need_scaling = intel_rotation_90_or_270(rotation) ?
4453 (src_h != dst_w || src_w != dst_h):
4454 (src_w != dst_w || src_h != dst_h);
a1b2278e
CK
4455
4456 /*
4457 * if plane is being disabled or scaler is no more required or force detach
4458 * - free scaler binded to this plane/crtc
4459 * - in order to do this, update crtc->scaler_usage
4460 *
4461 * Here scaler state in crtc_state is set free so that
4462 * scaler can be assigned to other user. Actual register
4463 * update to free the scaler is done in plane/panel-fit programming.
4464 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4465 */
4466 if (force_detach || !need_scaling || (intel_plane &&
4467 (!fb || !plane_state->visible))) {
4468 if (*scaler_id >= 0) {
4469 scaler_state->scaler_users &= ~(1 << idx);
4470 scaler_state->scalers[*scaler_id].in_use = 0;
4471
4472 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4473 "crtc_state = %p scaler_users = 0x%x\n",
4474 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4475 intel_plane ? intel_plane->base.base.id :
4476 intel_crtc->base.base.id, crtc_state,
4477 scaler_state->scaler_users);
4478 *scaler_id = -1;
4479 }
4480 return 0;
4481 }
4482
4483 /* range checks */
4484 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4485 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4486
4487 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4488 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4489 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4490 "size is out of scaler range\n",
4491 intel_plane ? "PLANE" : "CRTC",
4492 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4493 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4494 return -EINVAL;
4495 }
4496
4497 /* check colorkey */
4498 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4499 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4500 intel_plane->base.base.id);
4501 return -EINVAL;
4502 }
4503
4504 /* Check src format */
4505 if (intel_plane) {
4506 switch (fb->pixel_format) {
4507 case DRM_FORMAT_RGB565:
4508 case DRM_FORMAT_XBGR8888:
4509 case DRM_FORMAT_XRGB8888:
4510 case DRM_FORMAT_ABGR8888:
4511 case DRM_FORMAT_ARGB8888:
4512 case DRM_FORMAT_XRGB2101010:
a1b2278e 4513 case DRM_FORMAT_XBGR2101010:
a1b2278e
CK
4514 case DRM_FORMAT_YUYV:
4515 case DRM_FORMAT_YVYU:
4516 case DRM_FORMAT_UYVY:
4517 case DRM_FORMAT_VYUY:
4518 break;
4519 default:
4520 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4521 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4522 return -EINVAL;
4523 }
4524 }
4525
4526 /* mark this plane as a scaler user in crtc_state */
4527 scaler_state->scaler_users |= (1 << idx);
4528 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4529 "crtc_state = %p scaler_users = 0x%x\n",
4530 intel_plane ? "PLANE" : "CRTC",
4531 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4532 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4533 return 0;
4534}
4535
4536static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
bd2e244f
JB
4537{
4538 struct drm_device *dev = crtc->base.dev;
4539 struct drm_i915_private *dev_priv = dev->dev_private;
4540 int pipe = crtc->pipe;
a1b2278e
CK
4541 struct intel_crtc_scaler_state *scaler_state =
4542 &crtc->config->scaler_state;
4543
4544 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4545
4546 /* To update pfit, first update scaler state */
4547 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4548 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4549 skl_detach_scalers(crtc);
4550 if (!enable)
4551 return;
bd2e244f 4552
6e3c9717 4553 if (crtc->config->pch_pfit.enabled) {
a1b2278e
CK
4554 int id;
4555
4556 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4557 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4558 return;
4559 }
4560
4561 id = scaler_state->scaler_id;
4562 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4563 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4564 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4565 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4566
4567 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
bd2e244f
JB
4568 }
4569}
4570
b074cec8
JB
4571static void ironlake_pfit_enable(struct intel_crtc *crtc)
4572{
4573 struct drm_device *dev = crtc->base.dev;
4574 struct drm_i915_private *dev_priv = dev->dev_private;
4575 int pipe = crtc->pipe;
4576
6e3c9717 4577 if (crtc->config->pch_pfit.enabled) {
b074cec8
JB
4578 /* Force use of hard-coded filter coefficients
4579 * as some pre-programmed values are broken,
4580 * e.g. x201.
4581 */
4582 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4583 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4584 PF_PIPE_SEL_IVB(pipe));
4585 else
4586 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
6e3c9717
ACO
4587 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4588 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
d4270e57
JB
4589 }
4590}
4591
4a3b8769 4592static void intel_enable_sprite_planes(struct drm_crtc *crtc)
bb53d4ae
VS
4593{
4594 struct drm_device *dev = crtc->dev;
4595 enum pipe pipe = to_intel_crtc(crtc)->pipe;
af2b653b 4596 struct drm_plane *plane;
bb53d4ae
VS
4597 struct intel_plane *intel_plane;
4598
af2b653b
MR
4599 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4600 intel_plane = to_intel_plane(plane);
bb53d4ae
VS
4601 if (intel_plane->pipe == pipe)
4602 intel_plane_restore(&intel_plane->base);
af2b653b 4603 }
bb53d4ae
VS
4604}
4605
20bc8673 4606void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531 4607{
cea165c3
VS
4608 struct drm_device *dev = crtc->base.dev;
4609 struct drm_i915_private *dev_priv = dev->dev_private;
d77e4531 4610
6e3c9717 4611 if (!crtc->config->ips_enabled)
d77e4531
PZ
4612 return;
4613
cea165c3
VS
4614 /* We can only enable IPS after we enable a plane and wait for a vblank */
4615 intel_wait_for_vblank(dev, crtc->pipe);
4616
d77e4531 4617 assert_plane_enabled(dev_priv, crtc->plane);
cea165c3 4618 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4619 mutex_lock(&dev_priv->rps.hw_lock);
4620 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4621 mutex_unlock(&dev_priv->rps.hw_lock);
4622 /* Quoting Art Runyan: "its not safe to expect any particular
4623 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
4624 * mailbox." Moreover, the mailbox may return a bogus state,
4625 * so we need to just enable it and continue on.
2a114cc1
BW
4626 */
4627 } else {
4628 I915_WRITE(IPS_CTL, IPS_ENABLE);
4629 /* The bit only becomes 1 in the next vblank, so this wait here
4630 * is essentially intel_wait_for_vblank. If we don't have this
4631 * and don't wait for vblanks until the end of crtc_enable, then
4632 * the HW state readout code will complain that the expected
4633 * IPS_CTL value is not the one we read. */
4634 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4635 DRM_ERROR("Timed out waiting for IPS enable\n");
4636 }
d77e4531
PZ
4637}
4638
20bc8673 4639void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
4640{
4641 struct drm_device *dev = crtc->base.dev;
4642 struct drm_i915_private *dev_priv = dev->dev_private;
4643
6e3c9717 4644 if (!crtc->config->ips_enabled)
d77e4531
PZ
4645 return;
4646
4647 assert_plane_enabled(dev_priv, crtc->plane);
23d0b130 4648 if (IS_BROADWELL(dev)) {
2a114cc1
BW
4649 mutex_lock(&dev_priv->rps.hw_lock);
4650 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4651 mutex_unlock(&dev_priv->rps.hw_lock);
23d0b130
BW
4652 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4653 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4654 DRM_ERROR("Timed out waiting for IPS disable\n");
e59150dc 4655 } else {
2a114cc1 4656 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
4657 POSTING_READ(IPS_CTL);
4658 }
d77e4531
PZ
4659
4660 /* We need to wait for a vblank before we can disable the plane. */
4661 intel_wait_for_vblank(dev, crtc->pipe);
4662}
4663
4664/** Loads the palette/gamma unit for the CRTC with the prepared values */
4665static void intel_crtc_load_lut(struct drm_crtc *crtc)
4666{
4667 struct drm_device *dev = crtc->dev;
4668 struct drm_i915_private *dev_priv = dev->dev_private;
4669 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4670 enum pipe pipe = intel_crtc->pipe;
4671 int palreg = PALETTE(pipe);
4672 int i;
4673 bool reenable_ips = false;
4674
4675 /* The clocks have to be on to load the palette. */
83d65738 4676 if (!crtc->state->enable || !intel_crtc->active)
d77e4531
PZ
4677 return;
4678
50360403 4679 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
409ee761 4680 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
d77e4531
PZ
4681 assert_dsi_pll_enabled(dev_priv);
4682 else
4683 assert_pll_enabled(dev_priv, pipe);
4684 }
4685
4686 /* use legacy palette for Ironlake */
7a1db49a 4687 if (!HAS_GMCH_DISPLAY(dev))
d77e4531
PZ
4688 palreg = LGC_PALETTE(pipe);
4689
4690 /* Workaround : Do not read or write the pipe palette/gamma data while
4691 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4692 */
6e3c9717 4693 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
d77e4531
PZ
4694 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4695 GAMMA_MODE_MODE_SPLIT)) {
4696 hsw_disable_ips(intel_crtc);
4697 reenable_ips = true;
4698 }
4699
4700 for (i = 0; i < 256; i++) {
4701 I915_WRITE(palreg + 4 * i,
4702 (intel_crtc->lut_r[i] << 16) |
4703 (intel_crtc->lut_g[i] << 8) |
4704 intel_crtc->lut_b[i]);
4705 }
4706
4707 if (reenable_ips)
4708 hsw_enable_ips(intel_crtc);
4709}
4710
7cac945f 4711static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
d3eedb1a 4712{
7cac945f 4713 if (intel_crtc->overlay) {
d3eedb1a
VS
4714 struct drm_device *dev = intel_crtc->base.dev;
4715 struct drm_i915_private *dev_priv = dev->dev_private;
4716
4717 mutex_lock(&dev->struct_mutex);
4718 dev_priv->mm.interruptible = false;
4719 (void) intel_overlay_switch_off(intel_crtc->overlay);
4720 dev_priv->mm.interruptible = true;
4721 mutex_unlock(&dev->struct_mutex);
4722 }
4723
4724 /* Let userspace switch the overlay on again. In most cases userspace
4725 * has to recompute where to put it anyway.
4726 */
4727}
4728
87d4300a
ML
4729/**
4730 * intel_post_enable_primary - Perform operations after enabling primary plane
4731 * @crtc: the CRTC whose primary plane was just enabled
4732 *
4733 * Performs potentially sleeping operations that must be done after the primary
4734 * plane is enabled, such as updating FBC and IPS. Note that this may be
4735 * called due to an explicit primary plane update, or due to an implicit
4736 * re-enable that is caused when a sprite plane is updated to no longer
4737 * completely hide the primary plane.
4738 */
4739static void
4740intel_post_enable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4741{
4742 struct drm_device *dev = crtc->dev;
87d4300a 4743 struct drm_i915_private *dev_priv = dev->dev_private;
a5c4d7bc
VS
4744 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4745 int pipe = intel_crtc->pipe;
a5c4d7bc 4746
87d4300a
ML
4747 /*
4748 * BDW signals flip done immediately if the plane
4749 * is disabled, even if the plane enable is already
4750 * armed to occur at the next vblank :(
4751 */
4752 if (IS_BROADWELL(dev))
4753 intel_wait_for_vblank(dev, pipe);
a5c4d7bc 4754
87d4300a
ML
4755 /*
4756 * FIXME IPS should be fine as long as one plane is
4757 * enabled, but in practice it seems to have problems
4758 * when going from primary only to sprite only and vice
4759 * versa.
4760 */
a5c4d7bc
VS
4761 hsw_enable_ips(intel_crtc);
4762
4763 mutex_lock(&dev->struct_mutex);
7ff0ebcc 4764 intel_fbc_update(dev);
a5c4d7bc 4765 mutex_unlock(&dev->struct_mutex);
f99d7069
DV
4766
4767 /*
87d4300a
ML
4768 * Gen2 reports pipe underruns whenever all planes are disabled.
4769 * So don't enable underrun reporting before at least some planes
4770 * are enabled.
4771 * FIXME: Need to fix the logic to work when we turn off all planes
4772 * but leave the pipe running.
f99d7069 4773 */
87d4300a
ML
4774 if (IS_GEN2(dev))
4775 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4776
4777 /* Underruns don't raise interrupts, so check manually. */
4778 if (HAS_GMCH_DISPLAY(dev))
4779 i9xx_check_fifo_underruns(dev_priv);
a5c4d7bc
VS
4780}
4781
87d4300a
ML
4782/**
4783 * intel_pre_disable_primary - Perform operations before disabling primary plane
4784 * @crtc: the CRTC whose primary plane is to be disabled
4785 *
4786 * Performs potentially sleeping operations that must be done before the
4787 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4788 * be called due to an explicit primary plane update, or due to an implicit
4789 * disable that is caused when a sprite plane completely hides the primary
4790 * plane.
4791 */
4792static void
4793intel_pre_disable_primary(struct drm_crtc *crtc)
a5c4d7bc
VS
4794{
4795 struct drm_device *dev = crtc->dev;
4796 struct drm_i915_private *dev_priv = dev->dev_private;
4797 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4798 int pipe = intel_crtc->pipe;
a5c4d7bc 4799
87d4300a
ML
4800 /*
4801 * Gen2 reports pipe underruns whenever all planes are disabled.
4802 * So diasble underrun reporting before all the planes get disabled.
4803 * FIXME: Need to fix the logic to work when we turn off all planes
4804 * but leave the pipe running.
4805 */
4806 if (IS_GEN2(dev))
4807 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
a5c4d7bc 4808
87d4300a
ML
4809 /*
4810 * Vblank time updates from the shadow to live plane control register
4811 * are blocked if the memory self-refresh mode is active at that
4812 * moment. So to make sure the plane gets truly disabled, disable
4813 * first the self-refresh mode. The self-refresh enable bit in turn
4814 * will be checked/applied by the HW only at the next frame start
4815 * event which is after the vblank start event, so we need to have a
4816 * wait-for-vblank between disabling the plane and the pipe.
4817 */
4818 if (HAS_GMCH_DISPLAY(dev))
4819 intel_set_memory_cxsr(dev_priv, false);
4820
4821 mutex_lock(&dev->struct_mutex);
e35fef21 4822 if (dev_priv->fbc.crtc == intel_crtc)
7ff0ebcc 4823 intel_fbc_disable(dev);
87d4300a 4824 mutex_unlock(&dev->struct_mutex);
a5c4d7bc 4825
87d4300a
ML
4826 /*
4827 * FIXME IPS should be fine as long as one plane is
4828 * enabled, but in practice it seems to have problems
4829 * when going from primary only to sprite only and vice
4830 * versa.
4831 */
a5c4d7bc 4832 hsw_disable_ips(intel_crtc);
87d4300a
ML
4833}
4834
4835static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4836{
87d4300a
ML
4837 intel_enable_primary_hw_plane(crtc->primary, crtc);
4838 intel_enable_sprite_planes(crtc);
4839 intel_crtc_update_cursor(crtc, true);
87d4300a
ML
4840
4841 intel_post_enable_primary(crtc);
4842}
4843
4844static void intel_crtc_disable_planes(struct drm_crtc *crtc)
4845{
4846 struct drm_device *dev = crtc->dev;
4847 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4848 struct intel_plane *intel_plane;
4849 int pipe = intel_crtc->pipe;
4850
4851 intel_crtc_wait_for_pending_flips(crtc);
4852
4853 intel_pre_disable_primary(crtc);
a5c4d7bc 4854
7cac945f 4855 intel_crtc_dpms_overlay_disable(intel_crtc);
27321ae8
ML
4856 for_each_intel_plane(dev, intel_plane) {
4857 if (intel_plane->pipe == pipe) {
4858 struct drm_crtc *from = intel_plane->base.crtc;
4859
4860 intel_plane->disable_plane(&intel_plane->base,
4861 from ?: crtc, true);
4862 }
4863 }
f98551ae 4864
f99d7069
DV
4865 /*
4866 * FIXME: Once we grow proper nuclear flip support out of this we need
4867 * to compute the mask of flip planes precisely. For the time being
4868 * consider this a flip to a NULL plane.
4869 */
4870 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
a5c4d7bc
VS
4871}
4872
f67a559d
JB
4873static void ironlake_crtc_enable(struct drm_crtc *crtc)
4874{
4875 struct drm_device *dev = crtc->dev;
4876 struct drm_i915_private *dev_priv = dev->dev_private;
4877 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4878 struct intel_encoder *encoder;
f67a559d 4879 int pipe = intel_crtc->pipe;
f67a559d 4880
83d65738 4881 WARN_ON(!crtc->state->enable);
08a48469 4882
f67a559d
JB
4883 if (intel_crtc->active)
4884 return;
4885
6e3c9717 4886 if (intel_crtc->config->has_pch_encoder)
b14b1055
DV
4887 intel_prepare_shared_dpll(intel_crtc);
4888
6e3c9717 4889 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4890 intel_dp_set_m_n(intel_crtc, M1_N1);
29407aab
DV
4891
4892 intel_set_pipe_timings(intel_crtc);
4893
6e3c9717 4894 if (intel_crtc->config->has_pch_encoder) {
29407aab 4895 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 4896 &intel_crtc->config->fdi_m_n, NULL);
29407aab
DV
4897 }
4898
4899 ironlake_set_pipeconf(crtc);
4900
f67a559d 4901 intel_crtc->active = true;
8664281b 4902
a72e4c9f
DV
4903 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4904 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
8664281b 4905
f6736a1a 4906 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
4907 if (encoder->pre_enable)
4908 encoder->pre_enable(encoder);
f67a559d 4909
6e3c9717 4910 if (intel_crtc->config->has_pch_encoder) {
fff367c7
DV
4911 /* Note: FDI PLL enabling _must_ be done before we enable the
4912 * cpu pipes, hence this is separate from all the other fdi/pch
4913 * enabling. */
88cefb6c 4914 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
4915 } else {
4916 assert_fdi_tx_disabled(dev_priv, pipe);
4917 assert_fdi_rx_disabled(dev_priv, pipe);
4918 }
f67a559d 4919
b074cec8 4920 ironlake_pfit_enable(intel_crtc);
f67a559d 4921
9c54c0dd
JB
4922 /*
4923 * On ILK+ LUT must be loaded before the pipe is running but with
4924 * clocks enabled
4925 */
4926 intel_crtc_load_lut(crtc);
4927
f37fcc2a 4928 intel_update_watermarks(crtc);
e1fdc473 4929 intel_enable_pipe(intel_crtc);
f67a559d 4930
6e3c9717 4931 if (intel_crtc->config->has_pch_encoder)
f67a559d 4932 ironlake_pch_enable(crtc);
c98e9dcf 4933
f9b61ff6
DV
4934 assert_vblank_disabled(crtc);
4935 drm_crtc_vblank_on(crtc);
4936
fa5c73b1
DV
4937 for_each_encoder_on_crtc(dev, crtc, encoder)
4938 encoder->enable(encoder);
61b77ddd
DV
4939
4940 if (HAS_PCH_CPT(dev))
a1520318 4941 cpt_verify_modeset(dev, intel_crtc->pipe);
6be4a607
JB
4942}
4943
42db64ef
PZ
4944/* IPS only exists on ULT machines and is tied to pipe A. */
4945static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4946{
f5adf94e 4947 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
4948}
4949
e4916946
PZ
4950/*
4951 * This implements the workaround described in the "notes" section of the mode
4952 * set sequence documentation. When going from no pipes or single pipe to
4953 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4954 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4955 */
4956static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4957{
4958 struct drm_device *dev = crtc->base.dev;
4959 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4960
4961 /* We want to get the other_active_crtc only if there's only 1 other
4962 * active crtc. */
d3fcc808 4963 for_each_intel_crtc(dev, crtc_it) {
e4916946
PZ
4964 if (!crtc_it->active || crtc_it == crtc)
4965 continue;
4966
4967 if (other_active_crtc)
4968 return;
4969
4970 other_active_crtc = crtc_it;
4971 }
4972 if (!other_active_crtc)
4973 return;
4974
4975 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4976 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4977}
4978
4f771f10
PZ
4979static void haswell_crtc_enable(struct drm_crtc *crtc)
4980{
4981 struct drm_device *dev = crtc->dev;
4982 struct drm_i915_private *dev_priv = dev->dev_private;
4983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4984 struct intel_encoder *encoder;
4985 int pipe = intel_crtc->pipe;
4f771f10 4986
83d65738 4987 WARN_ON(!crtc->state->enable);
4f771f10
PZ
4988
4989 if (intel_crtc->active)
4990 return;
4991
df8ad70c
DV
4992 if (intel_crtc_to_shared_dpll(intel_crtc))
4993 intel_enable_shared_dpll(intel_crtc);
4994
6e3c9717 4995 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 4996 intel_dp_set_m_n(intel_crtc, M1_N1);
229fca97
DV
4997
4998 intel_set_pipe_timings(intel_crtc);
4999
6e3c9717
ACO
5000 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5001 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5002 intel_crtc->config->pixel_multiplier - 1);
ebb69c95
CT
5003 }
5004
6e3c9717 5005 if (intel_crtc->config->has_pch_encoder) {
229fca97 5006 intel_cpu_transcoder_set_m_n(intel_crtc,
6e3c9717 5007 &intel_crtc->config->fdi_m_n, NULL);
229fca97
DV
5008 }
5009
5010 haswell_set_pipeconf(crtc);
5011
5012 intel_set_pipe_csc(crtc);
5013
4f771f10 5014 intel_crtc->active = true;
8664281b 5015
a72e4c9f 5016 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4f771f10
PZ
5017 for_each_encoder_on_crtc(dev, crtc, encoder)
5018 if (encoder->pre_enable)
5019 encoder->pre_enable(encoder);
5020
6e3c9717 5021 if (intel_crtc->config->has_pch_encoder) {
a72e4c9f
DV
5022 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5023 true);
4fe9467d
ID
5024 dev_priv->display.fdi_link_train(crtc);
5025 }
5026
1f544388 5027 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 5028
ff6d9f55 5029 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5030 skylake_pfit_update(intel_crtc, 1);
ff6d9f55 5031 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5032 ironlake_pfit_enable(intel_crtc);
ff6d9f55
JB
5033 else
5034 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10
PZ
5035
5036 /*
5037 * On ILK+ LUT must be loaded before the pipe is running but with
5038 * clocks enabled
5039 */
5040 intel_crtc_load_lut(crtc);
5041
1f544388 5042 intel_ddi_set_pipe_settings(crtc);
8228c251 5043 intel_ddi_enable_transcoder_func(crtc);
4f771f10 5044
f37fcc2a 5045 intel_update_watermarks(crtc);
e1fdc473 5046 intel_enable_pipe(intel_crtc);
42db64ef 5047
6e3c9717 5048 if (intel_crtc->config->has_pch_encoder)
1507e5bd 5049 lpt_pch_enable(crtc);
4f771f10 5050
6e3c9717 5051 if (intel_crtc->config->dp_encoder_is_mst)
0e32b39c
DA
5052 intel_ddi_set_vc_payload_alloc(crtc, true);
5053
f9b61ff6
DV
5054 assert_vblank_disabled(crtc);
5055 drm_crtc_vblank_on(crtc);
5056
8807e55b 5057 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 5058 encoder->enable(encoder);
8807e55b
JN
5059 intel_opregion_notify_encoder(encoder, true);
5060 }
4f771f10 5061
e4916946
PZ
5062 /* If we change the relative order between pipe/planes enabling, we need
5063 * to change the workaround. */
5064 haswell_mode_set_planes_workaround(intel_crtc);
4f771f10
PZ
5065}
5066
3f8dce3a
DV
5067static void ironlake_pfit_disable(struct intel_crtc *crtc)
5068{
5069 struct drm_device *dev = crtc->base.dev;
5070 struct drm_i915_private *dev_priv = dev->dev_private;
5071 int pipe = crtc->pipe;
5072
5073 /* To avoid upsetting the power well on haswell only disable the pfit if
5074 * it's in use. The hw state code will make sure we get this right. */
6e3c9717 5075 if (crtc->config->pch_pfit.enabled) {
3f8dce3a
DV
5076 I915_WRITE(PF_CTL(pipe), 0);
5077 I915_WRITE(PF_WIN_POS(pipe), 0);
5078 I915_WRITE(PF_WIN_SZ(pipe), 0);
5079 }
5080}
5081
6be4a607
JB
5082static void ironlake_crtc_disable(struct drm_crtc *crtc)
5083{
5084 struct drm_device *dev = crtc->dev;
5085 struct drm_i915_private *dev_priv = dev->dev_private;
5086 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5087 struct intel_encoder *encoder;
6be4a607 5088 int pipe = intel_crtc->pipe;
5eddb70b 5089 u32 reg, temp;
b52eb4dc 5090
f7abfe8b
CW
5091 if (!intel_crtc->active)
5092 return;
5093
ea9d758d
DV
5094 for_each_encoder_on_crtc(dev, crtc, encoder)
5095 encoder->disable(encoder);
5096
f9b61ff6
DV
5097 drm_crtc_vblank_off(crtc);
5098 assert_vblank_disabled(crtc);
5099
6e3c9717 5100 if (intel_crtc->config->has_pch_encoder)
a72e4c9f 5101 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
d925c59a 5102
575f7ab7 5103 intel_disable_pipe(intel_crtc);
32f9d658 5104
3f8dce3a 5105 ironlake_pfit_disable(intel_crtc);
2c07245f 5106
bf49ec8c
DV
5107 for_each_encoder_on_crtc(dev, crtc, encoder)
5108 if (encoder->post_disable)
5109 encoder->post_disable(encoder);
2c07245f 5110
6e3c9717 5111 if (intel_crtc->config->has_pch_encoder) {
d925c59a 5112 ironlake_fdi_disable(crtc);
913d8d11 5113
d925c59a 5114 ironlake_disable_pch_transcoder(dev_priv, pipe);
6be4a607 5115
d925c59a
DV
5116 if (HAS_PCH_CPT(dev)) {
5117 /* disable TRANS_DP_CTL */
5118 reg = TRANS_DP_CTL(pipe);
5119 temp = I915_READ(reg);
5120 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5121 TRANS_DP_PORT_SEL_MASK);
5122 temp |= TRANS_DP_PORT_SEL_NONE;
5123 I915_WRITE(reg, temp);
5124
5125 /* disable DPLL_SEL */
5126 temp = I915_READ(PCH_DPLL_SEL);
11887397 5127 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 5128 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 5129 }
e3421a18 5130
d925c59a 5131 /* disable PCH DPLL */
e72f9fbf 5132 intel_disable_shared_dpll(intel_crtc);
8db9d77b 5133
d925c59a
DV
5134 ironlake_fdi_pll_disable(intel_crtc);
5135 }
6b383a7f 5136
f7abfe8b 5137 intel_crtc->active = false;
46ba614c 5138 intel_update_watermarks(crtc);
d1ebd816
BW
5139
5140 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5141 intel_fbc_update(dev);
d1ebd816 5142 mutex_unlock(&dev->struct_mutex);
6be4a607 5143}
1b3c7a47 5144
4f771f10 5145static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 5146{
4f771f10
PZ
5147 struct drm_device *dev = crtc->dev;
5148 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 5149 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10 5150 struct intel_encoder *encoder;
6e3c9717 5151 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee7b9f93 5152
4f771f10
PZ
5153 if (!intel_crtc->active)
5154 return;
5155
8807e55b
JN
5156 for_each_encoder_on_crtc(dev, crtc, encoder) {
5157 intel_opregion_notify_encoder(encoder, false);
4f771f10 5158 encoder->disable(encoder);
8807e55b 5159 }
4f771f10 5160
f9b61ff6
DV
5161 drm_crtc_vblank_off(crtc);
5162 assert_vblank_disabled(crtc);
5163
6e3c9717 5164 if (intel_crtc->config->has_pch_encoder)
a72e4c9f
DV
5165 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5166 false);
575f7ab7 5167 intel_disable_pipe(intel_crtc);
4f771f10 5168
6e3c9717 5169 if (intel_crtc->config->dp_encoder_is_mst)
a4bf214f
VS
5170 intel_ddi_set_vc_payload_alloc(crtc, false);
5171
ad80a810 5172 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 5173
ff6d9f55 5174 if (INTEL_INFO(dev)->gen == 9)
a1b2278e 5175 skylake_pfit_update(intel_crtc, 0);
ff6d9f55 5176 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 5177 ironlake_pfit_disable(intel_crtc);
ff6d9f55
JB
5178 else
5179 MISSING_CASE(INTEL_INFO(dev)->gen);
4f771f10 5180
1f544388 5181 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10 5182
6e3c9717 5183 if (intel_crtc->config->has_pch_encoder) {
ab4d966c 5184 lpt_disable_pch_transcoder(dev_priv);
1ad960f2 5185 intel_ddi_fdi_disable(crtc);
83616634 5186 }
4f771f10 5187
97b040aa
ID
5188 for_each_encoder_on_crtc(dev, crtc, encoder)
5189 if (encoder->post_disable)
5190 encoder->post_disable(encoder);
5191
4f771f10 5192 intel_crtc->active = false;
46ba614c 5193 intel_update_watermarks(crtc);
4f771f10
PZ
5194
5195 mutex_lock(&dev->struct_mutex);
7ff0ebcc 5196 intel_fbc_update(dev);
4f771f10 5197 mutex_unlock(&dev->struct_mutex);
df8ad70c
DV
5198
5199 if (intel_crtc_to_shared_dpll(intel_crtc))
5200 intel_disable_shared_dpll(intel_crtc);
4f771f10
PZ
5201}
5202
ee7b9f93
JB
5203static void ironlake_crtc_off(struct drm_crtc *crtc)
5204{
5205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 5206 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
5207}
5208
6441ab5f 5209
2dd24552
JB
5210static void i9xx_pfit_enable(struct intel_crtc *crtc)
5211{
5212 struct drm_device *dev = crtc->base.dev;
5213 struct drm_i915_private *dev_priv = dev->dev_private;
6e3c9717 5214 struct intel_crtc_state *pipe_config = crtc->config;
2dd24552 5215
681a8504 5216 if (!pipe_config->gmch_pfit.control)
2dd24552
JB
5217 return;
5218
2dd24552 5219 /*
c0b03411
DV
5220 * The panel fitter should only be adjusted whilst the pipe is disabled,
5221 * according to register description and PRM.
2dd24552 5222 */
c0b03411
DV
5223 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5224 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 5225
b074cec8
JB
5226 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5227 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
5228
5229 /* Border color in case we don't scale up to the full screen. Black by
5230 * default, change to something else for debugging. */
5231 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
5232}
5233
d05410f9
DA
5234static enum intel_display_power_domain port_to_power_domain(enum port port)
5235{
5236 switch (port) {
5237 case PORT_A:
5238 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5239 case PORT_B:
5240 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5241 case PORT_C:
5242 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5243 case PORT_D:
5244 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5245 default:
5246 WARN_ON_ONCE(1);
5247 return POWER_DOMAIN_PORT_OTHER;
5248 }
5249}
5250
77d22dca
ID
5251#define for_each_power_domain(domain, mask) \
5252 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5253 if ((1 << (domain)) & (mask))
5254
319be8ae
ID
5255enum intel_display_power_domain
5256intel_display_port_power_domain(struct intel_encoder *intel_encoder)
5257{
5258 struct drm_device *dev = intel_encoder->base.dev;
5259 struct intel_digital_port *intel_dig_port;
5260
5261 switch (intel_encoder->type) {
5262 case INTEL_OUTPUT_UNKNOWN:
5263 /* Only DDI platforms should ever use this output type */
5264 WARN_ON_ONCE(!HAS_DDI(dev));
5265 case INTEL_OUTPUT_DISPLAYPORT:
5266 case INTEL_OUTPUT_HDMI:
5267 case INTEL_OUTPUT_EDP:
5268 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
d05410f9 5269 return port_to_power_domain(intel_dig_port->port);
0e32b39c
DA
5270 case INTEL_OUTPUT_DP_MST:
5271 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5272 return port_to_power_domain(intel_dig_port->port);
319be8ae
ID
5273 case INTEL_OUTPUT_ANALOG:
5274 return POWER_DOMAIN_PORT_CRT;
5275 case INTEL_OUTPUT_DSI:
5276 return POWER_DOMAIN_PORT_DSI;
5277 default:
5278 return POWER_DOMAIN_PORT_OTHER;
5279 }
5280}
5281
5282static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 5283{
319be8ae
ID
5284 struct drm_device *dev = crtc->dev;
5285 struct intel_encoder *intel_encoder;
5286 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5287 enum pipe pipe = intel_crtc->pipe;
77d22dca
ID
5288 unsigned long mask;
5289 enum transcoder transcoder;
5290
5291 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5292
5293 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5294 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
6e3c9717
ACO
5295 if (intel_crtc->config->pch_pfit.enabled ||
5296 intel_crtc->config->pch_pfit.force_thru)
77d22dca
ID
5297 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5298
319be8ae
ID
5299 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5300 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5301
77d22dca
ID
5302 return mask;
5303}
5304
679dacd4 5305static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
77d22dca 5306{
679dacd4 5307 struct drm_device *dev = state->dev;
77d22dca
ID
5308 struct drm_i915_private *dev_priv = dev->dev_private;
5309 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5310 struct intel_crtc *crtc;
5311
5312 /*
5313 * First get all needed power domains, then put all unneeded, to avoid
5314 * any unnecessary toggling of the power wells.
5315 */
d3fcc808 5316 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5317 enum intel_display_power_domain domain;
5318
83d65738 5319 if (!crtc->base.state->enable)
77d22dca
ID
5320 continue;
5321
319be8ae 5322 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
5323
5324 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5325 intel_display_power_get(dev_priv, domain);
5326 }
5327
50f6e502 5328 if (dev_priv->display.modeset_global_resources)
679dacd4 5329 dev_priv->display.modeset_global_resources(state);
50f6e502 5330
d3fcc808 5331 for_each_intel_crtc(dev, crtc) {
77d22dca
ID
5332 enum intel_display_power_domain domain;
5333
5334 for_each_power_domain(domain, crtc->enabled_power_domains)
5335 intel_display_power_put(dev_priv, domain);
5336
5337 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5338 }
5339
5340 intel_display_set_init_power(dev_priv, false);
5341}
5342
f8437dd1
VK
5343void broxton_set_cdclk(struct drm_device *dev, int frequency)
5344{
5345 struct drm_i915_private *dev_priv = dev->dev_private;
5346 uint32_t divider;
5347 uint32_t ratio;
5348 uint32_t current_freq;
5349 int ret;
5350
5351 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5352 switch (frequency) {
5353 case 144000:
5354 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5355 ratio = BXT_DE_PLL_RATIO(60);
5356 break;
5357 case 288000:
5358 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5359 ratio = BXT_DE_PLL_RATIO(60);
5360 break;
5361 case 384000:
5362 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5363 ratio = BXT_DE_PLL_RATIO(60);
5364 break;
5365 case 576000:
5366 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5367 ratio = BXT_DE_PLL_RATIO(60);
5368 break;
5369 case 624000:
5370 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5371 ratio = BXT_DE_PLL_RATIO(65);
5372 break;
5373 case 19200:
5374 /*
5375 * Bypass frequency with DE PLL disabled. Init ratio, divider
5376 * to suppress GCC warning.
5377 */
5378 ratio = 0;
5379 divider = 0;
5380 break;
5381 default:
5382 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5383
5384 return;
5385 }
5386
5387 mutex_lock(&dev_priv->rps.hw_lock);
5388 /* Inform power controller of upcoming frequency change */
5389 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5390 0x80000000);
5391 mutex_unlock(&dev_priv->rps.hw_lock);
5392
5393 if (ret) {
5394 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5395 ret, frequency);
5396 return;
5397 }
5398
5399 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5400 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5401 current_freq = current_freq * 500 + 1000;
5402
5403 /*
5404 * DE PLL has to be disabled when
5405 * - setting to 19.2MHz (bypass, PLL isn't used)
5406 * - before setting to 624MHz (PLL needs toggling)
5407 * - before setting to any frequency from 624MHz (PLL needs toggling)
5408 */
5409 if (frequency == 19200 || frequency == 624000 ||
5410 current_freq == 624000) {
5411 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5412 /* Timeout 200us */
5413 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5414 1))
5415 DRM_ERROR("timout waiting for DE PLL unlock\n");
5416 }
5417
5418 if (frequency != 19200) {
5419 uint32_t val;
5420
5421 val = I915_READ(BXT_DE_PLL_CTL);
5422 val &= ~BXT_DE_PLL_RATIO_MASK;
5423 val |= ratio;
5424 I915_WRITE(BXT_DE_PLL_CTL, val);
5425
5426 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5427 /* Timeout 200us */
5428 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5429 DRM_ERROR("timeout waiting for DE PLL lock\n");
5430
5431 val = I915_READ(CDCLK_CTL);
5432 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5433 val |= divider;
5434 /*
5435 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5436 * enable otherwise.
5437 */
5438 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5439 if (frequency >= 500000)
5440 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5441
5442 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5443 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5444 val |= (frequency - 1000) / 500;
5445 I915_WRITE(CDCLK_CTL, val);
5446 }
5447
5448 mutex_lock(&dev_priv->rps.hw_lock);
5449 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5450 DIV_ROUND_UP(frequency, 25000));
5451 mutex_unlock(&dev_priv->rps.hw_lock);
5452
5453 if (ret) {
5454 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5455 ret, frequency);
5456 return;
5457 }
5458
5459 dev_priv->cdclk_freq = frequency;
5460}
5461
5462void broxton_init_cdclk(struct drm_device *dev)
5463{
5464 struct drm_i915_private *dev_priv = dev->dev_private;
5465 uint32_t val;
5466
5467 /*
5468 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5469 * or else the reset will hang because there is no PCH to respond.
5470 * Move the handshake programming to initialization sequence.
5471 * Previously was left up to BIOS.
5472 */
5473 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5474 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5475 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5476
5477 /* Enable PG1 for cdclk */
5478 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5479
5480 /* check if cd clock is enabled */
5481 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5482 DRM_DEBUG_KMS("Display already initialized\n");
5483 return;
5484 }
5485
5486 /*
5487 * FIXME:
5488 * - The initial CDCLK needs to be read from VBT.
5489 * Need to make this change after VBT has changes for BXT.
5490 * - check if setting the max (or any) cdclk freq is really necessary
5491 * here, it belongs to modeset time
5492 */
5493 broxton_set_cdclk(dev, 624000);
5494
5495 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
22e02c0b
VS
5496 POSTING_READ(DBUF_CTL);
5497
f8437dd1
VK
5498 udelay(10);
5499
5500 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5501 DRM_ERROR("DBuf power enable timeout!\n");
5502}
5503
5504void broxton_uninit_cdclk(struct drm_device *dev)
5505{
5506 struct drm_i915_private *dev_priv = dev->dev_private;
5507
5508 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
22e02c0b
VS
5509 POSTING_READ(DBUF_CTL);
5510
f8437dd1
VK
5511 udelay(10);
5512
5513 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5514 DRM_ERROR("DBuf power disable timeout!\n");
5515
5516 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5517 broxton_set_cdclk(dev, 19200);
5518
5519 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5520}
5521
dfcab17e 5522/* returns HPLL frequency in kHz */
f8bf63fd 5523static int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 5524{
586f49dc 5525 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 5526
586f49dc
JB
5527 /* Obtain SKU information */
5528 mutex_lock(&dev_priv->dpio_lock);
5529 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5530 CCK_FUSE_HPLL_FREQ_MASK;
5531 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 5532
dfcab17e 5533 return vco_freq[hpll_freq] * 1000;
30a970c6
JB
5534}
5535
f8bf63fd
VS
5536static void vlv_update_cdclk(struct drm_device *dev)
5537{
5538 struct drm_i915_private *dev_priv = dev->dev_private;
5539
164dfd28 5540 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
43dc52c3 5541 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
164dfd28 5542 dev_priv->cdclk_freq);
f8bf63fd
VS
5543
5544 /*
5545 * Program the gmbus_freq based on the cdclk frequency.
5546 * BSpec erroneously claims we should aim for 4MHz, but
5547 * in fact 1MHz is the correct frequency.
5548 */
164dfd28 5549 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
f8bf63fd
VS
5550}
5551
30a970c6
JB
5552/* Adjust CDclk dividers to allow high res or save power if possible */
5553static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5554{
5555 struct drm_i915_private *dev_priv = dev->dev_private;
5556 u32 val, cmd;
5557
164dfd28
VK
5558 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5559 != dev_priv->cdclk_freq);
d60c4473 5560
dfcab17e 5561 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
30a970c6 5562 cmd = 2;
dfcab17e 5563 else if (cdclk == 266667)
30a970c6
JB
5564 cmd = 1;
5565 else
5566 cmd = 0;
5567
5568 mutex_lock(&dev_priv->rps.hw_lock);
5569 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5570 val &= ~DSPFREQGUAR_MASK;
5571 val |= (cmd << DSPFREQGUAR_SHIFT);
5572 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5573 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5574 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5575 50)) {
5576 DRM_ERROR("timed out waiting for CDclk change\n");
5577 }
5578 mutex_unlock(&dev_priv->rps.hw_lock);
5579
dfcab17e 5580 if (cdclk == 400000) {
6bcda4f0 5581 u32 divider;
30a970c6 5582
6bcda4f0 5583 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
30a970c6
JB
5584
5585 mutex_lock(&dev_priv->dpio_lock);
5586 /* adjust cdclk divider */
5587 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
9cf33db5 5588 val &= ~DISPLAY_FREQUENCY_VALUES;
30a970c6
JB
5589 val |= divider;
5590 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
a877e801
VS
5591
5592 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5593 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5594 50))
5595 DRM_ERROR("timed out waiting for CDclk change\n");
30a970c6
JB
5596 mutex_unlock(&dev_priv->dpio_lock);
5597 }
5598
5599 mutex_lock(&dev_priv->dpio_lock);
5600 /* adjust self-refresh exit latency value */
5601 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5602 val &= ~0x7f;
5603
5604 /*
5605 * For high bandwidth configs, we set a higher latency in the bunit
5606 * so that the core display fetch happens in time to avoid underruns.
5607 */
dfcab17e 5608 if (cdclk == 400000)
30a970c6
JB
5609 val |= 4500 / 250; /* 4.5 usec */
5610 else
5611 val |= 3000 / 250; /* 3.0 usec */
5612 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5613 mutex_unlock(&dev_priv->dpio_lock);
5614
f8bf63fd 5615 vlv_update_cdclk(dev);
30a970c6
JB
5616}
5617
383c5a6a
VS
5618static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5619{
5620 struct drm_i915_private *dev_priv = dev->dev_private;
5621 u32 val, cmd;
5622
164dfd28
VK
5623 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5624 != dev_priv->cdclk_freq);
383c5a6a
VS
5625
5626 switch (cdclk) {
383c5a6a
VS
5627 case 333333:
5628 case 320000:
383c5a6a 5629 case 266667:
383c5a6a 5630 case 200000:
383c5a6a
VS
5631 break;
5632 default:
5f77eeb0 5633 MISSING_CASE(cdclk);
383c5a6a
VS
5634 return;
5635 }
5636
9d0d3fda
VS
5637 /*
5638 * Specs are full of misinformation, but testing on actual
5639 * hardware has shown that we just need to write the desired
5640 * CCK divider into the Punit register.
5641 */
5642 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5643
383c5a6a
VS
5644 mutex_lock(&dev_priv->rps.hw_lock);
5645 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5646 val &= ~DSPFREQGUAR_MASK_CHV;
5647 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5648 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5649 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5650 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5651 50)) {
5652 DRM_ERROR("timed out waiting for CDclk change\n");
5653 }
5654 mutex_unlock(&dev_priv->rps.hw_lock);
5655
5656 vlv_update_cdclk(dev);
5657}
5658
30a970c6
JB
5659static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5660 int max_pixclk)
5661{
6bcda4f0 5662 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
6cca3195 5663 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
29dc7ef3 5664
30a970c6
JB
5665 /*
5666 * Really only a few cases to deal with, as only 4 CDclks are supported:
5667 * 200MHz
5668 * 267MHz
29dc7ef3 5669 * 320/333MHz (depends on HPLL freq)
6cca3195
VS
5670 * 400MHz (VLV only)
5671 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5672 * of the lower bin and adjust if needed.
e37c67a1
VS
5673 *
5674 * We seem to get an unstable or solid color picture at 200MHz.
5675 * Not sure what's wrong. For now use 200MHz only when all pipes
5676 * are off.
30a970c6 5677 */
6cca3195
VS
5678 if (!IS_CHERRYVIEW(dev_priv) &&
5679 max_pixclk > freq_320*limit/100)
dfcab17e 5680 return 400000;
6cca3195 5681 else if (max_pixclk > 266667*limit/100)
29dc7ef3 5682 return freq_320;
e37c67a1 5683 else if (max_pixclk > 0)
dfcab17e 5684 return 266667;
e37c67a1
VS
5685 else
5686 return 200000;
30a970c6
JB
5687}
5688
f8437dd1
VK
5689static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5690 int max_pixclk)
5691{
5692 /*
5693 * FIXME:
5694 * - remove the guardband, it's not needed on BXT
5695 * - set 19.2MHz bypass frequency if there are no active pipes
5696 */
5697 if (max_pixclk > 576000*9/10)
5698 return 624000;
5699 else if (max_pixclk > 384000*9/10)
5700 return 576000;
5701 else if (max_pixclk > 288000*9/10)
5702 return 384000;
5703 else if (max_pixclk > 144000*9/10)
5704 return 288000;
5705 else
5706 return 144000;
5707}
5708
a821fc46
ACO
5709/* Compute the max pixel clock for new configuration. Uses atomic state if
5710 * that's non-NULL, look at current state otherwise. */
5711static int intel_mode_max_pixclk(struct drm_device *dev,
5712 struct drm_atomic_state *state)
30a970c6 5713{
30a970c6 5714 struct intel_crtc *intel_crtc;
304603f4 5715 struct intel_crtc_state *crtc_state;
30a970c6
JB
5716 int max_pixclk = 0;
5717
d3fcc808 5718 for_each_intel_crtc(dev, intel_crtc) {
a821fc46
ACO
5719 if (state)
5720 crtc_state =
5721 intel_atomic_get_crtc_state(state, intel_crtc);
5722 else
5723 crtc_state = intel_crtc->config;
304603f4
ACO
5724 if (IS_ERR(crtc_state))
5725 return PTR_ERR(crtc_state);
5726
5727 if (!crtc_state->base.enable)
5728 continue;
5729
5730 max_pixclk = max(max_pixclk,
5731 crtc_state->base.adjusted_mode.crtc_clock);
30a970c6
JB
5732 }
5733
5734 return max_pixclk;
5735}
5736
0a9ab303 5737static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
30a970c6 5738{
304603f4 5739 struct drm_i915_private *dev_priv = to_i915(state->dev);
0a9ab303
ACO
5740 struct drm_crtc *crtc;
5741 struct drm_crtc_state *crtc_state;
a821fc46 5742 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
0a9ab303 5743 int cdclk, i;
30a970c6 5744
304603f4
ACO
5745 if (max_pixclk < 0)
5746 return max_pixclk;
30a970c6 5747
f8437dd1
VK
5748 if (IS_VALLEYVIEW(dev_priv))
5749 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5750 else
5751 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5752
5753 if (cdclk == dev_priv->cdclk_freq)
304603f4 5754 return 0;
30a970c6 5755
0a9ab303
ACO
5756 /* add all active pipes to the state */
5757 for_each_crtc(state->dev, crtc) {
5758 if (!crtc->state->enable)
5759 continue;
5760
5761 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5762 if (IS_ERR(crtc_state))
5763 return PTR_ERR(crtc_state);
5764 }
5765
2f2d7aa1 5766 /* disable/enable all currently active pipes while we change cdclk */
0a9ab303
ACO
5767 for_each_crtc_in_state(state, crtc, crtc_state, i)
5768 if (crtc_state->enable)
5769 crtc_state->mode_changed = true;
304603f4
ACO
5770
5771 return 0;
30a970c6
JB
5772}
5773
1e69cd74
VS
5774static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5775{
5776 unsigned int credits, default_credits;
5777
5778 if (IS_CHERRYVIEW(dev_priv))
5779 default_credits = PFI_CREDIT(12);
5780 else
5781 default_credits = PFI_CREDIT(8);
5782
164dfd28 5783 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
1e69cd74
VS
5784 /* CHV suggested value is 31 or 63 */
5785 if (IS_CHERRYVIEW(dev_priv))
5786 credits = PFI_CREDIT_31;
5787 else
5788 credits = PFI_CREDIT(15);
5789 } else {
5790 credits = default_credits;
5791 }
5792
5793 /*
5794 * WA - write default credits before re-programming
5795 * FIXME: should we also set the resend bit here?
5796 */
5797 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5798 default_credits);
5799
5800 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5801 credits | PFI_CREDIT_RESEND);
5802
5803 /*
5804 * FIXME is this guaranteed to clear
5805 * immediately or should we poll for it?
5806 */
5807 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5808}
5809
a821fc46 5810static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
30a970c6 5811{
a821fc46 5812 struct drm_device *dev = old_state->dev;
30a970c6 5813 struct drm_i915_private *dev_priv = dev->dev_private;
a821fc46 5814 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
304603f4
ACO
5815 int req_cdclk;
5816
a821fc46
ACO
5817 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
5818 * never fail. */
304603f4
ACO
5819 if (WARN_ON(max_pixclk < 0))
5820 return;
30a970c6 5821
304603f4 5822 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
30a970c6 5823
164dfd28 5824 if (req_cdclk != dev_priv->cdclk_freq) {
738c05c0
ID
5825 /*
5826 * FIXME: We can end up here with all power domains off, yet
5827 * with a CDCLK frequency other than the minimum. To account
5828 * for this take the PIPE-A power domain, which covers the HW
5829 * blocks needed for the following programming. This can be
5830 * removed once it's guaranteed that we get here either with
5831 * the minimum CDCLK set, or the required power domains
5832 * enabled.
5833 */
5834 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5835
383c5a6a
VS
5836 if (IS_CHERRYVIEW(dev))
5837 cherryview_set_cdclk(dev, req_cdclk);
5838 else
5839 valleyview_set_cdclk(dev, req_cdclk);
738c05c0 5840
1e69cd74
VS
5841 vlv_program_pfi_credits(dev_priv);
5842
738c05c0 5843 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
383c5a6a 5844 }
30a970c6
JB
5845}
5846
89b667f8
JB
5847static void valleyview_crtc_enable(struct drm_crtc *crtc)
5848{
5849 struct drm_device *dev = crtc->dev;
a72e4c9f 5850 struct drm_i915_private *dev_priv = to_i915(dev);
89b667f8
JB
5851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5852 struct intel_encoder *encoder;
5853 int pipe = intel_crtc->pipe;
23538ef1 5854 bool is_dsi;
89b667f8 5855
83d65738 5856 WARN_ON(!crtc->state->enable);
89b667f8
JB
5857
5858 if (intel_crtc->active)
5859 return;
5860
409ee761 5861 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
8525a235 5862
1ae0d137
VS
5863 if (!is_dsi) {
5864 if (IS_CHERRYVIEW(dev))
6e3c9717 5865 chv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5866 else
6e3c9717 5867 vlv_prepare_pll(intel_crtc, intel_crtc->config);
1ae0d137 5868 }
5b18e57c 5869
6e3c9717 5870 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5871 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5872
5873 intel_set_pipe_timings(intel_crtc);
5874
c14b0485
VS
5875 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5876 struct drm_i915_private *dev_priv = dev->dev_private;
5877
5878 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5879 I915_WRITE(CHV_CANVAS(pipe), 0);
5880 }
5881
5b18e57c
DV
5882 i9xx_set_pipeconf(intel_crtc);
5883
89b667f8 5884 intel_crtc->active = true;
89b667f8 5885
a72e4c9f 5886 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5887
89b667f8
JB
5888 for_each_encoder_on_crtc(dev, crtc, encoder)
5889 if (encoder->pre_pll_enable)
5890 encoder->pre_pll_enable(encoder);
5891
9d556c99
CML
5892 if (!is_dsi) {
5893 if (IS_CHERRYVIEW(dev))
6e3c9717 5894 chv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5895 else
6e3c9717 5896 vlv_enable_pll(intel_crtc, intel_crtc->config);
9d556c99 5897 }
89b667f8
JB
5898
5899 for_each_encoder_on_crtc(dev, crtc, encoder)
5900 if (encoder->pre_enable)
5901 encoder->pre_enable(encoder);
5902
2dd24552
JB
5903 i9xx_pfit_enable(intel_crtc);
5904
63cbb074
VS
5905 intel_crtc_load_lut(crtc);
5906
f37fcc2a 5907 intel_update_watermarks(crtc);
e1fdc473 5908 intel_enable_pipe(intel_crtc);
be6a6f8e 5909
4b3a9526
VS
5910 assert_vblank_disabled(crtc);
5911 drm_crtc_vblank_on(crtc);
5912
f9b61ff6
DV
5913 for_each_encoder_on_crtc(dev, crtc, encoder)
5914 encoder->enable(encoder);
89b667f8
JB
5915}
5916
f13c2ef3
DV
5917static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5918{
5919 struct drm_device *dev = crtc->base.dev;
5920 struct drm_i915_private *dev_priv = dev->dev_private;
5921
6e3c9717
ACO
5922 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5923 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
f13c2ef3
DV
5924}
5925
0b8765c6 5926static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
5927{
5928 struct drm_device *dev = crtc->dev;
a72e4c9f 5929 struct drm_i915_private *dev_priv = to_i915(dev);
79e53945 5930 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5931 struct intel_encoder *encoder;
79e53945 5932 int pipe = intel_crtc->pipe;
79e53945 5933
83d65738 5934 WARN_ON(!crtc->state->enable);
08a48469 5935
f7abfe8b
CW
5936 if (intel_crtc->active)
5937 return;
5938
f13c2ef3
DV
5939 i9xx_set_pll_dividers(intel_crtc);
5940
6e3c9717 5941 if (intel_crtc->config->has_dp_encoder)
fe3cd48d 5942 intel_dp_set_m_n(intel_crtc, M1_N1);
5b18e57c
DV
5943
5944 intel_set_pipe_timings(intel_crtc);
5945
5b18e57c
DV
5946 i9xx_set_pipeconf(intel_crtc);
5947
f7abfe8b 5948 intel_crtc->active = true;
6b383a7f 5949
4a3436e8 5950 if (!IS_GEN2(dev))
a72e4c9f 5951 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4a3436e8 5952
9d6d9f19
MK
5953 for_each_encoder_on_crtc(dev, crtc, encoder)
5954 if (encoder->pre_enable)
5955 encoder->pre_enable(encoder);
5956
f6736a1a
DV
5957 i9xx_enable_pll(intel_crtc);
5958
2dd24552
JB
5959 i9xx_pfit_enable(intel_crtc);
5960
63cbb074
VS
5961 intel_crtc_load_lut(crtc);
5962
f37fcc2a 5963 intel_update_watermarks(crtc);
e1fdc473 5964 intel_enable_pipe(intel_crtc);
be6a6f8e 5965
4b3a9526
VS
5966 assert_vblank_disabled(crtc);
5967 drm_crtc_vblank_on(crtc);
5968
f9b61ff6
DV
5969 for_each_encoder_on_crtc(dev, crtc, encoder)
5970 encoder->enable(encoder);
0b8765c6 5971}
79e53945 5972
87476d63
DV
5973static void i9xx_pfit_disable(struct intel_crtc *crtc)
5974{
5975 struct drm_device *dev = crtc->base.dev;
5976 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 5977
6e3c9717 5978 if (!crtc->config->gmch_pfit.control)
328d8e82 5979 return;
87476d63 5980
328d8e82 5981 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 5982
328d8e82
DV
5983 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5984 I915_READ(PFIT_CONTROL));
5985 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
5986}
5987
0b8765c6
JB
5988static void i9xx_crtc_disable(struct drm_crtc *crtc)
5989{
5990 struct drm_device *dev = crtc->dev;
5991 struct drm_i915_private *dev_priv = dev->dev_private;
5992 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 5993 struct intel_encoder *encoder;
0b8765c6 5994 int pipe = intel_crtc->pipe;
ef9c3aee 5995
f7abfe8b
CW
5996 if (!intel_crtc->active)
5997 return;
5998
6304cd91
VS
5999 /*
6000 * On gen2 planes are double buffered but the pipe isn't, so we must
6001 * wait for planes to fully turn off before disabling the pipe.
564ed191
ID
6002 * We also need to wait on all gmch platforms because of the
6003 * self-refresh mode constraint explained above.
6304cd91 6004 */
564ed191 6005 intel_wait_for_vblank(dev, pipe);
6304cd91 6006
4b3a9526
VS
6007 for_each_encoder_on_crtc(dev, crtc, encoder)
6008 encoder->disable(encoder);
6009
f9b61ff6
DV
6010 drm_crtc_vblank_off(crtc);
6011 assert_vblank_disabled(crtc);
6012
575f7ab7 6013 intel_disable_pipe(intel_crtc);
24a1f16d 6014
87476d63 6015 i9xx_pfit_disable(intel_crtc);
24a1f16d 6016
89b667f8
JB
6017 for_each_encoder_on_crtc(dev, crtc, encoder)
6018 if (encoder->post_disable)
6019 encoder->post_disable(encoder);
6020
409ee761 6021 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
076ed3b2
CML
6022 if (IS_CHERRYVIEW(dev))
6023 chv_disable_pll(dev_priv, pipe);
6024 else if (IS_VALLEYVIEW(dev))
6025 vlv_disable_pll(dev_priv, pipe);
6026 else
1c4e0274 6027 i9xx_disable_pll(intel_crtc);
076ed3b2 6028 }
0b8765c6 6029
4a3436e8 6030 if (!IS_GEN2(dev))
a72e4c9f 6031 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4a3436e8 6032
f7abfe8b 6033 intel_crtc->active = false;
46ba614c 6034 intel_update_watermarks(crtc);
f37fcc2a 6035
efa9624e 6036 mutex_lock(&dev->struct_mutex);
7ff0ebcc 6037 intel_fbc_update(dev);
efa9624e 6038 mutex_unlock(&dev->struct_mutex);
0b8765c6
JB
6039}
6040
ee7b9f93
JB
6041static void i9xx_crtc_off(struct drm_crtc *crtc)
6042{
6043}
6044
b04c5bd6
BF
6045/* Master function to enable/disable CRTC and corresponding power wells */
6046void intel_crtc_control(struct drm_crtc *crtc, bool enable)
976f8a20
DV
6047{
6048 struct drm_device *dev = crtc->dev;
6049 struct drm_i915_private *dev_priv = dev->dev_private;
0e572fe7 6050 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
0e572fe7
DV
6051 enum intel_display_power_domain domain;
6052 unsigned long domains;
976f8a20 6053
0e572fe7
DV
6054 if (enable) {
6055 if (!intel_crtc->active) {
e1e9fb84
DV
6056 domains = get_crtc_power_domains(crtc);
6057 for_each_power_domain(domain, domains)
6058 intel_display_power_get(dev_priv, domain);
6059 intel_crtc->enabled_power_domains = domains;
0e572fe7
DV
6060
6061 dev_priv->display.crtc_enable(crtc);
ce22dba9 6062 intel_crtc_enable_planes(crtc);
0e572fe7
DV
6063 }
6064 } else {
6065 if (intel_crtc->active) {
ce22dba9 6066 intel_crtc_disable_planes(crtc);
0e572fe7
DV
6067 dev_priv->display.crtc_disable(crtc);
6068
e1e9fb84
DV
6069 domains = intel_crtc->enabled_power_domains;
6070 for_each_power_domain(domain, domains)
6071 intel_display_power_put(dev_priv, domain);
6072 intel_crtc->enabled_power_domains = 0;
0e572fe7
DV
6073 }
6074 }
b04c5bd6
BF
6075}
6076
6077/**
6078 * Sets the power management mode of the pipe and plane.
6079 */
6080void intel_crtc_update_dpms(struct drm_crtc *crtc)
6081{
6082 struct drm_device *dev = crtc->dev;
6083 struct intel_encoder *intel_encoder;
6084 bool enable = false;
6085
6086 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6087 enable |= intel_encoder->connectors_active;
6088
6089 intel_crtc_control(crtc, enable);
0f63cca2
ACO
6090
6091 crtc->state->active = enable;
976f8a20
DV
6092}
6093
cdd59983
CW
6094static void intel_crtc_disable(struct drm_crtc *crtc)
6095{
cdd59983 6096 struct drm_device *dev = crtc->dev;
976f8a20 6097 struct drm_connector *connector;
ee7b9f93 6098 struct drm_i915_private *dev_priv = dev->dev_private;
cdd59983 6099
976f8a20 6100 /* crtc should still be enabled when we disable it. */
83d65738 6101 WARN_ON(!crtc->state->enable);
976f8a20 6102
ce22dba9 6103 intel_crtc_disable_planes(crtc);
976f8a20 6104 dev_priv->display.crtc_disable(crtc);
ee7b9f93
JB
6105 dev_priv->display.off(crtc);
6106
70a101f8 6107 drm_plane_helper_disable(crtc->primary);
976f8a20
DV
6108
6109 /* Update computed state. */
6110 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6111 if (!connector->encoder || !connector->encoder->crtc)
6112 continue;
6113
6114 if (connector->encoder->crtc != crtc)
6115 continue;
6116
6117 connector->dpms = DRM_MODE_DPMS_OFF;
6118 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
6119 }
6120}
6121
ea5b213a 6122void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 6123{
4ef69c7a 6124 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 6125
ea5b213a
CW
6126 drm_encoder_cleanup(encoder);
6127 kfree(intel_encoder);
7e7d76c3
JB
6128}
6129
9237329d 6130/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
6131 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6132 * state of the entire output pipe. */
9237329d 6133static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 6134{
5ab432ef
DV
6135 if (mode == DRM_MODE_DPMS_ON) {
6136 encoder->connectors_active = true;
6137
b2cabb0e 6138 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
6139 } else {
6140 encoder->connectors_active = false;
6141
b2cabb0e 6142 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 6143 }
79e53945
JB
6144}
6145
0a91ca29
DV
6146/* Cross check the actual hw state with our own modeset state tracking (and it's
6147 * internal consistency). */
b980514c 6148static void intel_connector_check_state(struct intel_connector *connector)
79e53945 6149{
0a91ca29
DV
6150 if (connector->get_hw_state(connector)) {
6151 struct intel_encoder *encoder = connector->encoder;
6152 struct drm_crtc *crtc;
6153 bool encoder_enabled;
6154 enum pipe pipe;
6155
6156 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6157 connector->base.base.id,
c23cc417 6158 connector->base.name);
0a91ca29 6159
0e32b39c
DA
6160 /* there is no real hw state for MST connectors */
6161 if (connector->mst_port)
6162 return;
6163
e2c719b7 6164 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
0a91ca29 6165 "wrong connector dpms state\n");
e2c719b7 6166 I915_STATE_WARN(connector->base.encoder != &encoder->base,
0a91ca29 6167 "active connector not linked to encoder\n");
0a91ca29 6168
36cd7444 6169 if (encoder) {
e2c719b7 6170 I915_STATE_WARN(!encoder->connectors_active,
36cd7444
DA
6171 "encoder->connectors_active not set\n");
6172
6173 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
e2c719b7
RC
6174 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6175 if (I915_STATE_WARN_ON(!encoder->base.crtc))
36cd7444 6176 return;
0a91ca29 6177
36cd7444 6178 crtc = encoder->base.crtc;
0a91ca29 6179
83d65738
MR
6180 I915_STATE_WARN(!crtc->state->enable,
6181 "crtc not enabled\n");
e2c719b7
RC
6182 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6183 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
36cd7444
DA
6184 "encoder active on the wrong pipe\n");
6185 }
0a91ca29 6186 }
79e53945
JB
6187}
6188
08d9bc92
ACO
6189int intel_connector_init(struct intel_connector *connector)
6190{
6191 struct drm_connector_state *connector_state;
6192
6193 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6194 if (!connector_state)
6195 return -ENOMEM;
6196
6197 connector->base.state = connector_state;
6198 return 0;
6199}
6200
6201struct intel_connector *intel_connector_alloc(void)
6202{
6203 struct intel_connector *connector;
6204
6205 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6206 if (!connector)
6207 return NULL;
6208
6209 if (intel_connector_init(connector) < 0) {
6210 kfree(connector);
6211 return NULL;
6212 }
6213
6214 return connector;
6215}
6216
5ab432ef
DV
6217/* Even simpler default implementation, if there's really no special case to
6218 * consider. */
6219void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 6220{
5ab432ef
DV
6221 /* All the simple cases only support two dpms states. */
6222 if (mode != DRM_MODE_DPMS_ON)
6223 mode = DRM_MODE_DPMS_OFF;
d4270e57 6224
5ab432ef
DV
6225 if (mode == connector->dpms)
6226 return;
6227
6228 connector->dpms = mode;
6229
6230 /* Only need to change hw state when actually enabled */
c9976dcf
CW
6231 if (connector->encoder)
6232 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 6233
b980514c 6234 intel_modeset_check_state(connector->dev);
79e53945
JB
6235}
6236
f0947c37
DV
6237/* Simple connector->get_hw_state implementation for encoders that support only
6238 * one connector and no cloning and hence the encoder state determines the state
6239 * of the connector. */
6240bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 6241{
24929352 6242 enum pipe pipe = 0;
f0947c37 6243 struct intel_encoder *encoder = connector->encoder;
ea5b213a 6244
f0947c37 6245 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
6246}
6247
6d293983 6248static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
d272ddfa 6249{
6d293983
ACO
6250 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6251 return crtc_state->fdi_lanes;
d272ddfa
VS
6252
6253 return 0;
6254}
6255
6d293983 6256static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5cec258b 6257 struct intel_crtc_state *pipe_config)
1857e1da 6258{
6d293983
ACO
6259 struct drm_atomic_state *state = pipe_config->base.state;
6260 struct intel_crtc *other_crtc;
6261 struct intel_crtc_state *other_crtc_state;
6262
1857e1da
DV
6263 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6264 pipe_name(pipe), pipe_config->fdi_lanes);
6265 if (pipe_config->fdi_lanes > 4) {
6266 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6267 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6268 return -EINVAL;
1857e1da
DV
6269 }
6270
bafb6553 6271 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
6272 if (pipe_config->fdi_lanes > 2) {
6273 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6274 pipe_config->fdi_lanes);
6d293983 6275 return -EINVAL;
1857e1da 6276 } else {
6d293983 6277 return 0;
1857e1da
DV
6278 }
6279 }
6280
6281 if (INTEL_INFO(dev)->num_pipes == 2)
6d293983 6282 return 0;
1857e1da
DV
6283
6284 /* Ivybridge 3 pipe is really complicated */
6285 switch (pipe) {
6286 case PIPE_A:
6d293983 6287 return 0;
1857e1da 6288 case PIPE_B:
6d293983
ACO
6289 if (pipe_config->fdi_lanes <= 2)
6290 return 0;
6291
6292 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6293 other_crtc_state =
6294 intel_atomic_get_crtc_state(state, other_crtc);
6295 if (IS_ERR(other_crtc_state))
6296 return PTR_ERR(other_crtc_state);
6297
6298 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
1857e1da
DV
6299 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6300 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6301 return -EINVAL;
1857e1da 6302 }
6d293983 6303 return 0;
1857e1da 6304 case PIPE_C:
251cc67c
VS
6305 if (pipe_config->fdi_lanes > 2) {
6306 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6307 pipe_name(pipe), pipe_config->fdi_lanes);
6d293983 6308 return -EINVAL;
251cc67c 6309 }
6d293983
ACO
6310
6311 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6312 other_crtc_state =
6313 intel_atomic_get_crtc_state(state, other_crtc);
6314 if (IS_ERR(other_crtc_state))
6315 return PTR_ERR(other_crtc_state);
6316
6317 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
1857e1da 6318 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
6d293983 6319 return -EINVAL;
1857e1da 6320 }
6d293983 6321 return 0;
1857e1da
DV
6322 default:
6323 BUG();
6324 }
6325}
6326
e29c22c0
DV
6327#define RETRY 1
6328static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5cec258b 6329 struct intel_crtc_state *pipe_config)
877d48d5 6330{
1857e1da 6331 struct drm_device *dev = intel_crtc->base.dev;
2d112de7 6332 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
6d293983
ACO
6333 int lane, link_bw, fdi_dotclock, ret;
6334 bool needs_recompute = false;
877d48d5 6335
e29c22c0 6336retry:
877d48d5
DV
6337 /* FDI is a binary signal running at ~2.7GHz, encoding
6338 * each output octet as 10 bits. The actual frequency
6339 * is stored as a divider into a 100MHz clock, and the
6340 * mode pixel clock is stored in units of 1KHz.
6341 * Hence the bw of each lane in terms of the mode signal
6342 * is:
6343 */
6344 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6345
241bfc38 6346 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 6347
2bd89a07 6348 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
6349 pipe_config->pipe_bpp);
6350
6351 pipe_config->fdi_lanes = lane;
6352
2bd89a07 6353 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 6354 link_bw, &pipe_config->fdi_m_n);
1857e1da 6355
6d293983
ACO
6356 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6357 intel_crtc->pipe, pipe_config);
6358 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
e29c22c0
DV
6359 pipe_config->pipe_bpp -= 2*3;
6360 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6361 pipe_config->pipe_bpp);
6362 needs_recompute = true;
6363 pipe_config->bw_constrained = true;
6364
6365 goto retry;
6366 }
6367
6368 if (needs_recompute)
6369 return RETRY;
6370
6d293983 6371 return ret;
877d48d5
DV
6372}
6373
42db64ef 6374static void hsw_compute_ips_config(struct intel_crtc *crtc,
5cec258b 6375 struct intel_crtc_state *pipe_config)
42db64ef 6376{
d330a953 6377 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 6378 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 6379 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
6380}
6381
a43f6e0f 6382static int intel_crtc_compute_config(struct intel_crtc *crtc,
5cec258b 6383 struct intel_crtc_state *pipe_config)
79e53945 6384{
a43f6e0f 6385 struct drm_device *dev = crtc->base.dev;
8bd31e67 6386 struct drm_i915_private *dev_priv = dev->dev_private;
2d112de7 6387 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
d03c93d4 6388 int ret;
89749350 6389
ad3a4479 6390 /* FIXME should check pixel clock limits on all platforms */
cf532bb2 6391 if (INTEL_INFO(dev)->gen < 4) {
cf532bb2
VS
6392 int clock_limit =
6393 dev_priv->display.get_display_clock_speed(dev);
6394
6395 /*
6396 * Enable pixel doubling when the dot clock
6397 * is > 90% of the (display) core speed.
6398 *
b397c96b
VS
6399 * GDG double wide on either pipe,
6400 * otherwise pipe A only.
cf532bb2 6401 */
b397c96b 6402 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 6403 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 6404 clock_limit *= 2;
cf532bb2 6405 pipe_config->double_wide = true;
ad3a4479
VS
6406 }
6407
241bfc38 6408 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 6409 return -EINVAL;
2c07245f 6410 }
89749350 6411
1d1d0e27
VS
6412 /*
6413 * Pipe horizontal size must be even in:
6414 * - DVO ganged mode
6415 * - LVDS dual channel mode
6416 * - Double wide pipe
6417 */
a93e255f 6418 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
1d1d0e27
VS
6419 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6420 pipe_config->pipe_src_w &= ~1;
6421
8693a824
DL
6422 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6423 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
6424 */
6425 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6426 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 6427 return -EINVAL;
44f46b42 6428
f5adf94e 6429 if (HAS_IPS(dev))
a43f6e0f
DV
6430 hsw_compute_ips_config(crtc, pipe_config);
6431
877d48d5 6432 if (pipe_config->has_pch_encoder)
a43f6e0f 6433 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 6434
d03c93d4
CK
6435 /* FIXME: remove below call once atomic mode set is place and all crtc
6436 * related checks called from atomic_crtc_check function */
6437 ret = 0;
6438 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6439 crtc, pipe_config->base.state);
6440 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6441
6442 return ret;
79e53945
JB
6443}
6444
1652d19e
VS
6445static int skylake_get_display_clock_speed(struct drm_device *dev)
6446{
6447 struct drm_i915_private *dev_priv = to_i915(dev);
6448 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6449 uint32_t cdctl = I915_READ(CDCLK_CTL);
6450 uint32_t linkrate;
6451
6452 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6453 WARN(1, "LCPLL1 not enabled\n");
6454 return 24000; /* 24MHz is the cd freq with NSSC ref */
6455 }
6456
6457 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6458 return 540000;
6459
6460 linkrate = (I915_READ(DPLL_CTRL1) &
71cd8423 6461 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
1652d19e 6462
71cd8423
DL
6463 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6464 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
1652d19e
VS
6465 /* vco 8640 */
6466 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6467 case CDCLK_FREQ_450_432:
6468 return 432000;
6469 case CDCLK_FREQ_337_308:
6470 return 308570;
6471 case CDCLK_FREQ_675_617:
6472 return 617140;
6473 default:
6474 WARN(1, "Unknown cd freq selection\n");
6475 }
6476 } else {
6477 /* vco 8100 */
6478 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6479 case CDCLK_FREQ_450_432:
6480 return 450000;
6481 case CDCLK_FREQ_337_308:
6482 return 337500;
6483 case CDCLK_FREQ_675_617:
6484 return 675000;
6485 default:
6486 WARN(1, "Unknown cd freq selection\n");
6487 }
6488 }
6489
6490 /* error case, do as if DPLL0 isn't enabled */
6491 return 24000;
6492}
6493
6494static int broadwell_get_display_clock_speed(struct drm_device *dev)
6495{
6496 struct drm_i915_private *dev_priv = dev->dev_private;
6497 uint32_t lcpll = I915_READ(LCPLL_CTL);
6498 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6499
6500 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6501 return 800000;
6502 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6503 return 450000;
6504 else if (freq == LCPLL_CLK_FREQ_450)
6505 return 450000;
6506 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6507 return 540000;
6508 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6509 return 337500;
6510 else
6511 return 675000;
6512}
6513
6514static int haswell_get_display_clock_speed(struct drm_device *dev)
6515{
6516 struct drm_i915_private *dev_priv = dev->dev_private;
6517 uint32_t lcpll = I915_READ(LCPLL_CTL);
6518 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6519
6520 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6521 return 800000;
6522 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6523 return 450000;
6524 else if (freq == LCPLL_CLK_FREQ_450)
6525 return 450000;
6526 else if (IS_HSW_ULT(dev))
6527 return 337500;
6528 else
6529 return 540000;
79e53945
JB
6530}
6531
25eb05fc
JB
6532static int valleyview_get_display_clock_speed(struct drm_device *dev)
6533{
d197b7d3 6534 struct drm_i915_private *dev_priv = dev->dev_private;
d197b7d3
VS
6535 u32 val;
6536 int divider;
6537
6bcda4f0
VS
6538 if (dev_priv->hpll_freq == 0)
6539 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6540
d197b7d3
VS
6541 mutex_lock(&dev_priv->dpio_lock);
6542 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6543 mutex_unlock(&dev_priv->dpio_lock);
6544
6545 divider = val & DISPLAY_FREQUENCY_VALUES;
6546
7d007f40
VS
6547 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6548 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6549 "cdclk change in progress\n");
6550
6bcda4f0 6551 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
25eb05fc
JB
6552}
6553
b37a6434
VS
6554static int ilk_get_display_clock_speed(struct drm_device *dev)
6555{
6556 return 450000;
6557}
6558
e70236a8
JB
6559static int i945_get_display_clock_speed(struct drm_device *dev)
6560{
6561 return 400000;
6562}
79e53945 6563
e70236a8 6564static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 6565{
e907f170 6566 return 333333;
e70236a8 6567}
79e53945 6568
e70236a8
JB
6569static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6570{
6571 return 200000;
6572}
79e53945 6573
257a7ffc
DV
6574static int pnv_get_display_clock_speed(struct drm_device *dev)
6575{
6576 u16 gcfgc = 0;
6577
6578 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6579
6580 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6581 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
e907f170 6582 return 266667;
257a7ffc 6583 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
e907f170 6584 return 333333;
257a7ffc 6585 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
e907f170 6586 return 444444;
257a7ffc
DV
6587 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6588 return 200000;
6589 default:
6590 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6591 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
e907f170 6592 return 133333;
257a7ffc 6593 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
e907f170 6594 return 166667;
257a7ffc
DV
6595 }
6596}
6597
e70236a8
JB
6598static int i915gm_get_display_clock_speed(struct drm_device *dev)
6599{
6600 u16 gcfgc = 0;
79e53945 6601
e70236a8
JB
6602 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6603
6604 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
e907f170 6605 return 133333;
e70236a8
JB
6606 else {
6607 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6608 case GC_DISPLAY_CLOCK_333_MHZ:
e907f170 6609 return 333333;
e70236a8
JB
6610 default:
6611 case GC_DISPLAY_CLOCK_190_200_MHZ:
6612 return 190000;
79e53945 6613 }
e70236a8
JB
6614 }
6615}
6616
6617static int i865_get_display_clock_speed(struct drm_device *dev)
6618{
e907f170 6619 return 266667;
e70236a8
JB
6620}
6621
6622static int i855_get_display_clock_speed(struct drm_device *dev)
6623{
6624 u16 hpllcc = 0;
6625 /* Assume that the hardware is in the high speed state. This
6626 * should be the default.
6627 */
6628 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6629 case GC_CLOCK_133_200:
6630 case GC_CLOCK_100_200:
6631 return 200000;
6632 case GC_CLOCK_166_250:
6633 return 250000;
6634 case GC_CLOCK_100_133:
e907f170 6635 return 133333;
e70236a8 6636 }
79e53945 6637
e70236a8
JB
6638 /* Shouldn't happen */
6639 return 0;
6640}
79e53945 6641
e70236a8
JB
6642static int i830_get_display_clock_speed(struct drm_device *dev)
6643{
e907f170 6644 return 133333;
79e53945
JB
6645}
6646
2c07245f 6647static void
a65851af 6648intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 6649{
a65851af
VS
6650 while (*num > DATA_LINK_M_N_MASK ||
6651 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
6652 *num >>= 1;
6653 *den >>= 1;
6654 }
6655}
6656
a65851af
VS
6657static void compute_m_n(unsigned int m, unsigned int n,
6658 uint32_t *ret_m, uint32_t *ret_n)
6659{
6660 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6661 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6662 intel_reduce_m_n_ratio(ret_m, ret_n);
6663}
6664
e69d0bc1
DV
6665void
6666intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6667 int pixel_clock, int link_clock,
6668 struct intel_link_m_n *m_n)
2c07245f 6669{
e69d0bc1 6670 m_n->tu = 64;
a65851af
VS
6671
6672 compute_m_n(bits_per_pixel * pixel_clock,
6673 link_clock * nlanes * 8,
6674 &m_n->gmch_m, &m_n->gmch_n);
6675
6676 compute_m_n(pixel_clock, link_clock,
6677 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
6678}
6679
a7615030
CW
6680static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6681{
d330a953
JN
6682 if (i915.panel_use_ssc >= 0)
6683 return i915.panel_use_ssc != 0;
41aa3448 6684 return dev_priv->vbt.lvds_use_ssc
435793df 6685 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
6686}
6687
a93e255f
ACO
6688static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6689 int num_connectors)
c65d77d8 6690{
a93e255f 6691 struct drm_device *dev = crtc_state->base.crtc->dev;
c65d77d8
JB
6692 struct drm_i915_private *dev_priv = dev->dev_private;
6693 int refclk;
6694
a93e255f
ACO
6695 WARN_ON(!crtc_state->base.state);
6696
5ab7b0b7 6697 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
9a0ea498 6698 refclk = 100000;
a93e255f 6699 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
c65d77d8 6700 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
6701 refclk = dev_priv->vbt.lvds_ssc_freq;
6702 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
6703 } else if (!IS_GEN2(dev)) {
6704 refclk = 96000;
6705 } else {
6706 refclk = 48000;
6707 }
6708
6709 return refclk;
6710}
6711
7429e9d4 6712static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 6713{
7df00d7a 6714 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 6715}
f47709a9 6716
7429e9d4
DV
6717static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6718{
6719 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
6720}
6721
f47709a9 6722static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
190f68c5 6723 struct intel_crtc_state *crtc_state,
a7516a05
JB
6724 intel_clock_t *reduced_clock)
6725{
f47709a9 6726 struct drm_device *dev = crtc->base.dev;
a7516a05
JB
6727 u32 fp, fp2 = 0;
6728
6729 if (IS_PINEVIEW(dev)) {
190f68c5 6730 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6731 if (reduced_clock)
7429e9d4 6732 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 6733 } else {
190f68c5 6734 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
a7516a05 6735 if (reduced_clock)
7429e9d4 6736 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
6737 }
6738
190f68c5 6739 crtc_state->dpll_hw_state.fp0 = fp;
a7516a05 6740
f47709a9 6741 crtc->lowfreq_avail = false;
a93e255f 6742 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
ab585dea 6743 reduced_clock) {
190f68c5 6744 crtc_state->dpll_hw_state.fp1 = fp2;
f47709a9 6745 crtc->lowfreq_avail = true;
a7516a05 6746 } else {
190f68c5 6747 crtc_state->dpll_hw_state.fp1 = fp;
a7516a05
JB
6748 }
6749}
6750
5e69f97f
CML
6751static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6752 pipe)
89b667f8
JB
6753{
6754 u32 reg_val;
6755
6756 /*
6757 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6758 * and set it to a reasonable value instead.
6759 */
ab3c759a 6760 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
6761 reg_val &= 0xffffff00;
6762 reg_val |= 0x00000030;
ab3c759a 6763 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6764
ab3c759a 6765 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6766 reg_val &= 0x8cffffff;
6767 reg_val = 0x8c000000;
ab3c759a 6768 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 6769
ab3c759a 6770 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 6771 reg_val &= 0xffffff00;
ab3c759a 6772 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 6773
ab3c759a 6774 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
6775 reg_val &= 0x00ffffff;
6776 reg_val |= 0xb0000000;
ab3c759a 6777 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
6778}
6779
b551842d
DV
6780static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6781 struct intel_link_m_n *m_n)
6782{
6783 struct drm_device *dev = crtc->base.dev;
6784 struct drm_i915_private *dev_priv = dev->dev_private;
6785 int pipe = crtc->pipe;
6786
e3b95f1e
DV
6787 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6788 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6789 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6790 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
6791}
6792
6793static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
f769cd24
VK
6794 struct intel_link_m_n *m_n,
6795 struct intel_link_m_n *m2_n2)
b551842d
DV
6796{
6797 struct drm_device *dev = crtc->base.dev;
6798 struct drm_i915_private *dev_priv = dev->dev_private;
6799 int pipe = crtc->pipe;
6e3c9717 6800 enum transcoder transcoder = crtc->config->cpu_transcoder;
b551842d
DV
6801
6802 if (INTEL_INFO(dev)->gen >= 5) {
6803 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6804 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6805 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6806 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
f769cd24
VK
6807 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6808 * for gen < 8) and if DRRS is supported (to make sure the
6809 * registers are not unnecessarily accessed).
6810 */
44395bfe 6811 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
6e3c9717 6812 crtc->config->has_drrs) {
f769cd24
VK
6813 I915_WRITE(PIPE_DATA_M2(transcoder),
6814 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6815 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6816 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6817 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6818 }
b551842d 6819 } else {
e3b95f1e
DV
6820 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6821 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6822 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6823 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
6824 }
6825}
6826
fe3cd48d 6827void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
03afc4a2 6828{
fe3cd48d
R
6829 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6830
6831 if (m_n == M1_N1) {
6832 dp_m_n = &crtc->config->dp_m_n;
6833 dp_m2_n2 = &crtc->config->dp_m2_n2;
6834 } else if (m_n == M2_N2) {
6835
6836 /*
6837 * M2_N2 registers are not supported. Hence m2_n2 divider value
6838 * needs to be programmed into M1_N1.
6839 */
6840 dp_m_n = &crtc->config->dp_m2_n2;
6841 } else {
6842 DRM_ERROR("Unsupported divider value\n");
6843 return;
6844 }
6845
6e3c9717
ACO
6846 if (crtc->config->has_pch_encoder)
6847 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
03afc4a2 6848 else
fe3cd48d 6849 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
03afc4a2
DV
6850}
6851
d288f65f 6852static void vlv_update_pll(struct intel_crtc *crtc,
5cec258b 6853 struct intel_crtc_state *pipe_config)
bdd4b6a6
DV
6854{
6855 u32 dpll, dpll_md;
6856
6857 /*
6858 * Enable DPIO clock input. We should never disable the reference
6859 * clock for pipe B, since VGA hotplug / manual detection depends
6860 * on it.
6861 */
6862 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6863 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6864 /* We should never disable this, set it here for state tracking */
6865 if (crtc->pipe == PIPE_B)
6866 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6867 dpll |= DPLL_VCO_ENABLE;
d288f65f 6868 pipe_config->dpll_hw_state.dpll = dpll;
bdd4b6a6 6869
d288f65f 6870 dpll_md = (pipe_config->pixel_multiplier - 1)
bdd4b6a6 6871 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
d288f65f 6872 pipe_config->dpll_hw_state.dpll_md = dpll_md;
bdd4b6a6
DV
6873}
6874
d288f65f 6875static void vlv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6876 const struct intel_crtc_state *pipe_config)
a0c4da24 6877{
f47709a9 6878 struct drm_device *dev = crtc->base.dev;
a0c4da24 6879 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 6880 int pipe = crtc->pipe;
bdd4b6a6 6881 u32 mdiv;
a0c4da24 6882 u32 bestn, bestm1, bestm2, bestp1, bestp2;
bdd4b6a6 6883 u32 coreclk, reg_val;
a0c4da24 6884
09153000
DV
6885 mutex_lock(&dev_priv->dpio_lock);
6886
d288f65f
VS
6887 bestn = pipe_config->dpll.n;
6888 bestm1 = pipe_config->dpll.m1;
6889 bestm2 = pipe_config->dpll.m2;
6890 bestp1 = pipe_config->dpll.p1;
6891 bestp2 = pipe_config->dpll.p2;
a0c4da24 6892
89b667f8
JB
6893 /* See eDP HDMI DPIO driver vbios notes doc */
6894
6895 /* PLL B needs special handling */
bdd4b6a6 6896 if (pipe == PIPE_B)
5e69f97f 6897 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
6898
6899 /* Set up Tx target for periodic Rcomp update */
ab3c759a 6900 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
6901
6902 /* Disable target IRef on PLL */
ab3c759a 6903 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 6904 reg_val &= 0x00ffffff;
ab3c759a 6905 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
6906
6907 /* Disable fast lock */
ab3c759a 6908 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
6909
6910 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
6911 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6912 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6913 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 6914 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
6915
6916 /*
6917 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6918 * but we don't support that).
6919 * Note: don't use the DAC post divider as it seems unstable.
6920 */
6921 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 6922 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6923
a0c4da24 6924 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 6925 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 6926
89b667f8 6927 /* Set HBR and RBR LPF coefficients */
d288f65f 6928 if (pipe_config->port_clock == 162000 ||
409ee761
ACO
6929 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6930 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
ab3c759a 6931 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 6932 0x009f0003);
89b667f8 6933 else
ab3c759a 6934 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
6935 0x00d0000f);
6936
681a8504 6937 if (pipe_config->has_dp_encoder) {
89b667f8 6938 /* Use SSC source */
bdd4b6a6 6939 if (pipe == PIPE_A)
ab3c759a 6940 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6941 0x0df40000);
6942 else
ab3c759a 6943 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6944 0x0df70000);
6945 } else { /* HDMI or VGA */
6946 /* Use bend source */
bdd4b6a6 6947 if (pipe == PIPE_A)
ab3c759a 6948 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6949 0x0df70000);
6950 else
ab3c759a 6951 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
6952 0x0df40000);
6953 }
a0c4da24 6954
ab3c759a 6955 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8 6956 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
409ee761
ACO
6957 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6958 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
89b667f8 6959 coreclk |= 0x01000000;
ab3c759a 6960 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 6961
ab3c759a 6962 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
09153000 6963 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
6964}
6965
d288f65f 6966static void chv_update_pll(struct intel_crtc *crtc,
5cec258b 6967 struct intel_crtc_state *pipe_config)
1ae0d137 6968{
d288f65f 6969 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
1ae0d137
VS
6970 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6971 DPLL_VCO_ENABLE;
6972 if (crtc->pipe != PIPE_A)
d288f65f 6973 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
1ae0d137 6974
d288f65f
VS
6975 pipe_config->dpll_hw_state.dpll_md =
6976 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
1ae0d137
VS
6977}
6978
d288f65f 6979static void chv_prepare_pll(struct intel_crtc *crtc,
5cec258b 6980 const struct intel_crtc_state *pipe_config)
9d556c99
CML
6981{
6982 struct drm_device *dev = crtc->base.dev;
6983 struct drm_i915_private *dev_priv = dev->dev_private;
6984 int pipe = crtc->pipe;
6985 int dpll_reg = DPLL(crtc->pipe);
6986 enum dpio_channel port = vlv_pipe_to_channel(pipe);
9cbe40c1 6987 u32 loopfilter, tribuf_calcntr;
9d556c99 6988 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
a945ce7e 6989 u32 dpio_val;
9cbe40c1 6990 int vco;
9d556c99 6991
d288f65f
VS
6992 bestn = pipe_config->dpll.n;
6993 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6994 bestm1 = pipe_config->dpll.m1;
6995 bestm2 = pipe_config->dpll.m2 >> 22;
6996 bestp1 = pipe_config->dpll.p1;
6997 bestp2 = pipe_config->dpll.p2;
9cbe40c1 6998 vco = pipe_config->dpll.vco;
a945ce7e 6999 dpio_val = 0;
9cbe40c1 7000 loopfilter = 0;
9d556c99
CML
7001
7002 /*
7003 * Enable Refclk and SSC
7004 */
a11b0703 7005 I915_WRITE(dpll_reg,
d288f65f 7006 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
a11b0703
VS
7007
7008 mutex_lock(&dev_priv->dpio_lock);
9d556c99 7009
9d556c99
CML
7010 /* p1 and p2 divider */
7011 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7012 5 << DPIO_CHV_S1_DIV_SHIFT |
7013 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7014 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7015 1 << DPIO_CHV_K_DIV_SHIFT);
7016
7017 /* Feedback post-divider - m2 */
7018 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7019
7020 /* Feedback refclk divider - n and m1 */
7021 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7022 DPIO_CHV_M1_DIV_BY_2 |
7023 1 << DPIO_CHV_N_DIV_SHIFT);
7024
7025 /* M2 fraction division */
a945ce7e
VP
7026 if (bestm2_frac)
7027 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
9d556c99
CML
7028
7029 /* M2 fraction division enable */
a945ce7e
VP
7030 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7031 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7032 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7033 if (bestm2_frac)
7034 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7035 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
9d556c99 7036
de3a0fde
VP
7037 /* Program digital lock detect threshold */
7038 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7039 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7040 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7041 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7042 if (!bestm2_frac)
7043 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7044 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7045
9d556c99 7046 /* Loop filter */
9cbe40c1
VP
7047 if (vco == 5400000) {
7048 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7049 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7050 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7051 tribuf_calcntr = 0x9;
7052 } else if (vco <= 6200000) {
7053 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7054 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7055 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7056 tribuf_calcntr = 0x9;
7057 } else if (vco <= 6480000) {
7058 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7059 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7060 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7061 tribuf_calcntr = 0x8;
7062 } else {
7063 /* Not supported. Apply the same limits as in the max case */
7064 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7065 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7066 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7067 tribuf_calcntr = 0;
7068 }
9d556c99
CML
7069 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7070
968040b2 7071 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
9cbe40c1
VP
7072 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7073 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7074 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7075
9d556c99
CML
7076 /* AFC Recal */
7077 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7078 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7079 DPIO_AFC_RECAL);
7080
7081 mutex_unlock(&dev_priv->dpio_lock);
7082}
7083
d288f65f
VS
7084/**
7085 * vlv_force_pll_on - forcibly enable just the PLL
7086 * @dev_priv: i915 private structure
7087 * @pipe: pipe PLL to enable
7088 * @dpll: PLL configuration
7089 *
7090 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7091 * in cases where we need the PLL enabled even when @pipe is not going to
7092 * be enabled.
7093 */
7094void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7095 const struct dpll *dpll)
7096{
7097 struct intel_crtc *crtc =
7098 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5cec258b 7099 struct intel_crtc_state pipe_config = {
a93e255f 7100 .base.crtc = &crtc->base,
d288f65f
VS
7101 .pixel_multiplier = 1,
7102 .dpll = *dpll,
7103 };
7104
7105 if (IS_CHERRYVIEW(dev)) {
7106 chv_update_pll(crtc, &pipe_config);
7107 chv_prepare_pll(crtc, &pipe_config);
7108 chv_enable_pll(crtc, &pipe_config);
7109 } else {
7110 vlv_update_pll(crtc, &pipe_config);
7111 vlv_prepare_pll(crtc, &pipe_config);
7112 vlv_enable_pll(crtc, &pipe_config);
7113 }
7114}
7115
7116/**
7117 * vlv_force_pll_off - forcibly disable just the PLL
7118 * @dev_priv: i915 private structure
7119 * @pipe: pipe PLL to disable
7120 *
7121 * Disable the PLL for @pipe. To be used in cases where we need
7122 * the PLL enabled even when @pipe is not going to be enabled.
7123 */
7124void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7125{
7126 if (IS_CHERRYVIEW(dev))
7127 chv_disable_pll(to_i915(dev), pipe);
7128 else
7129 vlv_disable_pll(to_i915(dev), pipe);
7130}
7131
f47709a9 7132static void i9xx_update_pll(struct intel_crtc *crtc,
190f68c5 7133 struct intel_crtc_state *crtc_state,
f47709a9 7134 intel_clock_t *reduced_clock,
eb1cbe48
DV
7135 int num_connectors)
7136{
f47709a9 7137 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7138 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
7139 u32 dpll;
7140 bool is_sdvo;
190f68c5 7141 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7142
190f68c5 7143 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7144
a93e255f
ACO
7145 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7146 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
7147
7148 dpll = DPLL_VGA_MODE_DIS;
7149
a93e255f 7150 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
7151 dpll |= DPLLB_MODE_LVDS;
7152 else
7153 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 7154
ef1b460d 7155 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
190f68c5 7156 dpll |= (crtc_state->pixel_multiplier - 1)
198a037f 7157 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 7158 }
198a037f
DV
7159
7160 if (is_sdvo)
4a33e48d 7161 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 7162
190f68c5 7163 if (crtc_state->has_dp_encoder)
4a33e48d 7164 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
7165
7166 /* compute bitmask from p1 value */
7167 if (IS_PINEVIEW(dev))
7168 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7169 else {
7170 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7171 if (IS_G4X(dev) && reduced_clock)
7172 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7173 }
7174 switch (clock->p2) {
7175 case 5:
7176 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7177 break;
7178 case 7:
7179 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7180 break;
7181 case 10:
7182 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7183 break;
7184 case 14:
7185 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7186 break;
7187 }
7188 if (INTEL_INFO(dev)->gen >= 4)
7189 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7190
190f68c5 7191 if (crtc_state->sdvo_tv_clock)
eb1cbe48 7192 dpll |= PLL_REF_INPUT_TVCLKINBC;
a93e255f 7193 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7194 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7195 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7196 else
7197 dpll |= PLL_REF_INPUT_DREFCLK;
7198
7199 dpll |= DPLL_VCO_ENABLE;
190f68c5 7200 crtc_state->dpll_hw_state.dpll = dpll;
8bcc2795 7201
eb1cbe48 7202 if (INTEL_INFO(dev)->gen >= 4) {
190f68c5 7203 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
ef1b460d 7204 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
190f68c5 7205 crtc_state->dpll_hw_state.dpll_md = dpll_md;
eb1cbe48
DV
7206 }
7207}
7208
f47709a9 7209static void i8xx_update_pll(struct intel_crtc *crtc,
190f68c5 7210 struct intel_crtc_state *crtc_state,
f47709a9 7211 intel_clock_t *reduced_clock,
eb1cbe48
DV
7212 int num_connectors)
7213{
f47709a9 7214 struct drm_device *dev = crtc->base.dev;
eb1cbe48 7215 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 7216 u32 dpll;
190f68c5 7217 struct dpll *clock = &crtc_state->dpll;
eb1cbe48 7218
190f68c5 7219 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
2a8f64ca 7220
eb1cbe48
DV
7221 dpll = DPLL_VGA_MODE_DIS;
7222
a93e255f 7223 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
7224 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7225 } else {
7226 if (clock->p1 == 2)
7227 dpll |= PLL_P1_DIVIDE_BY_TWO;
7228 else
7229 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7230 if (clock->p2 == 4)
7231 dpll |= PLL_P2_DIVIDE_BY_4;
7232 }
7233
a93e255f 7234 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
4a33e48d
DV
7235 dpll |= DPLL_DVO_2X_MODE;
7236
a93e255f 7237 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
7238 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7239 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7240 else
7241 dpll |= PLL_REF_INPUT_DREFCLK;
7242
7243 dpll |= DPLL_VCO_ENABLE;
190f68c5 7244 crtc_state->dpll_hw_state.dpll = dpll;
eb1cbe48
DV
7245}
7246
8a654f3b 7247static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
7248{
7249 struct drm_device *dev = intel_crtc->base.dev;
7250 struct drm_i915_private *dev_priv = dev->dev_private;
7251 enum pipe pipe = intel_crtc->pipe;
6e3c9717 7252 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
8a654f3b 7253 struct drm_display_mode *adjusted_mode =
6e3c9717 7254 &intel_crtc->config->base.adjusted_mode;
1caea6e9
VS
7255 uint32_t crtc_vtotal, crtc_vblank_end;
7256 int vsyncshift = 0;
4d8a62ea
DV
7257
7258 /* We need to be careful not to changed the adjusted mode, for otherwise
7259 * the hw state checker will get angry at the mismatch. */
7260 crtc_vtotal = adjusted_mode->crtc_vtotal;
7261 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c 7262
609aeaca 7263 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
b0e77b9c 7264 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
7265 crtc_vtotal -= 1;
7266 crtc_vblank_end -= 1;
609aeaca 7267
409ee761 7268 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
609aeaca
VS
7269 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7270 else
7271 vsyncshift = adjusted_mode->crtc_hsync_start -
7272 adjusted_mode->crtc_htotal / 2;
1caea6e9
VS
7273 if (vsyncshift < 0)
7274 vsyncshift += adjusted_mode->crtc_htotal;
b0e77b9c
PZ
7275 }
7276
7277 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 7278 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 7279
fe2b8f9d 7280 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
7281 (adjusted_mode->crtc_hdisplay - 1) |
7282 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 7283 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
7284 (adjusted_mode->crtc_hblank_start - 1) |
7285 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 7286 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
7287 (adjusted_mode->crtc_hsync_start - 1) |
7288 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7289
fe2b8f9d 7290 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 7291 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 7292 ((crtc_vtotal - 1) << 16));
fe2b8f9d 7293 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 7294 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 7295 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 7296 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
7297 (adjusted_mode->crtc_vsync_start - 1) |
7298 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7299
b5e508d4
PZ
7300 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7301 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7302 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7303 * bits. */
7304 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7305 (pipe == PIPE_B || pipe == PIPE_C))
7306 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7307
b0e77b9c
PZ
7308 /* pipesrc controls the size that is scaled from, which should
7309 * always be the user's requested size.
7310 */
7311 I915_WRITE(PIPESRC(pipe),
6e3c9717
ACO
7312 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7313 (intel_crtc->config->pipe_src_h - 1));
b0e77b9c
PZ
7314}
7315
1bd1bd80 7316static void intel_get_pipe_timings(struct intel_crtc *crtc,
5cec258b 7317 struct intel_crtc_state *pipe_config)
1bd1bd80
DV
7318{
7319 struct drm_device *dev = crtc->base.dev;
7320 struct drm_i915_private *dev_priv = dev->dev_private;
7321 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7322 uint32_t tmp;
7323
7324 tmp = I915_READ(HTOTAL(cpu_transcoder));
2d112de7
ACO
7325 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7326 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7327 tmp = I915_READ(HBLANK(cpu_transcoder));
2d112de7
ACO
7328 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7329 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7330 tmp = I915_READ(HSYNC(cpu_transcoder));
2d112de7
ACO
7331 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7332 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7333
7334 tmp = I915_READ(VTOTAL(cpu_transcoder));
2d112de7
ACO
7335 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7336 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7337 tmp = I915_READ(VBLANK(cpu_transcoder));
2d112de7
ACO
7338 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7339 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80 7340 tmp = I915_READ(VSYNC(cpu_transcoder));
2d112de7
ACO
7341 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7342 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
1bd1bd80
DV
7343
7344 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
2d112de7
ACO
7345 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7346 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7347 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
1bd1bd80
DV
7348 }
7349
7350 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
7351 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7352 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7353
2d112de7
ACO
7354 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7355 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
7356}
7357
f6a83288 7358void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5cec258b 7359 struct intel_crtc_state *pipe_config)
babea61d 7360{
2d112de7
ACO
7361 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7362 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7363 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7364 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
babea61d 7365
2d112de7
ACO
7366 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7367 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7368 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7369 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
babea61d 7370
2d112de7 7371 mode->flags = pipe_config->base.adjusted_mode.flags;
babea61d 7372
2d112de7
ACO
7373 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7374 mode->flags |= pipe_config->base.adjusted_mode.flags;
babea61d
JB
7375}
7376
84b046f3
DV
7377static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7378{
7379 struct drm_device *dev = intel_crtc->base.dev;
7380 struct drm_i915_private *dev_priv = dev->dev_private;
7381 uint32_t pipeconf;
7382
9f11a9e4 7383 pipeconf = 0;
84b046f3 7384
b6b5d049
VS
7385 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7386 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7387 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
67c72a12 7388
6e3c9717 7389 if (intel_crtc->config->double_wide)
cf532bb2 7390 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 7391
ff9ce46e
DV
7392 /* only g4x and later have fancy bpc/dither controls */
7393 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e 7394 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6e3c9717 7395 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
ff9ce46e 7396 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 7397 PIPECONF_DITHER_TYPE_SP;
84b046f3 7398
6e3c9717 7399 switch (intel_crtc->config->pipe_bpp) {
ff9ce46e
DV
7400 case 18:
7401 pipeconf |= PIPECONF_6BPC;
7402 break;
7403 case 24:
7404 pipeconf |= PIPECONF_8BPC;
7405 break;
7406 case 30:
7407 pipeconf |= PIPECONF_10BPC;
7408 break;
7409 default:
7410 /* Case prevented by intel_choose_pipe_bpp_dither. */
7411 BUG();
84b046f3
DV
7412 }
7413 }
7414
7415 if (HAS_PIPE_CXSR(dev)) {
7416 if (intel_crtc->lowfreq_avail) {
7417 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7418 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7419 } else {
7420 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
7421 }
7422 }
7423
6e3c9717 7424 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
efc2cfff 7425 if (INTEL_INFO(dev)->gen < 4 ||
409ee761 7426 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
efc2cfff
VS
7427 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7428 else
7429 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7430 } else
84b046f3
DV
7431 pipeconf |= PIPECONF_PROGRESSIVE;
7432
6e3c9717 7433 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
9f11a9e4 7434 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 7435
84b046f3
DV
7436 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7437 POSTING_READ(PIPECONF(intel_crtc->pipe));
7438}
7439
190f68c5
ACO
7440static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7441 struct intel_crtc_state *crtc_state)
79e53945 7442{
c7653199 7443 struct drm_device *dev = crtc->base.dev;
79e53945 7444 struct drm_i915_private *dev_priv = dev->dev_private;
c751ce4f 7445 int refclk, num_connectors = 0;
652c393a 7446 intel_clock_t clock, reduced_clock;
a16af721 7447 bool ok, has_reduced_clock = false;
e9fd1c02 7448 bool is_lvds = false, is_dsi = false;
5eddb70b 7449 struct intel_encoder *encoder;
d4906093 7450 const intel_limit_t *limit;
55bb9992 7451 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 7452 struct drm_connector *connector;
55bb9992
ACO
7453 struct drm_connector_state *connector_state;
7454 int i;
79e53945 7455
dd3cd74a
ACO
7456 memset(&crtc_state->dpll_hw_state, 0,
7457 sizeof(crtc_state->dpll_hw_state));
7458
da3ced29 7459 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
7460 if (connector_state->crtc != &crtc->base)
7461 continue;
7462
7463 encoder = to_intel_encoder(connector_state->best_encoder);
7464
5eddb70b 7465 switch (encoder->type) {
79e53945
JB
7466 case INTEL_OUTPUT_LVDS:
7467 is_lvds = true;
7468 break;
e9fd1c02
JN
7469 case INTEL_OUTPUT_DSI:
7470 is_dsi = true;
7471 break;
6847d71b
PZ
7472 default:
7473 break;
79e53945 7474 }
43565a06 7475
c751ce4f 7476 num_connectors++;
79e53945
JB
7477 }
7478
f2335330 7479 if (is_dsi)
5b18e57c 7480 return 0;
f2335330 7481
190f68c5 7482 if (!crtc_state->clock_set) {
a93e255f 7483 refclk = i9xx_get_refclk(crtc_state, num_connectors);
79e53945 7484
e9fd1c02
JN
7485 /*
7486 * Returns a set of divisors for the desired target clock with
7487 * the given refclk, or FALSE. The returned values represent
7488 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7489 * 2) / p1 / p2.
7490 */
a93e255f
ACO
7491 limit = intel_limit(crtc_state, refclk);
7492 ok = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 7493 crtc_state->port_clock,
e9fd1c02 7494 refclk, NULL, &clock);
f2335330 7495 if (!ok) {
e9fd1c02
JN
7496 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7497 return -EINVAL;
7498 }
79e53945 7499
f2335330
JN
7500 if (is_lvds && dev_priv->lvds_downclock_avail) {
7501 /*
7502 * Ensure we match the reduced clock's P to the target
7503 * clock. If the clocks don't match, we can't switch
7504 * the display clock by using the FP0/FP1. In such case
7505 * we will disable the LVDS downclock feature.
7506 */
7507 has_reduced_clock =
a93e255f 7508 dev_priv->display.find_dpll(limit, crtc_state,
f2335330
JN
7509 dev_priv->lvds_downclock,
7510 refclk, &clock,
7511 &reduced_clock);
7512 }
7513 /* Compat-code for transition, will disappear. */
190f68c5
ACO
7514 crtc_state->dpll.n = clock.n;
7515 crtc_state->dpll.m1 = clock.m1;
7516 crtc_state->dpll.m2 = clock.m2;
7517 crtc_state->dpll.p1 = clock.p1;
7518 crtc_state->dpll.p2 = clock.p2;
f47709a9 7519 }
7026d4ac 7520
e9fd1c02 7521 if (IS_GEN2(dev)) {
190f68c5 7522 i8xx_update_pll(crtc, crtc_state,
2a8f64ca
VP
7523 has_reduced_clock ? &reduced_clock : NULL,
7524 num_connectors);
9d556c99 7525 } else if (IS_CHERRYVIEW(dev)) {
190f68c5 7526 chv_update_pll(crtc, crtc_state);
e9fd1c02 7527 } else if (IS_VALLEYVIEW(dev)) {
190f68c5 7528 vlv_update_pll(crtc, crtc_state);
e9fd1c02 7529 } else {
190f68c5 7530 i9xx_update_pll(crtc, crtc_state,
eb1cbe48 7531 has_reduced_clock ? &reduced_clock : NULL,
eba905b2 7532 num_connectors);
e9fd1c02 7533 }
79e53945 7534
c8f7a0db 7535 return 0;
f564048e
EA
7536}
7537
2fa2fe9a 7538static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5cec258b 7539 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
7540{
7541 struct drm_device *dev = crtc->base.dev;
7542 struct drm_i915_private *dev_priv = dev->dev_private;
7543 uint32_t tmp;
7544
dc9e7dec
VS
7545 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7546 return;
7547
2fa2fe9a 7548 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
7549 if (!(tmp & PFIT_ENABLE))
7550 return;
2fa2fe9a 7551
06922821 7552 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
7553 if (INTEL_INFO(dev)->gen < 4) {
7554 if (crtc->pipe != PIPE_B)
7555 return;
2fa2fe9a
DV
7556 } else {
7557 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7558 return;
7559 }
7560
06922821 7561 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
7562 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7563 if (INTEL_INFO(dev)->gen < 5)
7564 pipe_config->gmch_pfit.lvds_border_bits =
7565 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7566}
7567
acbec814 7568static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7569 struct intel_crtc_state *pipe_config)
acbec814
JB
7570{
7571 struct drm_device *dev = crtc->base.dev;
7572 struct drm_i915_private *dev_priv = dev->dev_private;
7573 int pipe = pipe_config->cpu_transcoder;
7574 intel_clock_t clock;
7575 u32 mdiv;
662c6ecb 7576 int refclk = 100000;
acbec814 7577
f573de5a
SK
7578 /* In case of MIPI DPLL will not even be used */
7579 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7580 return;
7581
acbec814 7582 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 7583 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
7584 mutex_unlock(&dev_priv->dpio_lock);
7585
7586 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7587 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7588 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7589 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7590 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7591
f646628b 7592 vlv_clock(refclk, &clock);
acbec814 7593
f646628b
VS
7594 /* clock.dot is the fast clock */
7595 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
7596}
7597
5724dbd1
DL
7598static void
7599i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7600 struct intel_initial_plane_config *plane_config)
1ad292b5
JB
7601{
7602 struct drm_device *dev = crtc->base.dev;
7603 struct drm_i915_private *dev_priv = dev->dev_private;
7604 u32 val, base, offset;
7605 int pipe = crtc->pipe, plane = crtc->plane;
7606 int fourcc, pixel_format;
6761dd31 7607 unsigned int aligned_height;
b113d5ee 7608 struct drm_framebuffer *fb;
1b842c89 7609 struct intel_framebuffer *intel_fb;
1ad292b5 7610
42a7b088
DL
7611 val = I915_READ(DSPCNTR(plane));
7612 if (!(val & DISPLAY_PLANE_ENABLE))
7613 return;
7614
d9806c9f 7615 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 7616 if (!intel_fb) {
1ad292b5
JB
7617 DRM_DEBUG_KMS("failed to alloc fb\n");
7618 return;
7619 }
7620
1b842c89
DL
7621 fb = &intel_fb->base;
7622
18c5247e
DV
7623 if (INTEL_INFO(dev)->gen >= 4) {
7624 if (val & DISPPLANE_TILED) {
49af449b 7625 plane_config->tiling = I915_TILING_X;
18c5247e
DV
7626 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7627 }
7628 }
1ad292b5
JB
7629
7630 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 7631 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
7632 fb->pixel_format = fourcc;
7633 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
1ad292b5
JB
7634
7635 if (INTEL_INFO(dev)->gen >= 4) {
49af449b 7636 if (plane_config->tiling)
1ad292b5
JB
7637 offset = I915_READ(DSPTILEOFF(plane));
7638 else
7639 offset = I915_READ(DSPLINOFF(plane));
7640 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7641 } else {
7642 base = I915_READ(DSPADDR(plane));
7643 }
7644 plane_config->base = base;
7645
7646 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
7647 fb->width = ((val >> 16) & 0xfff) + 1;
7648 fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
7649
7650 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 7651 fb->pitches[0] = val & 0xffffffc0;
1ad292b5 7652
b113d5ee 7653 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
7654 fb->pixel_format,
7655 fb->modifier[0]);
1ad292b5 7656
f37b5c2b 7657 plane_config->size = fb->pitches[0] * aligned_height;
1ad292b5 7658
2844a921
DL
7659 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7660 pipe_name(pipe), plane, fb->width, fb->height,
7661 fb->bits_per_pixel, base, fb->pitches[0],
7662 plane_config->size);
1ad292b5 7663
2d14030b 7664 plane_config->fb = intel_fb;
1ad292b5
JB
7665}
7666
70b23a98 7667static void chv_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 7668 struct intel_crtc_state *pipe_config)
70b23a98
VS
7669{
7670 struct drm_device *dev = crtc->base.dev;
7671 struct drm_i915_private *dev_priv = dev->dev_private;
7672 int pipe = pipe_config->cpu_transcoder;
7673 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7674 intel_clock_t clock;
7675 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7676 int refclk = 100000;
7677
7678 mutex_lock(&dev_priv->dpio_lock);
7679 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7680 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7681 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7682 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7683 mutex_unlock(&dev_priv->dpio_lock);
7684
7685 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7686 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7687 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7688 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7689 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7690
7691 chv_clock(refclk, &clock);
7692
7693 /* clock.dot is the fast clock */
7694 pipe_config->port_clock = clock.dot / 5;
7695}
7696
0e8ffe1b 7697static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5cec258b 7698 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
7699{
7700 struct drm_device *dev = crtc->base.dev;
7701 struct drm_i915_private *dev_priv = dev->dev_private;
7702 uint32_t tmp;
7703
f458ebbc
DV
7704 if (!intel_display_power_is_enabled(dev_priv,
7705 POWER_DOMAIN_PIPE(crtc->pipe)))
b5482bd0
ID
7706 return false;
7707
e143a21c 7708 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 7709 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 7710
0e8ffe1b
DV
7711 tmp = I915_READ(PIPECONF(crtc->pipe));
7712 if (!(tmp & PIPECONF_ENABLE))
7713 return false;
7714
42571aef
VS
7715 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7716 switch (tmp & PIPECONF_BPC_MASK) {
7717 case PIPECONF_6BPC:
7718 pipe_config->pipe_bpp = 18;
7719 break;
7720 case PIPECONF_8BPC:
7721 pipe_config->pipe_bpp = 24;
7722 break;
7723 case PIPECONF_10BPC:
7724 pipe_config->pipe_bpp = 30;
7725 break;
7726 default:
7727 break;
7728 }
7729 }
7730
b5a9fa09
DV
7731 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7732 pipe_config->limited_color_range = true;
7733
282740f7
VS
7734 if (INTEL_INFO(dev)->gen < 4)
7735 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7736
1bd1bd80
DV
7737 intel_get_pipe_timings(crtc, pipe_config);
7738
2fa2fe9a
DV
7739 i9xx_get_pfit_config(crtc, pipe_config);
7740
6c49f241
DV
7741 if (INTEL_INFO(dev)->gen >= 4) {
7742 tmp = I915_READ(DPLL_MD(crtc->pipe));
7743 pipe_config->pixel_multiplier =
7744 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7745 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 7746 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
7747 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7748 tmp = I915_READ(DPLL(crtc->pipe));
7749 pipe_config->pixel_multiplier =
7750 ((tmp & SDVO_MULTIPLIER_MASK)
7751 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7752 } else {
7753 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7754 * port and will be fixed up in the encoder->get_config
7755 * function. */
7756 pipe_config->pixel_multiplier = 1;
7757 }
8bcc2795
DV
7758 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7759 if (!IS_VALLEYVIEW(dev)) {
1c4e0274
VS
7760 /*
7761 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7762 * on 830. Filter it out here so that we don't
7763 * report errors due to that.
7764 */
7765 if (IS_I830(dev))
7766 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7767
8bcc2795
DV
7768 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7769 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
7770 } else {
7771 /* Mask out read-only status bits. */
7772 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7773 DPLL_PORTC_READY_MASK |
7774 DPLL_PORTB_READY_MASK);
8bcc2795 7775 }
6c49f241 7776
70b23a98
VS
7777 if (IS_CHERRYVIEW(dev))
7778 chv_crtc_clock_get(crtc, pipe_config);
7779 else if (IS_VALLEYVIEW(dev))
acbec814
JB
7780 vlv_crtc_clock_get(crtc, pipe_config);
7781 else
7782 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 7783
0e8ffe1b
DV
7784 return true;
7785}
7786
dde86e2d 7787static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
7788{
7789 struct drm_i915_private *dev_priv = dev->dev_private;
13d83a67 7790 struct intel_encoder *encoder;
74cfd7ac 7791 u32 val, final;
13d83a67 7792 bool has_lvds = false;
199e5d79 7793 bool has_cpu_edp = false;
199e5d79 7794 bool has_panel = false;
99eb6a01
KP
7795 bool has_ck505 = false;
7796 bool can_ssc = false;
13d83a67
JB
7797
7798 /* We need to take the global config into account */
b2784e15 7799 for_each_intel_encoder(dev, encoder) {
199e5d79
KP
7800 switch (encoder->type) {
7801 case INTEL_OUTPUT_LVDS:
7802 has_panel = true;
7803 has_lvds = true;
7804 break;
7805 case INTEL_OUTPUT_EDP:
7806 has_panel = true;
2de6905f 7807 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
7808 has_cpu_edp = true;
7809 break;
6847d71b
PZ
7810 default:
7811 break;
13d83a67
JB
7812 }
7813 }
7814
99eb6a01 7815 if (HAS_PCH_IBX(dev)) {
41aa3448 7816 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
7817 can_ssc = has_ck505;
7818 } else {
7819 has_ck505 = false;
7820 can_ssc = true;
7821 }
7822
2de6905f
ID
7823 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7824 has_panel, has_lvds, has_ck505);
13d83a67
JB
7825
7826 /* Ironlake: try to setup display ref clock before DPLL
7827 * enabling. This is only under driver's control after
7828 * PCH B stepping, previous chipset stepping should be
7829 * ignoring this setting.
7830 */
74cfd7ac
CW
7831 val = I915_READ(PCH_DREF_CONTROL);
7832
7833 /* As we must carefully and slowly disable/enable each source in turn,
7834 * compute the final state we want first and check if we need to
7835 * make any changes at all.
7836 */
7837 final = val;
7838 final &= ~DREF_NONSPREAD_SOURCE_MASK;
7839 if (has_ck505)
7840 final |= DREF_NONSPREAD_CK505_ENABLE;
7841 else
7842 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7843
7844 final &= ~DREF_SSC_SOURCE_MASK;
7845 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7846 final &= ~DREF_SSC1_ENABLE;
7847
7848 if (has_panel) {
7849 final |= DREF_SSC_SOURCE_ENABLE;
7850
7851 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7852 final |= DREF_SSC1_ENABLE;
7853
7854 if (has_cpu_edp) {
7855 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7856 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7857 else
7858 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7859 } else
7860 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7861 } else {
7862 final |= DREF_SSC_SOURCE_DISABLE;
7863 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7864 }
7865
7866 if (final == val)
7867 return;
7868
13d83a67 7869 /* Always enable nonspread source */
74cfd7ac 7870 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 7871
99eb6a01 7872 if (has_ck505)
74cfd7ac 7873 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 7874 else
74cfd7ac 7875 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 7876
199e5d79 7877 if (has_panel) {
74cfd7ac
CW
7878 val &= ~DREF_SSC_SOURCE_MASK;
7879 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 7880
199e5d79 7881 /* SSC must be turned on before enabling the CPU output */
99eb6a01 7882 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7883 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 7884 val |= DREF_SSC1_ENABLE;
e77166b5 7885 } else
74cfd7ac 7886 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
7887
7888 /* Get SSC going before enabling the outputs */
74cfd7ac 7889 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7890 POSTING_READ(PCH_DREF_CONTROL);
7891 udelay(200);
7892
74cfd7ac 7893 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
7894
7895 /* Enable CPU source on CPU attached eDP */
199e5d79 7896 if (has_cpu_edp) {
99eb6a01 7897 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 7898 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 7899 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
eba905b2 7900 } else
74cfd7ac 7901 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 7902 } else
74cfd7ac 7903 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7904
74cfd7ac 7905 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7906 POSTING_READ(PCH_DREF_CONTROL);
7907 udelay(200);
7908 } else {
7909 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7910
74cfd7ac 7911 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
7912
7913 /* Turn off CPU output */
74cfd7ac 7914 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 7915
74cfd7ac 7916 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
7917 POSTING_READ(PCH_DREF_CONTROL);
7918 udelay(200);
7919
7920 /* Turn off the SSC source */
74cfd7ac
CW
7921 val &= ~DREF_SSC_SOURCE_MASK;
7922 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
7923
7924 /* Turn off SSC1 */
74cfd7ac 7925 val &= ~DREF_SSC1_ENABLE;
199e5d79 7926
74cfd7ac 7927 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
7928 POSTING_READ(PCH_DREF_CONTROL);
7929 udelay(200);
7930 }
74cfd7ac
CW
7931
7932 BUG_ON(val != final);
13d83a67
JB
7933}
7934
f31f2d55 7935static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 7936{
f31f2d55 7937 uint32_t tmp;
dde86e2d 7938
0ff066a9
PZ
7939 tmp = I915_READ(SOUTH_CHICKEN2);
7940 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7941 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7942
0ff066a9
PZ
7943 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7944 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7945 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 7946
0ff066a9
PZ
7947 tmp = I915_READ(SOUTH_CHICKEN2);
7948 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7949 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 7950
0ff066a9
PZ
7951 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7952 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7953 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
7954}
7955
7956/* WaMPhyProgramming:hsw */
7957static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7958{
7959 uint32_t tmp;
dde86e2d
PZ
7960
7961 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7962 tmp &= ~(0xFF << 24);
7963 tmp |= (0x12 << 24);
7964 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7965
dde86e2d
PZ
7966 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7967 tmp |= (1 << 11);
7968 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7969
7970 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7971 tmp |= (1 << 11);
7972 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7973
dde86e2d
PZ
7974 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7975 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7976 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7977
7978 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7979 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7980 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7981
0ff066a9
PZ
7982 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7983 tmp &= ~(7 << 13);
7984 tmp |= (5 << 13);
7985 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 7986
0ff066a9
PZ
7987 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7988 tmp &= ~(7 << 13);
7989 tmp |= (5 << 13);
7990 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
7991
7992 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7993 tmp &= ~0xFF;
7994 tmp |= 0x1C;
7995 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7996
7997 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7998 tmp &= ~0xFF;
7999 tmp |= 0x1C;
8000 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8001
8002 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8003 tmp &= ~(0xFF << 16);
8004 tmp |= (0x1C << 16);
8005 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8006
8007 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8008 tmp &= ~(0xFF << 16);
8009 tmp |= (0x1C << 16);
8010 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8011
0ff066a9
PZ
8012 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8013 tmp |= (1 << 27);
8014 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 8015
0ff066a9
PZ
8016 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8017 tmp |= (1 << 27);
8018 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 8019
0ff066a9
PZ
8020 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8021 tmp &= ~(0xF << 28);
8022 tmp |= (4 << 28);
8023 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 8024
0ff066a9
PZ
8025 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8026 tmp &= ~(0xF << 28);
8027 tmp |= (4 << 28);
8028 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
8029}
8030
2fa86a1f
PZ
8031/* Implements 3 different sequences from BSpec chapter "Display iCLK
8032 * Programming" based on the parameters passed:
8033 * - Sequence to enable CLKOUT_DP
8034 * - Sequence to enable CLKOUT_DP without spread
8035 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8036 */
8037static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8038 bool with_fdi)
f31f2d55
PZ
8039{
8040 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
8041 uint32_t reg, tmp;
8042
8043 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8044 with_spread = true;
8045 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8046 with_fdi, "LP PCH doesn't have FDI\n"))
8047 with_fdi = false;
f31f2d55
PZ
8048
8049 mutex_lock(&dev_priv->dpio_lock);
8050
8051 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8052 tmp &= ~SBI_SSCCTL_DISABLE;
8053 tmp |= SBI_SSCCTL_PATHALT;
8054 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8055
8056 udelay(24);
8057
2fa86a1f
PZ
8058 if (with_spread) {
8059 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8060 tmp &= ~SBI_SSCCTL_PATHALT;
8061 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 8062
2fa86a1f
PZ
8063 if (with_fdi) {
8064 lpt_reset_fdi_mphy(dev_priv);
8065 lpt_program_fdi_mphy(dev_priv);
8066 }
8067 }
dde86e2d 8068
2fa86a1f
PZ
8069 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8070 SBI_GEN0 : SBI_DBUFF0;
8071 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8072 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8073 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
8074
8075 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
8076}
8077
47701c3b
PZ
8078/* Sequence to disable CLKOUT_DP */
8079static void lpt_disable_clkout_dp(struct drm_device *dev)
8080{
8081 struct drm_i915_private *dev_priv = dev->dev_private;
8082 uint32_t reg, tmp;
8083
8084 mutex_lock(&dev_priv->dpio_lock);
8085
8086 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8087 SBI_GEN0 : SBI_DBUFF0;
8088 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8089 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8090 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8091
8092 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8093 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8094 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8095 tmp |= SBI_SSCCTL_PATHALT;
8096 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8097 udelay(32);
8098 }
8099 tmp |= SBI_SSCCTL_DISABLE;
8100 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8101 }
8102
8103 mutex_unlock(&dev_priv->dpio_lock);
8104}
8105
bf8fa3d3
PZ
8106static void lpt_init_pch_refclk(struct drm_device *dev)
8107{
bf8fa3d3
PZ
8108 struct intel_encoder *encoder;
8109 bool has_vga = false;
8110
b2784e15 8111 for_each_intel_encoder(dev, encoder) {
bf8fa3d3
PZ
8112 switch (encoder->type) {
8113 case INTEL_OUTPUT_ANALOG:
8114 has_vga = true;
8115 break;
6847d71b
PZ
8116 default:
8117 break;
bf8fa3d3
PZ
8118 }
8119 }
8120
47701c3b
PZ
8121 if (has_vga)
8122 lpt_enable_clkout_dp(dev, true, true);
8123 else
8124 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
8125}
8126
dde86e2d
PZ
8127/*
8128 * Initialize reference clocks when the driver loads
8129 */
8130void intel_init_pch_refclk(struct drm_device *dev)
8131{
8132 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8133 ironlake_init_pch_refclk(dev);
8134 else if (HAS_PCH_LPT(dev))
8135 lpt_init_pch_refclk(dev);
8136}
8137
55bb9992 8138static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
d9d444cb 8139{
55bb9992 8140 struct drm_device *dev = crtc_state->base.crtc->dev;
d9d444cb 8141 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8142 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8143 struct drm_connector *connector;
55bb9992 8144 struct drm_connector_state *connector_state;
d9d444cb 8145 struct intel_encoder *encoder;
55bb9992 8146 int num_connectors = 0, i;
d9d444cb
JB
8147 bool is_lvds = false;
8148
da3ced29 8149 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8150 if (connector_state->crtc != crtc_state->base.crtc)
8151 continue;
8152
8153 encoder = to_intel_encoder(connector_state->best_encoder);
8154
d9d444cb
JB
8155 switch (encoder->type) {
8156 case INTEL_OUTPUT_LVDS:
8157 is_lvds = true;
8158 break;
6847d71b
PZ
8159 default:
8160 break;
d9d444cb
JB
8161 }
8162 num_connectors++;
8163 }
8164
8165 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 8166 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 8167 dev_priv->vbt.lvds_ssc_freq);
e91e941b 8168 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
8169 }
8170
8171 return 120000;
8172}
8173
6ff93609 8174static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 8175{
c8203565 8176 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
8177 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8178 int pipe = intel_crtc->pipe;
c8203565
PZ
8179 uint32_t val;
8180
78114071 8181 val = 0;
c8203565 8182
6e3c9717 8183 switch (intel_crtc->config->pipe_bpp) {
c8203565 8184 case 18:
dfd07d72 8185 val |= PIPECONF_6BPC;
c8203565
PZ
8186 break;
8187 case 24:
dfd07d72 8188 val |= PIPECONF_8BPC;
c8203565
PZ
8189 break;
8190 case 30:
dfd07d72 8191 val |= PIPECONF_10BPC;
c8203565
PZ
8192 break;
8193 case 36:
dfd07d72 8194 val |= PIPECONF_12BPC;
c8203565
PZ
8195 break;
8196 default:
cc769b62
PZ
8197 /* Case prevented by intel_choose_pipe_bpp_dither. */
8198 BUG();
c8203565
PZ
8199 }
8200
6e3c9717 8201 if (intel_crtc->config->dither)
c8203565
PZ
8202 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8203
6e3c9717 8204 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
8205 val |= PIPECONF_INTERLACED_ILK;
8206 else
8207 val |= PIPECONF_PROGRESSIVE;
8208
6e3c9717 8209 if (intel_crtc->config->limited_color_range)
3685a8f3 8210 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 8211
c8203565
PZ
8212 I915_WRITE(PIPECONF(pipe), val);
8213 POSTING_READ(PIPECONF(pipe));
8214}
8215
86d3efce
VS
8216/*
8217 * Set up the pipe CSC unit.
8218 *
8219 * Currently only full range RGB to limited range RGB conversion
8220 * is supported, but eventually this should handle various
8221 * RGB<->YCbCr scenarios as well.
8222 */
50f3b016 8223static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
8224{
8225 struct drm_device *dev = crtc->dev;
8226 struct drm_i915_private *dev_priv = dev->dev_private;
8227 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8228 int pipe = intel_crtc->pipe;
8229 uint16_t coeff = 0x7800; /* 1.0 */
8230
8231 /*
8232 * TODO: Check what kind of values actually come out of the pipe
8233 * with these coeff/postoff values and adjust to get the best
8234 * accuracy. Perhaps we even need to take the bpc value into
8235 * consideration.
8236 */
8237
6e3c9717 8238 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8239 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8240
8241 /*
8242 * GY/GU and RY/RU should be the other way around according
8243 * to BSpec, but reality doesn't agree. Just set them up in
8244 * a way that results in the correct picture.
8245 */
8246 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8247 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8248
8249 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8250 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8251
8252 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8253 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8254
8255 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8256 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8257 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8258
8259 if (INTEL_INFO(dev)->gen > 6) {
8260 uint16_t postoff = 0;
8261
6e3c9717 8262 if (intel_crtc->config->limited_color_range)
32cf0cb0 8263 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
8264
8265 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8266 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8267 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8268
8269 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8270 } else {
8271 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8272
6e3c9717 8273 if (intel_crtc->config->limited_color_range)
86d3efce
VS
8274 mode |= CSC_BLACK_SCREEN_OFFSET;
8275
8276 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8277 }
8278}
8279
6ff93609 8280static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 8281{
756f85cf
PZ
8282 struct drm_device *dev = crtc->dev;
8283 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 8284 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 8285 enum pipe pipe = intel_crtc->pipe;
6e3c9717 8286 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
ee2b0b38
PZ
8287 uint32_t val;
8288
3eff4faa 8289 val = 0;
ee2b0b38 8290
6e3c9717 8291 if (IS_HASWELL(dev) && intel_crtc->config->dither)
ee2b0b38
PZ
8292 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8293
6e3c9717 8294 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
8295 val |= PIPECONF_INTERLACED_ILK;
8296 else
8297 val |= PIPECONF_PROGRESSIVE;
8298
702e7a56
PZ
8299 I915_WRITE(PIPECONF(cpu_transcoder), val);
8300 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
8301
8302 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8303 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf 8304
3cdf122c 8305 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
756f85cf
PZ
8306 val = 0;
8307
6e3c9717 8308 switch (intel_crtc->config->pipe_bpp) {
756f85cf
PZ
8309 case 18:
8310 val |= PIPEMISC_DITHER_6_BPC;
8311 break;
8312 case 24:
8313 val |= PIPEMISC_DITHER_8_BPC;
8314 break;
8315 case 30:
8316 val |= PIPEMISC_DITHER_10_BPC;
8317 break;
8318 case 36:
8319 val |= PIPEMISC_DITHER_12_BPC;
8320 break;
8321 default:
8322 /* Case prevented by pipe_config_set_bpp. */
8323 BUG();
8324 }
8325
6e3c9717 8326 if (intel_crtc->config->dither)
756f85cf
PZ
8327 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8328
8329 I915_WRITE(PIPEMISC(pipe), val);
8330 }
ee2b0b38
PZ
8331}
8332
6591c6e4 8333static bool ironlake_compute_clocks(struct drm_crtc *crtc,
190f68c5 8334 struct intel_crtc_state *crtc_state,
6591c6e4
PZ
8335 intel_clock_t *clock,
8336 bool *has_reduced_clock,
8337 intel_clock_t *reduced_clock)
8338{
8339 struct drm_device *dev = crtc->dev;
8340 struct drm_i915_private *dev_priv = dev->dev_private;
6591c6e4 8341 int refclk;
d4906093 8342 const intel_limit_t *limit;
a16af721 8343 bool ret, is_lvds = false;
79e53945 8344
a93e255f 8345 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
79e53945 8346
55bb9992 8347 refclk = ironlake_get_refclk(crtc_state);
79e53945 8348
d4906093
ML
8349 /*
8350 * Returns a set of divisors for the desired target clock with the given
8351 * refclk, or FALSE. The returned values represent the clock equation:
8352 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8353 */
a93e255f
ACO
8354 limit = intel_limit(crtc_state, refclk);
8355 ret = dev_priv->display.find_dpll(limit, crtc_state,
190f68c5 8356 crtc_state->port_clock,
ee9300bb 8357 refclk, NULL, clock);
6591c6e4
PZ
8358 if (!ret)
8359 return false;
cda4b7d3 8360
ddc9003c 8361 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
8362 /*
8363 * Ensure we match the reduced clock's P to the target clock.
8364 * If the clocks don't match, we can't switch the display clock
8365 * by using the FP0/FP1. In such case we will disable the LVDS
8366 * downclock feature.
8367 */
ee9300bb 8368 *has_reduced_clock =
a93e255f 8369 dev_priv->display.find_dpll(limit, crtc_state,
ee9300bb
DV
8370 dev_priv->lvds_downclock,
8371 refclk, clock,
8372 reduced_clock);
652c393a 8373 }
61e9653f 8374
6591c6e4
PZ
8375 return true;
8376}
8377
d4b1931c
PZ
8378int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8379{
8380 /*
8381 * Account for spread spectrum to avoid
8382 * oversubscribing the link. Max center spread
8383 * is 2.5%; use 5% for safety's sake.
8384 */
8385 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 8386 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
8387}
8388
7429e9d4 8389static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 8390{
7429e9d4 8391 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
8392}
8393
de13a2e3 8394static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
190f68c5 8395 struct intel_crtc_state *crtc_state,
7429e9d4 8396 u32 *fp,
9a7c7890 8397 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 8398{
de13a2e3 8399 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
8400 struct drm_device *dev = crtc->dev;
8401 struct drm_i915_private *dev_priv = dev->dev_private;
55bb9992 8402 struct drm_atomic_state *state = crtc_state->base.state;
da3ced29 8403 struct drm_connector *connector;
55bb9992
ACO
8404 struct drm_connector_state *connector_state;
8405 struct intel_encoder *encoder;
de13a2e3 8406 uint32_t dpll;
55bb9992 8407 int factor, num_connectors = 0, i;
09ede541 8408 bool is_lvds = false, is_sdvo = false;
79e53945 8409
da3ced29 8410 for_each_connector_in_state(state, connector, connector_state, i) {
55bb9992
ACO
8411 if (connector_state->crtc != crtc_state->base.crtc)
8412 continue;
8413
8414 encoder = to_intel_encoder(connector_state->best_encoder);
8415
8416 switch (encoder->type) {
79e53945
JB
8417 case INTEL_OUTPUT_LVDS:
8418 is_lvds = true;
8419 break;
8420 case INTEL_OUTPUT_SDVO:
7d57382e 8421 case INTEL_OUTPUT_HDMI:
79e53945 8422 is_sdvo = true;
79e53945 8423 break;
6847d71b
PZ
8424 default:
8425 break;
79e53945 8426 }
43565a06 8427
c751ce4f 8428 num_connectors++;
79e53945 8429 }
79e53945 8430
c1858123 8431 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
8432 factor = 21;
8433 if (is_lvds) {
8434 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 8435 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 8436 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 8437 factor = 25;
190f68c5 8438 } else if (crtc_state->sdvo_tv_clock)
8febb297 8439 factor = 20;
c1858123 8440
190f68c5 8441 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
7d0ac5b7 8442 *fp |= FP_CB_TUNE;
2c07245f 8443
9a7c7890
DV
8444 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8445 *fp2 |= FP_CB_TUNE;
8446
5eddb70b 8447 dpll = 0;
2c07245f 8448
a07d6787
EA
8449 if (is_lvds)
8450 dpll |= DPLLB_MODE_LVDS;
8451 else
8452 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 8453
190f68c5 8454 dpll |= (crtc_state->pixel_multiplier - 1)
ef1b460d 8455 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
8456
8457 if (is_sdvo)
4a33e48d 8458 dpll |= DPLL_SDVO_HIGH_SPEED;
190f68c5 8459 if (crtc_state->has_dp_encoder)
4a33e48d 8460 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 8461
a07d6787 8462 /* compute bitmask from p1 value */
190f68c5 8463 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 8464 /* also FPA1 */
190f68c5 8465 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 8466
190f68c5 8467 switch (crtc_state->dpll.p2) {
a07d6787
EA
8468 case 5:
8469 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8470 break;
8471 case 7:
8472 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8473 break;
8474 case 10:
8475 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8476 break;
8477 case 14:
8478 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8479 break;
79e53945
JB
8480 }
8481
b4c09f3b 8482 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 8483 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
8484 else
8485 dpll |= PLL_REF_INPUT_DREFCLK;
8486
959e16d6 8487 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
8488}
8489
190f68c5
ACO
8490static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8491 struct intel_crtc_state *crtc_state)
de13a2e3 8492{
c7653199 8493 struct drm_device *dev = crtc->base.dev;
de13a2e3 8494 intel_clock_t clock, reduced_clock;
cbbab5bd 8495 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 8496 bool ok, has_reduced_clock = false;
8b47047b 8497 bool is_lvds = false;
e2b78267 8498 struct intel_shared_dpll *pll;
de13a2e3 8499
dd3cd74a
ACO
8500 memset(&crtc_state->dpll_hw_state, 0,
8501 sizeof(crtc_state->dpll_hw_state));
8502
409ee761 8503 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
79e53945 8504
5dc5298b
PZ
8505 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8506 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 8507
190f68c5 8508 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
de13a2e3 8509 &has_reduced_clock, &reduced_clock);
190f68c5 8510 if (!ok && !crtc_state->clock_set) {
de13a2e3
PZ
8511 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8512 return -EINVAL;
79e53945 8513 }
f47709a9 8514 /* Compat-code for transition, will disappear. */
190f68c5
ACO
8515 if (!crtc_state->clock_set) {
8516 crtc_state->dpll.n = clock.n;
8517 crtc_state->dpll.m1 = clock.m1;
8518 crtc_state->dpll.m2 = clock.m2;
8519 crtc_state->dpll.p1 = clock.p1;
8520 crtc_state->dpll.p2 = clock.p2;
f47709a9 8521 }
79e53945 8522
5dc5298b 8523 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
190f68c5
ACO
8524 if (crtc_state->has_pch_encoder) {
8525 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
cbbab5bd 8526 if (has_reduced_clock)
7429e9d4 8527 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 8528
190f68c5 8529 dpll = ironlake_compute_dpll(crtc, crtc_state,
cbbab5bd
DV
8530 &fp, &reduced_clock,
8531 has_reduced_clock ? &fp2 : NULL);
8532
190f68c5
ACO
8533 crtc_state->dpll_hw_state.dpll = dpll;
8534 crtc_state->dpll_hw_state.fp0 = fp;
66e985c0 8535 if (has_reduced_clock)
190f68c5 8536 crtc_state->dpll_hw_state.fp1 = fp2;
66e985c0 8537 else
190f68c5 8538 crtc_state->dpll_hw_state.fp1 = fp;
66e985c0 8539
190f68c5 8540 pll = intel_get_shared_dpll(crtc, crtc_state);
ee7b9f93 8541 if (pll == NULL) {
84f44ce7 8542 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
c7653199 8543 pipe_name(crtc->pipe));
4b645f14
JB
8544 return -EINVAL;
8545 }
3fb37703 8546 }
79e53945 8547
ab585dea 8548 if (is_lvds && has_reduced_clock)
c7653199 8549 crtc->lowfreq_avail = true;
bcd644e0 8550 else
c7653199 8551 crtc->lowfreq_avail = false;
e2b78267 8552
c8f7a0db 8553 return 0;
79e53945
JB
8554}
8555
eb14cb74
VS
8556static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8557 struct intel_link_m_n *m_n)
8558{
8559 struct drm_device *dev = crtc->base.dev;
8560 struct drm_i915_private *dev_priv = dev->dev_private;
8561 enum pipe pipe = crtc->pipe;
8562
8563 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8564 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8565 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8566 & ~TU_SIZE_MASK;
8567 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8568 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8569 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8570}
8571
8572static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8573 enum transcoder transcoder,
b95af8be
VK
8574 struct intel_link_m_n *m_n,
8575 struct intel_link_m_n *m2_n2)
72419203
DV
8576{
8577 struct drm_device *dev = crtc->base.dev;
8578 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 8579 enum pipe pipe = crtc->pipe;
72419203 8580
eb14cb74
VS
8581 if (INTEL_INFO(dev)->gen >= 5) {
8582 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8583 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8584 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8585 & ~TU_SIZE_MASK;
8586 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8587 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8588 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
b95af8be
VK
8589 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8590 * gen < 8) and if DRRS is supported (to make sure the
8591 * registers are not unnecessarily read).
8592 */
8593 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
6e3c9717 8594 crtc->config->has_drrs) {
b95af8be
VK
8595 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8596 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8597 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8598 & ~TU_SIZE_MASK;
8599 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8600 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8601 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8602 }
eb14cb74
VS
8603 } else {
8604 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8605 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8606 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8607 & ~TU_SIZE_MASK;
8608 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8609 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8610 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8611 }
8612}
8613
8614void intel_dp_get_m_n(struct intel_crtc *crtc,
5cec258b 8615 struct intel_crtc_state *pipe_config)
eb14cb74 8616{
681a8504 8617 if (pipe_config->has_pch_encoder)
eb14cb74
VS
8618 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8619 else
8620 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be
VK
8621 &pipe_config->dp_m_n,
8622 &pipe_config->dp_m2_n2);
eb14cb74 8623}
72419203 8624
eb14cb74 8625static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
5cec258b 8626 struct intel_crtc_state *pipe_config)
eb14cb74
VS
8627{
8628 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
b95af8be 8629 &pipe_config->fdi_m_n, NULL);
72419203
DV
8630}
8631
bd2e244f 8632static void skylake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8633 struct intel_crtc_state *pipe_config)
bd2e244f
JB
8634{
8635 struct drm_device *dev = crtc->base.dev;
8636 struct drm_i915_private *dev_priv = dev->dev_private;
a1b2278e
CK
8637 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8638 uint32_t ps_ctrl = 0;
8639 int id = -1;
8640 int i;
bd2e244f 8641
a1b2278e
CK
8642 /* find scaler attached to this pipe */
8643 for (i = 0; i < crtc->num_scalers; i++) {
8644 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8645 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8646 id = i;
8647 pipe_config->pch_pfit.enabled = true;
8648 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8649 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8650 break;
8651 }
8652 }
bd2e244f 8653
a1b2278e
CK
8654 scaler_state->scaler_id = id;
8655 if (id >= 0) {
8656 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8657 } else {
8658 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
bd2e244f
JB
8659 }
8660}
8661
5724dbd1
DL
8662static void
8663skylake_get_initial_plane_config(struct intel_crtc *crtc,
8664 struct intel_initial_plane_config *plane_config)
bc8d7dff
DL
8665{
8666 struct drm_device *dev = crtc->base.dev;
8667 struct drm_i915_private *dev_priv = dev->dev_private;
40f46283 8668 u32 val, base, offset, stride_mult, tiling;
bc8d7dff
DL
8669 int pipe = crtc->pipe;
8670 int fourcc, pixel_format;
6761dd31 8671 unsigned int aligned_height;
bc8d7dff 8672 struct drm_framebuffer *fb;
1b842c89 8673 struct intel_framebuffer *intel_fb;
bc8d7dff 8674
d9806c9f 8675 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8676 if (!intel_fb) {
bc8d7dff
DL
8677 DRM_DEBUG_KMS("failed to alloc fb\n");
8678 return;
8679 }
8680
1b842c89
DL
8681 fb = &intel_fb->base;
8682
bc8d7dff 8683 val = I915_READ(PLANE_CTL(pipe, 0));
42a7b088
DL
8684 if (!(val & PLANE_CTL_ENABLE))
8685 goto error;
8686
bc8d7dff
DL
8687 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8688 fourcc = skl_format_to_fourcc(pixel_format,
8689 val & PLANE_CTL_ORDER_RGBX,
8690 val & PLANE_CTL_ALPHA_MASK);
8691 fb->pixel_format = fourcc;
8692 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8693
40f46283
DL
8694 tiling = val & PLANE_CTL_TILED_MASK;
8695 switch (tiling) {
8696 case PLANE_CTL_TILED_LINEAR:
8697 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8698 break;
8699 case PLANE_CTL_TILED_X:
8700 plane_config->tiling = I915_TILING_X;
8701 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8702 break;
8703 case PLANE_CTL_TILED_Y:
8704 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8705 break;
8706 case PLANE_CTL_TILED_YF:
8707 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8708 break;
8709 default:
8710 MISSING_CASE(tiling);
8711 goto error;
8712 }
8713
bc8d7dff
DL
8714 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8715 plane_config->base = base;
8716
8717 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8718
8719 val = I915_READ(PLANE_SIZE(pipe, 0));
8720 fb->height = ((val >> 16) & 0xfff) + 1;
8721 fb->width = ((val >> 0) & 0x1fff) + 1;
8722
8723 val = I915_READ(PLANE_STRIDE(pipe, 0));
40f46283
DL
8724 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8725 fb->pixel_format);
bc8d7dff
DL
8726 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8727
8728 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8729 fb->pixel_format,
8730 fb->modifier[0]);
bc8d7dff 8731
f37b5c2b 8732 plane_config->size = fb->pitches[0] * aligned_height;
bc8d7dff
DL
8733
8734 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8735 pipe_name(pipe), fb->width, fb->height,
8736 fb->bits_per_pixel, base, fb->pitches[0],
8737 plane_config->size);
8738
2d14030b 8739 plane_config->fb = intel_fb;
bc8d7dff
DL
8740 return;
8741
8742error:
8743 kfree(fb);
8744}
8745
2fa2fe9a 8746static void ironlake_get_pfit_config(struct intel_crtc *crtc,
5cec258b 8747 struct intel_crtc_state *pipe_config)
2fa2fe9a
DV
8748{
8749 struct drm_device *dev = crtc->base.dev;
8750 struct drm_i915_private *dev_priv = dev->dev_private;
8751 uint32_t tmp;
8752
8753 tmp = I915_READ(PF_CTL(crtc->pipe));
8754
8755 if (tmp & PF_ENABLE) {
fd4daa9c 8756 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
8757 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8758 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
8759
8760 /* We currently do not free assignements of panel fitters on
8761 * ivb/hsw (since we don't use the higher upscaling modes which
8762 * differentiates them) so just WARN about this case for now. */
8763 if (IS_GEN7(dev)) {
8764 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8765 PF_PIPE_SEL_IVB(crtc->pipe));
8766 }
2fa2fe9a 8767 }
79e53945
JB
8768}
8769
5724dbd1
DL
8770static void
8771ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8772 struct intel_initial_plane_config *plane_config)
4c6baa59
JB
8773{
8774 struct drm_device *dev = crtc->base.dev;
8775 struct drm_i915_private *dev_priv = dev->dev_private;
8776 u32 val, base, offset;
aeee5a49 8777 int pipe = crtc->pipe;
4c6baa59 8778 int fourcc, pixel_format;
6761dd31 8779 unsigned int aligned_height;
b113d5ee 8780 struct drm_framebuffer *fb;
1b842c89 8781 struct intel_framebuffer *intel_fb;
4c6baa59 8782
42a7b088
DL
8783 val = I915_READ(DSPCNTR(pipe));
8784 if (!(val & DISPLAY_PLANE_ENABLE))
8785 return;
8786
d9806c9f 8787 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
1b842c89 8788 if (!intel_fb) {
4c6baa59
JB
8789 DRM_DEBUG_KMS("failed to alloc fb\n");
8790 return;
8791 }
8792
1b842c89
DL
8793 fb = &intel_fb->base;
8794
18c5247e
DV
8795 if (INTEL_INFO(dev)->gen >= 4) {
8796 if (val & DISPPLANE_TILED) {
49af449b 8797 plane_config->tiling = I915_TILING_X;
18c5247e
DV
8798 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8799 }
8800 }
4c6baa59
JB
8801
8802 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
b35d63fa 8803 fourcc = i9xx_format_to_fourcc(pixel_format);
b113d5ee
DL
8804 fb->pixel_format = fourcc;
8805 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
4c6baa59 8806
aeee5a49 8807 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
4c6baa59 8808 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
aeee5a49 8809 offset = I915_READ(DSPOFFSET(pipe));
4c6baa59 8810 } else {
49af449b 8811 if (plane_config->tiling)
aeee5a49 8812 offset = I915_READ(DSPTILEOFF(pipe));
4c6baa59 8813 else
aeee5a49 8814 offset = I915_READ(DSPLINOFF(pipe));
4c6baa59
JB
8815 }
8816 plane_config->base = base;
8817
8818 val = I915_READ(PIPESRC(pipe));
b113d5ee
DL
8819 fb->width = ((val >> 16) & 0xfff) + 1;
8820 fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
8821
8822 val = I915_READ(DSPSTRIDE(pipe));
b113d5ee 8823 fb->pitches[0] = val & 0xffffffc0;
4c6baa59 8824
b113d5ee 8825 aligned_height = intel_fb_align_height(dev, fb->height,
091df6cb
DV
8826 fb->pixel_format,
8827 fb->modifier[0]);
4c6baa59 8828
f37b5c2b 8829 plane_config->size = fb->pitches[0] * aligned_height;
4c6baa59 8830
2844a921
DL
8831 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8832 pipe_name(pipe), fb->width, fb->height,
8833 fb->bits_per_pixel, base, fb->pitches[0],
8834 plane_config->size);
b113d5ee 8835
2d14030b 8836 plane_config->fb = intel_fb;
4c6baa59
JB
8837}
8838
0e8ffe1b 8839static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
5cec258b 8840 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
8841{
8842 struct drm_device *dev = crtc->base.dev;
8843 struct drm_i915_private *dev_priv = dev->dev_private;
8844 uint32_t tmp;
8845
f458ebbc
DV
8846 if (!intel_display_power_is_enabled(dev_priv,
8847 POWER_DOMAIN_PIPE(crtc->pipe)))
930e8c9e
PZ
8848 return false;
8849
e143a21c 8850 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 8851 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 8852
0e8ffe1b
DV
8853 tmp = I915_READ(PIPECONF(crtc->pipe));
8854 if (!(tmp & PIPECONF_ENABLE))
8855 return false;
8856
42571aef
VS
8857 switch (tmp & PIPECONF_BPC_MASK) {
8858 case PIPECONF_6BPC:
8859 pipe_config->pipe_bpp = 18;
8860 break;
8861 case PIPECONF_8BPC:
8862 pipe_config->pipe_bpp = 24;
8863 break;
8864 case PIPECONF_10BPC:
8865 pipe_config->pipe_bpp = 30;
8866 break;
8867 case PIPECONF_12BPC:
8868 pipe_config->pipe_bpp = 36;
8869 break;
8870 default:
8871 break;
8872 }
8873
b5a9fa09
DV
8874 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8875 pipe_config->limited_color_range = true;
8876
ab9412ba 8877 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
8878 struct intel_shared_dpll *pll;
8879
88adfff1
DV
8880 pipe_config->has_pch_encoder = true;
8881
627eb5a3
DV
8882 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8883 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8884 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
8885
8886 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 8887
c0d43d62 8888 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
8889 pipe_config->shared_dpll =
8890 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
8891 } else {
8892 tmp = I915_READ(PCH_DPLL_SEL);
8893 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8894 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8895 else
8896 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8897 }
66e985c0
DV
8898
8899 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8900
8901 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8902 &pipe_config->dpll_hw_state));
c93f54cf
DV
8903
8904 tmp = pipe_config->dpll_hw_state.dpll;
8905 pipe_config->pixel_multiplier =
8906 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8907 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
8908
8909 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
8910 } else {
8911 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
8912 }
8913
1bd1bd80
DV
8914 intel_get_pipe_timings(crtc, pipe_config);
8915
2fa2fe9a
DV
8916 ironlake_get_pfit_config(crtc, pipe_config);
8917
0e8ffe1b
DV
8918 return true;
8919}
8920
be256dc7
PZ
8921static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8922{
8923 struct drm_device *dev = dev_priv->dev;
be256dc7 8924 struct intel_crtc *crtc;
be256dc7 8925
d3fcc808 8926 for_each_intel_crtc(dev, crtc)
e2c719b7 8927 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
8928 pipe_name(crtc->pipe));
8929
e2c719b7
RC
8930 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8931 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8932 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8933 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8934 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8935 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
be256dc7 8936 "CPU PWM1 enabled\n");
c5107b87 8937 if (IS_HASWELL(dev))
e2c719b7 8938 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
c5107b87 8939 "CPU PWM2 enabled\n");
e2c719b7 8940 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
be256dc7 8941 "PCH PWM1 enabled\n");
e2c719b7 8942 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
be256dc7 8943 "Utility pin enabled\n");
e2c719b7 8944 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
be256dc7 8945
9926ada1
PZ
8946 /*
8947 * In theory we can still leave IRQs enabled, as long as only the HPD
8948 * interrupts remain enabled. We used to check for that, but since it's
8949 * gen-specific and since we only disable LCPLL after we fully disable
8950 * the interrupts, the check below should be enough.
8951 */
e2c719b7 8952 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
be256dc7
PZ
8953}
8954
9ccd5aeb
PZ
8955static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8956{
8957 struct drm_device *dev = dev_priv->dev;
8958
8959 if (IS_HASWELL(dev))
8960 return I915_READ(D_COMP_HSW);
8961 else
8962 return I915_READ(D_COMP_BDW);
8963}
8964
3c4c9b81
PZ
8965static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8966{
8967 struct drm_device *dev = dev_priv->dev;
8968
8969 if (IS_HASWELL(dev)) {
8970 mutex_lock(&dev_priv->rps.hw_lock);
8971 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8972 val))
f475dadf 8973 DRM_ERROR("Failed to write to D_COMP\n");
3c4c9b81
PZ
8974 mutex_unlock(&dev_priv->rps.hw_lock);
8975 } else {
9ccd5aeb
PZ
8976 I915_WRITE(D_COMP_BDW, val);
8977 POSTING_READ(D_COMP_BDW);
3c4c9b81 8978 }
be256dc7
PZ
8979}
8980
8981/*
8982 * This function implements pieces of two sequences from BSpec:
8983 * - Sequence for display software to disable LCPLL
8984 * - Sequence for display software to allow package C8+
8985 * The steps implemented here are just the steps that actually touch the LCPLL
8986 * register. Callers should take care of disabling all the display engine
8987 * functions, doing the mode unset, fixing interrupts, etc.
8988 */
6ff58d53
PZ
8989static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8990 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
8991{
8992 uint32_t val;
8993
8994 assert_can_disable_lcpll(dev_priv);
8995
8996 val = I915_READ(LCPLL_CTL);
8997
8998 if (switch_to_fclk) {
8999 val |= LCPLL_CD_SOURCE_FCLK;
9000 I915_WRITE(LCPLL_CTL, val);
9001
9002 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9003 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9004 DRM_ERROR("Switching to FCLK failed\n");
9005
9006 val = I915_READ(LCPLL_CTL);
9007 }
9008
9009 val |= LCPLL_PLL_DISABLE;
9010 I915_WRITE(LCPLL_CTL, val);
9011 POSTING_READ(LCPLL_CTL);
9012
9013 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9014 DRM_ERROR("LCPLL still locked\n");
9015
9ccd5aeb 9016 val = hsw_read_dcomp(dev_priv);
be256dc7 9017 val |= D_COMP_COMP_DISABLE;
3c4c9b81 9018 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9019 ndelay(100);
9020
9ccd5aeb
PZ
9021 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9022 1))
be256dc7
PZ
9023 DRM_ERROR("D_COMP RCOMP still in progress\n");
9024
9025 if (allow_power_down) {
9026 val = I915_READ(LCPLL_CTL);
9027 val |= LCPLL_POWER_DOWN_ALLOW;
9028 I915_WRITE(LCPLL_CTL, val);
9029 POSTING_READ(LCPLL_CTL);
9030 }
9031}
9032
9033/*
9034 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9035 * source.
9036 */
6ff58d53 9037static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
9038{
9039 uint32_t val;
9040
9041 val = I915_READ(LCPLL_CTL);
9042
9043 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9044 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9045 return;
9046
a8a8bd54
PZ
9047 /*
9048 * Make sure we're not on PC8 state before disabling PC8, otherwise
9049 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
a8a8bd54 9050 */
59bad947 9051 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
215733fa 9052
be256dc7
PZ
9053 if (val & LCPLL_POWER_DOWN_ALLOW) {
9054 val &= ~LCPLL_POWER_DOWN_ALLOW;
9055 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 9056 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
9057 }
9058
9ccd5aeb 9059 val = hsw_read_dcomp(dev_priv);
be256dc7
PZ
9060 val |= D_COMP_COMP_FORCE;
9061 val &= ~D_COMP_COMP_DISABLE;
3c4c9b81 9062 hsw_write_dcomp(dev_priv, val);
be256dc7
PZ
9063
9064 val = I915_READ(LCPLL_CTL);
9065 val &= ~LCPLL_PLL_DISABLE;
9066 I915_WRITE(LCPLL_CTL, val);
9067
9068 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9069 DRM_ERROR("LCPLL not locked yet\n");
9070
9071 if (val & LCPLL_CD_SOURCE_FCLK) {
9072 val = I915_READ(LCPLL_CTL);
9073 val &= ~LCPLL_CD_SOURCE_FCLK;
9074 I915_WRITE(LCPLL_CTL, val);
9075
9076 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9077 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9078 DRM_ERROR("Switching back to LCPLL failed\n");
9079 }
215733fa 9080
59bad947 9081 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
be256dc7
PZ
9082}
9083
765dab67
PZ
9084/*
9085 * Package states C8 and deeper are really deep PC states that can only be
9086 * reached when all the devices on the system allow it, so even if the graphics
9087 * device allows PC8+, it doesn't mean the system will actually get to these
9088 * states. Our driver only allows PC8+ when going into runtime PM.
9089 *
9090 * The requirements for PC8+ are that all the outputs are disabled, the power
9091 * well is disabled and most interrupts are disabled, and these are also
9092 * requirements for runtime PM. When these conditions are met, we manually do
9093 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9094 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9095 * hang the machine.
9096 *
9097 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9098 * the state of some registers, so when we come back from PC8+ we need to
9099 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9100 * need to take care of the registers kept by RC6. Notice that this happens even
9101 * if we don't put the device in PCI D3 state (which is what currently happens
9102 * because of the runtime PM support).
9103 *
9104 * For more, read "Display Sequences for Package C8" on the hardware
9105 * documentation.
9106 */
a14cb6fc 9107void hsw_enable_pc8(struct drm_i915_private *dev_priv)
c67a470b 9108{
c67a470b
PZ
9109 struct drm_device *dev = dev_priv->dev;
9110 uint32_t val;
9111
c67a470b
PZ
9112 DRM_DEBUG_KMS("Enabling package C8+\n");
9113
c67a470b
PZ
9114 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9115 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9116 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9117 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9118 }
9119
9120 lpt_disable_clkout_dp(dev);
c67a470b
PZ
9121 hsw_disable_lcpll(dev_priv, true, true);
9122}
9123
a14cb6fc 9124void hsw_disable_pc8(struct drm_i915_private *dev_priv)
c67a470b
PZ
9125{
9126 struct drm_device *dev = dev_priv->dev;
9127 uint32_t val;
9128
c67a470b
PZ
9129 DRM_DEBUG_KMS("Disabling package C8+\n");
9130
9131 hsw_restore_lcpll(dev_priv);
c67a470b
PZ
9132 lpt_init_pch_refclk(dev);
9133
9134 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9135 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9136 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9137 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9138 }
9139
9140 intel_prepare_ddi(dev);
c67a470b
PZ
9141}
9142
a821fc46 9143static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
f8437dd1 9144{
a821fc46 9145 struct drm_device *dev = old_state->dev;
f8437dd1 9146 struct drm_i915_private *dev_priv = dev->dev_private;
a821fc46 9147 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
f8437dd1
VK
9148 int req_cdclk;
9149
9150 /* see the comment in valleyview_modeset_global_resources */
9151 if (WARN_ON(max_pixclk < 0))
9152 return;
9153
9154 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9155
9156 if (req_cdclk != dev_priv->cdclk_freq)
9157 broxton_set_cdclk(dev, req_cdclk);
9158}
9159
190f68c5
ACO
9160static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9161 struct intel_crtc_state *crtc_state)
09b4ddf9 9162{
190f68c5 9163 if (!intel_ddi_pll_select(crtc, crtc_state))
6441ab5f 9164 return -EINVAL;
716c2e55 9165
c7653199 9166 crtc->lowfreq_avail = false;
644cef34 9167
c8f7a0db 9168 return 0;
79e53945
JB
9169}
9170
3760b59c
S
9171static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9172 enum port port,
9173 struct intel_crtc_state *pipe_config)
9174{
9175 switch (port) {
9176 case PORT_A:
9177 pipe_config->ddi_pll_sel = SKL_DPLL0;
9178 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9179 break;
9180 case PORT_B:
9181 pipe_config->ddi_pll_sel = SKL_DPLL1;
9182 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9183 break;
9184 case PORT_C:
9185 pipe_config->ddi_pll_sel = SKL_DPLL2;
9186 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9187 break;
9188 default:
9189 DRM_ERROR("Incorrect port type\n");
9190 }
9191}
9192
96b7dfb7
S
9193static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9194 enum port port,
5cec258b 9195 struct intel_crtc_state *pipe_config)
96b7dfb7 9196{
3148ade7 9197 u32 temp, dpll_ctl1;
96b7dfb7
S
9198
9199 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9200 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9201
9202 switch (pipe_config->ddi_pll_sel) {
3148ade7
DL
9203 case SKL_DPLL0:
9204 /*
9205 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9206 * of the shared DPLL framework and thus needs to be read out
9207 * separately
9208 */
9209 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9210 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9211 break;
96b7dfb7
S
9212 case SKL_DPLL1:
9213 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9214 break;
9215 case SKL_DPLL2:
9216 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9217 break;
9218 case SKL_DPLL3:
9219 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9220 break;
96b7dfb7
S
9221 }
9222}
9223
7d2c8175
DL
9224static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9225 enum port port,
5cec258b 9226 struct intel_crtc_state *pipe_config)
7d2c8175
DL
9227{
9228 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9229
9230 switch (pipe_config->ddi_pll_sel) {
9231 case PORT_CLK_SEL_WRPLL1:
9232 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9233 break;
9234 case PORT_CLK_SEL_WRPLL2:
9235 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9236 break;
9237 }
9238}
9239
26804afd 9240static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
5cec258b 9241 struct intel_crtc_state *pipe_config)
26804afd
DV
9242{
9243 struct drm_device *dev = crtc->base.dev;
9244 struct drm_i915_private *dev_priv = dev->dev_private;
d452c5b6 9245 struct intel_shared_dpll *pll;
26804afd
DV
9246 enum port port;
9247 uint32_t tmp;
9248
9249 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9250
9251 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9252
96b7dfb7
S
9253 if (IS_SKYLAKE(dev))
9254 skylake_get_ddi_pll(dev_priv, port, pipe_config);
3760b59c
S
9255 else if (IS_BROXTON(dev))
9256 bxt_get_ddi_pll(dev_priv, port, pipe_config);
96b7dfb7
S
9257 else
9258 haswell_get_ddi_pll(dev_priv, port, pipe_config);
9cd86933 9259
d452c5b6
DV
9260 if (pipe_config->shared_dpll >= 0) {
9261 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9262
9263 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9264 &pipe_config->dpll_hw_state));
9265 }
9266
26804afd
DV
9267 /*
9268 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9269 * DDI E. So just check whether this pipe is wired to DDI E and whether
9270 * the PCH transcoder is on.
9271 */
ca370455
DL
9272 if (INTEL_INFO(dev)->gen < 9 &&
9273 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
26804afd
DV
9274 pipe_config->has_pch_encoder = true;
9275
9276 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9277 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9278 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9279
9280 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9281 }
9282}
9283
0e8ffe1b 9284static bool haswell_get_pipe_config(struct intel_crtc *crtc,
5cec258b 9285 struct intel_crtc_state *pipe_config)
0e8ffe1b
DV
9286{
9287 struct drm_device *dev = crtc->base.dev;
9288 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 9289 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
9290 uint32_t tmp;
9291
f458ebbc 9292 if (!intel_display_power_is_enabled(dev_priv,
b5482bd0
ID
9293 POWER_DOMAIN_PIPE(crtc->pipe)))
9294 return false;
9295
e143a21c 9296 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
9297 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9298
eccb140b
DV
9299 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9300 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9301 enum pipe trans_edp_pipe;
9302 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9303 default:
9304 WARN(1, "unknown pipe linked to edp transcoder\n");
9305 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9306 case TRANS_DDI_EDP_INPUT_A_ON:
9307 trans_edp_pipe = PIPE_A;
9308 break;
9309 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9310 trans_edp_pipe = PIPE_B;
9311 break;
9312 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9313 trans_edp_pipe = PIPE_C;
9314 break;
9315 }
9316
9317 if (trans_edp_pipe == crtc->pipe)
9318 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9319 }
9320
f458ebbc 9321 if (!intel_display_power_is_enabled(dev_priv,
eccb140b 9322 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
9323 return false;
9324
eccb140b 9325 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
9326 if (!(tmp & PIPECONF_ENABLE))
9327 return false;
9328
26804afd 9329 haswell_get_ddi_port_state(crtc, pipe_config);
627eb5a3 9330
1bd1bd80
DV
9331 intel_get_pipe_timings(crtc, pipe_config);
9332
a1b2278e
CK
9333 if (INTEL_INFO(dev)->gen >= 9) {
9334 skl_init_scalers(dev, crtc, pipe_config);
9335 }
9336
2fa2fe9a 9337 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
af99ceda
CK
9338
9339 if (INTEL_INFO(dev)->gen >= 9) {
9340 pipe_config->scaler_state.scaler_id = -1;
9341 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9342 }
9343
bd2e244f 9344 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
ff6d9f55 9345 if (INTEL_INFO(dev)->gen == 9)
bd2e244f 9346 skylake_get_pfit_config(crtc, pipe_config);
ff6d9f55 9347 else if (INTEL_INFO(dev)->gen < 9)
bd2e244f 9348 ironlake_get_pfit_config(crtc, pipe_config);
ff6d9f55
JB
9349 else
9350 MISSING_CASE(INTEL_INFO(dev)->gen);
bd2e244f 9351 }
88adfff1 9352
e59150dc
JB
9353 if (IS_HASWELL(dev))
9354 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9355 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 9356
ebb69c95
CT
9357 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9358 pipe_config->pixel_multiplier =
9359 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9360 } else {
9361 pipe_config->pixel_multiplier = 1;
9362 }
6c49f241 9363
0e8ffe1b
DV
9364 return true;
9365}
9366
560b85bb
CW
9367static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9368{
9369 struct drm_device *dev = crtc->dev;
9370 struct drm_i915_private *dev_priv = dev->dev_private;
9371 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
dc41c154 9372 uint32_t cntl = 0, size = 0;
560b85bb 9373
dc41c154 9374 if (base) {
3dd512fb
MR
9375 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9376 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
dc41c154
VS
9377 unsigned int stride = roundup_pow_of_two(width) * 4;
9378
9379 switch (stride) {
9380 default:
9381 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9382 width, stride);
9383 stride = 256;
9384 /* fallthrough */
9385 case 256:
9386 case 512:
9387 case 1024:
9388 case 2048:
9389 break;
4b0e333e
CW
9390 }
9391
dc41c154
VS
9392 cntl |= CURSOR_ENABLE |
9393 CURSOR_GAMMA_ENABLE |
9394 CURSOR_FORMAT_ARGB |
9395 CURSOR_STRIDE(stride);
9396
9397 size = (height << 12) | width;
4b0e333e 9398 }
560b85bb 9399
dc41c154
VS
9400 if (intel_crtc->cursor_cntl != 0 &&
9401 (intel_crtc->cursor_base != base ||
9402 intel_crtc->cursor_size != size ||
9403 intel_crtc->cursor_cntl != cntl)) {
9404 /* On these chipsets we can only modify the base/size/stride
9405 * whilst the cursor is disabled.
9406 */
9407 I915_WRITE(_CURACNTR, 0);
4b0e333e 9408 POSTING_READ(_CURACNTR);
dc41c154 9409 intel_crtc->cursor_cntl = 0;
4b0e333e 9410 }
560b85bb 9411
99d1f387 9412 if (intel_crtc->cursor_base != base) {
9db4a9c7 9413 I915_WRITE(_CURABASE, base);
99d1f387
VS
9414 intel_crtc->cursor_base = base;
9415 }
4726e0b0 9416
dc41c154
VS
9417 if (intel_crtc->cursor_size != size) {
9418 I915_WRITE(CURSIZE, size);
9419 intel_crtc->cursor_size = size;
4b0e333e 9420 }
560b85bb 9421
4b0e333e 9422 if (intel_crtc->cursor_cntl != cntl) {
4b0e333e
CW
9423 I915_WRITE(_CURACNTR, cntl);
9424 POSTING_READ(_CURACNTR);
4b0e333e 9425 intel_crtc->cursor_cntl = cntl;
560b85bb 9426 }
560b85bb
CW
9427}
9428
560b85bb 9429static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
65a21cd6
JB
9430{
9431 struct drm_device *dev = crtc->dev;
9432 struct drm_i915_private *dev_priv = dev->dev_private;
9433 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9434 int pipe = intel_crtc->pipe;
4b0e333e
CW
9435 uint32_t cntl;
9436
9437 cntl = 0;
9438 if (base) {
9439 cntl = MCURSOR_GAMMA_ENABLE;
3dd512fb 9440 switch (intel_crtc->base.cursor->state->crtc_w) {
4726e0b0
SK
9441 case 64:
9442 cntl |= CURSOR_MODE_64_ARGB_AX;
9443 break;
9444 case 128:
9445 cntl |= CURSOR_MODE_128_ARGB_AX;
9446 break;
9447 case 256:
9448 cntl |= CURSOR_MODE_256_ARGB_AX;
9449 break;
9450 default:
3dd512fb 9451 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
4726e0b0 9452 return;
65a21cd6 9453 }
4b0e333e 9454 cntl |= pipe << 28; /* Connect to correct pipe */
47bf17a7
VS
9455
9456 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9457 cntl |= CURSOR_PIPE_CSC_ENABLE;
4b0e333e 9458 }
65a21cd6 9459
8e7d688b 9460 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
4398ad45
VS
9461 cntl |= CURSOR_ROTATE_180;
9462
4b0e333e
CW
9463 if (intel_crtc->cursor_cntl != cntl) {
9464 I915_WRITE(CURCNTR(pipe), cntl);
9465 POSTING_READ(CURCNTR(pipe));
9466 intel_crtc->cursor_cntl = cntl;
65a21cd6 9467 }
4b0e333e 9468
65a21cd6 9469 /* and commit changes on next vblank */
5efb3e28
VS
9470 I915_WRITE(CURBASE(pipe), base);
9471 POSTING_READ(CURBASE(pipe));
99d1f387
VS
9472
9473 intel_crtc->cursor_base = base;
65a21cd6
JB
9474}
9475
cda4b7d3 9476/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
9477static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9478 bool on)
cda4b7d3
CW
9479{
9480 struct drm_device *dev = crtc->dev;
9481 struct drm_i915_private *dev_priv = dev->dev_private;
9482 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9483 int pipe = intel_crtc->pipe;
3d7d6510
MR
9484 int x = crtc->cursor_x;
9485 int y = crtc->cursor_y;
d6e4db15 9486 u32 base = 0, pos = 0;
cda4b7d3 9487
d6e4db15 9488 if (on)
cda4b7d3 9489 base = intel_crtc->cursor_addr;
cda4b7d3 9490
6e3c9717 9491 if (x >= intel_crtc->config->pipe_src_w)
d6e4db15
VS
9492 base = 0;
9493
6e3c9717 9494 if (y >= intel_crtc->config->pipe_src_h)
cda4b7d3
CW
9495 base = 0;
9496
9497 if (x < 0) {
3dd512fb 9498 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
cda4b7d3
CW
9499 base = 0;
9500
9501 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9502 x = -x;
9503 }
9504 pos |= x << CURSOR_X_SHIFT;
9505
9506 if (y < 0) {
3dd512fb 9507 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
cda4b7d3
CW
9508 base = 0;
9509
9510 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9511 y = -y;
9512 }
9513 pos |= y << CURSOR_Y_SHIFT;
9514
4b0e333e 9515 if (base == 0 && intel_crtc->cursor_base == 0)
cda4b7d3
CW
9516 return;
9517
5efb3e28
VS
9518 I915_WRITE(CURPOS(pipe), pos);
9519
4398ad45
VS
9520 /* ILK+ do this automagically */
9521 if (HAS_GMCH_DISPLAY(dev) &&
8e7d688b 9522 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
3dd512fb
MR
9523 base += (intel_crtc->base.cursor->state->crtc_h *
9524 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
4398ad45
VS
9525 }
9526
8ac54669 9527 if (IS_845G(dev) || IS_I865G(dev))
5efb3e28
VS
9528 i845_update_cursor(crtc, base);
9529 else
9530 i9xx_update_cursor(crtc, base);
cda4b7d3
CW
9531}
9532
dc41c154
VS
9533static bool cursor_size_ok(struct drm_device *dev,
9534 uint32_t width, uint32_t height)
9535{
9536 if (width == 0 || height == 0)
9537 return false;
9538
9539 /*
9540 * 845g/865g are special in that they are only limited by
9541 * the width of their cursors, the height is arbitrary up to
9542 * the precision of the register. Everything else requires
9543 * square cursors, limited to a few power-of-two sizes.
9544 */
9545 if (IS_845G(dev) || IS_I865G(dev)) {
9546 if ((width & 63) != 0)
9547 return false;
9548
9549 if (width > (IS_845G(dev) ? 64 : 512))
9550 return false;
9551
9552 if (height > 1023)
9553 return false;
9554 } else {
9555 switch (width | height) {
9556 case 256:
9557 case 128:
9558 if (IS_GEN2(dev))
9559 return false;
9560 case 64:
9561 break;
9562 default:
9563 return false;
9564 }
9565 }
9566
9567 return true;
9568}
9569
79e53945 9570static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 9571 u16 *blue, uint32_t start, uint32_t size)
79e53945 9572{
7203425a 9573 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 9574 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 9575
7203425a 9576 for (i = start; i < end; i++) {
79e53945
JB
9577 intel_crtc->lut_r[i] = red[i] >> 8;
9578 intel_crtc->lut_g[i] = green[i] >> 8;
9579 intel_crtc->lut_b[i] = blue[i] >> 8;
9580 }
9581
9582 intel_crtc_load_lut(crtc);
9583}
9584
79e53945
JB
9585/* VESA 640x480x72Hz mode to set on the pipe */
9586static struct drm_display_mode load_detect_mode = {
9587 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9588 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9589};
9590
a8bb6818
DV
9591struct drm_framebuffer *
9592__intel_framebuffer_create(struct drm_device *dev,
9593 struct drm_mode_fb_cmd2 *mode_cmd,
9594 struct drm_i915_gem_object *obj)
d2dff872
CW
9595{
9596 struct intel_framebuffer *intel_fb;
9597 int ret;
9598
9599 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9600 if (!intel_fb) {
6ccb81f2 9601 drm_gem_object_unreference(&obj->base);
d2dff872
CW
9602 return ERR_PTR(-ENOMEM);
9603 }
9604
9605 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
9606 if (ret)
9607 goto err;
d2dff872
CW
9608
9609 return &intel_fb->base;
dd4916c5 9610err:
6ccb81f2 9611 drm_gem_object_unreference(&obj->base);
dd4916c5
DV
9612 kfree(intel_fb);
9613
9614 return ERR_PTR(ret);
d2dff872
CW
9615}
9616
b5ea642a 9617static struct drm_framebuffer *
a8bb6818
DV
9618intel_framebuffer_create(struct drm_device *dev,
9619 struct drm_mode_fb_cmd2 *mode_cmd,
9620 struct drm_i915_gem_object *obj)
9621{
9622 struct drm_framebuffer *fb;
9623 int ret;
9624
9625 ret = i915_mutex_lock_interruptible(dev);
9626 if (ret)
9627 return ERR_PTR(ret);
9628 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9629 mutex_unlock(&dev->struct_mutex);
9630
9631 return fb;
9632}
9633
d2dff872
CW
9634static u32
9635intel_framebuffer_pitch_for_width(int width, int bpp)
9636{
9637 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9638 return ALIGN(pitch, 64);
9639}
9640
9641static u32
9642intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9643{
9644 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
1267a26b 9645 return PAGE_ALIGN(pitch * mode->vdisplay);
d2dff872
CW
9646}
9647
9648static struct drm_framebuffer *
9649intel_framebuffer_create_for_mode(struct drm_device *dev,
9650 struct drm_display_mode *mode,
9651 int depth, int bpp)
9652{
9653 struct drm_i915_gem_object *obj;
0fed39bd 9654 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
9655
9656 obj = i915_gem_alloc_object(dev,
9657 intel_framebuffer_size_for_mode(mode, bpp));
9658 if (obj == NULL)
9659 return ERR_PTR(-ENOMEM);
9660
9661 mode_cmd.width = mode->hdisplay;
9662 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
9663 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9664 bpp);
5ca0c34a 9665 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
9666
9667 return intel_framebuffer_create(dev, &mode_cmd, obj);
9668}
9669
9670static struct drm_framebuffer *
9671mode_fits_in_fbdev(struct drm_device *dev,
9672 struct drm_display_mode *mode)
9673{
4520f53a 9674#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
9675 struct drm_i915_private *dev_priv = dev->dev_private;
9676 struct drm_i915_gem_object *obj;
9677 struct drm_framebuffer *fb;
9678
4c0e5528 9679 if (!dev_priv->fbdev)
d2dff872
CW
9680 return NULL;
9681
4c0e5528 9682 if (!dev_priv->fbdev->fb)
d2dff872
CW
9683 return NULL;
9684
4c0e5528
DV
9685 obj = dev_priv->fbdev->fb->obj;
9686 BUG_ON(!obj);
9687
8bcd4553 9688 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
9689 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9690 fb->bits_per_pixel))
d2dff872
CW
9691 return NULL;
9692
01f2c773 9693 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
9694 return NULL;
9695
9696 return fb;
4520f53a
DV
9697#else
9698 return NULL;
9699#endif
d2dff872
CW
9700}
9701
d3a40d1b
ACO
9702static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9703 struct drm_crtc *crtc,
9704 struct drm_display_mode *mode,
9705 struct drm_framebuffer *fb,
9706 int x, int y)
9707{
9708 struct drm_plane_state *plane_state;
9709 int hdisplay, vdisplay;
9710 int ret;
9711
9712 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9713 if (IS_ERR(plane_state))
9714 return PTR_ERR(plane_state);
9715
9716 if (mode)
9717 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
9718 else
9719 hdisplay = vdisplay = 0;
9720
9721 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9722 if (ret)
9723 return ret;
9724 drm_atomic_set_fb_for_plane(plane_state, fb);
9725 plane_state->crtc_x = 0;
9726 plane_state->crtc_y = 0;
9727 plane_state->crtc_w = hdisplay;
9728 plane_state->crtc_h = vdisplay;
9729 plane_state->src_x = x << 16;
9730 plane_state->src_y = y << 16;
9731 plane_state->src_w = hdisplay << 16;
9732 plane_state->src_h = vdisplay << 16;
9733
9734 return 0;
9735}
9736
d2434ab7 9737bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 9738 struct drm_display_mode *mode,
51fd371b
RC
9739 struct intel_load_detect_pipe *old,
9740 struct drm_modeset_acquire_ctx *ctx)
79e53945
JB
9741{
9742 struct intel_crtc *intel_crtc;
d2434ab7
DV
9743 struct intel_encoder *intel_encoder =
9744 intel_attached_encoder(connector);
79e53945 9745 struct drm_crtc *possible_crtc;
4ef69c7a 9746 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
9747 struct drm_crtc *crtc = NULL;
9748 struct drm_device *dev = encoder->dev;
94352cf9 9749 struct drm_framebuffer *fb;
51fd371b 9750 struct drm_mode_config *config = &dev->mode_config;
83a57153 9751 struct drm_atomic_state *state = NULL;
944b0c76 9752 struct drm_connector_state *connector_state;
4be07317 9753 struct intel_crtc_state *crtc_state;
51fd371b 9754 int ret, i = -1;
79e53945 9755
d2dff872 9756 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9757 connector->base.id, connector->name,
8e329a03 9758 encoder->base.id, encoder->name);
d2dff872 9759
51fd371b
RC
9760retry:
9761 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9762 if (ret)
9763 goto fail_unlock;
6e9f798d 9764
79e53945
JB
9765 /*
9766 * Algorithm gets a little messy:
7a5e4805 9767 *
79e53945
JB
9768 * - if the connector already has an assigned crtc, use it (but make
9769 * sure it's on first)
7a5e4805 9770 *
79e53945
JB
9771 * - try to find the first unused crtc that can drive this connector,
9772 * and use that if we find one
79e53945
JB
9773 */
9774
9775 /* See if we already have a CRTC for this connector */
9776 if (encoder->crtc) {
9777 crtc = encoder->crtc;
8261b191 9778
51fd371b 9779 ret = drm_modeset_lock(&crtc->mutex, ctx);
4d02e2de
DV
9780 if (ret)
9781 goto fail_unlock;
9782 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
51fd371b
RC
9783 if (ret)
9784 goto fail_unlock;
7b24056b 9785
24218aac 9786 old->dpms_mode = connector->dpms;
8261b191
CW
9787 old->load_detect_temp = false;
9788
9789 /* Make sure the crtc and connector are running */
24218aac
DV
9790 if (connector->dpms != DRM_MODE_DPMS_ON)
9791 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 9792
7173188d 9793 return true;
79e53945
JB
9794 }
9795
9796 /* Find an unused one (if possible) */
70e1e0ec 9797 for_each_crtc(dev, possible_crtc) {
79e53945
JB
9798 i++;
9799 if (!(encoder->possible_crtcs & (1 << i)))
9800 continue;
83d65738 9801 if (possible_crtc->state->enable)
a459249c
VS
9802 continue;
9803 /* This can occur when applying the pipe A quirk on resume. */
9804 if (to_intel_crtc(possible_crtc)->new_enabled)
9805 continue;
9806
9807 crtc = possible_crtc;
9808 break;
79e53945
JB
9809 }
9810
9811 /*
9812 * If we didn't find an unused CRTC, don't use any.
9813 */
9814 if (!crtc) {
7173188d 9815 DRM_DEBUG_KMS("no pipe available for load-detect\n");
51fd371b 9816 goto fail_unlock;
79e53945
JB
9817 }
9818
51fd371b
RC
9819 ret = drm_modeset_lock(&crtc->mutex, ctx);
9820 if (ret)
4d02e2de
DV
9821 goto fail_unlock;
9822 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9823 if (ret)
51fd371b 9824 goto fail_unlock;
fc303101
DV
9825 intel_encoder->new_crtc = to_intel_crtc(crtc);
9826 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
9827
9828 intel_crtc = to_intel_crtc(crtc);
412b61d8 9829 intel_crtc->new_enabled = true;
24218aac 9830 old->dpms_mode = connector->dpms;
8261b191 9831 old->load_detect_temp = true;
d2dff872 9832 old->release_fb = NULL;
79e53945 9833
83a57153
ACO
9834 state = drm_atomic_state_alloc(dev);
9835 if (!state)
9836 return false;
9837
9838 state->acquire_ctx = ctx;
9839
944b0c76
ACO
9840 connector_state = drm_atomic_get_connector_state(state, connector);
9841 if (IS_ERR(connector_state)) {
9842 ret = PTR_ERR(connector_state);
9843 goto fail;
9844 }
9845
9846 connector_state->crtc = crtc;
9847 connector_state->best_encoder = &intel_encoder->base;
9848
4be07317
ACO
9849 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9850 if (IS_ERR(crtc_state)) {
9851 ret = PTR_ERR(crtc_state);
9852 goto fail;
9853 }
9854
49d6fa21 9855 crtc_state->base.active = crtc_state->base.enable = true;
4be07317 9856
6492711d
CW
9857 if (!mode)
9858 mode = &load_detect_mode;
79e53945 9859
d2dff872
CW
9860 /* We need a framebuffer large enough to accommodate all accesses
9861 * that the plane may generate whilst we perform load detection.
9862 * We can not rely on the fbcon either being present (we get called
9863 * during its initialisation to detect all boot displays, or it may
9864 * not even exist) or that it is large enough to satisfy the
9865 * requested mode.
9866 */
94352cf9
DV
9867 fb = mode_fits_in_fbdev(dev, mode);
9868 if (fb == NULL) {
d2dff872 9869 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
9870 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9871 old->release_fb = fb;
d2dff872
CW
9872 } else
9873 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 9874 if (IS_ERR(fb)) {
d2dff872 9875 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 9876 goto fail;
79e53945 9877 }
79e53945 9878
d3a40d1b
ACO
9879 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
9880 if (ret)
9881 goto fail;
9882
8c7b5ccb
ACO
9883 drm_mode_copy(&crtc_state->base.mode, mode);
9884
9885 if (intel_set_mode(crtc, state)) {
6492711d 9886 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
9887 if (old->release_fb)
9888 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 9889 goto fail;
79e53945 9890 }
9128b040 9891 crtc->primary->crtc = crtc;
7173188d 9892
79e53945 9893 /* let the connector get through one full cycle before testing */
9d0498a2 9894 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 9895 return true;
412b61d8
VS
9896
9897 fail:
83d65738 9898 intel_crtc->new_enabled = crtc->state->enable;
51fd371b 9899fail_unlock:
e5d958ef
ACO
9900 drm_atomic_state_free(state);
9901 state = NULL;
83a57153 9902
51fd371b
RC
9903 if (ret == -EDEADLK) {
9904 drm_modeset_backoff(ctx);
9905 goto retry;
9906 }
9907
412b61d8 9908 return false;
79e53945
JB
9909}
9910
d2434ab7 9911void intel_release_load_detect_pipe(struct drm_connector *connector,
49172fee
ACO
9912 struct intel_load_detect_pipe *old,
9913 struct drm_modeset_acquire_ctx *ctx)
79e53945 9914{
83a57153 9915 struct drm_device *dev = connector->dev;
d2434ab7
DV
9916 struct intel_encoder *intel_encoder =
9917 intel_attached_encoder(connector);
4ef69c7a 9918 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 9919 struct drm_crtc *crtc = encoder->crtc;
412b61d8 9920 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83a57153 9921 struct drm_atomic_state *state;
944b0c76 9922 struct drm_connector_state *connector_state;
4be07317 9923 struct intel_crtc_state *crtc_state;
d3a40d1b 9924 int ret;
79e53945 9925
d2dff872 9926 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
c23cc417 9927 connector->base.id, connector->name,
8e329a03 9928 encoder->base.id, encoder->name);
d2dff872 9929
8261b191 9930 if (old->load_detect_temp) {
83a57153 9931 state = drm_atomic_state_alloc(dev);
944b0c76
ACO
9932 if (!state)
9933 goto fail;
83a57153
ACO
9934
9935 state->acquire_ctx = ctx;
9936
944b0c76
ACO
9937 connector_state = drm_atomic_get_connector_state(state, connector);
9938 if (IS_ERR(connector_state))
9939 goto fail;
9940
4be07317
ACO
9941 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9942 if (IS_ERR(crtc_state))
9943 goto fail;
9944
fc303101
DV
9945 to_intel_connector(connector)->new_encoder = NULL;
9946 intel_encoder->new_crtc = NULL;
412b61d8 9947 intel_crtc->new_enabled = false;
944b0c76
ACO
9948
9949 connector_state->best_encoder = NULL;
9950 connector_state->crtc = NULL;
9951
49d6fa21 9952 crtc_state->base.enable = crtc_state->base.active = false;
4be07317 9953
d3a40d1b
ACO
9954 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
9955 0, 0);
9956 if (ret)
9957 goto fail;
9958
2bfb4627
ACO
9959 ret = intel_set_mode(crtc, state);
9960 if (ret)
9961 goto fail;
d2dff872 9962
36206361
DV
9963 if (old->release_fb) {
9964 drm_framebuffer_unregister_private(old->release_fb);
9965 drm_framebuffer_unreference(old->release_fb);
9966 }
d2dff872 9967
0622a53c 9968 return;
79e53945
JB
9969 }
9970
c751ce4f 9971 /* Switch crtc and encoder back off if necessary */
24218aac
DV
9972 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9973 connector->funcs->dpms(connector, old->dpms_mode);
944b0c76
ACO
9974
9975 return;
9976fail:
9977 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9978 drm_atomic_state_free(state);
79e53945
JB
9979}
9980
da4a1efa 9981static int i9xx_pll_refclk(struct drm_device *dev,
5cec258b 9982 const struct intel_crtc_state *pipe_config)
da4a1efa
VS
9983{
9984 struct drm_i915_private *dev_priv = dev->dev_private;
9985 u32 dpll = pipe_config->dpll_hw_state.dpll;
9986
9987 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 9988 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
9989 else if (HAS_PCH_SPLIT(dev))
9990 return 120000;
9991 else if (!IS_GEN2(dev))
9992 return 96000;
9993 else
9994 return 48000;
9995}
9996
79e53945 9997/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc 9998static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5cec258b 9999 struct intel_crtc_state *pipe_config)
79e53945 10000{
f1f644dc 10001 struct drm_device *dev = crtc->base.dev;
79e53945 10002 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 10003 int pipe = pipe_config->cpu_transcoder;
293623f7 10004 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
10005 u32 fp;
10006 intel_clock_t clock;
da4a1efa 10007 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
10008
10009 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 10010 fp = pipe_config->dpll_hw_state.fp0;
79e53945 10011 else
293623f7 10012 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
10013
10014 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
10015 if (IS_PINEVIEW(dev)) {
10016 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10017 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
10018 } else {
10019 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10020 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10021 }
10022
a6c45cf0 10023 if (!IS_GEN2(dev)) {
f2b115e6
AJ
10024 if (IS_PINEVIEW(dev))
10025 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10026 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
10027 else
10028 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
10029 DPLL_FPA01_P1_POST_DIV_SHIFT);
10030
10031 switch (dpll & DPLL_MODE_MASK) {
10032 case DPLLB_MODE_DAC_SERIAL:
10033 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10034 5 : 10;
10035 break;
10036 case DPLLB_MODE_LVDS:
10037 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10038 7 : 14;
10039 break;
10040 default:
28c97730 10041 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 10042 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 10043 return;
79e53945
JB
10044 }
10045
ac58c3f0 10046 if (IS_PINEVIEW(dev))
da4a1efa 10047 pineview_clock(refclk, &clock);
ac58c3f0 10048 else
da4a1efa 10049 i9xx_clock(refclk, &clock);
79e53945 10050 } else {
0fb58223 10051 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 10052 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
10053
10054 if (is_lvds) {
10055 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10056 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
10057
10058 if (lvds & LVDS_CLKB_POWER_UP)
10059 clock.p2 = 7;
10060 else
10061 clock.p2 = 14;
79e53945
JB
10062 } else {
10063 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10064 clock.p1 = 2;
10065 else {
10066 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10067 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10068 }
10069 if (dpll & PLL_P2_DIVIDE_BY_4)
10070 clock.p2 = 4;
10071 else
10072 clock.p2 = 2;
79e53945 10073 }
da4a1efa
VS
10074
10075 i9xx_clock(refclk, &clock);
79e53945
JB
10076 }
10077
18442d08
VS
10078 /*
10079 * This value includes pixel_multiplier. We will use
241bfc38 10080 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
10081 * encoder's get_config() function.
10082 */
10083 pipe_config->port_clock = clock.dot;
f1f644dc
JB
10084}
10085
6878da05
VS
10086int intel_dotclock_calculate(int link_freq,
10087 const struct intel_link_m_n *m_n)
f1f644dc 10088{
f1f644dc
JB
10089 /*
10090 * The calculation for the data clock is:
1041a02f 10091 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 10092 * But we want to avoid losing precison if possible, so:
1041a02f 10093 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
10094 *
10095 * and the link clock is simpler:
1041a02f 10096 * link_clock = (m * link_clock) / n
f1f644dc
JB
10097 */
10098
6878da05
VS
10099 if (!m_n->link_n)
10100 return 0;
f1f644dc 10101
6878da05
VS
10102 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10103}
f1f644dc 10104
18442d08 10105static void ironlake_pch_clock_get(struct intel_crtc *crtc,
5cec258b 10106 struct intel_crtc_state *pipe_config)
6878da05
VS
10107{
10108 struct drm_device *dev = crtc->base.dev;
79e53945 10109
18442d08
VS
10110 /* read out port_clock from the DPLL */
10111 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 10112
f1f644dc 10113 /*
18442d08 10114 * This value does not include pixel_multiplier.
241bfc38 10115 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
10116 * agree once we know their relationship in the encoder's
10117 * get_config() function.
79e53945 10118 */
2d112de7 10119 pipe_config->base.adjusted_mode.crtc_clock =
18442d08
VS
10120 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10121 &pipe_config->fdi_m_n);
79e53945
JB
10122}
10123
10124/** Returns the currently programmed mode of the given pipe. */
10125struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10126 struct drm_crtc *crtc)
10127{
548f245b 10128 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 10129 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6e3c9717 10130 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
79e53945 10131 struct drm_display_mode *mode;
5cec258b 10132 struct intel_crtc_state pipe_config;
fe2b8f9d
PZ
10133 int htot = I915_READ(HTOTAL(cpu_transcoder));
10134 int hsync = I915_READ(HSYNC(cpu_transcoder));
10135 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10136 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 10137 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
10138
10139 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10140 if (!mode)
10141 return NULL;
10142
f1f644dc
JB
10143 /*
10144 * Construct a pipe_config sufficient for getting the clock info
10145 * back out of crtc_clock_get.
10146 *
10147 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10148 * to use a real value here instead.
10149 */
293623f7 10150 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 10151 pipe_config.pixel_multiplier = 1;
293623f7
VS
10152 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10153 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10154 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
10155 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10156
773ae034 10157 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
10158 mode->hdisplay = (htot & 0xffff) + 1;
10159 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10160 mode->hsync_start = (hsync & 0xffff) + 1;
10161 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10162 mode->vdisplay = (vtot & 0xffff) + 1;
10163 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10164 mode->vsync_start = (vsync & 0xffff) + 1;
10165 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10166
10167 drm_mode_set_name(mode);
79e53945
JB
10168
10169 return mode;
10170}
10171
652c393a
JB
10172static void intel_decrease_pllclock(struct drm_crtc *crtc)
10173{
10174 struct drm_device *dev = crtc->dev;
fbee40df 10175 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10176 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 10177
baff296c 10178 if (!HAS_GMCH_DISPLAY(dev))
652c393a
JB
10179 return;
10180
10181 if (!dev_priv->lvds_downclock_avail)
10182 return;
10183
10184 /*
10185 * Since this is called by a timer, we should never get here in
10186 * the manual case.
10187 */
10188 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
10189 int pipe = intel_crtc->pipe;
10190 int dpll_reg = DPLL(pipe);
10191 int dpll;
f6e5b160 10192
44d98a61 10193 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 10194
8ac5a6d5 10195 assert_panel_unlocked(dev_priv, pipe);
652c393a 10196
dc257cf1 10197 dpll = I915_READ(dpll_reg);
652c393a
JB
10198 dpll |= DISPLAY_RATE_SELECT_FPA1;
10199 I915_WRITE(dpll_reg, dpll);
9d0498a2 10200 intel_wait_for_vblank(dev, pipe);
652c393a
JB
10201 dpll = I915_READ(dpll_reg);
10202 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 10203 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
10204 }
10205
10206}
10207
f047e395
CW
10208void intel_mark_busy(struct drm_device *dev)
10209{
c67a470b
PZ
10210 struct drm_i915_private *dev_priv = dev->dev_private;
10211
f62a0076
CW
10212 if (dev_priv->mm.busy)
10213 return;
10214
43694d69 10215 intel_runtime_pm_get(dev_priv);
c67a470b 10216 i915_update_gfx_val(dev_priv);
43cf3bf0
CW
10217 if (INTEL_INFO(dev)->gen >= 6)
10218 gen6_rps_busy(dev_priv);
f62a0076 10219 dev_priv->mm.busy = true;
f047e395
CW
10220}
10221
10222void intel_mark_idle(struct drm_device *dev)
652c393a 10223{
c67a470b 10224 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 10225 struct drm_crtc *crtc;
652c393a 10226
f62a0076
CW
10227 if (!dev_priv->mm.busy)
10228 return;
10229
10230 dev_priv->mm.busy = false;
10231
70e1e0ec 10232 for_each_crtc(dev, crtc) {
f4510a27 10233 if (!crtc->primary->fb)
652c393a
JB
10234 continue;
10235
725a5b54 10236 intel_decrease_pllclock(crtc);
652c393a 10237 }
b29c19b6 10238
3d13ef2e 10239 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 10240 gen6_rps_idle(dev->dev_private);
bb4cdd53 10241
43694d69 10242 intel_runtime_pm_put(dev_priv);
652c393a
JB
10243}
10244
79e53945
JB
10245static void intel_crtc_destroy(struct drm_crtc *crtc)
10246{
10247 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
10248 struct drm_device *dev = crtc->dev;
10249 struct intel_unpin_work *work;
67e77c5a 10250
5e2d7afc 10251 spin_lock_irq(&dev->event_lock);
67e77c5a
DV
10252 work = intel_crtc->unpin_work;
10253 intel_crtc->unpin_work = NULL;
5e2d7afc 10254 spin_unlock_irq(&dev->event_lock);
67e77c5a
DV
10255
10256 if (work) {
10257 cancel_work_sync(&work->work);
10258 kfree(work);
10259 }
79e53945
JB
10260
10261 drm_crtc_cleanup(crtc);
67e77c5a 10262
79e53945
JB
10263 kfree(intel_crtc);
10264}
10265
6b95a207
KH
10266static void intel_unpin_work_fn(struct work_struct *__work)
10267{
10268 struct intel_unpin_work *work =
10269 container_of(__work, struct intel_unpin_work, work);
b4a98e57 10270 struct drm_device *dev = work->crtc->dev;
f99d7069 10271 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
6b95a207 10272
b4a98e57 10273 mutex_lock(&dev->struct_mutex);
82bc3b2d 10274 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
05394f39 10275 drm_gem_object_unreference(&work->pending_flip_obj->base);
d9e86c0e 10276
7ff0ebcc 10277 intel_fbc_update(dev);
f06cc1b9
JH
10278
10279 if (work->flip_queued_req)
146d84f0 10280 i915_gem_request_assign(&work->flip_queued_req, NULL);
b4a98e57
CW
10281 mutex_unlock(&dev->struct_mutex);
10282
f99d7069 10283 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
89ed88ba 10284 drm_framebuffer_unreference(work->old_fb);
f99d7069 10285
b4a98e57
CW
10286 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10287 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10288
6b95a207
KH
10289 kfree(work);
10290}
10291
1afe3e9d 10292static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 10293 struct drm_crtc *crtc)
6b95a207 10294{
6b95a207
KH
10295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10296 struct intel_unpin_work *work;
6b95a207
KH
10297 unsigned long flags;
10298
10299 /* Ignore early vblank irqs */
10300 if (intel_crtc == NULL)
10301 return;
10302
f326038a
DV
10303 /*
10304 * This is called both by irq handlers and the reset code (to complete
10305 * lost pageflips) so needs the full irqsave spinlocks.
10306 */
6b95a207
KH
10307 spin_lock_irqsave(&dev->event_lock, flags);
10308 work = intel_crtc->unpin_work;
e7d841ca
CW
10309
10310 /* Ensure we don't miss a work->pending update ... */
10311 smp_rmb();
10312
10313 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
10314 spin_unlock_irqrestore(&dev->event_lock, flags);
10315 return;
10316 }
10317
d6bbafa1 10318 page_flip_completed(intel_crtc);
0af7e4df 10319
6b95a207 10320 spin_unlock_irqrestore(&dev->event_lock, flags);
6b95a207
KH
10321}
10322
1afe3e9d
JB
10323void intel_finish_page_flip(struct drm_device *dev, int pipe)
10324{
fbee40df 10325 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10326 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10327
49b14a5c 10328 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10329}
10330
10331void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10332{
fbee40df 10333 struct drm_i915_private *dev_priv = dev->dev_private;
1afe3e9d
JB
10334 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10335
49b14a5c 10336 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
10337}
10338
75f7f3ec
VS
10339/* Is 'a' after or equal to 'b'? */
10340static bool g4x_flip_count_after_eq(u32 a, u32 b)
10341{
10342 return !((a - b) & 0x80000000);
10343}
10344
10345static bool page_flip_finished(struct intel_crtc *crtc)
10346{
10347 struct drm_device *dev = crtc->base.dev;
10348 struct drm_i915_private *dev_priv = dev->dev_private;
10349
bdfa7542
VS
10350 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10351 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10352 return true;
10353
75f7f3ec
VS
10354 /*
10355 * The relevant registers doen't exist on pre-ctg.
10356 * As the flip done interrupt doesn't trigger for mmio
10357 * flips on gmch platforms, a flip count check isn't
10358 * really needed there. But since ctg has the registers,
10359 * include it in the check anyway.
10360 */
10361 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10362 return true;
10363
10364 /*
10365 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10366 * used the same base address. In that case the mmio flip might
10367 * have completed, but the CS hasn't even executed the flip yet.
10368 *
10369 * A flip count check isn't enough as the CS might have updated
10370 * the base address just after start of vblank, but before we
10371 * managed to process the interrupt. This means we'd complete the
10372 * CS flip too soon.
10373 *
10374 * Combining both checks should get us a good enough result. It may
10375 * still happen that the CS flip has been executed, but has not
10376 * yet actually completed. But in case the base address is the same
10377 * anyway, we don't really care.
10378 */
10379 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10380 crtc->unpin_work->gtt_offset &&
10381 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10382 crtc->unpin_work->flip_count);
10383}
10384
6b95a207
KH
10385void intel_prepare_page_flip(struct drm_device *dev, int plane)
10386{
fbee40df 10387 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
10388 struct intel_crtc *intel_crtc =
10389 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10390 unsigned long flags;
10391
f326038a
DV
10392
10393 /*
10394 * This is called both by irq handlers and the reset code (to complete
10395 * lost pageflips) so needs the full irqsave spinlocks.
10396 *
10397 * NB: An MMIO update of the plane base pointer will also
e7d841ca
CW
10398 * generate a page-flip completion irq, i.e. every modeset
10399 * is also accompanied by a spurious intel_prepare_page_flip().
10400 */
6b95a207 10401 spin_lock_irqsave(&dev->event_lock, flags);
75f7f3ec 10402 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
e7d841ca 10403 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
10404 spin_unlock_irqrestore(&dev->event_lock, flags);
10405}
10406
eba905b2 10407static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
e7d841ca
CW
10408{
10409 /* Ensure that the work item is consistent when activating it ... */
10410 smp_wmb();
10411 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10412 /* and that it is marked active as soon as the irq could fire. */
10413 smp_wmb();
10414}
10415
8c9f3aaf
JB
10416static int intel_gen2_queue_flip(struct drm_device *dev,
10417 struct drm_crtc *crtc,
10418 struct drm_framebuffer *fb,
ed8d1975 10419 struct drm_i915_gem_object *obj,
a4872ba6 10420 struct intel_engine_cs *ring,
ed8d1975 10421 uint32_t flags)
8c9f3aaf 10422{
8c9f3aaf 10423 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10424 u32 flip_mask;
10425 int ret;
10426
6d90c952 10427 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10428 if (ret)
4fa62c89 10429 return ret;
8c9f3aaf
JB
10430
10431 /* Can't queue multiple flips, so wait for the previous
10432 * one to finish before executing the next.
10433 */
10434 if (intel_crtc->plane)
10435 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10436 else
10437 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10438 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10439 intel_ring_emit(ring, MI_NOOP);
10440 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10441 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10442 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10443 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952 10444 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
10445
10446 intel_mark_page_flip_active(intel_crtc);
09246732 10447 __intel_ring_advance(ring);
83d4092b 10448 return 0;
8c9f3aaf
JB
10449}
10450
10451static int intel_gen3_queue_flip(struct drm_device *dev,
10452 struct drm_crtc *crtc,
10453 struct drm_framebuffer *fb,
ed8d1975 10454 struct drm_i915_gem_object *obj,
a4872ba6 10455 struct intel_engine_cs *ring,
ed8d1975 10456 uint32_t flags)
8c9f3aaf 10457{
8c9f3aaf 10458 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf
JB
10459 u32 flip_mask;
10460 int ret;
10461
6d90c952 10462 ret = intel_ring_begin(ring, 6);
8c9f3aaf 10463 if (ret)
4fa62c89 10464 return ret;
8c9f3aaf
JB
10465
10466 if (intel_crtc->plane)
10467 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10468 else
10469 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
10470 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10471 intel_ring_emit(ring, MI_NOOP);
10472 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10473 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10474 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10475 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
6d90c952
DV
10476 intel_ring_emit(ring, MI_NOOP);
10477
e7d841ca 10478 intel_mark_page_flip_active(intel_crtc);
09246732 10479 __intel_ring_advance(ring);
83d4092b 10480 return 0;
8c9f3aaf
JB
10481}
10482
10483static int intel_gen4_queue_flip(struct drm_device *dev,
10484 struct drm_crtc *crtc,
10485 struct drm_framebuffer *fb,
ed8d1975 10486 struct drm_i915_gem_object *obj,
a4872ba6 10487 struct intel_engine_cs *ring,
ed8d1975 10488 uint32_t flags)
8c9f3aaf
JB
10489{
10490 struct drm_i915_private *dev_priv = dev->dev_private;
10491 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10492 uint32_t pf, pipesrc;
10493 int ret;
10494
6d90c952 10495 ret = intel_ring_begin(ring, 4);
8c9f3aaf 10496 if (ret)
4fa62c89 10497 return ret;
8c9f3aaf
JB
10498
10499 /* i965+ uses the linear or tiled offsets from the
10500 * Display Registers (which do not change across a page-flip)
10501 * so we need only reprogram the base address.
10502 */
6d90c952
DV
10503 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10504 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10505 intel_ring_emit(ring, fb->pitches[0]);
75f7f3ec 10506 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
c2c75131 10507 obj->tiling_mode);
8c9f3aaf
JB
10508
10509 /* XXX Enabling the panel-fitter across page-flip is so far
10510 * untested on non-native modes, so ignore it for now.
10511 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10512 */
10513 pf = 0;
10514 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10515 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10516
10517 intel_mark_page_flip_active(intel_crtc);
09246732 10518 __intel_ring_advance(ring);
83d4092b 10519 return 0;
8c9f3aaf
JB
10520}
10521
10522static int intel_gen6_queue_flip(struct drm_device *dev,
10523 struct drm_crtc *crtc,
10524 struct drm_framebuffer *fb,
ed8d1975 10525 struct drm_i915_gem_object *obj,
a4872ba6 10526 struct intel_engine_cs *ring,
ed8d1975 10527 uint32_t flags)
8c9f3aaf
JB
10528{
10529 struct drm_i915_private *dev_priv = dev->dev_private;
10530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10531 uint32_t pf, pipesrc;
10532 int ret;
10533
6d90c952 10534 ret = intel_ring_begin(ring, 4);
8c9f3aaf 10535 if (ret)
4fa62c89 10536 return ret;
8c9f3aaf 10537
6d90c952
DV
10538 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10539 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10540 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
75f7f3ec 10541 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
8c9f3aaf 10542
dc257cf1
DV
10543 /* Contrary to the suggestions in the documentation,
10544 * "Enable Panel Fitter" does not seem to be required when page
10545 * flipping with a non-native mode, and worse causes a normal
10546 * modeset to fail.
10547 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10548 */
10549 pf = 0;
8c9f3aaf 10550 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 10551 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
10552
10553 intel_mark_page_flip_active(intel_crtc);
09246732 10554 __intel_ring_advance(ring);
83d4092b 10555 return 0;
8c9f3aaf
JB
10556}
10557
7c9017e5
JB
10558static int intel_gen7_queue_flip(struct drm_device *dev,
10559 struct drm_crtc *crtc,
10560 struct drm_framebuffer *fb,
ed8d1975 10561 struct drm_i915_gem_object *obj,
a4872ba6 10562 struct intel_engine_cs *ring,
ed8d1975 10563 uint32_t flags)
7c9017e5 10564{
7c9017e5 10565 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cb05d8de 10566 uint32_t plane_bit = 0;
ffe74d75
CW
10567 int len, ret;
10568
eba905b2 10569 switch (intel_crtc->plane) {
cb05d8de
DV
10570 case PLANE_A:
10571 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10572 break;
10573 case PLANE_B:
10574 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10575 break;
10576 case PLANE_C:
10577 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10578 break;
10579 default:
10580 WARN_ONCE(1, "unknown plane in flip command\n");
4fa62c89 10581 return -ENODEV;
cb05d8de
DV
10582 }
10583
ffe74d75 10584 len = 4;
f476828a 10585 if (ring->id == RCS) {
ffe74d75 10586 len += 6;
f476828a
DL
10587 /*
10588 * On Gen 8, SRM is now taking an extra dword to accommodate
10589 * 48bits addresses, and we need a NOOP for the batch size to
10590 * stay even.
10591 */
10592 if (IS_GEN8(dev))
10593 len += 2;
10594 }
ffe74d75 10595
f66fab8e
VS
10596 /*
10597 * BSpec MI_DISPLAY_FLIP for IVB:
10598 * "The full packet must be contained within the same cache line."
10599 *
10600 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10601 * cacheline, if we ever start emitting more commands before
10602 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10603 * then do the cacheline alignment, and finally emit the
10604 * MI_DISPLAY_FLIP.
10605 */
10606 ret = intel_ring_cacheline_align(ring);
10607 if (ret)
4fa62c89 10608 return ret;
f66fab8e 10609
ffe74d75 10610 ret = intel_ring_begin(ring, len);
7c9017e5 10611 if (ret)
4fa62c89 10612 return ret;
7c9017e5 10613
ffe74d75
CW
10614 /* Unmask the flip-done completion message. Note that the bspec says that
10615 * we should do this for both the BCS and RCS, and that we must not unmask
10616 * more than one flip event at any time (or ensure that one flip message
10617 * can be sent by waiting for flip-done prior to queueing new flips).
10618 * Experimentation says that BCS works despite DERRMR masking all
10619 * flip-done completion events and that unmasking all planes at once
10620 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10621 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10622 */
10623 if (ring->id == RCS) {
10624 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10625 intel_ring_emit(ring, DERRMR);
10626 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10627 DERRMR_PIPEB_PRI_FLIP_DONE |
10628 DERRMR_PIPEC_PRI_FLIP_DONE));
f476828a
DL
10629 if (IS_GEN8(dev))
10630 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10631 MI_SRM_LRM_GLOBAL_GTT);
10632 else
10633 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10634 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
10635 intel_ring_emit(ring, DERRMR);
10636 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
f476828a
DL
10637 if (IS_GEN8(dev)) {
10638 intel_ring_emit(ring, 0);
10639 intel_ring_emit(ring, MI_NOOP);
10640 }
ffe74d75
CW
10641 }
10642
cb05d8de 10643 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 10644 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
75f7f3ec 10645 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
7c9017e5 10646 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
10647
10648 intel_mark_page_flip_active(intel_crtc);
09246732 10649 __intel_ring_advance(ring);
83d4092b 10650 return 0;
7c9017e5
JB
10651}
10652
84c33a64
SG
10653static bool use_mmio_flip(struct intel_engine_cs *ring,
10654 struct drm_i915_gem_object *obj)
10655{
10656 /*
10657 * This is not being used for older platforms, because
10658 * non-availability of flip done interrupt forces us to use
10659 * CS flips. Older platforms derive flip done using some clever
10660 * tricks involving the flip_pending status bits and vblank irqs.
10661 * So using MMIO flips there would disrupt this mechanism.
10662 */
10663
8e09bf83
CW
10664 if (ring == NULL)
10665 return true;
10666
84c33a64
SG
10667 if (INTEL_INFO(ring->dev)->gen < 5)
10668 return false;
10669
10670 if (i915.use_mmio_flip < 0)
10671 return false;
10672 else if (i915.use_mmio_flip > 0)
10673 return true;
14bf993e
OM
10674 else if (i915.enable_execlists)
10675 return true;
84c33a64 10676 else
41c52415 10677 return ring != i915_gem_request_get_ring(obj->last_read_req);
84c33a64
SG
10678}
10679
ff944564
DL
10680static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10681{
10682 struct drm_device *dev = intel_crtc->base.dev;
10683 struct drm_i915_private *dev_priv = dev->dev_private;
10684 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
ff944564
DL
10685 const enum pipe pipe = intel_crtc->pipe;
10686 u32 ctl, stride;
10687
10688 ctl = I915_READ(PLANE_CTL(pipe, 0));
10689 ctl &= ~PLANE_CTL_TILED_MASK;
2ebef630
TU
10690 switch (fb->modifier[0]) {
10691 case DRM_FORMAT_MOD_NONE:
10692 break;
10693 case I915_FORMAT_MOD_X_TILED:
ff944564 10694 ctl |= PLANE_CTL_TILED_X;
2ebef630
TU
10695 break;
10696 case I915_FORMAT_MOD_Y_TILED:
10697 ctl |= PLANE_CTL_TILED_Y;
10698 break;
10699 case I915_FORMAT_MOD_Yf_TILED:
10700 ctl |= PLANE_CTL_TILED_YF;
10701 break;
10702 default:
10703 MISSING_CASE(fb->modifier[0]);
10704 }
ff944564
DL
10705
10706 /*
10707 * The stride is either expressed as a multiple of 64 bytes chunks for
10708 * linear buffers or in number of tiles for tiled buffers.
10709 */
2ebef630
TU
10710 stride = fb->pitches[0] /
10711 intel_fb_stride_alignment(dev, fb->modifier[0],
10712 fb->pixel_format);
ff944564
DL
10713
10714 /*
10715 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10716 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10717 */
10718 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10719 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10720
10721 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10722 POSTING_READ(PLANE_SURF(pipe, 0));
10723}
10724
10725static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
84c33a64
SG
10726{
10727 struct drm_device *dev = intel_crtc->base.dev;
10728 struct drm_i915_private *dev_priv = dev->dev_private;
10729 struct intel_framebuffer *intel_fb =
10730 to_intel_framebuffer(intel_crtc->base.primary->fb);
10731 struct drm_i915_gem_object *obj = intel_fb->obj;
10732 u32 dspcntr;
10733 u32 reg;
10734
84c33a64
SG
10735 reg = DSPCNTR(intel_crtc->plane);
10736 dspcntr = I915_READ(reg);
10737
c5d97472
DL
10738 if (obj->tiling_mode != I915_TILING_NONE)
10739 dspcntr |= DISPPLANE_TILED;
10740 else
10741 dspcntr &= ~DISPPLANE_TILED;
10742
84c33a64
SG
10743 I915_WRITE(reg, dspcntr);
10744
10745 I915_WRITE(DSPSURF(intel_crtc->plane),
10746 intel_crtc->unpin_work->gtt_offset);
10747 POSTING_READ(DSPSURF(intel_crtc->plane));
84c33a64 10748
ff944564
DL
10749}
10750
10751/*
10752 * XXX: This is the temporary way to update the plane registers until we get
10753 * around to using the usual plane update functions for MMIO flips
10754 */
10755static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10756{
10757 struct drm_device *dev = intel_crtc->base.dev;
10758 bool atomic_update;
10759 u32 start_vbl_count;
10760
10761 intel_mark_page_flip_active(intel_crtc);
10762
10763 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10764
10765 if (INTEL_INFO(dev)->gen >= 9)
10766 skl_do_mmio_flip(intel_crtc);
10767 else
10768 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10769 ilk_do_mmio_flip(intel_crtc);
10770
9362c7c5
ACO
10771 if (atomic_update)
10772 intel_pipe_update_end(intel_crtc, start_vbl_count);
84c33a64
SG
10773}
10774
9362c7c5 10775static void intel_mmio_flip_work_func(struct work_struct *work)
84c33a64 10776{
b2cfe0ab
CW
10777 struct intel_mmio_flip *mmio_flip =
10778 container_of(work, struct intel_mmio_flip, work);
84c33a64 10779
b2cfe0ab
CW
10780 if (mmio_flip->rq)
10781 WARN_ON(__i915_wait_request(mmio_flip->rq,
10782 mmio_flip->crtc->reset_counter,
10783 false, NULL, NULL));
84c33a64 10784
b2cfe0ab
CW
10785 intel_do_mmio_flip(mmio_flip->crtc);
10786
10787 i915_gem_request_unreference__unlocked(mmio_flip->rq);
10788 kfree(mmio_flip);
84c33a64
SG
10789}
10790
10791static int intel_queue_mmio_flip(struct drm_device *dev,
10792 struct drm_crtc *crtc,
10793 struct drm_framebuffer *fb,
10794 struct drm_i915_gem_object *obj,
10795 struct intel_engine_cs *ring,
10796 uint32_t flags)
10797{
b2cfe0ab
CW
10798 struct intel_mmio_flip *mmio_flip;
10799
10800 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
10801 if (mmio_flip == NULL)
10802 return -ENOMEM;
84c33a64 10803
b2cfe0ab
CW
10804 mmio_flip->rq = i915_gem_request_reference(obj->last_write_req);
10805 mmio_flip->crtc = to_intel_crtc(crtc);
536f5b5e 10806
b2cfe0ab
CW
10807 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
10808 schedule_work(&mmio_flip->work);
84c33a64 10809
84c33a64
SG
10810 return 0;
10811}
10812
8c9f3aaf
JB
10813static int intel_default_queue_flip(struct drm_device *dev,
10814 struct drm_crtc *crtc,
10815 struct drm_framebuffer *fb,
ed8d1975 10816 struct drm_i915_gem_object *obj,
a4872ba6 10817 struct intel_engine_cs *ring,
ed8d1975 10818 uint32_t flags)
8c9f3aaf
JB
10819{
10820 return -ENODEV;
10821}
10822
d6bbafa1
CW
10823static bool __intel_pageflip_stall_check(struct drm_device *dev,
10824 struct drm_crtc *crtc)
10825{
10826 struct drm_i915_private *dev_priv = dev->dev_private;
10827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10828 struct intel_unpin_work *work = intel_crtc->unpin_work;
10829 u32 addr;
10830
10831 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10832 return true;
10833
10834 if (!work->enable_stall_check)
10835 return false;
10836
10837 if (work->flip_ready_vblank == 0) {
3a8a946e
DV
10838 if (work->flip_queued_req &&
10839 !i915_gem_request_completed(work->flip_queued_req, true))
d6bbafa1
CW
10840 return false;
10841
1e3feefd 10842 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1
CW
10843 }
10844
1e3feefd 10845 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
d6bbafa1
CW
10846 return false;
10847
10848 /* Potential stall - if we see that the flip has happened,
10849 * assume a missed interrupt. */
10850 if (INTEL_INFO(dev)->gen >= 4)
10851 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10852 else
10853 addr = I915_READ(DSPADDR(intel_crtc->plane));
10854
10855 /* There is a potential issue here with a false positive after a flip
10856 * to the same address. We could address this by checking for a
10857 * non-incrementing frame counter.
10858 */
10859 return addr == work->gtt_offset;
10860}
10861
10862void intel_check_page_flip(struct drm_device *dev, int pipe)
10863{
10864 struct drm_i915_private *dev_priv = dev->dev_private;
10865 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10866 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6ad790c0 10867 struct intel_unpin_work *work;
f326038a 10868
6c51d46f 10869 WARN_ON(!in_interrupt());
d6bbafa1
CW
10870
10871 if (crtc == NULL)
10872 return;
10873
f326038a 10874 spin_lock(&dev->event_lock);
6ad790c0
CW
10875 work = intel_crtc->unpin_work;
10876 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
d6bbafa1 10877 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
6ad790c0 10878 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
d6bbafa1 10879 page_flip_completed(intel_crtc);
6ad790c0 10880 work = NULL;
d6bbafa1 10881 }
6ad790c0
CW
10882 if (work != NULL &&
10883 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10884 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
f326038a 10885 spin_unlock(&dev->event_lock);
d6bbafa1
CW
10886}
10887
6b95a207
KH
10888static int intel_crtc_page_flip(struct drm_crtc *crtc,
10889 struct drm_framebuffer *fb,
ed8d1975
KP
10890 struct drm_pending_vblank_event *event,
10891 uint32_t page_flip_flags)
6b95a207
KH
10892{
10893 struct drm_device *dev = crtc->dev;
10894 struct drm_i915_private *dev_priv = dev->dev_private;
f4510a27 10895 struct drm_framebuffer *old_fb = crtc->primary->fb;
2ff8fde1 10896 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
6b95a207 10897 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
455a6808 10898 struct drm_plane *primary = crtc->primary;
a071fa00 10899 enum pipe pipe = intel_crtc->pipe;
6b95a207 10900 struct intel_unpin_work *work;
a4872ba6 10901 struct intel_engine_cs *ring;
cf5d8a46 10902 bool mmio_flip;
52e68630 10903 int ret;
6b95a207 10904
2ff8fde1
MR
10905 /*
10906 * drm_mode_page_flip_ioctl() should already catch this, but double
10907 * check to be safe. In the future we may enable pageflipping from
10908 * a disabled primary plane.
10909 */
10910 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10911 return -EBUSY;
10912
e6a595d2 10913 /* Can't change pixel format via MI display flips. */
f4510a27 10914 if (fb->pixel_format != crtc->primary->fb->pixel_format)
e6a595d2
VS
10915 return -EINVAL;
10916
10917 /*
10918 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10919 * Note that pitch changes could also affect these register.
10920 */
10921 if (INTEL_INFO(dev)->gen > 3 &&
f4510a27
MR
10922 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10923 fb->pitches[0] != crtc->primary->fb->pitches[0]))
e6a595d2
VS
10924 return -EINVAL;
10925
f900db47
CW
10926 if (i915_terminally_wedged(&dev_priv->gpu_error))
10927 goto out_hang;
10928
b14c5679 10929 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
10930 if (work == NULL)
10931 return -ENOMEM;
10932
6b95a207 10933 work->event = event;
b4a98e57 10934 work->crtc = crtc;
ab8d6675 10935 work->old_fb = old_fb;
6b95a207
KH
10936 INIT_WORK(&work->work, intel_unpin_work_fn);
10937
87b6b101 10938 ret = drm_crtc_vblank_get(crtc);
7317c75e
JB
10939 if (ret)
10940 goto free_work;
10941
6b95a207 10942 /* We borrow the event spin lock for protecting unpin_work */
5e2d7afc 10943 spin_lock_irq(&dev->event_lock);
6b95a207 10944 if (intel_crtc->unpin_work) {
d6bbafa1
CW
10945 /* Before declaring the flip queue wedged, check if
10946 * the hardware completed the operation behind our backs.
10947 */
10948 if (__intel_pageflip_stall_check(dev, crtc)) {
10949 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10950 page_flip_completed(intel_crtc);
10951 } else {
10952 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
5e2d7afc 10953 spin_unlock_irq(&dev->event_lock);
468f0b44 10954
d6bbafa1
CW
10955 drm_crtc_vblank_put(crtc);
10956 kfree(work);
10957 return -EBUSY;
10958 }
6b95a207
KH
10959 }
10960 intel_crtc->unpin_work = work;
5e2d7afc 10961 spin_unlock_irq(&dev->event_lock);
6b95a207 10962
b4a98e57
CW
10963 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10964 flush_workqueue(dev_priv->wq);
10965
75dfca80 10966 /* Reference the objects for the scheduled work. */
ab8d6675 10967 drm_framebuffer_reference(work->old_fb);
05394f39 10968 drm_gem_object_reference(&obj->base);
6b95a207 10969
f4510a27 10970 crtc->primary->fb = fb;
afd65eb4 10971 update_state_fb(crtc->primary);
1ed1f968 10972
e1f99ce6 10973 work->pending_flip_obj = obj;
e1f99ce6 10974
89ed88ba
CW
10975 ret = i915_mutex_lock_interruptible(dev);
10976 if (ret)
10977 goto cleanup;
10978
b4a98e57 10979 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 10980 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 10981
75f7f3ec 10982 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
a071fa00 10983 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
75f7f3ec 10984
4fa62c89
VS
10985 if (IS_VALLEYVIEW(dev)) {
10986 ring = &dev_priv->ring[BCS];
ab8d6675 10987 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
8e09bf83
CW
10988 /* vlv: DISPLAY_FLIP fails to change tiling */
10989 ring = NULL;
48bf5b2d 10990 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
2a92d5bc 10991 ring = &dev_priv->ring[BCS];
4fa62c89 10992 } else if (INTEL_INFO(dev)->gen >= 7) {
41c52415 10993 ring = i915_gem_request_get_ring(obj->last_read_req);
4fa62c89
VS
10994 if (ring == NULL || ring->id != RCS)
10995 ring = &dev_priv->ring[BCS];
10996 } else {
10997 ring = &dev_priv->ring[RCS];
10998 }
10999
cf5d8a46
CW
11000 mmio_flip = use_mmio_flip(ring, obj);
11001
11002 /* When using CS flips, we want to emit semaphores between rings.
11003 * However, when using mmio flips we will create a task to do the
11004 * synchronisation, so all we want here is to pin the framebuffer
11005 * into the display plane and skip any waits.
11006 */
82bc3b2d 11007 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
cf5d8a46
CW
11008 crtc->primary->state,
11009 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
8c9f3aaf
JB
11010 if (ret)
11011 goto cleanup_pending;
6b95a207 11012
121920fa
TU
11013 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11014 + intel_crtc->dspaddr_offset;
4fa62c89 11015
cf5d8a46 11016 if (mmio_flip) {
84c33a64
SG
11017 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11018 page_flip_flags);
d6bbafa1
CW
11019 if (ret)
11020 goto cleanup_unpin;
11021
f06cc1b9
JH
11022 i915_gem_request_assign(&work->flip_queued_req,
11023 obj->last_write_req);
d6bbafa1 11024 } else {
d94b5030
CW
11025 if (obj->last_write_req) {
11026 ret = i915_gem_check_olr(obj->last_write_req);
11027 if (ret)
11028 goto cleanup_unpin;
11029 }
11030
84c33a64 11031 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
d6bbafa1
CW
11032 page_flip_flags);
11033 if (ret)
11034 goto cleanup_unpin;
11035
f06cc1b9
JH
11036 i915_gem_request_assign(&work->flip_queued_req,
11037 intel_ring_get_request(ring));
d6bbafa1
CW
11038 }
11039
1e3feefd 11040 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
d6bbafa1 11041 work->enable_stall_check = true;
4fa62c89 11042
ab8d6675 11043 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
a071fa00
DV
11044 INTEL_FRONTBUFFER_PRIMARY(pipe));
11045
7ff0ebcc 11046 intel_fbc_disable(dev);
f99d7069 11047 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
6b95a207
KH
11048 mutex_unlock(&dev->struct_mutex);
11049
e5510fac
JB
11050 trace_i915_flip_request(intel_crtc->plane, obj);
11051
6b95a207 11052 return 0;
96b099fd 11053
4fa62c89 11054cleanup_unpin:
82bc3b2d 11055 intel_unpin_fb_obj(fb, crtc->primary->state);
8c9f3aaf 11056cleanup_pending:
b4a98e57 11057 atomic_dec(&intel_crtc->unpin_work_count);
89ed88ba
CW
11058 mutex_unlock(&dev->struct_mutex);
11059cleanup:
f4510a27 11060 crtc->primary->fb = old_fb;
afd65eb4 11061 update_state_fb(crtc->primary);
89ed88ba
CW
11062
11063 drm_gem_object_unreference_unlocked(&obj->base);
ab8d6675 11064 drm_framebuffer_unreference(work->old_fb);
96b099fd 11065
5e2d7afc 11066 spin_lock_irq(&dev->event_lock);
96b099fd 11067 intel_crtc->unpin_work = NULL;
5e2d7afc 11068 spin_unlock_irq(&dev->event_lock);
96b099fd 11069
87b6b101 11070 drm_crtc_vblank_put(crtc);
7317c75e 11071free_work:
96b099fd
CW
11072 kfree(work);
11073
f900db47
CW
11074 if (ret == -EIO) {
11075out_hang:
53a366b9 11076 ret = intel_plane_restore(primary);
f0d3dad3 11077 if (ret == 0 && event) {
5e2d7afc 11078 spin_lock_irq(&dev->event_lock);
a071fa00 11079 drm_send_vblank_event(dev, pipe, event);
5e2d7afc 11080 spin_unlock_irq(&dev->event_lock);
f0d3dad3 11081 }
f900db47 11082 }
96b099fd 11083 return ret;
6b95a207
KH
11084}
11085
65b38e0d 11086static const struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
11087 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11088 .load_lut = intel_crtc_load_lut,
ea2c67bb
MR
11089 .atomic_begin = intel_begin_crtc_commit,
11090 .atomic_flush = intel_finish_crtc_commit,
f6e5b160
CW
11091};
11092
9a935856
DV
11093/**
11094 * intel_modeset_update_staged_output_state
11095 *
11096 * Updates the staged output configuration state, e.g. after we've read out the
11097 * current hw state.
11098 */
11099static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 11100{
7668851f 11101 struct intel_crtc *crtc;
9a935856
DV
11102 struct intel_encoder *encoder;
11103 struct intel_connector *connector;
f6e5b160 11104
3a3371ff 11105 for_each_intel_connector(dev, connector) {
9a935856
DV
11106 connector->new_encoder =
11107 to_intel_encoder(connector->base.encoder);
11108 }
f6e5b160 11109
b2784e15 11110 for_each_intel_encoder(dev, encoder) {
9a935856
DV
11111 encoder->new_crtc =
11112 to_intel_crtc(encoder->base.crtc);
11113 }
7668851f 11114
d3fcc808 11115 for_each_intel_crtc(dev, crtc) {
83d65738 11116 crtc->new_enabled = crtc->base.state->enable;
7668851f 11117 }
f6e5b160
CW
11118}
11119
d29b2f9d
ACO
11120/* Transitional helper to copy current connector/encoder state to
11121 * connector->state. This is needed so that code that is partially
11122 * converted to atomic does the right thing.
11123 */
11124static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11125{
11126 struct intel_connector *connector;
11127
11128 for_each_intel_connector(dev, connector) {
11129 if (connector->base.encoder) {
11130 connector->base.state->best_encoder =
11131 connector->base.encoder;
11132 connector->base.state->crtc =
11133 connector->base.encoder->crtc;
11134 } else {
11135 connector->base.state->best_encoder = NULL;
11136 connector->base.state->crtc = NULL;
11137 }
11138 }
11139}
11140
a821fc46 11141/* Fixup legacy state after an atomic state swap.
9a935856 11142 */
a821fc46 11143static void intel_modeset_fixup_state(struct drm_atomic_state *state)
9a935856 11144{
a821fc46 11145 struct intel_crtc *crtc;
9a935856 11146 struct intel_encoder *encoder;
a821fc46 11147 struct intel_connector *connector;
d5432a9d 11148
a821fc46
ACO
11149 for_each_intel_connector(state->dev, connector) {
11150 connector->base.encoder = connector->base.state->best_encoder;
11151 if (connector->base.encoder)
11152 connector->base.encoder->crtc =
11153 connector->base.state->crtc;
9a935856 11154 }
f6e5b160 11155
d5432a9d
ACO
11156 /* Update crtc of disabled encoders */
11157 for_each_intel_encoder(state->dev, encoder) {
11158 int num_connectors = 0;
11159
a821fc46
ACO
11160 for_each_intel_connector(state->dev, connector)
11161 if (connector->base.encoder == &encoder->base)
d5432a9d
ACO
11162 num_connectors++;
11163
11164 if (num_connectors == 0)
11165 encoder->base.crtc = NULL;
9a935856 11166 }
7668851f 11167
a821fc46
ACO
11168 for_each_intel_crtc(state->dev, crtc) {
11169 crtc->base.enabled = crtc->base.state->enable;
11170 crtc->config = to_intel_crtc_state(crtc->base.state);
7668851f 11171 }
d29b2f9d 11172
d5432a9d
ACO
11173 /* Copy the new configuration to the staged state, to keep the few
11174 * pieces of code that haven't been converted yet happy */
11175 intel_modeset_update_staged_output_state(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);
0ec463d3
TU
11297 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11298 crtc->num_scalers,
11299 pipe_config->scaler_state.scaler_users,
11300 pipe_config->scaler_state.scaler_id);
c0b03411
DV
11301 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11302 pipe_config->gmch_pfit.control,
11303 pipe_config->gmch_pfit.pgm_ratios,
11304 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 11305 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 11306 pipe_config->pch_pfit.pos,
fd4daa9c
CW
11307 pipe_config->pch_pfit.size,
11308 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 11309 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 11310 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
6a60cd87 11311
415ff0f6
TU
11312 if (IS_BROXTON(dev)) {
11313 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11314 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11315 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11316 pipe_config->ddi_pll_sel,
11317 pipe_config->dpll_hw_state.ebb0,
11318 pipe_config->dpll_hw_state.pll0,
11319 pipe_config->dpll_hw_state.pll1,
11320 pipe_config->dpll_hw_state.pll2,
11321 pipe_config->dpll_hw_state.pll3,
11322 pipe_config->dpll_hw_state.pll6,
11323 pipe_config->dpll_hw_state.pll8,
11324 pipe_config->dpll_hw_state.pcsdw12);
11325 } else if (IS_SKYLAKE(dev)) {
11326 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11327 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11328 pipe_config->ddi_pll_sel,
11329 pipe_config->dpll_hw_state.ctrl1,
11330 pipe_config->dpll_hw_state.cfgcr1,
11331 pipe_config->dpll_hw_state.cfgcr2);
11332 } else if (HAS_DDI(dev)) {
11333 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11334 pipe_config->ddi_pll_sel,
11335 pipe_config->dpll_hw_state.wrpll);
11336 } else {
11337 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11338 "fp0: 0x%x, fp1: 0x%x\n",
11339 pipe_config->dpll_hw_state.dpll,
11340 pipe_config->dpll_hw_state.dpll_md,
11341 pipe_config->dpll_hw_state.fp0,
11342 pipe_config->dpll_hw_state.fp1);
11343 }
11344
6a60cd87
CK
11345 DRM_DEBUG_KMS("planes on this crtc\n");
11346 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11347 intel_plane = to_intel_plane(plane);
11348 if (intel_plane->pipe != crtc->pipe)
11349 continue;
11350
11351 state = to_intel_plane_state(plane->state);
11352 fb = state->base.fb;
11353 if (!fb) {
11354 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11355 "disabled, scaler_id = %d\n",
11356 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11357 plane->base.id, intel_plane->pipe,
11358 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11359 drm_plane_index(plane), state->scaler_id);
11360 continue;
11361 }
11362
11363 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11364 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11365 plane->base.id, intel_plane->pipe,
11366 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11367 drm_plane_index(plane));
11368 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11369 fb->base.id, fb->width, fb->height, fb->pixel_format);
11370 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11371 state->scaler_id,
11372 state->src.x1 >> 16, state->src.y1 >> 16,
11373 drm_rect_width(&state->src) >> 16,
11374 drm_rect_height(&state->src) >> 16,
11375 state->dst.x1, state->dst.y1,
11376 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11377 }
c0b03411
DV
11378}
11379
bc079e8b
VS
11380static bool encoders_cloneable(const struct intel_encoder *a,
11381 const struct intel_encoder *b)
accfc0c5 11382{
bc079e8b
VS
11383 /* masks could be asymmetric, so check both ways */
11384 return a == b || (a->cloneable & (1 << b->type) &&
11385 b->cloneable & (1 << a->type));
11386}
11387
98a221da
ACO
11388static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11389 struct intel_crtc *crtc,
bc079e8b
VS
11390 struct intel_encoder *encoder)
11391{
bc079e8b 11392 struct intel_encoder *source_encoder;
da3ced29 11393 struct drm_connector *connector;
98a221da
ACO
11394 struct drm_connector_state *connector_state;
11395 int i;
bc079e8b 11396
da3ced29 11397 for_each_connector_in_state(state, connector, connector_state, i) {
98a221da 11398 if (connector_state->crtc != &crtc->base)
bc079e8b
VS
11399 continue;
11400
98a221da
ACO
11401 source_encoder =
11402 to_intel_encoder(connector_state->best_encoder);
bc079e8b
VS
11403 if (!encoders_cloneable(encoder, source_encoder))
11404 return false;
11405 }
11406
11407 return true;
11408}
11409
98a221da
ACO
11410static bool check_encoder_cloning(struct drm_atomic_state *state,
11411 struct intel_crtc *crtc)
bc079e8b 11412{
accfc0c5 11413 struct intel_encoder *encoder;
da3ced29 11414 struct drm_connector *connector;
98a221da
ACO
11415 struct drm_connector_state *connector_state;
11416 int i;
accfc0c5 11417
da3ced29 11418 for_each_connector_in_state(state, connector, connector_state, i) {
98a221da
ACO
11419 if (connector_state->crtc != &crtc->base)
11420 continue;
11421
11422 encoder = to_intel_encoder(connector_state->best_encoder);
11423 if (!check_single_encoder_cloning(state, crtc, encoder))
bc079e8b 11424 return false;
accfc0c5
DV
11425 }
11426
bc079e8b 11427 return true;
accfc0c5
DV
11428}
11429
5448a00d 11430static bool check_digital_port_conflicts(struct drm_atomic_state *state)
00f0b378 11431{
5448a00d
ACO
11432 struct drm_device *dev = state->dev;
11433 struct intel_encoder *encoder;
da3ced29 11434 struct drm_connector *connector;
5448a00d 11435 struct drm_connector_state *connector_state;
00f0b378 11436 unsigned int used_ports = 0;
5448a00d 11437 int i;
00f0b378
VS
11438
11439 /*
11440 * Walk the connector list instead of the encoder
11441 * list to detect the problem on ddi platforms
11442 * where there's just one encoder per digital port.
11443 */
da3ced29 11444 for_each_connector_in_state(state, connector, connector_state, i) {
5448a00d 11445 if (!connector_state->best_encoder)
00f0b378
VS
11446 continue;
11447
5448a00d
ACO
11448 encoder = to_intel_encoder(connector_state->best_encoder);
11449
11450 WARN_ON(!connector_state->crtc);
00f0b378
VS
11451
11452 switch (encoder->type) {
11453 unsigned int port_mask;
11454 case INTEL_OUTPUT_UNKNOWN:
11455 if (WARN_ON(!HAS_DDI(dev)))
11456 break;
11457 case INTEL_OUTPUT_DISPLAYPORT:
11458 case INTEL_OUTPUT_HDMI:
11459 case INTEL_OUTPUT_EDP:
11460 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11461
11462 /* the same port mustn't appear more than once */
11463 if (used_ports & port_mask)
11464 return false;
11465
11466 used_ports |= port_mask;
11467 default:
11468 break;
11469 }
11470 }
11471
11472 return true;
11473}
11474
83a57153
ACO
11475static void
11476clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11477{
11478 struct drm_crtc_state tmp_state;
663a3640 11479 struct intel_crtc_scaler_state scaler_state;
4978cc93
ACO
11480 struct intel_dpll_hw_state dpll_hw_state;
11481 enum intel_dpll_id shared_dpll;
8504c74c 11482 uint32_t ddi_pll_sel;
83a57153 11483
663a3640 11484 /* Clear only the intel specific part of the crtc state excluding scalers */
83a57153 11485 tmp_state = crtc_state->base;
663a3640 11486 scaler_state = crtc_state->scaler_state;
4978cc93
ACO
11487 shared_dpll = crtc_state->shared_dpll;
11488 dpll_hw_state = crtc_state->dpll_hw_state;
8504c74c 11489 ddi_pll_sel = crtc_state->ddi_pll_sel;
4978cc93 11490
83a57153 11491 memset(crtc_state, 0, sizeof *crtc_state);
4978cc93 11492
83a57153 11493 crtc_state->base = tmp_state;
663a3640 11494 crtc_state->scaler_state = scaler_state;
4978cc93
ACO
11495 crtc_state->shared_dpll = shared_dpll;
11496 crtc_state->dpll_hw_state = dpll_hw_state;
8504c74c 11497 crtc_state->ddi_pll_sel = ddi_pll_sel;
83a57153
ACO
11498}
11499
548ee15b 11500static int
b8cecdf5 11501intel_modeset_pipe_config(struct drm_crtc *crtc,
548ee15b
ACO
11502 struct drm_atomic_state *state,
11503 struct intel_crtc_state *pipe_config)
ee7b9f93 11504{
7758a113 11505 struct intel_encoder *encoder;
da3ced29 11506 struct drm_connector *connector;
0b901879 11507 struct drm_connector_state *connector_state;
d328c9d7 11508 int base_bpp, ret = -EINVAL;
0b901879 11509 int i;
e29c22c0 11510 bool retry = true;
ee7b9f93 11511
98a221da 11512 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
accfc0c5 11513 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
548ee15b 11514 return -EINVAL;
accfc0c5
DV
11515 }
11516
5448a00d 11517 if (!check_digital_port_conflicts(state)) {
00f0b378 11518 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
548ee15b 11519 return -EINVAL;
00f0b378
VS
11520 }
11521
83a57153 11522 clear_intel_crtc_state(pipe_config);
7758a113 11523
e143a21c
DV
11524 pipe_config->cpu_transcoder =
11525 (enum transcoder) to_intel_crtc(crtc)->pipe;
b8cecdf5 11526
2960bc9c
ID
11527 /*
11528 * Sanitize sync polarity flags based on requested ones. If neither
11529 * positive or negative polarity is requested, treat this as meaning
11530 * negative polarity.
11531 */
2d112de7 11532 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11533 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
2d112de7 11534 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
2960bc9c 11535
2d112de7 11536 if (!(pipe_config->base.adjusted_mode.flags &
2960bc9c 11537 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
2d112de7 11538 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
2960bc9c 11539
050f7aeb
DV
11540 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11541 * plane pixel format and any sink constraints into account. Returns the
11542 * source plane bpp so that dithering can be selected on mismatches
11543 * after encoders and crtc also have had their say. */
d328c9d7
DV
11544 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11545 pipe_config);
11546 if (base_bpp < 0)
4e53c2e0
DV
11547 goto fail;
11548
e41a56be
VS
11549 /*
11550 * Determine the real pipe dimensions. Note that stereo modes can
11551 * increase the actual pipe size due to the frame doubling and
11552 * insertion of additional space for blanks between the frame. This
11553 * is stored in the crtc timings. We use the requested mode to do this
11554 * computation to clearly distinguish it from the adjusted mode, which
11555 * can be changed by the connectors in the below retry loop.
11556 */
2d112de7 11557 drm_crtc_get_hv_timing(&pipe_config->base.mode,
ecb7e16b
GP
11558 &pipe_config->pipe_src_w,
11559 &pipe_config->pipe_src_h);
e41a56be 11560
e29c22c0 11561encoder_retry:
ef1b460d 11562 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 11563 pipe_config->port_clock = 0;
ef1b460d 11564 pipe_config->pixel_multiplier = 1;
ff9a6750 11565
135c81b8 11566 /* Fill in default crtc timings, allow encoders to overwrite them. */
2d112de7
ACO
11567 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11568 CRTC_STEREO_DOUBLE);
135c81b8 11569
7758a113
DV
11570 /* Pass our mode to the connectors and the CRTC to give them a chance to
11571 * adjust it according to limitations or connector properties, and also
11572 * a chance to reject the mode entirely.
47f1c6c9 11573 */
da3ced29 11574 for_each_connector_in_state(state, connector, connector_state, i) {
0b901879 11575 if (connector_state->crtc != crtc)
7758a113 11576 continue;
7ae89233 11577
0b901879
ACO
11578 encoder = to_intel_encoder(connector_state->best_encoder);
11579
efea6e8e
DV
11580 if (!(encoder->compute_config(encoder, pipe_config))) {
11581 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
11582 goto fail;
11583 }
ee7b9f93 11584 }
47f1c6c9 11585
ff9a6750
DV
11586 /* Set default port clock if not overwritten by the encoder. Needs to be
11587 * done afterwards in case the encoder adjusts the mode. */
11588 if (!pipe_config->port_clock)
2d112de7 11589 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
241bfc38 11590 * pipe_config->pixel_multiplier;
ff9a6750 11591
a43f6e0f 11592 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 11593 if (ret < 0) {
7758a113
DV
11594 DRM_DEBUG_KMS("CRTC fixup failed\n");
11595 goto fail;
ee7b9f93 11596 }
e29c22c0
DV
11597
11598 if (ret == RETRY) {
11599 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11600 ret = -EINVAL;
11601 goto fail;
11602 }
11603
11604 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11605 retry = false;
11606 goto encoder_retry;
11607 }
11608
d328c9d7 11609 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
4e53c2e0 11610 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
d328c9d7 11611 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
4e53c2e0 11612
548ee15b 11613 return 0;
7758a113 11614fail:
548ee15b 11615 return ret;
ee7b9f93 11616}
47f1c6c9 11617
ea9d758d 11618static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 11619{
ea9d758d 11620 struct drm_encoder *encoder;
f6e5b160 11621 struct drm_device *dev = crtc->dev;
f6e5b160 11622
ea9d758d
DV
11623 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11624 if (encoder->crtc == crtc)
11625 return true;
11626
11627 return false;
11628}
11629
0a9ab303
ACO
11630static bool
11631needs_modeset(struct drm_crtc_state *state)
11632{
11633 return state->mode_changed || state->active_changed;
11634}
11635
ea9d758d 11636static void
0a9ab303 11637intel_modeset_update_state(struct drm_atomic_state *state)
ea9d758d 11638{
0a9ab303 11639 struct drm_device *dev = state->dev;
ba41c0de 11640 struct drm_i915_private *dev_priv = dev->dev_private;
ea9d758d 11641 struct intel_encoder *intel_encoder;
0a9ab303
ACO
11642 struct drm_crtc *crtc;
11643 struct drm_crtc_state *crtc_state;
ea9d758d 11644 struct drm_connector *connector;
0a9ab303 11645 int i;
ea9d758d 11646
ba41c0de
DV
11647 intel_shared_dpll_commit(dev_priv);
11648
b2784e15 11649 for_each_intel_encoder(dev, intel_encoder) {
ea9d758d
DV
11650 if (!intel_encoder->base.crtc)
11651 continue;
11652
0a9ab303
ACO
11653 for_each_crtc_in_state(state, crtc, crtc_state, i)
11654 if (crtc == intel_encoder->base.crtc)
11655 break;
11656
11657 if (crtc != intel_encoder->base.crtc)
11658 continue;
ea9d758d 11659
0a9ab303 11660 if (crtc_state->enable && needs_modeset(crtc_state))
ea9d758d
DV
11661 intel_encoder->connectors_active = false;
11662 }
11663
a821fc46
ACO
11664 drm_atomic_helper_swap_state(state->dev, state);
11665 intel_modeset_fixup_state(state);
ea9d758d 11666
7668851f 11667 /* Double check state. */
0a9ab303
ACO
11668 for_each_crtc(dev, crtc) {
11669 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
ea9d758d
DV
11670 }
11671
11672 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11673 if (!connector->encoder || !connector->encoder->crtc)
11674 continue;
11675
0a9ab303
ACO
11676 for_each_crtc_in_state(state, crtc, crtc_state, i)
11677 if (crtc == connector->encoder->crtc)
11678 break;
11679
11680 if (crtc != connector->encoder->crtc)
11681 continue;
ea9d758d 11682
a821fc46 11683 if (crtc->state->enable && needs_modeset(crtc->state)) {
68d34720
DV
11684 struct drm_property *dpms_property =
11685 dev->mode_config.dpms_property;
11686
ea9d758d 11687 connector->dpms = DRM_MODE_DPMS_ON;
662595df 11688 drm_object_property_set_value(&connector->base,
68d34720
DV
11689 dpms_property,
11690 DRM_MODE_DPMS_ON);
ea9d758d
DV
11691
11692 intel_encoder = to_intel_encoder(connector->encoder);
11693 intel_encoder->connectors_active = true;
11694 }
11695 }
11696
11697}
11698
3bd26263 11699static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 11700{
3bd26263 11701 int diff;
f1f644dc
JB
11702
11703 if (clock1 == clock2)
11704 return true;
11705
11706 if (!clock1 || !clock2)
11707 return false;
11708
11709 diff = abs(clock1 - clock2);
11710
11711 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11712 return true;
11713
11714 return false;
11715}
11716
25c5b266
DV
11717#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11718 list_for_each_entry((intel_crtc), \
11719 &(dev)->mode_config.crtc_list, \
11720 base.head) \
0973f18f 11721 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 11722
0e8ffe1b 11723static bool
2fa2fe9a 11724intel_pipe_config_compare(struct drm_device *dev,
5cec258b
ACO
11725 struct intel_crtc_state *current_config,
11726 struct intel_crtc_state *pipe_config)
0e8ffe1b 11727{
66e985c0
DV
11728#define PIPE_CONF_CHECK_X(name) \
11729 if (current_config->name != pipe_config->name) { \
11730 DRM_ERROR("mismatch in " #name " " \
11731 "(expected 0x%08x, found 0x%08x)\n", \
11732 current_config->name, \
11733 pipe_config->name); \
11734 return false; \
11735 }
11736
08a24034
DV
11737#define PIPE_CONF_CHECK_I(name) \
11738 if (current_config->name != pipe_config->name) { \
11739 DRM_ERROR("mismatch in " #name " " \
11740 "(expected %i, found %i)\n", \
11741 current_config->name, \
11742 pipe_config->name); \
11743 return false; \
88adfff1
DV
11744 }
11745
b95af8be
VK
11746/* This is required for BDW+ where there is only one set of registers for
11747 * switching between high and low RR.
11748 * This macro can be used whenever a comparison has to be made between one
11749 * hw state and multiple sw state variables.
11750 */
11751#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11752 if ((current_config->name != pipe_config->name) && \
11753 (current_config->alt_name != pipe_config->name)) { \
11754 DRM_ERROR("mismatch in " #name " " \
11755 "(expected %i or %i, found %i)\n", \
11756 current_config->name, \
11757 current_config->alt_name, \
11758 pipe_config->name); \
11759 return false; \
11760 }
11761
1bd1bd80
DV
11762#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11763 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 11764 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
11765 "(expected %i, found %i)\n", \
11766 current_config->name & (mask), \
11767 pipe_config->name & (mask)); \
11768 return false; \
11769 }
11770
5e550656
VS
11771#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11772 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11773 DRM_ERROR("mismatch in " #name " " \
11774 "(expected %i, found %i)\n", \
11775 current_config->name, \
11776 pipe_config->name); \
11777 return false; \
11778 }
11779
bb760063
DV
11780#define PIPE_CONF_QUIRK(quirk) \
11781 ((current_config->quirks | pipe_config->quirks) & (quirk))
11782
eccb140b
DV
11783 PIPE_CONF_CHECK_I(cpu_transcoder);
11784
08a24034
DV
11785 PIPE_CONF_CHECK_I(has_pch_encoder);
11786 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
11787 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11788 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11789 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11790 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11791 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 11792
eb14cb74 11793 PIPE_CONF_CHECK_I(has_dp_encoder);
b95af8be
VK
11794
11795 if (INTEL_INFO(dev)->gen < 8) {
11796 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11797 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11798 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11799 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11800 PIPE_CONF_CHECK_I(dp_m_n.tu);
11801
11802 if (current_config->has_drrs) {
11803 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11804 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11805 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11806 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11807 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11808 }
11809 } else {
11810 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11811 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11812 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11813 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11814 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11815 }
eb14cb74 11816
2d112de7
ACO
11817 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11818 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11819 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11820 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11821 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11822 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
1bd1bd80 11823
2d112de7
ACO
11824 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11825 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11826 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11827 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11828 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11829 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
1bd1bd80 11830
c93f54cf 11831 PIPE_CONF_CHECK_I(pixel_multiplier);
6897b4b5 11832 PIPE_CONF_CHECK_I(has_hdmi_sink);
b5a9fa09
DV
11833 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11834 IS_VALLEYVIEW(dev))
11835 PIPE_CONF_CHECK_I(limited_color_range);
e43823ec 11836 PIPE_CONF_CHECK_I(has_infoframe);
6c49f241 11837
9ed109a7
DV
11838 PIPE_CONF_CHECK_I(has_audio);
11839
2d112de7 11840 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
1bd1bd80
DV
11841 DRM_MODE_FLAG_INTERLACE);
11842
bb760063 11843 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
2d112de7 11844 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11845 DRM_MODE_FLAG_PHSYNC);
2d112de7 11846 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11847 DRM_MODE_FLAG_NHSYNC);
2d112de7 11848 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063 11849 DRM_MODE_FLAG_PVSYNC);
2d112de7 11850 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
bb760063
DV
11851 DRM_MODE_FLAG_NVSYNC);
11852 }
045ac3b5 11853
37327abd
VS
11854 PIPE_CONF_CHECK_I(pipe_src_w);
11855 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 11856
9953599b
DV
11857 /*
11858 * FIXME: BIOS likes to set up a cloned config with lvds+external
11859 * screen. Since we don't yet re-compute the pipe config when moving
11860 * just the lvds port away to another pipe the sw tracking won't match.
11861 *
11862 * Proper atomic modesets with recomputed global state will fix this.
11863 * Until then just don't check gmch state for inherited modes.
11864 */
11865 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11866 PIPE_CONF_CHECK_I(gmch_pfit.control);
11867 /* pfit ratios are autocomputed by the hw on gen4+ */
11868 if (INTEL_INFO(dev)->gen < 4)
11869 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11870 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11871 }
11872
fd4daa9c
CW
11873 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11874 if (current_config->pch_pfit.enabled) {
11875 PIPE_CONF_CHECK_I(pch_pfit.pos);
11876 PIPE_CONF_CHECK_I(pch_pfit.size);
11877 }
2fa2fe9a 11878
a1b2278e
CK
11879 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11880
e59150dc
JB
11881 /* BDW+ don't expose a synchronous way to read the state */
11882 if (IS_HASWELL(dev))
11883 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 11884
282740f7
VS
11885 PIPE_CONF_CHECK_I(double_wide);
11886
26804afd
DV
11887 PIPE_CONF_CHECK_X(ddi_pll_sel);
11888
c0d43d62 11889 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 11890 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 11891 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
11892 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11893 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
d452c5b6 11894 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
3f4cd19f
DL
11895 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11896 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11897 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
c0d43d62 11898
42571aef
VS
11899 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11900 PIPE_CONF_CHECK_I(pipe_bpp);
11901
2d112de7 11902 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
a9a7e98a 11903 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 11904
66e985c0 11905#undef PIPE_CONF_CHECK_X
08a24034 11906#undef PIPE_CONF_CHECK_I
b95af8be 11907#undef PIPE_CONF_CHECK_I_ALT
1bd1bd80 11908#undef PIPE_CONF_CHECK_FLAGS
5e550656 11909#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 11910#undef PIPE_CONF_QUIRK
88adfff1 11911
0e8ffe1b
DV
11912 return true;
11913}
11914
08db6652
DL
11915static void check_wm_state(struct drm_device *dev)
11916{
11917 struct drm_i915_private *dev_priv = dev->dev_private;
11918 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11919 struct intel_crtc *intel_crtc;
11920 int plane;
11921
11922 if (INTEL_INFO(dev)->gen < 9)
11923 return;
11924
11925 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11926 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11927
11928 for_each_intel_crtc(dev, intel_crtc) {
11929 struct skl_ddb_entry *hw_entry, *sw_entry;
11930 const enum pipe pipe = intel_crtc->pipe;
11931
11932 if (!intel_crtc->active)
11933 continue;
11934
11935 /* planes */
dd740780 11936 for_each_plane(dev_priv, pipe, plane) {
08db6652
DL
11937 hw_entry = &hw_ddb.plane[pipe][plane];
11938 sw_entry = &sw_ddb->plane[pipe][plane];
11939
11940 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11941 continue;
11942
11943 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11944 "(expected (%u,%u), found (%u,%u))\n",
11945 pipe_name(pipe), plane + 1,
11946 sw_entry->start, sw_entry->end,
11947 hw_entry->start, hw_entry->end);
11948 }
11949
11950 /* cursor */
11951 hw_entry = &hw_ddb.cursor[pipe];
11952 sw_entry = &sw_ddb->cursor[pipe];
11953
11954 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11955 continue;
11956
11957 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11958 "(expected (%u,%u), found (%u,%u))\n",
11959 pipe_name(pipe),
11960 sw_entry->start, sw_entry->end,
11961 hw_entry->start, hw_entry->end);
11962 }
11963}
11964
91d1b4bd
DV
11965static void
11966check_connector_state(struct drm_device *dev)
8af6cf88 11967{
8af6cf88
DV
11968 struct intel_connector *connector;
11969
3a3371ff 11970 for_each_intel_connector(dev, connector) {
8af6cf88
DV
11971 /* This also checks the encoder/connector hw state with the
11972 * ->get_hw_state callbacks. */
11973 intel_connector_check_state(connector);
11974
e2c719b7 11975 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
8af6cf88
DV
11976 "connector's staged encoder doesn't match current encoder\n");
11977 }
91d1b4bd
DV
11978}
11979
11980static void
11981check_encoder_state(struct drm_device *dev)
11982{
11983 struct intel_encoder *encoder;
11984 struct intel_connector *connector;
8af6cf88 11985
b2784e15 11986 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
11987 bool enabled = false;
11988 bool active = false;
11989 enum pipe pipe, tracked_pipe;
11990
11991 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11992 encoder->base.base.id,
8e329a03 11993 encoder->base.name);
8af6cf88 11994
e2c719b7 11995 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
8af6cf88 11996 "encoder's stage crtc doesn't match current crtc\n");
e2c719b7 11997 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
8af6cf88
DV
11998 "encoder's active_connectors set, but no crtc\n");
11999
3a3371ff 12000 for_each_intel_connector(dev, connector) {
8af6cf88
DV
12001 if (connector->base.encoder != &encoder->base)
12002 continue;
12003 enabled = true;
12004 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12005 active = true;
12006 }
0e32b39c
DA
12007 /*
12008 * for MST connectors if we unplug the connector is gone
12009 * away but the encoder is still connected to a crtc
12010 * until a modeset happens in response to the hotplug.
12011 */
12012 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12013 continue;
12014
e2c719b7 12015 I915_STATE_WARN(!!encoder->base.crtc != enabled,
8af6cf88
DV
12016 "encoder's enabled state mismatch "
12017 "(expected %i, found %i)\n",
12018 !!encoder->base.crtc, enabled);
e2c719b7 12019 I915_STATE_WARN(active && !encoder->base.crtc,
8af6cf88
DV
12020 "active encoder with no crtc\n");
12021
e2c719b7 12022 I915_STATE_WARN(encoder->connectors_active != active,
8af6cf88
DV
12023 "encoder's computed active state doesn't match tracked active state "
12024 "(expected %i, found %i)\n", active, encoder->connectors_active);
12025
12026 active = encoder->get_hw_state(encoder, &pipe);
e2c719b7 12027 I915_STATE_WARN(active != encoder->connectors_active,
8af6cf88
DV
12028 "encoder's hw state doesn't match sw tracking "
12029 "(expected %i, found %i)\n",
12030 encoder->connectors_active, active);
12031
12032 if (!encoder->base.crtc)
12033 continue;
12034
12035 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
e2c719b7 12036 I915_STATE_WARN(active && pipe != tracked_pipe,
8af6cf88
DV
12037 "active encoder's pipe doesn't match"
12038 "(expected %i, found %i)\n",
12039 tracked_pipe, pipe);
12040
12041 }
91d1b4bd
DV
12042}
12043
12044static void
12045check_crtc_state(struct drm_device *dev)
12046{
fbee40df 12047 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12048 struct intel_crtc *crtc;
12049 struct intel_encoder *encoder;
5cec258b 12050 struct intel_crtc_state pipe_config;
8af6cf88 12051
d3fcc808 12052 for_each_intel_crtc(dev, crtc) {
8af6cf88
DV
12053 bool enabled = false;
12054 bool active = false;
12055
045ac3b5
JB
12056 memset(&pipe_config, 0, sizeof(pipe_config));
12057
8af6cf88
DV
12058 DRM_DEBUG_KMS("[CRTC:%d]\n",
12059 crtc->base.base.id);
12060
83d65738 12061 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
8af6cf88
DV
12062 "active crtc, but not enabled in sw tracking\n");
12063
b2784e15 12064 for_each_intel_encoder(dev, encoder) {
8af6cf88
DV
12065 if (encoder->base.crtc != &crtc->base)
12066 continue;
12067 enabled = true;
12068 if (encoder->connectors_active)
12069 active = true;
12070 }
6c49f241 12071
e2c719b7 12072 I915_STATE_WARN(active != crtc->active,
8af6cf88
DV
12073 "crtc's computed active state doesn't match tracked active state "
12074 "(expected %i, found %i)\n", active, crtc->active);
83d65738 12075 I915_STATE_WARN(enabled != crtc->base.state->enable,
8af6cf88 12076 "crtc's computed enabled state doesn't match tracked enabled state "
83d65738
MR
12077 "(expected %i, found %i)\n", enabled,
12078 crtc->base.state->enable);
8af6cf88 12079
0e8ffe1b
DV
12080 active = dev_priv->display.get_pipe_config(crtc,
12081 &pipe_config);
d62cf62a 12082
b6b5d049
VS
12083 /* hw state is inconsistent with the pipe quirk */
12084 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12085 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
d62cf62a
DV
12086 active = crtc->active;
12087
b2784e15 12088 for_each_intel_encoder(dev, encoder) {
3eaba51c 12089 enum pipe pipe;
6c49f241
DV
12090 if (encoder->base.crtc != &crtc->base)
12091 continue;
1d37b689 12092 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
12093 encoder->get_config(encoder, &pipe_config);
12094 }
12095
e2c719b7 12096 I915_STATE_WARN(crtc->active != active,
0e8ffe1b
DV
12097 "crtc active state doesn't match with hw state "
12098 "(expected %i, found %i)\n", crtc->active, active);
12099
c0b03411 12100 if (active &&
6e3c9717 12101 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
e2c719b7 12102 I915_STATE_WARN(1, "pipe state doesn't match!\n");
c0b03411
DV
12103 intel_dump_pipe_config(crtc, &pipe_config,
12104 "[hw state]");
6e3c9717 12105 intel_dump_pipe_config(crtc, crtc->config,
c0b03411
DV
12106 "[sw state]");
12107 }
8af6cf88
DV
12108 }
12109}
12110
91d1b4bd
DV
12111static void
12112check_shared_dpll_state(struct drm_device *dev)
12113{
fbee40df 12114 struct drm_i915_private *dev_priv = dev->dev_private;
91d1b4bd
DV
12115 struct intel_crtc *crtc;
12116 struct intel_dpll_hw_state dpll_hw_state;
12117 int i;
5358901f
DV
12118
12119 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12120 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12121 int enabled_crtcs = 0, active_crtcs = 0;
12122 bool active;
12123
12124 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12125
12126 DRM_DEBUG_KMS("%s\n", pll->name);
12127
12128 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12129
e2c719b7 12130 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
5358901f 12131 "more active pll users than references: %i vs %i\n",
3e369b76 12132 pll->active, hweight32(pll->config.crtc_mask));
e2c719b7 12133 I915_STATE_WARN(pll->active && !pll->on,
5358901f 12134 "pll in active use but not on in sw tracking\n");
e2c719b7 12135 I915_STATE_WARN(pll->on && !pll->active,
35c95375 12136 "pll in on but not on in use in sw tracking\n");
e2c719b7 12137 I915_STATE_WARN(pll->on != active,
5358901f
DV
12138 "pll on state mismatch (expected %i, found %i)\n",
12139 pll->on, active);
12140
d3fcc808 12141 for_each_intel_crtc(dev, crtc) {
83d65738 12142 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
5358901f
DV
12143 enabled_crtcs++;
12144 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12145 active_crtcs++;
12146 }
e2c719b7 12147 I915_STATE_WARN(pll->active != active_crtcs,
5358901f
DV
12148 "pll active crtcs mismatch (expected %i, found %i)\n",
12149 pll->active, active_crtcs);
e2c719b7 12150 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
5358901f 12151 "pll enabled crtcs mismatch (expected %i, found %i)\n",
3e369b76 12152 hweight32(pll->config.crtc_mask), enabled_crtcs);
66e985c0 12153
e2c719b7 12154 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
66e985c0
DV
12155 sizeof(dpll_hw_state)),
12156 "pll hw state mismatch\n");
5358901f 12157 }
8af6cf88
DV
12158}
12159
91d1b4bd
DV
12160void
12161intel_modeset_check_state(struct drm_device *dev)
12162{
08db6652 12163 check_wm_state(dev);
91d1b4bd
DV
12164 check_connector_state(dev);
12165 check_encoder_state(dev);
12166 check_crtc_state(dev);
12167 check_shared_dpll_state(dev);
12168}
12169
5cec258b 12170void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
18442d08
VS
12171 int dotclock)
12172{
12173 /*
12174 * FDI already provided one idea for the dotclock.
12175 * Yell if the encoder disagrees.
12176 */
2d112de7 12177 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
18442d08 12178 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
2d112de7 12179 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
18442d08
VS
12180}
12181
80715b2f
VS
12182static void update_scanline_offset(struct intel_crtc *crtc)
12183{
12184 struct drm_device *dev = crtc->base.dev;
12185
12186 /*
12187 * The scanline counter increments at the leading edge of hsync.
12188 *
12189 * On most platforms it starts counting from vtotal-1 on the
12190 * first active line. That means the scanline counter value is
12191 * always one less than what we would expect. Ie. just after
12192 * start of vblank, which also occurs at start of hsync (on the
12193 * last active line), the scanline counter will read vblank_start-1.
12194 *
12195 * On gen2 the scanline counter starts counting from 1 instead
12196 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12197 * to keep the value positive), instead of adding one.
12198 *
12199 * On HSW+ the behaviour of the scanline counter depends on the output
12200 * type. For DP ports it behaves like most other platforms, but on HDMI
12201 * there's an extra 1 line difference. So we need to add two instead of
12202 * one to the value.
12203 */
12204 if (IS_GEN2(dev)) {
6e3c9717 12205 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
80715b2f
VS
12206 int vtotal;
12207
12208 vtotal = mode->crtc_vtotal;
12209 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12210 vtotal /= 2;
12211
12212 crtc->scanline_offset = vtotal - 1;
12213 } else if (HAS_DDI(dev) &&
409ee761 12214 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
80715b2f
VS
12215 crtc->scanline_offset = 2;
12216 } else
12217 crtc->scanline_offset = 1;
12218}
12219
5cec258b 12220static struct intel_crtc_state *
7f27126e 12221intel_modeset_compute_config(struct drm_crtc *crtc,
0a9ab303 12222 struct drm_atomic_state *state)
7f27126e 12223{
548ee15b 12224 struct intel_crtc_state *pipe_config;
0b901879
ACO
12225 int ret = 0;
12226
12227 ret = drm_atomic_add_affected_connectors(state, crtc);
12228 if (ret)
12229 return ERR_PTR(ret);
7f27126e 12230
8c7b5ccb
ACO
12231 ret = drm_atomic_helper_check_modeset(state->dev, state);
12232 if (ret)
12233 return ERR_PTR(ret);
7f27126e 12234
7f27126e
JB
12235 /*
12236 * Note this needs changes when we start tracking multiple modes
12237 * and crtcs. At that point we'll need to compute the whole config
12238 * (i.e. one pipe_config for each crtc) rather than just the one
12239 * for this crtc.
12240 */
548ee15b
ACO
12241 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12242 if (IS_ERR(pipe_config))
12243 return pipe_config;
83a57153 12244
4fed33f6 12245 if (!pipe_config->base.enable)
548ee15b 12246 return pipe_config;
7f27126e 12247
8c7b5ccb 12248 ret = intel_modeset_pipe_config(crtc, state, pipe_config);
548ee15b
ACO
12249 if (ret)
12250 return ERR_PTR(ret);
12251
8d8c9b51
ACO
12252 /* Check things that can only be changed through modeset */
12253 if (pipe_config->has_audio !=
12254 to_intel_crtc(crtc)->config->has_audio)
12255 pipe_config->base.mode_changed = true;
12256
12257 /*
12258 * Note we have an issue here with infoframes: current code
12259 * only updates them on the full mode set path per hw
12260 * requirements. So here we should be checking for any
12261 * required changes and forcing a mode set.
12262 */
12263
548ee15b 12264 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,"[modeset]");
db7542dd 12265
8c7b5ccb
ACO
12266 ret = drm_atomic_helper_check_planes(state->dev, state);
12267 if (ret)
12268 return ERR_PTR(ret);
12269
548ee15b 12270 return pipe_config;
7f27126e
JB
12271}
12272
0a9ab303 12273static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
ed6739ef 12274{
225da59b 12275 struct drm_device *dev = state->dev;
ed6739ef 12276 struct drm_i915_private *dev_priv = to_i915(dev);
0a9ab303 12277 unsigned clear_pipes = 0;
ed6739ef 12278 struct intel_crtc *intel_crtc;
0a9ab303
ACO
12279 struct intel_crtc_state *intel_crtc_state;
12280 struct drm_crtc *crtc;
12281 struct drm_crtc_state *crtc_state;
ed6739ef 12282 int ret = 0;
0a9ab303 12283 int i;
ed6739ef
ACO
12284
12285 if (!dev_priv->display.crtc_compute_clock)
12286 return 0;
12287
0a9ab303
ACO
12288 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12289 intel_crtc = to_intel_crtc(crtc);
4978cc93 12290 intel_crtc_state = to_intel_crtc_state(crtc_state);
0a9ab303 12291
4978cc93 12292 if (needs_modeset(crtc_state)) {
0a9ab303 12293 clear_pipes |= 1 << intel_crtc->pipe;
4978cc93 12294 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
4978cc93 12295 }
0a9ab303
ACO
12296 }
12297
ed6739ef
ACO
12298 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12299 if (ret)
12300 goto done;
12301
0a9ab303
ACO
12302 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12303 if (!needs_modeset(crtc_state) || !crtc_state->enable)
225da59b
ACO
12304 continue;
12305
0a9ab303
ACO
12306 intel_crtc = to_intel_crtc(crtc);
12307 intel_crtc_state = to_intel_crtc_state(crtc_state);
12308
ed6739ef 12309 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
0a9ab303 12310 intel_crtc_state);
ed6739ef
ACO
12311 if (ret) {
12312 intel_shared_dpll_abort_config(dev_priv);
12313 goto done;
12314 }
12315 }
12316
12317done:
12318 return ret;
12319}
12320
054518dd
ACO
12321/* Code that should eventually be part of atomic_check() */
12322static int __intel_set_mode_checks(struct drm_atomic_state *state)
12323{
12324 struct drm_device *dev = state->dev;
12325 int ret;
12326
12327 /*
12328 * See if the config requires any additional preparation, e.g.
12329 * to adjust global state with pipes off. We need to do this
12330 * here so we can get the modeset_pipe updated config for the new
12331 * mode set on this crtc. For other crtcs we need to use the
12332 * adjusted_mode bits in the crtc directly.
12333 */
12334 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
12335 ret = valleyview_modeset_global_pipes(state);
12336 if (ret)
12337 return ret;
12338 }
12339
12340 ret = __intel_set_mode_setup_plls(state);
12341 if (ret)
12342 return ret;
12343
12344 return 0;
12345}
12346
0a9ab303 12347static int __intel_set_mode(struct drm_crtc *modeset_crtc,
0a9ab303 12348 struct intel_crtc_state *pipe_config)
a6778b3c 12349{
0a9ab303 12350 struct drm_device *dev = modeset_crtc->dev;
fbee40df 12351 struct drm_i915_private *dev_priv = dev->dev_private;
304603f4 12352 struct drm_atomic_state *state = pipe_config->base.state;
0a9ab303
ACO
12353 struct drm_crtc *crtc;
12354 struct drm_crtc_state *crtc_state;
c0c36b94 12355 int ret = 0;
0a9ab303 12356 int i;
a6778b3c 12357
054518dd
ACO
12358 ret = __intel_set_mode_checks(state);
12359 if (ret < 0)
12360 return ret;
12361
d4afb8cc
ACO
12362 ret = drm_atomic_helper_prepare_planes(dev, state);
12363 if (ret)
12364 return ret;
12365
0a9ab303
ACO
12366 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12367 if (!needs_modeset(crtc_state))
12368 continue;
460da916 12369
0a9ab303
ACO
12370 if (!crtc_state->enable) {
12371 intel_crtc_disable(crtc);
12372 } else if (crtc->state->enable) {
12373 intel_crtc_disable_planes(crtc);
12374 dev_priv->display.crtc_disable(crtc);
ce22dba9 12375 }
ea9d758d 12376 }
a6778b3c 12377
6c4c86f5
DV
12378 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12379 * to set it here already despite that we pass it down the callchain.
7f27126e
JB
12380 *
12381 * Note we'll need to fix this up when we start tracking multiple
12382 * pipes; here we assume a single modeset_pipe and only track the
12383 * single crtc and mode.
f6e5b160 12384 */
0a9ab303 12385 if (pipe_config->base.enable && needs_modeset(&pipe_config->base)) {
8c7b5ccb 12386 modeset_crtc->mode = pipe_config->base.mode;
c326c0a9
VS
12387
12388 /*
12389 * Calculate and store various constants which
12390 * are later needed by vblank and swap-completion
12391 * timestamping. They are derived from true hwmode.
12392 */
0a9ab303 12393 drm_calc_timestamping_constants(modeset_crtc,
2d112de7 12394 &pipe_config->base.adjusted_mode);
b8cecdf5 12395 }
7758a113 12396
ea9d758d
DV
12397 /* Only after disabling all output pipelines that will be changed can we
12398 * update the the output configuration. */
0a9ab303 12399 intel_modeset_update_state(state);
f6e5b160 12400
a821fc46
ACO
12401 /* The state has been swaped above, so state actually contains the
12402 * old state now. */
12403
304603f4 12404 modeset_update_crtc_power_domains(state);
47fab737 12405
d4afb8cc 12406 drm_atomic_helper_commit_planes(dev, state);
a6778b3c
DV
12407
12408 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
0a9ab303 12409 for_each_crtc_in_state(state, crtc, crtc_state, i) {
a821fc46 12410 if (!needs_modeset(crtc->state) || !crtc->state->enable)
0a9ab303
ACO
12411 continue;
12412
12413 update_scanline_offset(to_intel_crtc(crtc));
80715b2f 12414
0a9ab303
ACO
12415 dev_priv->display.crtc_enable(crtc);
12416 intel_crtc_enable_planes(crtc);
80715b2f 12417 }
a6778b3c 12418
a6778b3c 12419 /* FIXME: add subpixel order */
83a57153 12420
d4afb8cc
ACO
12421 drm_atomic_helper_cleanup_planes(dev, state);
12422
2bfb4627
ACO
12423 drm_atomic_state_free(state);
12424
9eb45f22 12425 return 0;
f6e5b160
CW
12426}
12427
0a9ab303 12428static int intel_set_mode_with_config(struct drm_crtc *crtc,
0a9ab303 12429 struct intel_crtc_state *pipe_config)
f30da187
DV
12430{
12431 int ret;
12432
8c7b5ccb 12433 ret = __intel_set_mode(crtc, pipe_config);
f30da187
DV
12434
12435 if (ret == 0)
12436 intel_modeset_check_state(crtc->dev);
12437
12438 return ret;
12439}
12440
7f27126e 12441static int intel_set_mode(struct drm_crtc *crtc,
83a57153 12442 struct drm_atomic_state *state)
7f27126e 12443{
5cec258b 12444 struct intel_crtc_state *pipe_config;
83a57153 12445 int ret = 0;
7f27126e 12446
8c7b5ccb 12447 pipe_config = intel_modeset_compute_config(crtc, state);
83a57153
ACO
12448 if (IS_ERR(pipe_config)) {
12449 ret = PTR_ERR(pipe_config);
12450 goto out;
12451 }
12452
8c7b5ccb 12453 ret = intel_set_mode_with_config(crtc, 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;
2bfb4627 12470 int ret;
83a57153
ACO
12471
12472 state = drm_atomic_state_alloc(dev);
12473 if (!state) {
12474 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12475 crtc->base.id);
12476 return;
12477 }
12478
12479 state->acquire_ctx = dev->mode_config.acquire_ctx;
12480
12481 /* The force restore path in the HW readout code relies on the staged
12482 * config still keeping the user requested config while the actual
12483 * state has been overwritten by the configuration read from HW. We
12484 * need to copy the staged config to the atomic state, otherwise the
12485 * mode set will just reapply the state the HW is already in. */
12486 for_each_intel_encoder(dev, encoder) {
12487 if (&encoder->new_crtc->base != crtc)
12488 continue;
12489
12490 for_each_intel_connector(dev, connector) {
12491 if (connector->new_encoder != encoder)
12492 continue;
12493
12494 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12495 if (IS_ERR(connector_state)) {
12496 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12497 connector->base.base.id,
12498 connector->base.name,
12499 PTR_ERR(connector_state));
12500 continue;
12501 }
12502
12503 connector_state->crtc = crtc;
12504 connector_state->best_encoder = &encoder->base;
12505 }
12506 }
12507
4be07317
ACO
12508 for_each_intel_crtc(dev, intel_crtc) {
12509 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
12510 continue;
12511
12512 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
12513 if (IS_ERR(crtc_state)) {
12514 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
12515 intel_crtc->base.base.id,
12516 PTR_ERR(crtc_state));
12517 continue;
12518 }
12519
49d6fa21
ML
12520 crtc_state->base.active = crtc_state->base.enable =
12521 intel_crtc->new_enabled;
8c7b5ccb
ACO
12522
12523 if (&intel_crtc->base == crtc)
12524 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
4be07317
ACO
12525 }
12526
d3a40d1b
ACO
12527 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
12528 crtc->primary->fb, crtc->x, crtc->y);
12529
2bfb4627
ACO
12530 ret = intel_set_mode(crtc, state);
12531 if (ret)
12532 drm_atomic_state_free(state);
c0c36b94
CW
12533}
12534
25c5b266
DV
12535#undef for_each_intel_crtc_masked
12536
b7885264
ACO
12537static bool intel_connector_in_mode_set(struct intel_connector *connector,
12538 struct drm_mode_set *set)
12539{
12540 int ro;
12541
12542 for (ro = 0; ro < set->num_connectors; ro++)
12543 if (set->connectors[ro] == &connector->base)
12544 return true;
12545
12546 return false;
12547}
12548
2e431051 12549static int
9a935856
DV
12550intel_modeset_stage_output_state(struct drm_device *dev,
12551 struct drm_mode_set *set,
944b0c76 12552 struct drm_atomic_state *state)
50f56119 12553{
9a935856 12554 struct intel_connector *connector;
d5432a9d 12555 struct drm_connector *drm_connector;
944b0c76 12556 struct drm_connector_state *connector_state;
d5432a9d
ACO
12557 struct drm_crtc *crtc;
12558 struct drm_crtc_state *crtc_state;
12559 int i, ret;
50f56119 12560
9abdda74 12561 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
12562 * of connectors. For paranoia, double-check this. */
12563 WARN_ON(!set->fb && (set->num_connectors != 0));
12564 WARN_ON(set->fb && (set->num_connectors == 0));
12565
3a3371ff 12566 for_each_intel_connector(dev, connector) {
b7885264
ACO
12567 bool in_mode_set = intel_connector_in_mode_set(connector, set);
12568
d5432a9d
ACO
12569 if (!in_mode_set && connector->base.state->crtc != set->crtc)
12570 continue;
12571
12572 connector_state =
12573 drm_atomic_get_connector_state(state, &connector->base);
12574 if (IS_ERR(connector_state))
12575 return PTR_ERR(connector_state);
12576
b7885264
ACO
12577 if (in_mode_set) {
12578 int pipe = to_intel_crtc(set->crtc)->pipe;
d5432a9d
ACO
12579 connector_state->best_encoder =
12580 &intel_find_encoder(connector, pipe)->base;
50f56119
DV
12581 }
12582
d5432a9d 12583 if (connector->base.state->crtc != set->crtc)
b7885264
ACO
12584 continue;
12585
9a935856
DV
12586 /* If we disable the crtc, disable all its connectors. Also, if
12587 * the connector is on the changing crtc but not on the new
12588 * connector list, disable it. */
b7885264 12589 if (!set->fb || !in_mode_set) {
d5432a9d 12590 connector_state->best_encoder = NULL;
9a935856
DV
12591
12592 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12593 connector->base.base.id,
c23cc417 12594 connector->base.name);
9a935856 12595 }
50f56119 12596 }
9a935856 12597 /* connector->new_encoder is now updated for all connectors. */
50f56119 12598
d5432a9d
ACO
12599 for_each_connector_in_state(state, drm_connector, connector_state, i) {
12600 connector = to_intel_connector(drm_connector);
12601
12602 if (!connector_state->best_encoder) {
12603 ret = drm_atomic_set_crtc_for_connector(connector_state,
12604 NULL);
12605 if (ret)
12606 return ret;
7668851f 12607
50f56119 12608 continue;
d5432a9d 12609 }
50f56119 12610
d5432a9d
ACO
12611 if (intel_connector_in_mode_set(connector, set)) {
12612 struct drm_crtc *crtc = connector->base.state->crtc;
12613
12614 /* If this connector was in a previous crtc, add it
12615 * to the state. We might need to disable it. */
12616 if (crtc) {
12617 crtc_state =
12618 drm_atomic_get_crtc_state(state, crtc);
12619 if (IS_ERR(crtc_state))
12620 return PTR_ERR(crtc_state);
12621 }
12622
12623 ret = drm_atomic_set_crtc_for_connector(connector_state,
12624 set->crtc);
12625 if (ret)
12626 return ret;
12627 }
50f56119
DV
12628
12629 /* Make sure the new CRTC will work with the encoder */
d5432a9d
ACO
12630 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
12631 connector_state->crtc)) {
5e2b584e 12632 return -EINVAL;
50f56119 12633 }
944b0c76 12634
9a935856
DV
12635 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12636 connector->base.base.id,
c23cc417 12637 connector->base.name,
d5432a9d 12638 connector_state->crtc->base.id);
944b0c76 12639
d5432a9d
ACO
12640 if (connector_state->best_encoder != &connector->encoder->base)
12641 connector->encoder =
12642 to_intel_encoder(connector_state->best_encoder);
0e32b39c 12643 }
7668851f 12644
d5432a9d 12645 for_each_crtc_in_state(state, crtc, crtc_state, i) {
49d6fa21
ML
12646 bool has_connectors;
12647
d5432a9d
ACO
12648 ret = drm_atomic_add_affected_connectors(state, crtc);
12649 if (ret)
12650 return ret;
4be07317 12651
49d6fa21
ML
12652 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
12653 if (has_connectors != crtc_state->enable)
12654 crtc_state->enable =
12655 crtc_state->active = has_connectors;
7668851f
VS
12656 }
12657
8c7b5ccb
ACO
12658 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
12659 set->fb, set->x, set->y);
12660 if (ret)
12661 return ret;
12662
12663 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
12664 if (IS_ERR(crtc_state))
12665 return PTR_ERR(crtc_state);
12666
12667 if (set->mode)
12668 drm_mode_copy(&crtc_state->mode, set->mode);
12669
12670 if (set->num_connectors)
12671 crtc_state->active = true;
12672
2e431051
DV
12673 return 0;
12674}
12675
bb546623
ACO
12676static bool primary_plane_visible(struct drm_crtc *crtc)
12677{
12678 struct intel_plane_state *plane_state =
12679 to_intel_plane_state(crtc->primary->state);
12680
12681 return plane_state->visible;
12682}
12683
2e431051
DV
12684static int intel_crtc_set_config(struct drm_mode_set *set)
12685{
12686 struct drm_device *dev;
83a57153 12687 struct drm_atomic_state *state = NULL;
5cec258b 12688 struct intel_crtc_state *pipe_config;
bb546623 12689 bool primary_plane_was_visible;
2e431051 12690 int ret;
2e431051 12691
8d3e375e
DV
12692 BUG_ON(!set);
12693 BUG_ON(!set->crtc);
12694 BUG_ON(!set->crtc->helper_private);
2e431051 12695
7e53f3a4
DV
12696 /* Enforce sane interface api - has been abused by the fb helper. */
12697 BUG_ON(!set->mode && set->fb);
12698 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 12699
2e431051
DV
12700 if (set->fb) {
12701 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12702 set->crtc->base.id, set->fb->base.id,
12703 (int)set->num_connectors, set->x, set->y);
12704 } else {
12705 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
12706 }
12707
12708 dev = set->crtc->dev;
12709
83a57153 12710 state = drm_atomic_state_alloc(dev);
7cbf41d6
ACO
12711 if (!state)
12712 return -ENOMEM;
83a57153
ACO
12713
12714 state->acquire_ctx = dev->mode_config.acquire_ctx;
12715
462a425a 12716 ret = intel_modeset_stage_output_state(dev, set, state);
2e431051 12717 if (ret)
7cbf41d6 12718 goto out;
2e431051 12719
8c7b5ccb 12720 pipe_config = intel_modeset_compute_config(set->crtc, state);
20664591 12721 if (IS_ERR(pipe_config)) {
6ac0483b 12722 ret = PTR_ERR(pipe_config);
7cbf41d6 12723 goto out;
20664591 12724 }
50f52756 12725
1f9954d0
JB
12726 intel_update_pipe_size(to_intel_crtc(set->crtc));
12727
bb546623
ACO
12728 primary_plane_was_visible = primary_plane_visible(set->crtc);
12729
8c7b5ccb 12730 ret = intel_set_mode_with_config(set->crtc, pipe_config);
bb546623
ACO
12731
12732 if (ret == 0 &&
12733 pipe_config->base.enable &&
12734 pipe_config->base.planes_changed &&
12735 !needs_modeset(&pipe_config->base)) {
3b150f08 12736 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
3b150f08
MR
12737
12738 /*
12739 * We need to make sure the primary plane is re-enabled if it
12740 * has previously been turned off.
12741 */
bb546623
ACO
12742 if (ret == 0 && !primary_plane_was_visible &&
12743 primary_plane_visible(set->crtc)) {
3b150f08 12744 WARN_ON(!intel_crtc->active);
87d4300a 12745 intel_post_enable_primary(set->crtc);
3b150f08
MR
12746 }
12747
7ca51a3a
JB
12748 /*
12749 * In the fastboot case this may be our only check of the
12750 * state after boot. It would be better to only do it on
12751 * the first update, but we don't have a nice way of doing that
12752 * (and really, set_config isn't used much for high freq page
12753 * flipping, so increasing its cost here shouldn't be a big
12754 * deal).
12755 */
d330a953 12756 if (i915.fastboot && ret == 0)
7ca51a3a 12757 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
12758 }
12759
2d05eae1 12760 if (ret) {
bf67dfeb
DV
12761 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12762 set->crtc->base.id, ret);
2d05eae1 12763 }
50f56119 12764
7cbf41d6 12765out:
2bfb4627
ACO
12766 if (ret)
12767 drm_atomic_state_free(state);
50f56119
DV
12768 return ret;
12769}
f6e5b160
CW
12770
12771static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160 12772 .gamma_set = intel_crtc_gamma_set,
50f56119 12773 .set_config = intel_crtc_set_config,
f6e5b160
CW
12774 .destroy = intel_crtc_destroy,
12775 .page_flip = intel_crtc_page_flip,
1356837e
MR
12776 .atomic_duplicate_state = intel_crtc_duplicate_state,
12777 .atomic_destroy_state = intel_crtc_destroy_state,
f6e5b160
CW
12778};
12779
5358901f
DV
12780static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12781 struct intel_shared_dpll *pll,
12782 struct intel_dpll_hw_state *hw_state)
ee7b9f93 12783{
5358901f 12784 uint32_t val;
ee7b9f93 12785
f458ebbc 12786 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
bd2bb1b9
PZ
12787 return false;
12788
5358901f 12789 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
12790 hw_state->dpll = val;
12791 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12792 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
12793
12794 return val & DPLL_VCO_ENABLE;
12795}
12796
15bdd4cf
DV
12797static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
12798 struct intel_shared_dpll *pll)
12799{
3e369b76
ACO
12800 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
12801 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
15bdd4cf
DV
12802}
12803
e7b903d2
DV
12804static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
12805 struct intel_shared_dpll *pll)
12806{
e7b903d2 12807 /* PCH refclock must be enabled first */
89eff4be 12808 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 12809
3e369b76 12810 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf
DV
12811
12812 /* Wait for the clocks to stabilize. */
12813 POSTING_READ(PCH_DPLL(pll->id));
12814 udelay(150);
12815
12816 /* The pixel multiplier can only be updated once the
12817 * DPLL is enabled and the clocks are stable.
12818 *
12819 * So write it again.
12820 */
3e369b76 12821 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
15bdd4cf 12822 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
12823 udelay(200);
12824}
12825
12826static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
12827 struct intel_shared_dpll *pll)
12828{
12829 struct drm_device *dev = dev_priv->dev;
12830 struct intel_crtc *crtc;
e7b903d2
DV
12831
12832 /* Make sure no transcoder isn't still depending on us. */
d3fcc808 12833 for_each_intel_crtc(dev, crtc) {
e7b903d2
DV
12834 if (intel_crtc_to_shared_dpll(crtc) == pll)
12835 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
12836 }
12837
15bdd4cf
DV
12838 I915_WRITE(PCH_DPLL(pll->id), 0);
12839 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
12840 udelay(200);
12841}
12842
46edb027
DV
12843static char *ibx_pch_dpll_names[] = {
12844 "PCH DPLL A",
12845 "PCH DPLL B",
12846};
12847
7c74ade1 12848static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 12849{
e7b903d2 12850 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
12851 int i;
12852
7c74ade1 12853 dev_priv->num_shared_dpll = 2;
ee7b9f93 12854
e72f9fbf 12855 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
12856 dev_priv->shared_dplls[i].id = i;
12857 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 12858 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
12859 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
12860 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
12861 dev_priv->shared_dplls[i].get_hw_state =
12862 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
12863 }
12864}
12865
7c74ade1
DV
12866static void intel_shared_dpll_init(struct drm_device *dev)
12867{
e7b903d2 12868 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1 12869
9cd86933
DV
12870 if (HAS_DDI(dev))
12871 intel_ddi_pll_init(dev);
12872 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
7c74ade1
DV
12873 ibx_pch_dpll_init(dev);
12874 else
12875 dev_priv->num_shared_dpll = 0;
12876
12877 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
12878}
12879
1fc0a8f7
TU
12880/**
12881 * intel_wm_need_update - Check whether watermarks need updating
12882 * @plane: drm plane
12883 * @state: new plane state
12884 *
12885 * Check current plane state versus the new one to determine whether
12886 * watermarks need to be recalculated.
12887 *
12888 * Returns true or false.
12889 */
12890bool intel_wm_need_update(struct drm_plane *plane,
12891 struct drm_plane_state *state)
12892{
12893 /* Update watermarks on tiling changes. */
12894 if (!plane->state->fb || !state->fb ||
12895 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
12896 plane->state->rotation != state->rotation)
12897 return true;
12898
12899 return false;
12900}
12901
6beb8c23
MR
12902/**
12903 * intel_prepare_plane_fb - Prepare fb for usage on plane
12904 * @plane: drm plane to prepare for
12905 * @fb: framebuffer to prepare for presentation
12906 *
12907 * Prepares a framebuffer for usage on a display plane. Generally this
12908 * involves pinning the underlying object and updating the frontbuffer tracking
12909 * bits. Some older platforms need special physical address handling for
12910 * cursor planes.
12911 *
12912 * Returns 0 on success, negative error code on failure.
12913 */
12914int
12915intel_prepare_plane_fb(struct drm_plane *plane,
d136dfee
TU
12916 struct drm_framebuffer *fb,
12917 const struct drm_plane_state *new_state)
465c120c
MR
12918{
12919 struct drm_device *dev = plane->dev;
6beb8c23
MR
12920 struct intel_plane *intel_plane = to_intel_plane(plane);
12921 enum pipe pipe = intel_plane->pipe;
12922 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12923 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
12924 unsigned frontbuffer_bits = 0;
12925 int ret = 0;
465c120c 12926
ea2c67bb 12927 if (!obj)
465c120c
MR
12928 return 0;
12929
6beb8c23
MR
12930 switch (plane->type) {
12931 case DRM_PLANE_TYPE_PRIMARY:
12932 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
12933 break;
12934 case DRM_PLANE_TYPE_CURSOR:
12935 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
12936 break;
12937 case DRM_PLANE_TYPE_OVERLAY:
12938 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
12939 break;
12940 }
465c120c 12941
6beb8c23 12942 mutex_lock(&dev->struct_mutex);
465c120c 12943
6beb8c23
MR
12944 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
12945 INTEL_INFO(dev)->cursor_needs_physical) {
12946 int align = IS_I830(dev) ? 16 * 1024 : 256;
12947 ret = i915_gem_object_attach_phys(obj, align);
12948 if (ret)
12949 DRM_DEBUG_KMS("failed to attach phys object\n");
12950 } else {
82bc3b2d 12951 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
6beb8c23 12952 }
465c120c 12953
6beb8c23
MR
12954 if (ret == 0)
12955 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
fdd508a6 12956
4c34574f 12957 mutex_unlock(&dev->struct_mutex);
465c120c 12958
6beb8c23
MR
12959 return ret;
12960}
12961
38f3ce3a
MR
12962/**
12963 * intel_cleanup_plane_fb - Cleans up an fb after plane use
12964 * @plane: drm plane to clean up for
12965 * @fb: old framebuffer that was on plane
12966 *
12967 * Cleans up a framebuffer that has just been removed from a plane.
12968 */
12969void
12970intel_cleanup_plane_fb(struct drm_plane *plane,
d136dfee
TU
12971 struct drm_framebuffer *fb,
12972 const struct drm_plane_state *old_state)
38f3ce3a
MR
12973{
12974 struct drm_device *dev = plane->dev;
12975 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
12976
12977 if (WARN_ON(!obj))
12978 return;
12979
12980 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
12981 !INTEL_INFO(dev)->cursor_needs_physical) {
12982 mutex_lock(&dev->struct_mutex);
82bc3b2d 12983 intel_unpin_fb_obj(fb, old_state);
38f3ce3a
MR
12984 mutex_unlock(&dev->struct_mutex);
12985 }
465c120c
MR
12986}
12987
6156a456
CK
12988int
12989skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
12990{
12991 int max_scale;
12992 struct drm_device *dev;
12993 struct drm_i915_private *dev_priv;
12994 int crtc_clock, cdclk;
12995
12996 if (!intel_crtc || !crtc_state)
12997 return DRM_PLANE_HELPER_NO_SCALING;
12998
12999 dev = intel_crtc->base.dev;
13000 dev_priv = dev->dev_private;
13001 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13002 cdclk = dev_priv->display.get_display_clock_speed(dev);
13003
13004 if (!crtc_clock || !cdclk)
13005 return DRM_PLANE_HELPER_NO_SCALING;
13006
13007 /*
13008 * skl max scale is lower of:
13009 * close to 3 but not 3, -1 is for that purpose
13010 * or
13011 * cdclk/crtc_clock
13012 */
13013 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13014
13015 return max_scale;
13016}
13017
465c120c 13018static int
3c692a41
GP
13019intel_check_primary_plane(struct drm_plane *plane,
13020 struct intel_plane_state *state)
13021{
32b7eeec
MR
13022 struct drm_device *dev = plane->dev;
13023 struct drm_i915_private *dev_priv = dev->dev_private;
2b875c22 13024 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13025 struct intel_crtc *intel_crtc;
6156a456 13026 struct intel_crtc_state *crtc_state;
2b875c22 13027 struct drm_framebuffer *fb = state->base.fb;
3c692a41
GP
13028 struct drm_rect *dest = &state->dst;
13029 struct drm_rect *src = &state->src;
13030 const struct drm_rect *clip = &state->clip;
d8106366 13031 bool can_position = false;
6156a456
CK
13032 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13033 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
465c120c
MR
13034 int ret;
13035
ea2c67bb
MR
13036 crtc = crtc ? crtc : plane->crtc;
13037 intel_crtc = to_intel_crtc(crtc);
6156a456
CK
13038 crtc_state = state->base.state ?
13039 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
ea2c67bb 13040
6156a456
CK
13041 if (INTEL_INFO(dev)->gen >= 9) {
13042 min_scale = 1;
13043 max_scale = skl_max_scale(intel_crtc, crtc_state);
d8106366 13044 can_position = true;
6156a456 13045 }
d8106366 13046
c59cb179
MR
13047 ret = drm_plane_helper_check_update(plane, crtc, fb,
13048 src, dest, clip,
6156a456
CK
13049 min_scale,
13050 max_scale,
d8106366
SJ
13051 can_position, true,
13052 &state->visible);
c59cb179
MR
13053 if (ret)
13054 return ret;
465c120c 13055
32b7eeec 13056 if (intel_crtc->active) {
b70709a6
ML
13057 struct intel_plane_state *old_state =
13058 to_intel_plane_state(plane->state);
13059
32b7eeec
MR
13060 intel_crtc->atomic.wait_for_flips = true;
13061
13062 /*
13063 * FBC does not work on some platforms for rotated
13064 * planes, so disable it when rotation is not 0 and
13065 * update it when rotation is set back to 0.
13066 *
13067 * FIXME: This is redundant with the fbc update done in
13068 * the primary plane enable function except that that
13069 * one is done too late. We eventually need to unify
13070 * this.
13071 */
b70709a6 13072 if (state->visible &&
32b7eeec 13073 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
e35fef21 13074 dev_priv->fbc.crtc == intel_crtc &&
8e7d688b 13075 state->base.rotation != BIT(DRM_ROTATE_0)) {
32b7eeec
MR
13076 intel_crtc->atomic.disable_fbc = true;
13077 }
13078
b70709a6 13079 if (state->visible && !old_state->visible) {
32b7eeec
MR
13080 /*
13081 * BDW signals flip done immediately if the plane
13082 * is disabled, even if the plane enable is already
13083 * armed to occur at the next vblank :(
13084 */
b70709a6 13085 if (IS_BROADWELL(dev))
32b7eeec
MR
13086 intel_crtc->atomic.wait_vblank = true;
13087 }
13088
13089 intel_crtc->atomic.fb_bits |=
13090 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13091
13092 intel_crtc->atomic.update_fbc = true;
0fda6568 13093
1fc0a8f7 13094 if (intel_wm_need_update(plane, &state->base))
0fda6568 13095 intel_crtc->atomic.update_wm = true;
ccc759dc
GP
13096 }
13097
6156a456
CK
13098 if (INTEL_INFO(dev)->gen >= 9) {
13099 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13100 to_intel_plane(plane), state, 0);
13101 if (ret)
13102 return ret;
13103 }
13104
14af293f
GP
13105 return 0;
13106}
13107
13108static void
13109intel_commit_primary_plane(struct drm_plane *plane,
13110 struct intel_plane_state *state)
13111{
2b875c22
MR
13112 struct drm_crtc *crtc = state->base.crtc;
13113 struct drm_framebuffer *fb = state->base.fb;
13114 struct drm_device *dev = plane->dev;
14af293f 13115 struct drm_i915_private *dev_priv = dev->dev_private;
ea2c67bb 13116 struct intel_crtc *intel_crtc;
14af293f
GP
13117 struct drm_rect *src = &state->src;
13118
ea2c67bb
MR
13119 crtc = crtc ? crtc : plane->crtc;
13120 intel_crtc = to_intel_crtc(crtc);
cf4c7c12
MR
13121
13122 plane->fb = fb;
9dc806fc
MR
13123 crtc->x = src->x1 >> 16;
13124 crtc->y = src->y1 >> 16;
ccc759dc 13125
ccc759dc 13126 if (intel_crtc->active) {
27321ae8 13127 if (state->visible)
ccc759dc
GP
13128 /* FIXME: kill this fastboot hack */
13129 intel_update_pipe_size(intel_crtc);
465c120c 13130
27321ae8
ML
13131 dev_priv->display.update_primary_plane(crtc, plane->fb,
13132 crtc->x, crtc->y);
ccc759dc 13133 }
465c120c
MR
13134}
13135
a8ad0d8e
ML
13136static void
13137intel_disable_primary_plane(struct drm_plane *plane,
13138 struct drm_crtc *crtc,
13139 bool force)
13140{
13141 struct drm_device *dev = plane->dev;
13142 struct drm_i915_private *dev_priv = dev->dev_private;
13143
a8ad0d8e
ML
13144 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13145}
13146
32b7eeec 13147static void intel_begin_crtc_commit(struct drm_crtc *crtc)
3c692a41 13148{
32b7eeec 13149 struct drm_device *dev = crtc->dev;
140fd38d 13150 struct drm_i915_private *dev_priv = dev->dev_private;
3c692a41 13151 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ea2c67bb
MR
13152 struct intel_plane *intel_plane;
13153 struct drm_plane *p;
13154 unsigned fb_bits = 0;
13155
13156 /* Track fb's for any planes being disabled */
13157 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13158 intel_plane = to_intel_plane(p);
13159
13160 if (intel_crtc->atomic.disabled_planes &
13161 (1 << drm_plane_index(p))) {
13162 switch (p->type) {
13163 case DRM_PLANE_TYPE_PRIMARY:
13164 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13165 break;
13166 case DRM_PLANE_TYPE_CURSOR:
13167 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13168 break;
13169 case DRM_PLANE_TYPE_OVERLAY:
13170 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13171 break;
13172 }
3c692a41 13173
ea2c67bb
MR
13174 mutex_lock(&dev->struct_mutex);
13175 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13176 mutex_unlock(&dev->struct_mutex);
13177 }
13178 }
3c692a41 13179
32b7eeec
MR
13180 if (intel_crtc->atomic.wait_for_flips)
13181 intel_crtc_wait_for_pending_flips(crtc);
3c692a41 13182
32b7eeec
MR
13183 if (intel_crtc->atomic.disable_fbc)
13184 intel_fbc_disable(dev);
3c692a41 13185
32b7eeec
MR
13186 if (intel_crtc->atomic.pre_disable_primary)
13187 intel_pre_disable_primary(crtc);
3c692a41 13188
32b7eeec
MR
13189 if (intel_crtc->atomic.update_wm)
13190 intel_update_watermarks(crtc);
3c692a41 13191
32b7eeec 13192 intel_runtime_pm_get(dev_priv);
3c692a41 13193
c34c9ee4
MR
13194 /* Perform vblank evasion around commit operation */
13195 if (intel_crtc->active)
13196 intel_crtc->atomic.evade =
13197 intel_pipe_update_start(intel_crtc,
13198 &intel_crtc->atomic.start_vbl_count);
32b7eeec
MR
13199}
13200
13201static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13202{
13203 struct drm_device *dev = crtc->dev;
13204 struct drm_i915_private *dev_priv = dev->dev_private;
13205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13206 struct drm_plane *p;
13207
c34c9ee4
MR
13208 if (intel_crtc->atomic.evade)
13209 intel_pipe_update_end(intel_crtc,
13210 intel_crtc->atomic.start_vbl_count);
3c692a41 13211
140fd38d 13212 intel_runtime_pm_put(dev_priv);
3c692a41 13213
32b7eeec
MR
13214 if (intel_crtc->atomic.wait_vblank)
13215 intel_wait_for_vblank(dev, intel_crtc->pipe);
13216
13217 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13218
13219 if (intel_crtc->atomic.update_fbc) {
ccc759dc 13220 mutex_lock(&dev->struct_mutex);
7ff0ebcc 13221 intel_fbc_update(dev);
ccc759dc 13222 mutex_unlock(&dev->struct_mutex);
38f3ce3a 13223 }
3c692a41 13224
32b7eeec
MR
13225 if (intel_crtc->atomic.post_enable_primary)
13226 intel_post_enable_primary(crtc);
3c692a41 13227
32b7eeec
MR
13228 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13229 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13230 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13231 false, false);
13232
13233 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
3c692a41
GP
13234}
13235
cf4c7c12 13236/**
4a3b8769
MR
13237 * intel_plane_destroy - destroy a plane
13238 * @plane: plane to destroy
cf4c7c12 13239 *
4a3b8769
MR
13240 * Common destruction function for all types of planes (primary, cursor,
13241 * sprite).
cf4c7c12 13242 */
4a3b8769 13243void intel_plane_destroy(struct drm_plane *plane)
465c120c
MR
13244{
13245 struct intel_plane *intel_plane = to_intel_plane(plane);
13246 drm_plane_cleanup(plane);
13247 kfree(intel_plane);
13248}
13249
65a3fea0 13250const struct drm_plane_funcs intel_plane_funcs = {
70a101f8
MR
13251 .update_plane = drm_atomic_helper_update_plane,
13252 .disable_plane = drm_atomic_helper_disable_plane,
3d7d6510 13253 .destroy = intel_plane_destroy,
c196e1d6 13254 .set_property = drm_atomic_helper_plane_set_property,
a98b3431
MR
13255 .atomic_get_property = intel_plane_atomic_get_property,
13256 .atomic_set_property = intel_plane_atomic_set_property,
ea2c67bb
MR
13257 .atomic_duplicate_state = intel_plane_duplicate_state,
13258 .atomic_destroy_state = intel_plane_destroy_state,
13259
465c120c
MR
13260};
13261
13262static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13263 int pipe)
13264{
13265 struct intel_plane *primary;
8e7d688b 13266 struct intel_plane_state *state;
465c120c
MR
13267 const uint32_t *intel_primary_formats;
13268 int num_formats;
13269
13270 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13271 if (primary == NULL)
13272 return NULL;
13273
8e7d688b
MR
13274 state = intel_create_plane_state(&primary->base);
13275 if (!state) {
ea2c67bb
MR
13276 kfree(primary);
13277 return NULL;
13278 }
8e7d688b 13279 primary->base.state = &state->base;
ea2c67bb 13280
465c120c
MR
13281 primary->can_scale = false;
13282 primary->max_downscale = 1;
6156a456
CK
13283 if (INTEL_INFO(dev)->gen >= 9) {
13284 primary->can_scale = true;
af99ceda 13285 state->scaler_id = -1;
6156a456 13286 }
465c120c
MR
13287 primary->pipe = pipe;
13288 primary->plane = pipe;
c59cb179
MR
13289 primary->check_plane = intel_check_primary_plane;
13290 primary->commit_plane = intel_commit_primary_plane;
a8ad0d8e 13291 primary->disable_plane = intel_disable_primary_plane;
08e221fb 13292 primary->ckey.flags = I915_SET_COLORKEY_NONE;
465c120c
MR
13293 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13294 primary->plane = !pipe;
13295
13296 if (INTEL_INFO(dev)->gen <= 3) {
568db4f2
DL
13297 intel_primary_formats = i8xx_primary_formats;
13298 num_formats = ARRAY_SIZE(i8xx_primary_formats);
465c120c 13299 } else {
568db4f2
DL
13300 intel_primary_formats = i965_primary_formats;
13301 num_formats = ARRAY_SIZE(i965_primary_formats);
465c120c
MR
13302 }
13303
13304 drm_universal_plane_init(dev, &primary->base, 0,
65a3fea0 13305 &intel_plane_funcs,
465c120c
MR
13306 intel_primary_formats, num_formats,
13307 DRM_PLANE_TYPE_PRIMARY);
48404c1e 13308
3b7a5119
SJ
13309 if (INTEL_INFO(dev)->gen >= 4)
13310 intel_create_rotation_property(dev, primary);
48404c1e 13311
ea2c67bb
MR
13312 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13313
465c120c
MR
13314 return &primary->base;
13315}
13316
3b7a5119
SJ
13317void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13318{
13319 if (!dev->mode_config.rotation_property) {
13320 unsigned long flags = BIT(DRM_ROTATE_0) |
13321 BIT(DRM_ROTATE_180);
13322
13323 if (INTEL_INFO(dev)->gen >= 9)
13324 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13325
13326 dev->mode_config.rotation_property =
13327 drm_mode_create_rotation_property(dev, flags);
13328 }
13329 if (dev->mode_config.rotation_property)
13330 drm_object_attach_property(&plane->base.base,
13331 dev->mode_config.rotation_property,
13332 plane->base.state->rotation);
13333}
13334
3d7d6510 13335static int
852e787c
GP
13336intel_check_cursor_plane(struct drm_plane *plane,
13337 struct intel_plane_state *state)
3d7d6510 13338{
2b875c22 13339 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb 13340 struct drm_device *dev = plane->dev;
2b875c22 13341 struct drm_framebuffer *fb = state->base.fb;
852e787c
GP
13342 struct drm_rect *dest = &state->dst;
13343 struct drm_rect *src = &state->src;
13344 const struct drm_rect *clip = &state->clip;
757f9a3e 13345 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
ea2c67bb 13346 struct intel_crtc *intel_crtc;
757f9a3e
GP
13347 unsigned stride;
13348 int ret;
3d7d6510 13349
ea2c67bb
MR
13350 crtc = crtc ? crtc : plane->crtc;
13351 intel_crtc = to_intel_crtc(crtc);
13352
757f9a3e 13353 ret = drm_plane_helper_check_update(plane, crtc, fb,
852e787c 13354 src, dest, clip,
3d7d6510
MR
13355 DRM_PLANE_HELPER_NO_SCALING,
13356 DRM_PLANE_HELPER_NO_SCALING,
852e787c 13357 true, true, &state->visible);
757f9a3e
GP
13358 if (ret)
13359 return ret;
13360
13361
13362 /* if we want to turn off the cursor ignore width and height */
13363 if (!obj)
32b7eeec 13364 goto finish;
757f9a3e 13365
757f9a3e 13366 /* Check for which cursor types we support */
ea2c67bb
MR
13367 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13368 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13369 state->base.crtc_w, state->base.crtc_h);
757f9a3e
GP
13370 return -EINVAL;
13371 }
13372
ea2c67bb
MR
13373 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13374 if (obj->base.size < stride * state->base.crtc_h) {
757f9a3e
GP
13375 DRM_DEBUG_KMS("buffer is too small\n");
13376 return -ENOMEM;
13377 }
13378
3a656b54 13379 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
757f9a3e
GP
13380 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13381 ret = -EINVAL;
13382 }
757f9a3e 13383
32b7eeec
MR
13384finish:
13385 if (intel_crtc->active) {
3749f463 13386 if (plane->state->crtc_w != state->base.crtc_w)
32b7eeec
MR
13387 intel_crtc->atomic.update_wm = true;
13388
13389 intel_crtc->atomic.fb_bits |=
13390 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13391 }
13392
757f9a3e 13393 return ret;
852e787c 13394}
3d7d6510 13395
a8ad0d8e
ML
13396static void
13397intel_disable_cursor_plane(struct drm_plane *plane,
13398 struct drm_crtc *crtc,
13399 bool force)
13400{
13401 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13402
13403 if (!force) {
13404 plane->fb = NULL;
13405 intel_crtc->cursor_bo = NULL;
13406 intel_crtc->cursor_addr = 0;
13407 }
13408
13409 intel_crtc_update_cursor(crtc, false);
13410}
13411
f4a2cf29 13412static void
852e787c
GP
13413intel_commit_cursor_plane(struct drm_plane *plane,
13414 struct intel_plane_state *state)
13415{
2b875c22 13416 struct drm_crtc *crtc = state->base.crtc;
ea2c67bb
MR
13417 struct drm_device *dev = plane->dev;
13418 struct intel_crtc *intel_crtc;
2b875c22 13419 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
a912f12f 13420 uint32_t addr;
852e787c 13421
ea2c67bb
MR
13422 crtc = crtc ? crtc : plane->crtc;
13423 intel_crtc = to_intel_crtc(crtc);
13424
2b875c22 13425 plane->fb = state->base.fb;
ea2c67bb
MR
13426 crtc->cursor_x = state->base.crtc_x;
13427 crtc->cursor_y = state->base.crtc_y;
13428
a912f12f
GP
13429 if (intel_crtc->cursor_bo == obj)
13430 goto update;
4ed91096 13431
f4a2cf29 13432 if (!obj)
a912f12f 13433 addr = 0;
f4a2cf29 13434 else if (!INTEL_INFO(dev)->cursor_needs_physical)
a912f12f 13435 addr = i915_gem_obj_ggtt_offset(obj);
f4a2cf29 13436 else
a912f12f 13437 addr = obj->phys_handle->busaddr;
852e787c 13438
a912f12f
GP
13439 intel_crtc->cursor_addr = addr;
13440 intel_crtc->cursor_bo = obj;
13441update:
852e787c 13442
32b7eeec 13443 if (intel_crtc->active)
a912f12f 13444 intel_crtc_update_cursor(crtc, state->visible);
852e787c
GP
13445}
13446
3d7d6510
MR
13447static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13448 int pipe)
13449{
13450 struct intel_plane *cursor;
8e7d688b 13451 struct intel_plane_state *state;
3d7d6510
MR
13452
13453 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13454 if (cursor == NULL)
13455 return NULL;
13456
8e7d688b
MR
13457 state = intel_create_plane_state(&cursor->base);
13458 if (!state) {
ea2c67bb
MR
13459 kfree(cursor);
13460 return NULL;
13461 }
8e7d688b 13462 cursor->base.state = &state->base;
ea2c67bb 13463
3d7d6510
MR
13464 cursor->can_scale = false;
13465 cursor->max_downscale = 1;
13466 cursor->pipe = pipe;
13467 cursor->plane = pipe;
c59cb179
MR
13468 cursor->check_plane = intel_check_cursor_plane;
13469 cursor->commit_plane = intel_commit_cursor_plane;
a8ad0d8e 13470 cursor->disable_plane = intel_disable_cursor_plane;
3d7d6510
MR
13471
13472 drm_universal_plane_init(dev, &cursor->base, 0,
65a3fea0 13473 &intel_plane_funcs,
3d7d6510
MR
13474 intel_cursor_formats,
13475 ARRAY_SIZE(intel_cursor_formats),
13476 DRM_PLANE_TYPE_CURSOR);
4398ad45
VS
13477
13478 if (INTEL_INFO(dev)->gen >= 4) {
13479 if (!dev->mode_config.rotation_property)
13480 dev->mode_config.rotation_property =
13481 drm_mode_create_rotation_property(dev,
13482 BIT(DRM_ROTATE_0) |
13483 BIT(DRM_ROTATE_180));
13484 if (dev->mode_config.rotation_property)
13485 drm_object_attach_property(&cursor->base.base,
13486 dev->mode_config.rotation_property,
8e7d688b 13487 state->base.rotation);
4398ad45
VS
13488 }
13489
af99ceda
CK
13490 if (INTEL_INFO(dev)->gen >=9)
13491 state->scaler_id = -1;
13492
ea2c67bb
MR
13493 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13494
3d7d6510
MR
13495 return &cursor->base;
13496}
13497
549e2bfb
CK
13498static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13499 struct intel_crtc_state *crtc_state)
13500{
13501 int i;
13502 struct intel_scaler *intel_scaler;
13503 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13504
13505 for (i = 0; i < intel_crtc->num_scalers; i++) {
13506 intel_scaler = &scaler_state->scalers[i];
13507 intel_scaler->in_use = 0;
13508 intel_scaler->id = i;
13509
13510 intel_scaler->mode = PS_SCALER_MODE_DYN;
13511 }
13512
13513 scaler_state->scaler_id = -1;
13514}
13515
b358d0a6 13516static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 13517{
fbee40df 13518 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 13519 struct intel_crtc *intel_crtc;
f5de6e07 13520 struct intel_crtc_state *crtc_state = NULL;
3d7d6510
MR
13521 struct drm_plane *primary = NULL;
13522 struct drm_plane *cursor = NULL;
465c120c 13523 int i, ret;
79e53945 13524
955382f3 13525 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
13526 if (intel_crtc == NULL)
13527 return;
13528
f5de6e07
ACO
13529 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13530 if (!crtc_state)
13531 goto fail;
550acefd
ACO
13532 intel_crtc->config = crtc_state;
13533 intel_crtc->base.state = &crtc_state->base;
07878248 13534 crtc_state->base.crtc = &intel_crtc->base;
f5de6e07 13535
549e2bfb
CK
13536 /* initialize shared scalers */
13537 if (INTEL_INFO(dev)->gen >= 9) {
13538 if (pipe == PIPE_C)
13539 intel_crtc->num_scalers = 1;
13540 else
13541 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13542
13543 skl_init_scalers(dev, intel_crtc, crtc_state);
13544 }
13545
465c120c 13546 primary = intel_primary_plane_create(dev, pipe);
3d7d6510
MR
13547 if (!primary)
13548 goto fail;
13549
13550 cursor = intel_cursor_plane_create(dev, pipe);
13551 if (!cursor)
13552 goto fail;
13553
465c120c 13554 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
3d7d6510
MR
13555 cursor, &intel_crtc_funcs);
13556 if (ret)
13557 goto fail;
79e53945
JB
13558
13559 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
13560 for (i = 0; i < 256; i++) {
13561 intel_crtc->lut_r[i] = i;
13562 intel_crtc->lut_g[i] = i;
13563 intel_crtc->lut_b[i] = i;
13564 }
13565
1f1c2e24
VS
13566 /*
13567 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
8c0f92e1 13568 * is hooked to pipe B. Hence we want plane A feeding pipe B.
1f1c2e24 13569 */
80824003
JB
13570 intel_crtc->pipe = pipe;
13571 intel_crtc->plane = pipe;
3a77c4c4 13572 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 13573 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 13574 intel_crtc->plane = !pipe;
80824003
JB
13575 }
13576
4b0e333e
CW
13577 intel_crtc->cursor_base = ~0;
13578 intel_crtc->cursor_cntl = ~0;
dc41c154 13579 intel_crtc->cursor_size = ~0;
8d7849db 13580
22fd0fab
JB
13581 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13582 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13583 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13584 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13585
79e53945 13586 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
87b6b101
DV
13587
13588 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
3d7d6510
MR
13589 return;
13590
13591fail:
13592 if (primary)
13593 drm_plane_cleanup(primary);
13594 if (cursor)
13595 drm_plane_cleanup(cursor);
f5de6e07 13596 kfree(crtc_state);
3d7d6510 13597 kfree(intel_crtc);
79e53945
JB
13598}
13599
752aa88a
JB
13600enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13601{
13602 struct drm_encoder *encoder = connector->base.encoder;
6e9f798d 13603 struct drm_device *dev = connector->base.dev;
752aa88a 13604
51fd371b 13605 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
752aa88a 13606
d3babd3f 13607 if (!encoder || WARN_ON(!encoder->crtc))
752aa88a
JB
13608 return INVALID_PIPE;
13609
13610 return to_intel_crtc(encoder->crtc)->pipe;
13611}
13612
08d7b3d1 13613int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 13614 struct drm_file *file)
08d7b3d1 13615{
08d7b3d1 13616 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7707e653 13617 struct drm_crtc *drmmode_crtc;
c05422d5 13618 struct intel_crtc *crtc;
08d7b3d1 13619
7707e653 13620 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
08d7b3d1 13621
7707e653 13622 if (!drmmode_crtc) {
08d7b3d1 13623 DRM_ERROR("no such CRTC id\n");
3f2c2057 13624 return -ENOENT;
08d7b3d1
CW
13625 }
13626
7707e653 13627 crtc = to_intel_crtc(drmmode_crtc);
c05422d5 13628 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 13629
c05422d5 13630 return 0;
08d7b3d1
CW
13631}
13632
66a9278e 13633static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 13634{
66a9278e
DV
13635 struct drm_device *dev = encoder->base.dev;
13636 struct intel_encoder *source_encoder;
79e53945 13637 int index_mask = 0;
79e53945
JB
13638 int entry = 0;
13639
b2784e15 13640 for_each_intel_encoder(dev, source_encoder) {
bc079e8b 13641 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
13642 index_mask |= (1 << entry);
13643
79e53945
JB
13644 entry++;
13645 }
4ef69c7a 13646
79e53945
JB
13647 return index_mask;
13648}
13649
4d302442
CW
13650static bool has_edp_a(struct drm_device *dev)
13651{
13652 struct drm_i915_private *dev_priv = dev->dev_private;
13653
13654 if (!IS_MOBILE(dev))
13655 return false;
13656
13657 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13658 return false;
13659
e3589908 13660 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
13661 return false;
13662
13663 return true;
13664}
13665
84b4e042
JB
13666static bool intel_crt_present(struct drm_device *dev)
13667{
13668 struct drm_i915_private *dev_priv = dev->dev_private;
13669
884497ed
DL
13670 if (INTEL_INFO(dev)->gen >= 9)
13671 return false;
13672
cf404ce4 13673 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
84b4e042
JB
13674 return false;
13675
13676 if (IS_CHERRYVIEW(dev))
13677 return false;
13678
13679 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13680 return false;
13681
13682 return true;
13683}
13684
79e53945
JB
13685static void intel_setup_outputs(struct drm_device *dev)
13686{
725e30ad 13687 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 13688 struct intel_encoder *encoder;
cb0953d7 13689 bool dpd_is_edp = false;
79e53945 13690
c9093354 13691 intel_lvds_init(dev);
79e53945 13692
84b4e042 13693 if (intel_crt_present(dev))
79935fca 13694 intel_crt_init(dev);
cb0953d7 13695
c776eb2e
VK
13696 if (IS_BROXTON(dev)) {
13697 /*
13698 * FIXME: Broxton doesn't support port detection via the
13699 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13700 * detect the ports.
13701 */
13702 intel_ddi_init(dev, PORT_A);
13703 intel_ddi_init(dev, PORT_B);
13704 intel_ddi_init(dev, PORT_C);
13705 } else if (HAS_DDI(dev)) {
0e72a5b5
ED
13706 int found;
13707
de31facd
JB
13708 /*
13709 * Haswell uses DDI functions to detect digital outputs.
13710 * On SKL pre-D0 the strap isn't connected, so we assume
13711 * it's there.
13712 */
0e72a5b5 13713 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
de31facd
JB
13714 /* WaIgnoreDDIAStrap: skl */
13715 if (found ||
13716 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
0e72a5b5
ED
13717 intel_ddi_init(dev, PORT_A);
13718
13719 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13720 * register */
13721 found = I915_READ(SFUSE_STRAP);
13722
13723 if (found & SFUSE_STRAP_DDIB_DETECTED)
13724 intel_ddi_init(dev, PORT_B);
13725 if (found & SFUSE_STRAP_DDIC_DETECTED)
13726 intel_ddi_init(dev, PORT_C);
13727 if (found & SFUSE_STRAP_DDID_DETECTED)
13728 intel_ddi_init(dev, PORT_D);
13729 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 13730 int found;
5d8a7752 13731 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
13732
13733 if (has_edp_a(dev))
13734 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 13735
dc0fa718 13736 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 13737 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 13738 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 13739 if (!found)
e2debe91 13740 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 13741 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 13742 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
13743 }
13744
dc0fa718 13745 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 13746 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 13747
dc0fa718 13748 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 13749 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 13750
5eb08b69 13751 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 13752 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 13753
270b3042 13754 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 13755 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 13756 } else if (IS_VALLEYVIEW(dev)) {
e17ac6db
VS
13757 /*
13758 * The DP_DETECTED bit is the latched state of the DDC
13759 * SDA pin at boot. However since eDP doesn't require DDC
13760 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13761 * eDP ports may have been muxed to an alternate function.
13762 * Thus we can't rely on the DP_DETECTED bit alone to detect
13763 * eDP ports. Consult the VBT as well as DP_DETECTED to
13764 * detect eDP ports.
13765 */
d2182a66
VS
13766 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13767 !intel_dp_is_edp(dev, PORT_B))
585a94b8
AB
13768 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13769 PORT_B);
e17ac6db
VS
13770 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13771 intel_dp_is_edp(dev, PORT_B))
13772 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
585a94b8 13773
d2182a66
VS
13774 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13775 !intel_dp_is_edp(dev, PORT_C))
6f6005a5
JB
13776 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13777 PORT_C);
e17ac6db
VS
13778 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13779 intel_dp_is_edp(dev, PORT_C))
13780 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
19c03924 13781
9418c1f1 13782 if (IS_CHERRYVIEW(dev)) {
e17ac6db 13783 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
9418c1f1
VS
13784 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13785 PORT_D);
e17ac6db
VS
13786 /* eDP not supported on port D, so don't check VBT */
13787 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13788 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
9418c1f1
VS
13789 }
13790
3cfca973 13791 intel_dsi_init(dev);
103a196f 13792 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 13793 bool found = false;
7d57382e 13794
e2debe91 13795 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 13796 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 13797 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
13798 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
13799 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 13800 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 13801 }
27185ae1 13802
e7281eab 13803 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 13804 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 13805 }
13520b05
KH
13806
13807 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 13808
e2debe91 13809 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 13810 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 13811 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 13812 }
27185ae1 13813
e2debe91 13814 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 13815
b01f2c3a
JB
13816 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
13817 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 13818 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 13819 }
e7281eab 13820 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 13821 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 13822 }
27185ae1 13823
b01f2c3a 13824 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 13825 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 13826 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 13827 } else if (IS_GEN2(dev))
79e53945
JB
13828 intel_dvo_init(dev);
13829
103a196f 13830 if (SUPPORTS_TV(dev))
79e53945
JB
13831 intel_tv_init(dev);
13832
0bc12bcb 13833 intel_psr_init(dev);
7c8f8a70 13834
b2784e15 13835 for_each_intel_encoder(dev, encoder) {
4ef69c7a
CW
13836 encoder->base.possible_crtcs = encoder->crtc_mask;
13837 encoder->base.possible_clones =
66a9278e 13838 intel_encoder_clones(encoder);
79e53945 13839 }
47356eb6 13840
dde86e2d 13841 intel_init_pch_refclk(dev);
270b3042
DV
13842
13843 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
13844}
13845
13846static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
13847{
60a5ca01 13848 struct drm_device *dev = fb->dev;
79e53945 13849 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 13850
ef2d633e 13851 drm_framebuffer_cleanup(fb);
60a5ca01 13852 mutex_lock(&dev->struct_mutex);
ef2d633e 13853 WARN_ON(!intel_fb->obj->framebuffer_references--);
60a5ca01
VS
13854 drm_gem_object_unreference(&intel_fb->obj->base);
13855 mutex_unlock(&dev->struct_mutex);
79e53945
JB
13856 kfree(intel_fb);
13857}
13858
13859static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 13860 struct drm_file *file,
79e53945
JB
13861 unsigned int *handle)
13862{
13863 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 13864 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 13865
05394f39 13866 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
13867}
13868
13869static const struct drm_framebuffer_funcs intel_fb_funcs = {
13870 .destroy = intel_user_framebuffer_destroy,
13871 .create_handle = intel_user_framebuffer_create_handle,
13872};
13873
b321803d
DL
13874static
13875u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
13876 uint32_t pixel_format)
13877{
13878 u32 gen = INTEL_INFO(dev)->gen;
13879
13880 if (gen >= 9) {
13881 /* "The stride in bytes must not exceed the of the size of 8K
13882 * pixels and 32K bytes."
13883 */
13884 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
13885 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
13886 return 32*1024;
13887 } else if (gen >= 4) {
13888 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13889 return 16*1024;
13890 else
13891 return 32*1024;
13892 } else if (gen >= 3) {
13893 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13894 return 8*1024;
13895 else
13896 return 16*1024;
13897 } else {
13898 /* XXX DSPC is limited to 4k tiled */
13899 return 8*1024;
13900 }
13901}
13902
b5ea642a
DV
13903static int intel_framebuffer_init(struct drm_device *dev,
13904 struct intel_framebuffer *intel_fb,
13905 struct drm_mode_fb_cmd2 *mode_cmd,
13906 struct drm_i915_gem_object *obj)
79e53945 13907{
6761dd31 13908 unsigned int aligned_height;
79e53945 13909 int ret;
b321803d 13910 u32 pitch_limit, stride_alignment;
79e53945 13911
dd4916c5
DV
13912 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
13913
2a80eada
DV
13914 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
13915 /* Enforce that fb modifier and tiling mode match, but only for
13916 * X-tiled. This is needed for FBC. */
13917 if (!!(obj->tiling_mode == I915_TILING_X) !=
13918 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
13919 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
13920 return -EINVAL;
13921 }
13922 } else {
13923 if (obj->tiling_mode == I915_TILING_X)
13924 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
13925 else if (obj->tiling_mode == I915_TILING_Y) {
13926 DRM_DEBUG("No Y tiling for legacy addfb\n");
13927 return -EINVAL;
13928 }
13929 }
13930
9a8f0a12
TU
13931 /* Passed in modifier sanity checking. */
13932 switch (mode_cmd->modifier[0]) {
13933 case I915_FORMAT_MOD_Y_TILED:
13934 case I915_FORMAT_MOD_Yf_TILED:
13935 if (INTEL_INFO(dev)->gen < 9) {
13936 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
13937 mode_cmd->modifier[0]);
13938 return -EINVAL;
13939 }
13940 case DRM_FORMAT_MOD_NONE:
13941 case I915_FORMAT_MOD_X_TILED:
13942 break;
13943 default:
c0f40428
JB
13944 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
13945 mode_cmd->modifier[0]);
57cd6508 13946 return -EINVAL;
c16ed4be 13947 }
57cd6508 13948
b321803d
DL
13949 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
13950 mode_cmd->pixel_format);
13951 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
13952 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
13953 mode_cmd->pitches[0], stride_alignment);
57cd6508 13954 return -EINVAL;
c16ed4be 13955 }
57cd6508 13956
b321803d
DL
13957 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
13958 mode_cmd->pixel_format);
a35cdaa0 13959 if (mode_cmd->pitches[0] > pitch_limit) {
b321803d
DL
13960 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
13961 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
2a80eada 13962 "tiled" : "linear",
a35cdaa0 13963 mode_cmd->pitches[0], pitch_limit);
5d7bd705 13964 return -EINVAL;
c16ed4be 13965 }
5d7bd705 13966
2a80eada 13967 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
c16ed4be
CW
13968 mode_cmd->pitches[0] != obj->stride) {
13969 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
13970 mode_cmd->pitches[0], obj->stride);
5d7bd705 13971 return -EINVAL;
c16ed4be 13972 }
5d7bd705 13973
57779d06 13974 /* Reject formats not supported by any plane early. */
308e5bcb 13975 switch (mode_cmd->pixel_format) {
57779d06 13976 case DRM_FORMAT_C8:
04b3924d
VS
13977 case DRM_FORMAT_RGB565:
13978 case DRM_FORMAT_XRGB8888:
13979 case DRM_FORMAT_ARGB8888:
57779d06
VS
13980 break;
13981 case DRM_FORMAT_XRGB1555:
c16ed4be 13982 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
13983 DRM_DEBUG("unsupported pixel format: %s\n",
13984 drm_get_format_name(mode_cmd->pixel_format));
57779d06 13985 return -EINVAL;
c16ed4be 13986 }
57779d06
VS
13987 break;
13988 case DRM_FORMAT_XBGR8888:
13989 case DRM_FORMAT_ABGR8888:
04b3924d 13990 case DRM_FORMAT_XRGB2101010:
57779d06 13991 case DRM_FORMAT_XBGR2101010:
c16ed4be 13992 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
13993 DRM_DEBUG("unsupported pixel format: %s\n",
13994 drm_get_format_name(mode_cmd->pixel_format));
57779d06 13995 return -EINVAL;
c16ed4be 13996 }
b5626747 13997 break;
7531208b
DL
13998 case DRM_FORMAT_ABGR2101010:
13999 if (!IS_VALLEYVIEW(dev)) {
14000 DRM_DEBUG("unsupported pixel format: %s\n",
14001 drm_get_format_name(mode_cmd->pixel_format));
14002 return -EINVAL;
14003 }
14004 break;
04b3924d
VS
14005 case DRM_FORMAT_YUYV:
14006 case DRM_FORMAT_UYVY:
14007 case DRM_FORMAT_YVYU:
14008 case DRM_FORMAT_VYUY:
c16ed4be 14009 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
14010 DRM_DEBUG("unsupported pixel format: %s\n",
14011 drm_get_format_name(mode_cmd->pixel_format));
57779d06 14012 return -EINVAL;
c16ed4be 14013 }
57cd6508
CW
14014 break;
14015 default:
4ee62c76
VS
14016 DRM_DEBUG("unsupported pixel format: %s\n",
14017 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
14018 return -EINVAL;
14019 }
14020
90f9a336
VS
14021 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14022 if (mode_cmd->offsets[0] != 0)
14023 return -EINVAL;
14024
ec2c981e 14025 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
091df6cb
DV
14026 mode_cmd->pixel_format,
14027 mode_cmd->modifier[0]);
53155c0a
DV
14028 /* FIXME drm helper for size checks (especially planar formats)? */
14029 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14030 return -EINVAL;
14031
c7d73f6a
DV
14032 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14033 intel_fb->obj = obj;
80075d49 14034 intel_fb->obj->framebuffer_references++;
c7d73f6a 14035
79e53945
JB
14036 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14037 if (ret) {
14038 DRM_ERROR("framebuffer init failed %d\n", ret);
14039 return ret;
14040 }
14041
79e53945
JB
14042 return 0;
14043}
14044
79e53945
JB
14045static struct drm_framebuffer *
14046intel_user_framebuffer_create(struct drm_device *dev,
14047 struct drm_file *filp,
308e5bcb 14048 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 14049{
05394f39 14050 struct drm_i915_gem_object *obj;
79e53945 14051
308e5bcb
JB
14052 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14053 mode_cmd->handles[0]));
c8725226 14054 if (&obj->base == NULL)
cce13ff7 14055 return ERR_PTR(-ENOENT);
79e53945 14056
d2dff872 14057 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
14058}
14059
4520f53a 14060#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 14061static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
14062{
14063}
14064#endif
14065
79e53945 14066static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 14067 .fb_create = intel_user_framebuffer_create,
0632fef6 14068 .output_poll_changed = intel_fbdev_output_poll_changed,
5ee67f1c
MR
14069 .atomic_check = intel_atomic_check,
14070 .atomic_commit = intel_atomic_commit,
79e53945
JB
14071};
14072
e70236a8
JB
14073/* Set up chip specific display functions */
14074static void intel_init_display(struct drm_device *dev)
14075{
14076 struct drm_i915_private *dev_priv = dev->dev_private;
14077
ee9300bb
DV
14078 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14079 dev_priv->display.find_dpll = g4x_find_best_dpll;
ef9348c8
CML
14080 else if (IS_CHERRYVIEW(dev))
14081 dev_priv->display.find_dpll = chv_find_best_dpll;
ee9300bb
DV
14082 else if (IS_VALLEYVIEW(dev))
14083 dev_priv->display.find_dpll = vlv_find_best_dpll;
14084 else if (IS_PINEVIEW(dev))
14085 dev_priv->display.find_dpll = pnv_find_best_dpll;
14086 else
14087 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14088
bc8d7dff
DL
14089 if (INTEL_INFO(dev)->gen >= 9) {
14090 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14091 dev_priv->display.get_initial_plane_config =
14092 skylake_get_initial_plane_config;
bc8d7dff
DL
14093 dev_priv->display.crtc_compute_clock =
14094 haswell_crtc_compute_clock;
14095 dev_priv->display.crtc_enable = haswell_crtc_enable;
14096 dev_priv->display.crtc_disable = haswell_crtc_disable;
14097 dev_priv->display.off = ironlake_crtc_off;
14098 dev_priv->display.update_primary_plane =
14099 skylake_update_primary_plane;
14100 } else if (HAS_DDI(dev)) {
0e8ffe1b 14101 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
5724dbd1
DL
14102 dev_priv->display.get_initial_plane_config =
14103 ironlake_get_initial_plane_config;
797d0259
ACO
14104 dev_priv->display.crtc_compute_clock =
14105 haswell_crtc_compute_clock;
4f771f10
PZ
14106 dev_priv->display.crtc_enable = haswell_crtc_enable;
14107 dev_priv->display.crtc_disable = haswell_crtc_disable;
df8ad70c 14108 dev_priv->display.off = ironlake_crtc_off;
bc8d7dff
DL
14109 dev_priv->display.update_primary_plane =
14110 ironlake_update_primary_plane;
09b4ddf9 14111 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 14112 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
5724dbd1
DL
14113 dev_priv->display.get_initial_plane_config =
14114 ironlake_get_initial_plane_config;
3fb37703
ACO
14115 dev_priv->display.crtc_compute_clock =
14116 ironlake_crtc_compute_clock;
76e5a89c
DV
14117 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14118 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 14119 dev_priv->display.off = ironlake_crtc_off;
262ca2b0
MR
14120 dev_priv->display.update_primary_plane =
14121 ironlake_update_primary_plane;
89b667f8
JB
14122 } else if (IS_VALLEYVIEW(dev)) {
14123 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14124 dev_priv->display.get_initial_plane_config =
14125 i9xx_get_initial_plane_config;
d6dfee7a 14126 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
89b667f8
JB
14127 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14128 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14129 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
14130 dev_priv->display.update_primary_plane =
14131 i9xx_update_primary_plane;
f564048e 14132 } else {
0e8ffe1b 14133 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
5724dbd1
DL
14134 dev_priv->display.get_initial_plane_config =
14135 i9xx_get_initial_plane_config;
d6dfee7a 14136 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
76e5a89c
DV
14137 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14138 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 14139 dev_priv->display.off = i9xx_crtc_off;
262ca2b0
MR
14140 dev_priv->display.update_primary_plane =
14141 i9xx_update_primary_plane;
f564048e 14142 }
e70236a8 14143
e70236a8 14144 /* Returns the core display clock speed */
1652d19e
VS
14145 if (IS_SKYLAKE(dev))
14146 dev_priv->display.get_display_clock_speed =
14147 skylake_get_display_clock_speed;
14148 else if (IS_BROADWELL(dev))
14149 dev_priv->display.get_display_clock_speed =
14150 broadwell_get_display_clock_speed;
14151 else if (IS_HASWELL(dev))
14152 dev_priv->display.get_display_clock_speed =
14153 haswell_get_display_clock_speed;
14154 else if (IS_VALLEYVIEW(dev))
25eb05fc
JB
14155 dev_priv->display.get_display_clock_speed =
14156 valleyview_get_display_clock_speed;
b37a6434
VS
14157 else if (IS_GEN5(dev))
14158 dev_priv->display.get_display_clock_speed =
14159 ilk_get_display_clock_speed;
a7c66cd8
VS
14160 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14161 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
14162 dev_priv->display.get_display_clock_speed =
14163 i945_get_display_clock_speed;
14164 else if (IS_I915G(dev))
14165 dev_priv->display.get_display_clock_speed =
14166 i915_get_display_clock_speed;
257a7ffc 14167 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
14168 dev_priv->display.get_display_clock_speed =
14169 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
14170 else if (IS_PINEVIEW(dev))
14171 dev_priv->display.get_display_clock_speed =
14172 pnv_get_display_clock_speed;
e70236a8
JB
14173 else if (IS_I915GM(dev))
14174 dev_priv->display.get_display_clock_speed =
14175 i915gm_get_display_clock_speed;
14176 else if (IS_I865G(dev))
14177 dev_priv->display.get_display_clock_speed =
14178 i865_get_display_clock_speed;
f0f8a9ce 14179 else if (IS_I85X(dev))
e70236a8
JB
14180 dev_priv->display.get_display_clock_speed =
14181 i855_get_display_clock_speed;
14182 else /* 852, 830 */
14183 dev_priv->display.get_display_clock_speed =
14184 i830_get_display_clock_speed;
14185
7c10a2b5 14186 if (IS_GEN5(dev)) {
3bb11b53 14187 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
3bb11b53
SJ
14188 } else if (IS_GEN6(dev)) {
14189 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
3bb11b53
SJ
14190 } else if (IS_IVYBRIDGE(dev)) {
14191 /* FIXME: detect B0+ stepping and use auto training */
14192 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
059b2fe9 14193 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
3bb11b53 14194 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
30a970c6
JB
14195 } else if (IS_VALLEYVIEW(dev)) {
14196 dev_priv->display.modeset_global_resources =
14197 valleyview_modeset_global_resources;
f8437dd1
VK
14198 } else if (IS_BROXTON(dev)) {
14199 dev_priv->display.modeset_global_resources =
14200 broxton_modeset_global_resources;
e70236a8 14201 }
8c9f3aaf 14202
8c9f3aaf
JB
14203 switch (INTEL_INFO(dev)->gen) {
14204 case 2:
14205 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14206 break;
14207
14208 case 3:
14209 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14210 break;
14211
14212 case 4:
14213 case 5:
14214 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14215 break;
14216
14217 case 6:
14218 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14219 break;
7c9017e5 14220 case 7:
4e0bbc31 14221 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
14222 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14223 break;
830c81db 14224 case 9:
ba343e02
TU
14225 /* Drop through - unsupported since execlist only. */
14226 default:
14227 /* Default just returns -ENODEV to indicate unsupported */
14228 dev_priv->display.queue_flip = intel_default_queue_flip;
8c9f3aaf 14229 }
7bd688cd
JN
14230
14231 intel_panel_init_backlight_funcs(dev);
e39b999a
VS
14232
14233 mutex_init(&dev_priv->pps_mutex);
e70236a8
JB
14234}
14235
b690e96c
JB
14236/*
14237 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14238 * resume, or other times. This quirk makes sure that's the case for
14239 * affected systems.
14240 */
0206e353 14241static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
14242{
14243 struct drm_i915_private *dev_priv = dev->dev_private;
14244
14245 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 14246 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
14247}
14248
b6b5d049
VS
14249static void quirk_pipeb_force(struct drm_device *dev)
14250{
14251 struct drm_i915_private *dev_priv = dev->dev_private;
14252
14253 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14254 DRM_INFO("applying pipe b force quirk\n");
14255}
14256
435793df
KP
14257/*
14258 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14259 */
14260static void quirk_ssc_force_disable(struct drm_device *dev)
14261{
14262 struct drm_i915_private *dev_priv = dev->dev_private;
14263 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 14264 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
14265}
14266
4dca20ef 14267/*
5a15ab5b
CE
14268 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14269 * brightness value
4dca20ef
CE
14270 */
14271static void quirk_invert_brightness(struct drm_device *dev)
14272{
14273 struct drm_i915_private *dev_priv = dev->dev_private;
14274 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 14275 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
14276}
14277
9c72cc6f
SD
14278/* Some VBT's incorrectly indicate no backlight is present */
14279static void quirk_backlight_present(struct drm_device *dev)
14280{
14281 struct drm_i915_private *dev_priv = dev->dev_private;
14282 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14283 DRM_INFO("applying backlight present quirk\n");
14284}
14285
b690e96c
JB
14286struct intel_quirk {
14287 int device;
14288 int subsystem_vendor;
14289 int subsystem_device;
14290 void (*hook)(struct drm_device *dev);
14291};
14292
5f85f176
EE
14293/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14294struct intel_dmi_quirk {
14295 void (*hook)(struct drm_device *dev);
14296 const struct dmi_system_id (*dmi_id_list)[];
14297};
14298
14299static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14300{
14301 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14302 return 1;
14303}
14304
14305static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14306 {
14307 .dmi_id_list = &(const struct dmi_system_id[]) {
14308 {
14309 .callback = intel_dmi_reverse_brightness,
14310 .ident = "NCR Corporation",
14311 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14312 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14313 },
14314 },
14315 { } /* terminating entry */
14316 },
14317 .hook = quirk_invert_brightness,
14318 },
14319};
14320
c43b5634 14321static struct intel_quirk intel_quirks[] = {
b690e96c
JB
14322 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14323 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14324
b690e96c
JB
14325 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14326 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14327
5f080c0f
VS
14328 /* 830 needs to leave pipe A & dpll A up */
14329 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14330
b6b5d049
VS
14331 /* 830 needs to leave pipe B & dpll B up */
14332 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14333
435793df
KP
14334 /* Lenovo U160 cannot use SSC on LVDS */
14335 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
14336
14337 /* Sony Vaio Y cannot use SSC on LVDS */
14338 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 14339
be505f64
AH
14340 /* Acer Aspire 5734Z must invert backlight brightness */
14341 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14342
14343 /* Acer/eMachines G725 */
14344 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14345
14346 /* Acer/eMachines e725 */
14347 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14348
14349 /* Acer/Packard Bell NCL20 */
14350 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14351
14352 /* Acer Aspire 4736Z */
14353 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
14354
14355 /* Acer Aspire 5336 */
14356 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
2e93a1aa
SD
14357
14358 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14359 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
d4967d8c 14360
dfb3d47b
SD
14361 /* Acer C720 Chromebook (Core i3 4005U) */
14362 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14363
b2a9601c 14364 /* Apple Macbook 2,1 (Core 2 T7400) */
14365 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14366
d4967d8c
SD
14367 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14368 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
724cb06f
SD
14369
14370 /* HP Chromebook 14 (Celeron 2955U) */
14371 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
cf6f0af9
JN
14372
14373 /* Dell Chromebook 11 */
14374 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
b690e96c
JB
14375};
14376
14377static void intel_init_quirks(struct drm_device *dev)
14378{
14379 struct pci_dev *d = dev->pdev;
14380 int i;
14381
14382 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14383 struct intel_quirk *q = &intel_quirks[i];
14384
14385 if (d->device == q->device &&
14386 (d->subsystem_vendor == q->subsystem_vendor ||
14387 q->subsystem_vendor == PCI_ANY_ID) &&
14388 (d->subsystem_device == q->subsystem_device ||
14389 q->subsystem_device == PCI_ANY_ID))
14390 q->hook(dev);
14391 }
5f85f176
EE
14392 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14393 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14394 intel_dmi_quirks[i].hook(dev);
14395 }
b690e96c
JB
14396}
14397
9cce37f4
JB
14398/* Disable the VGA plane that we never use */
14399static void i915_disable_vga(struct drm_device *dev)
14400{
14401 struct drm_i915_private *dev_priv = dev->dev_private;
14402 u8 sr1;
766aa1c4 14403 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 14404
2b37c616 14405 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 14406 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 14407 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
14408 sr1 = inb(VGA_SR_DATA);
14409 outb(sr1 | 1<<5, VGA_SR_DATA);
14410 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14411 udelay(300);
14412
01f5a626 14413 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9cce37f4
JB
14414 POSTING_READ(vga_reg);
14415}
14416
f817586c
DV
14417void intel_modeset_init_hw(struct drm_device *dev)
14418{
a8f78b58
ED
14419 intel_prepare_ddi(dev);
14420
f8bf63fd
VS
14421 if (IS_VALLEYVIEW(dev))
14422 vlv_update_cdclk(dev);
14423
f817586c
DV
14424 intel_init_clock_gating(dev);
14425
8090c6b9 14426 intel_enable_gt_powersave(dev);
f817586c
DV
14427}
14428
79e53945
JB
14429void intel_modeset_init(struct drm_device *dev)
14430{
652c393a 14431 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 14432 int sprite, ret;
8cc87b75 14433 enum pipe pipe;
46f297fb 14434 struct intel_crtc *crtc;
79e53945
JB
14435
14436 drm_mode_config_init(dev);
14437
14438 dev->mode_config.min_width = 0;
14439 dev->mode_config.min_height = 0;
14440
019d96cb
DA
14441 dev->mode_config.preferred_depth = 24;
14442 dev->mode_config.prefer_shadow = 1;
14443
25bab385
TU
14444 dev->mode_config.allow_fb_modifiers = true;
14445
e6ecefaa 14446 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 14447
b690e96c
JB
14448 intel_init_quirks(dev);
14449
1fa61106
ED
14450 intel_init_pm(dev);
14451
e3c74757
BW
14452 if (INTEL_INFO(dev)->num_pipes == 0)
14453 return;
14454
e70236a8 14455 intel_init_display(dev);
7c10a2b5 14456 intel_init_audio(dev);
e70236a8 14457
a6c45cf0
CW
14458 if (IS_GEN2(dev)) {
14459 dev->mode_config.max_width = 2048;
14460 dev->mode_config.max_height = 2048;
14461 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
14462 dev->mode_config.max_width = 4096;
14463 dev->mode_config.max_height = 4096;
79e53945 14464 } else {
a6c45cf0
CW
14465 dev->mode_config.max_width = 8192;
14466 dev->mode_config.max_height = 8192;
79e53945 14467 }
068be561 14468
dc41c154
VS
14469 if (IS_845G(dev) || IS_I865G(dev)) {
14470 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14471 dev->mode_config.cursor_height = 1023;
14472 } else if (IS_GEN2(dev)) {
068be561
DL
14473 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14474 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14475 } else {
14476 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14477 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14478 }
14479
5d4545ae 14480 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 14481
28c97730 14482 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
14483 INTEL_INFO(dev)->num_pipes,
14484 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 14485
055e393f 14486 for_each_pipe(dev_priv, pipe) {
8cc87b75 14487 intel_crtc_init(dev, pipe);
3bdcfc0c 14488 for_each_sprite(dev_priv, pipe, sprite) {
1fe47785 14489 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 14490 if (ret)
06da8da2 14491 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 14492 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 14493 }
79e53945
JB
14494 }
14495
f42bb70d
JB
14496 intel_init_dpio(dev);
14497
e72f9fbf 14498 intel_shared_dpll_init(dev);
ee7b9f93 14499
9cce37f4
JB
14500 /* Just disable it once at startup */
14501 i915_disable_vga(dev);
79e53945 14502 intel_setup_outputs(dev);
11be49eb
CW
14503
14504 /* Just in case the BIOS is doing something questionable. */
7ff0ebcc 14505 intel_fbc_disable(dev);
fa9fa083 14506
6e9f798d 14507 drm_modeset_lock_all(dev);
fa9fa083 14508 intel_modeset_setup_hw_state(dev, false);
6e9f798d 14509 drm_modeset_unlock_all(dev);
46f297fb 14510
d3fcc808 14511 for_each_intel_crtc(dev, crtc) {
46f297fb
JB
14512 if (!crtc->active)
14513 continue;
14514
46f297fb 14515 /*
46f297fb
JB
14516 * Note that reserving the BIOS fb up front prevents us
14517 * from stuffing other stolen allocations like the ring
14518 * on top. This prevents some ugliness at boot time, and
14519 * can even allow for smooth boot transitions if the BIOS
14520 * fb is large enough for the active pipe configuration.
14521 */
5724dbd1
DL
14522 if (dev_priv->display.get_initial_plane_config) {
14523 dev_priv->display.get_initial_plane_config(crtc,
46f297fb
JB
14524 &crtc->plane_config);
14525 /*
14526 * If the fb is shared between multiple heads, we'll
14527 * just get the first one.
14528 */
f6936e29 14529 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
46f297fb 14530 }
46f297fb 14531 }
2c7111db
CW
14532}
14533
7fad798e
DV
14534static void intel_enable_pipe_a(struct drm_device *dev)
14535{
14536 struct intel_connector *connector;
14537 struct drm_connector *crt = NULL;
14538 struct intel_load_detect_pipe load_detect_temp;
208bf9fd 14539 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
7fad798e
DV
14540
14541 /* We can't just switch on the pipe A, we need to set things up with a
14542 * proper mode and output configuration. As a gross hack, enable pipe A
14543 * by enabling the load detect pipe once. */
3a3371ff 14544 for_each_intel_connector(dev, connector) {
7fad798e
DV
14545 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14546 crt = &connector->base;
14547 break;
14548 }
14549 }
14550
14551 if (!crt)
14552 return;
14553
208bf9fd 14554 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
49172fee 14555 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
7fad798e
DV
14556}
14557
fa555837
DV
14558static bool
14559intel_check_plane_mapping(struct intel_crtc *crtc)
14560{
7eb552ae
BW
14561 struct drm_device *dev = crtc->base.dev;
14562 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
14563 u32 reg, val;
14564
7eb552ae 14565 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
14566 return true;
14567
14568 reg = DSPCNTR(!crtc->plane);
14569 val = I915_READ(reg);
14570
14571 if ((val & DISPLAY_PLANE_ENABLE) &&
14572 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14573 return false;
14574
14575 return true;
14576}
14577
24929352
DV
14578static void intel_sanitize_crtc(struct intel_crtc *crtc)
14579{
14580 struct drm_device *dev = crtc->base.dev;
14581 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 14582 u32 reg;
24929352 14583
24929352 14584 /* Clear any frame start delays used for debugging left by the BIOS */
6e3c9717 14585 reg = PIPECONF(crtc->config->cpu_transcoder);
24929352
DV
14586 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14587
d3eaf884 14588 /* restore vblank interrupts to correct state */
9625604c 14589 drm_crtc_vblank_reset(&crtc->base);
d297e103
VS
14590 if (crtc->active) {
14591 update_scanline_offset(crtc);
9625604c
DV
14592 drm_crtc_vblank_on(&crtc->base);
14593 }
d3eaf884 14594
24929352 14595 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
14596 * disable the crtc (and hence change the state) if it is wrong. Note
14597 * that gen4+ has a fixed plane -> pipe mapping. */
14598 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
14599 struct intel_connector *connector;
14600 bool plane;
14601
24929352
DV
14602 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14603 crtc->base.base.id);
14604
14605 /* Pipe has the wrong plane attached and the plane is active.
14606 * Temporarily change the plane mapping and disable everything
14607 * ... */
14608 plane = crtc->plane;
b70709a6 14609 to_intel_plane_state(crtc->base.primary->state)->visible = true;
24929352 14610 crtc->plane = !plane;
ce22dba9 14611 intel_crtc_disable_planes(&crtc->base);
24929352
DV
14612 dev_priv->display.crtc_disable(&crtc->base);
14613 crtc->plane = plane;
14614
14615 /* ... and break all links. */
3a3371ff 14616 for_each_intel_connector(dev, connector) {
24929352
DV
14617 if (connector->encoder->base.crtc != &crtc->base)
14618 continue;
14619
7f1950fb
EE
14620 connector->base.dpms = DRM_MODE_DPMS_OFF;
14621 connector->base.encoder = NULL;
24929352 14622 }
7f1950fb
EE
14623 /* multiple connectors may have the same encoder:
14624 * handle them and break crtc link separately */
3a3371ff 14625 for_each_intel_connector(dev, connector)
7f1950fb
EE
14626 if (connector->encoder->base.crtc == &crtc->base) {
14627 connector->encoder->base.crtc = NULL;
14628 connector->encoder->connectors_active = false;
14629 }
24929352
DV
14630
14631 WARN_ON(crtc->active);
83d65738 14632 crtc->base.state->enable = false;
49d6fa21 14633 crtc->base.state->active = false;
24929352
DV
14634 crtc->base.enabled = false;
14635 }
24929352 14636
7fad798e
DV
14637 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14638 crtc->pipe == PIPE_A && !crtc->active) {
14639 /* BIOS forgot to enable pipe A, this mostly happens after
14640 * resume. Force-enable the pipe to fix this, the update_dpms
14641 * call below we restore the pipe to the right state, but leave
14642 * the required bits on. */
14643 intel_enable_pipe_a(dev);
14644 }
14645
24929352
DV
14646 /* Adjust the state of the output pipe according to whether we
14647 * have active connectors/encoders. */
14648 intel_crtc_update_dpms(&crtc->base);
14649
83d65738 14650 if (crtc->active != crtc->base.state->enable) {
24929352
DV
14651 struct intel_encoder *encoder;
14652
14653 /* This can happen either due to bugs in the get_hw_state
14654 * functions or because the pipe is force-enabled due to the
14655 * pipe A quirk. */
14656 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14657 crtc->base.base.id,
83d65738 14658 crtc->base.state->enable ? "enabled" : "disabled",
24929352
DV
14659 crtc->active ? "enabled" : "disabled");
14660
83d65738 14661 crtc->base.state->enable = crtc->active;
49d6fa21 14662 crtc->base.state->active = crtc->active;
24929352
DV
14663 crtc->base.enabled = crtc->active;
14664
14665 /* Because we only establish the connector -> encoder ->
14666 * crtc links if something is active, this means the
14667 * crtc is now deactivated. Break the links. connector
14668 * -> encoder links are only establish when things are
14669 * actually up, hence no need to break them. */
14670 WARN_ON(crtc->active);
14671
14672 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14673 WARN_ON(encoder->connectors_active);
14674 encoder->base.crtc = NULL;
14675 }
14676 }
c5ab3bc0 14677
a3ed6aad 14678 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
4cc31489
DV
14679 /*
14680 * We start out with underrun reporting disabled to avoid races.
14681 * For correct bookkeeping mark this on active crtcs.
14682 *
c5ab3bc0
DV
14683 * Also on gmch platforms we dont have any hardware bits to
14684 * disable the underrun reporting. Which means we need to start
14685 * out with underrun reporting disabled also on inactive pipes,
14686 * since otherwise we'll complain about the garbage we read when
14687 * e.g. coming up after runtime pm.
14688 *
4cc31489
DV
14689 * No protection against concurrent access is required - at
14690 * worst a fifo underrun happens which also sets this to false.
14691 */
14692 crtc->cpu_fifo_underrun_disabled = true;
14693 crtc->pch_fifo_underrun_disabled = true;
14694 }
24929352
DV
14695}
14696
14697static void intel_sanitize_encoder(struct intel_encoder *encoder)
14698{
14699 struct intel_connector *connector;
14700 struct drm_device *dev = encoder->base.dev;
14701
14702 /* We need to check both for a crtc link (meaning that the
14703 * encoder is active and trying to read from a pipe) and the
14704 * pipe itself being active. */
14705 bool has_active_crtc = encoder->base.crtc &&
14706 to_intel_crtc(encoder->base.crtc)->active;
14707
14708 if (encoder->connectors_active && !has_active_crtc) {
14709 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14710 encoder->base.base.id,
8e329a03 14711 encoder->base.name);
24929352
DV
14712
14713 /* Connector is active, but has no active pipe. This is
14714 * fallout from our resume register restoring. Disable
14715 * the encoder manually again. */
14716 if (encoder->base.crtc) {
14717 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14718 encoder->base.base.id,
8e329a03 14719 encoder->base.name);
24929352 14720 encoder->disable(encoder);
a62d1497
VS
14721 if (encoder->post_disable)
14722 encoder->post_disable(encoder);
24929352 14723 }
7f1950fb
EE
14724 encoder->base.crtc = NULL;
14725 encoder->connectors_active = false;
24929352
DV
14726
14727 /* Inconsistent output/port/pipe state happens presumably due to
14728 * a bug in one of the get_hw_state functions. Or someplace else
14729 * in our code, like the register restore mess on resume. Clamp
14730 * things to off as a safer default. */
3a3371ff 14731 for_each_intel_connector(dev, connector) {
24929352
DV
14732 if (connector->encoder != encoder)
14733 continue;
7f1950fb
EE
14734 connector->base.dpms = DRM_MODE_DPMS_OFF;
14735 connector->base.encoder = NULL;
24929352
DV
14736 }
14737 }
14738 /* Enabled encoders without active connectors will be fixed in
14739 * the crtc fixup. */
14740}
14741
04098753 14742void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
14743{
14744 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 14745 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 14746
04098753
ID
14747 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14748 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14749 i915_disable_vga(dev);
14750 }
14751}
14752
14753void i915_redisable_vga(struct drm_device *dev)
14754{
14755 struct drm_i915_private *dev_priv = dev->dev_private;
14756
8dc8a27c
PZ
14757 /* This function can be called both from intel_modeset_setup_hw_state or
14758 * at a very early point in our resume sequence, where the power well
14759 * structures are not yet restored. Since this function is at a very
14760 * paranoid "someone might have enabled VGA while we were not looking"
14761 * level, just check if the power well is enabled instead of trying to
14762 * follow the "don't touch the power well if we don't need it" policy
14763 * the rest of the driver uses. */
f458ebbc 14764 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
14765 return;
14766
04098753 14767 i915_redisable_vga_power_on(dev);
0fde901f
KM
14768}
14769
98ec7739
VS
14770static bool primary_get_hw_state(struct intel_crtc *crtc)
14771{
14772 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14773
14774 if (!crtc->active)
14775 return false;
14776
14777 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14778}
14779
30e984df 14780static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
14781{
14782 struct drm_i915_private *dev_priv = dev->dev_private;
14783 enum pipe pipe;
24929352
DV
14784 struct intel_crtc *crtc;
14785 struct intel_encoder *encoder;
14786 struct intel_connector *connector;
5358901f 14787 int i;
24929352 14788
d3fcc808 14789 for_each_intel_crtc(dev, crtc) {
b70709a6
ML
14790 struct drm_plane *primary = crtc->base.primary;
14791 struct intel_plane_state *plane_state;
14792
6e3c9717 14793 memset(crtc->config, 0, sizeof(*crtc->config));
3b117c8f 14794
6e3c9717 14795 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
9953599b 14796
0e8ffe1b 14797 crtc->active = dev_priv->display.get_pipe_config(crtc,
6e3c9717 14798 crtc->config);
24929352 14799
83d65738 14800 crtc->base.state->enable = crtc->active;
49d6fa21 14801 crtc->base.state->active = crtc->active;
24929352 14802 crtc->base.enabled = crtc->active;
b70709a6
ML
14803
14804 plane_state = to_intel_plane_state(primary->state);
14805 plane_state->visible = primary_get_hw_state(crtc);
24929352
DV
14806
14807 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
14808 crtc->base.base.id,
14809 crtc->active ? "enabled" : "disabled");
14810 }
14811
5358901f
DV
14812 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14813 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14814
3e369b76
ACO
14815 pll->on = pll->get_hw_state(dev_priv, pll,
14816 &pll->config.hw_state);
5358901f 14817 pll->active = 0;
3e369b76 14818 pll->config.crtc_mask = 0;
d3fcc808 14819 for_each_intel_crtc(dev, crtc) {
1e6f2ddc 14820 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
5358901f 14821 pll->active++;
3e369b76 14822 pll->config.crtc_mask |= 1 << crtc->pipe;
1e6f2ddc 14823 }
5358901f 14824 }
5358901f 14825
1e6f2ddc 14826 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
3e369b76 14827 pll->name, pll->config.crtc_mask, pll->on);
bd2bb1b9 14828
3e369b76 14829 if (pll->config.crtc_mask)
bd2bb1b9 14830 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5358901f
DV
14831 }
14832
b2784e15 14833 for_each_intel_encoder(dev, encoder) {
24929352
DV
14834 pipe = 0;
14835
14836 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
14837 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14838 encoder->base.crtc = &crtc->base;
6e3c9717 14839 encoder->get_config(encoder, crtc->config);
24929352
DV
14840 } else {
14841 encoder->base.crtc = NULL;
14842 }
14843
14844 encoder->connectors_active = false;
6f2bcceb 14845 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352 14846 encoder->base.base.id,
8e329a03 14847 encoder->base.name,
24929352 14848 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 14849 pipe_name(pipe));
24929352
DV
14850 }
14851
3a3371ff 14852 for_each_intel_connector(dev, connector) {
24929352
DV
14853 if (connector->get_hw_state(connector)) {
14854 connector->base.dpms = DRM_MODE_DPMS_ON;
14855 connector->encoder->connectors_active = true;
14856 connector->base.encoder = &connector->encoder->base;
14857 } else {
14858 connector->base.dpms = DRM_MODE_DPMS_OFF;
14859 connector->base.encoder = NULL;
14860 }
14861 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
14862 connector->base.base.id,
c23cc417 14863 connector->base.name,
24929352
DV
14864 connector->base.encoder ? "enabled" : "disabled");
14865 }
30e984df
DV
14866}
14867
14868/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
14869 * and i915 state tracking structures. */
14870void intel_modeset_setup_hw_state(struct drm_device *dev,
14871 bool force_restore)
14872{
14873 struct drm_i915_private *dev_priv = dev->dev_private;
14874 enum pipe pipe;
30e984df
DV
14875 struct intel_crtc *crtc;
14876 struct intel_encoder *encoder;
35c95375 14877 int i;
30e984df
DV
14878
14879 intel_modeset_readout_hw_state(dev);
24929352 14880
babea61d
JB
14881 /*
14882 * Now that we have the config, copy it to each CRTC struct
14883 * Note that this could go away if we move to using crtc_config
14884 * checking everywhere.
14885 */
d3fcc808 14886 for_each_intel_crtc(dev, crtc) {
d330a953 14887 if (crtc->active && i915.fastboot) {
6e3c9717
ACO
14888 intel_mode_from_pipe_config(&crtc->base.mode,
14889 crtc->config);
babea61d
JB
14890 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
14891 crtc->base.base.id);
14892 drm_mode_debug_printmodeline(&crtc->base.mode);
14893 }
14894 }
14895
24929352 14896 /* HW state is read out, now we need to sanitize this mess. */
b2784e15 14897 for_each_intel_encoder(dev, encoder) {
24929352
DV
14898 intel_sanitize_encoder(encoder);
14899 }
14900
055e393f 14901 for_each_pipe(dev_priv, pipe) {
24929352
DV
14902 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14903 intel_sanitize_crtc(crtc);
6e3c9717
ACO
14904 intel_dump_pipe_config(crtc, crtc->config,
14905 "[setup_hw_state]");
24929352 14906 }
9a935856 14907
d29b2f9d
ACO
14908 intel_modeset_update_connector_atomic_state(dev);
14909
35c95375
DV
14910 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14911 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14912
14913 if (!pll->on || pll->active)
14914 continue;
14915
14916 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
14917
14918 pll->disable(dev_priv, pll);
14919 pll->on = false;
14920 }
14921
3078999f
PB
14922 if (IS_GEN9(dev))
14923 skl_wm_get_hw_state(dev);
14924 else if (HAS_PCH_SPLIT(dev))
243e6a44
VS
14925 ilk_wm_get_hw_state(dev);
14926
45e2b5f6 14927 if (force_restore) {
7d0bc1ea
VS
14928 i915_redisable_vga(dev);
14929
f30da187
DV
14930 /*
14931 * We need to use raw interfaces for restoring state to avoid
14932 * checking (bogus) intermediate states.
14933 */
055e393f 14934 for_each_pipe(dev_priv, pipe) {
b5644d05
JB
14935 struct drm_crtc *crtc =
14936 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187 14937
83a57153 14938 intel_crtc_restore_mode(crtc);
45e2b5f6
DV
14939 }
14940 } else {
14941 intel_modeset_update_staged_output_state(dev);
14942 }
8af6cf88
DV
14943
14944 intel_modeset_check_state(dev);
2c7111db
CW
14945}
14946
14947void intel_modeset_gem_init(struct drm_device *dev)
14948{
92122789 14949 struct drm_i915_private *dev_priv = dev->dev_private;
484b41dd 14950 struct drm_crtc *c;
2ff8fde1 14951 struct drm_i915_gem_object *obj;
e0d6149b 14952 int ret;
484b41dd 14953
ae48434c
ID
14954 mutex_lock(&dev->struct_mutex);
14955 intel_init_gt_powersave(dev);
14956 mutex_unlock(&dev->struct_mutex);
14957
92122789
JB
14958 /*
14959 * There may be no VBT; and if the BIOS enabled SSC we can
14960 * just keep using it to avoid unnecessary flicker. Whereas if the
14961 * BIOS isn't using it, don't assume it will work even if the VBT
14962 * indicates as much.
14963 */
14964 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
14965 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14966 DREF_SSC1_ENABLE);
14967
1833b134 14968 intel_modeset_init_hw(dev);
02e792fb
DV
14969
14970 intel_setup_overlay(dev);
484b41dd
JB
14971
14972 /*
14973 * Make sure any fbs we allocated at startup are properly
14974 * pinned & fenced. When we do the allocation it's too early
14975 * for this.
14976 */
70e1e0ec 14977 for_each_crtc(dev, c) {
2ff8fde1
MR
14978 obj = intel_fb_obj(c->primary->fb);
14979 if (obj == NULL)
484b41dd
JB
14980 continue;
14981
e0d6149b
TU
14982 mutex_lock(&dev->struct_mutex);
14983 ret = intel_pin_and_fence_fb_obj(c->primary,
14984 c->primary->fb,
14985 c->primary->state,
14986 NULL);
14987 mutex_unlock(&dev->struct_mutex);
14988 if (ret) {
484b41dd
JB
14989 DRM_ERROR("failed to pin boot fb on pipe %d\n",
14990 to_intel_crtc(c)->pipe);
66e514c1
DA
14991 drm_framebuffer_unreference(c->primary->fb);
14992 c->primary->fb = NULL;
afd65eb4 14993 update_state_fb(c->primary);
484b41dd
JB
14994 }
14995 }
0962c3c9
VS
14996
14997 intel_backlight_register(dev);
79e53945
JB
14998}
14999
4932e2c3
ID
15000void intel_connector_unregister(struct intel_connector *intel_connector)
15001{
15002 struct drm_connector *connector = &intel_connector->base;
15003
15004 intel_panel_destroy_backlight(connector);
34ea3d38 15005 drm_connector_unregister(connector);
4932e2c3
ID
15006}
15007
79e53945
JB
15008void intel_modeset_cleanup(struct drm_device *dev)
15009{
652c393a 15010 struct drm_i915_private *dev_priv = dev->dev_private;
d9255d57 15011 struct drm_connector *connector;
652c393a 15012
2eb5252e
ID
15013 intel_disable_gt_powersave(dev);
15014
0962c3c9
VS
15015 intel_backlight_unregister(dev);
15016
fd0c0642
DV
15017 /*
15018 * Interrupts and polling as the first thing to avoid creating havoc.
2eb5252e 15019 * Too much stuff here (turning of connectors, ...) would
fd0c0642
DV
15020 * experience fancy races otherwise.
15021 */
2aeb7d3a 15022 intel_irq_uninstall(dev_priv);
eb21b92b 15023
fd0c0642
DV
15024 /*
15025 * Due to the hpd irq storm handling the hotplug work can re-arm the
15026 * poll handlers. Hence disable polling after hpd handling is shut down.
15027 */
f87ea761 15028 drm_kms_helper_poll_fini(dev);
fd0c0642 15029
652c393a
JB
15030 mutex_lock(&dev->struct_mutex);
15031
723bfd70
JB
15032 intel_unregister_dsm_handler();
15033
7ff0ebcc 15034 intel_fbc_disable(dev);
e70236a8 15035
69341a5e
KH
15036 mutex_unlock(&dev->struct_mutex);
15037
1630fe75
CW
15038 /* flush any delayed tasks or pending work */
15039 flush_scheduled_work();
15040
db31af1d
JN
15041 /* destroy the backlight and sysfs files before encoders/connectors */
15042 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
15043 struct intel_connector *intel_connector;
15044
15045 intel_connector = to_intel_connector(connector);
15046 intel_connector->unregister(intel_connector);
db31af1d 15047 }
d9255d57 15048
79e53945 15049 drm_mode_config_cleanup(dev);
4d7bb011
DV
15050
15051 intel_cleanup_overlay(dev);
ae48434c
ID
15052
15053 mutex_lock(&dev->struct_mutex);
15054 intel_cleanup_gt_powersave(dev);
15055 mutex_unlock(&dev->struct_mutex);
79e53945
JB
15056}
15057
f1c79df3
ZW
15058/*
15059 * Return which encoder is currently attached for connector.
15060 */
df0e9248 15061struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 15062{
df0e9248
CW
15063 return &intel_attached_encoder(connector)->base;
15064}
f1c79df3 15065
df0e9248
CW
15066void intel_connector_attach_encoder(struct intel_connector *connector,
15067 struct intel_encoder *encoder)
15068{
15069 connector->encoder = encoder;
15070 drm_mode_connector_attach_encoder(&connector->base,
15071 &encoder->base);
79e53945 15072}
28d52043
DA
15073
15074/*
15075 * set vga decode state - true == enable VGA decode
15076 */
15077int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15078{
15079 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 15080 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
15081 u16 gmch_ctrl;
15082
75fa041d
CW
15083 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15084 DRM_ERROR("failed to read control word\n");
15085 return -EIO;
15086 }
15087
c0cc8a55
CW
15088 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15089 return 0;
15090
28d52043
DA
15091 if (state)
15092 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15093 else
15094 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
15095
15096 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15097 DRM_ERROR("failed to write control word\n");
15098 return -EIO;
15099 }
15100
28d52043
DA
15101 return 0;
15102}
c4a1d9e4 15103
c4a1d9e4 15104struct intel_display_error_state {
ff57f1b0
PZ
15105
15106 u32 power_well_driver;
15107
63b66e5b
CW
15108 int num_transcoders;
15109
c4a1d9e4
CW
15110 struct intel_cursor_error_state {
15111 u32 control;
15112 u32 position;
15113 u32 base;
15114 u32 size;
52331309 15115 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
15116
15117 struct intel_pipe_error_state {
ddf9c536 15118 bool power_domain_on;
c4a1d9e4 15119 u32 source;
f301b1e1 15120 u32 stat;
52331309 15121 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
15122
15123 struct intel_plane_error_state {
15124 u32 control;
15125 u32 stride;
15126 u32 size;
15127 u32 pos;
15128 u32 addr;
15129 u32 surface;
15130 u32 tile_offset;
52331309 15131 } plane[I915_MAX_PIPES];
63b66e5b
CW
15132
15133 struct intel_transcoder_error_state {
ddf9c536 15134 bool power_domain_on;
63b66e5b
CW
15135 enum transcoder cpu_transcoder;
15136
15137 u32 conf;
15138
15139 u32 htotal;
15140 u32 hblank;
15141 u32 hsync;
15142 u32 vtotal;
15143 u32 vblank;
15144 u32 vsync;
15145 } transcoder[4];
c4a1d9e4
CW
15146};
15147
15148struct intel_display_error_state *
15149intel_display_capture_error_state(struct drm_device *dev)
15150{
fbee40df 15151 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4 15152 struct intel_display_error_state *error;
63b66e5b
CW
15153 int transcoders[] = {
15154 TRANSCODER_A,
15155 TRANSCODER_B,
15156 TRANSCODER_C,
15157 TRANSCODER_EDP,
15158 };
c4a1d9e4
CW
15159 int i;
15160
63b66e5b
CW
15161 if (INTEL_INFO(dev)->num_pipes == 0)
15162 return NULL;
15163
9d1cb914 15164 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
15165 if (error == NULL)
15166 return NULL;
15167
190be112 15168 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
15169 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15170
055e393f 15171 for_each_pipe(dev_priv, i) {
ddf9c536 15172 error->pipe[i].power_domain_on =
f458ebbc
DV
15173 __intel_display_power_is_enabled(dev_priv,
15174 POWER_DOMAIN_PIPE(i));
ddf9c536 15175 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
15176 continue;
15177
5efb3e28
VS
15178 error->cursor[i].control = I915_READ(CURCNTR(i));
15179 error->cursor[i].position = I915_READ(CURPOS(i));
15180 error->cursor[i].base = I915_READ(CURBASE(i));
c4a1d9e4
CW
15181
15182 error->plane[i].control = I915_READ(DSPCNTR(i));
15183 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 15184 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 15185 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
15186 error->plane[i].pos = I915_READ(DSPPOS(i));
15187 }
ca291363
PZ
15188 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15189 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
15190 if (INTEL_INFO(dev)->gen >= 4) {
15191 error->plane[i].surface = I915_READ(DSPSURF(i));
15192 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15193 }
15194
c4a1d9e4 15195 error->pipe[i].source = I915_READ(PIPESRC(i));
f301b1e1 15196
3abfce77 15197 if (HAS_GMCH_DISPLAY(dev))
f301b1e1 15198 error->pipe[i].stat = I915_READ(PIPESTAT(i));
63b66e5b
CW
15199 }
15200
15201 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15202 if (HAS_DDI(dev_priv->dev))
15203 error->num_transcoders++; /* Account for eDP. */
15204
15205 for (i = 0; i < error->num_transcoders; i++) {
15206 enum transcoder cpu_transcoder = transcoders[i];
15207
ddf9c536 15208 error->transcoder[i].power_domain_on =
f458ebbc 15209 __intel_display_power_is_enabled(dev_priv,
38cc1daf 15210 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 15211 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
15212 continue;
15213
63b66e5b
CW
15214 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15215
15216 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15217 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15218 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15219 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15220 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15221 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15222 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
15223 }
15224
15225 return error;
15226}
15227
edc3d884
MK
15228#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15229
c4a1d9e4 15230void
edc3d884 15231intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
15232 struct drm_device *dev,
15233 struct intel_display_error_state *error)
15234{
055e393f 15235 struct drm_i915_private *dev_priv = dev->dev_private;
c4a1d9e4
CW
15236 int i;
15237
63b66e5b
CW
15238 if (!error)
15239 return;
15240
edc3d884 15241 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 15242 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 15243 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 15244 error->power_well_driver);
055e393f 15245 for_each_pipe(dev_priv, i) {
edc3d884 15246 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
15247 err_printf(m, " Power: %s\n",
15248 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 15249 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
f301b1e1 15250 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
edc3d884
MK
15251
15252 err_printf(m, "Plane [%d]:\n", i);
15253 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15254 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 15255 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
15256 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15257 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 15258 }
4b71a570 15259 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 15260 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 15261 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
15262 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15263 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
15264 }
15265
edc3d884
MK
15266 err_printf(m, "Cursor [%d]:\n", i);
15267 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15268 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15269 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 15270 }
63b66e5b
CW
15271
15272 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 15273 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 15274 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
15275 err_printf(m, " Power: %s\n",
15276 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
15277 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15278 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15279 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15280 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15281 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15282 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15283 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15284 }
c4a1d9e4 15285}
e2fcdaa9
VS
15286
15287void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15288{
15289 struct intel_crtc *crtc;
15290
15291 for_each_intel_crtc(dev, crtc) {
15292 struct intel_unpin_work *work;
e2fcdaa9 15293
5e2d7afc 15294 spin_lock_irq(&dev->event_lock);
e2fcdaa9
VS
15295
15296 work = crtc->unpin_work;
15297
15298 if (work && work->event &&
15299 work->event->base.file_priv == file) {
15300 kfree(work->event);
15301 work->event = NULL;
15302 }
15303
5e2d7afc 15304 spin_unlock_irq(&dev->event_lock);
e2fcdaa9
VS
15305 }
15306}