xref: /dragonfly/sys/dev/drm/i915/intel_display.c (revision cfd1aba3)
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  * $FreeBSD: src/sys/dev/drm2/i915/intel_display.c,v 1.2 2012/05/24 19:13:54 dim Exp $
27  */
28 
29 #include <ddb/ddb.h>
30 #include <sys/limits.h>
31 
32 #include <drm/drmP.h>
33 #include <drm/drm_edid.h>
34 #include "intel_drv.h"
35 #include <drm/i915_drm.h>
36 #include "i915_drv.h"
37 #include <drm/drm_dp_helper.h>
38 #include <drm/drm_crtc_helper.h>
39 
40 #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
41 
42 bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
43 static void intel_increase_pllclock(struct drm_crtc *crtc);
44 static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
45 
46 typedef struct {
47 	/* given values */
48 	int n;
49 	int m1, m2;
50 	int p1, p2;
51 	/* derived values */
52 	int	dot;
53 	int	vco;
54 	int	m;
55 	int	p;
56 } intel_clock_t;
57 
58 typedef struct {
59 	int	min, max;
60 } intel_range_t;
61 
62 typedef struct {
63 	int	dot_limit;
64 	int	p2_slow, p2_fast;
65 } intel_p2_t;
66 
67 #define INTEL_P2_NUM		      2
68 typedef struct intel_limit intel_limit_t;
69 struct intel_limit {
70 	intel_range_t   dot, vco, n, m, m1, m2, p, p1;
71 	intel_p2_t	    p2;
72 	bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
73 			int, int, intel_clock_t *, intel_clock_t *);
74 };
75 
76 /* FDI */
77 #define IRONLAKE_FDI_FREQ		2700000 /* in kHz for mode->clock */
78 
79 static bool
80 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
81 		    int target, int refclk, intel_clock_t *match_clock,
82 		    intel_clock_t *best_clock);
83 static bool
84 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
85 			int target, int refclk, intel_clock_t *match_clock,
86 			intel_clock_t *best_clock);
87 
88 static bool
89 intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
90 		      int target, int refclk, intel_clock_t *match_clock,
91 		      intel_clock_t *best_clock);
92 static bool
93 intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
94 			   int target, int refclk, intel_clock_t *match_clock,
95 			   intel_clock_t *best_clock);
96 
97 static inline u32 /* units of 100MHz */
98 intel_fdi_link_freq(struct drm_device *dev)
99 {
100 	if (IS_GEN5(dev)) {
101 		struct drm_i915_private *dev_priv = dev->dev_private;
102 		return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
103 	} else
104 		return 27;
105 }
106 
107 static const intel_limit_t intel_limits_i8xx_dvo = {
108 	.dot = { .min = 25000, .max = 350000 },
109 	.vco = { .min = 930000, .max = 1400000 },
110 	.n = { .min = 3, .max = 16 },
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 = 2 },
118 	.find_pll = intel_find_best_PLL,
119 };
120 
121 static const intel_limit_t intel_limits_i8xx_lvds = {
122 	.dot = { .min = 25000, .max = 350000 },
123 	.vco = { .min = 930000, .max = 1400000 },
124 	.n = { .min = 3, .max = 16 },
125 	.m = { .min = 96, .max = 140 },
126 	.m1 = { .min = 18, .max = 26 },
127 	.m2 = { .min = 6, .max = 16 },
128 	.p = { .min = 4, .max = 128 },
129 	.p1 = { .min = 1, .max = 6 },
130 	.p2 = { .dot_limit = 165000,
131 		.p2_slow = 14, .p2_fast = 7 },
132 	.find_pll = intel_find_best_PLL,
133 };
134 
135 static const intel_limit_t intel_limits_i9xx_sdvo = {
136 	.dot = { .min = 20000, .max = 400000 },
137 	.vco = { .min = 1400000, .max = 2800000 },
138 	.n = { .min = 1, .max = 6 },
139 	.m = { .min = 70, .max = 120 },
140 	.m1 = { .min = 10, .max = 22 },
141 	.m2 = { .min = 5, .max = 9 },
142 	.p = { .min = 5, .max = 80 },
143 	.p1 = { .min = 1, .max = 8 },
144 	.p2 = { .dot_limit = 200000,
145 		.p2_slow = 10, .p2_fast = 5 },
146 	.find_pll = intel_find_best_PLL,
147 };
148 
149 static const intel_limit_t intel_limits_i9xx_lvds = {
150 	.dot = { .min = 20000, .max = 400000 },
151 	.vco = { .min = 1400000, .max = 2800000 },
152 	.n = { .min = 1, .max = 6 },
153 	.m = { .min = 70, .max = 120 },
154 	.m1 = { .min = 10, .max = 22 },
155 	.m2 = { .min = 5, .max = 9 },
156 	.p = { .min = 7, .max = 98 },
157 	.p1 = { .min = 1, .max = 8 },
158 	.p2 = { .dot_limit = 112000,
159 		.p2_slow = 14, .p2_fast = 7 },
160 	.find_pll = intel_find_best_PLL,
161 };
162 
163 
164 static const intel_limit_t intel_limits_g4x_sdvo = {
165 	.dot = { .min = 25000, .max = 270000 },
166 	.vco = { .min = 1750000, .max = 3500000},
167 	.n = { .min = 1, .max = 4 },
168 	.m = { .min = 104, .max = 138 },
169 	.m1 = { .min = 17, .max = 23 },
170 	.m2 = { .min = 5, .max = 11 },
171 	.p = { .min = 10, .max = 30 },
172 	.p1 = { .min = 1, .max = 3},
173 	.p2 = { .dot_limit = 270000,
174 		.p2_slow = 10,
175 		.p2_fast = 10
176 	},
177 	.find_pll = intel_g4x_find_best_PLL,
178 };
179 
180 static const intel_limit_t intel_limits_g4x_hdmi = {
181 	.dot = { .min = 22000, .max = 400000 },
182 	.vco = { .min = 1750000, .max = 3500000},
183 	.n = { .min = 1, .max = 4 },
184 	.m = { .min = 104, .max = 138 },
185 	.m1 = { .min = 16, .max = 23 },
186 	.m2 = { .min = 5, .max = 11 },
187 	.p = { .min = 5, .max = 80 },
188 	.p1 = { .min = 1, .max = 8},
189 	.p2 = { .dot_limit = 165000,
190 		.p2_slow = 10, .p2_fast = 5 },
191 	.find_pll = intel_g4x_find_best_PLL,
192 };
193 
194 static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
195 	.dot = { .min = 20000, .max = 115000 },
196 	.vco = { .min = 1750000, .max = 3500000 },
197 	.n = { .min = 1, .max = 3 },
198 	.m = { .min = 104, .max = 138 },
199 	.m1 = { .min = 17, .max = 23 },
200 	.m2 = { .min = 5, .max = 11 },
201 	.p = { .min = 28, .max = 112 },
202 	.p1 = { .min = 2, .max = 8 },
203 	.p2 = { .dot_limit = 0,
204 		.p2_slow = 14, .p2_fast = 14
205 	},
206 	.find_pll = intel_g4x_find_best_PLL,
207 };
208 
209 static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
210 	.dot = { .min = 80000, .max = 224000 },
211 	.vco = { .min = 1750000, .max = 3500000 },
212 	.n = { .min = 1, .max = 3 },
213 	.m = { .min = 104, .max = 138 },
214 	.m1 = { .min = 17, .max = 23 },
215 	.m2 = { .min = 5, .max = 11 },
216 	.p = { .min = 14, .max = 42 },
217 	.p1 = { .min = 2, .max = 6 },
218 	.p2 = { .dot_limit = 0,
219 		.p2_slow = 7, .p2_fast = 7
220 	},
221 	.find_pll = intel_g4x_find_best_PLL,
222 };
223 
224 static const intel_limit_t intel_limits_g4x_display_port = {
225 	.dot = { .min = 161670, .max = 227000 },
226 	.vco = { .min = 1750000, .max = 3500000},
227 	.n = { .min = 1, .max = 2 },
228 	.m = { .min = 97, .max = 108 },
229 	.m1 = { .min = 0x10, .max = 0x12 },
230 	.m2 = { .min = 0x05, .max = 0x06 },
231 	.p = { .min = 10, .max = 20 },
232 	.p1 = { .min = 1, .max = 2},
233 	.p2 = { .dot_limit = 0,
234 		.p2_slow = 10, .p2_fast = 10 },
235 	.find_pll = intel_find_pll_g4x_dp,
236 };
237 
238 static const intel_limit_t intel_limits_pineview_sdvo = {
239 	.dot = { .min = 20000, .max = 400000},
240 	.vco = { .min = 1700000, .max = 3500000 },
241 	/* Pineview's Ncounter is a ring counter */
242 	.n = { .min = 3, .max = 6 },
243 	.m = { .min = 2, .max = 256 },
244 	/* Pineview only has one combined m divider, which we treat as m2. */
245 	.m1 = { .min = 0, .max = 0 },
246 	.m2 = { .min = 0, .max = 254 },
247 	.p = { .min = 5, .max = 80 },
248 	.p1 = { .min = 1, .max = 8 },
249 	.p2 = { .dot_limit = 200000,
250 		.p2_slow = 10, .p2_fast = 5 },
251 	.find_pll = intel_find_best_PLL,
252 };
253 
254 static const intel_limit_t intel_limits_pineview_lvds = {
255 	.dot = { .min = 20000, .max = 400000 },
256 	.vco = { .min = 1700000, .max = 3500000 },
257 	.n = { .min = 3, .max = 6 },
258 	.m = { .min = 2, .max = 256 },
259 	.m1 = { .min = 0, .max = 0 },
260 	.m2 = { .min = 0, .max = 254 },
261 	.p = { .min = 7, .max = 112 },
262 	.p1 = { .min = 1, .max = 8 },
263 	.p2 = { .dot_limit = 112000,
264 		.p2_slow = 14, .p2_fast = 14 },
265 	.find_pll = intel_find_best_PLL,
266 };
267 
268 /* Ironlake / Sandybridge
269  *
270  * We calculate clock using (register_value + 2) for N/M1/M2, so here
271  * the range value for them is (actual_value - 2).
272  */
273 static const intel_limit_t intel_limits_ironlake_dac = {
274 	.dot = { .min = 25000, .max = 350000 },
275 	.vco = { .min = 1760000, .max = 3510000 },
276 	.n = { .min = 1, .max = 5 },
277 	.m = { .min = 79, .max = 127 },
278 	.m1 = { .min = 12, .max = 22 },
279 	.m2 = { .min = 5, .max = 9 },
280 	.p = { .min = 5, .max = 80 },
281 	.p1 = { .min = 1, .max = 8 },
282 	.p2 = { .dot_limit = 225000,
283 		.p2_slow = 10, .p2_fast = 5 },
284 	.find_pll = intel_g4x_find_best_PLL,
285 };
286 
287 static const intel_limit_t intel_limits_ironlake_single_lvds = {
288 	.dot = { .min = 25000, .max = 350000 },
289 	.vco = { .min = 1760000, .max = 3510000 },
290 	.n = { .min = 1, .max = 3 },
291 	.m = { .min = 79, .max = 118 },
292 	.m1 = { .min = 12, .max = 22 },
293 	.m2 = { .min = 5, .max = 9 },
294 	.p = { .min = 28, .max = 112 },
295 	.p1 = { .min = 2, .max = 8 },
296 	.p2 = { .dot_limit = 225000,
297 		.p2_slow = 14, .p2_fast = 14 },
298 	.find_pll = intel_g4x_find_best_PLL,
299 };
300 
301 static const intel_limit_t intel_limits_ironlake_dual_lvds = {
302 	.dot = { .min = 25000, .max = 350000 },
303 	.vco = { .min = 1760000, .max = 3510000 },
304 	.n = { .min = 1, .max = 3 },
305 	.m = { .min = 79, .max = 127 },
306 	.m1 = { .min = 12, .max = 22 },
307 	.m2 = { .min = 5, .max = 9 },
308 	.p = { .min = 14, .max = 56 },
309 	.p1 = { .min = 2, .max = 8 },
310 	.p2 = { .dot_limit = 225000,
311 		.p2_slow = 7, .p2_fast = 7 },
312 	.find_pll = intel_g4x_find_best_PLL,
313 };
314 
315 /* LVDS 100mhz refclk limits. */
316 static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
317 	.dot = { .min = 25000, .max = 350000 },
318 	.vco = { .min = 1760000, .max = 3510000 },
319 	.n = { .min = 1, .max = 2 },
320 	.m = { .min = 79, .max = 126 },
321 	.m1 = { .min = 12, .max = 22 },
322 	.m2 = { .min = 5, .max = 9 },
323 	.p = { .min = 28, .max = 112 },
324 	.p1 = { .min = 2, .max = 8 },
325 	.p2 = { .dot_limit = 225000,
326 		.p2_slow = 14, .p2_fast = 14 },
327 	.find_pll = intel_g4x_find_best_PLL,
328 };
329 
330 static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
331 	.dot = { .min = 25000, .max = 350000 },
332 	.vco = { .min = 1760000, .max = 3510000 },
333 	.n = { .min = 1, .max = 3 },
334 	.m = { .min = 79, .max = 126 },
335 	.m1 = { .min = 12, .max = 22 },
336 	.m2 = { .min = 5, .max = 9 },
337 	.p = { .min = 14, .max = 42 },
338 	.p1 = { .min = 2, .max = 6 },
339 	.p2 = { .dot_limit = 225000,
340 		.p2_slow = 7, .p2_fast = 7 },
341 	.find_pll = intel_g4x_find_best_PLL,
342 };
343 
344 static const intel_limit_t intel_limits_ironlake_display_port = {
345 	.dot = { .min = 25000, .max = 350000 },
346 	.vco = { .min = 1760000, .max = 3510000},
347 	.n = { .min = 1, .max = 2 },
348 	.m = { .min = 81, .max = 90 },
349 	.m1 = { .min = 12, .max = 22 },
350 	.m2 = { .min = 5, .max = 9 },
351 	.p = { .min = 10, .max = 20 },
352 	.p1 = { .min = 1, .max = 2},
353 	.p2 = { .dot_limit = 0,
354 		.p2_slow = 10, .p2_fast = 10 },
355 	.find_pll = intel_find_pll_ironlake_dp,
356 };
357 
358 static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
359 						int refclk)
360 {
361 	struct drm_device *dev = crtc->dev;
362 	struct drm_i915_private *dev_priv = dev->dev_private;
363 	const intel_limit_t *limit;
364 
365 	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
366 		if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
367 		    LVDS_CLKB_POWER_UP) {
368 			/* LVDS dual channel */
369 			if (refclk == 100000)
370 				limit = &intel_limits_ironlake_dual_lvds_100m;
371 			else
372 				limit = &intel_limits_ironlake_dual_lvds;
373 		} else {
374 			if (refclk == 100000)
375 				limit = &intel_limits_ironlake_single_lvds_100m;
376 			else
377 				limit = &intel_limits_ironlake_single_lvds;
378 		}
379 	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
380 			HAS_eDP)
381 		limit = &intel_limits_ironlake_display_port;
382 	else
383 		limit = &intel_limits_ironlake_dac;
384 
385 	return limit;
386 }
387 
388 static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
389 {
390 	struct drm_device *dev = crtc->dev;
391 	struct drm_i915_private *dev_priv = dev->dev_private;
392 	const intel_limit_t *limit;
393 
394 	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
395 		if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
396 		    LVDS_CLKB_POWER_UP)
397 			/* LVDS with dual channel */
398 			limit = &intel_limits_g4x_dual_channel_lvds;
399 		else
400 			/* LVDS with dual channel */
401 			limit = &intel_limits_g4x_single_channel_lvds;
402 	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
403 		   intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
404 		limit = &intel_limits_g4x_hdmi;
405 	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
406 		limit = &intel_limits_g4x_sdvo;
407 	} else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
408 		limit = &intel_limits_g4x_display_port;
409 	} else /* The option is for other outputs */
410 		limit = &intel_limits_i9xx_sdvo;
411 
412 	return limit;
413 }
414 
415 static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
416 {
417 	struct drm_device *dev = crtc->dev;
418 	const intel_limit_t *limit;
419 
420 	if (HAS_PCH_SPLIT(dev))
421 		limit = intel_ironlake_limit(crtc, refclk);
422 	else if (IS_G4X(dev)) {
423 		limit = intel_g4x_limit(crtc);
424 	} else if (IS_PINEVIEW(dev)) {
425 		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
426 			limit = &intel_limits_pineview_lvds;
427 		else
428 			limit = &intel_limits_pineview_sdvo;
429 	} else if (!IS_GEN2(dev)) {
430 		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
431 			limit = &intel_limits_i9xx_lvds;
432 		else
433 			limit = &intel_limits_i9xx_sdvo;
434 	} else {
435 		if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
436 			limit = &intel_limits_i8xx_lvds;
437 		else
438 			limit = &intel_limits_i8xx_dvo;
439 	}
440 	return limit;
441 }
442 
443 /* m1 is reserved as 0 in Pineview, n is a ring counter */
444 static void pineview_clock(int refclk, intel_clock_t *clock)
445 {
446 	clock->m = clock->m2 + 2;
447 	clock->p = clock->p1 * clock->p2;
448 	clock->vco = refclk * clock->m / clock->n;
449 	clock->dot = clock->vco / clock->p;
450 }
451 
452 static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
453 {
454 	if (IS_PINEVIEW(dev)) {
455 		pineview_clock(refclk, clock);
456 		return;
457 	}
458 	clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
459 	clock->p = clock->p1 * clock->p2;
460 	clock->vco = refclk * clock->m / (clock->n + 2);
461 	clock->dot = clock->vco / clock->p;
462 }
463 
464 /**
465  * Returns whether any output on the specified pipe is of the specified type
466  */
467 bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
468 {
469 	struct drm_device *dev = crtc->dev;
470 	struct drm_mode_config *mode_config = &dev->mode_config;
471 	struct intel_encoder *encoder;
472 
473 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
474 		if (encoder->base.crtc == crtc && encoder->type == type)
475 			return true;
476 
477 	return false;
478 }
479 
480 #define INTELPllInvalid(s)   do { /* DRM_DEBUG(s); */ return false; } while (0)
481 /**
482  * Returns whether the given set of divisors are valid for a given refclk with
483  * the given connectors.
484  */
485 
486 static bool intel_PLL_is_valid(struct drm_device *dev,
487 			       const intel_limit_t *limit,
488 			       const intel_clock_t *clock)
489 {
490 	if (clock->p1  < limit->p1.min  || limit->p1.max  < clock->p1)
491 		INTELPllInvalid("p1 out of range\n");
492 	if (clock->p   < limit->p.min   || limit->p.max   < clock->p)
493 		INTELPllInvalid("p out of range\n");
494 	if (clock->m2  < limit->m2.min  || limit->m2.max  < clock->m2)
495 		INTELPllInvalid("m2 out of range\n");
496 	if (clock->m1  < limit->m1.min  || limit->m1.max  < clock->m1)
497 		INTELPllInvalid("m1 out of range\n");
498 	if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
499 		INTELPllInvalid("m1 <= m2\n");
500 	if (clock->m   < limit->m.min   || limit->m.max   < clock->m)
501 		INTELPllInvalid("m out of range\n");
502 	if (clock->n   < limit->n.min   || limit->n.max   < clock->n)
503 		INTELPllInvalid("n out of range\n");
504 	if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
505 		INTELPllInvalid("vco out of range\n");
506 	/* XXX: We may need to be checking "Dot clock" depending on the multiplier,
507 	 * connector, etc., rather than just a single range.
508 	 */
509 	if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
510 		INTELPllInvalid("dot out of range\n");
511 
512 	return true;
513 }
514 
515 static bool
516 intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
517 		    int target, int refclk, intel_clock_t *match_clock,
518 		    intel_clock_t *best_clock)
519 
520 {
521 	struct drm_device *dev = crtc->dev;
522 	struct drm_i915_private *dev_priv = dev->dev_private;
523 	intel_clock_t clock;
524 	int err = target;
525 
526 	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
527 	    (I915_READ(LVDS)) != 0) {
528 		/*
529 		 * For LVDS, if the panel is on, just rely on its current
530 		 * settings for dual-channel.  We haven't figured out how to
531 		 * reliably set up different single/dual channel state, if we
532 		 * even can.
533 		 */
534 		if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
535 		    LVDS_CLKB_POWER_UP)
536 			clock.p2 = limit->p2.p2_fast;
537 		else
538 			clock.p2 = limit->p2.p2_slow;
539 	} else {
540 		if (target < limit->p2.dot_limit)
541 			clock.p2 = limit->p2.p2_slow;
542 		else
543 			clock.p2 = limit->p2.p2_fast;
544 	}
545 
546 	memset(best_clock, 0, sizeof(*best_clock));
547 
548 	for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
549 	     clock.m1++) {
550 		for (clock.m2 = limit->m2.min;
551 		     clock.m2 <= limit->m2.max; clock.m2++) {
552 			/* m1 is always 0 in Pineview */
553 			if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
554 				break;
555 			for (clock.n = limit->n.min;
556 			     clock.n <= limit->n.max; clock.n++) {
557 				for (clock.p1 = limit->p1.min;
558 					clock.p1 <= limit->p1.max; clock.p1++) {
559 					int this_err;
560 
561 					intel_clock(dev, refclk, &clock);
562 					if (!intel_PLL_is_valid(dev, limit,
563 								&clock))
564 						continue;
565 					if (match_clock &&
566 					    clock.p != match_clock->p)
567 						continue;
568 
569 					this_err = abs(clock.dot - target);
570 					if (this_err < err) {
571 						*best_clock = clock;
572 						err = this_err;
573 					}
574 				}
575 			}
576 		}
577 	}
578 
579 	return (err != target);
580 }
581 
582 static bool
583 intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
584 			int target, int refclk, intel_clock_t *match_clock,
585 			intel_clock_t *best_clock)
586 {
587 	struct drm_device *dev = crtc->dev;
588 	struct drm_i915_private *dev_priv = dev->dev_private;
589 	intel_clock_t clock;
590 	int max_n;
591 	bool found;
592 	/* approximately equals target * 0.00585 */
593 	int err_most = (target >> 8) + (target >> 9);
594 	found = false;
595 
596 	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
597 		int lvds_reg;
598 
599 		if (HAS_PCH_SPLIT(dev))
600 			lvds_reg = PCH_LVDS;
601 		else
602 			lvds_reg = LVDS;
603 		if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
604 		    LVDS_CLKB_POWER_UP)
605 			clock.p2 = limit->p2.p2_fast;
606 		else
607 			clock.p2 = limit->p2.p2_slow;
608 	} else {
609 		if (target < limit->p2.dot_limit)
610 			clock.p2 = limit->p2.p2_slow;
611 		else
612 			clock.p2 = limit->p2.p2_fast;
613 	}
614 
615 	memset(best_clock, 0, sizeof(*best_clock));
616 	max_n = limit->n.max;
617 	/* based on hardware requirement, prefer smaller n to precision */
618 	for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
619 		/* based on hardware requirement, prefere larger m1,m2 */
620 		for (clock.m1 = limit->m1.max;
621 		     clock.m1 >= limit->m1.min; clock.m1--) {
622 			for (clock.m2 = limit->m2.max;
623 			     clock.m2 >= limit->m2.min; clock.m2--) {
624 				for (clock.p1 = limit->p1.max;
625 				     clock.p1 >= limit->p1.min; clock.p1--) {
626 					int this_err;
627 
628 					intel_clock(dev, refclk, &clock);
629 					if (!intel_PLL_is_valid(dev, limit,
630 								&clock))
631 						continue;
632 					if (match_clock &&
633 					    clock.p != match_clock->p)
634 						continue;
635 
636 					this_err = abs(clock.dot - target);
637 					if (this_err < err_most) {
638 						*best_clock = clock;
639 						err_most = this_err;
640 						max_n = clock.n;
641 						found = true;
642 					}
643 				}
644 			}
645 		}
646 	}
647 	return found;
648 }
649 
650 static bool
651 intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
652 			   int target, int refclk, intel_clock_t *match_clock,
653 			   intel_clock_t *best_clock)
654 {
655 	struct drm_device *dev = crtc->dev;
656 	intel_clock_t clock;
657 
658 	if (target < 200000) {
659 		clock.n = 1;
660 		clock.p1 = 2;
661 		clock.p2 = 10;
662 		clock.m1 = 12;
663 		clock.m2 = 9;
664 	} else {
665 		clock.n = 2;
666 		clock.p1 = 1;
667 		clock.p2 = 10;
668 		clock.m1 = 14;
669 		clock.m2 = 8;
670 	}
671 	intel_clock(dev, refclk, &clock);
672 	memcpy(best_clock, &clock, sizeof(intel_clock_t));
673 	return true;
674 }
675 
676 /* DisplayPort has only two frequencies, 162MHz and 270MHz */
677 static bool
678 intel_find_pll_g4x_dp(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)
681 {
682 	intel_clock_t clock;
683 	if (target < 200000) {
684 		clock.p1 = 2;
685 		clock.p2 = 10;
686 		clock.n = 2;
687 		clock.m1 = 23;
688 		clock.m2 = 8;
689 	} else {
690 		clock.p1 = 1;
691 		clock.p2 = 10;
692 		clock.n = 1;
693 		clock.m1 = 14;
694 		clock.m2 = 2;
695 	}
696 	clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
697 	clock.p = (clock.p1 * clock.p2);
698 	clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
699 	clock.vco = 0;
700 	memcpy(best_clock, &clock, sizeof(intel_clock_t));
701 	return true;
702 }
703 
704 /**
705  * intel_wait_for_vblank - wait for vblank on a given pipe
706  * @dev: drm device
707  * @pipe: pipe to wait for
708  *
709  * Wait for vblank to occur on a given pipe.  Needed for various bits of
710  * mode setting code.
711  */
712 void intel_wait_for_vblank(struct drm_device *dev, int pipe)
713 {
714 	struct drm_i915_private *dev_priv = dev->dev_private;
715 	int pipestat_reg = PIPESTAT(pipe);
716 
717 	/* Clear existing vblank status. Note this will clear any other
718 	 * sticky status fields as well.
719 	 *
720 	 * This races with i915_driver_irq_handler() with the result
721 	 * that either function could miss a vblank event.  Here it is not
722 	 * fatal, as we will either wait upon the next vblank interrupt or
723 	 * timeout.  Generally speaking intel_wait_for_vblank() is only
724 	 * called during modeset at which time the GPU should be idle and
725 	 * should *not* be performing page flips and thus not waiting on
726 	 * vblanks...
727 	 * Currently, the result of us stealing a vblank from the irq
728 	 * handler is that a single frame will be skipped during swapbuffers.
729 	 */
730 	I915_WRITE(pipestat_reg,
731 		   I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
732 
733 	/* Wait for vblank interrupt bit to set */
734 	if (_intel_wait_for(dev,
735 	    I915_READ(pipestat_reg) & PIPE_VBLANK_INTERRUPT_STATUS,
736 	    50, 1, "915vbl"))
737 		DRM_DEBUG_KMS("vblank wait timed out\n");
738 }
739 
740 /*
741  * intel_wait_for_pipe_off - wait for pipe to turn off
742  * @dev: drm device
743  * @pipe: pipe to wait for
744  *
745  * After disabling a pipe, we can't wait for vblank in the usual way,
746  * spinning on the vblank interrupt status bit, since we won't actually
747  * see an interrupt when the pipe is disabled.
748  *
749  * On Gen4 and above:
750  *   wait for the pipe register state bit to turn off
751  *
752  * Otherwise:
753  *   wait for the display line value to settle (it usually
754  *   ends up stopping at the start of the next frame).
755  *
756  */
757 void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
758 {
759 	struct drm_i915_private *dev_priv = dev->dev_private;
760 
761 	if (INTEL_INFO(dev)->gen >= 4) {
762 		int reg = PIPECONF(pipe);
763 
764 		/* Wait for the Pipe State to go off */
765 		if (_intel_wait_for(dev,
766 		    (I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0, 100,
767 		    1, "915pip"))
768 			DRM_DEBUG_KMS("pipe_off wait timed out\n");
769 	} else {
770 		u32 last_line, line_mask;
771 		int reg = PIPEDSL(pipe);
772 		unsigned long timeout = jiffies + msecs_to_jiffies(100);
773 
774 		if (IS_GEN2(dev))
775 			line_mask = DSL_LINEMASK_GEN2;
776 		else
777 			line_mask = DSL_LINEMASK_GEN3;
778 
779 		/* Wait for the display line to settle */
780 		do {
781 			last_line = I915_READ(reg) & line_mask;
782 			DELAY(5000);
783 		} while (((I915_READ(reg) & line_mask) != last_line) &&
784 			 time_after(timeout, jiffies));
785 		if (time_after(jiffies, timeout))
786 			DRM_DEBUG_KMS("pipe_off wait timed out\n");
787 	}
788 }
789 
790 static const char *state_string(bool enabled)
791 {
792 	return enabled ? "on" : "off";
793 }
794 
795 /* Only for pre-ILK configs */
796 static void assert_pll(struct drm_i915_private *dev_priv,
797 		       enum i915_pipe pipe, bool state)
798 {
799 	int reg;
800 	u32 val;
801 	bool cur_state;
802 
803 	reg = DPLL(pipe);
804 	val = I915_READ(reg);
805 	cur_state = !!(val & DPLL_VCO_ENABLE);
806 	if (cur_state != state)
807 		kprintf("PLL state assertion failure (expected %s, current %s)\n",
808 		    state_string(state), state_string(cur_state));
809 }
810 #define assert_pll_enabled(d, p) assert_pll(d, p, true)
811 #define assert_pll_disabled(d, p) assert_pll(d, p, false)
812 
813 /* For ILK+ */
814 static void assert_pch_pll(struct drm_i915_private *dev_priv,
815 			   enum i915_pipe pipe, bool state)
816 {
817 	int reg;
818 	u32 val;
819 	bool cur_state;
820 
821 	if (HAS_PCH_CPT(dev_priv->dev)) {
822 		u32 pch_dpll;
823 
824 		pch_dpll = I915_READ(PCH_DPLL_SEL);
825 
826 		/* Make sure the selected PLL is enabled to the transcoder */
827 		KASSERT(((pch_dpll >> (4 * pipe)) & 8) != 0,
828 		    ("transcoder %d PLL not enabled\n", pipe));
829 
830 		/* Convert the transcoder pipe number to a pll pipe number */
831 		pipe = (pch_dpll >> (4 * pipe)) & 1;
832 	}
833 
834 	reg = _PCH_DPLL(pipe);
835 	val = I915_READ(reg);
836 	cur_state = !!(val & DPLL_VCO_ENABLE);
837 	if (cur_state != state)
838 		kprintf("PCH PLL state assertion failure (expected %s, current %s)\n",
839 		    state_string(state), state_string(cur_state));
840 }
841 #define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
842 #define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
843 
844 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
845 			  enum i915_pipe pipe, bool state)
846 {
847 	int reg;
848 	u32 val;
849 	bool cur_state;
850 
851 	reg = FDI_TX_CTL(pipe);
852 	val = I915_READ(reg);
853 	cur_state = !!(val & FDI_TX_ENABLE);
854 	if (cur_state != state)
855 		kprintf("FDI TX state assertion failure (expected %s, current %s)\n",
856 		    state_string(state), state_string(cur_state));
857 }
858 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
859 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
860 
861 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
862 			  enum i915_pipe pipe, bool state)
863 {
864 	int reg;
865 	u32 val;
866 	bool cur_state;
867 
868 	reg = FDI_RX_CTL(pipe);
869 	val = I915_READ(reg);
870 	cur_state = !!(val & FDI_RX_ENABLE);
871 	if (cur_state != state)
872 		kprintf("FDI RX state assertion failure (expected %s, current %s)\n",
873 		    state_string(state), state_string(cur_state));
874 }
875 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
876 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
877 
878 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
879 				      enum i915_pipe pipe)
880 {
881 	int reg;
882 	u32 val;
883 
884 	/* ILK FDI PLL is always enabled */
885 	if (dev_priv->info->gen == 5)
886 		return;
887 
888 	reg = FDI_TX_CTL(pipe);
889 	val = I915_READ(reg);
890 	if (!(val & FDI_TX_PLL_ENABLE))
891 		kprintf("FDI TX PLL assertion failure, should be active but is disabled\n");
892 }
893 
894 static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
895 				      enum i915_pipe pipe)
896 {
897 	int reg;
898 	u32 val;
899 
900 	reg = FDI_RX_CTL(pipe);
901 	val = I915_READ(reg);
902 	if (!(val & FDI_RX_PLL_ENABLE))
903 		kprintf("FDI RX PLL assertion failure, should be active but is disabled\n");
904 }
905 
906 static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
907 				  enum i915_pipe pipe)
908 {
909 	int pp_reg, lvds_reg;
910 	u32 val;
911 	enum i915_pipe panel_pipe = PIPE_A;
912 	bool locked = true;
913 
914 	if (HAS_PCH_SPLIT(dev_priv->dev)) {
915 		pp_reg = PCH_PP_CONTROL;
916 		lvds_reg = PCH_LVDS;
917 	} else {
918 		pp_reg = PP_CONTROL;
919 		lvds_reg = LVDS;
920 	}
921 
922 	val = I915_READ(pp_reg);
923 	if (!(val & PANEL_POWER_ON) ||
924 	    ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
925 		locked = false;
926 
927 	if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
928 		panel_pipe = PIPE_B;
929 
930 	if (panel_pipe == pipe && locked)
931 		kprintf("panel assertion failure, pipe %c regs locked\n",
932 	     pipe_name(pipe));
933 }
934 
935 void assert_pipe(struct drm_i915_private *dev_priv,
936 		 enum i915_pipe pipe, bool state)
937 {
938 	int reg;
939 	u32 val;
940 	bool cur_state;
941 
942 	/* if we need the pipe A quirk it must be always on */
943 	if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
944 		state = true;
945 
946 	reg = PIPECONF(pipe);
947 	val = I915_READ(reg);
948 	cur_state = !!(val & PIPECONF_ENABLE);
949 	if (cur_state != state)
950 		kprintf("pipe %c assertion failure (expected %s, current %s)\n",
951 		    pipe_name(pipe), state_string(state), state_string(cur_state));
952 }
953 
954 static void assert_plane(struct drm_i915_private *dev_priv,
955 			 enum plane plane, bool state)
956 {
957 	int reg;
958 	u32 val;
959 	bool cur_state;
960 
961 	reg = DSPCNTR(plane);
962 	val = I915_READ(reg);
963 	cur_state = !!(val & DISPLAY_PLANE_ENABLE);
964 	if (cur_state != state)
965 		kprintf("plane %c assertion failure, (expected %s, current %s)\n",
966 		       plane_name(plane), state_string(state), state_string(cur_state));
967 }
968 
969 #define assert_plane_enabled(d, p) assert_plane(d, p, true)
970 #define assert_plane_disabled(d, p) assert_plane(d, p, false)
971 
972 static void assert_planes_disabled(struct drm_i915_private *dev_priv,
973 				   enum i915_pipe pipe)
974 {
975 	int reg, i;
976 	u32 val;
977 	int cur_pipe;
978 
979 	/* Planes are fixed to pipes on ILK+ */
980 	if (HAS_PCH_SPLIT(dev_priv->dev)) {
981 		reg = DSPCNTR(pipe);
982 		val = I915_READ(reg);
983 		if ((val & DISPLAY_PLANE_ENABLE) != 0)
984 			kprintf("plane %c assertion failure, should be disabled but not\n",
985 			       plane_name(pipe));
986 		return;
987 	}
988 
989 	/* Need to check both planes against the pipe */
990 	for (i = 0; i < 2; i++) {
991 		reg = DSPCNTR(i);
992 		val = I915_READ(reg);
993 		cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
994 			DISPPLANE_SEL_PIPE_SHIFT;
995 		if ((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe)
996 			kprintf("plane %c assertion failure, should be off on pipe %c but is still active\n",
997 		     plane_name(i), pipe_name(pipe));
998 	}
999 }
1000 
1001 static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1002 {
1003 	u32 val;
1004 	bool enabled;
1005 
1006 	val = I915_READ(PCH_DREF_CONTROL);
1007 	enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1008 			    DREF_SUPERSPREAD_SOURCE_MASK));
1009 	if (!enabled)
1010 		kprintf("PCH refclk assertion failure, should be active but is disabled\n");
1011 }
1012 
1013 static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1014 				       enum i915_pipe pipe)
1015 {
1016 	int reg;
1017 	u32 val;
1018 	bool enabled;
1019 
1020 	reg = TRANSCONF(pipe);
1021 	val = I915_READ(reg);
1022 	enabled = !!(val & TRANS_ENABLE);
1023 	if (enabled)
1024 		kprintf("transcoder assertion failed, should be off on pipe %c but is still active\n",
1025 	     pipe_name(pipe));
1026 }
1027 
1028 static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1029 			      enum i915_pipe pipe, u32 val)
1030 {
1031 	if ((val & PORT_ENABLE) == 0)
1032 		return false;
1033 
1034 	if (HAS_PCH_CPT(dev_priv->dev)) {
1035 		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1036 			return false;
1037 	} else {
1038 		if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1039 			return false;
1040 	}
1041 	return true;
1042 }
1043 
1044 static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1045 			      enum i915_pipe pipe, u32 val)
1046 {
1047 	if ((val & LVDS_PORT_EN) == 0)
1048 		return false;
1049 
1050 	if (HAS_PCH_CPT(dev_priv->dev)) {
1051 		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1052 			return false;
1053 	} else {
1054 		if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1055 			return false;
1056 	}
1057 	return true;
1058 }
1059 
1060 static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1061 			      enum i915_pipe pipe, u32 val)
1062 {
1063 	if ((val & ADPA_DAC_ENABLE) == 0)
1064 		return false;
1065 	if (HAS_PCH_CPT(dev_priv->dev)) {
1066 		if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1067 			return false;
1068 	} else {
1069 		if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1070 			return false;
1071 	}
1072 	return true;
1073 }
1074 
1075 static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1076 			    enum i915_pipe pipe, u32 port_sel, u32 val)
1077 {
1078 	if ((val & DP_PORT_EN) == 0)
1079 		return false;
1080 
1081 	if (HAS_PCH_CPT(dev_priv->dev)) {
1082 		u32	trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1083 		u32	trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1084 		if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1085 			return false;
1086 	} else {
1087 		if ((val & DP_PIPE_MASK) != (pipe << 30))
1088 			return false;
1089 	}
1090 	return true;
1091 }
1092 
1093 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
1094 				   enum i915_pipe pipe, int reg, u32 port_sel)
1095 {
1096 	u32 val = I915_READ(reg);
1097 	if (dp_pipe_enabled(dev_priv, pipe, port_sel, val))
1098 		kprintf("PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
1099 	     reg, pipe_name(pipe));
1100 }
1101 
1102 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1103 				     enum i915_pipe pipe, int reg)
1104 {
1105 	u32 val = I915_READ(reg);
1106 	if (hdmi_pipe_enabled(dev_priv, val, pipe))
1107 		kprintf("PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
1108 	     reg, pipe_name(pipe));
1109 }
1110 
1111 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1112 				      enum i915_pipe pipe)
1113 {
1114 	int reg;
1115 	u32 val;
1116 
1117 	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1118 	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1119 	assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1120 
1121 	reg = PCH_ADPA;
1122 	val = I915_READ(reg);
1123 	if (adpa_pipe_enabled(dev_priv, val, pipe))
1124 		kprintf("PCH VGA enabled on transcoder %c, should be disabled\n",
1125 	     pipe_name(pipe));
1126 
1127 	reg = PCH_LVDS;
1128 	val = I915_READ(reg);
1129 	if (lvds_pipe_enabled(dev_priv, val, pipe))
1130 		kprintf("PCH LVDS enabled on transcoder %c, should be disabled\n",
1131 	     pipe_name(pipe));
1132 
1133 	assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1134 	assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1135 	assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1136 }
1137 
1138 /**
1139  * intel_enable_pll - enable a PLL
1140  * @dev_priv: i915 private structure
1141  * @pipe: pipe PLL to enable
1142  *
1143  * Enable @pipe's PLL so we can start pumping pixels from a plane.  Check to
1144  * make sure the PLL reg is writable first though, since the panel write
1145  * protect mechanism may be enabled.
1146  *
1147  * Note!  This is for pre-ILK only.
1148  */
1149 static void intel_enable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1150 {
1151 	int reg;
1152 	u32 val;
1153 
1154 	/* No really, not for ILK+ */
1155 	KASSERT(dev_priv->info->gen < 5, ("Wrong device gen"));
1156 
1157 	/* PLL is protected by panel, make sure we can write it */
1158 	if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1159 		assert_panel_unlocked(dev_priv, pipe);
1160 
1161 	reg = DPLL(pipe);
1162 	val = I915_READ(reg);
1163 	val |= DPLL_VCO_ENABLE;
1164 
1165 	/* We do this three times for luck */
1166 	I915_WRITE(reg, val);
1167 	POSTING_READ(reg);
1168 	DELAY(150); /* wait for warmup */
1169 	I915_WRITE(reg, val);
1170 	POSTING_READ(reg);
1171 	DELAY(150); /* wait for warmup */
1172 	I915_WRITE(reg, val);
1173 	POSTING_READ(reg);
1174 	DELAY(150); /* wait for warmup */
1175 }
1176 
1177 /**
1178  * intel_disable_pll - disable a PLL
1179  * @dev_priv: i915 private structure
1180  * @pipe: pipe PLL to disable
1181  *
1182  * Disable the PLL for @pipe, making sure the pipe is off first.
1183  *
1184  * Note!  This is for pre-ILK only.
1185  */
1186 static void intel_disable_pll(struct drm_i915_private *dev_priv, enum i915_pipe pipe)
1187 {
1188 	int reg;
1189 	u32 val;
1190 
1191 	/* Don't disable pipe A or pipe A PLLs if needed */
1192 	if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1193 		return;
1194 
1195 	/* Make sure the pipe isn't still relying on us */
1196 	assert_pipe_disabled(dev_priv, pipe);
1197 
1198 	reg = DPLL(pipe);
1199 	val = I915_READ(reg);
1200 	val &= ~DPLL_VCO_ENABLE;
1201 	I915_WRITE(reg, val);
1202 	POSTING_READ(reg);
1203 }
1204 
1205 /**
1206  * intel_enable_pch_pll - enable PCH PLL
1207  * @dev_priv: i915 private structure
1208  * @pipe: pipe PLL to enable
1209  *
1210  * The PCH PLL needs to be enabled before the PCH transcoder, since it
1211  * drives the transcoder clock.
1212  */
1213 static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
1214 				 enum i915_pipe pipe)
1215 {
1216 	int reg;
1217 	u32 val;
1218 
1219 	if (pipe > 1)
1220 		return;
1221 
1222 	/* PCH only available on ILK+ */
1223 	KASSERT(dev_priv->info->gen >= 5, ("Wrong device gen"));
1224 
1225 	/* PCH refclock must be enabled first */
1226 	assert_pch_refclk_enabled(dev_priv);
1227 
1228 	reg = _PCH_DPLL(pipe);
1229 	val = I915_READ(reg);
1230 	val |= DPLL_VCO_ENABLE;
1231 	I915_WRITE(reg, val);
1232 	POSTING_READ(reg);
1233 	DELAY(200);
1234 }
1235 
1236 static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
1237 				  enum i915_pipe pipe)
1238 {
1239 	int reg;
1240 	u32 val, pll_mask = TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL,
1241 		pll_sel = TRANSC_DPLL_ENABLE;
1242 
1243 	if (pipe > 1)
1244 		return;
1245 
1246 	/* PCH only available on ILK+ */
1247 	KASSERT(dev_priv->info->gen >= 5, ("Wrong device gen"));
1248 
1249 	/* Make sure transcoder isn't still depending on us */
1250 	assert_transcoder_disabled(dev_priv, pipe);
1251 
1252 	if (pipe == 0)
1253 		pll_sel |= TRANSC_DPLLA_SEL;
1254 	else if (pipe == 1)
1255 		pll_sel |= TRANSC_DPLLB_SEL;
1256 
1257 
1258 	if ((I915_READ(PCH_DPLL_SEL) & pll_mask) == pll_sel)
1259 		return;
1260 
1261 	reg = _PCH_DPLL(pipe);
1262 	val = I915_READ(reg);
1263 	val &= ~DPLL_VCO_ENABLE;
1264 	I915_WRITE(reg, val);
1265 	POSTING_READ(reg);
1266 	DELAY(200);
1267 }
1268 
1269 static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
1270 				    enum i915_pipe pipe)
1271 {
1272 	int reg;
1273 	u32 val, pipeconf_val;
1274 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1275 
1276 	/* PCH only available on ILK+ */
1277 	KASSERT(dev_priv->info->gen >= 5, ("Wrong device gen"));
1278 
1279 	/* Make sure PCH DPLL is enabled */
1280 	assert_pch_pll_enabled(dev_priv, pipe);
1281 
1282 	/* FDI must be feeding us bits for PCH ports */
1283 	assert_fdi_tx_enabled(dev_priv, pipe);
1284 	assert_fdi_rx_enabled(dev_priv, pipe);
1285 
1286 
1287 	reg = TRANSCONF(pipe);
1288 	val = I915_READ(reg);
1289 	pipeconf_val = I915_READ(PIPECONF(pipe));
1290 
1291 	if (HAS_PCH_IBX(dev_priv->dev)) {
1292 		/*
1293 		 * make the BPC in transcoder be consistent with
1294 		 * that in pipeconf reg.
1295 		 */
1296 		val &= ~PIPE_BPC_MASK;
1297 		val |= pipeconf_val & PIPE_BPC_MASK;
1298 	}
1299 
1300 	val &= ~TRANS_INTERLACE_MASK;
1301 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
1302 		if (HAS_PCH_IBX(dev_priv->dev) &&
1303 		    intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1304 			val |= TRANS_LEGACY_INTERLACED_ILK;
1305 		else
1306 			val |= TRANS_INTERLACED;
1307 	else
1308 		val |= TRANS_PROGRESSIVE;
1309 
1310 	I915_WRITE(reg, val | TRANS_ENABLE);
1311 	if (_intel_wait_for(dev_priv->dev, I915_READ(reg) & TRANS_STATE_ENABLE,
1312 	    100, 1, "915trc"))
1313 		DRM_ERROR("failed to enable transcoder %d\n", pipe);
1314 }
1315 
1316 static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
1317 				     enum i915_pipe pipe)
1318 {
1319 	int reg;
1320 	u32 val;
1321 
1322 	/* FDI relies on the transcoder */
1323 	assert_fdi_tx_disabled(dev_priv, pipe);
1324 	assert_fdi_rx_disabled(dev_priv, pipe);
1325 
1326 	/* Ports must be off as well */
1327 	assert_pch_ports_disabled(dev_priv, pipe);
1328 
1329 	reg = TRANSCONF(pipe);
1330 	val = I915_READ(reg);
1331 	val &= ~TRANS_ENABLE;
1332 	I915_WRITE(reg, val);
1333 	/* wait for PCH transcoder off, transcoder state */
1334 	if (_intel_wait_for(dev_priv->dev,
1335 	    (I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50,
1336 	    1, "915trd"))
1337 		DRM_ERROR("failed to disable transcoder %d\n", pipe);
1338 }
1339 
1340 /**
1341  * intel_enable_pipe - enable a pipe, asserting requirements
1342  * @dev_priv: i915 private structure
1343  * @pipe: pipe to enable
1344  * @pch_port: on ILK+, is this pipe driving a PCH port or not
1345  *
1346  * Enable @pipe, making sure that various hardware specific requirements
1347  * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1348  *
1349  * @pipe should be %PIPE_A or %PIPE_B.
1350  *
1351  * Will wait until the pipe is actually running (i.e. first vblank) before
1352  * returning.
1353  */
1354 static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum i915_pipe pipe,
1355 			      bool pch_port)
1356 {
1357 	int reg;
1358 	u32 val;
1359 
1360 	/*
1361 	 * A pipe without a PLL won't actually be able to drive bits from
1362 	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
1363 	 * need the check.
1364 	 */
1365 	if (!HAS_PCH_SPLIT(dev_priv->dev))
1366 		assert_pll_enabled(dev_priv, pipe);
1367 	else {
1368 		if (pch_port) {
1369 			/* if driving the PCH, we need FDI enabled */
1370 			assert_fdi_rx_pll_enabled(dev_priv, pipe);
1371 			assert_fdi_tx_pll_enabled(dev_priv, pipe);
1372 		}
1373 		/* FIXME: assert CPU port conditions for SNB+ */
1374 	}
1375 
1376 	reg = PIPECONF(pipe);
1377 	val = I915_READ(reg);
1378 	if (val & PIPECONF_ENABLE)
1379 		return;
1380 
1381 	I915_WRITE(reg, val | PIPECONF_ENABLE);
1382 	intel_wait_for_vblank(dev_priv->dev, pipe);
1383 }
1384 
1385 /**
1386  * intel_disable_pipe - disable a pipe, asserting requirements
1387  * @dev_priv: i915 private structure
1388  * @pipe: pipe to disable
1389  *
1390  * Disable @pipe, making sure that various hardware specific requirements
1391  * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1392  *
1393  * @pipe should be %PIPE_A or %PIPE_B.
1394  *
1395  * Will wait until the pipe has shut down before returning.
1396  */
1397 static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1398 			       enum i915_pipe pipe)
1399 {
1400 	int reg;
1401 	u32 val;
1402 
1403 	/*
1404 	 * Make sure planes won't keep trying to pump pixels to us,
1405 	 * or we might hang the display.
1406 	 */
1407 	assert_planes_disabled(dev_priv, pipe);
1408 
1409 	/* Don't disable pipe A or pipe A PLLs if needed */
1410 	if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1411 		return;
1412 
1413 	reg = PIPECONF(pipe);
1414 	val = I915_READ(reg);
1415 	if ((val & PIPECONF_ENABLE) == 0)
1416 		return;
1417 
1418 	I915_WRITE(reg, val & ~PIPECONF_ENABLE);
1419 	intel_wait_for_pipe_off(dev_priv->dev, pipe);
1420 }
1421 
1422 /*
1423  * Plane regs are double buffered, going from enabled->disabled needs a
1424  * trigger in order to latch.  The display address reg provides this.
1425  */
1426 void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1427 				      enum plane plane)
1428 {
1429 	I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1430 	I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1431 }
1432 
1433 /**
1434  * intel_enable_plane - enable a display plane on a given pipe
1435  * @dev_priv: i915 private structure
1436  * @plane: plane to enable
1437  * @pipe: pipe being fed
1438  *
1439  * Enable @plane on @pipe, making sure that @pipe is running first.
1440  */
1441 static void intel_enable_plane(struct drm_i915_private *dev_priv,
1442 			       enum plane plane, enum i915_pipe pipe)
1443 {
1444 	int reg;
1445 	u32 val;
1446 
1447 	/* If the pipe isn't enabled, we can't pump pixels and may hang */
1448 	assert_pipe_enabled(dev_priv, pipe);
1449 
1450 	reg = DSPCNTR(plane);
1451 	val = I915_READ(reg);
1452 	if (val & DISPLAY_PLANE_ENABLE)
1453 		return;
1454 
1455 	I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
1456 	intel_flush_display_plane(dev_priv, plane);
1457 	intel_wait_for_vblank(dev_priv->dev, pipe);
1458 }
1459 
1460 /**
1461  * intel_disable_plane - disable a display plane
1462  * @dev_priv: i915 private structure
1463  * @plane: plane to disable
1464  * @pipe: pipe consuming the data
1465  *
1466  * Disable @plane; should be an independent operation.
1467  */
1468 static void intel_disable_plane(struct drm_i915_private *dev_priv,
1469 				enum plane plane, enum i915_pipe pipe)
1470 {
1471 	int reg;
1472 	u32 val;
1473 
1474 	reg = DSPCNTR(plane);
1475 	val = I915_READ(reg);
1476 	if ((val & DISPLAY_PLANE_ENABLE) == 0)
1477 		return;
1478 
1479 	I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
1480 	intel_flush_display_plane(dev_priv, plane);
1481 	intel_wait_for_vblank(dev_priv->dev, pipe);
1482 }
1483 
1484 static void disable_pch_dp(struct drm_i915_private *dev_priv,
1485 			   enum i915_pipe pipe, int reg, u32 port_sel)
1486 {
1487 	u32 val = I915_READ(reg);
1488 	if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
1489 		DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
1490 		I915_WRITE(reg, val & ~DP_PORT_EN);
1491 	}
1492 }
1493 
1494 static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
1495 			     enum i915_pipe pipe, int reg)
1496 {
1497 	u32 val = I915_READ(reg);
1498 	if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
1499 		DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
1500 			      reg, pipe);
1501 		I915_WRITE(reg, val & ~PORT_ENABLE);
1502 	}
1503 }
1504 
1505 /* Disable any ports connected to this transcoder */
1506 static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
1507 				    enum i915_pipe pipe)
1508 {
1509 	u32 reg, val;
1510 
1511 	val = I915_READ(PCH_PP_CONTROL);
1512 	I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
1513 
1514 	disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1515 	disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1516 	disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
1517 
1518 	reg = PCH_ADPA;
1519 	val = I915_READ(reg);
1520 	if (adpa_pipe_enabled(dev_priv, val, pipe))
1521 		I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
1522 
1523 	reg = PCH_LVDS;
1524 	val = I915_READ(reg);
1525 	if (lvds_pipe_enabled(dev_priv, val, pipe)) {
1526 		DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
1527 		I915_WRITE(reg, val & ~LVDS_PORT_EN);
1528 		POSTING_READ(reg);
1529 		DELAY(100);
1530 	}
1531 
1532 	disable_pch_hdmi(dev_priv, pipe, HDMIB);
1533 	disable_pch_hdmi(dev_priv, pipe, HDMIC);
1534 	disable_pch_hdmi(dev_priv, pipe, HDMID);
1535 }
1536 
1537 int
1538 intel_pin_and_fence_fb_obj(struct drm_device *dev,
1539 			   struct drm_i915_gem_object *obj,
1540 			   struct intel_ring_buffer *pipelined)
1541 {
1542 	struct drm_i915_private *dev_priv = dev->dev_private;
1543 	u32 alignment;
1544 	int ret;
1545 
1546 	alignment = 0; /* shut gcc */
1547 	switch (obj->tiling_mode) {
1548 	case I915_TILING_NONE:
1549 		if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1550 			alignment = 128 * 1024;
1551 		else if (INTEL_INFO(dev)->gen >= 4)
1552 			alignment = 4 * 1024;
1553 		else
1554 			alignment = 64 * 1024;
1555 		break;
1556 	case I915_TILING_X:
1557 		/* pin() will align the object as required by fence */
1558 		alignment = 0;
1559 		break;
1560 	case I915_TILING_Y:
1561 		/* FIXME: Is this true? */
1562 		DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1563 		return -EINVAL;
1564 	default:
1565 		KASSERT(0, ("Wrong tiling for fb obj"));
1566 	}
1567 
1568 	dev_priv->mm.interruptible = false;
1569 	ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
1570 	if (ret)
1571 		goto err_interruptible;
1572 
1573 	/* Install a fence for tiled scan-out. Pre-i965 always needs a
1574 	 * fence, whereas 965+ only requires a fence if using
1575 	 * framebuffer compression.  For simplicity, we always install
1576 	 * a fence as the cost is not that onerous.
1577 	 */
1578 	if (obj->tiling_mode != I915_TILING_NONE) {
1579 		ret = i915_gem_object_get_fence(obj, pipelined);
1580 		if (ret)
1581 			goto err_unpin;
1582 
1583 		i915_gem_object_pin_fence(obj);
1584 	}
1585 
1586 	dev_priv->mm.interruptible = true;
1587 	return 0;
1588 
1589 err_unpin:
1590 	i915_gem_object_unpin(obj);
1591 err_interruptible:
1592 	dev_priv->mm.interruptible = true;
1593 	return ret;
1594 }
1595 
1596 void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1597 {
1598 	i915_gem_object_unpin_fence(obj);
1599 	i915_gem_object_unpin(obj);
1600 }
1601 
1602 /* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
1603  * is assumed to be a power-of-two. */
1604 unsigned long intel_gen4_compute_offset_xtiled(int *x, int *y,
1605 					       unsigned int bpp,
1606 					       unsigned int pitch)
1607 {
1608 	int tile_rows, tiles;
1609 
1610 	tile_rows = *y / 8;
1611 	*y %= 8;
1612 	tiles = *x / (512/bpp);
1613 	*x %= 512/bpp;
1614 
1615 	return tile_rows * pitch * 8 + tiles * 4096;
1616 }
1617 
1618 static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1619 			     int x, int y)
1620 {
1621 	struct drm_device *dev = crtc->dev;
1622 	struct drm_i915_private *dev_priv = dev->dev_private;
1623 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1624 	struct intel_framebuffer *intel_fb;
1625 	struct drm_i915_gem_object *obj;
1626 	int plane = intel_crtc->plane;
1627 	unsigned long Start, Offset;
1628 	u32 dspcntr;
1629 	u32 reg;
1630 
1631 	switch (plane) {
1632 	case 0:
1633 	case 1:
1634 		break;
1635 	default:
1636 		DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1637 		return -EINVAL;
1638 	}
1639 
1640 	intel_fb = to_intel_framebuffer(fb);
1641 	obj = intel_fb->obj;
1642 
1643 	reg = DSPCNTR(plane);
1644 	dspcntr = I915_READ(reg);
1645 	/* Mask out pixel format bits in case we change it */
1646 	dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1647 	switch (fb->bits_per_pixel) {
1648 	case 8:
1649 		dspcntr |= DISPPLANE_8BPP;
1650 		break;
1651 	case 16:
1652 		if (fb->depth == 15)
1653 			dspcntr |= DISPPLANE_BGRX555;
1654 		else
1655 			dspcntr |= DISPPLANE_BGRX565;
1656 		break;
1657 	case 24:
1658 	case 32:
1659 		dspcntr |= DISPPLANE_BGRX888;
1660 		break;
1661 	default:
1662 		DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
1663 		return -EINVAL;
1664 	}
1665 	if (INTEL_INFO(dev)->gen >= 4) {
1666 		if (obj->tiling_mode != I915_TILING_NONE)
1667 			dspcntr |= DISPPLANE_TILED;
1668 		else
1669 			dspcntr &= ~DISPPLANE_TILED;
1670 	}
1671 
1672 	I915_WRITE(reg, dspcntr);
1673 
1674 	Start = obj->gtt_offset;
1675 	Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
1676 
1677 	DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
1678 		      Start, Offset, x, y, fb->pitches[0]);
1679 	I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
1680 	if (INTEL_INFO(dev)->gen >= 4) {
1681 		I915_WRITE(DSPSURF(plane), Start);
1682 		I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1683 		I915_WRITE(DSPADDR(plane), Offset);
1684 	} else
1685 		I915_WRITE(DSPADDR(plane), Start + Offset);
1686 	POSTING_READ(reg);
1687 
1688 	return (0);
1689 }
1690 
1691 static int ironlake_update_plane(struct drm_crtc *crtc,
1692 				 struct drm_framebuffer *fb, int x, int y)
1693 {
1694 	struct drm_device *dev = crtc->dev;
1695 	struct drm_i915_private *dev_priv = dev->dev_private;
1696 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1697 	struct intel_framebuffer *intel_fb;
1698 	struct drm_i915_gem_object *obj;
1699 	int plane = intel_crtc->plane;
1700 	unsigned long Start, Offset;
1701 	u32 dspcntr;
1702 	u32 reg;
1703 
1704 	switch (plane) {
1705 	case 0:
1706 	case 1:
1707 	case 2:
1708 		break;
1709 	default:
1710 		DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1711 		return -EINVAL;
1712 	}
1713 
1714 	intel_fb = to_intel_framebuffer(fb);
1715 	obj = intel_fb->obj;
1716 
1717 	reg = DSPCNTR(plane);
1718 	dspcntr = I915_READ(reg);
1719 	/* Mask out pixel format bits in case we change it */
1720 	dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1721 	switch (fb->bits_per_pixel) {
1722 	case 8:
1723 		dspcntr |= DISPPLANE_8BPP;
1724 		break;
1725 	case 16:
1726 		if (fb->depth != 16) {
1727 			DRM_ERROR("bpp 16, depth %d\n", fb->depth);
1728 			return -EINVAL;
1729 		}
1730 
1731 		dspcntr |= DISPPLANE_BGRX565;
1732 		break;
1733 	case 24:
1734 	case 32:
1735 		if (fb->depth == 24)
1736 			dspcntr |= DISPPLANE_BGRX888;
1737 		else if (fb->depth == 30)
1738 			dspcntr |= DISPPLANE_BGRX101010;
1739 		else {
1740 			DRM_ERROR("bpp %d depth %d\n", fb->bits_per_pixel,
1741 			    fb->depth);
1742 			return -EINVAL;
1743 		}
1744 		break;
1745 	default:
1746 		DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
1747 		return -EINVAL;
1748 	}
1749 
1750 	if (obj->tiling_mode != I915_TILING_NONE)
1751 		dspcntr |= DISPPLANE_TILED;
1752 	else
1753 		dspcntr &= ~DISPPLANE_TILED;
1754 
1755 	/* must disable */
1756 	dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1757 
1758 	I915_WRITE(reg, dspcntr);
1759 
1760 	Start = obj->gtt_offset;
1761 	Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
1762 
1763 	DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
1764 		      Start, Offset, x, y, fb->pitches[0]);
1765 	I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
1766 	I915_WRITE(DSPSURF(plane), Start);
1767 	I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1768 	I915_WRITE(DSPADDR(plane), Offset);
1769 	POSTING_READ(reg);
1770 
1771 	return 0;
1772 }
1773 
1774 /* Assume fb object is pinned & idle & fenced and just update base pointers */
1775 static int
1776 intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1777 			   int x, int y, enum mode_set_atomic state)
1778 {
1779 	struct drm_device *dev = crtc->dev;
1780 	struct drm_i915_private *dev_priv = dev->dev_private;
1781 
1782 	if (dev_priv->display.disable_fbc)
1783 		dev_priv->display.disable_fbc(dev);
1784 	intel_increase_pllclock(crtc);
1785 
1786 	return dev_priv->display.update_plane(crtc, fb, x, y);
1787 }
1788 
1789 static int
1790 intel_finish_fb(struct drm_framebuffer *old_fb)
1791 {
1792 	struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
1793 	struct drm_device *dev = obj->base.dev;
1794 	struct drm_i915_private *dev_priv = dev->dev_private;
1795 	bool was_interruptible = dev_priv->mm.interruptible;
1796 	int ret;
1797 
1798 /* XXX */	lockmgr(&dev->event_lock, LK_EXCLUSIVE);
1799 	while (!atomic_read(&dev_priv->mm.wedged) &&
1800 	       atomic_read(&obj->pending_flip) != 0) {
1801 		lksleep(&obj->pending_flip, &dev->event_lock,
1802 		    0, "915flp", 0);
1803 	}
1804 /* XXX */	lockmgr(&dev->event_lock, LK_RELEASE);
1805 
1806 	/* Big Hammer, we also need to ensure that any pending
1807 	 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1808 	 * current scanout is retired before unpinning the old
1809 	 * framebuffer.
1810 	 *
1811 	 * This should only fail upon a hung GPU, in which case we
1812 	 * can safely continue.
1813 	 */
1814 	dev_priv->mm.interruptible = false;
1815 	ret = i915_gem_object_finish_gpu(obj);
1816 	dev_priv->mm.interruptible = was_interruptible;
1817 	return ret;
1818 }
1819 
1820 static int
1821 intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
1822 		    struct drm_framebuffer *old_fb)
1823 {
1824 	struct drm_device *dev = crtc->dev;
1825 #if 0
1826 	struct drm_i915_master_private *master_priv;
1827 #else
1828 	drm_i915_private_t *dev_priv = dev->dev_private;
1829 #endif
1830 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1831 	int ret;
1832 
1833 	/* no fb bound */
1834 	if (!crtc->fb) {
1835 		DRM_ERROR("No FB bound\n");
1836 		return 0;
1837 	}
1838 
1839 	switch (intel_crtc->plane) {
1840 	case 0:
1841 	case 1:
1842 		break;
1843 	case 2:
1844 		if (IS_IVYBRIDGE(dev))
1845 			break;
1846 		/* fall through otherwise */
1847 	default:
1848 		DRM_ERROR("no plane for crtc\n");
1849 		return -EINVAL;
1850 	}
1851 
1852 	DRM_LOCK(dev);
1853 	ret = intel_pin_and_fence_fb_obj(dev,
1854 					 to_intel_framebuffer(crtc->fb)->obj,
1855 					 NULL);
1856 	if (ret != 0) {
1857 		DRM_UNLOCK(dev);
1858 		DRM_ERROR("pin & fence failed\n");
1859 		return ret;
1860 	}
1861 
1862 	if (old_fb)
1863 		intel_finish_fb(old_fb);
1864 
1865 	ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
1866 					 LEAVE_ATOMIC_MODE_SET);
1867 	if (ret) {
1868 		intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
1869 		DRM_UNLOCK(dev);
1870 		DRM_ERROR("failed to update base address\n");
1871 		return ret;
1872 	}
1873 
1874 	if (old_fb) {
1875 		intel_wait_for_vblank(dev, intel_crtc->pipe);
1876 		intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
1877 	}
1878 
1879 	DRM_UNLOCK(dev);
1880 
1881 #if 0
1882 	if (!dev->primary->master)
1883 		return 0;
1884 
1885 	master_priv = dev->primary->master->driver_priv;
1886 	if (!master_priv->sarea_priv)
1887 		return 0;
1888 
1889 	if (intel_crtc->pipe) {
1890 		master_priv->sarea_priv->pipeB_x = x;
1891 		master_priv->sarea_priv->pipeB_y = y;
1892 	} else {
1893 		master_priv->sarea_priv->pipeA_x = x;
1894 		master_priv->sarea_priv->pipeA_y = y;
1895 	}
1896 #else
1897 
1898 	if (!dev_priv->sarea_priv)
1899 		return 0;
1900 
1901 	if (intel_crtc->pipe) {
1902 		dev_priv->sarea_priv->planeB_x = x;
1903 		dev_priv->sarea_priv->planeB_y = y;
1904 	} else {
1905 		dev_priv->sarea_priv->planeA_x = x;
1906 		dev_priv->sarea_priv->planeA_y = y;
1907 	}
1908 #endif
1909 
1910 	return 0;
1911 }
1912 
1913 static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
1914 {
1915 	struct drm_device *dev = crtc->dev;
1916 	struct drm_i915_private *dev_priv = dev->dev_private;
1917 	u32 dpa_ctl;
1918 
1919 	DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
1920 	dpa_ctl = I915_READ(DP_A);
1921 	dpa_ctl &= ~DP_PLL_FREQ_MASK;
1922 
1923 	if (clock < 200000) {
1924 		u32 temp;
1925 		dpa_ctl |= DP_PLL_FREQ_160MHZ;
1926 		/* workaround for 160Mhz:
1927 		   1) program 0x4600c bits 15:0 = 0x8124
1928 		   2) program 0x46010 bit 0 = 1
1929 		   3) program 0x46034 bit 24 = 1
1930 		   4) program 0x64000 bit 14 = 1
1931 		   */
1932 		temp = I915_READ(0x4600c);
1933 		temp &= 0xffff0000;
1934 		I915_WRITE(0x4600c, temp | 0x8124);
1935 
1936 		temp = I915_READ(0x46010);
1937 		I915_WRITE(0x46010, temp | 1);
1938 
1939 		temp = I915_READ(0x46034);
1940 		I915_WRITE(0x46034, temp | (1 << 24));
1941 	} else {
1942 		dpa_ctl |= DP_PLL_FREQ_270MHZ;
1943 	}
1944 	I915_WRITE(DP_A, dpa_ctl);
1945 
1946 	POSTING_READ(DP_A);
1947 	DELAY(500);
1948 }
1949 
1950 static void intel_fdi_normal_train(struct drm_crtc *crtc)
1951 {
1952 	struct drm_device *dev = crtc->dev;
1953 	struct drm_i915_private *dev_priv = dev->dev_private;
1954 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1955 	int pipe = intel_crtc->pipe;
1956 	u32 reg, temp;
1957 
1958 	/* enable normal train */
1959 	reg = FDI_TX_CTL(pipe);
1960 	temp = I915_READ(reg);
1961 	if (IS_IVYBRIDGE(dev)) {
1962 		temp &= ~FDI_LINK_TRAIN_NONE_IVB;
1963 		temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
1964 	} else {
1965 		temp &= ~FDI_LINK_TRAIN_NONE;
1966 		temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
1967 	}
1968 	I915_WRITE(reg, temp);
1969 
1970 	reg = FDI_RX_CTL(pipe);
1971 	temp = I915_READ(reg);
1972 	if (HAS_PCH_CPT(dev)) {
1973 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
1974 		temp |= FDI_LINK_TRAIN_NORMAL_CPT;
1975 	} else {
1976 		temp &= ~FDI_LINK_TRAIN_NONE;
1977 		temp |= FDI_LINK_TRAIN_NONE;
1978 	}
1979 	I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
1980 
1981 	/* wait one idle pattern time */
1982 	POSTING_READ(reg);
1983 	DELAY(1000);
1984 
1985 	/* IVB wants error correction enabled */
1986 	if (IS_IVYBRIDGE(dev))
1987 		I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
1988 			   FDI_FE_ERRC_ENABLE);
1989 }
1990 
1991 static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
1992 {
1993 	struct drm_i915_private *dev_priv = dev->dev_private;
1994 	u32 flags = I915_READ(SOUTH_CHICKEN1);
1995 
1996 	flags |= FDI_PHASE_SYNC_OVR(pipe);
1997 	I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
1998 	flags |= FDI_PHASE_SYNC_EN(pipe);
1999 	I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2000 	POSTING_READ(SOUTH_CHICKEN1);
2001 }
2002 
2003 /* The FDI link training functions for ILK/Ibexpeak. */
2004 static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2005 {
2006 	struct drm_device *dev = crtc->dev;
2007 	struct drm_i915_private *dev_priv = dev->dev_private;
2008 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2009 	int pipe = intel_crtc->pipe;
2010 	int plane = intel_crtc->plane;
2011 	u32 reg, temp, tries;
2012 
2013 	/* FDI needs bits from pipe & plane first */
2014 	assert_pipe_enabled(dev_priv, pipe);
2015 	assert_plane_enabled(dev_priv, plane);
2016 
2017 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2018 	   for train result */
2019 	reg = FDI_RX_IMR(pipe);
2020 	temp = I915_READ(reg);
2021 	temp &= ~FDI_RX_SYMBOL_LOCK;
2022 	temp &= ~FDI_RX_BIT_LOCK;
2023 	I915_WRITE(reg, temp);
2024 	I915_READ(reg);
2025 	DELAY(150);
2026 
2027 	/* enable CPU FDI TX and PCH FDI RX */
2028 	reg = FDI_TX_CTL(pipe);
2029 	temp = I915_READ(reg);
2030 	temp &= ~(7 << 19);
2031 	temp |= (intel_crtc->fdi_lanes - 1) << 19;
2032 	temp &= ~FDI_LINK_TRAIN_NONE;
2033 	temp |= FDI_LINK_TRAIN_PATTERN_1;
2034 	I915_WRITE(reg, temp | FDI_TX_ENABLE);
2035 
2036 	reg = FDI_RX_CTL(pipe);
2037 	temp = I915_READ(reg);
2038 	temp &= ~FDI_LINK_TRAIN_NONE;
2039 	temp |= FDI_LINK_TRAIN_PATTERN_1;
2040 	I915_WRITE(reg, temp | FDI_RX_ENABLE);
2041 
2042 	POSTING_READ(reg);
2043 	DELAY(150);
2044 
2045 	/* Ironlake workaround, enable clock pointer after FDI enable*/
2046 	if (HAS_PCH_IBX(dev)) {
2047 		I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2048 		I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2049 			   FDI_RX_PHASE_SYNC_POINTER_EN);
2050 	}
2051 
2052 	reg = FDI_RX_IIR(pipe);
2053 	for (tries = 0; tries < 5; tries++) {
2054 		temp = I915_READ(reg);
2055 		DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2056 
2057 		if ((temp & FDI_RX_BIT_LOCK)) {
2058 			DRM_DEBUG_KMS("FDI train 1 done.\n");
2059 			I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2060 			break;
2061 		}
2062 	}
2063 	if (tries == 5)
2064 		DRM_ERROR("FDI train 1 fail!\n");
2065 
2066 	/* Train 2 */
2067 	reg = FDI_TX_CTL(pipe);
2068 	temp = I915_READ(reg);
2069 	temp &= ~FDI_LINK_TRAIN_NONE;
2070 	temp |= FDI_LINK_TRAIN_PATTERN_2;
2071 	I915_WRITE(reg, temp);
2072 
2073 	reg = FDI_RX_CTL(pipe);
2074 	temp = I915_READ(reg);
2075 	temp &= ~FDI_LINK_TRAIN_NONE;
2076 	temp |= FDI_LINK_TRAIN_PATTERN_2;
2077 	I915_WRITE(reg, temp);
2078 
2079 	POSTING_READ(reg);
2080 	DELAY(150);
2081 
2082 	reg = FDI_RX_IIR(pipe);
2083 	for (tries = 0; tries < 5; tries++) {
2084 		temp = I915_READ(reg);
2085 		DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2086 
2087 		if (temp & FDI_RX_SYMBOL_LOCK) {
2088 			I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2089 			DRM_DEBUG_KMS("FDI train 2 done.\n");
2090 			break;
2091 		}
2092 	}
2093 	if (tries == 5)
2094 		DRM_ERROR("FDI train 2 fail!\n");
2095 
2096 	DRM_DEBUG_KMS("FDI train done\n");
2097 
2098 }
2099 
2100 static const int snb_b_fdi_train_param[] = {
2101 	FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2102 	FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2103 	FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2104 	FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2105 };
2106 
2107 /* The FDI link training functions for SNB/Cougarpoint. */
2108 static void gen6_fdi_link_train(struct drm_crtc *crtc)
2109 {
2110 	struct drm_device *dev = crtc->dev;
2111 	struct drm_i915_private *dev_priv = dev->dev_private;
2112 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2113 	int pipe = intel_crtc->pipe;
2114 	u32 reg, temp, i;
2115 
2116 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2117 	   for train result */
2118 	reg = FDI_RX_IMR(pipe);
2119 	temp = I915_READ(reg);
2120 	temp &= ~FDI_RX_SYMBOL_LOCK;
2121 	temp &= ~FDI_RX_BIT_LOCK;
2122 	I915_WRITE(reg, temp);
2123 
2124 	POSTING_READ(reg);
2125 	DELAY(150);
2126 
2127 	/* enable CPU FDI TX and PCH FDI RX */
2128 	reg = FDI_TX_CTL(pipe);
2129 	temp = I915_READ(reg);
2130 	temp &= ~(7 << 19);
2131 	temp |= (intel_crtc->fdi_lanes - 1) << 19;
2132 	temp &= ~FDI_LINK_TRAIN_NONE;
2133 	temp |= FDI_LINK_TRAIN_PATTERN_1;
2134 	temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2135 	/* SNB-B */
2136 	temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2137 	I915_WRITE(reg, temp | FDI_TX_ENABLE);
2138 
2139 	reg = FDI_RX_CTL(pipe);
2140 	temp = I915_READ(reg);
2141 	if (HAS_PCH_CPT(dev)) {
2142 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2143 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2144 	} else {
2145 		temp &= ~FDI_LINK_TRAIN_NONE;
2146 		temp |= FDI_LINK_TRAIN_PATTERN_1;
2147 	}
2148 	I915_WRITE(reg, temp | FDI_RX_ENABLE);
2149 
2150 	POSTING_READ(reg);
2151 	DELAY(150);
2152 
2153 	if (HAS_PCH_CPT(dev))
2154 		cpt_phase_pointer_enable(dev, pipe);
2155 
2156 	for (i = 0; i < 4; i++) {
2157 		reg = FDI_TX_CTL(pipe);
2158 		temp = I915_READ(reg);
2159 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2160 		temp |= snb_b_fdi_train_param[i];
2161 		I915_WRITE(reg, temp);
2162 
2163 		POSTING_READ(reg);
2164 		DELAY(500);
2165 
2166 		reg = FDI_RX_IIR(pipe);
2167 		temp = I915_READ(reg);
2168 		DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2169 
2170 		if (temp & FDI_RX_BIT_LOCK) {
2171 			I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2172 			DRM_DEBUG_KMS("FDI train 1 done.\n");
2173 			break;
2174 		}
2175 	}
2176 	if (i == 4)
2177 		DRM_ERROR("FDI train 1 fail!\n");
2178 
2179 	/* Train 2 */
2180 	reg = FDI_TX_CTL(pipe);
2181 	temp = I915_READ(reg);
2182 	temp &= ~FDI_LINK_TRAIN_NONE;
2183 	temp |= FDI_LINK_TRAIN_PATTERN_2;
2184 	if (IS_GEN6(dev)) {
2185 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2186 		/* SNB-B */
2187 		temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2188 	}
2189 	I915_WRITE(reg, temp);
2190 
2191 	reg = FDI_RX_CTL(pipe);
2192 	temp = I915_READ(reg);
2193 	if (HAS_PCH_CPT(dev)) {
2194 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2195 		temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2196 	} else {
2197 		temp &= ~FDI_LINK_TRAIN_NONE;
2198 		temp |= FDI_LINK_TRAIN_PATTERN_2;
2199 	}
2200 	I915_WRITE(reg, temp);
2201 
2202 	POSTING_READ(reg);
2203 	DELAY(150);
2204 
2205 	for (i = 0; i < 4; i++) {
2206 		reg = FDI_TX_CTL(pipe);
2207 		temp = I915_READ(reg);
2208 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2209 		temp |= snb_b_fdi_train_param[i];
2210 		I915_WRITE(reg, temp);
2211 
2212 		POSTING_READ(reg);
2213 		DELAY(500);
2214 
2215 		reg = FDI_RX_IIR(pipe);
2216 		temp = I915_READ(reg);
2217 		DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2218 
2219 		if (temp & FDI_RX_SYMBOL_LOCK) {
2220 			I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2221 			DRM_DEBUG_KMS("FDI train 2 done.\n");
2222 			break;
2223 		}
2224 	}
2225 	if (i == 4)
2226 		DRM_ERROR("FDI train 2 fail!\n");
2227 
2228 	DRM_DEBUG_KMS("FDI train done.\n");
2229 }
2230 
2231 /* Manual link training for Ivy Bridge A0 parts */
2232 static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2233 {
2234 	struct drm_device *dev = crtc->dev;
2235 	struct drm_i915_private *dev_priv = dev->dev_private;
2236 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2237 	int pipe = intel_crtc->pipe;
2238 	u32 reg, temp, i;
2239 
2240 	/* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2241 	   for train result */
2242 	reg = FDI_RX_IMR(pipe);
2243 	temp = I915_READ(reg);
2244 	temp &= ~FDI_RX_SYMBOL_LOCK;
2245 	temp &= ~FDI_RX_BIT_LOCK;
2246 	I915_WRITE(reg, temp);
2247 
2248 	POSTING_READ(reg);
2249 	DELAY(150);
2250 
2251 	/* enable CPU FDI TX and PCH FDI RX */
2252 	reg = FDI_TX_CTL(pipe);
2253 	temp = I915_READ(reg);
2254 	temp &= ~(7 << 19);
2255 	temp |= (intel_crtc->fdi_lanes - 1) << 19;
2256 	temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2257 	temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2258 	temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2259 	temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2260 	temp |= FDI_COMPOSITE_SYNC;
2261 	I915_WRITE(reg, temp | FDI_TX_ENABLE);
2262 
2263 	reg = FDI_RX_CTL(pipe);
2264 	temp = I915_READ(reg);
2265 	temp &= ~FDI_LINK_TRAIN_AUTO;
2266 	temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2267 	temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2268 	temp |= FDI_COMPOSITE_SYNC;
2269 	I915_WRITE(reg, temp | FDI_RX_ENABLE);
2270 
2271 	POSTING_READ(reg);
2272 	DELAY(150);
2273 
2274 	for (i = 0; i < 4; i++) {
2275 		reg = FDI_TX_CTL(pipe);
2276 		temp = I915_READ(reg);
2277 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2278 		temp |= snb_b_fdi_train_param[i];
2279 		I915_WRITE(reg, temp);
2280 
2281 		POSTING_READ(reg);
2282 		DELAY(500);
2283 
2284 		reg = FDI_RX_IIR(pipe);
2285 		temp = I915_READ(reg);
2286 		DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2287 
2288 		if (temp & FDI_RX_BIT_LOCK ||
2289 		    (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2290 			I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2291 			DRM_DEBUG_KMS("FDI train 1 done.\n");
2292 			break;
2293 		}
2294 	}
2295 	if (i == 4)
2296 		DRM_ERROR("FDI train 1 fail!\n");
2297 
2298 	/* Train 2 */
2299 	reg = FDI_TX_CTL(pipe);
2300 	temp = I915_READ(reg);
2301 	temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2302 	temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2303 	temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2304 	temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2305 	I915_WRITE(reg, temp);
2306 
2307 	reg = FDI_RX_CTL(pipe);
2308 	temp = I915_READ(reg);
2309 	temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2310 	temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2311 	I915_WRITE(reg, temp);
2312 
2313 	POSTING_READ(reg);
2314 	DELAY(150);
2315 
2316 	for (i = 0; i < 4; i++ ) {
2317 		reg = FDI_TX_CTL(pipe);
2318 		temp = I915_READ(reg);
2319 		temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2320 		temp |= snb_b_fdi_train_param[i];
2321 		I915_WRITE(reg, temp);
2322 
2323 		POSTING_READ(reg);
2324 		DELAY(500);
2325 
2326 		reg = FDI_RX_IIR(pipe);
2327 		temp = I915_READ(reg);
2328 		DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2329 
2330 		if (temp & FDI_RX_SYMBOL_LOCK) {
2331 			I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2332 			DRM_DEBUG_KMS("FDI train 2 done.\n");
2333 			break;
2334 		}
2335 	}
2336 	if (i == 4)
2337 		DRM_ERROR("FDI train 2 fail!\n");
2338 
2339 	DRM_DEBUG_KMS("FDI train done.\n");
2340 }
2341 
2342 static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
2343 {
2344 	struct drm_device *dev = crtc->dev;
2345 	struct drm_i915_private *dev_priv = dev->dev_private;
2346 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2347 	int pipe = intel_crtc->pipe;
2348 	u32 reg, temp;
2349 
2350 	/* Write the TU size bits so error detection works */
2351 	I915_WRITE(FDI_RX_TUSIZE1(pipe),
2352 		   I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2353 
2354 	/* enable PCH FDI RX PLL, wait warmup plus DMI latency */
2355 	reg = FDI_RX_CTL(pipe);
2356 	temp = I915_READ(reg);
2357 	temp &= ~((0x7 << 19) | (0x7 << 16));
2358 	temp |= (intel_crtc->fdi_lanes - 1) << 19;
2359 	temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2360 	I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2361 
2362 	POSTING_READ(reg);
2363 	DELAY(200);
2364 
2365 	/* Switch from Rawclk to PCDclk */
2366 	temp = I915_READ(reg);
2367 	I915_WRITE(reg, temp | FDI_PCDCLK);
2368 
2369 	POSTING_READ(reg);
2370 	DELAY(200);
2371 
2372 	/* Enable CPU FDI TX PLL, always on for Ironlake */
2373 	reg = FDI_TX_CTL(pipe);
2374 	temp = I915_READ(reg);
2375 	if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2376 		I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2377 
2378 		POSTING_READ(reg);
2379 		DELAY(100);
2380 	}
2381 }
2382 
2383 static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2384 {
2385 	struct drm_i915_private *dev_priv = dev->dev_private;
2386 	u32 flags = I915_READ(SOUTH_CHICKEN1);
2387 
2388 	flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2389 	I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2390 	flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2391 	I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2392 	POSTING_READ(SOUTH_CHICKEN1);
2393 }
2394 
2395 static void ironlake_fdi_disable(struct drm_crtc *crtc)
2396 {
2397 	struct drm_device *dev = crtc->dev;
2398 	struct drm_i915_private *dev_priv = dev->dev_private;
2399 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2400 	int pipe = intel_crtc->pipe;
2401 	u32 reg, temp;
2402 
2403 	/* disable CPU FDI tx and PCH FDI rx */
2404 	reg = FDI_TX_CTL(pipe);
2405 	temp = I915_READ(reg);
2406 	I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2407 	POSTING_READ(reg);
2408 
2409 	reg = FDI_RX_CTL(pipe);
2410 	temp = I915_READ(reg);
2411 	temp &= ~(0x7 << 16);
2412 	temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2413 	I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2414 
2415 	POSTING_READ(reg);
2416 	DELAY(100);
2417 
2418 	/* Ironlake workaround, disable clock pointer after downing FDI */
2419 	if (HAS_PCH_IBX(dev)) {
2420 		I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2421 		I915_WRITE(FDI_RX_CHICKEN(pipe),
2422 			   I915_READ(FDI_RX_CHICKEN(pipe) &
2423 				     ~FDI_RX_PHASE_SYNC_POINTER_EN));
2424 	} else if (HAS_PCH_CPT(dev)) {
2425 		cpt_phase_pointer_disable(dev, pipe);
2426 	}
2427 
2428 	/* still set train pattern 1 */
2429 	reg = FDI_TX_CTL(pipe);
2430 	temp = I915_READ(reg);
2431 	temp &= ~FDI_LINK_TRAIN_NONE;
2432 	temp |= FDI_LINK_TRAIN_PATTERN_1;
2433 	I915_WRITE(reg, temp);
2434 
2435 	reg = FDI_RX_CTL(pipe);
2436 	temp = I915_READ(reg);
2437 	if (HAS_PCH_CPT(dev)) {
2438 		temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2439 		temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2440 	} else {
2441 		temp &= ~FDI_LINK_TRAIN_NONE;
2442 		temp |= FDI_LINK_TRAIN_PATTERN_1;
2443 	}
2444 	/* BPC in FDI rx is consistent with that in PIPECONF */
2445 	temp &= ~(0x07 << 16);
2446 	temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2447 	I915_WRITE(reg, temp);
2448 
2449 	POSTING_READ(reg);
2450 	DELAY(100);
2451 }
2452 
2453 /*
2454  * When we disable a pipe, we need to clear any pending scanline wait events
2455  * to avoid hanging the ring, which we assume we are waiting on.
2456  */
2457 static void intel_clear_scanline_wait(struct drm_device *dev)
2458 {
2459 	struct drm_i915_private *dev_priv = dev->dev_private;
2460 	struct intel_ring_buffer *ring;
2461 	u32 tmp;
2462 
2463 	if (IS_GEN2(dev))
2464 		/* Can't break the hang on i8xx */
2465 		return;
2466 
2467 	ring = LP_RING(dev_priv);
2468 	tmp = I915_READ_CTL(ring);
2469 	if (tmp & RING_WAIT)
2470 		I915_WRITE_CTL(ring, tmp);
2471 }
2472 
2473 static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2474 {
2475 	struct drm_i915_gem_object *obj;
2476 	struct drm_i915_private *dev_priv;
2477 	struct drm_device *dev;
2478 
2479 	if (crtc->fb == NULL)
2480 		return;
2481 
2482 	obj = to_intel_framebuffer(crtc->fb)->obj;
2483 	dev = crtc->dev;
2484 	dev_priv = dev->dev_private;
2485 	lockmgr(&dev->event_lock, LK_EXCLUSIVE);
2486 	while (atomic_read(&obj->pending_flip) != 0)
2487 		lksleep(&obj->pending_flip, &dev->event_lock, 0, "915wfl", 0);
2488 	lockmgr(&dev->event_lock, LK_RELEASE);
2489 }
2490 
2491 static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
2492 {
2493 	struct drm_device *dev = crtc->dev;
2494 	struct drm_mode_config *mode_config = &dev->mode_config;
2495 	struct intel_encoder *encoder;
2496 
2497 	/*
2498 	 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2499 	 * must be driven by its own crtc; no sharing is possible.
2500 	 */
2501 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
2502 		if (encoder->base.crtc != crtc)
2503 			continue;
2504 
2505 		switch (encoder->type) {
2506 		case INTEL_OUTPUT_EDP:
2507 			if (!intel_encoder_is_pch_edp(&encoder->base))
2508 				return false;
2509 			continue;
2510 		}
2511 	}
2512 
2513 	return true;
2514 }
2515 
2516 /*
2517  * Enable PCH resources required for PCH ports:
2518  *   - PCH PLLs
2519  *   - FDI training & RX/TX
2520  *   - update transcoder timings
2521  *   - DP transcoding bits
2522  *   - transcoder
2523  */
2524 static void ironlake_pch_enable(struct drm_crtc *crtc)
2525 {
2526 	struct drm_device *dev = crtc->dev;
2527 	struct drm_i915_private *dev_priv = dev->dev_private;
2528 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2529 	int pipe = intel_crtc->pipe;
2530 	u32 reg, temp, transc_sel;
2531 
2532 	/* For PCH output, training FDI link */
2533 	dev_priv->display.fdi_link_train(crtc);
2534 
2535 	intel_enable_pch_pll(dev_priv, pipe);
2536 
2537 	if (HAS_PCH_CPT(dev)) {
2538 		transc_sel = intel_crtc->use_pll_a ? TRANSC_DPLLA_SEL :
2539 			TRANSC_DPLLB_SEL;
2540 
2541 		/* Be sure PCH DPLL SEL is set */
2542 		temp = I915_READ(PCH_DPLL_SEL);
2543 		if (pipe == 0) {
2544 			temp &= ~(TRANSA_DPLLB_SEL);
2545 			temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
2546 		} else if (pipe == 1) {
2547 			temp &= ~(TRANSB_DPLLB_SEL);
2548 			temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2549 		} else if (pipe == 2) {
2550 			temp &= ~(TRANSC_DPLLB_SEL);
2551 			temp |= (TRANSC_DPLL_ENABLE | transc_sel);
2552 		}
2553 		I915_WRITE(PCH_DPLL_SEL, temp);
2554 	}
2555 
2556 	/* set transcoder timing, panel must allow it */
2557 	assert_panel_unlocked(dev_priv, pipe);
2558 	I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
2559 	I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
2560 	I915_WRITE(TRANS_HSYNC(pipe),  I915_READ(HSYNC(pipe)));
2561 
2562 	I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
2563 	I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
2564 	I915_WRITE(TRANS_VSYNC(pipe),  I915_READ(VSYNC(pipe)));
2565 	I915_WRITE(TRANS_VSYNCSHIFT(pipe),  I915_READ(VSYNCSHIFT(pipe)));
2566 
2567 	intel_fdi_normal_train(crtc);
2568 
2569 	/* For PCH DP, enable TRANS_DP_CTL */
2570 	if (HAS_PCH_CPT(dev) &&
2571 	    (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
2572 	     intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2573 		u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
2574 		reg = TRANS_DP_CTL(pipe);
2575 		temp = I915_READ(reg);
2576 		temp &= ~(TRANS_DP_PORT_SEL_MASK |
2577 			  TRANS_DP_SYNC_MASK |
2578 			  TRANS_DP_BPC_MASK);
2579 		temp |= (TRANS_DP_OUTPUT_ENABLE |
2580 			 TRANS_DP_ENH_FRAMING);
2581 		temp |= bpc << 9; /* same format but at 11:9 */
2582 
2583 		if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
2584 			temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
2585 		if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
2586 			temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
2587 
2588 		switch (intel_trans_dp_port_sel(crtc)) {
2589 		case PCH_DP_B:
2590 			temp |= TRANS_DP_PORT_SEL_B;
2591 			break;
2592 		case PCH_DP_C:
2593 			temp |= TRANS_DP_PORT_SEL_C;
2594 			break;
2595 		case PCH_DP_D:
2596 			temp |= TRANS_DP_PORT_SEL_D;
2597 			break;
2598 		default:
2599 			DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
2600 			temp |= TRANS_DP_PORT_SEL_B;
2601 			break;
2602 		}
2603 
2604 		I915_WRITE(reg, temp);
2605 	}
2606 
2607 	intel_enable_transcoder(dev_priv, pipe);
2608 }
2609 
2610 void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
2611 {
2612 	struct drm_i915_private *dev_priv = dev->dev_private;
2613 	int dslreg = PIPEDSL(pipe);
2614 	u32 temp;
2615 
2616 	temp = I915_READ(dslreg);
2617 	udelay(500);
2618 	if (wait_for(I915_READ(dslreg) != temp, 5)) {
2619 		if (wait_for(I915_READ(dslreg) != temp, 5))
2620 			DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
2621 	}
2622 }
2623 
2624 static void ironlake_crtc_enable(struct drm_crtc *crtc)
2625 {
2626 	struct drm_device *dev = crtc->dev;
2627 	struct drm_i915_private *dev_priv = dev->dev_private;
2628 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2629 	int pipe = intel_crtc->pipe;
2630 	int plane = intel_crtc->plane;
2631 	u32 temp;
2632 	bool is_pch_port;
2633 
2634 	if (intel_crtc->active)
2635 		return;
2636 
2637 	intel_crtc->active = true;
2638 	intel_update_watermarks(dev);
2639 
2640 	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
2641 		temp = I915_READ(PCH_LVDS);
2642 		if ((temp & LVDS_PORT_EN) == 0)
2643 			I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
2644 	}
2645 
2646 	is_pch_port = intel_crtc_driving_pch(crtc);
2647 
2648 	if (is_pch_port)
2649 		ironlake_fdi_pll_enable(crtc);
2650 	else
2651 		ironlake_fdi_disable(crtc);
2652 
2653 	/* Enable panel fitting for LVDS */
2654 	if (dev_priv->pch_pf_size &&
2655 	    (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
2656 		/* Force use of hard-coded filter coefficients
2657 		 * as some pre-programmed values are broken,
2658 		 * e.g. x201.
2659 		 */
2660 		I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
2661 		I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
2662 		I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
2663 	}
2664 
2665 	intel_enable_pipe(dev_priv, pipe, is_pch_port);
2666 	intel_enable_plane(dev_priv, plane, pipe);
2667 
2668 	if (is_pch_port)
2669 		ironlake_pch_enable(crtc);
2670 
2671 	intel_crtc_load_lut(crtc);
2672 
2673 	DRM_LOCK(dev);
2674 	intel_update_fbc(dev);
2675 	DRM_UNLOCK(dev);
2676 
2677 	intel_crtc_update_cursor(crtc, true);
2678 }
2679 
2680 static void ironlake_crtc_disable(struct drm_crtc *crtc)
2681 {
2682 	struct drm_device *dev = crtc->dev;
2683 	struct drm_i915_private *dev_priv = dev->dev_private;
2684 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2685 	int pipe = intel_crtc->pipe;
2686 	int plane = intel_crtc->plane;
2687 	u32 reg, temp;
2688 
2689 	if (!intel_crtc->active)
2690 		return;
2691 
2692 	intel_crtc_wait_for_pending_flips(crtc);
2693 	drm_vblank_off(dev, pipe);
2694 	intel_crtc_update_cursor(crtc, false);
2695 
2696 	intel_disable_plane(dev_priv, plane, pipe);
2697 
2698 	if (dev_priv->cfb_plane == plane)
2699 		intel_disable_fbc(dev);
2700 
2701 	intel_disable_pipe(dev_priv, pipe);
2702 
2703 	/* Disable PF */
2704 	I915_WRITE(PF_CTL(pipe), 0);
2705 	I915_WRITE(PF_WIN_SZ(pipe), 0);
2706 
2707 	ironlake_fdi_disable(crtc);
2708 
2709 	/* This is a horrible layering violation; we should be doing this in
2710 	 * the connector/encoder ->prepare instead, but we don't always have
2711 	 * enough information there about the config to know whether it will
2712 	 * actually be necessary or just cause undesired flicker.
2713 	 */
2714 	intel_disable_pch_ports(dev_priv, pipe);
2715 
2716 	intel_disable_transcoder(dev_priv, pipe);
2717 
2718 	if (HAS_PCH_CPT(dev)) {
2719 		/* disable TRANS_DP_CTL */
2720 		reg = TRANS_DP_CTL(pipe);
2721 		temp = I915_READ(reg);
2722 		temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
2723 		temp |= TRANS_DP_PORT_SEL_NONE;
2724 		I915_WRITE(reg, temp);
2725 
2726 		/* disable DPLL_SEL */
2727 		temp = I915_READ(PCH_DPLL_SEL);
2728 		switch (pipe) {
2729 		case 0:
2730 			temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
2731 			break;
2732 		case 1:
2733 			temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
2734 			break;
2735 		case 2:
2736 			/* C shares PLL A or B */
2737 			temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
2738 			break;
2739 		default:
2740 			KASSERT(1, ("Wrong pipe %d", pipe)); /* wtf */
2741 		}
2742 		I915_WRITE(PCH_DPLL_SEL, temp);
2743 	}
2744 
2745 	/* disable PCH DPLL */
2746 	if (!intel_crtc->no_pll)
2747 		intel_disable_pch_pll(dev_priv, pipe);
2748 
2749 	/* Switch from PCDclk to Rawclk */
2750 	reg = FDI_RX_CTL(pipe);
2751 	temp = I915_READ(reg);
2752 	I915_WRITE(reg, temp & ~FDI_PCDCLK);
2753 
2754 	/* Disable CPU FDI TX PLL */
2755 	reg = FDI_TX_CTL(pipe);
2756 	temp = I915_READ(reg);
2757 	I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2758 
2759 	POSTING_READ(reg);
2760 	DELAY(100);
2761 
2762 	reg = FDI_RX_CTL(pipe);
2763 	temp = I915_READ(reg);
2764 	I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2765 
2766 	/* Wait for the clocks to turn off. */
2767 	POSTING_READ(reg);
2768 	DELAY(100);
2769 
2770 	intel_crtc->active = false;
2771 	intel_update_watermarks(dev);
2772 
2773 	DRM_LOCK(dev);
2774 	intel_update_fbc(dev);
2775 	intel_clear_scanline_wait(dev);
2776 	DRM_UNLOCK(dev);
2777 }
2778 
2779 static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
2780 {
2781 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2782 	int pipe = intel_crtc->pipe;
2783 	int plane = intel_crtc->plane;
2784 
2785 	/* XXX: When our outputs are all unaware of DPMS modes other than off
2786 	 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2787 	 */
2788 	switch (mode) {
2789 	case DRM_MODE_DPMS_ON:
2790 	case DRM_MODE_DPMS_STANDBY:
2791 	case DRM_MODE_DPMS_SUSPEND:
2792 		DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
2793 		ironlake_crtc_enable(crtc);
2794 		break;
2795 
2796 	case DRM_MODE_DPMS_OFF:
2797 		DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
2798 		ironlake_crtc_disable(crtc);
2799 		break;
2800 	}
2801 }
2802 
2803 static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
2804 {
2805 	if (!enable && intel_crtc->overlay) {
2806 		struct drm_device *dev = intel_crtc->base.dev;
2807 		struct drm_i915_private *dev_priv = dev->dev_private;
2808 
2809 		DRM_LOCK(dev);
2810 		dev_priv->mm.interruptible = false;
2811 		(void) intel_overlay_switch_off(intel_crtc->overlay);
2812 		dev_priv->mm.interruptible = true;
2813 		DRM_UNLOCK(dev);
2814 	}
2815 
2816 	/* Let userspace switch the overlay on again. In most cases userspace
2817 	 * has to recompute where to put it anyway.
2818 	 */
2819 }
2820 
2821 static void i9xx_crtc_enable(struct drm_crtc *crtc)
2822 {
2823 	struct drm_device *dev = crtc->dev;
2824 	struct drm_i915_private *dev_priv = dev->dev_private;
2825 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2826 	int pipe = intel_crtc->pipe;
2827 	int plane = intel_crtc->plane;
2828 
2829 	if (intel_crtc->active)
2830 		return;
2831 
2832 	intel_crtc->active = true;
2833 	intel_update_watermarks(dev);
2834 
2835 	intel_enable_pll(dev_priv, pipe);
2836 	intel_enable_pipe(dev_priv, pipe, false);
2837 	intel_enable_plane(dev_priv, plane, pipe);
2838 
2839 	intel_crtc_load_lut(crtc);
2840 	intel_update_fbc(dev);
2841 
2842 	/* Give the overlay scaler a chance to enable if it's on this pipe */
2843 	intel_crtc_dpms_overlay(intel_crtc, true);
2844 	intel_crtc_update_cursor(crtc, true);
2845 }
2846 
2847 static void i9xx_crtc_disable(struct drm_crtc *crtc)
2848 {
2849 	struct drm_device *dev = crtc->dev;
2850 	struct drm_i915_private *dev_priv = dev->dev_private;
2851 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2852 	int pipe = intel_crtc->pipe;
2853 	int plane = intel_crtc->plane;
2854 
2855 	if (!intel_crtc->active)
2856 		return;
2857 
2858 	/* Give the overlay scaler a chance to disable if it's on this pipe */
2859 	intel_crtc_wait_for_pending_flips(crtc);
2860 	drm_vblank_off(dev, pipe);
2861 	intel_crtc_dpms_overlay(intel_crtc, false);
2862 	intel_crtc_update_cursor(crtc, false);
2863 
2864 	if (dev_priv->cfb_plane == plane)
2865 		intel_disable_fbc(dev);
2866 
2867 	intel_disable_plane(dev_priv, plane, pipe);
2868 	intel_disable_pipe(dev_priv, pipe);
2869 	intel_disable_pll(dev_priv, pipe);
2870 
2871 	intel_crtc->active = false;
2872 	intel_update_fbc(dev);
2873 	intel_update_watermarks(dev);
2874 	intel_clear_scanline_wait(dev);
2875 }
2876 
2877 static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
2878 {
2879 	/* XXX: When our outputs are all unaware of DPMS modes other than off
2880 	 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
2881 	 */
2882 	switch (mode) {
2883 	case DRM_MODE_DPMS_ON:
2884 	case DRM_MODE_DPMS_STANDBY:
2885 	case DRM_MODE_DPMS_SUSPEND:
2886 		i9xx_crtc_enable(crtc);
2887 		break;
2888 	case DRM_MODE_DPMS_OFF:
2889 		i9xx_crtc_disable(crtc);
2890 		break;
2891 	}
2892 }
2893 
2894 /**
2895  * Sets the power management mode of the pipe and plane.
2896  */
2897 static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
2898 {
2899 	struct drm_device *dev = crtc->dev;
2900 	struct drm_i915_private *dev_priv = dev->dev_private;
2901 #if 0
2902 	struct drm_i915_master_private *master_priv;
2903 #endif
2904 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2905 	int pipe = intel_crtc->pipe;
2906 	bool enabled;
2907 
2908 	if (intel_crtc->dpms_mode == mode)
2909 		return;
2910 
2911 	intel_crtc->dpms_mode = mode;
2912 
2913 	dev_priv->display.dpms(crtc, mode);
2914 
2915 #if 0
2916 	if (!dev->primary->master)
2917 		return;
2918 
2919 	master_priv = dev->primary->master->driver_priv;
2920 	if (!master_priv->sarea_priv)
2921 		return;
2922 #else
2923 	if (!dev_priv->sarea_priv)
2924 		return;
2925 #endif
2926 
2927 	enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
2928 
2929 	switch (pipe) {
2930 	case 0:
2931 #if 0
2932 		master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
2933 		master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
2934 #else
2935 		dev_priv->sarea_priv->planeA_w = enabled ? crtc->mode.hdisplay : 0;
2936 		dev_priv->sarea_priv->planeA_h = enabled ? crtc->mode.vdisplay : 0;
2937 #endif
2938 		break;
2939 	case 1:
2940 #if 0
2941 		master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
2942 		master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
2943 #else
2944 		dev_priv->sarea_priv->planeB_w = enabled ? crtc->mode.hdisplay : 0;
2945 		dev_priv->sarea_priv->planeB_h = enabled ? crtc->mode.vdisplay : 0;
2946 #endif
2947 		break;
2948 	default:
2949 		DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
2950 		break;
2951 	}
2952 }
2953 
2954 static void intel_crtc_disable(struct drm_crtc *crtc)
2955 {
2956 	struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
2957 	struct drm_device *dev = crtc->dev;
2958 
2959 	/* Flush any pending WAITs before we disable the pipe. Note that
2960 	 * we need to drop the struct_mutex in order to acquire it again
2961 	 * during the lowlevel dpms routines around a couple of the
2962 	 * operations. It does not look trivial nor desirable to move
2963 	 * that locking higher. So instead we leave a window for the
2964 	 * submission of further commands on the fb before we can actually
2965 	 * disable it. This race with userspace exists anyway, and we can
2966 	 * only rely on the pipe being disabled by userspace after it
2967 	 * receives the hotplug notification and has flushed any pending
2968 	 * batches.
2969 	 */
2970 	if (crtc->fb) {
2971 		DRM_LOCK(dev);
2972 		intel_finish_fb(crtc->fb);
2973 		DRM_UNLOCK(dev);
2974 	}
2975 
2976 	crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
2977  	assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
2978 	assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
2979 
2980 	if (crtc->fb) {
2981 		DRM_LOCK(dev);
2982 		intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
2983 		DRM_UNLOCK(dev);
2984 	}
2985 }
2986 
2987 /* Prepare for a mode set.
2988  *
2989  * Note we could be a lot smarter here.  We need to figure out which outputs
2990  * will be enabled, which disabled (in short, how the config will changes)
2991  * and perform the minimum necessary steps to accomplish that, e.g. updating
2992  * watermarks, FBC configuration, making sure PLLs are programmed correctly,
2993  * panel fitting is in the proper state, etc.
2994  */
2995 static void i9xx_crtc_prepare(struct drm_crtc *crtc)
2996 {
2997 	i9xx_crtc_disable(crtc);
2998 }
2999 
3000 static void i9xx_crtc_commit(struct drm_crtc *crtc)
3001 {
3002 	i9xx_crtc_enable(crtc);
3003 }
3004 
3005 static void ironlake_crtc_prepare(struct drm_crtc *crtc)
3006 {
3007 	ironlake_crtc_disable(crtc);
3008 }
3009 
3010 static void ironlake_crtc_commit(struct drm_crtc *crtc)
3011 {
3012 	ironlake_crtc_enable(crtc);
3013 }
3014 
3015 void intel_encoder_prepare(struct drm_encoder *encoder)
3016 {
3017 	struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3018 	/* lvds has its own version of prepare see intel_lvds_prepare */
3019 	encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
3020 }
3021 
3022 void intel_encoder_commit(struct drm_encoder *encoder)
3023 {
3024 	struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3025 	struct drm_device *dev = encoder->dev;
3026 	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3027 	struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
3028 
3029 	/* lvds has its own version of commit see intel_lvds_commit */
3030 	encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
3031 
3032 	if (HAS_PCH_CPT(dev))
3033 		intel_cpt_verify_modeset(dev, intel_crtc->pipe);
3034 }
3035 
3036 void intel_encoder_destroy(struct drm_encoder *encoder)
3037 {
3038 	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3039 
3040 	drm_encoder_cleanup(encoder);
3041 	drm_free(intel_encoder, DRM_MEM_KMS);
3042 }
3043 
3044 static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
3045 				  const struct drm_display_mode *mode,
3046 				  struct drm_display_mode *adjusted_mode)
3047 {
3048 	struct drm_device *dev = crtc->dev;
3049 
3050 	if (HAS_PCH_SPLIT(dev)) {
3051 		/* FDI link clock is fixed at 2.7G */
3052 		if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3053 			return false;
3054 	}
3055 
3056 	/* All interlaced capable intel hw wants timings in frames. Note though
3057 	 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3058 	 * timings, so we need to be careful not to clobber these.*/
3059 	if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3060 		drm_mode_set_crtcinfo(adjusted_mode, 0);
3061 
3062 	return true;
3063 }
3064 
3065 static int i945_get_display_clock_speed(struct drm_device *dev)
3066 {
3067 	return 400000;
3068 }
3069 
3070 static int i915_get_display_clock_speed(struct drm_device *dev)
3071 {
3072 	return 333000;
3073 }
3074 
3075 static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3076 {
3077 	return 200000;
3078 }
3079 
3080 static int i915gm_get_display_clock_speed(struct drm_device *dev)
3081 {
3082 	u16 gcfgc = 0;
3083 
3084 	gcfgc = pci_read_config(dev->dev, GCFGC, 2);
3085 
3086 	if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
3087 		return 133000;
3088 	else {
3089 		switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3090 		case GC_DISPLAY_CLOCK_333_MHZ:
3091 			return 333000;
3092 		default:
3093 		case GC_DISPLAY_CLOCK_190_200_MHZ:
3094 			return 190000;
3095 		}
3096 	}
3097 }
3098 
3099 static int i865_get_display_clock_speed(struct drm_device *dev)
3100 {
3101 	return 266000;
3102 }
3103 
3104 static int i855_get_display_clock_speed(struct drm_device *dev)
3105 {
3106 	u16 hpllcc = 0;
3107 	/* Assume that the hardware is in the high speed state.  This
3108 	 * should be the default.
3109 	 */
3110 	switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3111 	case GC_CLOCK_133_200:
3112 	case GC_CLOCK_100_200:
3113 		return 200000;
3114 	case GC_CLOCK_166_250:
3115 		return 250000;
3116 	case GC_CLOCK_100_133:
3117 		return 133000;
3118 	}
3119 
3120 	/* Shouldn't happen */
3121 	return 0;
3122 }
3123 
3124 static int i830_get_display_clock_speed(struct drm_device *dev)
3125 {
3126 	return 133000;
3127 }
3128 
3129 struct fdi_m_n {
3130 	u32        tu;
3131 	u32        gmch_m;
3132 	u32        gmch_n;
3133 	u32        link_m;
3134 	u32        link_n;
3135 };
3136 
3137 static void
3138 fdi_reduce_ratio(u32 *num, u32 *den)
3139 {
3140 	while (*num > 0xffffff || *den > 0xffffff) {
3141 		*num >>= 1;
3142 		*den >>= 1;
3143 	}
3144 }
3145 
3146 static void
3147 ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
3148 		     int link_clock, struct fdi_m_n *m_n)
3149 {
3150 	m_n->tu = 64; /* default size */
3151 
3152 	/* BUG_ON(pixel_clock > INT_MAX / 36); */
3153 	m_n->gmch_m = bits_per_pixel * pixel_clock;
3154 	m_n->gmch_n = link_clock * nlanes * 8;
3155 	fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
3156 
3157 	m_n->link_m = pixel_clock;
3158 	m_n->link_n = link_clock;
3159 	fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
3160 }
3161 
3162 static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
3163 {
3164 	if (i915_panel_use_ssc >= 0)
3165 		return i915_panel_use_ssc != 0;
3166 	return dev_priv->lvds_use_ssc
3167 		&& !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
3168 }
3169 
3170 /**
3171  * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
3172  * @crtc: CRTC structure
3173  * @mode: requested mode
3174  *
3175  * A pipe may be connected to one or more outputs.  Based on the depth of the
3176  * attached framebuffer, choose a good color depth to use on the pipe.
3177  *
3178  * If possible, match the pipe depth to the fb depth.  In some cases, this
3179  * isn't ideal, because the connected output supports a lesser or restricted
3180  * set of depths.  Resolve that here:
3181  *    LVDS typically supports only 6bpc, so clamp down in that case
3182  *    HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
3183  *    Displays may support a restricted set as well, check EDID and clamp as
3184  *      appropriate.
3185  *    DP may want to dither down to 6bpc to fit larger modes
3186  *
3187  * RETURNS:
3188  * Dithering requirement (i.e. false if display bpc and pipe bpc match,
3189  * true if they don't match).
3190  */
3191 static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
3192 					 unsigned int *pipe_bpp,
3193 					 struct drm_display_mode *mode)
3194 {
3195 	struct drm_device *dev = crtc->dev;
3196 	struct drm_i915_private *dev_priv = dev->dev_private;
3197 	struct drm_encoder *encoder;
3198 	struct drm_connector *connector;
3199 	unsigned int display_bpc = UINT_MAX, bpc;
3200 
3201 	/* Walk the encoders & connectors on this crtc, get min bpc */
3202 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
3203 		struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3204 
3205 		if (encoder->crtc != crtc)
3206 			continue;
3207 
3208 		if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
3209 			unsigned int lvds_bpc;
3210 
3211 			if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
3212 			    LVDS_A3_POWER_UP)
3213 				lvds_bpc = 8;
3214 			else
3215 				lvds_bpc = 6;
3216 
3217 			if (lvds_bpc < display_bpc) {
3218 				DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
3219 				display_bpc = lvds_bpc;
3220 			}
3221 			continue;
3222 		}
3223 
3224 		if (intel_encoder->type == INTEL_OUTPUT_EDP) {
3225 			/* Use VBT settings if we have an eDP panel */
3226 			unsigned int edp_bpc = dev_priv->edp.bpp / 3;
3227 
3228 			if (edp_bpc < display_bpc) {
3229 				DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
3230 				display_bpc = edp_bpc;
3231 			}
3232 			continue;
3233 		}
3234 
3235 		/* Not one of the known troublemakers, check the EDID */
3236 		list_for_each_entry(connector, &dev->mode_config.connector_list,
3237 				    head) {
3238 			if (connector->encoder != encoder)
3239 				continue;
3240 
3241 			/* Don't use an invalid EDID bpc value */
3242 			if (connector->display_info.bpc &&
3243 			    connector->display_info.bpc < display_bpc) {
3244 				DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
3245 				display_bpc = connector->display_info.bpc;
3246 			}
3247 		}
3248 
3249 		/*
3250 		 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
3251 		 * through, clamp it down.  (Note: >12bpc will be caught below.)
3252 		 */
3253 		if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
3254 			if (display_bpc > 8 && display_bpc < 12) {
3255 				DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
3256 				display_bpc = 12;
3257 			} else {
3258 				DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
3259 				display_bpc = 8;
3260 			}
3261 		}
3262 	}
3263 
3264 	if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
3265 		DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
3266 		display_bpc = 6;
3267 	}
3268 
3269 	/*
3270 	 * We could just drive the pipe at the highest bpc all the time and
3271 	 * enable dithering as needed, but that costs bandwidth.  So choose
3272 	 * the minimum value that expresses the full color range of the fb but
3273 	 * also stays within the max display bpc discovered above.
3274 	 */
3275 
3276 	switch (crtc->fb->depth) {
3277 	case 8:
3278 		bpc = 8; /* since we go through a colormap */
3279 		break;
3280 	case 15:
3281 	case 16:
3282 		bpc = 6; /* min is 18bpp */
3283 		break;
3284 	case 24:
3285 		bpc = 8;
3286 		break;
3287 	case 30:
3288 		bpc = 10;
3289 		break;
3290 	case 48:
3291 		bpc = 12;
3292 		break;
3293 	default:
3294 		DRM_DEBUG("unsupported depth, assuming 24 bits\n");
3295 		bpc = min((unsigned int)8, display_bpc);
3296 		break;
3297 	}
3298 
3299 	display_bpc = min(display_bpc, bpc);
3300 
3301 	DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
3302 			 bpc, display_bpc);
3303 
3304 	*pipe_bpp = display_bpc * 3;
3305 
3306 	return display_bpc != bpc;
3307 }
3308 
3309 static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
3310 {
3311 	struct drm_device *dev = crtc->dev;
3312 	struct drm_i915_private *dev_priv = dev->dev_private;
3313 	int refclk;
3314 
3315 	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
3316 	    intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
3317 		refclk = dev_priv->lvds_ssc_freq * 1000;
3318 		DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
3319 			      refclk / 1000);
3320 	} else if (!IS_GEN2(dev)) {
3321 		refclk = 96000;
3322 	} else {
3323 		refclk = 48000;
3324 	}
3325 
3326 	return refclk;
3327 }
3328 
3329 static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
3330 				      intel_clock_t *clock)
3331 {
3332 	/* SDVO TV has fixed PLL values depend on its clock range,
3333 	   this mirrors vbios setting. */
3334 	if (adjusted_mode->clock >= 100000
3335 	    && adjusted_mode->clock < 140500) {
3336 		clock->p1 = 2;
3337 		clock->p2 = 10;
3338 		clock->n = 3;
3339 		clock->m1 = 16;
3340 		clock->m2 = 8;
3341 	} else if (adjusted_mode->clock >= 140500
3342 		   && adjusted_mode->clock <= 200000) {
3343 		clock->p1 = 1;
3344 		clock->p2 = 10;
3345 		clock->n = 6;
3346 		clock->m1 = 12;
3347 		clock->m2 = 8;
3348 	}
3349 }
3350 
3351 static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
3352 				     intel_clock_t *clock,
3353 				     intel_clock_t *reduced_clock)
3354 {
3355 	struct drm_device *dev = crtc->dev;
3356 	struct drm_i915_private *dev_priv = dev->dev_private;
3357 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3358 	int pipe = intel_crtc->pipe;
3359 	u32 fp, fp2 = 0;
3360 
3361 	if (IS_PINEVIEW(dev)) {
3362 		fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
3363 		if (reduced_clock)
3364 			fp2 = (1 << reduced_clock->n) << 16 |
3365 				reduced_clock->m1 << 8 | reduced_clock->m2;
3366 	} else {
3367 		fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
3368 		if (reduced_clock)
3369 			fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
3370 				reduced_clock->m2;
3371 	}
3372 
3373 	I915_WRITE(FP0(pipe), fp);
3374 
3375 	intel_crtc->lowfreq_avail = false;
3376 	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
3377 	    reduced_clock && i915_powersave) {
3378 		I915_WRITE(FP1(pipe), fp2);
3379 		intel_crtc->lowfreq_avail = true;
3380 	} else {
3381 		I915_WRITE(FP1(pipe), fp);
3382 	}
3383 }
3384 
3385 static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
3386 			      struct drm_display_mode *mode,
3387 			      struct drm_display_mode *adjusted_mode,
3388 			      int x, int y,
3389 			      struct drm_framebuffer *old_fb)
3390 {
3391 	struct drm_device *dev = crtc->dev;
3392 	struct drm_i915_private *dev_priv = dev->dev_private;
3393 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3394 	int pipe = intel_crtc->pipe;
3395 	int plane = intel_crtc->plane;
3396 	int refclk, num_connectors = 0;
3397 	intel_clock_t clock, reduced_clock;
3398 	u32 dpll, dspcntr, pipeconf, vsyncshift;
3399 	bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
3400 	bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
3401 	struct drm_mode_config *mode_config = &dev->mode_config;
3402 	struct intel_encoder *encoder;
3403 	const intel_limit_t *limit;
3404 	int ret;
3405 	u32 temp;
3406 	u32 lvds_sync = 0;
3407 
3408 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3409 		if (encoder->base.crtc != crtc)
3410 			continue;
3411 
3412 		switch (encoder->type) {
3413 		case INTEL_OUTPUT_LVDS:
3414 			is_lvds = true;
3415 			break;
3416 		case INTEL_OUTPUT_SDVO:
3417 		case INTEL_OUTPUT_HDMI:
3418 			is_sdvo = true;
3419 			if (encoder->needs_tv_clock)
3420 				is_tv = true;
3421 			break;
3422 		case INTEL_OUTPUT_DVO:
3423 			is_dvo = true;
3424 			break;
3425 		case INTEL_OUTPUT_TVOUT:
3426 			is_tv = true;
3427 			break;
3428 		case INTEL_OUTPUT_ANALOG:
3429 			is_crt = true;
3430 			break;
3431 		case INTEL_OUTPUT_DISPLAYPORT:
3432 			is_dp = true;
3433 			break;
3434 		}
3435 
3436 		num_connectors++;
3437 	}
3438 
3439 	refclk = i9xx_get_refclk(crtc, num_connectors);
3440 
3441 	/*
3442 	 * Returns a set of divisors for the desired target clock with the given
3443 	 * refclk, or false.  The returned values represent the clock equation:
3444 	 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
3445 	 */
3446 	limit = intel_limit(crtc, refclk);
3447 	ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
3448 			     &clock);
3449 	if (!ok) {
3450 		DRM_ERROR("Couldn't find PLL settings for mode!\n");
3451 		return -EINVAL;
3452 	}
3453 
3454 	/* Ensure that the cursor is valid for the new mode before changing... */
3455 	intel_crtc_update_cursor(crtc, true);
3456 
3457 	if (is_lvds && dev_priv->lvds_downclock_avail) {
3458 		/*
3459 		 * Ensure we match the reduced clock's P to the target clock.
3460 		 * If the clocks don't match, we can't switch the display clock
3461 		 * by using the FP0/FP1. In such case we will disable the LVDS
3462 		 * downclock feature.
3463 		*/
3464 		has_reduced_clock = limit->find_pll(limit, crtc,
3465 						    dev_priv->lvds_downclock,
3466 						    refclk,
3467 						    &clock,
3468 						    &reduced_clock);
3469 	}
3470 
3471 	if (is_sdvo && is_tv)
3472 		i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
3473 
3474 	i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
3475 				 &reduced_clock : NULL);
3476 
3477 	dpll = DPLL_VGA_MODE_DIS;
3478 
3479 	if (!IS_GEN2(dev)) {
3480 		if (is_lvds)
3481 			dpll |= DPLLB_MODE_LVDS;
3482 		else
3483 			dpll |= DPLLB_MODE_DAC_SERIAL;
3484 		if (is_sdvo) {
3485 			int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
3486 			if (pixel_multiplier > 1) {
3487 				if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
3488 					dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
3489 			}
3490 			dpll |= DPLL_DVO_HIGH_SPEED;
3491 		}
3492 		if (is_dp)
3493 			dpll |= DPLL_DVO_HIGH_SPEED;
3494 
3495 		/* compute bitmask from p1 value */
3496 		if (IS_PINEVIEW(dev))
3497 			dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
3498 		else {
3499 			dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3500 			if (IS_G4X(dev) && has_reduced_clock)
3501 				dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
3502 		}
3503 		switch (clock.p2) {
3504 		case 5:
3505 			dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
3506 			break;
3507 		case 7:
3508 			dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
3509 			break;
3510 		case 10:
3511 			dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
3512 			break;
3513 		case 14:
3514 			dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
3515 			break;
3516 		}
3517 		if (INTEL_INFO(dev)->gen >= 4)
3518 			dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
3519 	} else {
3520 		if (is_lvds) {
3521 			dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3522 		} else {
3523 			if (clock.p1 == 2)
3524 				dpll |= PLL_P1_DIVIDE_BY_TWO;
3525 			else
3526 				dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3527 			if (clock.p2 == 4)
3528 				dpll |= PLL_P2_DIVIDE_BY_4;
3529 		}
3530 	}
3531 
3532 	if (is_sdvo && is_tv)
3533 		dpll |= PLL_REF_INPUT_TVCLKINBC;
3534 	else if (is_tv)
3535 		/* XXX: just matching BIOS for now */
3536 		/*	dpll |= PLL_REF_INPUT_TVCLKINBC; */
3537 		dpll |= 3;
3538 	else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
3539 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
3540 	else
3541 		dpll |= PLL_REF_INPUT_DREFCLK;
3542 
3543 	/* setup pipeconf */
3544 	pipeconf = I915_READ(PIPECONF(pipe));
3545 
3546 	/* Set up the display plane register */
3547 	dspcntr = DISPPLANE_GAMMA_ENABLE;
3548 
3549 	if (pipe == 0)
3550 		dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
3551 	else
3552 		dspcntr |= DISPPLANE_SEL_PIPE_B;
3553 
3554 	if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
3555 		/* Enable pixel doubling when the dot clock is > 90% of the (display)
3556 		 * core speed.
3557 		 *
3558 		 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
3559 		 * pipe == 0 check?
3560 		 */
3561 		if (mode->clock >
3562 		    dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
3563 			pipeconf |= PIPECONF_DOUBLE_WIDE;
3564 		else
3565 			pipeconf &= ~PIPECONF_DOUBLE_WIDE;
3566 	}
3567 
3568 	/* default to 8bpc */
3569 	pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
3570 	if (is_dp) {
3571 		if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
3572 			pipeconf |= PIPECONF_BPP_6 |
3573 				    PIPECONF_DITHER_EN |
3574 				    PIPECONF_DITHER_TYPE_SP;
3575 		}
3576 	}
3577 
3578 	dpll |= DPLL_VCO_ENABLE;
3579 
3580 	DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
3581 	drm_mode_debug_printmodeline(mode);
3582 
3583 	I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
3584 
3585 	POSTING_READ(DPLL(pipe));
3586 	DELAY(150);
3587 
3588 	/* The LVDS pin pair needs to be on before the DPLLs are enabled.
3589 	 * This is an exception to the general rule that mode_set doesn't turn
3590 	 * things on.
3591 	 */
3592 	if (is_lvds) {
3593 		temp = I915_READ(LVDS);
3594 		temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
3595 		if (pipe == 1) {
3596 			temp |= LVDS_PIPEB_SELECT;
3597 		} else {
3598 			temp &= ~LVDS_PIPEB_SELECT;
3599 		}
3600 		/* set the corresponsding LVDS_BORDER bit */
3601 		temp |= dev_priv->lvds_border_bits;
3602 		/* Set the B0-B3 data pairs corresponding to whether we're going to
3603 		 * set the DPLLs for dual-channel mode or not.
3604 		 */
3605 		if (clock.p2 == 7)
3606 			temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
3607 		else
3608 			temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
3609 
3610 		/* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
3611 		 * appropriately here, but we need to look more thoroughly into how
3612 		 * panels behave in the two modes.
3613 		 */
3614 		/* set the dithering flag on LVDS as needed */
3615 		if (INTEL_INFO(dev)->gen >= 4) {
3616 			if (dev_priv->lvds_dither)
3617 				temp |= LVDS_ENABLE_DITHER;
3618 			else
3619 				temp &= ~LVDS_ENABLE_DITHER;
3620 		}
3621 		if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
3622 			lvds_sync |= LVDS_HSYNC_POLARITY;
3623 		if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
3624 			lvds_sync |= LVDS_VSYNC_POLARITY;
3625 		if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
3626 		    != lvds_sync) {
3627 			char flags[2] = "-+";
3628 			DRM_INFO("Changing LVDS panel from "
3629 				 "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
3630 				 flags[!(temp & LVDS_HSYNC_POLARITY)],
3631 				 flags[!(temp & LVDS_VSYNC_POLARITY)],
3632 				 flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
3633 				 flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
3634 			temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
3635 			temp |= lvds_sync;
3636 		}
3637 		I915_WRITE(LVDS, temp);
3638 	}
3639 
3640 	if (is_dp) {
3641 		intel_dp_set_m_n(crtc, mode, adjusted_mode);
3642 	}
3643 
3644 	I915_WRITE(DPLL(pipe), dpll);
3645 
3646 	/* Wait for the clocks to stabilize. */
3647 	POSTING_READ(DPLL(pipe));
3648 	DELAY(150);
3649 
3650 	if (INTEL_INFO(dev)->gen >= 4) {
3651 		temp = 0;
3652 		if (is_sdvo) {
3653 			temp = intel_mode_get_pixel_multiplier(adjusted_mode);
3654 			if (temp > 1)
3655 				temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
3656 			else
3657 				temp = 0;
3658 		}
3659 		I915_WRITE(DPLL_MD(pipe), temp);
3660 	} else {
3661 		/* The pixel multiplier can only be updated once the
3662 		 * DPLL is enabled and the clocks are stable.
3663 		 *
3664 		 * So write it again.
3665 		 */
3666 		I915_WRITE(DPLL(pipe), dpll);
3667 	}
3668 
3669 	if (HAS_PIPE_CXSR(dev)) {
3670 		if (intel_crtc->lowfreq_avail) {
3671 			DRM_DEBUG_KMS("enabling CxSR downclocking\n");
3672 			pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
3673 		} else {
3674 			DRM_DEBUG_KMS("disabling CxSR downclocking\n");
3675 			pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
3676 		}
3677 	}
3678 
3679 	pipeconf &= ~PIPECONF_INTERLACE_MASK;
3680 	if (!IS_GEN2(dev) &&
3681 	    adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
3682 		pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
3683 		/* the chip adds 2 halflines automatically */
3684 		adjusted_mode->crtc_vtotal -= 1;
3685 		adjusted_mode->crtc_vblank_end -= 1;
3686 		vsyncshift = adjusted_mode->crtc_hsync_start
3687 			     - adjusted_mode->crtc_htotal/2;
3688 	} else {
3689 		pipeconf |= PIPECONF_PROGRESSIVE;
3690 		vsyncshift = 0;
3691 	}
3692 
3693 	if (!IS_GEN3(dev))
3694 		I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
3695 
3696 	I915_WRITE(HTOTAL(pipe),
3697 		   (adjusted_mode->crtc_hdisplay - 1) |
3698 		   ((adjusted_mode->crtc_htotal - 1) << 16));
3699 	I915_WRITE(HBLANK(pipe),
3700 		   (adjusted_mode->crtc_hblank_start - 1) |
3701 		   ((adjusted_mode->crtc_hblank_end - 1) << 16));
3702 	I915_WRITE(HSYNC(pipe),
3703 		   (adjusted_mode->crtc_hsync_start - 1) |
3704 		   ((adjusted_mode->crtc_hsync_end - 1) << 16));
3705 
3706 	I915_WRITE(VTOTAL(pipe),
3707 		   (adjusted_mode->crtc_vdisplay - 1) |
3708 		   ((adjusted_mode->crtc_vtotal - 1) << 16));
3709 	I915_WRITE(VBLANK(pipe),
3710 		   (adjusted_mode->crtc_vblank_start - 1) |
3711 		   ((adjusted_mode->crtc_vblank_end - 1) << 16));
3712 	I915_WRITE(VSYNC(pipe),
3713 		   (adjusted_mode->crtc_vsync_start - 1) |
3714 		   ((adjusted_mode->crtc_vsync_end - 1) << 16));
3715 
3716 	/* pipesrc and dspsize control the size that is scaled from,
3717 	 * which should always be the user's requested size.
3718 	 */
3719 	I915_WRITE(DSPSIZE(plane),
3720 		   ((mode->vdisplay - 1) << 16) |
3721 		   (mode->hdisplay - 1));
3722 	I915_WRITE(DSPPOS(plane), 0);
3723 	I915_WRITE(PIPESRC(pipe),
3724 		   ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
3725 
3726 	I915_WRITE(PIPECONF(pipe), pipeconf);
3727 	POSTING_READ(PIPECONF(pipe));
3728 	intel_enable_pipe(dev_priv, pipe, false);
3729 
3730 	intel_wait_for_vblank(dev, pipe);
3731 
3732 	I915_WRITE(DSPCNTR(plane), dspcntr);
3733 	POSTING_READ(DSPCNTR(plane));
3734 	intel_enable_plane(dev_priv, plane, pipe);
3735 
3736 	ret = intel_pipe_set_base(crtc, x, y, old_fb);
3737 
3738 	intel_update_watermarks(dev);
3739 
3740 	return ret;
3741 }
3742 
3743 /*
3744  * Initialize reference clocks when the driver loads
3745  */
3746 void ironlake_init_pch_refclk(struct drm_device *dev)
3747 {
3748 	struct drm_i915_private *dev_priv = dev->dev_private;
3749 	struct drm_mode_config *mode_config = &dev->mode_config;
3750 	struct intel_encoder *encoder;
3751 	u32 temp;
3752 	bool has_lvds = false;
3753 	bool has_cpu_edp = false;
3754 	bool has_pch_edp = false;
3755 	bool has_panel = false;
3756 	bool has_ck505 = false;
3757 	bool can_ssc = false;
3758 
3759 	/* We need to take the global config into account */
3760 	list_for_each_entry(encoder, &mode_config->encoder_list,
3761 			    base.head) {
3762 		switch (encoder->type) {
3763 		case INTEL_OUTPUT_LVDS:
3764 			has_panel = true;
3765 			has_lvds = true;
3766 			break;
3767 		case INTEL_OUTPUT_EDP:
3768 			has_panel = true;
3769 			if (intel_encoder_is_pch_edp(&encoder->base))
3770 				has_pch_edp = true;
3771 			else
3772 				has_cpu_edp = true;
3773 			break;
3774 		}
3775 	}
3776 
3777 	if (HAS_PCH_IBX(dev)) {
3778 		has_ck505 = dev_priv->display_clock_mode;
3779 		can_ssc = has_ck505;
3780 	} else {
3781 		has_ck505 = false;
3782 		can_ssc = true;
3783 	}
3784 
3785 	DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
3786 		      has_panel, has_lvds, has_pch_edp, has_cpu_edp,
3787 		      has_ck505);
3788 
3789 	/* Ironlake: try to setup display ref clock before DPLL
3790 	 * enabling. This is only under driver's control after
3791 	 * PCH B stepping, previous chipset stepping should be
3792 	 * ignoring this setting.
3793 	 */
3794 	temp = I915_READ(PCH_DREF_CONTROL);
3795 	/* Always enable nonspread source */
3796 	temp &= ~DREF_NONSPREAD_SOURCE_MASK;
3797 
3798 	if (has_ck505)
3799 		temp |= DREF_NONSPREAD_CK505_ENABLE;
3800 	else
3801 		temp |= DREF_NONSPREAD_SOURCE_ENABLE;
3802 
3803 	if (has_panel) {
3804 		temp &= ~DREF_SSC_SOURCE_MASK;
3805 		temp |= DREF_SSC_SOURCE_ENABLE;
3806 
3807 		/* SSC must be turned on before enabling the CPU output  */
3808 		if (intel_panel_use_ssc(dev_priv) && can_ssc) {
3809 			DRM_DEBUG_KMS("Using SSC on panel\n");
3810 			temp |= DREF_SSC1_ENABLE;
3811 		} else
3812 			temp &= ~DREF_SSC1_ENABLE;
3813 
3814 		/* Get SSC going before enabling the outputs */
3815 		I915_WRITE(PCH_DREF_CONTROL, temp);
3816 		POSTING_READ(PCH_DREF_CONTROL);
3817 		DELAY(200);
3818 
3819 		temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
3820 
3821 		/* Enable CPU source on CPU attached eDP */
3822 		if (has_cpu_edp) {
3823 			if (intel_panel_use_ssc(dev_priv) && can_ssc) {
3824 				DRM_DEBUG_KMS("Using SSC on eDP\n");
3825 				temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
3826 			}
3827 			else
3828 				temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
3829 		} else
3830 			temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
3831 
3832 		I915_WRITE(PCH_DREF_CONTROL, temp);
3833 		POSTING_READ(PCH_DREF_CONTROL);
3834 		DELAY(200);
3835 	} else {
3836 		DRM_DEBUG_KMS("Disabling SSC entirely\n");
3837 
3838 		temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
3839 
3840 		/* Turn off CPU output */
3841 		temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
3842 
3843 		I915_WRITE(PCH_DREF_CONTROL, temp);
3844 		POSTING_READ(PCH_DREF_CONTROL);
3845 		DELAY(200);
3846 
3847 		/* Turn off the SSC source */
3848 		temp &= ~DREF_SSC_SOURCE_MASK;
3849 		temp |= DREF_SSC_SOURCE_DISABLE;
3850 
3851 		/* Turn off SSC1 */
3852 		temp &= ~ DREF_SSC1_ENABLE;
3853 
3854 		I915_WRITE(PCH_DREF_CONTROL, temp);
3855 		POSTING_READ(PCH_DREF_CONTROL);
3856 		DELAY(200);
3857 	}
3858 }
3859 
3860 static int ironlake_get_refclk(struct drm_crtc *crtc)
3861 {
3862 	struct drm_device *dev = crtc->dev;
3863 	struct drm_i915_private *dev_priv = dev->dev_private;
3864 	struct intel_encoder *encoder;
3865 	struct drm_mode_config *mode_config = &dev->mode_config;
3866 	struct intel_encoder *edp_encoder = NULL;
3867 	int num_connectors = 0;
3868 	bool is_lvds = false;
3869 
3870 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3871 		if (encoder->base.crtc != crtc)
3872 			continue;
3873 
3874 		switch (encoder->type) {
3875 		case INTEL_OUTPUT_LVDS:
3876 			is_lvds = true;
3877 			break;
3878 		case INTEL_OUTPUT_EDP:
3879 			edp_encoder = encoder;
3880 			break;
3881 		}
3882 		num_connectors++;
3883 	}
3884 
3885 	if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
3886 		DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
3887 			      dev_priv->lvds_ssc_freq);
3888 		return dev_priv->lvds_ssc_freq * 1000;
3889 	}
3890 
3891 	return 120000;
3892 }
3893 
3894 static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
3895 				  struct drm_display_mode *mode,
3896 				  struct drm_display_mode *adjusted_mode,
3897 				  int x, int y,
3898 				  struct drm_framebuffer *old_fb)
3899 {
3900 	struct drm_device *dev = crtc->dev;
3901 	struct drm_i915_private *dev_priv = dev->dev_private;
3902 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3903 	int pipe = intel_crtc->pipe;
3904 	int plane = intel_crtc->plane;
3905 	int refclk, num_connectors = 0;
3906 	intel_clock_t clock, reduced_clock;
3907 	u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
3908 	bool ok, has_reduced_clock = false, is_sdvo = false;
3909 	bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
3910 	struct intel_encoder *has_edp_encoder = NULL;
3911 	struct drm_mode_config *mode_config = &dev->mode_config;
3912 	struct intel_encoder *encoder;
3913 	const intel_limit_t *limit;
3914 	int ret;
3915 	struct fdi_m_n m_n = {0};
3916 	u32 temp;
3917 	u32 lvds_sync = 0;
3918 	int target_clock, pixel_multiplier, lane, link_bw, factor;
3919 	unsigned int pipe_bpp;
3920 	bool dither;
3921 
3922 	list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3923 		if (encoder->base.crtc != crtc)
3924 			continue;
3925 
3926 		switch (encoder->type) {
3927 		case INTEL_OUTPUT_LVDS:
3928 			is_lvds = true;
3929 			break;
3930 		case INTEL_OUTPUT_SDVO:
3931 		case INTEL_OUTPUT_HDMI:
3932 			is_sdvo = true;
3933 			if (encoder->needs_tv_clock)
3934 				is_tv = true;
3935 			break;
3936 		case INTEL_OUTPUT_TVOUT:
3937 			is_tv = true;
3938 			break;
3939 		case INTEL_OUTPUT_ANALOG:
3940 			is_crt = true;
3941 			break;
3942 		case INTEL_OUTPUT_DISPLAYPORT:
3943 			is_dp = true;
3944 			break;
3945 		case INTEL_OUTPUT_EDP:
3946 			has_edp_encoder = encoder;
3947 			break;
3948 		}
3949 
3950 		num_connectors++;
3951 	}
3952 
3953 	refclk = ironlake_get_refclk(crtc);
3954 
3955 	/*
3956 	 * Returns a set of divisors for the desired target clock with the given
3957 	 * refclk, or false.  The returned values represent the clock equation:
3958 	 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
3959 	 */
3960 	limit = intel_limit(crtc, refclk);
3961 	ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
3962 			     &clock);
3963 	if (!ok) {
3964 		DRM_ERROR("Couldn't find PLL settings for mode!\n");
3965 		return -EINVAL;
3966 	}
3967 
3968 	/* Ensure that the cursor is valid for the new mode before changing... */
3969 	intel_crtc_update_cursor(crtc, true);
3970 
3971 	if (is_lvds && dev_priv->lvds_downclock_avail) {
3972 		/*
3973 		 * Ensure we match the reduced clock's P to the target clock.
3974 		 * If the clocks don't match, we can't switch the display clock
3975 		 * by using the FP0/FP1. In such case we will disable the LVDS
3976 		 * downclock feature.
3977 		*/
3978 		has_reduced_clock = limit->find_pll(limit, crtc,
3979 						    dev_priv->lvds_downclock,
3980 						    refclk,
3981 						    &clock,
3982 						    &reduced_clock);
3983 	}
3984 	/* SDVO TV has fixed PLL values depend on its clock range,
3985 	   this mirrors vbios setting. */
3986 	if (is_sdvo && is_tv) {
3987 		if (adjusted_mode->clock >= 100000
3988 		    && adjusted_mode->clock < 140500) {
3989 			clock.p1 = 2;
3990 			clock.p2 = 10;
3991 			clock.n = 3;
3992 			clock.m1 = 16;
3993 			clock.m2 = 8;
3994 		} else if (adjusted_mode->clock >= 140500
3995 			   && adjusted_mode->clock <= 200000) {
3996 			clock.p1 = 1;
3997 			clock.p2 = 10;
3998 			clock.n = 6;
3999 			clock.m1 = 12;
4000 			clock.m2 = 8;
4001 		}
4002 	}
4003 
4004 	/* FDI link */
4005 	pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4006 	lane = 0;
4007 	/* CPU eDP doesn't require FDI link, so just set DP M/N
4008 	   according to current link config */
4009 	if (has_edp_encoder &&
4010 	    !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
4011 		target_clock = mode->clock;
4012 		intel_edp_link_config(has_edp_encoder,
4013 				      &lane, &link_bw);
4014 	} else {
4015 		/* [e]DP over FDI requires target mode clock
4016 		   instead of link clock */
4017 		if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
4018 			target_clock = mode->clock;
4019 		else
4020 			target_clock = adjusted_mode->clock;
4021 
4022 		/* FDI is a binary signal running at ~2.7GHz, encoding
4023 		 * each output octet as 10 bits. The actual frequency
4024 		 * is stored as a divider into a 100MHz clock, and the
4025 		 * mode pixel clock is stored in units of 1KHz.
4026 		 * Hence the bw of each lane in terms of the mode signal
4027 		 * is:
4028 		 */
4029 		link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
4030 	}
4031 
4032 	/* determine panel color depth */
4033 	temp = I915_READ(PIPECONF(pipe));
4034 	temp &= ~PIPE_BPC_MASK;
4035 	dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
4036 	switch (pipe_bpp) {
4037 	case 18:
4038 		temp |= PIPE_6BPC;
4039 		break;
4040 	case 24:
4041 		temp |= PIPE_8BPC;
4042 		break;
4043 	case 30:
4044 		temp |= PIPE_10BPC;
4045 		break;
4046 	case 36:
4047 		temp |= PIPE_12BPC;
4048 		break;
4049 	default:
4050 		kprintf("intel_choose_pipe_bpp returned invalid value %d\n",
4051 			pipe_bpp);
4052 		temp |= PIPE_8BPC;
4053 		pipe_bpp = 24;
4054 		break;
4055 	}
4056 
4057 	intel_crtc->bpp = pipe_bpp;
4058 	I915_WRITE(PIPECONF(pipe), temp);
4059 
4060 	if (!lane) {
4061 		/*
4062 		 * Account for spread spectrum to avoid
4063 		 * oversubscribing the link. Max center spread
4064 		 * is 2.5%; use 5% for safety's sake.
4065 		 */
4066 		u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
4067 		lane = bps / (link_bw * 8) + 1;
4068 	}
4069 
4070 	intel_crtc->fdi_lanes = lane;
4071 
4072 	if (pixel_multiplier > 1)
4073 		link_bw *= pixel_multiplier;
4074 	ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
4075 			     &m_n);
4076 
4077 	fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
4078 	if (has_reduced_clock)
4079 		fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
4080 			reduced_clock.m2;
4081 
4082 	/* Enable autotuning of the PLL clock (if permissible) */
4083 	factor = 21;
4084 	if (is_lvds) {
4085 		if ((intel_panel_use_ssc(dev_priv) &&
4086 		     dev_priv->lvds_ssc_freq == 100) ||
4087 		    (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
4088 			factor = 25;
4089 	} else if (is_sdvo && is_tv)
4090 		factor = 20;
4091 
4092 	if (clock.m < factor * clock.n)
4093 		fp |= FP_CB_TUNE;
4094 
4095 	dpll = 0;
4096 
4097 	if (is_lvds)
4098 		dpll |= DPLLB_MODE_LVDS;
4099 	else
4100 		dpll |= DPLLB_MODE_DAC_SERIAL;
4101 	if (is_sdvo) {
4102 		int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4103 		if (pixel_multiplier > 1) {
4104 			dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
4105 		}
4106 		dpll |= DPLL_DVO_HIGH_SPEED;
4107 	}
4108 	if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
4109 		dpll |= DPLL_DVO_HIGH_SPEED;
4110 
4111 	/* compute bitmask from p1 value */
4112 	dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4113 	/* also FPA1 */
4114 	dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4115 
4116 	switch (clock.p2) {
4117 	case 5:
4118 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4119 		break;
4120 	case 7:
4121 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4122 		break;
4123 	case 10:
4124 		dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4125 		break;
4126 	case 14:
4127 		dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4128 		break;
4129 	}
4130 
4131 	if (is_sdvo && is_tv)
4132 		dpll |= PLL_REF_INPUT_TVCLKINBC;
4133 	else if (is_tv)
4134 		/* XXX: just matching BIOS for now */
4135 		/*	dpll |= PLL_REF_INPUT_TVCLKINBC; */
4136 		dpll |= 3;
4137 	else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4138 		dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4139 	else
4140 		dpll |= PLL_REF_INPUT_DREFCLK;
4141 
4142 	/* setup pipeconf */
4143 	pipeconf = I915_READ(PIPECONF(pipe));
4144 
4145 	/* Set up the display plane register */
4146 	dspcntr = DISPPLANE_GAMMA_ENABLE;
4147 
4148 	DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
4149 	drm_mode_debug_printmodeline(mode);
4150 
4151 	/* PCH eDP needs FDI, but CPU eDP does not */
4152 	if (!intel_crtc->no_pll) {
4153 		if (!has_edp_encoder ||
4154 		    intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
4155 			I915_WRITE(_PCH_FP0(pipe), fp);
4156 			I915_WRITE(_PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4157 
4158 			POSTING_READ(_PCH_DPLL(pipe));
4159 			DELAY(150);
4160 		}
4161 	} else {
4162 		if (dpll == (I915_READ(_PCH_DPLL(0)) & 0x7fffffff) &&
4163 		    fp == I915_READ(_PCH_FP0(0))) {
4164 			intel_crtc->use_pll_a = true;
4165 			DRM_DEBUG_KMS("using pipe a dpll\n");
4166 		} else if (dpll == (I915_READ(_PCH_DPLL(1)) & 0x7fffffff) &&
4167 			   fp == I915_READ(_PCH_FP0(1))) {
4168 			intel_crtc->use_pll_a = false;
4169 			DRM_DEBUG_KMS("using pipe b dpll\n");
4170 		} else {
4171 			DRM_DEBUG_KMS("no matching PLL configuration for pipe 2\n");
4172 			return -EINVAL;
4173 		}
4174 	}
4175 
4176 	/* The LVDS pin pair needs to be on before the DPLLs are enabled.
4177 	 * This is an exception to the general rule that mode_set doesn't turn
4178 	 * things on.
4179 	 */
4180 	if (is_lvds) {
4181 		temp = I915_READ(PCH_LVDS);
4182 		temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
4183 		if (HAS_PCH_CPT(dev)) {
4184 			temp &= ~PORT_TRANS_SEL_MASK;
4185 			temp |= PORT_TRANS_SEL_CPT(pipe);
4186 		} else {
4187 			if (pipe == 1)
4188 				temp |= LVDS_PIPEB_SELECT;
4189 			else
4190 				temp &= ~LVDS_PIPEB_SELECT;
4191 		}
4192 
4193 		/* set the corresponsding LVDS_BORDER bit */
4194 		temp |= dev_priv->lvds_border_bits;
4195 		/* Set the B0-B3 data pairs corresponding to whether we're going to
4196 		 * set the DPLLs for dual-channel mode or not.
4197 		 */
4198 		if (clock.p2 == 7)
4199 			temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
4200 		else
4201 			temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
4202 
4203 		/* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
4204 		 * appropriately here, but we need to look more thoroughly into how
4205 		 * panels behave in the two modes.
4206 		 */
4207 		if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
4208 			lvds_sync |= LVDS_HSYNC_POLARITY;
4209 		if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
4210 			lvds_sync |= LVDS_VSYNC_POLARITY;
4211 		if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
4212 		    != lvds_sync) {
4213 			char flags[2] = "-+";
4214 			DRM_INFO("Changing LVDS panel from "
4215 				 "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
4216 				 flags[!(temp & LVDS_HSYNC_POLARITY)],
4217 				 flags[!(temp & LVDS_VSYNC_POLARITY)],
4218 				 flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
4219 				 flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
4220 			temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
4221 			temp |= lvds_sync;
4222 		}
4223 		I915_WRITE(PCH_LVDS, temp);
4224 	}
4225 
4226 	pipeconf &= ~PIPECONF_DITHER_EN;
4227 	pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
4228 	if ((is_lvds && dev_priv->lvds_dither) || dither) {
4229 		pipeconf |= PIPECONF_DITHER_EN;
4230 		pipeconf |= PIPECONF_DITHER_TYPE_SP;
4231 	}
4232 	if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
4233 		intel_dp_set_m_n(crtc, mode, adjusted_mode);
4234 	} else {
4235 		/* For non-DP output, clear any trans DP clock recovery setting.*/
4236 		I915_WRITE(TRANSDATA_M1(pipe), 0);
4237 		I915_WRITE(TRANSDATA_N1(pipe), 0);
4238 		I915_WRITE(TRANSDPLINK_M1(pipe), 0);
4239 		I915_WRITE(TRANSDPLINK_N1(pipe), 0);
4240 	}
4241 
4242 	if (!intel_crtc->no_pll &&
4243 	    (!has_edp_encoder ||
4244 	     intel_encoder_is_pch_edp(&has_edp_encoder->base))) {
4245 		I915_WRITE(_PCH_DPLL(pipe), dpll);
4246 
4247 		/* Wait for the clocks to stabilize. */
4248 		POSTING_READ(_PCH_DPLL(pipe));
4249 		DELAY(150);
4250 
4251 		/* The pixel multiplier can only be updated once the
4252 		 * DPLL is enabled and the clocks are stable.
4253 		 *
4254 		 * So write it again.
4255 		 */
4256 		I915_WRITE(_PCH_DPLL(pipe), dpll);
4257 	}
4258 
4259 	intel_crtc->lowfreq_avail = false;
4260 	if (!intel_crtc->no_pll) {
4261 		if (is_lvds && has_reduced_clock && i915_powersave) {
4262 			I915_WRITE(_PCH_FP1(pipe), fp2);
4263 			intel_crtc->lowfreq_avail = true;
4264 			if (HAS_PIPE_CXSR(dev)) {
4265 				DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4266 				pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4267 			}
4268 		} else {
4269 			I915_WRITE(_PCH_FP1(pipe), fp);
4270 			if (HAS_PIPE_CXSR(dev)) {
4271 				DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4272 				pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4273 			}
4274 		}
4275 	}
4276 
4277 	pipeconf &= ~PIPECONF_INTERLACE_MASK;
4278 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4279 		pipeconf |= PIPECONF_INTERLACED_ILK;
4280 		/* the chip adds 2 halflines automatically */
4281 		adjusted_mode->crtc_vtotal -= 1;
4282 		adjusted_mode->crtc_vblank_end -= 1;
4283 		I915_WRITE(VSYNCSHIFT(pipe),
4284 			   adjusted_mode->crtc_hsync_start
4285 			   - adjusted_mode->crtc_htotal/2);
4286 	} else {
4287 		pipeconf |= PIPECONF_PROGRESSIVE;
4288 		I915_WRITE(VSYNCSHIFT(pipe), 0);
4289 	}
4290 
4291 	I915_WRITE(HTOTAL(pipe),
4292 		   (adjusted_mode->crtc_hdisplay - 1) |
4293 		   ((adjusted_mode->crtc_htotal - 1) << 16));
4294 	I915_WRITE(HBLANK(pipe),
4295 		   (adjusted_mode->crtc_hblank_start - 1) |
4296 		   ((adjusted_mode->crtc_hblank_end - 1) << 16));
4297 	I915_WRITE(HSYNC(pipe),
4298 		   (adjusted_mode->crtc_hsync_start - 1) |
4299 		   ((adjusted_mode->crtc_hsync_end - 1) << 16));
4300 
4301 	I915_WRITE(VTOTAL(pipe),
4302 		   (adjusted_mode->crtc_vdisplay - 1) |
4303 		   ((adjusted_mode->crtc_vtotal - 1) << 16));
4304 	I915_WRITE(VBLANK(pipe),
4305 		   (adjusted_mode->crtc_vblank_start - 1) |
4306 		   ((adjusted_mode->crtc_vblank_end - 1) << 16));
4307 	I915_WRITE(VSYNC(pipe),
4308 		   (adjusted_mode->crtc_vsync_start - 1) |
4309 		   ((adjusted_mode->crtc_vsync_end - 1) << 16));
4310 
4311 	/* pipesrc controls the size that is scaled from, which should
4312 	 * always be the user's requested size.
4313 	 */
4314 	I915_WRITE(PIPESRC(pipe),
4315 		   ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4316 
4317 	I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
4318 	I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
4319 	I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
4320 	I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
4321 
4322 	if (has_edp_encoder &&
4323 	    !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
4324 		ironlake_set_pll_edp(crtc, adjusted_mode->clock);
4325 	}
4326 
4327 	I915_WRITE(PIPECONF(pipe), pipeconf);
4328 	POSTING_READ(PIPECONF(pipe));
4329 
4330 	intel_wait_for_vblank(dev, pipe);
4331 
4332 	I915_WRITE(DSPCNTR(plane), dspcntr);
4333 	POSTING_READ(DSPCNTR(plane));
4334 
4335 	ret = intel_pipe_set_base(crtc, x, y, old_fb);
4336 
4337 	intel_update_watermarks(dev);
4338 
4339 	return ret;
4340 }
4341 
4342 static int intel_crtc_mode_set(struct drm_crtc *crtc,
4343 			       struct drm_display_mode *mode,
4344 			       struct drm_display_mode *adjusted_mode,
4345 			       int x, int y,
4346 			       struct drm_framebuffer *old_fb)
4347 {
4348 	struct drm_device *dev = crtc->dev;
4349 	struct drm_i915_private *dev_priv = dev->dev_private;
4350 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4351 	int pipe = intel_crtc->pipe;
4352 	int ret;
4353 
4354 	drm_vblank_pre_modeset(dev, pipe);
4355 
4356 	ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
4357 					      x, y, old_fb);
4358 	drm_vblank_post_modeset(dev, pipe);
4359 
4360 	if (ret)
4361 		intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
4362 	else
4363 		intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
4364 
4365 	return ret;
4366 }
4367 
4368 static bool intel_eld_uptodate(struct drm_connector *connector,
4369 			       int reg_eldv, uint32_t bits_eldv,
4370 			       int reg_elda, uint32_t bits_elda,
4371 			       int reg_edid)
4372 {
4373 	struct drm_i915_private *dev_priv = connector->dev->dev_private;
4374 	uint8_t *eld = connector->eld;
4375 	uint32_t i;
4376 
4377 	i = I915_READ(reg_eldv);
4378 	i &= bits_eldv;
4379 
4380 	if (!eld[0])
4381 		return !i;
4382 
4383 	if (!i)
4384 		return false;
4385 
4386 	i = I915_READ(reg_elda);
4387 	i &= ~bits_elda;
4388 	I915_WRITE(reg_elda, i);
4389 
4390 	for (i = 0; i < eld[2]; i++)
4391 		if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
4392 			return false;
4393 
4394 	return true;
4395 }
4396 
4397 static void g4x_write_eld(struct drm_connector *connector,
4398 			  struct drm_crtc *crtc)
4399 {
4400 	struct drm_i915_private *dev_priv = connector->dev->dev_private;
4401 	uint8_t *eld = connector->eld;
4402 	uint32_t eldv;
4403 	uint32_t len;
4404 	uint32_t i;
4405 
4406 	i = I915_READ(G4X_AUD_VID_DID);
4407 
4408 	if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
4409 		eldv = G4X_ELDV_DEVCL_DEVBLC;
4410 	else
4411 		eldv = G4X_ELDV_DEVCTG;
4412 
4413 	if (intel_eld_uptodate(connector,
4414 			       G4X_AUD_CNTL_ST, eldv,
4415 			       G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
4416 			       G4X_HDMIW_HDMIEDID))
4417 		return;
4418 
4419 	i = I915_READ(G4X_AUD_CNTL_ST);
4420 	i &= ~(eldv | G4X_ELD_ADDR);
4421 	len = (i >> 9) & 0x1f;		/* ELD buffer size */
4422 	I915_WRITE(G4X_AUD_CNTL_ST, i);
4423 
4424 	if (!eld[0])
4425 		return;
4426 
4427 	if (eld[2] < (uint8_t)len)
4428 		len = eld[2];
4429 	DRM_DEBUG_KMS("ELD size %d\n", len);
4430 	for (i = 0; i < len; i++)
4431 		I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
4432 
4433 	i = I915_READ(G4X_AUD_CNTL_ST);
4434 	i |= eldv;
4435 	I915_WRITE(G4X_AUD_CNTL_ST, i);
4436 }
4437 
4438 static void ironlake_write_eld(struct drm_connector *connector,
4439 				     struct drm_crtc *crtc)
4440 {
4441 	struct drm_i915_private *dev_priv = connector->dev->dev_private;
4442 	uint8_t *eld = connector->eld;
4443 	uint32_t eldv;
4444 	uint32_t i;
4445 	int len;
4446 	int hdmiw_hdmiedid;
4447 	int aud_config;
4448 	int aud_cntl_st;
4449 	int aud_cntrl_st2;
4450 
4451 	if (HAS_PCH_IBX(connector->dev)) {
4452 		hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
4453 		aud_config = IBX_AUD_CONFIG_A;
4454 		aud_cntl_st = IBX_AUD_CNTL_ST_A;
4455 		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
4456 	} else {
4457 		hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
4458 		aud_config = CPT_AUD_CONFIG_A;
4459 		aud_cntl_st = CPT_AUD_CNTL_ST_A;
4460 		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
4461 	}
4462 
4463 	i = to_intel_crtc(crtc)->pipe;
4464 	hdmiw_hdmiedid += i * 0x100;
4465 	aud_cntl_st += i * 0x100;
4466 	aud_config += i * 0x100;
4467 
4468 	DRM_DEBUG_KMS("ELD on pipe %c\n", pipe_name(i));
4469 
4470 	i = I915_READ(aud_cntl_st);
4471 	i = (i >> 29) & 0x3;		/* DIP_Port_Select, 0x1 = PortB */
4472 	if (!i) {
4473 		DRM_DEBUG_KMS("Audio directed to unknown port\n");
4474 		/* operate blindly on all ports */
4475 		eldv = IBX_ELD_VALIDB;
4476 		eldv |= IBX_ELD_VALIDB << 4;
4477 		eldv |= IBX_ELD_VALIDB << 8;
4478 	} else {
4479 		DRM_DEBUG_KMS("ELD on port %c\n", 'A' + i);
4480 		eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
4481 	}
4482 
4483 	if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
4484 		DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
4485 		eld[5] |= (1 << 2);	/* Conn_Type, 0x1 = DisplayPort */
4486 		I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
4487 	} else
4488 		I915_WRITE(aud_config, 0);
4489 
4490 	if (intel_eld_uptodate(connector,
4491 			       aud_cntrl_st2, eldv,
4492 			       aud_cntl_st, IBX_ELD_ADDRESS,
4493 			       hdmiw_hdmiedid))
4494 		return;
4495 
4496 	i = I915_READ(aud_cntrl_st2);
4497 	i &= ~eldv;
4498 	I915_WRITE(aud_cntrl_st2, i);
4499 
4500 	if (!eld[0])
4501 		return;
4502 
4503 	i = I915_READ(aud_cntl_st);
4504 	i &= ~IBX_ELD_ADDRESS;
4505 	I915_WRITE(aud_cntl_st, i);
4506 
4507 	/* 84 bytes of hw ELD buffer */
4508 	len = 21;
4509 	if (eld[2] < (uint8_t)len)
4510 		len = eld[2];
4511 	DRM_DEBUG_KMS("ELD size %d\n", len);
4512 	for (i = 0; i < len; i++)
4513 		I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
4514 
4515 	i = I915_READ(aud_cntrl_st2);
4516 	i |= eldv;
4517 	I915_WRITE(aud_cntrl_st2, i);
4518 }
4519 
4520 void intel_write_eld(struct drm_encoder *encoder,
4521 		     struct drm_display_mode *mode)
4522 {
4523 	struct drm_crtc *crtc = encoder->crtc;
4524 	struct drm_connector *connector;
4525 	struct drm_device *dev = encoder->dev;
4526 	struct drm_i915_private *dev_priv = dev->dev_private;
4527 
4528 	connector = drm_select_eld(encoder, mode);
4529 	if (!connector)
4530 		return;
4531 
4532 	DRM_DEBUG_KMS("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
4533 			 connector->base.id,
4534 			 drm_get_connector_name(connector),
4535 			 connector->encoder->base.id,
4536 			 drm_get_encoder_name(connector->encoder));
4537 
4538 	connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
4539 
4540 	if (dev_priv->display.write_eld)
4541 		dev_priv->display.write_eld(connector, crtc);
4542 }
4543 
4544 /** Loads the palette/gamma unit for the CRTC with the prepared values */
4545 void intel_crtc_load_lut(struct drm_crtc *crtc)
4546 {
4547 	struct drm_device *dev = crtc->dev;
4548 	struct drm_i915_private *dev_priv = dev->dev_private;
4549 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4550 	int palreg = PALETTE(intel_crtc->pipe);
4551 	int i;
4552 
4553 	/* The clocks have to be on to load the palette. */
4554 	if (!crtc->enabled || !intel_crtc->active)
4555 		return;
4556 
4557 	/* use legacy palette for Ironlake */
4558 	if (HAS_PCH_SPLIT(dev))
4559 		palreg = LGC_PALETTE(intel_crtc->pipe);
4560 
4561 	for (i = 0; i < 256; i++) {
4562 		I915_WRITE(palreg + 4 * i,
4563 			   (intel_crtc->lut_r[i] << 16) |
4564 			   (intel_crtc->lut_g[i] << 8) |
4565 			   intel_crtc->lut_b[i]);
4566 	}
4567 }
4568 
4569 static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
4570 {
4571 	struct drm_device *dev = crtc->dev;
4572 	struct drm_i915_private *dev_priv = dev->dev_private;
4573 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4574 	bool visible = base != 0;
4575 	u32 cntl;
4576 
4577 	if (intel_crtc->cursor_visible == visible)
4578 		return;
4579 
4580 	cntl = I915_READ(_CURACNTR);
4581 	if (visible) {
4582 		/* On these chipsets we can only modify the base whilst
4583 		 * the cursor is disabled.
4584 		 */
4585 		I915_WRITE(_CURABASE, base);
4586 
4587 		cntl &= ~(CURSOR_FORMAT_MASK);
4588 		/* XXX width must be 64, stride 256 => 0x00 << 28 */
4589 		cntl |= CURSOR_ENABLE |
4590 			CURSOR_GAMMA_ENABLE |
4591 			CURSOR_FORMAT_ARGB;
4592 	} else
4593 		cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
4594 	I915_WRITE(_CURACNTR, cntl);
4595 
4596 	intel_crtc->cursor_visible = visible;
4597 }
4598 
4599 static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
4600 {
4601 	struct drm_device *dev = crtc->dev;
4602 	struct drm_i915_private *dev_priv = dev->dev_private;
4603 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4604 	int pipe = intel_crtc->pipe;
4605 	bool visible = base != 0;
4606 
4607 	if (intel_crtc->cursor_visible != visible) {
4608 		uint32_t cntl = I915_READ(CURCNTR(pipe));
4609 		if (base) {
4610 			cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
4611 			cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
4612 			cntl |= pipe << 28; /* Connect to correct pipe */
4613 		} else {
4614 			cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
4615 			cntl |= CURSOR_MODE_DISABLE;
4616 		}
4617 		I915_WRITE(CURCNTR(pipe), cntl);
4618 
4619 		intel_crtc->cursor_visible = visible;
4620 	}
4621 	/* and commit changes on next vblank */
4622 	I915_WRITE(CURBASE(pipe), base);
4623 }
4624 
4625 static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
4626 {
4627 	struct drm_device *dev = crtc->dev;
4628 	struct drm_i915_private *dev_priv = dev->dev_private;
4629 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4630 	int pipe = intel_crtc->pipe;
4631 	bool visible = base != 0;
4632 
4633 	if (intel_crtc->cursor_visible != visible) {
4634 		uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
4635 		if (base) {
4636 			cntl &= ~CURSOR_MODE;
4637 			cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
4638 		} else {
4639 			cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
4640 			cntl |= CURSOR_MODE_DISABLE;
4641 		}
4642 		I915_WRITE(CURCNTR_IVB(pipe), cntl);
4643 
4644 		intel_crtc->cursor_visible = visible;
4645 	}
4646 	/* and commit changes on next vblank */
4647 	I915_WRITE(CURBASE_IVB(pipe), base);
4648 }
4649 
4650 /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
4651 static void intel_crtc_update_cursor(struct drm_crtc *crtc,
4652 				     bool on)
4653 {
4654 	struct drm_device *dev = crtc->dev;
4655 	struct drm_i915_private *dev_priv = dev->dev_private;
4656 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4657 	int pipe = intel_crtc->pipe;
4658 	int x = intel_crtc->cursor_x;
4659 	int y = intel_crtc->cursor_y;
4660 	u32 base, pos;
4661 	bool visible;
4662 
4663 	pos = 0;
4664 
4665 	if (on && crtc->enabled && crtc->fb) {
4666 		base = intel_crtc->cursor_addr;
4667 		if (x > (int) crtc->fb->width)
4668 			base = 0;
4669 
4670 		if (y > (int) crtc->fb->height)
4671 			base = 0;
4672 	} else
4673 		base = 0;
4674 
4675 	if (x < 0) {
4676 		if (x + intel_crtc->cursor_width < 0)
4677 			base = 0;
4678 
4679 		pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
4680 		x = -x;
4681 	}
4682 	pos |= x << CURSOR_X_SHIFT;
4683 
4684 	if (y < 0) {
4685 		if (y + intel_crtc->cursor_height < 0)
4686 			base = 0;
4687 
4688 		pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
4689 		y = -y;
4690 	}
4691 	pos |= y << CURSOR_Y_SHIFT;
4692 
4693 	visible = base != 0;
4694 	if (!visible && !intel_crtc->cursor_visible)
4695 		return;
4696 
4697 	if (IS_IVYBRIDGE(dev)) {
4698 		I915_WRITE(CURPOS_IVB(pipe), pos);
4699 		ivb_update_cursor(crtc, base);
4700 	} else {
4701 		I915_WRITE(CURPOS(pipe), pos);
4702 		if (IS_845G(dev) || IS_I865G(dev))
4703 			i845_update_cursor(crtc, base);
4704 		else
4705 			i9xx_update_cursor(crtc, base);
4706 	}
4707 
4708 	if (visible)
4709 		intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
4710 }
4711 
4712 static int intel_crtc_cursor_set(struct drm_crtc *crtc,
4713 				 struct drm_file *file,
4714 				 uint32_t handle,
4715 				 uint32_t width, uint32_t height)
4716 {
4717 	struct drm_device *dev = crtc->dev;
4718 	struct drm_i915_private *dev_priv = dev->dev_private;
4719 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4720 	struct drm_i915_gem_object *obj;
4721 	uint32_t addr;
4722 	int ret;
4723 
4724 	DRM_DEBUG_KMS("\n");
4725 
4726 	/* if we want to turn off the cursor ignore width and height */
4727 	if (!handle) {
4728 		DRM_DEBUG_KMS("cursor off\n");
4729 		addr = 0;
4730 		obj = NULL;
4731 		DRM_LOCK(dev);
4732 		goto finish;
4733 	}
4734 
4735 	/* Currently we only support 64x64 cursors */
4736 	if (width != 64 || height != 64) {
4737 		DRM_ERROR("we currently only support 64x64 cursors\n");
4738 		return -EINVAL;
4739 	}
4740 
4741 	obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
4742 	if (&obj->base == NULL)
4743 		return -ENOENT;
4744 
4745 	if (obj->base.size < width * height * 4) {
4746 		DRM_ERROR("buffer is to small\n");
4747 		ret = -ENOMEM;
4748 		goto fail;
4749 	}
4750 
4751 	/* we only need to pin inside GTT if cursor is non-phy */
4752 	DRM_LOCK(dev);
4753 	if (!dev_priv->info->cursor_needs_physical) {
4754 		if (obj->tiling_mode) {
4755 			DRM_ERROR("cursor cannot be tiled\n");
4756 			ret = -EINVAL;
4757 			goto fail_locked;
4758 		}
4759 
4760 		ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
4761 		if (ret) {
4762 			DRM_ERROR("failed to move cursor bo into the GTT\n");
4763 			goto fail_locked;
4764 		}
4765 
4766 		ret = i915_gem_object_put_fence(obj);
4767 		if (ret) {
4768 			DRM_ERROR("failed to release fence for cursor\n");
4769 			goto fail_unpin;
4770 		}
4771 
4772 		addr = obj->gtt_offset;
4773 	} else {
4774 		int align = IS_I830(dev) ? 16 * 1024 : 256;
4775 		ret = i915_gem_attach_phys_object(dev, obj,
4776 						  (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
4777 						  align);
4778 		if (ret) {
4779 			DRM_ERROR("failed to attach phys object\n");
4780 			goto fail_locked;
4781 		}
4782 		addr = obj->phys_obj->handle->busaddr;
4783 	}
4784 
4785 	if (IS_GEN2(dev))
4786 		I915_WRITE(CURSIZE, (height << 12) | width);
4787 
4788  finish:
4789 	if (intel_crtc->cursor_bo) {
4790 		if (dev_priv->info->cursor_needs_physical) {
4791 			if (intel_crtc->cursor_bo != obj)
4792 				i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
4793 		} else
4794 			i915_gem_object_unpin(intel_crtc->cursor_bo);
4795 		drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
4796 	}
4797 
4798 	DRM_UNLOCK(dev);
4799 
4800 	intel_crtc->cursor_addr = addr;
4801 	intel_crtc->cursor_bo = obj;
4802 	intel_crtc->cursor_width = width;
4803 	intel_crtc->cursor_height = height;
4804 
4805 	intel_crtc_update_cursor(crtc, true);
4806 
4807 	return 0;
4808 fail_unpin:
4809 	i915_gem_object_unpin(obj);
4810 fail_locked:
4811 	DRM_UNLOCK(dev);
4812 fail:
4813 	drm_gem_object_unreference_unlocked(&obj->base);
4814 	return ret;
4815 }
4816 
4817 static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
4818 {
4819 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4820 
4821 	intel_crtc->cursor_x = x;
4822 	intel_crtc->cursor_y = y;
4823 
4824 	intel_crtc_update_cursor(crtc, true);
4825 
4826 	return 0;
4827 }
4828 
4829 /** Sets the color ramps on behalf of RandR */
4830 void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
4831 				 u16 blue, int regno)
4832 {
4833 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4834 
4835 	intel_crtc->lut_r[regno] = red >> 8;
4836 	intel_crtc->lut_g[regno] = green >> 8;
4837 	intel_crtc->lut_b[regno] = blue >> 8;
4838 }
4839 
4840 void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
4841 			     u16 *blue, int regno)
4842 {
4843 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4844 
4845 	*red = intel_crtc->lut_r[regno] << 8;
4846 	*green = intel_crtc->lut_g[regno] << 8;
4847 	*blue = intel_crtc->lut_b[regno] << 8;
4848 }
4849 
4850 static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
4851 				 u16 *blue, uint32_t start, uint32_t size)
4852 {
4853 	int end = (start + size > 256) ? 256 : start + size, i;
4854 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4855 
4856 	for (i = start; i < end; i++) {
4857 		intel_crtc->lut_r[i] = red[i] >> 8;
4858 		intel_crtc->lut_g[i] = green[i] >> 8;
4859 		intel_crtc->lut_b[i] = blue[i] >> 8;
4860 	}
4861 
4862 	intel_crtc_load_lut(crtc);
4863 }
4864 
4865 /**
4866  * Get a pipe with a simple mode set on it for doing load-based monitor
4867  * detection.
4868  *
4869  * It will be up to the load-detect code to adjust the pipe as appropriate for
4870  * its requirements.  The pipe will be connected to no other encoders.
4871  *
4872  * Currently this code will only succeed if there is a pipe with no encoders
4873  * configured for it.  In the future, it could choose to temporarily disable
4874  * some outputs to free up a pipe for its use.
4875  *
4876  * \return crtc, or NULL if no pipes are available.
4877  */
4878 
4879 /* VESA 640x480x72Hz mode to set on the pipe */
4880 static struct drm_display_mode load_detect_mode = {
4881 	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
4882 		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
4883 };
4884 
4885 static int
4886 intel_framebuffer_create(struct drm_device *dev,
4887     struct drm_mode_fb_cmd2 *mode_cmd, struct drm_i915_gem_object *obj,
4888      struct drm_framebuffer **res)
4889 {
4890 	struct intel_framebuffer *intel_fb;
4891 	int ret;
4892 
4893 	intel_fb = kmalloc(sizeof(*intel_fb), DRM_MEM_KMS, M_WAITOK | M_ZERO);
4894 	ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
4895 	if (ret) {
4896 		drm_gem_object_unreference_unlocked(&obj->base);
4897 		drm_free(intel_fb, DRM_MEM_KMS);
4898 		return (ret);
4899 	}
4900 
4901 	*res = &intel_fb->base;
4902 	return (0);
4903 }
4904 
4905 static u32
4906 intel_framebuffer_pitch_for_width(int width, int bpp)
4907 {
4908 	u32 pitch = howmany(width * bpp, 8);
4909 	return roundup2(pitch, 64);
4910 }
4911 
4912 static u32
4913 intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
4914 {
4915 	u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
4916 	return roundup2(pitch * mode->vdisplay, PAGE_SIZE);
4917 }
4918 
4919 static int
4920 intel_framebuffer_create_for_mode(struct drm_device *dev,
4921     struct drm_display_mode *mode, int depth, int bpp,
4922     struct drm_framebuffer **res)
4923 {
4924 	struct drm_i915_gem_object *obj;
4925 	struct drm_mode_fb_cmd2 mode_cmd;
4926 
4927 	obj = i915_gem_alloc_object(dev,
4928 				    intel_framebuffer_size_for_mode(mode, bpp));
4929 	if (obj == NULL)
4930 		return (-ENOMEM);
4931 
4932 	mode_cmd.width = mode->hdisplay;
4933 	mode_cmd.height = mode->vdisplay;
4934 	mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
4935 								bpp);
4936 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
4937 
4938 	return (intel_framebuffer_create(dev, &mode_cmd, obj, res));
4939 }
4940 
4941 static int
4942 mode_fits_in_fbdev(struct drm_device *dev,
4943     struct drm_display_mode *mode, struct drm_framebuffer **res)
4944 {
4945 	struct drm_i915_private *dev_priv = dev->dev_private;
4946 	struct drm_i915_gem_object *obj;
4947 	struct drm_framebuffer *fb;
4948 
4949 	if (dev_priv->fbdev == NULL) {
4950 		*res = NULL;
4951 		return (0);
4952 	}
4953 
4954 	obj = dev_priv->fbdev->ifb.obj;
4955 	if (obj == NULL) {
4956 		*res = NULL;
4957 		return (0);
4958 	}
4959 
4960 	fb = &dev_priv->fbdev->ifb.base;
4961 	if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
4962 	    fb->bits_per_pixel)) {
4963 		*res = NULL;
4964 		return (0);
4965 	}
4966 
4967 	if (obj->base.size < mode->vdisplay * fb->pitches[0]) {
4968 		*res = NULL;
4969 		return (0);
4970 	}
4971 
4972 	*res = fb;
4973 	return (0);
4974 }
4975 
4976 bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
4977 				struct drm_connector *connector,
4978 				struct drm_display_mode *mode,
4979 				struct intel_load_detect_pipe *old)
4980 {
4981 	struct intel_crtc *intel_crtc;
4982 	struct drm_crtc *possible_crtc;
4983 	struct drm_encoder *encoder = &intel_encoder->base;
4984 	struct drm_crtc *crtc = NULL;
4985 	struct drm_device *dev = encoder->dev;
4986 	struct drm_framebuffer *old_fb;
4987 	int i = -1, r;
4988 
4989 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
4990 		      connector->base.id, drm_get_connector_name(connector),
4991 		      encoder->base.id, drm_get_encoder_name(encoder));
4992 
4993 	/*
4994 	 * Algorithm gets a little messy:
4995 	 *
4996 	 *   - if the connector already has an assigned crtc, use it (but make
4997 	 *     sure it's on first)
4998 	 *
4999 	 *   - try to find the first unused crtc that can drive this connector,
5000 	 *     and use that if we find one
5001 	 */
5002 
5003 	/* See if we already have a CRTC for this connector */
5004 	if (encoder->crtc) {
5005 		crtc = encoder->crtc;
5006 
5007 		intel_crtc = to_intel_crtc(crtc);
5008 		old->dpms_mode = intel_crtc->dpms_mode;
5009 		old->load_detect_temp = false;
5010 
5011 		/* Make sure the crtc and connector are running */
5012 		if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
5013 			struct drm_encoder_helper_funcs *encoder_funcs;
5014 			struct drm_crtc_helper_funcs *crtc_funcs;
5015 
5016 			crtc_funcs = crtc->helper_private;
5017 			crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
5018 
5019 			encoder_funcs = encoder->helper_private;
5020 			encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
5021 		}
5022 
5023 		return true;
5024 	}
5025 
5026 	/* Find an unused one (if possible) */
5027 	list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
5028 		i++;
5029 		if (!(encoder->possible_crtcs & (1 << i)))
5030 			continue;
5031 		if (!possible_crtc->enabled) {
5032 			crtc = possible_crtc;
5033 			break;
5034 		}
5035 	}
5036 
5037 	/*
5038 	 * If we didn't find an unused CRTC, don't use any.
5039 	 */
5040 	if (!crtc) {
5041 		DRM_DEBUG_KMS("no pipe available for load-detect\n");
5042 		return false;
5043 	}
5044 
5045 	encoder->crtc = crtc;
5046 	connector->encoder = encoder;
5047 
5048 	intel_crtc = to_intel_crtc(crtc);
5049 	old->dpms_mode = intel_crtc->dpms_mode;
5050 	old->load_detect_temp = true;
5051 	old->release_fb = NULL;
5052 
5053 	if (!mode)
5054 		mode = &load_detect_mode;
5055 
5056 	old_fb = crtc->fb;
5057 
5058 	/* We need a framebuffer large enough to accommodate all accesses
5059 	 * that the plane may generate whilst we perform load detection.
5060 	 * We can not rely on the fbcon either being present (we get called
5061 	 * during its initialisation to detect all boot displays, or it may
5062 	 * not even exist) or that it is large enough to satisfy the
5063 	 * requested mode.
5064 	 */
5065 	r = mode_fits_in_fbdev(dev, mode, &crtc->fb);
5066 	if (crtc->fb == NULL) {
5067 		DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
5068 		r = intel_framebuffer_create_for_mode(dev, mode, 24, 32,
5069 		    &crtc->fb);
5070 		old->release_fb = crtc->fb;
5071 	} else
5072 		DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
5073 	if (r != 0) {
5074 		DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
5075 		crtc->fb = old_fb;
5076 		return false;
5077 	}
5078 
5079 	if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
5080 		DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
5081 		if (old->release_fb)
5082 			old->release_fb->funcs->destroy(old->release_fb);
5083 		crtc->fb = old_fb;
5084 		return false;
5085 	}
5086 
5087 	/* let the connector get through one full cycle before testing */
5088 	intel_wait_for_vblank(dev, intel_crtc->pipe);
5089 
5090 	return true;
5091 }
5092 
5093 void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
5094 				    struct drm_connector *connector,
5095 				    struct intel_load_detect_pipe *old)
5096 {
5097 	struct drm_encoder *encoder = &intel_encoder->base;
5098 	struct drm_device *dev = encoder->dev;
5099 	struct drm_crtc *crtc = encoder->crtc;
5100 	struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
5101 	struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
5102 
5103 	DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5104 		      connector->base.id, drm_get_connector_name(connector),
5105 		      encoder->base.id, drm_get_encoder_name(encoder));
5106 
5107 	if (old->load_detect_temp) {
5108 		connector->encoder = NULL;
5109 		drm_helper_disable_unused_functions(dev);
5110 
5111 		if (old->release_fb)
5112 			old->release_fb->funcs->destroy(old->release_fb);
5113 
5114 		return;
5115 	}
5116 
5117 	/* Switch crtc and encoder back off if necessary */
5118 	if (old->dpms_mode != DRM_MODE_DPMS_ON) {
5119 		encoder_funcs->dpms(encoder, old->dpms_mode);
5120 		crtc_funcs->dpms(crtc, old->dpms_mode);
5121 	}
5122 }
5123 
5124 /* Returns the clock of the currently programmed mode of the given pipe. */
5125 static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
5126 {
5127 	struct drm_i915_private *dev_priv = dev->dev_private;
5128 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5129 	int pipe = intel_crtc->pipe;
5130 	u32 dpll = I915_READ(DPLL(pipe));
5131 	u32 fp;
5132 	intel_clock_t clock;
5133 
5134 	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
5135 		fp = I915_READ(FP0(pipe));
5136 	else
5137 		fp = I915_READ(FP1(pipe));
5138 
5139 	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
5140 	if (IS_PINEVIEW(dev)) {
5141 		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
5142 		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
5143 	} else {
5144 		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
5145 		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
5146 	}
5147 
5148 	if (!IS_GEN2(dev)) {
5149 		if (IS_PINEVIEW(dev))
5150 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
5151 				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
5152 		else
5153 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
5154 			       DPLL_FPA01_P1_POST_DIV_SHIFT);
5155 
5156 		switch (dpll & DPLL_MODE_MASK) {
5157 		case DPLLB_MODE_DAC_SERIAL:
5158 			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
5159 				5 : 10;
5160 			break;
5161 		case DPLLB_MODE_LVDS:
5162 			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
5163 				7 : 14;
5164 			break;
5165 		default:
5166 			DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
5167 				  "mode\n", (int)(dpll & DPLL_MODE_MASK));
5168 			return 0;
5169 		}
5170 
5171 		/* XXX: Handle the 100Mhz refclk */
5172 		intel_clock(dev, 96000, &clock);
5173 	} else {
5174 		bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
5175 
5176 		if (is_lvds) {
5177 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
5178 				       DPLL_FPA01_P1_POST_DIV_SHIFT);
5179 			clock.p2 = 14;
5180 
5181 			if ((dpll & PLL_REF_INPUT_MASK) ==
5182 			    PLLB_REF_INPUT_SPREADSPECTRUMIN) {
5183 				/* XXX: might not be 66MHz */
5184 				intel_clock(dev, 66000, &clock);
5185 			} else
5186 				intel_clock(dev, 48000, &clock);
5187 		} else {
5188 			if (dpll & PLL_P1_DIVIDE_BY_TWO)
5189 				clock.p1 = 2;
5190 			else {
5191 				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
5192 					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
5193 			}
5194 			if (dpll & PLL_P2_DIVIDE_BY_4)
5195 				clock.p2 = 4;
5196 			else
5197 				clock.p2 = 2;
5198 
5199 			intel_clock(dev, 48000, &clock);
5200 		}
5201 	}
5202 
5203 	/* XXX: It would be nice to validate the clocks, but we can't reuse
5204 	 * i830PllIsValid() because it relies on the xf86_config connector
5205 	 * configuration being accurate, which it isn't necessarily.
5206 	 */
5207 
5208 	return clock.dot;
5209 }
5210 
5211 /** Returns the currently programmed mode of the given pipe. */
5212 struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
5213 					     struct drm_crtc *crtc)
5214 {
5215 	struct drm_i915_private *dev_priv = dev->dev_private;
5216 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5217 	int pipe = intel_crtc->pipe;
5218 	struct drm_display_mode *mode;
5219 	int htot = I915_READ(HTOTAL(pipe));
5220 	int hsync = I915_READ(HSYNC(pipe));
5221 	int vtot = I915_READ(VTOTAL(pipe));
5222 	int vsync = I915_READ(VSYNC(pipe));
5223 
5224 	mode = kmalloc(sizeof(*mode), DRM_MEM_KMS, M_WAITOK | M_ZERO);
5225 
5226 	mode->clock = intel_crtc_clock_get(dev, crtc);
5227 	mode->hdisplay = (htot & 0xffff) + 1;
5228 	mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
5229 	mode->hsync_start = (hsync & 0xffff) + 1;
5230 	mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
5231 	mode->vdisplay = (vtot & 0xffff) + 1;
5232 	mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
5233 	mode->vsync_start = (vsync & 0xffff) + 1;
5234 	mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
5235 
5236 	drm_mode_set_name(mode);
5237 	drm_mode_set_crtcinfo(mode, 0);
5238 
5239 	return mode;
5240 }
5241 
5242 #define GPU_IDLE_TIMEOUT (500 /* ms */ * 1000 / hz)
5243 
5244 /* When this timer fires, we've been idle for awhile */
5245 static void intel_gpu_idle_timer(void *arg)
5246 {
5247 	struct drm_device *dev = arg;
5248 	drm_i915_private_t *dev_priv = dev->dev_private;
5249 
5250 	if (!list_empty(&dev_priv->mm.active_list)) {
5251 		/* Still processing requests, so just re-arm the timer. */
5252 		callout_reset(&dev_priv->idle_callout, GPU_IDLE_TIMEOUT,
5253 		    i915_hangcheck_elapsed, dev);
5254 		return;
5255 	}
5256 
5257 	dev_priv->busy = false;
5258 	taskqueue_enqueue(dev_priv->tq, &dev_priv->idle_task);
5259 }
5260 
5261 #define CRTC_IDLE_TIMEOUT (1000 /* ms */ * 1000 / hz)
5262 
5263 static void intel_crtc_idle_timer(void *arg)
5264 {
5265 	struct intel_crtc *intel_crtc = arg;
5266 	struct drm_crtc *crtc = &intel_crtc->base;
5267 	drm_i915_private_t *dev_priv = crtc->dev->dev_private;
5268 	struct intel_framebuffer *intel_fb;
5269 
5270 	intel_fb = to_intel_framebuffer(crtc->fb);
5271 	if (intel_fb && intel_fb->obj->active) {
5272 		/* The framebuffer is still being accessed by the GPU. */
5273 		callout_reset(&intel_crtc->idle_callout, CRTC_IDLE_TIMEOUT,
5274 		    i915_hangcheck_elapsed, crtc->dev);
5275 		return;
5276 	}
5277 
5278 	intel_crtc->busy = false;
5279 	taskqueue_enqueue(dev_priv->tq, &dev_priv->idle_task);
5280 }
5281 
5282 static void intel_increase_pllclock(struct drm_crtc *crtc)
5283 {
5284 	struct drm_device *dev = crtc->dev;
5285 	drm_i915_private_t *dev_priv = dev->dev_private;
5286 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5287 	int pipe = intel_crtc->pipe;
5288 	int dpll_reg = DPLL(pipe);
5289 	int dpll;
5290 
5291 	if (HAS_PCH_SPLIT(dev))
5292 		return;
5293 
5294 	if (!dev_priv->lvds_downclock_avail)
5295 		return;
5296 
5297 	dpll = I915_READ(dpll_reg);
5298 	if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
5299 		DRM_DEBUG_DRIVER("upclocking LVDS\n");
5300 
5301 		assert_panel_unlocked(dev_priv, pipe);
5302 
5303 		dpll &= ~DISPLAY_RATE_SELECT_FPA1;
5304 		I915_WRITE(dpll_reg, dpll);
5305 		intel_wait_for_vblank(dev, pipe);
5306 
5307 		dpll = I915_READ(dpll_reg);
5308 		if (dpll & DISPLAY_RATE_SELECT_FPA1)
5309 			DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
5310 	}
5311 
5312 	/* Schedule downclock */
5313 	callout_reset(&intel_crtc->idle_callout, CRTC_IDLE_TIMEOUT,
5314 	    intel_crtc_idle_timer, intel_crtc);
5315 }
5316 
5317 static void intel_decrease_pllclock(struct drm_crtc *crtc)
5318 {
5319 	struct drm_device *dev = crtc->dev;
5320 	drm_i915_private_t *dev_priv = dev->dev_private;
5321 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5322 
5323 	if (HAS_PCH_SPLIT(dev))
5324 		return;
5325 
5326 	if (!dev_priv->lvds_downclock_avail)
5327 		return;
5328 
5329 	/*
5330 	 * Since this is called by a timer, we should never get here in
5331 	 * the manual case.
5332 	 */
5333 	if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
5334 		int pipe = intel_crtc->pipe;
5335 		int dpll_reg = DPLL(pipe);
5336 		u32 dpll;
5337 
5338 		DRM_DEBUG_DRIVER("downclocking LVDS\n");
5339 
5340 		assert_panel_unlocked(dev_priv, pipe);
5341 
5342 		dpll = I915_READ(dpll_reg);
5343 		dpll |= DISPLAY_RATE_SELECT_FPA1;
5344 		I915_WRITE(dpll_reg, dpll);
5345 		intel_wait_for_vblank(dev, pipe);
5346 		dpll = I915_READ(dpll_reg);
5347 		if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
5348 			DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
5349 	}
5350 }
5351 
5352 /**
5353  * intel_idle_update - adjust clocks for idleness
5354  * @work: work struct
5355  *
5356  * Either the GPU or display (or both) went idle.  Check the busy status
5357  * here and adjust the CRTC and GPU clocks as necessary.
5358  */
5359 static void intel_idle_update(void *arg, int pending)
5360 {
5361 	drm_i915_private_t *dev_priv = arg;
5362 	struct drm_device *dev = dev_priv->dev;
5363 	struct drm_crtc *crtc;
5364 	struct intel_crtc *intel_crtc;
5365 
5366 	if (!i915_powersave)
5367 		return;
5368 
5369 	DRM_LOCK(dev);
5370 
5371 	i915_update_gfx_val(dev_priv);
5372 
5373 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5374 		/* Skip inactive CRTCs */
5375 		if (!crtc->fb)
5376 			continue;
5377 
5378 		intel_crtc = to_intel_crtc(crtc);
5379 		if (!intel_crtc->busy)
5380 			intel_decrease_pllclock(crtc);
5381 	}
5382 
5383 	DRM_UNLOCK(dev);
5384 }
5385 
5386 /**
5387  * intel_mark_busy - mark the GPU and possibly the display busy
5388  * @dev: drm device
5389  * @obj: object we're operating on
5390  *
5391  * Callers can use this function to indicate that the GPU is busy processing
5392  * commands.  If @obj matches one of the CRTC objects (i.e. it's a scanout
5393  * buffer), we'll also mark the display as busy, so we know to increase its
5394  * clock frequency.
5395  */
5396 void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
5397 {
5398 	drm_i915_private_t *dev_priv = dev->dev_private;
5399 	struct drm_crtc *crtc = NULL;
5400 	struct intel_framebuffer *intel_fb;
5401 	struct intel_crtc *intel_crtc;
5402 
5403 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
5404 		return;
5405 
5406 	if (!dev_priv->busy)
5407 		dev_priv->busy = true;
5408 	else
5409 		callout_reset(&dev_priv->idle_callout, GPU_IDLE_TIMEOUT,
5410 		    intel_gpu_idle_timer, dev);
5411 
5412 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5413 		if (!crtc->fb)
5414 			continue;
5415 
5416 		intel_crtc = to_intel_crtc(crtc);
5417 		intel_fb = to_intel_framebuffer(crtc->fb);
5418 		if (intel_fb->obj == obj) {
5419 			if (!intel_crtc->busy) {
5420 				/* Non-busy -> busy, upclock */
5421 				intel_increase_pllclock(crtc);
5422 				intel_crtc->busy = true;
5423 			} else {
5424 				/* Busy -> busy, put off timer */
5425 				callout_reset(&intel_crtc->idle_callout,
5426 				    CRTC_IDLE_TIMEOUT, intel_crtc_idle_timer,
5427 				    intel_crtc);
5428 			}
5429 		}
5430 	}
5431 }
5432 
5433 static void intel_crtc_destroy(struct drm_crtc *crtc)
5434 {
5435 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5436 	struct drm_device *dev = crtc->dev;
5437 	struct drm_i915_private *dev_priv = dev->dev_private;
5438 	struct intel_unpin_work *work;
5439 
5440 	lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5441 	work = intel_crtc->unpin_work;
5442 	intel_crtc->unpin_work = NULL;
5443 	lockmgr(&dev->event_lock, LK_RELEASE);
5444 
5445 	if (work) {
5446 		taskqueue_cancel(dev_priv->tq, &work->task, NULL);
5447 		taskqueue_drain(dev_priv->tq, &work->task);
5448 		drm_free(work, DRM_MEM_KMS);
5449 	}
5450 
5451 	drm_crtc_cleanup(crtc);
5452 
5453 	drm_free(intel_crtc, DRM_MEM_KMS);
5454 }
5455 
5456 static void intel_unpin_work_fn(void *arg, int pending)
5457 {
5458 	struct intel_unpin_work *work = arg;
5459 	struct drm_device *dev;
5460 
5461 	dev = work->dev;
5462 	DRM_LOCK(dev);
5463 	intel_unpin_fb_obj(work->old_fb_obj);
5464 	drm_gem_object_unreference(&work->pending_flip_obj->base);
5465 	drm_gem_object_unreference(&work->old_fb_obj->base);
5466 
5467 	intel_update_fbc(work->dev);
5468 	DRM_UNLOCK(dev);
5469 	drm_free(work, DRM_MEM_KMS);
5470 }
5471 
5472 static void do_intel_finish_page_flip(struct drm_device *dev,
5473 				      struct drm_crtc *crtc)
5474 {
5475 	drm_i915_private_t *dev_priv = dev->dev_private;
5476 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5477 	struct intel_unpin_work *work;
5478 	struct drm_i915_gem_object *obj;
5479 
5480 	/* Ignore early vblank irqs */
5481 	if (intel_crtc == NULL)
5482 		return;
5483 
5484 	lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5485 	work = intel_crtc->unpin_work;
5486 	if (work == NULL || !atomic_read(&work->pending)) {
5487 		lockmgr(&dev->event_lock, LK_RELEASE);
5488 		return;
5489 	}
5490 
5491 	intel_crtc->unpin_work = NULL;
5492 
5493 	if (work->event)
5494 		drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
5495 
5496 	drm_vblank_put(dev, intel_crtc->pipe);
5497 
5498 	lockmgr(&dev->event_lock, LK_RELEASE);
5499 
5500 	obj = work->old_fb_obj;
5501 
5502 	atomic_clear_mask(1 << intel_crtc->plane,
5503 			  &obj->pending_flip.counter);
5504 	wakeup(&obj->pending_flip);
5505 
5506 	taskqueue_enqueue(dev_priv->tq, &work->task);
5507 }
5508 
5509 void intel_finish_page_flip(struct drm_device *dev, int pipe)
5510 {
5511 	drm_i915_private_t *dev_priv = dev->dev_private;
5512 	struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
5513 
5514 	do_intel_finish_page_flip(dev, crtc);
5515 }
5516 
5517 void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
5518 {
5519 	drm_i915_private_t *dev_priv = dev->dev_private;
5520 	struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
5521 
5522 	do_intel_finish_page_flip(dev, crtc);
5523 }
5524 
5525 void intel_prepare_page_flip(struct drm_device *dev, int plane)
5526 {
5527 	drm_i915_private_t *dev_priv = dev->dev_private;
5528 	struct intel_crtc *intel_crtc =
5529 		to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
5530 
5531 	lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5532 	if (intel_crtc->unpin_work)
5533 		atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
5534 	lockmgr(&dev->event_lock, LK_RELEASE);
5535 }
5536 
5537 static int intel_gen2_queue_flip(struct drm_device *dev,
5538 				 struct drm_crtc *crtc,
5539 				 struct drm_framebuffer *fb,
5540 				 struct drm_i915_gem_object *obj)
5541 {
5542 	struct drm_i915_private *dev_priv = dev->dev_private;
5543 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5544 	unsigned long offset;
5545 	u32 flip_mask;
5546 	int ret;
5547 
5548 	ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
5549 	if (ret)
5550 		goto out;
5551 
5552 	/* Offset into the new buffer for cases of shared fbs between CRTCs */
5553 	offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
5554 
5555 	ret = BEGIN_LP_RING(6);
5556 	if (ret)
5557 		goto out;
5558 
5559 	/* Can't queue multiple flips, so wait for the previous
5560 	 * one to finish before executing the next.
5561 	 */
5562 	if (intel_crtc->plane)
5563 		flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
5564 	else
5565 		flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
5566 	OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
5567 	OUT_RING(MI_NOOP);
5568 	OUT_RING(MI_DISPLAY_FLIP |
5569 		 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5570 	OUT_RING(fb->pitches[0]);
5571 	OUT_RING(obj->gtt_offset + offset);
5572 	OUT_RING(0); /* aux display base address, unused */
5573 	ADVANCE_LP_RING();
5574 out:
5575 	return ret;
5576 }
5577 
5578 static int intel_gen3_queue_flip(struct drm_device *dev,
5579 				 struct drm_crtc *crtc,
5580 				 struct drm_framebuffer *fb,
5581 				 struct drm_i915_gem_object *obj)
5582 {
5583 	struct drm_i915_private *dev_priv = dev->dev_private;
5584 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5585 	unsigned long offset;
5586 	u32 flip_mask;
5587 	int ret;
5588 
5589 	ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
5590 	if (ret)
5591 		goto out;
5592 
5593 	/* Offset into the new buffer for cases of shared fbs between CRTCs */
5594 	offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
5595 
5596 	ret = BEGIN_LP_RING(6);
5597 	if (ret)
5598 		goto out;
5599 
5600 	if (intel_crtc->plane)
5601 		flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
5602 	else
5603 		flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
5604 	OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
5605 	OUT_RING(MI_NOOP);
5606 	OUT_RING(MI_DISPLAY_FLIP_I915 |
5607 		 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5608 	OUT_RING(fb->pitches[0]);
5609 	OUT_RING(obj->gtt_offset + offset);
5610 	OUT_RING(MI_NOOP);
5611 
5612 	ADVANCE_LP_RING();
5613 out:
5614 	return ret;
5615 }
5616 
5617 static int intel_gen4_queue_flip(struct drm_device *dev,
5618 				 struct drm_crtc *crtc,
5619 				 struct drm_framebuffer *fb,
5620 				 struct drm_i915_gem_object *obj)
5621 {
5622 	struct drm_i915_private *dev_priv = dev->dev_private;
5623 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5624 	uint32_t pf, pipesrc;
5625 	int ret;
5626 
5627 	ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
5628 	if (ret)
5629 		goto out;
5630 
5631 	ret = BEGIN_LP_RING(4);
5632 	if (ret)
5633 		goto out;
5634 
5635 	/* i965+ uses the linear or tiled offsets from the
5636 	 * Display Registers (which do not change across a page-flip)
5637 	 * so we need only reprogram the base address.
5638 	 */
5639 	OUT_RING(MI_DISPLAY_FLIP |
5640 		 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5641 	OUT_RING(fb->pitches[0]);
5642 	OUT_RING(obj->gtt_offset | obj->tiling_mode);
5643 
5644 	/* XXX Enabling the panel-fitter across page-flip is so far
5645 	 * untested on non-native modes, so ignore it for now.
5646 	 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
5647 	 */
5648 	pf = 0;
5649 	pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
5650 	OUT_RING(pf | pipesrc);
5651 	ADVANCE_LP_RING();
5652 out:
5653 	return ret;
5654 }
5655 
5656 static int intel_gen6_queue_flip(struct drm_device *dev,
5657 				 struct drm_crtc *crtc,
5658 				 struct drm_framebuffer *fb,
5659 				 struct drm_i915_gem_object *obj)
5660 {
5661 	struct drm_i915_private *dev_priv = dev->dev_private;
5662 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5663 	uint32_t pf, pipesrc;
5664 	int ret;
5665 
5666 	ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
5667 	if (ret)
5668 		goto out;
5669 
5670 	ret = BEGIN_LP_RING(4);
5671 	if (ret)
5672 		goto out;
5673 
5674 	OUT_RING(MI_DISPLAY_FLIP |
5675 		 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5676 	OUT_RING(fb->pitches[0] | obj->tiling_mode);
5677 	OUT_RING(obj->gtt_offset);
5678 
5679 	/* Contrary to the suggestions in the documentation,
5680 	 * "Enable Panel Fitter" does not seem to be required when page
5681 	 * flipping with a non-native mode, and worse causes a normal
5682 	 * modeset to fail.
5683 	 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
5684 	 */
5685 	pf = 0;
5686 	pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
5687 	OUT_RING(pf | pipesrc);
5688 	ADVANCE_LP_RING();
5689 out:
5690 	return ret;
5691 }
5692 
5693 /*
5694  * On gen7 we currently use the blit ring because (in early silicon at least)
5695  * the render ring doesn't give us interrpts for page flip completion, which
5696  * means clients will hang after the first flip is queued.  Fortunately the
5697  * blit ring generates interrupts properly, so use it instead.
5698  */
5699 static int intel_gen7_queue_flip(struct drm_device *dev,
5700 				 struct drm_crtc *crtc,
5701 				 struct drm_framebuffer *fb,
5702 				 struct drm_i915_gem_object *obj)
5703 {
5704 	struct drm_i915_private *dev_priv = dev->dev_private;
5705 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5706 	struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
5707 	int ret;
5708 
5709 	ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
5710 	if (ret)
5711 		goto out;
5712 
5713 	ret = intel_ring_begin(ring, 4);
5714 	if (ret)
5715 		goto out;
5716 
5717 	intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
5718 	intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
5719 	intel_ring_emit(ring, (obj->gtt_offset));
5720 	intel_ring_emit(ring, (MI_NOOP));
5721 	intel_ring_advance(ring);
5722 out:
5723 	return ret;
5724 }
5725 
5726 static int intel_default_queue_flip(struct drm_device *dev,
5727 				    struct drm_crtc *crtc,
5728 				    struct drm_framebuffer *fb,
5729 				    struct drm_i915_gem_object *obj)
5730 {
5731 	return -ENODEV;
5732 }
5733 
5734 static int intel_crtc_page_flip(struct drm_crtc *crtc,
5735 				struct drm_framebuffer *fb,
5736 				struct drm_pending_vblank_event *event)
5737 {
5738 	struct drm_device *dev = crtc->dev;
5739 	struct drm_i915_private *dev_priv = dev->dev_private;
5740 	struct intel_framebuffer *intel_fb;
5741 	struct drm_i915_gem_object *obj;
5742 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5743 	struct intel_unpin_work *work;
5744 	int ret;
5745 
5746 	work = kmalloc(sizeof *work, DRM_MEM_KMS, M_WAITOK | M_ZERO);
5747 
5748 	work->event = event;
5749 	work->dev = crtc->dev;
5750 	intel_fb = to_intel_framebuffer(crtc->fb);
5751 	work->old_fb_obj = intel_fb->obj;
5752 	TASK_INIT(&work->task, 0, intel_unpin_work_fn, work);
5753 
5754 	ret = drm_vblank_get(dev, intel_crtc->pipe);
5755 	if (ret)
5756 		goto free_work;
5757 
5758 	/* We borrow the event spin lock for protecting unpin_work */
5759 	lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5760 	if (intel_crtc->unpin_work) {
5761 		lockmgr(&dev->event_lock, LK_RELEASE);
5762 		drm_free(work, DRM_MEM_KMS);
5763 		drm_vblank_put(dev, intel_crtc->pipe);
5764 
5765 		DRM_DEBUG("flip queue: crtc already busy\n");
5766 		return -EBUSY;
5767 	}
5768 	intel_crtc->unpin_work = work;
5769 	lockmgr(&dev->event_lock, LK_RELEASE);
5770 
5771 	intel_fb = to_intel_framebuffer(fb);
5772 	obj = intel_fb->obj;
5773 
5774 	DRM_LOCK(dev);
5775 
5776 	/* Reference the objects for the scheduled work. */
5777 	drm_gem_object_reference(&work->old_fb_obj->base);
5778 	drm_gem_object_reference(&obj->base);
5779 
5780 	crtc->fb = fb;
5781 
5782 	work->pending_flip_obj = obj;
5783 
5784 	work->enable_stall_check = true;
5785 
5786 	/* Block clients from rendering to the new back buffer until
5787 	 * the flip occurs and the object is no longer visible.
5788 	 */
5789 	atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
5790 
5791 	ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
5792 	if (ret)
5793 		goto cleanup_pending;
5794 	intel_disable_fbc(dev);
5795 	DRM_UNLOCK(dev);
5796 
5797 	return 0;
5798 
5799 cleanup_pending:
5800 	atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
5801 	drm_gem_object_unreference(&work->old_fb_obj->base);
5802 	drm_gem_object_unreference(&obj->base);
5803 	DRM_UNLOCK(dev);
5804 
5805 	lockmgr(&dev->event_lock, LK_EXCLUSIVE);
5806 	intel_crtc->unpin_work = NULL;
5807 	lockmgr(&dev->event_lock, LK_RELEASE);
5808 
5809 	drm_vblank_put(dev, intel_crtc->pipe);
5810 free_work:
5811 	drm_free(work, DRM_MEM_KMS);
5812 
5813 	return ret;
5814 }
5815 
5816 static void intel_sanitize_modesetting(struct drm_device *dev,
5817 				       int pipe, int plane)
5818 {
5819 	struct drm_i915_private *dev_priv = dev->dev_private;
5820 	u32 reg, val;
5821 
5822 	/* Clear any frame start delays used for debugging left by the BIOS */
5823 	for_each_pipe(pipe) {
5824 		reg = PIPECONF(pipe);
5825 		I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
5826 	}
5827 
5828 	if (HAS_PCH_SPLIT(dev))
5829 		return;
5830 
5831 	/* Who knows what state these registers were left in by the BIOS or
5832 	 * grub?
5833 	 *
5834 	 * If we leave the registers in a conflicting state (e.g. with the
5835 	 * display plane reading from the other pipe than the one we intend
5836 	 * to use) then when we attempt to teardown the active mode, we will
5837 	 * not disable the pipes and planes in the correct order -- leaving
5838 	 * a plane reading from a disabled pipe and possibly leading to
5839 	 * undefined behaviour.
5840 	 */
5841 
5842 	reg = DSPCNTR(plane);
5843 	val = I915_READ(reg);
5844 
5845 	if ((val & DISPLAY_PLANE_ENABLE) == 0)
5846 		return;
5847 	if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
5848 		return;
5849 
5850 	/* This display plane is active and attached to the other CPU pipe. */
5851 	pipe = !pipe;
5852 
5853 	/* Disable the plane and wait for it to stop reading from the pipe. */
5854 	intel_disable_plane(dev_priv, plane, pipe);
5855 	intel_disable_pipe(dev_priv, pipe);
5856 }
5857 
5858 static void intel_crtc_reset(struct drm_crtc *crtc)
5859 {
5860 	struct drm_device *dev = crtc->dev;
5861 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5862 
5863 	/* Reset flags back to the 'unknown' status so that they
5864 	 * will be correctly set on the initial modeset.
5865 	 */
5866 	intel_crtc->dpms_mode = -1;
5867 
5868 	/* We need to fix up any BIOS configuration that conflicts with
5869 	 * our expectations.
5870 	 */
5871 	intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
5872 }
5873 
5874 static struct drm_crtc_helper_funcs intel_helper_funcs = {
5875 	.dpms = intel_crtc_dpms,
5876 	.mode_fixup = intel_crtc_mode_fixup,
5877 	.mode_set = intel_crtc_mode_set,
5878 	.mode_set_base = intel_pipe_set_base,
5879 	.mode_set_base_atomic = intel_pipe_set_base_atomic,
5880 	.load_lut = intel_crtc_load_lut,
5881 	.disable = intel_crtc_disable,
5882 };
5883 
5884 static const struct drm_crtc_funcs intel_crtc_funcs = {
5885 	.reset = intel_crtc_reset,
5886 	.cursor_set = intel_crtc_cursor_set,
5887 	.cursor_move = intel_crtc_cursor_move,
5888 	.gamma_set = intel_crtc_gamma_set,
5889 	.set_config = drm_crtc_helper_set_config,
5890 	.destroy = intel_crtc_destroy,
5891 	.page_flip = intel_crtc_page_flip,
5892 };
5893 
5894 static void intel_crtc_init(struct drm_device *dev, int pipe)
5895 {
5896 	drm_i915_private_t *dev_priv = dev->dev_private;
5897 	struct intel_crtc *intel_crtc;
5898 	int i;
5899 
5900 	intel_crtc = kmalloc(sizeof(struct intel_crtc) +
5901 	    (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
5902 	    DRM_MEM_KMS, M_WAITOK | M_ZERO);
5903 
5904 	drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
5905 
5906 	drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
5907 	for (i = 0; i < 256; i++) {
5908 		intel_crtc->lut_r[i] = i;
5909 		intel_crtc->lut_g[i] = i;
5910 		intel_crtc->lut_b[i] = i;
5911 	}
5912 
5913 	/* Swap pipes & planes for FBC on pre-965 */
5914 	intel_crtc->pipe = pipe;
5915 	intel_crtc->plane = pipe;
5916 	if (IS_MOBILE(dev) && IS_GEN3(dev)) {
5917 		DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
5918 		intel_crtc->plane = !pipe;
5919 	}
5920 
5921 	KASSERT(pipe < DRM_ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) &&
5922 	    dev_priv->plane_to_crtc_mapping[intel_crtc->plane] == NULL,
5923 	    ("plane_to_crtc is already initialized"));
5924 	dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
5925 	dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
5926 
5927 	intel_crtc_reset(&intel_crtc->base);
5928 	intel_crtc->active = true; /* force the pipe off on setup_init_config */
5929 	intel_crtc->bpp = 24; /* default for pre-Ironlake */
5930 
5931 	if (HAS_PCH_SPLIT(dev)) {
5932 		if (pipe == 2 && IS_IVYBRIDGE(dev))
5933 			intel_crtc->no_pll = true;
5934 		intel_helper_funcs.prepare = ironlake_crtc_prepare;
5935 		intel_helper_funcs.commit = ironlake_crtc_commit;
5936 	} else {
5937 		intel_helper_funcs.prepare = i9xx_crtc_prepare;
5938 		intel_helper_funcs.commit = i9xx_crtc_commit;
5939 	}
5940 
5941 	drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
5942 
5943 	intel_crtc->busy = false;
5944 
5945 	callout_init_mp(&intel_crtc->idle_callout);
5946 }
5947 
5948 int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
5949 				struct drm_file *file)
5950 {
5951 	drm_i915_private_t *dev_priv = dev->dev_private;
5952 	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
5953 	struct drm_mode_object *drmmode_obj;
5954 	struct intel_crtc *crtc;
5955 
5956 	if (!dev_priv) {
5957 		DRM_ERROR("called with no initialization\n");
5958 		return -EINVAL;
5959 	}
5960 
5961 	drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
5962 			DRM_MODE_OBJECT_CRTC);
5963 
5964 	if (!drmmode_obj) {
5965 		DRM_ERROR("no such CRTC id\n");
5966 		return -EINVAL;
5967 	}
5968 
5969 	crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
5970 	pipe_from_crtc_id->pipe = crtc->pipe;
5971 
5972 	return 0;
5973 }
5974 
5975 static int intel_encoder_clones(struct drm_device *dev, int type_mask)
5976 {
5977 	struct intel_encoder *encoder;
5978 	int index_mask = 0;
5979 	int entry = 0;
5980 
5981 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
5982 		if (type_mask & encoder->clone_mask)
5983 			index_mask |= (1 << entry);
5984 		entry++;
5985 	}
5986 
5987 	return index_mask;
5988 }
5989 
5990 static bool has_edp_a(struct drm_device *dev)
5991 {
5992 	struct drm_i915_private *dev_priv = dev->dev_private;
5993 
5994 	if (!IS_MOBILE(dev))
5995 		return false;
5996 
5997 	if ((I915_READ(DP_A) & DP_DETECTED) == 0)
5998 		return false;
5999 
6000 	if (IS_GEN5(dev) &&
6001 	    (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
6002 		return false;
6003 
6004 	return true;
6005 }
6006 
6007 static void intel_setup_outputs(struct drm_device *dev)
6008 {
6009 	struct drm_i915_private *dev_priv = dev->dev_private;
6010 	struct intel_encoder *encoder;
6011 	bool dpd_is_edp = false;
6012 	bool has_lvds;
6013 
6014 	has_lvds = intel_lvds_init(dev);
6015 	if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
6016 		/* disable the panel fitter on everything but LVDS */
6017 		I915_WRITE(PFIT_CONTROL, 0);
6018 	}
6019 
6020 	if (HAS_PCH_SPLIT(dev)) {
6021 		dpd_is_edp = intel_dpd_is_edp(dev);
6022 
6023 		if (has_edp_a(dev))
6024 			intel_dp_init(dev, DP_A);
6025 
6026 		if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
6027 			intel_dp_init(dev, PCH_DP_D);
6028 	}
6029 
6030 	intel_crt_init(dev);
6031 
6032 	if (HAS_PCH_SPLIT(dev)) {
6033 		int found;
6034 
6035 		DRM_DEBUG_KMS(
6036 "HDMIB %d PCH_DP_B %d HDMIC %d HDMID %d PCH_DP_C %d PCH_DP_D %d LVDS %d\n",
6037 		    (I915_READ(HDMIB) & PORT_DETECTED) != 0,
6038 		    (I915_READ(PCH_DP_B) & DP_DETECTED) != 0,
6039 		    (I915_READ(HDMIC) & PORT_DETECTED) != 0,
6040 		    (I915_READ(HDMID) & PORT_DETECTED) != 0,
6041 		    (I915_READ(PCH_DP_C) & DP_DETECTED) != 0,
6042 		    (I915_READ(PCH_DP_D) & DP_DETECTED) != 0,
6043 		    (I915_READ(PCH_LVDS) & LVDS_DETECTED) != 0);
6044 
6045 		if (I915_READ(HDMIB) & PORT_DETECTED) {
6046 			/* PCH SDVOB multiplex with HDMIB */
6047 			found = intel_sdvo_init(dev, PCH_SDVOB);
6048 			if (!found)
6049 				intel_hdmi_init(dev, HDMIB);
6050 			if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
6051 				intel_dp_init(dev, PCH_DP_B);
6052 		}
6053 
6054 		if (I915_READ(HDMIC) & PORT_DETECTED)
6055 			intel_hdmi_init(dev, HDMIC);
6056 
6057 		if (I915_READ(HDMID) & PORT_DETECTED)
6058 			intel_hdmi_init(dev, HDMID);
6059 
6060 		if (I915_READ(PCH_DP_C) & DP_DETECTED)
6061 			intel_dp_init(dev, PCH_DP_C);
6062 
6063 		if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
6064 			intel_dp_init(dev, PCH_DP_D);
6065 
6066 	} else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
6067 		bool found = false;
6068 
6069 		if (I915_READ(SDVOB) & SDVO_DETECTED) {
6070 			DRM_DEBUG_KMS("probing SDVOB\n");
6071 			found = intel_sdvo_init(dev, SDVOB);
6072 			if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
6073 				DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
6074 				intel_hdmi_init(dev, SDVOB);
6075 			}
6076 
6077 			if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
6078 				DRM_DEBUG_KMS("probing DP_B\n");
6079 				intel_dp_init(dev, DP_B);
6080 			}
6081 		}
6082 
6083 		/* Before G4X SDVOC doesn't have its own detect register */
6084 
6085 		if (I915_READ(SDVOB) & SDVO_DETECTED) {
6086 			DRM_DEBUG_KMS("probing SDVOC\n");
6087 			found = intel_sdvo_init(dev, SDVOC);
6088 		}
6089 
6090 		if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
6091 
6092 			if (SUPPORTS_INTEGRATED_HDMI(dev)) {
6093 				DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
6094 				intel_hdmi_init(dev, SDVOC);
6095 			}
6096 			if (SUPPORTS_INTEGRATED_DP(dev)) {
6097 				DRM_DEBUG_KMS("probing DP_C\n");
6098 				intel_dp_init(dev, DP_C);
6099 			}
6100 		}
6101 
6102 		if (SUPPORTS_INTEGRATED_DP(dev) &&
6103 		    (I915_READ(DP_D) & DP_DETECTED)) {
6104 			DRM_DEBUG_KMS("probing DP_D\n");
6105 			intel_dp_init(dev, DP_D);
6106 		}
6107 	} else if (IS_GEN2(dev)) {
6108 #if 1
6109 		KIB_NOTYET();
6110 #else
6111 		intel_dvo_init(dev);
6112 #endif
6113 	}
6114 
6115 	if (SUPPORTS_TV(dev))
6116 		intel_tv_init(dev);
6117 
6118 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
6119 		encoder->base.possible_crtcs = encoder->crtc_mask;
6120 		encoder->base.possible_clones =
6121 			intel_encoder_clones(dev, encoder->clone_mask);
6122 	}
6123 
6124 	/* disable all the possible outputs/crtcs before entering KMS mode */
6125 	drm_helper_disable_unused_functions(dev);
6126 
6127 	if (HAS_PCH_SPLIT(dev))
6128 		ironlake_init_pch_refclk(dev);
6129 }
6130 
6131 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
6132 {
6133 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
6134 
6135 	drm_framebuffer_cleanup(fb);
6136 	drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
6137 
6138 	drm_free(intel_fb, DRM_MEM_KMS);
6139 }
6140 
6141 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
6142 						struct drm_file *file,
6143 						unsigned int *handle)
6144 {
6145 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
6146 	struct drm_i915_gem_object *obj = intel_fb->obj;
6147 
6148 	return drm_gem_handle_create(file, &obj->base, handle);
6149 }
6150 
6151 static const struct drm_framebuffer_funcs intel_fb_funcs = {
6152 	.destroy = intel_user_framebuffer_destroy,
6153 	.create_handle = intel_user_framebuffer_create_handle,
6154 };
6155 
6156 int intel_framebuffer_init(struct drm_device *dev,
6157 			   struct intel_framebuffer *intel_fb,
6158 			   struct drm_mode_fb_cmd2 *mode_cmd,
6159 			   struct drm_i915_gem_object *obj)
6160 {
6161 	int ret;
6162 
6163 	if (obj->tiling_mode == I915_TILING_Y)
6164 		return -EINVAL;
6165 
6166 	if (mode_cmd->pitches[0] & 63)
6167 		return -EINVAL;
6168 
6169 	switch (mode_cmd->pixel_format) {
6170 	case DRM_FORMAT_RGB332:
6171 	case DRM_FORMAT_RGB565:
6172 	case DRM_FORMAT_XRGB8888:
6173 	case DRM_FORMAT_XBGR8888:
6174 	case DRM_FORMAT_ARGB8888:
6175 	case DRM_FORMAT_XRGB2101010:
6176 	case DRM_FORMAT_ARGB2101010:
6177 		/* RGB formats are common across chipsets */
6178 		break;
6179 	case DRM_FORMAT_YUYV:
6180 	case DRM_FORMAT_UYVY:
6181 	case DRM_FORMAT_YVYU:
6182 	case DRM_FORMAT_VYUY:
6183 		break;
6184 	default:
6185 		DRM_DEBUG_KMS("unsupported pixel format %u\n",
6186 				mode_cmd->pixel_format);
6187 		return -EINVAL;
6188 	}
6189 
6190 	ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
6191 	if (ret) {
6192 		DRM_ERROR("framebuffer init failed %d\n", ret);
6193 		return ret;
6194 	}
6195 
6196 	drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
6197 	intel_fb->obj = obj;
6198 	return 0;
6199 }
6200 
6201 static int
6202 intel_user_framebuffer_create(struct drm_device *dev,
6203     struct drm_file *filp, struct drm_mode_fb_cmd2 *mode_cmd,
6204     struct drm_framebuffer **res)
6205 {
6206 	struct drm_i915_gem_object *obj;
6207 
6208 	obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
6209 						mode_cmd->handles[0]));
6210 	if (&obj->base == NULL)
6211 		return (-ENOENT);
6212 
6213 	return (intel_framebuffer_create(dev, mode_cmd, obj, res));
6214 }
6215 
6216 static const struct drm_mode_config_funcs intel_mode_funcs = {
6217 	.fb_create = intel_user_framebuffer_create,
6218 	.output_poll_changed = intel_fb_output_poll_changed,
6219 };
6220 
6221 /* Set up chip specific display functions */
6222 static void intel_init_display(struct drm_device *dev)
6223 {
6224 	struct drm_i915_private *dev_priv = dev->dev_private;
6225 
6226 	/* We always want a DPMS function */
6227 	if (HAS_PCH_SPLIT(dev)) {
6228 		dev_priv->display.dpms = ironlake_crtc_dpms;
6229 		dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
6230 		dev_priv->display.update_plane = ironlake_update_plane;
6231 	} else {
6232 		dev_priv->display.dpms = i9xx_crtc_dpms;
6233 		dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
6234 		dev_priv->display.update_plane = i9xx_update_plane;
6235 	}
6236 
6237 	if (I915_HAS_FBC(dev)) {
6238 		if (HAS_PCH_SPLIT(dev)) {
6239 			dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
6240 			dev_priv->display.enable_fbc = ironlake_enable_fbc;
6241 			dev_priv->display.disable_fbc = ironlake_disable_fbc;
6242 		} else if (IS_GM45(dev)) {
6243 			dev_priv->display.fbc_enabled = g4x_fbc_enabled;
6244 			dev_priv->display.enable_fbc = g4x_enable_fbc;
6245 			dev_priv->display.disable_fbc = g4x_disable_fbc;
6246 		} else if (IS_CRESTLINE(dev)) {
6247 			dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
6248 			dev_priv->display.enable_fbc = i8xx_enable_fbc;
6249 			dev_priv->display.disable_fbc = i8xx_disable_fbc;
6250 		}
6251 		/* 855GM needs testing */
6252 	}
6253 
6254 	/* Returns the core display clock speed */
6255 	if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
6256 		dev_priv->display.get_display_clock_speed =
6257 			i945_get_display_clock_speed;
6258 	else if (IS_I915G(dev))
6259 		dev_priv->display.get_display_clock_speed =
6260 			i915_get_display_clock_speed;
6261 	else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
6262 		dev_priv->display.get_display_clock_speed =
6263 			i9xx_misc_get_display_clock_speed;
6264 	else if (IS_I915GM(dev))
6265 		dev_priv->display.get_display_clock_speed =
6266 			i915gm_get_display_clock_speed;
6267 	else if (IS_I865G(dev))
6268 		dev_priv->display.get_display_clock_speed =
6269 			i865_get_display_clock_speed;
6270 	else if (IS_I85X(dev))
6271 		dev_priv->display.get_display_clock_speed =
6272 			i855_get_display_clock_speed;
6273 	else /* 852, 830 */
6274 		dev_priv->display.get_display_clock_speed =
6275 			i830_get_display_clock_speed;
6276 
6277 	/* For FIFO watermark updates */
6278 	if (HAS_PCH_SPLIT(dev)) {
6279 		dev_priv->display.force_wake_get = __gen6_gt_force_wake_get;
6280 		dev_priv->display.force_wake_put = __gen6_gt_force_wake_put;
6281 
6282 		/* IVB configs may use multi-threaded forcewake */
6283 		if (IS_IVYBRIDGE(dev)) {
6284 			u32	ecobus;
6285 
6286 			/* A small trick here - if the bios hasn't configured MT forcewake,
6287 			 * and if the device is in RC6, then force_wake_mt_get will not wake
6288 			 * the device and the ECOBUS read will return zero. Which will be
6289 			 * (correctly) interpreted by the test below as MT forcewake being
6290 			 * disabled.
6291 			 */
6292 			DRM_LOCK(dev);
6293 			__gen6_gt_force_wake_mt_get(dev_priv);
6294 			ecobus = I915_READ_NOTRACE(ECOBUS);
6295 			__gen6_gt_force_wake_mt_put(dev_priv);
6296 			DRM_UNLOCK(dev);
6297 
6298 			if (ecobus & FORCEWAKE_MT_ENABLE) {
6299 				DRM_DEBUG_KMS("Using MT version of forcewake\n");
6300 				dev_priv->display.force_wake_get =
6301 					__gen6_gt_force_wake_mt_get;
6302 				dev_priv->display.force_wake_put =
6303 					__gen6_gt_force_wake_mt_put;
6304 			}
6305 		}
6306 
6307 		if (HAS_PCH_IBX(dev))
6308 			dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
6309 		else if (HAS_PCH_CPT(dev))
6310 			dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
6311 
6312 		if (IS_GEN5(dev)) {
6313 			if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
6314 				dev_priv->display.update_wm = ironlake_update_wm;
6315 			else {
6316 				DRM_DEBUG_KMS("Failed to get proper latency. "
6317 					      "Disable CxSR\n");
6318 				dev_priv->display.update_wm = NULL;
6319 			}
6320 			dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
6321 			dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
6322 			dev_priv->display.write_eld = ironlake_write_eld;
6323 		} else if (IS_GEN6(dev)) {
6324 			if (SNB_READ_WM0_LATENCY()) {
6325 				dev_priv->display.update_wm = sandybridge_update_wm;
6326 				dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
6327 			} else {
6328 				DRM_DEBUG_KMS("Failed to read display plane latency. "
6329 					      "Disable CxSR\n");
6330 				dev_priv->display.update_wm = NULL;
6331 			}
6332 			dev_priv->display.fdi_link_train = gen6_fdi_link_train;
6333 			dev_priv->display.init_clock_gating = gen6_init_clock_gating;
6334 			dev_priv->display.write_eld = ironlake_write_eld;
6335 		} else if (IS_IVYBRIDGE(dev)) {
6336 			/* FIXME: detect B0+ stepping and use auto training */
6337 			dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
6338 			if (SNB_READ_WM0_LATENCY()) {
6339 				dev_priv->display.update_wm = sandybridge_update_wm;
6340 				dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
6341 			} else {
6342 				DRM_DEBUG_KMS("Failed to read display plane latency. "
6343 					      "Disable CxSR\n");
6344 				dev_priv->display.update_wm = NULL;
6345 			}
6346 			dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
6347 			dev_priv->display.write_eld = ironlake_write_eld;
6348 		} else
6349 			dev_priv->display.update_wm = NULL;
6350 	} else if (IS_PINEVIEW(dev)) {
6351 		if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
6352 					    dev_priv->is_ddr3,
6353 					    dev_priv->fsb_freq,
6354 					    dev_priv->mem_freq)) {
6355 			DRM_INFO("failed to find known CxSR latency "
6356 				 "(found ddr%s fsb freq %d, mem freq %d), "
6357 				 "disabling CxSR\n",
6358 				 (dev_priv->is_ddr3 == 1) ? "3" : "2",
6359 				 dev_priv->fsb_freq, dev_priv->mem_freq);
6360 			/* Disable CxSR and never update its watermark again */
6361 			pineview_disable_cxsr(dev);
6362 			dev_priv->display.update_wm = NULL;
6363 		} else
6364 			dev_priv->display.update_wm = pineview_update_wm;
6365 		dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6366 	} else if (IS_G4X(dev)) {
6367 		dev_priv->display.write_eld = g4x_write_eld;
6368 		dev_priv->display.update_wm = g4x_update_wm;
6369 		dev_priv->display.init_clock_gating = g4x_init_clock_gating;
6370 	} else if (IS_GEN4(dev)) {
6371 		dev_priv->display.update_wm = i965_update_wm;
6372 		if (IS_CRESTLINE(dev))
6373 			dev_priv->display.init_clock_gating = crestline_init_clock_gating;
6374 		else if (IS_BROADWATER(dev))
6375 			dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
6376 	} else if (IS_GEN3(dev)) {
6377 		dev_priv->display.update_wm = i9xx_update_wm;
6378 		dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
6379 		dev_priv->display.init_clock_gating = gen3_init_clock_gating;
6380 	} else if (IS_I865G(dev)) {
6381 		dev_priv->display.update_wm = i830_update_wm;
6382 		dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6383 		dev_priv->display.get_fifo_size = i830_get_fifo_size;
6384 	} else if (IS_I85X(dev)) {
6385 		dev_priv->display.update_wm = i9xx_update_wm;
6386 		dev_priv->display.get_fifo_size = i85x_get_fifo_size;
6387 		dev_priv->display.init_clock_gating = i85x_init_clock_gating;
6388 	} else {
6389 		dev_priv->display.update_wm = i830_update_wm;
6390 		dev_priv->display.init_clock_gating = i830_init_clock_gating;
6391 		if (IS_845G(dev))
6392 			dev_priv->display.get_fifo_size = i845_get_fifo_size;
6393 		else
6394 			dev_priv->display.get_fifo_size = i830_get_fifo_size;
6395 	}
6396 
6397 	/* Default just returns -ENODEV to indicate unsupported */
6398 	dev_priv->display.queue_flip = intel_default_queue_flip;
6399 
6400 	switch (INTEL_INFO(dev)->gen) {
6401 	case 2:
6402 		dev_priv->display.queue_flip = intel_gen2_queue_flip;
6403 		break;
6404 
6405 	case 3:
6406 		dev_priv->display.queue_flip = intel_gen3_queue_flip;
6407 		break;
6408 
6409 	case 4:
6410 	case 5:
6411 		dev_priv->display.queue_flip = intel_gen4_queue_flip;
6412 		break;
6413 
6414 	case 6:
6415 		dev_priv->display.queue_flip = intel_gen6_queue_flip;
6416 		break;
6417 	case 7:
6418 		dev_priv->display.queue_flip = intel_gen7_queue_flip;
6419 		break;
6420 	}
6421 }
6422 
6423 /*
6424  * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
6425  * resume, or other times.  This quirk makes sure that's the case for
6426  * affected systems.
6427  */
6428 static void quirk_pipea_force(struct drm_device *dev)
6429 {
6430 	struct drm_i915_private *dev_priv = dev->dev_private;
6431 
6432 	dev_priv->quirks |= QUIRK_PIPEA_FORCE;
6433 	DRM_DEBUG("applying pipe a force quirk\n");
6434 }
6435 
6436 /*
6437  * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
6438  */
6439 static void quirk_ssc_force_disable(struct drm_device *dev)
6440 {
6441 	struct drm_i915_private *dev_priv = dev->dev_private;
6442 	dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
6443 }
6444 
6445 struct intel_quirk {
6446 	int device;
6447 	int subsystem_vendor;
6448 	int subsystem_device;
6449 	void (*hook)(struct drm_device *dev);
6450 };
6451 
6452 #define	PCI_ANY_ID	(~0u)
6453 
6454 struct intel_quirk intel_quirks[] = {
6455 	/* HP Mini needs pipe A force quirk (LP: #322104) */
6456 	{ 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
6457 
6458 	/* Thinkpad R31 needs pipe A force quirk */
6459 	{ 0x3577, 0x1014, 0x0505, quirk_pipea_force },
6460 	/* Toshiba Protege R-205, S-209 needs pipe A force quirk */
6461 	{ 0x2592, 0x1179, 0x0001, quirk_pipea_force },
6462 
6463 	/* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
6464 	{ 0x3577,  0x1014, 0x0513, quirk_pipea_force },
6465 	/* ThinkPad X40 needs pipe A force quirk */
6466 
6467 	/* ThinkPad T60 needs pipe A force quirk (bug #16494) */
6468 	{ 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
6469 
6470 	/* 855 & before need to leave pipe A & dpll A up */
6471 	{ 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6472 	{ 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6473 
6474 	/* Lenovo U160 cannot use SSC on LVDS */
6475 	{ 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
6476 
6477 	/* Sony Vaio Y cannot use SSC on LVDS */
6478 	{ 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
6479 };
6480 
6481 static void intel_init_quirks(struct drm_device *dev)
6482 {
6483 	struct intel_quirk *q;
6484 	device_t d;
6485 	int i;
6486 
6487 	d = dev->dev;
6488 	for (i = 0; i < DRM_ARRAY_SIZE(intel_quirks); i++) {
6489 		q = &intel_quirks[i];
6490 		if (pci_get_device(d) == q->device &&
6491 		    (pci_get_subvendor(d) == q->subsystem_vendor ||
6492 		     q->subsystem_vendor == PCI_ANY_ID) &&
6493 		    (pci_get_subdevice(d) == q->subsystem_device ||
6494 		     q->subsystem_device == PCI_ANY_ID))
6495 			q->hook(dev);
6496 	}
6497 }
6498 
6499 /* Disable the VGA plane that we never use */
6500 static void i915_disable_vga(struct drm_device *dev)
6501 {
6502 	struct drm_i915_private *dev_priv = dev->dev_private;
6503 	u8 sr1;
6504 	u32 vga_reg;
6505 
6506 	if (HAS_PCH_SPLIT(dev))
6507 		vga_reg = CPU_VGACNTRL;
6508 	else
6509 		vga_reg = VGACNTRL;
6510 
6511 #if 0
6512 	vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
6513 #endif
6514 	outb(VGA_SR_INDEX, 1);
6515 	sr1 = inb(VGA_SR_DATA);
6516 	outb(VGA_SR_DATA, sr1 | 1 << 5);
6517 #if 0
6518 	vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
6519 #endif
6520 	DELAY(300);
6521 
6522 	I915_WRITE(vga_reg, VGA_DISP_DISABLE);
6523 	POSTING_READ(vga_reg);
6524 }
6525 
6526 void intel_modeset_init(struct drm_device *dev)
6527 {
6528 	struct drm_i915_private *dev_priv = dev->dev_private;
6529 	int i, ret;
6530 
6531 	drm_mode_config_init(dev);
6532 
6533 	dev->mode_config.min_width = 0;
6534 	dev->mode_config.min_height = 0;
6535 
6536 	dev->mode_config.preferred_depth = 24;
6537 	dev->mode_config.prefer_shadow = 1;
6538 
6539 	dev->mode_config.funcs = __DECONST(struct drm_mode_config_funcs *,
6540 	    &intel_mode_funcs);
6541 
6542 	intel_init_quirks(dev);
6543 
6544 	intel_init_display(dev);
6545 
6546 	if (IS_GEN2(dev)) {
6547 		dev->mode_config.max_width = 2048;
6548 		dev->mode_config.max_height = 2048;
6549 	} else if (IS_GEN3(dev)) {
6550 		dev->mode_config.max_width = 4096;
6551 		dev->mode_config.max_height = 4096;
6552 	} else {
6553 		dev->mode_config.max_width = 8192;
6554 		dev->mode_config.max_height = 8192;
6555 	}
6556 	dev->mode_config.fb_base = dev->agp->base;
6557 
6558 	DRM_DEBUG_KMS("%d display pipe%s available.\n",
6559 		      dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
6560 
6561 	for (i = 0; i < dev_priv->num_pipe; i++) {
6562 		intel_crtc_init(dev, i);
6563 		ret = intel_plane_init(dev, i);
6564 		if (ret)
6565 			DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
6566 	}
6567 
6568 	/* Just disable it once at startup */
6569 	i915_disable_vga(dev);
6570 	intel_setup_outputs(dev);
6571 
6572 	intel_init_clock_gating(dev);
6573 
6574 	if (IS_IRONLAKE_M(dev)) {
6575 		ironlake_enable_drps(dev);
6576 		intel_init_emon(dev);
6577 	}
6578 
6579 	if (IS_GEN6(dev)) {
6580 		gen6_enable_rps(dev_priv);
6581 		gen6_update_ring_freq(dev_priv);
6582 	}
6583 
6584 	TASK_INIT(&dev_priv->idle_task, 0, intel_idle_update, dev_priv);
6585 	callout_init_mp(&dev_priv->idle_callout);
6586 }
6587 
6588 void intel_modeset_gem_init(struct drm_device *dev)
6589 {
6590 	if (IS_IRONLAKE_M(dev))
6591 		ironlake_enable_rc6(dev);
6592 
6593 	intel_setup_overlay(dev);
6594 }
6595 
6596 void intel_modeset_cleanup(struct drm_device *dev)
6597 {
6598 	struct drm_i915_private *dev_priv = dev->dev_private;
6599 	struct drm_crtc *crtc;
6600 	struct intel_crtc *intel_crtc;
6601 
6602 	drm_kms_helper_poll_fini(dev);
6603 	DRM_LOCK(dev);
6604 
6605 #if 0
6606 	intel_unregister_dsm_handler();
6607 #endif
6608 
6609 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6610 		/* Skip inactive CRTCs */
6611 		if (!crtc->fb)
6612 			continue;
6613 
6614 		intel_crtc = to_intel_crtc(crtc);
6615 		intel_increase_pllclock(crtc);
6616 	}
6617 
6618 	intel_disable_fbc(dev);
6619 
6620 	if (IS_IRONLAKE_M(dev))
6621 		ironlake_disable_drps(dev);
6622 	if (IS_GEN6(dev))
6623 		gen6_disable_rps(dev);
6624 
6625 	if (IS_IRONLAKE_M(dev))
6626 		ironlake_disable_rc6(dev);
6627 
6628 	/* Disable the irq before mode object teardown, for the irq might
6629 	 * enqueue unpin/hotplug work. */
6630 	drm_irq_uninstall(dev);
6631 	DRM_UNLOCK(dev);
6632 
6633 	if (taskqueue_cancel(dev_priv->tq, &dev_priv->hotplug_task, NULL))
6634 		taskqueue_drain(dev_priv->tq, &dev_priv->hotplug_task);
6635 	if (taskqueue_cancel(dev_priv->tq, &dev_priv->rps_task, NULL))
6636 		taskqueue_drain(dev_priv->tq, &dev_priv->rps_task);
6637 
6638 	/* Shut off idle work before the crtcs get freed. */
6639 	if (taskqueue_cancel(dev_priv->tq, &dev_priv->idle_task, NULL))
6640 		taskqueue_drain(dev_priv->tq, &dev_priv->idle_task);
6641 
6642 	drm_mode_config_cleanup(dev);
6643 }
6644 
6645 /*
6646  * Return which encoder is currently attached for connector.
6647  */
6648 struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
6649 {
6650 	return &intel_attached_encoder(connector)->base;
6651 }
6652 
6653 void intel_connector_attach_encoder(struct intel_connector *connector,
6654 				    struct intel_encoder *encoder)
6655 {
6656 	connector->encoder = encoder;
6657 	drm_mode_connector_attach_encoder(&connector->base,
6658 					  &encoder->base);
6659 }
6660 
6661 /*
6662  * set vga decode state - true == enable VGA decode
6663  */
6664 int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
6665 {
6666 	struct drm_i915_private *dev_priv;
6667 	device_t bridge_dev;
6668 	u16 gmch_ctrl;
6669 
6670 	dev_priv = dev->dev_private;
6671 	bridge_dev = intel_gtt_get_bridge_device();
6672 	gmch_ctrl = pci_read_config(bridge_dev, INTEL_GMCH_CTRL, 2);
6673 	if (state)
6674 		gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
6675 	else
6676 		gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
6677 	pci_write_config(bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl, 2);
6678 	return (0);
6679 }
6680 
6681 struct intel_display_error_state {
6682 	struct intel_cursor_error_state {
6683 		u32 control;
6684 		u32 position;
6685 		u32 base;
6686 		u32 size;
6687 	} cursor[2];
6688 
6689 	struct intel_pipe_error_state {
6690 		u32 conf;
6691 		u32 source;
6692 
6693 		u32 htotal;
6694 		u32 hblank;
6695 		u32 hsync;
6696 		u32 vtotal;
6697 		u32 vblank;
6698 		u32 vsync;
6699 	} pipe[2];
6700 
6701 	struct intel_plane_error_state {
6702 		u32 control;
6703 		u32 stride;
6704 		u32 size;
6705 		u32 pos;
6706 		u32 addr;
6707 		u32 surface;
6708 		u32 tile_offset;
6709 	} plane[2];
6710 };
6711 
6712 struct intel_display_error_state *
6713 intel_display_capture_error_state(struct drm_device *dev)
6714 {
6715 	drm_i915_private_t *dev_priv = dev->dev_private;
6716 	struct intel_display_error_state *error;
6717 	int i;
6718 
6719 	error = kmalloc(sizeof(*error), DRM_MEM_KMS, M_NOWAIT);
6720 	if (error == NULL)
6721 		return NULL;
6722 
6723 	for (i = 0; i < 2; i++) {
6724 		error->cursor[i].control = I915_READ(CURCNTR(i));
6725 		error->cursor[i].position = I915_READ(CURPOS(i));
6726 		error->cursor[i].base = I915_READ(CURBASE(i));
6727 
6728 		error->plane[i].control = I915_READ(DSPCNTR(i));
6729 		error->plane[i].stride = I915_READ(DSPSTRIDE(i));
6730 		error->plane[i].size = I915_READ(DSPSIZE(i));
6731 		error->plane[i].pos = I915_READ(DSPPOS(i));
6732 		error->plane[i].addr = I915_READ(DSPADDR(i));
6733 		if (INTEL_INFO(dev)->gen >= 4) {
6734 			error->plane[i].surface = I915_READ(DSPSURF(i));
6735 			error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
6736 		}
6737 
6738 		error->pipe[i].conf = I915_READ(PIPECONF(i));
6739 		error->pipe[i].source = I915_READ(PIPESRC(i));
6740 		error->pipe[i].htotal = I915_READ(HTOTAL(i));
6741 		error->pipe[i].hblank = I915_READ(HBLANK(i));
6742 		error->pipe[i].hsync = I915_READ(HSYNC(i));
6743 		error->pipe[i].vtotal = I915_READ(VTOTAL(i));
6744 		error->pipe[i].vblank = I915_READ(VBLANK(i));
6745 		error->pipe[i].vsync = I915_READ(VSYNC(i));
6746 	}
6747 
6748 	return error;
6749 }
6750 
6751 void
6752 intel_display_print_error_state(struct sbuf *m,
6753 				struct drm_device *dev,
6754 				struct intel_display_error_state *error)
6755 {
6756 	int i;
6757 
6758 	for (i = 0; i < 2; i++) {
6759 		sbuf_printf(m, "Pipe [%d]:\n", i);
6760 		sbuf_printf(m, "  CONF: %08x\n", error->pipe[i].conf);
6761 		sbuf_printf(m, "  SRC: %08x\n", error->pipe[i].source);
6762 		sbuf_printf(m, "  HTOTAL: %08x\n", error->pipe[i].htotal);
6763 		sbuf_printf(m, "  HBLANK: %08x\n", error->pipe[i].hblank);
6764 		sbuf_printf(m, "  HSYNC: %08x\n", error->pipe[i].hsync);
6765 		sbuf_printf(m, "  VTOTAL: %08x\n", error->pipe[i].vtotal);
6766 		sbuf_printf(m, "  VBLANK: %08x\n", error->pipe[i].vblank);
6767 		sbuf_printf(m, "  VSYNC: %08x\n", error->pipe[i].vsync);
6768 
6769 		sbuf_printf(m, "Plane [%d]:\n", i);
6770 		sbuf_printf(m, "  CNTR: %08x\n", error->plane[i].control);
6771 		sbuf_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
6772 		sbuf_printf(m, "  SIZE: %08x\n", error->plane[i].size);
6773 		sbuf_printf(m, "  POS: %08x\n", error->plane[i].pos);
6774 		sbuf_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
6775 		if (INTEL_INFO(dev)->gen >= 4) {
6776 			sbuf_printf(m, "  SURF: %08x\n", error->plane[i].surface);
6777 			sbuf_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
6778 		}
6779 
6780 		sbuf_printf(m, "Cursor [%d]:\n", i);
6781 		sbuf_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
6782 		sbuf_printf(m, "  POS: %08x\n", error->cursor[i].position);
6783 		sbuf_printf(m, "  BASE: %08x\n", error->cursor[i].base);
6784 	}
6785 }
6786