]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_display.c
drm/i915/vlv: no MCHBAR on VLV
[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"
760285e7
DH
40#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
c0f372b3 42#include <linux/dma_remapping.h>
79e53945 43
3dec0095 44static void intel_increase_pllclock(struct drm_crtc *crtc);
6b383a7f 45static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
79e53945 46
f1f644dc
JB
47static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
48 struct intel_crtc_config *pipe_config);
18442d08
VS
49static void ironlake_pch_clock_get(struct intel_crtc *crtc,
50 struct intel_crtc_config *pipe_config);
f1f644dc 51
e7457a9a
DL
52static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
53 int x, int y, struct drm_framebuffer *old_fb);
eb1bfe80
JB
54static int intel_framebuffer_init(struct drm_device *dev,
55 struct intel_framebuffer *ifb,
56 struct drm_mode_fb_cmd2 *mode_cmd,
57 struct drm_i915_gem_object *obj);
e7457a9a 58
79e53945 59typedef struct {
0206e353 60 int min, max;
79e53945
JB
61} intel_range_t;
62
63typedef struct {
0206e353
AJ
64 int dot_limit;
65 int p2_slow, p2_fast;
79e53945
JB
66} intel_p2_t;
67
d4906093
ML
68typedef struct intel_limit intel_limit_t;
69struct intel_limit {
0206e353
AJ
70 intel_range_t dot, vco, n, m, m1, m2, p, p1;
71 intel_p2_t p2;
d4906093 72};
79e53945 73
d2acd215
DV
74int
75intel_pch_rawclk(struct drm_device *dev)
76{
77 struct drm_i915_private *dev_priv = dev->dev_private;
78
79 WARN_ON(!HAS_PCH_SPLIT(dev));
80
81 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
82}
83
021357ac
CW
84static inline u32 /* units of 100MHz */
85intel_fdi_link_freq(struct drm_device *dev)
86{
8b99e68c
CW
87 if (IS_GEN5(dev)) {
88 struct drm_i915_private *dev_priv = dev->dev_private;
89 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
90 } else
91 return 27;
021357ac
CW
92}
93
5d536e28 94static const intel_limit_t intel_limits_i8xx_dac = {
0206e353 95 .dot = { .min = 25000, .max = 350000 },
9c333719 96 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 97 .n = { .min = 2, .max = 16 },
0206e353
AJ
98 .m = { .min = 96, .max = 140 },
99 .m1 = { .min = 18, .max = 26 },
100 .m2 = { .min = 6, .max = 16 },
101 .p = { .min = 4, .max = 128 },
102 .p1 = { .min = 2, .max = 33 },
273e27ca
EA
103 .p2 = { .dot_limit = 165000,
104 .p2_slow = 4, .p2_fast = 2 },
e4b36699
KP
105};
106
5d536e28
DV
107static const intel_limit_t intel_limits_i8xx_dvo = {
108 .dot = { .min = 25000, .max = 350000 },
9c333719 109 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 110 .n = { .min = 2, .max = 16 },
5d536e28
DV
111 .m = { .min = 96, .max = 140 },
112 .m1 = { .min = 18, .max = 26 },
113 .m2 = { .min = 6, .max = 16 },
114 .p = { .min = 4, .max = 128 },
115 .p1 = { .min = 2, .max = 33 },
116 .p2 = { .dot_limit = 165000,
117 .p2_slow = 4, .p2_fast = 4 },
118};
119
e4b36699 120static const intel_limit_t intel_limits_i8xx_lvds = {
0206e353 121 .dot = { .min = 25000, .max = 350000 },
9c333719 122 .vco = { .min = 908000, .max = 1512000 },
91dbe5fb 123 .n = { .min = 2, .max = 16 },
0206e353
AJ
124 .m = { .min = 96, .max = 140 },
125 .m1 = { .min = 18, .max = 26 },
126 .m2 = { .min = 6, .max = 16 },
127 .p = { .min = 4, .max = 128 },
128 .p1 = { .min = 1, .max = 6 },
273e27ca
EA
129 .p2 = { .dot_limit = 165000,
130 .p2_slow = 14, .p2_fast = 7 },
e4b36699 131};
273e27ca 132
e4b36699 133static const intel_limit_t intel_limits_i9xx_sdvo = {
0206e353
AJ
134 .dot = { .min = 20000, .max = 400000 },
135 .vco = { .min = 1400000, .max = 2800000 },
136 .n = { .min = 1, .max = 6 },
137 .m = { .min = 70, .max = 120 },
4f7dfb67
PJ
138 .m1 = { .min = 8, .max = 18 },
139 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
140 .p = { .min = 5, .max = 80 },
141 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
142 .p2 = { .dot_limit = 200000,
143 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
144};
145
146static const intel_limit_t intel_limits_i9xx_lvds = {
0206e353
AJ
147 .dot = { .min = 20000, .max = 400000 },
148 .vco = { .min = 1400000, .max = 2800000 },
149 .n = { .min = 1, .max = 6 },
150 .m = { .min = 70, .max = 120 },
53a7d2d1
PJ
151 .m1 = { .min = 8, .max = 18 },
152 .m2 = { .min = 3, .max = 7 },
0206e353
AJ
153 .p = { .min = 7, .max = 98 },
154 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
155 .p2 = { .dot_limit = 112000,
156 .p2_slow = 14, .p2_fast = 7 },
e4b36699
KP
157};
158
273e27ca 159
e4b36699 160static const intel_limit_t intel_limits_g4x_sdvo = {
273e27ca
EA
161 .dot = { .min = 25000, .max = 270000 },
162 .vco = { .min = 1750000, .max = 3500000},
163 .n = { .min = 1, .max = 4 },
164 .m = { .min = 104, .max = 138 },
165 .m1 = { .min = 17, .max = 23 },
166 .m2 = { .min = 5, .max = 11 },
167 .p = { .min = 10, .max = 30 },
168 .p1 = { .min = 1, .max = 3},
169 .p2 = { .dot_limit = 270000,
170 .p2_slow = 10,
171 .p2_fast = 10
044c7c41 172 },
e4b36699
KP
173};
174
175static const intel_limit_t intel_limits_g4x_hdmi = {
273e27ca
EA
176 .dot = { .min = 22000, .max = 400000 },
177 .vco = { .min = 1750000, .max = 3500000},
178 .n = { .min = 1, .max = 4 },
179 .m = { .min = 104, .max = 138 },
180 .m1 = { .min = 16, .max = 23 },
181 .m2 = { .min = 5, .max = 11 },
182 .p = { .min = 5, .max = 80 },
183 .p1 = { .min = 1, .max = 8},
184 .p2 = { .dot_limit = 165000,
185 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
186};
187
188static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
273e27ca
EA
189 .dot = { .min = 20000, .max = 115000 },
190 .vco = { .min = 1750000, .max = 3500000 },
191 .n = { .min = 1, .max = 3 },
192 .m = { .min = 104, .max = 138 },
193 .m1 = { .min = 17, .max = 23 },
194 .m2 = { .min = 5, .max = 11 },
195 .p = { .min = 28, .max = 112 },
196 .p1 = { .min = 2, .max = 8 },
197 .p2 = { .dot_limit = 0,
198 .p2_slow = 14, .p2_fast = 14
044c7c41 199 },
e4b36699
KP
200};
201
202static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
273e27ca
EA
203 .dot = { .min = 80000, .max = 224000 },
204 .vco = { .min = 1750000, .max = 3500000 },
205 .n = { .min = 1, .max = 3 },
206 .m = { .min = 104, .max = 138 },
207 .m1 = { .min = 17, .max = 23 },
208 .m2 = { .min = 5, .max = 11 },
209 .p = { .min = 14, .max = 42 },
210 .p1 = { .min = 2, .max = 6 },
211 .p2 = { .dot_limit = 0,
212 .p2_slow = 7, .p2_fast = 7
044c7c41 213 },
e4b36699
KP
214};
215
f2b115e6 216static const intel_limit_t intel_limits_pineview_sdvo = {
0206e353
AJ
217 .dot = { .min = 20000, .max = 400000},
218 .vco = { .min = 1700000, .max = 3500000 },
273e27ca 219 /* Pineview's Ncounter is a ring counter */
0206e353
AJ
220 .n = { .min = 3, .max = 6 },
221 .m = { .min = 2, .max = 256 },
273e27ca 222 /* Pineview only has one combined m divider, which we treat as m2. */
0206e353
AJ
223 .m1 = { .min = 0, .max = 0 },
224 .m2 = { .min = 0, .max = 254 },
225 .p = { .min = 5, .max = 80 },
226 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
227 .p2 = { .dot_limit = 200000,
228 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
229};
230
f2b115e6 231static const intel_limit_t intel_limits_pineview_lvds = {
0206e353
AJ
232 .dot = { .min = 20000, .max = 400000 },
233 .vco = { .min = 1700000, .max = 3500000 },
234 .n = { .min = 3, .max = 6 },
235 .m = { .min = 2, .max = 256 },
236 .m1 = { .min = 0, .max = 0 },
237 .m2 = { .min = 0, .max = 254 },
238 .p = { .min = 7, .max = 112 },
239 .p1 = { .min = 1, .max = 8 },
273e27ca
EA
240 .p2 = { .dot_limit = 112000,
241 .p2_slow = 14, .p2_fast = 14 },
e4b36699
KP
242};
243
273e27ca
EA
244/* Ironlake / Sandybridge
245 *
246 * We calculate clock using (register_value + 2) for N/M1/M2, so here
247 * the range value for them is (actual_value - 2).
248 */
b91ad0ec 249static const intel_limit_t intel_limits_ironlake_dac = {
273e27ca
EA
250 .dot = { .min = 25000, .max = 350000 },
251 .vco = { .min = 1760000, .max = 3510000 },
252 .n = { .min = 1, .max = 5 },
253 .m = { .min = 79, .max = 127 },
254 .m1 = { .min = 12, .max = 22 },
255 .m2 = { .min = 5, .max = 9 },
256 .p = { .min = 5, .max = 80 },
257 .p1 = { .min = 1, .max = 8 },
258 .p2 = { .dot_limit = 225000,
259 .p2_slow = 10, .p2_fast = 5 },
e4b36699
KP
260};
261
b91ad0ec 262static const intel_limit_t intel_limits_ironlake_single_lvds = {
273e27ca
EA
263 .dot = { .min = 25000, .max = 350000 },
264 .vco = { .min = 1760000, .max = 3510000 },
265 .n = { .min = 1, .max = 3 },
266 .m = { .min = 79, .max = 118 },
267 .m1 = { .min = 12, .max = 22 },
268 .m2 = { .min = 5, .max = 9 },
269 .p = { .min = 28, .max = 112 },
270 .p1 = { .min = 2, .max = 8 },
271 .p2 = { .dot_limit = 225000,
272 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
273};
274
275static const intel_limit_t intel_limits_ironlake_dual_lvds = {
273e27ca
EA
276 .dot = { .min = 25000, .max = 350000 },
277 .vco = { .min = 1760000, .max = 3510000 },
278 .n = { .min = 1, .max = 3 },
279 .m = { .min = 79, .max = 127 },
280 .m1 = { .min = 12, .max = 22 },
281 .m2 = { .min = 5, .max = 9 },
282 .p = { .min = 14, .max = 56 },
283 .p1 = { .min = 2, .max = 8 },
284 .p2 = { .dot_limit = 225000,
285 .p2_slow = 7, .p2_fast = 7 },
b91ad0ec
ZW
286};
287
273e27ca 288/* LVDS 100mhz refclk limits. */
b91ad0ec 289static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
273e27ca
EA
290 .dot = { .min = 25000, .max = 350000 },
291 .vco = { .min = 1760000, .max = 3510000 },
292 .n = { .min = 1, .max = 2 },
293 .m = { .min = 79, .max = 126 },
294 .m1 = { .min = 12, .max = 22 },
295 .m2 = { .min = 5, .max = 9 },
296 .p = { .min = 28, .max = 112 },
0206e353 297 .p1 = { .min = 2, .max = 8 },
273e27ca
EA
298 .p2 = { .dot_limit = 225000,
299 .p2_slow = 14, .p2_fast = 14 },
b91ad0ec
ZW
300};
301
302static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
273e27ca
EA
303 .dot = { .min = 25000, .max = 350000 },
304 .vco = { .min = 1760000, .max = 3510000 },
305 .n = { .min = 1, .max = 3 },
306 .m = { .min = 79, .max = 126 },
307 .m1 = { .min = 12, .max = 22 },
308 .m2 = { .min = 5, .max = 9 },
309 .p = { .min = 14, .max = 42 },
0206e353 310 .p1 = { .min = 2, .max = 6 },
273e27ca
EA
311 .p2 = { .dot_limit = 225000,
312 .p2_slow = 7, .p2_fast = 7 },
4547668a
ZY
313};
314
dc730512 315static const intel_limit_t intel_limits_vlv = {
f01b7962
VS
316 /*
317 * These are the data rate limits (measured in fast clocks)
318 * since those are the strictest limits we have. The fast
319 * clock and actual rate limits are more relaxed, so checking
320 * them would make no difference.
321 */
322 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
75e53986 323 .vco = { .min = 4000000, .max = 6000000 },
a0c4da24 324 .n = { .min = 1, .max = 7 },
a0c4da24
JB
325 .m1 = { .min = 2, .max = 3 },
326 .m2 = { .min = 11, .max = 156 },
b99ab663 327 .p1 = { .min = 2, .max = 3 },
5fdc9c49 328 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
a0c4da24
JB
329};
330
6b4bf1c4
VS
331static void vlv_clock(int refclk, intel_clock_t *clock)
332{
333 clock->m = clock->m1 * clock->m2;
334 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
335 if (WARN_ON(clock->n == 0 || clock->p == 0))
336 return;
fb03ac01
VS
337 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
338 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
6b4bf1c4
VS
339}
340
e0638cdf
PZ
341/**
342 * Returns whether any output on the specified pipe is of the specified type
343 */
344static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
345{
346 struct drm_device *dev = crtc->dev;
347 struct intel_encoder *encoder;
348
349 for_each_encoder_on_crtc(dev, crtc, encoder)
350 if (encoder->type == type)
351 return true;
352
353 return false;
354}
355
1b894b59
CW
356static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
357 int refclk)
2c07245f 358{
b91ad0ec 359 struct drm_device *dev = crtc->dev;
2c07245f 360 const intel_limit_t *limit;
b91ad0ec
ZW
361
362 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 363 if (intel_is_dual_link_lvds(dev)) {
1b894b59 364 if (refclk == 100000)
b91ad0ec
ZW
365 limit = &intel_limits_ironlake_dual_lvds_100m;
366 else
367 limit = &intel_limits_ironlake_dual_lvds;
368 } else {
1b894b59 369 if (refclk == 100000)
b91ad0ec
ZW
370 limit = &intel_limits_ironlake_single_lvds_100m;
371 else
372 limit = &intel_limits_ironlake_single_lvds;
373 }
c6bb3538 374 } else
b91ad0ec 375 limit = &intel_limits_ironlake_dac;
2c07245f
ZW
376
377 return limit;
378}
379
044c7c41
ML
380static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
381{
382 struct drm_device *dev = crtc->dev;
044c7c41
ML
383 const intel_limit_t *limit;
384
385 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 386 if (intel_is_dual_link_lvds(dev))
e4b36699 387 limit = &intel_limits_g4x_dual_channel_lvds;
044c7c41 388 else
e4b36699 389 limit = &intel_limits_g4x_single_channel_lvds;
044c7c41
ML
390 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
391 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
e4b36699 392 limit = &intel_limits_g4x_hdmi;
044c7c41 393 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
e4b36699 394 limit = &intel_limits_g4x_sdvo;
044c7c41 395 } else /* The option is for other outputs */
e4b36699 396 limit = &intel_limits_i9xx_sdvo;
044c7c41
ML
397
398 return limit;
399}
400
1b894b59 401static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
79e53945
JB
402{
403 struct drm_device *dev = crtc->dev;
404 const intel_limit_t *limit;
405
bad720ff 406 if (HAS_PCH_SPLIT(dev))
1b894b59 407 limit = intel_ironlake_limit(crtc, refclk);
2c07245f 408 else if (IS_G4X(dev)) {
044c7c41 409 limit = intel_g4x_limit(crtc);
f2b115e6 410 } else if (IS_PINEVIEW(dev)) {
2177832f 411 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
f2b115e6 412 limit = &intel_limits_pineview_lvds;
2177832f 413 else
f2b115e6 414 limit = &intel_limits_pineview_sdvo;
a0c4da24 415 } else if (IS_VALLEYVIEW(dev)) {
dc730512 416 limit = &intel_limits_vlv;
a6c45cf0
CW
417 } else if (!IS_GEN2(dev)) {
418 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
419 limit = &intel_limits_i9xx_lvds;
420 else
421 limit = &intel_limits_i9xx_sdvo;
79e53945
JB
422 } else {
423 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
e4b36699 424 limit = &intel_limits_i8xx_lvds;
5d536e28 425 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
e4b36699 426 limit = &intel_limits_i8xx_dvo;
5d536e28
DV
427 else
428 limit = &intel_limits_i8xx_dac;
79e53945
JB
429 }
430 return limit;
431}
432
f2b115e6
AJ
433/* m1 is reserved as 0 in Pineview, n is a ring counter */
434static void pineview_clock(int refclk, intel_clock_t *clock)
79e53945 435{
2177832f
SL
436 clock->m = clock->m2 + 2;
437 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
438 if (WARN_ON(clock->n == 0 || clock->p == 0))
439 return;
fb03ac01
VS
440 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
441 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
2177832f
SL
442}
443
7429e9d4
DV
444static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
445{
446 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
447}
448
ac58c3f0 449static void i9xx_clock(int refclk, intel_clock_t *clock)
2177832f 450{
7429e9d4 451 clock->m = i9xx_dpll_compute_m(clock);
79e53945 452 clock->p = clock->p1 * clock->p2;
ed5ca77e
VS
453 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
454 return;
fb03ac01
VS
455 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
456 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
79e53945
JB
457}
458
7c04d1d9 459#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
79e53945
JB
460/**
461 * Returns whether the given set of divisors are valid for a given refclk with
462 * the given connectors.
463 */
464
1b894b59
CW
465static bool intel_PLL_is_valid(struct drm_device *dev,
466 const intel_limit_t *limit,
467 const intel_clock_t *clock)
79e53945 468{
f01b7962
VS
469 if (clock->n < limit->n.min || limit->n.max < clock->n)
470 INTELPllInvalid("n out of range\n");
79e53945 471 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
0206e353 472 INTELPllInvalid("p1 out of range\n");
79e53945 473 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
0206e353 474 INTELPllInvalid("m2 out of range\n");
79e53945 475 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
0206e353 476 INTELPllInvalid("m1 out of range\n");
f01b7962
VS
477
478 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
479 if (clock->m1 <= clock->m2)
480 INTELPllInvalid("m1 <= m2\n");
481
482 if (!IS_VALLEYVIEW(dev)) {
483 if (clock->p < limit->p.min || limit->p.max < clock->p)
484 INTELPllInvalid("p out of range\n");
485 if (clock->m < limit->m.min || limit->m.max < clock->m)
486 INTELPllInvalid("m out of range\n");
487 }
488
79e53945 489 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
0206e353 490 INTELPllInvalid("vco out of range\n");
79e53945
JB
491 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
492 * connector, etc., rather than just a single range.
493 */
494 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
0206e353 495 INTELPllInvalid("dot out of range\n");
79e53945
JB
496
497 return true;
498}
499
d4906093 500static bool
ee9300bb 501i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
cec2f356
SP
502 int target, int refclk, intel_clock_t *match_clock,
503 intel_clock_t *best_clock)
79e53945
JB
504{
505 struct drm_device *dev = crtc->dev;
79e53945 506 intel_clock_t clock;
79e53945
JB
507 int err = target;
508
a210b028 509 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 510 /*
a210b028
DV
511 * For LVDS just rely on its current settings for dual-channel.
512 * We haven't figured out how to reliably set up different
513 * single/dual channel state, if we even can.
79e53945 514 */
1974cad0 515 if (intel_is_dual_link_lvds(dev))
79e53945
JB
516 clock.p2 = limit->p2.p2_fast;
517 else
518 clock.p2 = limit->p2.p2_slow;
519 } else {
520 if (target < limit->p2.dot_limit)
521 clock.p2 = limit->p2.p2_slow;
522 else
523 clock.p2 = limit->p2.p2_fast;
524 }
525
0206e353 526 memset(best_clock, 0, sizeof(*best_clock));
79e53945 527
42158660
ZY
528 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
529 clock.m1++) {
530 for (clock.m2 = limit->m2.min;
531 clock.m2 <= limit->m2.max; clock.m2++) {
c0efc387 532 if (clock.m2 >= clock.m1)
42158660
ZY
533 break;
534 for (clock.n = limit->n.min;
535 clock.n <= limit->n.max; clock.n++) {
536 for (clock.p1 = limit->p1.min;
537 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
538 int this_err;
539
ac58c3f0
DV
540 i9xx_clock(refclk, &clock);
541 if (!intel_PLL_is_valid(dev, limit,
542 &clock))
543 continue;
544 if (match_clock &&
545 clock.p != match_clock->p)
546 continue;
547
548 this_err = abs(clock.dot - target);
549 if (this_err < err) {
550 *best_clock = clock;
551 err = this_err;
552 }
553 }
554 }
555 }
556 }
557
558 return (err != target);
559}
560
561static bool
ee9300bb
DV
562pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
563 int target, int refclk, intel_clock_t *match_clock,
564 intel_clock_t *best_clock)
79e53945
JB
565{
566 struct drm_device *dev = crtc->dev;
79e53945 567 intel_clock_t clock;
79e53945
JB
568 int err = target;
569
a210b028 570 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
79e53945 571 /*
a210b028
DV
572 * For LVDS just rely on its current settings for dual-channel.
573 * We haven't figured out how to reliably set up different
574 * single/dual channel state, if we even can.
79e53945 575 */
1974cad0 576 if (intel_is_dual_link_lvds(dev))
79e53945
JB
577 clock.p2 = limit->p2.p2_fast;
578 else
579 clock.p2 = limit->p2.p2_slow;
580 } else {
581 if (target < limit->p2.dot_limit)
582 clock.p2 = limit->p2.p2_slow;
583 else
584 clock.p2 = limit->p2.p2_fast;
585 }
586
0206e353 587 memset(best_clock, 0, sizeof(*best_clock));
79e53945 588
42158660
ZY
589 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
590 clock.m1++) {
591 for (clock.m2 = limit->m2.min;
592 clock.m2 <= limit->m2.max; clock.m2++) {
42158660
ZY
593 for (clock.n = limit->n.min;
594 clock.n <= limit->n.max; clock.n++) {
595 for (clock.p1 = limit->p1.min;
596 clock.p1 <= limit->p1.max; clock.p1++) {
79e53945
JB
597 int this_err;
598
ac58c3f0 599 pineview_clock(refclk, &clock);
1b894b59
CW
600 if (!intel_PLL_is_valid(dev, limit,
601 &clock))
79e53945 602 continue;
cec2f356
SP
603 if (match_clock &&
604 clock.p != match_clock->p)
605 continue;
79e53945
JB
606
607 this_err = abs(clock.dot - target);
608 if (this_err < err) {
609 *best_clock = clock;
610 err = this_err;
611 }
612 }
613 }
614 }
615 }
616
617 return (err != target);
618}
619
d4906093 620static bool
ee9300bb
DV
621g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
622 int target, int refclk, intel_clock_t *match_clock,
623 intel_clock_t *best_clock)
d4906093
ML
624{
625 struct drm_device *dev = crtc->dev;
d4906093
ML
626 intel_clock_t clock;
627 int max_n;
628 bool found;
6ba770dc
AJ
629 /* approximately equals target * 0.00585 */
630 int err_most = (target >> 8) + (target >> 9);
d4906093
ML
631 found = false;
632
633 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
1974cad0 634 if (intel_is_dual_link_lvds(dev))
d4906093
ML
635 clock.p2 = limit->p2.p2_fast;
636 else
637 clock.p2 = limit->p2.p2_slow;
638 } else {
639 if (target < limit->p2.dot_limit)
640 clock.p2 = limit->p2.p2_slow;
641 else
642 clock.p2 = limit->p2.p2_fast;
643 }
644
645 memset(best_clock, 0, sizeof(*best_clock));
646 max_n = limit->n.max;
f77f13e2 647 /* based on hardware requirement, prefer smaller n to precision */
d4906093 648 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
f77f13e2 649 /* based on hardware requirement, prefere larger m1,m2 */
d4906093
ML
650 for (clock.m1 = limit->m1.max;
651 clock.m1 >= limit->m1.min; clock.m1--) {
652 for (clock.m2 = limit->m2.max;
653 clock.m2 >= limit->m2.min; clock.m2--) {
654 for (clock.p1 = limit->p1.max;
655 clock.p1 >= limit->p1.min; clock.p1--) {
656 int this_err;
657
ac58c3f0 658 i9xx_clock(refclk, &clock);
1b894b59
CW
659 if (!intel_PLL_is_valid(dev, limit,
660 &clock))
d4906093 661 continue;
1b894b59
CW
662
663 this_err = abs(clock.dot - target);
d4906093
ML
664 if (this_err < err_most) {
665 *best_clock = clock;
666 err_most = this_err;
667 max_n = clock.n;
668 found = true;
669 }
670 }
671 }
672 }
673 }
2c07245f
ZW
674 return found;
675}
676
a0c4da24 677static bool
ee9300bb
DV
678vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
679 int target, int refclk, intel_clock_t *match_clock,
680 intel_clock_t *best_clock)
a0c4da24 681{
f01b7962 682 struct drm_device *dev = crtc->dev;
6b4bf1c4 683 intel_clock_t clock;
69e4f900 684 unsigned int bestppm = 1000000;
27e639bf
VS
685 /* min update 19.2 MHz */
686 int max_n = min(limit->n.max, refclk / 19200);
49e497ef 687 bool found = false;
a0c4da24 688
6b4bf1c4
VS
689 target *= 5; /* fast clock */
690
691 memset(best_clock, 0, sizeof(*best_clock));
a0c4da24
JB
692
693 /* based on hardware requirement, prefer smaller n to precision */
27e639bf 694 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
811bbf05 695 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
889059d8 696 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
c1a9ae43 697 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
6b4bf1c4 698 clock.p = clock.p1 * clock.p2;
a0c4da24 699 /* based on hardware requirement, prefer bigger m1,m2 values */
6b4bf1c4 700 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
69e4f900
VS
701 unsigned int ppm, diff;
702
6b4bf1c4
VS
703 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
704 refclk * clock.m1);
705
706 vlv_clock(refclk, &clock);
43b0ac53 707
f01b7962
VS
708 if (!intel_PLL_is_valid(dev, limit,
709 &clock))
43b0ac53
VS
710 continue;
711
6b4bf1c4
VS
712 diff = abs(clock.dot - target);
713 ppm = div_u64(1000000ULL * diff, target);
714
715 if (ppm < 100 && clock.p > best_clock->p) {
43b0ac53 716 bestppm = 0;
6b4bf1c4 717 *best_clock = clock;
49e497ef 718 found = true;
43b0ac53 719 }
6b4bf1c4 720
c686122c 721 if (bestppm >= 10 && ppm < bestppm - 10) {
69e4f900 722 bestppm = ppm;
6b4bf1c4 723 *best_clock = clock;
49e497ef 724 found = true;
a0c4da24
JB
725 }
726 }
727 }
728 }
729 }
a0c4da24 730
49e497ef 731 return found;
a0c4da24 732}
a4fc5ed6 733
20ddf665
VS
734bool intel_crtc_active(struct drm_crtc *crtc)
735{
736 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
737
738 /* Be paranoid as we can arrive here with only partial
739 * state retrieved from the hardware during setup.
740 *
241bfc38 741 * We can ditch the adjusted_mode.crtc_clock check as soon
20ddf665
VS
742 * as Haswell has gained clock readout/fastboot support.
743 *
744 * We can ditch the crtc->fb check as soon as we can
745 * properly reconstruct framebuffers.
746 */
747 return intel_crtc->active && crtc->fb &&
241bfc38 748 intel_crtc->config.adjusted_mode.crtc_clock;
20ddf665
VS
749}
750
a5c961d1
PZ
751enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
752 enum pipe pipe)
753{
754 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
755 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756
3b117c8f 757 return intel_crtc->config.cpu_transcoder;
a5c961d1
PZ
758}
759
57e22f4a 760static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
a928d536
PZ
761{
762 struct drm_i915_private *dev_priv = dev->dev_private;
57e22f4a 763 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
a928d536
PZ
764
765 frame = I915_READ(frame_reg);
766
767 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
768 DRM_DEBUG_KMS("vblank wait timed out\n");
769}
770
9d0498a2
JB
771/**
772 * intel_wait_for_vblank - wait for vblank on a given pipe
773 * @dev: drm device
774 * @pipe: pipe to wait for
775 *
776 * Wait for vblank to occur on a given pipe. Needed for various bits of
777 * mode setting code.
778 */
779void intel_wait_for_vblank(struct drm_device *dev, int pipe)
79e53945 780{
9d0498a2 781 struct drm_i915_private *dev_priv = dev->dev_private;
9db4a9c7 782 int pipestat_reg = PIPESTAT(pipe);
9d0498a2 783
57e22f4a
VS
784 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
785 g4x_wait_for_vblank(dev, pipe);
a928d536
PZ
786 return;
787 }
788
300387c0
CW
789 /* Clear existing vblank status. Note this will clear any other
790 * sticky status fields as well.
791 *
792 * This races with i915_driver_irq_handler() with the result
793 * that either function could miss a vblank event. Here it is not
794 * fatal, as we will either wait upon the next vblank interrupt or
795 * timeout. Generally speaking intel_wait_for_vblank() is only
796 * called during modeset at which time the GPU should be idle and
797 * should *not* be performing page flips and thus not waiting on
798 * vblanks...
799 * Currently, the result of us stealing a vblank from the irq
800 * handler is that a single frame will be skipped during swapbuffers.
801 */
802 I915_WRITE(pipestat_reg,
803 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
804
9d0498a2 805 /* Wait for vblank interrupt bit to set */
481b6af3
CW
806 if (wait_for(I915_READ(pipestat_reg) &
807 PIPE_VBLANK_INTERRUPT_STATUS,
808 50))
9d0498a2
JB
809 DRM_DEBUG_KMS("vblank wait timed out\n");
810}
811
fbf49ea2
VS
812static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
813{
814 struct drm_i915_private *dev_priv = dev->dev_private;
815 u32 reg = PIPEDSL(pipe);
816 u32 line1, line2;
817 u32 line_mask;
818
819 if (IS_GEN2(dev))
820 line_mask = DSL_LINEMASK_GEN2;
821 else
822 line_mask = DSL_LINEMASK_GEN3;
823
824 line1 = I915_READ(reg) & line_mask;
825 mdelay(5);
826 line2 = I915_READ(reg) & line_mask;
827
828 return line1 == line2;
829}
830
ab7ad7f6
KP
831/*
832 * intel_wait_for_pipe_off - wait for pipe to turn off
9d0498a2
JB
833 * @dev: drm device
834 * @pipe: pipe to wait for
835 *
836 * After disabling a pipe, we can't wait for vblank in the usual way,
837 * spinning on the vblank interrupt status bit, since we won't actually
838 * see an interrupt when the pipe is disabled.
839 *
ab7ad7f6
KP
840 * On Gen4 and above:
841 * wait for the pipe register state bit to turn off
842 *
843 * Otherwise:
844 * wait for the display line value to settle (it usually
845 * ends up stopping at the start of the next frame).
58e10eb9 846 *
9d0498a2 847 */
58e10eb9 848void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
9d0498a2
JB
849{
850 struct drm_i915_private *dev_priv = dev->dev_private;
702e7a56
PZ
851 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
852 pipe);
ab7ad7f6
KP
853
854 if (INTEL_INFO(dev)->gen >= 4) {
702e7a56 855 int reg = PIPECONF(cpu_transcoder);
ab7ad7f6
KP
856
857 /* Wait for the Pipe State to go off */
58e10eb9
CW
858 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
859 100))
284637d9 860 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 861 } else {
ab7ad7f6 862 /* Wait for the display line to settle */
fbf49ea2 863 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
284637d9 864 WARN(1, "pipe_off wait timed out\n");
ab7ad7f6 865 }
79e53945
JB
866}
867
b0ea7d37
DL
868/*
869 * ibx_digital_port_connected - is the specified port connected?
870 * @dev_priv: i915 private structure
871 * @port: the port to test
872 *
873 * Returns true if @port is connected, false otherwise.
874 */
875bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
876 struct intel_digital_port *port)
877{
878 u32 bit;
879
c36346e3
DL
880 if (HAS_PCH_IBX(dev_priv->dev)) {
881 switch(port->port) {
882 case PORT_B:
883 bit = SDE_PORTB_HOTPLUG;
884 break;
885 case PORT_C:
886 bit = SDE_PORTC_HOTPLUG;
887 break;
888 case PORT_D:
889 bit = SDE_PORTD_HOTPLUG;
890 break;
891 default:
892 return true;
893 }
894 } else {
895 switch(port->port) {
896 case PORT_B:
897 bit = SDE_PORTB_HOTPLUG_CPT;
898 break;
899 case PORT_C:
900 bit = SDE_PORTC_HOTPLUG_CPT;
901 break;
902 case PORT_D:
903 bit = SDE_PORTD_HOTPLUG_CPT;
904 break;
905 default:
906 return true;
907 }
b0ea7d37
DL
908 }
909
910 return I915_READ(SDEISR) & bit;
911}
912
b24e7179
JB
913static const char *state_string(bool enabled)
914{
915 return enabled ? "on" : "off";
916}
917
918/* Only for pre-ILK configs */
55607e8a
DV
919void assert_pll(struct drm_i915_private *dev_priv,
920 enum pipe pipe, bool state)
b24e7179
JB
921{
922 int reg;
923 u32 val;
924 bool cur_state;
925
926 reg = DPLL(pipe);
927 val = I915_READ(reg);
928 cur_state = !!(val & DPLL_VCO_ENABLE);
929 WARN(cur_state != state,
930 "PLL state assertion failure (expected %s, current %s)\n",
931 state_string(state), state_string(cur_state));
932}
b24e7179 933
23538ef1
JN
934/* XXX: the dsi pll is shared between MIPI DSI ports */
935static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
936{
937 u32 val;
938 bool cur_state;
939
940 mutex_lock(&dev_priv->dpio_lock);
941 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
942 mutex_unlock(&dev_priv->dpio_lock);
943
944 cur_state = val & DSI_PLL_VCO_EN;
945 WARN(cur_state != state,
946 "DSI PLL state assertion failure (expected %s, current %s)\n",
947 state_string(state), state_string(cur_state));
948}
949#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
950#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
951
55607e8a 952struct intel_shared_dpll *
e2b78267
DV
953intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
954{
955 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
956
a43f6e0f 957 if (crtc->config.shared_dpll < 0)
e2b78267
DV
958 return NULL;
959
a43f6e0f 960 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
e2b78267
DV
961}
962
040484af 963/* For ILK+ */
55607e8a
DV
964void assert_shared_dpll(struct drm_i915_private *dev_priv,
965 struct intel_shared_dpll *pll,
966 bool state)
040484af 967{
040484af 968 bool cur_state;
5358901f 969 struct intel_dpll_hw_state hw_state;
040484af 970
9d82aa17
ED
971 if (HAS_PCH_LPT(dev_priv->dev)) {
972 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
973 return;
974 }
975
92b27b08 976 if (WARN (!pll,
46edb027 977 "asserting DPLL %s with no DPLL\n", state_string(state)))
ee7b9f93 978 return;
ee7b9f93 979
5358901f 980 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
92b27b08 981 WARN(cur_state != state,
5358901f
DV
982 "%s assertion failure (expected %s, current %s)\n",
983 pll->name, state_string(state), state_string(cur_state));
040484af 984}
040484af
JB
985
986static void assert_fdi_tx(struct drm_i915_private *dev_priv,
987 enum pipe pipe, bool state)
988{
989 int reg;
990 u32 val;
991 bool cur_state;
ad80a810
PZ
992 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
993 pipe);
040484af 994
affa9354
PZ
995 if (HAS_DDI(dev_priv->dev)) {
996 /* DDI does not have a specific FDI_TX register */
ad80a810 997 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
bf507ef7 998 val = I915_READ(reg);
ad80a810 999 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
bf507ef7
ED
1000 } else {
1001 reg = FDI_TX_CTL(pipe);
1002 val = I915_READ(reg);
1003 cur_state = !!(val & FDI_TX_ENABLE);
1004 }
040484af
JB
1005 WARN(cur_state != state,
1006 "FDI TX state assertion failure (expected %s, current %s)\n",
1007 state_string(state), state_string(cur_state));
1008}
1009#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1010#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1011
1012static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1013 enum pipe pipe, bool state)
1014{
1015 int reg;
1016 u32 val;
1017 bool cur_state;
1018
d63fa0dc
PZ
1019 reg = FDI_RX_CTL(pipe);
1020 val = I915_READ(reg);
1021 cur_state = !!(val & FDI_RX_ENABLE);
040484af
JB
1022 WARN(cur_state != state,
1023 "FDI RX state assertion failure (expected %s, current %s)\n",
1024 state_string(state), state_string(cur_state));
1025}
1026#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1027#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1028
1029static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1030 enum pipe pipe)
1031{
1032 int reg;
1033 u32 val;
1034
1035 /* ILK FDI PLL is always enabled */
3d13ef2e 1036 if (INTEL_INFO(dev_priv->dev)->gen == 5)
040484af
JB
1037 return;
1038
bf507ef7 1039 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
affa9354 1040 if (HAS_DDI(dev_priv->dev))
bf507ef7
ED
1041 return;
1042
040484af
JB
1043 reg = FDI_TX_CTL(pipe);
1044 val = I915_READ(reg);
1045 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1046}
1047
55607e8a
DV
1048void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1049 enum pipe pipe, bool state)
040484af
JB
1050{
1051 int reg;
1052 u32 val;
55607e8a 1053 bool cur_state;
040484af
JB
1054
1055 reg = FDI_RX_CTL(pipe);
1056 val = I915_READ(reg);
55607e8a
DV
1057 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1058 WARN(cur_state != state,
1059 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1060 state_string(state), state_string(cur_state));
040484af
JB
1061}
1062
ea0760cf
JB
1063static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1064 enum pipe pipe)
1065{
1066 int pp_reg, lvds_reg;
1067 u32 val;
1068 enum pipe panel_pipe = PIPE_A;
0de3b485 1069 bool locked = true;
ea0760cf
JB
1070
1071 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1072 pp_reg = PCH_PP_CONTROL;
1073 lvds_reg = PCH_LVDS;
1074 } else {
1075 pp_reg = PP_CONTROL;
1076 lvds_reg = LVDS;
1077 }
1078
1079 val = I915_READ(pp_reg);
1080 if (!(val & PANEL_POWER_ON) ||
1081 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1082 locked = false;
1083
1084 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1085 panel_pipe = PIPE_B;
1086
1087 WARN(panel_pipe == pipe && locked,
1088 "panel assertion failure, pipe %c regs locked\n",
9db4a9c7 1089 pipe_name(pipe));
ea0760cf
JB
1090}
1091
93ce0ba6
JN
1092static void assert_cursor(struct drm_i915_private *dev_priv,
1093 enum pipe pipe, bool state)
1094{
1095 struct drm_device *dev = dev_priv->dev;
1096 bool cur_state;
1097
d9d82081 1098 if (IS_845G(dev) || IS_I865G(dev))
93ce0ba6 1099 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
d9d82081 1100 else if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev))
93ce0ba6 1101 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
d9d82081
PZ
1102 else
1103 cur_state = I915_READ(CURCNTR_IVB(pipe)) & CURSOR_MODE;
93ce0ba6
JN
1104
1105 WARN(cur_state != state,
1106 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1107 pipe_name(pipe), state_string(state), state_string(cur_state));
1108}
1109#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1110#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1111
b840d907
JB
1112void assert_pipe(struct drm_i915_private *dev_priv,
1113 enum pipe pipe, bool state)
b24e7179
JB
1114{
1115 int reg;
1116 u32 val;
63d7bbe9 1117 bool cur_state;
702e7a56
PZ
1118 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1119 pipe);
b24e7179 1120
8e636784
DV
1121 /* if we need the pipe A quirk it must be always on */
1122 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1123 state = true;
1124
da7e29bd 1125 if (!intel_display_power_enabled(dev_priv,
b97186f0 1126 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
69310161
PZ
1127 cur_state = false;
1128 } else {
1129 reg = PIPECONF(cpu_transcoder);
1130 val = I915_READ(reg);
1131 cur_state = !!(val & PIPECONF_ENABLE);
1132 }
1133
63d7bbe9
JB
1134 WARN(cur_state != state,
1135 "pipe %c assertion failure (expected %s, current %s)\n",
9db4a9c7 1136 pipe_name(pipe), state_string(state), state_string(cur_state));
b24e7179
JB
1137}
1138
931872fc
CW
1139static void assert_plane(struct drm_i915_private *dev_priv,
1140 enum plane plane, bool state)
b24e7179
JB
1141{
1142 int reg;
1143 u32 val;
931872fc 1144 bool cur_state;
b24e7179
JB
1145
1146 reg = DSPCNTR(plane);
1147 val = I915_READ(reg);
931872fc
CW
1148 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1149 WARN(cur_state != state,
1150 "plane %c assertion failure (expected %s, current %s)\n",
1151 plane_name(plane), state_string(state), state_string(cur_state));
b24e7179
JB
1152}
1153
931872fc
CW
1154#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1155#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1156
b24e7179
JB
1157static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1158 enum pipe pipe)
1159{
653e1026 1160 struct drm_device *dev = dev_priv->dev;
b24e7179
JB
1161 int reg, i;
1162 u32 val;
1163 int cur_pipe;
1164
653e1026
VS
1165 /* Primary planes are fixed to pipes on gen4+ */
1166 if (INTEL_INFO(dev)->gen >= 4) {
28c05794
AJ
1167 reg = DSPCNTR(pipe);
1168 val = I915_READ(reg);
83f26f16 1169 WARN(val & DISPLAY_PLANE_ENABLE,
28c05794
AJ
1170 "plane %c assertion failure, should be disabled but not\n",
1171 plane_name(pipe));
19ec1358 1172 return;
28c05794 1173 }
19ec1358 1174
b24e7179 1175 /* Need to check both planes against the pipe */
08e2a7de 1176 for_each_pipe(i) {
b24e7179
JB
1177 reg = DSPCNTR(i);
1178 val = I915_READ(reg);
1179 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1180 DISPPLANE_SEL_PIPE_SHIFT;
1181 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
9db4a9c7
JB
1182 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1183 plane_name(i), pipe_name(pipe));
b24e7179
JB
1184 }
1185}
1186
19332d7a
JB
1187static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1188 enum pipe pipe)
1189{
20674eef 1190 struct drm_device *dev = dev_priv->dev;
1fe47785 1191 int reg, sprite;
19332d7a
JB
1192 u32 val;
1193
20674eef 1194 if (IS_VALLEYVIEW(dev)) {
1fe47785
DL
1195 for_each_sprite(pipe, sprite) {
1196 reg = SPCNTR(pipe, sprite);
20674eef 1197 val = I915_READ(reg);
83f26f16 1198 WARN(val & SP_ENABLE,
20674eef 1199 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1fe47785 1200 sprite_name(pipe, sprite), pipe_name(pipe));
20674eef
VS
1201 }
1202 } else if (INTEL_INFO(dev)->gen >= 7) {
1203 reg = SPRCTL(pipe);
19332d7a 1204 val = I915_READ(reg);
83f26f16 1205 WARN(val & SPRITE_ENABLE,
06da8da2 1206 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef
VS
1207 plane_name(pipe), pipe_name(pipe));
1208 } else if (INTEL_INFO(dev)->gen >= 5) {
1209 reg = DVSCNTR(pipe);
19332d7a 1210 val = I915_READ(reg);
83f26f16 1211 WARN(val & DVS_ENABLE,
06da8da2 1212 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
20674eef 1213 plane_name(pipe), pipe_name(pipe));
19332d7a
JB
1214 }
1215}
1216
89eff4be 1217static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
92f2584a
JB
1218{
1219 u32 val;
1220 bool enabled;
1221
89eff4be 1222 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
9d82aa17 1223
92f2584a
JB
1224 val = I915_READ(PCH_DREF_CONTROL);
1225 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1226 DREF_SUPERSPREAD_SOURCE_MASK));
1227 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1228}
1229
ab9412ba
DV
1230static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1231 enum pipe pipe)
92f2584a
JB
1232{
1233 int reg;
1234 u32 val;
1235 bool enabled;
1236
ab9412ba 1237 reg = PCH_TRANSCONF(pipe);
92f2584a
JB
1238 val = I915_READ(reg);
1239 enabled = !!(val & TRANS_ENABLE);
9db4a9c7
JB
1240 WARN(enabled,
1241 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1242 pipe_name(pipe));
92f2584a
JB
1243}
1244
4e634389
KP
1245static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1246 enum pipe pipe, u32 port_sel, u32 val)
f0575e92
KP
1247{
1248 if ((val & DP_PORT_EN) == 0)
1249 return false;
1250
1251 if (HAS_PCH_CPT(dev_priv->dev)) {
1252 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1253 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1254 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1255 return false;
1256 } else {
1257 if ((val & DP_PIPE_MASK) != (pipe << 30))
1258 return false;
1259 }
1260 return true;
1261}
1262
1519b995
KP
1263static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1264 enum pipe pipe, u32 val)
1265{
dc0fa718 1266 if ((val & SDVO_ENABLE) == 0)
1519b995
KP
1267 return false;
1268
1269 if (HAS_PCH_CPT(dev_priv->dev)) {
dc0fa718 1270 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
1519b995
KP
1271 return false;
1272 } else {
dc0fa718 1273 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
1519b995
KP
1274 return false;
1275 }
1276 return true;
1277}
1278
1279static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1280 enum pipe pipe, u32 val)
1281{
1282 if ((val & LVDS_PORT_EN) == 0)
1283 return false;
1284
1285 if (HAS_PCH_CPT(dev_priv->dev)) {
1286 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1287 return false;
1288 } else {
1289 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1290 return false;
1291 }
1292 return true;
1293}
1294
1295static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1296 enum pipe pipe, u32 val)
1297{
1298 if ((val & ADPA_DAC_ENABLE) == 0)
1299 return false;
1300 if (HAS_PCH_CPT(dev_priv->dev)) {
1301 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1302 return false;
1303 } else {
1304 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1305 return false;
1306 }
1307 return true;
1308}
1309
291906f1 1310static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
f0575e92 1311 enum pipe pipe, int reg, u32 port_sel)
291906f1 1312{
47a05eca 1313 u32 val = I915_READ(reg);
4e634389 1314 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
291906f1 1315 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1316 reg, pipe_name(pipe));
de9a35ab 1317
75c5da27
DV
1318 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1319 && (val & DP_PIPEB_SELECT),
de9a35ab 1320 "IBX PCH dp port still using transcoder B\n");
291906f1
JB
1321}
1322
1323static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1324 enum pipe pipe, int reg)
1325{
47a05eca 1326 u32 val = I915_READ(reg);
b70ad586 1327 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
23c99e77 1328 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
9db4a9c7 1329 reg, pipe_name(pipe));
de9a35ab 1330
dc0fa718 1331 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
75c5da27 1332 && (val & SDVO_PIPE_B_SELECT),
de9a35ab 1333 "IBX PCH hdmi port still using transcoder B\n");
291906f1
JB
1334}
1335
1336static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1337 enum pipe pipe)
1338{
1339 int reg;
1340 u32 val;
291906f1 1341
f0575e92
KP
1342 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1343 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1344 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
291906f1
JB
1345
1346 reg = PCH_ADPA;
1347 val = I915_READ(reg);
b70ad586 1348 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
291906f1 1349 "PCH VGA enabled on transcoder %c, should be disabled\n",
9db4a9c7 1350 pipe_name(pipe));
291906f1
JB
1351
1352 reg = PCH_LVDS;
1353 val = I915_READ(reg);
b70ad586 1354 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
291906f1 1355 "PCH LVDS enabled on transcoder %c, should be disabled\n",
9db4a9c7 1356 pipe_name(pipe));
291906f1 1357
e2debe91
PZ
1358 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1359 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1360 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
291906f1
JB
1361}
1362
40e9cf64
JB
1363static void intel_init_dpio(struct drm_device *dev)
1364{
1365 struct drm_i915_private *dev_priv = dev->dev_private;
1366
1367 if (!IS_VALLEYVIEW(dev))
1368 return;
1369
e4607fcf 1370 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
5382f5f3
JB
1371}
1372
1373static void intel_reset_dpio(struct drm_device *dev)
1374{
1375 struct drm_i915_private *dev_priv = dev->dev_private;
1376
1377 if (!IS_VALLEYVIEW(dev))
1378 return;
1379
e5cbfbfb
ID
1380 /*
1381 * Enable the CRI clock source so we can get at the display and the
1382 * reference clock for VGA hotplug / manual detection.
1383 */
404faabc 1384 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
e5cbfbfb 1385 DPLL_REFA_CLK_ENABLE_VLV |
404faabc
ID
1386 DPLL_INTEGRATED_CRI_CLK_VLV);
1387
40e9cf64
JB
1388 /*
1389 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1390 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1391 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1392 * b. The other bits such as sfr settings / modesel may all be set
1393 * to 0.
1394 *
1395 * This should only be done on init and resume from S3 with both
1396 * PLLs disabled, or we risk losing DPIO and PLL synchronization.
1397 */
1398 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1399}
1400
426115cf 1401static void vlv_enable_pll(struct intel_crtc *crtc)
87442f73 1402{
426115cf
DV
1403 struct drm_device *dev = crtc->base.dev;
1404 struct drm_i915_private *dev_priv = dev->dev_private;
1405 int reg = DPLL(crtc->pipe);
1406 u32 dpll = crtc->config.dpll_hw_state.dpll;
87442f73 1407
426115cf 1408 assert_pipe_disabled(dev_priv, crtc->pipe);
87442f73
DV
1409
1410 /* No really, not for ILK+ */
1411 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1412
1413 /* PLL is protected by panel, make sure we can write it */
1414 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
426115cf 1415 assert_panel_unlocked(dev_priv, crtc->pipe);
87442f73 1416
426115cf
DV
1417 I915_WRITE(reg, dpll);
1418 POSTING_READ(reg);
1419 udelay(150);
1420
1421 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1422 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1423
1424 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1425 POSTING_READ(DPLL_MD(crtc->pipe));
87442f73
DV
1426
1427 /* We do this three times for luck */
426115cf 1428 I915_WRITE(reg, dpll);
87442f73
DV
1429 POSTING_READ(reg);
1430 udelay(150); /* wait for warmup */
426115cf 1431 I915_WRITE(reg, dpll);
87442f73
DV
1432 POSTING_READ(reg);
1433 udelay(150); /* wait for warmup */
426115cf 1434 I915_WRITE(reg, dpll);
87442f73
DV
1435 POSTING_READ(reg);
1436 udelay(150); /* wait for warmup */
1437}
1438
66e3d5c0 1439static void i9xx_enable_pll(struct intel_crtc *crtc)
63d7bbe9 1440{
66e3d5c0
DV
1441 struct drm_device *dev = crtc->base.dev;
1442 struct drm_i915_private *dev_priv = dev->dev_private;
1443 int reg = DPLL(crtc->pipe);
1444 u32 dpll = crtc->config.dpll_hw_state.dpll;
63d7bbe9 1445
66e3d5c0 1446 assert_pipe_disabled(dev_priv, crtc->pipe);
58c6eaa2 1447
63d7bbe9 1448 /* No really, not for ILK+ */
3d13ef2e 1449 BUG_ON(INTEL_INFO(dev)->gen >= 5);
63d7bbe9
JB
1450
1451 /* PLL is protected by panel, make sure we can write it */
66e3d5c0
DV
1452 if (IS_MOBILE(dev) && !IS_I830(dev))
1453 assert_panel_unlocked(dev_priv, crtc->pipe);
63d7bbe9 1454
66e3d5c0
DV
1455 I915_WRITE(reg, dpll);
1456
1457 /* Wait for the clocks to stabilize. */
1458 POSTING_READ(reg);
1459 udelay(150);
1460
1461 if (INTEL_INFO(dev)->gen >= 4) {
1462 I915_WRITE(DPLL_MD(crtc->pipe),
1463 crtc->config.dpll_hw_state.dpll_md);
1464 } else {
1465 /* The pixel multiplier can only be updated once the
1466 * DPLL is enabled and the clocks are stable.
1467 *
1468 * So write it again.
1469 */
1470 I915_WRITE(reg, dpll);
1471 }
63d7bbe9
JB
1472
1473 /* We do this three times for luck */
66e3d5c0 1474 I915_WRITE(reg, dpll);
63d7bbe9
JB
1475 POSTING_READ(reg);
1476 udelay(150); /* wait for warmup */
66e3d5c0 1477 I915_WRITE(reg, dpll);
63d7bbe9
JB
1478 POSTING_READ(reg);
1479 udelay(150); /* wait for warmup */
66e3d5c0 1480 I915_WRITE(reg, dpll);
63d7bbe9
JB
1481 POSTING_READ(reg);
1482 udelay(150); /* wait for warmup */
1483}
1484
1485/**
50b44a44 1486 * i9xx_disable_pll - disable a PLL
63d7bbe9
JB
1487 * @dev_priv: i915 private structure
1488 * @pipe: pipe PLL to disable
1489 *
1490 * Disable the PLL for @pipe, making sure the pipe is off first.
1491 *
1492 * Note! This is for pre-ILK only.
1493 */
50b44a44 1494static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
63d7bbe9 1495{
63d7bbe9
JB
1496 /* Don't disable pipe A or pipe A PLLs if needed */
1497 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1498 return;
1499
1500 /* Make sure the pipe isn't still relying on us */
1501 assert_pipe_disabled(dev_priv, pipe);
1502
50b44a44
DV
1503 I915_WRITE(DPLL(pipe), 0);
1504 POSTING_READ(DPLL(pipe));
63d7bbe9
JB
1505}
1506
f6071166
JB
1507static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1508{
1509 u32 val = 0;
1510
1511 /* Make sure the pipe isn't still relying on us */
1512 assert_pipe_disabled(dev_priv, pipe);
1513
e5cbfbfb
ID
1514 /*
1515 * Leave integrated clock source and reference clock enabled for pipe B.
1516 * The latter is needed for VGA hotplug / manual detection.
1517 */
f6071166 1518 if (pipe == PIPE_B)
e5cbfbfb 1519 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
f6071166
JB
1520 I915_WRITE(DPLL(pipe), val);
1521 POSTING_READ(DPLL(pipe));
1522}
1523
e4607fcf
CML
1524void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1525 struct intel_digital_port *dport)
89b667f8
JB
1526{
1527 u32 port_mask;
1528
e4607fcf
CML
1529 switch (dport->port) {
1530 case PORT_B:
89b667f8 1531 port_mask = DPLL_PORTB_READY_MASK;
e4607fcf
CML
1532 break;
1533 case PORT_C:
89b667f8 1534 port_mask = DPLL_PORTC_READY_MASK;
e4607fcf
CML
1535 break;
1536 default:
1537 BUG();
1538 }
89b667f8
JB
1539
1540 if (wait_for((I915_READ(DPLL(0)) & port_mask) == 0, 1000))
1541 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
be46ffd4 1542 port_name(dport->port), I915_READ(DPLL(0)));
89b667f8
JB
1543}
1544
92f2584a 1545/**
e72f9fbf 1546 * ironlake_enable_shared_dpll - enable PCH PLL
92f2584a
JB
1547 * @dev_priv: i915 private structure
1548 * @pipe: pipe PLL to enable
1549 *
1550 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1551 * drives the transcoder clock.
1552 */
e2b78267 1553static void ironlake_enable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1554{
3d13ef2e
DL
1555 struct drm_device *dev = crtc->base.dev;
1556 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1557 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
92f2584a 1558
48da64a8 1559 /* PCH PLLs only available on ILK, SNB and IVB */
3d13ef2e 1560 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1561 if (WARN_ON(pll == NULL))
48da64a8
CW
1562 return;
1563
1564 if (WARN_ON(pll->refcount == 0))
1565 return;
ee7b9f93 1566
46edb027
DV
1567 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1568 pll->name, pll->active, pll->on,
e2b78267 1569 crtc->base.base.id);
92f2584a 1570
cdbd2316
DV
1571 if (pll->active++) {
1572 WARN_ON(!pll->on);
e9d6944e 1573 assert_shared_dpll_enabled(dev_priv, pll);
ee7b9f93
JB
1574 return;
1575 }
f4a091c7 1576 WARN_ON(pll->on);
ee7b9f93 1577
46edb027 1578 DRM_DEBUG_KMS("enabling %s\n", pll->name);
e7b903d2 1579 pll->enable(dev_priv, pll);
ee7b9f93 1580 pll->on = true;
92f2584a
JB
1581}
1582
e2b78267 1583static void intel_disable_shared_dpll(struct intel_crtc *crtc)
92f2584a 1584{
3d13ef2e
DL
1585 struct drm_device *dev = crtc->base.dev;
1586 struct drm_i915_private *dev_priv = dev->dev_private;
e2b78267 1587 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
4c609cb8 1588
92f2584a 1589 /* PCH only available on ILK+ */
3d13ef2e 1590 BUG_ON(INTEL_INFO(dev)->gen < 5);
87a875bb 1591 if (WARN_ON(pll == NULL))
ee7b9f93 1592 return;
92f2584a 1593
48da64a8
CW
1594 if (WARN_ON(pll->refcount == 0))
1595 return;
7a419866 1596
46edb027
DV
1597 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1598 pll->name, pll->active, pll->on,
e2b78267 1599 crtc->base.base.id);
7a419866 1600
48da64a8 1601 if (WARN_ON(pll->active == 0)) {
e9d6944e 1602 assert_shared_dpll_disabled(dev_priv, pll);
48da64a8
CW
1603 return;
1604 }
1605
e9d6944e 1606 assert_shared_dpll_enabled(dev_priv, pll);
f4a091c7 1607 WARN_ON(!pll->on);
cdbd2316 1608 if (--pll->active)
7a419866 1609 return;
ee7b9f93 1610
46edb027 1611 DRM_DEBUG_KMS("disabling %s\n", pll->name);
e7b903d2 1612 pll->disable(dev_priv, pll);
ee7b9f93 1613 pll->on = false;
92f2584a
JB
1614}
1615
b8a4f404
PZ
1616static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1617 enum pipe pipe)
040484af 1618{
23670b32 1619 struct drm_device *dev = dev_priv->dev;
7c26e5c6 1620 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
e2b78267 1621 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
23670b32 1622 uint32_t reg, val, pipeconf_val;
040484af
JB
1623
1624 /* PCH only available on ILK+ */
3d13ef2e 1625 BUG_ON(INTEL_INFO(dev)->gen < 5);
040484af
JB
1626
1627 /* Make sure PCH DPLL is enabled */
e72f9fbf 1628 assert_shared_dpll_enabled(dev_priv,
e9d6944e 1629 intel_crtc_to_shared_dpll(intel_crtc));
040484af
JB
1630
1631 /* FDI must be feeding us bits for PCH ports */
1632 assert_fdi_tx_enabled(dev_priv, pipe);
1633 assert_fdi_rx_enabled(dev_priv, pipe);
1634
23670b32
DV
1635 if (HAS_PCH_CPT(dev)) {
1636 /* Workaround: Set the timing override bit before enabling the
1637 * pch transcoder. */
1638 reg = TRANS_CHICKEN2(pipe);
1639 val = I915_READ(reg);
1640 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1641 I915_WRITE(reg, val);
59c859d6 1642 }
23670b32 1643
ab9412ba 1644 reg = PCH_TRANSCONF(pipe);
040484af 1645 val = I915_READ(reg);
5f7f726d 1646 pipeconf_val = I915_READ(PIPECONF(pipe));
e9bcff5c
JB
1647
1648 if (HAS_PCH_IBX(dev_priv->dev)) {
1649 /*
1650 * make the BPC in transcoder be consistent with
1651 * that in pipeconf reg.
1652 */
dfd07d72
DV
1653 val &= ~PIPECONF_BPC_MASK;
1654 val |= pipeconf_val & PIPECONF_BPC_MASK;
e9bcff5c 1655 }
5f7f726d
PZ
1656
1657 val &= ~TRANS_INTERLACE_MASK;
1658 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
7c26e5c6
PZ
1659 if (HAS_PCH_IBX(dev_priv->dev) &&
1660 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1661 val |= TRANS_LEGACY_INTERLACED_ILK;
1662 else
1663 val |= TRANS_INTERLACED;
5f7f726d
PZ
1664 else
1665 val |= TRANS_PROGRESSIVE;
1666
040484af
JB
1667 I915_WRITE(reg, val | TRANS_ENABLE);
1668 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
4bb6f1f3 1669 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
040484af
JB
1670}
1671
8fb033d7 1672static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
937bb610 1673 enum transcoder cpu_transcoder)
040484af 1674{
8fb033d7 1675 u32 val, pipeconf_val;
8fb033d7
PZ
1676
1677 /* PCH only available on ILK+ */
3d13ef2e 1678 BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
8fb033d7 1679
8fb033d7 1680 /* FDI must be feeding us bits for PCH ports */
1a240d4d 1681 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
937bb610 1682 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
8fb033d7 1683
223a6fdf
PZ
1684 /* Workaround: set timing override bit. */
1685 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1686 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf
PZ
1687 I915_WRITE(_TRANSA_CHICKEN2, val);
1688
25f3ef11 1689 val = TRANS_ENABLE;
937bb610 1690 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
8fb033d7 1691
9a76b1c6
PZ
1692 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1693 PIPECONF_INTERLACED_ILK)
a35f2679 1694 val |= TRANS_INTERLACED;
8fb033d7
PZ
1695 else
1696 val |= TRANS_PROGRESSIVE;
1697
ab9412ba
DV
1698 I915_WRITE(LPT_TRANSCONF, val);
1699 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
937bb610 1700 DRM_ERROR("Failed to enable PCH transcoder\n");
8fb033d7
PZ
1701}
1702
b8a4f404
PZ
1703static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1704 enum pipe pipe)
040484af 1705{
23670b32
DV
1706 struct drm_device *dev = dev_priv->dev;
1707 uint32_t reg, val;
040484af
JB
1708
1709 /* FDI relies on the transcoder */
1710 assert_fdi_tx_disabled(dev_priv, pipe);
1711 assert_fdi_rx_disabled(dev_priv, pipe);
1712
291906f1
JB
1713 /* Ports must be off as well */
1714 assert_pch_ports_disabled(dev_priv, pipe);
1715
ab9412ba 1716 reg = PCH_TRANSCONF(pipe);
040484af
JB
1717 val = I915_READ(reg);
1718 val &= ~TRANS_ENABLE;
1719 I915_WRITE(reg, val);
1720 /* wait for PCH transcoder off, transcoder state */
1721 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
4bb6f1f3 1722 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
23670b32
DV
1723
1724 if (!HAS_PCH_IBX(dev)) {
1725 /* Workaround: Clear the timing override chicken bit again. */
1726 reg = TRANS_CHICKEN2(pipe);
1727 val = I915_READ(reg);
1728 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1729 I915_WRITE(reg, val);
1730 }
040484af
JB
1731}
1732
ab4d966c 1733static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
8fb033d7 1734{
8fb033d7
PZ
1735 u32 val;
1736
ab9412ba 1737 val = I915_READ(LPT_TRANSCONF);
8fb033d7 1738 val &= ~TRANS_ENABLE;
ab9412ba 1739 I915_WRITE(LPT_TRANSCONF, val);
8fb033d7 1740 /* wait for PCH transcoder off, transcoder state */
ab9412ba 1741 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
8a52fd9f 1742 DRM_ERROR("Failed to disable PCH transcoder\n");
223a6fdf
PZ
1743
1744 /* Workaround: clear timing override bit. */
1745 val = I915_READ(_TRANSA_CHICKEN2);
23670b32 1746 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
223a6fdf 1747 I915_WRITE(_TRANSA_CHICKEN2, val);
040484af
JB
1748}
1749
b24e7179 1750/**
309cfea8 1751 * intel_enable_pipe - enable a pipe, asserting requirements
0372264a 1752 * @crtc: crtc responsible for the pipe
b24e7179 1753 *
0372264a 1754 * Enable @crtc's pipe, making sure that various hardware specific requirements
b24e7179 1755 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
b24e7179 1756 */
e1fdc473 1757static void intel_enable_pipe(struct intel_crtc *crtc)
b24e7179 1758{
0372264a
PZ
1759 struct drm_device *dev = crtc->base.dev;
1760 struct drm_i915_private *dev_priv = dev->dev_private;
1761 enum pipe pipe = crtc->pipe;
702e7a56
PZ
1762 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1763 pipe);
1a240d4d 1764 enum pipe pch_transcoder;
b24e7179
JB
1765 int reg;
1766 u32 val;
1767
58c6eaa2 1768 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1769 assert_cursor_disabled(dev_priv, pipe);
58c6eaa2
DV
1770 assert_sprites_disabled(dev_priv, pipe);
1771
681e5811 1772 if (HAS_PCH_LPT(dev_priv->dev))
cc391bbb
PZ
1773 pch_transcoder = TRANSCODER_A;
1774 else
1775 pch_transcoder = pipe;
1776
b24e7179
JB
1777 /*
1778 * A pipe without a PLL won't actually be able to drive bits from
1779 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1780 * need the check.
1781 */
1782 if (!HAS_PCH_SPLIT(dev_priv->dev))
fbf3218a 1783 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
23538ef1
JN
1784 assert_dsi_pll_enabled(dev_priv);
1785 else
1786 assert_pll_enabled(dev_priv, pipe);
040484af 1787 else {
30421c4f 1788 if (crtc->config.has_pch_encoder) {
040484af 1789 /* if driving the PCH, we need FDI enabled */
cc391bbb 1790 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
1a240d4d
DV
1791 assert_fdi_tx_pll_enabled(dev_priv,
1792 (enum pipe) cpu_transcoder);
040484af
JB
1793 }
1794 /* FIXME: assert CPU port conditions for SNB+ */
1795 }
b24e7179 1796
702e7a56 1797 reg = PIPECONF(cpu_transcoder);
b24e7179 1798 val = I915_READ(reg);
7ad25d48
PZ
1799 if (val & PIPECONF_ENABLE) {
1800 WARN_ON(!(pipe == PIPE_A &&
1801 dev_priv->quirks & QUIRK_PIPEA_FORCE));
00d70b15 1802 return;
7ad25d48 1803 }
00d70b15
CW
1804
1805 I915_WRITE(reg, val | PIPECONF_ENABLE);
851855d8 1806 POSTING_READ(reg);
e1fdc473
PZ
1807
1808 /*
1809 * There's no guarantee the pipe will really start running now. It
1810 * depends on the Gen, the output type and the relative order between
1811 * pipe and plane enabling. Avoid waiting on HSW+ since it's not
1812 * necessary.
1813 * TODO: audit the previous gens.
1814 */
1815 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
851855d8 1816 intel_wait_for_vblank(dev_priv->dev, pipe);
b24e7179
JB
1817}
1818
1819/**
309cfea8 1820 * intel_disable_pipe - disable a pipe, asserting requirements
b24e7179
JB
1821 * @dev_priv: i915 private structure
1822 * @pipe: pipe to disable
1823 *
1824 * Disable @pipe, making sure that various hardware specific requirements
1825 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1826 *
1827 * @pipe should be %PIPE_A or %PIPE_B.
1828 *
1829 * Will wait until the pipe has shut down before returning.
1830 */
1831static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1832 enum pipe pipe)
1833{
702e7a56
PZ
1834 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1835 pipe);
b24e7179
JB
1836 int reg;
1837 u32 val;
1838
1839 /*
1840 * Make sure planes won't keep trying to pump pixels to us,
1841 * or we might hang the display.
1842 */
1843 assert_planes_disabled(dev_priv, pipe);
93ce0ba6 1844 assert_cursor_disabled(dev_priv, pipe);
19332d7a 1845 assert_sprites_disabled(dev_priv, pipe);
b24e7179
JB
1846
1847 /* Don't disable pipe A or pipe A PLLs if needed */
1848 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1849 return;
1850
702e7a56 1851 reg = PIPECONF(cpu_transcoder);
b24e7179 1852 val = I915_READ(reg);
00d70b15
CW
1853 if ((val & PIPECONF_ENABLE) == 0)
1854 return;
1855
1856 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
b24e7179
JB
1857 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1858}
1859
d74362c9
KP
1860/*
1861 * Plane regs are double buffered, going from enabled->disabled needs a
1862 * trigger in order to latch. The display address reg provides this.
1863 */
1dba99f4
VS
1864void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
1865 enum plane plane)
d74362c9 1866{
3d13ef2e
DL
1867 struct drm_device *dev = dev_priv->dev;
1868 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
1dba99f4
VS
1869
1870 I915_WRITE(reg, I915_READ(reg));
1871 POSTING_READ(reg);
d74362c9
KP
1872}
1873
b24e7179 1874/**
d1de00ef 1875 * intel_enable_primary_plane - enable the primary plane on a given pipe
b24e7179
JB
1876 * @dev_priv: i915 private structure
1877 * @plane: plane to enable
1878 * @pipe: pipe being fed
1879 *
1880 * Enable @plane on @pipe, making sure that @pipe is running first.
1881 */
d1de00ef
VS
1882static void intel_enable_primary_plane(struct drm_i915_private *dev_priv,
1883 enum plane plane, enum pipe pipe)
b24e7179 1884{
939c2fe8
VS
1885 struct intel_crtc *intel_crtc =
1886 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
b24e7179
JB
1887 int reg;
1888 u32 val;
1889
1890 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1891 assert_pipe_enabled(dev_priv, pipe);
1892
4c445e0e 1893 WARN(intel_crtc->primary_enabled, "Primary plane already enabled\n");
0037f71c 1894
4c445e0e 1895 intel_crtc->primary_enabled = true;
939c2fe8 1896
b24e7179
JB
1897 reg = DSPCNTR(plane);
1898 val = I915_READ(reg);
00d70b15
CW
1899 if (val & DISPLAY_PLANE_ENABLE)
1900 return;
1901
1902 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1dba99f4 1903 intel_flush_primary_plane(dev_priv, plane);
b24e7179
JB
1904 intel_wait_for_vblank(dev_priv->dev, pipe);
1905}
1906
b24e7179 1907/**
d1de00ef 1908 * intel_disable_primary_plane - disable the primary plane
b24e7179
JB
1909 * @dev_priv: i915 private structure
1910 * @plane: plane to disable
1911 * @pipe: pipe consuming the data
1912 *
1913 * Disable @plane; should be an independent operation.
1914 */
d1de00ef
VS
1915static void intel_disable_primary_plane(struct drm_i915_private *dev_priv,
1916 enum plane plane, enum pipe pipe)
b24e7179 1917{
939c2fe8
VS
1918 struct intel_crtc *intel_crtc =
1919 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
b24e7179
JB
1920 int reg;
1921 u32 val;
1922
4c445e0e 1923 WARN(!intel_crtc->primary_enabled, "Primary plane already disabled\n");
0037f71c 1924
4c445e0e 1925 intel_crtc->primary_enabled = false;
939c2fe8 1926
b24e7179
JB
1927 reg = DSPCNTR(plane);
1928 val = I915_READ(reg);
00d70b15
CW
1929 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1930 return;
1931
1932 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1dba99f4 1933 intel_flush_primary_plane(dev_priv, plane);
b24e7179
JB
1934 intel_wait_for_vblank(dev_priv->dev, pipe);
1935}
1936
693db184
CW
1937static bool need_vtd_wa(struct drm_device *dev)
1938{
1939#ifdef CONFIG_INTEL_IOMMU
1940 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
1941 return true;
1942#endif
1943 return false;
1944}
1945
a57ce0b2
JB
1946static int intel_align_height(struct drm_device *dev, int height, bool tiled)
1947{
1948 int tile_height;
1949
1950 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
1951 return ALIGN(height, tile_height);
1952}
1953
127bd2ac 1954int
48b956c5 1955intel_pin_and_fence_fb_obj(struct drm_device *dev,
05394f39 1956 struct drm_i915_gem_object *obj,
919926ae 1957 struct intel_ring_buffer *pipelined)
6b95a207 1958{
ce453d81 1959 struct drm_i915_private *dev_priv = dev->dev_private;
6b95a207
KH
1960 u32 alignment;
1961 int ret;
1962
05394f39 1963 switch (obj->tiling_mode) {
6b95a207 1964 case I915_TILING_NONE:
534843da
CW
1965 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1966 alignment = 128 * 1024;
a6c45cf0 1967 else if (INTEL_INFO(dev)->gen >= 4)
534843da
CW
1968 alignment = 4 * 1024;
1969 else
1970 alignment = 64 * 1024;
6b95a207
KH
1971 break;
1972 case I915_TILING_X:
1973 /* pin() will align the object as required by fence */
1974 alignment = 0;
1975 break;
1976 case I915_TILING_Y:
80075d49 1977 WARN(1, "Y tiled bo slipped through, driver bug!\n");
6b95a207
KH
1978 return -EINVAL;
1979 default:
1980 BUG();
1981 }
1982
693db184
CW
1983 /* Note that the w/a also requires 64 PTE of padding following the
1984 * bo. We currently fill all unused PTE with the shadow page and so
1985 * we should always have valid PTE following the scanout preventing
1986 * the VT-d warning.
1987 */
1988 if (need_vtd_wa(dev) && alignment < 256 * 1024)
1989 alignment = 256 * 1024;
1990
ce453d81 1991 dev_priv->mm.interruptible = false;
2da3b9b9 1992 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
48b956c5 1993 if (ret)
ce453d81 1994 goto err_interruptible;
6b95a207
KH
1995
1996 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1997 * fence, whereas 965+ only requires a fence if using
1998 * framebuffer compression. For simplicity, we always install
1999 * a fence as the cost is not that onerous.
2000 */
06d98131 2001 ret = i915_gem_object_get_fence(obj);
9a5a53b3
CW
2002 if (ret)
2003 goto err_unpin;
1690e1eb 2004
9a5a53b3 2005 i915_gem_object_pin_fence(obj);
6b95a207 2006
ce453d81 2007 dev_priv->mm.interruptible = true;
6b95a207 2008 return 0;
48b956c5
CW
2009
2010err_unpin:
cc98b413 2011 i915_gem_object_unpin_from_display_plane(obj);
ce453d81
CW
2012err_interruptible:
2013 dev_priv->mm.interruptible = true;
48b956c5 2014 return ret;
6b95a207
KH
2015}
2016
1690e1eb
CW
2017void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2018{
2019 i915_gem_object_unpin_fence(obj);
cc98b413 2020 i915_gem_object_unpin_from_display_plane(obj);
1690e1eb
CW
2021}
2022
c2c75131
DV
2023/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2024 * is assumed to be a power-of-two. */
bc752862
CW
2025unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2026 unsigned int tiling_mode,
2027 unsigned int cpp,
2028 unsigned int pitch)
c2c75131 2029{
bc752862
CW
2030 if (tiling_mode != I915_TILING_NONE) {
2031 unsigned int tile_rows, tiles;
c2c75131 2032
bc752862
CW
2033 tile_rows = *y / 8;
2034 *y %= 8;
c2c75131 2035
bc752862
CW
2036 tiles = *x / (512/cpp);
2037 *x %= 512/cpp;
2038
2039 return tile_rows * pitch * 8 + tiles * 4096;
2040 } else {
2041 unsigned int offset;
2042
2043 offset = *y * pitch + *x * cpp;
2044 *y = 0;
2045 *x = (offset & 4095) / cpp;
2046 return offset & -4096;
2047 }
c2c75131
DV
2048}
2049
46f297fb
JB
2050int intel_format_to_fourcc(int format)
2051{
2052 switch (format) {
2053 case DISPPLANE_8BPP:
2054 return DRM_FORMAT_C8;
2055 case DISPPLANE_BGRX555:
2056 return DRM_FORMAT_XRGB1555;
2057 case DISPPLANE_BGRX565:
2058 return DRM_FORMAT_RGB565;
2059 default:
2060 case DISPPLANE_BGRX888:
2061 return DRM_FORMAT_XRGB8888;
2062 case DISPPLANE_RGBX888:
2063 return DRM_FORMAT_XBGR8888;
2064 case DISPPLANE_BGRX101010:
2065 return DRM_FORMAT_XRGB2101010;
2066 case DISPPLANE_RGBX101010:
2067 return DRM_FORMAT_XBGR2101010;
2068 }
2069}
2070
484b41dd 2071static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
46f297fb
JB
2072 struct intel_plane_config *plane_config)
2073{
2074 struct drm_device *dev = crtc->base.dev;
2075 struct drm_i915_gem_object *obj = NULL;
2076 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2077 u32 base = plane_config->base;
2078
ff2652ea
CW
2079 if (plane_config->size == 0)
2080 return false;
2081
46f297fb
JB
2082 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2083 plane_config->size);
2084 if (!obj)
484b41dd 2085 return false;
46f297fb
JB
2086
2087 if (plane_config->tiled) {
2088 obj->tiling_mode = I915_TILING_X;
484b41dd 2089 obj->stride = crtc->base.fb->pitches[0];
46f297fb
JB
2090 }
2091
484b41dd
JB
2092 mode_cmd.pixel_format = crtc->base.fb->pixel_format;
2093 mode_cmd.width = crtc->base.fb->width;
2094 mode_cmd.height = crtc->base.fb->height;
2095 mode_cmd.pitches[0] = crtc->base.fb->pitches[0];
46f297fb
JB
2096
2097 mutex_lock(&dev->struct_mutex);
2098
484b41dd
JB
2099 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.fb),
2100 &mode_cmd, obj)) {
46f297fb
JB
2101 DRM_DEBUG_KMS("intel fb init failed\n");
2102 goto out_unref_obj;
2103 }
2104
2105 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2106
2107 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2108 return true;
46f297fb
JB
2109
2110out_unref_obj:
2111 drm_gem_object_unreference(&obj->base);
2112 mutex_unlock(&dev->struct_mutex);
484b41dd
JB
2113 return false;
2114}
2115
2116static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2117 struct intel_plane_config *plane_config)
2118{
2119 struct drm_device *dev = intel_crtc->base.dev;
2120 struct drm_crtc *c;
2121 struct intel_crtc *i;
2122 struct intel_framebuffer *fb;
2123
2124 if (!intel_crtc->base.fb)
2125 return;
2126
2127 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2128 return;
2129
2130 kfree(intel_crtc->base.fb);
d1a59868 2131 intel_crtc->base.fb = NULL;
484b41dd
JB
2132
2133 /*
2134 * Failed to alloc the obj, check to see if we should share
2135 * an fb with another CRTC instead
2136 */
2137 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
2138 i = to_intel_crtc(c);
2139
2140 if (c == &intel_crtc->base)
2141 continue;
2142
2143 if (!i->active || !c->fb)
2144 continue;
2145
2146 fb = to_intel_framebuffer(c->fb);
2147 if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
2148 drm_framebuffer_reference(c->fb);
2149 intel_crtc->base.fb = c->fb;
2150 break;
2151 }
2152 }
46f297fb
JB
2153}
2154
17638cd6
JB
2155static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2156 int x, int y)
81255565
JB
2157{
2158 struct drm_device *dev = crtc->dev;
2159 struct drm_i915_private *dev_priv = dev->dev_private;
2160 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2161 struct intel_framebuffer *intel_fb;
05394f39 2162 struct drm_i915_gem_object *obj;
81255565 2163 int plane = intel_crtc->plane;
e506a0c6 2164 unsigned long linear_offset;
81255565 2165 u32 dspcntr;
5eddb70b 2166 u32 reg;
81255565
JB
2167
2168 switch (plane) {
2169 case 0:
2170 case 1:
2171 break;
2172 default:
84f44ce7 2173 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
81255565
JB
2174 return -EINVAL;
2175 }
2176
2177 intel_fb = to_intel_framebuffer(fb);
2178 obj = intel_fb->obj;
81255565 2179
5eddb70b
CW
2180 reg = DSPCNTR(plane);
2181 dspcntr = I915_READ(reg);
81255565
JB
2182 /* Mask out pixel format bits in case we change it */
2183 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
57779d06
VS
2184 switch (fb->pixel_format) {
2185 case DRM_FORMAT_C8:
81255565
JB
2186 dspcntr |= DISPPLANE_8BPP;
2187 break;
57779d06
VS
2188 case DRM_FORMAT_XRGB1555:
2189 case DRM_FORMAT_ARGB1555:
2190 dspcntr |= DISPPLANE_BGRX555;
81255565 2191 break;
57779d06
VS
2192 case DRM_FORMAT_RGB565:
2193 dspcntr |= DISPPLANE_BGRX565;
2194 break;
2195 case DRM_FORMAT_XRGB8888:
2196 case DRM_FORMAT_ARGB8888:
2197 dspcntr |= DISPPLANE_BGRX888;
2198 break;
2199 case DRM_FORMAT_XBGR8888:
2200 case DRM_FORMAT_ABGR8888:
2201 dspcntr |= DISPPLANE_RGBX888;
2202 break;
2203 case DRM_FORMAT_XRGB2101010:
2204 case DRM_FORMAT_ARGB2101010:
2205 dspcntr |= DISPPLANE_BGRX101010;
2206 break;
2207 case DRM_FORMAT_XBGR2101010:
2208 case DRM_FORMAT_ABGR2101010:
2209 dspcntr |= DISPPLANE_RGBX101010;
81255565
JB
2210 break;
2211 default:
baba133a 2212 BUG();
81255565 2213 }
57779d06 2214
a6c45cf0 2215 if (INTEL_INFO(dev)->gen >= 4) {
05394f39 2216 if (obj->tiling_mode != I915_TILING_NONE)
81255565
JB
2217 dspcntr |= DISPPLANE_TILED;
2218 else
2219 dspcntr &= ~DISPPLANE_TILED;
2220 }
2221
de1aa629
VS
2222 if (IS_G4X(dev))
2223 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2224
5eddb70b 2225 I915_WRITE(reg, dspcntr);
81255565 2226
e506a0c6 2227 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
81255565 2228
c2c75131
DV
2229 if (INTEL_INFO(dev)->gen >= 4) {
2230 intel_crtc->dspaddr_offset =
bc752862
CW
2231 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2232 fb->bits_per_pixel / 8,
2233 fb->pitches[0]);
c2c75131
DV
2234 linear_offset -= intel_crtc->dspaddr_offset;
2235 } else {
e506a0c6 2236 intel_crtc->dspaddr_offset = linear_offset;
c2c75131 2237 }
e506a0c6 2238
f343c5f6
BW
2239 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2240 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2241 fb->pitches[0]);
01f2c773 2242 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
a6c45cf0 2243 if (INTEL_INFO(dev)->gen >= 4) {
85ba7b7d
DV
2244 I915_WRITE(DSPSURF(plane),
2245 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
5eddb70b 2246 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
e506a0c6 2247 I915_WRITE(DSPLINOFF(plane), linear_offset);
5eddb70b 2248 } else
f343c5f6 2249 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
5eddb70b 2250 POSTING_READ(reg);
81255565 2251
17638cd6
JB
2252 return 0;
2253}
2254
2255static int ironlake_update_plane(struct drm_crtc *crtc,
2256 struct drm_framebuffer *fb, int x, int y)
2257{
2258 struct drm_device *dev = crtc->dev;
2259 struct drm_i915_private *dev_priv = dev->dev_private;
2260 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2261 struct intel_framebuffer *intel_fb;
2262 struct drm_i915_gem_object *obj;
2263 int plane = intel_crtc->plane;
e506a0c6 2264 unsigned long linear_offset;
17638cd6
JB
2265 u32 dspcntr;
2266 u32 reg;
2267
2268 switch (plane) {
2269 case 0:
2270 case 1:
27f8227b 2271 case 2:
17638cd6
JB
2272 break;
2273 default:
84f44ce7 2274 DRM_ERROR("Can't update plane %c in SAREA\n", plane_name(plane));
17638cd6
JB
2275 return -EINVAL;
2276 }
2277
2278 intel_fb = to_intel_framebuffer(fb);
2279 obj = intel_fb->obj;
2280
2281 reg = DSPCNTR(plane);
2282 dspcntr = I915_READ(reg);
2283 /* Mask out pixel format bits in case we change it */
2284 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
57779d06
VS
2285 switch (fb->pixel_format) {
2286 case DRM_FORMAT_C8:
17638cd6
JB
2287 dspcntr |= DISPPLANE_8BPP;
2288 break;
57779d06
VS
2289 case DRM_FORMAT_RGB565:
2290 dspcntr |= DISPPLANE_BGRX565;
17638cd6 2291 break;
57779d06
VS
2292 case DRM_FORMAT_XRGB8888:
2293 case DRM_FORMAT_ARGB8888:
2294 dspcntr |= DISPPLANE_BGRX888;
2295 break;
2296 case DRM_FORMAT_XBGR8888:
2297 case DRM_FORMAT_ABGR8888:
2298 dspcntr |= DISPPLANE_RGBX888;
2299 break;
2300 case DRM_FORMAT_XRGB2101010:
2301 case DRM_FORMAT_ARGB2101010:
2302 dspcntr |= DISPPLANE_BGRX101010;
2303 break;
2304 case DRM_FORMAT_XBGR2101010:
2305 case DRM_FORMAT_ABGR2101010:
2306 dspcntr |= DISPPLANE_RGBX101010;
17638cd6
JB
2307 break;
2308 default:
baba133a 2309 BUG();
17638cd6
JB
2310 }
2311
2312 if (obj->tiling_mode != I915_TILING_NONE)
2313 dspcntr |= DISPPLANE_TILED;
2314 else
2315 dspcntr &= ~DISPPLANE_TILED;
2316
b42c6009 2317 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
1f5d76db
PZ
2318 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2319 else
2320 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
17638cd6
JB
2321
2322 I915_WRITE(reg, dspcntr);
2323
e506a0c6 2324 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
c2c75131 2325 intel_crtc->dspaddr_offset =
bc752862
CW
2326 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2327 fb->bits_per_pixel / 8,
2328 fb->pitches[0]);
c2c75131 2329 linear_offset -= intel_crtc->dspaddr_offset;
17638cd6 2330
f343c5f6
BW
2331 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2332 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2333 fb->pitches[0]);
01f2c773 2334 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
85ba7b7d
DV
2335 I915_WRITE(DSPSURF(plane),
2336 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
b3dc685e 2337 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
bc1c91eb
DL
2338 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2339 } else {
2340 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2341 I915_WRITE(DSPLINOFF(plane), linear_offset);
2342 }
17638cd6
JB
2343 POSTING_READ(reg);
2344
2345 return 0;
2346}
2347
2348/* Assume fb object is pinned & idle & fenced and just update base pointers */
2349static int
2350intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2351 int x, int y, enum mode_set_atomic state)
2352{
2353 struct drm_device *dev = crtc->dev;
2354 struct drm_i915_private *dev_priv = dev->dev_private;
17638cd6 2355
6b8e6ed0
CW
2356 if (dev_priv->display.disable_fbc)
2357 dev_priv->display.disable_fbc(dev);
3dec0095 2358 intel_increase_pllclock(crtc);
81255565 2359
6b8e6ed0 2360 return dev_priv->display.update_plane(crtc, fb, x, y);
81255565
JB
2361}
2362
96a02917
VS
2363void intel_display_handle_reset(struct drm_device *dev)
2364{
2365 struct drm_i915_private *dev_priv = dev->dev_private;
2366 struct drm_crtc *crtc;
2367
2368 /*
2369 * Flips in the rings have been nuked by the reset,
2370 * so complete all pending flips so that user space
2371 * will get its events and not get stuck.
2372 *
2373 * Also update the base address of all primary
2374 * planes to the the last fb to make sure we're
2375 * showing the correct fb after a reset.
2376 *
2377 * Need to make two loops over the crtcs so that we
2378 * don't try to grab a crtc mutex before the
2379 * pending_flip_queue really got woken up.
2380 */
2381
2382 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2383 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2384 enum plane plane = intel_crtc->plane;
2385
2386 intel_prepare_page_flip(dev, plane);
2387 intel_finish_page_flip_plane(dev, plane);
2388 }
2389
2390 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2391 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2392
2393 mutex_lock(&crtc->mutex);
947fdaad
CW
2394 /*
2395 * FIXME: Once we have proper support for primary planes (and
2396 * disabling them without disabling the entire crtc) allow again
2397 * a NULL crtc->fb.
2398 */
2399 if (intel_crtc->active && crtc->fb)
96a02917
VS
2400 dev_priv->display.update_plane(crtc, crtc->fb,
2401 crtc->x, crtc->y);
2402 mutex_unlock(&crtc->mutex);
2403 }
2404}
2405
14667a4b
CW
2406static int
2407intel_finish_fb(struct drm_framebuffer *old_fb)
2408{
2409 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2410 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2411 bool was_interruptible = dev_priv->mm.interruptible;
2412 int ret;
2413
14667a4b
CW
2414 /* Big Hammer, we also need to ensure that any pending
2415 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2416 * current scanout is retired before unpinning the old
2417 * framebuffer.
2418 *
2419 * This should only fail upon a hung GPU, in which case we
2420 * can safely continue.
2421 */
2422 dev_priv->mm.interruptible = false;
2423 ret = i915_gem_object_finish_gpu(obj);
2424 dev_priv->mm.interruptible = was_interruptible;
2425
2426 return ret;
2427}
2428
7d5e3799
CW
2429static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2430{
2431 struct drm_device *dev = crtc->dev;
2432 struct drm_i915_private *dev_priv = dev->dev_private;
2433 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2434 unsigned long flags;
2435 bool pending;
2436
2437 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2438 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2439 return false;
2440
2441 spin_lock_irqsave(&dev->event_lock, flags);
2442 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2443 spin_unlock_irqrestore(&dev->event_lock, flags);
2444
2445 return pending;
2446}
2447
5c3b82e2 2448static int
3c4fdcfb 2449intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
94352cf9 2450 struct drm_framebuffer *fb)
79e53945
JB
2451{
2452 struct drm_device *dev = crtc->dev;
6b8e6ed0 2453 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 2454 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
94352cf9 2455 struct drm_framebuffer *old_fb;
5c3b82e2 2456 int ret;
79e53945 2457
7d5e3799
CW
2458 if (intel_crtc_has_pending_flip(crtc)) {
2459 DRM_ERROR("pipe is still busy with an old pageflip\n");
2460 return -EBUSY;
2461 }
2462
79e53945 2463 /* no fb bound */
94352cf9 2464 if (!fb) {
a5071c2f 2465 DRM_ERROR("No FB bound\n");
5c3b82e2
CW
2466 return 0;
2467 }
2468
7eb552ae 2469 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
84f44ce7
VS
2470 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2471 plane_name(intel_crtc->plane),
2472 INTEL_INFO(dev)->num_pipes);
5c3b82e2 2473 return -EINVAL;
79e53945
JB
2474 }
2475
5c3b82e2 2476 mutex_lock(&dev->struct_mutex);
265db958 2477 ret = intel_pin_and_fence_fb_obj(dev,
94352cf9 2478 to_intel_framebuffer(fb)->obj,
919926ae 2479 NULL);
8ac36ec1 2480 mutex_unlock(&dev->struct_mutex);
5c3b82e2 2481 if (ret != 0) {
a5071c2f 2482 DRM_ERROR("pin & fence failed\n");
5c3b82e2
CW
2483 return ret;
2484 }
79e53945 2485
bb2043de
DL
2486 /*
2487 * Update pipe size and adjust fitter if needed: the reason for this is
2488 * that in compute_mode_changes we check the native mode (not the pfit
2489 * mode) to see if we can flip rather than do a full mode set. In the
2490 * fastboot case, we'll flip, but if we don't update the pipesrc and
2491 * pfit state, we'll end up with a big fb scanned out into the wrong
2492 * sized surface.
2493 *
2494 * To fix this properly, we need to hoist the checks up into
2495 * compute_mode_changes (or above), check the actual pfit state and
2496 * whether the platform allows pfit disable with pipe active, and only
2497 * then update the pipesrc and pfit state, even on the flip path.
2498 */
d330a953 2499 if (i915.fastboot) {
d7bf63f2
DL
2500 const struct drm_display_mode *adjusted_mode =
2501 &intel_crtc->config.adjusted_mode;
2502
4d6a3e63 2503 I915_WRITE(PIPESRC(intel_crtc->pipe),
d7bf63f2
DL
2504 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2505 (adjusted_mode->crtc_vdisplay - 1));
fd4daa9c 2506 if (!intel_crtc->config.pch_pfit.enabled &&
4d6a3e63
JB
2507 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2508 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2509 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2510 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2511 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2512 }
0637d60d
JB
2513 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2514 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
4d6a3e63
JB
2515 }
2516
94352cf9 2517 ret = dev_priv->display.update_plane(crtc, fb, x, y);
4e6cfefc 2518 if (ret) {
8ac36ec1 2519 mutex_lock(&dev->struct_mutex);
94352cf9 2520 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
5c3b82e2 2521 mutex_unlock(&dev->struct_mutex);
a5071c2f 2522 DRM_ERROR("failed to update base address\n");
4e6cfefc 2523 return ret;
79e53945 2524 }
3c4fdcfb 2525
94352cf9
DV
2526 old_fb = crtc->fb;
2527 crtc->fb = fb;
6c4c86f5
DV
2528 crtc->x = x;
2529 crtc->y = y;
94352cf9 2530
b7f1de28 2531 if (old_fb) {
d7697eea
DV
2532 if (intel_crtc->active && old_fb != fb)
2533 intel_wait_for_vblank(dev, intel_crtc->pipe);
8ac36ec1 2534 mutex_lock(&dev->struct_mutex);
1690e1eb 2535 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
8ac36ec1 2536 mutex_unlock(&dev->struct_mutex);
b7f1de28 2537 }
652c393a 2538
8ac36ec1 2539 mutex_lock(&dev->struct_mutex);
6b8e6ed0 2540 intel_update_fbc(dev);
4906557e 2541 intel_edp_psr_update(dev);
5c3b82e2 2542 mutex_unlock(&dev->struct_mutex);
79e53945 2543
5c3b82e2 2544 return 0;
79e53945
JB
2545}
2546
5e84e1a4
ZW
2547static void intel_fdi_normal_train(struct drm_crtc *crtc)
2548{
2549 struct drm_device *dev = crtc->dev;
2550 struct drm_i915_private *dev_priv = dev->dev_private;
2551 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2552 int pipe = intel_crtc->pipe;
2553 u32 reg, temp;
2554
2555 /* enable normal train */
2556 reg = FDI_TX_CTL(pipe);
2557 temp = I915_READ(reg);
61e499bf 2558 if (IS_IVYBRIDGE(dev)) {
357555c0
JB
2559 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2560 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
61e499bf
KP
2561 } else {
2562 temp &= ~FDI_LINK_TRAIN_NONE;
2563 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
357555c0 2564 }
5e84e1a4
ZW
2565 I915_WRITE(reg, temp);
2566
2567 reg = FDI_RX_CTL(pipe);
2568 temp = I915_READ(reg);
2569 if (HAS_PCH_CPT(dev)) {
2570 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2571 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2572 } else {
2573 temp &= ~FDI_LINK_TRAIN_NONE;
2574 temp |= FDI_LINK_TRAIN_NONE;
2575 }
2576 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2577
2578 /* wait one idle pattern time */
2579 POSTING_READ(reg);
2580 udelay(1000);
357555c0
JB
2581
2582 /* IVB wants error correction enabled */
2583 if (IS_IVYBRIDGE(dev))
2584 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2585 FDI_FE_ERRC_ENABLE);
5e84e1a4
ZW
2586}
2587
1fbc0d78 2588static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
1e833f40 2589{
1fbc0d78
DV
2590 return crtc->base.enabled && crtc->active &&
2591 crtc->config.has_pch_encoder;
1e833f40
DV
2592}
2593
01a415fd
DV
2594static void ivb_modeset_global_resources(struct drm_device *dev)
2595{
2596 struct drm_i915_private *dev_priv = dev->dev_private;
2597 struct intel_crtc *pipe_B_crtc =
2598 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2599 struct intel_crtc *pipe_C_crtc =
2600 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2601 uint32_t temp;
2602
1e833f40
DV
2603 /*
2604 * When everything is off disable fdi C so that we could enable fdi B
2605 * with all lanes. Note that we don't care about enabled pipes without
2606 * an enabled pch encoder.
2607 */
2608 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2609 !pipe_has_enabled_pch(pipe_C_crtc)) {
01a415fd
DV
2610 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2611 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2612
2613 temp = I915_READ(SOUTH_CHICKEN1);
2614 temp &= ~FDI_BC_BIFURCATION_SELECT;
2615 DRM_DEBUG_KMS("disabling fdi C rx\n");
2616 I915_WRITE(SOUTH_CHICKEN1, temp);
2617 }
2618}
2619
8db9d77b
ZW
2620/* The FDI link training functions for ILK/Ibexpeak. */
2621static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2622{
2623 struct drm_device *dev = crtc->dev;
2624 struct drm_i915_private *dev_priv = dev->dev_private;
2625 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2626 int pipe = intel_crtc->pipe;
0fc932b8 2627 int plane = intel_crtc->plane;
5eddb70b 2628 u32 reg, temp, tries;
8db9d77b 2629
0fc932b8
JB
2630 /* FDI needs bits from pipe & plane first */
2631 assert_pipe_enabled(dev_priv, pipe);
2632 assert_plane_enabled(dev_priv, plane);
2633
e1a44743
AJ
2634 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2635 for train result */
5eddb70b
CW
2636 reg = FDI_RX_IMR(pipe);
2637 temp = I915_READ(reg);
e1a44743
AJ
2638 temp &= ~FDI_RX_SYMBOL_LOCK;
2639 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
2640 I915_WRITE(reg, temp);
2641 I915_READ(reg);
e1a44743
AJ
2642 udelay(150);
2643
8db9d77b 2644 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
2645 reg = FDI_TX_CTL(pipe);
2646 temp = I915_READ(reg);
627eb5a3
DV
2647 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2648 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
2649 temp &= ~FDI_LINK_TRAIN_NONE;
2650 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b 2651 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 2652
5eddb70b
CW
2653 reg = FDI_RX_CTL(pipe);
2654 temp = I915_READ(reg);
8db9d77b
ZW
2655 temp &= ~FDI_LINK_TRAIN_NONE;
2656 temp |= FDI_LINK_TRAIN_PATTERN_1;
5eddb70b
CW
2657 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2658
2659 POSTING_READ(reg);
8db9d77b
ZW
2660 udelay(150);
2661
5b2adf89 2662 /* Ironlake workaround, enable clock pointer after FDI enable*/
8f5718a6
DV
2663 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2664 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2665 FDI_RX_PHASE_SYNC_POINTER_EN);
5b2adf89 2666
5eddb70b 2667 reg = FDI_RX_IIR(pipe);
e1a44743 2668 for (tries = 0; tries < 5; tries++) {
5eddb70b 2669 temp = I915_READ(reg);
8db9d77b
ZW
2670 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2671
2672 if ((temp & FDI_RX_BIT_LOCK)) {
2673 DRM_DEBUG_KMS("FDI train 1 done.\n");
5eddb70b 2674 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
8db9d77b
ZW
2675 break;
2676 }
8db9d77b 2677 }
e1a44743 2678 if (tries == 5)
5eddb70b 2679 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
2680
2681 /* Train 2 */
5eddb70b
CW
2682 reg = FDI_TX_CTL(pipe);
2683 temp = I915_READ(reg);
8db9d77b
ZW
2684 temp &= ~FDI_LINK_TRAIN_NONE;
2685 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2686 I915_WRITE(reg, temp);
8db9d77b 2687
5eddb70b
CW
2688 reg = FDI_RX_CTL(pipe);
2689 temp = I915_READ(reg);
8db9d77b
ZW
2690 temp &= ~FDI_LINK_TRAIN_NONE;
2691 temp |= FDI_LINK_TRAIN_PATTERN_2;
5eddb70b 2692 I915_WRITE(reg, temp);
8db9d77b 2693
5eddb70b
CW
2694 POSTING_READ(reg);
2695 udelay(150);
8db9d77b 2696
5eddb70b 2697 reg = FDI_RX_IIR(pipe);
e1a44743 2698 for (tries = 0; tries < 5; tries++) {
5eddb70b 2699 temp = I915_READ(reg);
8db9d77b
ZW
2700 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2701
2702 if (temp & FDI_RX_SYMBOL_LOCK) {
5eddb70b 2703 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
8db9d77b
ZW
2704 DRM_DEBUG_KMS("FDI train 2 done.\n");
2705 break;
2706 }
8db9d77b 2707 }
e1a44743 2708 if (tries == 5)
5eddb70b 2709 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
2710
2711 DRM_DEBUG_KMS("FDI train done\n");
5c5313c8 2712
8db9d77b
ZW
2713}
2714
0206e353 2715static const int snb_b_fdi_train_param[] = {
8db9d77b
ZW
2716 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2717 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2718 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2719 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2720};
2721
2722/* The FDI link training functions for SNB/Cougarpoint. */
2723static void gen6_fdi_link_train(struct drm_crtc *crtc)
2724{
2725 struct drm_device *dev = crtc->dev;
2726 struct drm_i915_private *dev_priv = dev->dev_private;
2727 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2728 int pipe = intel_crtc->pipe;
fa37d39e 2729 u32 reg, temp, i, retry;
8db9d77b 2730
e1a44743
AJ
2731 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2732 for train result */
5eddb70b
CW
2733 reg = FDI_RX_IMR(pipe);
2734 temp = I915_READ(reg);
e1a44743
AJ
2735 temp &= ~FDI_RX_SYMBOL_LOCK;
2736 temp &= ~FDI_RX_BIT_LOCK;
5eddb70b
CW
2737 I915_WRITE(reg, temp);
2738
2739 POSTING_READ(reg);
e1a44743
AJ
2740 udelay(150);
2741
8db9d77b 2742 /* enable CPU FDI TX and PCH FDI RX */
5eddb70b
CW
2743 reg = FDI_TX_CTL(pipe);
2744 temp = I915_READ(reg);
627eb5a3
DV
2745 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2746 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
8db9d77b
ZW
2747 temp &= ~FDI_LINK_TRAIN_NONE;
2748 temp |= FDI_LINK_TRAIN_PATTERN_1;
2749 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2750 /* SNB-B */
2751 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
5eddb70b 2752 I915_WRITE(reg, temp | FDI_TX_ENABLE);
8db9d77b 2753
d74cf324
DV
2754 I915_WRITE(FDI_RX_MISC(pipe),
2755 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2756
5eddb70b
CW
2757 reg = FDI_RX_CTL(pipe);
2758 temp = I915_READ(reg);
8db9d77b
ZW
2759 if (HAS_PCH_CPT(dev)) {
2760 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2761 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2762 } else {
2763 temp &= ~FDI_LINK_TRAIN_NONE;
2764 temp |= FDI_LINK_TRAIN_PATTERN_1;
2765 }
5eddb70b
CW
2766 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2767
2768 POSTING_READ(reg);
8db9d77b
ZW
2769 udelay(150);
2770
0206e353 2771 for (i = 0; i < 4; i++) {
5eddb70b
CW
2772 reg = FDI_TX_CTL(pipe);
2773 temp = I915_READ(reg);
8db9d77b
ZW
2774 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2775 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
2776 I915_WRITE(reg, temp);
2777
2778 POSTING_READ(reg);
8db9d77b
ZW
2779 udelay(500);
2780
fa37d39e
SP
2781 for (retry = 0; retry < 5; retry++) {
2782 reg = FDI_RX_IIR(pipe);
2783 temp = I915_READ(reg);
2784 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2785 if (temp & FDI_RX_BIT_LOCK) {
2786 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2787 DRM_DEBUG_KMS("FDI train 1 done.\n");
2788 break;
2789 }
2790 udelay(50);
8db9d77b 2791 }
fa37d39e
SP
2792 if (retry < 5)
2793 break;
8db9d77b
ZW
2794 }
2795 if (i == 4)
5eddb70b 2796 DRM_ERROR("FDI train 1 fail!\n");
8db9d77b
ZW
2797
2798 /* Train 2 */
5eddb70b
CW
2799 reg = FDI_TX_CTL(pipe);
2800 temp = I915_READ(reg);
8db9d77b
ZW
2801 temp &= ~FDI_LINK_TRAIN_NONE;
2802 temp |= FDI_LINK_TRAIN_PATTERN_2;
2803 if (IS_GEN6(dev)) {
2804 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2805 /* SNB-B */
2806 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2807 }
5eddb70b 2808 I915_WRITE(reg, temp);
8db9d77b 2809
5eddb70b
CW
2810 reg = FDI_RX_CTL(pipe);
2811 temp = I915_READ(reg);
8db9d77b
ZW
2812 if (HAS_PCH_CPT(dev)) {
2813 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2814 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2815 } else {
2816 temp &= ~FDI_LINK_TRAIN_NONE;
2817 temp |= FDI_LINK_TRAIN_PATTERN_2;
2818 }
5eddb70b
CW
2819 I915_WRITE(reg, temp);
2820
2821 POSTING_READ(reg);
8db9d77b
ZW
2822 udelay(150);
2823
0206e353 2824 for (i = 0; i < 4; i++) {
5eddb70b
CW
2825 reg = FDI_TX_CTL(pipe);
2826 temp = I915_READ(reg);
8db9d77b
ZW
2827 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2828 temp |= snb_b_fdi_train_param[i];
5eddb70b
CW
2829 I915_WRITE(reg, temp);
2830
2831 POSTING_READ(reg);
8db9d77b
ZW
2832 udelay(500);
2833
fa37d39e
SP
2834 for (retry = 0; retry < 5; retry++) {
2835 reg = FDI_RX_IIR(pipe);
2836 temp = I915_READ(reg);
2837 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2838 if (temp & FDI_RX_SYMBOL_LOCK) {
2839 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2840 DRM_DEBUG_KMS("FDI train 2 done.\n");
2841 break;
2842 }
2843 udelay(50);
8db9d77b 2844 }
fa37d39e
SP
2845 if (retry < 5)
2846 break;
8db9d77b
ZW
2847 }
2848 if (i == 4)
5eddb70b 2849 DRM_ERROR("FDI train 2 fail!\n");
8db9d77b
ZW
2850
2851 DRM_DEBUG_KMS("FDI train done.\n");
2852}
2853
357555c0
JB
2854/* Manual link training for Ivy Bridge A0 parts */
2855static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2856{
2857 struct drm_device *dev = crtc->dev;
2858 struct drm_i915_private *dev_priv = dev->dev_private;
2859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2860 int pipe = intel_crtc->pipe;
139ccd3f 2861 u32 reg, temp, i, j;
357555c0
JB
2862
2863 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2864 for train result */
2865 reg = FDI_RX_IMR(pipe);
2866 temp = I915_READ(reg);
2867 temp &= ~FDI_RX_SYMBOL_LOCK;
2868 temp &= ~FDI_RX_BIT_LOCK;
2869 I915_WRITE(reg, temp);
2870
2871 POSTING_READ(reg);
2872 udelay(150);
2873
01a415fd
DV
2874 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2875 I915_READ(FDI_RX_IIR(pipe)));
2876
139ccd3f
JB
2877 /* Try each vswing and preemphasis setting twice before moving on */
2878 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
2879 /* disable first in case we need to retry */
2880 reg = FDI_TX_CTL(pipe);
2881 temp = I915_READ(reg);
2882 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2883 temp &= ~FDI_TX_ENABLE;
2884 I915_WRITE(reg, temp);
357555c0 2885
139ccd3f
JB
2886 reg = FDI_RX_CTL(pipe);
2887 temp = I915_READ(reg);
2888 temp &= ~FDI_LINK_TRAIN_AUTO;
2889 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2890 temp &= ~FDI_RX_ENABLE;
2891 I915_WRITE(reg, temp);
357555c0 2892
139ccd3f 2893 /* enable CPU FDI TX and PCH FDI RX */
357555c0
JB
2894 reg = FDI_TX_CTL(pipe);
2895 temp = I915_READ(reg);
139ccd3f
JB
2896 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2897 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
2898 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
357555c0 2899 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
139ccd3f
JB
2900 temp |= snb_b_fdi_train_param[j/2];
2901 temp |= FDI_COMPOSITE_SYNC;
2902 I915_WRITE(reg, temp | FDI_TX_ENABLE);
357555c0 2903
139ccd3f
JB
2904 I915_WRITE(FDI_RX_MISC(pipe),
2905 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
357555c0 2906
139ccd3f 2907 reg = FDI_RX_CTL(pipe);
357555c0 2908 temp = I915_READ(reg);
139ccd3f
JB
2909 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2910 temp |= FDI_COMPOSITE_SYNC;
2911 I915_WRITE(reg, temp | FDI_RX_ENABLE);
357555c0 2912
139ccd3f
JB
2913 POSTING_READ(reg);
2914 udelay(1); /* should be 0.5us */
357555c0 2915
139ccd3f
JB
2916 for (i = 0; i < 4; i++) {
2917 reg = FDI_RX_IIR(pipe);
2918 temp = I915_READ(reg);
2919 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 2920
139ccd3f
JB
2921 if (temp & FDI_RX_BIT_LOCK ||
2922 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2923 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2924 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
2925 i);
2926 break;
2927 }
2928 udelay(1); /* should be 0.5us */
2929 }
2930 if (i == 4) {
2931 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
2932 continue;
2933 }
357555c0 2934
139ccd3f 2935 /* Train 2 */
357555c0
JB
2936 reg = FDI_TX_CTL(pipe);
2937 temp = I915_READ(reg);
139ccd3f
JB
2938 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2939 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2940 I915_WRITE(reg, temp);
2941
2942 reg = FDI_RX_CTL(pipe);
2943 temp = I915_READ(reg);
2944 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2945 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
357555c0
JB
2946 I915_WRITE(reg, temp);
2947
2948 POSTING_READ(reg);
139ccd3f 2949 udelay(2); /* should be 1.5us */
357555c0 2950
139ccd3f
JB
2951 for (i = 0; i < 4; i++) {
2952 reg = FDI_RX_IIR(pipe);
2953 temp = I915_READ(reg);
2954 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
357555c0 2955
139ccd3f
JB
2956 if (temp & FDI_RX_SYMBOL_LOCK ||
2957 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
2958 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2959 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
2960 i);
2961 goto train_done;
2962 }
2963 udelay(2); /* should be 1.5us */
357555c0 2964 }
139ccd3f
JB
2965 if (i == 4)
2966 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
357555c0 2967 }
357555c0 2968
139ccd3f 2969train_done:
357555c0
JB
2970 DRM_DEBUG_KMS("FDI train done.\n");
2971}
2972
88cefb6c 2973static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
2c07245f 2974{
88cefb6c 2975 struct drm_device *dev = intel_crtc->base.dev;
2c07245f 2976 struct drm_i915_private *dev_priv = dev->dev_private;
2c07245f 2977 int pipe = intel_crtc->pipe;
5eddb70b 2978 u32 reg, temp;
79e53945 2979
c64e311e 2980
c98e9dcf 2981 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
5eddb70b
CW
2982 reg = FDI_RX_CTL(pipe);
2983 temp = I915_READ(reg);
627eb5a3
DV
2984 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
2985 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
dfd07d72 2986 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
5eddb70b
CW
2987 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2988
2989 POSTING_READ(reg);
c98e9dcf
JB
2990 udelay(200);
2991
2992 /* Switch from Rawclk to PCDclk */
5eddb70b
CW
2993 temp = I915_READ(reg);
2994 I915_WRITE(reg, temp | FDI_PCDCLK);
2995
2996 POSTING_READ(reg);
c98e9dcf
JB
2997 udelay(200);
2998
20749730
PZ
2999 /* Enable CPU FDI TX PLL, always on for Ironlake */
3000 reg = FDI_TX_CTL(pipe);
3001 temp = I915_READ(reg);
3002 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3003 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
5eddb70b 3004
20749730
PZ
3005 POSTING_READ(reg);
3006 udelay(100);
6be4a607 3007 }
0e23b99d
JB
3008}
3009
88cefb6c
DV
3010static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3011{
3012 struct drm_device *dev = intel_crtc->base.dev;
3013 struct drm_i915_private *dev_priv = dev->dev_private;
3014 int pipe = intel_crtc->pipe;
3015 u32 reg, temp;
3016
3017 /* Switch from PCDclk to Rawclk */
3018 reg = FDI_RX_CTL(pipe);
3019 temp = I915_READ(reg);
3020 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3021
3022 /* Disable CPU FDI TX PLL */
3023 reg = FDI_TX_CTL(pipe);
3024 temp = I915_READ(reg);
3025 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3026
3027 POSTING_READ(reg);
3028 udelay(100);
3029
3030 reg = FDI_RX_CTL(pipe);
3031 temp = I915_READ(reg);
3032 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3033
3034 /* Wait for the clocks to turn off. */
3035 POSTING_READ(reg);
3036 udelay(100);
3037}
3038
0fc932b8
JB
3039static void ironlake_fdi_disable(struct drm_crtc *crtc)
3040{
3041 struct drm_device *dev = crtc->dev;
3042 struct drm_i915_private *dev_priv = dev->dev_private;
3043 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3044 int pipe = intel_crtc->pipe;
3045 u32 reg, temp;
3046
3047 /* disable CPU FDI tx and PCH FDI rx */
3048 reg = FDI_TX_CTL(pipe);
3049 temp = I915_READ(reg);
3050 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3051 POSTING_READ(reg);
3052
3053 reg = FDI_RX_CTL(pipe);
3054 temp = I915_READ(reg);
3055 temp &= ~(0x7 << 16);
dfd07d72 3056 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3057 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3058
3059 POSTING_READ(reg);
3060 udelay(100);
3061
3062 /* Ironlake workaround, disable clock pointer after downing FDI */
6f06ce18
JB
3063 if (HAS_PCH_IBX(dev)) {
3064 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
6f06ce18 3065 }
0fc932b8
JB
3066
3067 /* still set train pattern 1 */
3068 reg = FDI_TX_CTL(pipe);
3069 temp = I915_READ(reg);
3070 temp &= ~FDI_LINK_TRAIN_NONE;
3071 temp |= FDI_LINK_TRAIN_PATTERN_1;
3072 I915_WRITE(reg, temp);
3073
3074 reg = FDI_RX_CTL(pipe);
3075 temp = I915_READ(reg);
3076 if (HAS_PCH_CPT(dev)) {
3077 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3078 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3079 } else {
3080 temp &= ~FDI_LINK_TRAIN_NONE;
3081 temp |= FDI_LINK_TRAIN_PATTERN_1;
3082 }
3083 /* BPC in FDI rx is consistent with that in PIPECONF */
3084 temp &= ~(0x07 << 16);
dfd07d72 3085 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
0fc932b8
JB
3086 I915_WRITE(reg, temp);
3087
3088 POSTING_READ(reg);
3089 udelay(100);
3090}
3091
5dce5b93
CW
3092bool intel_has_pending_fb_unpin(struct drm_device *dev)
3093{
3094 struct intel_crtc *crtc;
3095
3096 /* Note that we don't need to be called with mode_config.lock here
3097 * as our list of CRTC objects is static for the lifetime of the
3098 * device and so cannot disappear as we iterate. Similarly, we can
3099 * happily treat the predicates as racy, atomic checks as userspace
3100 * cannot claim and pin a new fb without at least acquring the
3101 * struct_mutex and so serialising with us.
3102 */
3103 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
3104 if (atomic_read(&crtc->unpin_work_count) == 0)
3105 continue;
3106
3107 if (crtc->unpin_work)
3108 intel_wait_for_vblank(dev, crtc->pipe);
3109
3110 return true;
3111 }
3112
3113 return false;
3114}
3115
e6c3a2a6
CW
3116static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
3117{
0f91128d 3118 struct drm_device *dev = crtc->dev;
5bb61643 3119 struct drm_i915_private *dev_priv = dev->dev_private;
e6c3a2a6
CW
3120
3121 if (crtc->fb == NULL)
3122 return;
3123
2c10d571
DV
3124 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3125
5bb61643
CW
3126 wait_event(dev_priv->pending_flip_queue,
3127 !intel_crtc_has_pending_flip(crtc));
3128
0f91128d
CW
3129 mutex_lock(&dev->struct_mutex);
3130 intel_finish_fb(crtc->fb);
3131 mutex_unlock(&dev->struct_mutex);
e6c3a2a6
CW
3132}
3133
e615efe4
ED
3134/* Program iCLKIP clock to the desired frequency */
3135static void lpt_program_iclkip(struct drm_crtc *crtc)
3136{
3137 struct drm_device *dev = crtc->dev;
3138 struct drm_i915_private *dev_priv = dev->dev_private;
241bfc38 3139 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
e615efe4
ED
3140 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3141 u32 temp;
3142
09153000
DV
3143 mutex_lock(&dev_priv->dpio_lock);
3144
e615efe4
ED
3145 /* It is necessary to ungate the pixclk gate prior to programming
3146 * the divisors, and gate it back when it is done.
3147 */
3148 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3149
3150 /* Disable SSCCTL */
3151 intel_sbi_write(dev_priv, SBI_SSCCTL6,
988d6ee8
PZ
3152 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3153 SBI_SSCCTL_DISABLE,
3154 SBI_ICLK);
e615efe4
ED
3155
3156 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
12d7ceed 3157 if (clock == 20000) {
e615efe4
ED
3158 auxdiv = 1;
3159 divsel = 0x41;
3160 phaseinc = 0x20;
3161 } else {
3162 /* The iCLK virtual clock root frequency is in MHz,
241bfc38
DL
3163 * but the adjusted_mode->crtc_clock in in KHz. To get the
3164 * divisors, it is necessary to divide one by another, so we
e615efe4
ED
3165 * convert the virtual clock precision to KHz here for higher
3166 * precision.
3167 */
3168 u32 iclk_virtual_root_freq = 172800 * 1000;
3169 u32 iclk_pi_range = 64;
3170 u32 desired_divisor, msb_divisor_value, pi_value;
3171
12d7ceed 3172 desired_divisor = (iclk_virtual_root_freq / clock);
e615efe4
ED
3173 msb_divisor_value = desired_divisor / iclk_pi_range;
3174 pi_value = desired_divisor % iclk_pi_range;
3175
3176 auxdiv = 0;
3177 divsel = msb_divisor_value - 2;
3178 phaseinc = pi_value;
3179 }
3180
3181 /* This should not happen with any sane values */
3182 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3183 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3184 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3185 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3186
3187 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
12d7ceed 3188 clock,
e615efe4
ED
3189 auxdiv,
3190 divsel,
3191 phasedir,
3192 phaseinc);
3193
3194 /* Program SSCDIVINTPHASE6 */
988d6ee8 3195 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
e615efe4
ED
3196 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3197 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3198 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3199 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3200 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3201 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
988d6ee8 3202 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
e615efe4
ED
3203
3204 /* Program SSCAUXDIV */
988d6ee8 3205 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
e615efe4
ED
3206 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3207 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
988d6ee8 3208 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
e615efe4
ED
3209
3210 /* Enable modulator and associated divider */
988d6ee8 3211 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
e615efe4 3212 temp &= ~SBI_SSCCTL_DISABLE;
988d6ee8 3213 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
e615efe4
ED
3214
3215 /* Wait for initialization time */
3216 udelay(24);
3217
3218 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
09153000
DV
3219
3220 mutex_unlock(&dev_priv->dpio_lock);
e615efe4
ED
3221}
3222
275f01b2
DV
3223static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3224 enum pipe pch_transcoder)
3225{
3226 struct drm_device *dev = crtc->base.dev;
3227 struct drm_i915_private *dev_priv = dev->dev_private;
3228 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3229
3230 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3231 I915_READ(HTOTAL(cpu_transcoder)));
3232 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3233 I915_READ(HBLANK(cpu_transcoder)));
3234 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3235 I915_READ(HSYNC(cpu_transcoder)));
3236
3237 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3238 I915_READ(VTOTAL(cpu_transcoder)));
3239 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3240 I915_READ(VBLANK(cpu_transcoder)));
3241 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3242 I915_READ(VSYNC(cpu_transcoder)));
3243 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3244 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3245}
3246
1fbc0d78
DV
3247static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3248{
3249 struct drm_i915_private *dev_priv = dev->dev_private;
3250 uint32_t temp;
3251
3252 temp = I915_READ(SOUTH_CHICKEN1);
3253 if (temp & FDI_BC_BIFURCATION_SELECT)
3254 return;
3255
3256 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3257 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3258
3259 temp |= FDI_BC_BIFURCATION_SELECT;
3260 DRM_DEBUG_KMS("enabling fdi C rx\n");
3261 I915_WRITE(SOUTH_CHICKEN1, temp);
3262 POSTING_READ(SOUTH_CHICKEN1);
3263}
3264
3265static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3266{
3267 struct drm_device *dev = intel_crtc->base.dev;
3268 struct drm_i915_private *dev_priv = dev->dev_private;
3269
3270 switch (intel_crtc->pipe) {
3271 case PIPE_A:
3272 break;
3273 case PIPE_B:
3274 if (intel_crtc->config.fdi_lanes > 2)
3275 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3276 else
3277 cpt_enable_fdi_bc_bifurcation(dev);
3278
3279 break;
3280 case PIPE_C:
3281 cpt_enable_fdi_bc_bifurcation(dev);
3282
3283 break;
3284 default:
3285 BUG();
3286 }
3287}
3288
f67a559d
JB
3289/*
3290 * Enable PCH resources required for PCH ports:
3291 * - PCH PLLs
3292 * - FDI training & RX/TX
3293 * - update transcoder timings
3294 * - DP transcoding bits
3295 * - transcoder
3296 */
3297static void ironlake_pch_enable(struct drm_crtc *crtc)
0e23b99d
JB
3298{
3299 struct drm_device *dev = crtc->dev;
3300 struct drm_i915_private *dev_priv = dev->dev_private;
3301 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3302 int pipe = intel_crtc->pipe;
ee7b9f93 3303 u32 reg, temp;
2c07245f 3304
ab9412ba 3305 assert_pch_transcoder_disabled(dev_priv, pipe);
e7e164db 3306
1fbc0d78
DV
3307 if (IS_IVYBRIDGE(dev))
3308 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3309
cd986abb
DV
3310 /* Write the TU size bits before fdi link training, so that error
3311 * detection works. */
3312 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3313 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3314
c98e9dcf 3315 /* For PCH output, training FDI link */
674cf967 3316 dev_priv->display.fdi_link_train(crtc);
2c07245f 3317
3ad8a208
DV
3318 /* We need to program the right clock selection before writing the pixel
3319 * mutliplier into the DPLL. */
303b81e0 3320 if (HAS_PCH_CPT(dev)) {
ee7b9f93 3321 u32 sel;
4b645f14 3322
c98e9dcf 3323 temp = I915_READ(PCH_DPLL_SEL);
11887397
DV
3324 temp |= TRANS_DPLL_ENABLE(pipe);
3325 sel = TRANS_DPLLB_SEL(pipe);
a43f6e0f 3326 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
ee7b9f93
JB
3327 temp |= sel;
3328 else
3329 temp &= ~sel;
c98e9dcf 3330 I915_WRITE(PCH_DPLL_SEL, temp);
c98e9dcf 3331 }
5eddb70b 3332
3ad8a208
DV
3333 /* XXX: pch pll's can be enabled any time before we enable the PCH
3334 * transcoder, and we actually should do this to not upset any PCH
3335 * transcoder that already use the clock when we share it.
3336 *
3337 * Note that enable_shared_dpll tries to do the right thing, but
3338 * get_shared_dpll unconditionally resets the pll - we need that to have
3339 * the right LVDS enable sequence. */
3340 ironlake_enable_shared_dpll(intel_crtc);
3341
d9b6cb56
JB
3342 /* set transcoder timing, panel must allow it */
3343 assert_panel_unlocked(dev_priv, pipe);
275f01b2 3344 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
8db9d77b 3345
303b81e0 3346 intel_fdi_normal_train(crtc);
5e84e1a4 3347
c98e9dcf
JB
3348 /* For PCH DP, enable TRANS_DP_CTL */
3349 if (HAS_PCH_CPT(dev) &&
417e822d
KP
3350 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3351 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
dfd07d72 3352 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
5eddb70b
CW
3353 reg = TRANS_DP_CTL(pipe);
3354 temp = I915_READ(reg);
3355 temp &= ~(TRANS_DP_PORT_SEL_MASK |
220cad3c
EA
3356 TRANS_DP_SYNC_MASK |
3357 TRANS_DP_BPC_MASK);
5eddb70b
CW
3358 temp |= (TRANS_DP_OUTPUT_ENABLE |
3359 TRANS_DP_ENH_FRAMING);
9325c9f0 3360 temp |= bpc << 9; /* same format but at 11:9 */
c98e9dcf
JB
3361
3362 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
5eddb70b 3363 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
c98e9dcf 3364 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
5eddb70b 3365 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
c98e9dcf
JB
3366
3367 switch (intel_trans_dp_port_sel(crtc)) {
3368 case PCH_DP_B:
5eddb70b 3369 temp |= TRANS_DP_PORT_SEL_B;
c98e9dcf
JB
3370 break;
3371 case PCH_DP_C:
5eddb70b 3372 temp |= TRANS_DP_PORT_SEL_C;
c98e9dcf
JB
3373 break;
3374 case PCH_DP_D:
5eddb70b 3375 temp |= TRANS_DP_PORT_SEL_D;
c98e9dcf
JB
3376 break;
3377 default:
e95d41e1 3378 BUG();
32f9d658 3379 }
2c07245f 3380
5eddb70b 3381 I915_WRITE(reg, temp);
6be4a607 3382 }
b52eb4dc 3383
b8a4f404 3384 ironlake_enable_pch_transcoder(dev_priv, pipe);
f67a559d
JB
3385}
3386
1507e5bd
PZ
3387static void lpt_pch_enable(struct drm_crtc *crtc)
3388{
3389 struct drm_device *dev = crtc->dev;
3390 struct drm_i915_private *dev_priv = dev->dev_private;
3391 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 3392 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
1507e5bd 3393
ab9412ba 3394 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
1507e5bd 3395
8c52b5e8 3396 lpt_program_iclkip(crtc);
1507e5bd 3397
0540e488 3398 /* Set transcoder timing. */
275f01b2 3399 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
1507e5bd 3400
937bb610 3401 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
f67a559d
JB
3402}
3403
e2b78267 3404static void intel_put_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3405{
e2b78267 3406 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
ee7b9f93
JB
3407
3408 if (pll == NULL)
3409 return;
3410
3411 if (pll->refcount == 0) {
46edb027 3412 WARN(1, "bad %s refcount\n", pll->name);
ee7b9f93
JB
3413 return;
3414 }
3415
f4a091c7
DV
3416 if (--pll->refcount == 0) {
3417 WARN_ON(pll->on);
3418 WARN_ON(pll->active);
3419 }
3420
a43f6e0f 3421 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
ee7b9f93
JB
3422}
3423
b89a1d39 3424static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
ee7b9f93 3425{
e2b78267
DV
3426 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3427 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3428 enum intel_dpll_id i;
ee7b9f93 3429
ee7b9f93 3430 if (pll) {
46edb027
DV
3431 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3432 crtc->base.base.id, pll->name);
e2b78267 3433 intel_put_shared_dpll(crtc);
ee7b9f93
JB
3434 }
3435
98b6bd99
DV
3436 if (HAS_PCH_IBX(dev_priv->dev)) {
3437 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
d94ab068 3438 i = (enum intel_dpll_id) crtc->pipe;
e72f9fbf 3439 pll = &dev_priv->shared_dplls[i];
98b6bd99 3440
46edb027
DV
3441 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3442 crtc->base.base.id, pll->name);
98b6bd99
DV
3443
3444 goto found;
3445 }
3446
e72f9fbf
DV
3447 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3448 pll = &dev_priv->shared_dplls[i];
ee7b9f93
JB
3449
3450 /* Only want to check enabled timings first */
3451 if (pll->refcount == 0)
3452 continue;
3453
b89a1d39
DV
3454 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3455 sizeof(pll->hw_state)) == 0) {
46edb027 3456 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
e2b78267 3457 crtc->base.base.id,
46edb027 3458 pll->name, pll->refcount, pll->active);
ee7b9f93
JB
3459
3460 goto found;
3461 }
3462 }
3463
3464 /* Ok no matching timings, maybe there's a free one? */
e72f9fbf
DV
3465 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3466 pll = &dev_priv->shared_dplls[i];
ee7b9f93 3467 if (pll->refcount == 0) {
46edb027
DV
3468 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3469 crtc->base.base.id, pll->name);
ee7b9f93
JB
3470 goto found;
3471 }
3472 }
3473
3474 return NULL;
3475
3476found:
a43f6e0f 3477 crtc->config.shared_dpll = i;
46edb027
DV
3478 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3479 pipe_name(crtc->pipe));
ee7b9f93 3480
cdbd2316 3481 if (pll->active == 0) {
66e985c0
DV
3482 memcpy(&pll->hw_state, &crtc->config.dpll_hw_state,
3483 sizeof(pll->hw_state));
3484
46edb027 3485 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
cdbd2316 3486 WARN_ON(pll->on);
e9d6944e 3487 assert_shared_dpll_disabled(dev_priv, pll);
ee7b9f93 3488
15bdd4cf 3489 pll->mode_set(dev_priv, pll);
cdbd2316
DV
3490 }
3491 pll->refcount++;
e04c7350 3492
ee7b9f93
JB
3493 return pll;
3494}
3495
a1520318 3496static void cpt_verify_modeset(struct drm_device *dev, int pipe)
d4270e57
JB
3497{
3498 struct drm_i915_private *dev_priv = dev->dev_private;
23670b32 3499 int dslreg = PIPEDSL(pipe);
d4270e57
JB
3500 u32 temp;
3501
3502 temp = I915_READ(dslreg);
3503 udelay(500);
3504 if (wait_for(I915_READ(dslreg) != temp, 5)) {
d4270e57 3505 if (wait_for(I915_READ(dslreg) != temp, 5))
84f44ce7 3506 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
d4270e57
JB
3507 }
3508}
3509
b074cec8
JB
3510static void ironlake_pfit_enable(struct intel_crtc *crtc)
3511{
3512 struct drm_device *dev = crtc->base.dev;
3513 struct drm_i915_private *dev_priv = dev->dev_private;
3514 int pipe = crtc->pipe;
3515
fd4daa9c 3516 if (crtc->config.pch_pfit.enabled) {
b074cec8
JB
3517 /* Force use of hard-coded filter coefficients
3518 * as some pre-programmed values are broken,
3519 * e.g. x201.
3520 */
3521 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3522 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3523 PF_PIPE_SEL_IVB(pipe));
3524 else
3525 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3526 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3527 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
d4270e57
JB
3528 }
3529}
3530
bb53d4ae
VS
3531static void intel_enable_planes(struct drm_crtc *crtc)
3532{
3533 struct drm_device *dev = crtc->dev;
3534 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3535 struct intel_plane *intel_plane;
3536
3537 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3538 if (intel_plane->pipe == pipe)
3539 intel_plane_restore(&intel_plane->base);
3540}
3541
3542static void intel_disable_planes(struct drm_crtc *crtc)
3543{
3544 struct drm_device *dev = crtc->dev;
3545 enum pipe pipe = to_intel_crtc(crtc)->pipe;
3546 struct intel_plane *intel_plane;
3547
3548 list_for_each_entry(intel_plane, &dev->mode_config.plane_list, base.head)
3549 if (intel_plane->pipe == pipe)
3550 intel_plane_disable(&intel_plane->base);
3551}
3552
20bc8673 3553void hsw_enable_ips(struct intel_crtc *crtc)
d77e4531
PZ
3554{
3555 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3556
3557 if (!crtc->config.ips_enabled)
3558 return;
3559
3560 /* We can only enable IPS after we enable a plane and wait for a vblank.
3561 * We guarantee that the plane is enabled by calling intel_enable_ips
3562 * only after intel_enable_plane. And intel_enable_plane already waits
3563 * for a vblank, so all we need to do here is to enable the IPS bit. */
3564 assert_plane_enabled(dev_priv, crtc->plane);
2a114cc1
BW
3565 if (IS_BROADWELL(crtc->base.dev)) {
3566 mutex_lock(&dev_priv->rps.hw_lock);
3567 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3568 mutex_unlock(&dev_priv->rps.hw_lock);
3569 /* Quoting Art Runyan: "its not safe to expect any particular
3570 * value in IPS_CTL bit 31 after enabling IPS through the
e59150dc
JB
3571 * mailbox." Moreover, the mailbox may return a bogus state,
3572 * so we need to just enable it and continue on.
2a114cc1
BW
3573 */
3574 } else {
3575 I915_WRITE(IPS_CTL, IPS_ENABLE);
3576 /* The bit only becomes 1 in the next vblank, so this wait here
3577 * is essentially intel_wait_for_vblank. If we don't have this
3578 * and don't wait for vblanks until the end of crtc_enable, then
3579 * the HW state readout code will complain that the expected
3580 * IPS_CTL value is not the one we read. */
3581 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3582 DRM_ERROR("Timed out waiting for IPS enable\n");
3583 }
d77e4531
PZ
3584}
3585
20bc8673 3586void hsw_disable_ips(struct intel_crtc *crtc)
d77e4531
PZ
3587{
3588 struct drm_device *dev = crtc->base.dev;
3589 struct drm_i915_private *dev_priv = dev->dev_private;
3590
3591 if (!crtc->config.ips_enabled)
3592 return;
3593
3594 assert_plane_enabled(dev_priv, crtc->plane);
2a114cc1
BW
3595 if (IS_BROADWELL(crtc->base.dev)) {
3596 mutex_lock(&dev_priv->rps.hw_lock);
3597 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3598 mutex_unlock(&dev_priv->rps.hw_lock);
e59150dc 3599 } else {
2a114cc1 3600 I915_WRITE(IPS_CTL, 0);
e59150dc
JB
3601 POSTING_READ(IPS_CTL);
3602 }
d77e4531
PZ
3603
3604 /* We need to wait for a vblank before we can disable the plane. */
3605 intel_wait_for_vblank(dev, crtc->pipe);
3606}
3607
3608/** Loads the palette/gamma unit for the CRTC with the prepared values */
3609static void intel_crtc_load_lut(struct drm_crtc *crtc)
3610{
3611 struct drm_device *dev = crtc->dev;
3612 struct drm_i915_private *dev_priv = dev->dev_private;
3613 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3614 enum pipe pipe = intel_crtc->pipe;
3615 int palreg = PALETTE(pipe);
3616 int i;
3617 bool reenable_ips = false;
3618
3619 /* The clocks have to be on to load the palette. */
3620 if (!crtc->enabled || !intel_crtc->active)
3621 return;
3622
3623 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3624 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3625 assert_dsi_pll_enabled(dev_priv);
3626 else
3627 assert_pll_enabled(dev_priv, pipe);
3628 }
3629
3630 /* use legacy palette for Ironlake */
3631 if (HAS_PCH_SPLIT(dev))
3632 palreg = LGC_PALETTE(pipe);
3633
3634 /* Workaround : Do not read or write the pipe palette/gamma data while
3635 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3636 */
41e6fc4c 3637 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
d77e4531
PZ
3638 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3639 GAMMA_MODE_MODE_SPLIT)) {
3640 hsw_disable_ips(intel_crtc);
3641 reenable_ips = true;
3642 }
3643
3644 for (i = 0; i < 256; i++) {
3645 I915_WRITE(palreg + 4 * i,
3646 (intel_crtc->lut_r[i] << 16) |
3647 (intel_crtc->lut_g[i] << 8) |
3648 intel_crtc->lut_b[i]);
3649 }
3650
3651 if (reenable_ips)
3652 hsw_enable_ips(intel_crtc);
3653}
3654
f67a559d
JB
3655static void ironlake_crtc_enable(struct drm_crtc *crtc)
3656{
3657 struct drm_device *dev = crtc->dev;
3658 struct drm_i915_private *dev_priv = dev->dev_private;
3659 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 3660 struct intel_encoder *encoder;
f67a559d
JB
3661 int pipe = intel_crtc->pipe;
3662 int plane = intel_crtc->plane;
f67a559d 3663
08a48469
DV
3664 WARN_ON(!crtc->enabled);
3665
f67a559d
JB
3666 if (intel_crtc->active)
3667 return;
3668
3669 intel_crtc->active = true;
8664281b
PZ
3670
3671 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3672 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3673
f6736a1a 3674 for_each_encoder_on_crtc(dev, crtc, encoder)
952735ee
DV
3675 if (encoder->pre_enable)
3676 encoder->pre_enable(encoder);
f67a559d 3677
5bfe2ac0 3678 if (intel_crtc->config.has_pch_encoder) {
fff367c7
DV
3679 /* Note: FDI PLL enabling _must_ be done before we enable the
3680 * cpu pipes, hence this is separate from all the other fdi/pch
3681 * enabling. */
88cefb6c 3682 ironlake_fdi_pll_enable(intel_crtc);
46b6f814
DV
3683 } else {
3684 assert_fdi_tx_disabled(dev_priv, pipe);
3685 assert_fdi_rx_disabled(dev_priv, pipe);
3686 }
f67a559d 3687
b074cec8 3688 ironlake_pfit_enable(intel_crtc);
f67a559d 3689
9c54c0dd
JB
3690 /*
3691 * On ILK+ LUT must be loaded before the pipe is running but with
3692 * clocks enabled
3693 */
3694 intel_crtc_load_lut(crtc);
3695
f37fcc2a 3696 intel_update_watermarks(crtc);
e1fdc473 3697 intel_enable_pipe(intel_crtc);
d1de00ef 3698 intel_enable_primary_plane(dev_priv, plane, pipe);
bb53d4ae 3699 intel_enable_planes(crtc);
5c38d48c 3700 intel_crtc_update_cursor(crtc, true);
f67a559d 3701
5bfe2ac0 3702 if (intel_crtc->config.has_pch_encoder)
f67a559d 3703 ironlake_pch_enable(crtc);
c98e9dcf 3704
d1ebd816 3705 mutex_lock(&dev->struct_mutex);
bed4a673 3706 intel_update_fbc(dev);
d1ebd816
BW
3707 mutex_unlock(&dev->struct_mutex);
3708
fa5c73b1
DV
3709 for_each_encoder_on_crtc(dev, crtc, encoder)
3710 encoder->enable(encoder);
61b77ddd
DV
3711
3712 if (HAS_PCH_CPT(dev))
a1520318 3713 cpt_verify_modeset(dev, intel_crtc->pipe);
6ce94100
DV
3714
3715 /*
3716 * There seems to be a race in PCH platform hw (at least on some
3717 * outputs) where an enabled pipe still completes any pageflip right
3718 * away (as if the pipe is off) instead of waiting for vblank. As soon
3719 * as the first vblank happend, everything works as expected. Hence just
3720 * wait for one vblank before returning to avoid strange things
3721 * happening.
3722 */
3723 intel_wait_for_vblank(dev, intel_crtc->pipe);
6be4a607
JB
3724}
3725
42db64ef
PZ
3726/* IPS only exists on ULT machines and is tied to pipe A. */
3727static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3728{
f5adf94e 3729 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
42db64ef
PZ
3730}
3731
dda9a66a
VS
3732static void haswell_crtc_enable_planes(struct drm_crtc *crtc)
3733{
3734 struct drm_device *dev = crtc->dev;
3735 struct drm_i915_private *dev_priv = dev->dev_private;
3736 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3737 int pipe = intel_crtc->pipe;
3738 int plane = intel_crtc->plane;
3739
d1de00ef 3740 intel_enable_primary_plane(dev_priv, plane, pipe);
dda9a66a
VS
3741 intel_enable_planes(crtc);
3742 intel_crtc_update_cursor(crtc, true);
3743
3744 hsw_enable_ips(intel_crtc);
3745
3746 mutex_lock(&dev->struct_mutex);
3747 intel_update_fbc(dev);
3748 mutex_unlock(&dev->struct_mutex);
3749}
3750
3751static void haswell_crtc_disable_planes(struct drm_crtc *crtc)
3752{
3753 struct drm_device *dev = crtc->dev;
3754 struct drm_i915_private *dev_priv = dev->dev_private;
3755 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3756 int pipe = intel_crtc->pipe;
3757 int plane = intel_crtc->plane;
3758
3759 intel_crtc_wait_for_pending_flips(crtc);
3760 drm_vblank_off(dev, pipe);
3761
3762 /* FBC must be disabled before disabling the plane on HSW. */
3763 if (dev_priv->fbc.plane == plane)
3764 intel_disable_fbc(dev);
3765
3766 hsw_disable_ips(intel_crtc);
3767
3768 intel_crtc_update_cursor(crtc, false);
3769 intel_disable_planes(crtc);
d1de00ef 3770 intel_disable_primary_plane(dev_priv, plane, pipe);
dda9a66a
VS
3771}
3772
e4916946
PZ
3773/*
3774 * This implements the workaround described in the "notes" section of the mode
3775 * set sequence documentation. When going from no pipes or single pipe to
3776 * multiple pipes, and planes are enabled after the pipe, we need to wait at
3777 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
3778 */
3779static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
3780{
3781 struct drm_device *dev = crtc->base.dev;
3782 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
3783
3784 /* We want to get the other_active_crtc only if there's only 1 other
3785 * active crtc. */
3786 list_for_each_entry(crtc_it, &dev->mode_config.crtc_list, base.head) {
3787 if (!crtc_it->active || crtc_it == crtc)
3788 continue;
3789
3790 if (other_active_crtc)
3791 return;
3792
3793 other_active_crtc = crtc_it;
3794 }
3795 if (!other_active_crtc)
3796 return;
3797
3798 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3799 intel_wait_for_vblank(dev, other_active_crtc->pipe);
3800}
3801
4f771f10
PZ
3802static void haswell_crtc_enable(struct drm_crtc *crtc)
3803{
3804 struct drm_device *dev = crtc->dev;
3805 struct drm_i915_private *dev_priv = dev->dev_private;
3806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3807 struct intel_encoder *encoder;
3808 int pipe = intel_crtc->pipe;
4f771f10
PZ
3809
3810 WARN_ON(!crtc->enabled);
3811
3812 if (intel_crtc->active)
3813 return;
3814
3815 intel_crtc->active = true;
8664281b
PZ
3816
3817 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3818 if (intel_crtc->config.has_pch_encoder)
3819 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
3820
5bfe2ac0 3821 if (intel_crtc->config.has_pch_encoder)
04945641 3822 dev_priv->display.fdi_link_train(crtc);
4f771f10
PZ
3823
3824 for_each_encoder_on_crtc(dev, crtc, encoder)
3825 if (encoder->pre_enable)
3826 encoder->pre_enable(encoder);
3827
1f544388 3828 intel_ddi_enable_pipe_clock(intel_crtc);
4f771f10 3829
b074cec8 3830 ironlake_pfit_enable(intel_crtc);
4f771f10
PZ
3831
3832 /*
3833 * On ILK+ LUT must be loaded before the pipe is running but with
3834 * clocks enabled
3835 */
3836 intel_crtc_load_lut(crtc);
3837
1f544388 3838 intel_ddi_set_pipe_settings(crtc);
8228c251 3839 intel_ddi_enable_transcoder_func(crtc);
4f771f10 3840
f37fcc2a 3841 intel_update_watermarks(crtc);
e1fdc473 3842 intel_enable_pipe(intel_crtc);
42db64ef 3843
5bfe2ac0 3844 if (intel_crtc->config.has_pch_encoder)
1507e5bd 3845 lpt_pch_enable(crtc);
4f771f10 3846
8807e55b 3847 for_each_encoder_on_crtc(dev, crtc, encoder) {
4f771f10 3848 encoder->enable(encoder);
8807e55b
JN
3849 intel_opregion_notify_encoder(encoder, true);
3850 }
4f771f10 3851
e4916946
PZ
3852 /* If we change the relative order between pipe/planes enabling, we need
3853 * to change the workaround. */
3854 haswell_mode_set_planes_workaround(intel_crtc);
dda9a66a 3855 haswell_crtc_enable_planes(crtc);
4f771f10
PZ
3856}
3857
3f8dce3a
DV
3858static void ironlake_pfit_disable(struct intel_crtc *crtc)
3859{
3860 struct drm_device *dev = crtc->base.dev;
3861 struct drm_i915_private *dev_priv = dev->dev_private;
3862 int pipe = crtc->pipe;
3863
3864 /* To avoid upsetting the power well on haswell only disable the pfit if
3865 * it's in use. The hw state code will make sure we get this right. */
fd4daa9c 3866 if (crtc->config.pch_pfit.enabled) {
3f8dce3a
DV
3867 I915_WRITE(PF_CTL(pipe), 0);
3868 I915_WRITE(PF_WIN_POS(pipe), 0);
3869 I915_WRITE(PF_WIN_SZ(pipe), 0);
3870 }
3871}
3872
6be4a607
JB
3873static void ironlake_crtc_disable(struct drm_crtc *crtc)
3874{
3875 struct drm_device *dev = crtc->dev;
3876 struct drm_i915_private *dev_priv = dev->dev_private;
3877 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 3878 struct intel_encoder *encoder;
6be4a607
JB
3879 int pipe = intel_crtc->pipe;
3880 int plane = intel_crtc->plane;
5eddb70b 3881 u32 reg, temp;
b52eb4dc 3882
ef9c3aee 3883
f7abfe8b
CW
3884 if (!intel_crtc->active)
3885 return;
3886
ea9d758d
DV
3887 for_each_encoder_on_crtc(dev, crtc, encoder)
3888 encoder->disable(encoder);
3889
e6c3a2a6 3890 intel_crtc_wait_for_pending_flips(crtc);
6be4a607 3891 drm_vblank_off(dev, pipe);
913d8d11 3892
5c3fe8b0 3893 if (dev_priv->fbc.plane == plane)
973d04f9 3894 intel_disable_fbc(dev);
2c07245f 3895
0d5b8c61 3896 intel_crtc_update_cursor(crtc, false);
bb53d4ae 3897 intel_disable_planes(crtc);
d1de00ef 3898 intel_disable_primary_plane(dev_priv, plane, pipe);
0d5b8c61 3899
d925c59a
DV
3900 if (intel_crtc->config.has_pch_encoder)
3901 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
3902
b24e7179 3903 intel_disable_pipe(dev_priv, pipe);
32f9d658 3904
3f8dce3a 3905 ironlake_pfit_disable(intel_crtc);
2c07245f 3906
bf49ec8c
DV
3907 for_each_encoder_on_crtc(dev, crtc, encoder)
3908 if (encoder->post_disable)
3909 encoder->post_disable(encoder);
2c07245f 3910
d925c59a
DV
3911 if (intel_crtc->config.has_pch_encoder) {
3912 ironlake_fdi_disable(crtc);
913d8d11 3913
d925c59a
DV
3914 ironlake_disable_pch_transcoder(dev_priv, pipe);
3915 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
6be4a607 3916
d925c59a
DV
3917 if (HAS_PCH_CPT(dev)) {
3918 /* disable TRANS_DP_CTL */
3919 reg = TRANS_DP_CTL(pipe);
3920 temp = I915_READ(reg);
3921 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
3922 TRANS_DP_PORT_SEL_MASK);
3923 temp |= TRANS_DP_PORT_SEL_NONE;
3924 I915_WRITE(reg, temp);
3925
3926 /* disable DPLL_SEL */
3927 temp = I915_READ(PCH_DPLL_SEL);
11887397 3928 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
d925c59a 3929 I915_WRITE(PCH_DPLL_SEL, temp);
9db4a9c7 3930 }
e3421a18 3931
d925c59a 3932 /* disable PCH DPLL */
e72f9fbf 3933 intel_disable_shared_dpll(intel_crtc);
8db9d77b 3934
d925c59a
DV
3935 ironlake_fdi_pll_disable(intel_crtc);
3936 }
6b383a7f 3937
f7abfe8b 3938 intel_crtc->active = false;
46ba614c 3939 intel_update_watermarks(crtc);
d1ebd816
BW
3940
3941 mutex_lock(&dev->struct_mutex);
6b383a7f 3942 intel_update_fbc(dev);
d1ebd816 3943 mutex_unlock(&dev->struct_mutex);
6be4a607 3944}
1b3c7a47 3945
4f771f10 3946static void haswell_crtc_disable(struct drm_crtc *crtc)
ee7b9f93 3947{
4f771f10
PZ
3948 struct drm_device *dev = crtc->dev;
3949 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93 3950 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4f771f10
PZ
3951 struct intel_encoder *encoder;
3952 int pipe = intel_crtc->pipe;
3b117c8f 3953 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee7b9f93 3954
4f771f10
PZ
3955 if (!intel_crtc->active)
3956 return;
3957
dda9a66a
VS
3958 haswell_crtc_disable_planes(crtc);
3959
8807e55b
JN
3960 for_each_encoder_on_crtc(dev, crtc, encoder) {
3961 intel_opregion_notify_encoder(encoder, false);
4f771f10 3962 encoder->disable(encoder);
8807e55b 3963 }
4f771f10 3964
8664281b
PZ
3965 if (intel_crtc->config.has_pch_encoder)
3966 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
4f771f10
PZ
3967 intel_disable_pipe(dev_priv, pipe);
3968
ad80a810 3969 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
4f771f10 3970
3f8dce3a 3971 ironlake_pfit_disable(intel_crtc);
4f771f10 3972
1f544388 3973 intel_ddi_disable_pipe_clock(intel_crtc);
4f771f10
PZ
3974
3975 for_each_encoder_on_crtc(dev, crtc, encoder)
3976 if (encoder->post_disable)
3977 encoder->post_disable(encoder);
3978
88adfff1 3979 if (intel_crtc->config.has_pch_encoder) {
ab4d966c 3980 lpt_disable_pch_transcoder(dev_priv);
8664281b 3981 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
1ad960f2 3982 intel_ddi_fdi_disable(crtc);
83616634 3983 }
4f771f10
PZ
3984
3985 intel_crtc->active = false;
46ba614c 3986 intel_update_watermarks(crtc);
4f771f10
PZ
3987
3988 mutex_lock(&dev->struct_mutex);
3989 intel_update_fbc(dev);
3990 mutex_unlock(&dev->struct_mutex);
3991}
3992
ee7b9f93
JB
3993static void ironlake_crtc_off(struct drm_crtc *crtc)
3994{
3995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
e72f9fbf 3996 intel_put_shared_dpll(intel_crtc);
ee7b9f93
JB
3997}
3998
6441ab5f
PZ
3999static void haswell_crtc_off(struct drm_crtc *crtc)
4000{
4001 intel_ddi_put_crtc_pll(crtc);
4002}
4003
02e792fb
DV
4004static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4005{
02e792fb 4006 if (!enable && intel_crtc->overlay) {
23f09ce3 4007 struct drm_device *dev = intel_crtc->base.dev;
ce453d81 4008 struct drm_i915_private *dev_priv = dev->dev_private;
03f77ea5 4009
23f09ce3 4010 mutex_lock(&dev->struct_mutex);
ce453d81
CW
4011 dev_priv->mm.interruptible = false;
4012 (void) intel_overlay_switch_off(intel_crtc->overlay);
4013 dev_priv->mm.interruptible = true;
23f09ce3 4014 mutex_unlock(&dev->struct_mutex);
02e792fb 4015 }
02e792fb 4016
5dcdbcb0
CW
4017 /* Let userspace switch the overlay on again. In most cases userspace
4018 * has to recompute where to put it anyway.
4019 */
02e792fb
DV
4020}
4021
61bc95c1
EE
4022/**
4023 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
4024 * cursor plane briefly if not already running after enabling the display
4025 * plane.
4026 * This workaround avoids occasional blank screens when self refresh is
4027 * enabled.
4028 */
4029static void
4030g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
4031{
4032 u32 cntl = I915_READ(CURCNTR(pipe));
4033
4034 if ((cntl & CURSOR_MODE) == 0) {
4035 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
4036
4037 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
4038 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
4039 intel_wait_for_vblank(dev_priv->dev, pipe);
4040 I915_WRITE(CURCNTR(pipe), cntl);
4041 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
4042 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
4043 }
4044}
4045
2dd24552
JB
4046static void i9xx_pfit_enable(struct intel_crtc *crtc)
4047{
4048 struct drm_device *dev = crtc->base.dev;
4049 struct drm_i915_private *dev_priv = dev->dev_private;
4050 struct intel_crtc_config *pipe_config = &crtc->config;
4051
328d8e82 4052 if (!crtc->config.gmch_pfit.control)
2dd24552
JB
4053 return;
4054
2dd24552 4055 /*
c0b03411
DV
4056 * The panel fitter should only be adjusted whilst the pipe is disabled,
4057 * according to register description and PRM.
2dd24552 4058 */
c0b03411
DV
4059 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4060 assert_pipe_disabled(dev_priv, crtc->pipe);
2dd24552 4061
b074cec8
JB
4062 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4063 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
5a80c45c
DV
4064
4065 /* Border color in case we don't scale up to the full screen. Black by
4066 * default, change to something else for debugging. */
4067 I915_WRITE(BCLRPAT(crtc->pipe), 0);
2dd24552
JB
4068}
4069
77d22dca
ID
4070#define for_each_power_domain(domain, mask) \
4071 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4072 if ((1 << (domain)) & (mask))
4073
319be8ae
ID
4074enum intel_display_power_domain
4075intel_display_port_power_domain(struct intel_encoder *intel_encoder)
4076{
4077 struct drm_device *dev = intel_encoder->base.dev;
4078 struct intel_digital_port *intel_dig_port;
4079
4080 switch (intel_encoder->type) {
4081 case INTEL_OUTPUT_UNKNOWN:
4082 /* Only DDI platforms should ever use this output type */
4083 WARN_ON_ONCE(!HAS_DDI(dev));
4084 case INTEL_OUTPUT_DISPLAYPORT:
4085 case INTEL_OUTPUT_HDMI:
4086 case INTEL_OUTPUT_EDP:
4087 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
4088 switch (intel_dig_port->port) {
4089 case PORT_A:
4090 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4091 case PORT_B:
4092 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4093 case PORT_C:
4094 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4095 case PORT_D:
4096 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4097 default:
4098 WARN_ON_ONCE(1);
4099 return POWER_DOMAIN_PORT_OTHER;
4100 }
4101 case INTEL_OUTPUT_ANALOG:
4102 return POWER_DOMAIN_PORT_CRT;
4103 case INTEL_OUTPUT_DSI:
4104 return POWER_DOMAIN_PORT_DSI;
4105 default:
4106 return POWER_DOMAIN_PORT_OTHER;
4107 }
4108}
4109
4110static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
77d22dca 4111{
319be8ae
ID
4112 struct drm_device *dev = crtc->dev;
4113 struct intel_encoder *intel_encoder;
4114 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4115 enum pipe pipe = intel_crtc->pipe;
4116 bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
77d22dca
ID
4117 unsigned long mask;
4118 enum transcoder transcoder;
4119
4120 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4121
4122 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4123 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
4124 if (pfit_enabled)
4125 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4126
319be8ae
ID
4127 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4128 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4129
77d22dca
ID
4130 return mask;
4131}
4132
4133void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4134 bool enable)
4135{
4136 if (dev_priv->power_domains.init_power_on == enable)
4137 return;
4138
4139 if (enable)
4140 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4141 else
4142 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4143
4144 dev_priv->power_domains.init_power_on = enable;
4145}
4146
4147static void modeset_update_crtc_power_domains(struct drm_device *dev)
4148{
4149 struct drm_i915_private *dev_priv = dev->dev_private;
4150 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4151 struct intel_crtc *crtc;
4152
4153 /*
4154 * First get all needed power domains, then put all unneeded, to avoid
4155 * any unnecessary toggling of the power wells.
4156 */
4157 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
4158 enum intel_display_power_domain domain;
4159
4160 if (!crtc->base.enabled)
4161 continue;
4162
319be8ae 4163 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
77d22dca
ID
4164
4165 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4166 intel_display_power_get(dev_priv, domain);
4167 }
4168
4169 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
4170 enum intel_display_power_domain domain;
4171
4172 for_each_power_domain(domain, crtc->enabled_power_domains)
4173 intel_display_power_put(dev_priv, domain);
4174
4175 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4176 }
4177
4178 intel_display_set_init_power(dev_priv, false);
4179}
4180
586f49dc 4181int valleyview_get_vco(struct drm_i915_private *dev_priv)
30a970c6 4182{
586f49dc 4183 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
30a970c6 4184
586f49dc
JB
4185 /* Obtain SKU information */
4186 mutex_lock(&dev_priv->dpio_lock);
4187 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4188 CCK_FUSE_HPLL_FREQ_MASK;
4189 mutex_unlock(&dev_priv->dpio_lock);
30a970c6 4190
586f49dc 4191 return vco_freq[hpll_freq];
30a970c6
JB
4192}
4193
4194/* Adjust CDclk dividers to allow high res or save power if possible */
4195static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4196{
4197 struct drm_i915_private *dev_priv = dev->dev_private;
4198 u32 val, cmd;
4199
4200 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
4201 cmd = 2;
4202 else if (cdclk == 266)
4203 cmd = 1;
4204 else
4205 cmd = 0;
4206
4207 mutex_lock(&dev_priv->rps.hw_lock);
4208 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4209 val &= ~DSPFREQGUAR_MASK;
4210 val |= (cmd << DSPFREQGUAR_SHIFT);
4211 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4212 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4213 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4214 50)) {
4215 DRM_ERROR("timed out waiting for CDclk change\n");
4216 }
4217 mutex_unlock(&dev_priv->rps.hw_lock);
4218
4219 if (cdclk == 400) {
4220 u32 divider, vco;
4221
4222 vco = valleyview_get_vco(dev_priv);
4223 divider = ((vco << 1) / cdclk) - 1;
4224
4225 mutex_lock(&dev_priv->dpio_lock);
4226 /* adjust cdclk divider */
4227 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4228 val &= ~0xf;
4229 val |= divider;
4230 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
4231 mutex_unlock(&dev_priv->dpio_lock);
4232 }
4233
4234 mutex_lock(&dev_priv->dpio_lock);
4235 /* adjust self-refresh exit latency value */
4236 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4237 val &= ~0x7f;
4238
4239 /*
4240 * For high bandwidth configs, we set a higher latency in the bunit
4241 * so that the core display fetch happens in time to avoid underruns.
4242 */
4243 if (cdclk == 400)
4244 val |= 4500 / 250; /* 4.5 usec */
4245 else
4246 val |= 3000 / 250; /* 3.0 usec */
4247 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4248 mutex_unlock(&dev_priv->dpio_lock);
4249
4250 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4251 intel_i2c_reset(dev);
4252}
4253
4254static int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
4255{
4256 int cur_cdclk, vco;
4257 int divider;
4258
4259 vco = valleyview_get_vco(dev_priv);
4260
4261 mutex_lock(&dev_priv->dpio_lock);
4262 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4263 mutex_unlock(&dev_priv->dpio_lock);
4264
4265 divider &= 0xf;
4266
4267 cur_cdclk = (vco << 1) / (divider + 1);
4268
4269 return cur_cdclk;
4270}
4271
4272static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4273 int max_pixclk)
4274{
4275 int cur_cdclk;
4276
4277 cur_cdclk = valleyview_cur_cdclk(dev_priv);
4278
4279 /*
4280 * Really only a few cases to deal with, as only 4 CDclks are supported:
4281 * 200MHz
4282 * 267MHz
4283 * 320MHz
4284 * 400MHz
4285 * So we check to see whether we're above 90% of the lower bin and
4286 * adjust if needed.
4287 */
4288 if (max_pixclk > 288000) {
4289 return 400;
4290 } else if (max_pixclk > 240000) {
4291 return 320;
4292 } else
4293 return 266;
4294 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4295}
4296
2f2d7aa1
VS
4297/* compute the max pixel clock for new configuration */
4298static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
30a970c6
JB
4299{
4300 struct drm_device *dev = dev_priv->dev;
4301 struct intel_crtc *intel_crtc;
4302 int max_pixclk = 0;
4303
4304 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4305 base.head) {
2f2d7aa1 4306 if (intel_crtc->new_enabled)
30a970c6 4307 max_pixclk = max(max_pixclk,
2f2d7aa1 4308 intel_crtc->new_config->adjusted_mode.crtc_clock);
30a970c6
JB
4309 }
4310
4311 return max_pixclk;
4312}
4313
4314static void valleyview_modeset_global_pipes(struct drm_device *dev,
2f2d7aa1 4315 unsigned *prepare_pipes)
30a970c6
JB
4316{
4317 struct drm_i915_private *dev_priv = dev->dev_private;
4318 struct intel_crtc *intel_crtc;
2f2d7aa1 4319 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
4320 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4321
4322 if (valleyview_calc_cdclk(dev_priv, max_pixclk) == cur_cdclk)
4323 return;
4324
2f2d7aa1 4325 /* disable/enable all currently active pipes while we change cdclk */
30a970c6
JB
4326 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
4327 base.head)
4328 if (intel_crtc->base.enabled)
4329 *prepare_pipes |= (1 << intel_crtc->pipe);
4330}
4331
4332static void valleyview_modeset_global_resources(struct drm_device *dev)
4333{
4334 struct drm_i915_private *dev_priv = dev->dev_private;
2f2d7aa1 4335 int max_pixclk = intel_mode_max_pixclk(dev_priv);
30a970c6
JB
4336 int cur_cdclk = valleyview_cur_cdclk(dev_priv);
4337 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4338
4339 if (req_cdclk != cur_cdclk)
4340 valleyview_set_cdclk(dev, req_cdclk);
77961eb9 4341 modeset_update_crtc_power_domains(dev);
30a970c6
JB
4342}
4343
89b667f8
JB
4344static void valleyview_crtc_enable(struct drm_crtc *crtc)
4345{
4346 struct drm_device *dev = crtc->dev;
4347 struct drm_i915_private *dev_priv = dev->dev_private;
4348 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4349 struct intel_encoder *encoder;
4350 int pipe = intel_crtc->pipe;
4351 int plane = intel_crtc->plane;
23538ef1 4352 bool is_dsi;
89b667f8
JB
4353
4354 WARN_ON(!crtc->enabled);
4355
4356 if (intel_crtc->active)
4357 return;
4358
4359 intel_crtc->active = true;
89b667f8 4360
89b667f8
JB
4361 for_each_encoder_on_crtc(dev, crtc, encoder)
4362 if (encoder->pre_pll_enable)
4363 encoder->pre_pll_enable(encoder);
4364
23538ef1
JN
4365 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4366
e9fd1c02
JN
4367 if (!is_dsi)
4368 vlv_enable_pll(intel_crtc);
89b667f8
JB
4369
4370 for_each_encoder_on_crtc(dev, crtc, encoder)
4371 if (encoder->pre_enable)
4372 encoder->pre_enable(encoder);
4373
2dd24552
JB
4374 i9xx_pfit_enable(intel_crtc);
4375
63cbb074
VS
4376 intel_crtc_load_lut(crtc);
4377
f37fcc2a 4378 intel_update_watermarks(crtc);
e1fdc473 4379 intel_enable_pipe(intel_crtc);
2d9d2b0b 4380 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
d1de00ef 4381 intel_enable_primary_plane(dev_priv, plane, pipe);
bb53d4ae 4382 intel_enable_planes(crtc);
5c38d48c 4383 intel_crtc_update_cursor(crtc, true);
89b667f8 4384
89b667f8 4385 intel_update_fbc(dev);
5004945f
JN
4386
4387 for_each_encoder_on_crtc(dev, crtc, encoder)
4388 encoder->enable(encoder);
89b667f8
JB
4389}
4390
0b8765c6 4391static void i9xx_crtc_enable(struct drm_crtc *crtc)
79e53945
JB
4392{
4393 struct drm_device *dev = crtc->dev;
79e53945
JB
4394 struct drm_i915_private *dev_priv = dev->dev_private;
4395 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4396 struct intel_encoder *encoder;
79e53945 4397 int pipe = intel_crtc->pipe;
80824003 4398 int plane = intel_crtc->plane;
79e53945 4399
08a48469
DV
4400 WARN_ON(!crtc->enabled);
4401
f7abfe8b
CW
4402 if (intel_crtc->active)
4403 return;
4404
4405 intel_crtc->active = true;
6b383a7f 4406
9d6d9f19
MK
4407 for_each_encoder_on_crtc(dev, crtc, encoder)
4408 if (encoder->pre_enable)
4409 encoder->pre_enable(encoder);
4410
f6736a1a
DV
4411 i9xx_enable_pll(intel_crtc);
4412
2dd24552
JB
4413 i9xx_pfit_enable(intel_crtc);
4414
63cbb074
VS
4415 intel_crtc_load_lut(crtc);
4416
f37fcc2a 4417 intel_update_watermarks(crtc);
e1fdc473 4418 intel_enable_pipe(intel_crtc);
2d9d2b0b 4419 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
d1de00ef 4420 intel_enable_primary_plane(dev_priv, plane, pipe);
bb53d4ae 4421 intel_enable_planes(crtc);
22e407d7 4422 /* The fixup needs to happen before cursor is enabled */
61bc95c1
EE
4423 if (IS_G4X(dev))
4424 g4x_fixup_plane(dev_priv, pipe);
22e407d7 4425 intel_crtc_update_cursor(crtc, true);
79e53945 4426
0b8765c6
JB
4427 /* Give the overlay scaler a chance to enable if it's on this pipe */
4428 intel_crtc_dpms_overlay(intel_crtc, true);
ef9c3aee 4429
f440eb13 4430 intel_update_fbc(dev);
ef9c3aee 4431
fa5c73b1
DV
4432 for_each_encoder_on_crtc(dev, crtc, encoder)
4433 encoder->enable(encoder);
0b8765c6 4434}
79e53945 4435
87476d63
DV
4436static void i9xx_pfit_disable(struct intel_crtc *crtc)
4437{
4438 struct drm_device *dev = crtc->base.dev;
4439 struct drm_i915_private *dev_priv = dev->dev_private;
87476d63 4440
328d8e82
DV
4441 if (!crtc->config.gmch_pfit.control)
4442 return;
87476d63 4443
328d8e82 4444 assert_pipe_disabled(dev_priv, crtc->pipe);
87476d63 4445
328d8e82
DV
4446 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4447 I915_READ(PFIT_CONTROL));
4448 I915_WRITE(PFIT_CONTROL, 0);
87476d63
DV
4449}
4450
0b8765c6
JB
4451static void i9xx_crtc_disable(struct drm_crtc *crtc)
4452{
4453 struct drm_device *dev = crtc->dev;
4454 struct drm_i915_private *dev_priv = dev->dev_private;
4455 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ef9c3aee 4456 struct intel_encoder *encoder;
0b8765c6
JB
4457 int pipe = intel_crtc->pipe;
4458 int plane = intel_crtc->plane;
ef9c3aee 4459
f7abfe8b
CW
4460 if (!intel_crtc->active)
4461 return;
4462
ea9d758d
DV
4463 for_each_encoder_on_crtc(dev, crtc, encoder)
4464 encoder->disable(encoder);
4465
0b8765c6 4466 /* Give the overlay scaler a chance to disable if it's on this pipe */
e6c3a2a6
CW
4467 intel_crtc_wait_for_pending_flips(crtc);
4468 drm_vblank_off(dev, pipe);
0b8765c6 4469
5c3fe8b0 4470 if (dev_priv->fbc.plane == plane)
973d04f9 4471 intel_disable_fbc(dev);
79e53945 4472
0d5b8c61
VS
4473 intel_crtc_dpms_overlay(intel_crtc, false);
4474 intel_crtc_update_cursor(crtc, false);
bb53d4ae 4475 intel_disable_planes(crtc);
d1de00ef 4476 intel_disable_primary_plane(dev_priv, plane, pipe);
0d5b8c61 4477
2d9d2b0b 4478 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
b24e7179 4479 intel_disable_pipe(dev_priv, pipe);
24a1f16d 4480
87476d63 4481 i9xx_pfit_disable(intel_crtc);
24a1f16d 4482
89b667f8
JB
4483 for_each_encoder_on_crtc(dev, crtc, encoder)
4484 if (encoder->post_disable)
4485 encoder->post_disable(encoder);
4486
f6071166
JB
4487 if (IS_VALLEYVIEW(dev) && !intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
4488 vlv_disable_pll(dev_priv, pipe);
4489 else if (!IS_VALLEYVIEW(dev))
e9fd1c02 4490 i9xx_disable_pll(dev_priv, pipe);
0b8765c6 4491
f7abfe8b 4492 intel_crtc->active = false;
46ba614c 4493 intel_update_watermarks(crtc);
f37fcc2a 4494
6b383a7f 4495 intel_update_fbc(dev);
0b8765c6
JB
4496}
4497
ee7b9f93
JB
4498static void i9xx_crtc_off(struct drm_crtc *crtc)
4499{
4500}
4501
976f8a20
DV
4502static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4503 bool enabled)
2c07245f
ZW
4504{
4505 struct drm_device *dev = crtc->dev;
4506 struct drm_i915_master_private *master_priv;
4507 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4508 int pipe = intel_crtc->pipe;
79e53945
JB
4509
4510 if (!dev->primary->master)
4511 return;
4512
4513 master_priv = dev->primary->master->driver_priv;
4514 if (!master_priv->sarea_priv)
4515 return;
4516
79e53945
JB
4517 switch (pipe) {
4518 case 0:
4519 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4520 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4521 break;
4522 case 1:
4523 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4524 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4525 break;
4526 default:
9db4a9c7 4527 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
79e53945
JB
4528 break;
4529 }
79e53945
JB
4530}
4531
976f8a20
DV
4532/**
4533 * Sets the power management mode of the pipe and plane.
4534 */
4535void intel_crtc_update_dpms(struct drm_crtc *crtc)
4536{
4537 struct drm_device *dev = crtc->dev;
4538 struct drm_i915_private *dev_priv = dev->dev_private;
4539 struct intel_encoder *intel_encoder;
4540 bool enable = false;
4541
4542 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4543 enable |= intel_encoder->connectors_active;
4544
4545 if (enable)
4546 dev_priv->display.crtc_enable(crtc);
4547 else
4548 dev_priv->display.crtc_disable(crtc);
4549
4550 intel_crtc_update_sarea(crtc, enable);
4551}
4552
cdd59983
CW
4553static void intel_crtc_disable(struct drm_crtc *crtc)
4554{
cdd59983 4555 struct drm_device *dev = crtc->dev;
976f8a20 4556 struct drm_connector *connector;
ee7b9f93 4557 struct drm_i915_private *dev_priv = dev->dev_private;
7b9f35a6 4558 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
cdd59983 4559
976f8a20
DV
4560 /* crtc should still be enabled when we disable it. */
4561 WARN_ON(!crtc->enabled);
4562
4563 dev_priv->display.crtc_disable(crtc);
c77bf565 4564 intel_crtc->eld_vld = false;
976f8a20 4565 intel_crtc_update_sarea(crtc, false);
ee7b9f93
JB
4566 dev_priv->display.off(crtc);
4567
931872fc 4568 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
93ce0ba6 4569 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
931872fc 4570 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
cdd59983
CW
4571
4572 if (crtc->fb) {
4573 mutex_lock(&dev->struct_mutex);
1690e1eb 4574 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
cdd59983 4575 mutex_unlock(&dev->struct_mutex);
976f8a20
DV
4576 crtc->fb = NULL;
4577 }
4578
4579 /* Update computed state. */
4580 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4581 if (!connector->encoder || !connector->encoder->crtc)
4582 continue;
4583
4584 if (connector->encoder->crtc != crtc)
4585 continue;
4586
4587 connector->dpms = DRM_MODE_DPMS_OFF;
4588 to_intel_encoder(connector->encoder)->connectors_active = false;
cdd59983
CW
4589 }
4590}
4591
ea5b213a 4592void intel_encoder_destroy(struct drm_encoder *encoder)
7e7d76c3 4593{
4ef69c7a 4594 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
ea5b213a 4595
ea5b213a
CW
4596 drm_encoder_cleanup(encoder);
4597 kfree(intel_encoder);
7e7d76c3
JB
4598}
4599
9237329d 4600/* Simple dpms helper for encoders with just one connector, no cloning and only
5ab432ef
DV
4601 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4602 * state of the entire output pipe. */
9237329d 4603static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
7e7d76c3 4604{
5ab432ef
DV
4605 if (mode == DRM_MODE_DPMS_ON) {
4606 encoder->connectors_active = true;
4607
b2cabb0e 4608 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef
DV
4609 } else {
4610 encoder->connectors_active = false;
4611
b2cabb0e 4612 intel_crtc_update_dpms(encoder->base.crtc);
5ab432ef 4613 }
79e53945
JB
4614}
4615
0a91ca29
DV
4616/* Cross check the actual hw state with our own modeset state tracking (and it's
4617 * internal consistency). */
b980514c 4618static void intel_connector_check_state(struct intel_connector *connector)
79e53945 4619{
0a91ca29
DV
4620 if (connector->get_hw_state(connector)) {
4621 struct intel_encoder *encoder = connector->encoder;
4622 struct drm_crtc *crtc;
4623 bool encoder_enabled;
4624 enum pipe pipe;
4625
4626 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4627 connector->base.base.id,
4628 drm_get_connector_name(&connector->base));
4629
4630 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4631 "wrong connector dpms state\n");
4632 WARN(connector->base.encoder != &encoder->base,
4633 "active connector not linked to encoder\n");
4634 WARN(!encoder->connectors_active,
4635 "encoder->connectors_active not set\n");
4636
4637 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4638 WARN(!encoder_enabled, "encoder not enabled\n");
4639 if (WARN_ON(!encoder->base.crtc))
4640 return;
4641
4642 crtc = encoder->base.crtc;
4643
4644 WARN(!crtc->enabled, "crtc not enabled\n");
4645 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4646 WARN(pipe != to_intel_crtc(crtc)->pipe,
4647 "encoder active on the wrong pipe\n");
4648 }
79e53945
JB
4649}
4650
5ab432ef
DV
4651/* Even simpler default implementation, if there's really no special case to
4652 * consider. */
4653void intel_connector_dpms(struct drm_connector *connector, int mode)
79e53945 4654{
5ab432ef
DV
4655 /* All the simple cases only support two dpms states. */
4656 if (mode != DRM_MODE_DPMS_ON)
4657 mode = DRM_MODE_DPMS_OFF;
d4270e57 4658
5ab432ef
DV
4659 if (mode == connector->dpms)
4660 return;
4661
4662 connector->dpms = mode;
4663
4664 /* Only need to change hw state when actually enabled */
c9976dcf
CW
4665 if (connector->encoder)
4666 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
0a91ca29 4667
b980514c 4668 intel_modeset_check_state(connector->dev);
79e53945
JB
4669}
4670
f0947c37
DV
4671/* Simple connector->get_hw_state implementation for encoders that support only
4672 * one connector and no cloning and hence the encoder state determines the state
4673 * of the connector. */
4674bool intel_connector_get_hw_state(struct intel_connector *connector)
ea5b213a 4675{
24929352 4676 enum pipe pipe = 0;
f0947c37 4677 struct intel_encoder *encoder = connector->encoder;
ea5b213a 4678
f0947c37 4679 return encoder->get_hw_state(encoder, &pipe);
ea5b213a
CW
4680}
4681
1857e1da
DV
4682static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
4683 struct intel_crtc_config *pipe_config)
4684{
4685 struct drm_i915_private *dev_priv = dev->dev_private;
4686 struct intel_crtc *pipe_B_crtc =
4687 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
4688
4689 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
4690 pipe_name(pipe), pipe_config->fdi_lanes);
4691 if (pipe_config->fdi_lanes > 4) {
4692 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
4693 pipe_name(pipe), pipe_config->fdi_lanes);
4694 return false;
4695 }
4696
bafb6553 4697 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
1857e1da
DV
4698 if (pipe_config->fdi_lanes > 2) {
4699 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
4700 pipe_config->fdi_lanes);
4701 return false;
4702 } else {
4703 return true;
4704 }
4705 }
4706
4707 if (INTEL_INFO(dev)->num_pipes == 2)
4708 return true;
4709
4710 /* Ivybridge 3 pipe is really complicated */
4711 switch (pipe) {
4712 case PIPE_A:
4713 return true;
4714 case PIPE_B:
4715 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
4716 pipe_config->fdi_lanes > 2) {
4717 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4718 pipe_name(pipe), pipe_config->fdi_lanes);
4719 return false;
4720 }
4721 return true;
4722 case PIPE_C:
1e833f40 4723 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
1857e1da
DV
4724 pipe_B_crtc->config.fdi_lanes <= 2) {
4725 if (pipe_config->fdi_lanes > 2) {
4726 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
4727 pipe_name(pipe), pipe_config->fdi_lanes);
4728 return false;
4729 }
4730 } else {
4731 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
4732 return false;
4733 }
4734 return true;
4735 default:
4736 BUG();
4737 }
4738}
4739
e29c22c0
DV
4740#define RETRY 1
4741static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
4742 struct intel_crtc_config *pipe_config)
877d48d5 4743{
1857e1da 4744 struct drm_device *dev = intel_crtc->base.dev;
877d48d5 4745 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
ff9a6750 4746 int lane, link_bw, fdi_dotclock;
e29c22c0 4747 bool setup_ok, needs_recompute = false;
877d48d5 4748
e29c22c0 4749retry:
877d48d5
DV
4750 /* FDI is a binary signal running at ~2.7GHz, encoding
4751 * each output octet as 10 bits. The actual frequency
4752 * is stored as a divider into a 100MHz clock, and the
4753 * mode pixel clock is stored in units of 1KHz.
4754 * Hence the bw of each lane in terms of the mode signal
4755 * is:
4756 */
4757 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4758
241bfc38 4759 fdi_dotclock = adjusted_mode->crtc_clock;
877d48d5 4760
2bd89a07 4761 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
877d48d5
DV
4762 pipe_config->pipe_bpp);
4763
4764 pipe_config->fdi_lanes = lane;
4765
2bd89a07 4766 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
877d48d5 4767 link_bw, &pipe_config->fdi_m_n);
1857e1da 4768
e29c22c0
DV
4769 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
4770 intel_crtc->pipe, pipe_config);
4771 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
4772 pipe_config->pipe_bpp -= 2*3;
4773 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
4774 pipe_config->pipe_bpp);
4775 needs_recompute = true;
4776 pipe_config->bw_constrained = true;
4777
4778 goto retry;
4779 }
4780
4781 if (needs_recompute)
4782 return RETRY;
4783
4784 return setup_ok ? 0 : -EINVAL;
877d48d5
DV
4785}
4786
42db64ef
PZ
4787static void hsw_compute_ips_config(struct intel_crtc *crtc,
4788 struct intel_crtc_config *pipe_config)
4789{
d330a953 4790 pipe_config->ips_enabled = i915.enable_ips &&
3c4ca58c 4791 hsw_crtc_supports_ips(crtc) &&
b6dfdc9b 4792 pipe_config->pipe_bpp <= 24;
42db64ef
PZ
4793}
4794
a43f6e0f 4795static int intel_crtc_compute_config(struct intel_crtc *crtc,
e29c22c0 4796 struct intel_crtc_config *pipe_config)
79e53945 4797{
a43f6e0f 4798 struct drm_device *dev = crtc->base.dev;
b8cecdf5 4799 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
89749350 4800
ad3a4479 4801 /* FIXME should check pixel clock limits on all platforms */
cf532bb2
VS
4802 if (INTEL_INFO(dev)->gen < 4) {
4803 struct drm_i915_private *dev_priv = dev->dev_private;
4804 int clock_limit =
4805 dev_priv->display.get_display_clock_speed(dev);
4806
4807 /*
4808 * Enable pixel doubling when the dot clock
4809 * is > 90% of the (display) core speed.
4810 *
b397c96b
VS
4811 * GDG double wide on either pipe,
4812 * otherwise pipe A only.
cf532bb2 4813 */
b397c96b 4814 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
241bfc38 4815 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
ad3a4479 4816 clock_limit *= 2;
cf532bb2 4817 pipe_config->double_wide = true;
ad3a4479
VS
4818 }
4819
241bfc38 4820 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
e29c22c0 4821 return -EINVAL;
2c07245f 4822 }
89749350 4823
1d1d0e27
VS
4824 /*
4825 * Pipe horizontal size must be even in:
4826 * - DVO ganged mode
4827 * - LVDS dual channel mode
4828 * - Double wide pipe
4829 */
4830 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
4831 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
4832 pipe_config->pipe_src_w &= ~1;
4833
8693a824
DL
4834 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
4835 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
44f46b42
CW
4836 */
4837 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
4838 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
e29c22c0 4839 return -EINVAL;
44f46b42 4840
bd080ee5 4841 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
5d2d38dd 4842 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
bd080ee5 4843 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
5d2d38dd
DV
4844 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
4845 * for lvds. */
4846 pipe_config->pipe_bpp = 8*3;
4847 }
4848
f5adf94e 4849 if (HAS_IPS(dev))
a43f6e0f
DV
4850 hsw_compute_ips_config(crtc, pipe_config);
4851
4852 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
4853 * clock survives for now. */
4854 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
4855 pipe_config->shared_dpll = crtc->config.shared_dpll;
42db64ef 4856
877d48d5 4857 if (pipe_config->has_pch_encoder)
a43f6e0f 4858 return ironlake_fdi_compute_config(crtc, pipe_config);
877d48d5 4859
e29c22c0 4860 return 0;
79e53945
JB
4861}
4862
25eb05fc
JB
4863static int valleyview_get_display_clock_speed(struct drm_device *dev)
4864{
4865 return 400000; /* FIXME */
4866}
4867
e70236a8
JB
4868static int i945_get_display_clock_speed(struct drm_device *dev)
4869{
4870 return 400000;
4871}
79e53945 4872
e70236a8 4873static int i915_get_display_clock_speed(struct drm_device *dev)
79e53945 4874{
e70236a8
JB
4875 return 333000;
4876}
79e53945 4877
e70236a8
JB
4878static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
4879{
4880 return 200000;
4881}
79e53945 4882
257a7ffc
DV
4883static int pnv_get_display_clock_speed(struct drm_device *dev)
4884{
4885 u16 gcfgc = 0;
4886
4887 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4888
4889 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4890 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
4891 return 267000;
4892 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
4893 return 333000;
4894 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
4895 return 444000;
4896 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
4897 return 200000;
4898 default:
4899 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
4900 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
4901 return 133000;
4902 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
4903 return 167000;
4904 }
4905}
4906
e70236a8
JB
4907static int i915gm_get_display_clock_speed(struct drm_device *dev)
4908{
4909 u16 gcfgc = 0;
79e53945 4910
e70236a8
JB
4911 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
4912
4913 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
4914 return 133000;
4915 else {
4916 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
4917 case GC_DISPLAY_CLOCK_333_MHZ:
4918 return 333000;
4919 default:
4920 case GC_DISPLAY_CLOCK_190_200_MHZ:
4921 return 190000;
79e53945 4922 }
e70236a8
JB
4923 }
4924}
4925
4926static int i865_get_display_clock_speed(struct drm_device *dev)
4927{
4928 return 266000;
4929}
4930
4931static int i855_get_display_clock_speed(struct drm_device *dev)
4932{
4933 u16 hpllcc = 0;
4934 /* Assume that the hardware is in the high speed state. This
4935 * should be the default.
4936 */
4937 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4938 case GC_CLOCK_133_200:
4939 case GC_CLOCK_100_200:
4940 return 200000;
4941 case GC_CLOCK_166_250:
4942 return 250000;
4943 case GC_CLOCK_100_133:
79e53945 4944 return 133000;
e70236a8 4945 }
79e53945 4946
e70236a8
JB
4947 /* Shouldn't happen */
4948 return 0;
4949}
79e53945 4950
e70236a8
JB
4951static int i830_get_display_clock_speed(struct drm_device *dev)
4952{
4953 return 133000;
79e53945
JB
4954}
4955
2c07245f 4956static void
a65851af 4957intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
2c07245f 4958{
a65851af
VS
4959 while (*num > DATA_LINK_M_N_MASK ||
4960 *den > DATA_LINK_M_N_MASK) {
2c07245f
ZW
4961 *num >>= 1;
4962 *den >>= 1;
4963 }
4964}
4965
a65851af
VS
4966static void compute_m_n(unsigned int m, unsigned int n,
4967 uint32_t *ret_m, uint32_t *ret_n)
4968{
4969 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
4970 *ret_m = div_u64((uint64_t) m * *ret_n, n);
4971 intel_reduce_m_n_ratio(ret_m, ret_n);
4972}
4973
e69d0bc1
DV
4974void
4975intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4976 int pixel_clock, int link_clock,
4977 struct intel_link_m_n *m_n)
2c07245f 4978{
e69d0bc1 4979 m_n->tu = 64;
a65851af
VS
4980
4981 compute_m_n(bits_per_pixel * pixel_clock,
4982 link_clock * nlanes * 8,
4983 &m_n->gmch_m, &m_n->gmch_n);
4984
4985 compute_m_n(pixel_clock, link_clock,
4986 &m_n->link_m, &m_n->link_n);
2c07245f
ZW
4987}
4988
a7615030
CW
4989static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4990{
d330a953
JN
4991 if (i915.panel_use_ssc >= 0)
4992 return i915.panel_use_ssc != 0;
41aa3448 4993 return dev_priv->vbt.lvds_use_ssc
435793df 4994 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
a7615030
CW
4995}
4996
c65d77d8
JB
4997static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4998{
4999 struct drm_device *dev = crtc->dev;
5000 struct drm_i915_private *dev_priv = dev->dev_private;
5001 int refclk;
5002
a0c4da24 5003 if (IS_VALLEYVIEW(dev)) {
9a0ea498 5004 refclk = 100000;
a0c4da24 5005 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
c65d77d8 5006 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b
VS
5007 refclk = dev_priv->vbt.lvds_ssc_freq;
5008 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
c65d77d8
JB
5009 } else if (!IS_GEN2(dev)) {
5010 refclk = 96000;
5011 } else {
5012 refclk = 48000;
5013 }
5014
5015 return refclk;
5016}
5017
7429e9d4 5018static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
c65d77d8 5019{
7df00d7a 5020 return (1 << dpll->n) << 16 | dpll->m2;
7429e9d4 5021}
f47709a9 5022
7429e9d4
DV
5023static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5024{
5025 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
c65d77d8
JB
5026}
5027
f47709a9 5028static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
a7516a05
JB
5029 intel_clock_t *reduced_clock)
5030{
f47709a9 5031 struct drm_device *dev = crtc->base.dev;
a7516a05 5032 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5033 int pipe = crtc->pipe;
a7516a05
JB
5034 u32 fp, fp2 = 0;
5035
5036 if (IS_PINEVIEW(dev)) {
7429e9d4 5037 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5038 if (reduced_clock)
7429e9d4 5039 fp2 = pnv_dpll_compute_fp(reduced_clock);
a7516a05 5040 } else {
7429e9d4 5041 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
a7516a05 5042 if (reduced_clock)
7429e9d4 5043 fp2 = i9xx_dpll_compute_fp(reduced_clock);
a7516a05
JB
5044 }
5045
5046 I915_WRITE(FP0(pipe), fp);
8bcc2795 5047 crtc->config.dpll_hw_state.fp0 = fp;
a7516a05 5048
f47709a9
DV
5049 crtc->lowfreq_avail = false;
5050 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
d330a953 5051 reduced_clock && i915.powersave) {
a7516a05 5052 I915_WRITE(FP1(pipe), fp2);
8bcc2795 5053 crtc->config.dpll_hw_state.fp1 = fp2;
f47709a9 5054 crtc->lowfreq_avail = true;
a7516a05
JB
5055 } else {
5056 I915_WRITE(FP1(pipe), fp);
8bcc2795 5057 crtc->config.dpll_hw_state.fp1 = fp;
a7516a05
JB
5058 }
5059}
5060
5e69f97f
CML
5061static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5062 pipe)
89b667f8
JB
5063{
5064 u32 reg_val;
5065
5066 /*
5067 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5068 * and set it to a reasonable value instead.
5069 */
ab3c759a 5070 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8
JB
5071 reg_val &= 0xffffff00;
5072 reg_val |= 0x00000030;
ab3c759a 5073 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5074
ab3c759a 5075 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5076 reg_val &= 0x8cffffff;
5077 reg_val = 0x8c000000;
ab3c759a 5078 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8 5079
ab3c759a 5080 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
89b667f8 5081 reg_val &= 0xffffff00;
ab3c759a 5082 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
89b667f8 5083
ab3c759a 5084 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
89b667f8
JB
5085 reg_val &= 0x00ffffff;
5086 reg_val |= 0xb0000000;
ab3c759a 5087 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
89b667f8
JB
5088}
5089
b551842d
DV
5090static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5091 struct intel_link_m_n *m_n)
5092{
5093 struct drm_device *dev = crtc->base.dev;
5094 struct drm_i915_private *dev_priv = dev->dev_private;
5095 int pipe = crtc->pipe;
5096
e3b95f1e
DV
5097 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5098 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5099 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5100 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
b551842d
DV
5101}
5102
5103static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5104 struct intel_link_m_n *m_n)
5105{
5106 struct drm_device *dev = crtc->base.dev;
5107 struct drm_i915_private *dev_priv = dev->dev_private;
5108 int pipe = crtc->pipe;
5109 enum transcoder transcoder = crtc->config.cpu_transcoder;
5110
5111 if (INTEL_INFO(dev)->gen >= 5) {
5112 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5113 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5114 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5115 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5116 } else {
e3b95f1e
DV
5117 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5118 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5119 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5120 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
b551842d
DV
5121 }
5122}
5123
03afc4a2
DV
5124static void intel_dp_set_m_n(struct intel_crtc *crtc)
5125{
5126 if (crtc->config.has_pch_encoder)
5127 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5128 else
5129 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5130}
5131
f47709a9 5132static void vlv_update_pll(struct intel_crtc *crtc)
a0c4da24 5133{
f47709a9 5134 struct drm_device *dev = crtc->base.dev;
a0c4da24 5135 struct drm_i915_private *dev_priv = dev->dev_private;
f47709a9 5136 int pipe = crtc->pipe;
89b667f8 5137 u32 dpll, mdiv;
a0c4da24 5138 u32 bestn, bestm1, bestm2, bestp1, bestp2;
198a037f 5139 u32 coreclk, reg_val, dpll_md;
a0c4da24 5140
09153000
DV
5141 mutex_lock(&dev_priv->dpio_lock);
5142
f47709a9
DV
5143 bestn = crtc->config.dpll.n;
5144 bestm1 = crtc->config.dpll.m1;
5145 bestm2 = crtc->config.dpll.m2;
5146 bestp1 = crtc->config.dpll.p1;
5147 bestp2 = crtc->config.dpll.p2;
a0c4da24 5148
89b667f8
JB
5149 /* See eDP HDMI DPIO driver vbios notes doc */
5150
5151 /* PLL B needs special handling */
5152 if (pipe)
5e69f97f 5153 vlv_pllb_recal_opamp(dev_priv, pipe);
89b667f8
JB
5154
5155 /* Set up Tx target for periodic Rcomp update */
ab3c759a 5156 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
89b667f8
JB
5157
5158 /* Disable target IRef on PLL */
ab3c759a 5159 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
89b667f8 5160 reg_val &= 0x00ffffff;
ab3c759a 5161 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
89b667f8
JB
5162
5163 /* Disable fast lock */
ab3c759a 5164 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
89b667f8
JB
5165
5166 /* Set idtafcrecal before PLL is enabled */
a0c4da24
JB
5167 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5168 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5169 mdiv |= ((bestn << DPIO_N_SHIFT));
a0c4da24 5170 mdiv |= (1 << DPIO_K_SHIFT);
7df5080b
JB
5171
5172 /*
5173 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5174 * but we don't support that).
5175 * Note: don't use the DAC post divider as it seems unstable.
5176 */
5177 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
ab3c759a 5178 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5179
a0c4da24 5180 mdiv |= DPIO_ENABLE_CALIBRATION;
ab3c759a 5181 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
a0c4da24 5182
89b667f8 5183 /* Set HBR and RBR LPF coefficients */
ff9a6750 5184 if (crtc->config.port_clock == 162000 ||
99750bd4 5185 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
89b667f8 5186 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
ab3c759a 5187 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
885b0120 5188 0x009f0003);
89b667f8 5189 else
ab3c759a 5190 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
89b667f8
JB
5191 0x00d0000f);
5192
5193 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5194 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5195 /* Use SSC source */
5196 if (!pipe)
ab3c759a 5197 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5198 0x0df40000);
5199 else
ab3c759a 5200 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5201 0x0df70000);
5202 } else { /* HDMI or VGA */
5203 /* Use bend source */
5204 if (!pipe)
ab3c759a 5205 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5206 0x0df70000);
5207 else
ab3c759a 5208 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
89b667f8
JB
5209 0x0df40000);
5210 }
a0c4da24 5211
ab3c759a 5212 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
89b667f8
JB
5213 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5214 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5215 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5216 coreclk |= 0x01000000;
ab3c759a 5217 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
a0c4da24 5218
ab3c759a 5219 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
a0c4da24 5220
e5cbfbfb
ID
5221 /*
5222 * Enable DPIO clock input. We should never disable the reference
5223 * clock for pipe B, since VGA hotplug / manual detection depends
5224 * on it.
5225 */
89b667f8
JB
5226 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5227 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
f6071166
JB
5228 /* We should never disable this, set it here for state tracking */
5229 if (pipe == PIPE_B)
89b667f8 5230 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
a0c4da24 5231 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
5232 crtc->config.dpll_hw_state.dpll = dpll;
5233
ef1b460d
DV
5234 dpll_md = (crtc->config.pixel_multiplier - 1)
5235 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795
DV
5236 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5237
89b667f8
JB
5238 if (crtc->config.has_dp_encoder)
5239 intel_dp_set_m_n(crtc);
09153000
DV
5240
5241 mutex_unlock(&dev_priv->dpio_lock);
a0c4da24
JB
5242}
5243
f47709a9
DV
5244static void i9xx_update_pll(struct intel_crtc *crtc,
5245 intel_clock_t *reduced_clock,
eb1cbe48
DV
5246 int num_connectors)
5247{
f47709a9 5248 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5249 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48
DV
5250 u32 dpll;
5251 bool is_sdvo;
f47709a9 5252 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5253
f47709a9 5254 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5255
f47709a9
DV
5256 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5257 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
eb1cbe48
DV
5258
5259 dpll = DPLL_VGA_MODE_DIS;
5260
f47709a9 5261 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
eb1cbe48
DV
5262 dpll |= DPLLB_MODE_LVDS;
5263 else
5264 dpll |= DPLLB_MODE_DAC_SERIAL;
6cc5f341 5265
ef1b460d 5266 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
198a037f
DV
5267 dpll |= (crtc->config.pixel_multiplier - 1)
5268 << SDVO_MULTIPLIER_SHIFT_HIRES;
eb1cbe48 5269 }
198a037f
DV
5270
5271 if (is_sdvo)
4a33e48d 5272 dpll |= DPLL_SDVO_HIGH_SPEED;
198a037f 5273
f47709a9 5274 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
4a33e48d 5275 dpll |= DPLL_SDVO_HIGH_SPEED;
eb1cbe48
DV
5276
5277 /* compute bitmask from p1 value */
5278 if (IS_PINEVIEW(dev))
5279 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5280 else {
5281 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5282 if (IS_G4X(dev) && reduced_clock)
5283 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5284 }
5285 switch (clock->p2) {
5286 case 5:
5287 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5288 break;
5289 case 7:
5290 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5291 break;
5292 case 10:
5293 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5294 break;
5295 case 14:
5296 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5297 break;
5298 }
5299 if (INTEL_INFO(dev)->gen >= 4)
5300 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5301
09ede541 5302 if (crtc->config.sdvo_tv_clock)
eb1cbe48 5303 dpll |= PLL_REF_INPUT_TVCLKINBC;
f47709a9 5304 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5305 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5306 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5307 else
5308 dpll |= PLL_REF_INPUT_DREFCLK;
5309
5310 dpll |= DPLL_VCO_ENABLE;
8bcc2795
DV
5311 crtc->config.dpll_hw_state.dpll = dpll;
5312
eb1cbe48 5313 if (INTEL_INFO(dev)->gen >= 4) {
ef1b460d
DV
5314 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5315 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
8bcc2795 5316 crtc->config.dpll_hw_state.dpll_md = dpll_md;
eb1cbe48 5317 }
66e3d5c0
DV
5318
5319 if (crtc->config.has_dp_encoder)
5320 intel_dp_set_m_n(crtc);
eb1cbe48
DV
5321}
5322
f47709a9 5323static void i8xx_update_pll(struct intel_crtc *crtc,
f47709a9 5324 intel_clock_t *reduced_clock,
eb1cbe48
DV
5325 int num_connectors)
5326{
f47709a9 5327 struct drm_device *dev = crtc->base.dev;
eb1cbe48 5328 struct drm_i915_private *dev_priv = dev->dev_private;
eb1cbe48 5329 u32 dpll;
f47709a9 5330 struct dpll *clock = &crtc->config.dpll;
eb1cbe48 5331
f47709a9 5332 i9xx_update_pll_dividers(crtc, reduced_clock);
2a8f64ca 5333
eb1cbe48
DV
5334 dpll = DPLL_VGA_MODE_DIS;
5335
f47709a9 5336 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
eb1cbe48
DV
5337 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5338 } else {
5339 if (clock->p1 == 2)
5340 dpll |= PLL_P1_DIVIDE_BY_TWO;
5341 else
5342 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5343 if (clock->p2 == 4)
5344 dpll |= PLL_P2_DIVIDE_BY_4;
5345 }
5346
4a33e48d
DV
5347 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5348 dpll |= DPLL_DVO_2X_MODE;
5349
f47709a9 5350 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
eb1cbe48
DV
5351 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5352 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5353 else
5354 dpll |= PLL_REF_INPUT_DREFCLK;
5355
5356 dpll |= DPLL_VCO_ENABLE;
8bcc2795 5357 crtc->config.dpll_hw_state.dpll = dpll;
eb1cbe48
DV
5358}
5359
8a654f3b 5360static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
b0e77b9c
PZ
5361{
5362 struct drm_device *dev = intel_crtc->base.dev;
5363 struct drm_i915_private *dev_priv = dev->dev_private;
5364 enum pipe pipe = intel_crtc->pipe;
3b117c8f 5365 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
8a654f3b
DV
5366 struct drm_display_mode *adjusted_mode =
5367 &intel_crtc->config.adjusted_mode;
4d8a62ea
DV
5368 uint32_t vsyncshift, crtc_vtotal, crtc_vblank_end;
5369
5370 /* We need to be careful not to changed the adjusted mode, for otherwise
5371 * the hw state checker will get angry at the mismatch. */
5372 crtc_vtotal = adjusted_mode->crtc_vtotal;
5373 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
b0e77b9c
PZ
5374
5375 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5376 /* the chip adds 2 halflines automatically */
4d8a62ea
DV
5377 crtc_vtotal -= 1;
5378 crtc_vblank_end -= 1;
b0e77b9c
PZ
5379 vsyncshift = adjusted_mode->crtc_hsync_start
5380 - adjusted_mode->crtc_htotal / 2;
5381 } else {
5382 vsyncshift = 0;
5383 }
5384
5385 if (INTEL_INFO(dev)->gen > 3)
fe2b8f9d 5386 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
b0e77b9c 5387
fe2b8f9d 5388 I915_WRITE(HTOTAL(cpu_transcoder),
b0e77b9c
PZ
5389 (adjusted_mode->crtc_hdisplay - 1) |
5390 ((adjusted_mode->crtc_htotal - 1) << 16));
fe2b8f9d 5391 I915_WRITE(HBLANK(cpu_transcoder),
b0e77b9c
PZ
5392 (adjusted_mode->crtc_hblank_start - 1) |
5393 ((adjusted_mode->crtc_hblank_end - 1) << 16));
fe2b8f9d 5394 I915_WRITE(HSYNC(cpu_transcoder),
b0e77b9c
PZ
5395 (adjusted_mode->crtc_hsync_start - 1) |
5396 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5397
fe2b8f9d 5398 I915_WRITE(VTOTAL(cpu_transcoder),
b0e77b9c 5399 (adjusted_mode->crtc_vdisplay - 1) |
4d8a62ea 5400 ((crtc_vtotal - 1) << 16));
fe2b8f9d 5401 I915_WRITE(VBLANK(cpu_transcoder),
b0e77b9c 5402 (adjusted_mode->crtc_vblank_start - 1) |
4d8a62ea 5403 ((crtc_vblank_end - 1) << 16));
fe2b8f9d 5404 I915_WRITE(VSYNC(cpu_transcoder),
b0e77b9c
PZ
5405 (adjusted_mode->crtc_vsync_start - 1) |
5406 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5407
b5e508d4
PZ
5408 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5409 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5410 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5411 * bits. */
5412 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5413 (pipe == PIPE_B || pipe == PIPE_C))
5414 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5415
b0e77b9c
PZ
5416 /* pipesrc controls the size that is scaled from, which should
5417 * always be the user's requested size.
5418 */
5419 I915_WRITE(PIPESRC(pipe),
37327abd
VS
5420 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5421 (intel_crtc->config.pipe_src_h - 1));
b0e77b9c
PZ
5422}
5423
1bd1bd80
DV
5424static void intel_get_pipe_timings(struct intel_crtc *crtc,
5425 struct intel_crtc_config *pipe_config)
5426{
5427 struct drm_device *dev = crtc->base.dev;
5428 struct drm_i915_private *dev_priv = dev->dev_private;
5429 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5430 uint32_t tmp;
5431
5432 tmp = I915_READ(HTOTAL(cpu_transcoder));
5433 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5434 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5435 tmp = I915_READ(HBLANK(cpu_transcoder));
5436 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5437 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5438 tmp = I915_READ(HSYNC(cpu_transcoder));
5439 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5440 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5441
5442 tmp = I915_READ(VTOTAL(cpu_transcoder));
5443 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5444 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5445 tmp = I915_READ(VBLANK(cpu_transcoder));
5446 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5447 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5448 tmp = I915_READ(VSYNC(cpu_transcoder));
5449 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5450 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5451
5452 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5453 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5454 pipe_config->adjusted_mode.crtc_vtotal += 1;
5455 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5456 }
5457
5458 tmp = I915_READ(PIPESRC(crtc->pipe));
37327abd
VS
5459 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5460 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5461
5462 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5463 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
1bd1bd80
DV
5464}
5465
f6a83288
DV
5466void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5467 struct intel_crtc_config *pipe_config)
babea61d 5468{
f6a83288
DV
5469 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5470 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
5471 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5472 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
babea61d 5473
f6a83288
DV
5474 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5475 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5476 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5477 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
babea61d 5478
f6a83288 5479 mode->flags = pipe_config->adjusted_mode.flags;
babea61d 5480
f6a83288
DV
5481 mode->clock = pipe_config->adjusted_mode.crtc_clock;
5482 mode->flags |= pipe_config->adjusted_mode.flags;
babea61d
JB
5483}
5484
84b046f3
DV
5485static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5486{
5487 struct drm_device *dev = intel_crtc->base.dev;
5488 struct drm_i915_private *dev_priv = dev->dev_private;
5489 uint32_t pipeconf;
5490
9f11a9e4 5491 pipeconf = 0;
84b046f3 5492
67c72a12
DV
5493 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5494 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5495 pipeconf |= PIPECONF_ENABLE;
5496
cf532bb2
VS
5497 if (intel_crtc->config.double_wide)
5498 pipeconf |= PIPECONF_DOUBLE_WIDE;
84b046f3 5499
ff9ce46e
DV
5500 /* only g4x and later have fancy bpc/dither controls */
5501 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
ff9ce46e
DV
5502 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5503 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5504 pipeconf |= PIPECONF_DITHER_EN |
84b046f3 5505 PIPECONF_DITHER_TYPE_SP;
84b046f3 5506
ff9ce46e
DV
5507 switch (intel_crtc->config.pipe_bpp) {
5508 case 18:
5509 pipeconf |= PIPECONF_6BPC;
5510 break;
5511 case 24:
5512 pipeconf |= PIPECONF_8BPC;
5513 break;
5514 case 30:
5515 pipeconf |= PIPECONF_10BPC;
5516 break;
5517 default:
5518 /* Case prevented by intel_choose_pipe_bpp_dither. */
5519 BUG();
84b046f3
DV
5520 }
5521 }
5522
5523 if (HAS_PIPE_CXSR(dev)) {
5524 if (intel_crtc->lowfreq_avail) {
5525 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5526 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5527 } else {
5528 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
84b046f3
DV
5529 }
5530 }
5531
84b046f3
DV
5532 if (!IS_GEN2(dev) &&
5533 intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
5534 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5535 else
5536 pipeconf |= PIPECONF_PROGRESSIVE;
5537
9f11a9e4
DV
5538 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5539 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
9c8e09b7 5540
84b046f3
DV
5541 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5542 POSTING_READ(PIPECONF(intel_crtc->pipe));
5543}
5544
f564048e 5545static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
f564048e 5546 int x, int y,
94352cf9 5547 struct drm_framebuffer *fb)
79e53945
JB
5548{
5549 struct drm_device *dev = crtc->dev;
5550 struct drm_i915_private *dev_priv = dev->dev_private;
5551 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5552 int pipe = intel_crtc->pipe;
80824003 5553 int plane = intel_crtc->plane;
c751ce4f 5554 int refclk, num_connectors = 0;
652c393a 5555 intel_clock_t clock, reduced_clock;
84b046f3 5556 u32 dspcntr;
a16af721 5557 bool ok, has_reduced_clock = false;
e9fd1c02 5558 bool is_lvds = false, is_dsi = false;
5eddb70b 5559 struct intel_encoder *encoder;
d4906093 5560 const intel_limit_t *limit;
5c3b82e2 5561 int ret;
79e53945 5562
6c2b7c12 5563 for_each_encoder_on_crtc(dev, crtc, encoder) {
5eddb70b 5564 switch (encoder->type) {
79e53945
JB
5565 case INTEL_OUTPUT_LVDS:
5566 is_lvds = true;
5567 break;
e9fd1c02
JN
5568 case INTEL_OUTPUT_DSI:
5569 is_dsi = true;
5570 break;
79e53945 5571 }
43565a06 5572
c751ce4f 5573 num_connectors++;
79e53945
JB
5574 }
5575
f2335330
JN
5576 if (is_dsi)
5577 goto skip_dpll;
5578
5579 if (!intel_crtc->config.clock_set) {
5580 refclk = i9xx_get_refclk(crtc, num_connectors);
79e53945 5581
e9fd1c02
JN
5582 /*
5583 * Returns a set of divisors for the desired target clock with
5584 * the given refclk, or FALSE. The returned values represent
5585 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5586 * 2) / p1 / p2.
5587 */
5588 limit = intel_limit(crtc, refclk);
5589 ok = dev_priv->display.find_dpll(limit, crtc,
5590 intel_crtc->config.port_clock,
5591 refclk, NULL, &clock);
f2335330 5592 if (!ok) {
e9fd1c02
JN
5593 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5594 return -EINVAL;
5595 }
79e53945 5596
f2335330
JN
5597 if (is_lvds && dev_priv->lvds_downclock_avail) {
5598 /*
5599 * Ensure we match the reduced clock's P to the target
5600 * clock. If the clocks don't match, we can't switch
5601 * the display clock by using the FP0/FP1. In such case
5602 * we will disable the LVDS downclock feature.
5603 */
5604 has_reduced_clock =
5605 dev_priv->display.find_dpll(limit, crtc,
5606 dev_priv->lvds_downclock,
5607 refclk, &clock,
5608 &reduced_clock);
5609 }
5610 /* Compat-code for transition, will disappear. */
f47709a9
DV
5611 intel_crtc->config.dpll.n = clock.n;
5612 intel_crtc->config.dpll.m1 = clock.m1;
5613 intel_crtc->config.dpll.m2 = clock.m2;
5614 intel_crtc->config.dpll.p1 = clock.p1;
5615 intel_crtc->config.dpll.p2 = clock.p2;
5616 }
7026d4ac 5617
e9fd1c02 5618 if (IS_GEN2(dev)) {
8a654f3b 5619 i8xx_update_pll(intel_crtc,
2a8f64ca
VP
5620 has_reduced_clock ? &reduced_clock : NULL,
5621 num_connectors);
e9fd1c02 5622 } else if (IS_VALLEYVIEW(dev)) {
f2335330 5623 vlv_update_pll(intel_crtc);
e9fd1c02 5624 } else {
f47709a9 5625 i9xx_update_pll(intel_crtc,
eb1cbe48 5626 has_reduced_clock ? &reduced_clock : NULL,
89b667f8 5627 num_connectors);
e9fd1c02 5628 }
79e53945 5629
f2335330 5630skip_dpll:
79e53945
JB
5631 /* Set up the display plane register */
5632 dspcntr = DISPPLANE_GAMMA_ENABLE;
5633
da6ecc5d
JB
5634 if (!IS_VALLEYVIEW(dev)) {
5635 if (pipe == 0)
5636 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5637 else
5638 dspcntr |= DISPPLANE_SEL_PIPE_B;
5639 }
79e53945 5640
8a654f3b 5641 intel_set_pipe_timings(intel_crtc);
5eddb70b
CW
5642
5643 /* pipesrc and dspsize control the size that is scaled from,
5644 * which should always be the user's requested size.
79e53945 5645 */
929c77fb 5646 I915_WRITE(DSPSIZE(plane),
37327abd
VS
5647 ((intel_crtc->config.pipe_src_h - 1) << 16) |
5648 (intel_crtc->config.pipe_src_w - 1));
929c77fb 5649 I915_WRITE(DSPPOS(plane), 0);
2c07245f 5650
84b046f3
DV
5651 i9xx_set_pipeconf(intel_crtc);
5652
f564048e
EA
5653 I915_WRITE(DSPCNTR(plane), dspcntr);
5654 POSTING_READ(DSPCNTR(plane));
5655
94352cf9 5656 ret = intel_pipe_set_base(crtc, x, y, fb);
f564048e 5657
f564048e
EA
5658 return ret;
5659}
5660
2fa2fe9a
DV
5661static void i9xx_get_pfit_config(struct intel_crtc *crtc,
5662 struct intel_crtc_config *pipe_config)
5663{
5664 struct drm_device *dev = crtc->base.dev;
5665 struct drm_i915_private *dev_priv = dev->dev_private;
5666 uint32_t tmp;
5667
dc9e7dec
VS
5668 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
5669 return;
5670
2fa2fe9a 5671 tmp = I915_READ(PFIT_CONTROL);
06922821
DV
5672 if (!(tmp & PFIT_ENABLE))
5673 return;
2fa2fe9a 5674
06922821 5675 /* Check whether the pfit is attached to our pipe. */
2fa2fe9a
DV
5676 if (INTEL_INFO(dev)->gen < 4) {
5677 if (crtc->pipe != PIPE_B)
5678 return;
2fa2fe9a
DV
5679 } else {
5680 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5681 return;
5682 }
5683
06922821 5684 pipe_config->gmch_pfit.control = tmp;
2fa2fe9a
DV
5685 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
5686 if (INTEL_INFO(dev)->gen < 5)
5687 pipe_config->gmch_pfit.lvds_border_bits =
5688 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
5689}
5690
acbec814
JB
5691static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5692 struct intel_crtc_config *pipe_config)
5693{
5694 struct drm_device *dev = crtc->base.dev;
5695 struct drm_i915_private *dev_priv = dev->dev_private;
5696 int pipe = pipe_config->cpu_transcoder;
5697 intel_clock_t clock;
5698 u32 mdiv;
662c6ecb 5699 int refclk = 100000;
acbec814
JB
5700
5701 mutex_lock(&dev_priv->dpio_lock);
ab3c759a 5702 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
acbec814
JB
5703 mutex_unlock(&dev_priv->dpio_lock);
5704
5705 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5706 clock.m2 = mdiv & DPIO_M2DIV_MASK;
5707 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5708 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5709 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5710
f646628b 5711 vlv_clock(refclk, &clock);
acbec814 5712
f646628b
VS
5713 /* clock.dot is the fast clock */
5714 pipe_config->port_clock = clock.dot / 5;
acbec814
JB
5715}
5716
1ad292b5
JB
5717static void i9xx_get_plane_config(struct intel_crtc *crtc,
5718 struct intel_plane_config *plane_config)
5719{
5720 struct drm_device *dev = crtc->base.dev;
5721 struct drm_i915_private *dev_priv = dev->dev_private;
5722 u32 val, base, offset;
5723 int pipe = crtc->pipe, plane = crtc->plane;
5724 int fourcc, pixel_format;
5725 int aligned_height;
5726
484b41dd
JB
5727 crtc->base.fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
5728 if (!crtc->base.fb) {
1ad292b5
JB
5729 DRM_DEBUG_KMS("failed to alloc fb\n");
5730 return;
5731 }
5732
5733 val = I915_READ(DSPCNTR(plane));
5734
5735 if (INTEL_INFO(dev)->gen >= 4)
5736 if (val & DISPPLANE_TILED)
5737 plane_config->tiled = true;
5738
5739 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
5740 fourcc = intel_format_to_fourcc(pixel_format);
484b41dd
JB
5741 crtc->base.fb->pixel_format = fourcc;
5742 crtc->base.fb->bits_per_pixel =
1ad292b5
JB
5743 drm_format_plane_cpp(fourcc, 0) * 8;
5744
5745 if (INTEL_INFO(dev)->gen >= 4) {
5746 if (plane_config->tiled)
5747 offset = I915_READ(DSPTILEOFF(plane));
5748 else
5749 offset = I915_READ(DSPLINOFF(plane));
5750 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
5751 } else {
5752 base = I915_READ(DSPADDR(plane));
5753 }
5754 plane_config->base = base;
5755
5756 val = I915_READ(PIPESRC(pipe));
484b41dd
JB
5757 crtc->base.fb->width = ((val >> 16) & 0xfff) + 1;
5758 crtc->base.fb->height = ((val >> 0) & 0xfff) + 1;
1ad292b5
JB
5759
5760 val = I915_READ(DSPSTRIDE(pipe));
484b41dd 5761 crtc->base.fb->pitches[0] = val & 0xffffff80;
1ad292b5 5762
484b41dd 5763 aligned_height = intel_align_height(dev, crtc->base.fb->height,
1ad292b5
JB
5764 plane_config->tiled);
5765
484b41dd 5766 plane_config->size = ALIGN(crtc->base.fb->pitches[0] *
1ad292b5
JB
5767 aligned_height, PAGE_SIZE);
5768
5769 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
484b41dd
JB
5770 pipe, plane, crtc->base.fb->width,
5771 crtc->base.fb->height,
5772 crtc->base.fb->bits_per_pixel, base,
5773 crtc->base.fb->pitches[0],
1ad292b5
JB
5774 plane_config->size);
5775
5776}
5777
0e8ffe1b
DV
5778static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5779 struct intel_crtc_config *pipe_config)
5780{
5781 struct drm_device *dev = crtc->base.dev;
5782 struct drm_i915_private *dev_priv = dev->dev_private;
5783 uint32_t tmp;
5784
b5482bd0
ID
5785 if (!intel_display_power_enabled(dev_priv,
5786 POWER_DOMAIN_PIPE(crtc->pipe)))
5787 return false;
5788
e143a21c 5789 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 5790 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 5791
0e8ffe1b
DV
5792 tmp = I915_READ(PIPECONF(crtc->pipe));
5793 if (!(tmp & PIPECONF_ENABLE))
5794 return false;
5795
42571aef
VS
5796 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
5797 switch (tmp & PIPECONF_BPC_MASK) {
5798 case PIPECONF_6BPC:
5799 pipe_config->pipe_bpp = 18;
5800 break;
5801 case PIPECONF_8BPC:
5802 pipe_config->pipe_bpp = 24;
5803 break;
5804 case PIPECONF_10BPC:
5805 pipe_config->pipe_bpp = 30;
5806 break;
5807 default:
5808 break;
5809 }
5810 }
5811
282740f7
VS
5812 if (INTEL_INFO(dev)->gen < 4)
5813 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5814
1bd1bd80
DV
5815 intel_get_pipe_timings(crtc, pipe_config);
5816
2fa2fe9a
DV
5817 i9xx_get_pfit_config(crtc, pipe_config);
5818
6c49f241
DV
5819 if (INTEL_INFO(dev)->gen >= 4) {
5820 tmp = I915_READ(DPLL_MD(crtc->pipe));
5821 pipe_config->pixel_multiplier =
5822 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5823 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
8bcc2795 5824 pipe_config->dpll_hw_state.dpll_md = tmp;
6c49f241
DV
5825 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
5826 tmp = I915_READ(DPLL(crtc->pipe));
5827 pipe_config->pixel_multiplier =
5828 ((tmp & SDVO_MULTIPLIER_MASK)
5829 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5830 } else {
5831 /* Note that on i915G/GM the pixel multiplier is in the sdvo
5832 * port and will be fixed up in the encoder->get_config
5833 * function. */
5834 pipe_config->pixel_multiplier = 1;
5835 }
8bcc2795
DV
5836 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
5837 if (!IS_VALLEYVIEW(dev)) {
5838 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
5839 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
165e901c
VS
5840 } else {
5841 /* Mask out read-only status bits. */
5842 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5843 DPLL_PORTC_READY_MASK |
5844 DPLL_PORTB_READY_MASK);
8bcc2795 5845 }
6c49f241 5846
acbec814
JB
5847 if (IS_VALLEYVIEW(dev))
5848 vlv_crtc_clock_get(crtc, pipe_config);
5849 else
5850 i9xx_crtc_clock_get(crtc, pipe_config);
18442d08 5851
0e8ffe1b
DV
5852 return true;
5853}
5854
dde86e2d 5855static void ironlake_init_pch_refclk(struct drm_device *dev)
13d83a67
JB
5856{
5857 struct drm_i915_private *dev_priv = dev->dev_private;
5858 struct drm_mode_config *mode_config = &dev->mode_config;
13d83a67 5859 struct intel_encoder *encoder;
74cfd7ac 5860 u32 val, final;
13d83a67 5861 bool has_lvds = false;
199e5d79 5862 bool has_cpu_edp = false;
199e5d79 5863 bool has_panel = false;
99eb6a01
KP
5864 bool has_ck505 = false;
5865 bool can_ssc = false;
13d83a67
JB
5866
5867 /* We need to take the global config into account */
199e5d79
KP
5868 list_for_each_entry(encoder, &mode_config->encoder_list,
5869 base.head) {
5870 switch (encoder->type) {
5871 case INTEL_OUTPUT_LVDS:
5872 has_panel = true;
5873 has_lvds = true;
5874 break;
5875 case INTEL_OUTPUT_EDP:
5876 has_panel = true;
2de6905f 5877 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
199e5d79
KP
5878 has_cpu_edp = true;
5879 break;
13d83a67
JB
5880 }
5881 }
5882
99eb6a01 5883 if (HAS_PCH_IBX(dev)) {
41aa3448 5884 has_ck505 = dev_priv->vbt.display_clock_mode;
99eb6a01
KP
5885 can_ssc = has_ck505;
5886 } else {
5887 has_ck505 = false;
5888 can_ssc = true;
5889 }
5890
2de6905f
ID
5891 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
5892 has_panel, has_lvds, has_ck505);
13d83a67
JB
5893
5894 /* Ironlake: try to setup display ref clock before DPLL
5895 * enabling. This is only under driver's control after
5896 * PCH B stepping, previous chipset stepping should be
5897 * ignoring this setting.
5898 */
74cfd7ac
CW
5899 val = I915_READ(PCH_DREF_CONTROL);
5900
5901 /* As we must carefully and slowly disable/enable each source in turn,
5902 * compute the final state we want first and check if we need to
5903 * make any changes at all.
5904 */
5905 final = val;
5906 final &= ~DREF_NONSPREAD_SOURCE_MASK;
5907 if (has_ck505)
5908 final |= DREF_NONSPREAD_CK505_ENABLE;
5909 else
5910 final |= DREF_NONSPREAD_SOURCE_ENABLE;
5911
5912 final &= ~DREF_SSC_SOURCE_MASK;
5913 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5914 final &= ~DREF_SSC1_ENABLE;
5915
5916 if (has_panel) {
5917 final |= DREF_SSC_SOURCE_ENABLE;
5918
5919 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5920 final |= DREF_SSC1_ENABLE;
5921
5922 if (has_cpu_edp) {
5923 if (intel_panel_use_ssc(dev_priv) && can_ssc)
5924 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5925 else
5926 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5927 } else
5928 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5929 } else {
5930 final |= DREF_SSC_SOURCE_DISABLE;
5931 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5932 }
5933
5934 if (final == val)
5935 return;
5936
13d83a67 5937 /* Always enable nonspread source */
74cfd7ac 5938 val &= ~DREF_NONSPREAD_SOURCE_MASK;
13d83a67 5939
99eb6a01 5940 if (has_ck505)
74cfd7ac 5941 val |= DREF_NONSPREAD_CK505_ENABLE;
99eb6a01 5942 else
74cfd7ac 5943 val |= DREF_NONSPREAD_SOURCE_ENABLE;
13d83a67 5944
199e5d79 5945 if (has_panel) {
74cfd7ac
CW
5946 val &= ~DREF_SSC_SOURCE_MASK;
5947 val |= DREF_SSC_SOURCE_ENABLE;
13d83a67 5948
199e5d79 5949 /* SSC must be turned on before enabling the CPU output */
99eb6a01 5950 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 5951 DRM_DEBUG_KMS("Using SSC on panel\n");
74cfd7ac 5952 val |= DREF_SSC1_ENABLE;
e77166b5 5953 } else
74cfd7ac 5954 val &= ~DREF_SSC1_ENABLE;
199e5d79
KP
5955
5956 /* Get SSC going before enabling the outputs */
74cfd7ac 5957 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
5958 POSTING_READ(PCH_DREF_CONTROL);
5959 udelay(200);
5960
74cfd7ac 5961 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
13d83a67
JB
5962
5963 /* Enable CPU source on CPU attached eDP */
199e5d79 5964 if (has_cpu_edp) {
99eb6a01 5965 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
199e5d79 5966 DRM_DEBUG_KMS("Using SSC on eDP\n");
74cfd7ac 5967 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
199e5d79 5968 }
13d83a67 5969 else
74cfd7ac 5970 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
199e5d79 5971 } else
74cfd7ac 5972 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 5973
74cfd7ac 5974 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
5975 POSTING_READ(PCH_DREF_CONTROL);
5976 udelay(200);
5977 } else {
5978 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5979
74cfd7ac 5980 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
199e5d79
KP
5981
5982 /* Turn off CPU output */
74cfd7ac 5983 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
199e5d79 5984
74cfd7ac 5985 I915_WRITE(PCH_DREF_CONTROL, val);
199e5d79
KP
5986 POSTING_READ(PCH_DREF_CONTROL);
5987 udelay(200);
5988
5989 /* Turn off the SSC source */
74cfd7ac
CW
5990 val &= ~DREF_SSC_SOURCE_MASK;
5991 val |= DREF_SSC_SOURCE_DISABLE;
199e5d79
KP
5992
5993 /* Turn off SSC1 */
74cfd7ac 5994 val &= ~DREF_SSC1_ENABLE;
199e5d79 5995
74cfd7ac 5996 I915_WRITE(PCH_DREF_CONTROL, val);
13d83a67
JB
5997 POSTING_READ(PCH_DREF_CONTROL);
5998 udelay(200);
5999 }
74cfd7ac
CW
6000
6001 BUG_ON(val != final);
13d83a67
JB
6002}
6003
f31f2d55 6004static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
dde86e2d 6005{
f31f2d55 6006 uint32_t tmp;
dde86e2d 6007
0ff066a9
PZ
6008 tmp = I915_READ(SOUTH_CHICKEN2);
6009 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6010 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6011
0ff066a9
PZ
6012 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6013 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6014 DRM_ERROR("FDI mPHY reset assert timeout\n");
dde86e2d 6015
0ff066a9
PZ
6016 tmp = I915_READ(SOUTH_CHICKEN2);
6017 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6018 I915_WRITE(SOUTH_CHICKEN2, tmp);
dde86e2d 6019
0ff066a9
PZ
6020 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6021 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6022 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
f31f2d55
PZ
6023}
6024
6025/* WaMPhyProgramming:hsw */
6026static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6027{
6028 uint32_t tmp;
dde86e2d
PZ
6029
6030 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6031 tmp &= ~(0xFF << 24);
6032 tmp |= (0x12 << 24);
6033 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6034
dde86e2d
PZ
6035 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6036 tmp |= (1 << 11);
6037 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6038
6039 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6040 tmp |= (1 << 11);
6041 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6042
dde86e2d
PZ
6043 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6044 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6045 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6046
6047 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6048 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6049 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6050
0ff066a9
PZ
6051 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6052 tmp &= ~(7 << 13);
6053 tmp |= (5 << 13);
6054 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
dde86e2d 6055
0ff066a9
PZ
6056 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6057 tmp &= ~(7 << 13);
6058 tmp |= (5 << 13);
6059 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
dde86e2d
PZ
6060
6061 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6062 tmp &= ~0xFF;
6063 tmp |= 0x1C;
6064 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6065
6066 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6067 tmp &= ~0xFF;
6068 tmp |= 0x1C;
6069 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6070
6071 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6072 tmp &= ~(0xFF << 16);
6073 tmp |= (0x1C << 16);
6074 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6075
6076 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6077 tmp &= ~(0xFF << 16);
6078 tmp |= (0x1C << 16);
6079 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6080
0ff066a9
PZ
6081 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6082 tmp |= (1 << 27);
6083 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
dde86e2d 6084
0ff066a9
PZ
6085 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6086 tmp |= (1 << 27);
6087 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
dde86e2d 6088
0ff066a9
PZ
6089 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6090 tmp &= ~(0xF << 28);
6091 tmp |= (4 << 28);
6092 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
dde86e2d 6093
0ff066a9
PZ
6094 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6095 tmp &= ~(0xF << 28);
6096 tmp |= (4 << 28);
6097 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
f31f2d55
PZ
6098}
6099
2fa86a1f
PZ
6100/* Implements 3 different sequences from BSpec chapter "Display iCLK
6101 * Programming" based on the parameters passed:
6102 * - Sequence to enable CLKOUT_DP
6103 * - Sequence to enable CLKOUT_DP without spread
6104 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6105 */
6106static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6107 bool with_fdi)
f31f2d55
PZ
6108{
6109 struct drm_i915_private *dev_priv = dev->dev_private;
2fa86a1f
PZ
6110 uint32_t reg, tmp;
6111
6112 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6113 with_spread = true;
6114 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6115 with_fdi, "LP PCH doesn't have FDI\n"))
6116 with_fdi = false;
f31f2d55
PZ
6117
6118 mutex_lock(&dev_priv->dpio_lock);
6119
6120 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6121 tmp &= ~SBI_SSCCTL_DISABLE;
6122 tmp |= SBI_SSCCTL_PATHALT;
6123 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6124
6125 udelay(24);
6126
2fa86a1f
PZ
6127 if (with_spread) {
6128 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6129 tmp &= ~SBI_SSCCTL_PATHALT;
6130 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
f31f2d55 6131
2fa86a1f
PZ
6132 if (with_fdi) {
6133 lpt_reset_fdi_mphy(dev_priv);
6134 lpt_program_fdi_mphy(dev_priv);
6135 }
6136 }
dde86e2d 6137
2fa86a1f
PZ
6138 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6139 SBI_GEN0 : SBI_DBUFF0;
6140 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6141 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6142 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
c00db246
DV
6143
6144 mutex_unlock(&dev_priv->dpio_lock);
dde86e2d
PZ
6145}
6146
47701c3b
PZ
6147/* Sequence to disable CLKOUT_DP */
6148static void lpt_disable_clkout_dp(struct drm_device *dev)
6149{
6150 struct drm_i915_private *dev_priv = dev->dev_private;
6151 uint32_t reg, tmp;
6152
6153 mutex_lock(&dev_priv->dpio_lock);
6154
6155 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6156 SBI_GEN0 : SBI_DBUFF0;
6157 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6158 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6159 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6160
6161 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6162 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6163 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6164 tmp |= SBI_SSCCTL_PATHALT;
6165 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6166 udelay(32);
6167 }
6168 tmp |= SBI_SSCCTL_DISABLE;
6169 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6170 }
6171
6172 mutex_unlock(&dev_priv->dpio_lock);
6173}
6174
bf8fa3d3
PZ
6175static void lpt_init_pch_refclk(struct drm_device *dev)
6176{
6177 struct drm_mode_config *mode_config = &dev->mode_config;
6178 struct intel_encoder *encoder;
6179 bool has_vga = false;
6180
6181 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
6182 switch (encoder->type) {
6183 case INTEL_OUTPUT_ANALOG:
6184 has_vga = true;
6185 break;
6186 }
6187 }
6188
47701c3b
PZ
6189 if (has_vga)
6190 lpt_enable_clkout_dp(dev, true, true);
6191 else
6192 lpt_disable_clkout_dp(dev);
bf8fa3d3
PZ
6193}
6194
dde86e2d
PZ
6195/*
6196 * Initialize reference clocks when the driver loads
6197 */
6198void intel_init_pch_refclk(struct drm_device *dev)
6199{
6200 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6201 ironlake_init_pch_refclk(dev);
6202 else if (HAS_PCH_LPT(dev))
6203 lpt_init_pch_refclk(dev);
6204}
6205
d9d444cb
JB
6206static int ironlake_get_refclk(struct drm_crtc *crtc)
6207{
6208 struct drm_device *dev = crtc->dev;
6209 struct drm_i915_private *dev_priv = dev->dev_private;
6210 struct intel_encoder *encoder;
d9d444cb
JB
6211 int num_connectors = 0;
6212 bool is_lvds = false;
6213
6c2b7c12 6214 for_each_encoder_on_crtc(dev, crtc, encoder) {
d9d444cb
JB
6215 switch (encoder->type) {
6216 case INTEL_OUTPUT_LVDS:
6217 is_lvds = true;
6218 break;
d9d444cb
JB
6219 }
6220 num_connectors++;
6221 }
6222
6223 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
e91e941b 6224 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
41aa3448 6225 dev_priv->vbt.lvds_ssc_freq);
e91e941b 6226 return dev_priv->vbt.lvds_ssc_freq;
d9d444cb
JB
6227 }
6228
6229 return 120000;
6230}
6231
6ff93609 6232static void ironlake_set_pipeconf(struct drm_crtc *crtc)
79e53945 6233{
c8203565 6234 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
79e53945
JB
6235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6236 int pipe = intel_crtc->pipe;
c8203565
PZ
6237 uint32_t val;
6238
78114071 6239 val = 0;
c8203565 6240
965e0c48 6241 switch (intel_crtc->config.pipe_bpp) {
c8203565 6242 case 18:
dfd07d72 6243 val |= PIPECONF_6BPC;
c8203565
PZ
6244 break;
6245 case 24:
dfd07d72 6246 val |= PIPECONF_8BPC;
c8203565
PZ
6247 break;
6248 case 30:
dfd07d72 6249 val |= PIPECONF_10BPC;
c8203565
PZ
6250 break;
6251 case 36:
dfd07d72 6252 val |= PIPECONF_12BPC;
c8203565
PZ
6253 break;
6254 default:
cc769b62
PZ
6255 /* Case prevented by intel_choose_pipe_bpp_dither. */
6256 BUG();
c8203565
PZ
6257 }
6258
d8b32247 6259 if (intel_crtc->config.dither)
c8203565
PZ
6260 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6261
6ff93609 6262 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
c8203565
PZ
6263 val |= PIPECONF_INTERLACED_ILK;
6264 else
6265 val |= PIPECONF_PROGRESSIVE;
6266
50f3b016 6267 if (intel_crtc->config.limited_color_range)
3685a8f3 6268 val |= PIPECONF_COLOR_RANGE_SELECT;
3685a8f3 6269
c8203565
PZ
6270 I915_WRITE(PIPECONF(pipe), val);
6271 POSTING_READ(PIPECONF(pipe));
6272}
6273
86d3efce
VS
6274/*
6275 * Set up the pipe CSC unit.
6276 *
6277 * Currently only full range RGB to limited range RGB conversion
6278 * is supported, but eventually this should handle various
6279 * RGB<->YCbCr scenarios as well.
6280 */
50f3b016 6281static void intel_set_pipe_csc(struct drm_crtc *crtc)
86d3efce
VS
6282{
6283 struct drm_device *dev = crtc->dev;
6284 struct drm_i915_private *dev_priv = dev->dev_private;
6285 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6286 int pipe = intel_crtc->pipe;
6287 uint16_t coeff = 0x7800; /* 1.0 */
6288
6289 /*
6290 * TODO: Check what kind of values actually come out of the pipe
6291 * with these coeff/postoff values and adjust to get the best
6292 * accuracy. Perhaps we even need to take the bpc value into
6293 * consideration.
6294 */
6295
50f3b016 6296 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6297 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6298
6299 /*
6300 * GY/GU and RY/RU should be the other way around according
6301 * to BSpec, but reality doesn't agree. Just set them up in
6302 * a way that results in the correct picture.
6303 */
6304 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6305 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6306
6307 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6308 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6309
6310 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6311 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6312
6313 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6314 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6315 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6316
6317 if (INTEL_INFO(dev)->gen > 6) {
6318 uint16_t postoff = 0;
6319
50f3b016 6320 if (intel_crtc->config.limited_color_range)
32cf0cb0 6321 postoff = (16 * (1 << 12) / 255) & 0x1fff;
86d3efce
VS
6322
6323 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6324 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6325 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6326
6327 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6328 } else {
6329 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6330
50f3b016 6331 if (intel_crtc->config.limited_color_range)
86d3efce
VS
6332 mode |= CSC_BLACK_SCREEN_OFFSET;
6333
6334 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6335 }
6336}
6337
6ff93609 6338static void haswell_set_pipeconf(struct drm_crtc *crtc)
ee2b0b38 6339{
756f85cf
PZ
6340 struct drm_device *dev = crtc->dev;
6341 struct drm_i915_private *dev_priv = dev->dev_private;
ee2b0b38 6342 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
756f85cf 6343 enum pipe pipe = intel_crtc->pipe;
3b117c8f 6344 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
ee2b0b38
PZ
6345 uint32_t val;
6346
3eff4faa 6347 val = 0;
ee2b0b38 6348
756f85cf 6349 if (IS_HASWELL(dev) && intel_crtc->config.dither)
ee2b0b38
PZ
6350 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6351
6ff93609 6352 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
ee2b0b38
PZ
6353 val |= PIPECONF_INTERLACED_ILK;
6354 else
6355 val |= PIPECONF_PROGRESSIVE;
6356
702e7a56
PZ
6357 I915_WRITE(PIPECONF(cpu_transcoder), val);
6358 POSTING_READ(PIPECONF(cpu_transcoder));
3eff4faa
DV
6359
6360 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6361 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
756f85cf
PZ
6362
6363 if (IS_BROADWELL(dev)) {
6364 val = 0;
6365
6366 switch (intel_crtc->config.pipe_bpp) {
6367 case 18:
6368 val |= PIPEMISC_DITHER_6_BPC;
6369 break;
6370 case 24:
6371 val |= PIPEMISC_DITHER_8_BPC;
6372 break;
6373 case 30:
6374 val |= PIPEMISC_DITHER_10_BPC;
6375 break;
6376 case 36:
6377 val |= PIPEMISC_DITHER_12_BPC;
6378 break;
6379 default:
6380 /* Case prevented by pipe_config_set_bpp. */
6381 BUG();
6382 }
6383
6384 if (intel_crtc->config.dither)
6385 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6386
6387 I915_WRITE(PIPEMISC(pipe), val);
6388 }
ee2b0b38
PZ
6389}
6390
6591c6e4 6391static bool ironlake_compute_clocks(struct drm_crtc *crtc,
6591c6e4
PZ
6392 intel_clock_t *clock,
6393 bool *has_reduced_clock,
6394 intel_clock_t *reduced_clock)
6395{
6396 struct drm_device *dev = crtc->dev;
6397 struct drm_i915_private *dev_priv = dev->dev_private;
6398 struct intel_encoder *intel_encoder;
6399 int refclk;
d4906093 6400 const intel_limit_t *limit;
a16af721 6401 bool ret, is_lvds = false;
79e53945 6402
6591c6e4
PZ
6403 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6404 switch (intel_encoder->type) {
79e53945
JB
6405 case INTEL_OUTPUT_LVDS:
6406 is_lvds = true;
6407 break;
79e53945
JB
6408 }
6409 }
6410
d9d444cb 6411 refclk = ironlake_get_refclk(crtc);
79e53945 6412
d4906093
ML
6413 /*
6414 * Returns a set of divisors for the desired target clock with the given
6415 * refclk, or FALSE. The returned values represent the clock equation:
6416 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6417 */
1b894b59 6418 limit = intel_limit(crtc, refclk);
ff9a6750
DV
6419 ret = dev_priv->display.find_dpll(limit, crtc,
6420 to_intel_crtc(crtc)->config.port_clock,
ee9300bb 6421 refclk, NULL, clock);
6591c6e4
PZ
6422 if (!ret)
6423 return false;
cda4b7d3 6424
ddc9003c 6425 if (is_lvds && dev_priv->lvds_downclock_avail) {
cec2f356
SP
6426 /*
6427 * Ensure we match the reduced clock's P to the target clock.
6428 * If the clocks don't match, we can't switch the display clock
6429 * by using the FP0/FP1. In such case we will disable the LVDS
6430 * downclock feature.
6431 */
ee9300bb
DV
6432 *has_reduced_clock =
6433 dev_priv->display.find_dpll(limit, crtc,
6434 dev_priv->lvds_downclock,
6435 refclk, clock,
6436 reduced_clock);
652c393a 6437 }
61e9653f 6438
6591c6e4
PZ
6439 return true;
6440}
6441
d4b1931c
PZ
6442int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6443{
6444 /*
6445 * Account for spread spectrum to avoid
6446 * oversubscribing the link. Max center spread
6447 * is 2.5%; use 5% for safety's sake.
6448 */
6449 u32 bps = target_clock * bpp * 21 / 20;
619d4d04 6450 return DIV_ROUND_UP(bps, link_bw * 8);
d4b1931c
PZ
6451}
6452
7429e9d4 6453static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
6cf86a5e 6454{
7429e9d4 6455 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
f48d8f23
PZ
6456}
6457
de13a2e3 6458static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
7429e9d4 6459 u32 *fp,
9a7c7890 6460 intel_clock_t *reduced_clock, u32 *fp2)
79e53945 6461{
de13a2e3 6462 struct drm_crtc *crtc = &intel_crtc->base;
79e53945
JB
6463 struct drm_device *dev = crtc->dev;
6464 struct drm_i915_private *dev_priv = dev->dev_private;
de13a2e3
PZ
6465 struct intel_encoder *intel_encoder;
6466 uint32_t dpll;
6cc5f341 6467 int factor, num_connectors = 0;
09ede541 6468 bool is_lvds = false, is_sdvo = false;
79e53945 6469
de13a2e3
PZ
6470 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6471 switch (intel_encoder->type) {
79e53945
JB
6472 case INTEL_OUTPUT_LVDS:
6473 is_lvds = true;
6474 break;
6475 case INTEL_OUTPUT_SDVO:
7d57382e 6476 case INTEL_OUTPUT_HDMI:
79e53945 6477 is_sdvo = true;
79e53945 6478 break;
79e53945 6479 }
43565a06 6480
c751ce4f 6481 num_connectors++;
79e53945 6482 }
79e53945 6483
c1858123 6484 /* Enable autotuning of the PLL clock (if permissible) */
8febb297
EA
6485 factor = 21;
6486 if (is_lvds) {
6487 if ((intel_panel_use_ssc(dev_priv) &&
e91e941b 6488 dev_priv->vbt.lvds_ssc_freq == 100000) ||
f0b44056 6489 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
8febb297 6490 factor = 25;
09ede541 6491 } else if (intel_crtc->config.sdvo_tv_clock)
8febb297 6492 factor = 20;
c1858123 6493
7429e9d4 6494 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
7d0ac5b7 6495 *fp |= FP_CB_TUNE;
2c07245f 6496
9a7c7890
DV
6497 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6498 *fp2 |= FP_CB_TUNE;
6499
5eddb70b 6500 dpll = 0;
2c07245f 6501
a07d6787
EA
6502 if (is_lvds)
6503 dpll |= DPLLB_MODE_LVDS;
6504 else
6505 dpll |= DPLLB_MODE_DAC_SERIAL;
198a037f 6506
ef1b460d
DV
6507 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6508 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
198a037f
DV
6509
6510 if (is_sdvo)
4a33e48d 6511 dpll |= DPLL_SDVO_HIGH_SPEED;
9566e9af 6512 if (intel_crtc->config.has_dp_encoder)
4a33e48d 6513 dpll |= DPLL_SDVO_HIGH_SPEED;
79e53945 6514
a07d6787 6515 /* compute bitmask from p1 value */
7429e9d4 6516 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
a07d6787 6517 /* also FPA1 */
7429e9d4 6518 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
a07d6787 6519
7429e9d4 6520 switch (intel_crtc->config.dpll.p2) {
a07d6787
EA
6521 case 5:
6522 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6523 break;
6524 case 7:
6525 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6526 break;
6527 case 10:
6528 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6529 break;
6530 case 14:
6531 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6532 break;
79e53945
JB
6533 }
6534
b4c09f3b 6535 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
43565a06 6536 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
79e53945
JB
6537 else
6538 dpll |= PLL_REF_INPUT_DREFCLK;
6539
959e16d6 6540 return dpll | DPLL_VCO_ENABLE;
de13a2e3
PZ
6541}
6542
6543static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
de13a2e3
PZ
6544 int x, int y,
6545 struct drm_framebuffer *fb)
6546{
6547 struct drm_device *dev = crtc->dev;
6548 struct drm_i915_private *dev_priv = dev->dev_private;
6549 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6550 int pipe = intel_crtc->pipe;
6551 int plane = intel_crtc->plane;
6552 int num_connectors = 0;
6553 intel_clock_t clock, reduced_clock;
cbbab5bd 6554 u32 dpll = 0, fp = 0, fp2 = 0;
e2f12b07 6555 bool ok, has_reduced_clock = false;
8b47047b 6556 bool is_lvds = false;
de13a2e3 6557 struct intel_encoder *encoder;
e2b78267 6558 struct intel_shared_dpll *pll;
de13a2e3 6559 int ret;
de13a2e3
PZ
6560
6561 for_each_encoder_on_crtc(dev, crtc, encoder) {
6562 switch (encoder->type) {
6563 case INTEL_OUTPUT_LVDS:
6564 is_lvds = true;
6565 break;
de13a2e3
PZ
6566 }
6567
6568 num_connectors++;
a07d6787 6569 }
79e53945 6570
5dc5298b
PZ
6571 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6572 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
a07d6787 6573
ff9a6750 6574 ok = ironlake_compute_clocks(crtc, &clock,
de13a2e3 6575 &has_reduced_clock, &reduced_clock);
ee9300bb 6576 if (!ok && !intel_crtc->config.clock_set) {
de13a2e3
PZ
6577 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6578 return -EINVAL;
79e53945 6579 }
f47709a9
DV
6580 /* Compat-code for transition, will disappear. */
6581 if (!intel_crtc->config.clock_set) {
6582 intel_crtc->config.dpll.n = clock.n;
6583 intel_crtc->config.dpll.m1 = clock.m1;
6584 intel_crtc->config.dpll.m2 = clock.m2;
6585 intel_crtc->config.dpll.p1 = clock.p1;
6586 intel_crtc->config.dpll.p2 = clock.p2;
6587 }
79e53945 6588
5dc5298b 6589 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
8b47047b 6590 if (intel_crtc->config.has_pch_encoder) {
7429e9d4 6591 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
cbbab5bd 6592 if (has_reduced_clock)
7429e9d4 6593 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
cbbab5bd 6594
7429e9d4 6595 dpll = ironlake_compute_dpll(intel_crtc,
cbbab5bd
DV
6596 &fp, &reduced_clock,
6597 has_reduced_clock ? &fp2 : NULL);
6598
959e16d6 6599 intel_crtc->config.dpll_hw_state.dpll = dpll;
66e985c0
DV
6600 intel_crtc->config.dpll_hw_state.fp0 = fp;
6601 if (has_reduced_clock)
6602 intel_crtc->config.dpll_hw_state.fp1 = fp2;
6603 else
6604 intel_crtc->config.dpll_hw_state.fp1 = fp;
6605
b89a1d39 6606 pll = intel_get_shared_dpll(intel_crtc);
ee7b9f93 6607 if (pll == NULL) {
84f44ce7
VS
6608 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
6609 pipe_name(pipe));
4b645f14
JB
6610 return -EINVAL;
6611 }
ee7b9f93 6612 } else
e72f9fbf 6613 intel_put_shared_dpll(intel_crtc);
79e53945 6614
03afc4a2
DV
6615 if (intel_crtc->config.has_dp_encoder)
6616 intel_dp_set_m_n(intel_crtc);
79e53945 6617
d330a953 6618 if (is_lvds && has_reduced_clock && i915.powersave)
bcd644e0
DV
6619 intel_crtc->lowfreq_avail = true;
6620 else
6621 intel_crtc->lowfreq_avail = false;
e2b78267 6622
8a654f3b 6623 intel_set_pipe_timings(intel_crtc);
5eddb70b 6624
ca3a0ff8 6625 if (intel_crtc->config.has_pch_encoder) {
ca3a0ff8
DV
6626 intel_cpu_transcoder_set_m_n(intel_crtc,
6627 &intel_crtc->config.fdi_m_n);
6628 }
2c07245f 6629
6ff93609 6630 ironlake_set_pipeconf(crtc);
79e53945 6631
a1f9e77e
PZ
6632 /* Set up the display plane register */
6633 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
b24e7179 6634 POSTING_READ(DSPCNTR(plane));
79e53945 6635
94352cf9 6636 ret = intel_pipe_set_base(crtc, x, y, fb);
7662c8bd 6637
1857e1da 6638 return ret;
79e53945
JB
6639}
6640
eb14cb74
VS
6641static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6642 struct intel_link_m_n *m_n)
6643{
6644 struct drm_device *dev = crtc->base.dev;
6645 struct drm_i915_private *dev_priv = dev->dev_private;
6646 enum pipe pipe = crtc->pipe;
6647
6648 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
6649 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
6650 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
6651 & ~TU_SIZE_MASK;
6652 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
6653 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
6654 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6655}
6656
6657static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6658 enum transcoder transcoder,
6659 struct intel_link_m_n *m_n)
72419203
DV
6660{
6661 struct drm_device *dev = crtc->base.dev;
6662 struct drm_i915_private *dev_priv = dev->dev_private;
eb14cb74 6663 enum pipe pipe = crtc->pipe;
72419203 6664
eb14cb74
VS
6665 if (INTEL_INFO(dev)->gen >= 5) {
6666 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
6667 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
6668 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
6669 & ~TU_SIZE_MASK;
6670 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
6671 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
6672 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6673 } else {
6674 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
6675 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
6676 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
6677 & ~TU_SIZE_MASK;
6678 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
6679 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
6680 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6681 }
6682}
6683
6684void intel_dp_get_m_n(struct intel_crtc *crtc,
6685 struct intel_crtc_config *pipe_config)
6686{
6687 if (crtc->config.has_pch_encoder)
6688 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6689 else
6690 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6691 &pipe_config->dp_m_n);
6692}
72419203 6693
eb14cb74
VS
6694static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
6695 struct intel_crtc_config *pipe_config)
6696{
6697 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6698 &pipe_config->fdi_m_n);
72419203
DV
6699}
6700
2fa2fe9a
DV
6701static void ironlake_get_pfit_config(struct intel_crtc *crtc,
6702 struct intel_crtc_config *pipe_config)
6703{
6704 struct drm_device *dev = crtc->base.dev;
6705 struct drm_i915_private *dev_priv = dev->dev_private;
6706 uint32_t tmp;
6707
6708 tmp = I915_READ(PF_CTL(crtc->pipe));
6709
6710 if (tmp & PF_ENABLE) {
fd4daa9c 6711 pipe_config->pch_pfit.enabled = true;
2fa2fe9a
DV
6712 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
6713 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
cb8b2a30
DV
6714
6715 /* We currently do not free assignements of panel fitters on
6716 * ivb/hsw (since we don't use the higher upscaling modes which
6717 * differentiates them) so just WARN about this case for now. */
6718 if (IS_GEN7(dev)) {
6719 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
6720 PF_PIPE_SEL_IVB(crtc->pipe));
6721 }
2fa2fe9a 6722 }
79e53945
JB
6723}
6724
4c6baa59
JB
6725static void ironlake_get_plane_config(struct intel_crtc *crtc,
6726 struct intel_plane_config *plane_config)
6727{
6728 struct drm_device *dev = crtc->base.dev;
6729 struct drm_i915_private *dev_priv = dev->dev_private;
6730 u32 val, base, offset;
6731 int pipe = crtc->pipe, plane = crtc->plane;
6732 int fourcc, pixel_format;
6733 int aligned_height;
6734
484b41dd
JB
6735 crtc->base.fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6736 if (!crtc->base.fb) {
4c6baa59
JB
6737 DRM_DEBUG_KMS("failed to alloc fb\n");
6738 return;
6739 }
6740
6741 val = I915_READ(DSPCNTR(plane));
6742
6743 if (INTEL_INFO(dev)->gen >= 4)
6744 if (val & DISPPLANE_TILED)
6745 plane_config->tiled = true;
6746
6747 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6748 fourcc = intel_format_to_fourcc(pixel_format);
484b41dd
JB
6749 crtc->base.fb->pixel_format = fourcc;
6750 crtc->base.fb->bits_per_pixel =
4c6baa59
JB
6751 drm_format_plane_cpp(fourcc, 0) * 8;
6752
6753 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6754 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
6755 offset = I915_READ(DSPOFFSET(plane));
6756 } else {
6757 if (plane_config->tiled)
6758 offset = I915_READ(DSPTILEOFF(plane));
6759 else
6760 offset = I915_READ(DSPLINOFF(plane));
6761 }
6762 plane_config->base = base;
6763
6764 val = I915_READ(PIPESRC(pipe));
484b41dd
JB
6765 crtc->base.fb->width = ((val >> 16) & 0xfff) + 1;
6766 crtc->base.fb->height = ((val >> 0) & 0xfff) + 1;
4c6baa59
JB
6767
6768 val = I915_READ(DSPSTRIDE(pipe));
484b41dd 6769 crtc->base.fb->pitches[0] = val & 0xffffff80;
4c6baa59 6770
484b41dd 6771 aligned_height = intel_align_height(dev, crtc->base.fb->height,
4c6baa59
JB
6772 plane_config->tiled);
6773
484b41dd 6774 plane_config->size = ALIGN(crtc->base.fb->pitches[0] *
4c6baa59
JB
6775 aligned_height, PAGE_SIZE);
6776
6777 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
484b41dd
JB
6778 pipe, plane, crtc->base.fb->width,
6779 crtc->base.fb->height,
6780 crtc->base.fb->bits_per_pixel, base,
6781 crtc->base.fb->pitches[0],
4c6baa59
JB
6782 plane_config->size);
6783}
6784
0e8ffe1b
DV
6785static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
6786 struct intel_crtc_config *pipe_config)
6787{
6788 struct drm_device *dev = crtc->base.dev;
6789 struct drm_i915_private *dev_priv = dev->dev_private;
6790 uint32_t tmp;
6791
e143a21c 6792 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62 6793 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
eccb140b 6794
0e8ffe1b
DV
6795 tmp = I915_READ(PIPECONF(crtc->pipe));
6796 if (!(tmp & PIPECONF_ENABLE))
6797 return false;
6798
42571aef
VS
6799 switch (tmp & PIPECONF_BPC_MASK) {
6800 case PIPECONF_6BPC:
6801 pipe_config->pipe_bpp = 18;
6802 break;
6803 case PIPECONF_8BPC:
6804 pipe_config->pipe_bpp = 24;
6805 break;
6806 case PIPECONF_10BPC:
6807 pipe_config->pipe_bpp = 30;
6808 break;
6809 case PIPECONF_12BPC:
6810 pipe_config->pipe_bpp = 36;
6811 break;
6812 default:
6813 break;
6814 }
6815
ab9412ba 6816 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
66e985c0
DV
6817 struct intel_shared_dpll *pll;
6818
88adfff1
DV
6819 pipe_config->has_pch_encoder = true;
6820
627eb5a3
DV
6821 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
6822 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6823 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
6824
6825 ironlake_get_fdi_m_n_config(crtc, pipe_config);
6c49f241 6826
c0d43d62 6827 if (HAS_PCH_IBX(dev_priv->dev)) {
d94ab068
DV
6828 pipe_config->shared_dpll =
6829 (enum intel_dpll_id) crtc->pipe;
c0d43d62
DV
6830 } else {
6831 tmp = I915_READ(PCH_DPLL_SEL);
6832 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6833 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
6834 else
6835 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
6836 }
66e985c0
DV
6837
6838 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
6839
6840 WARN_ON(!pll->get_hw_state(dev_priv, pll,
6841 &pipe_config->dpll_hw_state));
c93f54cf
DV
6842
6843 tmp = pipe_config->dpll_hw_state.dpll;
6844 pipe_config->pixel_multiplier =
6845 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6846 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
18442d08
VS
6847
6848 ironlake_pch_clock_get(crtc, pipe_config);
6c49f241
DV
6849 } else {
6850 pipe_config->pixel_multiplier = 1;
627eb5a3
DV
6851 }
6852
1bd1bd80
DV
6853 intel_get_pipe_timings(crtc, pipe_config);
6854
2fa2fe9a
DV
6855 ironlake_get_pfit_config(crtc, pipe_config);
6856
0e8ffe1b
DV
6857 return true;
6858}
6859
be256dc7
PZ
6860static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
6861{
6862 struct drm_device *dev = dev_priv->dev;
6863 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
6864 struct intel_crtc *crtc;
6865 unsigned long irqflags;
bd633a7c 6866 uint32_t val;
be256dc7
PZ
6867
6868 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
798183c5 6869 WARN(crtc->active, "CRTC for pipe %c enabled\n",
be256dc7
PZ
6870 pipe_name(crtc->pipe));
6871
6872 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
6873 WARN(plls->spll_refcount, "SPLL enabled\n");
6874 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
6875 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
6876 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
6877 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
6878 "CPU PWM1 enabled\n");
6879 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
6880 "CPU PWM2 enabled\n");
6881 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
6882 "PCH PWM1 enabled\n");
6883 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
6884 "Utility pin enabled\n");
6885 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
6886
6887 spin_lock_irqsave(&dev_priv->irq_lock, irqflags);
6888 val = I915_READ(DEIMR);
6806e63f 6889 WARN((val | DE_PCH_EVENT_IVB) != 0xffffffff,
be256dc7
PZ
6890 "Unexpected DEIMR bits enabled: 0x%x\n", val);
6891 val = I915_READ(SDEIMR);
bd633a7c 6892 WARN((val | SDE_HOTPLUG_MASK_CPT) != 0xffffffff,
be256dc7
PZ
6893 "Unexpected SDEIMR bits enabled: 0x%x\n", val);
6894 spin_unlock_irqrestore(&dev_priv->irq_lock, irqflags);
6895}
6896
6897/*
6898 * This function implements pieces of two sequences from BSpec:
6899 * - Sequence for display software to disable LCPLL
6900 * - Sequence for display software to allow package C8+
6901 * The steps implemented here are just the steps that actually touch the LCPLL
6902 * register. Callers should take care of disabling all the display engine
6903 * functions, doing the mode unset, fixing interrupts, etc.
6904 */
6ff58d53
PZ
6905static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
6906 bool switch_to_fclk, bool allow_power_down)
be256dc7
PZ
6907{
6908 uint32_t val;
6909
6910 assert_can_disable_lcpll(dev_priv);
6911
6912 val = I915_READ(LCPLL_CTL);
6913
6914 if (switch_to_fclk) {
6915 val |= LCPLL_CD_SOURCE_FCLK;
6916 I915_WRITE(LCPLL_CTL, val);
6917
6918 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
6919 LCPLL_CD_SOURCE_FCLK_DONE, 1))
6920 DRM_ERROR("Switching to FCLK failed\n");
6921
6922 val = I915_READ(LCPLL_CTL);
6923 }
6924
6925 val |= LCPLL_PLL_DISABLE;
6926 I915_WRITE(LCPLL_CTL, val);
6927 POSTING_READ(LCPLL_CTL);
6928
6929 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
6930 DRM_ERROR("LCPLL still locked\n");
6931
6932 val = I915_READ(D_COMP);
6933 val |= D_COMP_COMP_DISABLE;
515b2392
PZ
6934 mutex_lock(&dev_priv->rps.hw_lock);
6935 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6936 DRM_ERROR("Failed to disable D_COMP\n");
6937 mutex_unlock(&dev_priv->rps.hw_lock);
be256dc7
PZ
6938 POSTING_READ(D_COMP);
6939 ndelay(100);
6940
6941 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
6942 DRM_ERROR("D_COMP RCOMP still in progress\n");
6943
6944 if (allow_power_down) {
6945 val = I915_READ(LCPLL_CTL);
6946 val |= LCPLL_POWER_DOWN_ALLOW;
6947 I915_WRITE(LCPLL_CTL, val);
6948 POSTING_READ(LCPLL_CTL);
6949 }
6950}
6951
6952/*
6953 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
6954 * source.
6955 */
6ff58d53 6956static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
be256dc7
PZ
6957{
6958 uint32_t val;
6959
6960 val = I915_READ(LCPLL_CTL);
6961
6962 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
6963 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
6964 return;
6965
215733fa
PZ
6966 /* Make sure we're not on PC8 state before disabling PC8, otherwise
6967 * we'll hang the machine! */
0d9d349d 6968 gen6_gt_force_wake_get(dev_priv, FORCEWAKE_ALL);
215733fa 6969
be256dc7
PZ
6970 if (val & LCPLL_POWER_DOWN_ALLOW) {
6971 val &= ~LCPLL_POWER_DOWN_ALLOW;
6972 I915_WRITE(LCPLL_CTL, val);
35d8f2eb 6973 POSTING_READ(LCPLL_CTL);
be256dc7
PZ
6974 }
6975
6976 val = I915_READ(D_COMP);
6977 val |= D_COMP_COMP_FORCE;
6978 val &= ~D_COMP_COMP_DISABLE;
515b2392
PZ
6979 mutex_lock(&dev_priv->rps.hw_lock);
6980 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP, val))
6981 DRM_ERROR("Failed to enable D_COMP\n");
6982 mutex_unlock(&dev_priv->rps.hw_lock);
35d8f2eb 6983 POSTING_READ(D_COMP);
be256dc7
PZ
6984
6985 val = I915_READ(LCPLL_CTL);
6986 val &= ~LCPLL_PLL_DISABLE;
6987 I915_WRITE(LCPLL_CTL, val);
6988
6989 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
6990 DRM_ERROR("LCPLL not locked yet\n");
6991
6992 if (val & LCPLL_CD_SOURCE_FCLK) {
6993 val = I915_READ(LCPLL_CTL);
6994 val &= ~LCPLL_CD_SOURCE_FCLK;
6995 I915_WRITE(LCPLL_CTL, val);
6996
6997 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
6998 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
6999 DRM_ERROR("Switching back to LCPLL failed\n");
7000 }
215733fa 7001
0d9d349d 7002 gen6_gt_force_wake_put(dev_priv, FORCEWAKE_ALL);
be256dc7
PZ
7003}
7004
c67a470b
PZ
7005void hsw_enable_pc8_work(struct work_struct *__work)
7006{
7007 struct drm_i915_private *dev_priv =
7008 container_of(to_delayed_work(__work), struct drm_i915_private,
7009 pc8.enable_work);
7010 struct drm_device *dev = dev_priv->dev;
7011 uint32_t val;
7012
7125ecb8
PZ
7013 WARN_ON(!HAS_PC8(dev));
7014
c67a470b
PZ
7015 if (dev_priv->pc8.enabled)
7016 return;
7017
7018 DRM_DEBUG_KMS("Enabling package C8+\n");
7019
7020 dev_priv->pc8.enabled = true;
7021
7022 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7023 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7024 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7025 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7026 }
7027
7028 lpt_disable_clkout_dp(dev);
7029 hsw_pc8_disable_interrupts(dev);
7030 hsw_disable_lcpll(dev_priv, true, true);
8771a7f8
PZ
7031
7032 intel_runtime_pm_put(dev_priv);
c67a470b
PZ
7033}
7034
7035static void __hsw_enable_package_c8(struct drm_i915_private *dev_priv)
7036{
7037 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
7038 WARN(dev_priv->pc8.disable_count < 1,
7039 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
7040
7041 dev_priv->pc8.disable_count--;
7042 if (dev_priv->pc8.disable_count != 0)
7043 return;
7044
7045 schedule_delayed_work(&dev_priv->pc8.enable_work,
d330a953 7046 msecs_to_jiffies(i915.pc8_timeout));
c67a470b
PZ
7047}
7048
7049static void __hsw_disable_package_c8(struct drm_i915_private *dev_priv)
7050{
7051 struct drm_device *dev = dev_priv->dev;
7052 uint32_t val;
7053
7054 WARN_ON(!mutex_is_locked(&dev_priv->pc8.lock));
7055 WARN(dev_priv->pc8.disable_count < 0,
7056 "pc8.disable_count: %d\n", dev_priv->pc8.disable_count);
7057
7058 dev_priv->pc8.disable_count++;
7059 if (dev_priv->pc8.disable_count != 1)
7060 return;
7061
7125ecb8
PZ
7062 WARN_ON(!HAS_PC8(dev));
7063
c67a470b
PZ
7064 cancel_delayed_work_sync(&dev_priv->pc8.enable_work);
7065 if (!dev_priv->pc8.enabled)
7066 return;
7067
7068 DRM_DEBUG_KMS("Disabling package C8+\n");
7069
8771a7f8
PZ
7070 intel_runtime_pm_get(dev_priv);
7071
c67a470b
PZ
7072 hsw_restore_lcpll(dev_priv);
7073 hsw_pc8_restore_interrupts(dev);
7074 lpt_init_pch_refclk(dev);
7075
7076 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7077 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7078 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7079 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7080 }
7081
7082 intel_prepare_ddi(dev);
7083 i915_gem_init_swizzling(dev);
7084 mutex_lock(&dev_priv->rps.hw_lock);
7085 gen6_update_ring_freq(dev);
7086 mutex_unlock(&dev_priv->rps.hw_lock);
7087 dev_priv->pc8.enabled = false;
7088}
7089
7090void hsw_enable_package_c8(struct drm_i915_private *dev_priv)
7091{
7c6c2652
CW
7092 if (!HAS_PC8(dev_priv->dev))
7093 return;
7094
c67a470b
PZ
7095 mutex_lock(&dev_priv->pc8.lock);
7096 __hsw_enable_package_c8(dev_priv);
7097 mutex_unlock(&dev_priv->pc8.lock);
7098}
7099
7100void hsw_disable_package_c8(struct drm_i915_private *dev_priv)
7101{
7c6c2652
CW
7102 if (!HAS_PC8(dev_priv->dev))
7103 return;
7104
c67a470b
PZ
7105 mutex_lock(&dev_priv->pc8.lock);
7106 __hsw_disable_package_c8(dev_priv);
7107 mutex_unlock(&dev_priv->pc8.lock);
7108}
7109
7110static bool hsw_can_enable_package_c8(struct drm_i915_private *dev_priv)
7111{
7112 struct drm_device *dev = dev_priv->dev;
7113 struct intel_crtc *crtc;
7114 uint32_t val;
7115
7116 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head)
7117 if (crtc->base.enabled)
7118 return false;
7119
7120 /* This case is still possible since we have the i915.disable_power_well
7121 * parameter and also the KVMr or something else might be requesting the
7122 * power well. */
7123 val = I915_READ(HSW_PWR_WELL_DRIVER);
7124 if (val != 0) {
7125 DRM_DEBUG_KMS("Not enabling PC8: power well on\n");
7126 return false;
7127 }
7128
7129 return true;
7130}
7131
7132/* Since we're called from modeset_global_resources there's no way to
7133 * symmetrically increase and decrease the refcount, so we use
7134 * dev_priv->pc8.requirements_met to track whether we already have the refcount
7135 * or not.
7136 */
7137static void hsw_update_package_c8(struct drm_device *dev)
7138{
7139 struct drm_i915_private *dev_priv = dev->dev_private;
7140 bool allow;
7141
7c6c2652
CW
7142 if (!HAS_PC8(dev_priv->dev))
7143 return;
7144
d330a953 7145 if (!i915.enable_pc8)
c67a470b
PZ
7146 return;
7147
7148 mutex_lock(&dev_priv->pc8.lock);
7149
7150 allow = hsw_can_enable_package_c8(dev_priv);
7151
7152 if (allow == dev_priv->pc8.requirements_met)
7153 goto done;
7154
7155 dev_priv->pc8.requirements_met = allow;
7156
7157 if (allow)
7158 __hsw_enable_package_c8(dev_priv);
7159 else
7160 __hsw_disable_package_c8(dev_priv);
7161
7162done:
7163 mutex_unlock(&dev_priv->pc8.lock);
7164}
7165
4f074129
ID
7166static void haswell_modeset_global_resources(struct drm_device *dev)
7167{
da723569 7168 modeset_update_crtc_power_domains(dev);
c67a470b 7169 hsw_update_package_c8(dev);
d6dd9eb1
DV
7170}
7171
09b4ddf9 7172static int haswell_crtc_mode_set(struct drm_crtc *crtc,
09b4ddf9
PZ
7173 int x, int y,
7174 struct drm_framebuffer *fb)
7175{
7176 struct drm_device *dev = crtc->dev;
7177 struct drm_i915_private *dev_priv = dev->dev_private;
7178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
09b4ddf9 7179 int plane = intel_crtc->plane;
09b4ddf9 7180 int ret;
09b4ddf9 7181
566b734a 7182 if (!intel_ddi_pll_select(intel_crtc))
6441ab5f 7183 return -EINVAL;
566b734a 7184 intel_ddi_pll_enable(intel_crtc);
6441ab5f 7185
03afc4a2
DV
7186 if (intel_crtc->config.has_dp_encoder)
7187 intel_dp_set_m_n(intel_crtc);
09b4ddf9
PZ
7188
7189 intel_crtc->lowfreq_avail = false;
09b4ddf9 7190
8a654f3b 7191 intel_set_pipe_timings(intel_crtc);
09b4ddf9 7192
ca3a0ff8 7193 if (intel_crtc->config.has_pch_encoder) {
ca3a0ff8
DV
7194 intel_cpu_transcoder_set_m_n(intel_crtc,
7195 &intel_crtc->config.fdi_m_n);
7196 }
09b4ddf9 7197
6ff93609 7198 haswell_set_pipeconf(crtc);
09b4ddf9 7199
50f3b016 7200 intel_set_pipe_csc(crtc);
86d3efce 7201
09b4ddf9 7202 /* Set up the display plane register */
86d3efce 7203 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
09b4ddf9
PZ
7204 POSTING_READ(DSPCNTR(plane));
7205
7206 ret = intel_pipe_set_base(crtc, x, y, fb);
7207
1f803ee5 7208 return ret;
79e53945
JB
7209}
7210
0e8ffe1b
DV
7211static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7212 struct intel_crtc_config *pipe_config)
7213{
7214 struct drm_device *dev = crtc->base.dev;
7215 struct drm_i915_private *dev_priv = dev->dev_private;
2fa2fe9a 7216 enum intel_display_power_domain pfit_domain;
0e8ffe1b
DV
7217 uint32_t tmp;
7218
b5482bd0
ID
7219 if (!intel_display_power_enabled(dev_priv,
7220 POWER_DOMAIN_PIPE(crtc->pipe)))
7221 return false;
7222
e143a21c 7223 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
c0d43d62
DV
7224 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7225
eccb140b
DV
7226 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7227 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7228 enum pipe trans_edp_pipe;
7229 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7230 default:
7231 WARN(1, "unknown pipe linked to edp transcoder\n");
7232 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7233 case TRANS_DDI_EDP_INPUT_A_ON:
7234 trans_edp_pipe = PIPE_A;
7235 break;
7236 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7237 trans_edp_pipe = PIPE_B;
7238 break;
7239 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7240 trans_edp_pipe = PIPE_C;
7241 break;
7242 }
7243
7244 if (trans_edp_pipe == crtc->pipe)
7245 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7246 }
7247
da7e29bd 7248 if (!intel_display_power_enabled(dev_priv,
eccb140b 7249 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
2bfce950
PZ
7250 return false;
7251
eccb140b 7252 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
0e8ffe1b
DV
7253 if (!(tmp & PIPECONF_ENABLE))
7254 return false;
7255
88adfff1 7256 /*
f196e6be 7257 * Haswell has only FDI/PCH transcoder A. It is which is connected to
88adfff1
DV
7258 * DDI E. So just check whether this pipe is wired to DDI E and whether
7259 * the PCH transcoder is on.
7260 */
eccb140b 7261 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
88adfff1 7262 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
ab9412ba 7263 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
88adfff1
DV
7264 pipe_config->has_pch_encoder = true;
7265
627eb5a3
DV
7266 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7267 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7268 FDI_DP_PORT_WIDTH_SHIFT) + 1;
72419203
DV
7269
7270 ironlake_get_fdi_m_n_config(crtc, pipe_config);
627eb5a3
DV
7271 }
7272
1bd1bd80
DV
7273 intel_get_pipe_timings(crtc, pipe_config);
7274
2fa2fe9a 7275 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
da7e29bd 7276 if (intel_display_power_enabled(dev_priv, pfit_domain))
2fa2fe9a 7277 ironlake_get_pfit_config(crtc, pipe_config);
88adfff1 7278
e59150dc
JB
7279 if (IS_HASWELL(dev))
7280 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7281 (I915_READ(IPS_CTL) & IPS_ENABLE);
42db64ef 7282
6c49f241
DV
7283 pipe_config->pixel_multiplier = 1;
7284
0e8ffe1b
DV
7285 return true;
7286}
7287
f564048e 7288static int intel_crtc_mode_set(struct drm_crtc *crtc,
f564048e 7289 int x, int y,
94352cf9 7290 struct drm_framebuffer *fb)
f564048e
EA
7291{
7292 struct drm_device *dev = crtc->dev;
7293 struct drm_i915_private *dev_priv = dev->dev_private;
9256aa19 7294 struct intel_encoder *encoder;
0b701d27 7295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
b8cecdf5 7296 struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
0b701d27 7297 int pipe = intel_crtc->pipe;
f564048e
EA
7298 int ret;
7299
0b701d27 7300 drm_vblank_pre_modeset(dev, pipe);
7662c8bd 7301
b8cecdf5
DV
7302 ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
7303
79e53945 7304 drm_vblank_post_modeset(dev, pipe);
5c3b82e2 7305
9256aa19
DV
7306 if (ret != 0)
7307 return ret;
7308
7309 for_each_encoder_on_crtc(dev, crtc, encoder) {
7310 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
7311 encoder->base.base.id,
7312 drm_get_encoder_name(&encoder->base),
7313 mode->base.id, mode->name);
36f2d1f1 7314 encoder->mode_set(encoder);
9256aa19
DV
7315 }
7316
7317 return 0;
79e53945
JB
7318}
7319
1a91510d
JN
7320static struct {
7321 int clock;
7322 u32 config;
7323} hdmi_audio_clock[] = {
7324 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7325 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7326 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7327 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7328 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7329 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7330 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7331 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7332 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7333 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7334};
7335
7336/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7337static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7338{
7339 int i;
7340
7341 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7342 if (mode->clock == hdmi_audio_clock[i].clock)
7343 break;
7344 }
7345
7346 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7347 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7348 i = 1;
7349 }
7350
7351 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7352 hdmi_audio_clock[i].clock,
7353 hdmi_audio_clock[i].config);
7354
7355 return hdmi_audio_clock[i].config;
7356}
7357
3a9627f4
WF
7358static bool intel_eld_uptodate(struct drm_connector *connector,
7359 int reg_eldv, uint32_t bits_eldv,
7360 int reg_elda, uint32_t bits_elda,
7361 int reg_edid)
7362{
7363 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7364 uint8_t *eld = connector->eld;
7365 uint32_t i;
7366
7367 i = I915_READ(reg_eldv);
7368 i &= bits_eldv;
7369
7370 if (!eld[0])
7371 return !i;
7372
7373 if (!i)
7374 return false;
7375
7376 i = I915_READ(reg_elda);
7377 i &= ~bits_elda;
7378 I915_WRITE(reg_elda, i);
7379
7380 for (i = 0; i < eld[2]; i++)
7381 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7382 return false;
7383
7384 return true;
7385}
7386
e0dac65e 7387static void g4x_write_eld(struct drm_connector *connector,
34427052
JN
7388 struct drm_crtc *crtc,
7389 struct drm_display_mode *mode)
e0dac65e
WF
7390{
7391 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7392 uint8_t *eld = connector->eld;
7393 uint32_t eldv;
7394 uint32_t len;
7395 uint32_t i;
7396
7397 i = I915_READ(G4X_AUD_VID_DID);
7398
7399 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7400 eldv = G4X_ELDV_DEVCL_DEVBLC;
7401 else
7402 eldv = G4X_ELDV_DEVCTG;
7403
3a9627f4
WF
7404 if (intel_eld_uptodate(connector,
7405 G4X_AUD_CNTL_ST, eldv,
7406 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7407 G4X_HDMIW_HDMIEDID))
7408 return;
7409
e0dac65e
WF
7410 i = I915_READ(G4X_AUD_CNTL_ST);
7411 i &= ~(eldv | G4X_ELD_ADDR);
7412 len = (i >> 9) & 0x1f; /* ELD buffer size */
7413 I915_WRITE(G4X_AUD_CNTL_ST, i);
7414
7415 if (!eld[0])
7416 return;
7417
7418 len = min_t(uint8_t, eld[2], len);
7419 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7420 for (i = 0; i < len; i++)
7421 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7422
7423 i = I915_READ(G4X_AUD_CNTL_ST);
7424 i |= eldv;
7425 I915_WRITE(G4X_AUD_CNTL_ST, i);
7426}
7427
83358c85 7428static void haswell_write_eld(struct drm_connector *connector,
34427052
JN
7429 struct drm_crtc *crtc,
7430 struct drm_display_mode *mode)
83358c85
WX
7431{
7432 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7433 uint8_t *eld = connector->eld;
7434 struct drm_device *dev = crtc->dev;
7b9f35a6 7435 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
83358c85
WX
7436 uint32_t eldv;
7437 uint32_t i;
7438 int len;
7439 int pipe = to_intel_crtc(crtc)->pipe;
7440 int tmp;
7441
7442 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7443 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7444 int aud_config = HSW_AUD_CFG(pipe);
7445 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7446
7447
7448 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
7449
7450 /* Audio output enable */
7451 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7452 tmp = I915_READ(aud_cntrl_st2);
7453 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7454 I915_WRITE(aud_cntrl_st2, tmp);
7455
7456 /* Wait for 1 vertical blank */
7457 intel_wait_for_vblank(dev, pipe);
7458
7459 /* Set ELD valid state */
7460 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7461 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
83358c85
WX
7462 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7463 I915_WRITE(aud_cntrl_st2, tmp);
7464 tmp = I915_READ(aud_cntrl_st2);
7e7cb34f 7465 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
83358c85
WX
7466
7467 /* Enable HDMI mode */
7468 tmp = I915_READ(aud_config);
7e7cb34f 7469 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
83358c85
WX
7470 /* clear N_programing_enable and N_value_index */
7471 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7472 I915_WRITE(aud_config, tmp);
7473
7474 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7475
7476 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
7b9f35a6 7477 intel_crtc->eld_vld = true;
83358c85
WX
7478
7479 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7480 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7481 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7482 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
7483 } else {
7484 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7485 }
83358c85
WX
7486
7487 if (intel_eld_uptodate(connector,
7488 aud_cntrl_st2, eldv,
7489 aud_cntl_st, IBX_ELD_ADDRESS,
7490 hdmiw_hdmiedid))
7491 return;
7492
7493 i = I915_READ(aud_cntrl_st2);
7494 i &= ~eldv;
7495 I915_WRITE(aud_cntrl_st2, i);
7496
7497 if (!eld[0])
7498 return;
7499
7500 i = I915_READ(aud_cntl_st);
7501 i &= ~IBX_ELD_ADDRESS;
7502 I915_WRITE(aud_cntl_st, i);
7503 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7504 DRM_DEBUG_DRIVER("port num:%d\n", i);
7505
7506 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7507 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7508 for (i = 0; i < len; i++)
7509 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7510
7511 i = I915_READ(aud_cntrl_st2);
7512 i |= eldv;
7513 I915_WRITE(aud_cntrl_st2, i);
7514
7515}
7516
e0dac65e 7517static void ironlake_write_eld(struct drm_connector *connector,
34427052
JN
7518 struct drm_crtc *crtc,
7519 struct drm_display_mode *mode)
e0dac65e
WF
7520{
7521 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7522 uint8_t *eld = connector->eld;
7523 uint32_t eldv;
7524 uint32_t i;
7525 int len;
7526 int hdmiw_hdmiedid;
b6daa025 7527 int aud_config;
e0dac65e
WF
7528 int aud_cntl_st;
7529 int aud_cntrl_st2;
9b138a83 7530 int pipe = to_intel_crtc(crtc)->pipe;
e0dac65e 7531
b3f33cbf 7532 if (HAS_PCH_IBX(connector->dev)) {
9b138a83
WX
7533 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7534 aud_config = IBX_AUD_CFG(pipe);
7535 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
1202b4c6 7536 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
9ca2fe73
ML
7537 } else if (IS_VALLEYVIEW(connector->dev)) {
7538 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7539 aud_config = VLV_AUD_CFG(pipe);
7540 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7541 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
e0dac65e 7542 } else {
9b138a83
WX
7543 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7544 aud_config = CPT_AUD_CFG(pipe);
7545 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
1202b4c6 7546 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
e0dac65e
WF
7547 }
7548
9b138a83 7549 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
e0dac65e 7550
9ca2fe73
ML
7551 if (IS_VALLEYVIEW(connector->dev)) {
7552 struct intel_encoder *intel_encoder;
7553 struct intel_digital_port *intel_dig_port;
7554
7555 intel_encoder = intel_attached_encoder(connector);
7556 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7557 i = intel_dig_port->port;
7558 } else {
7559 i = I915_READ(aud_cntl_st);
7560 i = (i >> 29) & DIP_PORT_SEL_MASK;
7561 /* DIP_Port_Select, 0x1 = PortB */
7562 }
7563
e0dac65e
WF
7564 if (!i) {
7565 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7566 /* operate blindly on all ports */
1202b4c6
WF
7567 eldv = IBX_ELD_VALIDB;
7568 eldv |= IBX_ELD_VALIDB << 4;
7569 eldv |= IBX_ELD_VALIDB << 8;
e0dac65e 7570 } else {
2582a850 7571 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
1202b4c6 7572 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
e0dac65e
WF
7573 }
7574
3a9627f4
WF
7575 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7576 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7577 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
b6daa025 7578 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
1a91510d
JN
7579 } else {
7580 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7581 }
e0dac65e 7582
3a9627f4
WF
7583 if (intel_eld_uptodate(connector,
7584 aud_cntrl_st2, eldv,
7585 aud_cntl_st, IBX_ELD_ADDRESS,
7586 hdmiw_hdmiedid))
7587 return;
7588
e0dac65e
WF
7589 i = I915_READ(aud_cntrl_st2);
7590 i &= ~eldv;
7591 I915_WRITE(aud_cntrl_st2, i);
7592
7593 if (!eld[0])
7594 return;
7595
e0dac65e 7596 i = I915_READ(aud_cntl_st);
1202b4c6 7597 i &= ~IBX_ELD_ADDRESS;
e0dac65e
WF
7598 I915_WRITE(aud_cntl_st, i);
7599
7600 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7601 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7602 for (i = 0; i < len; i++)
7603 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7604
7605 i = I915_READ(aud_cntrl_st2);
7606 i |= eldv;
7607 I915_WRITE(aud_cntrl_st2, i);
7608}
7609
7610void intel_write_eld(struct drm_encoder *encoder,
7611 struct drm_display_mode *mode)
7612{
7613 struct drm_crtc *crtc = encoder->crtc;
7614 struct drm_connector *connector;
7615 struct drm_device *dev = encoder->dev;
7616 struct drm_i915_private *dev_priv = dev->dev_private;
7617
7618 connector = drm_select_eld(encoder, mode);
7619 if (!connector)
7620 return;
7621
7622 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7623 connector->base.id,
7624 drm_get_connector_name(connector),
7625 connector->encoder->base.id,
7626 drm_get_encoder_name(connector->encoder));
7627
7628 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7629
7630 if (dev_priv->display.write_eld)
34427052 7631 dev_priv->display.write_eld(connector, crtc, mode);
e0dac65e
WF
7632}
7633
560b85bb
CW
7634static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7635{
7636 struct drm_device *dev = crtc->dev;
7637 struct drm_i915_private *dev_priv = dev->dev_private;
7638 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7639 bool visible = base != 0;
7640 u32 cntl;
7641
7642 if (intel_crtc->cursor_visible == visible)
7643 return;
7644
9db4a9c7 7645 cntl = I915_READ(_CURACNTR);
560b85bb
CW
7646 if (visible) {
7647 /* On these chipsets we can only modify the base whilst
7648 * the cursor is disabled.
7649 */
9db4a9c7 7650 I915_WRITE(_CURABASE, base);
560b85bb
CW
7651
7652 cntl &= ~(CURSOR_FORMAT_MASK);
7653 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7654 cntl |= CURSOR_ENABLE |
7655 CURSOR_GAMMA_ENABLE |
7656 CURSOR_FORMAT_ARGB;
7657 } else
7658 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
9db4a9c7 7659 I915_WRITE(_CURACNTR, cntl);
560b85bb
CW
7660
7661 intel_crtc->cursor_visible = visible;
7662}
7663
7664static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7665{
7666 struct drm_device *dev = crtc->dev;
7667 struct drm_i915_private *dev_priv = dev->dev_private;
7668 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7669 int pipe = intel_crtc->pipe;
7670 bool visible = base != 0;
7671
7672 if (intel_crtc->cursor_visible != visible) {
548f245b 7673 uint32_t cntl = I915_READ(CURCNTR(pipe));
560b85bb
CW
7674 if (base) {
7675 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
7676 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7677 cntl |= pipe << 28; /* Connect to correct pipe */
7678 } else {
7679 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7680 cntl |= CURSOR_MODE_DISABLE;
7681 }
9db4a9c7 7682 I915_WRITE(CURCNTR(pipe), cntl);
560b85bb
CW
7683
7684 intel_crtc->cursor_visible = visible;
7685 }
7686 /* and commit changes on next vblank */
b2ea8ef5 7687 POSTING_READ(CURCNTR(pipe));
9db4a9c7 7688 I915_WRITE(CURBASE(pipe), base);
b2ea8ef5 7689 POSTING_READ(CURBASE(pipe));
560b85bb
CW
7690}
7691
65a21cd6
JB
7692static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7693{
7694 struct drm_device *dev = crtc->dev;
7695 struct drm_i915_private *dev_priv = dev->dev_private;
7696 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7697 int pipe = intel_crtc->pipe;
7698 bool visible = base != 0;
7699
7700 if (intel_crtc->cursor_visible != visible) {
7701 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
7702 if (base) {
7703 cntl &= ~CURSOR_MODE;
7704 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
7705 } else {
7706 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7707 cntl |= CURSOR_MODE_DISABLE;
7708 }
6bbfa1c5 7709 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
86d3efce 7710 cntl |= CURSOR_PIPE_CSC_ENABLE;
1f5d76db
PZ
7711 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7712 }
65a21cd6
JB
7713 I915_WRITE(CURCNTR_IVB(pipe), cntl);
7714
7715 intel_crtc->cursor_visible = visible;
7716 }
7717 /* and commit changes on next vblank */
b2ea8ef5 7718 POSTING_READ(CURCNTR_IVB(pipe));
65a21cd6 7719 I915_WRITE(CURBASE_IVB(pipe), base);
b2ea8ef5 7720 POSTING_READ(CURBASE_IVB(pipe));
65a21cd6
JB
7721}
7722
cda4b7d3 7723/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
6b383a7f
CW
7724static void intel_crtc_update_cursor(struct drm_crtc *crtc,
7725 bool on)
cda4b7d3
CW
7726{
7727 struct drm_device *dev = crtc->dev;
7728 struct drm_i915_private *dev_priv = dev->dev_private;
7729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7730 int pipe = intel_crtc->pipe;
7731 int x = intel_crtc->cursor_x;
7732 int y = intel_crtc->cursor_y;
d6e4db15 7733 u32 base = 0, pos = 0;
cda4b7d3
CW
7734 bool visible;
7735
d6e4db15 7736 if (on)
cda4b7d3 7737 base = intel_crtc->cursor_addr;
cda4b7d3 7738
d6e4db15
VS
7739 if (x >= intel_crtc->config.pipe_src_w)
7740 base = 0;
7741
7742 if (y >= intel_crtc->config.pipe_src_h)
cda4b7d3
CW
7743 base = 0;
7744
7745 if (x < 0) {
efc9064e 7746 if (x + intel_crtc->cursor_width <= 0)
cda4b7d3
CW
7747 base = 0;
7748
7749 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
7750 x = -x;
7751 }
7752 pos |= x << CURSOR_X_SHIFT;
7753
7754 if (y < 0) {
efc9064e 7755 if (y + intel_crtc->cursor_height <= 0)
cda4b7d3
CW
7756 base = 0;
7757
7758 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
7759 y = -y;
7760 }
7761 pos |= y << CURSOR_Y_SHIFT;
7762
7763 visible = base != 0;
560b85bb 7764 if (!visible && !intel_crtc->cursor_visible)
cda4b7d3
CW
7765 return;
7766
b3dc685e 7767 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev)) {
65a21cd6
JB
7768 I915_WRITE(CURPOS_IVB(pipe), pos);
7769 ivb_update_cursor(crtc, base);
7770 } else {
7771 I915_WRITE(CURPOS(pipe), pos);
7772 if (IS_845G(dev) || IS_I865G(dev))
7773 i845_update_cursor(crtc, base);
7774 else
7775 i9xx_update_cursor(crtc, base);
7776 }
cda4b7d3
CW
7777}
7778
79e53945 7779static int intel_crtc_cursor_set(struct drm_crtc *crtc,
05394f39 7780 struct drm_file *file,
79e53945
JB
7781 uint32_t handle,
7782 uint32_t width, uint32_t height)
7783{
7784 struct drm_device *dev = crtc->dev;
7785 struct drm_i915_private *dev_priv = dev->dev_private;
7786 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
05394f39 7787 struct drm_i915_gem_object *obj;
cda4b7d3 7788 uint32_t addr;
3f8bc370 7789 int ret;
79e53945 7790
79e53945
JB
7791 /* if we want to turn off the cursor ignore width and height */
7792 if (!handle) {
28c97730 7793 DRM_DEBUG_KMS("cursor off\n");
3f8bc370 7794 addr = 0;
05394f39 7795 obj = NULL;
5004417d 7796 mutex_lock(&dev->struct_mutex);
3f8bc370 7797 goto finish;
79e53945
JB
7798 }
7799
7800 /* Currently we only support 64x64 cursors */
7801 if (width != 64 || height != 64) {
7802 DRM_ERROR("we currently only support 64x64 cursors\n");
7803 return -EINVAL;
7804 }
7805
05394f39 7806 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
c8725226 7807 if (&obj->base == NULL)
79e53945
JB
7808 return -ENOENT;
7809
05394f39 7810 if (obj->base.size < width * height * 4) {
3b25b31f 7811 DRM_DEBUG_KMS("buffer is to small\n");
34b8686e
DA
7812 ret = -ENOMEM;
7813 goto fail;
79e53945
JB
7814 }
7815
71acb5eb 7816 /* we only need to pin inside GTT if cursor is non-phy */
7f9872e0 7817 mutex_lock(&dev->struct_mutex);
3d13ef2e 7818 if (!INTEL_INFO(dev)->cursor_needs_physical) {
693db184
CW
7819 unsigned alignment;
7820
d9e86c0e 7821 if (obj->tiling_mode) {
3b25b31f 7822 DRM_DEBUG_KMS("cursor cannot be tiled\n");
d9e86c0e
CW
7823 ret = -EINVAL;
7824 goto fail_locked;
7825 }
7826
693db184
CW
7827 /* Note that the w/a also requires 2 PTE of padding following
7828 * the bo. We currently fill all unused PTE with the shadow
7829 * page and so we should always have valid PTE following the
7830 * cursor preventing the VT-d warning.
7831 */
7832 alignment = 0;
7833 if (need_vtd_wa(dev))
7834 alignment = 64*1024;
7835
7836 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
e7b526bb 7837 if (ret) {
3b25b31f 7838 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
2da3b9b9 7839 goto fail_locked;
e7b526bb
CW
7840 }
7841
d9e86c0e
CW
7842 ret = i915_gem_object_put_fence(obj);
7843 if (ret) {
3b25b31f 7844 DRM_DEBUG_KMS("failed to release fence for cursor");
d9e86c0e
CW
7845 goto fail_unpin;
7846 }
7847
f343c5f6 7848 addr = i915_gem_obj_ggtt_offset(obj);
71acb5eb 7849 } else {
6eeefaf3 7850 int align = IS_I830(dev) ? 16 * 1024 : 256;
05394f39 7851 ret = i915_gem_attach_phys_object(dev, obj,
6eeefaf3
CW
7852 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
7853 align);
71acb5eb 7854 if (ret) {
3b25b31f 7855 DRM_DEBUG_KMS("failed to attach phys object\n");
7f9872e0 7856 goto fail_locked;
71acb5eb 7857 }
05394f39 7858 addr = obj->phys_obj->handle->busaddr;
3f8bc370
KH
7859 }
7860
a6c45cf0 7861 if (IS_GEN2(dev))
14b60391
JB
7862 I915_WRITE(CURSIZE, (height << 12) | width);
7863
3f8bc370 7864 finish:
3f8bc370 7865 if (intel_crtc->cursor_bo) {
3d13ef2e 7866 if (INTEL_INFO(dev)->cursor_needs_physical) {
05394f39 7867 if (intel_crtc->cursor_bo != obj)
71acb5eb
DA
7868 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
7869 } else
cc98b413 7870 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
05394f39 7871 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
3f8bc370 7872 }
80824003 7873
7f9872e0 7874 mutex_unlock(&dev->struct_mutex);
3f8bc370
KH
7875
7876 intel_crtc->cursor_addr = addr;
05394f39 7877 intel_crtc->cursor_bo = obj;
cda4b7d3
CW
7878 intel_crtc->cursor_width = width;
7879 intel_crtc->cursor_height = height;
7880
f2f5f771
VS
7881 if (intel_crtc->active)
7882 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
3f8bc370 7883
79e53945 7884 return 0;
e7b526bb 7885fail_unpin:
cc98b413 7886 i915_gem_object_unpin_from_display_plane(obj);
7f9872e0 7887fail_locked:
34b8686e 7888 mutex_unlock(&dev->struct_mutex);
bc9025bd 7889fail:
05394f39 7890 drm_gem_object_unreference_unlocked(&obj->base);
34b8686e 7891 return ret;
79e53945
JB
7892}
7893
7894static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
7895{
79e53945 7896 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 7897
92e76c8c
VS
7898 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
7899 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
652c393a 7900
f2f5f771
VS
7901 if (intel_crtc->active)
7902 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
79e53945
JB
7903
7904 return 0;
b8c00ac5
DA
7905}
7906
79e53945 7907static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
7203425a 7908 u16 *blue, uint32_t start, uint32_t size)
79e53945 7909{
7203425a 7910 int end = (start + size > 256) ? 256 : start + size, i;
79e53945 7911 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 7912
7203425a 7913 for (i = start; i < end; i++) {
79e53945
JB
7914 intel_crtc->lut_r[i] = red[i] >> 8;
7915 intel_crtc->lut_g[i] = green[i] >> 8;
7916 intel_crtc->lut_b[i] = blue[i] >> 8;
7917 }
7918
7919 intel_crtc_load_lut(crtc);
7920}
7921
79e53945
JB
7922/* VESA 640x480x72Hz mode to set on the pipe */
7923static struct drm_display_mode load_detect_mode = {
7924 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
7925 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
7926};
7927
a8bb6818
DV
7928struct drm_framebuffer *
7929__intel_framebuffer_create(struct drm_device *dev,
7930 struct drm_mode_fb_cmd2 *mode_cmd,
7931 struct drm_i915_gem_object *obj)
d2dff872
CW
7932{
7933 struct intel_framebuffer *intel_fb;
7934 int ret;
7935
7936 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
7937 if (!intel_fb) {
7938 drm_gem_object_unreference_unlocked(&obj->base);
7939 return ERR_PTR(-ENOMEM);
7940 }
7941
7942 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
dd4916c5
DV
7943 if (ret)
7944 goto err;
d2dff872
CW
7945
7946 return &intel_fb->base;
dd4916c5
DV
7947err:
7948 drm_gem_object_unreference_unlocked(&obj->base);
7949 kfree(intel_fb);
7950
7951 return ERR_PTR(ret);
d2dff872
CW
7952}
7953
b5ea642a 7954static struct drm_framebuffer *
a8bb6818
DV
7955intel_framebuffer_create(struct drm_device *dev,
7956 struct drm_mode_fb_cmd2 *mode_cmd,
7957 struct drm_i915_gem_object *obj)
7958{
7959 struct drm_framebuffer *fb;
7960 int ret;
7961
7962 ret = i915_mutex_lock_interruptible(dev);
7963 if (ret)
7964 return ERR_PTR(ret);
7965 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
7966 mutex_unlock(&dev->struct_mutex);
7967
7968 return fb;
7969}
7970
d2dff872
CW
7971static u32
7972intel_framebuffer_pitch_for_width(int width, int bpp)
7973{
7974 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
7975 return ALIGN(pitch, 64);
7976}
7977
7978static u32
7979intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
7980{
7981 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
7982 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
7983}
7984
7985static struct drm_framebuffer *
7986intel_framebuffer_create_for_mode(struct drm_device *dev,
7987 struct drm_display_mode *mode,
7988 int depth, int bpp)
7989{
7990 struct drm_i915_gem_object *obj;
0fed39bd 7991 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
d2dff872
CW
7992
7993 obj = i915_gem_alloc_object(dev,
7994 intel_framebuffer_size_for_mode(mode, bpp));
7995 if (obj == NULL)
7996 return ERR_PTR(-ENOMEM);
7997
7998 mode_cmd.width = mode->hdisplay;
7999 mode_cmd.height = mode->vdisplay;
308e5bcb
JB
8000 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8001 bpp);
5ca0c34a 8002 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
d2dff872
CW
8003
8004 return intel_framebuffer_create(dev, &mode_cmd, obj);
8005}
8006
8007static struct drm_framebuffer *
8008mode_fits_in_fbdev(struct drm_device *dev,
8009 struct drm_display_mode *mode)
8010{
4520f53a 8011#ifdef CONFIG_DRM_I915_FBDEV
d2dff872
CW
8012 struct drm_i915_private *dev_priv = dev->dev_private;
8013 struct drm_i915_gem_object *obj;
8014 struct drm_framebuffer *fb;
8015
4c0e5528 8016 if (!dev_priv->fbdev)
d2dff872
CW
8017 return NULL;
8018
4c0e5528 8019 if (!dev_priv->fbdev->fb)
d2dff872
CW
8020 return NULL;
8021
4c0e5528
DV
8022 obj = dev_priv->fbdev->fb->obj;
8023 BUG_ON(!obj);
8024
8bcd4553 8025 fb = &dev_priv->fbdev->fb->base;
01f2c773
VS
8026 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8027 fb->bits_per_pixel))
d2dff872
CW
8028 return NULL;
8029
01f2c773 8030 if (obj->base.size < mode->vdisplay * fb->pitches[0])
d2dff872
CW
8031 return NULL;
8032
8033 return fb;
4520f53a
DV
8034#else
8035 return NULL;
8036#endif
d2dff872
CW
8037}
8038
d2434ab7 8039bool intel_get_load_detect_pipe(struct drm_connector *connector,
7173188d 8040 struct drm_display_mode *mode,
8261b191 8041 struct intel_load_detect_pipe *old)
79e53945
JB
8042{
8043 struct intel_crtc *intel_crtc;
d2434ab7
DV
8044 struct intel_encoder *intel_encoder =
8045 intel_attached_encoder(connector);
79e53945 8046 struct drm_crtc *possible_crtc;
4ef69c7a 8047 struct drm_encoder *encoder = &intel_encoder->base;
79e53945
JB
8048 struct drm_crtc *crtc = NULL;
8049 struct drm_device *dev = encoder->dev;
94352cf9 8050 struct drm_framebuffer *fb;
79e53945
JB
8051 int i = -1;
8052
d2dff872
CW
8053 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8054 connector->base.id, drm_get_connector_name(connector),
8055 encoder->base.id, drm_get_encoder_name(encoder));
8056
79e53945
JB
8057 /*
8058 * Algorithm gets a little messy:
7a5e4805 8059 *
79e53945
JB
8060 * - if the connector already has an assigned crtc, use it (but make
8061 * sure it's on first)
7a5e4805 8062 *
79e53945
JB
8063 * - try to find the first unused crtc that can drive this connector,
8064 * and use that if we find one
79e53945
JB
8065 */
8066
8067 /* See if we already have a CRTC for this connector */
8068 if (encoder->crtc) {
8069 crtc = encoder->crtc;
8261b191 8070
7b24056b
DV
8071 mutex_lock(&crtc->mutex);
8072
24218aac 8073 old->dpms_mode = connector->dpms;
8261b191
CW
8074 old->load_detect_temp = false;
8075
8076 /* Make sure the crtc and connector are running */
24218aac
DV
8077 if (connector->dpms != DRM_MODE_DPMS_ON)
8078 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
8261b191 8079
7173188d 8080 return true;
79e53945
JB
8081 }
8082
8083 /* Find an unused one (if possible) */
8084 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
8085 i++;
8086 if (!(encoder->possible_crtcs & (1 << i)))
8087 continue;
8088 if (!possible_crtc->enabled) {
8089 crtc = possible_crtc;
8090 break;
8091 }
79e53945
JB
8092 }
8093
8094 /*
8095 * If we didn't find an unused CRTC, don't use any.
8096 */
8097 if (!crtc) {
7173188d
CW
8098 DRM_DEBUG_KMS("no pipe available for load-detect\n");
8099 return false;
79e53945
JB
8100 }
8101
7b24056b 8102 mutex_lock(&crtc->mutex);
fc303101
DV
8103 intel_encoder->new_crtc = to_intel_crtc(crtc);
8104 to_intel_connector(connector)->new_encoder = intel_encoder;
79e53945
JB
8105
8106 intel_crtc = to_intel_crtc(crtc);
412b61d8
VS
8107 intel_crtc->new_enabled = true;
8108 intel_crtc->new_config = &intel_crtc->config;
24218aac 8109 old->dpms_mode = connector->dpms;
8261b191 8110 old->load_detect_temp = true;
d2dff872 8111 old->release_fb = NULL;
79e53945 8112
6492711d
CW
8113 if (!mode)
8114 mode = &load_detect_mode;
79e53945 8115
d2dff872
CW
8116 /* We need a framebuffer large enough to accommodate all accesses
8117 * that the plane may generate whilst we perform load detection.
8118 * We can not rely on the fbcon either being present (we get called
8119 * during its initialisation to detect all boot displays, or it may
8120 * not even exist) or that it is large enough to satisfy the
8121 * requested mode.
8122 */
94352cf9
DV
8123 fb = mode_fits_in_fbdev(dev, mode);
8124 if (fb == NULL) {
d2dff872 8125 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
94352cf9
DV
8126 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8127 old->release_fb = fb;
d2dff872
CW
8128 } else
8129 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
94352cf9 8130 if (IS_ERR(fb)) {
d2dff872 8131 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
412b61d8 8132 goto fail;
79e53945 8133 }
79e53945 8134
c0c36b94 8135 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
6492711d 8136 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
d2dff872
CW
8137 if (old->release_fb)
8138 old->release_fb->funcs->destroy(old->release_fb);
412b61d8 8139 goto fail;
79e53945 8140 }
7173188d 8141
79e53945 8142 /* let the connector get through one full cycle before testing */
9d0498a2 8143 intel_wait_for_vblank(dev, intel_crtc->pipe);
7173188d 8144 return true;
412b61d8
VS
8145
8146 fail:
8147 intel_crtc->new_enabled = crtc->enabled;
8148 if (intel_crtc->new_enabled)
8149 intel_crtc->new_config = &intel_crtc->config;
8150 else
8151 intel_crtc->new_config = NULL;
8152 mutex_unlock(&crtc->mutex);
8153 return false;
79e53945
JB
8154}
8155
d2434ab7 8156void intel_release_load_detect_pipe(struct drm_connector *connector,
8261b191 8157 struct intel_load_detect_pipe *old)
79e53945 8158{
d2434ab7
DV
8159 struct intel_encoder *intel_encoder =
8160 intel_attached_encoder(connector);
4ef69c7a 8161 struct drm_encoder *encoder = &intel_encoder->base;
7b24056b 8162 struct drm_crtc *crtc = encoder->crtc;
412b61d8 8163 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
79e53945 8164
d2dff872
CW
8165 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
8166 connector->base.id, drm_get_connector_name(connector),
8167 encoder->base.id, drm_get_encoder_name(encoder));
8168
8261b191 8169 if (old->load_detect_temp) {
fc303101
DV
8170 to_intel_connector(connector)->new_encoder = NULL;
8171 intel_encoder->new_crtc = NULL;
412b61d8
VS
8172 intel_crtc->new_enabled = false;
8173 intel_crtc->new_config = NULL;
fc303101 8174 intel_set_mode(crtc, NULL, 0, 0, NULL);
d2dff872 8175
36206361
DV
8176 if (old->release_fb) {
8177 drm_framebuffer_unregister_private(old->release_fb);
8178 drm_framebuffer_unreference(old->release_fb);
8179 }
d2dff872 8180
67c96400 8181 mutex_unlock(&crtc->mutex);
0622a53c 8182 return;
79e53945
JB
8183 }
8184
c751ce4f 8185 /* Switch crtc and encoder back off if necessary */
24218aac
DV
8186 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8187 connector->funcs->dpms(connector, old->dpms_mode);
7b24056b
DV
8188
8189 mutex_unlock(&crtc->mutex);
79e53945
JB
8190}
8191
da4a1efa
VS
8192static int i9xx_pll_refclk(struct drm_device *dev,
8193 const struct intel_crtc_config *pipe_config)
8194{
8195 struct drm_i915_private *dev_priv = dev->dev_private;
8196 u32 dpll = pipe_config->dpll_hw_state.dpll;
8197
8198 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
e91e941b 8199 return dev_priv->vbt.lvds_ssc_freq;
da4a1efa
VS
8200 else if (HAS_PCH_SPLIT(dev))
8201 return 120000;
8202 else if (!IS_GEN2(dev))
8203 return 96000;
8204 else
8205 return 48000;
8206}
8207
79e53945 8208/* Returns the clock of the currently programmed mode of the given pipe. */
f1f644dc
JB
8209static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8210 struct intel_crtc_config *pipe_config)
79e53945 8211{
f1f644dc 8212 struct drm_device *dev = crtc->base.dev;
79e53945 8213 struct drm_i915_private *dev_priv = dev->dev_private;
f1f644dc 8214 int pipe = pipe_config->cpu_transcoder;
293623f7 8215 u32 dpll = pipe_config->dpll_hw_state.dpll;
79e53945
JB
8216 u32 fp;
8217 intel_clock_t clock;
da4a1efa 8218 int refclk = i9xx_pll_refclk(dev, pipe_config);
79e53945
JB
8219
8220 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
293623f7 8221 fp = pipe_config->dpll_hw_state.fp0;
79e53945 8222 else
293623f7 8223 fp = pipe_config->dpll_hw_state.fp1;
79e53945
JB
8224
8225 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
f2b115e6
AJ
8226 if (IS_PINEVIEW(dev)) {
8227 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8228 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
2177832f
SL
8229 } else {
8230 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8231 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8232 }
8233
a6c45cf0 8234 if (!IS_GEN2(dev)) {
f2b115e6
AJ
8235 if (IS_PINEVIEW(dev))
8236 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8237 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
2177832f
SL
8238 else
8239 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
79e53945
JB
8240 DPLL_FPA01_P1_POST_DIV_SHIFT);
8241
8242 switch (dpll & DPLL_MODE_MASK) {
8243 case DPLLB_MODE_DAC_SERIAL:
8244 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8245 5 : 10;
8246 break;
8247 case DPLLB_MODE_LVDS:
8248 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8249 7 : 14;
8250 break;
8251 default:
28c97730 8252 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
79e53945 8253 "mode\n", (int)(dpll & DPLL_MODE_MASK));
f1f644dc 8254 return;
79e53945
JB
8255 }
8256
ac58c3f0 8257 if (IS_PINEVIEW(dev))
da4a1efa 8258 pineview_clock(refclk, &clock);
ac58c3f0 8259 else
da4a1efa 8260 i9xx_clock(refclk, &clock);
79e53945 8261 } else {
0fb58223 8262 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
b1c560d1 8263 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
79e53945
JB
8264
8265 if (is_lvds) {
8266 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8267 DPLL_FPA01_P1_POST_DIV_SHIFT);
b1c560d1
VS
8268
8269 if (lvds & LVDS_CLKB_POWER_UP)
8270 clock.p2 = 7;
8271 else
8272 clock.p2 = 14;
79e53945
JB
8273 } else {
8274 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8275 clock.p1 = 2;
8276 else {
8277 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8278 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8279 }
8280 if (dpll & PLL_P2_DIVIDE_BY_4)
8281 clock.p2 = 4;
8282 else
8283 clock.p2 = 2;
79e53945 8284 }
da4a1efa
VS
8285
8286 i9xx_clock(refclk, &clock);
79e53945
JB
8287 }
8288
18442d08
VS
8289 /*
8290 * This value includes pixel_multiplier. We will use
241bfc38 8291 * port_clock to compute adjusted_mode.crtc_clock in the
18442d08
VS
8292 * encoder's get_config() function.
8293 */
8294 pipe_config->port_clock = clock.dot;
f1f644dc
JB
8295}
8296
6878da05
VS
8297int intel_dotclock_calculate(int link_freq,
8298 const struct intel_link_m_n *m_n)
f1f644dc 8299{
f1f644dc
JB
8300 /*
8301 * The calculation for the data clock is:
1041a02f 8302 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
f1f644dc 8303 * But we want to avoid losing precison if possible, so:
1041a02f 8304 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
f1f644dc
JB
8305 *
8306 * and the link clock is simpler:
1041a02f 8307 * link_clock = (m * link_clock) / n
f1f644dc
JB
8308 */
8309
6878da05
VS
8310 if (!m_n->link_n)
8311 return 0;
f1f644dc 8312
6878da05
VS
8313 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8314}
f1f644dc 8315
18442d08
VS
8316static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8317 struct intel_crtc_config *pipe_config)
6878da05
VS
8318{
8319 struct drm_device *dev = crtc->base.dev;
79e53945 8320
18442d08
VS
8321 /* read out port_clock from the DPLL */
8322 i9xx_crtc_clock_get(crtc, pipe_config);
f1f644dc 8323
f1f644dc 8324 /*
18442d08 8325 * This value does not include pixel_multiplier.
241bfc38 8326 * We will check that port_clock and adjusted_mode.crtc_clock
18442d08
VS
8327 * agree once we know their relationship in the encoder's
8328 * get_config() function.
79e53945 8329 */
241bfc38 8330 pipe_config->adjusted_mode.crtc_clock =
18442d08
VS
8331 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8332 &pipe_config->fdi_m_n);
79e53945
JB
8333}
8334
8335/** Returns the currently programmed mode of the given pipe. */
8336struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8337 struct drm_crtc *crtc)
8338{
548f245b 8339 struct drm_i915_private *dev_priv = dev->dev_private;
79e53945 8340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3b117c8f 8341 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
79e53945 8342 struct drm_display_mode *mode;
f1f644dc 8343 struct intel_crtc_config pipe_config;
fe2b8f9d
PZ
8344 int htot = I915_READ(HTOTAL(cpu_transcoder));
8345 int hsync = I915_READ(HSYNC(cpu_transcoder));
8346 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8347 int vsync = I915_READ(VSYNC(cpu_transcoder));
293623f7 8348 enum pipe pipe = intel_crtc->pipe;
79e53945
JB
8349
8350 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8351 if (!mode)
8352 return NULL;
8353
f1f644dc
JB
8354 /*
8355 * Construct a pipe_config sufficient for getting the clock info
8356 * back out of crtc_clock_get.
8357 *
8358 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8359 * to use a real value here instead.
8360 */
293623f7 8361 pipe_config.cpu_transcoder = (enum transcoder) pipe;
f1f644dc 8362 pipe_config.pixel_multiplier = 1;
293623f7
VS
8363 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8364 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8365 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
f1f644dc
JB
8366 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8367
773ae034 8368 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
79e53945
JB
8369 mode->hdisplay = (htot & 0xffff) + 1;
8370 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8371 mode->hsync_start = (hsync & 0xffff) + 1;
8372 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8373 mode->vdisplay = (vtot & 0xffff) + 1;
8374 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8375 mode->vsync_start = (vsync & 0xffff) + 1;
8376 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8377
8378 drm_mode_set_name(mode);
79e53945
JB
8379
8380 return mode;
8381}
8382
3dec0095 8383static void intel_increase_pllclock(struct drm_crtc *crtc)
652c393a
JB
8384{
8385 struct drm_device *dev = crtc->dev;
8386 drm_i915_private_t *dev_priv = dev->dev_private;
8387 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8388 int pipe = intel_crtc->pipe;
dbdc6479
JB
8389 int dpll_reg = DPLL(pipe);
8390 int dpll;
652c393a 8391
bad720ff 8392 if (HAS_PCH_SPLIT(dev))
652c393a
JB
8393 return;
8394
8395 if (!dev_priv->lvds_downclock_avail)
8396 return;
8397
dbdc6479 8398 dpll = I915_READ(dpll_reg);
652c393a 8399 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
44d98a61 8400 DRM_DEBUG_DRIVER("upclocking LVDS\n");
652c393a 8401
8ac5a6d5 8402 assert_panel_unlocked(dev_priv, pipe);
652c393a
JB
8403
8404 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8405 I915_WRITE(dpll_reg, dpll);
9d0498a2 8406 intel_wait_for_vblank(dev, pipe);
dbdc6479 8407
652c393a
JB
8408 dpll = I915_READ(dpll_reg);
8409 if (dpll & DISPLAY_RATE_SELECT_FPA1)
44d98a61 8410 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
652c393a 8411 }
652c393a
JB
8412}
8413
8414static void intel_decrease_pllclock(struct drm_crtc *crtc)
8415{
8416 struct drm_device *dev = crtc->dev;
8417 drm_i915_private_t *dev_priv = dev->dev_private;
8418 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
652c393a 8419
bad720ff 8420 if (HAS_PCH_SPLIT(dev))
652c393a
JB
8421 return;
8422
8423 if (!dev_priv->lvds_downclock_avail)
8424 return;
8425
8426 /*
8427 * Since this is called by a timer, we should never get here in
8428 * the manual case.
8429 */
8430 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
dc257cf1
DV
8431 int pipe = intel_crtc->pipe;
8432 int dpll_reg = DPLL(pipe);
8433 int dpll;
f6e5b160 8434
44d98a61 8435 DRM_DEBUG_DRIVER("downclocking LVDS\n");
652c393a 8436
8ac5a6d5 8437 assert_panel_unlocked(dev_priv, pipe);
652c393a 8438
dc257cf1 8439 dpll = I915_READ(dpll_reg);
652c393a
JB
8440 dpll |= DISPLAY_RATE_SELECT_FPA1;
8441 I915_WRITE(dpll_reg, dpll);
9d0498a2 8442 intel_wait_for_vblank(dev, pipe);
652c393a
JB
8443 dpll = I915_READ(dpll_reg);
8444 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
44d98a61 8445 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
652c393a
JB
8446 }
8447
8448}
8449
f047e395
CW
8450void intel_mark_busy(struct drm_device *dev)
8451{
c67a470b
PZ
8452 struct drm_i915_private *dev_priv = dev->dev_private;
8453
f62a0076
CW
8454 if (dev_priv->mm.busy)
8455 return;
8456
86c4ec0d 8457 hsw_disable_package_c8(dev_priv);
c67a470b 8458 i915_update_gfx_val(dev_priv);
f62a0076 8459 dev_priv->mm.busy = true;
f047e395
CW
8460}
8461
8462void intel_mark_idle(struct drm_device *dev)
652c393a 8463{
c67a470b 8464 struct drm_i915_private *dev_priv = dev->dev_private;
652c393a 8465 struct drm_crtc *crtc;
652c393a 8466
f62a0076
CW
8467 if (!dev_priv->mm.busy)
8468 return;
8469
8470 dev_priv->mm.busy = false;
8471
d330a953 8472 if (!i915.powersave)
bb4cdd53 8473 goto out;
652c393a 8474
652c393a 8475 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
652c393a
JB
8476 if (!crtc->fb)
8477 continue;
8478
725a5b54 8479 intel_decrease_pllclock(crtc);
652c393a 8480 }
b29c19b6 8481
3d13ef2e 8482 if (INTEL_INFO(dev)->gen >= 6)
b29c19b6 8483 gen6_rps_idle(dev->dev_private);
bb4cdd53
PZ
8484
8485out:
86c4ec0d 8486 hsw_enable_package_c8(dev_priv);
652c393a
JB
8487}
8488
c65355bb
CW
8489void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
8490 struct intel_ring_buffer *ring)
652c393a 8491{
f047e395
CW
8492 struct drm_device *dev = obj->base.dev;
8493 struct drm_crtc *crtc;
652c393a 8494
d330a953 8495 if (!i915.powersave)
acb87dfb
CW
8496 return;
8497
652c393a
JB
8498 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
8499 if (!crtc->fb)
8500 continue;
8501
c65355bb
CW
8502 if (to_intel_framebuffer(crtc->fb)->obj != obj)
8503 continue;
8504
8505 intel_increase_pllclock(crtc);
8506 if (ring && intel_fbc_enabled(dev))
8507 ring->fbc_dirty = true;
652c393a
JB
8508 }
8509}
8510
79e53945
JB
8511static void intel_crtc_destroy(struct drm_crtc *crtc)
8512{
8513 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
67e77c5a
DV
8514 struct drm_device *dev = crtc->dev;
8515 struct intel_unpin_work *work;
8516 unsigned long flags;
8517
8518 spin_lock_irqsave(&dev->event_lock, flags);
8519 work = intel_crtc->unpin_work;
8520 intel_crtc->unpin_work = NULL;
8521 spin_unlock_irqrestore(&dev->event_lock, flags);
8522
8523 if (work) {
8524 cancel_work_sync(&work->work);
8525 kfree(work);
8526 }
79e53945 8527
40ccc72b
MK
8528 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8529
79e53945 8530 drm_crtc_cleanup(crtc);
67e77c5a 8531
79e53945
JB
8532 kfree(intel_crtc);
8533}
8534
6b95a207
KH
8535static void intel_unpin_work_fn(struct work_struct *__work)
8536{
8537 struct intel_unpin_work *work =
8538 container_of(__work, struct intel_unpin_work, work);
b4a98e57 8539 struct drm_device *dev = work->crtc->dev;
6b95a207 8540
b4a98e57 8541 mutex_lock(&dev->struct_mutex);
1690e1eb 8542 intel_unpin_fb_obj(work->old_fb_obj);
05394f39
CW
8543 drm_gem_object_unreference(&work->pending_flip_obj->base);
8544 drm_gem_object_unreference(&work->old_fb_obj->base);
d9e86c0e 8545
b4a98e57
CW
8546 intel_update_fbc(dev);
8547 mutex_unlock(&dev->struct_mutex);
8548
8549 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8550 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8551
6b95a207
KH
8552 kfree(work);
8553}
8554
1afe3e9d 8555static void do_intel_finish_page_flip(struct drm_device *dev,
49b14a5c 8556 struct drm_crtc *crtc)
6b95a207
KH
8557{
8558 drm_i915_private_t *dev_priv = dev->dev_private;
6b95a207
KH
8559 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8560 struct intel_unpin_work *work;
6b95a207
KH
8561 unsigned long flags;
8562
8563 /* Ignore early vblank irqs */
8564 if (intel_crtc == NULL)
8565 return;
8566
8567 spin_lock_irqsave(&dev->event_lock, flags);
8568 work = intel_crtc->unpin_work;
e7d841ca
CW
8569
8570 /* Ensure we don't miss a work->pending update ... */
8571 smp_rmb();
8572
8573 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
6b95a207
KH
8574 spin_unlock_irqrestore(&dev->event_lock, flags);
8575 return;
8576 }
8577
e7d841ca
CW
8578 /* and that the unpin work is consistent wrt ->pending. */
8579 smp_rmb();
8580
6b95a207 8581 intel_crtc->unpin_work = NULL;
6b95a207 8582
45a066eb
RC
8583 if (work->event)
8584 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
6b95a207 8585
0af7e4df
MK
8586 drm_vblank_put(dev, intel_crtc->pipe);
8587
6b95a207
KH
8588 spin_unlock_irqrestore(&dev->event_lock, flags);
8589
2c10d571 8590 wake_up_all(&dev_priv->pending_flip_queue);
b4a98e57
CW
8591
8592 queue_work(dev_priv->wq, &work->work);
e5510fac
JB
8593
8594 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
6b95a207
KH
8595}
8596
1afe3e9d
JB
8597void intel_finish_page_flip(struct drm_device *dev, int pipe)
8598{
8599 drm_i915_private_t *dev_priv = dev->dev_private;
8600 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8601
49b14a5c 8602 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
8603}
8604
8605void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8606{
8607 drm_i915_private_t *dev_priv = dev->dev_private;
8608 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8609
49b14a5c 8610 do_intel_finish_page_flip(dev, crtc);
1afe3e9d
JB
8611}
8612
6b95a207
KH
8613void intel_prepare_page_flip(struct drm_device *dev, int plane)
8614{
8615 drm_i915_private_t *dev_priv = dev->dev_private;
8616 struct intel_crtc *intel_crtc =
8617 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8618 unsigned long flags;
8619
e7d841ca
CW
8620 /* NB: An MMIO update of the plane base pointer will also
8621 * generate a page-flip completion irq, i.e. every modeset
8622 * is also accompanied by a spurious intel_prepare_page_flip().
8623 */
6b95a207 8624 spin_lock_irqsave(&dev->event_lock, flags);
e7d841ca
CW
8625 if (intel_crtc->unpin_work)
8626 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
6b95a207
KH
8627 spin_unlock_irqrestore(&dev->event_lock, flags);
8628}
8629
e7d841ca
CW
8630inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
8631{
8632 /* Ensure that the work item is consistent when activating it ... */
8633 smp_wmb();
8634 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8635 /* and that it is marked active as soon as the irq could fire. */
8636 smp_wmb();
8637}
8638
8c9f3aaf
JB
8639static int intel_gen2_queue_flip(struct drm_device *dev,
8640 struct drm_crtc *crtc,
8641 struct drm_framebuffer *fb,
ed8d1975
KP
8642 struct drm_i915_gem_object *obj,
8643 uint32_t flags)
8c9f3aaf
JB
8644{
8645 struct drm_i915_private *dev_priv = dev->dev_private;
8646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf 8647 u32 flip_mask;
6d90c952 8648 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
8c9f3aaf
JB
8649 int ret;
8650
6d90c952 8651 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf 8652 if (ret)
83d4092b 8653 goto err;
8c9f3aaf 8654
6d90c952 8655 ret = intel_ring_begin(ring, 6);
8c9f3aaf 8656 if (ret)
83d4092b 8657 goto err_unpin;
8c9f3aaf
JB
8658
8659 /* Can't queue multiple flips, so wait for the previous
8660 * one to finish before executing the next.
8661 */
8662 if (intel_crtc->plane)
8663 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8664 else
8665 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
8666 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8667 intel_ring_emit(ring, MI_NOOP);
8668 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8669 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8670 intel_ring_emit(ring, fb->pitches[0]);
f343c5f6 8671 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
6d90c952 8672 intel_ring_emit(ring, 0); /* aux display base address, unused */
e7d841ca
CW
8673
8674 intel_mark_page_flip_active(intel_crtc);
09246732 8675 __intel_ring_advance(ring);
83d4092b
CW
8676 return 0;
8677
8678err_unpin:
8679 intel_unpin_fb_obj(obj);
8680err:
8c9f3aaf
JB
8681 return ret;
8682}
8683
8684static int intel_gen3_queue_flip(struct drm_device *dev,
8685 struct drm_crtc *crtc,
8686 struct drm_framebuffer *fb,
ed8d1975
KP
8687 struct drm_i915_gem_object *obj,
8688 uint32_t flags)
8c9f3aaf
JB
8689{
8690 struct drm_i915_private *dev_priv = dev->dev_private;
8691 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8c9f3aaf 8692 u32 flip_mask;
6d90c952 8693 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
8c9f3aaf
JB
8694 int ret;
8695
6d90c952 8696 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf 8697 if (ret)
83d4092b 8698 goto err;
8c9f3aaf 8699
6d90c952 8700 ret = intel_ring_begin(ring, 6);
8c9f3aaf 8701 if (ret)
83d4092b 8702 goto err_unpin;
8c9f3aaf
JB
8703
8704 if (intel_crtc->plane)
8705 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
8706 else
8707 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
6d90c952
DV
8708 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
8709 intel_ring_emit(ring, MI_NOOP);
8710 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
8711 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8712 intel_ring_emit(ring, fb->pitches[0]);
f343c5f6 8713 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
6d90c952
DV
8714 intel_ring_emit(ring, MI_NOOP);
8715
e7d841ca 8716 intel_mark_page_flip_active(intel_crtc);
09246732 8717 __intel_ring_advance(ring);
83d4092b
CW
8718 return 0;
8719
8720err_unpin:
8721 intel_unpin_fb_obj(obj);
8722err:
8c9f3aaf
JB
8723 return ret;
8724}
8725
8726static int intel_gen4_queue_flip(struct drm_device *dev,
8727 struct drm_crtc *crtc,
8728 struct drm_framebuffer *fb,
ed8d1975
KP
8729 struct drm_i915_gem_object *obj,
8730 uint32_t flags)
8c9f3aaf
JB
8731{
8732 struct drm_i915_private *dev_priv = dev->dev_private;
8733 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8734 uint32_t pf, pipesrc;
6d90c952 8735 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
8c9f3aaf
JB
8736 int ret;
8737
6d90c952 8738 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf 8739 if (ret)
83d4092b 8740 goto err;
8c9f3aaf 8741
6d90c952 8742 ret = intel_ring_begin(ring, 4);
8c9f3aaf 8743 if (ret)
83d4092b 8744 goto err_unpin;
8c9f3aaf
JB
8745
8746 /* i965+ uses the linear or tiled offsets from the
8747 * Display Registers (which do not change across a page-flip)
8748 * so we need only reprogram the base address.
8749 */
6d90c952
DV
8750 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8751 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8752 intel_ring_emit(ring, fb->pitches[0]);
c2c75131 8753 intel_ring_emit(ring,
f343c5f6 8754 (i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset) |
c2c75131 8755 obj->tiling_mode);
8c9f3aaf
JB
8756
8757 /* XXX Enabling the panel-fitter across page-flip is so far
8758 * untested on non-native modes, so ignore it for now.
8759 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
8760 */
8761 pf = 0;
8762 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 8763 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
8764
8765 intel_mark_page_flip_active(intel_crtc);
09246732 8766 __intel_ring_advance(ring);
83d4092b
CW
8767 return 0;
8768
8769err_unpin:
8770 intel_unpin_fb_obj(obj);
8771err:
8c9f3aaf
JB
8772 return ret;
8773}
8774
8775static int intel_gen6_queue_flip(struct drm_device *dev,
8776 struct drm_crtc *crtc,
8777 struct drm_framebuffer *fb,
ed8d1975
KP
8778 struct drm_i915_gem_object *obj,
8779 uint32_t flags)
8c9f3aaf
JB
8780{
8781 struct drm_i915_private *dev_priv = dev->dev_private;
8782 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6d90c952 8783 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
8c9f3aaf
JB
8784 uint32_t pf, pipesrc;
8785 int ret;
8786
6d90c952 8787 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8c9f3aaf 8788 if (ret)
83d4092b 8789 goto err;
8c9f3aaf 8790
6d90c952 8791 ret = intel_ring_begin(ring, 4);
8c9f3aaf 8792 if (ret)
83d4092b 8793 goto err_unpin;
8c9f3aaf 8794
6d90c952
DV
8795 intel_ring_emit(ring, MI_DISPLAY_FLIP |
8796 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
8797 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
f343c5f6 8798 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
8c9f3aaf 8799
dc257cf1
DV
8800 /* Contrary to the suggestions in the documentation,
8801 * "Enable Panel Fitter" does not seem to be required when page
8802 * flipping with a non-native mode, and worse causes a normal
8803 * modeset to fail.
8804 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
8805 */
8806 pf = 0;
8c9f3aaf 8807 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
6d90c952 8808 intel_ring_emit(ring, pf | pipesrc);
e7d841ca
CW
8809
8810 intel_mark_page_flip_active(intel_crtc);
09246732 8811 __intel_ring_advance(ring);
83d4092b
CW
8812 return 0;
8813
8814err_unpin:
8815 intel_unpin_fb_obj(obj);
8816err:
8c9f3aaf
JB
8817 return ret;
8818}
8819
7c9017e5
JB
8820static int intel_gen7_queue_flip(struct drm_device *dev,
8821 struct drm_crtc *crtc,
8822 struct drm_framebuffer *fb,
ed8d1975
KP
8823 struct drm_i915_gem_object *obj,
8824 uint32_t flags)
7c9017e5
JB
8825{
8826 struct drm_i915_private *dev_priv = dev->dev_private;
8827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
ffe74d75 8828 struct intel_ring_buffer *ring;
cb05d8de 8829 uint32_t plane_bit = 0;
ffe74d75
CW
8830 int len, ret;
8831
8832 ring = obj->ring;
1c5fd085 8833 if (IS_VALLEYVIEW(dev) || ring == NULL || ring->id != RCS)
ffe74d75 8834 ring = &dev_priv->ring[BCS];
7c9017e5
JB
8835
8836 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
8837 if (ret)
83d4092b 8838 goto err;
7c9017e5 8839
cb05d8de
DV
8840 switch(intel_crtc->plane) {
8841 case PLANE_A:
8842 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
8843 break;
8844 case PLANE_B:
8845 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
8846 break;
8847 case PLANE_C:
8848 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
8849 break;
8850 default:
8851 WARN_ONCE(1, "unknown plane in flip command\n");
8852 ret = -ENODEV;
ab3951eb 8853 goto err_unpin;
cb05d8de
DV
8854 }
8855
ffe74d75
CW
8856 len = 4;
8857 if (ring->id == RCS)
8858 len += 6;
8859
f66fab8e
VS
8860 /*
8861 * BSpec MI_DISPLAY_FLIP for IVB:
8862 * "The full packet must be contained within the same cache line."
8863 *
8864 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
8865 * cacheline, if we ever start emitting more commands before
8866 * the MI_DISPLAY_FLIP we may need to first emit everything else,
8867 * then do the cacheline alignment, and finally emit the
8868 * MI_DISPLAY_FLIP.
8869 */
8870 ret = intel_ring_cacheline_align(ring);
8871 if (ret)
8872 goto err_unpin;
8873
ffe74d75 8874 ret = intel_ring_begin(ring, len);
7c9017e5 8875 if (ret)
83d4092b 8876 goto err_unpin;
7c9017e5 8877
ffe74d75
CW
8878 /* Unmask the flip-done completion message. Note that the bspec says that
8879 * we should do this for both the BCS and RCS, and that we must not unmask
8880 * more than one flip event at any time (or ensure that one flip message
8881 * can be sent by waiting for flip-done prior to queueing new flips).
8882 * Experimentation says that BCS works despite DERRMR masking all
8883 * flip-done completion events and that unmasking all planes at once
8884 * for the RCS also doesn't appear to drop events. Setting the DERRMR
8885 * to zero does lead to lockups within MI_DISPLAY_FLIP.
8886 */
8887 if (ring->id == RCS) {
8888 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
8889 intel_ring_emit(ring, DERRMR);
8890 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
8891 DERRMR_PIPEB_PRI_FLIP_DONE |
8892 DERRMR_PIPEC_PRI_FLIP_DONE));
22613c96
VS
8893 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
8894 MI_SRM_LRM_GLOBAL_GTT);
ffe74d75
CW
8895 intel_ring_emit(ring, DERRMR);
8896 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
8897 }
8898
cb05d8de 8899 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
01f2c773 8900 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
f343c5f6 8901 intel_ring_emit(ring, i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
7c9017e5 8902 intel_ring_emit(ring, (MI_NOOP));
e7d841ca
CW
8903
8904 intel_mark_page_flip_active(intel_crtc);
09246732 8905 __intel_ring_advance(ring);
83d4092b
CW
8906 return 0;
8907
8908err_unpin:
8909 intel_unpin_fb_obj(obj);
8910err:
7c9017e5
JB
8911 return ret;
8912}
8913
8c9f3aaf
JB
8914static int intel_default_queue_flip(struct drm_device *dev,
8915 struct drm_crtc *crtc,
8916 struct drm_framebuffer *fb,
ed8d1975
KP
8917 struct drm_i915_gem_object *obj,
8918 uint32_t flags)
8c9f3aaf
JB
8919{
8920 return -ENODEV;
8921}
8922
6b95a207
KH
8923static int intel_crtc_page_flip(struct drm_crtc *crtc,
8924 struct drm_framebuffer *fb,
ed8d1975
KP
8925 struct drm_pending_vblank_event *event,
8926 uint32_t page_flip_flags)
6b95a207
KH
8927{
8928 struct drm_device *dev = crtc->dev;
8929 struct drm_i915_private *dev_priv = dev->dev_private;
4a35f83b
VS
8930 struct drm_framebuffer *old_fb = crtc->fb;
8931 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
6b95a207
KH
8932 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8933 struct intel_unpin_work *work;
8c9f3aaf 8934 unsigned long flags;
52e68630 8935 int ret;
6b95a207 8936
e6a595d2
VS
8937 /* Can't change pixel format via MI display flips. */
8938 if (fb->pixel_format != crtc->fb->pixel_format)
8939 return -EINVAL;
8940
8941 /*
8942 * TILEOFF/LINOFF registers can't be changed via MI display flips.
8943 * Note that pitch changes could also affect these register.
8944 */
8945 if (INTEL_INFO(dev)->gen > 3 &&
8946 (fb->offsets[0] != crtc->fb->offsets[0] ||
8947 fb->pitches[0] != crtc->fb->pitches[0]))
8948 return -EINVAL;
8949
f900db47
CW
8950 if (i915_terminally_wedged(&dev_priv->gpu_error))
8951 goto out_hang;
8952
b14c5679 8953 work = kzalloc(sizeof(*work), GFP_KERNEL);
6b95a207
KH
8954 if (work == NULL)
8955 return -ENOMEM;
8956
6b95a207 8957 work->event = event;
b4a98e57 8958 work->crtc = crtc;
4a35f83b 8959 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
6b95a207
KH
8960 INIT_WORK(&work->work, intel_unpin_work_fn);
8961
7317c75e
JB
8962 ret = drm_vblank_get(dev, intel_crtc->pipe);
8963 if (ret)
8964 goto free_work;
8965
6b95a207
KH
8966 /* We borrow the event spin lock for protecting unpin_work */
8967 spin_lock_irqsave(&dev->event_lock, flags);
8968 if (intel_crtc->unpin_work) {
8969 spin_unlock_irqrestore(&dev->event_lock, flags);
8970 kfree(work);
7317c75e 8971 drm_vblank_put(dev, intel_crtc->pipe);
468f0b44
CW
8972
8973 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
6b95a207
KH
8974 return -EBUSY;
8975 }
8976 intel_crtc->unpin_work = work;
8977 spin_unlock_irqrestore(&dev->event_lock, flags);
8978
b4a98e57
CW
8979 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
8980 flush_workqueue(dev_priv->wq);
8981
79158103
CW
8982 ret = i915_mutex_lock_interruptible(dev);
8983 if (ret)
8984 goto cleanup;
6b95a207 8985
75dfca80 8986 /* Reference the objects for the scheduled work. */
05394f39
CW
8987 drm_gem_object_reference(&work->old_fb_obj->base);
8988 drm_gem_object_reference(&obj->base);
6b95a207
KH
8989
8990 crtc->fb = fb;
96b099fd 8991
e1f99ce6 8992 work->pending_flip_obj = obj;
e1f99ce6 8993
4e5359cd
SF
8994 work->enable_stall_check = true;
8995
b4a98e57 8996 atomic_inc(&intel_crtc->unpin_work_count);
10d83730 8997 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
e1f99ce6 8998
ed8d1975 8999 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, page_flip_flags);
8c9f3aaf
JB
9000 if (ret)
9001 goto cleanup_pending;
6b95a207 9002
7782de3b 9003 intel_disable_fbc(dev);
c65355bb 9004 intel_mark_fb_busy(obj, NULL);
6b95a207
KH
9005 mutex_unlock(&dev->struct_mutex);
9006
e5510fac
JB
9007 trace_i915_flip_request(intel_crtc->plane, obj);
9008
6b95a207 9009 return 0;
96b099fd 9010
8c9f3aaf 9011cleanup_pending:
b4a98e57 9012 atomic_dec(&intel_crtc->unpin_work_count);
4a35f83b 9013 crtc->fb = old_fb;
05394f39
CW
9014 drm_gem_object_unreference(&work->old_fb_obj->base);
9015 drm_gem_object_unreference(&obj->base);
96b099fd
CW
9016 mutex_unlock(&dev->struct_mutex);
9017
79158103 9018cleanup:
96b099fd
CW
9019 spin_lock_irqsave(&dev->event_lock, flags);
9020 intel_crtc->unpin_work = NULL;
9021 spin_unlock_irqrestore(&dev->event_lock, flags);
9022
7317c75e
JB
9023 drm_vblank_put(dev, intel_crtc->pipe);
9024free_work:
96b099fd
CW
9025 kfree(work);
9026
f900db47
CW
9027 if (ret == -EIO) {
9028out_hang:
9029 intel_crtc_wait_for_pending_flips(crtc);
9030 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
9031 if (ret == 0 && event)
9032 drm_send_vblank_event(dev, intel_crtc->pipe, event);
9033 }
96b099fd 9034 return ret;
6b95a207
KH
9035}
9036
f6e5b160 9037static struct drm_crtc_helper_funcs intel_helper_funcs = {
f6e5b160
CW
9038 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9039 .load_lut = intel_crtc_load_lut,
f6e5b160
CW
9040};
9041
9a935856
DV
9042/**
9043 * intel_modeset_update_staged_output_state
9044 *
9045 * Updates the staged output configuration state, e.g. after we've read out the
9046 * current hw state.
9047 */
9048static void intel_modeset_update_staged_output_state(struct drm_device *dev)
f6e5b160 9049{
7668851f 9050 struct intel_crtc *crtc;
9a935856
DV
9051 struct intel_encoder *encoder;
9052 struct intel_connector *connector;
f6e5b160 9053
9a935856
DV
9054 list_for_each_entry(connector, &dev->mode_config.connector_list,
9055 base.head) {
9056 connector->new_encoder =
9057 to_intel_encoder(connector->base.encoder);
9058 }
f6e5b160 9059
9a935856
DV
9060 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9061 base.head) {
9062 encoder->new_crtc =
9063 to_intel_crtc(encoder->base.crtc);
9064 }
7668851f
VS
9065
9066 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9067 base.head) {
9068 crtc->new_enabled = crtc->base.enabled;
7bd0a8e7
VS
9069
9070 if (crtc->new_enabled)
9071 crtc->new_config = &crtc->config;
9072 else
9073 crtc->new_config = NULL;
7668851f 9074 }
f6e5b160
CW
9075}
9076
9a935856
DV
9077/**
9078 * intel_modeset_commit_output_state
9079 *
9080 * This function copies the stage display pipe configuration to the real one.
9081 */
9082static void intel_modeset_commit_output_state(struct drm_device *dev)
9083{
7668851f 9084 struct intel_crtc *crtc;
9a935856
DV
9085 struct intel_encoder *encoder;
9086 struct intel_connector *connector;
f6e5b160 9087
9a935856
DV
9088 list_for_each_entry(connector, &dev->mode_config.connector_list,
9089 base.head) {
9090 connector->base.encoder = &connector->new_encoder->base;
9091 }
f6e5b160 9092
9a935856
DV
9093 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9094 base.head) {
9095 encoder->base.crtc = &encoder->new_crtc->base;
9096 }
7668851f
VS
9097
9098 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9099 base.head) {
9100 crtc->base.enabled = crtc->new_enabled;
9101 }
9a935856
DV
9102}
9103
050f7aeb
DV
9104static void
9105connected_sink_compute_bpp(struct intel_connector * connector,
9106 struct intel_crtc_config *pipe_config)
9107{
9108 int bpp = pipe_config->pipe_bpp;
9109
9110 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9111 connector->base.base.id,
9112 drm_get_connector_name(&connector->base));
9113
9114 /* Don't use an invalid EDID bpc value */
9115 if (connector->base.display_info.bpc &&
9116 connector->base.display_info.bpc * 3 < bpp) {
9117 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9118 bpp, connector->base.display_info.bpc*3);
9119 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9120 }
9121
9122 /* Clamp bpp to 8 on screens without EDID 1.4 */
9123 if (connector->base.display_info.bpc == 0 && bpp > 24) {
9124 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
9125 bpp);
9126 pipe_config->pipe_bpp = 24;
9127 }
9128}
9129
4e53c2e0 9130static int
050f7aeb
DV
9131compute_baseline_pipe_bpp(struct intel_crtc *crtc,
9132 struct drm_framebuffer *fb,
9133 struct intel_crtc_config *pipe_config)
4e53c2e0 9134{
050f7aeb
DV
9135 struct drm_device *dev = crtc->base.dev;
9136 struct intel_connector *connector;
4e53c2e0
DV
9137 int bpp;
9138
d42264b1
DV
9139 switch (fb->pixel_format) {
9140 case DRM_FORMAT_C8:
4e53c2e0
DV
9141 bpp = 8*3; /* since we go through a colormap */
9142 break;
d42264b1
DV
9143 case DRM_FORMAT_XRGB1555:
9144 case DRM_FORMAT_ARGB1555:
9145 /* checked in intel_framebuffer_init already */
9146 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
9147 return -EINVAL;
9148 case DRM_FORMAT_RGB565:
4e53c2e0
DV
9149 bpp = 6*3; /* min is 18bpp */
9150 break;
d42264b1
DV
9151 case DRM_FORMAT_XBGR8888:
9152 case DRM_FORMAT_ABGR8888:
9153 /* checked in intel_framebuffer_init already */
9154 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
9155 return -EINVAL;
9156 case DRM_FORMAT_XRGB8888:
9157 case DRM_FORMAT_ARGB8888:
4e53c2e0
DV
9158 bpp = 8*3;
9159 break;
d42264b1
DV
9160 case DRM_FORMAT_XRGB2101010:
9161 case DRM_FORMAT_ARGB2101010:
9162 case DRM_FORMAT_XBGR2101010:
9163 case DRM_FORMAT_ABGR2101010:
9164 /* checked in intel_framebuffer_init already */
9165 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
baba133a 9166 return -EINVAL;
4e53c2e0
DV
9167 bpp = 10*3;
9168 break;
baba133a 9169 /* TODO: gen4+ supports 16 bpc floating point, too. */
4e53c2e0
DV
9170 default:
9171 DRM_DEBUG_KMS("unsupported depth\n");
9172 return -EINVAL;
9173 }
9174
4e53c2e0
DV
9175 pipe_config->pipe_bpp = bpp;
9176
9177 /* Clamp display bpp to EDID value */
9178 list_for_each_entry(connector, &dev->mode_config.connector_list,
050f7aeb 9179 base.head) {
1b829e05
DV
9180 if (!connector->new_encoder ||
9181 connector->new_encoder->new_crtc != crtc)
4e53c2e0
DV
9182 continue;
9183
050f7aeb 9184 connected_sink_compute_bpp(connector, pipe_config);
4e53c2e0
DV
9185 }
9186
9187 return bpp;
9188}
9189
644db711
DV
9190static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
9191{
9192 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
9193 "type: 0x%x flags: 0x%x\n",
1342830c 9194 mode->crtc_clock,
644db711
DV
9195 mode->crtc_hdisplay, mode->crtc_hsync_start,
9196 mode->crtc_hsync_end, mode->crtc_htotal,
9197 mode->crtc_vdisplay, mode->crtc_vsync_start,
9198 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
9199}
9200
c0b03411
DV
9201static void intel_dump_pipe_config(struct intel_crtc *crtc,
9202 struct intel_crtc_config *pipe_config,
9203 const char *context)
9204{
9205 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
9206 context, pipe_name(crtc->pipe));
9207
9208 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
9209 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
9210 pipe_config->pipe_bpp, pipe_config->dither);
9211 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9212 pipe_config->has_pch_encoder,
9213 pipe_config->fdi_lanes,
9214 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
9215 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
9216 pipe_config->fdi_m_n.tu);
eb14cb74
VS
9217 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9218 pipe_config->has_dp_encoder,
9219 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
9220 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
9221 pipe_config->dp_m_n.tu);
c0b03411
DV
9222 DRM_DEBUG_KMS("requested mode:\n");
9223 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
9224 DRM_DEBUG_KMS("adjusted mode:\n");
9225 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
644db711 9226 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
d71b8d4a 9227 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
37327abd
VS
9228 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
9229 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
c0b03411
DV
9230 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
9231 pipe_config->gmch_pfit.control,
9232 pipe_config->gmch_pfit.pgm_ratios,
9233 pipe_config->gmch_pfit.lvds_border_bits);
fd4daa9c 9234 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
c0b03411 9235 pipe_config->pch_pfit.pos,
fd4daa9c
CW
9236 pipe_config->pch_pfit.size,
9237 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
42db64ef 9238 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
cf532bb2 9239 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
c0b03411
DV
9240}
9241
bc079e8b
VS
9242static bool encoders_cloneable(const struct intel_encoder *a,
9243 const struct intel_encoder *b)
accfc0c5 9244{
bc079e8b
VS
9245 /* masks could be asymmetric, so check both ways */
9246 return a == b || (a->cloneable & (1 << b->type) &&
9247 b->cloneable & (1 << a->type));
9248}
9249
9250static bool check_single_encoder_cloning(struct intel_crtc *crtc,
9251 struct intel_encoder *encoder)
9252{
9253 struct drm_device *dev = crtc->base.dev;
9254 struct intel_encoder *source_encoder;
9255
9256 list_for_each_entry(source_encoder,
9257 &dev->mode_config.encoder_list, base.head) {
9258 if (source_encoder->new_crtc != crtc)
9259 continue;
9260
9261 if (!encoders_cloneable(encoder, source_encoder))
9262 return false;
9263 }
9264
9265 return true;
9266}
9267
9268static bool check_encoder_cloning(struct intel_crtc *crtc)
9269{
9270 struct drm_device *dev = crtc->base.dev;
accfc0c5
DV
9271 struct intel_encoder *encoder;
9272
bc079e8b
VS
9273 list_for_each_entry(encoder,
9274 &dev->mode_config.encoder_list, base.head) {
9275 if (encoder->new_crtc != crtc)
accfc0c5
DV
9276 continue;
9277
bc079e8b
VS
9278 if (!check_single_encoder_cloning(crtc, encoder))
9279 return false;
accfc0c5
DV
9280 }
9281
bc079e8b 9282 return true;
accfc0c5
DV
9283}
9284
b8cecdf5
DV
9285static struct intel_crtc_config *
9286intel_modeset_pipe_config(struct drm_crtc *crtc,
4e53c2e0 9287 struct drm_framebuffer *fb,
b8cecdf5 9288 struct drm_display_mode *mode)
ee7b9f93 9289{
7758a113 9290 struct drm_device *dev = crtc->dev;
7758a113 9291 struct intel_encoder *encoder;
b8cecdf5 9292 struct intel_crtc_config *pipe_config;
e29c22c0
DV
9293 int plane_bpp, ret = -EINVAL;
9294 bool retry = true;
ee7b9f93 9295
bc079e8b 9296 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
accfc0c5
DV
9297 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
9298 return ERR_PTR(-EINVAL);
9299 }
9300
b8cecdf5
DV
9301 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9302 if (!pipe_config)
7758a113
DV
9303 return ERR_PTR(-ENOMEM);
9304
b8cecdf5
DV
9305 drm_mode_copy(&pipe_config->adjusted_mode, mode);
9306 drm_mode_copy(&pipe_config->requested_mode, mode);
37327abd 9307
e143a21c
DV
9308 pipe_config->cpu_transcoder =
9309 (enum transcoder) to_intel_crtc(crtc)->pipe;
c0d43d62 9310 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
b8cecdf5 9311
2960bc9c
ID
9312 /*
9313 * Sanitize sync polarity flags based on requested ones. If neither
9314 * positive or negative polarity is requested, treat this as meaning
9315 * negative polarity.
9316 */
9317 if (!(pipe_config->adjusted_mode.flags &
9318 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
9319 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
9320
9321 if (!(pipe_config->adjusted_mode.flags &
9322 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
9323 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
9324
050f7aeb
DV
9325 /* Compute a starting value for pipe_config->pipe_bpp taking the source
9326 * plane pixel format and any sink constraints into account. Returns the
9327 * source plane bpp so that dithering can be selected on mismatches
9328 * after encoders and crtc also have had their say. */
9329 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
9330 fb, pipe_config);
4e53c2e0
DV
9331 if (plane_bpp < 0)
9332 goto fail;
9333
e41a56be
VS
9334 /*
9335 * Determine the real pipe dimensions. Note that stereo modes can
9336 * increase the actual pipe size due to the frame doubling and
9337 * insertion of additional space for blanks between the frame. This
9338 * is stored in the crtc timings. We use the requested mode to do this
9339 * computation to clearly distinguish it from the adjusted mode, which
9340 * can be changed by the connectors in the below retry loop.
9341 */
9342 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
9343 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
9344 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
9345
e29c22c0 9346encoder_retry:
ef1b460d 9347 /* Ensure the port clock defaults are reset when retrying. */
ff9a6750 9348 pipe_config->port_clock = 0;
ef1b460d 9349 pipe_config->pixel_multiplier = 1;
ff9a6750 9350
135c81b8 9351 /* Fill in default crtc timings, allow encoders to overwrite them. */
6ce70f5e 9352 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
135c81b8 9353
7758a113
DV
9354 /* Pass our mode to the connectors and the CRTC to give them a chance to
9355 * adjust it according to limitations or connector properties, and also
9356 * a chance to reject the mode entirely.
47f1c6c9 9357 */
7758a113
DV
9358 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9359 base.head) {
47f1c6c9 9360
7758a113
DV
9361 if (&encoder->new_crtc->base != crtc)
9362 continue;
7ae89233 9363
efea6e8e
DV
9364 if (!(encoder->compute_config(encoder, pipe_config))) {
9365 DRM_DEBUG_KMS("Encoder config failure\n");
7758a113
DV
9366 goto fail;
9367 }
ee7b9f93 9368 }
47f1c6c9 9369
ff9a6750
DV
9370 /* Set default port clock if not overwritten by the encoder. Needs to be
9371 * done afterwards in case the encoder adjusts the mode. */
9372 if (!pipe_config->port_clock)
241bfc38
DL
9373 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9374 * pipe_config->pixel_multiplier;
ff9a6750 9375
a43f6e0f 9376 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
e29c22c0 9377 if (ret < 0) {
7758a113
DV
9378 DRM_DEBUG_KMS("CRTC fixup failed\n");
9379 goto fail;
ee7b9f93 9380 }
e29c22c0
DV
9381
9382 if (ret == RETRY) {
9383 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9384 ret = -EINVAL;
9385 goto fail;
9386 }
9387
9388 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9389 retry = false;
9390 goto encoder_retry;
9391 }
9392
4e53c2e0
DV
9393 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9394 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9395 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9396
b8cecdf5 9397 return pipe_config;
7758a113 9398fail:
b8cecdf5 9399 kfree(pipe_config);
e29c22c0 9400 return ERR_PTR(ret);
ee7b9f93 9401}
47f1c6c9 9402
e2e1ed41
DV
9403/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9404 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9405static void
9406intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9407 unsigned *prepare_pipes, unsigned *disable_pipes)
79e53945
JB
9408{
9409 struct intel_crtc *intel_crtc;
e2e1ed41
DV
9410 struct drm_device *dev = crtc->dev;
9411 struct intel_encoder *encoder;
9412 struct intel_connector *connector;
9413 struct drm_crtc *tmp_crtc;
79e53945 9414
e2e1ed41 9415 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
79e53945 9416
e2e1ed41
DV
9417 /* Check which crtcs have changed outputs connected to them, these need
9418 * to be part of the prepare_pipes mask. We don't (yet) support global
9419 * modeset across multiple crtcs, so modeset_pipes will only have one
9420 * bit set at most. */
9421 list_for_each_entry(connector, &dev->mode_config.connector_list,
9422 base.head) {
9423 if (connector->base.encoder == &connector->new_encoder->base)
9424 continue;
79e53945 9425
e2e1ed41
DV
9426 if (connector->base.encoder) {
9427 tmp_crtc = connector->base.encoder->crtc;
9428
9429 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9430 }
9431
9432 if (connector->new_encoder)
9433 *prepare_pipes |=
9434 1 << connector->new_encoder->new_crtc->pipe;
79e53945
JB
9435 }
9436
e2e1ed41
DV
9437 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9438 base.head) {
9439 if (encoder->base.crtc == &encoder->new_crtc->base)
9440 continue;
9441
9442 if (encoder->base.crtc) {
9443 tmp_crtc = encoder->base.crtc;
9444
9445 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9446 }
9447
9448 if (encoder->new_crtc)
9449 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
80824003
JB
9450 }
9451
7668851f 9452 /* Check for pipes that will be enabled/disabled ... */
e2e1ed41
DV
9453 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9454 base.head) {
7668851f 9455 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
e2e1ed41 9456 continue;
7e7d76c3 9457
7668851f 9458 if (!intel_crtc->new_enabled)
e2e1ed41 9459 *disable_pipes |= 1 << intel_crtc->pipe;
7668851f
VS
9460 else
9461 *prepare_pipes |= 1 << intel_crtc->pipe;
7e7d76c3
JB
9462 }
9463
e2e1ed41
DV
9464
9465 /* set_mode is also used to update properties on life display pipes. */
9466 intel_crtc = to_intel_crtc(crtc);
7668851f 9467 if (intel_crtc->new_enabled)
e2e1ed41
DV
9468 *prepare_pipes |= 1 << intel_crtc->pipe;
9469
b6c5164d
DV
9470 /*
9471 * For simplicity do a full modeset on any pipe where the output routing
9472 * changed. We could be more clever, but that would require us to be
9473 * more careful with calling the relevant encoder->mode_set functions.
9474 */
e2e1ed41
DV
9475 if (*prepare_pipes)
9476 *modeset_pipes = *prepare_pipes;
9477
9478 /* ... and mask these out. */
9479 *modeset_pipes &= ~(*disable_pipes);
9480 *prepare_pipes &= ~(*disable_pipes);
b6c5164d
DV
9481
9482 /*
9483 * HACK: We don't (yet) fully support global modesets. intel_set_config
9484 * obies this rule, but the modeset restore mode of
9485 * intel_modeset_setup_hw_state does not.
9486 */
9487 *modeset_pipes &= 1 << intel_crtc->pipe;
9488 *prepare_pipes &= 1 << intel_crtc->pipe;
e3641d3f
DV
9489
9490 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9491 *modeset_pipes, *prepare_pipes, *disable_pipes);
47f1c6c9 9492}
79e53945 9493
ea9d758d 9494static bool intel_crtc_in_use(struct drm_crtc *crtc)
f6e5b160 9495{
ea9d758d 9496 struct drm_encoder *encoder;
f6e5b160 9497 struct drm_device *dev = crtc->dev;
f6e5b160 9498
ea9d758d
DV
9499 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9500 if (encoder->crtc == crtc)
9501 return true;
9502
9503 return false;
9504}
9505
9506static void
9507intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9508{
9509 struct intel_encoder *intel_encoder;
9510 struct intel_crtc *intel_crtc;
9511 struct drm_connector *connector;
9512
9513 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9514 base.head) {
9515 if (!intel_encoder->base.crtc)
9516 continue;
9517
9518 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9519
9520 if (prepare_pipes & (1 << intel_crtc->pipe))
9521 intel_encoder->connectors_active = false;
9522 }
9523
9524 intel_modeset_commit_output_state(dev);
9525
7668851f 9526 /* Double check state. */
ea9d758d
DV
9527 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
9528 base.head) {
7668851f 9529 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
7bd0a8e7
VS
9530 WARN_ON(intel_crtc->new_config &&
9531 intel_crtc->new_config != &intel_crtc->config);
9532 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
ea9d758d
DV
9533 }
9534
9535 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9536 if (!connector->encoder || !connector->encoder->crtc)
9537 continue;
9538
9539 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9540
9541 if (prepare_pipes & (1 << intel_crtc->pipe)) {
68d34720
DV
9542 struct drm_property *dpms_property =
9543 dev->mode_config.dpms_property;
9544
ea9d758d 9545 connector->dpms = DRM_MODE_DPMS_ON;
662595df 9546 drm_object_property_set_value(&connector->base,
68d34720
DV
9547 dpms_property,
9548 DRM_MODE_DPMS_ON);
ea9d758d
DV
9549
9550 intel_encoder = to_intel_encoder(connector->encoder);
9551 intel_encoder->connectors_active = true;
9552 }
9553 }
9554
9555}
9556
3bd26263 9557static bool intel_fuzzy_clock_check(int clock1, int clock2)
f1f644dc 9558{
3bd26263 9559 int diff;
f1f644dc
JB
9560
9561 if (clock1 == clock2)
9562 return true;
9563
9564 if (!clock1 || !clock2)
9565 return false;
9566
9567 diff = abs(clock1 - clock2);
9568
9569 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9570 return true;
9571
9572 return false;
9573}
9574
25c5b266
DV
9575#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9576 list_for_each_entry((intel_crtc), \
9577 &(dev)->mode_config.crtc_list, \
9578 base.head) \
0973f18f 9579 if (mask & (1 <<(intel_crtc)->pipe))
25c5b266 9580
0e8ffe1b 9581static bool
2fa2fe9a
DV
9582intel_pipe_config_compare(struct drm_device *dev,
9583 struct intel_crtc_config *current_config,
0e8ffe1b
DV
9584 struct intel_crtc_config *pipe_config)
9585{
66e985c0
DV
9586#define PIPE_CONF_CHECK_X(name) \
9587 if (current_config->name != pipe_config->name) { \
9588 DRM_ERROR("mismatch in " #name " " \
9589 "(expected 0x%08x, found 0x%08x)\n", \
9590 current_config->name, \
9591 pipe_config->name); \
9592 return false; \
9593 }
9594
08a24034
DV
9595#define PIPE_CONF_CHECK_I(name) \
9596 if (current_config->name != pipe_config->name) { \
9597 DRM_ERROR("mismatch in " #name " " \
9598 "(expected %i, found %i)\n", \
9599 current_config->name, \
9600 pipe_config->name); \
9601 return false; \
88adfff1
DV
9602 }
9603
1bd1bd80
DV
9604#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9605 if ((current_config->name ^ pipe_config->name) & (mask)) { \
6f02488e 9606 DRM_ERROR("mismatch in " #name "(" #mask ") " \
1bd1bd80
DV
9607 "(expected %i, found %i)\n", \
9608 current_config->name & (mask), \
9609 pipe_config->name & (mask)); \
9610 return false; \
9611 }
9612
5e550656
VS
9613#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9614 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9615 DRM_ERROR("mismatch in " #name " " \
9616 "(expected %i, found %i)\n", \
9617 current_config->name, \
9618 pipe_config->name); \
9619 return false; \
9620 }
9621
bb760063
DV
9622#define PIPE_CONF_QUIRK(quirk) \
9623 ((current_config->quirks | pipe_config->quirks) & (quirk))
9624
eccb140b
DV
9625 PIPE_CONF_CHECK_I(cpu_transcoder);
9626
08a24034
DV
9627 PIPE_CONF_CHECK_I(has_pch_encoder);
9628 PIPE_CONF_CHECK_I(fdi_lanes);
72419203
DV
9629 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9630 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9631 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9632 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9633 PIPE_CONF_CHECK_I(fdi_m_n.tu);
08a24034 9634
eb14cb74
VS
9635 PIPE_CONF_CHECK_I(has_dp_encoder);
9636 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9637 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9638 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9639 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9640 PIPE_CONF_CHECK_I(dp_m_n.tu);
9641
1bd1bd80
DV
9642 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9643 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9644 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9645 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9646 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9647 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9648
9649 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9650 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9651 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9652 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9653 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9654 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9655
c93f54cf 9656 PIPE_CONF_CHECK_I(pixel_multiplier);
6c49f241 9657
1bd1bd80
DV
9658 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9659 DRM_MODE_FLAG_INTERLACE);
9660
bb760063
DV
9661 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
9662 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9663 DRM_MODE_FLAG_PHSYNC);
9664 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9665 DRM_MODE_FLAG_NHSYNC);
9666 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9667 DRM_MODE_FLAG_PVSYNC);
9668 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
9669 DRM_MODE_FLAG_NVSYNC);
9670 }
045ac3b5 9671
37327abd
VS
9672 PIPE_CONF_CHECK_I(pipe_src_w);
9673 PIPE_CONF_CHECK_I(pipe_src_h);
1bd1bd80 9674
2fa2fe9a
DV
9675 PIPE_CONF_CHECK_I(gmch_pfit.control);
9676 /* pfit ratios are autocomputed by the hw on gen4+ */
9677 if (INTEL_INFO(dev)->gen < 4)
9678 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
9679 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
fd4daa9c
CW
9680 PIPE_CONF_CHECK_I(pch_pfit.enabled);
9681 if (current_config->pch_pfit.enabled) {
9682 PIPE_CONF_CHECK_I(pch_pfit.pos);
9683 PIPE_CONF_CHECK_I(pch_pfit.size);
9684 }
2fa2fe9a 9685
e59150dc
JB
9686 /* BDW+ don't expose a synchronous way to read the state */
9687 if (IS_HASWELL(dev))
9688 PIPE_CONF_CHECK_I(ips_enabled);
42db64ef 9689
282740f7
VS
9690 PIPE_CONF_CHECK_I(double_wide);
9691
c0d43d62 9692 PIPE_CONF_CHECK_I(shared_dpll);
66e985c0 9693 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
8bcc2795 9694 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
66e985c0
DV
9695 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
9696 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
c0d43d62 9697
42571aef
VS
9698 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
9699 PIPE_CONF_CHECK_I(pipe_bpp);
9700
a9a7e98a
JB
9701 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
9702 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
5e550656 9703
66e985c0 9704#undef PIPE_CONF_CHECK_X
08a24034 9705#undef PIPE_CONF_CHECK_I
1bd1bd80 9706#undef PIPE_CONF_CHECK_FLAGS
5e550656 9707#undef PIPE_CONF_CHECK_CLOCK_FUZZY
bb760063 9708#undef PIPE_CONF_QUIRK
88adfff1 9709
0e8ffe1b
DV
9710 return true;
9711}
9712
91d1b4bd
DV
9713static void
9714check_connector_state(struct drm_device *dev)
8af6cf88 9715{
8af6cf88
DV
9716 struct intel_connector *connector;
9717
9718 list_for_each_entry(connector, &dev->mode_config.connector_list,
9719 base.head) {
9720 /* This also checks the encoder/connector hw state with the
9721 * ->get_hw_state callbacks. */
9722 intel_connector_check_state(connector);
9723
9724 WARN(&connector->new_encoder->base != connector->base.encoder,
9725 "connector's staged encoder doesn't match current encoder\n");
9726 }
91d1b4bd
DV
9727}
9728
9729static void
9730check_encoder_state(struct drm_device *dev)
9731{
9732 struct intel_encoder *encoder;
9733 struct intel_connector *connector;
8af6cf88
DV
9734
9735 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9736 base.head) {
9737 bool enabled = false;
9738 bool active = false;
9739 enum pipe pipe, tracked_pipe;
9740
9741 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
9742 encoder->base.base.id,
9743 drm_get_encoder_name(&encoder->base));
9744
9745 WARN(&encoder->new_crtc->base != encoder->base.crtc,
9746 "encoder's stage crtc doesn't match current crtc\n");
9747 WARN(encoder->connectors_active && !encoder->base.crtc,
9748 "encoder's active_connectors set, but no crtc\n");
9749
9750 list_for_each_entry(connector, &dev->mode_config.connector_list,
9751 base.head) {
9752 if (connector->base.encoder != &encoder->base)
9753 continue;
9754 enabled = true;
9755 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
9756 active = true;
9757 }
9758 WARN(!!encoder->base.crtc != enabled,
9759 "encoder's enabled state mismatch "
9760 "(expected %i, found %i)\n",
9761 !!encoder->base.crtc, enabled);
9762 WARN(active && !encoder->base.crtc,
9763 "active encoder with no crtc\n");
9764
9765 WARN(encoder->connectors_active != active,
9766 "encoder's computed active state doesn't match tracked active state "
9767 "(expected %i, found %i)\n", active, encoder->connectors_active);
9768
9769 active = encoder->get_hw_state(encoder, &pipe);
9770 WARN(active != encoder->connectors_active,
9771 "encoder's hw state doesn't match sw tracking "
9772 "(expected %i, found %i)\n",
9773 encoder->connectors_active, active);
9774
9775 if (!encoder->base.crtc)
9776 continue;
9777
9778 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
9779 WARN(active && pipe != tracked_pipe,
9780 "active encoder's pipe doesn't match"
9781 "(expected %i, found %i)\n",
9782 tracked_pipe, pipe);
9783
9784 }
91d1b4bd
DV
9785}
9786
9787static void
9788check_crtc_state(struct drm_device *dev)
9789{
9790 drm_i915_private_t *dev_priv = dev->dev_private;
9791 struct intel_crtc *crtc;
9792 struct intel_encoder *encoder;
9793 struct intel_crtc_config pipe_config;
8af6cf88
DV
9794
9795 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9796 base.head) {
9797 bool enabled = false;
9798 bool active = false;
9799
045ac3b5
JB
9800 memset(&pipe_config, 0, sizeof(pipe_config));
9801
8af6cf88
DV
9802 DRM_DEBUG_KMS("[CRTC:%d]\n",
9803 crtc->base.base.id);
9804
9805 WARN(crtc->active && !crtc->base.enabled,
9806 "active crtc, but not enabled in sw tracking\n");
9807
9808 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9809 base.head) {
9810 if (encoder->base.crtc != &crtc->base)
9811 continue;
9812 enabled = true;
9813 if (encoder->connectors_active)
9814 active = true;
9815 }
6c49f241 9816
8af6cf88
DV
9817 WARN(active != crtc->active,
9818 "crtc's computed active state doesn't match tracked active state "
9819 "(expected %i, found %i)\n", active, crtc->active);
9820 WARN(enabled != crtc->base.enabled,
9821 "crtc's computed enabled state doesn't match tracked enabled state "
9822 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
9823
0e8ffe1b
DV
9824 active = dev_priv->display.get_pipe_config(crtc,
9825 &pipe_config);
d62cf62a
DV
9826
9827 /* hw state is inconsistent with the pipe A quirk */
9828 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
9829 active = crtc->active;
9830
6c49f241
DV
9831 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9832 base.head) {
3eaba51c 9833 enum pipe pipe;
6c49f241
DV
9834 if (encoder->base.crtc != &crtc->base)
9835 continue;
1d37b689 9836 if (encoder->get_hw_state(encoder, &pipe))
6c49f241
DV
9837 encoder->get_config(encoder, &pipe_config);
9838 }
9839
0e8ffe1b
DV
9840 WARN(crtc->active != active,
9841 "crtc active state doesn't match with hw state "
9842 "(expected %i, found %i)\n", crtc->active, active);
9843
c0b03411
DV
9844 if (active &&
9845 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
9846 WARN(1, "pipe state doesn't match!\n");
9847 intel_dump_pipe_config(crtc, &pipe_config,
9848 "[hw state]");
9849 intel_dump_pipe_config(crtc, &crtc->config,
9850 "[sw state]");
9851 }
8af6cf88
DV
9852 }
9853}
9854
91d1b4bd
DV
9855static void
9856check_shared_dpll_state(struct drm_device *dev)
9857{
9858 drm_i915_private_t *dev_priv = dev->dev_private;
9859 struct intel_crtc *crtc;
9860 struct intel_dpll_hw_state dpll_hw_state;
9861 int i;
5358901f
DV
9862
9863 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
9864 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
9865 int enabled_crtcs = 0, active_crtcs = 0;
9866 bool active;
9867
9868 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9869
9870 DRM_DEBUG_KMS("%s\n", pll->name);
9871
9872 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
9873
9874 WARN(pll->active > pll->refcount,
9875 "more active pll users than references: %i vs %i\n",
9876 pll->active, pll->refcount);
9877 WARN(pll->active && !pll->on,
9878 "pll in active use but not on in sw tracking\n");
35c95375
DV
9879 WARN(pll->on && !pll->active,
9880 "pll in on but not on in use in sw tracking\n");
5358901f
DV
9881 WARN(pll->on != active,
9882 "pll on state mismatch (expected %i, found %i)\n",
9883 pll->on, active);
9884
9885 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
9886 base.head) {
9887 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
9888 enabled_crtcs++;
9889 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
9890 active_crtcs++;
9891 }
9892 WARN(pll->active != active_crtcs,
9893 "pll active crtcs mismatch (expected %i, found %i)\n",
9894 pll->active, active_crtcs);
9895 WARN(pll->refcount != enabled_crtcs,
9896 "pll enabled crtcs mismatch (expected %i, found %i)\n",
9897 pll->refcount, enabled_crtcs);
66e985c0
DV
9898
9899 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
9900 sizeof(dpll_hw_state)),
9901 "pll hw state mismatch\n");
5358901f 9902 }
8af6cf88
DV
9903}
9904
91d1b4bd
DV
9905void
9906intel_modeset_check_state(struct drm_device *dev)
9907{
9908 check_connector_state(dev);
9909 check_encoder_state(dev);
9910 check_crtc_state(dev);
9911 check_shared_dpll_state(dev);
9912}
9913
18442d08
VS
9914void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
9915 int dotclock)
9916{
9917 /*
9918 * FDI already provided one idea for the dotclock.
9919 * Yell if the encoder disagrees.
9920 */
241bfc38 9921 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
18442d08 9922 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
241bfc38 9923 pipe_config->adjusted_mode.crtc_clock, dotclock);
18442d08
VS
9924}
9925
f30da187
DV
9926static int __intel_set_mode(struct drm_crtc *crtc,
9927 struct drm_display_mode *mode,
9928 int x, int y, struct drm_framebuffer *fb)
a6778b3c
DV
9929{
9930 struct drm_device *dev = crtc->dev;
dbf2b54e 9931 drm_i915_private_t *dev_priv = dev->dev_private;
4b4b9238 9932 struct drm_display_mode *saved_mode;
b8cecdf5 9933 struct intel_crtc_config *pipe_config = NULL;
25c5b266
DV
9934 struct intel_crtc *intel_crtc;
9935 unsigned disable_pipes, prepare_pipes, modeset_pipes;
c0c36b94 9936 int ret = 0;
a6778b3c 9937
4b4b9238 9938 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
c0c36b94
CW
9939 if (!saved_mode)
9940 return -ENOMEM;
a6778b3c 9941
e2e1ed41 9942 intel_modeset_affected_pipes(crtc, &modeset_pipes,
25c5b266
DV
9943 &prepare_pipes, &disable_pipes);
9944
3ac18232 9945 *saved_mode = crtc->mode;
a6778b3c 9946
25c5b266
DV
9947 /* Hack: Because we don't (yet) support global modeset on multiple
9948 * crtcs, we don't keep track of the new mode for more than one crtc.
9949 * Hence simply check whether any bit is set in modeset_pipes in all the
9950 * pieces of code that are not yet converted to deal with mutliple crtcs
9951 * changing their mode at the same time. */
25c5b266 9952 if (modeset_pipes) {
4e53c2e0 9953 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
b8cecdf5
DV
9954 if (IS_ERR(pipe_config)) {
9955 ret = PTR_ERR(pipe_config);
9956 pipe_config = NULL;
9957
3ac18232 9958 goto out;
25c5b266 9959 }
c0b03411
DV
9960 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
9961 "[modeset]");
50741abc 9962 to_intel_crtc(crtc)->new_config = pipe_config;
25c5b266 9963 }
a6778b3c 9964
30a970c6
JB
9965 /*
9966 * See if the config requires any additional preparation, e.g.
9967 * to adjust global state with pipes off. We need to do this
9968 * here so we can get the modeset_pipe updated config for the new
9969 * mode set on this crtc. For other crtcs we need to use the
9970 * adjusted_mode bits in the crtc directly.
9971 */
c164f833 9972 if (IS_VALLEYVIEW(dev)) {
2f2d7aa1 9973 valleyview_modeset_global_pipes(dev, &prepare_pipes);
30a970c6 9974
c164f833
VS
9975 /* may have added more to prepare_pipes than we should */
9976 prepare_pipes &= ~disable_pipes;
9977 }
9978
460da916
DV
9979 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
9980 intel_crtc_disable(&intel_crtc->base);
9981
ea9d758d
DV
9982 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
9983 if (intel_crtc->base.enabled)
9984 dev_priv->display.crtc_disable(&intel_crtc->base);
9985 }
a6778b3c 9986
6c4c86f5
DV
9987 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
9988 * to set it here already despite that we pass it down the callchain.
f6e5b160 9989 */
b8cecdf5 9990 if (modeset_pipes) {
25c5b266 9991 crtc->mode = *mode;
b8cecdf5
DV
9992 /* mode_set/enable/disable functions rely on a correct pipe
9993 * config. */
9994 to_intel_crtc(crtc)->config = *pipe_config;
50741abc 9995 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
c326c0a9
VS
9996
9997 /*
9998 * Calculate and store various constants which
9999 * are later needed by vblank and swap-completion
10000 * timestamping. They are derived from true hwmode.
10001 */
10002 drm_calc_timestamping_constants(crtc,
10003 &pipe_config->adjusted_mode);
b8cecdf5 10004 }
7758a113 10005
ea9d758d
DV
10006 /* Only after disabling all output pipelines that will be changed can we
10007 * update the the output configuration. */
10008 intel_modeset_update_state(dev, prepare_pipes);
f6e5b160 10009
47fab737
DV
10010 if (dev_priv->display.modeset_global_resources)
10011 dev_priv->display.modeset_global_resources(dev);
10012
a6778b3c
DV
10013 /* Set up the DPLL and any encoders state that needs to adjust or depend
10014 * on the DPLL.
f6e5b160 10015 */
25c5b266 10016 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
c0c36b94 10017 ret = intel_crtc_mode_set(&intel_crtc->base,
c0c36b94
CW
10018 x, y, fb);
10019 if (ret)
10020 goto done;
a6778b3c
DV
10021 }
10022
10023 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
25c5b266
DV
10024 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
10025 dev_priv->display.crtc_enable(&intel_crtc->base);
a6778b3c 10026
a6778b3c
DV
10027 /* FIXME: add subpixel order */
10028done:
4b4b9238 10029 if (ret && crtc->enabled)
3ac18232 10030 crtc->mode = *saved_mode;
a6778b3c 10031
3ac18232 10032out:
b8cecdf5 10033 kfree(pipe_config);
3ac18232 10034 kfree(saved_mode);
a6778b3c 10035 return ret;
f6e5b160
CW
10036}
10037
e7457a9a
DL
10038static int intel_set_mode(struct drm_crtc *crtc,
10039 struct drm_display_mode *mode,
10040 int x, int y, struct drm_framebuffer *fb)
f30da187
DV
10041{
10042 int ret;
10043
10044 ret = __intel_set_mode(crtc, mode, x, y, fb);
10045
10046 if (ret == 0)
10047 intel_modeset_check_state(crtc->dev);
10048
10049 return ret;
10050}
10051
c0c36b94
CW
10052void intel_crtc_restore_mode(struct drm_crtc *crtc)
10053{
10054 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
10055}
10056
25c5b266
DV
10057#undef for_each_intel_crtc_masked
10058
d9e55608
DV
10059static void intel_set_config_free(struct intel_set_config *config)
10060{
10061 if (!config)
10062 return;
10063
1aa4b628
DV
10064 kfree(config->save_connector_encoders);
10065 kfree(config->save_encoder_crtcs);
7668851f 10066 kfree(config->save_crtc_enabled);
d9e55608
DV
10067 kfree(config);
10068}
10069
85f9eb71
DV
10070static int intel_set_config_save_state(struct drm_device *dev,
10071 struct intel_set_config *config)
10072{
7668851f 10073 struct drm_crtc *crtc;
85f9eb71
DV
10074 struct drm_encoder *encoder;
10075 struct drm_connector *connector;
10076 int count;
10077
7668851f
VS
10078 config->save_crtc_enabled =
10079 kcalloc(dev->mode_config.num_crtc,
10080 sizeof(bool), GFP_KERNEL);
10081 if (!config->save_crtc_enabled)
10082 return -ENOMEM;
10083
1aa4b628
DV
10084 config->save_encoder_crtcs =
10085 kcalloc(dev->mode_config.num_encoder,
10086 sizeof(struct drm_crtc *), GFP_KERNEL);
10087 if (!config->save_encoder_crtcs)
85f9eb71
DV
10088 return -ENOMEM;
10089
1aa4b628
DV
10090 config->save_connector_encoders =
10091 kcalloc(dev->mode_config.num_connector,
10092 sizeof(struct drm_encoder *), GFP_KERNEL);
10093 if (!config->save_connector_encoders)
85f9eb71
DV
10094 return -ENOMEM;
10095
10096 /* Copy data. Note that driver private data is not affected.
10097 * Should anything bad happen only the expected state is
10098 * restored, not the drivers personal bookkeeping.
10099 */
7668851f
VS
10100 count = 0;
10101 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
10102 config->save_crtc_enabled[count++] = crtc->enabled;
10103 }
10104
85f9eb71
DV
10105 count = 0;
10106 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
1aa4b628 10107 config->save_encoder_crtcs[count++] = encoder->crtc;
85f9eb71
DV
10108 }
10109
10110 count = 0;
10111 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1aa4b628 10112 config->save_connector_encoders[count++] = connector->encoder;
85f9eb71
DV
10113 }
10114
10115 return 0;
10116}
10117
10118static void intel_set_config_restore_state(struct drm_device *dev,
10119 struct intel_set_config *config)
10120{
7668851f 10121 struct intel_crtc *crtc;
9a935856
DV
10122 struct intel_encoder *encoder;
10123 struct intel_connector *connector;
85f9eb71
DV
10124 int count;
10125
7668851f
VS
10126 count = 0;
10127 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
10128 crtc->new_enabled = config->save_crtc_enabled[count++];
7bd0a8e7
VS
10129
10130 if (crtc->new_enabled)
10131 crtc->new_config = &crtc->config;
10132 else
10133 crtc->new_config = NULL;
7668851f
VS
10134 }
10135
85f9eb71 10136 count = 0;
9a935856
DV
10137 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10138 encoder->new_crtc =
10139 to_intel_crtc(config->save_encoder_crtcs[count++]);
85f9eb71
DV
10140 }
10141
10142 count = 0;
9a935856
DV
10143 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10144 connector->new_encoder =
10145 to_intel_encoder(config->save_connector_encoders[count++]);
85f9eb71
DV
10146 }
10147}
10148
e3de42b6 10149static bool
2e57f47d 10150is_crtc_connector_off(struct drm_mode_set *set)
e3de42b6
ID
10151{
10152 int i;
10153
2e57f47d
CW
10154 if (set->num_connectors == 0)
10155 return false;
10156
10157 if (WARN_ON(set->connectors == NULL))
10158 return false;
10159
10160 for (i = 0; i < set->num_connectors; i++)
10161 if (set->connectors[i]->encoder &&
10162 set->connectors[i]->encoder->crtc == set->crtc &&
10163 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
e3de42b6
ID
10164 return true;
10165
10166 return false;
10167}
10168
5e2b584e
DV
10169static void
10170intel_set_config_compute_mode_changes(struct drm_mode_set *set,
10171 struct intel_set_config *config)
10172{
10173
10174 /* We should be able to check here if the fb has the same properties
10175 * and then just flip_or_move it */
2e57f47d
CW
10176 if (is_crtc_connector_off(set)) {
10177 config->mode_changed = true;
e3de42b6 10178 } else if (set->crtc->fb != set->fb) {
5e2b584e
DV
10179 /* If we have no fb then treat it as a full mode set */
10180 if (set->crtc->fb == NULL) {
319d9827
JB
10181 struct intel_crtc *intel_crtc =
10182 to_intel_crtc(set->crtc);
10183
d330a953 10184 if (intel_crtc->active && i915.fastboot) {
319d9827
JB
10185 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
10186 config->fb_changed = true;
10187 } else {
10188 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
10189 config->mode_changed = true;
10190 }
5e2b584e
DV
10191 } else if (set->fb == NULL) {
10192 config->mode_changed = true;
72f4901e
DV
10193 } else if (set->fb->pixel_format !=
10194 set->crtc->fb->pixel_format) {
5e2b584e 10195 config->mode_changed = true;
e3de42b6 10196 } else {
5e2b584e 10197 config->fb_changed = true;
e3de42b6 10198 }
5e2b584e
DV
10199 }
10200
835c5873 10201 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
5e2b584e
DV
10202 config->fb_changed = true;
10203
10204 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
10205 DRM_DEBUG_KMS("modes are different, full mode set\n");
10206 drm_mode_debug_printmodeline(&set->crtc->mode);
10207 drm_mode_debug_printmodeline(set->mode);
10208 config->mode_changed = true;
10209 }
a1d95703
CW
10210
10211 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
10212 set->crtc->base.id, config->mode_changed, config->fb_changed);
5e2b584e
DV
10213}
10214
2e431051 10215static int
9a935856
DV
10216intel_modeset_stage_output_state(struct drm_device *dev,
10217 struct drm_mode_set *set,
10218 struct intel_set_config *config)
50f56119 10219{
9a935856
DV
10220 struct intel_connector *connector;
10221 struct intel_encoder *encoder;
7668851f 10222 struct intel_crtc *crtc;
f3f08572 10223 int ro;
50f56119 10224
9abdda74 10225 /* The upper layers ensure that we either disable a crtc or have a list
9a935856
DV
10226 * of connectors. For paranoia, double-check this. */
10227 WARN_ON(!set->fb && (set->num_connectors != 0));
10228 WARN_ON(set->fb && (set->num_connectors == 0));
10229
9a935856
DV
10230 list_for_each_entry(connector, &dev->mode_config.connector_list,
10231 base.head) {
10232 /* Otherwise traverse passed in connector list and get encoders
10233 * for them. */
50f56119 10234 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856
DV
10235 if (set->connectors[ro] == &connector->base) {
10236 connector->new_encoder = connector->encoder;
50f56119
DV
10237 break;
10238 }
10239 }
10240
9a935856
DV
10241 /* If we disable the crtc, disable all its connectors. Also, if
10242 * the connector is on the changing crtc but not on the new
10243 * connector list, disable it. */
10244 if ((!set->fb || ro == set->num_connectors) &&
10245 connector->base.encoder &&
10246 connector->base.encoder->crtc == set->crtc) {
10247 connector->new_encoder = NULL;
10248
10249 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
10250 connector->base.base.id,
10251 drm_get_connector_name(&connector->base));
10252 }
10253
10254
10255 if (&connector->new_encoder->base != connector->base.encoder) {
50f56119 10256 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
5e2b584e 10257 config->mode_changed = true;
50f56119
DV
10258 }
10259 }
9a935856 10260 /* connector->new_encoder is now updated for all connectors. */
50f56119 10261
9a935856 10262 /* Update crtc of enabled connectors. */
9a935856
DV
10263 list_for_each_entry(connector, &dev->mode_config.connector_list,
10264 base.head) {
7668851f
VS
10265 struct drm_crtc *new_crtc;
10266
9a935856 10267 if (!connector->new_encoder)
50f56119
DV
10268 continue;
10269
9a935856 10270 new_crtc = connector->new_encoder->base.crtc;
50f56119
DV
10271
10272 for (ro = 0; ro < set->num_connectors; ro++) {
9a935856 10273 if (set->connectors[ro] == &connector->base)
50f56119
DV
10274 new_crtc = set->crtc;
10275 }
10276
10277 /* Make sure the new CRTC will work with the encoder */
14509916
TR
10278 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
10279 new_crtc)) {
5e2b584e 10280 return -EINVAL;
50f56119 10281 }
9a935856
DV
10282 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
10283
10284 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
10285 connector->base.base.id,
10286 drm_get_connector_name(&connector->base),
10287 new_crtc->base.id);
10288 }
10289
10290 /* Check for any encoders that needs to be disabled. */
10291 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10292 base.head) {
5a65f358 10293 int num_connectors = 0;
9a935856
DV
10294 list_for_each_entry(connector,
10295 &dev->mode_config.connector_list,
10296 base.head) {
10297 if (connector->new_encoder == encoder) {
10298 WARN_ON(!connector->new_encoder->new_crtc);
5a65f358 10299 num_connectors++;
9a935856
DV
10300 }
10301 }
5a65f358
PZ
10302
10303 if (num_connectors == 0)
10304 encoder->new_crtc = NULL;
10305 else if (num_connectors > 1)
10306 return -EINVAL;
10307
9a935856
DV
10308 /* Only now check for crtc changes so we don't miss encoders
10309 * that will be disabled. */
10310 if (&encoder->new_crtc->base != encoder->base.crtc) {
50f56119 10311 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
5e2b584e 10312 config->mode_changed = true;
50f56119
DV
10313 }
10314 }
9a935856 10315 /* Now we've also updated encoder->new_crtc for all encoders. */
50f56119 10316
7668851f
VS
10317 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
10318 base.head) {
10319 crtc->new_enabled = false;
10320
10321 list_for_each_entry(encoder,
10322 &dev->mode_config.encoder_list,
10323 base.head) {
10324 if (encoder->new_crtc == crtc) {
10325 crtc->new_enabled = true;
10326 break;
10327 }
10328 }
10329
10330 if (crtc->new_enabled != crtc->base.enabled) {
10331 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
10332 crtc->new_enabled ? "en" : "dis");
10333 config->mode_changed = true;
10334 }
7bd0a8e7
VS
10335
10336 if (crtc->new_enabled)
10337 crtc->new_config = &crtc->config;
10338 else
10339 crtc->new_config = NULL;
7668851f
VS
10340 }
10341
2e431051
DV
10342 return 0;
10343}
10344
7d00a1f5
VS
10345static void disable_crtc_nofb(struct intel_crtc *crtc)
10346{
10347 struct drm_device *dev = crtc->base.dev;
10348 struct intel_encoder *encoder;
10349 struct intel_connector *connector;
10350
10351 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
10352 pipe_name(crtc->pipe));
10353
10354 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10355 if (connector->new_encoder &&
10356 connector->new_encoder->new_crtc == crtc)
10357 connector->new_encoder = NULL;
10358 }
10359
10360 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10361 if (encoder->new_crtc == crtc)
10362 encoder->new_crtc = NULL;
10363 }
10364
10365 crtc->new_enabled = false;
7bd0a8e7 10366 crtc->new_config = NULL;
7d00a1f5
VS
10367}
10368
2e431051
DV
10369static int intel_crtc_set_config(struct drm_mode_set *set)
10370{
10371 struct drm_device *dev;
2e431051
DV
10372 struct drm_mode_set save_set;
10373 struct intel_set_config *config;
10374 int ret;
2e431051 10375
8d3e375e
DV
10376 BUG_ON(!set);
10377 BUG_ON(!set->crtc);
10378 BUG_ON(!set->crtc->helper_private);
2e431051 10379
7e53f3a4
DV
10380 /* Enforce sane interface api - has been abused by the fb helper. */
10381 BUG_ON(!set->mode && set->fb);
10382 BUG_ON(set->fb && set->num_connectors == 0);
431e50f7 10383
2e431051
DV
10384 if (set->fb) {
10385 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
10386 set->crtc->base.id, set->fb->base.id,
10387 (int)set->num_connectors, set->x, set->y);
10388 } else {
10389 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
2e431051
DV
10390 }
10391
10392 dev = set->crtc->dev;
10393
10394 ret = -ENOMEM;
10395 config = kzalloc(sizeof(*config), GFP_KERNEL);
10396 if (!config)
10397 goto out_config;
10398
10399 ret = intel_set_config_save_state(dev, config);
10400 if (ret)
10401 goto out_config;
10402
10403 save_set.crtc = set->crtc;
10404 save_set.mode = &set->crtc->mode;
10405 save_set.x = set->crtc->x;
10406 save_set.y = set->crtc->y;
10407 save_set.fb = set->crtc->fb;
10408
10409 /* Compute whether we need a full modeset, only an fb base update or no
10410 * change at all. In the future we might also check whether only the
10411 * mode changed, e.g. for LVDS where we only change the panel fitter in
10412 * such cases. */
10413 intel_set_config_compute_mode_changes(set, config);
10414
9a935856 10415 ret = intel_modeset_stage_output_state(dev, set, config);
2e431051
DV
10416 if (ret)
10417 goto fail;
10418
5e2b584e 10419 if (config->mode_changed) {
c0c36b94
CW
10420 ret = intel_set_mode(set->crtc, set->mode,
10421 set->x, set->y, set->fb);
5e2b584e 10422 } else if (config->fb_changed) {
4878cae2
VS
10423 intel_crtc_wait_for_pending_flips(set->crtc);
10424
4f660f49 10425 ret = intel_pipe_set_base(set->crtc,
94352cf9 10426 set->x, set->y, set->fb);
7ca51a3a
JB
10427 /*
10428 * In the fastboot case this may be our only check of the
10429 * state after boot. It would be better to only do it on
10430 * the first update, but we don't have a nice way of doing that
10431 * (and really, set_config isn't used much for high freq page
10432 * flipping, so increasing its cost here shouldn't be a big
10433 * deal).
10434 */
d330a953 10435 if (i915.fastboot && ret == 0)
7ca51a3a 10436 intel_modeset_check_state(set->crtc->dev);
50f56119
DV
10437 }
10438
2d05eae1 10439 if (ret) {
bf67dfeb
DV
10440 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
10441 set->crtc->base.id, ret);
50f56119 10442fail:
2d05eae1 10443 intel_set_config_restore_state(dev, config);
50f56119 10444
7d00a1f5
VS
10445 /*
10446 * HACK: if the pipe was on, but we didn't have a framebuffer,
10447 * force the pipe off to avoid oopsing in the modeset code
10448 * due to fb==NULL. This should only happen during boot since
10449 * we don't yet reconstruct the FB from the hardware state.
10450 */
10451 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
10452 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
10453
2d05eae1
CW
10454 /* Try to restore the config */
10455 if (config->mode_changed &&
10456 intel_set_mode(save_set.crtc, save_set.mode,
10457 save_set.x, save_set.y, save_set.fb))
10458 DRM_ERROR("failed to restore config after modeset failure\n");
10459 }
50f56119 10460
d9e55608
DV
10461out_config:
10462 intel_set_config_free(config);
50f56119
DV
10463 return ret;
10464}
f6e5b160
CW
10465
10466static const struct drm_crtc_funcs intel_crtc_funcs = {
f6e5b160
CW
10467 .cursor_set = intel_crtc_cursor_set,
10468 .cursor_move = intel_crtc_cursor_move,
10469 .gamma_set = intel_crtc_gamma_set,
50f56119 10470 .set_config = intel_crtc_set_config,
f6e5b160
CW
10471 .destroy = intel_crtc_destroy,
10472 .page_flip = intel_crtc_page_flip,
10473};
10474
79f689aa
PZ
10475static void intel_cpu_pll_init(struct drm_device *dev)
10476{
affa9354 10477 if (HAS_DDI(dev))
79f689aa
PZ
10478 intel_ddi_pll_init(dev);
10479}
10480
5358901f
DV
10481static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
10482 struct intel_shared_dpll *pll,
10483 struct intel_dpll_hw_state *hw_state)
ee7b9f93 10484{
5358901f 10485 uint32_t val;
ee7b9f93 10486
5358901f 10487 val = I915_READ(PCH_DPLL(pll->id));
66e985c0
DV
10488 hw_state->dpll = val;
10489 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10490 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
5358901f
DV
10491
10492 return val & DPLL_VCO_ENABLE;
10493}
10494
15bdd4cf
DV
10495static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10496 struct intel_shared_dpll *pll)
10497{
10498 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10499 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10500}
10501
e7b903d2
DV
10502static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10503 struct intel_shared_dpll *pll)
10504{
e7b903d2 10505 /* PCH refclock must be enabled first */
89eff4be 10506 ibx_assert_pch_refclk_enabled(dev_priv);
e7b903d2 10507
15bdd4cf
DV
10508 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10509
10510 /* Wait for the clocks to stabilize. */
10511 POSTING_READ(PCH_DPLL(pll->id));
10512 udelay(150);
10513
10514 /* The pixel multiplier can only be updated once the
10515 * DPLL is enabled and the clocks are stable.
10516 *
10517 * So write it again.
10518 */
10519 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10520 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
10521 udelay(200);
10522}
10523
10524static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10525 struct intel_shared_dpll *pll)
10526{
10527 struct drm_device *dev = dev_priv->dev;
10528 struct intel_crtc *crtc;
e7b903d2
DV
10529
10530 /* Make sure no transcoder isn't still depending on us. */
10531 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
10532 if (intel_crtc_to_shared_dpll(crtc) == pll)
10533 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
ee7b9f93
JB
10534 }
10535
15bdd4cf
DV
10536 I915_WRITE(PCH_DPLL(pll->id), 0);
10537 POSTING_READ(PCH_DPLL(pll->id));
e7b903d2
DV
10538 udelay(200);
10539}
10540
46edb027
DV
10541static char *ibx_pch_dpll_names[] = {
10542 "PCH DPLL A",
10543 "PCH DPLL B",
10544};
10545
7c74ade1 10546static void ibx_pch_dpll_init(struct drm_device *dev)
ee7b9f93 10547{
e7b903d2 10548 struct drm_i915_private *dev_priv = dev->dev_private;
ee7b9f93
JB
10549 int i;
10550
7c74ade1 10551 dev_priv->num_shared_dpll = 2;
ee7b9f93 10552
e72f9fbf 10553 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
46edb027
DV
10554 dev_priv->shared_dplls[i].id = i;
10555 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
15bdd4cf 10556 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
e7b903d2
DV
10557 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
10558 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
5358901f
DV
10559 dev_priv->shared_dplls[i].get_hw_state =
10560 ibx_pch_dpll_get_hw_state;
ee7b9f93
JB
10561 }
10562}
10563
7c74ade1
DV
10564static void intel_shared_dpll_init(struct drm_device *dev)
10565{
e7b903d2 10566 struct drm_i915_private *dev_priv = dev->dev_private;
7c74ade1
DV
10567
10568 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
10569 ibx_pch_dpll_init(dev);
10570 else
10571 dev_priv->num_shared_dpll = 0;
10572
10573 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
7c74ade1
DV
10574}
10575
b358d0a6 10576static void intel_crtc_init(struct drm_device *dev, int pipe)
79e53945 10577{
22fd0fab 10578 drm_i915_private_t *dev_priv = dev->dev_private;
79e53945
JB
10579 struct intel_crtc *intel_crtc;
10580 int i;
10581
955382f3 10582 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
79e53945
JB
10583 if (intel_crtc == NULL)
10584 return;
10585
10586 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
10587
10588 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
79e53945
JB
10589 for (i = 0; i < 256; i++) {
10590 intel_crtc->lut_r[i] = i;
10591 intel_crtc->lut_g[i] = i;
10592 intel_crtc->lut_b[i] = i;
10593 }
10594
1f1c2e24
VS
10595 /*
10596 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
10597 * is hooked to plane B. Hence we want plane A feeding pipe B.
10598 */
80824003
JB
10599 intel_crtc->pipe = pipe;
10600 intel_crtc->plane = pipe;
3a77c4c4 10601 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
28c97730 10602 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
e2e767ab 10603 intel_crtc->plane = !pipe;
80824003
JB
10604 }
10605
22fd0fab
JB
10606 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
10607 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
10608 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
10609 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
10610
79e53945 10611 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
79e53945
JB
10612}
10613
752aa88a
JB
10614enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
10615{
10616 struct drm_encoder *encoder = connector->base.encoder;
10617
10618 WARN_ON(!mutex_is_locked(&connector->base.dev->mode_config.mutex));
10619
10620 if (!encoder)
10621 return INVALID_PIPE;
10622
10623 return to_intel_crtc(encoder->crtc)->pipe;
10624}
10625
08d7b3d1 10626int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
05394f39 10627 struct drm_file *file)
08d7b3d1 10628{
08d7b3d1 10629 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
c05422d5
DV
10630 struct drm_mode_object *drmmode_obj;
10631 struct intel_crtc *crtc;
08d7b3d1 10632
1cff8f6b
DV
10633 if (!drm_core_check_feature(dev, DRIVER_MODESET))
10634 return -ENODEV;
08d7b3d1 10635
c05422d5
DV
10636 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
10637 DRM_MODE_OBJECT_CRTC);
08d7b3d1 10638
c05422d5 10639 if (!drmmode_obj) {
08d7b3d1 10640 DRM_ERROR("no such CRTC id\n");
3f2c2057 10641 return -ENOENT;
08d7b3d1
CW
10642 }
10643
c05422d5
DV
10644 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
10645 pipe_from_crtc_id->pipe = crtc->pipe;
08d7b3d1 10646
c05422d5 10647 return 0;
08d7b3d1
CW
10648}
10649
66a9278e 10650static int intel_encoder_clones(struct intel_encoder *encoder)
79e53945 10651{
66a9278e
DV
10652 struct drm_device *dev = encoder->base.dev;
10653 struct intel_encoder *source_encoder;
79e53945 10654 int index_mask = 0;
79e53945
JB
10655 int entry = 0;
10656
66a9278e
DV
10657 list_for_each_entry(source_encoder,
10658 &dev->mode_config.encoder_list, base.head) {
bc079e8b 10659 if (encoders_cloneable(encoder, source_encoder))
66a9278e
DV
10660 index_mask |= (1 << entry);
10661
79e53945
JB
10662 entry++;
10663 }
4ef69c7a 10664
79e53945
JB
10665 return index_mask;
10666}
10667
4d302442
CW
10668static bool has_edp_a(struct drm_device *dev)
10669{
10670 struct drm_i915_private *dev_priv = dev->dev_private;
10671
10672 if (!IS_MOBILE(dev))
10673 return false;
10674
10675 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
10676 return false;
10677
e3589908 10678 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
4d302442
CW
10679 return false;
10680
10681 return true;
10682}
10683
ba0fbca4
DL
10684const char *intel_output_name(int output)
10685{
10686 static const char *names[] = {
10687 [INTEL_OUTPUT_UNUSED] = "Unused",
10688 [INTEL_OUTPUT_ANALOG] = "Analog",
10689 [INTEL_OUTPUT_DVO] = "DVO",
10690 [INTEL_OUTPUT_SDVO] = "SDVO",
10691 [INTEL_OUTPUT_LVDS] = "LVDS",
10692 [INTEL_OUTPUT_TVOUT] = "TV",
10693 [INTEL_OUTPUT_HDMI] = "HDMI",
10694 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
10695 [INTEL_OUTPUT_EDP] = "eDP",
10696 [INTEL_OUTPUT_DSI] = "DSI",
10697 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
10698 };
10699
10700 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
10701 return "Invalid";
10702
10703 return names[output];
10704}
10705
79e53945
JB
10706static void intel_setup_outputs(struct drm_device *dev)
10707{
725e30ad 10708 struct drm_i915_private *dev_priv = dev->dev_private;
4ef69c7a 10709 struct intel_encoder *encoder;
cb0953d7 10710 bool dpd_is_edp = false;
79e53945 10711
c9093354 10712 intel_lvds_init(dev);
79e53945 10713
c40c0f5b 10714 if (!IS_ULT(dev))
79935fca 10715 intel_crt_init(dev);
cb0953d7 10716
affa9354 10717 if (HAS_DDI(dev)) {
0e72a5b5
ED
10718 int found;
10719
10720 /* Haswell uses DDI functions to detect digital outputs */
10721 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
10722 /* DDI A only supports eDP */
10723 if (found)
10724 intel_ddi_init(dev, PORT_A);
10725
10726 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
10727 * register */
10728 found = I915_READ(SFUSE_STRAP);
10729
10730 if (found & SFUSE_STRAP_DDIB_DETECTED)
10731 intel_ddi_init(dev, PORT_B);
10732 if (found & SFUSE_STRAP_DDIC_DETECTED)
10733 intel_ddi_init(dev, PORT_C);
10734 if (found & SFUSE_STRAP_DDID_DETECTED)
10735 intel_ddi_init(dev, PORT_D);
10736 } else if (HAS_PCH_SPLIT(dev)) {
cb0953d7 10737 int found;
5d8a7752 10738 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
270b3042
DV
10739
10740 if (has_edp_a(dev))
10741 intel_dp_init(dev, DP_A, PORT_A);
cb0953d7 10742
dc0fa718 10743 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
461ed3ca 10744 /* PCH SDVOB multiplex with HDMIB */
eef4eacb 10745 found = intel_sdvo_init(dev, PCH_SDVOB, true);
30ad48b7 10746 if (!found)
e2debe91 10747 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
5eb08b69 10748 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
ab9d7c30 10749 intel_dp_init(dev, PCH_DP_B, PORT_B);
30ad48b7
ZW
10750 }
10751
dc0fa718 10752 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
e2debe91 10753 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
30ad48b7 10754
dc0fa718 10755 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
e2debe91 10756 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
30ad48b7 10757
5eb08b69 10758 if (I915_READ(PCH_DP_C) & DP_DETECTED)
ab9d7c30 10759 intel_dp_init(dev, PCH_DP_C, PORT_C);
5eb08b69 10760
270b3042 10761 if (I915_READ(PCH_DP_D) & DP_DETECTED)
ab9d7c30 10762 intel_dp_init(dev, PCH_DP_D, PORT_D);
4a87d65d 10763 } else if (IS_VALLEYVIEW(dev)) {
585a94b8
AB
10764 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
10765 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
10766 PORT_B);
10767 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
10768 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
10769 }
10770
6f6005a5
JB
10771 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
10772 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
10773 PORT_C);
10774 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
5d8a7752 10775 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
6f6005a5 10776 }
19c03924 10777
3cfca973 10778 intel_dsi_init(dev);
103a196f 10779 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
27185ae1 10780 bool found = false;
7d57382e 10781
e2debe91 10782 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 10783 DRM_DEBUG_KMS("probing SDVOB\n");
e2debe91 10784 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
b01f2c3a
JB
10785 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
10786 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
e2debe91 10787 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
b01f2c3a 10788 }
27185ae1 10789
e7281eab 10790 if (!found && SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 10791 intel_dp_init(dev, DP_B, PORT_B);
725e30ad 10792 }
13520b05
KH
10793
10794 /* Before G4X SDVOC doesn't have its own detect register */
13520b05 10795
e2debe91 10796 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
b01f2c3a 10797 DRM_DEBUG_KMS("probing SDVOC\n");
e2debe91 10798 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
b01f2c3a 10799 }
27185ae1 10800
e2debe91 10801 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
27185ae1 10802
b01f2c3a
JB
10803 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
10804 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
e2debe91 10805 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
b01f2c3a 10806 }
e7281eab 10807 if (SUPPORTS_INTEGRATED_DP(dev))
ab9d7c30 10808 intel_dp_init(dev, DP_C, PORT_C);
725e30ad 10809 }
27185ae1 10810
b01f2c3a 10811 if (SUPPORTS_INTEGRATED_DP(dev) &&
e7281eab 10812 (I915_READ(DP_D) & DP_DETECTED))
ab9d7c30 10813 intel_dp_init(dev, DP_D, PORT_D);
bad720ff 10814 } else if (IS_GEN2(dev))
79e53945
JB
10815 intel_dvo_init(dev);
10816
103a196f 10817 if (SUPPORTS_TV(dev))
79e53945
JB
10818 intel_tv_init(dev);
10819
4ef69c7a
CW
10820 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10821 encoder->base.possible_crtcs = encoder->crtc_mask;
10822 encoder->base.possible_clones =
66a9278e 10823 intel_encoder_clones(encoder);
79e53945 10824 }
47356eb6 10825
dde86e2d 10826 intel_init_pch_refclk(dev);
270b3042
DV
10827
10828 drm_helper_move_panel_connectors_to_head(dev);
79e53945
JB
10829}
10830
10831static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
10832{
10833 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
79e53945 10834
ef2d633e
DV
10835 drm_framebuffer_cleanup(fb);
10836 WARN_ON(!intel_fb->obj->framebuffer_references--);
10837 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
79e53945
JB
10838 kfree(intel_fb);
10839}
10840
10841static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
05394f39 10842 struct drm_file *file,
79e53945
JB
10843 unsigned int *handle)
10844{
10845 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
05394f39 10846 struct drm_i915_gem_object *obj = intel_fb->obj;
79e53945 10847
05394f39 10848 return drm_gem_handle_create(file, &obj->base, handle);
79e53945
JB
10849}
10850
10851static const struct drm_framebuffer_funcs intel_fb_funcs = {
10852 .destroy = intel_user_framebuffer_destroy,
10853 .create_handle = intel_user_framebuffer_create_handle,
10854};
10855
b5ea642a
DV
10856static int intel_framebuffer_init(struct drm_device *dev,
10857 struct intel_framebuffer *intel_fb,
10858 struct drm_mode_fb_cmd2 *mode_cmd,
10859 struct drm_i915_gem_object *obj)
79e53945 10860{
a57ce0b2 10861 int aligned_height;
a35cdaa0 10862 int pitch_limit;
79e53945
JB
10863 int ret;
10864
dd4916c5
DV
10865 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
10866
c16ed4be
CW
10867 if (obj->tiling_mode == I915_TILING_Y) {
10868 DRM_DEBUG("hardware does not support tiling Y\n");
57cd6508 10869 return -EINVAL;
c16ed4be 10870 }
57cd6508 10871
c16ed4be
CW
10872 if (mode_cmd->pitches[0] & 63) {
10873 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
10874 mode_cmd->pitches[0]);
57cd6508 10875 return -EINVAL;
c16ed4be 10876 }
57cd6508 10877
a35cdaa0
CW
10878 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
10879 pitch_limit = 32*1024;
10880 } else if (INTEL_INFO(dev)->gen >= 4) {
10881 if (obj->tiling_mode)
10882 pitch_limit = 16*1024;
10883 else
10884 pitch_limit = 32*1024;
10885 } else if (INTEL_INFO(dev)->gen >= 3) {
10886 if (obj->tiling_mode)
10887 pitch_limit = 8*1024;
10888 else
10889 pitch_limit = 16*1024;
10890 } else
10891 /* XXX DSPC is limited to 4k tiled */
10892 pitch_limit = 8*1024;
10893
10894 if (mode_cmd->pitches[0] > pitch_limit) {
10895 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
10896 obj->tiling_mode ? "tiled" : "linear",
10897 mode_cmd->pitches[0], pitch_limit);
5d7bd705 10898 return -EINVAL;
c16ed4be 10899 }
5d7bd705
VS
10900
10901 if (obj->tiling_mode != I915_TILING_NONE &&
c16ed4be
CW
10902 mode_cmd->pitches[0] != obj->stride) {
10903 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
10904 mode_cmd->pitches[0], obj->stride);
5d7bd705 10905 return -EINVAL;
c16ed4be 10906 }
5d7bd705 10907
57779d06 10908 /* Reject formats not supported by any plane early. */
308e5bcb 10909 switch (mode_cmd->pixel_format) {
57779d06 10910 case DRM_FORMAT_C8:
04b3924d
VS
10911 case DRM_FORMAT_RGB565:
10912 case DRM_FORMAT_XRGB8888:
10913 case DRM_FORMAT_ARGB8888:
57779d06
VS
10914 break;
10915 case DRM_FORMAT_XRGB1555:
10916 case DRM_FORMAT_ARGB1555:
c16ed4be 10917 if (INTEL_INFO(dev)->gen > 3) {
4ee62c76
VS
10918 DRM_DEBUG("unsupported pixel format: %s\n",
10919 drm_get_format_name(mode_cmd->pixel_format));
57779d06 10920 return -EINVAL;
c16ed4be 10921 }
57779d06
VS
10922 break;
10923 case DRM_FORMAT_XBGR8888:
10924 case DRM_FORMAT_ABGR8888:
04b3924d
VS
10925 case DRM_FORMAT_XRGB2101010:
10926 case DRM_FORMAT_ARGB2101010:
57779d06
VS
10927 case DRM_FORMAT_XBGR2101010:
10928 case DRM_FORMAT_ABGR2101010:
c16ed4be 10929 if (INTEL_INFO(dev)->gen < 4) {
4ee62c76
VS
10930 DRM_DEBUG("unsupported pixel format: %s\n",
10931 drm_get_format_name(mode_cmd->pixel_format));
57779d06 10932 return -EINVAL;
c16ed4be 10933 }
b5626747 10934 break;
04b3924d
VS
10935 case DRM_FORMAT_YUYV:
10936 case DRM_FORMAT_UYVY:
10937 case DRM_FORMAT_YVYU:
10938 case DRM_FORMAT_VYUY:
c16ed4be 10939 if (INTEL_INFO(dev)->gen < 5) {
4ee62c76
VS
10940 DRM_DEBUG("unsupported pixel format: %s\n",
10941 drm_get_format_name(mode_cmd->pixel_format));
57779d06 10942 return -EINVAL;
c16ed4be 10943 }
57cd6508
CW
10944 break;
10945 default:
4ee62c76
VS
10946 DRM_DEBUG("unsupported pixel format: %s\n",
10947 drm_get_format_name(mode_cmd->pixel_format));
57cd6508
CW
10948 return -EINVAL;
10949 }
10950
90f9a336
VS
10951 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
10952 if (mode_cmd->offsets[0] != 0)
10953 return -EINVAL;
10954
a57ce0b2
JB
10955 aligned_height = intel_align_height(dev, mode_cmd->height,
10956 obj->tiling_mode);
53155c0a
DV
10957 /* FIXME drm helper for size checks (especially planar formats)? */
10958 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
10959 return -EINVAL;
10960
c7d73f6a
DV
10961 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
10962 intel_fb->obj = obj;
80075d49 10963 intel_fb->obj->framebuffer_references++;
c7d73f6a 10964
79e53945
JB
10965 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
10966 if (ret) {
10967 DRM_ERROR("framebuffer init failed %d\n", ret);
10968 return ret;
10969 }
10970
79e53945
JB
10971 return 0;
10972}
10973
79e53945
JB
10974static struct drm_framebuffer *
10975intel_user_framebuffer_create(struct drm_device *dev,
10976 struct drm_file *filp,
308e5bcb 10977 struct drm_mode_fb_cmd2 *mode_cmd)
79e53945 10978{
05394f39 10979 struct drm_i915_gem_object *obj;
79e53945 10980
308e5bcb
JB
10981 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
10982 mode_cmd->handles[0]));
c8725226 10983 if (&obj->base == NULL)
cce13ff7 10984 return ERR_PTR(-ENOENT);
79e53945 10985
d2dff872 10986 return intel_framebuffer_create(dev, mode_cmd, obj);
79e53945
JB
10987}
10988
4520f53a 10989#ifndef CONFIG_DRM_I915_FBDEV
0632fef6 10990static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
4520f53a
DV
10991{
10992}
10993#endif
10994
79e53945 10995static const struct drm_mode_config_funcs intel_mode_funcs = {
79e53945 10996 .fb_create = intel_user_framebuffer_create,
0632fef6 10997 .output_poll_changed = intel_fbdev_output_poll_changed,
79e53945
JB
10998};
10999
e70236a8
JB
11000/* Set up chip specific display functions */
11001static void intel_init_display(struct drm_device *dev)
11002{
11003 struct drm_i915_private *dev_priv = dev->dev_private;
11004
ee9300bb
DV
11005 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
11006 dev_priv->display.find_dpll = g4x_find_best_dpll;
11007 else if (IS_VALLEYVIEW(dev))
11008 dev_priv->display.find_dpll = vlv_find_best_dpll;
11009 else if (IS_PINEVIEW(dev))
11010 dev_priv->display.find_dpll = pnv_find_best_dpll;
11011 else
11012 dev_priv->display.find_dpll = i9xx_find_best_dpll;
11013
affa9354 11014 if (HAS_DDI(dev)) {
0e8ffe1b 11015 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
4c6baa59 11016 dev_priv->display.get_plane_config = ironlake_get_plane_config;
09b4ddf9 11017 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
4f771f10
PZ
11018 dev_priv->display.crtc_enable = haswell_crtc_enable;
11019 dev_priv->display.crtc_disable = haswell_crtc_disable;
6441ab5f 11020 dev_priv->display.off = haswell_crtc_off;
09b4ddf9
PZ
11021 dev_priv->display.update_plane = ironlake_update_plane;
11022 } else if (HAS_PCH_SPLIT(dev)) {
0e8ffe1b 11023 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
4c6baa59 11024 dev_priv->display.get_plane_config = ironlake_get_plane_config;
f564048e 11025 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
76e5a89c
DV
11026 dev_priv->display.crtc_enable = ironlake_crtc_enable;
11027 dev_priv->display.crtc_disable = ironlake_crtc_disable;
ee7b9f93 11028 dev_priv->display.off = ironlake_crtc_off;
17638cd6 11029 dev_priv->display.update_plane = ironlake_update_plane;
89b667f8
JB
11030 } else if (IS_VALLEYVIEW(dev)) {
11031 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 11032 dev_priv->display.get_plane_config = i9xx_get_plane_config;
89b667f8
JB
11033 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
11034 dev_priv->display.crtc_enable = valleyview_crtc_enable;
11035 dev_priv->display.crtc_disable = i9xx_crtc_disable;
11036 dev_priv->display.off = i9xx_crtc_off;
11037 dev_priv->display.update_plane = i9xx_update_plane;
f564048e 11038 } else {
0e8ffe1b 11039 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
1ad292b5 11040 dev_priv->display.get_plane_config = i9xx_get_plane_config;
f564048e 11041 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
76e5a89c
DV
11042 dev_priv->display.crtc_enable = i9xx_crtc_enable;
11043 dev_priv->display.crtc_disable = i9xx_crtc_disable;
ee7b9f93 11044 dev_priv->display.off = i9xx_crtc_off;
17638cd6 11045 dev_priv->display.update_plane = i9xx_update_plane;
f564048e 11046 }
e70236a8 11047
e70236a8 11048 /* Returns the core display clock speed */
25eb05fc
JB
11049 if (IS_VALLEYVIEW(dev))
11050 dev_priv->display.get_display_clock_speed =
11051 valleyview_get_display_clock_speed;
11052 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
e70236a8
JB
11053 dev_priv->display.get_display_clock_speed =
11054 i945_get_display_clock_speed;
11055 else if (IS_I915G(dev))
11056 dev_priv->display.get_display_clock_speed =
11057 i915_get_display_clock_speed;
257a7ffc 11058 else if (IS_I945GM(dev) || IS_845G(dev))
e70236a8
JB
11059 dev_priv->display.get_display_clock_speed =
11060 i9xx_misc_get_display_clock_speed;
257a7ffc
DV
11061 else if (IS_PINEVIEW(dev))
11062 dev_priv->display.get_display_clock_speed =
11063 pnv_get_display_clock_speed;
e70236a8
JB
11064 else if (IS_I915GM(dev))
11065 dev_priv->display.get_display_clock_speed =
11066 i915gm_get_display_clock_speed;
11067 else if (IS_I865G(dev))
11068 dev_priv->display.get_display_clock_speed =
11069 i865_get_display_clock_speed;
f0f8a9ce 11070 else if (IS_I85X(dev))
e70236a8
JB
11071 dev_priv->display.get_display_clock_speed =
11072 i855_get_display_clock_speed;
11073 else /* 852, 830 */
11074 dev_priv->display.get_display_clock_speed =
11075 i830_get_display_clock_speed;
11076
7f8a8569 11077 if (HAS_PCH_SPLIT(dev)) {
f00a3ddf 11078 if (IS_GEN5(dev)) {
674cf967 11079 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
e0dac65e 11080 dev_priv->display.write_eld = ironlake_write_eld;
1398261a 11081 } else if (IS_GEN6(dev)) {
674cf967 11082 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
e0dac65e 11083 dev_priv->display.write_eld = ironlake_write_eld;
357555c0
JB
11084 } else if (IS_IVYBRIDGE(dev)) {
11085 /* FIXME: detect B0+ stepping and use auto training */
11086 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
e0dac65e 11087 dev_priv->display.write_eld = ironlake_write_eld;
01a415fd
DV
11088 dev_priv->display.modeset_global_resources =
11089 ivb_modeset_global_resources;
4e0bbc31 11090 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
c82e4d26 11091 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
83358c85 11092 dev_priv->display.write_eld = haswell_write_eld;
d6dd9eb1
DV
11093 dev_priv->display.modeset_global_resources =
11094 haswell_modeset_global_resources;
a0e63c22 11095 }
6067aaea 11096 } else if (IS_G4X(dev)) {
e0dac65e 11097 dev_priv->display.write_eld = g4x_write_eld;
30a970c6
JB
11098 } else if (IS_VALLEYVIEW(dev)) {
11099 dev_priv->display.modeset_global_resources =
11100 valleyview_modeset_global_resources;
9ca2fe73 11101 dev_priv->display.write_eld = ironlake_write_eld;
e70236a8 11102 }
8c9f3aaf
JB
11103
11104 /* Default just returns -ENODEV to indicate unsupported */
11105 dev_priv->display.queue_flip = intel_default_queue_flip;
11106
11107 switch (INTEL_INFO(dev)->gen) {
11108 case 2:
11109 dev_priv->display.queue_flip = intel_gen2_queue_flip;
11110 break;
11111
11112 case 3:
11113 dev_priv->display.queue_flip = intel_gen3_queue_flip;
11114 break;
11115
11116 case 4:
11117 case 5:
11118 dev_priv->display.queue_flip = intel_gen4_queue_flip;
11119 break;
11120
11121 case 6:
11122 dev_priv->display.queue_flip = intel_gen6_queue_flip;
11123 break;
7c9017e5 11124 case 7:
4e0bbc31 11125 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
7c9017e5
JB
11126 dev_priv->display.queue_flip = intel_gen7_queue_flip;
11127 break;
8c9f3aaf 11128 }
7bd688cd
JN
11129
11130 intel_panel_init_backlight_funcs(dev);
e70236a8
JB
11131}
11132
b690e96c
JB
11133/*
11134 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
11135 * resume, or other times. This quirk makes sure that's the case for
11136 * affected systems.
11137 */
0206e353 11138static void quirk_pipea_force(struct drm_device *dev)
b690e96c
JB
11139{
11140 struct drm_i915_private *dev_priv = dev->dev_private;
11141
11142 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
bc0daf48 11143 DRM_INFO("applying pipe a force quirk\n");
b690e96c
JB
11144}
11145
435793df
KP
11146/*
11147 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
11148 */
11149static void quirk_ssc_force_disable(struct drm_device *dev)
11150{
11151 struct drm_i915_private *dev_priv = dev->dev_private;
11152 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
bc0daf48 11153 DRM_INFO("applying lvds SSC disable quirk\n");
435793df
KP
11154}
11155
4dca20ef 11156/*
5a15ab5b
CE
11157 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
11158 * brightness value
4dca20ef
CE
11159 */
11160static void quirk_invert_brightness(struct drm_device *dev)
11161{
11162 struct drm_i915_private *dev_priv = dev->dev_private;
11163 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
bc0daf48 11164 DRM_INFO("applying inverted panel brightness quirk\n");
435793df
KP
11165}
11166
b690e96c
JB
11167struct intel_quirk {
11168 int device;
11169 int subsystem_vendor;
11170 int subsystem_device;
11171 void (*hook)(struct drm_device *dev);
11172};
11173
5f85f176
EE
11174/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
11175struct intel_dmi_quirk {
11176 void (*hook)(struct drm_device *dev);
11177 const struct dmi_system_id (*dmi_id_list)[];
11178};
11179
11180static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
11181{
11182 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
11183 return 1;
11184}
11185
11186static const struct intel_dmi_quirk intel_dmi_quirks[] = {
11187 {
11188 .dmi_id_list = &(const struct dmi_system_id[]) {
11189 {
11190 .callback = intel_dmi_reverse_brightness,
11191 .ident = "NCR Corporation",
11192 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
11193 DMI_MATCH(DMI_PRODUCT_NAME, ""),
11194 },
11195 },
11196 { } /* terminating entry */
11197 },
11198 .hook = quirk_invert_brightness,
11199 },
11200};
11201
c43b5634 11202static struct intel_quirk intel_quirks[] = {
b690e96c 11203 /* HP Mini needs pipe A force quirk (LP: #322104) */
0206e353 11204 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
b690e96c 11205
b690e96c
JB
11206 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
11207 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
11208
b690e96c
JB
11209 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
11210 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
11211
a4945f95 11212 /* 830 needs to leave pipe A & dpll A up */
dcdaed6e 11213 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
435793df
KP
11214
11215 /* Lenovo U160 cannot use SSC on LVDS */
11216 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
070d329a
MAS
11217
11218 /* Sony Vaio Y cannot use SSC on LVDS */
11219 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
5a15ab5b 11220
be505f64
AH
11221 /* Acer Aspire 5734Z must invert backlight brightness */
11222 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
11223
11224 /* Acer/eMachines G725 */
11225 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
11226
11227 /* Acer/eMachines e725 */
11228 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
11229
11230 /* Acer/Packard Bell NCL20 */
11231 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
11232
11233 /* Acer Aspire 4736Z */
11234 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
0f540c3a
JN
11235
11236 /* Acer Aspire 5336 */
11237 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
b690e96c
JB
11238};
11239
11240static void intel_init_quirks(struct drm_device *dev)
11241{
11242 struct pci_dev *d = dev->pdev;
11243 int i;
11244
11245 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
11246 struct intel_quirk *q = &intel_quirks[i];
11247
11248 if (d->device == q->device &&
11249 (d->subsystem_vendor == q->subsystem_vendor ||
11250 q->subsystem_vendor == PCI_ANY_ID) &&
11251 (d->subsystem_device == q->subsystem_device ||
11252 q->subsystem_device == PCI_ANY_ID))
11253 q->hook(dev);
11254 }
5f85f176
EE
11255 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
11256 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
11257 intel_dmi_quirks[i].hook(dev);
11258 }
b690e96c
JB
11259}
11260
9cce37f4
JB
11261/* Disable the VGA plane that we never use */
11262static void i915_disable_vga(struct drm_device *dev)
11263{
11264 struct drm_i915_private *dev_priv = dev->dev_private;
11265 u8 sr1;
766aa1c4 11266 u32 vga_reg = i915_vgacntrl_reg(dev);
9cce37f4 11267
2b37c616 11268 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
9cce37f4 11269 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
3fdcf431 11270 outb(SR01, VGA_SR_INDEX);
9cce37f4
JB
11271 sr1 = inb(VGA_SR_DATA);
11272 outb(sr1 | 1<<5, VGA_SR_DATA);
11273 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
11274 udelay(300);
11275
11276 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
11277 POSTING_READ(vga_reg);
11278}
11279
f817586c
DV
11280void intel_modeset_init_hw(struct drm_device *dev)
11281{
a8f78b58
ED
11282 intel_prepare_ddi(dev);
11283
f817586c
DV
11284 intel_init_clock_gating(dev);
11285
5382f5f3 11286 intel_reset_dpio(dev);
40e9cf64 11287
79f5b2c7 11288 mutex_lock(&dev->struct_mutex);
8090c6b9 11289 intel_enable_gt_powersave(dev);
79f5b2c7 11290 mutex_unlock(&dev->struct_mutex);
f817586c
DV
11291}
11292
7d708ee4
ID
11293void intel_modeset_suspend_hw(struct drm_device *dev)
11294{
11295 intel_suspend_hw(dev);
11296}
11297
79e53945
JB
11298void intel_modeset_init(struct drm_device *dev)
11299{
652c393a 11300 struct drm_i915_private *dev_priv = dev->dev_private;
1fe47785 11301 int sprite, ret;
8cc87b75 11302 enum pipe pipe;
46f297fb 11303 struct intel_crtc *crtc;
79e53945
JB
11304
11305 drm_mode_config_init(dev);
11306
11307 dev->mode_config.min_width = 0;
11308 dev->mode_config.min_height = 0;
11309
019d96cb
DA
11310 dev->mode_config.preferred_depth = 24;
11311 dev->mode_config.prefer_shadow = 1;
11312
e6ecefaa 11313 dev->mode_config.funcs = &intel_mode_funcs;
79e53945 11314
b690e96c
JB
11315 intel_init_quirks(dev);
11316
1fa61106
ED
11317 intel_init_pm(dev);
11318
e3c74757
BW
11319 if (INTEL_INFO(dev)->num_pipes == 0)
11320 return;
11321
e70236a8
JB
11322 intel_init_display(dev);
11323
a6c45cf0
CW
11324 if (IS_GEN2(dev)) {
11325 dev->mode_config.max_width = 2048;
11326 dev->mode_config.max_height = 2048;
11327 } else if (IS_GEN3(dev)) {
5e4d6fa7
KP
11328 dev->mode_config.max_width = 4096;
11329 dev->mode_config.max_height = 4096;
79e53945 11330 } else {
a6c45cf0
CW
11331 dev->mode_config.max_width = 8192;
11332 dev->mode_config.max_height = 8192;
79e53945 11333 }
5d4545ae 11334 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
79e53945 11335
28c97730 11336 DRM_DEBUG_KMS("%d display pipe%s available.\n",
7eb552ae
BW
11337 INTEL_INFO(dev)->num_pipes,
11338 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
79e53945 11339
8cc87b75
DL
11340 for_each_pipe(pipe) {
11341 intel_crtc_init(dev, pipe);
1fe47785
DL
11342 for_each_sprite(pipe, sprite) {
11343 ret = intel_plane_init(dev, pipe, sprite);
7f1f3851 11344 if (ret)
06da8da2 11345 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
1fe47785 11346 pipe_name(pipe), sprite_name(pipe, sprite), ret);
7f1f3851 11347 }
79e53945
JB
11348 }
11349
f42bb70d 11350 intel_init_dpio(dev);
5382f5f3 11351 intel_reset_dpio(dev);
f42bb70d 11352
79f689aa 11353 intel_cpu_pll_init(dev);
e72f9fbf 11354 intel_shared_dpll_init(dev);
ee7b9f93 11355
9cce37f4
JB
11356 /* Just disable it once at startup */
11357 i915_disable_vga(dev);
79e53945 11358 intel_setup_outputs(dev);
11be49eb
CW
11359
11360 /* Just in case the BIOS is doing something questionable. */
11361 intel_disable_fbc(dev);
fa9fa083 11362
8b687df4 11363 mutex_lock(&dev->mode_config.mutex);
fa9fa083 11364 intel_modeset_setup_hw_state(dev, false);
8b687df4 11365 mutex_unlock(&dev->mode_config.mutex);
46f297fb
JB
11366
11367 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11368 base.head) {
11369 if (!crtc->active)
11370 continue;
11371
46f297fb 11372 /*
46f297fb
JB
11373 * Note that reserving the BIOS fb up front prevents us
11374 * from stuffing other stolen allocations like the ring
11375 * on top. This prevents some ugliness at boot time, and
11376 * can even allow for smooth boot transitions if the BIOS
11377 * fb is large enough for the active pipe configuration.
11378 */
11379 if (dev_priv->display.get_plane_config) {
11380 dev_priv->display.get_plane_config(crtc,
11381 &crtc->plane_config);
11382 /*
11383 * If the fb is shared between multiple heads, we'll
11384 * just get the first one.
11385 */
484b41dd 11386 intel_find_plane_obj(crtc, &crtc->plane_config);
46f297fb 11387 }
46f297fb 11388 }
2c7111db
CW
11389}
11390
24929352
DV
11391static void
11392intel_connector_break_all_links(struct intel_connector *connector)
11393{
11394 connector->base.dpms = DRM_MODE_DPMS_OFF;
11395 connector->base.encoder = NULL;
11396 connector->encoder->connectors_active = false;
11397 connector->encoder->base.crtc = NULL;
11398}
11399
7fad798e
DV
11400static void intel_enable_pipe_a(struct drm_device *dev)
11401{
11402 struct intel_connector *connector;
11403 struct drm_connector *crt = NULL;
11404 struct intel_load_detect_pipe load_detect_temp;
11405
11406 /* We can't just switch on the pipe A, we need to set things up with a
11407 * proper mode and output configuration. As a gross hack, enable pipe A
11408 * by enabling the load detect pipe once. */
11409 list_for_each_entry(connector,
11410 &dev->mode_config.connector_list,
11411 base.head) {
11412 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
11413 crt = &connector->base;
11414 break;
11415 }
11416 }
11417
11418 if (!crt)
11419 return;
11420
11421 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
11422 intel_release_load_detect_pipe(crt, &load_detect_temp);
11423
652c393a 11424
7fad798e
DV
11425}
11426
fa555837
DV
11427static bool
11428intel_check_plane_mapping(struct intel_crtc *crtc)
11429{
7eb552ae
BW
11430 struct drm_device *dev = crtc->base.dev;
11431 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837
DV
11432 u32 reg, val;
11433
7eb552ae 11434 if (INTEL_INFO(dev)->num_pipes == 1)
fa555837
DV
11435 return true;
11436
11437 reg = DSPCNTR(!crtc->plane);
11438 val = I915_READ(reg);
11439
11440 if ((val & DISPLAY_PLANE_ENABLE) &&
11441 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
11442 return false;
11443
11444 return true;
11445}
11446
24929352
DV
11447static void intel_sanitize_crtc(struct intel_crtc *crtc)
11448{
11449 struct drm_device *dev = crtc->base.dev;
11450 struct drm_i915_private *dev_priv = dev->dev_private;
fa555837 11451 u32 reg;
24929352 11452
24929352 11453 /* Clear any frame start delays used for debugging left by the BIOS */
3b117c8f 11454 reg = PIPECONF(crtc->config.cpu_transcoder);
24929352
DV
11455 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
11456
11457 /* We need to sanitize the plane -> pipe mapping first because this will
fa555837
DV
11458 * disable the crtc (and hence change the state) if it is wrong. Note
11459 * that gen4+ has a fixed plane -> pipe mapping. */
11460 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
24929352
DV
11461 struct intel_connector *connector;
11462 bool plane;
11463
24929352
DV
11464 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
11465 crtc->base.base.id);
11466
11467 /* Pipe has the wrong plane attached and the plane is active.
11468 * Temporarily change the plane mapping and disable everything
11469 * ... */
11470 plane = crtc->plane;
11471 crtc->plane = !plane;
11472 dev_priv->display.crtc_disable(&crtc->base);
11473 crtc->plane = plane;
11474
11475 /* ... and break all links. */
11476 list_for_each_entry(connector, &dev->mode_config.connector_list,
11477 base.head) {
11478 if (connector->encoder->base.crtc != &crtc->base)
11479 continue;
11480
11481 intel_connector_break_all_links(connector);
11482 }
11483
11484 WARN_ON(crtc->active);
11485 crtc->base.enabled = false;
11486 }
24929352 11487
7fad798e
DV
11488 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
11489 crtc->pipe == PIPE_A && !crtc->active) {
11490 /* BIOS forgot to enable pipe A, this mostly happens after
11491 * resume. Force-enable the pipe to fix this, the update_dpms
11492 * call below we restore the pipe to the right state, but leave
11493 * the required bits on. */
11494 intel_enable_pipe_a(dev);
11495 }
11496
24929352
DV
11497 /* Adjust the state of the output pipe according to whether we
11498 * have active connectors/encoders. */
11499 intel_crtc_update_dpms(&crtc->base);
11500
11501 if (crtc->active != crtc->base.enabled) {
11502 struct intel_encoder *encoder;
11503
11504 /* This can happen either due to bugs in the get_hw_state
11505 * functions or because the pipe is force-enabled due to the
11506 * pipe A quirk. */
11507 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
11508 crtc->base.base.id,
11509 crtc->base.enabled ? "enabled" : "disabled",
11510 crtc->active ? "enabled" : "disabled");
11511
11512 crtc->base.enabled = crtc->active;
11513
11514 /* Because we only establish the connector -> encoder ->
11515 * crtc links if something is active, this means the
11516 * crtc is now deactivated. Break the links. connector
11517 * -> encoder links are only establish when things are
11518 * actually up, hence no need to break them. */
11519 WARN_ON(crtc->active);
11520
11521 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
11522 WARN_ON(encoder->connectors_active);
11523 encoder->base.crtc = NULL;
11524 }
11525 }
11526}
11527
11528static void intel_sanitize_encoder(struct intel_encoder *encoder)
11529{
11530 struct intel_connector *connector;
11531 struct drm_device *dev = encoder->base.dev;
11532
11533 /* We need to check both for a crtc link (meaning that the
11534 * encoder is active and trying to read from a pipe) and the
11535 * pipe itself being active. */
11536 bool has_active_crtc = encoder->base.crtc &&
11537 to_intel_crtc(encoder->base.crtc)->active;
11538
11539 if (encoder->connectors_active && !has_active_crtc) {
11540 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
11541 encoder->base.base.id,
11542 drm_get_encoder_name(&encoder->base));
11543
11544 /* Connector is active, but has no active pipe. This is
11545 * fallout from our resume register restoring. Disable
11546 * the encoder manually again. */
11547 if (encoder->base.crtc) {
11548 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
11549 encoder->base.base.id,
11550 drm_get_encoder_name(&encoder->base));
11551 encoder->disable(encoder);
11552 }
11553
11554 /* Inconsistent output/port/pipe state happens presumably due to
11555 * a bug in one of the get_hw_state functions. Or someplace else
11556 * in our code, like the register restore mess on resume. Clamp
11557 * things to off as a safer default. */
11558 list_for_each_entry(connector,
11559 &dev->mode_config.connector_list,
11560 base.head) {
11561 if (connector->encoder != encoder)
11562 continue;
11563
11564 intel_connector_break_all_links(connector);
11565 }
11566 }
11567 /* Enabled encoders without active connectors will be fixed in
11568 * the crtc fixup. */
11569}
11570
04098753 11571void i915_redisable_vga_power_on(struct drm_device *dev)
0fde901f
KM
11572{
11573 struct drm_i915_private *dev_priv = dev->dev_private;
766aa1c4 11574 u32 vga_reg = i915_vgacntrl_reg(dev);
0fde901f 11575
04098753
ID
11576 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
11577 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
11578 i915_disable_vga(dev);
11579 }
11580}
11581
11582void i915_redisable_vga(struct drm_device *dev)
11583{
11584 struct drm_i915_private *dev_priv = dev->dev_private;
11585
8dc8a27c
PZ
11586 /* This function can be called both from intel_modeset_setup_hw_state or
11587 * at a very early point in our resume sequence, where the power well
11588 * structures are not yet restored. Since this function is at a very
11589 * paranoid "someone might have enabled VGA while we were not looking"
11590 * level, just check if the power well is enabled instead of trying to
11591 * follow the "don't touch the power well if we don't need it" policy
11592 * the rest of the driver uses. */
04098753 11593 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
8dc8a27c
PZ
11594 return;
11595
04098753 11596 i915_redisable_vga_power_on(dev);
0fde901f
KM
11597}
11598
30e984df 11599static void intel_modeset_readout_hw_state(struct drm_device *dev)
24929352
DV
11600{
11601 struct drm_i915_private *dev_priv = dev->dev_private;
11602 enum pipe pipe;
24929352
DV
11603 struct intel_crtc *crtc;
11604 struct intel_encoder *encoder;
11605 struct intel_connector *connector;
5358901f 11606 int i;
24929352 11607
0e8ffe1b
DV
11608 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11609 base.head) {
88adfff1 11610 memset(&crtc->config, 0, sizeof(crtc->config));
3b117c8f 11611
0e8ffe1b
DV
11612 crtc->active = dev_priv->display.get_pipe_config(crtc,
11613 &crtc->config);
24929352
DV
11614
11615 crtc->base.enabled = crtc->active;
4c445e0e 11616 crtc->primary_enabled = crtc->active;
24929352
DV
11617
11618 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
11619 crtc->base.base.id,
11620 crtc->active ? "enabled" : "disabled");
11621 }
11622
5358901f 11623 /* FIXME: Smash this into the new shared dpll infrastructure. */
affa9354 11624 if (HAS_DDI(dev))
6441ab5f
PZ
11625 intel_ddi_setup_hw_pll_state(dev);
11626
5358901f
DV
11627 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11628 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11629
11630 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
11631 pll->active = 0;
11632 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11633 base.head) {
11634 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11635 pll->active++;
11636 }
11637 pll->refcount = pll->active;
11638
35c95375
DV
11639 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
11640 pll->name, pll->refcount, pll->on);
5358901f
DV
11641 }
11642
24929352
DV
11643 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11644 base.head) {
11645 pipe = 0;
11646
11647 if (encoder->get_hw_state(encoder, &pipe)) {
045ac3b5
JB
11648 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11649 encoder->base.crtc = &crtc->base;
1d37b689 11650 encoder->get_config(encoder, &crtc->config);
24929352
DV
11651 } else {
11652 encoder->base.crtc = NULL;
11653 }
11654
11655 encoder->connectors_active = false;
6f2bcceb 11656 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
24929352
DV
11657 encoder->base.base.id,
11658 drm_get_encoder_name(&encoder->base),
11659 encoder->base.crtc ? "enabled" : "disabled",
6f2bcceb 11660 pipe_name(pipe));
24929352
DV
11661 }
11662
11663 list_for_each_entry(connector, &dev->mode_config.connector_list,
11664 base.head) {
11665 if (connector->get_hw_state(connector)) {
11666 connector->base.dpms = DRM_MODE_DPMS_ON;
11667 connector->encoder->connectors_active = true;
11668 connector->base.encoder = &connector->encoder->base;
11669 } else {
11670 connector->base.dpms = DRM_MODE_DPMS_OFF;
11671 connector->base.encoder = NULL;
11672 }
11673 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
11674 connector->base.base.id,
11675 drm_get_connector_name(&connector->base),
11676 connector->base.encoder ? "enabled" : "disabled");
11677 }
30e984df
DV
11678}
11679
11680/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
11681 * and i915 state tracking structures. */
11682void intel_modeset_setup_hw_state(struct drm_device *dev,
11683 bool force_restore)
11684{
11685 struct drm_i915_private *dev_priv = dev->dev_private;
11686 enum pipe pipe;
30e984df
DV
11687 struct intel_crtc *crtc;
11688 struct intel_encoder *encoder;
35c95375 11689 int i;
30e984df
DV
11690
11691 intel_modeset_readout_hw_state(dev);
24929352 11692
babea61d
JB
11693 /*
11694 * Now that we have the config, copy it to each CRTC struct
11695 * Note that this could go away if we move to using crtc_config
11696 * checking everywhere.
11697 */
11698 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
11699 base.head) {
d330a953 11700 if (crtc->active && i915.fastboot) {
f6a83288 11701 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
babea61d
JB
11702 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
11703 crtc->base.base.id);
11704 drm_mode_debug_printmodeline(&crtc->base.mode);
11705 }
11706 }
11707
24929352
DV
11708 /* HW state is read out, now we need to sanitize this mess. */
11709 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
11710 base.head) {
11711 intel_sanitize_encoder(encoder);
11712 }
11713
11714 for_each_pipe(pipe) {
11715 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
11716 intel_sanitize_crtc(crtc);
c0b03411 11717 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
24929352 11718 }
9a935856 11719
35c95375
DV
11720 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11721 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11722
11723 if (!pll->on || pll->active)
11724 continue;
11725
11726 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
11727
11728 pll->disable(dev_priv, pll);
11729 pll->on = false;
11730 }
11731
96f90c54 11732 if (HAS_PCH_SPLIT(dev))
243e6a44
VS
11733 ilk_wm_get_hw_state(dev);
11734
45e2b5f6 11735 if (force_restore) {
7d0bc1ea
VS
11736 i915_redisable_vga(dev);
11737
f30da187
DV
11738 /*
11739 * We need to use raw interfaces for restoring state to avoid
11740 * checking (bogus) intermediate states.
11741 */
45e2b5f6 11742 for_each_pipe(pipe) {
b5644d05
JB
11743 struct drm_crtc *crtc =
11744 dev_priv->pipe_to_crtc_mapping[pipe];
f30da187
DV
11745
11746 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
11747 crtc->fb);
45e2b5f6
DV
11748 }
11749 } else {
11750 intel_modeset_update_staged_output_state(dev);
11751 }
8af6cf88
DV
11752
11753 intel_modeset_check_state(dev);
2c7111db
CW
11754}
11755
11756void intel_modeset_gem_init(struct drm_device *dev)
11757{
484b41dd
JB
11758 struct drm_crtc *c;
11759 struct intel_framebuffer *fb;
11760
1833b134 11761 intel_modeset_init_hw(dev);
02e792fb
DV
11762
11763 intel_setup_overlay(dev);
484b41dd
JB
11764
11765 /*
11766 * Make sure any fbs we allocated at startup are properly
11767 * pinned & fenced. When we do the allocation it's too early
11768 * for this.
11769 */
11770 mutex_lock(&dev->struct_mutex);
11771 list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
11772 if (!c->fb)
11773 continue;
11774
11775 fb = to_intel_framebuffer(c->fb);
11776 if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
11777 DRM_ERROR("failed to pin boot fb on pipe %d\n",
11778 to_intel_crtc(c)->pipe);
11779 drm_framebuffer_unreference(c->fb);
11780 c->fb = NULL;
11781 }
11782 }
11783 mutex_unlock(&dev->struct_mutex);
79e53945
JB
11784}
11785
4932e2c3
ID
11786void intel_connector_unregister(struct intel_connector *intel_connector)
11787{
11788 struct drm_connector *connector = &intel_connector->base;
11789
11790 intel_panel_destroy_backlight(connector);
11791 drm_sysfs_connector_remove(connector);
11792}
11793
79e53945
JB
11794void intel_modeset_cleanup(struct drm_device *dev)
11795{
652c393a
JB
11796 struct drm_i915_private *dev_priv = dev->dev_private;
11797 struct drm_crtc *crtc;
d9255d57 11798 struct drm_connector *connector;
652c393a 11799
fd0c0642
DV
11800 /*
11801 * Interrupts and polling as the first thing to avoid creating havoc.
11802 * Too much stuff here (turning of rps, connectors, ...) would
11803 * experience fancy races otherwise.
11804 */
11805 drm_irq_uninstall(dev);
11806 cancel_work_sync(&dev_priv->hotplug_work);
11807 /*
11808 * Due to the hpd irq storm handling the hotplug work can re-arm the
11809 * poll handlers. Hence disable polling after hpd handling is shut down.
11810 */
f87ea761 11811 drm_kms_helper_poll_fini(dev);
fd0c0642 11812
652c393a
JB
11813 mutex_lock(&dev->struct_mutex);
11814
723bfd70
JB
11815 intel_unregister_dsm_handler();
11816
652c393a
JB
11817 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
11818 /* Skip inactive CRTCs */
11819 if (!crtc->fb)
11820 continue;
11821
3dec0095 11822 intel_increase_pllclock(crtc);
652c393a
JB
11823 }
11824
973d04f9 11825 intel_disable_fbc(dev);
e70236a8 11826
8090c6b9 11827 intel_disable_gt_powersave(dev);
0cdab21f 11828
930ebb46
DV
11829 ironlake_teardown_rc6(dev);
11830
69341a5e
KH
11831 mutex_unlock(&dev->struct_mutex);
11832
1630fe75
CW
11833 /* flush any delayed tasks or pending work */
11834 flush_scheduled_work();
11835
db31af1d
JN
11836 /* destroy the backlight and sysfs files before encoders/connectors */
11837 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4932e2c3
ID
11838 struct intel_connector *intel_connector;
11839
11840 intel_connector = to_intel_connector(connector);
11841 intel_connector->unregister(intel_connector);
db31af1d 11842 }
d9255d57 11843
79e53945 11844 drm_mode_config_cleanup(dev);
4d7bb011
DV
11845
11846 intel_cleanup_overlay(dev);
79e53945
JB
11847}
11848
f1c79df3
ZW
11849/*
11850 * Return which encoder is currently attached for connector.
11851 */
df0e9248 11852struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
79e53945 11853{
df0e9248
CW
11854 return &intel_attached_encoder(connector)->base;
11855}
f1c79df3 11856
df0e9248
CW
11857void intel_connector_attach_encoder(struct intel_connector *connector,
11858 struct intel_encoder *encoder)
11859{
11860 connector->encoder = encoder;
11861 drm_mode_connector_attach_encoder(&connector->base,
11862 &encoder->base);
79e53945 11863}
28d52043
DA
11864
11865/*
11866 * set vga decode state - true == enable VGA decode
11867 */
11868int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
11869{
11870 struct drm_i915_private *dev_priv = dev->dev_private;
a885b3cc 11871 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
28d52043
DA
11872 u16 gmch_ctrl;
11873
75fa041d
CW
11874 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
11875 DRM_ERROR("failed to read control word\n");
11876 return -EIO;
11877 }
11878
c0cc8a55
CW
11879 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
11880 return 0;
11881
28d52043
DA
11882 if (state)
11883 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
11884 else
11885 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
75fa041d
CW
11886
11887 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
11888 DRM_ERROR("failed to write control word\n");
11889 return -EIO;
11890 }
11891
28d52043
DA
11892 return 0;
11893}
c4a1d9e4 11894
c4a1d9e4 11895struct intel_display_error_state {
ff57f1b0
PZ
11896
11897 u32 power_well_driver;
11898
63b66e5b
CW
11899 int num_transcoders;
11900
c4a1d9e4
CW
11901 struct intel_cursor_error_state {
11902 u32 control;
11903 u32 position;
11904 u32 base;
11905 u32 size;
52331309 11906 } cursor[I915_MAX_PIPES];
c4a1d9e4
CW
11907
11908 struct intel_pipe_error_state {
ddf9c536 11909 bool power_domain_on;
c4a1d9e4 11910 u32 source;
52331309 11911 } pipe[I915_MAX_PIPES];
c4a1d9e4
CW
11912
11913 struct intel_plane_error_state {
11914 u32 control;
11915 u32 stride;
11916 u32 size;
11917 u32 pos;
11918 u32 addr;
11919 u32 surface;
11920 u32 tile_offset;
52331309 11921 } plane[I915_MAX_PIPES];
63b66e5b
CW
11922
11923 struct intel_transcoder_error_state {
ddf9c536 11924 bool power_domain_on;
63b66e5b
CW
11925 enum transcoder cpu_transcoder;
11926
11927 u32 conf;
11928
11929 u32 htotal;
11930 u32 hblank;
11931 u32 hsync;
11932 u32 vtotal;
11933 u32 vblank;
11934 u32 vsync;
11935 } transcoder[4];
c4a1d9e4
CW
11936};
11937
11938struct intel_display_error_state *
11939intel_display_capture_error_state(struct drm_device *dev)
11940{
0206e353 11941 drm_i915_private_t *dev_priv = dev->dev_private;
c4a1d9e4 11942 struct intel_display_error_state *error;
63b66e5b
CW
11943 int transcoders[] = {
11944 TRANSCODER_A,
11945 TRANSCODER_B,
11946 TRANSCODER_C,
11947 TRANSCODER_EDP,
11948 };
c4a1d9e4
CW
11949 int i;
11950
63b66e5b
CW
11951 if (INTEL_INFO(dev)->num_pipes == 0)
11952 return NULL;
11953
9d1cb914 11954 error = kzalloc(sizeof(*error), GFP_ATOMIC);
c4a1d9e4
CW
11955 if (error == NULL)
11956 return NULL;
11957
190be112 11958 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ff57f1b0
PZ
11959 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
11960
52331309 11961 for_each_pipe(i) {
ddf9c536 11962 error->pipe[i].power_domain_on =
da7e29bd
ID
11963 intel_display_power_enabled_sw(dev_priv,
11964 POWER_DOMAIN_PIPE(i));
ddf9c536 11965 if (!error->pipe[i].power_domain_on)
9d1cb914
PZ
11966 continue;
11967
a18c4c3d
PZ
11968 if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
11969 error->cursor[i].control = I915_READ(CURCNTR(i));
11970 error->cursor[i].position = I915_READ(CURPOS(i));
11971 error->cursor[i].base = I915_READ(CURBASE(i));
11972 } else {
11973 error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
11974 error->cursor[i].position = I915_READ(CURPOS_IVB(i));
11975 error->cursor[i].base = I915_READ(CURBASE_IVB(i));
11976 }
c4a1d9e4
CW
11977
11978 error->plane[i].control = I915_READ(DSPCNTR(i));
11979 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
80ca378b 11980 if (INTEL_INFO(dev)->gen <= 3) {
51889b35 11981 error->plane[i].size = I915_READ(DSPSIZE(i));
80ca378b
PZ
11982 error->plane[i].pos = I915_READ(DSPPOS(i));
11983 }
ca291363
PZ
11984 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
11985 error->plane[i].addr = I915_READ(DSPADDR(i));
c4a1d9e4
CW
11986 if (INTEL_INFO(dev)->gen >= 4) {
11987 error->plane[i].surface = I915_READ(DSPSURF(i));
11988 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
11989 }
11990
c4a1d9e4 11991 error->pipe[i].source = I915_READ(PIPESRC(i));
63b66e5b
CW
11992 }
11993
11994 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
11995 if (HAS_DDI(dev_priv->dev))
11996 error->num_transcoders++; /* Account for eDP. */
11997
11998 for (i = 0; i < error->num_transcoders; i++) {
11999 enum transcoder cpu_transcoder = transcoders[i];
12000
ddf9c536 12001 error->transcoder[i].power_domain_on =
da7e29bd 12002 intel_display_power_enabled_sw(dev_priv,
38cc1daf 12003 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
ddf9c536 12004 if (!error->transcoder[i].power_domain_on)
9d1cb914
PZ
12005 continue;
12006
63b66e5b
CW
12007 error->transcoder[i].cpu_transcoder = cpu_transcoder;
12008
12009 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
12010 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
12011 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
12012 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
12013 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
12014 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
12015 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
c4a1d9e4
CW
12016 }
12017
12018 return error;
12019}
12020
edc3d884
MK
12021#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
12022
c4a1d9e4 12023void
edc3d884 12024intel_display_print_error_state(struct drm_i915_error_state_buf *m,
c4a1d9e4
CW
12025 struct drm_device *dev,
12026 struct intel_display_error_state *error)
12027{
12028 int i;
12029
63b66e5b
CW
12030 if (!error)
12031 return;
12032
edc3d884 12033 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
190be112 12034 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
edc3d884 12035 err_printf(m, "PWR_WELL_CTL2: %08x\n",
ff57f1b0 12036 error->power_well_driver);
52331309 12037 for_each_pipe(i) {
edc3d884 12038 err_printf(m, "Pipe [%d]:\n", i);
ddf9c536
ID
12039 err_printf(m, " Power: %s\n",
12040 error->pipe[i].power_domain_on ? "on" : "off");
edc3d884 12041 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
edc3d884
MK
12042
12043 err_printf(m, "Plane [%d]:\n", i);
12044 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
12045 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
80ca378b 12046 if (INTEL_INFO(dev)->gen <= 3) {
edc3d884
MK
12047 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
12048 err_printf(m, " POS: %08x\n", error->plane[i].pos);
80ca378b 12049 }
4b71a570 12050 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
edc3d884 12051 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
c4a1d9e4 12052 if (INTEL_INFO(dev)->gen >= 4) {
edc3d884
MK
12053 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
12054 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
c4a1d9e4
CW
12055 }
12056
edc3d884
MK
12057 err_printf(m, "Cursor [%d]:\n", i);
12058 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
12059 err_printf(m, " POS: %08x\n", error->cursor[i].position);
12060 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
c4a1d9e4 12061 }
63b66e5b
CW
12062
12063 for (i = 0; i < error->num_transcoders; i++) {
1cf84bb6 12064 err_printf(m, "CPU transcoder: %c\n",
63b66e5b 12065 transcoder_name(error->transcoder[i].cpu_transcoder));
ddf9c536
ID
12066 err_printf(m, " Power: %s\n",
12067 error->transcoder[i].power_domain_on ? "on" : "off");
63b66e5b
CW
12068 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
12069 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
12070 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
12071 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
12072 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
12073 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
12074 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
12075 }
c4a1d9e4 12076}