blob: b4482b22a1ac2167cd508cc2b18387d5dcc6a188 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
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
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
David Howells760285e2012-10-02 18:01:07 +010040#include <drm/drm_dp_helper.h>
41#include <drm/drm_crtc_helper.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080042#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080043
Akshay Joshi0206e352011-08-16 15:34:10 -040044bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
Daniel Vetter3dec0092010-08-20 21:40:52 +020045static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010046static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080047
48typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040049 /* given values */
50 int n;
51 int m1, m2;
52 int p1, p2;
53 /* derived values */
54 int dot;
55 int vco;
56 int m;
57 int p;
Jesse Barnes79e53942008-11-07 14:24:08 -080058} intel_clock_t;
59
60typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040061 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080062} intel_range_t;
63
64typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040065 int dot_limit;
66 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080067} intel_p2_t;
68
69#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080070typedef struct intel_limit intel_limit_t;
71struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040072 intel_range_t dot, vco, n, m, m1, m2, p, p1;
73 intel_p2_t p2;
Ville Syrjäläf4808ab2013-02-28 19:19:44 +020074 /**
75 * find_pll() - Find the best values for the PLL
76 * @limit: limits for the PLL
77 * @crtc: current CRTC
78 * @target: target frequency in kHz
79 * @refclk: reference clock frequency in kHz
80 * @match_clock: if provided, @best_clock P divider must
81 * match the P divider from @match_clock
82 * used for LVDS downclocking
83 * @best_clock: best PLL values found
84 *
85 * Returns true on success, false on failure.
86 */
87 bool (*find_pll)(const intel_limit_t *limit,
88 struct drm_crtc *crtc,
89 int target, int refclk,
90 intel_clock_t *match_clock,
91 intel_clock_t *best_clock);
Ma Lingd4906092009-03-18 20:13:27 +080092};
Jesse Barnes79e53942008-11-07 14:24:08 -080093
Jesse Barnes2377b742010-07-07 14:06:43 -070094/* FDI */
95#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
96
Daniel Vetterd2acd212012-10-20 20:57:43 +020097int
98intel_pch_rawclk(struct drm_device *dev)
99{
100 struct drm_i915_private *dev_priv = dev->dev_private;
101
102 WARN_ON(!HAS_PCH_SPLIT(dev));
103
104 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
105}
106
Ma Lingd4906092009-03-18 20:13:27 +0800107static bool
108intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800109 int target, int refclk, intel_clock_t *match_clock,
110 intel_clock_t *best_clock);
Ma Lingd4906092009-03-18 20:13:27 +0800111static bool
112intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800113 int target, int refclk, intel_clock_t *match_clock,
114 intel_clock_t *best_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800115
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700116static bool
117intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800118 int target, int refclk, intel_clock_t *match_clock,
119 intel_clock_t *best_clock);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800120static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500121intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800122 int target, int refclk, intel_clock_t *match_clock,
123 intel_clock_t *best_clock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700124
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700125static bool
126intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
127 int target, int refclk, intel_clock_t *match_clock,
128 intel_clock_t *best_clock);
129
Chris Wilson021357a2010-09-07 20:54:59 +0100130static inline u32 /* units of 100MHz */
131intel_fdi_link_freq(struct drm_device *dev)
132{
Chris Wilson8b99e682010-10-13 09:59:17 +0100133 if (IS_GEN5(dev)) {
134 struct drm_i915_private *dev_priv = dev->dev_private;
135 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
136 } else
137 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100138}
139
Keith Packarde4b36692009-06-05 19:22:17 -0700140static const intel_limit_t intel_limits_i8xx_dvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400141 .dot = { .min = 25000, .max = 350000 },
142 .vco = { .min = 930000, .max = 1400000 },
143 .n = { .min = 3, .max = 16 },
144 .m = { .min = 96, .max = 140 },
145 .m1 = { .min = 18, .max = 26 },
146 .m2 = { .min = 6, .max = 16 },
147 .p = { .min = 4, .max = 128 },
148 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700149 .p2 = { .dot_limit = 165000,
150 .p2_slow = 4, .p2_fast = 2 },
Ma Lingd4906092009-03-18 20:13:27 +0800151 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700152};
153
154static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400155 .dot = { .min = 25000, .max = 350000 },
156 .vco = { .min = 930000, .max = 1400000 },
157 .n = { .min = 3, .max = 16 },
158 .m = { .min = 96, .max = 140 },
159 .m1 = { .min = 18, .max = 26 },
160 .m2 = { .min = 6, .max = 16 },
161 .p = { .min = 4, .max = 128 },
162 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700163 .p2 = { .dot_limit = 165000,
164 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800165 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700166};
Eric Anholt273e27c2011-03-30 13:01:10 -0700167
Keith Packarde4b36692009-06-05 19:22:17 -0700168static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400169 .dot = { .min = 20000, .max = 400000 },
170 .vco = { .min = 1400000, .max = 2800000 },
171 .n = { .min = 1, .max = 6 },
172 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100173 .m1 = { .min = 8, .max = 18 },
174 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400175 .p = { .min = 5, .max = 80 },
176 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700177 .p2 = { .dot_limit = 200000,
178 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800179 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700180};
181
182static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400183 .dot = { .min = 20000, .max = 400000 },
184 .vco = { .min = 1400000, .max = 2800000 },
185 .n = { .min = 1, .max = 6 },
186 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100187 .m1 = { .min = 8, .max = 18 },
188 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400189 .p = { .min = 7, .max = 98 },
190 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700191 .p2 = { .dot_limit = 112000,
192 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800193 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700194};
195
Eric Anholt273e27c2011-03-30 13:01:10 -0700196
Keith Packarde4b36692009-06-05 19:22:17 -0700197static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700198 .dot = { .min = 25000, .max = 270000 },
199 .vco = { .min = 1750000, .max = 3500000},
200 .n = { .min = 1, .max = 4 },
201 .m = { .min = 104, .max = 138 },
202 .m1 = { .min = 17, .max = 23 },
203 .m2 = { .min = 5, .max = 11 },
204 .p = { .min = 10, .max = 30 },
205 .p1 = { .min = 1, .max = 3},
206 .p2 = { .dot_limit = 270000,
207 .p2_slow = 10,
208 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800209 },
Ma Lingd4906092009-03-18 20:13:27 +0800210 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700211};
212
213static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700214 .dot = { .min = 22000, .max = 400000 },
215 .vco = { .min = 1750000, .max = 3500000},
216 .n = { .min = 1, .max = 4 },
217 .m = { .min = 104, .max = 138 },
218 .m1 = { .min = 16, .max = 23 },
219 .m2 = { .min = 5, .max = 11 },
220 .p = { .min = 5, .max = 80 },
221 .p1 = { .min = 1, .max = 8},
222 .p2 = { .dot_limit = 165000,
223 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800224 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700225};
226
227static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700228 .dot = { .min = 20000, .max = 115000 },
229 .vco = { .min = 1750000, .max = 3500000 },
230 .n = { .min = 1, .max = 3 },
231 .m = { .min = 104, .max = 138 },
232 .m1 = { .min = 17, .max = 23 },
233 .m2 = { .min = 5, .max = 11 },
234 .p = { .min = 28, .max = 112 },
235 .p1 = { .min = 2, .max = 8 },
236 .p2 = { .dot_limit = 0,
237 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800238 },
Ma Lingd4906092009-03-18 20:13:27 +0800239 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700240};
241
242static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700243 .dot = { .min = 80000, .max = 224000 },
244 .vco = { .min = 1750000, .max = 3500000 },
245 .n = { .min = 1, .max = 3 },
246 .m = { .min = 104, .max = 138 },
247 .m1 = { .min = 17, .max = 23 },
248 .m2 = { .min = 5, .max = 11 },
249 .p = { .min = 14, .max = 42 },
250 .p1 = { .min = 2, .max = 6 },
251 .p2 = { .dot_limit = 0,
252 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800253 },
Ma Lingd4906092009-03-18 20:13:27 +0800254 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700255};
256
257static const intel_limit_t intel_limits_g4x_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400258 .dot = { .min = 161670, .max = 227000 },
259 .vco = { .min = 1750000, .max = 3500000},
260 .n = { .min = 1, .max = 2 },
261 .m = { .min = 97, .max = 108 },
262 .m1 = { .min = 0x10, .max = 0x12 },
263 .m2 = { .min = 0x05, .max = 0x06 },
264 .p = { .min = 10, .max = 20 },
265 .p1 = { .min = 1, .max = 2},
266 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700267 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400268 .find_pll = intel_find_pll_g4x_dp,
Keith Packarde4b36692009-06-05 19:22:17 -0700269};
270
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500271static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400272 .dot = { .min = 20000, .max = 400000},
273 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700274 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400275 .n = { .min = 3, .max = 6 },
276 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700277 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400278 .m1 = { .min = 0, .max = 0 },
279 .m2 = { .min = 0, .max = 254 },
280 .p = { .min = 5, .max = 80 },
281 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700282 .p2 = { .dot_limit = 200000,
283 .p2_slow = 10, .p2_fast = 5 },
Shaohua Li61157072009-04-03 15:24:43 +0800284 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700285};
286
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500287static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400288 .dot = { .min = 20000, .max = 400000 },
289 .vco = { .min = 1700000, .max = 3500000 },
290 .n = { .min = 3, .max = 6 },
291 .m = { .min = 2, .max = 256 },
292 .m1 = { .min = 0, .max = 0 },
293 .m2 = { .min = 0, .max = 254 },
294 .p = { .min = 7, .max = 112 },
295 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700296 .p2 = { .dot_limit = 112000,
297 .p2_slow = 14, .p2_fast = 14 },
Shaohua Li61157072009-04-03 15:24:43 +0800298 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700299};
300
Eric Anholt273e27c2011-03-30 13:01:10 -0700301/* Ironlake / Sandybridge
302 *
303 * We calculate clock using (register_value + 2) for N/M1/M2, so here
304 * the range value for them is (actual_value - 2).
305 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800306static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700307 .dot = { .min = 25000, .max = 350000 },
308 .vco = { .min = 1760000, .max = 3510000 },
309 .n = { .min = 1, .max = 5 },
310 .m = { .min = 79, .max = 127 },
311 .m1 = { .min = 12, .max = 22 },
312 .m2 = { .min = 5, .max = 9 },
313 .p = { .min = 5, .max = 80 },
314 .p1 = { .min = 1, .max = 8 },
315 .p2 = { .dot_limit = 225000,
316 .p2_slow = 10, .p2_fast = 5 },
Zhao Yakui45476682009-12-31 16:06:04 +0800317 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700318};
319
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800320static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700321 .dot = { .min = 25000, .max = 350000 },
322 .vco = { .min = 1760000, .max = 3510000 },
323 .n = { .min = 1, .max = 3 },
324 .m = { .min = 79, .max = 118 },
325 .m1 = { .min = 12, .max = 22 },
326 .m2 = { .min = 5, .max = 9 },
327 .p = { .min = 28, .max = 112 },
328 .p1 = { .min = 2, .max = 8 },
329 .p2 = { .dot_limit = 225000,
330 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800331 .find_pll = intel_g4x_find_best_PLL,
332};
333
334static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700335 .dot = { .min = 25000, .max = 350000 },
336 .vco = { .min = 1760000, .max = 3510000 },
337 .n = { .min = 1, .max = 3 },
338 .m = { .min = 79, .max = 127 },
339 .m1 = { .min = 12, .max = 22 },
340 .m2 = { .min = 5, .max = 9 },
341 .p = { .min = 14, .max = 56 },
342 .p1 = { .min = 2, .max = 8 },
343 .p2 = { .dot_limit = 225000,
344 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800345 .find_pll = intel_g4x_find_best_PLL,
346};
347
Eric Anholt273e27c2011-03-30 13:01:10 -0700348/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800349static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700350 .dot = { .min = 25000, .max = 350000 },
351 .vco = { .min = 1760000, .max = 3510000 },
352 .n = { .min = 1, .max = 2 },
353 .m = { .min = 79, .max = 126 },
354 .m1 = { .min = 12, .max = 22 },
355 .m2 = { .min = 5, .max = 9 },
356 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400357 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700358 .p2 = { .dot_limit = 225000,
359 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800360 .find_pll = intel_g4x_find_best_PLL,
361};
362
363static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700364 .dot = { .min = 25000, .max = 350000 },
365 .vco = { .min = 1760000, .max = 3510000 },
366 .n = { .min = 1, .max = 3 },
367 .m = { .min = 79, .max = 126 },
368 .m1 = { .min = 12, .max = 22 },
369 .m2 = { .min = 5, .max = 9 },
370 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400371 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700372 .p2 = { .dot_limit = 225000,
373 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800374 .find_pll = intel_g4x_find_best_PLL,
375};
376
377static const intel_limit_t intel_limits_ironlake_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400378 .dot = { .min = 25000, .max = 350000 },
379 .vco = { .min = 1760000, .max = 3510000},
380 .n = { .min = 1, .max = 2 },
381 .m = { .min = 81, .max = 90 },
382 .m1 = { .min = 12, .max = 22 },
383 .m2 = { .min = 5, .max = 9 },
384 .p = { .min = 10, .max = 20 },
385 .p1 = { .min = 1, .max = 2},
386 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700387 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400388 .find_pll = intel_find_pll_ironlake_dp,
Jesse Barnes79e53942008-11-07 14:24:08 -0800389};
390
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700391static const intel_limit_t intel_limits_vlv_dac = {
392 .dot = { .min = 25000, .max = 270000 },
393 .vco = { .min = 4000000, .max = 6000000 },
394 .n = { .min = 1, .max = 7 },
395 .m = { .min = 22, .max = 450 }, /* guess */
396 .m1 = { .min = 2, .max = 3 },
397 .m2 = { .min = 11, .max = 156 },
398 .p = { .min = 10, .max = 30 },
399 .p1 = { .min = 2, .max = 3 },
400 .p2 = { .dot_limit = 270000,
401 .p2_slow = 2, .p2_fast = 20 },
402 .find_pll = intel_vlv_find_best_pll,
403};
404
405static const intel_limit_t intel_limits_vlv_hdmi = {
406 .dot = { .min = 20000, .max = 165000 },
Vijay Purushothaman17dc9252012-09-27 19:13:09 +0530407 .vco = { .min = 4000000, .max = 5994000},
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700408 .n = { .min = 1, .max = 7 },
409 .m = { .min = 60, .max = 300 }, /* guess */
410 .m1 = { .min = 2, .max = 3 },
411 .m2 = { .min = 11, .max = 156 },
412 .p = { .min = 10, .max = 30 },
413 .p1 = { .min = 2, .max = 3 },
414 .p2 = { .dot_limit = 270000,
415 .p2_slow = 2, .p2_fast = 20 },
416 .find_pll = intel_vlv_find_best_pll,
417};
418
419static const intel_limit_t intel_limits_vlv_dp = {
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530420 .dot = { .min = 25000, .max = 270000 },
421 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700422 .n = { .min = 1, .max = 7 },
Vijay Purushothaman74a4dd22012-09-27 19:13:04 +0530423 .m = { .min = 22, .max = 450 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700424 .m1 = { .min = 2, .max = 3 },
425 .m2 = { .min = 11, .max = 156 },
426 .p = { .min = 10, .max = 30 },
427 .p1 = { .min = 2, .max = 3 },
428 .p2 = { .dot_limit = 270000,
429 .p2_slow = 2, .p2_fast = 20 },
430 .find_pll = intel_vlv_find_best_pll,
431};
432
Jesse Barnes57f350b2012-03-28 13:39:25 -0700433u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
434{
Daniel Vetter09153002012-12-12 14:06:44 +0100435 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Jesse Barnes57f350b2012-03-28 13:39:25 -0700436
Jesse Barnes57f350b2012-03-28 13:39:25 -0700437 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
438 DRM_ERROR("DPIO idle wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100439 return 0;
Jesse Barnes57f350b2012-03-28 13:39:25 -0700440 }
441
442 I915_WRITE(DPIO_REG, reg);
443 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
444 DPIO_BYTE);
445 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
446 DRM_ERROR("DPIO read wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100447 return 0;
Jesse Barnes57f350b2012-03-28 13:39:25 -0700448 }
Jesse Barnes57f350b2012-03-28 13:39:25 -0700449
Daniel Vetter09153002012-12-12 14:06:44 +0100450 return I915_READ(DPIO_DATA);
Jesse Barnes57f350b2012-03-28 13:39:25 -0700451}
452
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700453static void intel_dpio_write(struct drm_i915_private *dev_priv, int reg,
454 u32 val)
455{
Daniel Vetter09153002012-12-12 14:06:44 +0100456 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700457
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700458 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
459 DRM_ERROR("DPIO idle wait timed out\n");
Daniel Vetter09153002012-12-12 14:06:44 +0100460 return;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700461 }
462
463 I915_WRITE(DPIO_DATA, val);
464 I915_WRITE(DPIO_REG, reg);
465 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_WRITE | DPIO_PORTID |
466 DPIO_BYTE);
467 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100))
468 DRM_ERROR("DPIO write wait timed out\n");
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700469}
470
Jesse Barnes57f350b2012-03-28 13:39:25 -0700471static void vlv_init_dpio(struct drm_device *dev)
472{
473 struct drm_i915_private *dev_priv = dev->dev_private;
474
475 /* Reset the DPIO config */
476 I915_WRITE(DPIO_CTL, 0);
477 POSTING_READ(DPIO_CTL);
478 I915_WRITE(DPIO_CTL, 1);
479 POSTING_READ(DPIO_CTL);
480}
481
Chris Wilson1b894b52010-12-14 20:04:54 +0000482static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
483 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800484{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800485 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800486 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800487
488 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100489 if (intel_is_dual_link_lvds(dev)) {
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800490 /* LVDS dual channel */
Chris Wilson1b894b52010-12-14 20:04:54 +0000491 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800492 limit = &intel_limits_ironlake_dual_lvds_100m;
493 else
494 limit = &intel_limits_ironlake_dual_lvds;
495 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000496 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800497 limit = &intel_limits_ironlake_single_lvds_100m;
498 else
499 limit = &intel_limits_ironlake_single_lvds;
500 }
501 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
Jani Nikula547dc042012-11-02 11:24:03 +0200502 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Zhao Yakui45476682009-12-31 16:06:04 +0800503 limit = &intel_limits_ironlake_display_port;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800504 else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800505 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800506
507 return limit;
508}
509
Ma Ling044c7c42009-03-18 20:13:23 +0800510static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
511{
512 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800513 const intel_limit_t *limit;
514
515 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100516 if (intel_is_dual_link_lvds(dev))
Ma Ling044c7c42009-03-18 20:13:23 +0800517 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700518 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800519 else
520 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700521 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800522 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
523 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700524 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800525 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700526 limit = &intel_limits_g4x_sdvo;
Akshay Joshi0206e352011-08-16 15:34:10 -0400527 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700528 limit = &intel_limits_g4x_display_port;
Ma Ling044c7c42009-03-18 20:13:23 +0800529 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700530 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800531
532 return limit;
533}
534
Chris Wilson1b894b52010-12-14 20:04:54 +0000535static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800536{
537 struct drm_device *dev = crtc->dev;
538 const intel_limit_t *limit;
539
Eric Anholtbad720f2009-10-22 16:11:14 -0700540 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000541 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800542 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800543 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500544 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800545 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500546 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800547 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500548 limit = &intel_limits_pineview_sdvo;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700549 } else if (IS_VALLEYVIEW(dev)) {
550 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG))
551 limit = &intel_limits_vlv_dac;
552 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
553 limit = &intel_limits_vlv_hdmi;
554 else
555 limit = &intel_limits_vlv_dp;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100556 } else if (!IS_GEN2(dev)) {
557 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
558 limit = &intel_limits_i9xx_lvds;
559 else
560 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800561 } else {
562 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700563 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800564 else
Keith Packarde4b36692009-06-05 19:22:17 -0700565 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800566 }
567 return limit;
568}
569
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500570/* m1 is reserved as 0 in Pineview, n is a ring counter */
571static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800572{
Shaohua Li21778322009-02-23 15:19:16 +0800573 clock->m = clock->m2 + 2;
574 clock->p = clock->p1 * clock->p2;
575 clock->vco = refclk * clock->m / clock->n;
576 clock->dot = clock->vco / clock->p;
577}
578
579static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
580{
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500581 if (IS_PINEVIEW(dev)) {
582 pineview_clock(refclk, clock);
Shaohua Li21778322009-02-23 15:19:16 +0800583 return;
584 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800585 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
586 clock->p = clock->p1 * clock->p2;
587 clock->vco = refclk * clock->m / (clock->n + 2);
588 clock->dot = clock->vco / clock->p;
589}
590
Jesse Barnes79e53942008-11-07 14:24:08 -0800591/**
592 * Returns whether any output on the specified pipe is of the specified type
593 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100594bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800595{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100596 struct drm_device *dev = crtc->dev;
Chris Wilson4ef69c72010-09-09 15:14:28 +0100597 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800598
Daniel Vetter6c2b7c12012-07-05 09:50:24 +0200599 for_each_encoder_on_crtc(dev, crtc, encoder)
600 if (encoder->type == type)
Chris Wilson4ef69c72010-09-09 15:14:28 +0100601 return true;
602
603 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800604}
605
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800606#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800607/**
608 * Returns whether the given set of divisors are valid for a given refclk with
609 * the given connectors.
610 */
611
Chris Wilson1b894b52010-12-14 20:04:54 +0000612static bool intel_PLL_is_valid(struct drm_device *dev,
613 const intel_limit_t *limit,
614 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800615{
Jesse Barnes79e53942008-11-07 14:24:08 -0800616 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400617 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800618 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400619 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800620 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400621 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800622 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400623 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500624 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400625 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800626 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400627 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800628 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400629 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800630 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400631 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800632 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
633 * connector, etc., rather than just a single range.
634 */
635 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400636 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800637
638 return true;
639}
640
Ma Lingd4906092009-03-18 20:13:27 +0800641static bool
642intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800643 int target, int refclk, intel_clock_t *match_clock,
644 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800645
Jesse Barnes79e53942008-11-07 14:24:08 -0800646{
647 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800648 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800649 int err = target;
650
Daniel Vettera210b022012-11-26 17:22:08 +0100651 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800652 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100653 * For LVDS just rely on its current settings for dual-channel.
654 * We haven't figured out how to reliably set up different
655 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100657 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800658 clock.p2 = limit->p2.p2_fast;
659 else
660 clock.p2 = limit->p2.p2_slow;
661 } else {
662 if (target < limit->p2.dot_limit)
663 clock.p2 = limit->p2.p2_slow;
664 else
665 clock.p2 = limit->p2.p2_fast;
666 }
667
Akshay Joshi0206e352011-08-16 15:34:10 -0400668 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800669
Zhao Yakui42158662009-11-20 11:24:18 +0800670 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
671 clock.m1++) {
672 for (clock.m2 = limit->m2.min;
673 clock.m2 <= limit->m2.max; clock.m2++) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500674 /* m1 is always 0 in Pineview */
675 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
Zhao Yakui42158662009-11-20 11:24:18 +0800676 break;
677 for (clock.n = limit->n.min;
678 clock.n <= limit->n.max; clock.n++) {
679 for (clock.p1 = limit->p1.min;
680 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800681 int this_err;
682
Shaohua Li21778322009-02-23 15:19:16 +0800683 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000684 if (!intel_PLL_is_valid(dev, limit,
685 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800686 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800687 if (match_clock &&
688 clock.p != match_clock->p)
689 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800690
691 this_err = abs(clock.dot - target);
692 if (this_err < err) {
693 *best_clock = clock;
694 err = this_err;
695 }
696 }
697 }
698 }
699 }
700
701 return (err != target);
702}
703
Ma Lingd4906092009-03-18 20:13:27 +0800704static bool
705intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800706 int target, int refclk, intel_clock_t *match_clock,
707 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800708{
709 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800710 intel_clock_t clock;
711 int max_n;
712 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400713 /* approximately equals target * 0.00585 */
714 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800715 found = false;
716
717 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhao Yakui45476682009-12-31 16:06:04 +0800718 int lvds_reg;
719
Eric Anholtc619eed2010-01-28 16:45:52 -0800720 if (HAS_PCH_SPLIT(dev))
Zhao Yakui45476682009-12-31 16:06:04 +0800721 lvds_reg = PCH_LVDS;
722 else
723 lvds_reg = LVDS;
Daniel Vetter1974cad2012-11-26 17:22:09 +0100724 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800725 clock.p2 = limit->p2.p2_fast;
726 else
727 clock.p2 = limit->p2.p2_slow;
728 } else {
729 if (target < limit->p2.dot_limit)
730 clock.p2 = limit->p2.p2_slow;
731 else
732 clock.p2 = limit->p2.p2_fast;
733 }
734
735 memset(best_clock, 0, sizeof(*best_clock));
736 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200737 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800738 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200739 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800740 for (clock.m1 = limit->m1.max;
741 clock.m1 >= limit->m1.min; clock.m1--) {
742 for (clock.m2 = limit->m2.max;
743 clock.m2 >= limit->m2.min; clock.m2--) {
744 for (clock.p1 = limit->p1.max;
745 clock.p1 >= limit->p1.min; clock.p1--) {
746 int this_err;
747
Shaohua Li21778322009-02-23 15:19:16 +0800748 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000749 if (!intel_PLL_is_valid(dev, limit,
750 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800751 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800752 if (match_clock &&
753 clock.p != match_clock->p)
754 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000755
756 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800757 if (this_err < err_most) {
758 *best_clock = clock;
759 err_most = this_err;
760 max_n = clock.n;
761 found = true;
762 }
763 }
764 }
765 }
766 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800767 return found;
768}
Ma Lingd4906092009-03-18 20:13:27 +0800769
Zhenyu Wang2c072452009-06-05 15:38:42 +0800770static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500771intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800772 int target, int refclk, intel_clock_t *match_clock,
773 intel_clock_t *best_clock)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800774{
775 struct drm_device *dev = crtc->dev;
776 intel_clock_t clock;
Zhao Yakui45476682009-12-31 16:06:04 +0800777
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800778 if (target < 200000) {
779 clock.n = 1;
780 clock.p1 = 2;
781 clock.p2 = 10;
782 clock.m1 = 12;
783 clock.m2 = 9;
784 } else {
785 clock.n = 2;
786 clock.p1 = 1;
787 clock.p2 = 10;
788 clock.m1 = 14;
789 clock.m2 = 8;
790 }
791 intel_clock(dev, refclk, &clock);
792 memcpy(best_clock, &clock, sizeof(intel_clock_t));
793 return true;
794}
795
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700796/* DisplayPort has only two frequencies, 162MHz and 270MHz */
797static bool
798intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800799 int target, int refclk, intel_clock_t *match_clock,
800 intel_clock_t *best_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700801{
Chris Wilson5eddb702010-09-11 13:48:45 +0100802 intel_clock_t clock;
803 if (target < 200000) {
804 clock.p1 = 2;
805 clock.p2 = 10;
806 clock.n = 2;
807 clock.m1 = 23;
808 clock.m2 = 8;
809 } else {
810 clock.p1 = 1;
811 clock.p2 = 10;
812 clock.n = 1;
813 clock.m1 = 14;
814 clock.m2 = 2;
815 }
816 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
817 clock.p = (clock.p1 * clock.p2);
818 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
819 clock.vco = 0;
820 memcpy(best_clock, &clock, sizeof(intel_clock_t));
821 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700822}
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700823static bool
824intel_vlv_find_best_pll(const intel_limit_t *limit, struct drm_crtc *crtc,
825 int target, int refclk, intel_clock_t *match_clock,
826 intel_clock_t *best_clock)
827{
828 u32 p1, p2, m1, m2, vco, bestn, bestm1, bestm2, bestp1, bestp2;
829 u32 m, n, fastclk;
830 u32 updrate, minupdate, fracbits, p;
831 unsigned long bestppm, ppm, absppm;
832 int dotclk, flag;
833
Alan Coxaf447bd2012-07-25 13:49:18 +0100834 flag = 0;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700835 dotclk = target * 1000;
836 bestppm = 1000000;
837 ppm = absppm = 0;
838 fastclk = dotclk / (2*100);
839 updrate = 0;
840 minupdate = 19200;
841 fracbits = 1;
842 n = p = p1 = p2 = m = m1 = m2 = vco = bestn = 0;
843 bestm1 = bestm2 = bestp1 = bestp2 = 0;
844
845 /* based on hardware requirement, prefer smaller n to precision */
846 for (n = limit->n.min; n <= ((refclk) / minupdate); n++) {
847 updrate = refclk / n;
848 for (p1 = limit->p1.max; p1 > limit->p1.min; p1--) {
849 for (p2 = limit->p2.p2_fast+1; p2 > 0; p2--) {
850 if (p2 > 10)
851 p2 = p2 - 1;
852 p = p1 * p2;
853 /* based on hardware requirement, prefer bigger m1,m2 values */
854 for (m1 = limit->m1.min; m1 <= limit->m1.max; m1++) {
855 m2 = (((2*(fastclk * p * n / m1 )) +
856 refclk) / (2*refclk));
857 m = m1 * m2;
858 vco = updrate * m;
859 if (vco >= limit->vco.min && vco < limit->vco.max) {
860 ppm = 1000000 * ((vco / p) - fastclk) / fastclk;
861 absppm = (ppm > 0) ? ppm : (-ppm);
862 if (absppm < 100 && ((p1 * p2) > (bestp1 * bestp2))) {
863 bestppm = 0;
864 flag = 1;
865 }
866 if (absppm < bestppm - 10) {
867 bestppm = absppm;
868 flag = 1;
869 }
870 if (flag) {
871 bestn = n;
872 bestm1 = m1;
873 bestm2 = m2;
874 bestp1 = p1;
875 bestp2 = p2;
876 flag = 0;
877 }
878 }
879 }
880 }
881 }
882 }
883 best_clock->n = bestn;
884 best_clock->m1 = bestm1;
885 best_clock->m2 = bestm2;
886 best_clock->p1 = bestp1;
887 best_clock->p2 = bestp2;
888
889 return true;
890}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700891
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200892enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
893 enum pipe pipe)
894{
895 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
896 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
897
898 return intel_crtc->cpu_transcoder;
899}
900
Paulo Zanonia928d532012-05-04 17:18:15 -0300901static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
902{
903 struct drm_i915_private *dev_priv = dev->dev_private;
904 u32 frame, frame_reg = PIPEFRAME(pipe);
905
906 frame = I915_READ(frame_reg);
907
908 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
909 DRM_DEBUG_KMS("vblank wait timed out\n");
910}
911
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700912/**
913 * intel_wait_for_vblank - wait for vblank on a given pipe
914 * @dev: drm device
915 * @pipe: pipe to wait for
916 *
917 * Wait for vblank to occur on a given pipe. Needed for various bits of
918 * mode setting code.
919 */
920void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800921{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700922 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800923 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700924
Paulo Zanonia928d532012-05-04 17:18:15 -0300925 if (INTEL_INFO(dev)->gen >= 5) {
926 ironlake_wait_for_vblank(dev, pipe);
927 return;
928 }
929
Chris Wilson300387c2010-09-05 20:25:43 +0100930 /* Clear existing vblank status. Note this will clear any other
931 * sticky status fields as well.
932 *
933 * This races with i915_driver_irq_handler() with the result
934 * that either function could miss a vblank event. Here it is not
935 * fatal, as we will either wait upon the next vblank interrupt or
936 * timeout. Generally speaking intel_wait_for_vblank() is only
937 * called during modeset at which time the GPU should be idle and
938 * should *not* be performing page flips and thus not waiting on
939 * vblanks...
940 * Currently, the result of us stealing a vblank from the irq
941 * handler is that a single frame will be skipped during swapbuffers.
942 */
943 I915_WRITE(pipestat_reg,
944 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
945
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700946 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100947 if (wait_for(I915_READ(pipestat_reg) &
948 PIPE_VBLANK_INTERRUPT_STATUS,
949 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700950 DRM_DEBUG_KMS("vblank wait timed out\n");
951}
952
Keith Packardab7ad7f2010-10-03 00:33:06 -0700953/*
954 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700955 * @dev: drm device
956 * @pipe: pipe to wait for
957 *
958 * After disabling a pipe, we can't wait for vblank in the usual way,
959 * spinning on the vblank interrupt status bit, since we won't actually
960 * see an interrupt when the pipe is disabled.
961 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700962 * On Gen4 and above:
963 * wait for the pipe register state bit to turn off
964 *
965 * Otherwise:
966 * wait for the display line value to settle (it usually
967 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100968 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700969 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100970void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700971{
972 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200973 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
974 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700975
Keith Packardab7ad7f2010-10-03 00:33:06 -0700976 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200977 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700978
Keith Packardab7ad7f2010-10-03 00:33:06 -0700979 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100980 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
981 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200982 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700983 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300984 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100985 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700986 unsigned long timeout = jiffies + msecs_to_jiffies(100);
987
Paulo Zanoni837ba002012-05-04 17:18:14 -0300988 if (IS_GEN2(dev))
989 line_mask = DSL_LINEMASK_GEN2;
990 else
991 line_mask = DSL_LINEMASK_GEN3;
992
Keith Packardab7ad7f2010-10-03 00:33:06 -0700993 /* Wait for the display line to settle */
994 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300995 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700996 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300997 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700998 time_after(timeout, jiffies));
999 if (time_after(jiffies, timeout))
Daniel Vetter284637d2012-07-09 09:51:57 +02001000 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001001 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001002}
1003
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001004/*
1005 * ibx_digital_port_connected - is the specified port connected?
1006 * @dev_priv: i915 private structure
1007 * @port: the port to test
1008 *
1009 * Returns true if @port is connected, false otherwise.
1010 */
1011bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1012 struct intel_digital_port *port)
1013{
1014 u32 bit;
1015
Damien Lespiauc36346e2012-12-13 16:09:03 +00001016 if (HAS_PCH_IBX(dev_priv->dev)) {
1017 switch(port->port) {
1018 case PORT_B:
1019 bit = SDE_PORTB_HOTPLUG;
1020 break;
1021 case PORT_C:
1022 bit = SDE_PORTC_HOTPLUG;
1023 break;
1024 case PORT_D:
1025 bit = SDE_PORTD_HOTPLUG;
1026 break;
1027 default:
1028 return true;
1029 }
1030 } else {
1031 switch(port->port) {
1032 case PORT_B:
1033 bit = SDE_PORTB_HOTPLUG_CPT;
1034 break;
1035 case PORT_C:
1036 bit = SDE_PORTC_HOTPLUG_CPT;
1037 break;
1038 case PORT_D:
1039 bit = SDE_PORTD_HOTPLUG_CPT;
1040 break;
1041 default:
1042 return true;
1043 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001044 }
1045
1046 return I915_READ(SDEISR) & bit;
1047}
1048
Jesse Barnesb24e7172011-01-04 15:09:30 -08001049static const char *state_string(bool enabled)
1050{
1051 return enabled ? "on" : "off";
1052}
1053
1054/* Only for pre-ILK configs */
1055static void assert_pll(struct drm_i915_private *dev_priv,
1056 enum pipe pipe, bool state)
1057{
1058 int reg;
1059 u32 val;
1060 bool cur_state;
1061
1062 reg = DPLL(pipe);
1063 val = I915_READ(reg);
1064 cur_state = !!(val & DPLL_VCO_ENABLE);
1065 WARN(cur_state != state,
1066 "PLL state assertion failure (expected %s, current %s)\n",
1067 state_string(state), state_string(cur_state));
1068}
1069#define assert_pll_enabled(d, p) assert_pll(d, p, true)
1070#define assert_pll_disabled(d, p) assert_pll(d, p, false)
1071
Jesse Barnes040484a2011-01-03 12:14:26 -08001072/* For ILK+ */
1073static void assert_pch_pll(struct drm_i915_private *dev_priv,
Chris Wilson92b27b02012-05-20 18:10:50 +01001074 struct intel_pch_pll *pll,
1075 struct intel_crtc *crtc,
1076 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001077{
Jesse Barnes040484a2011-01-03 12:14:26 -08001078 u32 val;
1079 bool cur_state;
1080
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001081 if (HAS_PCH_LPT(dev_priv->dev)) {
1082 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1083 return;
1084 }
1085
Chris Wilson92b27b02012-05-20 18:10:50 +01001086 if (WARN (!pll,
1087 "asserting PCH PLL %s with no PLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001088 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001089
Chris Wilson92b27b02012-05-20 18:10:50 +01001090 val = I915_READ(pll->pll_reg);
1091 cur_state = !!(val & DPLL_VCO_ENABLE);
1092 WARN(cur_state != state,
1093 "PCH PLL state for reg %x assertion failure (expected %s, current %s), val=%08x\n",
1094 pll->pll_reg, state_string(state), state_string(cur_state), val);
1095
1096 /* Make sure the selected PLL is correctly attached to the transcoder */
1097 if (crtc && HAS_PCH_CPT(dev_priv->dev)) {
Jesse Barnesd3ccbe82011-10-12 09:27:42 -07001098 u32 pch_dpll;
1099
1100 pch_dpll = I915_READ(PCH_DPLL_SEL);
Chris Wilson92b27b02012-05-20 18:10:50 +01001101 cur_state = pll->pll_reg == _PCH_DPLL_B;
1102 if (!WARN(((pch_dpll >> (4 * crtc->pipe)) & 1) != cur_state,
1103 "PLL[%d] not attached to this transcoder %d: %08x\n",
1104 cur_state, crtc->pipe, pch_dpll)) {
1105 cur_state = !!(val >> (4*crtc->pipe + 3));
1106 WARN(cur_state != state,
1107 "PLL[%d] not %s on this transcoder %d: %08x\n",
1108 pll->pll_reg == _PCH_DPLL_B,
1109 state_string(state),
1110 crtc->pipe,
1111 val);
1112 }
Jesse Barnesd3ccbe82011-10-12 09:27:42 -07001113 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001114}
Chris Wilson92b27b02012-05-20 18:10:50 +01001115#define assert_pch_pll_enabled(d, p, c) assert_pch_pll(d, p, c, true)
1116#define assert_pch_pll_disabled(d, p, c) assert_pch_pll(d, p, c, false)
Jesse Barnes040484a2011-01-03 12:14:26 -08001117
1118static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1119 enum pipe pipe, bool state)
1120{
1121 int reg;
1122 u32 val;
1123 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001124 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1125 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001126
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001127 if (HAS_DDI(dev_priv->dev)) {
1128 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001129 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001130 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001131 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001132 } else {
1133 reg = FDI_TX_CTL(pipe);
1134 val = I915_READ(reg);
1135 cur_state = !!(val & FDI_TX_ENABLE);
1136 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001137 WARN(cur_state != state,
1138 "FDI TX state assertion failure (expected %s, current %s)\n",
1139 state_string(state), state_string(cur_state));
1140}
1141#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1142#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1143
1144static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1145 enum pipe pipe, bool state)
1146{
1147 int reg;
1148 u32 val;
1149 bool cur_state;
1150
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001151 reg = FDI_RX_CTL(pipe);
1152 val = I915_READ(reg);
1153 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001154 WARN(cur_state != state,
1155 "FDI RX state assertion failure (expected %s, current %s)\n",
1156 state_string(state), state_string(cur_state));
1157}
1158#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1159#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1160
1161static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1162 enum pipe pipe)
1163{
1164 int reg;
1165 u32 val;
1166
1167 /* ILK FDI PLL is always enabled */
1168 if (dev_priv->info->gen == 5)
1169 return;
1170
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001171 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001172 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001173 return;
1174
Jesse Barnes040484a2011-01-03 12:14:26 -08001175 reg = FDI_TX_CTL(pipe);
1176 val = I915_READ(reg);
1177 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1178}
1179
1180static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1181 enum pipe pipe)
1182{
1183 int reg;
1184 u32 val;
1185
1186 reg = FDI_RX_CTL(pipe);
1187 val = I915_READ(reg);
1188 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1189}
1190
Jesse Barnesea0760c2011-01-04 15:09:32 -08001191static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1192 enum pipe pipe)
1193{
1194 int pp_reg, lvds_reg;
1195 u32 val;
1196 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001197 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001198
1199 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1200 pp_reg = PCH_PP_CONTROL;
1201 lvds_reg = PCH_LVDS;
1202 } else {
1203 pp_reg = PP_CONTROL;
1204 lvds_reg = LVDS;
1205 }
1206
1207 val = I915_READ(pp_reg);
1208 if (!(val & PANEL_POWER_ON) ||
1209 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1210 locked = false;
1211
1212 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1213 panel_pipe = PIPE_B;
1214
1215 WARN(panel_pipe == pipe && locked,
1216 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001217 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001218}
1219
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001220void assert_pipe(struct drm_i915_private *dev_priv,
1221 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001222{
1223 int reg;
1224 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001225 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001226 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1227 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001228
Daniel Vetter8e636782012-01-22 01:36:48 +01001229 /* if we need the pipe A quirk it must be always on */
1230 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1231 state = true;
1232
Paulo Zanoni69310162013-01-29 16:35:19 -02001233 if (IS_HASWELL(dev_priv->dev) && cpu_transcoder != TRANSCODER_EDP &&
1234 !(I915_READ(HSW_PWR_WELL_DRIVER) & HSW_PWR_WELL_ENABLE)) {
1235 cur_state = false;
1236 } else {
1237 reg = PIPECONF(cpu_transcoder);
1238 val = I915_READ(reg);
1239 cur_state = !!(val & PIPECONF_ENABLE);
1240 }
1241
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001242 WARN(cur_state != state,
1243 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001244 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001245}
1246
Chris Wilson931872f2012-01-16 23:01:13 +00001247static void assert_plane(struct drm_i915_private *dev_priv,
1248 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001249{
1250 int reg;
1251 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001252 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001253
1254 reg = DSPCNTR(plane);
1255 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001256 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1257 WARN(cur_state != state,
1258 "plane %c assertion failure (expected %s, current %s)\n",
1259 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001260}
1261
Chris Wilson931872f2012-01-16 23:01:13 +00001262#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1263#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1264
Jesse Barnesb24e7172011-01-04 15:09:30 -08001265static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1266 enum pipe pipe)
1267{
1268 int reg, i;
1269 u32 val;
1270 int cur_pipe;
1271
Jesse Barnes19ec1352011-02-02 12:28:02 -08001272 /* Planes are fixed to pipes on ILK+ */
Adam Jackson28c057942011-10-07 14:38:42 -04001273 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1274 reg = DSPCNTR(pipe);
1275 val = I915_READ(reg);
1276 WARN((val & DISPLAY_PLANE_ENABLE),
1277 "plane %c assertion failure, should be disabled but not\n",
1278 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001279 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001280 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001281
Jesse Barnesb24e7172011-01-04 15:09:30 -08001282 /* Need to check both planes against the pipe */
1283 for (i = 0; i < 2; i++) {
1284 reg = DSPCNTR(i);
1285 val = I915_READ(reg);
1286 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1287 DISPPLANE_SEL_PIPE_SHIFT;
1288 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001289 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1290 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001291 }
1292}
1293
Jesse Barnes92f25842011-01-04 15:09:34 -08001294static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1295{
1296 u32 val;
1297 bool enabled;
1298
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001299 if (HAS_PCH_LPT(dev_priv->dev)) {
1300 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1301 return;
1302 }
1303
Jesse Barnes92f25842011-01-04 15:09:34 -08001304 val = I915_READ(PCH_DREF_CONTROL);
1305 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1306 DREF_SUPERSPREAD_SOURCE_MASK));
1307 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1308}
1309
1310static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1311 enum pipe pipe)
1312{
1313 int reg;
1314 u32 val;
1315 bool enabled;
1316
1317 reg = TRANSCONF(pipe);
1318 val = I915_READ(reg);
1319 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001320 WARN(enabled,
1321 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1322 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001323}
1324
Keith Packard4e634382011-08-06 10:39:45 -07001325static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1326 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001327{
1328 if ((val & DP_PORT_EN) == 0)
1329 return false;
1330
1331 if (HAS_PCH_CPT(dev_priv->dev)) {
1332 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1333 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1334 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1335 return false;
1336 } else {
1337 if ((val & DP_PIPE_MASK) != (pipe << 30))
1338 return false;
1339 }
1340 return true;
1341}
1342
Keith Packard1519b992011-08-06 10:35:34 -07001343static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1344 enum pipe pipe, u32 val)
1345{
1346 if ((val & PORT_ENABLE) == 0)
1347 return false;
1348
1349 if (HAS_PCH_CPT(dev_priv->dev)) {
1350 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1351 return false;
1352 } else {
1353 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1354 return false;
1355 }
1356 return true;
1357}
1358
1359static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1360 enum pipe pipe, u32 val)
1361{
1362 if ((val & LVDS_PORT_EN) == 0)
1363 return false;
1364
1365 if (HAS_PCH_CPT(dev_priv->dev)) {
1366 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1367 return false;
1368 } else {
1369 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1370 return false;
1371 }
1372 return true;
1373}
1374
1375static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1376 enum pipe pipe, u32 val)
1377{
1378 if ((val & ADPA_DAC_ENABLE) == 0)
1379 return false;
1380 if (HAS_PCH_CPT(dev_priv->dev)) {
1381 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1382 return false;
1383 } else {
1384 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1385 return false;
1386 }
1387 return true;
1388}
1389
Jesse Barnes291906f2011-02-02 12:28:03 -08001390static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001391 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001392{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001393 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001394 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001395 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001396 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001397
Daniel Vetter75c5da22012-09-10 21:58:29 +02001398 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1399 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001400 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001401}
1402
1403static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1404 enum pipe pipe, int reg)
1405{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001406 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001407 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001408 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001409 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001410
Daniel Vetter75c5da22012-09-10 21:58:29 +02001411 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & PORT_ENABLE) == 0
1412 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001413 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001414}
1415
1416static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1417 enum pipe pipe)
1418{
1419 int reg;
1420 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001421
Keith Packardf0575e92011-07-25 22:12:43 -07001422 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1423 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1424 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001425
1426 reg = PCH_ADPA;
1427 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001428 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001429 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001430 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001431
1432 reg = PCH_LVDS;
1433 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001434 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001435 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001436 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001437
1438 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1439 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1440 assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1441}
1442
Jesse Barnesb24e7172011-01-04 15:09:30 -08001443/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001444 * intel_enable_pll - enable a PLL
1445 * @dev_priv: i915 private structure
1446 * @pipe: pipe PLL to enable
1447 *
1448 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1449 * make sure the PLL reg is writable first though, since the panel write
1450 * protect mechanism may be enabled.
1451 *
1452 * Note! This is for pre-ILK only.
Thomas Richter7434a252012-07-18 19:22:30 +02001453 *
1454 * Unfortunately needed by dvo_ns2501 since the dvo depends on it running.
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001455 */
1456static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1457{
1458 int reg;
1459 u32 val;
1460
1461 /* No really, not for ILK+ */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07001462 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev) && dev_priv->info->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001463
1464 /* PLL is protected by panel, make sure we can write it */
1465 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1466 assert_panel_unlocked(dev_priv, pipe);
1467
1468 reg = DPLL(pipe);
1469 val = I915_READ(reg);
1470 val |= DPLL_VCO_ENABLE;
1471
1472 /* We do this three times for luck */
1473 I915_WRITE(reg, val);
1474 POSTING_READ(reg);
1475 udelay(150); /* wait for warmup */
1476 I915_WRITE(reg, val);
1477 POSTING_READ(reg);
1478 udelay(150); /* wait for warmup */
1479 I915_WRITE(reg, val);
1480 POSTING_READ(reg);
1481 udelay(150); /* wait for warmup */
1482}
1483
1484/**
1485 * intel_disable_pll - disable a PLL
1486 * @dev_priv: i915 private structure
1487 * @pipe: pipe PLL to disable
1488 *
1489 * Disable the PLL for @pipe, making sure the pipe is off first.
1490 *
1491 * Note! This is for pre-ILK only.
1492 */
1493static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1494{
1495 int reg;
1496 u32 val;
1497
1498 /* Don't disable pipe A or pipe A PLLs if needed */
1499 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1500 return;
1501
1502 /* Make sure the pipe isn't still relying on us */
1503 assert_pipe_disabled(dev_priv, pipe);
1504
1505 reg = DPLL(pipe);
1506 val = I915_READ(reg);
1507 val &= ~DPLL_VCO_ENABLE;
1508 I915_WRITE(reg, val);
1509 POSTING_READ(reg);
1510}
1511
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001512/* SBI access */
1513static void
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001514intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value,
1515 enum intel_sbi_destination destination)
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001516{
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001517 u32 tmp;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001518
Daniel Vetter09153002012-12-12 14:06:44 +01001519 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001520
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001521 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001522 100)) {
1523 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001524 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001525 }
1526
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001527 I915_WRITE(SBI_ADDR, (reg << 16));
1528 I915_WRITE(SBI_DATA, value);
1529
1530 if (destination == SBI_ICLK)
1531 tmp = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRWR;
1532 else
1533 tmp = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IOWR;
1534 I915_WRITE(SBI_CTL_STAT, SBI_BUSY | tmp);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001535
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001536 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001537 100)) {
1538 DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001539 return;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001540 }
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001541}
1542
1543static u32
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001544intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg,
1545 enum intel_sbi_destination destination)
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001546{
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001547 u32 value = 0;
Daniel Vetter09153002012-12-12 14:06:44 +01001548 WARN_ON(!mutex_is_locked(&dev_priv->dpio_lock));
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001549
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001550 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_BUSY) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001551 100)) {
1552 DRM_ERROR("timeout waiting for SBI to become ready\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001553 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001554 }
1555
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02001556 I915_WRITE(SBI_ADDR, (reg << 16));
1557
1558 if (destination == SBI_ICLK)
1559 value = SBI_CTL_DEST_ICLK | SBI_CTL_OP_CRRD;
1560 else
1561 value = SBI_CTL_DEST_MPHY | SBI_CTL_OP_IORD;
1562 I915_WRITE(SBI_CTL_STAT, value | SBI_BUSY);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001563
Eugeni Dodonov39fb50f2012-06-08 16:43:19 -03001564 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_BUSY | SBI_RESPONSE_FAIL)) == 0,
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001565 100)) {
1566 DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
Daniel Vetter09153002012-12-12 14:06:44 +01001567 return 0;
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001568 }
1569
Daniel Vetter09153002012-12-12 14:06:44 +01001570 return I915_READ(SBI_DATA);
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001571}
1572
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001573/**
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001574 * ironlake_enable_pch_pll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001575 * @dev_priv: i915 private structure
1576 * @pipe: pipe PLL to enable
1577 *
1578 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1579 * drives the transcoder clock.
1580 */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02001581static void ironlake_enable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001582{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001583 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
Chris Wilson48da64a2012-05-13 20:16:12 +01001584 struct intel_pch_pll *pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001585 int reg;
1586 u32 val;
1587
Chris Wilson48da64a2012-05-13 20:16:12 +01001588 /* PCH PLLs only available on ILK, SNB and IVB */
Jesse Barnes92f25842011-01-04 15:09:34 -08001589 BUG_ON(dev_priv->info->gen < 5);
Chris Wilson48da64a2012-05-13 20:16:12 +01001590 pll = intel_crtc->pch_pll;
1591 if (pll == NULL)
1592 return;
1593
1594 if (WARN_ON(pll->refcount == 0))
1595 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001596
1597 DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1598 pll->pll_reg, pll->active, pll->on,
1599 intel_crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001600
1601 /* PCH refclock must be enabled first */
1602 assert_pch_refclk_enabled(dev_priv);
1603
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001604 if (pll->active++ && pll->on) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001605 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001606 return;
1607 }
1608
1609 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1610
1611 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001612 val = I915_READ(reg);
1613 val |= DPLL_VCO_ENABLE;
1614 I915_WRITE(reg, val);
1615 POSTING_READ(reg);
1616 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001617
1618 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001619}
1620
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001621static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001622{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001623 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1624 struct intel_pch_pll *pll = intel_crtc->pch_pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001625 int reg;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001626 u32 val;
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001627
Jesse Barnes92f25842011-01-04 15:09:34 -08001628 /* PCH only available on ILK+ */
1629 BUG_ON(dev_priv->info->gen < 5);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001630 if (pll == NULL)
1631 return;
1632
Chris Wilson48da64a2012-05-13 20:16:12 +01001633 if (WARN_ON(pll->refcount == 0))
1634 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001635
1636 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1637 pll->pll_reg, pll->active, pll->on,
1638 intel_crtc->base.base.id);
1639
Chris Wilson48da64a2012-05-13 20:16:12 +01001640 if (WARN_ON(pll->active == 0)) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001641 assert_pch_pll_disabled(dev_priv, pll, NULL);
Chris Wilson48da64a2012-05-13 20:16:12 +01001642 return;
1643 }
1644
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001645 if (--pll->active) {
Chris Wilson92b27b02012-05-20 18:10:50 +01001646 assert_pch_pll_enabled(dev_priv, pll, NULL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001647 return;
1648 }
1649
1650 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
Jesse Barnes92f25842011-01-04 15:09:34 -08001651
1652 /* Make sure transcoder isn't still depending on us */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001653 assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001654
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001655 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001656 val = I915_READ(reg);
1657 val &= ~DPLL_VCO_ENABLE;
1658 I915_WRITE(reg, val);
1659 POSTING_READ(reg);
1660 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001661
1662 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001663}
1664
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001665static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1666 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001667{
Daniel Vetter23670b322012-11-01 09:15:30 +01001668 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001669 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetter23670b322012-11-01 09:15:30 +01001670 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001671
1672 /* PCH only available on ILK+ */
1673 BUG_ON(dev_priv->info->gen < 5);
1674
1675 /* Make sure PCH DPLL is enabled */
Chris Wilson92b27b02012-05-20 18:10:50 +01001676 assert_pch_pll_enabled(dev_priv,
1677 to_intel_crtc(crtc)->pch_pll,
1678 to_intel_crtc(crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001679
1680 /* FDI must be feeding us bits for PCH ports */
1681 assert_fdi_tx_enabled(dev_priv, pipe);
1682 assert_fdi_rx_enabled(dev_priv, pipe);
1683
Daniel Vetter23670b322012-11-01 09:15:30 +01001684 if (HAS_PCH_CPT(dev)) {
1685 /* Workaround: Set the timing override bit before enabling the
1686 * pch transcoder. */
1687 reg = TRANS_CHICKEN2(pipe);
1688 val = I915_READ(reg);
1689 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1690 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001691 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001692
Jesse Barnes040484a2011-01-03 12:14:26 -08001693 reg = TRANSCONF(pipe);
1694 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001695 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001696
1697 if (HAS_PCH_IBX(dev_priv->dev)) {
1698 /*
1699 * make the BPC in transcoder be consistent with
1700 * that in pipeconf reg.
1701 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001702 val &= ~PIPECONF_BPC_MASK;
1703 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001704 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001705
1706 val &= ~TRANS_INTERLACE_MASK;
1707 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001708 if (HAS_PCH_IBX(dev_priv->dev) &&
1709 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1710 val |= TRANS_LEGACY_INTERLACED_ILK;
1711 else
1712 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001713 else
1714 val |= TRANS_PROGRESSIVE;
1715
Jesse Barnes040484a2011-01-03 12:14:26 -08001716 I915_WRITE(reg, val | TRANS_ENABLE);
1717 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1718 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1719}
1720
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001721static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001722 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001723{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001724 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001725
1726 /* PCH only available on ILK+ */
1727 BUG_ON(dev_priv->info->gen < 5);
1728
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001729 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001730 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001731 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001732
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001733 /* Workaround: set timing override bit. */
1734 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001735 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001736 I915_WRITE(_TRANSA_CHICKEN2, val);
1737
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001738 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001739 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001740
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001741 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1742 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001743 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001744 else
1745 val |= TRANS_PROGRESSIVE;
1746
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001747 I915_WRITE(TRANSCONF(TRANSCODER_A), val);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001748 if (wait_for(I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE, 100))
1749 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001750}
1751
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001752static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1753 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001754{
Daniel Vetter23670b322012-11-01 09:15:30 +01001755 struct drm_device *dev = dev_priv->dev;
1756 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001757
1758 /* FDI relies on the transcoder */
1759 assert_fdi_tx_disabled(dev_priv, pipe);
1760 assert_fdi_rx_disabled(dev_priv, pipe);
1761
Jesse Barnes291906f2011-02-02 12:28:03 -08001762 /* Ports must be off as well */
1763 assert_pch_ports_disabled(dev_priv, pipe);
1764
Jesse Barnes040484a2011-01-03 12:14:26 -08001765 reg = TRANSCONF(pipe);
1766 val = I915_READ(reg);
1767 val &= ~TRANS_ENABLE;
1768 I915_WRITE(reg, val);
1769 /* wait for PCH transcoder off, transcoder state */
1770 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes4c9c18c2011-10-13 09:46:32 -07001771 DRM_ERROR("failed to disable transcoder %d\n", pipe);
Daniel Vetter23670b322012-11-01 09:15:30 +01001772
1773 if (!HAS_PCH_IBX(dev)) {
1774 /* Workaround: Clear the timing override chicken bit again. */
1775 reg = TRANS_CHICKEN2(pipe);
1776 val = I915_READ(reg);
1777 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1778 I915_WRITE(reg, val);
1779 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001780}
1781
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001782static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001783{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001784 u32 val;
1785
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001786 val = I915_READ(_TRANSACONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001787 val &= ~TRANS_ENABLE;
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001788 I915_WRITE(_TRANSACONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001789 /* wait for PCH transcoder off, transcoder state */
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001790 if (wait_for((I915_READ(_TRANSACONF) & TRANS_STATE_ENABLE) == 0, 50))
1791 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001792
1793 /* Workaround: clear timing override bit. */
1794 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001795 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001796 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001797}
1798
1799/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001800 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001801 * @dev_priv: i915 private structure
1802 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001803 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001804 *
1805 * Enable @pipe, making sure that various hardware specific requirements
1806 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1807 *
1808 * @pipe should be %PIPE_A or %PIPE_B.
1809 *
1810 * Will wait until the pipe is actually running (i.e. first vblank) before
1811 * returning.
1812 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001813static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1814 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001815{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001816 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1817 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001818 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001819 int reg;
1820 u32 val;
1821
Paulo Zanoni681e5812012-12-06 11:12:38 -02001822 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001823 pch_transcoder = TRANSCODER_A;
1824 else
1825 pch_transcoder = pipe;
1826
Jesse Barnesb24e7172011-01-04 15:09:30 -08001827 /*
1828 * A pipe without a PLL won't actually be able to drive bits from
1829 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1830 * need the check.
1831 */
1832 if (!HAS_PCH_SPLIT(dev_priv->dev))
1833 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001834 else {
1835 if (pch_port) {
1836 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02001837 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001838 assert_fdi_tx_pll_enabled(dev_priv,
1839 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08001840 }
1841 /* FIXME: assert CPU port conditions for SNB+ */
1842 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001843
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001844 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001845 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001846 if (val & PIPECONF_ENABLE)
1847 return;
1848
1849 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001850 intel_wait_for_vblank(dev_priv->dev, pipe);
1851}
1852
1853/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001854 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001855 * @dev_priv: i915 private structure
1856 * @pipe: pipe to disable
1857 *
1858 * Disable @pipe, making sure that various hardware specific requirements
1859 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1860 *
1861 * @pipe should be %PIPE_A or %PIPE_B.
1862 *
1863 * Will wait until the pipe has shut down before returning.
1864 */
1865static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1866 enum pipe pipe)
1867{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001868 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1869 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001870 int reg;
1871 u32 val;
1872
1873 /*
1874 * Make sure planes won't keep trying to pump pixels to us,
1875 * or we might hang the display.
1876 */
1877 assert_planes_disabled(dev_priv, pipe);
1878
1879 /* Don't disable pipe A or pipe A PLLs if needed */
1880 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1881 return;
1882
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001883 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001884 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001885 if ((val & PIPECONF_ENABLE) == 0)
1886 return;
1887
1888 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001889 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1890}
1891
Keith Packardd74362c2011-07-28 14:47:14 -07001892/*
1893 * Plane regs are double buffered, going from enabled->disabled needs a
1894 * trigger in order to latch. The display address reg provides this.
1895 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001896void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001897 enum plane plane)
1898{
Damien Lespiau14f86142012-10-29 15:24:49 +00001899 if (dev_priv->info->gen >= 4)
1900 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1901 else
1902 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
Keith Packardd74362c2011-07-28 14:47:14 -07001903}
1904
Jesse Barnesb24e7172011-01-04 15:09:30 -08001905/**
1906 * intel_enable_plane - enable a display plane on a given pipe
1907 * @dev_priv: i915 private structure
1908 * @plane: plane to enable
1909 * @pipe: pipe being fed
1910 *
1911 * Enable @plane on @pipe, making sure that @pipe is running first.
1912 */
1913static void intel_enable_plane(struct drm_i915_private *dev_priv,
1914 enum plane plane, enum pipe pipe)
1915{
1916 int reg;
1917 u32 val;
1918
1919 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1920 assert_pipe_enabled(dev_priv, pipe);
1921
1922 reg = DSPCNTR(plane);
1923 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001924 if (val & DISPLAY_PLANE_ENABLE)
1925 return;
1926
1927 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001928 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001929 intel_wait_for_vblank(dev_priv->dev, pipe);
1930}
1931
Jesse Barnesb24e7172011-01-04 15:09:30 -08001932/**
1933 * intel_disable_plane - disable a display plane
1934 * @dev_priv: i915 private structure
1935 * @plane: plane to disable
1936 * @pipe: pipe consuming the data
1937 *
1938 * Disable @plane; should be an independent operation.
1939 */
1940static void intel_disable_plane(struct drm_i915_private *dev_priv,
1941 enum plane plane, enum pipe pipe)
1942{
1943 int reg;
1944 u32 val;
1945
1946 reg = DSPCNTR(plane);
1947 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001948 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1949 return;
1950
1951 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001952 intel_flush_display_plane(dev_priv, plane);
1953 intel_wait_for_vblank(dev_priv->dev, pipe);
1954}
1955
Chris Wilson127bd2a2010-07-23 23:32:05 +01001956int
Chris Wilson48b956c2010-09-14 12:50:34 +01001957intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001958 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001959 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001960{
Chris Wilsonce453d82011-02-21 14:43:56 +00001961 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001962 u32 alignment;
1963 int ret;
1964
Chris Wilson05394f32010-11-08 19:18:58 +00001965 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001966 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001967 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1968 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001969 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001970 alignment = 4 * 1024;
1971 else
1972 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001973 break;
1974 case I915_TILING_X:
1975 /* pin() will align the object as required by fence */
1976 alignment = 0;
1977 break;
1978 case I915_TILING_Y:
1979 /* FIXME: Is this true? */
1980 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1981 return -EINVAL;
1982 default:
1983 BUG();
1984 }
1985
Chris Wilsonce453d82011-02-21 14:43:56 +00001986 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001987 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001988 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001989 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001990
1991 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1992 * fence, whereas 965+ only requires a fence if using
1993 * framebuffer compression. For simplicity, we always install
1994 * a fence as the cost is not that onerous.
1995 */
Chris Wilson06d98132012-04-17 15:31:24 +01001996 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001997 if (ret)
1998 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001999
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002000 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002001
Chris Wilsonce453d82011-02-21 14:43:56 +00002002 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002003 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002004
2005err_unpin:
2006 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002007err_interruptible:
2008 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01002009 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002010}
2011
Chris Wilson1690e1e2011-12-14 13:57:08 +01002012void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2013{
2014 i915_gem_object_unpin_fence(obj);
2015 i915_gem_object_unpin(obj);
2016}
2017
Daniel Vetterc2c75132012-07-05 12:17:30 +02002018/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2019 * is assumed to be a power-of-two. */
Damien Lespiau5a35e992012-10-26 18:20:12 +01002020unsigned long intel_gen4_compute_offset_xtiled(int *x, int *y,
2021 unsigned int bpp,
2022 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002023{
2024 int tile_rows, tiles;
2025
2026 tile_rows = *y / 8;
2027 *y %= 8;
2028 tiles = *x / (512/bpp);
2029 *x %= 512/bpp;
2030
2031 return tile_rows * pitch * 8 + tiles * 4096;
2032}
2033
Jesse Barnes17638cd2011-06-24 12:19:23 -07002034static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2035 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002036{
2037 struct drm_device *dev = crtc->dev;
2038 struct drm_i915_private *dev_priv = dev->dev_private;
2039 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2040 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002041 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002042 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002043 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002044 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002045 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002046
2047 switch (plane) {
2048 case 0:
2049 case 1:
2050 break;
2051 default:
2052 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2053 return -EINVAL;
2054 }
2055
2056 intel_fb = to_intel_framebuffer(fb);
2057 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002058
Chris Wilson5eddb702010-09-11 13:48:45 +01002059 reg = DSPCNTR(plane);
2060 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002061 /* Mask out pixel format bits in case we change it */
2062 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002063 switch (fb->pixel_format) {
2064 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002065 dspcntr |= DISPPLANE_8BPP;
2066 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002067 case DRM_FORMAT_XRGB1555:
2068 case DRM_FORMAT_ARGB1555:
2069 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002070 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002071 case DRM_FORMAT_RGB565:
2072 dspcntr |= DISPPLANE_BGRX565;
2073 break;
2074 case DRM_FORMAT_XRGB8888:
2075 case DRM_FORMAT_ARGB8888:
2076 dspcntr |= DISPPLANE_BGRX888;
2077 break;
2078 case DRM_FORMAT_XBGR8888:
2079 case DRM_FORMAT_ABGR8888:
2080 dspcntr |= DISPPLANE_RGBX888;
2081 break;
2082 case DRM_FORMAT_XRGB2101010:
2083 case DRM_FORMAT_ARGB2101010:
2084 dspcntr |= DISPPLANE_BGRX101010;
2085 break;
2086 case DRM_FORMAT_XBGR2101010:
2087 case DRM_FORMAT_ABGR2101010:
2088 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002089 break;
2090 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002091 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes81255562010-08-02 12:07:50 -07002092 return -EINVAL;
2093 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002094
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002095 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002096 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002097 dspcntr |= DISPPLANE_TILED;
2098 else
2099 dspcntr &= ~DISPPLANE_TILED;
2100 }
2101
Chris Wilson5eddb702010-09-11 13:48:45 +01002102 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002103
Daniel Vettere506a0c2012-07-05 12:17:29 +02002104 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002105
Daniel Vetterc2c75132012-07-05 12:17:30 +02002106 if (INTEL_INFO(dev)->gen >= 4) {
2107 intel_crtc->dspaddr_offset =
Damien Lespiau5a35e992012-10-26 18:20:12 +01002108 intel_gen4_compute_offset_xtiled(&x, &y,
2109 fb->bits_per_pixel / 8,
2110 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002111 linear_offset -= intel_crtc->dspaddr_offset;
2112 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002113 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002114 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002115
2116 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2117 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002118 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002119 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetterc2c75132012-07-05 12:17:30 +02002120 I915_MODIFY_DISPBASE(DSPSURF(plane),
2121 obj->gtt_offset + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002122 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002123 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002124 } else
Daniel Vettere506a0c2012-07-05 12:17:29 +02002125 I915_WRITE(DSPADDR(plane), obj->gtt_offset + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002126 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002127
Jesse Barnes17638cd2011-06-24 12:19:23 -07002128 return 0;
2129}
2130
2131static int ironlake_update_plane(struct drm_crtc *crtc,
2132 struct drm_framebuffer *fb, int x, int y)
2133{
2134 struct drm_device *dev = crtc->dev;
2135 struct drm_i915_private *dev_priv = dev->dev_private;
2136 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2137 struct intel_framebuffer *intel_fb;
2138 struct drm_i915_gem_object *obj;
2139 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002140 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002141 u32 dspcntr;
2142 u32 reg;
2143
2144 switch (plane) {
2145 case 0:
2146 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002147 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002148 break;
2149 default:
2150 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2151 return -EINVAL;
2152 }
2153
2154 intel_fb = to_intel_framebuffer(fb);
2155 obj = intel_fb->obj;
2156
2157 reg = DSPCNTR(plane);
2158 dspcntr = I915_READ(reg);
2159 /* Mask out pixel format bits in case we change it */
2160 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002161 switch (fb->pixel_format) {
2162 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002163 dspcntr |= DISPPLANE_8BPP;
2164 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002165 case DRM_FORMAT_RGB565:
2166 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002167 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002168 case DRM_FORMAT_XRGB8888:
2169 case DRM_FORMAT_ARGB8888:
2170 dspcntr |= DISPPLANE_BGRX888;
2171 break;
2172 case DRM_FORMAT_XBGR8888:
2173 case DRM_FORMAT_ABGR8888:
2174 dspcntr |= DISPPLANE_RGBX888;
2175 break;
2176 case DRM_FORMAT_XRGB2101010:
2177 case DRM_FORMAT_ARGB2101010:
2178 dspcntr |= DISPPLANE_BGRX101010;
2179 break;
2180 case DRM_FORMAT_XBGR2101010:
2181 case DRM_FORMAT_ABGR2101010:
2182 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002183 break;
2184 default:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002185 DRM_ERROR("Unknown pixel format 0x%08x\n", fb->pixel_format);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002186 return -EINVAL;
2187 }
2188
2189 if (obj->tiling_mode != I915_TILING_NONE)
2190 dspcntr |= DISPPLANE_TILED;
2191 else
2192 dspcntr &= ~DISPPLANE_TILED;
2193
2194 /* must disable */
2195 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2196
2197 I915_WRITE(reg, dspcntr);
2198
Daniel Vettere506a0c2012-07-05 12:17:29 +02002199 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002200 intel_crtc->dspaddr_offset =
Damien Lespiau5a35e992012-10-26 18:20:12 +01002201 intel_gen4_compute_offset_xtiled(&x, &y,
2202 fb->bits_per_pixel / 8,
2203 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002204 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002205
Daniel Vettere506a0c2012-07-05 12:17:29 +02002206 DRM_DEBUG_KMS("Writing base %08X %08lX %d %d %d\n",
2207 obj->gtt_offset, linear_offset, x, y, fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002208 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002209 I915_MODIFY_DISPBASE(DSPSURF(plane),
2210 obj->gtt_offset + intel_crtc->dspaddr_offset);
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002211 if (IS_HASWELL(dev)) {
2212 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2213 } else {
2214 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2215 I915_WRITE(DSPLINOFF(plane), linear_offset);
2216 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002217 POSTING_READ(reg);
2218
2219 return 0;
2220}
2221
2222/* Assume fb object is pinned & idle & fenced and just update base pointers */
2223static int
2224intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2225 int x, int y, enum mode_set_atomic state)
2226{
2227 struct drm_device *dev = crtc->dev;
2228 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002229
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002230 if (dev_priv->display.disable_fbc)
2231 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002232 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002233
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002234 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07002235}
2236
Ville Syrjälä96a02912013-02-18 19:08:49 +02002237void intel_display_handle_reset(struct drm_device *dev)
2238{
2239 struct drm_i915_private *dev_priv = dev->dev_private;
2240 struct drm_crtc *crtc;
2241
2242 /*
2243 * Flips in the rings have been nuked by the reset,
2244 * so complete all pending flips so that user space
2245 * will get its events and not get stuck.
2246 *
2247 * Also update the base address of all primary
2248 * planes to the the last fb to make sure we're
2249 * showing the correct fb after a reset.
2250 *
2251 * Need to make two loops over the crtcs so that we
2252 * don't try to grab a crtc mutex before the
2253 * pending_flip_queue really got woken up.
2254 */
2255
2256 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2258 enum plane plane = intel_crtc->plane;
2259
2260 intel_prepare_page_flip(dev, plane);
2261 intel_finish_page_flip_plane(dev, plane);
2262 }
2263
2264 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2265 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2266
2267 mutex_lock(&crtc->mutex);
2268 if (intel_crtc->active)
2269 dev_priv->display.update_plane(crtc, crtc->fb,
2270 crtc->x, crtc->y);
2271 mutex_unlock(&crtc->mutex);
2272 }
2273}
2274
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002275static int
Chris Wilson14667a42012-04-03 17:58:35 +01002276intel_finish_fb(struct drm_framebuffer *old_fb)
2277{
2278 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2279 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2280 bool was_interruptible = dev_priv->mm.interruptible;
2281 int ret;
2282
Chris Wilson14667a42012-04-03 17:58:35 +01002283 /* Big Hammer, we also need to ensure that any pending
2284 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2285 * current scanout is retired before unpinning the old
2286 * framebuffer.
2287 *
2288 * This should only fail upon a hung GPU, in which case we
2289 * can safely continue.
2290 */
2291 dev_priv->mm.interruptible = false;
2292 ret = i915_gem_object_finish_gpu(obj);
2293 dev_priv->mm.interruptible = was_interruptible;
2294
2295 return ret;
2296}
2297
Ville Syrjälä198598d2012-10-31 17:50:24 +02002298static void intel_crtc_update_sarea_pos(struct drm_crtc *crtc, int x, int y)
2299{
2300 struct drm_device *dev = crtc->dev;
2301 struct drm_i915_master_private *master_priv;
2302 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2303
2304 if (!dev->primary->master)
2305 return;
2306
2307 master_priv = dev->primary->master->driver_priv;
2308 if (!master_priv->sarea_priv)
2309 return;
2310
2311 switch (intel_crtc->pipe) {
2312 case 0:
2313 master_priv->sarea_priv->pipeA_x = x;
2314 master_priv->sarea_priv->pipeA_y = y;
2315 break;
2316 case 1:
2317 master_priv->sarea_priv->pipeB_x = x;
2318 master_priv->sarea_priv->pipeB_y = y;
2319 break;
2320 default:
2321 break;
2322 }
2323}
2324
Chris Wilson14667a42012-04-03 17:58:35 +01002325static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002326intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002327 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002328{
2329 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002330 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002331 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002332 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002333 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002334
2335 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002336 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002337 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002338 return 0;
2339 }
2340
Eugeni Dodonov5826eca2012-05-09 15:37:12 -03002341 if(intel_crtc->plane > dev_priv->num_pipe) {
2342 DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
2343 intel_crtc->plane,
2344 dev_priv->num_pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002345 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002346 }
2347
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002348 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002349 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002350 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002351 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002352 if (ret != 0) {
2353 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002354 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002355 return ret;
2356 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002357
Daniel Vetter94352cf2012-07-05 22:51:56 +02002358 ret = dev_priv->display.update_plane(crtc, fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002359 if (ret) {
Daniel Vetter94352cf2012-07-05 22:51:56 +02002360 intel_unpin_fb_obj(to_intel_framebuffer(fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002361 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002362 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002363 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002364 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002365
Daniel Vetter94352cf2012-07-05 22:51:56 +02002366 old_fb = crtc->fb;
2367 crtc->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002368 crtc->x = x;
2369 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002370
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002371 if (old_fb) {
2372 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002373 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002374 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002375
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002376 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002377 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002378
Ville Syrjälä198598d2012-10-31 17:50:24 +02002379 intel_crtc_update_sarea_pos(crtc, x, y);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002380
2381 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002382}
2383
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002384static void intel_fdi_normal_train(struct drm_crtc *crtc)
2385{
2386 struct drm_device *dev = crtc->dev;
2387 struct drm_i915_private *dev_priv = dev->dev_private;
2388 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2389 int pipe = intel_crtc->pipe;
2390 u32 reg, temp;
2391
2392 /* enable normal train */
2393 reg = FDI_TX_CTL(pipe);
2394 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002395 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002396 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2397 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002398 } else {
2399 temp &= ~FDI_LINK_TRAIN_NONE;
2400 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002401 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002402 I915_WRITE(reg, temp);
2403
2404 reg = FDI_RX_CTL(pipe);
2405 temp = I915_READ(reg);
2406 if (HAS_PCH_CPT(dev)) {
2407 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2408 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2409 } else {
2410 temp &= ~FDI_LINK_TRAIN_NONE;
2411 temp |= FDI_LINK_TRAIN_NONE;
2412 }
2413 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2414
2415 /* wait one idle pattern time */
2416 POSTING_READ(reg);
2417 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002418
2419 /* IVB wants error correction enabled */
2420 if (IS_IVYBRIDGE(dev))
2421 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2422 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002423}
2424
Daniel Vetter01a415f2012-10-27 15:58:40 +02002425static void ivb_modeset_global_resources(struct drm_device *dev)
2426{
2427 struct drm_i915_private *dev_priv = dev->dev_private;
2428 struct intel_crtc *pipe_B_crtc =
2429 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2430 struct intel_crtc *pipe_C_crtc =
2431 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2432 uint32_t temp;
2433
2434 /* When everything is off disable fdi C so that we could enable fdi B
2435 * with all lanes. XXX: This misses the case where a pipe is not using
2436 * any pch resources and so doesn't need any fdi lanes. */
2437 if (!pipe_B_crtc->base.enabled && !pipe_C_crtc->base.enabled) {
2438 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2439 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2440
2441 temp = I915_READ(SOUTH_CHICKEN1);
2442 temp &= ~FDI_BC_BIFURCATION_SELECT;
2443 DRM_DEBUG_KMS("disabling fdi C rx\n");
2444 I915_WRITE(SOUTH_CHICKEN1, temp);
2445 }
2446}
2447
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002448/* The FDI link training functions for ILK/Ibexpeak. */
2449static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2450{
2451 struct drm_device *dev = crtc->dev;
2452 struct drm_i915_private *dev_priv = dev->dev_private;
2453 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2454 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002455 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002456 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002457
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002458 /* FDI needs bits from pipe & plane first */
2459 assert_pipe_enabled(dev_priv, pipe);
2460 assert_plane_enabled(dev_priv, plane);
2461
Adam Jacksone1a44742010-06-25 15:32:14 -04002462 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2463 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002464 reg = FDI_RX_IMR(pipe);
2465 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002466 temp &= ~FDI_RX_SYMBOL_LOCK;
2467 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002468 I915_WRITE(reg, temp);
2469 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002470 udelay(150);
2471
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002472 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002473 reg = FDI_TX_CTL(pipe);
2474 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002475 temp &= ~(7 << 19);
2476 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002477 temp &= ~FDI_LINK_TRAIN_NONE;
2478 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002479 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002480
Chris Wilson5eddb702010-09-11 13:48:45 +01002481 reg = FDI_RX_CTL(pipe);
2482 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002483 temp &= ~FDI_LINK_TRAIN_NONE;
2484 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002485 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2486
2487 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002488 udelay(150);
2489
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002490 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002491 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2492 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2493 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002494
Chris Wilson5eddb702010-09-11 13:48:45 +01002495 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002496 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002497 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002498 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2499
2500 if ((temp & FDI_RX_BIT_LOCK)) {
2501 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002502 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002503 break;
2504 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002505 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002506 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002507 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002508
2509 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002510 reg = FDI_TX_CTL(pipe);
2511 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002512 temp &= ~FDI_LINK_TRAIN_NONE;
2513 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002514 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002515
Chris Wilson5eddb702010-09-11 13:48:45 +01002516 reg = FDI_RX_CTL(pipe);
2517 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002518 temp &= ~FDI_LINK_TRAIN_NONE;
2519 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002520 I915_WRITE(reg, temp);
2521
2522 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002523 udelay(150);
2524
Chris Wilson5eddb702010-09-11 13:48:45 +01002525 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002526 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002527 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002528 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2529
2530 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002531 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002532 DRM_DEBUG_KMS("FDI train 2 done.\n");
2533 break;
2534 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002535 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002536 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002537 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002538
2539 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002540
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002541}
2542
Akshay Joshi0206e352011-08-16 15:34:10 -04002543static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002544 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2545 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2546 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2547 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2548};
2549
2550/* The FDI link training functions for SNB/Cougarpoint. */
2551static void gen6_fdi_link_train(struct drm_crtc *crtc)
2552{
2553 struct drm_device *dev = crtc->dev;
2554 struct drm_i915_private *dev_priv = dev->dev_private;
2555 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2556 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002557 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002558
Adam Jacksone1a44742010-06-25 15:32:14 -04002559 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2560 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002561 reg = FDI_RX_IMR(pipe);
2562 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002563 temp &= ~FDI_RX_SYMBOL_LOCK;
2564 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002565 I915_WRITE(reg, temp);
2566
2567 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002568 udelay(150);
2569
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002570 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002571 reg = FDI_TX_CTL(pipe);
2572 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002573 temp &= ~(7 << 19);
2574 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002575 temp &= ~FDI_LINK_TRAIN_NONE;
2576 temp |= FDI_LINK_TRAIN_PATTERN_1;
2577 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2578 /* SNB-B */
2579 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002580 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002581
Daniel Vetterd74cf322012-10-26 10:58:13 +02002582 I915_WRITE(FDI_RX_MISC(pipe),
2583 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2584
Chris Wilson5eddb702010-09-11 13:48:45 +01002585 reg = FDI_RX_CTL(pipe);
2586 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002587 if (HAS_PCH_CPT(dev)) {
2588 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2589 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2590 } else {
2591 temp &= ~FDI_LINK_TRAIN_NONE;
2592 temp |= FDI_LINK_TRAIN_PATTERN_1;
2593 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002594 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2595
2596 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002597 udelay(150);
2598
Akshay Joshi0206e352011-08-16 15:34:10 -04002599 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002600 reg = FDI_TX_CTL(pipe);
2601 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002602 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2603 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002604 I915_WRITE(reg, temp);
2605
2606 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002607 udelay(500);
2608
Sean Paulfa37d392012-03-02 12:53:39 -05002609 for (retry = 0; retry < 5; retry++) {
2610 reg = FDI_RX_IIR(pipe);
2611 temp = I915_READ(reg);
2612 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2613 if (temp & FDI_RX_BIT_LOCK) {
2614 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2615 DRM_DEBUG_KMS("FDI train 1 done.\n");
2616 break;
2617 }
2618 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002619 }
Sean Paulfa37d392012-03-02 12:53:39 -05002620 if (retry < 5)
2621 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002622 }
2623 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002624 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002625
2626 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002627 reg = FDI_TX_CTL(pipe);
2628 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002629 temp &= ~FDI_LINK_TRAIN_NONE;
2630 temp |= FDI_LINK_TRAIN_PATTERN_2;
2631 if (IS_GEN6(dev)) {
2632 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2633 /* SNB-B */
2634 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2635 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002636 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002637
Chris Wilson5eddb702010-09-11 13:48:45 +01002638 reg = FDI_RX_CTL(pipe);
2639 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002640 if (HAS_PCH_CPT(dev)) {
2641 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2642 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2643 } else {
2644 temp &= ~FDI_LINK_TRAIN_NONE;
2645 temp |= FDI_LINK_TRAIN_PATTERN_2;
2646 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002647 I915_WRITE(reg, temp);
2648
2649 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002650 udelay(150);
2651
Akshay Joshi0206e352011-08-16 15:34:10 -04002652 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002653 reg = FDI_TX_CTL(pipe);
2654 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002655 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2656 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002657 I915_WRITE(reg, temp);
2658
2659 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002660 udelay(500);
2661
Sean Paulfa37d392012-03-02 12:53:39 -05002662 for (retry = 0; retry < 5; retry++) {
2663 reg = FDI_RX_IIR(pipe);
2664 temp = I915_READ(reg);
2665 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2666 if (temp & FDI_RX_SYMBOL_LOCK) {
2667 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2668 DRM_DEBUG_KMS("FDI train 2 done.\n");
2669 break;
2670 }
2671 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002672 }
Sean Paulfa37d392012-03-02 12:53:39 -05002673 if (retry < 5)
2674 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002675 }
2676 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002677 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002678
2679 DRM_DEBUG_KMS("FDI train done.\n");
2680}
2681
Jesse Barnes357555c2011-04-28 15:09:55 -07002682/* Manual link training for Ivy Bridge A0 parts */
2683static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2684{
2685 struct drm_device *dev = crtc->dev;
2686 struct drm_i915_private *dev_priv = dev->dev_private;
2687 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2688 int pipe = intel_crtc->pipe;
2689 u32 reg, temp, i;
2690
2691 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2692 for train result */
2693 reg = FDI_RX_IMR(pipe);
2694 temp = I915_READ(reg);
2695 temp &= ~FDI_RX_SYMBOL_LOCK;
2696 temp &= ~FDI_RX_BIT_LOCK;
2697 I915_WRITE(reg, temp);
2698
2699 POSTING_READ(reg);
2700 udelay(150);
2701
Daniel Vetter01a415f2012-10-27 15:58:40 +02002702 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
2703 I915_READ(FDI_RX_IIR(pipe)));
2704
Jesse Barnes357555c2011-04-28 15:09:55 -07002705 /* enable CPU FDI TX and PCH FDI RX */
2706 reg = FDI_TX_CTL(pipe);
2707 temp = I915_READ(reg);
2708 temp &= ~(7 << 19);
2709 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2710 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2711 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2712 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2713 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002714 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002715 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2716
Daniel Vetterd74cf322012-10-26 10:58:13 +02002717 I915_WRITE(FDI_RX_MISC(pipe),
2718 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2719
Jesse Barnes357555c2011-04-28 15:09:55 -07002720 reg = FDI_RX_CTL(pipe);
2721 temp = I915_READ(reg);
2722 temp &= ~FDI_LINK_TRAIN_AUTO;
2723 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2724 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002725 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002726 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2727
2728 POSTING_READ(reg);
2729 udelay(150);
2730
Akshay Joshi0206e352011-08-16 15:34:10 -04002731 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002732 reg = FDI_TX_CTL(pipe);
2733 temp = I915_READ(reg);
2734 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2735 temp |= snb_b_fdi_train_param[i];
2736 I915_WRITE(reg, temp);
2737
2738 POSTING_READ(reg);
2739 udelay(500);
2740
2741 reg = FDI_RX_IIR(pipe);
2742 temp = I915_READ(reg);
2743 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2744
2745 if (temp & FDI_RX_BIT_LOCK ||
2746 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2747 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002748 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002749 break;
2750 }
2751 }
2752 if (i == 4)
2753 DRM_ERROR("FDI train 1 fail!\n");
2754
2755 /* Train 2 */
2756 reg = FDI_TX_CTL(pipe);
2757 temp = I915_READ(reg);
2758 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2759 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2760 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2761 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2762 I915_WRITE(reg, temp);
2763
2764 reg = FDI_RX_CTL(pipe);
2765 temp = I915_READ(reg);
2766 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2767 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2768 I915_WRITE(reg, temp);
2769
2770 POSTING_READ(reg);
2771 udelay(150);
2772
Akshay Joshi0206e352011-08-16 15:34:10 -04002773 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002774 reg = FDI_TX_CTL(pipe);
2775 temp = I915_READ(reg);
2776 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2777 temp |= snb_b_fdi_train_param[i];
2778 I915_WRITE(reg, temp);
2779
2780 POSTING_READ(reg);
2781 udelay(500);
2782
2783 reg = FDI_RX_IIR(pipe);
2784 temp = I915_READ(reg);
2785 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2786
2787 if (temp & FDI_RX_SYMBOL_LOCK) {
2788 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Daniel Vetter01a415f2012-10-27 15:58:40 +02002789 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n", i);
Jesse Barnes357555c2011-04-28 15:09:55 -07002790 break;
2791 }
2792 }
2793 if (i == 4)
2794 DRM_ERROR("FDI train 2 fail!\n");
2795
2796 DRM_DEBUG_KMS("FDI train done.\n");
2797}
2798
Daniel Vetter88cefb62012-08-12 19:27:14 +02002799static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002800{
Daniel Vetter88cefb62012-08-12 19:27:14 +02002801 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002802 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002803 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002804 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002805
Jesse Barnesc64e3112010-09-10 11:27:03 -07002806
Jesse Barnes0e23b992010-09-10 11:10:00 -07002807 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002808 reg = FDI_RX_CTL(pipe);
2809 temp = I915_READ(reg);
2810 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002811 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002812 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01002813 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2814
2815 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002816 udelay(200);
2817
2818 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002819 temp = I915_READ(reg);
2820 I915_WRITE(reg, temp | FDI_PCDCLK);
2821
2822 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002823 udelay(200);
2824
Paulo Zanoni20749732012-11-23 15:30:38 -02002825 /* Enable CPU FDI TX PLL, always on for Ironlake */
2826 reg = FDI_TX_CTL(pipe);
2827 temp = I915_READ(reg);
2828 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2829 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002830
Paulo Zanoni20749732012-11-23 15:30:38 -02002831 POSTING_READ(reg);
2832 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002833 }
2834}
2835
Daniel Vetter88cefb62012-08-12 19:27:14 +02002836static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
2837{
2838 struct drm_device *dev = intel_crtc->base.dev;
2839 struct drm_i915_private *dev_priv = dev->dev_private;
2840 int pipe = intel_crtc->pipe;
2841 u32 reg, temp;
2842
2843 /* Switch from PCDclk to Rawclk */
2844 reg = FDI_RX_CTL(pipe);
2845 temp = I915_READ(reg);
2846 I915_WRITE(reg, temp & ~FDI_PCDCLK);
2847
2848 /* Disable CPU FDI TX PLL */
2849 reg = FDI_TX_CTL(pipe);
2850 temp = I915_READ(reg);
2851 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2852
2853 POSTING_READ(reg);
2854 udelay(100);
2855
2856 reg = FDI_RX_CTL(pipe);
2857 temp = I915_READ(reg);
2858 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
2859
2860 /* Wait for the clocks to turn off. */
2861 POSTING_READ(reg);
2862 udelay(100);
2863}
2864
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002865static void ironlake_fdi_disable(struct drm_crtc *crtc)
2866{
2867 struct drm_device *dev = crtc->dev;
2868 struct drm_i915_private *dev_priv = dev->dev_private;
2869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2870 int pipe = intel_crtc->pipe;
2871 u32 reg, temp;
2872
2873 /* disable CPU FDI tx and PCH FDI rx */
2874 reg = FDI_TX_CTL(pipe);
2875 temp = I915_READ(reg);
2876 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2877 POSTING_READ(reg);
2878
2879 reg = FDI_RX_CTL(pipe);
2880 temp = I915_READ(reg);
2881 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002882 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002883 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2884
2885 POSTING_READ(reg);
2886 udelay(100);
2887
2888 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002889 if (HAS_PCH_IBX(dev)) {
2890 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002891 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002892
2893 /* still set train pattern 1 */
2894 reg = FDI_TX_CTL(pipe);
2895 temp = I915_READ(reg);
2896 temp &= ~FDI_LINK_TRAIN_NONE;
2897 temp |= FDI_LINK_TRAIN_PATTERN_1;
2898 I915_WRITE(reg, temp);
2899
2900 reg = FDI_RX_CTL(pipe);
2901 temp = I915_READ(reg);
2902 if (HAS_PCH_CPT(dev)) {
2903 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2904 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2905 } else {
2906 temp &= ~FDI_LINK_TRAIN_NONE;
2907 temp |= FDI_LINK_TRAIN_PATTERN_1;
2908 }
2909 /* BPC in FDI rx is consistent with that in PIPECONF */
2910 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002911 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002912 I915_WRITE(reg, temp);
2913
2914 POSTING_READ(reg);
2915 udelay(100);
2916}
2917
Chris Wilson5bb61642012-09-27 21:25:58 +01002918static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2919{
2920 struct drm_device *dev = crtc->dev;
2921 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä10d83732013-01-29 18:13:34 +02002922 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5bb61642012-09-27 21:25:58 +01002923 unsigned long flags;
2924 bool pending;
2925
Ville Syrjälä10d83732013-01-29 18:13:34 +02002926 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2927 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
Chris Wilson5bb61642012-09-27 21:25:58 +01002928 return false;
2929
2930 spin_lock_irqsave(&dev->event_lock, flags);
2931 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2932 spin_unlock_irqrestore(&dev->event_lock, flags);
2933
2934 return pending;
2935}
2936
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002937static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2938{
Chris Wilson0f911282012-04-17 10:05:38 +01002939 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01002940 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002941
2942 if (crtc->fb == NULL)
2943 return;
2944
Daniel Vetter2c10d572012-12-20 21:24:07 +01002945 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
2946
Chris Wilson5bb61642012-09-27 21:25:58 +01002947 wait_event(dev_priv->pending_flip_queue,
2948 !intel_crtc_has_pending_flip(crtc));
2949
Chris Wilson0f911282012-04-17 10:05:38 +01002950 mutex_lock(&dev->struct_mutex);
2951 intel_finish_fb(crtc->fb);
2952 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002953}
2954
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002955static bool ironlake_crtc_driving_pch(struct drm_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08002956{
2957 struct drm_device *dev = crtc->dev;
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002958 struct intel_encoder *intel_encoder;
Jesse Barnes040484a2011-01-03 12:14:26 -08002959
2960 /*
2961 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2962 * must be driven by its own crtc; no sharing is possible.
2963 */
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002964 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002965 switch (intel_encoder->type) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002966 case INTEL_OUTPUT_EDP:
Paulo Zanoni228d3e32012-08-10 10:05:10 -03002967 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnes040484a2011-01-03 12:14:26 -08002968 return false;
2969 continue;
2970 }
2971 }
2972
2973 return true;
2974}
2975
Paulo Zanonifc316cb2012-10-25 10:37:43 -02002976static bool haswell_crtc_driving_pch(struct drm_crtc *crtc)
2977{
2978 return intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG);
2979}
2980
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002981/* Program iCLKIP clock to the desired frequency */
2982static void lpt_program_iclkip(struct drm_crtc *crtc)
2983{
2984 struct drm_device *dev = crtc->dev;
2985 struct drm_i915_private *dev_priv = dev->dev_private;
2986 u32 divsel, phaseinc, auxdiv, phasedir = 0;
2987 u32 temp;
2988
Daniel Vetter09153002012-12-12 14:06:44 +01002989 mutex_lock(&dev_priv->dpio_lock);
2990
Eugeni Dodonove615efe2012-05-09 15:37:26 -03002991 /* It is necessary to ungate the pixclk gate prior to programming
2992 * the divisors, and gate it back when it is done.
2993 */
2994 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
2995
2996 /* Disable SSCCTL */
2997 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02002998 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
2999 SBI_SSCCTL_DISABLE,
3000 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003001
3002 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
3003 if (crtc->mode.clock == 20000) {
3004 auxdiv = 1;
3005 divsel = 0x41;
3006 phaseinc = 0x20;
3007 } else {
3008 /* The iCLK virtual clock root frequency is in MHz,
3009 * but the crtc->mode.clock in in KHz. To get the divisors,
3010 * it is necessary to divide one by another, so we
3011 * convert the virtual clock precision to KHz here for higher
3012 * precision.
3013 */
3014 u32 iclk_virtual_root_freq = 172800 * 1000;
3015 u32 iclk_pi_range = 64;
3016 u32 desired_divisor, msb_divisor_value, pi_value;
3017
3018 desired_divisor = (iclk_virtual_root_freq / crtc->mode.clock);
3019 msb_divisor_value = desired_divisor / iclk_pi_range;
3020 pi_value = desired_divisor % iclk_pi_range;
3021
3022 auxdiv = 0;
3023 divsel = msb_divisor_value - 2;
3024 phaseinc = pi_value;
3025 }
3026
3027 /* This should not happen with any sane values */
3028 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3029 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3030 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3031 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3032
3033 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
3034 crtc->mode.clock,
3035 auxdiv,
3036 divsel,
3037 phasedir,
3038 phaseinc);
3039
3040 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003041 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003042 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3043 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3044 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3045 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3046 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3047 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003048 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003049
3050 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003051 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003052 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3053 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003054 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003055
3056 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003057 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003058 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003059 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003060
3061 /* Wait for initialization time */
3062 udelay(24);
3063
3064 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003065
3066 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003067}
3068
Jesse Barnesf67a5592011-01-05 10:31:48 -08003069/*
3070 * Enable PCH resources required for PCH ports:
3071 * - PCH PLLs
3072 * - FDI training & RX/TX
3073 * - update transcoder timings
3074 * - DP transcoding bits
3075 * - transcoder
3076 */
3077static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003078{
3079 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003080 struct drm_i915_private *dev_priv = dev->dev_private;
3081 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3082 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003083 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003084
Chris Wilsone7e164d2012-05-11 09:21:25 +01003085 assert_transcoder_disabled(dev_priv, pipe);
3086
Daniel Vettercd986ab2012-10-26 10:58:12 +02003087 /* Write the TU size bits before fdi link training, so that error
3088 * detection works. */
3089 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3090 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3091
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003092 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003093 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003094
Daniel Vetter572deb32012-10-27 18:46:14 +02003095 /* XXX: pch pll's can be enabled any time before we enable the PCH
3096 * transcoder, and we actually should do this to not upset any PCH
3097 * transcoder that already use the clock when we share it.
3098 *
3099 * Note that enable_pch_pll tries to do the right thing, but get_pch_pll
3100 * unconditionally resets the pll - we need that to have the right LVDS
3101 * enable sequence. */
Paulo Zanonib6b4e182012-10-31 18:12:38 -02003102 ironlake_enable_pch_pll(intel_crtc);
Chris Wilson6f13b7b2012-05-13 09:54:09 +01003103
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003104 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003105 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003106
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003107 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003108 switch (pipe) {
3109 default:
3110 case 0:
3111 temp |= TRANSA_DPLL_ENABLE;
3112 sel = TRANSA_DPLLB_SEL;
3113 break;
3114 case 1:
3115 temp |= TRANSB_DPLL_ENABLE;
3116 sel = TRANSB_DPLLB_SEL;
3117 break;
3118 case 2:
3119 temp |= TRANSC_DPLL_ENABLE;
3120 sel = TRANSC_DPLLB_SEL;
3121 break;
Jesse Barnesd64311a2011-10-12 15:01:33 -07003122 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003123 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
3124 temp |= sel;
3125 else
3126 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003127 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003128 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003129
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003130 /* set transcoder timing, panel must allow it */
3131 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01003132 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3133 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3134 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3135
3136 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3137 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3138 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003139 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003140
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003141 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003142
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003143 /* For PCH DP, enable TRANS_DP_CTL */
3144 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003145 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3146 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003147 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003148 reg = TRANS_DP_CTL(pipe);
3149 temp = I915_READ(reg);
3150 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003151 TRANS_DP_SYNC_MASK |
3152 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003153 temp |= (TRANS_DP_OUTPUT_ENABLE |
3154 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003155 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003156
3157 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003158 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003159 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003160 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003161
3162 switch (intel_trans_dp_port_sel(crtc)) {
3163 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003164 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003165 break;
3166 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003167 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003168 break;
3169 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003170 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003171 break;
3172 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003173 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003174 }
3175
Chris Wilson5eddb702010-09-11 13:48:45 +01003176 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003177 }
3178
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003179 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003180}
3181
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003182static void lpt_pch_enable(struct drm_crtc *crtc)
3183{
3184 struct drm_device *dev = crtc->dev;
3185 struct drm_i915_private *dev_priv = dev->dev_private;
3186 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003187 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003188
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003189 assert_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003190
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003191 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003192
Paulo Zanoni0540e482012-10-31 18:12:40 -02003193 /* Set transcoder timing. */
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003194 I915_WRITE(_TRANS_HTOTAL_A, I915_READ(HTOTAL(cpu_transcoder)));
3195 I915_WRITE(_TRANS_HBLANK_A, I915_READ(HBLANK(cpu_transcoder)));
3196 I915_WRITE(_TRANS_HSYNC_A, I915_READ(HSYNC(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003197
Paulo Zanonidaed2db2012-10-31 18:12:41 -02003198 I915_WRITE(_TRANS_VTOTAL_A, I915_READ(VTOTAL(cpu_transcoder)));
3199 I915_WRITE(_TRANS_VBLANK_A, I915_READ(VBLANK(cpu_transcoder)));
3200 I915_WRITE(_TRANS_VSYNC_A, I915_READ(VSYNC(cpu_transcoder)));
3201 I915_WRITE(_TRANS_VSYNCSHIFT_A, I915_READ(VSYNCSHIFT(cpu_transcoder)));
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003202
Paulo Zanoni937bb612012-10-31 18:12:47 -02003203 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003204}
3205
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003206static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
3207{
3208 struct intel_pch_pll *pll = intel_crtc->pch_pll;
3209
3210 if (pll == NULL)
3211 return;
3212
3213 if (pll->refcount == 0) {
3214 WARN(1, "bad PCH PLL refcount\n");
3215 return;
3216 }
3217
3218 --pll->refcount;
3219 intel_crtc->pch_pll = NULL;
3220}
3221
3222static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
3223{
3224 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
3225 struct intel_pch_pll *pll;
3226 int i;
3227
3228 pll = intel_crtc->pch_pll;
3229 if (pll) {
3230 DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
3231 intel_crtc->base.base.id, pll->pll_reg);
3232 goto prepare;
3233 }
3234
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003235 if (HAS_PCH_IBX(dev_priv->dev)) {
3236 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
3237 i = intel_crtc->pipe;
3238 pll = &dev_priv->pch_plls[i];
3239
3240 DRM_DEBUG_KMS("CRTC:%d using pre-allocated PCH PLL %x\n",
3241 intel_crtc->base.base.id, pll->pll_reg);
3242
3243 goto found;
3244 }
3245
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003246 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3247 pll = &dev_priv->pch_plls[i];
3248
3249 /* Only want to check enabled timings first */
3250 if (pll->refcount == 0)
3251 continue;
3252
3253 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
3254 fp == I915_READ(pll->fp0_reg)) {
3255 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
3256 intel_crtc->base.base.id,
3257 pll->pll_reg, pll->refcount, pll->active);
3258
3259 goto found;
3260 }
3261 }
3262
3263 /* Ok no matching timings, maybe there's a free one? */
3264 for (i = 0; i < dev_priv->num_pch_pll; i++) {
3265 pll = &dev_priv->pch_plls[i];
3266 if (pll->refcount == 0) {
3267 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
3268 intel_crtc->base.base.id, pll->pll_reg);
3269 goto found;
3270 }
3271 }
3272
3273 return NULL;
3274
3275found:
3276 intel_crtc->pch_pll = pll;
3277 pll->refcount++;
3278 DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
3279prepare: /* separate function? */
3280 DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003281
Chris Wilsone04c7352012-05-02 20:43:56 +01003282 /* Wait for the clocks to stabilize before rewriting the regs */
3283 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003284 POSTING_READ(pll->pll_reg);
3285 udelay(150);
Chris Wilsone04c7352012-05-02 20:43:56 +01003286
3287 I915_WRITE(pll->fp0_reg, fp);
3288 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003289 pll->on = false;
3290 return pll;
3291}
3292
Jesse Barnesd4270e52011-10-11 10:43:02 -07003293void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3294{
3295 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003296 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003297 u32 temp;
3298
3299 temp = I915_READ(dslreg);
3300 udelay(500);
3301 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003302 if (wait_for(I915_READ(dslreg) != temp, 5))
3303 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3304 }
3305}
3306
Jesse Barnesf67a5592011-01-05 10:31:48 -08003307static void ironlake_crtc_enable(struct drm_crtc *crtc)
3308{
3309 struct drm_device *dev = crtc->dev;
3310 struct drm_i915_private *dev_priv = dev->dev_private;
3311 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003312 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003313 int pipe = intel_crtc->pipe;
3314 int plane = intel_crtc->plane;
3315 u32 temp;
3316 bool is_pch_port;
3317
Daniel Vetter08a48462012-07-02 11:43:47 +02003318 WARN_ON(!crtc->enabled);
3319
Jesse Barnesf67a5592011-01-05 10:31:48 -08003320 if (intel_crtc->active)
3321 return;
3322
3323 intel_crtc->active = true;
3324 intel_update_watermarks(dev);
3325
3326 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3327 temp = I915_READ(PCH_LVDS);
3328 if ((temp & LVDS_PORT_EN) == 0)
3329 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3330 }
3331
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003332 is_pch_port = ironlake_crtc_driving_pch(crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003333
Daniel Vetter46b6f812012-09-06 22:08:33 +02003334 if (is_pch_port) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003335 /* Note: FDI PLL enabling _must_ be done before we enable the
3336 * cpu pipes, hence this is separate from all the other fdi/pch
3337 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003338 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003339 } else {
3340 assert_fdi_tx_disabled(dev_priv, pipe);
3341 assert_fdi_rx_disabled(dev_priv, pipe);
3342 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003343
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003344 for_each_encoder_on_crtc(dev, crtc, encoder)
3345 if (encoder->pre_enable)
3346 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003347
3348 /* Enable panel fitting for LVDS */
3349 if (dev_priv->pch_pf_size &&
Jani Nikula547dc042012-11-02 11:24:03 +02003350 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3351 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnesf67a5592011-01-05 10:31:48 -08003352 /* Force use of hard-coded filter coefficients
3353 * as some pre-programmed values are broken,
3354 * e.g. x201.
3355 */
Paulo Zanoni13888d72012-11-20 13:27:41 -02003356 if (IS_IVYBRIDGE(dev))
3357 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3358 PF_PIPE_SEL_IVB(pipe));
3359 else
3360 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003361 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3362 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003363 }
3364
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003365 /*
3366 * On ILK+ LUT must be loaded before the pipe is running but with
3367 * clocks enabled
3368 */
3369 intel_crtc_load_lut(crtc);
3370
Jesse Barnesf67a5592011-01-05 10:31:48 -08003371 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3372 intel_enable_plane(dev_priv, plane, pipe);
3373
3374 if (is_pch_port)
3375 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003376
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003377 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003378 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003379 mutex_unlock(&dev->struct_mutex);
3380
Chris Wilson6b383a72010-09-13 13:54:26 +01003381 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003382
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003383 for_each_encoder_on_crtc(dev, crtc, encoder)
3384 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02003385
3386 if (HAS_PCH_CPT(dev))
3387 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02003388
3389 /*
3390 * There seems to be a race in PCH platform hw (at least on some
3391 * outputs) where an enabled pipe still completes any pageflip right
3392 * away (as if the pipe is off) instead of waiting for vblank. As soon
3393 * as the first vblank happend, everything works as expected. Hence just
3394 * wait for one vblank before returning to avoid strange things
3395 * happening.
3396 */
3397 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003398}
3399
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003400static void haswell_crtc_enable(struct drm_crtc *crtc)
3401{
3402 struct drm_device *dev = crtc->dev;
3403 struct drm_i915_private *dev_priv = dev->dev_private;
3404 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3405 struct intel_encoder *encoder;
3406 int pipe = intel_crtc->pipe;
3407 int plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003408 bool is_pch_port;
3409
3410 WARN_ON(!crtc->enabled);
3411
3412 if (intel_crtc->active)
3413 return;
3414
3415 intel_crtc->active = true;
3416 intel_update_watermarks(dev);
3417
Paulo Zanonifc316cb2012-10-25 10:37:43 -02003418 is_pch_port = haswell_crtc_driving_pch(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003419
Paulo Zanoni83616632012-10-23 18:29:54 -02003420 if (is_pch_port)
Paulo Zanoni04945642012-11-01 21:00:59 -02003421 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003422
3423 for_each_encoder_on_crtc(dev, crtc, encoder)
3424 if (encoder->pre_enable)
3425 encoder->pre_enable(encoder);
3426
Paulo Zanoni1f544382012-10-24 11:32:00 -02003427 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003428
Paulo Zanoni1f544382012-10-24 11:32:00 -02003429 /* Enable panel fitting for eDP */
Jani Nikula547dc042012-11-02 11:24:03 +02003430 if (dev_priv->pch_pf_size &&
3431 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003432 /* Force use of hard-coded filter coefficients
3433 * as some pre-programmed values are broken,
3434 * e.g. x201.
3435 */
Paulo Zanoni54075a72012-11-20 13:27:42 -02003436 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3437 PF_PIPE_SEL_IVB(pipe));
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003438 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3439 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
3440 }
3441
3442 /*
3443 * On ILK+ LUT must be loaded before the pipe is running but with
3444 * clocks enabled
3445 */
3446 intel_crtc_load_lut(crtc);
3447
Paulo Zanoni1f544382012-10-24 11:32:00 -02003448 intel_ddi_set_pipe_settings(crtc);
3449 intel_ddi_enable_pipe_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003450
3451 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3452 intel_enable_plane(dev_priv, plane, pipe);
3453
3454 if (is_pch_port)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003455 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003456
3457 mutex_lock(&dev->struct_mutex);
3458 intel_update_fbc(dev);
3459 mutex_unlock(&dev->struct_mutex);
3460
3461 intel_crtc_update_cursor(crtc, true);
3462
3463 for_each_encoder_on_crtc(dev, crtc, encoder)
3464 encoder->enable(encoder);
3465
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003466 /*
3467 * There seems to be a race in PCH platform hw (at least on some
3468 * outputs) where an enabled pipe still completes any pageflip right
3469 * away (as if the pipe is off) instead of waiting for vblank. As soon
3470 * as the first vblank happend, everything works as expected. Hence just
3471 * wait for one vblank before returning to avoid strange things
3472 * happening.
3473 */
3474 intel_wait_for_vblank(dev, intel_crtc->pipe);
3475}
3476
Jesse Barnes6be4a602010-09-10 10:26:01 -07003477static void ironlake_crtc_disable(struct drm_crtc *crtc)
3478{
3479 struct drm_device *dev = crtc->dev;
3480 struct drm_i915_private *dev_priv = dev->dev_private;
3481 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003482 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003483 int pipe = intel_crtc->pipe;
3484 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003485 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003486
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003487
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003488 if (!intel_crtc->active)
3489 return;
3490
Daniel Vetterea9d7582012-07-10 10:42:52 +02003491 for_each_encoder_on_crtc(dev, crtc, encoder)
3492 encoder->disable(encoder);
3493
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003494 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003495 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01003496 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01003497
Jesse Barnesb24e7172011-01-04 15:09:30 -08003498 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003499
Chris Wilson973d04f2011-07-08 12:22:37 +01003500 if (dev_priv->cfb_plane == plane)
3501 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003502
Jesse Barnesb24e7172011-01-04 15:09:30 -08003503 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003504
Jesse Barnes6be4a602010-09-10 10:26:01 -07003505 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003506 I915_WRITE(PF_CTL(pipe), 0);
3507 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003508
Daniel Vetterbf49ec82012-09-06 22:15:40 +02003509 for_each_encoder_on_crtc(dev, crtc, encoder)
3510 if (encoder->post_disable)
3511 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003512
Chris Wilson5eddb702010-09-11 13:48:45 +01003513 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003514
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003515 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003516
3517 if (HAS_PCH_CPT(dev)) {
3518 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003519 reg = TRANS_DP_CTL(pipe);
3520 temp = I915_READ(reg);
3521 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08003522 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01003523 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003524
3525 /* disable DPLL_SEL */
3526 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003527 switch (pipe) {
3528 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07003529 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003530 break;
3531 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07003532 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003533 break;
3534 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07003535 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07003536 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003537 break;
3538 default:
3539 BUG(); /* wtf */
3540 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07003541 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003542 }
3543
3544 /* disable PCH DPLL */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003545 intel_disable_pch_pll(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003546
Daniel Vetter88cefb62012-08-12 19:27:14 +02003547 ironlake_fdi_pll_disable(intel_crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +01003548
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003549 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003550 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003551
3552 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003553 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003554 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003555}
3556
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003557static void haswell_crtc_disable(struct drm_crtc *crtc)
3558{
3559 struct drm_device *dev = crtc->dev;
3560 struct drm_i915_private *dev_priv = dev->dev_private;
3561 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3562 struct intel_encoder *encoder;
3563 int pipe = intel_crtc->pipe;
3564 int plane = intel_crtc->plane;
Paulo Zanoniad80a812012-10-24 16:06:19 -02003565 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoni83616632012-10-23 18:29:54 -02003566 bool is_pch_port;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003567
3568 if (!intel_crtc->active)
3569 return;
3570
Paulo Zanoni83616632012-10-23 18:29:54 -02003571 is_pch_port = haswell_crtc_driving_pch(crtc);
3572
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003573 for_each_encoder_on_crtc(dev, crtc, encoder)
3574 encoder->disable(encoder);
3575
3576 intel_crtc_wait_for_pending_flips(crtc);
3577 drm_vblank_off(dev, pipe);
3578 intel_crtc_update_cursor(crtc, false);
3579
3580 intel_disable_plane(dev_priv, plane, pipe);
3581
3582 if (dev_priv->cfb_plane == plane)
3583 intel_disable_fbc(dev);
3584
3585 intel_disable_pipe(dev_priv, pipe);
3586
Paulo Zanoniad80a812012-10-24 16:06:19 -02003587 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003588
3589 /* Disable PF */
3590 I915_WRITE(PF_CTL(pipe), 0);
3591 I915_WRITE(PF_WIN_SZ(pipe), 0);
3592
Paulo Zanoni1f544382012-10-24 11:32:00 -02003593 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003594
3595 for_each_encoder_on_crtc(dev, crtc, encoder)
3596 if (encoder->post_disable)
3597 encoder->post_disable(encoder);
3598
Paulo Zanoni83616632012-10-23 18:29:54 -02003599 if (is_pch_port) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02003600 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02003601 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02003602 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02003603
3604 intel_crtc->active = false;
3605 intel_update_watermarks(dev);
3606
3607 mutex_lock(&dev->struct_mutex);
3608 intel_update_fbc(dev);
3609 mutex_unlock(&dev->struct_mutex);
3610}
3611
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003612static void ironlake_crtc_off(struct drm_crtc *crtc)
3613{
3614 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3615 intel_put_pch_pll(intel_crtc);
3616}
3617
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003618static void haswell_crtc_off(struct drm_crtc *crtc)
3619{
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003620 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3621
3622 /* Stop saying we're using TRANSCODER_EDP because some other CRTC might
3623 * start using it. */
Daniel Vetter1a240d42012-11-29 22:18:51 +01003624 intel_crtc->cpu_transcoder = (enum transcoder) intel_crtc->pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02003625
Paulo Zanoni6441ab52012-10-05 12:05:58 -03003626 intel_ddi_put_crtc_pll(crtc);
3627}
3628
Daniel Vetter02e792f2009-09-15 22:57:34 +02003629static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3630{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003631 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003632 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003633 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003634
Chris Wilson23f09ce2010-08-12 13:53:37 +01003635 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003636 dev_priv->mm.interruptible = false;
3637 (void) intel_overlay_switch_off(intel_crtc->overlay);
3638 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003639 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003640 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003641
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003642 /* Let userspace switch the overlay on again. In most cases userspace
3643 * has to recompute where to put it anyway.
3644 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003645}
3646
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003647static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003648{
3649 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003650 struct drm_i915_private *dev_priv = dev->dev_private;
3651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003652 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08003653 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003654 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003655
Daniel Vetter08a48462012-07-02 11:43:47 +02003656 WARN_ON(!crtc->enabled);
3657
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003658 if (intel_crtc->active)
3659 return;
3660
3661 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003662 intel_update_watermarks(dev);
3663
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003664 intel_enable_pll(dev_priv, pipe);
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02003665
3666 for_each_encoder_on_crtc(dev, crtc, encoder)
3667 if (encoder->pre_enable)
3668 encoder->pre_enable(encoder);
3669
Jesse Barnes040484a2011-01-03 12:14:26 -08003670 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003671 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003672
3673 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003674 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003675
3676 /* Give the overlay scaler a chance to enable if it's on this pipe */
3677 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003678 intel_crtc_update_cursor(crtc, true);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003679
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02003680 for_each_encoder_on_crtc(dev, crtc, encoder)
3681 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003682}
3683
3684static void i9xx_crtc_disable(struct drm_crtc *crtc)
3685{
3686 struct drm_device *dev = crtc->dev;
3687 struct drm_i915_private *dev_priv = dev->dev_private;
3688 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003689 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003690 int pipe = intel_crtc->pipe;
3691 int plane = intel_crtc->plane;
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003692 u32 pctl;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003693
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003694
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003695 if (!intel_crtc->active)
3696 return;
3697
Daniel Vetterea9d7582012-07-10 10:42:52 +02003698 for_each_encoder_on_crtc(dev, crtc, encoder)
3699 encoder->disable(encoder);
3700
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003701 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003702 intel_crtc_wait_for_pending_flips(crtc);
3703 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003704 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003705 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003706
Chris Wilson973d04f2011-07-08 12:22:37 +01003707 if (dev_priv->cfb_plane == plane)
3708 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003709
Jesse Barnesb24e7172011-01-04 15:09:30 -08003710 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003711 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02003712
3713 /* Disable pannel fitter if it is on this pipe. */
3714 pctl = I915_READ(PFIT_CONTROL);
3715 if ((pctl & PFIT_ENABLE) &&
3716 ((pctl & PFIT_PIPE_MASK) >> PFIT_PIPE_SHIFT) == pipe)
3717 I915_WRITE(PFIT_CONTROL, 0);
3718
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003719 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003720
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003721 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003722 intel_update_fbc(dev);
3723 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003724}
3725
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003726static void i9xx_crtc_off(struct drm_crtc *crtc)
3727{
3728}
3729
Daniel Vetter976f8a22012-07-08 22:34:21 +02003730static void intel_crtc_update_sarea(struct drm_crtc *crtc,
3731 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003732{
3733 struct drm_device *dev = crtc->dev;
3734 struct drm_i915_master_private *master_priv;
3735 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3736 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08003737
3738 if (!dev->primary->master)
3739 return;
3740
3741 master_priv = dev->primary->master->driver_priv;
3742 if (!master_priv->sarea_priv)
3743 return;
3744
Jesse Barnes79e53942008-11-07 14:24:08 -08003745 switch (pipe) {
3746 case 0:
3747 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3748 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3749 break;
3750 case 1:
3751 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3752 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3753 break;
3754 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003755 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003756 break;
3757 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003758}
3759
Daniel Vetter976f8a22012-07-08 22:34:21 +02003760/**
3761 * Sets the power management mode of the pipe and plane.
3762 */
3763void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01003764{
Chris Wilsoncdd59982010-09-08 16:30:16 +01003765 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003766 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003767 struct intel_encoder *intel_encoder;
3768 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003769
Daniel Vetter976f8a22012-07-08 22:34:21 +02003770 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
3771 enable |= intel_encoder->connectors_active;
3772
3773 if (enable)
3774 dev_priv->display.crtc_enable(crtc);
3775 else
3776 dev_priv->display.crtc_disable(crtc);
3777
3778 intel_crtc_update_sarea(crtc, enable);
3779}
3780
3781static void intel_crtc_noop(struct drm_crtc *crtc)
3782{
3783}
3784
3785static void intel_crtc_disable(struct drm_crtc *crtc)
3786{
3787 struct drm_device *dev = crtc->dev;
3788 struct drm_connector *connector;
3789 struct drm_i915_private *dev_priv = dev->dev_private;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003790 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003791
3792 /* crtc should still be enabled when we disable it. */
3793 WARN_ON(!crtc->enabled);
3794
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08003795 intel_crtc->eld_vld = false;
Daniel Vetter976f8a22012-07-08 22:34:21 +02003796 dev_priv->display.crtc_disable(crtc);
3797 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003798 dev_priv->display.off(crtc);
3799
Chris Wilson931872f2012-01-16 23:01:13 +00003800 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3801 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003802
3803 if (crtc->fb) {
3804 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003805 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003806 mutex_unlock(&dev->struct_mutex);
Daniel Vetter976f8a22012-07-08 22:34:21 +02003807 crtc->fb = NULL;
3808 }
3809
3810 /* Update computed state. */
3811 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3812 if (!connector->encoder || !connector->encoder->crtc)
3813 continue;
3814
3815 if (connector->encoder->crtc != crtc)
3816 continue;
3817
3818 connector->dpms = DRM_MODE_DPMS_OFF;
3819 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003820 }
3821}
3822
Daniel Vettera261b242012-07-26 19:21:47 +02003823void intel_modeset_disable(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003824{
Daniel Vettera261b242012-07-26 19:21:47 +02003825 struct drm_crtc *crtc;
3826
3827 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3828 if (crtc->enabled)
3829 intel_crtc_disable(crtc);
3830 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003831}
3832
Daniel Vetter1f703852012-07-11 16:51:39 +02003833void intel_encoder_noop(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003834{
Jesse Barnes79e53942008-11-07 14:24:08 -08003835}
3836
Chris Wilsonea5b2132010-08-04 13:50:23 +01003837void intel_encoder_destroy(struct drm_encoder *encoder)
3838{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003839 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003840
Chris Wilsonea5b2132010-08-04 13:50:23 +01003841 drm_encoder_cleanup(encoder);
3842 kfree(intel_encoder);
3843}
3844
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003845/* Simple dpms helper for encodres with just one connector, no cloning and only
3846 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
3847 * state of the entire output pipe. */
3848void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
3849{
3850 if (mode == DRM_MODE_DPMS_ON) {
3851 encoder->connectors_active = true;
3852
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003853 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003854 } else {
3855 encoder->connectors_active = false;
3856
Daniel Vetterb2cabb02012-07-01 22:42:24 +02003857 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003858 }
3859}
3860
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003861/* Cross check the actual hw state with our own modeset state tracking (and it's
3862 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02003863static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003864{
3865 if (connector->get_hw_state(connector)) {
3866 struct intel_encoder *encoder = connector->encoder;
3867 struct drm_crtc *crtc;
3868 bool encoder_enabled;
3869 enum pipe pipe;
3870
3871 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
3872 connector->base.base.id,
3873 drm_get_connector_name(&connector->base));
3874
3875 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
3876 "wrong connector dpms state\n");
3877 WARN(connector->base.encoder != &encoder->base,
3878 "active connector not linked to encoder\n");
3879 WARN(!encoder->connectors_active,
3880 "encoder->connectors_active not set\n");
3881
3882 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
3883 WARN(!encoder_enabled, "encoder not enabled\n");
3884 if (WARN_ON(!encoder->base.crtc))
3885 return;
3886
3887 crtc = encoder->base.crtc;
3888
3889 WARN(!crtc->enabled, "crtc not enabled\n");
3890 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
3891 WARN(pipe != to_intel_crtc(crtc)->pipe,
3892 "encoder active on the wrong pipe\n");
3893 }
3894}
3895
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003896/* Even simpler default implementation, if there's really no special case to
3897 * consider. */
3898void intel_connector_dpms(struct drm_connector *connector, int mode)
3899{
3900 struct intel_encoder *encoder = intel_attached_encoder(connector);
3901
3902 /* All the simple cases only support two dpms states. */
3903 if (mode != DRM_MODE_DPMS_ON)
3904 mode = DRM_MODE_DPMS_OFF;
3905
3906 if (mode == connector->dpms)
3907 return;
3908
3909 connector->dpms = mode;
3910
3911 /* Only need to change hw state when actually enabled */
3912 if (encoder->base.crtc)
3913 intel_encoder_dpms(encoder, mode);
3914 else
Daniel Vetter8af6cf82012-07-10 09:50:11 +02003915 WARN_ON(encoder->connectors_active != false);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02003916
Daniel Vetterb9805142012-08-31 17:37:33 +02003917 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02003918}
3919
Daniel Vetterf0947c32012-07-02 13:10:34 +02003920/* Simple connector->get_hw_state implementation for encoders that support only
3921 * one connector and no cloning and hence the encoder state determines the state
3922 * of the connector. */
3923bool intel_connector_get_hw_state(struct intel_connector *connector)
3924{
Daniel Vetter24929352012-07-02 20:28:59 +02003925 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02003926 struct intel_encoder *encoder = connector->encoder;
3927
3928 return encoder->get_hw_state(encoder, &pipe);
3929}
3930
Jesse Barnes79e53942008-11-07 14:24:08 -08003931static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
Daniel Vetter35313cd2012-07-20 10:30:45 +02003932 const struct drm_display_mode *mode,
Jesse Barnes79e53942008-11-07 14:24:08 -08003933 struct drm_display_mode *adjusted_mode)
3934{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003935 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01003936
Eric Anholtbad720f2009-10-22 16:11:14 -07003937 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08003938 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07003939 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3940 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003941 }
Chris Wilson89749352010-09-12 18:25:19 +01003942
Daniel Vetterf9bef082012-04-15 19:53:19 +02003943 /* All interlaced capable intel hw wants timings in frames. Note though
3944 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3945 * timings, so we need to be careful not to clobber these.*/
3946 if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3947 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01003948
Chris Wilson44f46b422012-06-21 13:19:59 +03003949 /* WaPruneModeWithIncorrectHsyncOffset: Cantiga+ cannot handle modes
3950 * with a hsync front porch of 0.
3951 */
3952 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
3953 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
3954 return false;
3955
Jesse Barnes79e53942008-11-07 14:24:08 -08003956 return true;
3957}
3958
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07003959static int valleyview_get_display_clock_speed(struct drm_device *dev)
3960{
3961 return 400000; /* FIXME */
3962}
3963
Jesse Barnese70236a2009-09-21 10:42:27 -07003964static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003965{
Jesse Barnese70236a2009-09-21 10:42:27 -07003966 return 400000;
3967}
Jesse Barnes79e53942008-11-07 14:24:08 -08003968
Jesse Barnese70236a2009-09-21 10:42:27 -07003969static int i915_get_display_clock_speed(struct drm_device *dev)
3970{
3971 return 333000;
3972}
Jesse Barnes79e53942008-11-07 14:24:08 -08003973
Jesse Barnese70236a2009-09-21 10:42:27 -07003974static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3975{
3976 return 200000;
3977}
Jesse Barnes79e53942008-11-07 14:24:08 -08003978
Jesse Barnese70236a2009-09-21 10:42:27 -07003979static int i915gm_get_display_clock_speed(struct drm_device *dev)
3980{
3981 u16 gcfgc = 0;
3982
3983 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3984
3985 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08003986 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07003987 else {
3988 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3989 case GC_DISPLAY_CLOCK_333_MHZ:
3990 return 333000;
3991 default:
3992 case GC_DISPLAY_CLOCK_190_200_MHZ:
3993 return 190000;
3994 }
3995 }
3996}
Jesse Barnes79e53942008-11-07 14:24:08 -08003997
Jesse Barnese70236a2009-09-21 10:42:27 -07003998static int i865_get_display_clock_speed(struct drm_device *dev)
3999{
4000 return 266000;
4001}
4002
4003static int i855_get_display_clock_speed(struct drm_device *dev)
4004{
4005 u16 hpllcc = 0;
4006 /* Assume that the hardware is in the high speed state. This
4007 * should be the default.
4008 */
4009 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
4010 case GC_CLOCK_133_200:
4011 case GC_CLOCK_100_200:
4012 return 200000;
4013 case GC_CLOCK_166_250:
4014 return 250000;
4015 case GC_CLOCK_100_133:
4016 return 133000;
4017 }
4018
4019 /* Shouldn't happen */
4020 return 0;
4021}
4022
4023static int i830_get_display_clock_speed(struct drm_device *dev)
4024{
4025 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08004026}
4027
Zhenyu Wang2c072452009-06-05 15:38:42 +08004028static void
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004029intel_reduce_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004030{
4031 while (*num > 0xffffff || *den > 0xffffff) {
4032 *num >>= 1;
4033 *den >>= 1;
4034 }
4035}
4036
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004037void
4038intel_link_compute_m_n(int bits_per_pixel, int nlanes,
4039 int pixel_clock, int link_clock,
4040 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004041{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004042 m_n->tu = 64;
Chris Wilson22ed1112010-12-04 01:01:29 +00004043 m_n->gmch_m = bits_per_pixel * pixel_clock;
4044 m_n->gmch_n = link_clock * nlanes * 8;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004045 intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
Chris Wilson22ed1112010-12-04 01:01:29 +00004046 m_n->link_m = pixel_clock;
4047 m_n->link_n = link_clock;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01004048 intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004049}
4050
Chris Wilsona7615032011-01-12 17:04:08 +00004051static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4052{
Keith Packard72bbe582011-09-26 16:09:45 -07004053 if (i915_panel_use_ssc >= 0)
4054 return i915_panel_use_ssc != 0;
4055 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004056 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004057}
4058
Jesse Barnes5a354202011-06-24 12:19:22 -07004059/**
4060 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
4061 * @crtc: CRTC structure
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004062 * @mode: requested mode
Jesse Barnes5a354202011-06-24 12:19:22 -07004063 *
4064 * A pipe may be connected to one or more outputs. Based on the depth of the
4065 * attached framebuffer, choose a good color depth to use on the pipe.
4066 *
4067 * If possible, match the pipe depth to the fb depth. In some cases, this
4068 * isn't ideal, because the connected output supports a lesser or restricted
4069 * set of depths. Resolve that here:
4070 * LVDS typically supports only 6bpc, so clamp down in that case
4071 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
4072 * Displays may support a restricted set as well, check EDID and clamp as
4073 * appropriate.
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004074 * DP may want to dither down to 6bpc to fit larger modes
Jesse Barnes5a354202011-06-24 12:19:22 -07004075 *
4076 * RETURNS:
4077 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
4078 * true if they don't match).
4079 */
4080static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004081 struct drm_framebuffer *fb,
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004082 unsigned int *pipe_bpp,
4083 struct drm_display_mode *mode)
Jesse Barnes5a354202011-06-24 12:19:22 -07004084{
4085 struct drm_device *dev = crtc->dev;
4086 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes5a354202011-06-24 12:19:22 -07004087 struct drm_connector *connector;
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004088 struct intel_encoder *intel_encoder;
Jesse Barnes5a354202011-06-24 12:19:22 -07004089 unsigned int display_bpc = UINT_MAX, bpc;
4090
4091 /* Walk the encoders & connectors on this crtc, get min bpc */
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004092 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004093
4094 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
4095 unsigned int lvds_bpc;
4096
4097 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
4098 LVDS_A3_POWER_UP)
4099 lvds_bpc = 8;
4100 else
4101 lvds_bpc = 6;
4102
4103 if (lvds_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004104 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004105 display_bpc = lvds_bpc;
4106 }
4107 continue;
4108 }
4109
Jesse Barnes5a354202011-06-24 12:19:22 -07004110 /* Not one of the known troublemakers, check the EDID */
4111 list_for_each_entry(connector, &dev->mode_config.connector_list,
4112 head) {
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004113 if (connector->encoder != &intel_encoder->base)
Jesse Barnes5a354202011-06-24 12:19:22 -07004114 continue;
4115
Jesse Barnes62ac41a2011-07-28 12:55:14 -07004116 /* Don't use an invalid EDID bpc value */
4117 if (connector->display_info.bpc &&
4118 connector->display_info.bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004119 DRM_DEBUG_KMS("clamping display bpc (was %d) to EDID reported max of %d\n", display_bpc, connector->display_info.bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004120 display_bpc = connector->display_info.bpc;
4121 }
4122 }
4123
Jani Nikula2f4f6492012-11-12 14:33:44 +02004124 if (intel_encoder->type == INTEL_OUTPUT_EDP) {
4125 /* Use VBT settings if we have an eDP panel */
4126 unsigned int edp_bpc = dev_priv->edp.bpp / 3;
4127
Jani Nikula9a30a612012-11-12 14:33:45 +02004128 if (edp_bpc && edp_bpc < display_bpc) {
Jani Nikula2f4f6492012-11-12 14:33:44 +02004129 DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
4130 display_bpc = edp_bpc;
4131 }
4132 continue;
4133 }
4134
Jesse Barnes5a354202011-06-24 12:19:22 -07004135 /*
4136 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
4137 * through, clamp it down. (Note: >12bpc will be caught below.)
4138 */
4139 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
4140 if (display_bpc > 8 && display_bpc < 12) {
Adam Jackson82820492011-10-10 16:33:34 -04004141 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004142 display_bpc = 12;
4143 } else {
Adam Jackson82820492011-10-10 16:33:34 -04004144 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004145 display_bpc = 8;
4146 }
4147 }
4148 }
4149
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004150 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4151 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
4152 display_bpc = 6;
4153 }
4154
Jesse Barnes5a354202011-06-24 12:19:22 -07004155 /*
4156 * We could just drive the pipe at the highest bpc all the time and
4157 * enable dithering as needed, but that costs bandwidth. So choose
4158 * the minimum value that expresses the full color range of the fb but
4159 * also stays within the max display bpc discovered above.
4160 */
4161
Daniel Vetter94352cf2012-07-05 22:51:56 +02004162 switch (fb->depth) {
Jesse Barnes5a354202011-06-24 12:19:22 -07004163 case 8:
4164 bpc = 8; /* since we go through a colormap */
4165 break;
4166 case 15:
4167 case 16:
4168 bpc = 6; /* min is 18bpp */
4169 break;
4170 case 24:
Keith Packard578393c2011-09-05 11:53:21 -07004171 bpc = 8;
Jesse Barnes5a354202011-06-24 12:19:22 -07004172 break;
4173 case 30:
Keith Packard578393c2011-09-05 11:53:21 -07004174 bpc = 10;
Jesse Barnes5a354202011-06-24 12:19:22 -07004175 break;
4176 case 48:
Keith Packard578393c2011-09-05 11:53:21 -07004177 bpc = 12;
Jesse Barnes5a354202011-06-24 12:19:22 -07004178 break;
4179 default:
4180 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
4181 bpc = min((unsigned int)8, display_bpc);
4182 break;
4183 }
4184
Keith Packard578393c2011-09-05 11:53:21 -07004185 display_bpc = min(display_bpc, bpc);
4186
Adam Jackson82820492011-10-10 16:33:34 -04004187 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
4188 bpc, display_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004189
Keith Packard578393c2011-09-05 11:53:21 -07004190 *pipe_bpp = display_bpc * 3;
Jesse Barnes5a354202011-06-24 12:19:22 -07004191
4192 return display_bpc != bpc;
4193}
4194
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004195static int vlv_get_refclk(struct drm_crtc *crtc)
4196{
4197 struct drm_device *dev = crtc->dev;
4198 struct drm_i915_private *dev_priv = dev->dev_private;
4199 int refclk = 27000; /* for DP & HDMI */
4200
4201 return 100000; /* only one validated so far */
4202
4203 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
4204 refclk = 96000;
4205 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4206 if (intel_panel_use_ssc(dev_priv))
4207 refclk = 100000;
4208 else
4209 refclk = 96000;
4210 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4211 refclk = 100000;
4212 }
4213
4214 return refclk;
4215}
4216
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004217static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
4218{
4219 struct drm_device *dev = crtc->dev;
4220 struct drm_i915_private *dev_priv = dev->dev_private;
4221 int refclk;
4222
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004223 if (IS_VALLEYVIEW(dev)) {
4224 refclk = vlv_get_refclk(crtc);
4225 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004226 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4227 refclk = dev_priv->lvds_ssc_freq * 1000;
4228 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4229 refclk / 1000);
4230 } else if (!IS_GEN2(dev)) {
4231 refclk = 96000;
4232 } else {
4233 refclk = 48000;
4234 }
4235
4236 return refclk;
4237}
4238
4239static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
4240 intel_clock_t *clock)
4241{
4242 /* SDVO TV has fixed PLL values depend on its clock range,
4243 this mirrors vbios setting. */
4244 if (adjusted_mode->clock >= 100000
4245 && adjusted_mode->clock < 140500) {
4246 clock->p1 = 2;
4247 clock->p2 = 10;
4248 clock->n = 3;
4249 clock->m1 = 16;
4250 clock->m2 = 8;
4251 } else if (adjusted_mode->clock >= 140500
4252 && adjusted_mode->clock <= 200000) {
4253 clock->p1 = 1;
4254 clock->p2 = 10;
4255 clock->n = 6;
4256 clock->m1 = 12;
4257 clock->m2 = 8;
4258 }
4259}
4260
Jesse Barnesa7516a02011-12-15 12:30:37 -08004261static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
4262 intel_clock_t *clock,
4263 intel_clock_t *reduced_clock)
4264{
4265 struct drm_device *dev = crtc->dev;
4266 struct drm_i915_private *dev_priv = dev->dev_private;
4267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4268 int pipe = intel_crtc->pipe;
4269 u32 fp, fp2 = 0;
4270
4271 if (IS_PINEVIEW(dev)) {
4272 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
4273 if (reduced_clock)
4274 fp2 = (1 << reduced_clock->n) << 16 |
4275 reduced_clock->m1 << 8 | reduced_clock->m2;
4276 } else {
4277 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
4278 if (reduced_clock)
4279 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
4280 reduced_clock->m2;
4281 }
4282
4283 I915_WRITE(FP0(pipe), fp);
4284
4285 intel_crtc->lowfreq_avail = false;
4286 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4287 reduced_clock && i915_powersave) {
4288 I915_WRITE(FP1(pipe), fp2);
4289 intel_crtc->lowfreq_avail = true;
4290 } else {
4291 I915_WRITE(FP1(pipe), fp);
4292 }
4293}
4294
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004295static void vlv_update_pll(struct drm_crtc *crtc,
4296 struct drm_display_mode *mode,
4297 struct drm_display_mode *adjusted_mode,
4298 intel_clock_t *clock, intel_clock_t *reduced_clock,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304299 int num_connectors)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004300{
4301 struct drm_device *dev = crtc->dev;
4302 struct drm_i915_private *dev_priv = dev->dev_private;
4303 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4304 int pipe = intel_crtc->pipe;
4305 u32 dpll, mdiv, pdiv;
4306 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304307 bool is_sdvo;
4308 u32 temp;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004309
Daniel Vetter09153002012-12-12 14:06:44 +01004310 mutex_lock(&dev_priv->dpio_lock);
4311
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304312 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4313 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4314
4315 dpll = DPLL_VGA_MODE_DIS;
4316 dpll |= DPLL_EXT_BUFFER_ENABLE_VLV;
4317 dpll |= DPLL_REFA_CLK_ENABLE_VLV;
4318 dpll |= DPLL_INTEGRATED_CLOCK_VLV;
4319
4320 I915_WRITE(DPLL(pipe), dpll);
4321 POSTING_READ(DPLL(pipe));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004322
4323 bestn = clock->n;
4324 bestm1 = clock->m1;
4325 bestm2 = clock->m2;
4326 bestp1 = clock->p1;
4327 bestp2 = clock->p2;
4328
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304329 /*
4330 * In Valleyview PLL and program lane counter registers are exposed
4331 * through DPIO interface
4332 */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004333 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
4334 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
4335 mdiv |= ((bestn << DPIO_N_SHIFT));
4336 mdiv |= (1 << DPIO_POST_DIV_SHIFT);
4337 mdiv |= (1 << DPIO_K_SHIFT);
4338 mdiv |= DPIO_ENABLE_CALIBRATION;
4339 intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
4340
4341 intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), 0x01000000);
4342
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304343 pdiv = (1 << DPIO_REFSEL_OVERRIDE) | (5 << DPIO_PLL_MODESEL_SHIFT) |
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004344 (3 << DPIO_BIAS_CURRENT_CTL_SHIFT) | (1<<20) |
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304345 (7 << DPIO_PLL_REFCLK_SEL_SHIFT) | (8 << DPIO_DRIVER_CTL_SHIFT) |
4346 (5 << DPIO_CLK_BIAS_CTL_SHIFT);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004347 intel_dpio_write(dev_priv, DPIO_REFSFR(pipe), pdiv);
4348
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304349 intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe), 0x005f003b);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004350
4351 dpll |= DPLL_VCO_ENABLE;
4352 I915_WRITE(DPLL(pipe), dpll);
4353 POSTING_READ(DPLL(pipe));
4354 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
4355 DRM_ERROR("DPLL %d failed to lock\n", pipe);
4356
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304357 intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x620);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004358
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304359 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4360 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4361
4362 I915_WRITE(DPLL(pipe), dpll);
4363
4364 /* Wait for the clocks to stabilize. */
4365 POSTING_READ(DPLL(pipe));
4366 udelay(150);
4367
4368 temp = 0;
4369 if (is_sdvo) {
4370 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004371 if (temp > 1)
4372 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4373 else
4374 temp = 0;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004375 }
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304376 I915_WRITE(DPLL_MD(pipe), temp);
4377 POSTING_READ(DPLL_MD(pipe));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004378
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304379 /* Now program lane control registers */
4380 if(intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)
4381 || intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
4382 {
4383 temp = 0x1000C4;
4384 if(pipe == 1)
4385 temp |= (1 << 21);
4386 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL1, temp);
4387 }
4388 if(intel_pipe_has_type(crtc,INTEL_OUTPUT_EDP))
4389 {
4390 temp = 0x1000C4;
4391 if(pipe == 1)
4392 temp |= (1 << 21);
4393 intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL2, temp);
4394 }
Daniel Vetter09153002012-12-12 14:06:44 +01004395
4396 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004397}
4398
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004399static void i9xx_update_pll(struct drm_crtc *crtc,
4400 struct drm_display_mode *mode,
4401 struct drm_display_mode *adjusted_mode,
4402 intel_clock_t *clock, intel_clock_t *reduced_clock,
4403 int num_connectors)
4404{
4405 struct drm_device *dev = crtc->dev;
4406 struct drm_i915_private *dev_priv = dev->dev_private;
4407 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004408 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004409 int pipe = intel_crtc->pipe;
4410 u32 dpll;
4411 bool is_sdvo;
4412
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304413 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4414
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004415 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
4416 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
4417
4418 dpll = DPLL_VGA_MODE_DIS;
4419
4420 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
4421 dpll |= DPLLB_MODE_LVDS;
4422 else
4423 dpll |= DPLLB_MODE_DAC_SERIAL;
4424 if (is_sdvo) {
4425 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4426 if (pixel_multiplier > 1) {
4427 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4428 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
4429 }
4430 dpll |= DPLL_DVO_HIGH_SPEED;
4431 }
4432 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4433 dpll |= DPLL_DVO_HIGH_SPEED;
4434
4435 /* compute bitmask from p1 value */
4436 if (IS_PINEVIEW(dev))
4437 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
4438 else {
4439 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4440 if (IS_G4X(dev) && reduced_clock)
4441 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4442 }
4443 switch (clock->p2) {
4444 case 5:
4445 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4446 break;
4447 case 7:
4448 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4449 break;
4450 case 10:
4451 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4452 break;
4453 case 14:
4454 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4455 break;
4456 }
4457 if (INTEL_INFO(dev)->gen >= 4)
4458 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
4459
4460 if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4461 dpll |= PLL_REF_INPUT_TVCLKINBC;
4462 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
4463 /* XXX: just matching BIOS for now */
4464 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4465 dpll |= 3;
4466 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
4467 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4468 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4469 else
4470 dpll |= PLL_REF_INPUT_DREFCLK;
4471
4472 dpll |= DPLL_VCO_ENABLE;
4473 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4474 POSTING_READ(DPLL(pipe));
4475 udelay(150);
4476
Daniel Vetterdafd2262012-11-26 17:22:07 +01004477 for_each_encoder_on_crtc(dev, crtc, encoder)
4478 if (encoder->pre_pll_enable)
4479 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004480
4481 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
4482 intel_dp_set_m_n(crtc, mode, adjusted_mode);
4483
4484 I915_WRITE(DPLL(pipe), dpll);
4485
4486 /* Wait for the clocks to stabilize. */
4487 POSTING_READ(DPLL(pipe));
4488 udelay(150);
4489
4490 if (INTEL_INFO(dev)->gen >= 4) {
4491 u32 temp = 0;
4492 if (is_sdvo) {
4493 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
4494 if (temp > 1)
4495 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
4496 else
4497 temp = 0;
4498 }
4499 I915_WRITE(DPLL_MD(pipe), temp);
4500 } else {
4501 /* The pixel multiplier can only be updated once the
4502 * DPLL is enabled and the clocks are stable.
4503 *
4504 * So write it again.
4505 */
4506 I915_WRITE(DPLL(pipe), dpll);
4507 }
4508}
4509
4510static void i8xx_update_pll(struct drm_crtc *crtc,
4511 struct drm_display_mode *adjusted_mode,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304512 intel_clock_t *clock, intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004513 int num_connectors)
4514{
4515 struct drm_device *dev = crtc->dev;
4516 struct drm_i915_private *dev_priv = dev->dev_private;
4517 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterdafd2262012-11-26 17:22:07 +01004518 struct intel_encoder *encoder;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004519 int pipe = intel_crtc->pipe;
4520 u32 dpll;
4521
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304522 i9xx_update_pll_dividers(crtc, clock, reduced_clock);
4523
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004524 dpll = DPLL_VGA_MODE_DIS;
4525
4526 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
4527 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4528 } else {
4529 if (clock->p1 == 2)
4530 dpll |= PLL_P1_DIVIDE_BY_TWO;
4531 else
4532 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4533 if (clock->p2 == 4)
4534 dpll |= PLL_P2_DIVIDE_BY_4;
4535 }
4536
Daniel Vetter83f377a2013-02-22 00:53:05 +01004537 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004538 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
4539 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4540 else
4541 dpll |= PLL_REF_INPUT_DREFCLK;
4542
4543 dpll |= DPLL_VCO_ENABLE;
4544 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
4545 POSTING_READ(DPLL(pipe));
4546 udelay(150);
4547
Daniel Vetterdafd2262012-11-26 17:22:07 +01004548 for_each_encoder_on_crtc(dev, crtc, encoder)
4549 if (encoder->pre_pll_enable)
4550 encoder->pre_pll_enable(encoder);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004551
Daniel Vetter5b5896e2012-09-11 12:37:55 +02004552 I915_WRITE(DPLL(pipe), dpll);
4553
4554 /* Wait for the clocks to stabilize. */
4555 POSTING_READ(DPLL(pipe));
4556 udelay(150);
4557
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004558 /* The pixel multiplier can only be updated once the
4559 * DPLL is enabled and the clocks are stable.
4560 *
4561 * So write it again.
4562 */
4563 I915_WRITE(DPLL(pipe), dpll);
4564}
4565
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004566static void intel_set_pipe_timings(struct intel_crtc *intel_crtc,
4567 struct drm_display_mode *mode,
4568 struct drm_display_mode *adjusted_mode)
4569{
4570 struct drm_device *dev = intel_crtc->base.dev;
4571 struct drm_i915_private *dev_priv = dev->dev_private;
4572 enum pipe pipe = intel_crtc->pipe;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004573 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004574 uint32_t vsyncshift;
4575
4576 if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
4577 /* the chip adds 2 halflines automatically */
4578 adjusted_mode->crtc_vtotal -= 1;
4579 adjusted_mode->crtc_vblank_end -= 1;
4580 vsyncshift = adjusted_mode->crtc_hsync_start
4581 - adjusted_mode->crtc_htotal / 2;
4582 } else {
4583 vsyncshift = 0;
4584 }
4585
4586 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004587 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004588
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004589 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004590 (adjusted_mode->crtc_hdisplay - 1) |
4591 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004592 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004593 (adjusted_mode->crtc_hblank_start - 1) |
4594 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004595 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004596 (adjusted_mode->crtc_hsync_start - 1) |
4597 ((adjusted_mode->crtc_hsync_end - 1) << 16));
4598
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004599 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004600 (adjusted_mode->crtc_vdisplay - 1) |
4601 ((adjusted_mode->crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004602 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004603 (adjusted_mode->crtc_vblank_start - 1) |
4604 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02004605 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004606 (adjusted_mode->crtc_vsync_start - 1) |
4607 ((adjusted_mode->crtc_vsync_end - 1) << 16));
4608
Paulo Zanonib5e508d2012-10-24 11:34:43 -02004609 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
4610 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
4611 * documented on the DDI_FUNC_CTL register description, EDP Input Select
4612 * bits. */
4613 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
4614 (pipe == PIPE_B || pipe == PIPE_C))
4615 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
4616
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004617 /* pipesrc controls the size that is scaled from, which should
4618 * always be the user's requested size.
4619 */
4620 I915_WRITE(PIPESRC(pipe),
4621 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4622}
4623
Eric Anholtf564048e2011-03-30 13:01:02 -07004624static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4625 struct drm_display_mode *mode,
4626 struct drm_display_mode *adjusted_mode,
4627 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02004628 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004629{
4630 struct drm_device *dev = crtc->dev;
4631 struct drm_i915_private *dev_priv = dev->dev_private;
4632 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4633 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004634 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004635 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004636 intel_clock_t clock, reduced_clock;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004637 u32 dspcntr, pipeconf;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004638 bool ok, has_reduced_clock = false, is_sdvo = false;
4639 bool is_lvds = false, is_tv = false, is_dp = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01004640 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004641 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004642 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004643
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02004644 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01004645 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004646 case INTEL_OUTPUT_LVDS:
4647 is_lvds = true;
4648 break;
4649 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08004650 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08004651 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01004652 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08004653 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004654 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004655 case INTEL_OUTPUT_TVOUT:
4656 is_tv = true;
4657 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004658 case INTEL_OUTPUT_DISPLAYPORT:
4659 is_dp = true;
4660 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004661 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004662
Eric Anholtc751ce42010-03-25 11:48:48 -07004663 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08004664 }
4665
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004666 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08004667
Ma Lingd4906092009-03-18 20:13:27 +08004668 /*
4669 * Returns a set of divisors for the desired target clock with the given
4670 * refclk, or FALSE. The returned values represent the clock equation:
4671 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4672 */
Chris Wilson1b894b52010-12-14 20:04:54 +00004673 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08004674 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4675 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004676 if (!ok) {
4677 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07004678 return -EINVAL;
4679 }
4680
4681 /* Ensure that the cursor is valid for the new mode before changing... */
4682 intel_crtc_update_cursor(crtc, true);
4683
4684 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004685 /*
4686 * Ensure we match the reduced clock's P to the target clock.
4687 * If the clocks don't match, we can't switch the display clock
4688 * by using the FP0/FP1. In such case we will disable the LVDS
4689 * downclock feature.
4690 */
Eric Anholtf564048e2011-03-30 13:01:02 -07004691 has_reduced_clock = limit->find_pll(limit, crtc,
4692 dev_priv->lvds_downclock,
4693 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08004694 &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07004695 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004696 }
4697
Jesse Barnesc65d77d2011-12-15 12:30:36 -08004698 if (is_sdvo && is_tv)
4699 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07004700
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004701 if (IS_GEN2(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304702 i8xx_update_pll(crtc, adjusted_mode, &clock,
4703 has_reduced_clock ? &reduced_clock : NULL,
4704 num_connectors);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07004705 else if (IS_VALLEYVIEW(dev))
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05304706 vlv_update_pll(crtc, mode, adjusted_mode, &clock,
4707 has_reduced_clock ? &reduced_clock : NULL,
4708 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004709 else
Daniel Vettereb1cbe42012-03-28 23:12:16 +02004710 i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
4711 has_reduced_clock ? &reduced_clock : NULL,
4712 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07004713
4714 /* setup pipeconf */
4715 pipeconf = I915_READ(PIPECONF(pipe));
4716
4717 /* Set up the display plane register */
4718 dspcntr = DISPPLANE_GAMMA_ENABLE;
4719
Eric Anholt929c77f2011-03-30 13:01:04 -07004720 if (pipe == 0)
4721 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4722 else
4723 dspcntr |= DISPPLANE_SEL_PIPE_B;
Eric Anholtf564048e2011-03-30 13:01:02 -07004724
4725 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
4726 /* Enable pixel doubling when the dot clock is > 90% of the (display)
4727 * core speed.
4728 *
4729 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
4730 * pipe == 0 check?
4731 */
4732 if (mode->clock >
4733 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
4734 pipeconf |= PIPECONF_DOUBLE_WIDE;
4735 else
4736 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
4737 }
4738
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004739 /* default to 8bpc */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004740 pipeconf &= ~(PIPECONF_BPC_MASK | PIPECONF_DITHER_EN);
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004741 if (is_dp) {
Jani Nikula0c96c652012-09-26 18:43:10 +03004742 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004743 pipeconf |= PIPECONF_6BPC |
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004744 PIPECONF_DITHER_EN |
4745 PIPECONF_DITHER_TYPE_SP;
4746 }
4747 }
4748
Gajanan Bhat19c03922012-09-27 19:13:07 +05304749 if (IS_VALLEYVIEW(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
4750 if (adjusted_mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004751 pipeconf |= PIPECONF_6BPC |
Gajanan Bhat19c03922012-09-27 19:13:07 +05304752 PIPECONF_ENABLE |
4753 I965_PIPECONF_ACTIVE;
4754 }
4755 }
4756
Eric Anholtf564048e2011-03-30 13:01:02 -07004757 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
4758 drm_mode_debug_printmodeline(mode);
4759
Jesse Barnesa7516a02011-12-15 12:30:37 -08004760 if (HAS_PIPE_CXSR(dev)) {
4761 if (intel_crtc->lowfreq_avail) {
Eric Anholtf564048e2011-03-30 13:01:02 -07004762 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4763 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
Jesse Barnesa7516a02011-12-15 12:30:37 -08004764 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07004765 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4766 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4767 }
4768 }
4769
Keith Packard617cf882012-02-08 13:53:38 -08004770 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Daniel Vetterdbb02572012-01-28 14:49:23 +01004771 if (!IS_GEN2(dev) &&
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004772 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
Eric Anholtf564048e2011-03-30 13:01:02 -07004773 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004774 else
Keith Packard617cf882012-02-08 13:53:38 -08004775 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01004776
Paulo Zanonib0e77b92012-10-01 18:10:53 -03004777 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Eric Anholtf564048e2011-03-30 13:01:02 -07004778
4779 /* pipesrc and dspsize control the size that is scaled from,
4780 * which should always be the user's requested size.
4781 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004782 I915_WRITE(DSPSIZE(plane),
4783 ((mode->vdisplay - 1) << 16) |
4784 (mode->hdisplay - 1));
4785 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004786
Eric Anholtf564048e2011-03-30 13:01:02 -07004787 I915_WRITE(PIPECONF(pipe), pipeconf);
4788 POSTING_READ(PIPECONF(pipe));
Eric Anholt929c77f2011-03-30 13:01:04 -07004789 intel_enable_pipe(dev_priv, pipe, false);
Eric Anholtf564048e2011-03-30 13:01:02 -07004790
4791 intel_wait_for_vblank(dev, pipe);
4792
Eric Anholtf564048e2011-03-30 13:01:02 -07004793 I915_WRITE(DSPCNTR(plane), dspcntr);
4794 POSTING_READ(DSPCNTR(plane));
4795
Daniel Vetter94352cf2012-07-05 22:51:56 +02004796 ret = intel_pipe_set_base(crtc, x, y, fb);
Eric Anholtf564048e2011-03-30 13:01:02 -07004797
4798 intel_update_watermarks(dev);
4799
Eric Anholtf564048e2011-03-30 13:01:02 -07004800 return ret;
4801}
4802
Paulo Zanonidde86e22012-12-01 12:04:25 -02004803static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07004804{
4805 struct drm_i915_private *dev_priv = dev->dev_private;
4806 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004807 struct intel_encoder *encoder;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004808 u32 temp;
4809 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07004810 bool has_cpu_edp = false;
4811 bool has_pch_edp = false;
4812 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07004813 bool has_ck505 = false;
4814 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004815
4816 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07004817 list_for_each_entry(encoder, &mode_config->encoder_list,
4818 base.head) {
4819 switch (encoder->type) {
4820 case INTEL_OUTPUT_LVDS:
4821 has_panel = true;
4822 has_lvds = true;
4823 break;
4824 case INTEL_OUTPUT_EDP:
4825 has_panel = true;
4826 if (intel_encoder_is_pch_edp(&encoder->base))
4827 has_pch_edp = true;
4828 else
4829 has_cpu_edp = true;
4830 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004831 }
4832 }
4833
Keith Packard99eb6a02011-09-26 14:29:12 -07004834 if (HAS_PCH_IBX(dev)) {
4835 has_ck505 = dev_priv->display_clock_mode;
4836 can_ssc = has_ck505;
4837 } else {
4838 has_ck505 = false;
4839 can_ssc = true;
4840 }
4841
4842 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4843 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4844 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004845
4846 /* Ironlake: try to setup display ref clock before DPLL
4847 * enabling. This is only under driver's control after
4848 * PCH B stepping, previous chipset stepping should be
4849 * ignoring this setting.
4850 */
4851 temp = I915_READ(PCH_DREF_CONTROL);
4852 /* Always enable nonspread source */
4853 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004854
Keith Packard99eb6a02011-09-26 14:29:12 -07004855 if (has_ck505)
4856 temp |= DREF_NONSPREAD_CK505_ENABLE;
4857 else
4858 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004859
Keith Packard199e5d72011-09-22 12:01:57 -07004860 if (has_panel) {
4861 temp &= ~DREF_SSC_SOURCE_MASK;
4862 temp |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004863
Keith Packard199e5d72011-09-22 12:01:57 -07004864 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07004865 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004866 DRM_DEBUG_KMS("Using SSC on panel\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004867 temp |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02004868 } else
4869 temp &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004870
4871 /* Get SSC going before enabling the outputs */
4872 I915_WRITE(PCH_DREF_CONTROL, temp);
4873 POSTING_READ(PCH_DREF_CONTROL);
4874 udelay(200);
4875
Jesse Barnes13d83a62011-08-03 12:59:20 -07004876 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4877
4878 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07004879 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07004880 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004881 DRM_DEBUG_KMS("Using SSC on eDP\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004882 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004883 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07004884 else
4885 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004886 } else
4887 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4888
4889 I915_WRITE(PCH_DREF_CONTROL, temp);
4890 POSTING_READ(PCH_DREF_CONTROL);
4891 udelay(200);
4892 } else {
4893 DRM_DEBUG_KMS("Disabling SSC entirely\n");
4894
4895 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4896
4897 /* Turn off CPU output */
4898 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4899
4900 I915_WRITE(PCH_DREF_CONTROL, temp);
4901 POSTING_READ(PCH_DREF_CONTROL);
4902 udelay(200);
4903
4904 /* Turn off the SSC source */
4905 temp &= ~DREF_SSC_SOURCE_MASK;
4906 temp |= DREF_SSC_SOURCE_DISABLE;
4907
4908 /* Turn off SSC1 */
4909 temp &= ~ DREF_SSC1_ENABLE;
4910
Jesse Barnes13d83a62011-08-03 12:59:20 -07004911 I915_WRITE(PCH_DREF_CONTROL, temp);
4912 POSTING_READ(PCH_DREF_CONTROL);
4913 udelay(200);
4914 }
4915}
4916
Paulo Zanonidde86e22012-12-01 12:04:25 -02004917/* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
4918static void lpt_init_pch_refclk(struct drm_device *dev)
4919{
4920 struct drm_i915_private *dev_priv = dev->dev_private;
4921 struct drm_mode_config *mode_config = &dev->mode_config;
4922 struct intel_encoder *encoder;
4923 bool has_vga = false;
4924 bool is_sdv = false;
4925 u32 tmp;
4926
4927 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
4928 switch (encoder->type) {
4929 case INTEL_OUTPUT_ANALOG:
4930 has_vga = true;
4931 break;
4932 }
4933 }
4934
4935 if (!has_vga)
4936 return;
4937
Daniel Vetterc00db242013-01-22 15:33:27 +01004938 mutex_lock(&dev_priv->dpio_lock);
4939
Paulo Zanonidde86e22012-12-01 12:04:25 -02004940 /* XXX: Rip out SDV support once Haswell ships for real. */
4941 if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
4942 is_sdv = true;
4943
4944 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
4945 tmp &= ~SBI_SSCCTL_DISABLE;
4946 tmp |= SBI_SSCCTL_PATHALT;
4947 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4948
4949 udelay(24);
4950
4951 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
4952 tmp &= ~SBI_SSCCTL_PATHALT;
4953 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4954
4955 if (!is_sdv) {
4956 tmp = I915_READ(SOUTH_CHICKEN2);
4957 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
4958 I915_WRITE(SOUTH_CHICKEN2, tmp);
4959
4960 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
4961 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
4962 DRM_ERROR("FDI mPHY reset assert timeout\n");
4963
4964 tmp = I915_READ(SOUTH_CHICKEN2);
4965 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
4966 I915_WRITE(SOUTH_CHICKEN2, tmp);
4967
4968 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
4969 FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
4970 100))
4971 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
4972 }
4973
4974 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
4975 tmp &= ~(0xFF << 24);
4976 tmp |= (0x12 << 24);
4977 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
4978
4979 if (!is_sdv) {
4980 tmp = intel_sbi_read(dev_priv, 0x808C, SBI_MPHY);
4981 tmp &= ~(0x3 << 6);
4982 tmp |= (1 << 6) | (1 << 0);
4983 intel_sbi_write(dev_priv, 0x808C, tmp, SBI_MPHY);
4984 }
4985
4986 if (is_sdv) {
4987 tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
4988 tmp |= 0x7FFF;
4989 intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
4990 }
4991
4992 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
4993 tmp |= (1 << 11);
4994 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
4995
4996 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
4997 tmp |= (1 << 11);
4998 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
4999
5000 if (is_sdv) {
5001 tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
5002 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5003 intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
5004
5005 tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
5006 tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
5007 intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
5008
5009 tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
5010 tmp |= (0x3F << 8);
5011 intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
5012
5013 tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
5014 tmp |= (0x3F << 8);
5015 intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
5016 }
5017
5018 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5019 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5020 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5021
5022 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5023 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5024 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5025
5026 if (!is_sdv) {
5027 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5028 tmp &= ~(7 << 13);
5029 tmp |= (5 << 13);
5030 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5031
5032 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5033 tmp &= ~(7 << 13);
5034 tmp |= (5 << 13);
5035 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5036 }
5037
5038 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5039 tmp &= ~0xFF;
5040 tmp |= 0x1C;
5041 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5042
5043 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5044 tmp &= ~0xFF;
5045 tmp |= 0x1C;
5046 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5047
5048 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5049 tmp &= ~(0xFF << 16);
5050 tmp |= (0x1C << 16);
5051 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5052
5053 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5054 tmp &= ~(0xFF << 16);
5055 tmp |= (0x1C << 16);
5056 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5057
5058 if (!is_sdv) {
5059 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5060 tmp |= (1 << 27);
5061 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5062
5063 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5064 tmp |= (1 << 27);
5065 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5066
5067 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5068 tmp &= ~(0xF << 28);
5069 tmp |= (4 << 28);
5070 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5071
5072 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5073 tmp &= ~(0xF << 28);
5074 tmp |= (4 << 28);
5075 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5076 }
5077
5078 /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
5079 tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
5080 tmp |= SBI_DBUFF0_ENABLE;
5081 intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01005082
5083 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02005084}
5085
5086/*
5087 * Initialize reference clocks when the driver loads
5088 */
5089void intel_init_pch_refclk(struct drm_device *dev)
5090{
5091 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5092 ironlake_init_pch_refclk(dev);
5093 else if (HAS_PCH_LPT(dev))
5094 lpt_init_pch_refclk(dev);
5095}
5096
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005097static int ironlake_get_refclk(struct drm_crtc *crtc)
5098{
5099 struct drm_device *dev = crtc->dev;
5100 struct drm_i915_private *dev_priv = dev->dev_private;
5101 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005102 struct intel_encoder *edp_encoder = NULL;
5103 int num_connectors = 0;
5104 bool is_lvds = false;
5105
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02005106 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005107 switch (encoder->type) {
5108 case INTEL_OUTPUT_LVDS:
5109 is_lvds = true;
5110 break;
5111 case INTEL_OUTPUT_EDP:
5112 edp_encoder = encoder;
5113 break;
5114 }
5115 num_connectors++;
5116 }
5117
5118 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5119 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5120 dev_priv->lvds_ssc_freq);
5121 return dev_priv->lvds_ssc_freq * 1000;
5122 }
5123
5124 return 120000;
5125}
5126
Paulo Zanonic8203562012-09-12 10:06:29 -03005127static void ironlake_set_pipeconf(struct drm_crtc *crtc,
5128 struct drm_display_mode *adjusted_mode,
5129 bool dither)
5130{
5131 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5132 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5133 int pipe = intel_crtc->pipe;
5134 uint32_t val;
5135
5136 val = I915_READ(PIPECONF(pipe));
5137
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005138 val &= ~PIPECONF_BPC_MASK;
Paulo Zanonic8203562012-09-12 10:06:29 -03005139 switch (intel_crtc->bpp) {
5140 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005141 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005142 break;
5143 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005144 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005145 break;
5146 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005147 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005148 break;
5149 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01005150 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03005151 break;
5152 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03005153 /* Case prevented by intel_choose_pipe_bpp_dither. */
5154 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03005155 }
5156
5157 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5158 if (dither)
5159 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5160
5161 val &= ~PIPECONF_INTERLACE_MASK;
5162 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5163 val |= PIPECONF_INTERLACED_ILK;
5164 else
5165 val |= PIPECONF_PROGRESSIVE;
5166
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02005167 if (adjusted_mode->private_flags & INTEL_MODE_LIMITED_COLOR_RANGE)
5168 val |= PIPECONF_COLOR_RANGE_SELECT;
5169 else
5170 val &= ~PIPECONF_COLOR_RANGE_SELECT;
5171
Paulo Zanonic8203562012-09-12 10:06:29 -03005172 I915_WRITE(PIPECONF(pipe), val);
5173 POSTING_READ(PIPECONF(pipe));
5174}
5175
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005176/*
5177 * Set up the pipe CSC unit.
5178 *
5179 * Currently only full range RGB to limited range RGB conversion
5180 * is supported, but eventually this should handle various
5181 * RGB<->YCbCr scenarios as well.
5182 */
5183static void intel_set_pipe_csc(struct drm_crtc *crtc,
5184 const struct drm_display_mode *adjusted_mode)
5185{
5186 struct drm_device *dev = crtc->dev;
5187 struct drm_i915_private *dev_priv = dev->dev_private;
5188 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5189 int pipe = intel_crtc->pipe;
5190 uint16_t coeff = 0x7800; /* 1.0 */
5191
5192 /*
5193 * TODO: Check what kind of values actually come out of the pipe
5194 * with these coeff/postoff values and adjust to get the best
5195 * accuracy. Perhaps we even need to take the bpc value into
5196 * consideration.
5197 */
5198
5199 if (adjusted_mode->private_flags & INTEL_MODE_LIMITED_COLOR_RANGE)
5200 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
5201
5202 /*
5203 * GY/GU and RY/RU should be the other way around according
5204 * to BSpec, but reality doesn't agree. Just set them up in
5205 * a way that results in the correct picture.
5206 */
5207 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
5208 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
5209
5210 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
5211 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
5212
5213 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
5214 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
5215
5216 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
5217 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
5218 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
5219
5220 if (INTEL_INFO(dev)->gen > 6) {
5221 uint16_t postoff = 0;
5222
5223 if (adjusted_mode->private_flags & INTEL_MODE_LIMITED_COLOR_RANGE)
5224 postoff = (16 * (1 << 13) / 255) & 0x1fff;
5225
5226 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
5227 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
5228 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
5229
5230 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
5231 } else {
5232 uint32_t mode = CSC_MODE_YUV_TO_RGB;
5233
5234 if (adjusted_mode->private_flags & INTEL_MODE_LIMITED_COLOR_RANGE)
5235 mode |= CSC_BLACK_SCREEN_OFFSET;
5236
5237 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
5238 }
5239}
5240
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005241static void haswell_set_pipeconf(struct drm_crtc *crtc,
5242 struct drm_display_mode *adjusted_mode,
5243 bool dither)
5244{
5245 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5246 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005247 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005248 uint32_t val;
5249
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005250 val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005251
5252 val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
5253 if (dither)
5254 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
5255
5256 val &= ~PIPECONF_INTERLACE_MASK_HSW;
5257 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
5258 val |= PIPECONF_INTERLACED_ILK;
5259 else
5260 val |= PIPECONF_PROGRESSIVE;
5261
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005262 I915_WRITE(PIPECONF(cpu_transcoder), val);
5263 POSTING_READ(PIPECONF(cpu_transcoder));
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005264}
5265
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005266static bool ironlake_compute_clocks(struct drm_crtc *crtc,
5267 struct drm_display_mode *adjusted_mode,
5268 intel_clock_t *clock,
5269 bool *has_reduced_clock,
5270 intel_clock_t *reduced_clock)
5271{
5272 struct drm_device *dev = crtc->dev;
5273 struct drm_i915_private *dev_priv = dev->dev_private;
5274 struct intel_encoder *intel_encoder;
5275 int refclk;
5276 const intel_limit_t *limit;
5277 bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
5278
5279 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5280 switch (intel_encoder->type) {
5281 case INTEL_OUTPUT_LVDS:
5282 is_lvds = true;
5283 break;
5284 case INTEL_OUTPUT_SDVO:
5285 case INTEL_OUTPUT_HDMI:
5286 is_sdvo = true;
5287 if (intel_encoder->needs_tv_clock)
5288 is_tv = true;
5289 break;
5290 case INTEL_OUTPUT_TVOUT:
5291 is_tv = true;
5292 break;
5293 }
5294 }
5295
5296 refclk = ironlake_get_refclk(crtc);
5297
5298 /*
5299 * Returns a set of divisors for the desired target clock with the given
5300 * refclk, or FALSE. The returned values represent the clock equation:
5301 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5302 */
5303 limit = intel_limit(crtc, refclk);
5304 ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5305 clock);
5306 if (!ret)
5307 return false;
5308
5309 if (is_lvds && dev_priv->lvds_downclock_avail) {
5310 /*
5311 * Ensure we match the reduced clock's P to the target clock.
5312 * If the clocks don't match, we can't switch the display clock
5313 * by using the FP0/FP1. In such case we will disable the LVDS
5314 * downclock feature.
5315 */
5316 *has_reduced_clock = limit->find_pll(limit, crtc,
5317 dev_priv->lvds_downclock,
5318 refclk,
5319 clock,
5320 reduced_clock);
5321 }
5322
5323 if (is_sdvo && is_tv)
5324 i9xx_adjust_sdvo_tv_clock(adjusted_mode, clock);
5325
5326 return true;
5327}
5328
Daniel Vetter01a415f2012-10-27 15:58:40 +02005329static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
5330{
5331 struct drm_i915_private *dev_priv = dev->dev_private;
5332 uint32_t temp;
5333
5334 temp = I915_READ(SOUTH_CHICKEN1);
5335 if (temp & FDI_BC_BIFURCATION_SELECT)
5336 return;
5337
5338 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
5339 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
5340
5341 temp |= FDI_BC_BIFURCATION_SELECT;
5342 DRM_DEBUG_KMS("enabling fdi C rx\n");
5343 I915_WRITE(SOUTH_CHICKEN1, temp);
5344 POSTING_READ(SOUTH_CHICKEN1);
5345}
5346
5347static bool ironlake_check_fdi_lanes(struct intel_crtc *intel_crtc)
5348{
5349 struct drm_device *dev = intel_crtc->base.dev;
5350 struct drm_i915_private *dev_priv = dev->dev_private;
5351 struct intel_crtc *pipe_B_crtc =
5352 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5353
5354 DRM_DEBUG_KMS("checking fdi config on pipe %i, lanes %i\n",
5355 intel_crtc->pipe, intel_crtc->fdi_lanes);
5356 if (intel_crtc->fdi_lanes > 4) {
5357 DRM_DEBUG_KMS("invalid fdi lane config on pipe %i: %i lanes\n",
5358 intel_crtc->pipe, intel_crtc->fdi_lanes);
5359 /* Clamp lanes to avoid programming the hw with bogus values. */
5360 intel_crtc->fdi_lanes = 4;
5361
5362 return false;
5363 }
5364
5365 if (dev_priv->num_pipe == 2)
5366 return true;
5367
5368 switch (intel_crtc->pipe) {
5369 case PIPE_A:
5370 return true;
5371 case PIPE_B:
5372 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5373 intel_crtc->fdi_lanes > 2) {
5374 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5375 intel_crtc->pipe, intel_crtc->fdi_lanes);
5376 /* Clamp lanes to avoid programming the hw with bogus values. */
5377 intel_crtc->fdi_lanes = 2;
5378
5379 return false;
5380 }
5381
5382 if (intel_crtc->fdi_lanes > 2)
5383 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
5384 else
5385 cpt_enable_fdi_bc_bifurcation(dev);
5386
5387 return true;
5388 case PIPE_C:
5389 if (!pipe_B_crtc->base.enabled || pipe_B_crtc->fdi_lanes <= 2) {
5390 if (intel_crtc->fdi_lanes > 2) {
5391 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
5392 intel_crtc->pipe, intel_crtc->fdi_lanes);
5393 /* Clamp lanes to avoid programming the hw with bogus values. */
5394 intel_crtc->fdi_lanes = 2;
5395
5396 return false;
5397 }
5398 } else {
5399 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5400 return false;
5401 }
5402
5403 cpt_enable_fdi_bc_bifurcation(dev);
5404
5405 return true;
5406 default:
5407 BUG();
5408 }
5409}
5410
Paulo Zanonid4b19312012-11-29 11:29:32 -02005411int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
5412{
5413 /*
5414 * Account for spread spectrum to avoid
5415 * oversubscribing the link. Max center spread
5416 * is 2.5%; use 5% for safety's sake.
5417 */
5418 u32 bps = target_clock * bpp * 21 / 20;
5419 return bps / (link_bw * 8) + 1;
5420}
5421
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005422static void ironlake_set_m_n(struct drm_crtc *crtc,
5423 struct drm_display_mode *mode,
5424 struct drm_display_mode *adjusted_mode)
Jesse Barnes79e53942008-11-07 14:24:08 -08005425{
5426 struct drm_device *dev = crtc->dev;
5427 struct drm_i915_private *dev_priv = dev->dev_private;
5428 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005429 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005430 struct intel_encoder *intel_encoder, *edp_encoder = NULL;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005431 struct intel_link_m_n m_n = {0};
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005432 int target_clock, pixel_multiplier, lane, link_bw;
5433 bool is_dp = false, is_cpu_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005434
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005435 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5436 switch (intel_encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005437 case INTEL_OUTPUT_DISPLAYPORT:
5438 is_dp = true;
5439 break;
5440 case INTEL_OUTPUT_EDP:
Jesse Barnese3aef172012-04-10 11:58:03 -07005441 is_dp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005442 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
Jesse Barnese3aef172012-04-10 11:58:03 -07005443 is_cpu_edp = true;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005444 edp_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005445 break;
5446 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005447 }
5448
Zhenyu Wang2c072452009-06-05 15:38:42 +08005449 /* FDI link */
Eric Anholt8febb292011-03-30 13:01:07 -07005450 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5451 lane = 0;
5452 /* CPU eDP doesn't require FDI link, so just set DP M/N
5453 according to current link config */
Jesse Barnese3aef172012-04-10 11:58:03 -07005454 if (is_cpu_edp) {
Jesse Barnese3aef172012-04-10 11:58:03 -07005455 intel_edp_link_config(edp_encoder, &lane, &link_bw);
Eric Anholt8febb292011-03-30 13:01:07 -07005456 } else {
Eric Anholt8febb292011-03-30 13:01:07 -07005457 /* FDI is a binary signal running at ~2.7GHz, encoding
5458 * each output octet as 10 bits. The actual frequency
5459 * is stored as a divider into a 100MHz clock, and the
5460 * mode pixel clock is stored in units of 1KHz.
5461 * Hence the bw of each lane in terms of the mode signal
5462 * is:
5463 */
5464 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005465 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08005466
Daniel Vetter94bf2ce2012-06-04 18:39:19 +02005467 /* [e]DP over FDI requires target mode clock instead of link clock. */
5468 if (edp_encoder)
5469 target_clock = intel_edp_target_clock(edp_encoder, mode);
5470 else if (is_dp)
5471 target_clock = mode->clock;
5472 else
5473 target_clock = adjusted_mode->clock;
5474
Paulo Zanonid4b19312012-11-29 11:29:32 -02005475 if (!lane)
5476 lane = ironlake_get_lanes_required(target_clock, link_bw,
5477 intel_crtc->bpp);
Eric Anholt8febb292011-03-30 13:01:07 -07005478
5479 intel_crtc->fdi_lanes = lane;
5480
5481 if (pixel_multiplier > 1)
5482 link_bw *= pixel_multiplier;
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005483 intel_link_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw, &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07005484
Paulo Zanoniafe2fcf2012-10-23 18:30:01 -02005485 I915_WRITE(PIPE_DATA_M1(cpu_transcoder), TU_SIZE(m_n.tu) | m_n.gmch_m);
5486 I915_WRITE(PIPE_DATA_N1(cpu_transcoder), m_n.gmch_n);
5487 I915_WRITE(PIPE_LINK_M1(cpu_transcoder), m_n.link_m);
5488 I915_WRITE(PIPE_LINK_N1(cpu_transcoder), m_n.link_n);
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005489}
5490
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005491static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
5492 struct drm_display_mode *adjusted_mode,
5493 intel_clock_t *clock, u32 fp)
5494{
5495 struct drm_crtc *crtc = &intel_crtc->base;
5496 struct drm_device *dev = crtc->dev;
5497 struct drm_i915_private *dev_priv = dev->dev_private;
5498 struct intel_encoder *intel_encoder;
5499 uint32_t dpll;
5500 int factor, pixel_multiplier, num_connectors = 0;
5501 bool is_lvds = false, is_sdvo = false, is_tv = false;
5502 bool is_dp = false, is_cpu_edp = false;
5503
5504 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
5505 switch (intel_encoder->type) {
5506 case INTEL_OUTPUT_LVDS:
5507 is_lvds = true;
5508 break;
5509 case INTEL_OUTPUT_SDVO:
5510 case INTEL_OUTPUT_HDMI:
5511 is_sdvo = true;
5512 if (intel_encoder->needs_tv_clock)
5513 is_tv = true;
5514 break;
5515 case INTEL_OUTPUT_TVOUT:
5516 is_tv = true;
5517 break;
5518 case INTEL_OUTPUT_DISPLAYPORT:
5519 is_dp = true;
5520 break;
5521 case INTEL_OUTPUT_EDP:
5522 is_dp = true;
5523 if (!intel_encoder_is_pch_edp(&intel_encoder->base))
5524 is_cpu_edp = true;
5525 break;
5526 }
5527
5528 num_connectors++;
5529 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005530
Chris Wilsonc1858122010-12-03 21:35:48 +00005531 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005532 factor = 21;
5533 if (is_lvds) {
5534 if ((intel_panel_use_ssc(dev_priv) &&
5535 dev_priv->lvds_ssc_freq == 100) ||
Daniel Vetter1974cad2012-11-26 17:22:09 +01005536 intel_is_dual_link_lvds(dev))
Eric Anholt8febb292011-03-30 13:01:07 -07005537 factor = 25;
5538 } else if (is_sdvo && is_tv)
5539 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005540
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005541 if (clock->m < factor * clock->n)
Eric Anholt8febb292011-03-30 13:01:07 -07005542 fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005543
Chris Wilson5eddb702010-09-11 13:48:45 +01005544 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005545
Eric Anholta07d6782011-03-30 13:01:08 -07005546 if (is_lvds)
5547 dpll |= DPLLB_MODE_LVDS;
5548 else
5549 dpll |= DPLLB_MODE_DAC_SERIAL;
5550 if (is_sdvo) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005551 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
Eric Anholta07d6782011-03-30 13:01:08 -07005552 if (pixel_multiplier > 1) {
5553 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08005554 }
Eric Anholta07d6782011-03-30 13:01:08 -07005555 dpll |= DPLL_DVO_HIGH_SPEED;
5556 }
Jesse Barnese3aef172012-04-10 11:58:03 -07005557 if (is_dp && !is_cpu_edp)
Eric Anholta07d6782011-03-30 13:01:08 -07005558 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005559
Eric Anholta07d6782011-03-30 13:01:08 -07005560 /* compute bitmask from p1 value */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005561 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005562 /* also FPA1 */
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005563 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07005564
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005565 switch (clock->p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07005566 case 5:
5567 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5568 break;
5569 case 7:
5570 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5571 break;
5572 case 10:
5573 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5574 break;
5575 case 14:
5576 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5577 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005578 }
5579
5580 if (is_sdvo && is_tv)
5581 dpll |= PLL_REF_INPUT_TVCLKINBC;
5582 else if (is_tv)
5583 /* XXX: just matching BIOS for now */
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005584 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
Jesse Barnes79e53942008-11-07 14:24:08 -08005585 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00005586 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005587 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08005588 else
5589 dpll |= PLL_REF_INPUT_DREFCLK;
5590
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005591 return dpll;
5592}
5593
Jesse Barnes79e53942008-11-07 14:24:08 -08005594static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5595 struct drm_display_mode *mode,
5596 struct drm_display_mode *adjusted_mode,
5597 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005598 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005599{
5600 struct drm_device *dev = crtc->dev;
5601 struct drm_i915_private *dev_priv = dev->dev_private;
5602 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5603 int pipe = intel_crtc->pipe;
5604 int plane = intel_crtc->plane;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005605 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005606 intel_clock_t clock, reduced_clock;
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005607 u32 dpll, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005608 bool ok, has_reduced_clock = false;
5609 bool is_lvds = false, is_dp = false, is_cpu_edp = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005610 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005611 int ret;
Daniel Vetter01a415f2012-10-27 15:58:40 +02005612 bool dither, fdi_config_ok;
Jesse Barnes79e53942008-11-07 14:24:08 -08005613
5614 for_each_encoder_on_crtc(dev, crtc, encoder) {
5615 switch (encoder->type) {
5616 case INTEL_OUTPUT_LVDS:
5617 is_lvds = true;
5618 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005619 case INTEL_OUTPUT_DISPLAYPORT:
5620 is_dp = true;
5621 break;
5622 case INTEL_OUTPUT_EDP:
5623 is_dp = true;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03005624 if (!intel_encoder_is_pch_edp(&encoder->base))
Jesse Barnes79e53942008-11-07 14:24:08 -08005625 is_cpu_edp = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005626 break;
5627 }
5628
5629 num_connectors++;
5630 }
5631
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005632 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
5633 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
5634
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03005635 ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
5636 &has_reduced_clock, &reduced_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005637 if (!ok) {
5638 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5639 return -EINVAL;
5640 }
5641
5642 /* Ensure that the cursor is valid for the new mode before changing... */
5643 intel_crtc_update_cursor(crtc, true);
5644
Jesse Barnes79e53942008-11-07 14:24:08 -08005645 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005646 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5647 adjusted_mode);
Paulo Zanonic8203562012-09-12 10:06:29 -03005648 if (is_lvds && dev_priv->lvds_dither)
5649 dither = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005650
Jesse Barnes79e53942008-11-07 14:24:08 -08005651 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5652 if (has_reduced_clock)
5653 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5654 reduced_clock.m2;
5655
Paulo Zanonide13a2e2012-09-20 18:36:05 -03005656 dpll = ironlake_compute_dpll(intel_crtc, adjusted_mode, &clock, fp);
Jesse Barnes79e53942008-11-07 14:24:08 -08005657
Jesse Barnesf7cb34d2011-10-12 10:49:14 -07005658 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005659 drm_mode_debug_printmodeline(mode);
5660
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005661 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
5662 if (!is_cpu_edp) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005663 struct intel_pch_pll *pll;
Chris Wilson5eddb702010-09-11 13:48:45 +01005664
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005665 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
5666 if (pll == NULL) {
5667 DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
5668 pipe);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005669 return -EINVAL;
5670 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005671 } else
5672 intel_put_pch_pll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005673
Daniel Vetter2f0c2ad2012-11-29 15:59:35 +01005674 if (is_dp && !is_cpu_edp)
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005675 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08005676
Daniel Vetterdafd2262012-11-26 17:22:07 +01005677 for_each_encoder_on_crtc(dev, crtc, encoder)
5678 if (encoder->pre_pll_enable)
5679 encoder->pre_pll_enable(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08005680
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005681 if (intel_crtc->pch_pll) {
5682 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01005683
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005684 /* Wait for the clocks to stabilize. */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005685 POSTING_READ(intel_crtc->pch_pll->pll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005686 udelay(150);
5687
Eric Anholt8febb292011-03-30 13:01:07 -07005688 /* The pixel multiplier can only be updated once the
5689 * DPLL is enabled and the clocks are stable.
5690 *
5691 * So write it again.
5692 */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005693 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08005694 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005695
Chris Wilson5eddb702010-09-11 13:48:45 +01005696 intel_crtc->lowfreq_avail = false;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005697 if (intel_crtc->pch_pll) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07005698 if (is_lvds && has_reduced_clock && i915_powersave) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005699 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
Jesse Barnes4b645f12011-10-12 09:51:31 -07005700 intel_crtc->lowfreq_avail = true;
Jesse Barnes4b645f12011-10-12 09:51:31 -07005701 } else {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005702 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
Jesse Barnes652c3932009-08-17 13:31:43 -07005703 }
5704 }
5705
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005706 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005707
Daniel Vetter01a415f2012-10-27 15:58:40 +02005708 /* Note, this also computes intel_crtc->fdi_lanes which is used below in
5709 * ironlake_check_fdi_lanes. */
Paulo Zanonif48d8f22012-09-20 18:36:04 -03005710 ironlake_set_m_n(crtc, mode, adjusted_mode);
Chris Wilson5eddb702010-09-11 13:48:45 +01005711
Daniel Vetter01a415f2012-10-27 15:58:40 +02005712 fdi_config_ok = ironlake_check_fdi_lanes(intel_crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005713
Paulo Zanonic8203562012-09-12 10:06:29 -03005714 ironlake_set_pipeconf(crtc, adjusted_mode, dither);
Jesse Barnes79e53942008-11-07 14:24:08 -08005715
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005716 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005717
Paulo Zanonia1f9e772012-09-12 10:06:32 -03005718 /* Set up the display plane register */
5719 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005720 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005721
Daniel Vetter94352cf2012-07-05 22:51:56 +02005722 ret = intel_pipe_set_base(crtc, x, y, fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005723
5724 intel_update_watermarks(dev);
5725
Eugeni Dodonov1f8eeab2012-05-09 15:37:24 -03005726 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5727
Daniel Vetter01a415f2012-10-27 15:58:40 +02005728 return fdi_config_ok ? ret : -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08005729}
5730
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02005731static void haswell_modeset_global_resources(struct drm_device *dev)
5732{
5733 struct drm_i915_private *dev_priv = dev->dev_private;
5734 bool enable = false;
5735 struct intel_crtc *crtc;
5736 struct intel_encoder *encoder;
5737
5738 list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
5739 if (crtc->pipe != PIPE_A && crtc->base.enabled)
5740 enable = true;
5741 /* XXX: Should check for edp transcoder here, but thanks to init
5742 * sequence that's not yet available. Just in case desktop eDP
5743 * on PORT D is possible on haswell, too. */
5744 }
5745
5746 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
5747 base.head) {
5748 if (encoder->type != INTEL_OUTPUT_EDP &&
5749 encoder->connectors_active)
5750 enable = true;
5751 }
5752
5753 /* Even the eDP panel fitter is outside the always-on well. */
5754 if (dev_priv->pch_pf_size)
5755 enable = true;
5756
5757 intel_set_power_well(dev, enable);
5758}
5759
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005760static int haswell_crtc_mode_set(struct drm_crtc *crtc,
5761 struct drm_display_mode *mode,
5762 struct drm_display_mode *adjusted_mode,
5763 int x, int y,
5764 struct drm_framebuffer *fb)
5765{
5766 struct drm_device *dev = crtc->dev;
5767 struct drm_i915_private *dev_priv = dev->dev_private;
5768 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5769 int pipe = intel_crtc->pipe;
5770 int plane = intel_crtc->plane;
5771 int num_connectors = 0;
Daniel Vettered7ef432012-12-06 14:24:21 +01005772 bool is_dp = false, is_cpu_edp = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005773 struct intel_encoder *encoder;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005774 int ret;
5775 bool dither;
5776
5777 for_each_encoder_on_crtc(dev, crtc, encoder) {
5778 switch (encoder->type) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005779 case INTEL_OUTPUT_DISPLAYPORT:
5780 is_dp = true;
5781 break;
5782 case INTEL_OUTPUT_EDP:
5783 is_dp = true;
5784 if (!intel_encoder_is_pch_edp(&encoder->base))
5785 is_cpu_edp = true;
5786 break;
5787 }
5788
5789 num_connectors++;
5790 }
5791
Paulo Zanoni5dc52982012-10-05 12:05:56 -03005792 /* We are not sure yet this won't happen. */
5793 WARN(!HAS_PCH_LPT(dev), "Unexpected PCH type %d\n",
5794 INTEL_PCH_TYPE(dev));
5795
5796 WARN(num_connectors != 1, "%d connectors attached to pipe %c\n",
5797 num_connectors, pipe_name(pipe));
5798
Paulo Zanoni702e7a52012-10-23 18:29:59 -02005799 WARN_ON(I915_READ(PIPECONF(intel_crtc->cpu_transcoder)) &
Paulo Zanoni1ce42922012-10-05 12:06:01 -03005800 (PIPECONF_ENABLE | I965_PIPECONF_ACTIVE));
5801
5802 WARN_ON(I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE);
5803
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005804 if (!intel_ddi_pll_mode_set(crtc, adjusted_mode->clock))
5805 return -EINVAL;
5806
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005807 /* Ensure that the cursor is valid for the new mode before changing... */
5808 intel_crtc_update_cursor(crtc, true);
5809
5810 /* determine panel color depth */
Jani Nikulac8241962012-11-02 10:19:55 +02005811 dither = intel_choose_pipe_bpp_dither(crtc, fb, &intel_crtc->bpp,
5812 adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005813
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005814 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
5815 drm_mode_debug_printmodeline(mode);
5816
Daniel Vettered7ef432012-12-06 14:24:21 +01005817 if (is_dp && !is_cpu_edp)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005818 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005819
5820 intel_crtc->lowfreq_avail = false;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005821
5822 intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
5823
Paulo Zanoni1eb8dfe2012-10-18 12:42:10 -03005824 if (!is_dp || is_cpu_edp)
5825 ironlake_set_m_n(crtc, mode, adjusted_mode);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005826
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03005827 haswell_set_pipeconf(crtc, adjusted_mode, dither);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005828
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005829 intel_set_pipe_csc(crtc, adjusted_mode);
5830
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005831 /* Set up the display plane register */
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02005832 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03005833 POSTING_READ(DSPCNTR(plane));
5834
5835 ret = intel_pipe_set_base(crtc, x, y, fb);
5836
5837 intel_update_watermarks(dev);
5838
5839 intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
5840
Jesse Barnes79e53942008-11-07 14:24:08 -08005841 return ret;
5842}
5843
Eric Anholtf564048e2011-03-30 13:01:02 -07005844static int intel_crtc_mode_set(struct drm_crtc *crtc,
5845 struct drm_display_mode *mode,
5846 struct drm_display_mode *adjusted_mode,
5847 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005848 struct drm_framebuffer *fb)
Eric Anholtf564048e2011-03-30 13:01:02 -07005849{
5850 struct drm_device *dev = crtc->dev;
5851 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter9256aa12012-10-31 19:26:13 +01005852 struct drm_encoder_helper_funcs *encoder_funcs;
5853 struct intel_encoder *encoder;
Eric Anholt0b701d22011-03-30 13:01:03 -07005854 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5855 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07005856 int ret;
5857
Paulo Zanonicc464b22013-01-25 16:59:16 -02005858 if (IS_HASWELL(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
5859 intel_crtc->cpu_transcoder = TRANSCODER_EDP;
5860 else
5861 intel_crtc->cpu_transcoder = pipe;
5862
Eric Anholt0b701d22011-03-30 13:01:03 -07005863 drm_vblank_pre_modeset(dev, pipe);
5864
Eric Anholtf564048e2011-03-30 13:01:02 -07005865 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005866 x, y, fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08005867 drm_vblank_post_modeset(dev, pipe);
5868
Daniel Vetter9256aa12012-10-31 19:26:13 +01005869 if (ret != 0)
5870 return ret;
5871
5872 for_each_encoder_on_crtc(dev, crtc, encoder) {
5873 DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
5874 encoder->base.base.id,
5875 drm_get_encoder_name(&encoder->base),
5876 mode->base.id, mode->name);
5877 encoder_funcs = encoder->base.helper_private;
5878 encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
5879 }
5880
5881 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005882}
5883
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005884static bool intel_eld_uptodate(struct drm_connector *connector,
5885 int reg_eldv, uint32_t bits_eldv,
5886 int reg_elda, uint32_t bits_elda,
5887 int reg_edid)
5888{
5889 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5890 uint8_t *eld = connector->eld;
5891 uint32_t i;
5892
5893 i = I915_READ(reg_eldv);
5894 i &= bits_eldv;
5895
5896 if (!eld[0])
5897 return !i;
5898
5899 if (!i)
5900 return false;
5901
5902 i = I915_READ(reg_elda);
5903 i &= ~bits_elda;
5904 I915_WRITE(reg_elda, i);
5905
5906 for (i = 0; i < eld[2]; i++)
5907 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
5908 return false;
5909
5910 return true;
5911}
5912
Wu Fengguange0dac652011-09-05 14:25:34 +08005913static void g4x_write_eld(struct drm_connector *connector,
5914 struct drm_crtc *crtc)
5915{
5916 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5917 uint8_t *eld = connector->eld;
5918 uint32_t eldv;
5919 uint32_t len;
5920 uint32_t i;
5921
5922 i = I915_READ(G4X_AUD_VID_DID);
5923
5924 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
5925 eldv = G4X_ELDV_DEVCL_DEVBLC;
5926 else
5927 eldv = G4X_ELDV_DEVCTG;
5928
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005929 if (intel_eld_uptodate(connector,
5930 G4X_AUD_CNTL_ST, eldv,
5931 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
5932 G4X_HDMIW_HDMIEDID))
5933 return;
5934
Wu Fengguange0dac652011-09-05 14:25:34 +08005935 i = I915_READ(G4X_AUD_CNTL_ST);
5936 i &= ~(eldv | G4X_ELD_ADDR);
5937 len = (i >> 9) & 0x1f; /* ELD buffer size */
5938 I915_WRITE(G4X_AUD_CNTL_ST, i);
5939
5940 if (!eld[0])
5941 return;
5942
5943 len = min_t(uint8_t, eld[2], len);
5944 DRM_DEBUG_DRIVER("ELD size %d\n", len);
5945 for (i = 0; i < len; i++)
5946 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
5947
5948 i = I915_READ(G4X_AUD_CNTL_ST);
5949 i |= eldv;
5950 I915_WRITE(G4X_AUD_CNTL_ST, i);
5951}
5952
Wang Xingchao83358c852012-08-16 22:43:37 +08005953static void haswell_write_eld(struct drm_connector *connector,
5954 struct drm_crtc *crtc)
5955{
5956 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5957 uint8_t *eld = connector->eld;
5958 struct drm_device *dev = crtc->dev;
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08005959 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Wang Xingchao83358c852012-08-16 22:43:37 +08005960 uint32_t eldv;
5961 uint32_t i;
5962 int len;
5963 int pipe = to_intel_crtc(crtc)->pipe;
5964 int tmp;
5965
5966 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
5967 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
5968 int aud_config = HSW_AUD_CFG(pipe);
5969 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
5970
5971
5972 DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
5973
5974 /* Audio output enable */
5975 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
5976 tmp = I915_READ(aud_cntrl_st2);
5977 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
5978 I915_WRITE(aud_cntrl_st2, tmp);
5979
5980 /* Wait for 1 vertical blank */
5981 intel_wait_for_vblank(dev, pipe);
5982
5983 /* Set ELD valid state */
5984 tmp = I915_READ(aud_cntrl_st2);
5985 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
5986 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
5987 I915_WRITE(aud_cntrl_st2, tmp);
5988 tmp = I915_READ(aud_cntrl_st2);
5989 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
5990
5991 /* Enable HDMI mode */
5992 tmp = I915_READ(aud_config);
5993 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
5994 /* clear N_programing_enable and N_value_index */
5995 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
5996 I915_WRITE(aud_config, tmp);
5997
5998 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
5999
6000 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
Wang Xingchao7b9f35a2013-01-22 23:25:25 +08006001 intel_crtc->eld_vld = true;
Wang Xingchao83358c852012-08-16 22:43:37 +08006002
6003 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6004 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6005 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6006 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6007 } else
6008 I915_WRITE(aud_config, 0);
6009
6010 if (intel_eld_uptodate(connector,
6011 aud_cntrl_st2, eldv,
6012 aud_cntl_st, IBX_ELD_ADDRESS,
6013 hdmiw_hdmiedid))
6014 return;
6015
6016 i = I915_READ(aud_cntrl_st2);
6017 i &= ~eldv;
6018 I915_WRITE(aud_cntrl_st2, i);
6019
6020 if (!eld[0])
6021 return;
6022
6023 i = I915_READ(aud_cntl_st);
6024 i &= ~IBX_ELD_ADDRESS;
6025 I915_WRITE(aud_cntl_st, i);
6026 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
6027 DRM_DEBUG_DRIVER("port num:%d\n", i);
6028
6029 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6030 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6031 for (i = 0; i < len; i++)
6032 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6033
6034 i = I915_READ(aud_cntrl_st2);
6035 i |= eldv;
6036 I915_WRITE(aud_cntrl_st2, i);
6037
6038}
6039
Wu Fengguange0dac652011-09-05 14:25:34 +08006040static void ironlake_write_eld(struct drm_connector *connector,
6041 struct drm_crtc *crtc)
6042{
6043 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6044 uint8_t *eld = connector->eld;
6045 uint32_t eldv;
6046 uint32_t i;
6047 int len;
6048 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006049 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006050 int aud_cntl_st;
6051 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08006052 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08006053
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006054 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006055 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
6056 aud_config = IBX_AUD_CFG(pipe);
6057 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006058 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006059 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08006060 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
6061 aud_config = CPT_AUD_CFG(pipe);
6062 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006063 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006064 }
6065
Wang Xingchao9b138a82012-08-09 16:52:18 +08006066 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08006067
6068 i = I915_READ(aud_cntl_st);
Wang Xingchao9b138a82012-08-09 16:52:18 +08006069 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
Wu Fengguange0dac652011-09-05 14:25:34 +08006070 if (!i) {
6071 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6072 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006073 eldv = IBX_ELD_VALIDB;
6074 eldv |= IBX_ELD_VALIDB << 4;
6075 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006076 } else {
6077 DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006078 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006079 }
6080
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006081 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6082 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6083 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006084 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6085 } else
6086 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006087
6088 if (intel_eld_uptodate(connector,
6089 aud_cntrl_st2, eldv,
6090 aud_cntl_st, IBX_ELD_ADDRESS,
6091 hdmiw_hdmiedid))
6092 return;
6093
Wu Fengguange0dac652011-09-05 14:25:34 +08006094 i = I915_READ(aud_cntrl_st2);
6095 i &= ~eldv;
6096 I915_WRITE(aud_cntrl_st2, i);
6097
6098 if (!eld[0])
6099 return;
6100
Wu Fengguange0dac652011-09-05 14:25:34 +08006101 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006102 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006103 I915_WRITE(aud_cntl_st, i);
6104
6105 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6106 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6107 for (i = 0; i < len; i++)
6108 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6109
6110 i = I915_READ(aud_cntrl_st2);
6111 i |= eldv;
6112 I915_WRITE(aud_cntrl_st2, i);
6113}
6114
6115void intel_write_eld(struct drm_encoder *encoder,
6116 struct drm_display_mode *mode)
6117{
6118 struct drm_crtc *crtc = encoder->crtc;
6119 struct drm_connector *connector;
6120 struct drm_device *dev = encoder->dev;
6121 struct drm_i915_private *dev_priv = dev->dev_private;
6122
6123 connector = drm_select_eld(encoder, mode);
6124 if (!connector)
6125 return;
6126
6127 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6128 connector->base.id,
6129 drm_get_connector_name(connector),
6130 connector->encoder->base.id,
6131 drm_get_encoder_name(connector->encoder));
6132
6133 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6134
6135 if (dev_priv->display.write_eld)
6136 dev_priv->display.write_eld(connector, crtc);
6137}
6138
Jesse Barnes79e53942008-11-07 14:24:08 -08006139/** Loads the palette/gamma unit for the CRTC with the prepared values */
6140void intel_crtc_load_lut(struct drm_crtc *crtc)
6141{
6142 struct drm_device *dev = crtc->dev;
6143 struct drm_i915_private *dev_priv = dev->dev_private;
6144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006145 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006146 int i;
6147
6148 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00006149 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08006150 return;
6151
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006152 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006153 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006154 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006155
Jesse Barnes79e53942008-11-07 14:24:08 -08006156 for (i = 0; i < 256; i++) {
6157 I915_WRITE(palreg + 4 * i,
6158 (intel_crtc->lut_r[i] << 16) |
6159 (intel_crtc->lut_g[i] << 8) |
6160 intel_crtc->lut_b[i]);
6161 }
6162}
6163
Chris Wilson560b85b2010-08-07 11:01:38 +01006164static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6165{
6166 struct drm_device *dev = crtc->dev;
6167 struct drm_i915_private *dev_priv = dev->dev_private;
6168 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6169 bool visible = base != 0;
6170 u32 cntl;
6171
6172 if (intel_crtc->cursor_visible == visible)
6173 return;
6174
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006175 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006176 if (visible) {
6177 /* On these chipsets we can only modify the base whilst
6178 * the cursor is disabled.
6179 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006180 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006181
6182 cntl &= ~(CURSOR_FORMAT_MASK);
6183 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6184 cntl |= CURSOR_ENABLE |
6185 CURSOR_GAMMA_ENABLE |
6186 CURSOR_FORMAT_ARGB;
6187 } else
6188 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006189 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006190
6191 intel_crtc->cursor_visible = visible;
6192}
6193
6194static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6195{
6196 struct drm_device *dev = crtc->dev;
6197 struct drm_i915_private *dev_priv = dev->dev_private;
6198 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6199 int pipe = intel_crtc->pipe;
6200 bool visible = base != 0;
6201
6202 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006203 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006204 if (base) {
6205 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6206 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6207 cntl |= pipe << 28; /* Connect to correct pipe */
6208 } else {
6209 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6210 cntl |= CURSOR_MODE_DISABLE;
6211 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006212 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006213
6214 intel_crtc->cursor_visible = visible;
6215 }
6216 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006217 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006218}
6219
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006220static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6221{
6222 struct drm_device *dev = crtc->dev;
6223 struct drm_i915_private *dev_priv = dev->dev_private;
6224 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6225 int pipe = intel_crtc->pipe;
6226 bool visible = base != 0;
6227
6228 if (intel_crtc->cursor_visible != visible) {
6229 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6230 if (base) {
6231 cntl &= ~CURSOR_MODE;
6232 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6233 } else {
6234 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6235 cntl |= CURSOR_MODE_DISABLE;
6236 }
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006237 if (IS_HASWELL(dev))
6238 cntl |= CURSOR_PIPE_CSC_ENABLE;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006239 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6240
6241 intel_crtc->cursor_visible = visible;
6242 }
6243 /* and commit changes on next vblank */
6244 I915_WRITE(CURBASE_IVB(pipe), base);
6245}
6246
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006247/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006248static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6249 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006250{
6251 struct drm_device *dev = crtc->dev;
6252 struct drm_i915_private *dev_priv = dev->dev_private;
6253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6254 int pipe = intel_crtc->pipe;
6255 int x = intel_crtc->cursor_x;
6256 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006257 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006258 bool visible;
6259
6260 pos = 0;
6261
Chris Wilson6b383a72010-09-13 13:54:26 +01006262 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006263 base = intel_crtc->cursor_addr;
6264 if (x > (int) crtc->fb->width)
6265 base = 0;
6266
6267 if (y > (int) crtc->fb->height)
6268 base = 0;
6269 } else
6270 base = 0;
6271
6272 if (x < 0) {
6273 if (x + intel_crtc->cursor_width < 0)
6274 base = 0;
6275
6276 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6277 x = -x;
6278 }
6279 pos |= x << CURSOR_X_SHIFT;
6280
6281 if (y < 0) {
6282 if (y + intel_crtc->cursor_height < 0)
6283 base = 0;
6284
6285 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6286 y = -y;
6287 }
6288 pos |= y << CURSOR_Y_SHIFT;
6289
6290 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006291 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006292 return;
6293
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03006294 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006295 I915_WRITE(CURPOS_IVB(pipe), pos);
6296 ivb_update_cursor(crtc, base);
6297 } else {
6298 I915_WRITE(CURPOS(pipe), pos);
6299 if (IS_845G(dev) || IS_I865G(dev))
6300 i845_update_cursor(crtc, base);
6301 else
6302 i9xx_update_cursor(crtc, base);
6303 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006304}
6305
Jesse Barnes79e53942008-11-07 14:24:08 -08006306static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006307 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006308 uint32_t handle,
6309 uint32_t width, uint32_t height)
6310{
6311 struct drm_device *dev = crtc->dev;
6312 struct drm_i915_private *dev_priv = dev->dev_private;
6313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006314 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006315 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006316 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006317
Jesse Barnes79e53942008-11-07 14:24:08 -08006318 /* if we want to turn off the cursor ignore width and height */
6319 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006320 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006321 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006322 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006323 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006324 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006325 }
6326
6327 /* Currently we only support 64x64 cursors */
6328 if (width != 64 || height != 64) {
6329 DRM_ERROR("we currently only support 64x64 cursors\n");
6330 return -EINVAL;
6331 }
6332
Chris Wilson05394f32010-11-08 19:18:58 +00006333 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006334 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006335 return -ENOENT;
6336
Chris Wilson05394f32010-11-08 19:18:58 +00006337 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006338 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006339 ret = -ENOMEM;
6340 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006341 }
6342
Dave Airlie71acb5e2008-12-30 20:31:46 +10006343 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006344 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006345 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00006346 if (obj->tiling_mode) {
6347 DRM_ERROR("cursor cannot be tiled\n");
6348 ret = -EINVAL;
6349 goto fail_locked;
6350 }
6351
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006352 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006353 if (ret) {
6354 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006355 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006356 }
6357
Chris Wilsond9e86c02010-11-10 16:40:20 +00006358 ret = i915_gem_object_put_fence(obj);
6359 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006360 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006361 goto fail_unpin;
6362 }
6363
Chris Wilson05394f32010-11-08 19:18:58 +00006364 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006365 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006366 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006367 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006368 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6369 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006370 if (ret) {
6371 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006372 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006373 }
Chris Wilson05394f32010-11-08 19:18:58 +00006374 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006375 }
6376
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006377 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006378 I915_WRITE(CURSIZE, (height << 12) | width);
6379
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006380 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006381 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006382 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006383 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006384 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6385 } else
6386 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006387 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006388 }
Jesse Barnes80824002009-09-10 15:28:06 -07006389
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006390 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006391
6392 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006393 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006394 intel_crtc->cursor_width = width;
6395 intel_crtc->cursor_height = height;
6396
Chris Wilson6b383a72010-09-13 13:54:26 +01006397 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006398
Jesse Barnes79e53942008-11-07 14:24:08 -08006399 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006400fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006401 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006402fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006403 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006404fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006405 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006406 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006407}
6408
6409static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6410{
Jesse Barnes79e53942008-11-07 14:24:08 -08006411 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006412
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006413 intel_crtc->cursor_x = x;
6414 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006415
Chris Wilson6b383a72010-09-13 13:54:26 +01006416 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08006417
6418 return 0;
6419}
6420
6421/** Sets the color ramps on behalf of RandR */
6422void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6423 u16 blue, int regno)
6424{
6425 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6426
6427 intel_crtc->lut_r[regno] = red >> 8;
6428 intel_crtc->lut_g[regno] = green >> 8;
6429 intel_crtc->lut_b[regno] = blue >> 8;
6430}
6431
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006432void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6433 u16 *blue, int regno)
6434{
6435 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6436
6437 *red = intel_crtc->lut_r[regno] << 8;
6438 *green = intel_crtc->lut_g[regno] << 8;
6439 *blue = intel_crtc->lut_b[regno] << 8;
6440}
6441
Jesse Barnes79e53942008-11-07 14:24:08 -08006442static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006443 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006444{
James Simmons72034252010-08-03 01:33:19 +01006445 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006446 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006447
James Simmons72034252010-08-03 01:33:19 +01006448 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006449 intel_crtc->lut_r[i] = red[i] >> 8;
6450 intel_crtc->lut_g[i] = green[i] >> 8;
6451 intel_crtc->lut_b[i] = blue[i] >> 8;
6452 }
6453
6454 intel_crtc_load_lut(crtc);
6455}
6456
6457/**
6458 * Get a pipe with a simple mode set on it for doing load-based monitor
6459 * detection.
6460 *
6461 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07006462 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08006463 *
Eric Anholtc751ce42010-03-25 11:48:48 -07006464 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08006465 * configured for it. In the future, it could choose to temporarily disable
6466 * some outputs to free up a pipe for its use.
6467 *
6468 * \return crtc, or NULL if no pipes are available.
6469 */
6470
6471/* VESA 640x480x72Hz mode to set on the pipe */
6472static struct drm_display_mode load_detect_mode = {
6473 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6474 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6475};
6476
Chris Wilsond2dff872011-04-19 08:36:26 +01006477static struct drm_framebuffer *
6478intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006479 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006480 struct drm_i915_gem_object *obj)
6481{
6482 struct intel_framebuffer *intel_fb;
6483 int ret;
6484
6485 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6486 if (!intel_fb) {
6487 drm_gem_object_unreference_unlocked(&obj->base);
6488 return ERR_PTR(-ENOMEM);
6489 }
6490
6491 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6492 if (ret) {
6493 drm_gem_object_unreference_unlocked(&obj->base);
6494 kfree(intel_fb);
6495 return ERR_PTR(ret);
6496 }
6497
6498 return &intel_fb->base;
6499}
6500
6501static u32
6502intel_framebuffer_pitch_for_width(int width, int bpp)
6503{
6504 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6505 return ALIGN(pitch, 64);
6506}
6507
6508static u32
6509intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6510{
6511 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6512 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6513}
6514
6515static struct drm_framebuffer *
6516intel_framebuffer_create_for_mode(struct drm_device *dev,
6517 struct drm_display_mode *mode,
6518 int depth, int bpp)
6519{
6520 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00006521 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01006522
6523 obj = i915_gem_alloc_object(dev,
6524 intel_framebuffer_size_for_mode(mode, bpp));
6525 if (obj == NULL)
6526 return ERR_PTR(-ENOMEM);
6527
6528 mode_cmd.width = mode->hdisplay;
6529 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006530 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6531 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00006532 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01006533
6534 return intel_framebuffer_create(dev, &mode_cmd, obj);
6535}
6536
6537static struct drm_framebuffer *
6538mode_fits_in_fbdev(struct drm_device *dev,
6539 struct drm_display_mode *mode)
6540{
6541 struct drm_i915_private *dev_priv = dev->dev_private;
6542 struct drm_i915_gem_object *obj;
6543 struct drm_framebuffer *fb;
6544
6545 if (dev_priv->fbdev == NULL)
6546 return NULL;
6547
6548 obj = dev_priv->fbdev->ifb.obj;
6549 if (obj == NULL)
6550 return NULL;
6551
6552 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006553 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6554 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006555 return NULL;
6556
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006557 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006558 return NULL;
6559
6560 return fb;
6561}
6562
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006563bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01006564 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006565 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006566{
6567 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006568 struct intel_encoder *intel_encoder =
6569 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08006570 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006571 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006572 struct drm_crtc *crtc = NULL;
6573 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02006574 struct drm_framebuffer *fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006575 int i = -1;
6576
Chris Wilsond2dff872011-04-19 08:36:26 +01006577 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6578 connector->base.id, drm_get_connector_name(connector),
6579 encoder->base.id, drm_get_encoder_name(encoder));
6580
Jesse Barnes79e53942008-11-07 14:24:08 -08006581 /*
6582 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006583 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006584 * - if the connector already has an assigned crtc, use it (but make
6585 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006586 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006587 * - try to find the first unused crtc that can drive this connector,
6588 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006589 */
6590
6591 /* See if we already have a CRTC for this connector */
6592 if (encoder->crtc) {
6593 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006594
Daniel Vetter7b240562012-12-12 00:35:33 +01006595 mutex_lock(&crtc->mutex);
6596
Daniel Vetter24218aa2012-08-12 19:27:11 +02006597 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006598 old->load_detect_temp = false;
6599
6600 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006601 if (connector->dpms != DRM_MODE_DPMS_ON)
6602 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01006603
Chris Wilson71731882011-04-19 23:10:58 +01006604 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006605 }
6606
6607 /* Find an unused one (if possible) */
6608 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6609 i++;
6610 if (!(encoder->possible_crtcs & (1 << i)))
6611 continue;
6612 if (!possible_crtc->enabled) {
6613 crtc = possible_crtc;
6614 break;
6615 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006616 }
6617
6618 /*
6619 * If we didn't find an unused CRTC, don't use any.
6620 */
6621 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006622 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6623 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006624 }
6625
Daniel Vetter7b240562012-12-12 00:35:33 +01006626 mutex_lock(&crtc->mutex);
Daniel Vetterfc303102012-07-09 10:40:58 +02006627 intel_encoder->new_crtc = to_intel_crtc(crtc);
6628 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006629
6630 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +02006631 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01006632 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006633 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006634
Chris Wilson64927112011-04-20 07:25:26 +01006635 if (!mode)
6636 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006637
Chris Wilsond2dff872011-04-19 08:36:26 +01006638 /* We need a framebuffer large enough to accommodate all accesses
6639 * that the plane may generate whilst we perform load detection.
6640 * We can not rely on the fbcon either being present (we get called
6641 * during its initialisation to detect all boot displays, or it may
6642 * not even exist) or that it is large enough to satisfy the
6643 * requested mode.
6644 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02006645 fb = mode_fits_in_fbdev(dev, mode);
6646 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006647 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006648 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6649 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01006650 } else
6651 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02006652 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01006653 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Daniel Vetter7b240562012-12-12 00:35:33 +01006654 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006655 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006656 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006657
Chris Wilsonc0c36b942012-12-19 16:08:43 +00006658 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006659 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006660 if (old->release_fb)
6661 old->release_fb->funcs->destroy(old->release_fb);
Daniel Vetter7b240562012-12-12 00:35:33 +01006662 mutex_unlock(&crtc->mutex);
Chris Wilson0e8b3d32012-11-05 22:25:08 +00006663 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006664 }
Chris Wilson71731882011-04-19 23:10:58 +01006665
Jesse Barnes79e53942008-11-07 14:24:08 -08006666 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006667 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01006668 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006669}
6670
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006671void intel_release_load_detect_pipe(struct drm_connector *connector,
Chris Wilson8261b192011-04-19 23:18:09 +01006672 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006673{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02006674 struct intel_encoder *intel_encoder =
6675 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01006676 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01006677 struct drm_crtc *crtc = encoder->crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -08006678
Chris Wilsond2dff872011-04-19 08:36:26 +01006679 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6680 connector->base.id, drm_get_connector_name(connector),
6681 encoder->base.id, drm_get_encoder_name(encoder));
6682
Chris Wilson8261b192011-04-19 23:18:09 +01006683 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02006684 to_intel_connector(connector)->new_encoder = NULL;
6685 intel_encoder->new_crtc = NULL;
6686 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01006687
Daniel Vetter36206362012-12-10 20:42:17 +01006688 if (old->release_fb) {
6689 drm_framebuffer_unregister_private(old->release_fb);
6690 drm_framebuffer_unreference(old->release_fb);
6691 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006692
Daniel Vetter67c96402013-01-23 16:25:09 +00006693 mutex_unlock(&crtc->mutex);
Chris Wilson0622a532011-04-21 09:32:11 +01006694 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006695 }
6696
Eric Anholtc751ce42010-03-25 11:48:48 -07006697 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02006698 if (old->dpms_mode != DRM_MODE_DPMS_ON)
6699 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01006700
6701 mutex_unlock(&crtc->mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08006702}
6703
6704/* Returns the clock of the currently programmed mode of the given pipe. */
6705static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6706{
6707 struct drm_i915_private *dev_priv = dev->dev_private;
6708 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6709 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08006710 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006711 u32 fp;
6712 intel_clock_t clock;
6713
6714 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006715 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006716 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006717 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006718
6719 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006720 if (IS_PINEVIEW(dev)) {
6721 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6722 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006723 } else {
6724 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6725 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6726 }
6727
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006728 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006729 if (IS_PINEVIEW(dev))
6730 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6731 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006732 else
6733 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006734 DPLL_FPA01_P1_POST_DIV_SHIFT);
6735
6736 switch (dpll & DPLL_MODE_MASK) {
6737 case DPLLB_MODE_DAC_SERIAL:
6738 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6739 5 : 10;
6740 break;
6741 case DPLLB_MODE_LVDS:
6742 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6743 7 : 14;
6744 break;
6745 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006746 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006747 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6748 return 0;
6749 }
6750
6751 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08006752 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006753 } else {
6754 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6755
6756 if (is_lvds) {
6757 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6758 DPLL_FPA01_P1_POST_DIV_SHIFT);
6759 clock.p2 = 14;
6760
6761 if ((dpll & PLL_REF_INPUT_MASK) ==
6762 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6763 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08006764 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006765 } else
Shaohua Li21778322009-02-23 15:19:16 +08006766 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006767 } else {
6768 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6769 clock.p1 = 2;
6770 else {
6771 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6772 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6773 }
6774 if (dpll & PLL_P2_DIVIDE_BY_4)
6775 clock.p2 = 4;
6776 else
6777 clock.p2 = 2;
6778
Shaohua Li21778322009-02-23 15:19:16 +08006779 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006780 }
6781 }
6782
6783 /* XXX: It would be nice to validate the clocks, but we can't reuse
6784 * i830PllIsValid() because it relies on the xf86_config connector
6785 * configuration being accurate, which it isn't necessarily.
6786 */
6787
6788 return clock.dot;
6789}
6790
6791/** Returns the currently programmed mode of the given pipe. */
6792struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6793 struct drm_crtc *crtc)
6794{
Jesse Barnes548f2452011-02-17 10:40:53 -08006795 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006797 enum transcoder cpu_transcoder = intel_crtc->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006798 struct drm_display_mode *mode;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006799 int htot = I915_READ(HTOTAL(cpu_transcoder));
6800 int hsync = I915_READ(HSYNC(cpu_transcoder));
6801 int vtot = I915_READ(VTOTAL(cpu_transcoder));
6802 int vsync = I915_READ(VSYNC(cpu_transcoder));
Jesse Barnes79e53942008-11-07 14:24:08 -08006803
6804 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6805 if (!mode)
6806 return NULL;
6807
6808 mode->clock = intel_crtc_clock_get(dev, crtc);
6809 mode->hdisplay = (htot & 0xffff) + 1;
6810 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6811 mode->hsync_start = (hsync & 0xffff) + 1;
6812 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6813 mode->vdisplay = (vtot & 0xffff) + 1;
6814 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6815 mode->vsync_start = (vsync & 0xffff) + 1;
6816 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6817
6818 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006819
6820 return mode;
6821}
6822
Daniel Vetter3dec0092010-08-20 21:40:52 +02006823static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07006824{
6825 struct drm_device *dev = crtc->dev;
6826 drm_i915_private_t *dev_priv = dev->dev_private;
6827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6828 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006829 int dpll_reg = DPLL(pipe);
6830 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07006831
Eric Anholtbad720f2009-10-22 16:11:14 -07006832 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006833 return;
6834
6835 if (!dev_priv->lvds_downclock_avail)
6836 return;
6837
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006838 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006839 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08006840 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006841
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006842 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006843
6844 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6845 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006846 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006847
Jesse Barnes652c3932009-08-17 13:31:43 -07006848 dpll = I915_READ(dpll_reg);
6849 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08006850 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006851 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006852}
6853
6854static void intel_decrease_pllclock(struct drm_crtc *crtc)
6855{
6856 struct drm_device *dev = crtc->dev;
6857 drm_i915_private_t *dev_priv = dev->dev_private;
6858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006859
Eric Anholtbad720f2009-10-22 16:11:14 -07006860 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006861 return;
6862
6863 if (!dev_priv->lvds_downclock_avail)
6864 return;
6865
6866 /*
6867 * Since this is called by a timer, we should never get here in
6868 * the manual case.
6869 */
6870 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01006871 int pipe = intel_crtc->pipe;
6872 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02006873 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01006874
Zhao Yakui44d98a62009-10-09 11:39:40 +08006875 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006876
Sean Paul8ac5a6d2012-02-13 13:14:51 -05006877 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006878
Chris Wilson074b5e12012-05-02 12:07:06 +01006879 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006880 dpll |= DISPLAY_RATE_SELECT_FPA1;
6881 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006882 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006883 dpll = I915_READ(dpll_reg);
6884 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08006885 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006886 }
6887
6888}
6889
Chris Wilsonf047e392012-07-21 12:31:41 +01006890void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07006891{
Chris Wilsonf047e392012-07-21 12:31:41 +01006892 i915_update_gfx_val(dev->dev_private);
6893}
6894
6895void intel_mark_idle(struct drm_device *dev)
6896{
Chris Wilson725a5b52013-01-08 11:02:57 +00006897 struct drm_crtc *crtc;
6898
6899 if (!i915_powersave)
6900 return;
6901
6902 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6903 if (!crtc->fb)
6904 continue;
6905
6906 intel_decrease_pllclock(crtc);
6907 }
Chris Wilsonf047e392012-07-21 12:31:41 +01006908}
6909
6910void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
6911{
6912 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07006913 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07006914
6915 if (!i915_powersave)
6916 return;
6917
Jesse Barnes652c3932009-08-17 13:31:43 -07006918 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Jesse Barnes652c3932009-08-17 13:31:43 -07006919 if (!crtc->fb)
6920 continue;
6921
Chris Wilsonf047e392012-07-21 12:31:41 +01006922 if (to_intel_framebuffer(crtc->fb)->obj == obj)
6923 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006924 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006925}
6926
Jesse Barnes79e53942008-11-07 14:24:08 -08006927static void intel_crtc_destroy(struct drm_crtc *crtc)
6928{
6929 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006930 struct drm_device *dev = crtc->dev;
6931 struct intel_unpin_work *work;
6932 unsigned long flags;
6933
6934 spin_lock_irqsave(&dev->event_lock, flags);
6935 work = intel_crtc->unpin_work;
6936 intel_crtc->unpin_work = NULL;
6937 spin_unlock_irqrestore(&dev->event_lock, flags);
6938
6939 if (work) {
6940 cancel_work_sync(&work->work);
6941 kfree(work);
6942 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006943
6944 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02006945
Jesse Barnes79e53942008-11-07 14:24:08 -08006946 kfree(intel_crtc);
6947}
6948
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006949static void intel_unpin_work_fn(struct work_struct *__work)
6950{
6951 struct intel_unpin_work *work =
6952 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006953 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006954
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006955 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01006956 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00006957 drm_gem_object_unreference(&work->pending_flip_obj->base);
6958 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00006959
Chris Wilsonb4a98e52012-11-01 09:26:26 +00006960 intel_update_fbc(dev);
6961 mutex_unlock(&dev->struct_mutex);
6962
6963 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
6964 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
6965
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006966 kfree(work);
6967}
6968
Jesse Barnes1afe3e92010-03-26 10:35:20 -07006969static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01006970 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006971{
6972 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6974 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006975 unsigned long flags;
6976
6977 /* Ignore early vblank irqs */
6978 if (intel_crtc == NULL)
6979 return;
6980
6981 spin_lock_irqsave(&dev->event_lock, flags);
6982 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00006983
6984 /* Ensure we don't miss a work->pending update ... */
6985 smp_rmb();
6986
6987 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006988 spin_unlock_irqrestore(&dev->event_lock, flags);
6989 return;
6990 }
6991
Chris Wilsone7d841c2012-12-03 11:36:30 +00006992 /* and that the unpin work is consistent wrt ->pending. */
6993 smp_rmb();
6994
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006995 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006996
Rob Clark45a066e2012-10-08 14:50:40 -05006997 if (work->event)
6998 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006999
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007000 drm_vblank_put(dev, intel_crtc->pipe);
7001
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007002 spin_unlock_irqrestore(&dev->event_lock, flags);
7003
Daniel Vetter2c10d572012-12-20 21:24:07 +01007004 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007005
7006 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007007
7008 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007009}
7010
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007011void intel_finish_page_flip(struct drm_device *dev, int pipe)
7012{
7013 drm_i915_private_t *dev_priv = dev->dev_private;
7014 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7015
Mario Kleiner49b14a52010-12-09 07:00:07 +01007016 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007017}
7018
7019void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7020{
7021 drm_i915_private_t *dev_priv = dev->dev_private;
7022 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7023
Mario Kleiner49b14a52010-12-09 07:00:07 +01007024 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007025}
7026
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007027void intel_prepare_page_flip(struct drm_device *dev, int plane)
7028{
7029 drm_i915_private_t *dev_priv = dev->dev_private;
7030 struct intel_crtc *intel_crtc =
7031 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7032 unsigned long flags;
7033
Chris Wilsone7d841c2012-12-03 11:36:30 +00007034 /* NB: An MMIO update of the plane base pointer will also
7035 * generate a page-flip completion irq, i.e. every modeset
7036 * is also accompanied by a spurious intel_prepare_page_flip().
7037 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007038 spin_lock_irqsave(&dev->event_lock, flags);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007039 if (intel_crtc->unpin_work)
7040 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007041 spin_unlock_irqrestore(&dev->event_lock, flags);
7042}
7043
Chris Wilsone7d841c2012-12-03 11:36:30 +00007044inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
7045{
7046 /* Ensure that the work item is consistent when activating it ... */
7047 smp_wmb();
7048 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
7049 /* and that it is marked active as soon as the irq could fire. */
7050 smp_wmb();
7051}
7052
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007053static int intel_gen2_queue_flip(struct drm_device *dev,
7054 struct drm_crtc *crtc,
7055 struct drm_framebuffer *fb,
7056 struct drm_i915_gem_object *obj)
7057{
7058 struct drm_i915_private *dev_priv = dev->dev_private;
7059 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007060 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007061 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007062 int ret;
7063
Daniel Vetter6d90c952012-04-26 23:28:05 +02007064 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007065 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007066 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007067
Daniel Vetter6d90c952012-04-26 23:28:05 +02007068 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007069 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007070 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007071
7072 /* Can't queue multiple flips, so wait for the previous
7073 * one to finish before executing the next.
7074 */
7075 if (intel_crtc->plane)
7076 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7077 else
7078 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007079 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7080 intel_ring_emit(ring, MI_NOOP);
7081 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7082 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7083 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02007084 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007085 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00007086
7087 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007088 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007089 return 0;
7090
7091err_unpin:
7092 intel_unpin_fb_obj(obj);
7093err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007094 return ret;
7095}
7096
7097static int intel_gen3_queue_flip(struct drm_device *dev,
7098 struct drm_crtc *crtc,
7099 struct drm_framebuffer *fb,
7100 struct drm_i915_gem_object *obj)
7101{
7102 struct drm_i915_private *dev_priv = dev->dev_private;
7103 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007104 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007105 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007106 int ret;
7107
Daniel Vetter6d90c952012-04-26 23:28:05 +02007108 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007109 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007110 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007111
Daniel Vetter6d90c952012-04-26 23:28:05 +02007112 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007113 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007114 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007115
7116 if (intel_crtc->plane)
7117 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7118 else
7119 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007120 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
7121 intel_ring_emit(ring, MI_NOOP);
7122 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
7123 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7124 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vettere506a0c2012-07-05 12:17:29 +02007125 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007126 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007127
Chris Wilsone7d841c2012-12-03 11:36:30 +00007128 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007129 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007130 return 0;
7131
7132err_unpin:
7133 intel_unpin_fb_obj(obj);
7134err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007135 return ret;
7136}
7137
7138static int intel_gen4_queue_flip(struct drm_device *dev,
7139 struct drm_crtc *crtc,
7140 struct drm_framebuffer *fb,
7141 struct drm_i915_gem_object *obj)
7142{
7143 struct drm_i915_private *dev_priv = dev->dev_private;
7144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7145 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007146 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007147 int ret;
7148
Daniel Vetter6d90c952012-04-26 23:28:05 +02007149 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007150 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007151 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007152
Daniel Vetter6d90c952012-04-26 23:28:05 +02007153 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007154 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007155 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007156
7157 /* i965+ uses the linear or tiled offsets from the
7158 * Display Registers (which do not change across a page-flip)
7159 * so we need only reprogram the base address.
7160 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02007161 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7162 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7163 intel_ring_emit(ring, fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007164 intel_ring_emit(ring,
7165 (obj->gtt_offset + intel_crtc->dspaddr_offset) |
7166 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007167
7168 /* XXX Enabling the panel-fitter across page-flip is so far
7169 * untested on non-native modes, so ignore it for now.
7170 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7171 */
7172 pf = 0;
7173 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007174 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007175
7176 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007177 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007178 return 0;
7179
7180err_unpin:
7181 intel_unpin_fb_obj(obj);
7182err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007183 return ret;
7184}
7185
7186static int intel_gen6_queue_flip(struct drm_device *dev,
7187 struct drm_crtc *crtc,
7188 struct drm_framebuffer *fb,
7189 struct drm_i915_gem_object *obj)
7190{
7191 struct drm_i915_private *dev_priv = dev->dev_private;
7192 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007193 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007194 uint32_t pf, pipesrc;
7195 int ret;
7196
Daniel Vetter6d90c952012-04-26 23:28:05 +02007197 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007198 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007199 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007200
Daniel Vetter6d90c952012-04-26 23:28:05 +02007201 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007202 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007203 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007204
Daniel Vetter6d90c952012-04-26 23:28:05 +02007205 intel_ring_emit(ring, MI_DISPLAY_FLIP |
7206 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
7207 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Daniel Vetterc2c75132012-07-05 12:17:30 +02007208 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007209
Chris Wilson99d9acd2012-04-17 20:37:00 +01007210 /* Contrary to the suggestions in the documentation,
7211 * "Enable Panel Fitter" does not seem to be required when page
7212 * flipping with a non-native mode, and worse causes a normal
7213 * modeset to fail.
7214 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7215 */
7216 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007217 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02007218 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00007219
7220 intel_mark_page_flip_active(intel_crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02007221 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007222 return 0;
7223
7224err_unpin:
7225 intel_unpin_fb_obj(obj);
7226err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007227 return ret;
7228}
7229
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007230/*
7231 * On gen7 we currently use the blit ring because (in early silicon at least)
7232 * the render ring doesn't give us interrpts for page flip completion, which
7233 * means clients will hang after the first flip is queued. Fortunately the
7234 * blit ring generates interrupts properly, so use it instead.
7235 */
7236static int intel_gen7_queue_flip(struct drm_device *dev,
7237 struct drm_crtc *crtc,
7238 struct drm_framebuffer *fb,
7239 struct drm_i915_gem_object *obj)
7240{
7241 struct drm_i915_private *dev_priv = dev->dev_private;
7242 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7243 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007244 uint32_t plane_bit = 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007245 int ret;
7246
7247 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7248 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007249 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007250
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007251 switch(intel_crtc->plane) {
7252 case PLANE_A:
7253 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
7254 break;
7255 case PLANE_B:
7256 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
7257 break;
7258 case PLANE_C:
7259 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
7260 break;
7261 default:
7262 WARN_ONCE(1, "unknown plane in flip command\n");
7263 ret = -ENODEV;
Eugeni Dodonovab3951e2012-06-18 19:03:38 -03007264 goto err_unpin;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007265 }
7266
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007267 ret = intel_ring_begin(ring, 4);
7268 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01007269 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007270
Daniel Vettercb05d8d2012-05-23 14:02:00 +02007271 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007272 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Daniel Vetterc2c75132012-07-05 12:17:30 +02007273 intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007274 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00007275
7276 intel_mark_page_flip_active(intel_crtc);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007277 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01007278 return 0;
7279
7280err_unpin:
7281 intel_unpin_fb_obj(obj);
7282err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007283 return ret;
7284}
7285
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007286static int intel_default_queue_flip(struct drm_device *dev,
7287 struct drm_crtc *crtc,
7288 struct drm_framebuffer *fb,
7289 struct drm_i915_gem_object *obj)
7290{
7291 return -ENODEV;
7292}
7293
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007294static int intel_crtc_page_flip(struct drm_crtc *crtc,
7295 struct drm_framebuffer *fb,
7296 struct drm_pending_vblank_event *event)
7297{
7298 struct drm_device *dev = crtc->dev;
7299 struct drm_i915_private *dev_priv = dev->dev_private;
7300 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007301 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007302 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7303 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007304 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007305 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007306
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03007307 /* Can't change pixel format via MI display flips. */
7308 if (fb->pixel_format != crtc->fb->pixel_format)
7309 return -EINVAL;
7310
7311 /*
7312 * TILEOFF/LINOFF registers can't be changed via MI display flips.
7313 * Note that pitch changes could also affect these register.
7314 */
7315 if (INTEL_INFO(dev)->gen > 3 &&
7316 (fb->offsets[0] != crtc->fb->offsets[0] ||
7317 fb->pitches[0] != crtc->fb->pitches[0]))
7318 return -EINVAL;
7319
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007320 work = kzalloc(sizeof *work, GFP_KERNEL);
7321 if (work == NULL)
7322 return -ENOMEM;
7323
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007324 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007325 work->crtc = crtc;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007326 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08007327 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007328 INIT_WORK(&work->work, intel_unpin_work_fn);
7329
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007330 ret = drm_vblank_get(dev, intel_crtc->pipe);
7331 if (ret)
7332 goto free_work;
7333
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007334 /* We borrow the event spin lock for protecting unpin_work */
7335 spin_lock_irqsave(&dev->event_lock, flags);
7336 if (intel_crtc->unpin_work) {
7337 spin_unlock_irqrestore(&dev->event_lock, flags);
7338 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007339 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007340
7341 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007342 return -EBUSY;
7343 }
7344 intel_crtc->unpin_work = work;
7345 spin_unlock_irqrestore(&dev->event_lock, flags);
7346
7347 intel_fb = to_intel_framebuffer(fb);
7348 obj = intel_fb->obj;
7349
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007350 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
7351 flush_workqueue(dev_priv->wq);
7352
Chris Wilson79158102012-05-23 11:13:58 +01007353 ret = i915_mutex_lock_interruptible(dev);
7354 if (ret)
7355 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007356
Jesse Barnes75dfca82010-02-10 15:09:44 -08007357 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007358 drm_gem_object_reference(&work->old_fb_obj->base);
7359 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007360
7361 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007362
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007363 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007364
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007365 work->enable_stall_check = true;
7366
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007367 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02007368 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007369
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007370 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7371 if (ret)
7372 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007373
Chris Wilson7782de32011-07-08 12:22:41 +01007374 intel_disable_fbc(dev);
Chris Wilsonf047e392012-07-21 12:31:41 +01007375 intel_mark_fb_busy(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007376 mutex_unlock(&dev->struct_mutex);
7377
Jesse Barnese5510fa2010-07-01 16:48:37 -07007378 trace_i915_flip_request(intel_crtc->plane, obj);
7379
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007380 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007381
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007382cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00007383 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson05394f32010-11-08 19:18:58 +00007384 drm_gem_object_unreference(&work->old_fb_obj->base);
7385 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007386 mutex_unlock(&dev->struct_mutex);
7387
Chris Wilson79158102012-05-23 11:13:58 +01007388cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01007389 spin_lock_irqsave(&dev->event_lock, flags);
7390 intel_crtc->unpin_work = NULL;
7391 spin_unlock_irqrestore(&dev->event_lock, flags);
7392
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007393 drm_vblank_put(dev, intel_crtc->pipe);
7394free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007395 kfree(work);
7396
7397 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007398}
7399
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007400static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007401 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7402 .load_lut = intel_crtc_load_lut,
Daniel Vetter976f8a22012-07-08 22:34:21 +02007403 .disable = intel_crtc_noop,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007404};
7405
Daniel Vetter6ed0f792012-07-08 19:41:43 +02007406bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
7407{
7408 struct intel_encoder *other_encoder;
7409 struct drm_crtc *crtc = &encoder->new_crtc->base;
7410
7411 if (WARN_ON(!crtc))
7412 return false;
7413
7414 list_for_each_entry(other_encoder,
7415 &crtc->dev->mode_config.encoder_list,
7416 base.head) {
7417
7418 if (&other_encoder->new_crtc->base != crtc ||
7419 encoder == other_encoder)
7420 continue;
7421 else
7422 return true;
7423 }
7424
7425 return false;
7426}
7427
Daniel Vetter50f56112012-07-02 09:35:43 +02007428static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
7429 struct drm_crtc *crtc)
7430{
7431 struct drm_device *dev;
7432 struct drm_crtc *tmp;
7433 int crtc_mask = 1;
7434
7435 WARN(!crtc, "checking null crtc?\n");
7436
7437 dev = crtc->dev;
7438
7439 list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
7440 if (tmp == crtc)
7441 break;
7442 crtc_mask <<= 1;
7443 }
7444
7445 if (encoder->possible_crtcs & crtc_mask)
7446 return true;
7447 return false;
7448}
7449
Daniel Vetter9a935852012-07-05 22:34:27 +02007450/**
7451 * intel_modeset_update_staged_output_state
7452 *
7453 * Updates the staged output configuration state, e.g. after we've read out the
7454 * current hw state.
7455 */
7456static void intel_modeset_update_staged_output_state(struct drm_device *dev)
7457{
7458 struct intel_encoder *encoder;
7459 struct intel_connector *connector;
7460
7461 list_for_each_entry(connector, &dev->mode_config.connector_list,
7462 base.head) {
7463 connector->new_encoder =
7464 to_intel_encoder(connector->base.encoder);
7465 }
7466
7467 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7468 base.head) {
7469 encoder->new_crtc =
7470 to_intel_crtc(encoder->base.crtc);
7471 }
7472}
7473
7474/**
7475 * intel_modeset_commit_output_state
7476 *
7477 * This function copies the stage display pipe configuration to the real one.
7478 */
7479static void intel_modeset_commit_output_state(struct drm_device *dev)
7480{
7481 struct intel_encoder *encoder;
7482 struct intel_connector *connector;
7483
7484 list_for_each_entry(connector, &dev->mode_config.connector_list,
7485 base.head) {
7486 connector->base.encoder = &connector->new_encoder->base;
7487 }
7488
7489 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7490 base.head) {
7491 encoder->base.crtc = &encoder->new_crtc->base;
7492 }
7493}
7494
Daniel Vetter7758a112012-07-08 19:40:39 +02007495static struct drm_display_mode *
7496intel_modeset_adjusted_mode(struct drm_crtc *crtc,
7497 struct drm_display_mode *mode)
7498{
7499 struct drm_device *dev = crtc->dev;
7500 struct drm_display_mode *adjusted_mode;
7501 struct drm_encoder_helper_funcs *encoder_funcs;
7502 struct intel_encoder *encoder;
7503
7504 adjusted_mode = drm_mode_duplicate(dev, mode);
7505 if (!adjusted_mode)
7506 return ERR_PTR(-ENOMEM);
7507
7508 /* Pass our mode to the connectors and the CRTC to give them a chance to
7509 * adjust it according to limitations or connector properties, and also
7510 * a chance to reject the mode entirely.
7511 */
7512 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7513 base.head) {
7514
7515 if (&encoder->new_crtc->base != crtc)
7516 continue;
7517 encoder_funcs = encoder->base.helper_private;
7518 if (!(encoder_funcs->mode_fixup(&encoder->base, mode,
7519 adjusted_mode))) {
7520 DRM_DEBUG_KMS("Encoder fixup failed\n");
7521 goto fail;
7522 }
7523 }
7524
7525 if (!(intel_crtc_mode_fixup(crtc, mode, adjusted_mode))) {
7526 DRM_DEBUG_KMS("CRTC fixup failed\n");
7527 goto fail;
7528 }
7529 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
7530
7531 return adjusted_mode;
7532fail:
7533 drm_mode_destroy(dev, adjusted_mode);
7534 return ERR_PTR(-EINVAL);
7535}
7536
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007537/* Computes which crtcs are affected and sets the relevant bits in the mask. For
7538 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
7539static void
7540intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
7541 unsigned *prepare_pipes, unsigned *disable_pipes)
7542{
7543 struct intel_crtc *intel_crtc;
7544 struct drm_device *dev = crtc->dev;
7545 struct intel_encoder *encoder;
7546 struct intel_connector *connector;
7547 struct drm_crtc *tmp_crtc;
7548
7549 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
7550
7551 /* Check which crtcs have changed outputs connected to them, these need
7552 * to be part of the prepare_pipes mask. We don't (yet) support global
7553 * modeset across multiple crtcs, so modeset_pipes will only have one
7554 * bit set at most. */
7555 list_for_each_entry(connector, &dev->mode_config.connector_list,
7556 base.head) {
7557 if (connector->base.encoder == &connector->new_encoder->base)
7558 continue;
7559
7560 if (connector->base.encoder) {
7561 tmp_crtc = connector->base.encoder->crtc;
7562
7563 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7564 }
7565
7566 if (connector->new_encoder)
7567 *prepare_pipes |=
7568 1 << connector->new_encoder->new_crtc->pipe;
7569 }
7570
7571 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7572 base.head) {
7573 if (encoder->base.crtc == &encoder->new_crtc->base)
7574 continue;
7575
7576 if (encoder->base.crtc) {
7577 tmp_crtc = encoder->base.crtc;
7578
7579 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
7580 }
7581
7582 if (encoder->new_crtc)
7583 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
7584 }
7585
7586 /* Check for any pipes that will be fully disabled ... */
7587 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7588 base.head) {
7589 bool used = false;
7590
7591 /* Don't try to disable disabled crtcs. */
7592 if (!intel_crtc->base.enabled)
7593 continue;
7594
7595 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7596 base.head) {
7597 if (encoder->new_crtc == intel_crtc)
7598 used = true;
7599 }
7600
7601 if (!used)
7602 *disable_pipes |= 1 << intel_crtc->pipe;
7603 }
7604
7605
7606 /* set_mode is also used to update properties on life display pipes. */
7607 intel_crtc = to_intel_crtc(crtc);
7608 if (crtc->enabled)
7609 *prepare_pipes |= 1 << intel_crtc->pipe;
7610
7611 /* We only support modeset on one single crtc, hence we need to do that
7612 * only for the passed in crtc iff we change anything else than just
7613 * disable crtcs.
7614 *
7615 * This is actually not true, to be fully compatible with the old crtc
7616 * helper we automatically disable _any_ output (i.e. doesn't need to be
7617 * connected to the crtc we're modesetting on) if it's disconnected.
7618 * Which is a rather nutty api (since changed the output configuration
7619 * without userspace's explicit request can lead to confusion), but
7620 * alas. Hence we currently need to modeset on all pipes we prepare. */
7621 if (*prepare_pipes)
7622 *modeset_pipes = *prepare_pipes;
7623
7624 /* ... and mask these out. */
7625 *modeset_pipes &= ~(*disable_pipes);
7626 *prepare_pipes &= ~(*disable_pipes);
7627}
7628
Daniel Vetterea9d7582012-07-10 10:42:52 +02007629static bool intel_crtc_in_use(struct drm_crtc *crtc)
7630{
7631 struct drm_encoder *encoder;
7632 struct drm_device *dev = crtc->dev;
7633
7634 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
7635 if (encoder->crtc == crtc)
7636 return true;
7637
7638 return false;
7639}
7640
7641static void
7642intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
7643{
7644 struct intel_encoder *intel_encoder;
7645 struct intel_crtc *intel_crtc;
7646 struct drm_connector *connector;
7647
7648 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
7649 base.head) {
7650 if (!intel_encoder->base.crtc)
7651 continue;
7652
7653 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
7654
7655 if (prepare_pipes & (1 << intel_crtc->pipe))
7656 intel_encoder->connectors_active = false;
7657 }
7658
7659 intel_modeset_commit_output_state(dev);
7660
7661 /* Update computed state. */
7662 list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
7663 base.head) {
7664 intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
7665 }
7666
7667 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
7668 if (!connector->encoder || !connector->encoder->crtc)
7669 continue;
7670
7671 intel_crtc = to_intel_crtc(connector->encoder->crtc);
7672
7673 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02007674 struct drm_property *dpms_property =
7675 dev->mode_config.dpms_property;
7676
Daniel Vetterea9d7582012-07-10 10:42:52 +02007677 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05007678 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02007679 dpms_property,
7680 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02007681
7682 intel_encoder = to_intel_encoder(connector->encoder);
7683 intel_encoder->connectors_active = true;
7684 }
7685 }
7686
7687}
7688
Daniel Vetter25c5b262012-07-08 22:08:04 +02007689#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
7690 list_for_each_entry((intel_crtc), \
7691 &(dev)->mode_config.crtc_list, \
7692 base.head) \
7693 if (mask & (1 <<(intel_crtc)->pipe)) \
7694
Daniel Vetterb9805142012-08-31 17:37:33 +02007695void
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007696intel_modeset_check_state(struct drm_device *dev)
7697{
7698 struct intel_crtc *crtc;
7699 struct intel_encoder *encoder;
7700 struct intel_connector *connector;
7701
7702 list_for_each_entry(connector, &dev->mode_config.connector_list,
7703 base.head) {
7704 /* This also checks the encoder/connector hw state with the
7705 * ->get_hw_state callbacks. */
7706 intel_connector_check_state(connector);
7707
7708 WARN(&connector->new_encoder->base != connector->base.encoder,
7709 "connector's staged encoder doesn't match current encoder\n");
7710 }
7711
7712 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7713 base.head) {
7714 bool enabled = false;
7715 bool active = false;
7716 enum pipe pipe, tracked_pipe;
7717
7718 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
7719 encoder->base.base.id,
7720 drm_get_encoder_name(&encoder->base));
7721
7722 WARN(&encoder->new_crtc->base != encoder->base.crtc,
7723 "encoder's stage crtc doesn't match current crtc\n");
7724 WARN(encoder->connectors_active && !encoder->base.crtc,
7725 "encoder's active_connectors set, but no crtc\n");
7726
7727 list_for_each_entry(connector, &dev->mode_config.connector_list,
7728 base.head) {
7729 if (connector->base.encoder != &encoder->base)
7730 continue;
7731 enabled = true;
7732 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
7733 active = true;
7734 }
7735 WARN(!!encoder->base.crtc != enabled,
7736 "encoder's enabled state mismatch "
7737 "(expected %i, found %i)\n",
7738 !!encoder->base.crtc, enabled);
7739 WARN(active && !encoder->base.crtc,
7740 "active encoder with no crtc\n");
7741
7742 WARN(encoder->connectors_active != active,
7743 "encoder's computed active state doesn't match tracked active state "
7744 "(expected %i, found %i)\n", active, encoder->connectors_active);
7745
7746 active = encoder->get_hw_state(encoder, &pipe);
7747 WARN(active != encoder->connectors_active,
7748 "encoder's hw state doesn't match sw tracking "
7749 "(expected %i, found %i)\n",
7750 encoder->connectors_active, active);
7751
7752 if (!encoder->base.crtc)
7753 continue;
7754
7755 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
7756 WARN(active && pipe != tracked_pipe,
7757 "active encoder's pipe doesn't match"
7758 "(expected %i, found %i)\n",
7759 tracked_pipe, pipe);
7760
7761 }
7762
7763 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
7764 base.head) {
7765 bool enabled = false;
7766 bool active = false;
7767
7768 DRM_DEBUG_KMS("[CRTC:%d]\n",
7769 crtc->base.base.id);
7770
7771 WARN(crtc->active && !crtc->base.enabled,
7772 "active crtc, but not enabled in sw tracking\n");
7773
7774 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
7775 base.head) {
7776 if (encoder->base.crtc != &crtc->base)
7777 continue;
7778 enabled = true;
7779 if (encoder->connectors_active)
7780 active = true;
7781 }
7782 WARN(active != crtc->active,
7783 "crtc's computed active state doesn't match tracked active state "
7784 "(expected %i, found %i)\n", active, crtc->active);
7785 WARN(enabled != crtc->base.enabled,
7786 "crtc's computed enabled state doesn't match tracked enabled state "
7787 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
7788
7789 assert_pipe(dev->dev_private, crtc->pipe, crtc->active);
7790 }
7791}
7792
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007793int intel_set_mode(struct drm_crtc *crtc,
7794 struct drm_display_mode *mode,
7795 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +02007796{
7797 struct drm_device *dev = crtc->dev;
Daniel Vetterdbf2b54e2012-07-02 11:18:29 +02007798 drm_i915_private_t *dev_priv = dev->dev_private;
Tim Gardner3ac18232012-12-07 07:54:26 -07007799 struct drm_display_mode *adjusted_mode, *saved_mode, *saved_hwmode;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007800 struct intel_crtc *intel_crtc;
7801 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007802 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +02007803
Tim Gardner3ac18232012-12-07 07:54:26 -07007804 saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007805 if (!saved_mode)
7806 return -ENOMEM;
Tim Gardner3ac18232012-12-07 07:54:26 -07007807 saved_hwmode = saved_mode + 1;
Daniel Vettera6778b32012-07-02 09:56:42 +02007808
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007809 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +02007810 &prepare_pipes, &disable_pipes);
7811
7812 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
7813 modeset_pipes, prepare_pipes, disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02007814
Daniel Vetter976f8a22012-07-08 22:34:21 +02007815 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
7816 intel_crtc_disable(&intel_crtc->base);
7817
Tim Gardner3ac18232012-12-07 07:54:26 -07007818 *saved_hwmode = crtc->hwmode;
7819 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007820
Daniel Vetter25c5b262012-07-08 22:08:04 +02007821 /* Hack: Because we don't (yet) support global modeset on multiple
7822 * crtcs, we don't keep track of the new mode for more than one crtc.
7823 * Hence simply check whether any bit is set in modeset_pipes in all the
7824 * pieces of code that are not yet converted to deal with mutliple crtcs
7825 * changing their mode at the same time. */
7826 adjusted_mode = NULL;
7827 if (modeset_pipes) {
7828 adjusted_mode = intel_modeset_adjusted_mode(crtc, mode);
7829 if (IS_ERR(adjusted_mode)) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007830 ret = PTR_ERR(adjusted_mode);
Tim Gardner3ac18232012-12-07 07:54:26 -07007831 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +02007832 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007833 }
7834
Daniel Vetterea9d7582012-07-10 10:42:52 +02007835 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
7836 if (intel_crtc->base.enabled)
7837 dev_priv->display.crtc_disable(&intel_crtc->base);
7838 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007839
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02007840 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
7841 * to set it here already despite that we pass it down the callchain.
7842 */
7843 if (modeset_pipes)
Daniel Vetter25c5b262012-07-08 22:08:04 +02007844 crtc->mode = *mode;
Daniel Vetter7758a112012-07-08 19:40:39 +02007845
Daniel Vetterea9d7582012-07-10 10:42:52 +02007846 /* Only after disabling all output pipelines that will be changed can we
7847 * update the the output configuration. */
7848 intel_modeset_update_state(dev, prepare_pipes);
7849
Daniel Vetter47fab732012-10-26 10:58:18 +02007850 if (dev_priv->display.modeset_global_resources)
7851 dev_priv->display.modeset_global_resources(dev);
7852
Daniel Vettera6778b32012-07-02 09:56:42 +02007853 /* Set up the DPLL and any encoders state that needs to adjust or depend
7854 * on the DPLL.
7855 */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007856 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007857 ret = intel_crtc_mode_set(&intel_crtc->base,
7858 mode, adjusted_mode,
7859 x, y, fb);
7860 if (ret)
7861 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +02007862 }
7863
7864 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Daniel Vetter25c5b262012-07-08 22:08:04 +02007865 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
7866 dev_priv->display.crtc_enable(&intel_crtc->base);
Daniel Vettera6778b32012-07-02 09:56:42 +02007867
Daniel Vetter25c5b262012-07-08 22:08:04 +02007868 if (modeset_pipes) {
7869 /* Store real post-adjustment hardware mode. */
7870 crtc->hwmode = *adjusted_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +02007871
Daniel Vetter25c5b262012-07-08 22:08:04 +02007872 /* Calculate and store various constants which
7873 * are later needed by vblank and swap-completion
7874 * timestamping. They are derived from true hwmode.
7875 */
7876 drm_calc_timestamping_constants(crtc);
7877 }
Daniel Vettera6778b32012-07-02 09:56:42 +02007878
7879 /* FIXME: add subpixel order */
7880done:
7881 drm_mode_destroy(dev, adjusted_mode);
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007882 if (ret && crtc->enabled) {
Tim Gardner3ac18232012-12-07 07:54:26 -07007883 crtc->hwmode = *saved_hwmode;
7884 crtc->mode = *saved_mode;
Daniel Vetter8af6cf82012-07-10 09:50:11 +02007885 } else {
7886 intel_modeset_check_state(dev);
Daniel Vettera6778b32012-07-02 09:56:42 +02007887 }
7888
Tim Gardner3ac18232012-12-07 07:54:26 -07007889out:
7890 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +02007891 return ret;
7892}
7893
Chris Wilsonc0c36b942012-12-19 16:08:43 +00007894void intel_crtc_restore_mode(struct drm_crtc *crtc)
7895{
7896 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
7897}
7898
Daniel Vetter25c5b262012-07-08 22:08:04 +02007899#undef for_each_intel_crtc_masked
7900
Daniel Vetterd9e55602012-07-04 22:16:09 +02007901static void intel_set_config_free(struct intel_set_config *config)
7902{
7903 if (!config)
7904 return;
7905
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007906 kfree(config->save_connector_encoders);
7907 kfree(config->save_encoder_crtcs);
Daniel Vetterd9e55602012-07-04 22:16:09 +02007908 kfree(config);
7909}
7910
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007911static int intel_set_config_save_state(struct drm_device *dev,
7912 struct intel_set_config *config)
7913{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007914 struct drm_encoder *encoder;
7915 struct drm_connector *connector;
7916 int count;
7917
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007918 config->save_encoder_crtcs =
7919 kcalloc(dev->mode_config.num_encoder,
7920 sizeof(struct drm_crtc *), GFP_KERNEL);
7921 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007922 return -ENOMEM;
7923
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007924 config->save_connector_encoders =
7925 kcalloc(dev->mode_config.num_connector,
7926 sizeof(struct drm_encoder *), GFP_KERNEL);
7927 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007928 return -ENOMEM;
7929
7930 /* Copy data. Note that driver private data is not affected.
7931 * Should anything bad happen only the expected state is
7932 * restored, not the drivers personal bookkeeping.
7933 */
7934 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007935 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007936 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007937 }
7938
7939 count = 0;
7940 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +02007941 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007942 }
7943
7944 return 0;
7945}
7946
7947static void intel_set_config_restore_state(struct drm_device *dev,
7948 struct intel_set_config *config)
7949{
Daniel Vetter9a935852012-07-05 22:34:27 +02007950 struct intel_encoder *encoder;
7951 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007952 int count;
7953
7954 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007955 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7956 encoder->new_crtc =
7957 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007958 }
7959
7960 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02007961 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
7962 connector->new_encoder =
7963 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +02007964 }
7965}
7966
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007967static void
7968intel_set_config_compute_mode_changes(struct drm_mode_set *set,
7969 struct intel_set_config *config)
7970{
7971
7972 /* We should be able to check here if the fb has the same properties
7973 * and then just flip_or_move it */
7974 if (set->crtc->fb != set->fb) {
7975 /* If we have no fb then treat it as a full mode set */
7976 if (set->crtc->fb == NULL) {
7977 DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
7978 config->mode_changed = true;
7979 } else if (set->fb == NULL) {
7980 config->mode_changed = true;
7981 } else if (set->fb->depth != set->crtc->fb->depth) {
7982 config->mode_changed = true;
7983 } else if (set->fb->bits_per_pixel !=
7984 set->crtc->fb->bits_per_pixel) {
7985 config->mode_changed = true;
7986 } else
7987 config->fb_changed = true;
7988 }
7989
Daniel Vetter835c5872012-07-10 18:11:08 +02007990 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +02007991 config->fb_changed = true;
7992
7993 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
7994 DRM_DEBUG_KMS("modes are different, full mode set\n");
7995 drm_mode_debug_printmodeline(&set->crtc->mode);
7996 drm_mode_debug_printmodeline(set->mode);
7997 config->mode_changed = true;
7998 }
7999}
8000
Daniel Vetter2e431052012-07-04 22:42:15 +02008001static int
Daniel Vetter9a935852012-07-05 22:34:27 +02008002intel_modeset_stage_output_state(struct drm_device *dev,
8003 struct drm_mode_set *set,
8004 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +02008005{
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008006 struct drm_crtc *new_crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02008007 struct intel_connector *connector;
8008 struct intel_encoder *encoder;
Daniel Vetter2e431052012-07-04 22:42:15 +02008009 int count, ro;
Daniel Vetter50f56112012-07-02 09:35:43 +02008010
Damien Lespiau9abdda72013-02-13 13:29:23 +00008011 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +02008012 * of connectors. For paranoia, double-check this. */
8013 WARN_ON(!set->fb && (set->num_connectors != 0));
8014 WARN_ON(set->fb && (set->num_connectors == 0));
8015
Daniel Vetter50f56112012-07-02 09:35:43 +02008016 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008017 list_for_each_entry(connector, &dev->mode_config.connector_list,
8018 base.head) {
8019 /* Otherwise traverse passed in connector list and get encoders
8020 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008021 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008022 if (set->connectors[ro] == &connector->base) {
8023 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +02008024 break;
8025 }
8026 }
8027
Daniel Vetter9a935852012-07-05 22:34:27 +02008028 /* If we disable the crtc, disable all its connectors. Also, if
8029 * the connector is on the changing crtc but not on the new
8030 * connector list, disable it. */
8031 if ((!set->fb || ro == set->num_connectors) &&
8032 connector->base.encoder &&
8033 connector->base.encoder->crtc == set->crtc) {
8034 connector->new_encoder = NULL;
8035
8036 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
8037 connector->base.base.id,
8038 drm_get_connector_name(&connector->base));
8039 }
8040
8041
8042 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008043 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008044 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008045 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008046 }
8047 /* connector->new_encoder is now updated for all connectors. */
8048
8049 /* Update crtc of enabled connectors. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008050 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +02008051 list_for_each_entry(connector, &dev->mode_config.connector_list,
8052 base.head) {
8053 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +02008054 continue;
8055
Daniel Vetter9a935852012-07-05 22:34:27 +02008056 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +02008057
8058 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +02008059 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +02008060 new_crtc = set->crtc;
8061 }
8062
8063 /* Make sure the new CRTC will work with the encoder */
Daniel Vetter9a935852012-07-05 22:34:27 +02008064 if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
8065 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008066 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +02008067 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008068 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
8069
8070 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
8071 connector->base.base.id,
8072 drm_get_connector_name(&connector->base),
8073 new_crtc->base.id);
8074 }
8075
8076 /* Check for any encoders that needs to be disabled. */
8077 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
8078 base.head) {
8079 list_for_each_entry(connector,
8080 &dev->mode_config.connector_list,
8081 base.head) {
8082 if (connector->new_encoder == encoder) {
8083 WARN_ON(!connector->new_encoder->new_crtc);
8084
8085 goto next_encoder;
8086 }
8087 }
8088 encoder->new_crtc = NULL;
8089next_encoder:
8090 /* Only now check for crtc changes so we don't miss encoders
8091 * that will be disabled. */
8092 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008093 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008094 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +02008095 }
8096 }
Daniel Vetter9a935852012-07-05 22:34:27 +02008097 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +02008098
Daniel Vetter2e431052012-07-04 22:42:15 +02008099 return 0;
8100}
8101
8102static int intel_crtc_set_config(struct drm_mode_set *set)
8103{
8104 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +02008105 struct drm_mode_set save_set;
8106 struct intel_set_config *config;
8107 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +02008108
Daniel Vetter8d3e3752012-07-05 16:09:09 +02008109 BUG_ON(!set);
8110 BUG_ON(!set->crtc);
8111 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +02008112
8113 if (!set->mode)
8114 set->fb = NULL;
8115
Daniel Vetter431e50f2012-07-10 17:53:42 +02008116 /* The fb helper likes to play gross jokes with ->mode_set_config.
8117 * Unfortunately the crtc helper doesn't do much at all for this case,
8118 * so we have to cope with this madness until the fb helper is fixed up. */
8119 if (set->fb && set->num_connectors == 0)
8120 return 0;
8121
Daniel Vetter2e431052012-07-04 22:42:15 +02008122 if (set->fb) {
8123 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
8124 set->crtc->base.id, set->fb->base.id,
8125 (int)set->num_connectors, set->x, set->y);
8126 } else {
8127 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +02008128 }
8129
8130 dev = set->crtc->dev;
8131
8132 ret = -ENOMEM;
8133 config = kzalloc(sizeof(*config), GFP_KERNEL);
8134 if (!config)
8135 goto out_config;
8136
8137 ret = intel_set_config_save_state(dev, config);
8138 if (ret)
8139 goto out_config;
8140
8141 save_set.crtc = set->crtc;
8142 save_set.mode = &set->crtc->mode;
8143 save_set.x = set->crtc->x;
8144 save_set.y = set->crtc->y;
8145 save_set.fb = set->crtc->fb;
8146
8147 /* Compute whether we need a full modeset, only an fb base update or no
8148 * change at all. In the future we might also check whether only the
8149 * mode changed, e.g. for LVDS where we only change the panel fitter in
8150 * such cases. */
8151 intel_set_config_compute_mode_changes(set, config);
8152
Daniel Vetter9a935852012-07-05 22:34:27 +02008153 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +02008154 if (ret)
8155 goto fail;
8156
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008157 if (config->mode_changed) {
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008158 if (set->mode) {
Daniel Vetter50f56112012-07-02 09:35:43 +02008159 DRM_DEBUG_KMS("attempting to set mode from"
8160 " userspace\n");
8161 drm_mode_debug_printmodeline(set->mode);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008162 }
8163
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008164 ret = intel_set_mode(set->crtc, set->mode,
8165 set->x, set->y, set->fb);
8166 if (ret) {
8167 DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
8168 set->crtc->base.id, ret);
Daniel Vetter87f1faa2012-07-05 23:36:17 +02008169 goto fail;
8170 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008171 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +02008172 intel_crtc_wait_for_pending_flips(set->crtc);
8173
Daniel Vetter4f660f42012-07-02 09:47:37 +02008174 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +02008175 set->x, set->y, set->fb);
Daniel Vetter50f56112012-07-02 09:35:43 +02008176 }
8177
Daniel Vetterd9e55602012-07-04 22:16:09 +02008178 intel_set_config_free(config);
8179
Daniel Vetter50f56112012-07-02 09:35:43 +02008180 return 0;
8181
8182fail:
Daniel Vetter85f9eb72012-07-04 22:24:08 +02008183 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008184
8185 /* Try to restore the config */
Daniel Vetter5e2b5842012-07-04 22:41:29 +02008186 if (config->mode_changed &&
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008187 intel_set_mode(save_set.crtc, save_set.mode,
8188 save_set.x, save_set.y, save_set.fb))
Daniel Vetter50f56112012-07-02 09:35:43 +02008189 DRM_ERROR("failed to restore config after modeset failure\n");
8190
Daniel Vetterd9e55602012-07-04 22:16:09 +02008191out_config:
8192 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +02008193 return ret;
8194}
8195
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008196static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008197 .cursor_set = intel_crtc_cursor_set,
8198 .cursor_move = intel_crtc_cursor_move,
8199 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +02008200 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01008201 .destroy = intel_crtc_destroy,
8202 .page_flip = intel_crtc_page_flip,
8203};
8204
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008205static void intel_cpu_pll_init(struct drm_device *dev)
8206{
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008207 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008208 intel_ddi_pll_init(dev);
8209}
8210
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008211static void intel_pch_pll_init(struct drm_device *dev)
8212{
8213 drm_i915_private_t *dev_priv = dev->dev_private;
8214 int i;
8215
8216 if (dev_priv->num_pch_pll == 0) {
8217 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
8218 return;
8219 }
8220
8221 for (i = 0; i < dev_priv->num_pch_pll; i++) {
8222 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
8223 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
8224 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
8225 }
8226}
8227
Hannes Ederb358d0a2008-12-18 21:18:47 +01008228static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08008229{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008230 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008231 struct intel_crtc *intel_crtc;
8232 int i;
8233
8234 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
8235 if (intel_crtc == NULL)
8236 return;
8237
8238 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
8239
8240 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08008241 for (i = 0; i < 256; i++) {
8242 intel_crtc->lut_r[i] = i;
8243 intel_crtc->lut_g[i] = i;
8244 intel_crtc->lut_b[i] = i;
8245 }
8246
Jesse Barnes80824002009-09-10 15:28:06 -07008247 /* Swap pipes & planes for FBC on pre-965 */
8248 intel_crtc->pipe = pipe;
8249 intel_crtc->plane = pipe;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02008250 intel_crtc->cpu_transcoder = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01008251 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008252 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01008253 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07008254 }
8255
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08008256 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
8257 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
8258 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
8259 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
8260
Jesse Barnes5a354202011-06-24 12:19:22 -07008261 intel_crtc->bpp = 24; /* default for pre-Ironlake */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07008262
Jesse Barnes79e53942008-11-07 14:24:08 -08008263 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Jesse Barnes79e53942008-11-07 14:24:08 -08008264}
8265
Carl Worth08d7b3d2009-04-29 14:43:54 -07008266int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00008267 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07008268{
Carl Worth08d7b3d2009-04-29 14:43:54 -07008269 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02008270 struct drm_mode_object *drmmode_obj;
8271 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008272
Daniel Vetter1cff8f62012-04-24 09:55:08 +02008273 if (!drm_core_check_feature(dev, DRIVER_MODESET))
8274 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008275
Daniel Vetterc05422d2009-08-11 16:05:30 +02008276 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
8277 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07008278
Daniel Vetterc05422d2009-08-11 16:05:30 +02008279 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07008280 DRM_ERROR("no such CRTC id\n");
8281 return -EINVAL;
8282 }
8283
Daniel Vetterc05422d2009-08-11 16:05:30 +02008284 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
8285 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008286
Daniel Vetterc05422d2009-08-11 16:05:30 +02008287 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07008288}
8289
Daniel Vetter66a92782012-07-12 20:08:18 +02008290static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008291{
Daniel Vetter66a92782012-07-12 20:08:18 +02008292 struct drm_device *dev = encoder->base.dev;
8293 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008294 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008295 int entry = 0;
8296
Daniel Vetter66a92782012-07-12 20:08:18 +02008297 list_for_each_entry(source_encoder,
8298 &dev->mode_config.encoder_list, base.head) {
8299
8300 if (encoder == source_encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08008301 index_mask |= (1 << entry);
Daniel Vetter66a92782012-07-12 20:08:18 +02008302
8303 /* Intel hw has only one MUX where enocoders could be cloned. */
8304 if (encoder->cloneable && source_encoder->cloneable)
8305 index_mask |= (1 << entry);
8306
Jesse Barnes79e53942008-11-07 14:24:08 -08008307 entry++;
8308 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01008309
Jesse Barnes79e53942008-11-07 14:24:08 -08008310 return index_mask;
8311}
8312
Chris Wilson4d302442010-12-14 19:21:29 +00008313static bool has_edp_a(struct drm_device *dev)
8314{
8315 struct drm_i915_private *dev_priv = dev->dev_private;
8316
8317 if (!IS_MOBILE(dev))
8318 return false;
8319
8320 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
8321 return false;
8322
8323 if (IS_GEN5(dev) &&
8324 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
8325 return false;
8326
8327 return true;
8328}
8329
Jesse Barnes79e53942008-11-07 14:24:08 -08008330static void intel_setup_outputs(struct drm_device *dev)
8331{
Eric Anholt725e30a2009-01-22 13:01:02 -08008332 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008333 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008334 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008335 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08008336
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00008337 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00008338 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
8339 /* disable the panel fitter on everything but LVDS */
8340 I915_WRITE(PFIT_CONTROL, 0);
8341 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008342
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008343 if (!(HAS_DDI(dev) && (I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)))
Paulo Zanoni79935fc2012-11-20 13:27:40 -02008344 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008345
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008346 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -03008347 int found;
8348
8349 /* Haswell uses DDI functions to detect digital outputs */
8350 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
8351 /* DDI A only supports eDP */
8352 if (found)
8353 intel_ddi_init(dev, PORT_A);
8354
8355 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
8356 * register */
8357 found = I915_READ(SFUSE_STRAP);
8358
8359 if (found & SFUSE_STRAP_DDIB_DETECTED)
8360 intel_ddi_init(dev, PORT_B);
8361 if (found & SFUSE_STRAP_DDIC_DETECTED)
8362 intel_ddi_init(dev, PORT_C);
8363 if (found & SFUSE_STRAP_DDID_DETECTED)
8364 intel_ddi_init(dev, PORT_D);
8365 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008366 int found;
Daniel Vetter270b3042012-10-27 15:52:05 +02008367 dpd_is_edp = intel_dpd_is_edp(dev);
8368
8369 if (has_edp_a(dev))
8370 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -04008371
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008372 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08008373 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01008374 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008375 if (!found)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008376 intel_hdmi_init(dev, HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008377 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008378 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008379 }
8380
8381 if (I915_READ(HDMIC) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008382 intel_hdmi_init(dev, HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008383
Jesse Barnesb708a1d2012-06-11 14:39:56 -04008384 if (!dpd_is_edp && I915_READ(HDMID) & PORT_DETECTED)
Daniel Vetter08d644a2012-07-12 20:19:59 +02008385 intel_hdmi_init(dev, HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08008386
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008387 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008388 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08008389
Daniel Vetter270b3042012-10-27 15:52:05 +02008390 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008391 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008392 } else if (IS_VALLEYVIEW(dev)) {
Gajanan Bhat19c03922012-09-27 19:13:07 +05308393 /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008394 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
8395 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +05308396
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008397 if (I915_READ(VLV_DISPLAY_BASE + SDVOB) & PORT_DETECTED) {
8398 intel_hdmi_init(dev, VLV_DISPLAY_BASE + SDVOB, PORT_B);
8399 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
8400 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008401 }
8402
Ville Syrjälä67cfc202013-01-25 21:44:44 +02008403 if (I915_READ(VLV_DISPLAY_BASE + SDVOC) & PORT_DETECTED)
8404 intel_hdmi_init(dev, VLV_DISPLAY_BASE + SDVOC, PORT_C);
Jesse Barnes4a87d652012-06-15 11:55:16 -07008405
Zhenyu Wang103a1962009-11-27 11:44:36 +08008406 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08008407 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08008408
Eric Anholt725e30a2009-01-22 13:01:02 -08008409 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008410 DRM_DEBUG_KMS("probing SDVOB\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008411 found = intel_sdvo_init(dev, SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008412 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
8413 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008414 intel_hdmi_init(dev, SDVOB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008415 }
Ma Ling27185ae2009-08-24 13:50:23 +08008416
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008417 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
8418 DRM_DEBUG_KMS("probing DP_B\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008419 intel_dp_init(dev, DP_B, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008420 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008421 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008422
8423 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04008424
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008425 if (I915_READ(SDVOB) & SDVO_DETECTED) {
8426 DRM_DEBUG_KMS("probing SDVOC\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01008427 found = intel_sdvo_init(dev, SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008428 }
Ma Ling27185ae2009-08-24 13:50:23 +08008429
8430 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
8431
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008432 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
8433 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Daniel Vetter08d644a2012-07-12 20:19:59 +02008434 intel_hdmi_init(dev, SDVOC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008435 }
8436 if (SUPPORTS_INTEGRATED_DP(dev)) {
8437 DRM_DEBUG_KMS("probing DP_C\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008438 intel_dp_init(dev, DP_C, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008439 }
Eric Anholt725e30a2009-01-22 13:01:02 -08008440 }
Ma Ling27185ae2009-08-24 13:50:23 +08008441
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008442 if (SUPPORTS_INTEGRATED_DP(dev) &&
8443 (I915_READ(DP_D) & DP_DETECTED)) {
8444 DRM_DEBUG_KMS("probing DP_D\n");
Paulo Zanoniab9d7c32012-07-17 17:53:45 -03008445 intel_dp_init(dev, DP_D, PORT_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08008446 }
Eric Anholtbad720f2009-10-22 16:11:14 -07008447 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008448 intel_dvo_init(dev);
8449
Zhenyu Wang103a1962009-11-27 11:44:36 +08008450 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08008451 intel_tv_init(dev);
8452
Chris Wilson4ef69c72010-09-09 15:14:28 +01008453 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
8454 encoder->base.possible_crtcs = encoder->crtc_mask;
8455 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +02008456 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -08008457 }
Chris Wilson47356eb2011-01-11 17:06:04 +00008458
Paulo Zanonidde86e22012-12-01 12:04:25 -02008459 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +02008460
8461 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008462}
8463
8464static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
8465{
8466 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08008467
8468 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008469 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08008470
8471 kfree(intel_fb);
8472}
8473
8474static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00008475 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008476 unsigned int *handle)
8477{
8478 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00008479 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008480
Chris Wilson05394f32010-11-08 19:18:58 +00008481 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08008482}
8483
8484static const struct drm_framebuffer_funcs intel_fb_funcs = {
8485 .destroy = intel_user_framebuffer_destroy,
8486 .create_handle = intel_user_framebuffer_create_handle,
8487};
8488
Dave Airlie38651672010-03-30 05:34:13 +00008489int intel_framebuffer_init(struct drm_device *dev,
8490 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008491 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00008492 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08008493{
Jesse Barnes79e53942008-11-07 14:24:08 -08008494 int ret;
8495
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008496 if (obj->tiling_mode == I915_TILING_Y) {
8497 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01008498 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008499 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008500
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008501 if (mode_cmd->pitches[0] & 63) {
8502 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
8503 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +01008504 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008505 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008506
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008507 /* FIXME <= Gen4 stride limits are bit unclear */
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008508 if (mode_cmd->pitches[0] > 32768) {
8509 DRM_DEBUG("pitch (%d) must be at less than 32768\n",
8510 mode_cmd->pitches[0]);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008511 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008512 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008513
8514 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008515 mode_cmd->pitches[0] != obj->stride) {
8516 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
8517 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008518 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008519 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +02008520
Ville Syrjälä57779d02012-10-31 17:50:14 +02008521 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008522 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +02008523 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008524 case DRM_FORMAT_RGB565:
8525 case DRM_FORMAT_XRGB8888:
8526 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008527 break;
8528 case DRM_FORMAT_XRGB1555:
8529 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008530 if (INTEL_INFO(dev)->gen > 3) {
8531 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008532 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008533 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02008534 break;
8535 case DRM_FORMAT_XBGR8888:
8536 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02008537 case DRM_FORMAT_XRGB2101010:
8538 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02008539 case DRM_FORMAT_XBGR2101010:
8540 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008541 if (INTEL_INFO(dev)->gen < 4) {
8542 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008543 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008544 }
Jesse Barnesb5626742011-06-24 12:19:27 -07008545 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02008546 case DRM_FORMAT_YUYV:
8547 case DRM_FORMAT_UYVY:
8548 case DRM_FORMAT_YVYU:
8549 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008550 if (INTEL_INFO(dev)->gen < 5) {
8551 DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
Ville Syrjälä57779d02012-10-31 17:50:14 +02008552 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008553 }
Chris Wilson57cd6502010-08-08 12:34:44 +01008554 break;
8555 default:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +00008556 DRM_DEBUG("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01008557 return -EINVAL;
8558 }
8559
Ville Syrjälä90f9a332012-10-31 17:50:19 +02008560 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
8561 if (mode_cmd->offsets[0] != 0)
8562 return -EINVAL;
8563
Daniel Vetterc7d73f62012-12-13 23:38:38 +01008564 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
8565 intel_fb->obj = obj;
8566
Jesse Barnes79e53942008-11-07 14:24:08 -08008567 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
8568 if (ret) {
8569 DRM_ERROR("framebuffer init failed %d\n", ret);
8570 return ret;
8571 }
8572
Jesse Barnes79e53942008-11-07 14:24:08 -08008573 return 0;
8574}
8575
Jesse Barnes79e53942008-11-07 14:24:08 -08008576static struct drm_framebuffer *
8577intel_user_framebuffer_create(struct drm_device *dev,
8578 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008579 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08008580{
Chris Wilson05394f32010-11-08 19:18:58 +00008581 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08008582
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008583 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
8584 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00008585 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01008586 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08008587
Chris Wilsond2dff872011-04-19 08:36:26 +01008588 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08008589}
8590
Jesse Barnes79e53942008-11-07 14:24:08 -08008591static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08008592 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00008593 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08008594};
8595
Jesse Barnese70236a2009-09-21 10:42:27 -07008596/* Set up chip specific display functions */
8597static void intel_init_display(struct drm_device *dev)
8598{
8599 struct drm_i915_private *dev_priv = dev->dev_private;
8600
8601 /* We always want a DPMS function */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02008602 if (HAS_DDI(dev)) {
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008603 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02008604 dev_priv->display.crtc_enable = haswell_crtc_enable;
8605 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -03008606 dev_priv->display.off = haswell_crtc_off;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03008607 dev_priv->display.update_plane = ironlake_update_plane;
8608 } else if (HAS_PCH_SPLIT(dev)) {
Eric Anholtf564048e2011-03-30 13:01:02 -07008609 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008610 dev_priv->display.crtc_enable = ironlake_crtc_enable;
8611 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008612 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008613 dev_priv->display.update_plane = ironlake_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008614 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07008615 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +02008616 dev_priv->display.crtc_enable = i9xx_crtc_enable;
8617 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008618 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008619 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008620 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008621
Jesse Barnese70236a2009-09-21 10:42:27 -07008622 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07008623 if (IS_VALLEYVIEW(dev))
8624 dev_priv->display.get_display_clock_speed =
8625 valleyview_get_display_clock_speed;
8626 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07008627 dev_priv->display.get_display_clock_speed =
8628 i945_get_display_clock_speed;
8629 else if (IS_I915G(dev))
8630 dev_priv->display.get_display_clock_speed =
8631 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008632 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008633 dev_priv->display.get_display_clock_speed =
8634 i9xx_misc_get_display_clock_speed;
8635 else if (IS_I915GM(dev))
8636 dev_priv->display.get_display_clock_speed =
8637 i915gm_get_display_clock_speed;
8638 else if (IS_I865G(dev))
8639 dev_priv->display.get_display_clock_speed =
8640 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02008641 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008642 dev_priv->display.get_display_clock_speed =
8643 i855_get_display_clock_speed;
8644 else /* 852, 830 */
8645 dev_priv->display.get_display_clock_speed =
8646 i830_get_display_clock_speed;
8647
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008648 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01008649 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008650 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008651 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08008652 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07008653 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008654 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07008655 } else if (IS_IVYBRIDGE(dev)) {
8656 /* FIXME: detect B0+ stepping and use auto training */
8657 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08008658 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +02008659 dev_priv->display.modeset_global_resources =
8660 ivb_modeset_global_resources;
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -03008661 } else if (IS_HASWELL(dev)) {
8662 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +08008663 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02008664 dev_priv->display.modeset_global_resources =
8665 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -02008666 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07008667 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08008668 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07008669 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008670
8671 /* Default just returns -ENODEV to indicate unsupported */
8672 dev_priv->display.queue_flip = intel_default_queue_flip;
8673
8674 switch (INTEL_INFO(dev)->gen) {
8675 case 2:
8676 dev_priv->display.queue_flip = intel_gen2_queue_flip;
8677 break;
8678
8679 case 3:
8680 dev_priv->display.queue_flip = intel_gen3_queue_flip;
8681 break;
8682
8683 case 4:
8684 case 5:
8685 dev_priv->display.queue_flip = intel_gen4_queue_flip;
8686 break;
8687
8688 case 6:
8689 dev_priv->display.queue_flip = intel_gen6_queue_flip;
8690 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008691 case 7:
8692 dev_priv->display.queue_flip = intel_gen7_queue_flip;
8693 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008694 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008695}
8696
Jesse Barnesb690e962010-07-19 13:53:12 -07008697/*
8698 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
8699 * resume, or other times. This quirk makes sure that's the case for
8700 * affected systems.
8701 */
Akshay Joshi0206e352011-08-16 15:34:10 -04008702static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07008703{
8704 struct drm_i915_private *dev_priv = dev->dev_private;
8705
8706 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008707 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008708}
8709
Keith Packard435793d2011-07-12 14:56:22 -07008710/*
8711 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
8712 */
8713static void quirk_ssc_force_disable(struct drm_device *dev)
8714{
8715 struct drm_i915_private *dev_priv = dev->dev_private;
8716 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008717 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07008718}
8719
Carsten Emde4dca20e2012-03-15 15:56:26 +01008720/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01008721 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
8722 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01008723 */
8724static void quirk_invert_brightness(struct drm_device *dev)
8725{
8726 struct drm_i915_private *dev_priv = dev->dev_private;
8727 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02008728 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07008729}
8730
8731struct intel_quirk {
8732 int device;
8733 int subsystem_vendor;
8734 int subsystem_device;
8735 void (*hook)(struct drm_device *dev);
8736};
8737
Egbert Eich5f85f1762012-10-14 15:46:38 +02008738/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
8739struct intel_dmi_quirk {
8740 void (*hook)(struct drm_device *dev);
8741 const struct dmi_system_id (*dmi_id_list)[];
8742};
8743
8744static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
8745{
8746 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
8747 return 1;
8748}
8749
8750static const struct intel_dmi_quirk intel_dmi_quirks[] = {
8751 {
8752 .dmi_id_list = &(const struct dmi_system_id[]) {
8753 {
8754 .callback = intel_dmi_reverse_brightness,
8755 .ident = "NCR Corporation",
8756 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
8757 DMI_MATCH(DMI_PRODUCT_NAME, ""),
8758 },
8759 },
8760 { } /* terminating entry */
8761 },
8762 .hook = quirk_invert_brightness,
8763 },
8764};
8765
Ben Widawskyc43b5632012-04-16 14:07:40 -07008766static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07008767 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04008768 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07008769
Jesse Barnesb690e962010-07-19 13:53:12 -07008770 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
8771 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
8772
Jesse Barnesb690e962010-07-19 13:53:12 -07008773 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
8774 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
8775
Daniel Vetterccd0d362012-10-10 23:13:59 +02008776 /* 830/845 need to leave pipe A & dpll A up */
Jesse Barnesb690e962010-07-19 13:53:12 -07008777 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Daniel Vetterdcdaed62012-08-12 21:19:34 +02008778 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07008779
8780 /* Lenovo U160 cannot use SSC on LVDS */
8781 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02008782
8783 /* Sony Vaio Y cannot use SSC on LVDS */
8784 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01008785
8786 /* Acer Aspire 5734Z must invert backlight brightness */
8787 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jani Nikula1ffff602013-01-22 12:50:34 +02008788
8789 /* Acer/eMachines G725 */
8790 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
Jani Nikula01e3a8f2013-01-22 12:50:35 +02008791
8792 /* Acer/eMachines e725 */
8793 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
Jani Nikula5559eca2013-01-22 12:50:36 +02008794
8795 /* Acer/Packard Bell NCL20 */
8796 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
Daniel Vetterac4199e2013-02-15 18:35:30 +01008797
8798 /* Acer Aspire 4736Z */
8799 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07008800};
8801
8802static void intel_init_quirks(struct drm_device *dev)
8803{
8804 struct pci_dev *d = dev->pdev;
8805 int i;
8806
8807 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
8808 struct intel_quirk *q = &intel_quirks[i];
8809
8810 if (d->device == q->device &&
8811 (d->subsystem_vendor == q->subsystem_vendor ||
8812 q->subsystem_vendor == PCI_ANY_ID) &&
8813 (d->subsystem_device == q->subsystem_device ||
8814 q->subsystem_device == PCI_ANY_ID))
8815 q->hook(dev);
8816 }
Egbert Eich5f85f1762012-10-14 15:46:38 +02008817 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
8818 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
8819 intel_dmi_quirks[i].hook(dev);
8820 }
Jesse Barnesb690e962010-07-19 13:53:12 -07008821}
8822
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008823/* Disable the VGA plane that we never use */
8824static void i915_disable_vga(struct drm_device *dev)
8825{
8826 struct drm_i915_private *dev_priv = dev->dev_private;
8827 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02008828 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008829
8830 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07008831 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008832 sr1 = inb(VGA_SR_DATA);
8833 outb(sr1 | 1<<5, VGA_SR_DATA);
8834 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
8835 udelay(300);
8836
8837 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
8838 POSTING_READ(vga_reg);
8839}
8840
Daniel Vetterf8175862012-04-10 15:50:11 +02008841void intel_modeset_init_hw(struct drm_device *dev)
8842{
Paulo Zanonifa42e232013-01-25 16:59:11 -02008843 intel_init_power_well(dev);
Eugeni Dodonov0232e922012-07-06 15:42:36 -03008844
Eugeni Dodonova8f78b52012-06-28 15:55:35 -03008845 intel_prepare_ddi(dev);
8846
Daniel Vetterf8175862012-04-10 15:50:11 +02008847 intel_init_clock_gating(dev);
8848
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008849 mutex_lock(&dev->struct_mutex);
Daniel Vetter8090c6b2012-06-24 16:42:32 +02008850 intel_enable_gt_powersave(dev);
Daniel Vetter79f5b2c2012-06-24 16:42:33 +02008851 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf8175862012-04-10 15:50:11 +02008852}
8853
Jesse Barnes79e53942008-11-07 14:24:08 -08008854void intel_modeset_init(struct drm_device *dev)
8855{
Jesse Barnes652c3932009-08-17 13:31:43 -07008856 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08008857 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008858
8859 drm_mode_config_init(dev);
8860
8861 dev->mode_config.min_width = 0;
8862 dev->mode_config.min_height = 0;
8863
Dave Airlie019d96c2011-09-29 16:20:42 +01008864 dev->mode_config.preferred_depth = 24;
8865 dev->mode_config.prefer_shadow = 1;
8866
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02008867 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -08008868
Jesse Barnesb690e962010-07-19 13:53:12 -07008869 intel_init_quirks(dev);
8870
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03008871 intel_init_pm(dev);
8872
Jesse Barnese70236a2009-09-21 10:42:27 -07008873 intel_init_display(dev);
8874
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008875 if (IS_GEN2(dev)) {
8876 dev->mode_config.max_width = 2048;
8877 dev->mode_config.max_height = 2048;
8878 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07008879 dev->mode_config.max_width = 4096;
8880 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08008881 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008882 dev->mode_config.max_width = 8192;
8883 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08008884 }
Ben Widawsky5d4545a2013-01-17 12:45:15 -08008885 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008886
Zhao Yakui28c97732009-10-09 11:39:41 +08008887 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10008888 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08008889
Dave Airliea3524f12010-06-06 18:59:41 +10008890 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008891 intel_crtc_init(dev, i);
Jesse Barnes00c2064b2012-01-13 15:48:39 -08008892 ret = intel_plane_init(dev, i);
8893 if (ret)
8894 DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08008895 }
8896
Paulo Zanoni79f689a2012-10-05 12:05:52 -03008897 intel_cpu_pll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008898 intel_pch_pll_init(dev);
8899
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008900 /* Just disable it once at startup */
8901 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08008902 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +00008903
8904 /* Just in case the BIOS is doing something questionable. */
8905 intel_disable_fbc(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01008906}
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008907
Daniel Vetter24929352012-07-02 20:28:59 +02008908static void
8909intel_connector_break_all_links(struct intel_connector *connector)
8910{
8911 connector->base.dpms = DRM_MODE_DPMS_OFF;
8912 connector->base.encoder = NULL;
8913 connector->encoder->connectors_active = false;
8914 connector->encoder->base.crtc = NULL;
8915}
8916
Daniel Vetter7fad7982012-07-04 17:51:47 +02008917static void intel_enable_pipe_a(struct drm_device *dev)
8918{
8919 struct intel_connector *connector;
8920 struct drm_connector *crt = NULL;
8921 struct intel_load_detect_pipe load_detect_temp;
8922
8923 /* We can't just switch on the pipe A, we need to set things up with a
8924 * proper mode and output configuration. As a gross hack, enable pipe A
8925 * by enabling the load detect pipe once. */
8926 list_for_each_entry(connector,
8927 &dev->mode_config.connector_list,
8928 base.head) {
8929 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
8930 crt = &connector->base;
8931 break;
8932 }
8933 }
8934
8935 if (!crt)
8936 return;
8937
8938 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
8939 intel_release_load_detect_pipe(crt, &load_detect_temp);
8940
8941
8942}
8943
Daniel Vetterfa555832012-10-10 23:14:00 +02008944static bool
8945intel_check_plane_mapping(struct intel_crtc *crtc)
8946{
8947 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
8948 u32 reg, val;
8949
8950 if (dev_priv->num_pipe == 1)
8951 return true;
8952
8953 reg = DSPCNTR(!crtc->plane);
8954 val = I915_READ(reg);
8955
8956 if ((val & DISPLAY_PLANE_ENABLE) &&
8957 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
8958 return false;
8959
8960 return true;
8961}
8962
Daniel Vetter24929352012-07-02 20:28:59 +02008963static void intel_sanitize_crtc(struct intel_crtc *crtc)
8964{
8965 struct drm_device *dev = crtc->base.dev;
8966 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +02008967 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +02008968
Daniel Vetter24929352012-07-02 20:28:59 +02008969 /* Clear any frame start delays used for debugging left by the BIOS */
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008970 reg = PIPECONF(crtc->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +02008971 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
8972
8973 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +02008974 * disable the crtc (and hence change the state) if it is wrong. Note
8975 * that gen4+ has a fixed plane -> pipe mapping. */
8976 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +02008977 struct intel_connector *connector;
8978 bool plane;
8979
Daniel Vetter24929352012-07-02 20:28:59 +02008980 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
8981 crtc->base.base.id);
8982
8983 /* Pipe has the wrong plane attached and the plane is active.
8984 * Temporarily change the plane mapping and disable everything
8985 * ... */
8986 plane = crtc->plane;
8987 crtc->plane = !plane;
8988 dev_priv->display.crtc_disable(&crtc->base);
8989 crtc->plane = plane;
8990
8991 /* ... and break all links. */
8992 list_for_each_entry(connector, &dev->mode_config.connector_list,
8993 base.head) {
8994 if (connector->encoder->base.crtc != &crtc->base)
8995 continue;
8996
8997 intel_connector_break_all_links(connector);
8998 }
8999
9000 WARN_ON(crtc->active);
9001 crtc->base.enabled = false;
9002 }
Daniel Vetter24929352012-07-02 20:28:59 +02009003
Daniel Vetter7fad7982012-07-04 17:51:47 +02009004 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
9005 crtc->pipe == PIPE_A && !crtc->active) {
9006 /* BIOS forgot to enable pipe A, this mostly happens after
9007 * resume. Force-enable the pipe to fix this, the update_dpms
9008 * call below we restore the pipe to the right state, but leave
9009 * the required bits on. */
9010 intel_enable_pipe_a(dev);
9011 }
9012
Daniel Vetter24929352012-07-02 20:28:59 +02009013 /* Adjust the state of the output pipe according to whether we
9014 * have active connectors/encoders. */
9015 intel_crtc_update_dpms(&crtc->base);
9016
9017 if (crtc->active != crtc->base.enabled) {
9018 struct intel_encoder *encoder;
9019
9020 /* This can happen either due to bugs in the get_hw_state
9021 * functions or because the pipe is force-enabled due to the
9022 * pipe A quirk. */
9023 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
9024 crtc->base.base.id,
9025 crtc->base.enabled ? "enabled" : "disabled",
9026 crtc->active ? "enabled" : "disabled");
9027
9028 crtc->base.enabled = crtc->active;
9029
9030 /* Because we only establish the connector -> encoder ->
9031 * crtc links if something is active, this means the
9032 * crtc is now deactivated. Break the links. connector
9033 * -> encoder links are only establish when things are
9034 * actually up, hence no need to break them. */
9035 WARN_ON(crtc->active);
9036
9037 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
9038 WARN_ON(encoder->connectors_active);
9039 encoder->base.crtc = NULL;
9040 }
9041 }
9042}
9043
9044static void intel_sanitize_encoder(struct intel_encoder *encoder)
9045{
9046 struct intel_connector *connector;
9047 struct drm_device *dev = encoder->base.dev;
9048
9049 /* We need to check both for a crtc link (meaning that the
9050 * encoder is active and trying to read from a pipe) and the
9051 * pipe itself being active. */
9052 bool has_active_crtc = encoder->base.crtc &&
9053 to_intel_crtc(encoder->base.crtc)->active;
9054
9055 if (encoder->connectors_active && !has_active_crtc) {
9056 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
9057 encoder->base.base.id,
9058 drm_get_encoder_name(&encoder->base));
9059
9060 /* Connector is active, but has no active pipe. This is
9061 * fallout from our resume register restoring. Disable
9062 * the encoder manually again. */
9063 if (encoder->base.crtc) {
9064 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
9065 encoder->base.base.id,
9066 drm_get_encoder_name(&encoder->base));
9067 encoder->disable(encoder);
9068 }
9069
9070 /* Inconsistent output/port/pipe state happens presumably due to
9071 * a bug in one of the get_hw_state functions. Or someplace else
9072 * in our code, like the register restore mess on resume. Clamp
9073 * things to off as a safer default. */
9074 list_for_each_entry(connector,
9075 &dev->mode_config.connector_list,
9076 base.head) {
9077 if (connector->encoder != encoder)
9078 continue;
9079
9080 intel_connector_break_all_links(connector);
9081 }
9082 }
9083 /* Enabled encoders without active connectors will be fixed in
9084 * the crtc fixup. */
9085}
9086
Daniel Vetter44cec742013-01-25 17:53:21 +01009087void i915_redisable_vga(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009088{
9089 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +02009090 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009091
9092 if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
9093 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
Ville Syrjälä209d5212013-01-25 21:44:48 +02009094 i915_disable_vga(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009095 }
9096}
9097
Daniel Vetter24929352012-07-02 20:28:59 +02009098/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
9099 * and i915 state tracking structures. */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009100void intel_modeset_setup_hw_state(struct drm_device *dev,
9101 bool force_restore)
Daniel Vetter24929352012-07-02 20:28:59 +02009102{
9103 struct drm_i915_private *dev_priv = dev->dev_private;
9104 enum pipe pipe;
9105 u32 tmp;
9106 struct intel_crtc *crtc;
9107 struct intel_encoder *encoder;
9108 struct intel_connector *connector;
9109
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009110 if (HAS_DDI(dev)) {
Paulo Zanonie28d54c2012-10-24 16:09:25 -02009111 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9112
9113 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9114 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9115 case TRANS_DDI_EDP_INPUT_A_ON:
9116 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9117 pipe = PIPE_A;
9118 break;
9119 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9120 pipe = PIPE_B;
9121 break;
9122 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9123 pipe = PIPE_C;
9124 break;
9125 }
9126
9127 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9128 crtc->cpu_transcoder = TRANSCODER_EDP;
9129
9130 DRM_DEBUG_KMS("Pipe %c using transcoder EDP\n",
9131 pipe_name(pipe));
9132 }
9133 }
9134
Daniel Vetter24929352012-07-02 20:28:59 +02009135 for_each_pipe(pipe) {
9136 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9137
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009138 tmp = I915_READ(PIPECONF(crtc->cpu_transcoder));
Daniel Vetter24929352012-07-02 20:28:59 +02009139 if (tmp & PIPECONF_ENABLE)
9140 crtc->active = true;
9141 else
9142 crtc->active = false;
9143
9144 crtc->base.enabled = crtc->active;
9145
9146 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
9147 crtc->base.base.id,
9148 crtc->active ? "enabled" : "disabled");
9149 }
9150
Paulo Zanoniaffa9352012-11-23 15:30:39 -02009151 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009152 intel_ddi_setup_hw_pll_state(dev);
9153
Daniel Vetter24929352012-07-02 20:28:59 +02009154 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9155 base.head) {
9156 pipe = 0;
9157
9158 if (encoder->get_hw_state(encoder, &pipe)) {
9159 encoder->base.crtc =
9160 dev_priv->pipe_to_crtc_mapping[pipe];
9161 } else {
9162 encoder->base.crtc = NULL;
9163 }
9164
9165 encoder->connectors_active = false;
9166 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
9167 encoder->base.base.id,
9168 drm_get_encoder_name(&encoder->base),
9169 encoder->base.crtc ? "enabled" : "disabled",
9170 pipe);
9171 }
9172
9173 list_for_each_entry(connector, &dev->mode_config.connector_list,
9174 base.head) {
9175 if (connector->get_hw_state(connector)) {
9176 connector->base.dpms = DRM_MODE_DPMS_ON;
9177 connector->encoder->connectors_active = true;
9178 connector->base.encoder = &connector->encoder->base;
9179 } else {
9180 connector->base.dpms = DRM_MODE_DPMS_OFF;
9181 connector->base.encoder = NULL;
9182 }
9183 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
9184 connector->base.base.id,
9185 drm_get_connector_name(&connector->base),
9186 connector->base.encoder ? "enabled" : "disabled");
9187 }
9188
9189 /* HW state is read out, now we need to sanitize this mess. */
9190 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9191 base.head) {
9192 intel_sanitize_encoder(encoder);
9193 }
9194
9195 for_each_pipe(pipe) {
9196 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
9197 intel_sanitize_crtc(crtc);
9198 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009199
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009200 if (force_restore) {
9201 for_each_pipe(pipe) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +00009202 intel_crtc_restore_mode(dev_priv->pipe_to_crtc_mapping[pipe]);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009203 }
Krzysztof Mazur0fde9012012-12-19 11:03:41 +01009204
9205 i915_redisable_vga(dev);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009206 } else {
9207 intel_modeset_update_staged_output_state(dev);
9208 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +02009209
9210 intel_modeset_check_state(dev);
Daniel Vetter2e938892012-10-11 20:08:24 +02009211
9212 drm_mode_config_reset(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009213}
9214
9215void intel_modeset_gem_init(struct drm_device *dev)
9216{
Chris Wilson1833b132012-05-09 11:56:28 +01009217 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02009218
9219 intel_setup_overlay(dev);
Daniel Vetter24929352012-07-02 20:28:59 +02009220
Daniel Vetter45e2b5f2012-11-23 18:16:34 +01009221 intel_modeset_setup_hw_state(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -08009222}
9223
9224void intel_modeset_cleanup(struct drm_device *dev)
9225{
Jesse Barnes652c3932009-08-17 13:31:43 -07009226 struct drm_i915_private *dev_priv = dev->dev_private;
9227 struct drm_crtc *crtc;
9228 struct intel_crtc *intel_crtc;
9229
Keith Packardf87ea762010-10-03 19:36:26 -07009230 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07009231 mutex_lock(&dev->struct_mutex);
9232
Jesse Barnes723bfd72010-10-07 16:01:13 -07009233 intel_unregister_dsm_handler();
9234
9235
Jesse Barnes652c3932009-08-17 13:31:43 -07009236 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9237 /* Skip inactive CRTCs */
9238 if (!crtc->fb)
9239 continue;
9240
9241 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02009242 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009243 }
9244
Chris Wilson973d04f2011-07-08 12:22:37 +01009245 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07009246
Daniel Vetter8090c6b2012-06-24 16:42:32 +02009247 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00009248
Daniel Vetter930ebb42012-06-29 23:32:16 +02009249 ironlake_teardown_rc6(dev);
9250
Jesse Barnes57f350b2012-03-28 13:39:25 -07009251 if (IS_VALLEYVIEW(dev))
9252 vlv_init_dpio(dev);
9253
Kristian Høgsberg69341a52009-11-11 12:19:17 -05009254 mutex_unlock(&dev->struct_mutex);
9255
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009256 /* Disable the irq before mode object teardown, for the irq might
9257 * enqueue unpin/hotplug work. */
9258 drm_irq_uninstall(dev);
9259 cancel_work_sync(&dev_priv->hotplug_work);
Daniel Vetterc6a828d2012-08-08 23:35:35 +02009260 cancel_work_sync(&dev_priv->rps.work);
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009261
Chris Wilson1630fe72011-07-08 12:22:42 +01009262 /* flush any delayed tasks or pending work */
9263 flush_scheduled_work();
9264
Jesse Barnes79e53942008-11-07 14:24:08 -08009265 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +01009266
9267 intel_cleanup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009268}
9269
Dave Airlie28d52042009-09-21 14:33:58 +10009270/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08009271 * Return which encoder is currently attached for connector.
9272 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01009273struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08009274{
Chris Wilsondf0e9242010-09-09 16:20:55 +01009275 return &intel_attached_encoder(connector)->base;
9276}
Jesse Barnes79e53942008-11-07 14:24:08 -08009277
Chris Wilsondf0e9242010-09-09 16:20:55 +01009278void intel_connector_attach_encoder(struct intel_connector *connector,
9279 struct intel_encoder *encoder)
9280{
9281 connector->encoder = encoder;
9282 drm_mode_connector_attach_encoder(&connector->base,
9283 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009284}
Dave Airlie28d52042009-09-21 14:33:58 +10009285
9286/*
9287 * set vga decode state - true == enable VGA decode
9288 */
9289int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9290{
9291 struct drm_i915_private *dev_priv = dev->dev_private;
9292 u16 gmch_ctrl;
9293
9294 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9295 if (state)
9296 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9297 else
9298 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9299 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9300 return 0;
9301}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009302
9303#ifdef CONFIG_DEBUG_FS
9304#include <linux/seq_file.h>
9305
9306struct intel_display_error_state {
9307 struct intel_cursor_error_state {
9308 u32 control;
9309 u32 position;
9310 u32 base;
9311 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +01009312 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009313
9314 struct intel_pipe_error_state {
9315 u32 conf;
9316 u32 source;
9317
9318 u32 htotal;
9319 u32 hblank;
9320 u32 hsync;
9321 u32 vtotal;
9322 u32 vblank;
9323 u32 vsync;
Damien Lespiau52331302012-08-15 19:23:25 +01009324 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009325
9326 struct intel_plane_error_state {
9327 u32 control;
9328 u32 stride;
9329 u32 size;
9330 u32 pos;
9331 u32 addr;
9332 u32 surface;
9333 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +01009334 } plane[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009335};
9336
9337struct intel_display_error_state *
9338intel_display_capture_error_state(struct drm_device *dev)
9339{
Akshay Joshi0206e352011-08-16 15:34:10 -04009340 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009341 struct intel_display_error_state *error;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009342 enum transcoder cpu_transcoder;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009343 int i;
9344
9345 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9346 if (error == NULL)
9347 return NULL;
9348
Damien Lespiau52331302012-08-15 19:23:25 +01009349 for_each_pipe(i) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009350 cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
9351
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009352 error->cursor[i].control = I915_READ(CURCNTR(i));
9353 error->cursor[i].position = I915_READ(CURPOS(i));
9354 error->cursor[i].base = I915_READ(CURBASE(i));
9355
9356 error->plane[i].control = I915_READ(DSPCNTR(i));
9357 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
9358 error->plane[i].size = I915_READ(DSPSIZE(i));
Akshay Joshi0206e352011-08-16 15:34:10 -04009359 error->plane[i].pos = I915_READ(DSPPOS(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009360 error->plane[i].addr = I915_READ(DSPADDR(i));
9361 if (INTEL_INFO(dev)->gen >= 4) {
9362 error->plane[i].surface = I915_READ(DSPSURF(i));
9363 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9364 }
9365
Paulo Zanoni702e7a52012-10-23 18:29:59 -02009366 error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009367 error->pipe[i].source = I915_READ(PIPESRC(i));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009368 error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
9369 error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
9370 error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
9371 error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
9372 error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
9373 error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009374 }
9375
9376 return error;
9377}
9378
9379void
9380intel_display_print_error_state(struct seq_file *m,
9381 struct drm_device *dev,
9382 struct intel_display_error_state *error)
9383{
Damien Lespiau52331302012-08-15 19:23:25 +01009384 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009385 int i;
9386
Damien Lespiau52331302012-08-15 19:23:25 +01009387 seq_printf(m, "Num Pipes: %d\n", dev_priv->num_pipe);
9388 for_each_pipe(i) {
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009389 seq_printf(m, "Pipe [%d]:\n", i);
9390 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9391 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
9392 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9393 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9394 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9395 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9396 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9397 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9398
9399 seq_printf(m, "Plane [%d]:\n", i);
9400 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
9401 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
9402 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
9403 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
9404 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
9405 if (INTEL_INFO(dev)->gen >= 4) {
9406 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9407 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9408 }
9409
9410 seq_printf(m, "Cursor [%d]:\n", i);
9411 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9412 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9413 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
9414 }
9415}
9416#endif