blob: 7d59d0e852f8739ba94776caba180fa352a51ea9 [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>
Jesse Barnes79e53942008-11-07 14:24:08 -080035#include "drmP.h"
36#include "intel_drv.h"
37#include "i915_drm.h"
38#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
Dave Airlieab2c0672009-12-04 10:55:24 +100040#include "drm_dp_helper.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080041#include "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
Zhenyu Wang32f9d652009-07-24 01:00:32 +080044#define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
45
Akshay Joshi0206e352011-08-16 15:34:10 -040046bool intel_pipe_has_type(struct drm_crtc *crtc, int type);
Daniel Vetter3dec0092010-08-20 21:40:52 +020047static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010048static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080049
50typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040051 /* given values */
52 int n;
53 int m1, m2;
54 int p1, p2;
55 /* derived values */
56 int dot;
57 int vco;
58 int m;
59 int p;
Jesse Barnes79e53942008-11-07 14:24:08 -080060} intel_clock_t;
61
62typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040063 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080064} intel_range_t;
65
66typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040067 int dot_limit;
68 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080069} intel_p2_t;
70
71#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080072typedef struct intel_limit intel_limit_t;
73struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040074 intel_range_t dot, vco, n, m, m1, m2, p, p1;
75 intel_p2_t p2;
76 bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
Sean Paulcec2f352012-01-10 15:09:36 -080077 int, int, intel_clock_t *, intel_clock_t *);
Ma Lingd4906092009-03-18 20:13:27 +080078};
Jesse Barnes79e53942008-11-07 14:24:08 -080079
Jesse Barnes2377b742010-07-07 14:06:43 -070080/* FDI */
81#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
82
Ma Lingd4906092009-03-18 20:13:27 +080083static bool
84intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080085 int target, int refclk, intel_clock_t *match_clock,
86 intel_clock_t *best_clock);
Ma Lingd4906092009-03-18 20:13:27 +080087static bool
88intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080089 int target, int refclk, intel_clock_t *match_clock,
90 intel_clock_t *best_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080091
Keith Packarda4fc5ed2009-04-07 16:16:42 -070092static bool
93intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080094 int target, int refclk, intel_clock_t *match_clock,
95 intel_clock_t *best_clock);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080096static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -050097intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080098 int target, int refclk, intel_clock_t *match_clock,
99 intel_clock_t *best_clock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700100
Chris Wilson021357a2010-09-07 20:54:59 +0100101static inline u32 /* units of 100MHz */
102intel_fdi_link_freq(struct drm_device *dev)
103{
Chris Wilson8b99e682010-10-13 09:59:17 +0100104 if (IS_GEN5(dev)) {
105 struct drm_i915_private *dev_priv = dev->dev_private;
106 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
107 } else
108 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100109}
110
Keith Packarde4b36692009-06-05 19:22:17 -0700111static const intel_limit_t intel_limits_i8xx_dvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400112 .dot = { .min = 25000, .max = 350000 },
113 .vco = { .min = 930000, .max = 1400000 },
114 .n = { .min = 3, .max = 16 },
115 .m = { .min = 96, .max = 140 },
116 .m1 = { .min = 18, .max = 26 },
117 .m2 = { .min = 6, .max = 16 },
118 .p = { .min = 4, .max = 128 },
119 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700120 .p2 = { .dot_limit = 165000,
121 .p2_slow = 4, .p2_fast = 2 },
Ma Lingd4906092009-03-18 20:13:27 +0800122 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700123};
124
125static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400126 .dot = { .min = 25000, .max = 350000 },
127 .vco = { .min = 930000, .max = 1400000 },
128 .n = { .min = 3, .max = 16 },
129 .m = { .min = 96, .max = 140 },
130 .m1 = { .min = 18, .max = 26 },
131 .m2 = { .min = 6, .max = 16 },
132 .p = { .min = 4, .max = 128 },
133 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700134 .p2 = { .dot_limit = 165000,
135 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800136 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700137};
Eric Anholt273e27c2011-03-30 13:01:10 -0700138
Keith Packarde4b36692009-06-05 19:22:17 -0700139static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400140 .dot = { .min = 20000, .max = 400000 },
141 .vco = { .min = 1400000, .max = 2800000 },
142 .n = { .min = 1, .max = 6 },
143 .m = { .min = 70, .max = 120 },
144 .m1 = { .min = 10, .max = 22 },
145 .m2 = { .min = 5, .max = 9 },
146 .p = { .min = 5, .max = 80 },
147 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700148 .p2 = { .dot_limit = 200000,
149 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800150 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700151};
152
153static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400154 .dot = { .min = 20000, .max = 400000 },
155 .vco = { .min = 1400000, .max = 2800000 },
156 .n = { .min = 1, .max = 6 },
157 .m = { .min = 70, .max = 120 },
158 .m1 = { .min = 10, .max = 22 },
159 .m2 = { .min = 5, .max = 9 },
160 .p = { .min = 7, .max = 98 },
161 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700162 .p2 = { .dot_limit = 112000,
163 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800164 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700165};
166
Eric Anholt273e27c2011-03-30 13:01:10 -0700167
Keith Packarde4b36692009-06-05 19:22:17 -0700168static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700169 .dot = { .min = 25000, .max = 270000 },
170 .vco = { .min = 1750000, .max = 3500000},
171 .n = { .min = 1, .max = 4 },
172 .m = { .min = 104, .max = 138 },
173 .m1 = { .min = 17, .max = 23 },
174 .m2 = { .min = 5, .max = 11 },
175 .p = { .min = 10, .max = 30 },
176 .p1 = { .min = 1, .max = 3},
177 .p2 = { .dot_limit = 270000,
178 .p2_slow = 10,
179 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800180 },
Ma Lingd4906092009-03-18 20:13:27 +0800181 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700182};
183
184static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700185 .dot = { .min = 22000, .max = 400000 },
186 .vco = { .min = 1750000, .max = 3500000},
187 .n = { .min = 1, .max = 4 },
188 .m = { .min = 104, .max = 138 },
189 .m1 = { .min = 16, .max = 23 },
190 .m2 = { .min = 5, .max = 11 },
191 .p = { .min = 5, .max = 80 },
192 .p1 = { .min = 1, .max = 8},
193 .p2 = { .dot_limit = 165000,
194 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800195 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700196};
197
198static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700199 .dot = { .min = 20000, .max = 115000 },
200 .vco = { .min = 1750000, .max = 3500000 },
201 .n = { .min = 1, .max = 3 },
202 .m = { .min = 104, .max = 138 },
203 .m1 = { .min = 17, .max = 23 },
204 .m2 = { .min = 5, .max = 11 },
205 .p = { .min = 28, .max = 112 },
206 .p1 = { .min = 2, .max = 8 },
207 .p2 = { .dot_limit = 0,
208 .p2_slow = 14, .p2_fast = 14
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_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700214 .dot = { .min = 80000, .max = 224000 },
215 .vco = { .min = 1750000, .max = 3500000 },
216 .n = { .min = 1, .max = 3 },
217 .m = { .min = 104, .max = 138 },
218 .m1 = { .min = 17, .max = 23 },
219 .m2 = { .min = 5, .max = 11 },
220 .p = { .min = 14, .max = 42 },
221 .p1 = { .min = 2, .max = 6 },
222 .p2 = { .dot_limit = 0,
223 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800224 },
Ma Lingd4906092009-03-18 20:13:27 +0800225 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700226};
227
228static const intel_limit_t intel_limits_g4x_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400229 .dot = { .min = 161670, .max = 227000 },
230 .vco = { .min = 1750000, .max = 3500000},
231 .n = { .min = 1, .max = 2 },
232 .m = { .min = 97, .max = 108 },
233 .m1 = { .min = 0x10, .max = 0x12 },
234 .m2 = { .min = 0x05, .max = 0x06 },
235 .p = { .min = 10, .max = 20 },
236 .p1 = { .min = 1, .max = 2},
237 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700238 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400239 .find_pll = intel_find_pll_g4x_dp,
Keith Packarde4b36692009-06-05 19:22:17 -0700240};
241
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500242static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400243 .dot = { .min = 20000, .max = 400000},
244 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700245 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400246 .n = { .min = 3, .max = 6 },
247 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700248 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400249 .m1 = { .min = 0, .max = 0 },
250 .m2 = { .min = 0, .max = 254 },
251 .p = { .min = 5, .max = 80 },
252 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700253 .p2 = { .dot_limit = 200000,
254 .p2_slow = 10, .p2_fast = 5 },
Shaohua Li61157072009-04-03 15:24:43 +0800255 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700256};
257
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500258static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400259 .dot = { .min = 20000, .max = 400000 },
260 .vco = { .min = 1700000, .max = 3500000 },
261 .n = { .min = 3, .max = 6 },
262 .m = { .min = 2, .max = 256 },
263 .m1 = { .min = 0, .max = 0 },
264 .m2 = { .min = 0, .max = 254 },
265 .p = { .min = 7, .max = 112 },
266 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700267 .p2 = { .dot_limit = 112000,
268 .p2_slow = 14, .p2_fast = 14 },
Shaohua Li61157072009-04-03 15:24:43 +0800269 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700270};
271
Eric Anholt273e27c2011-03-30 13:01:10 -0700272/* Ironlake / Sandybridge
273 *
274 * We calculate clock using (register_value + 2) for N/M1/M2, so here
275 * the range value for them is (actual_value - 2).
276 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800277static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700278 .dot = { .min = 25000, .max = 350000 },
279 .vco = { .min = 1760000, .max = 3510000 },
280 .n = { .min = 1, .max = 5 },
281 .m = { .min = 79, .max = 127 },
282 .m1 = { .min = 12, .max = 22 },
283 .m2 = { .min = 5, .max = 9 },
284 .p = { .min = 5, .max = 80 },
285 .p1 = { .min = 1, .max = 8 },
286 .p2 = { .dot_limit = 225000,
287 .p2_slow = 10, .p2_fast = 5 },
Zhao Yakui45476682009-12-31 16:06:04 +0800288 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700289};
290
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800291static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700292 .dot = { .min = 25000, .max = 350000 },
293 .vco = { .min = 1760000, .max = 3510000 },
294 .n = { .min = 1, .max = 3 },
295 .m = { .min = 79, .max = 118 },
296 .m1 = { .min = 12, .max = 22 },
297 .m2 = { .min = 5, .max = 9 },
298 .p = { .min = 28, .max = 112 },
299 .p1 = { .min = 2, .max = 8 },
300 .p2 = { .dot_limit = 225000,
301 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800302 .find_pll = intel_g4x_find_best_PLL,
303};
304
305static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700306 .dot = { .min = 25000, .max = 350000 },
307 .vco = { .min = 1760000, .max = 3510000 },
308 .n = { .min = 1, .max = 3 },
309 .m = { .min = 79, .max = 127 },
310 .m1 = { .min = 12, .max = 22 },
311 .m2 = { .min = 5, .max = 9 },
312 .p = { .min = 14, .max = 56 },
313 .p1 = { .min = 2, .max = 8 },
314 .p2 = { .dot_limit = 225000,
315 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800316 .find_pll = intel_g4x_find_best_PLL,
317};
318
Eric Anholt273e27c2011-03-30 13:01:10 -0700319/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800320static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700321 .dot = { .min = 25000, .max = 350000 },
322 .vco = { .min = 1760000, .max = 3510000 },
323 .n = { .min = 1, .max = 2 },
324 .m = { .min = 79, .max = 126 },
325 .m1 = { .min = 12, .max = 22 },
326 .m2 = { .min = 5, .max = 9 },
327 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400328 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700329 .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_100m = {
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 = 126 },
339 .m1 = { .min = 12, .max = 22 },
340 .m2 = { .min = 5, .max = 9 },
341 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400342 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700343 .p2 = { .dot_limit = 225000,
344 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800345 .find_pll = intel_g4x_find_best_PLL,
346};
347
348static const intel_limit_t intel_limits_ironlake_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400349 .dot = { .min = 25000, .max = 350000 },
350 .vco = { .min = 1760000, .max = 3510000},
351 .n = { .min = 1, .max = 2 },
352 .m = { .min = 81, .max = 90 },
353 .m1 = { .min = 12, .max = 22 },
354 .m2 = { .min = 5, .max = 9 },
355 .p = { .min = 10, .max = 20 },
356 .p1 = { .min = 1, .max = 2},
357 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700358 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400359 .find_pll = intel_find_pll_ironlake_dp,
Jesse Barnes79e53942008-11-07 14:24:08 -0800360};
361
Jesse Barnes57f350b2012-03-28 13:39:25 -0700362u32 intel_dpio_read(struct drm_i915_private *dev_priv, int reg)
363{
364 unsigned long flags;
365 u32 val = 0;
366
367 spin_lock_irqsave(&dev_priv->dpio_lock, flags);
368 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
369 DRM_ERROR("DPIO idle wait timed out\n");
370 goto out_unlock;
371 }
372
373 I915_WRITE(DPIO_REG, reg);
374 I915_WRITE(DPIO_PKT, DPIO_RID | DPIO_OP_READ | DPIO_PORTID |
375 DPIO_BYTE);
376 if (wait_for_atomic_us((I915_READ(DPIO_PKT) & DPIO_BUSY) == 0, 100)) {
377 DRM_ERROR("DPIO read wait timed out\n");
378 goto out_unlock;
379 }
380 val = I915_READ(DPIO_DATA);
381
382out_unlock:
383 spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
384 return val;
385}
386
Jesse Barnes57f350b2012-03-28 13:39:25 -0700387static void vlv_init_dpio(struct drm_device *dev)
388{
389 struct drm_i915_private *dev_priv = dev->dev_private;
390
391 /* Reset the DPIO config */
392 I915_WRITE(DPIO_CTL, 0);
393 POSTING_READ(DPIO_CTL);
394 I915_WRITE(DPIO_CTL, 1);
395 POSTING_READ(DPIO_CTL);
396}
397
Daniel Vetter618563e2012-04-01 13:38:50 +0200398static int intel_dual_link_lvds_callback(const struct dmi_system_id *id)
399{
400 DRM_INFO("Forcing lvds to dual link mode on %s\n", id->ident);
401 return 1;
402}
403
404static const struct dmi_system_id intel_dual_link_lvds[] = {
405 {
406 .callback = intel_dual_link_lvds_callback,
407 .ident = "Apple MacBook Pro (Core i5/i7 Series)",
408 .matches = {
409 DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
410 DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro8,2"),
411 },
412 },
413 { } /* terminating entry */
414};
415
Takashi Iwaib0354382012-03-20 13:07:05 +0100416static bool is_dual_link_lvds(struct drm_i915_private *dev_priv,
417 unsigned int reg)
418{
419 unsigned int val;
420
Takashi Iwai121d5272012-03-20 13:07:06 +0100421 /* use the module option value if specified */
422 if (i915_lvds_channel_mode > 0)
423 return i915_lvds_channel_mode == 2;
424
Daniel Vetter618563e2012-04-01 13:38:50 +0200425 if (dmi_check_system(intel_dual_link_lvds))
426 return true;
427
Takashi Iwaib0354382012-03-20 13:07:05 +0100428 if (dev_priv->lvds_val)
429 val = dev_priv->lvds_val;
430 else {
431 /* BIOS should set the proper LVDS register value at boot, but
432 * in reality, it doesn't set the value when the lid is closed;
433 * we need to check "the value to be set" in VBT when LVDS
434 * register is uninitialized.
435 */
436 val = I915_READ(reg);
437 if (!(val & ~LVDS_DETECTED))
438 val = dev_priv->bios_lvds_val;
439 dev_priv->lvds_val = val;
440 }
441 return (val & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP;
442}
443
Chris Wilson1b894b52010-12-14 20:04:54 +0000444static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
445 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800446{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800447 struct drm_device *dev = crtc->dev;
448 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800449 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800450
451 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Takashi Iwaib0354382012-03-20 13:07:05 +0100452 if (is_dual_link_lvds(dev_priv, PCH_LVDS)) {
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800453 /* LVDS dual channel */
Chris Wilson1b894b52010-12-14 20:04:54 +0000454 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800455 limit = &intel_limits_ironlake_dual_lvds_100m;
456 else
457 limit = &intel_limits_ironlake_dual_lvds;
458 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000459 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800460 limit = &intel_limits_ironlake_single_lvds_100m;
461 else
462 limit = &intel_limits_ironlake_single_lvds;
463 }
464 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
Zhao Yakui45476682009-12-31 16:06:04 +0800465 HAS_eDP)
466 limit = &intel_limits_ironlake_display_port;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800467 else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800468 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800469
470 return limit;
471}
472
Ma Ling044c7c42009-03-18 20:13:23 +0800473static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
474{
475 struct drm_device *dev = crtc->dev;
476 struct drm_i915_private *dev_priv = dev->dev_private;
477 const intel_limit_t *limit;
478
479 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Takashi Iwaib0354382012-03-20 13:07:05 +0100480 if (is_dual_link_lvds(dev_priv, LVDS))
Ma Ling044c7c42009-03-18 20:13:23 +0800481 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700482 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800483 else
484 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700485 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800486 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
487 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700488 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800489 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700490 limit = &intel_limits_g4x_sdvo;
Akshay Joshi0206e352011-08-16 15:34:10 -0400491 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700492 limit = &intel_limits_g4x_display_port;
Ma Ling044c7c42009-03-18 20:13:23 +0800493 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700494 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800495
496 return limit;
497}
498
Chris Wilson1b894b52010-12-14 20:04:54 +0000499static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800500{
501 struct drm_device *dev = crtc->dev;
502 const intel_limit_t *limit;
503
Eric Anholtbad720f2009-10-22 16:11:14 -0700504 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000505 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800506 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800507 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500508 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800509 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500510 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800511 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500512 limit = &intel_limits_pineview_sdvo;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100513 } else if (!IS_GEN2(dev)) {
514 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
515 limit = &intel_limits_i9xx_lvds;
516 else
517 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800518 } else {
519 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700520 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800521 else
Keith Packarde4b36692009-06-05 19:22:17 -0700522 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800523 }
524 return limit;
525}
526
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500527/* m1 is reserved as 0 in Pineview, n is a ring counter */
528static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800529{
Shaohua Li21778322009-02-23 15:19:16 +0800530 clock->m = clock->m2 + 2;
531 clock->p = clock->p1 * clock->p2;
532 clock->vco = refclk * clock->m / clock->n;
533 clock->dot = clock->vco / clock->p;
534}
535
536static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
537{
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500538 if (IS_PINEVIEW(dev)) {
539 pineview_clock(refclk, clock);
Shaohua Li21778322009-02-23 15:19:16 +0800540 return;
541 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800542 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
543 clock->p = clock->p1 * clock->p2;
544 clock->vco = refclk * clock->m / (clock->n + 2);
545 clock->dot = clock->vco / clock->p;
546}
547
Jesse Barnes79e53942008-11-07 14:24:08 -0800548/**
549 * Returns whether any output on the specified pipe is of the specified type
550 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100551bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800552{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100553 struct drm_device *dev = crtc->dev;
554 struct drm_mode_config *mode_config = &dev->mode_config;
555 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800556
Chris Wilson4ef69c72010-09-09 15:14:28 +0100557 list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
558 if (encoder->base.crtc == crtc && encoder->type == type)
559 return true;
560
561 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800562}
563
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800564#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800565/**
566 * Returns whether the given set of divisors are valid for a given refclk with
567 * the given connectors.
568 */
569
Chris Wilson1b894b52010-12-14 20:04:54 +0000570static bool intel_PLL_is_valid(struct drm_device *dev,
571 const intel_limit_t *limit,
572 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800573{
Jesse Barnes79e53942008-11-07 14:24:08 -0800574 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400575 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800576 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400577 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800578 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400579 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800580 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400581 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500582 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400583 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800584 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400585 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800586 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400587 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800588 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400589 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800590 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
591 * connector, etc., rather than just a single range.
592 */
593 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400594 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800595
596 return true;
597}
598
Ma Lingd4906092009-03-18 20:13:27 +0800599static bool
600intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800601 int target, int refclk, intel_clock_t *match_clock,
602 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800603
Jesse Barnes79e53942008-11-07 14:24:08 -0800604{
605 struct drm_device *dev = crtc->dev;
606 struct drm_i915_private *dev_priv = dev->dev_private;
607 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800608 int err = target;
609
Bruno Prémontbc5e5712009-08-08 13:01:17 +0200610 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Florian Mickler832cc282009-07-13 18:40:32 +0800611 (I915_READ(LVDS)) != 0) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 /*
613 * For LVDS, if the panel is on, just rely on its current
614 * settings for dual-channel. We haven't figured out how to
615 * reliably set up different single/dual channel state, if we
616 * even can.
617 */
Takashi Iwaib0354382012-03-20 13:07:05 +0100618 if (is_dual_link_lvds(dev_priv, LVDS))
Jesse Barnes79e53942008-11-07 14:24:08 -0800619 clock.p2 = limit->p2.p2_fast;
620 else
621 clock.p2 = limit->p2.p2_slow;
622 } else {
623 if (target < limit->p2.dot_limit)
624 clock.p2 = limit->p2.p2_slow;
625 else
626 clock.p2 = limit->p2.p2_fast;
627 }
628
Akshay Joshi0206e352011-08-16 15:34:10 -0400629 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800630
Zhao Yakui42158662009-11-20 11:24:18 +0800631 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
632 clock.m1++) {
633 for (clock.m2 = limit->m2.min;
634 clock.m2 <= limit->m2.max; clock.m2++) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500635 /* m1 is always 0 in Pineview */
636 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
Zhao Yakui42158662009-11-20 11:24:18 +0800637 break;
638 for (clock.n = limit->n.min;
639 clock.n <= limit->n.max; clock.n++) {
640 for (clock.p1 = limit->p1.min;
641 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800642 int this_err;
643
Shaohua Li21778322009-02-23 15:19:16 +0800644 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000645 if (!intel_PLL_is_valid(dev, limit,
646 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800647 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800648 if (match_clock &&
649 clock.p != match_clock->p)
650 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800651
652 this_err = abs(clock.dot - target);
653 if (this_err < err) {
654 *best_clock = clock;
655 err = this_err;
656 }
657 }
658 }
659 }
660 }
661
662 return (err != target);
663}
664
Ma Lingd4906092009-03-18 20:13:27 +0800665static bool
666intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800667 int target, int refclk, intel_clock_t *match_clock,
668 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800669{
670 struct drm_device *dev = crtc->dev;
671 struct drm_i915_private *dev_priv = dev->dev_private;
672 intel_clock_t clock;
673 int max_n;
674 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400675 /* approximately equals target * 0.00585 */
676 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800677 found = false;
678
679 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhao Yakui45476682009-12-31 16:06:04 +0800680 int lvds_reg;
681
Eric Anholtc619eed2010-01-28 16:45:52 -0800682 if (HAS_PCH_SPLIT(dev))
Zhao Yakui45476682009-12-31 16:06:04 +0800683 lvds_reg = PCH_LVDS;
684 else
685 lvds_reg = LVDS;
686 if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
Ma Lingd4906092009-03-18 20:13:27 +0800687 LVDS_CLKB_POWER_UP)
688 clock.p2 = limit->p2.p2_fast;
689 else
690 clock.p2 = limit->p2.p2_slow;
691 } else {
692 if (target < limit->p2.dot_limit)
693 clock.p2 = limit->p2.p2_slow;
694 else
695 clock.p2 = limit->p2.p2_fast;
696 }
697
698 memset(best_clock, 0, sizeof(*best_clock));
699 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200700 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800701 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200702 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800703 for (clock.m1 = limit->m1.max;
704 clock.m1 >= limit->m1.min; clock.m1--) {
705 for (clock.m2 = limit->m2.max;
706 clock.m2 >= limit->m2.min; clock.m2--) {
707 for (clock.p1 = limit->p1.max;
708 clock.p1 >= limit->p1.min; clock.p1--) {
709 int this_err;
710
Shaohua Li21778322009-02-23 15:19:16 +0800711 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000712 if (!intel_PLL_is_valid(dev, limit,
713 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800714 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800715 if (match_clock &&
716 clock.p != match_clock->p)
717 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000718
719 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800720 if (this_err < err_most) {
721 *best_clock = clock;
722 err_most = this_err;
723 max_n = clock.n;
724 found = true;
725 }
726 }
727 }
728 }
729 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800730 return found;
731}
Ma Lingd4906092009-03-18 20:13:27 +0800732
Zhenyu Wang2c072452009-06-05 15:38:42 +0800733static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500734intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800735 int target, int refclk, intel_clock_t *match_clock,
736 intel_clock_t *best_clock)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800737{
738 struct drm_device *dev = crtc->dev;
739 intel_clock_t clock;
Zhao Yakui45476682009-12-31 16:06:04 +0800740
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800741 if (target < 200000) {
742 clock.n = 1;
743 clock.p1 = 2;
744 clock.p2 = 10;
745 clock.m1 = 12;
746 clock.m2 = 9;
747 } else {
748 clock.n = 2;
749 clock.p1 = 1;
750 clock.p2 = 10;
751 clock.m1 = 14;
752 clock.m2 = 8;
753 }
754 intel_clock(dev, refclk, &clock);
755 memcpy(best_clock, &clock, sizeof(intel_clock_t));
756 return true;
757}
758
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700759/* DisplayPort has only two frequencies, 162MHz and 270MHz */
760static bool
761intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800762 int target, int refclk, intel_clock_t *match_clock,
763 intel_clock_t *best_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700764{
Chris Wilson5eddb702010-09-11 13:48:45 +0100765 intel_clock_t clock;
766 if (target < 200000) {
767 clock.p1 = 2;
768 clock.p2 = 10;
769 clock.n = 2;
770 clock.m1 = 23;
771 clock.m2 = 8;
772 } else {
773 clock.p1 = 1;
774 clock.p2 = 10;
775 clock.n = 1;
776 clock.m1 = 14;
777 clock.m2 = 2;
778 }
779 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
780 clock.p = (clock.p1 * clock.p2);
781 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
782 clock.vco = 0;
783 memcpy(best_clock, &clock, sizeof(intel_clock_t));
784 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700785}
786
Paulo Zanonia928d532012-05-04 17:18:15 -0300787static void ironlake_wait_for_vblank(struct drm_device *dev, int pipe)
788{
789 struct drm_i915_private *dev_priv = dev->dev_private;
790 u32 frame, frame_reg = PIPEFRAME(pipe);
791
792 frame = I915_READ(frame_reg);
793
794 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
795 DRM_DEBUG_KMS("vblank wait timed out\n");
796}
797
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700798/**
799 * intel_wait_for_vblank - wait for vblank on a given pipe
800 * @dev: drm device
801 * @pipe: pipe to wait for
802 *
803 * Wait for vblank to occur on a given pipe. Needed for various bits of
804 * mode setting code.
805 */
806void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800807{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700808 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800809 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700810
Paulo Zanonia928d532012-05-04 17:18:15 -0300811 if (INTEL_INFO(dev)->gen >= 5) {
812 ironlake_wait_for_vblank(dev, pipe);
813 return;
814 }
815
Chris Wilson300387c2010-09-05 20:25:43 +0100816 /* Clear existing vblank status. Note this will clear any other
817 * sticky status fields as well.
818 *
819 * This races with i915_driver_irq_handler() with the result
820 * that either function could miss a vblank event. Here it is not
821 * fatal, as we will either wait upon the next vblank interrupt or
822 * timeout. Generally speaking intel_wait_for_vblank() is only
823 * called during modeset at which time the GPU should be idle and
824 * should *not* be performing page flips and thus not waiting on
825 * vblanks...
826 * Currently, the result of us stealing a vblank from the irq
827 * handler is that a single frame will be skipped during swapbuffers.
828 */
829 I915_WRITE(pipestat_reg,
830 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
831
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700832 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100833 if (wait_for(I915_READ(pipestat_reg) &
834 PIPE_VBLANK_INTERRUPT_STATUS,
835 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700836 DRM_DEBUG_KMS("vblank wait timed out\n");
837}
838
Keith Packardab7ad7f2010-10-03 00:33:06 -0700839/*
840 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700841 * @dev: drm device
842 * @pipe: pipe to wait for
843 *
844 * After disabling a pipe, we can't wait for vblank in the usual way,
845 * spinning on the vblank interrupt status bit, since we won't actually
846 * see an interrupt when the pipe is disabled.
847 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700848 * On Gen4 and above:
849 * wait for the pipe register state bit to turn off
850 *
851 * Otherwise:
852 * wait for the display line value to settle (it usually
853 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100854 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700855 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100856void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700857{
858 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700859
Keith Packardab7ad7f2010-10-03 00:33:06 -0700860 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson58e10eb2010-10-03 10:56:11 +0100861 int reg = PIPECONF(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700862
Keith Packardab7ad7f2010-10-03 00:33:06 -0700863 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100864 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
865 100))
Keith Packardab7ad7f2010-10-03 00:33:06 -0700866 DRM_DEBUG_KMS("pipe_off wait timed out\n");
867 } else {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300868 u32 last_line, line_mask;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100869 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700870 unsigned long timeout = jiffies + msecs_to_jiffies(100);
871
Paulo Zanoni837ba002012-05-04 17:18:14 -0300872 if (IS_GEN2(dev))
873 line_mask = DSL_LINEMASK_GEN2;
874 else
875 line_mask = DSL_LINEMASK_GEN3;
876
Keith Packardab7ad7f2010-10-03 00:33:06 -0700877 /* Wait for the display line to settle */
878 do {
Paulo Zanoni837ba002012-05-04 17:18:14 -0300879 last_line = I915_READ(reg) & line_mask;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700880 mdelay(5);
Paulo Zanoni837ba002012-05-04 17:18:14 -0300881 } while (((I915_READ(reg) & line_mask) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700882 time_after(timeout, jiffies));
883 if (time_after(jiffies, timeout))
884 DRM_DEBUG_KMS("pipe_off wait timed out\n");
885 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800886}
887
Jesse Barnesb24e7172011-01-04 15:09:30 -0800888static const char *state_string(bool enabled)
889{
890 return enabled ? "on" : "off";
891}
892
893/* Only for pre-ILK configs */
894static void assert_pll(struct drm_i915_private *dev_priv,
895 enum pipe pipe, bool state)
896{
897 int reg;
898 u32 val;
899 bool cur_state;
900
901 reg = DPLL(pipe);
902 val = I915_READ(reg);
903 cur_state = !!(val & DPLL_VCO_ENABLE);
904 WARN(cur_state != state,
905 "PLL state assertion failure (expected %s, current %s)\n",
906 state_string(state), state_string(cur_state));
907}
908#define assert_pll_enabled(d, p) assert_pll(d, p, true)
909#define assert_pll_disabled(d, p) assert_pll(d, p, false)
910
Jesse Barnes040484a2011-01-03 12:14:26 -0800911/* For ILK+ */
912static void assert_pch_pll(struct drm_i915_private *dev_priv,
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100913 struct intel_crtc *intel_crtc, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -0800914{
915 int reg;
916 u32 val;
917 bool cur_state;
918
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -0300919 if (HAS_PCH_LPT(dev_priv->dev)) {
920 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
921 return;
922 }
923
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100924 if (!intel_crtc->pch_pll) {
925 WARN(1, "asserting PCH PLL enabled with no PLL\n");
926 return;
927 }
928
Jesse Barnesd3ccbe82011-10-12 09:27:42 -0700929 if (HAS_PCH_CPT(dev_priv->dev)) {
930 u32 pch_dpll;
931
932 pch_dpll = I915_READ(PCH_DPLL_SEL);
933
934 /* Make sure the selected PLL is enabled to the transcoder */
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100935 WARN(!((pch_dpll >> (4 * intel_crtc->pipe)) & 8),
936 "transcoder %d PLL not enabled\n", intel_crtc->pipe);
Jesse Barnesd3ccbe82011-10-12 09:27:42 -0700937 }
938
Jesse Barnesee7b9f92012-04-20 17:11:53 +0100939 reg = intel_crtc->pch_pll->pll_reg;
Jesse Barnes040484a2011-01-03 12:14:26 -0800940 val = I915_READ(reg);
941 cur_state = !!(val & DPLL_VCO_ENABLE);
942 WARN(cur_state != state,
943 "PCH PLL state assertion failure (expected %s, current %s)\n",
944 state_string(state), state_string(cur_state));
945}
946#define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
947#define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
948
949static void assert_fdi_tx(struct drm_i915_private *dev_priv,
950 enum pipe pipe, bool state)
951{
952 int reg;
953 u32 val;
954 bool cur_state;
955
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -0300956 if (IS_HASWELL(dev_priv->dev)) {
957 /* On Haswell, DDI is used instead of FDI_TX_CTL */
958 reg = DDI_FUNC_CTL(pipe);
959 val = I915_READ(reg);
960 cur_state = !!(val & PIPE_DDI_FUNC_ENABLE);
961 } else {
962 reg = FDI_TX_CTL(pipe);
963 val = I915_READ(reg);
964 cur_state = !!(val & FDI_TX_ENABLE);
965 }
Jesse Barnes040484a2011-01-03 12:14:26 -0800966 WARN(cur_state != state,
967 "FDI TX state assertion failure (expected %s, current %s)\n",
968 state_string(state), state_string(cur_state));
969}
970#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
971#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
972
973static void assert_fdi_rx(struct drm_i915_private *dev_priv,
974 enum pipe pipe, bool state)
975{
976 int reg;
977 u32 val;
978 bool cur_state;
979
980 reg = FDI_RX_CTL(pipe);
981 val = I915_READ(reg);
982 cur_state = !!(val & FDI_RX_ENABLE);
983 WARN(cur_state != state,
984 "FDI RX state assertion failure (expected %s, current %s)\n",
985 state_string(state), state_string(cur_state));
986}
987#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
988#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
989
990static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
991 enum pipe pipe)
992{
993 int reg;
994 u32 val;
995
996 /* ILK FDI PLL is always enabled */
997 if (dev_priv->info->gen == 5)
998 return;
999
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001000 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
1001 if (IS_HASWELL(dev_priv->dev))
1002 return;
1003
Jesse Barnes040484a2011-01-03 12:14:26 -08001004 reg = FDI_TX_CTL(pipe);
1005 val = I915_READ(reg);
1006 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1007}
1008
1009static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
1010 enum pipe pipe)
1011{
1012 int reg;
1013 u32 val;
1014
1015 reg = FDI_RX_CTL(pipe);
1016 val = I915_READ(reg);
1017 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
1018}
1019
Jesse Barnesea0760c2011-01-04 15:09:32 -08001020static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1021 enum pipe pipe)
1022{
1023 int pp_reg, lvds_reg;
1024 u32 val;
1025 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001026 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001027
1028 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1029 pp_reg = PCH_PP_CONTROL;
1030 lvds_reg = PCH_LVDS;
1031 } else {
1032 pp_reg = PP_CONTROL;
1033 lvds_reg = LVDS;
1034 }
1035
1036 val = I915_READ(pp_reg);
1037 if (!(val & PANEL_POWER_ON) ||
1038 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1039 locked = false;
1040
1041 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1042 panel_pipe = PIPE_B;
1043
1044 WARN(panel_pipe == pipe && locked,
1045 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001046 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001047}
1048
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001049void assert_pipe(struct drm_i915_private *dev_priv,
1050 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001051{
1052 int reg;
1053 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001054 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001055
Daniel Vetter8e636782012-01-22 01:36:48 +01001056 /* if we need the pipe A quirk it must be always on */
1057 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1058 state = true;
1059
Jesse Barnesb24e7172011-01-04 15:09:30 -08001060 reg = PIPECONF(pipe);
1061 val = I915_READ(reg);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001062 cur_state = !!(val & PIPECONF_ENABLE);
1063 WARN(cur_state != state,
1064 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001065 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001066}
1067
Chris Wilson931872f2012-01-16 23:01:13 +00001068static void assert_plane(struct drm_i915_private *dev_priv,
1069 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001070{
1071 int reg;
1072 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001073 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001074
1075 reg = DSPCNTR(plane);
1076 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001077 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1078 WARN(cur_state != state,
1079 "plane %c assertion failure (expected %s, current %s)\n",
1080 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001081}
1082
Chris Wilson931872f2012-01-16 23:01:13 +00001083#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1084#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1085
Jesse Barnesb24e7172011-01-04 15:09:30 -08001086static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1087 enum pipe pipe)
1088{
1089 int reg, i;
1090 u32 val;
1091 int cur_pipe;
1092
Jesse Barnes19ec1352011-02-02 12:28:02 -08001093 /* Planes are fixed to pipes on ILK+ */
Adam Jackson28c057942011-10-07 14:38:42 -04001094 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1095 reg = DSPCNTR(pipe);
1096 val = I915_READ(reg);
1097 WARN((val & DISPLAY_PLANE_ENABLE),
1098 "plane %c assertion failure, should be disabled but not\n",
1099 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001100 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001101 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001102
Jesse Barnesb24e7172011-01-04 15:09:30 -08001103 /* Need to check both planes against the pipe */
1104 for (i = 0; i < 2; i++) {
1105 reg = DSPCNTR(i);
1106 val = I915_READ(reg);
1107 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1108 DISPPLANE_SEL_PIPE_SHIFT;
1109 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001110 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1111 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001112 }
1113}
1114
Jesse Barnes92f25842011-01-04 15:09:34 -08001115static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1116{
1117 u32 val;
1118 bool enabled;
1119
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001120 if (HAS_PCH_LPT(dev_priv->dev)) {
1121 DRM_DEBUG_DRIVER("LPT does not has PCH refclk, skipping check\n");
1122 return;
1123 }
1124
Jesse Barnes92f25842011-01-04 15:09:34 -08001125 val = I915_READ(PCH_DREF_CONTROL);
1126 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1127 DREF_SUPERSPREAD_SOURCE_MASK));
1128 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1129}
1130
1131static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1132 enum pipe pipe)
1133{
1134 int reg;
1135 u32 val;
1136 bool enabled;
1137
1138 reg = TRANSCONF(pipe);
1139 val = I915_READ(reg);
1140 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001141 WARN(enabled,
1142 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1143 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001144}
1145
Keith Packard4e634382011-08-06 10:39:45 -07001146static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1147 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001148{
1149 if ((val & DP_PORT_EN) == 0)
1150 return false;
1151
1152 if (HAS_PCH_CPT(dev_priv->dev)) {
1153 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1154 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1155 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1156 return false;
1157 } else {
1158 if ((val & DP_PIPE_MASK) != (pipe << 30))
1159 return false;
1160 }
1161 return true;
1162}
1163
Keith Packard1519b992011-08-06 10:35:34 -07001164static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1165 enum pipe pipe, u32 val)
1166{
1167 if ((val & PORT_ENABLE) == 0)
1168 return false;
1169
1170 if (HAS_PCH_CPT(dev_priv->dev)) {
1171 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1172 return false;
1173 } else {
1174 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1175 return false;
1176 }
1177 return true;
1178}
1179
1180static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1181 enum pipe pipe, u32 val)
1182{
1183 if ((val & LVDS_PORT_EN) == 0)
1184 return false;
1185
1186 if (HAS_PCH_CPT(dev_priv->dev)) {
1187 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1188 return false;
1189 } else {
1190 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1191 return false;
1192 }
1193 return true;
1194}
1195
1196static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1197 enum pipe pipe, u32 val)
1198{
1199 if ((val & ADPA_DAC_ENABLE) == 0)
1200 return false;
1201 if (HAS_PCH_CPT(dev_priv->dev)) {
1202 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1203 return false;
1204 } else {
1205 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1206 return false;
1207 }
1208 return true;
1209}
1210
Jesse Barnes291906f2011-02-02 12:28:03 -08001211static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001212 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001213{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001214 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001215 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001216 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001217 reg, pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001218}
1219
1220static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1221 enum pipe pipe, int reg)
1222{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001223 u32 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001224 WARN(hdmi_pipe_enabled(dev_priv, val, pipe),
Adam Jackson23c99e72011-10-07 14:38:43 -04001225 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001226 reg, pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001227}
1228
1229static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1230 enum pipe pipe)
1231{
1232 int reg;
1233 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001234
Keith Packardf0575e92011-07-25 22:12:43 -07001235 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1236 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1237 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001238
1239 reg = PCH_ADPA;
1240 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001241 WARN(adpa_pipe_enabled(dev_priv, val, pipe),
Jesse Barnes291906f2011-02-02 12:28:03 -08001242 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001243 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001244
1245 reg = PCH_LVDS;
1246 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001247 WARN(lvds_pipe_enabled(dev_priv, val, pipe),
Jesse Barnes291906f2011-02-02 12:28:03 -08001248 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001249 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001250
1251 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1252 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1253 assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1254}
1255
Jesse Barnesb24e7172011-01-04 15:09:30 -08001256/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001257 * intel_enable_pll - enable a PLL
1258 * @dev_priv: i915 private structure
1259 * @pipe: pipe PLL to enable
1260 *
1261 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1262 * make sure the PLL reg is writable first though, since the panel write
1263 * protect mechanism may be enabled.
1264 *
1265 * Note! This is for pre-ILK only.
1266 */
1267static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1268{
1269 int reg;
1270 u32 val;
1271
1272 /* No really, not for ILK+ */
1273 BUG_ON(dev_priv->info->gen >= 5);
1274
1275 /* PLL is protected by panel, make sure we can write it */
1276 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1277 assert_panel_unlocked(dev_priv, pipe);
1278
1279 reg = DPLL(pipe);
1280 val = I915_READ(reg);
1281 val |= DPLL_VCO_ENABLE;
1282
1283 /* We do this three times for luck */
1284 I915_WRITE(reg, val);
1285 POSTING_READ(reg);
1286 udelay(150); /* wait for warmup */
1287 I915_WRITE(reg, val);
1288 POSTING_READ(reg);
1289 udelay(150); /* wait for warmup */
1290 I915_WRITE(reg, val);
1291 POSTING_READ(reg);
1292 udelay(150); /* wait for warmup */
1293}
1294
1295/**
1296 * intel_disable_pll - disable a PLL
1297 * @dev_priv: i915 private structure
1298 * @pipe: pipe PLL to disable
1299 *
1300 * Disable the PLL for @pipe, making sure the pipe is off first.
1301 *
1302 * Note! This is for pre-ILK only.
1303 */
1304static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1305{
1306 int reg;
1307 u32 val;
1308
1309 /* Don't disable pipe A or pipe A PLLs if needed */
1310 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1311 return;
1312
1313 /* Make sure the pipe isn't still relying on us */
1314 assert_pipe_disabled(dev_priv, pipe);
1315
1316 reg = DPLL(pipe);
1317 val = I915_READ(reg);
1318 val &= ~DPLL_VCO_ENABLE;
1319 I915_WRITE(reg, val);
1320 POSTING_READ(reg);
1321}
1322
Eugeni Dodonova416ede2012-05-09 15:37:10 -03001323/* SBI access */
1324static void
1325intel_sbi_write(struct drm_i915_private *dev_priv, u16 reg, u32 value)
1326{
1327 unsigned long flags;
1328
1329 spin_lock_irqsave(&dev_priv->dpio_lock, flags);
1330 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_READY) == 0,
1331 100)) {
1332 DRM_ERROR("timeout waiting for SBI to become ready\n");
1333 goto out_unlock;
1334 }
1335
1336 I915_WRITE(SBI_ADDR,
1337 (reg << 16));
1338 I915_WRITE(SBI_DATA,
1339 value);
1340 I915_WRITE(SBI_CTL_STAT,
1341 SBI_BUSY |
1342 SBI_CTL_OP_CRWR);
1343
1344 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_READY | SBI_RESPONSE_SUCCESS)) == 0,
1345 100)) {
1346 DRM_ERROR("timeout waiting for SBI to complete write transaction\n");
1347 goto out_unlock;
1348 }
1349
1350out_unlock:
1351 spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
1352}
1353
1354static u32
1355intel_sbi_read(struct drm_i915_private *dev_priv, u16 reg)
1356{
1357 unsigned long flags;
1358 u32 value;
1359
1360 spin_lock_irqsave(&dev_priv->dpio_lock, flags);
1361 if (wait_for((I915_READ(SBI_CTL_STAT) & SBI_READY) == 0,
1362 100)) {
1363 DRM_ERROR("timeout waiting for SBI to become ready\n");
1364 goto out_unlock;
1365 }
1366
1367 I915_WRITE(SBI_ADDR,
1368 (reg << 16));
1369 I915_WRITE(SBI_CTL_STAT,
1370 SBI_BUSY |
1371 SBI_CTL_OP_CRRD);
1372
1373 if (wait_for((I915_READ(SBI_CTL_STAT) & (SBI_READY | SBI_RESPONSE_SUCCESS)) == 0,
1374 100)) {
1375 DRM_ERROR("timeout waiting for SBI to complete read transaction\n");
1376 goto out_unlock;
1377 }
1378
1379 value = I915_READ(SBI_DATA);
1380
1381out_unlock:
1382 spin_unlock_irqrestore(&dev_priv->dpio_lock, flags);
1383 return value;
1384}
1385
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001386/**
Jesse Barnes92f25842011-01-04 15:09:34 -08001387 * intel_enable_pch_pll - enable PCH PLL
1388 * @dev_priv: i915 private structure
1389 * @pipe: pipe PLL to enable
1390 *
1391 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1392 * drives the transcoder clock.
1393 */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001394static void intel_enable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001395{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001396 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1397 struct intel_pch_pll *pll = intel_crtc->pch_pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001398 int reg;
1399 u32 val;
1400
1401 /* PCH only available on ILK+ */
1402 BUG_ON(dev_priv->info->gen < 5);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001403 BUG_ON(pll == NULL);
1404 BUG_ON(pll->refcount == 0);
1405
1406 DRM_DEBUG_KMS("enable PCH PLL %x (active %d, on? %d)for crtc %d\n",
1407 pll->pll_reg, pll->active, pll->on,
1408 intel_crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001409
1410 /* PCH refclock must be enabled first */
1411 assert_pch_refclk_enabled(dev_priv);
1412
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001413 if (pll->active++ && pll->on) {
1414 assert_pch_pll_enabled(dev_priv, intel_crtc);
1415 return;
1416 }
1417
1418 DRM_DEBUG_KMS("enabling PCH PLL %x\n", pll->pll_reg);
1419
1420 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001421 val = I915_READ(reg);
1422 val |= DPLL_VCO_ENABLE;
1423 I915_WRITE(reg, val);
1424 POSTING_READ(reg);
1425 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001426
1427 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001428}
1429
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001430static void intel_disable_pch_pll(struct intel_crtc *intel_crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001431{
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001432 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
1433 struct intel_pch_pll *pll = intel_crtc->pch_pll;
Jesse Barnes92f25842011-01-04 15:09:34 -08001434 int reg;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001435 u32 val;
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001436
Jesse Barnes92f25842011-01-04 15:09:34 -08001437 /* PCH only available on ILK+ */
1438 BUG_ON(dev_priv->info->gen < 5);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001439 if (pll == NULL)
1440 return;
1441
1442 BUG_ON(pll->refcount == 0);
1443
1444 DRM_DEBUG_KMS("disable PCH PLL %x (active %d, on? %d) for crtc %d\n",
1445 pll->pll_reg, pll->active, pll->on,
1446 intel_crtc->base.base.id);
1447
1448 BUG_ON(pll->active == 0);
1449 if (--pll->active) {
1450 assert_pch_pll_enabled(dev_priv, intel_crtc);
1451 return;
1452 }
1453
1454 DRM_DEBUG_KMS("disabling PCH PLL %x\n", pll->pll_reg);
Jesse Barnes92f25842011-01-04 15:09:34 -08001455
1456 /* Make sure transcoder isn't still depending on us */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001457 assert_transcoder_disabled(dev_priv, intel_crtc->pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001458
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001459 reg = pll->pll_reg;
Jesse Barnes92f25842011-01-04 15:09:34 -08001460 val = I915_READ(reg);
1461 val &= ~DPLL_VCO_ENABLE;
1462 I915_WRITE(reg, val);
1463 POSTING_READ(reg);
1464 udelay(200);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001465
1466 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001467}
1468
Jesse Barnes040484a2011-01-03 12:14:26 -08001469static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
1470 enum pipe pipe)
1471{
1472 int reg;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001473 u32 val, pipeconf_val;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001474 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Jesse Barnes040484a2011-01-03 12:14:26 -08001475
1476 /* PCH only available on ILK+ */
1477 BUG_ON(dev_priv->info->gen < 5);
1478
1479 /* Make sure PCH DPLL is enabled */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001480 assert_pch_pll_enabled(dev_priv, to_intel_crtc(crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001481
1482 /* FDI must be feeding us bits for PCH ports */
1483 assert_fdi_tx_enabled(dev_priv, pipe);
1484 assert_fdi_rx_enabled(dev_priv, pipe);
1485
1486 reg = TRANSCONF(pipe);
1487 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001488 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001489
1490 if (HAS_PCH_IBX(dev_priv->dev)) {
1491 /*
1492 * make the BPC in transcoder be consistent with
1493 * that in pipeconf reg.
1494 */
1495 val &= ~PIPE_BPC_MASK;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001496 val |= pipeconf_val & PIPE_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001497 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001498
1499 val &= ~TRANS_INTERLACE_MASK;
1500 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001501 if (HAS_PCH_IBX(dev_priv->dev) &&
1502 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1503 val |= TRANS_LEGACY_INTERLACED_ILK;
1504 else
1505 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001506 else
1507 val |= TRANS_PROGRESSIVE;
1508
Jesse Barnes040484a2011-01-03 12:14:26 -08001509 I915_WRITE(reg, val | TRANS_ENABLE);
1510 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1511 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1512}
1513
1514static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
1515 enum pipe pipe)
1516{
1517 int reg;
1518 u32 val;
1519
1520 /* FDI relies on the transcoder */
1521 assert_fdi_tx_disabled(dev_priv, pipe);
1522 assert_fdi_rx_disabled(dev_priv, pipe);
1523
Jesse Barnes291906f2011-02-02 12:28:03 -08001524 /* Ports must be off as well */
1525 assert_pch_ports_disabled(dev_priv, pipe);
1526
Jesse Barnes040484a2011-01-03 12:14:26 -08001527 reg = TRANSCONF(pipe);
1528 val = I915_READ(reg);
1529 val &= ~TRANS_ENABLE;
1530 I915_WRITE(reg, val);
1531 /* wait for PCH transcoder off, transcoder state */
1532 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes4c9c18c2011-10-13 09:46:32 -07001533 DRM_ERROR("failed to disable transcoder %d\n", pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001534}
1535
Jesse Barnes92f25842011-01-04 15:09:34 -08001536/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001537 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001538 * @dev_priv: i915 private structure
1539 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001540 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001541 *
1542 * Enable @pipe, making sure that various hardware specific requirements
1543 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1544 *
1545 * @pipe should be %PIPE_A or %PIPE_B.
1546 *
1547 * Will wait until the pipe is actually running (i.e. first vblank) before
1548 * returning.
1549 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001550static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1551 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001552{
1553 int reg;
1554 u32 val;
1555
1556 /*
1557 * A pipe without a PLL won't actually be able to drive bits from
1558 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1559 * need the check.
1560 */
1561 if (!HAS_PCH_SPLIT(dev_priv->dev))
1562 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001563 else {
1564 if (pch_port) {
1565 /* if driving the PCH, we need FDI enabled */
1566 assert_fdi_rx_pll_enabled(dev_priv, pipe);
1567 assert_fdi_tx_pll_enabled(dev_priv, pipe);
1568 }
1569 /* FIXME: assert CPU port conditions for SNB+ */
1570 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001571
1572 reg = PIPECONF(pipe);
1573 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001574 if (val & PIPECONF_ENABLE)
1575 return;
1576
1577 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001578 intel_wait_for_vblank(dev_priv->dev, pipe);
1579}
1580
1581/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001582 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001583 * @dev_priv: i915 private structure
1584 * @pipe: pipe to disable
1585 *
1586 * Disable @pipe, making sure that various hardware specific requirements
1587 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1588 *
1589 * @pipe should be %PIPE_A or %PIPE_B.
1590 *
1591 * Will wait until the pipe has shut down before returning.
1592 */
1593static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1594 enum pipe pipe)
1595{
1596 int reg;
1597 u32 val;
1598
1599 /*
1600 * Make sure planes won't keep trying to pump pixels to us,
1601 * or we might hang the display.
1602 */
1603 assert_planes_disabled(dev_priv, pipe);
1604
1605 /* Don't disable pipe A or pipe A PLLs if needed */
1606 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1607 return;
1608
1609 reg = PIPECONF(pipe);
1610 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001611 if ((val & PIPECONF_ENABLE) == 0)
1612 return;
1613
1614 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001615 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1616}
1617
Keith Packardd74362c2011-07-28 14:47:14 -07001618/*
1619 * Plane regs are double buffered, going from enabled->disabled needs a
1620 * trigger in order to latch. The display address reg provides this.
1621 */
Eugeni Dodonov6f1d69b2012-04-18 15:29:25 -03001622void intel_flush_display_plane(struct drm_i915_private *dev_priv,
Keith Packardd74362c2011-07-28 14:47:14 -07001623 enum plane plane)
1624{
1625 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1626 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1627}
1628
Jesse Barnesb24e7172011-01-04 15:09:30 -08001629/**
1630 * intel_enable_plane - enable a display plane on a given pipe
1631 * @dev_priv: i915 private structure
1632 * @plane: plane to enable
1633 * @pipe: pipe being fed
1634 *
1635 * Enable @plane on @pipe, making sure that @pipe is running first.
1636 */
1637static void intel_enable_plane(struct drm_i915_private *dev_priv,
1638 enum plane plane, enum pipe pipe)
1639{
1640 int reg;
1641 u32 val;
1642
1643 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1644 assert_pipe_enabled(dev_priv, pipe);
1645
1646 reg = DSPCNTR(plane);
1647 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001648 if (val & DISPLAY_PLANE_ENABLE)
1649 return;
1650
1651 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001652 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001653 intel_wait_for_vblank(dev_priv->dev, pipe);
1654}
1655
Jesse Barnesb24e7172011-01-04 15:09:30 -08001656/**
1657 * intel_disable_plane - disable a display plane
1658 * @dev_priv: i915 private structure
1659 * @plane: plane to disable
1660 * @pipe: pipe consuming the data
1661 *
1662 * Disable @plane; should be an independent operation.
1663 */
1664static void intel_disable_plane(struct drm_i915_private *dev_priv,
1665 enum plane plane, enum pipe pipe)
1666{
1667 int reg;
1668 u32 val;
1669
1670 reg = DSPCNTR(plane);
1671 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001672 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1673 return;
1674
1675 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001676 intel_flush_display_plane(dev_priv, plane);
1677 intel_wait_for_vblank(dev_priv->dev, pipe);
1678}
1679
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001680static void disable_pch_dp(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001681 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001682{
1683 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001684 if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
Keith Packardf0575e92011-07-25 22:12:43 -07001685 DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001686 I915_WRITE(reg, val & ~DP_PORT_EN);
Keith Packardf0575e92011-07-25 22:12:43 -07001687 }
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001688}
1689
1690static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
1691 enum pipe pipe, int reg)
1692{
1693 u32 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001694 if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
Keith Packardf0575e92011-07-25 22:12:43 -07001695 DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
1696 reg, pipe);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001697 I915_WRITE(reg, val & ~PORT_ENABLE);
Keith Packardf0575e92011-07-25 22:12:43 -07001698 }
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001699}
1700
1701/* Disable any ports connected to this transcoder */
1702static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
1703 enum pipe pipe)
1704{
1705 u32 reg, val;
1706
1707 val = I915_READ(PCH_PP_CONTROL);
1708 I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
1709
Keith Packardf0575e92011-07-25 22:12:43 -07001710 disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1711 disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1712 disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001713
1714 reg = PCH_ADPA;
1715 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001716 if (adpa_pipe_enabled(dev_priv, val, pipe))
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001717 I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
1718
1719 reg = PCH_LVDS;
1720 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001721 if (lvds_pipe_enabled(dev_priv, val, pipe)) {
1722 DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001723 I915_WRITE(reg, val & ~LVDS_PORT_EN);
1724 POSTING_READ(reg);
1725 udelay(100);
1726 }
1727
1728 disable_pch_hdmi(dev_priv, pipe, HDMIB);
1729 disable_pch_hdmi(dev_priv, pipe, HDMIC);
1730 disable_pch_hdmi(dev_priv, pipe, HDMID);
1731}
1732
Chris Wilson127bd2a2010-07-23 23:32:05 +01001733int
Chris Wilson48b956c2010-09-14 12:50:34 +01001734intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001735 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001736 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001737{
Chris Wilsonce453d82011-02-21 14:43:56 +00001738 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001739 u32 alignment;
1740 int ret;
1741
Chris Wilson05394f32010-11-08 19:18:58 +00001742 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001743 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001744 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1745 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001746 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001747 alignment = 4 * 1024;
1748 else
1749 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001750 break;
1751 case I915_TILING_X:
1752 /* pin() will align the object as required by fence */
1753 alignment = 0;
1754 break;
1755 case I915_TILING_Y:
1756 /* FIXME: Is this true? */
1757 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1758 return -EINVAL;
1759 default:
1760 BUG();
1761 }
1762
Chris Wilsonce453d82011-02-21 14:43:56 +00001763 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001764 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001765 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001766 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001767
1768 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1769 * fence, whereas 965+ only requires a fence if using
1770 * framebuffer compression. For simplicity, we always install
1771 * a fence as the cost is not that onerous.
1772 */
Chris Wilson06d98132012-04-17 15:31:24 +01001773 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001774 if (ret)
1775 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01001776
Chris Wilson9a5a53b2012-03-22 15:10:00 +00001777 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001778
Chris Wilsonce453d82011-02-21 14:43:56 +00001779 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001780 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01001781
1782err_unpin:
1783 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00001784err_interruptible:
1785 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01001786 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001787}
1788
Chris Wilson1690e1e2011-12-14 13:57:08 +01001789void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
1790{
1791 i915_gem_object_unpin_fence(obj);
1792 i915_gem_object_unpin(obj);
1793}
1794
Jesse Barnes17638cd2011-06-24 12:19:23 -07001795static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1796 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07001797{
1798 struct drm_device *dev = crtc->dev;
1799 struct drm_i915_private *dev_priv = dev->dev_private;
1800 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1801 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001802 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001803 int plane = intel_crtc->plane;
1804 unsigned long Start, Offset;
Jesse Barnes81255562010-08-02 12:07:50 -07001805 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01001806 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07001807
1808 switch (plane) {
1809 case 0:
1810 case 1:
1811 break;
1812 default:
1813 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1814 return -EINVAL;
1815 }
1816
1817 intel_fb = to_intel_framebuffer(fb);
1818 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07001819
Chris Wilson5eddb702010-09-11 13:48:45 +01001820 reg = DSPCNTR(plane);
1821 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001822 /* Mask out pixel format bits in case we change it */
1823 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1824 switch (fb->bits_per_pixel) {
1825 case 8:
1826 dspcntr |= DISPPLANE_8BPP;
1827 break;
1828 case 16:
1829 if (fb->depth == 15)
1830 dspcntr |= DISPPLANE_15_16BPP;
1831 else
1832 dspcntr |= DISPPLANE_16BPP;
1833 break;
1834 case 24:
1835 case 32:
1836 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
1837 break;
1838 default:
Jesse Barnes17638cd2011-06-24 12:19:23 -07001839 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
Jesse Barnes81255562010-08-02 12:07:50 -07001840 return -EINVAL;
1841 }
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001842 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00001843 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07001844 dspcntr |= DISPPLANE_TILED;
1845 else
1846 dspcntr &= ~DISPPLANE_TILED;
1847 }
1848
Chris Wilson5eddb702010-09-11 13:48:45 +01001849 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07001850
Chris Wilson05394f32010-11-08 19:18:58 +00001851 Start = obj->gtt_offset;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001852 Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07001853
Chris Wilson4e6cfef2010-08-08 13:20:19 +01001854 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001855 Start, Offset, x, y, fb->pitches[0]);
1856 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001857 if (INTEL_INFO(dev)->gen >= 4) {
Armin Reese446f2542012-03-30 16:20:16 -07001858 I915_MODIFY_DISPBASE(DSPSURF(plane), Start);
Chris Wilson5eddb702010-09-11 13:48:45 +01001859 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1860 I915_WRITE(DSPADDR(plane), Offset);
1861 } else
1862 I915_WRITE(DSPADDR(plane), Start + Offset);
1863 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07001864
Jesse Barnes17638cd2011-06-24 12:19:23 -07001865 return 0;
1866}
1867
1868static int ironlake_update_plane(struct drm_crtc *crtc,
1869 struct drm_framebuffer *fb, int x, int y)
1870{
1871 struct drm_device *dev = crtc->dev;
1872 struct drm_i915_private *dev_priv = dev->dev_private;
1873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1874 struct intel_framebuffer *intel_fb;
1875 struct drm_i915_gem_object *obj;
1876 int plane = intel_crtc->plane;
1877 unsigned long Start, Offset;
1878 u32 dspcntr;
1879 u32 reg;
1880
1881 switch (plane) {
1882 case 0:
1883 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07001884 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07001885 break;
1886 default:
1887 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
1888 return -EINVAL;
1889 }
1890
1891 intel_fb = to_intel_framebuffer(fb);
1892 obj = intel_fb->obj;
1893
1894 reg = DSPCNTR(plane);
1895 dspcntr = I915_READ(reg);
1896 /* Mask out pixel format bits in case we change it */
1897 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
1898 switch (fb->bits_per_pixel) {
1899 case 8:
1900 dspcntr |= DISPPLANE_8BPP;
1901 break;
1902 case 16:
1903 if (fb->depth != 16)
1904 return -EINVAL;
1905
1906 dspcntr |= DISPPLANE_16BPP;
1907 break;
1908 case 24:
1909 case 32:
1910 if (fb->depth == 24)
1911 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
1912 else if (fb->depth == 30)
1913 dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
1914 else
1915 return -EINVAL;
1916 break;
1917 default:
1918 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
1919 return -EINVAL;
1920 }
1921
1922 if (obj->tiling_mode != I915_TILING_NONE)
1923 dspcntr |= DISPPLANE_TILED;
1924 else
1925 dspcntr &= ~DISPPLANE_TILED;
1926
1927 /* must disable */
1928 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
1929
1930 I915_WRITE(reg, dspcntr);
1931
1932 Start = obj->gtt_offset;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001933 Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes17638cd2011-06-24 12:19:23 -07001934
1935 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001936 Start, Offset, x, y, fb->pitches[0]);
1937 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Armin Reese446f2542012-03-30 16:20:16 -07001938 I915_MODIFY_DISPBASE(DSPSURF(plane), Start);
Jesse Barnes17638cd2011-06-24 12:19:23 -07001939 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
1940 I915_WRITE(DSPADDR(plane), Offset);
1941 POSTING_READ(reg);
1942
1943 return 0;
1944}
1945
1946/* Assume fb object is pinned & idle & fenced and just update base pointers */
1947static int
1948intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
1949 int x, int y, enum mode_set_atomic state)
1950{
1951 struct drm_device *dev = crtc->dev;
1952 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07001953
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01001954 if (dev_priv->display.disable_fbc)
1955 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02001956 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07001957
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01001958 return dev_priv->display.update_plane(crtc, fb, x, y);
Jesse Barnes81255562010-08-02 12:07:50 -07001959}
1960
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001961static int
Chris Wilson14667a42012-04-03 17:58:35 +01001962intel_finish_fb(struct drm_framebuffer *old_fb)
1963{
1964 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
1965 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1966 bool was_interruptible = dev_priv->mm.interruptible;
1967 int ret;
1968
1969 wait_event(dev_priv->pending_flip_queue,
1970 atomic_read(&dev_priv->mm.wedged) ||
1971 atomic_read(&obj->pending_flip) == 0);
1972
1973 /* Big Hammer, we also need to ensure that any pending
1974 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
1975 * current scanout is retired before unpinning the old
1976 * framebuffer.
1977 *
1978 * This should only fail upon a hung GPU, in which case we
1979 * can safely continue.
1980 */
1981 dev_priv->mm.interruptible = false;
1982 ret = i915_gem_object_finish_gpu(obj);
1983 dev_priv->mm.interruptible = was_interruptible;
1984
1985 return ret;
1986}
1987
1988static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05001989intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
1990 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08001991{
1992 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01001993 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08001994 struct drm_i915_master_private *master_priv;
1995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00001996 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08001997
1998 /* no fb bound */
1999 if (!crtc->fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002000 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002001 return 0;
2002 }
2003
Eugeni Dodonov5826eca2012-05-09 15:37:12 -03002004 if(intel_crtc->plane > dev_priv->num_pipe) {
2005 DRM_ERROR("no plane for crtc: plane %d, num_pipes %d\n",
2006 intel_crtc->plane,
2007 dev_priv->num_pipe);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002008 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002009 }
2010
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002011 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002012 ret = intel_pin_and_fence_fb_obj(dev,
2013 to_intel_framebuffer(crtc->fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002014 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002015 if (ret != 0) {
2016 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002017 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002018 return ret;
2019 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002020
Chris Wilson14667a42012-04-03 17:58:35 +01002021 if (old_fb)
2022 intel_finish_fb(old_fb);
Chris Wilson265db952010-09-20 15:41:01 +01002023
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002024 ret = dev_priv->display.update_plane(crtc, crtc->fb, x, y);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002025 if (ret) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002026 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002027 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002028 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002029 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002030 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002031
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002032 if (old_fb) {
2033 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002034 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002035 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002036
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002037 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002038 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002039
2040 if (!dev->primary->master)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002041 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002042
2043 master_priv = dev->primary->master->driver_priv;
2044 if (!master_priv->sarea_priv)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002045 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002046
Chris Wilson265db952010-09-20 15:41:01 +01002047 if (intel_crtc->pipe) {
Jesse Barnes79e53942008-11-07 14:24:08 -08002048 master_priv->sarea_priv->pipeB_x = x;
2049 master_priv->sarea_priv->pipeB_y = y;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002050 } else {
2051 master_priv->sarea_priv->pipeA_x = x;
2052 master_priv->sarea_priv->pipeA_y = y;
Jesse Barnes79e53942008-11-07 14:24:08 -08002053 }
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002054
2055 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002056}
2057
Chris Wilson5eddb702010-09-11 13:48:45 +01002058static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002059{
2060 struct drm_device *dev = crtc->dev;
2061 struct drm_i915_private *dev_priv = dev->dev_private;
2062 u32 dpa_ctl;
2063
Zhao Yakui28c97732009-10-09 11:39:41 +08002064 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002065 dpa_ctl = I915_READ(DP_A);
2066 dpa_ctl &= ~DP_PLL_FREQ_MASK;
2067
2068 if (clock < 200000) {
2069 u32 temp;
2070 dpa_ctl |= DP_PLL_FREQ_160MHZ;
2071 /* workaround for 160Mhz:
2072 1) program 0x4600c bits 15:0 = 0x8124
2073 2) program 0x46010 bit 0 = 1
2074 3) program 0x46034 bit 24 = 1
2075 4) program 0x64000 bit 14 = 1
2076 */
2077 temp = I915_READ(0x4600c);
2078 temp &= 0xffff0000;
2079 I915_WRITE(0x4600c, temp | 0x8124);
2080
2081 temp = I915_READ(0x46010);
2082 I915_WRITE(0x46010, temp | 1);
2083
2084 temp = I915_READ(0x46034);
2085 I915_WRITE(0x46034, temp | (1 << 24));
2086 } else {
2087 dpa_ctl |= DP_PLL_FREQ_270MHZ;
2088 }
2089 I915_WRITE(DP_A, dpa_ctl);
2090
Chris Wilson5eddb702010-09-11 13:48:45 +01002091 POSTING_READ(DP_A);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002092 udelay(500);
2093}
2094
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002095static void intel_fdi_normal_train(struct drm_crtc *crtc)
2096{
2097 struct drm_device *dev = crtc->dev;
2098 struct drm_i915_private *dev_priv = dev->dev_private;
2099 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2100 int pipe = intel_crtc->pipe;
2101 u32 reg, temp;
2102
2103 /* enable normal train */
2104 reg = FDI_TX_CTL(pipe);
2105 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002106 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002107 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2108 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002109 } else {
2110 temp &= ~FDI_LINK_TRAIN_NONE;
2111 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002112 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002113 I915_WRITE(reg, temp);
2114
2115 reg = FDI_RX_CTL(pipe);
2116 temp = I915_READ(reg);
2117 if (HAS_PCH_CPT(dev)) {
2118 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2119 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2120 } else {
2121 temp &= ~FDI_LINK_TRAIN_NONE;
2122 temp |= FDI_LINK_TRAIN_NONE;
2123 }
2124 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2125
2126 /* wait one idle pattern time */
2127 POSTING_READ(reg);
2128 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002129
2130 /* IVB wants error correction enabled */
2131 if (IS_IVYBRIDGE(dev))
2132 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2133 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002134}
2135
Jesse Barnes291427f2011-07-29 12:42:37 -07002136static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
2137{
2138 struct drm_i915_private *dev_priv = dev->dev_private;
2139 u32 flags = I915_READ(SOUTH_CHICKEN1);
2140
2141 flags |= FDI_PHASE_SYNC_OVR(pipe);
2142 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
2143 flags |= FDI_PHASE_SYNC_EN(pipe);
2144 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2145 POSTING_READ(SOUTH_CHICKEN1);
2146}
2147
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002148/* The FDI link training functions for ILK/Ibexpeak. */
2149static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2150{
2151 struct drm_device *dev = crtc->dev;
2152 struct drm_i915_private *dev_priv = dev->dev_private;
2153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2154 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002155 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002156 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002157
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002158 /* FDI needs bits from pipe & plane first */
2159 assert_pipe_enabled(dev_priv, pipe);
2160 assert_plane_enabled(dev_priv, plane);
2161
Adam Jacksone1a44742010-06-25 15:32:14 -04002162 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2163 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002164 reg = FDI_RX_IMR(pipe);
2165 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002166 temp &= ~FDI_RX_SYMBOL_LOCK;
2167 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002168 I915_WRITE(reg, temp);
2169 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002170 udelay(150);
2171
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002172 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002173 reg = FDI_TX_CTL(pipe);
2174 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002175 temp &= ~(7 << 19);
2176 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002177 temp &= ~FDI_LINK_TRAIN_NONE;
2178 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002179 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002180
Chris Wilson5eddb702010-09-11 13:48:45 +01002181 reg = FDI_RX_CTL(pipe);
2182 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002183 temp &= ~FDI_LINK_TRAIN_NONE;
2184 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002185 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2186
2187 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002188 udelay(150);
2189
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002190 /* Ironlake workaround, enable clock pointer after FDI enable*/
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002191 if (HAS_PCH_IBX(dev)) {
2192 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2193 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2194 FDI_RX_PHASE_SYNC_POINTER_EN);
2195 }
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002196
Chris Wilson5eddb702010-09-11 13:48:45 +01002197 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002198 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002199 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002200 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2201
2202 if ((temp & FDI_RX_BIT_LOCK)) {
2203 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002204 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002205 break;
2206 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002207 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002208 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002209 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002210
2211 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002212 reg = FDI_TX_CTL(pipe);
2213 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002214 temp &= ~FDI_LINK_TRAIN_NONE;
2215 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002216 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002217
Chris Wilson5eddb702010-09-11 13:48:45 +01002218 reg = FDI_RX_CTL(pipe);
2219 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002220 temp &= ~FDI_LINK_TRAIN_NONE;
2221 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002222 I915_WRITE(reg, temp);
2223
2224 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002225 udelay(150);
2226
Chris Wilson5eddb702010-09-11 13:48:45 +01002227 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002228 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002229 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002230 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2231
2232 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002233 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002234 DRM_DEBUG_KMS("FDI train 2 done.\n");
2235 break;
2236 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002237 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002238 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002239 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002240
2241 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002242
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002243}
2244
Akshay Joshi0206e352011-08-16 15:34:10 -04002245static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002246 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2247 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2248 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2249 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2250};
2251
2252/* The FDI link training functions for SNB/Cougarpoint. */
2253static void gen6_fdi_link_train(struct drm_crtc *crtc)
2254{
2255 struct drm_device *dev = crtc->dev;
2256 struct drm_i915_private *dev_priv = dev->dev_private;
2257 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2258 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002259 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002260
Adam Jacksone1a44742010-06-25 15:32:14 -04002261 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2262 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002263 reg = FDI_RX_IMR(pipe);
2264 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002265 temp &= ~FDI_RX_SYMBOL_LOCK;
2266 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002267 I915_WRITE(reg, temp);
2268
2269 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002270 udelay(150);
2271
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002272 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002273 reg = FDI_TX_CTL(pipe);
2274 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002275 temp &= ~(7 << 19);
2276 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002277 temp &= ~FDI_LINK_TRAIN_NONE;
2278 temp |= FDI_LINK_TRAIN_PATTERN_1;
2279 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2280 /* SNB-B */
2281 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002282 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002283
Chris Wilson5eddb702010-09-11 13:48:45 +01002284 reg = FDI_RX_CTL(pipe);
2285 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002286 if (HAS_PCH_CPT(dev)) {
2287 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2288 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2289 } else {
2290 temp &= ~FDI_LINK_TRAIN_NONE;
2291 temp |= FDI_LINK_TRAIN_PATTERN_1;
2292 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002293 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2294
2295 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002296 udelay(150);
2297
Jesse Barnes291427f2011-07-29 12:42:37 -07002298 if (HAS_PCH_CPT(dev))
2299 cpt_phase_pointer_enable(dev, pipe);
2300
Akshay Joshi0206e352011-08-16 15:34:10 -04002301 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002302 reg = FDI_TX_CTL(pipe);
2303 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002304 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2305 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002306 I915_WRITE(reg, temp);
2307
2308 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002309 udelay(500);
2310
Sean Paulfa37d392012-03-02 12:53:39 -05002311 for (retry = 0; retry < 5; retry++) {
2312 reg = FDI_RX_IIR(pipe);
2313 temp = I915_READ(reg);
2314 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2315 if (temp & FDI_RX_BIT_LOCK) {
2316 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2317 DRM_DEBUG_KMS("FDI train 1 done.\n");
2318 break;
2319 }
2320 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002321 }
Sean Paulfa37d392012-03-02 12:53:39 -05002322 if (retry < 5)
2323 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002324 }
2325 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002326 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002327
2328 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002329 reg = FDI_TX_CTL(pipe);
2330 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002331 temp &= ~FDI_LINK_TRAIN_NONE;
2332 temp |= FDI_LINK_TRAIN_PATTERN_2;
2333 if (IS_GEN6(dev)) {
2334 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2335 /* SNB-B */
2336 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2337 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002338 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002339
Chris Wilson5eddb702010-09-11 13:48:45 +01002340 reg = FDI_RX_CTL(pipe);
2341 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002342 if (HAS_PCH_CPT(dev)) {
2343 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2344 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2345 } else {
2346 temp &= ~FDI_LINK_TRAIN_NONE;
2347 temp |= FDI_LINK_TRAIN_PATTERN_2;
2348 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002349 I915_WRITE(reg, temp);
2350
2351 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002352 udelay(150);
2353
Akshay Joshi0206e352011-08-16 15:34:10 -04002354 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002355 reg = FDI_TX_CTL(pipe);
2356 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002357 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2358 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002359 I915_WRITE(reg, temp);
2360
2361 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002362 udelay(500);
2363
Sean Paulfa37d392012-03-02 12:53:39 -05002364 for (retry = 0; retry < 5; retry++) {
2365 reg = FDI_RX_IIR(pipe);
2366 temp = I915_READ(reg);
2367 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2368 if (temp & FDI_RX_SYMBOL_LOCK) {
2369 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2370 DRM_DEBUG_KMS("FDI train 2 done.\n");
2371 break;
2372 }
2373 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002374 }
Sean Paulfa37d392012-03-02 12:53:39 -05002375 if (retry < 5)
2376 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002377 }
2378 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002379 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002380
2381 DRM_DEBUG_KMS("FDI train done.\n");
2382}
2383
Jesse Barnes357555c2011-04-28 15:09:55 -07002384/* Manual link training for Ivy Bridge A0 parts */
2385static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2386{
2387 struct drm_device *dev = crtc->dev;
2388 struct drm_i915_private *dev_priv = dev->dev_private;
2389 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2390 int pipe = intel_crtc->pipe;
2391 u32 reg, temp, i;
2392
2393 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2394 for train result */
2395 reg = FDI_RX_IMR(pipe);
2396 temp = I915_READ(reg);
2397 temp &= ~FDI_RX_SYMBOL_LOCK;
2398 temp &= ~FDI_RX_BIT_LOCK;
2399 I915_WRITE(reg, temp);
2400
2401 POSTING_READ(reg);
2402 udelay(150);
2403
2404 /* enable CPU FDI TX and PCH FDI RX */
2405 reg = FDI_TX_CTL(pipe);
2406 temp = I915_READ(reg);
2407 temp &= ~(7 << 19);
2408 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2409 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2410 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2411 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2412 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002413 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002414 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2415
2416 reg = FDI_RX_CTL(pipe);
2417 temp = I915_READ(reg);
2418 temp &= ~FDI_LINK_TRAIN_AUTO;
2419 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2420 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002421 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002422 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2423
2424 POSTING_READ(reg);
2425 udelay(150);
2426
Jesse Barnes291427f2011-07-29 12:42:37 -07002427 if (HAS_PCH_CPT(dev))
2428 cpt_phase_pointer_enable(dev, pipe);
2429
Akshay Joshi0206e352011-08-16 15:34:10 -04002430 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002431 reg = FDI_TX_CTL(pipe);
2432 temp = I915_READ(reg);
2433 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2434 temp |= snb_b_fdi_train_param[i];
2435 I915_WRITE(reg, temp);
2436
2437 POSTING_READ(reg);
2438 udelay(500);
2439
2440 reg = FDI_RX_IIR(pipe);
2441 temp = I915_READ(reg);
2442 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2443
2444 if (temp & FDI_RX_BIT_LOCK ||
2445 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2446 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2447 DRM_DEBUG_KMS("FDI train 1 done.\n");
2448 break;
2449 }
2450 }
2451 if (i == 4)
2452 DRM_ERROR("FDI train 1 fail!\n");
2453
2454 /* Train 2 */
2455 reg = FDI_TX_CTL(pipe);
2456 temp = I915_READ(reg);
2457 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2458 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2459 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2460 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2461 I915_WRITE(reg, temp);
2462
2463 reg = FDI_RX_CTL(pipe);
2464 temp = I915_READ(reg);
2465 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2466 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2467 I915_WRITE(reg, temp);
2468
2469 POSTING_READ(reg);
2470 udelay(150);
2471
Akshay Joshi0206e352011-08-16 15:34:10 -04002472 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002473 reg = FDI_TX_CTL(pipe);
2474 temp = I915_READ(reg);
2475 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2476 temp |= snb_b_fdi_train_param[i];
2477 I915_WRITE(reg, temp);
2478
2479 POSTING_READ(reg);
2480 udelay(500);
2481
2482 reg = FDI_RX_IIR(pipe);
2483 temp = I915_READ(reg);
2484 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2485
2486 if (temp & FDI_RX_SYMBOL_LOCK) {
2487 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2488 DRM_DEBUG_KMS("FDI train 2 done.\n");
2489 break;
2490 }
2491 }
2492 if (i == 4)
2493 DRM_ERROR("FDI train 2 fail!\n");
2494
2495 DRM_DEBUG_KMS("FDI train done.\n");
2496}
2497
2498static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002499{
2500 struct drm_device *dev = crtc->dev;
2501 struct drm_i915_private *dev_priv = dev->dev_private;
2502 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2503 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002504 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002505
Jesse Barnesc64e3112010-09-10 11:27:03 -07002506 /* Write the TU size bits so error detection works */
Chris Wilson5eddb702010-09-11 13:48:45 +01002507 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2508 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
Jesse Barnesc64e3112010-09-10 11:27:03 -07002509
Jesse Barnes0e23b992010-09-10 11:10:00 -07002510 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002511 reg = FDI_RX_CTL(pipe);
2512 temp = I915_READ(reg);
2513 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002514 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Chris Wilson5eddb702010-09-11 13:48:45 +01002515 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2516 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2517
2518 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002519 udelay(200);
2520
2521 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002522 temp = I915_READ(reg);
2523 I915_WRITE(reg, temp | FDI_PCDCLK);
2524
2525 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002526 udelay(200);
2527
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03002528 /* On Haswell, the PLL configuration for ports and pipes is handled
2529 * separately, as part of DDI setup */
2530 if (!IS_HASWELL(dev)) {
2531 /* Enable CPU FDI TX PLL, always on for Ironlake */
2532 reg = FDI_TX_CTL(pipe);
2533 temp = I915_READ(reg);
2534 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
2535 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01002536
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03002537 POSTING_READ(reg);
2538 udelay(100);
2539 }
Jesse Barnes0e23b992010-09-10 11:10:00 -07002540 }
2541}
2542
Jesse Barnes291427f2011-07-29 12:42:37 -07002543static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2544{
2545 struct drm_i915_private *dev_priv = dev->dev_private;
2546 u32 flags = I915_READ(SOUTH_CHICKEN1);
2547
2548 flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2549 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2550 flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2551 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2552 POSTING_READ(SOUTH_CHICKEN1);
2553}
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002554static void ironlake_fdi_disable(struct drm_crtc *crtc)
2555{
2556 struct drm_device *dev = crtc->dev;
2557 struct drm_i915_private *dev_priv = dev->dev_private;
2558 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2559 int pipe = intel_crtc->pipe;
2560 u32 reg, temp;
2561
2562 /* disable CPU FDI tx and PCH FDI rx */
2563 reg = FDI_TX_CTL(pipe);
2564 temp = I915_READ(reg);
2565 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2566 POSTING_READ(reg);
2567
2568 reg = FDI_RX_CTL(pipe);
2569 temp = I915_READ(reg);
2570 temp &= ~(0x7 << 16);
2571 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2572 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2573
2574 POSTING_READ(reg);
2575 udelay(100);
2576
2577 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002578 if (HAS_PCH_IBX(dev)) {
2579 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002580 I915_WRITE(FDI_RX_CHICKEN(pipe),
2581 I915_READ(FDI_RX_CHICKEN(pipe) &
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002582 ~FDI_RX_PHASE_SYNC_POINTER_EN));
Jesse Barnes291427f2011-07-29 12:42:37 -07002583 } else if (HAS_PCH_CPT(dev)) {
2584 cpt_phase_pointer_disable(dev, pipe);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002585 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002586
2587 /* still set train pattern 1 */
2588 reg = FDI_TX_CTL(pipe);
2589 temp = I915_READ(reg);
2590 temp &= ~FDI_LINK_TRAIN_NONE;
2591 temp |= FDI_LINK_TRAIN_PATTERN_1;
2592 I915_WRITE(reg, temp);
2593
2594 reg = FDI_RX_CTL(pipe);
2595 temp = I915_READ(reg);
2596 if (HAS_PCH_CPT(dev)) {
2597 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2598 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2599 } else {
2600 temp &= ~FDI_LINK_TRAIN_NONE;
2601 temp |= FDI_LINK_TRAIN_PATTERN_1;
2602 }
2603 /* BPC in FDI rx is consistent with that in PIPECONF */
2604 temp &= ~(0x07 << 16);
2605 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2606 I915_WRITE(reg, temp);
2607
2608 POSTING_READ(reg);
2609 udelay(100);
2610}
2611
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002612static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2613{
Chris Wilson0f911282012-04-17 10:05:38 +01002614 struct drm_device *dev = crtc->dev;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002615
2616 if (crtc->fb == NULL)
2617 return;
2618
Chris Wilson0f911282012-04-17 10:05:38 +01002619 mutex_lock(&dev->struct_mutex);
2620 intel_finish_fb(crtc->fb);
2621 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002622}
2623
Jesse Barnes040484a2011-01-03 12:14:26 -08002624static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
2625{
2626 struct drm_device *dev = crtc->dev;
2627 struct drm_mode_config *mode_config = &dev->mode_config;
2628 struct intel_encoder *encoder;
2629
2630 /*
2631 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2632 * must be driven by its own crtc; no sharing is possible.
2633 */
2634 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
2635 if (encoder->base.crtc != crtc)
2636 continue;
2637
Eugeni Dodonov6ee8bab2012-05-09 20:30:31 -03002638 /* On Haswell, LPT PCH handles the VGA connection via FDI, and Haswell
2639 * CPU handles all others */
2640 if (IS_HASWELL(dev)) {
2641 /* It is still unclear how this will work on PPT, so throw up a warning */
2642 WARN_ON(!HAS_PCH_LPT(dev));
2643
2644 if (encoder->type == DRM_MODE_ENCODER_DAC) {
2645 DRM_DEBUG_KMS("Haswell detected DAC encoder, assuming is PCH\n");
2646 return true;
2647 } else {
2648 DRM_DEBUG_KMS("Haswell detected encoder %d, assuming is CPU\n",
2649 encoder->type);
2650 return false;
2651 }
2652 }
2653
Jesse Barnes040484a2011-01-03 12:14:26 -08002654 switch (encoder->type) {
2655 case INTEL_OUTPUT_EDP:
2656 if (!intel_encoder_is_pch_edp(&encoder->base))
2657 return false;
2658 continue;
2659 }
2660 }
2661
2662 return true;
2663}
2664
Jesse Barnesf67a5592011-01-05 10:31:48 -08002665/*
2666 * Enable PCH resources required for PCH ports:
2667 * - PCH PLLs
2668 * - FDI training & RX/TX
2669 * - update transcoder timings
2670 * - DP transcoding bits
2671 * - transcoder
2672 */
2673static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002674{
2675 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002676 struct drm_i915_private *dev_priv = dev->dev_private;
2677 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2678 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002679 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002680
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002681 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07002682 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002683
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002684 intel_enable_pch_pll(intel_crtc);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002685
2686 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002687 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07002688
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002689 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002690 switch (pipe) {
2691 default:
2692 case 0:
2693 temp |= TRANSA_DPLL_ENABLE;
2694 sel = TRANSA_DPLLB_SEL;
2695 break;
2696 case 1:
2697 temp |= TRANSB_DPLL_ENABLE;
2698 sel = TRANSB_DPLLB_SEL;
2699 break;
2700 case 2:
2701 temp |= TRANSC_DPLL_ENABLE;
2702 sel = TRANSC_DPLLB_SEL;
2703 break;
Jesse Barnesd64311a2011-10-12 15:01:33 -07002704 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002705 if (intel_crtc->pch_pll->pll_reg == _PCH_DPLL_B)
2706 temp |= sel;
2707 else
2708 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002709 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002710 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002711
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08002712 /* set transcoder timing, panel must allow it */
2713 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01002714 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
2715 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
2716 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
2717
2718 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
2719 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
2720 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Daniel Vetter0529a0d2012-01-28 14:49:24 +01002721 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002722
Eugeni Dodonovf57e1e32012-05-09 15:37:14 -03002723 if (!IS_HASWELL(dev))
2724 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002725
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002726 /* For PCH DP, enable TRANS_DP_CTL */
2727 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07002728 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
2729 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnes9325c9f2011-06-24 12:19:21 -07002730 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01002731 reg = TRANS_DP_CTL(pipe);
2732 temp = I915_READ(reg);
2733 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08002734 TRANS_DP_SYNC_MASK |
2735 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01002736 temp |= (TRANS_DP_OUTPUT_ENABLE |
2737 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07002738 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002739
2740 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01002741 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002742 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01002743 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002744
2745 switch (intel_trans_dp_port_sel(crtc)) {
2746 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01002747 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002748 break;
2749 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01002750 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002751 break;
2752 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01002753 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002754 break;
2755 default:
2756 DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002757 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002758 break;
2759 }
2760
Chris Wilson5eddb702010-09-11 13:48:45 +01002761 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002762 }
2763
Jesse Barnes040484a2011-01-03 12:14:26 -08002764 intel_enable_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08002765}
2766
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002767static void intel_put_pch_pll(struct intel_crtc *intel_crtc)
2768{
2769 struct intel_pch_pll *pll = intel_crtc->pch_pll;
2770
2771 if (pll == NULL)
2772 return;
2773
2774 if (pll->refcount == 0) {
2775 WARN(1, "bad PCH PLL refcount\n");
2776 return;
2777 }
2778
2779 --pll->refcount;
2780 intel_crtc->pch_pll = NULL;
2781}
2782
2783static struct intel_pch_pll *intel_get_pch_pll(struct intel_crtc *intel_crtc, u32 dpll, u32 fp)
2784{
2785 struct drm_i915_private *dev_priv = intel_crtc->base.dev->dev_private;
2786 struct intel_pch_pll *pll;
2787 int i;
2788
2789 pll = intel_crtc->pch_pll;
2790 if (pll) {
2791 DRM_DEBUG_KMS("CRTC:%d reusing existing PCH PLL %x\n",
2792 intel_crtc->base.base.id, pll->pll_reg);
2793 goto prepare;
2794 }
2795
2796 for (i = 0; i < dev_priv->num_pch_pll; i++) {
2797 pll = &dev_priv->pch_plls[i];
2798
2799 /* Only want to check enabled timings first */
2800 if (pll->refcount == 0)
2801 continue;
2802
2803 if (dpll == (I915_READ(pll->pll_reg) & 0x7fffffff) &&
2804 fp == I915_READ(pll->fp0_reg)) {
2805 DRM_DEBUG_KMS("CRTC:%d sharing existing PCH PLL %x (refcount %d, ative %d)\n",
2806 intel_crtc->base.base.id,
2807 pll->pll_reg, pll->refcount, pll->active);
2808
2809 goto found;
2810 }
2811 }
2812
2813 /* Ok no matching timings, maybe there's a free one? */
2814 for (i = 0; i < dev_priv->num_pch_pll; i++) {
2815 pll = &dev_priv->pch_plls[i];
2816 if (pll->refcount == 0) {
2817 DRM_DEBUG_KMS("CRTC:%d allocated PCH PLL %x\n",
2818 intel_crtc->base.base.id, pll->pll_reg);
2819 goto found;
2820 }
2821 }
2822
2823 return NULL;
2824
2825found:
2826 intel_crtc->pch_pll = pll;
2827 pll->refcount++;
2828 DRM_DEBUG_DRIVER("using pll %d for pipe %d\n", i, intel_crtc->pipe);
2829prepare: /* separate function? */
2830 DRM_DEBUG_DRIVER("switching PLL %x off\n", pll->pll_reg);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002831
Chris Wilsone04c7352012-05-02 20:43:56 +01002832 /* Wait for the clocks to stabilize before rewriting the regs */
2833 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002834 POSTING_READ(pll->pll_reg);
2835 udelay(150);
Chris Wilsone04c7352012-05-02 20:43:56 +01002836
2837 I915_WRITE(pll->fp0_reg, fp);
2838 I915_WRITE(pll->pll_reg, dpll & ~DPLL_VCO_ENABLE);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002839 pll->on = false;
2840 return pll;
2841}
2842
Jesse Barnesd4270e52011-10-11 10:43:02 -07002843void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
2844{
2845 struct drm_i915_private *dev_priv = dev->dev_private;
2846 int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
2847 u32 temp;
2848
2849 temp = I915_READ(dslreg);
2850 udelay(500);
2851 if (wait_for(I915_READ(dslreg) != temp, 5)) {
2852 /* Without this, mode sets may fail silently on FDI */
2853 I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
2854 udelay(250);
2855 I915_WRITE(tc2reg, 0);
2856 if (wait_for(I915_READ(dslreg) != temp, 5))
2857 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
2858 }
2859}
2860
Jesse Barnesf67a5592011-01-05 10:31:48 -08002861static void ironlake_crtc_enable(struct drm_crtc *crtc)
2862{
2863 struct drm_device *dev = crtc->dev;
2864 struct drm_i915_private *dev_priv = dev->dev_private;
2865 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2866 int pipe = intel_crtc->pipe;
2867 int plane = intel_crtc->plane;
2868 u32 temp;
2869 bool is_pch_port;
2870
2871 if (intel_crtc->active)
2872 return;
2873
2874 intel_crtc->active = true;
2875 intel_update_watermarks(dev);
2876
2877 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
2878 temp = I915_READ(PCH_LVDS);
2879 if ((temp & LVDS_PORT_EN) == 0)
2880 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
2881 }
2882
2883 is_pch_port = intel_crtc_driving_pch(crtc);
2884
2885 if (is_pch_port)
Jesse Barnes357555c2011-04-28 15:09:55 -07002886 ironlake_fdi_pll_enable(crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08002887 else
2888 ironlake_fdi_disable(crtc);
2889
2890 /* Enable panel fitting for LVDS */
2891 if (dev_priv->pch_pf_size &&
2892 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
2893 /* Force use of hard-coded filter coefficients
2894 * as some pre-programmed values are broken,
2895 * e.g. x201.
2896 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002897 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
2898 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
2899 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08002900 }
2901
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02002902 /*
2903 * On ILK+ LUT must be loaded before the pipe is running but with
2904 * clocks enabled
2905 */
2906 intel_crtc_load_lut(crtc);
2907
Jesse Barnesf67a5592011-01-05 10:31:48 -08002908 intel_enable_pipe(dev_priv, pipe, is_pch_port);
2909 intel_enable_plane(dev_priv, plane, pipe);
2910
2911 if (is_pch_port)
2912 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002913
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01002914 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01002915 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01002916 mutex_unlock(&dev->struct_mutex);
2917
Chris Wilson6b383a72010-09-13 13:54:26 +01002918 intel_crtc_update_cursor(crtc, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002919}
2920
2921static void ironlake_crtc_disable(struct drm_crtc *crtc)
2922{
2923 struct drm_device *dev = crtc->dev;
2924 struct drm_i915_private *dev_priv = dev->dev_private;
2925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2926 int pipe = intel_crtc->pipe;
2927 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002928 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002929
Chris Wilsonf7abfe82010-09-13 14:19:16 +01002930 if (!intel_crtc->active)
2931 return;
2932
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002933 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002934 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01002935 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01002936
Jesse Barnesb24e7172011-01-04 15:09:30 -08002937 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002938
Chris Wilson973d04f2011-07-08 12:22:37 +01002939 if (dev_priv->cfb_plane == plane)
2940 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002941
Jesse Barnesb24e7172011-01-04 15:09:30 -08002942 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002943
Jesse Barnes6be4a602010-09-10 10:26:01 -07002944 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002945 I915_WRITE(PF_CTL(pipe), 0);
2946 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002947
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002948 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002949
Jesse Barnes47a05ec2011-02-07 13:46:40 -08002950 /* This is a horrible layering violation; we should be doing this in
2951 * the connector/encoder ->prepare instead, but we don't always have
2952 * enough information there about the config to know whether it will
2953 * actually be necessary or just cause undesired flicker.
2954 */
2955 intel_disable_pch_ports(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002956
Jesse Barnes040484a2011-01-03 12:14:26 -08002957 intel_disable_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002958
Jesse Barnes6be4a602010-09-10 10:26:01 -07002959 if (HAS_PCH_CPT(dev)) {
2960 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002961 reg = TRANS_DP_CTL(pipe);
2962 temp = I915_READ(reg);
2963 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08002964 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01002965 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002966
2967 /* disable DPLL_SEL */
2968 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002969 switch (pipe) {
2970 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07002971 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002972 break;
2973 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07002974 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002975 break;
2976 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07002977 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07002978 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08002979 break;
2980 default:
2981 BUG(); /* wtf */
2982 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07002983 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002984 }
2985
2986 /* disable PCH DPLL */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01002987 intel_disable_pch_pll(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002988
2989 /* Switch from PCDclk to Rawclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002990 reg = FDI_RX_CTL(pipe);
2991 temp = I915_READ(reg);
2992 I915_WRITE(reg, temp & ~FDI_PCDCLK);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002993
2994 /* Disable CPU FDI TX PLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01002995 reg = FDI_TX_CTL(pipe);
2996 temp = I915_READ(reg);
2997 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
2998
2999 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003000 udelay(100);
3001
Chris Wilson5eddb702010-09-11 13:48:45 +01003002 reg = FDI_RX_CTL(pipe);
3003 temp = I915_READ(reg);
3004 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003005
3006 /* Wait for the clocks to turn off. */
Chris Wilson5eddb702010-09-11 13:48:45 +01003007 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003008 udelay(100);
Chris Wilson6b383a72010-09-13 13:54:26 +01003009
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003010 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003011 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003012
3013 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003014 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003015 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003016}
3017
3018static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
3019{
3020 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3021 int pipe = intel_crtc->pipe;
3022 int plane = intel_crtc->plane;
3023
Zhenyu Wang2c072452009-06-05 15:38:42 +08003024 /* XXX: When our outputs are all unaware of DPMS modes other than off
3025 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
3026 */
3027 switch (mode) {
3028 case DRM_MODE_DPMS_ON:
3029 case DRM_MODE_DPMS_STANDBY:
3030 case DRM_MODE_DPMS_SUSPEND:
Chris Wilson868dc582010-08-07 11:01:31 +01003031 DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003032 ironlake_crtc_enable(crtc);
Chris Wilson868dc582010-08-07 11:01:31 +01003033 break;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08003034
Zhenyu Wang2c072452009-06-05 15:38:42 +08003035 case DRM_MODE_DPMS_OFF:
Chris Wilson868dc582010-08-07 11:01:31 +01003036 DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003037 ironlake_crtc_disable(crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08003038 break;
3039 }
3040}
3041
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003042static void ironlake_crtc_off(struct drm_crtc *crtc)
3043{
3044 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3045 intel_put_pch_pll(intel_crtc);
3046}
3047
Daniel Vetter02e792f2009-09-15 22:57:34 +02003048static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3049{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003050 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003051 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003052 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003053
Chris Wilson23f09ce2010-08-12 13:53:37 +01003054 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003055 dev_priv->mm.interruptible = false;
3056 (void) intel_overlay_switch_off(intel_crtc->overlay);
3057 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003058 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003059 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003060
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003061 /* Let userspace switch the overlay on again. In most cases userspace
3062 * has to recompute where to put it anyway.
3063 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003064}
3065
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003066static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003067{
3068 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003069 struct drm_i915_private *dev_priv = dev->dev_private;
3070 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3071 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003072 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003073
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003074 if (intel_crtc->active)
3075 return;
3076
3077 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003078 intel_update_watermarks(dev);
3079
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003080 intel_enable_pll(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08003081 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003082 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003083
3084 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003085 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003086
3087 /* Give the overlay scaler a chance to enable if it's on this pipe */
3088 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003089 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003090}
3091
3092static void i9xx_crtc_disable(struct drm_crtc *crtc)
3093{
3094 struct drm_device *dev = crtc->dev;
3095 struct drm_i915_private *dev_priv = dev->dev_private;
3096 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3097 int pipe = intel_crtc->pipe;
3098 int plane = intel_crtc->plane;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003099
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003100 if (!intel_crtc->active)
3101 return;
3102
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003103 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003104 intel_crtc_wait_for_pending_flips(crtc);
3105 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003106 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003107 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003108
Chris Wilson973d04f2011-07-08 12:22:37 +01003109 if (dev_priv->cfb_plane == plane)
3110 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003111
Jesse Barnesb24e7172011-01-04 15:09:30 -08003112 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003113 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003114 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003115
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003116 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003117 intel_update_fbc(dev);
3118 intel_update_watermarks(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003119}
3120
3121static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
3122{
Jesse Barnes79e53942008-11-07 14:24:08 -08003123 /* XXX: When our outputs are all unaware of DPMS modes other than off
3124 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
3125 */
3126 switch (mode) {
3127 case DRM_MODE_DPMS_ON:
3128 case DRM_MODE_DPMS_STANDBY:
3129 case DRM_MODE_DPMS_SUSPEND:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003130 i9xx_crtc_enable(crtc);
3131 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08003132 case DRM_MODE_DPMS_OFF:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003133 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003134 break;
3135 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08003136}
3137
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003138static void i9xx_crtc_off(struct drm_crtc *crtc)
3139{
3140}
3141
Zhenyu Wang2c072452009-06-05 15:38:42 +08003142/**
3143 * Sets the power management mode of the pipe and plane.
Zhenyu Wang2c072452009-06-05 15:38:42 +08003144 */
3145static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
3146{
3147 struct drm_device *dev = crtc->dev;
Jesse Barnese70236a2009-09-21 10:42:27 -07003148 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003149 struct drm_i915_master_private *master_priv;
3150 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3151 int pipe = intel_crtc->pipe;
3152 bool enabled;
3153
Chris Wilson032d2a02010-09-06 16:17:22 +01003154 if (intel_crtc->dpms_mode == mode)
3155 return;
3156
Chris Wilsondebcadd2010-08-07 11:01:33 +01003157 intel_crtc->dpms_mode = mode;
Chris Wilsondebcadd2010-08-07 11:01:33 +01003158
Jesse Barnese70236a2009-09-21 10:42:27 -07003159 dev_priv->display.dpms(crtc, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08003160
3161 if (!dev->primary->master)
3162 return;
3163
3164 master_priv = dev->primary->master->driver_priv;
3165 if (!master_priv->sarea_priv)
3166 return;
3167
3168 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
3169
3170 switch (pipe) {
3171 case 0:
3172 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3173 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3174 break;
3175 case 1:
3176 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3177 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3178 break;
3179 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003180 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003181 break;
3182 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003183}
3184
Chris Wilsoncdd59982010-09-08 16:30:16 +01003185static void intel_crtc_disable(struct drm_crtc *crtc)
3186{
3187 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
3188 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003189 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsoncdd59982010-09-08 16:30:16 +01003190
3191 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003192 dev_priv->display.off(crtc);
3193
Chris Wilson931872f2012-01-16 23:01:13 +00003194 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3195 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003196
3197 if (crtc->fb) {
3198 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003199 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003200 mutex_unlock(&dev->struct_mutex);
3201 }
3202}
3203
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003204/* Prepare for a mode set.
3205 *
3206 * Note we could be a lot smarter here. We need to figure out which outputs
3207 * will be enabled, which disabled (in short, how the config will changes)
3208 * and perform the minimum necessary steps to accomplish that, e.g. updating
3209 * watermarks, FBC configuration, making sure PLLs are programmed correctly,
3210 * panel fitting is in the proper state, etc.
3211 */
3212static void i9xx_crtc_prepare(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003213{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003214 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003215}
3216
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003217static void i9xx_crtc_commit(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003218{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003219 i9xx_crtc_enable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003220}
3221
3222static void ironlake_crtc_prepare(struct drm_crtc *crtc)
3223{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003224 ironlake_crtc_disable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003225}
3226
3227static void ironlake_crtc_commit(struct drm_crtc *crtc)
3228{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003229 ironlake_crtc_enable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003230}
3231
Akshay Joshi0206e352011-08-16 15:34:10 -04003232void intel_encoder_prepare(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003233{
3234 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3235 /* lvds has its own version of prepare see intel_lvds_prepare */
3236 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
3237}
3238
Akshay Joshi0206e352011-08-16 15:34:10 -04003239void intel_encoder_commit(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003240{
3241 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
Jesse Barnesd4270e52011-10-11 10:43:02 -07003242 struct drm_device *dev = encoder->dev;
Paulo Zanonid47d7cb2012-05-04 17:18:23 -03003243 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->crtc);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003244
Jesse Barnes79e53942008-11-07 14:24:08 -08003245 /* lvds has its own version of commit see intel_lvds_commit */
3246 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003247
3248 if (HAS_PCH_CPT(dev))
3249 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08003250}
3251
Chris Wilsonea5b2132010-08-04 13:50:23 +01003252void intel_encoder_destroy(struct drm_encoder *encoder)
3253{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003254 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003255
Chris Wilsonea5b2132010-08-04 13:50:23 +01003256 drm_encoder_cleanup(encoder);
3257 kfree(intel_encoder);
3258}
3259
Jesse Barnes79e53942008-11-07 14:24:08 -08003260static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
3261 struct drm_display_mode *mode,
3262 struct drm_display_mode *adjusted_mode)
3263{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003264 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01003265
Eric Anholtbad720f2009-10-22 16:11:14 -07003266 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08003267 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07003268 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3269 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003270 }
Chris Wilson89749352010-09-12 18:25:19 +01003271
Daniel Vetterf9bef082012-04-15 19:53:19 +02003272 /* All interlaced capable intel hw wants timings in frames. Note though
3273 * that intel_lvds_mode_fixup does some funny tricks with the crtc
3274 * timings, so we need to be careful not to clobber these.*/
3275 if (!(adjusted_mode->private_flags & INTEL_MODE_CRTC_TIMINGS_SET))
3276 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01003277
Jesse Barnes79e53942008-11-07 14:24:08 -08003278 return true;
3279}
3280
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07003281static int valleyview_get_display_clock_speed(struct drm_device *dev)
3282{
3283 return 400000; /* FIXME */
3284}
3285
Jesse Barnese70236a2009-09-21 10:42:27 -07003286static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003287{
Jesse Barnese70236a2009-09-21 10:42:27 -07003288 return 400000;
3289}
Jesse Barnes79e53942008-11-07 14:24:08 -08003290
Jesse Barnese70236a2009-09-21 10:42:27 -07003291static int i915_get_display_clock_speed(struct drm_device *dev)
3292{
3293 return 333000;
3294}
Jesse Barnes79e53942008-11-07 14:24:08 -08003295
Jesse Barnese70236a2009-09-21 10:42:27 -07003296static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3297{
3298 return 200000;
3299}
Jesse Barnes79e53942008-11-07 14:24:08 -08003300
Jesse Barnese70236a2009-09-21 10:42:27 -07003301static int i915gm_get_display_clock_speed(struct drm_device *dev)
3302{
3303 u16 gcfgc = 0;
3304
3305 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3306
3307 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08003308 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07003309 else {
3310 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3311 case GC_DISPLAY_CLOCK_333_MHZ:
3312 return 333000;
3313 default:
3314 case GC_DISPLAY_CLOCK_190_200_MHZ:
3315 return 190000;
3316 }
3317 }
3318}
Jesse Barnes79e53942008-11-07 14:24:08 -08003319
Jesse Barnese70236a2009-09-21 10:42:27 -07003320static int i865_get_display_clock_speed(struct drm_device *dev)
3321{
3322 return 266000;
3323}
3324
3325static int i855_get_display_clock_speed(struct drm_device *dev)
3326{
3327 u16 hpllcc = 0;
3328 /* Assume that the hardware is in the high speed state. This
3329 * should be the default.
3330 */
3331 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3332 case GC_CLOCK_133_200:
3333 case GC_CLOCK_100_200:
3334 return 200000;
3335 case GC_CLOCK_166_250:
3336 return 250000;
3337 case GC_CLOCK_100_133:
3338 return 133000;
3339 }
3340
3341 /* Shouldn't happen */
3342 return 0;
3343}
3344
3345static int i830_get_display_clock_speed(struct drm_device *dev)
3346{
3347 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08003348}
3349
Zhenyu Wang2c072452009-06-05 15:38:42 +08003350struct fdi_m_n {
3351 u32 tu;
3352 u32 gmch_m;
3353 u32 gmch_n;
3354 u32 link_m;
3355 u32 link_n;
3356};
3357
3358static void
3359fdi_reduce_ratio(u32 *num, u32 *den)
3360{
3361 while (*num > 0xffffff || *den > 0xffffff) {
3362 *num >>= 1;
3363 *den >>= 1;
3364 }
3365}
3366
Zhenyu Wang2c072452009-06-05 15:38:42 +08003367static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003368ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
3369 int link_clock, struct fdi_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003370{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003371 m_n->tu = 64; /* default size */
3372
Chris Wilson22ed1112010-12-04 01:01:29 +00003373 /* BUG_ON(pixel_clock > INT_MAX / 36); */
3374 m_n->gmch_m = bits_per_pixel * pixel_clock;
3375 m_n->gmch_n = link_clock * nlanes * 8;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003376 fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
3377
Chris Wilson22ed1112010-12-04 01:01:29 +00003378 m_n->link_m = pixel_clock;
3379 m_n->link_n = link_clock;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003380 fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
3381}
3382
Chris Wilsona7615032011-01-12 17:04:08 +00003383static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
3384{
Keith Packard72bbe582011-09-26 16:09:45 -07003385 if (i915_panel_use_ssc >= 0)
3386 return i915_panel_use_ssc != 0;
3387 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07003388 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00003389}
3390
Jesse Barnes5a354202011-06-24 12:19:22 -07003391/**
3392 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
3393 * @crtc: CRTC structure
Adam Jackson3b5c78a2011-12-13 15:41:00 -08003394 * @mode: requested mode
Jesse Barnes5a354202011-06-24 12:19:22 -07003395 *
3396 * A pipe may be connected to one or more outputs. Based on the depth of the
3397 * attached framebuffer, choose a good color depth to use on the pipe.
3398 *
3399 * If possible, match the pipe depth to the fb depth. In some cases, this
3400 * isn't ideal, because the connected output supports a lesser or restricted
3401 * set of depths. Resolve that here:
3402 * LVDS typically supports only 6bpc, so clamp down in that case
3403 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
3404 * Displays may support a restricted set as well, check EDID and clamp as
3405 * appropriate.
Adam Jackson3b5c78a2011-12-13 15:41:00 -08003406 * DP may want to dither down to 6bpc to fit larger modes
Jesse Barnes5a354202011-06-24 12:19:22 -07003407 *
3408 * RETURNS:
3409 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
3410 * true if they don't match).
3411 */
3412static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
Adam Jackson3b5c78a2011-12-13 15:41:00 -08003413 unsigned int *pipe_bpp,
3414 struct drm_display_mode *mode)
Jesse Barnes5a354202011-06-24 12:19:22 -07003415{
3416 struct drm_device *dev = crtc->dev;
3417 struct drm_i915_private *dev_priv = dev->dev_private;
3418 struct drm_encoder *encoder;
3419 struct drm_connector *connector;
3420 unsigned int display_bpc = UINT_MAX, bpc;
3421
3422 /* Walk the encoders & connectors on this crtc, get min bpc */
3423 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
3424 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3425
3426 if (encoder->crtc != crtc)
3427 continue;
3428
3429 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
3430 unsigned int lvds_bpc;
3431
3432 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
3433 LVDS_A3_POWER_UP)
3434 lvds_bpc = 8;
3435 else
3436 lvds_bpc = 6;
3437
3438 if (lvds_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04003439 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07003440 display_bpc = lvds_bpc;
3441 }
3442 continue;
3443 }
3444
3445 if (intel_encoder->type == INTEL_OUTPUT_EDP) {
3446 /* Use VBT settings if we have an eDP panel */
3447 unsigned int edp_bpc = dev_priv->edp.bpp / 3;
3448
3449 if (edp_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04003450 DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07003451 display_bpc = edp_bpc;
3452 }
3453 continue;
3454 }
3455
3456 /* Not one of the known troublemakers, check the EDID */
3457 list_for_each_entry(connector, &dev->mode_config.connector_list,
3458 head) {
3459 if (connector->encoder != encoder)
3460 continue;
3461
Jesse Barnes62ac41a2011-07-28 12:55:14 -07003462 /* Don't use an invalid EDID bpc value */
3463 if (connector->display_info.bpc &&
3464 connector->display_info.bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04003465 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 -07003466 display_bpc = connector->display_info.bpc;
3467 }
3468 }
3469
3470 /*
3471 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
3472 * through, clamp it down. (Note: >12bpc will be caught below.)
3473 */
3474 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
3475 if (display_bpc > 8 && display_bpc < 12) {
Adam Jackson82820492011-10-10 16:33:34 -04003476 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07003477 display_bpc = 12;
3478 } else {
Adam Jackson82820492011-10-10 16:33:34 -04003479 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07003480 display_bpc = 8;
3481 }
3482 }
3483 }
3484
Adam Jackson3b5c78a2011-12-13 15:41:00 -08003485 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
3486 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
3487 display_bpc = 6;
3488 }
3489
Jesse Barnes5a354202011-06-24 12:19:22 -07003490 /*
3491 * We could just drive the pipe at the highest bpc all the time and
3492 * enable dithering as needed, but that costs bandwidth. So choose
3493 * the minimum value that expresses the full color range of the fb but
3494 * also stays within the max display bpc discovered above.
3495 */
3496
3497 switch (crtc->fb->depth) {
3498 case 8:
3499 bpc = 8; /* since we go through a colormap */
3500 break;
3501 case 15:
3502 case 16:
3503 bpc = 6; /* min is 18bpp */
3504 break;
3505 case 24:
Keith Packard578393c2011-09-05 11:53:21 -07003506 bpc = 8;
Jesse Barnes5a354202011-06-24 12:19:22 -07003507 break;
3508 case 30:
Keith Packard578393c2011-09-05 11:53:21 -07003509 bpc = 10;
Jesse Barnes5a354202011-06-24 12:19:22 -07003510 break;
3511 case 48:
Keith Packard578393c2011-09-05 11:53:21 -07003512 bpc = 12;
Jesse Barnes5a354202011-06-24 12:19:22 -07003513 break;
3514 default:
3515 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
3516 bpc = min((unsigned int)8, display_bpc);
3517 break;
3518 }
3519
Keith Packard578393c2011-09-05 11:53:21 -07003520 display_bpc = min(display_bpc, bpc);
3521
Adam Jackson82820492011-10-10 16:33:34 -04003522 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
3523 bpc, display_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07003524
Keith Packard578393c2011-09-05 11:53:21 -07003525 *pipe_bpp = display_bpc * 3;
Jesse Barnes5a354202011-06-24 12:19:22 -07003526
3527 return display_bpc != bpc;
3528}
3529
Jesse Barnesc65d77d2011-12-15 12:30:36 -08003530static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
3531{
3532 struct drm_device *dev = crtc->dev;
3533 struct drm_i915_private *dev_priv = dev->dev_private;
3534 int refclk;
3535
3536 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
3537 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
3538 refclk = dev_priv->lvds_ssc_freq * 1000;
3539 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
3540 refclk / 1000);
3541 } else if (!IS_GEN2(dev)) {
3542 refclk = 96000;
3543 } else {
3544 refclk = 48000;
3545 }
3546
3547 return refclk;
3548}
3549
3550static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
3551 intel_clock_t *clock)
3552{
3553 /* SDVO TV has fixed PLL values depend on its clock range,
3554 this mirrors vbios setting. */
3555 if (adjusted_mode->clock >= 100000
3556 && adjusted_mode->clock < 140500) {
3557 clock->p1 = 2;
3558 clock->p2 = 10;
3559 clock->n = 3;
3560 clock->m1 = 16;
3561 clock->m2 = 8;
3562 } else if (adjusted_mode->clock >= 140500
3563 && adjusted_mode->clock <= 200000) {
3564 clock->p1 = 1;
3565 clock->p2 = 10;
3566 clock->n = 6;
3567 clock->m1 = 12;
3568 clock->m2 = 8;
3569 }
3570}
3571
Jesse Barnesa7516a02011-12-15 12:30:37 -08003572static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
3573 intel_clock_t *clock,
3574 intel_clock_t *reduced_clock)
3575{
3576 struct drm_device *dev = crtc->dev;
3577 struct drm_i915_private *dev_priv = dev->dev_private;
3578 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3579 int pipe = intel_crtc->pipe;
3580 u32 fp, fp2 = 0;
3581
3582 if (IS_PINEVIEW(dev)) {
3583 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
3584 if (reduced_clock)
3585 fp2 = (1 << reduced_clock->n) << 16 |
3586 reduced_clock->m1 << 8 | reduced_clock->m2;
3587 } else {
3588 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
3589 if (reduced_clock)
3590 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
3591 reduced_clock->m2;
3592 }
3593
3594 I915_WRITE(FP0(pipe), fp);
3595
3596 intel_crtc->lowfreq_avail = false;
3597 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
3598 reduced_clock && i915_powersave) {
3599 I915_WRITE(FP1(pipe), fp2);
3600 intel_crtc->lowfreq_avail = true;
3601 } else {
3602 I915_WRITE(FP1(pipe), fp);
3603 }
3604}
3605
Daniel Vetter93e537a2012-03-28 23:11:26 +02003606static void intel_update_lvds(struct drm_crtc *crtc, intel_clock_t *clock,
3607 struct drm_display_mode *adjusted_mode)
3608{
3609 struct drm_device *dev = crtc->dev;
3610 struct drm_i915_private *dev_priv = dev->dev_private;
3611 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3612 int pipe = intel_crtc->pipe;
Chris Wilson284d5df2012-04-14 17:41:59 +01003613 u32 temp;
Daniel Vetter93e537a2012-03-28 23:11:26 +02003614
3615 temp = I915_READ(LVDS);
3616 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
3617 if (pipe == 1) {
3618 temp |= LVDS_PIPEB_SELECT;
3619 } else {
3620 temp &= ~LVDS_PIPEB_SELECT;
3621 }
3622 /* set the corresponsding LVDS_BORDER bit */
3623 temp |= dev_priv->lvds_border_bits;
3624 /* Set the B0-B3 data pairs corresponding to whether we're going to
3625 * set the DPLLs for dual-channel mode or not.
3626 */
3627 if (clock->p2 == 7)
3628 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
3629 else
3630 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
3631
3632 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
3633 * appropriately here, but we need to look more thoroughly into how
3634 * panels behave in the two modes.
3635 */
3636 /* set the dithering flag on LVDS as needed */
3637 if (INTEL_INFO(dev)->gen >= 4) {
3638 if (dev_priv->lvds_dither)
3639 temp |= LVDS_ENABLE_DITHER;
3640 else
3641 temp &= ~LVDS_ENABLE_DITHER;
3642 }
Chris Wilson284d5df2012-04-14 17:41:59 +01003643 temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
Daniel Vetter93e537a2012-03-28 23:11:26 +02003644 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
Chris Wilson284d5df2012-04-14 17:41:59 +01003645 temp |= LVDS_HSYNC_POLARITY;
Daniel Vetter93e537a2012-03-28 23:11:26 +02003646 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
Chris Wilson284d5df2012-04-14 17:41:59 +01003647 temp |= LVDS_VSYNC_POLARITY;
Daniel Vetter93e537a2012-03-28 23:11:26 +02003648 I915_WRITE(LVDS, temp);
3649}
3650
Daniel Vettereb1cbe42012-03-28 23:12:16 +02003651static void i9xx_update_pll(struct drm_crtc *crtc,
3652 struct drm_display_mode *mode,
3653 struct drm_display_mode *adjusted_mode,
3654 intel_clock_t *clock, intel_clock_t *reduced_clock,
3655 int num_connectors)
3656{
3657 struct drm_device *dev = crtc->dev;
3658 struct drm_i915_private *dev_priv = dev->dev_private;
3659 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3660 int pipe = intel_crtc->pipe;
3661 u32 dpll;
3662 bool is_sdvo;
3663
3664 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
3665 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
3666
3667 dpll = DPLL_VGA_MODE_DIS;
3668
3669 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
3670 dpll |= DPLLB_MODE_LVDS;
3671 else
3672 dpll |= DPLLB_MODE_DAC_SERIAL;
3673 if (is_sdvo) {
3674 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
3675 if (pixel_multiplier > 1) {
3676 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
3677 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
3678 }
3679 dpll |= DPLL_DVO_HIGH_SPEED;
3680 }
3681 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
3682 dpll |= DPLL_DVO_HIGH_SPEED;
3683
3684 /* compute bitmask from p1 value */
3685 if (IS_PINEVIEW(dev))
3686 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
3687 else {
3688 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3689 if (IS_G4X(dev) && reduced_clock)
3690 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
3691 }
3692 switch (clock->p2) {
3693 case 5:
3694 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
3695 break;
3696 case 7:
3697 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
3698 break;
3699 case 10:
3700 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
3701 break;
3702 case 14:
3703 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
3704 break;
3705 }
3706 if (INTEL_INFO(dev)->gen >= 4)
3707 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
3708
3709 if (is_sdvo && intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
3710 dpll |= PLL_REF_INPUT_TVCLKINBC;
3711 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
3712 /* XXX: just matching BIOS for now */
3713 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
3714 dpll |= 3;
3715 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
3716 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
3717 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
3718 else
3719 dpll |= PLL_REF_INPUT_DREFCLK;
3720
3721 dpll |= DPLL_VCO_ENABLE;
3722 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
3723 POSTING_READ(DPLL(pipe));
3724 udelay(150);
3725
3726 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
3727 * This is an exception to the general rule that mode_set doesn't turn
3728 * things on.
3729 */
3730 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
3731 intel_update_lvds(crtc, clock, adjusted_mode);
3732
3733 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
3734 intel_dp_set_m_n(crtc, mode, adjusted_mode);
3735
3736 I915_WRITE(DPLL(pipe), dpll);
3737
3738 /* Wait for the clocks to stabilize. */
3739 POSTING_READ(DPLL(pipe));
3740 udelay(150);
3741
3742 if (INTEL_INFO(dev)->gen >= 4) {
3743 u32 temp = 0;
3744 if (is_sdvo) {
3745 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
3746 if (temp > 1)
3747 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
3748 else
3749 temp = 0;
3750 }
3751 I915_WRITE(DPLL_MD(pipe), temp);
3752 } else {
3753 /* The pixel multiplier can only be updated once the
3754 * DPLL is enabled and the clocks are stable.
3755 *
3756 * So write it again.
3757 */
3758 I915_WRITE(DPLL(pipe), dpll);
3759 }
3760}
3761
3762static void i8xx_update_pll(struct drm_crtc *crtc,
3763 struct drm_display_mode *adjusted_mode,
3764 intel_clock_t *clock,
3765 int num_connectors)
3766{
3767 struct drm_device *dev = crtc->dev;
3768 struct drm_i915_private *dev_priv = dev->dev_private;
3769 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3770 int pipe = intel_crtc->pipe;
3771 u32 dpll;
3772
3773 dpll = DPLL_VGA_MODE_DIS;
3774
3775 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3776 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3777 } else {
3778 if (clock->p1 == 2)
3779 dpll |= PLL_P1_DIVIDE_BY_TWO;
3780 else
3781 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
3782 if (clock->p2 == 4)
3783 dpll |= PLL_P2_DIVIDE_BY_4;
3784 }
3785
3786 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_TVOUT))
3787 /* XXX: just matching BIOS for now */
3788 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
3789 dpll |= 3;
3790 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
3791 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
3792 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
3793 else
3794 dpll |= PLL_REF_INPUT_DREFCLK;
3795
3796 dpll |= DPLL_VCO_ENABLE;
3797 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
3798 POSTING_READ(DPLL(pipe));
3799 udelay(150);
3800
3801 I915_WRITE(DPLL(pipe), dpll);
3802
3803 /* Wait for the clocks to stabilize. */
3804 POSTING_READ(DPLL(pipe));
3805 udelay(150);
3806
3807 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
3808 * This is an exception to the general rule that mode_set doesn't turn
3809 * things on.
3810 */
3811 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
3812 intel_update_lvds(crtc, clock, adjusted_mode);
3813
3814 /* The pixel multiplier can only be updated once the
3815 * DPLL is enabled and the clocks are stable.
3816 *
3817 * So write it again.
3818 */
3819 I915_WRITE(DPLL(pipe), dpll);
3820}
3821
Eric Anholtf564048e2011-03-30 13:01:02 -07003822static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
3823 struct drm_display_mode *mode,
3824 struct drm_display_mode *adjusted_mode,
3825 int x, int y,
3826 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08003827{
3828 struct drm_device *dev = crtc->dev;
3829 struct drm_i915_private *dev_priv = dev->dev_private;
3830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3831 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003832 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07003833 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07003834 intel_clock_t clock, reduced_clock;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02003835 u32 dspcntr, pipeconf, vsyncshift;
3836 bool ok, has_reduced_clock = false, is_sdvo = false;
3837 bool is_lvds = false, is_tv = false, is_dp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08003838 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson5eddb702010-09-11 13:48:45 +01003839 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08003840 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00003841 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08003842
Chris Wilson5eddb702010-09-11 13:48:45 +01003843 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
3844 if (encoder->base.crtc != crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003845 continue;
3846
Chris Wilson5eddb702010-09-11 13:48:45 +01003847 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08003848 case INTEL_OUTPUT_LVDS:
3849 is_lvds = true;
3850 break;
3851 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08003852 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08003853 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01003854 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08003855 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08003856 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08003857 case INTEL_OUTPUT_TVOUT:
3858 is_tv = true;
3859 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07003860 case INTEL_OUTPUT_DISPLAYPORT:
3861 is_dp = true;
3862 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08003863 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05003864
Eric Anholtc751ce42010-03-25 11:48:48 -07003865 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08003866 }
3867
Jesse Barnesc65d77d2011-12-15 12:30:36 -08003868 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08003869
Ma Lingd4906092009-03-18 20:13:27 +08003870 /*
3871 * Returns a set of divisors for the desired target clock with the given
3872 * refclk, or FALSE. The returned values represent the clock equation:
3873 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
3874 */
Chris Wilson1b894b52010-12-14 20:04:54 +00003875 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08003876 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
3877 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08003878 if (!ok) {
3879 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07003880 return -EINVAL;
3881 }
3882
3883 /* Ensure that the cursor is valid for the new mode before changing... */
3884 intel_crtc_update_cursor(crtc, true);
3885
3886 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08003887 /*
3888 * Ensure we match the reduced clock's P to the target clock.
3889 * If the clocks don't match, we can't switch the display clock
3890 * by using the FP0/FP1. In such case we will disable the LVDS
3891 * downclock feature.
3892 */
Eric Anholtf564048e2011-03-30 13:01:02 -07003893 has_reduced_clock = limit->find_pll(limit, crtc,
3894 dev_priv->lvds_downclock,
3895 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08003896 &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07003897 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07003898 }
3899
Jesse Barnesc65d77d2011-12-15 12:30:36 -08003900 if (is_sdvo && is_tv)
3901 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07003902
Jesse Barnesa7516a02011-12-15 12:30:37 -08003903 i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
3904 &reduced_clock : NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07003905
Daniel Vettereb1cbe42012-03-28 23:12:16 +02003906 if (IS_GEN2(dev))
3907 i8xx_update_pll(crtc, adjusted_mode, &clock, num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07003908 else
Daniel Vettereb1cbe42012-03-28 23:12:16 +02003909 i9xx_update_pll(crtc, mode, adjusted_mode, &clock,
3910 has_reduced_clock ? &reduced_clock : NULL,
3911 num_connectors);
Eric Anholtf564048e2011-03-30 13:01:02 -07003912
3913 /* setup pipeconf */
3914 pipeconf = I915_READ(PIPECONF(pipe));
3915
3916 /* Set up the display plane register */
3917 dspcntr = DISPPLANE_GAMMA_ENABLE;
3918
Eric Anholt929c77f2011-03-30 13:01:04 -07003919 if (pipe == 0)
3920 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
3921 else
3922 dspcntr |= DISPPLANE_SEL_PIPE_B;
Eric Anholtf564048e2011-03-30 13:01:02 -07003923
3924 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
3925 /* Enable pixel doubling when the dot clock is > 90% of the (display)
3926 * core speed.
3927 *
3928 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
3929 * pipe == 0 check?
3930 */
3931 if (mode->clock >
3932 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
3933 pipeconf |= PIPECONF_DOUBLE_WIDE;
3934 else
3935 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
3936 }
3937
Adam Jackson3b5c78a2011-12-13 15:41:00 -08003938 /* default to 8bpc */
3939 pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
3940 if (is_dp) {
3941 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
3942 pipeconf |= PIPECONF_BPP_6 |
3943 PIPECONF_DITHER_EN |
3944 PIPECONF_DITHER_TYPE_SP;
3945 }
3946 }
3947
Eric Anholtf564048e2011-03-30 13:01:02 -07003948 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
3949 drm_mode_debug_printmodeline(mode);
3950
Jesse Barnesa7516a02011-12-15 12:30:37 -08003951 if (HAS_PIPE_CXSR(dev)) {
3952 if (intel_crtc->lowfreq_avail) {
Eric Anholtf564048e2011-03-30 13:01:02 -07003953 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
3954 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
Jesse Barnesa7516a02011-12-15 12:30:37 -08003955 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07003956 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
3957 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
3958 }
3959 }
3960
Keith Packard617cf882012-02-08 13:53:38 -08003961 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Daniel Vetterdbb02572012-01-28 14:49:23 +01003962 if (!IS_GEN2(dev) &&
3963 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Eric Anholtf564048e2011-03-30 13:01:02 -07003964 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
3965 /* the chip adds 2 halflines automatically */
Eric Anholtf564048e2011-03-30 13:01:02 -07003966 adjusted_mode->crtc_vtotal -= 1;
Eric Anholtf564048e2011-03-30 13:01:02 -07003967 adjusted_mode->crtc_vblank_end -= 1;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003968 vsyncshift = adjusted_mode->crtc_hsync_start
3969 - adjusted_mode->crtc_htotal/2;
3970 } else {
Keith Packard617cf882012-02-08 13:53:38 -08003971 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003972 vsyncshift = 0;
3973 }
3974
3975 if (!IS_GEN3(dev))
3976 I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
Eric Anholtf564048e2011-03-30 13:01:02 -07003977
3978 I915_WRITE(HTOTAL(pipe),
3979 (adjusted_mode->crtc_hdisplay - 1) |
3980 ((adjusted_mode->crtc_htotal - 1) << 16));
3981 I915_WRITE(HBLANK(pipe),
3982 (adjusted_mode->crtc_hblank_start - 1) |
3983 ((adjusted_mode->crtc_hblank_end - 1) << 16));
3984 I915_WRITE(HSYNC(pipe),
3985 (adjusted_mode->crtc_hsync_start - 1) |
3986 ((adjusted_mode->crtc_hsync_end - 1) << 16));
3987
3988 I915_WRITE(VTOTAL(pipe),
3989 (adjusted_mode->crtc_vdisplay - 1) |
3990 ((adjusted_mode->crtc_vtotal - 1) << 16));
3991 I915_WRITE(VBLANK(pipe),
3992 (adjusted_mode->crtc_vblank_start - 1) |
3993 ((adjusted_mode->crtc_vblank_end - 1) << 16));
3994 I915_WRITE(VSYNC(pipe),
3995 (adjusted_mode->crtc_vsync_start - 1) |
3996 ((adjusted_mode->crtc_vsync_end - 1) << 16));
3997
3998 /* pipesrc and dspsize control the size that is scaled from,
3999 * which should always be the user's requested size.
4000 */
Eric Anholt929c77f2011-03-30 13:01:04 -07004001 I915_WRITE(DSPSIZE(plane),
4002 ((mode->vdisplay - 1) << 16) |
4003 (mode->hdisplay - 1));
4004 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07004005 I915_WRITE(PIPESRC(pipe),
4006 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
4007
Eric Anholtf564048e2011-03-30 13:01:02 -07004008 I915_WRITE(PIPECONF(pipe), pipeconf);
4009 POSTING_READ(PIPECONF(pipe));
Eric Anholt929c77f2011-03-30 13:01:04 -07004010 intel_enable_pipe(dev_priv, pipe, false);
Eric Anholtf564048e2011-03-30 13:01:02 -07004011
4012 intel_wait_for_vblank(dev, pipe);
4013
Eric Anholtf564048e2011-03-30 13:01:02 -07004014 I915_WRITE(DSPCNTR(plane), dspcntr);
4015 POSTING_READ(DSPCNTR(plane));
4016
4017 ret = intel_pipe_set_base(crtc, x, y, old_fb);
4018
4019 intel_update_watermarks(dev);
4020
Eric Anholtf564048e2011-03-30 13:01:02 -07004021 return ret;
4022}
4023
Keith Packard9fb526d2011-09-26 22:24:57 -07004024/*
4025 * Initialize reference clocks when the driver loads
4026 */
4027void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07004028{
4029 struct drm_i915_private *dev_priv = dev->dev_private;
4030 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004031 struct intel_encoder *encoder;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004032 u32 temp;
4033 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07004034 bool has_cpu_edp = false;
4035 bool has_pch_edp = false;
4036 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07004037 bool has_ck505 = false;
4038 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004039
4040 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07004041 list_for_each_entry(encoder, &mode_config->encoder_list,
4042 base.head) {
4043 switch (encoder->type) {
4044 case INTEL_OUTPUT_LVDS:
4045 has_panel = true;
4046 has_lvds = true;
4047 break;
4048 case INTEL_OUTPUT_EDP:
4049 has_panel = true;
4050 if (intel_encoder_is_pch_edp(&encoder->base))
4051 has_pch_edp = true;
4052 else
4053 has_cpu_edp = true;
4054 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004055 }
4056 }
4057
Keith Packard99eb6a02011-09-26 14:29:12 -07004058 if (HAS_PCH_IBX(dev)) {
4059 has_ck505 = dev_priv->display_clock_mode;
4060 can_ssc = has_ck505;
4061 } else {
4062 has_ck505 = false;
4063 can_ssc = true;
4064 }
4065
4066 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
4067 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
4068 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07004069
4070 /* Ironlake: try to setup display ref clock before DPLL
4071 * enabling. This is only under driver's control after
4072 * PCH B stepping, previous chipset stepping should be
4073 * ignoring this setting.
4074 */
4075 temp = I915_READ(PCH_DREF_CONTROL);
4076 /* Always enable nonspread source */
4077 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004078
Keith Packard99eb6a02011-09-26 14:29:12 -07004079 if (has_ck505)
4080 temp |= DREF_NONSPREAD_CK505_ENABLE;
4081 else
4082 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004083
Keith Packard199e5d72011-09-22 12:01:57 -07004084 if (has_panel) {
4085 temp &= ~DREF_SSC_SOURCE_MASK;
4086 temp |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07004087
Keith Packard199e5d72011-09-22 12:01:57 -07004088 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07004089 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004090 DRM_DEBUG_KMS("Using SSC on panel\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004091 temp |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02004092 } else
4093 temp &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07004094
4095 /* Get SSC going before enabling the outputs */
4096 I915_WRITE(PCH_DREF_CONTROL, temp);
4097 POSTING_READ(PCH_DREF_CONTROL);
4098 udelay(200);
4099
Jesse Barnes13d83a62011-08-03 12:59:20 -07004100 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4101
4102 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07004103 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07004104 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07004105 DRM_DEBUG_KMS("Using SSC on eDP\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07004106 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004107 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07004108 else
4109 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07004110 } else
4111 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4112
4113 I915_WRITE(PCH_DREF_CONTROL, temp);
4114 POSTING_READ(PCH_DREF_CONTROL);
4115 udelay(200);
4116 } else {
4117 DRM_DEBUG_KMS("Disabling SSC entirely\n");
4118
4119 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4120
4121 /* Turn off CPU output */
4122 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4123
4124 I915_WRITE(PCH_DREF_CONTROL, temp);
4125 POSTING_READ(PCH_DREF_CONTROL);
4126 udelay(200);
4127
4128 /* Turn off the SSC source */
4129 temp &= ~DREF_SSC_SOURCE_MASK;
4130 temp |= DREF_SSC_SOURCE_DISABLE;
4131
4132 /* Turn off SSC1 */
4133 temp &= ~ DREF_SSC1_ENABLE;
4134
Jesse Barnes13d83a62011-08-03 12:59:20 -07004135 I915_WRITE(PCH_DREF_CONTROL, temp);
4136 POSTING_READ(PCH_DREF_CONTROL);
4137 udelay(200);
4138 }
4139}
4140
Jesse Barnesd9d444c2011-09-02 13:03:05 -07004141static int ironlake_get_refclk(struct drm_crtc *crtc)
4142{
4143 struct drm_device *dev = crtc->dev;
4144 struct drm_i915_private *dev_priv = dev->dev_private;
4145 struct intel_encoder *encoder;
4146 struct drm_mode_config *mode_config = &dev->mode_config;
4147 struct intel_encoder *edp_encoder = NULL;
4148 int num_connectors = 0;
4149 bool is_lvds = false;
4150
4151 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
4152 if (encoder->base.crtc != crtc)
4153 continue;
4154
4155 switch (encoder->type) {
4156 case INTEL_OUTPUT_LVDS:
4157 is_lvds = true;
4158 break;
4159 case INTEL_OUTPUT_EDP:
4160 edp_encoder = encoder;
4161 break;
4162 }
4163 num_connectors++;
4164 }
4165
4166 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
4167 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
4168 dev_priv->lvds_ssc_freq);
4169 return dev_priv->lvds_ssc_freq * 1000;
4170 }
4171
4172 return 120000;
4173}
4174
Eric Anholtf564048e2011-03-30 13:01:02 -07004175static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
4176 struct drm_display_mode *mode,
4177 struct drm_display_mode *adjusted_mode,
4178 int x, int y,
4179 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004180{
4181 struct drm_device *dev = crtc->dev;
4182 struct drm_i915_private *dev_priv = dev->dev_private;
4183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4184 int pipe = intel_crtc->pipe;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004185 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08004186 int refclk, num_connectors = 0;
4187 intel_clock_t clock, reduced_clock;
4188 u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
Eric Anholta07d6782011-03-30 13:01:08 -07004189 bool ok, has_reduced_clock = false, is_sdvo = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08004190 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08004191 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnese3aef172012-04-10 11:58:03 -07004192 struct intel_encoder *encoder, *edp_encoder = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08004193 const intel_limit_t *limit;
4194 int ret;
4195 struct fdi_m_n m_n = {0};
Eric Anholtfae14982011-03-30 13:01:09 -07004196 u32 temp;
Jesse Barnes5a354202011-06-24 12:19:22 -07004197 int target_clock, pixel_multiplier, lane, link_bw, factor;
4198 unsigned int pipe_bpp;
4199 bool dither;
Jesse Barnese3aef172012-04-10 11:58:03 -07004200 bool is_cpu_edp = false, is_pch_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08004201
Jesse Barnes79e53942008-11-07 14:24:08 -08004202 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
4203 if (encoder->base.crtc != crtc)
4204 continue;
4205
4206 switch (encoder->type) {
4207 case INTEL_OUTPUT_LVDS:
4208 is_lvds = true;
4209 break;
4210 case INTEL_OUTPUT_SDVO:
4211 case INTEL_OUTPUT_HDMI:
4212 is_sdvo = true;
4213 if (encoder->needs_tv_clock)
4214 is_tv = true;
4215 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004216 case INTEL_OUTPUT_TVOUT:
4217 is_tv = true;
4218 break;
4219 case INTEL_OUTPUT_ANALOG:
4220 is_crt = true;
4221 break;
4222 case INTEL_OUTPUT_DISPLAYPORT:
4223 is_dp = true;
4224 break;
4225 case INTEL_OUTPUT_EDP:
Jesse Barnese3aef172012-04-10 11:58:03 -07004226 is_dp = true;
4227 if (intel_encoder_is_pch_edp(&encoder->base))
4228 is_pch_edp = true;
4229 else
4230 is_cpu_edp = true;
4231 edp_encoder = encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004232 break;
4233 }
4234
Kristian Høgsberg43565a02009-02-13 20:56:52 -05004235 num_connectors++;
4236 }
4237
Jesse Barnesd9d444c2011-09-02 13:03:05 -07004238 refclk = ironlake_get_refclk(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08004239
4240 /*
4241 * Returns a set of divisors for the desired target clock with the given
4242 * refclk, or FALSE. The returned values represent the clock equation:
4243 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
4244 */
4245 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08004246 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
4247 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08004248 if (!ok) {
4249 DRM_ERROR("Couldn't find PLL settings for mode!\n");
4250 return -EINVAL;
4251 }
4252
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004253 /* Ensure that the cursor is valid for the new mode before changing... */
Chris Wilson6b383a72010-09-13 13:54:26 +01004254 intel_crtc_update_cursor(crtc, true);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004255
Zhao Yakuiddc90032010-01-06 22:05:56 +08004256 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08004257 /*
4258 * Ensure we match the reduced clock's P to the target clock.
4259 * If the clocks don't match, we can't switch the display clock
4260 * by using the FP0/FP1. In such case we will disable the LVDS
4261 * downclock feature.
4262 */
Zhao Yakuiddc90032010-01-06 22:05:56 +08004263 has_reduced_clock = limit->find_pll(limit, crtc,
Chris Wilson5eddb702010-09-11 13:48:45 +01004264 dev_priv->lvds_downclock,
4265 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08004266 &clock,
Chris Wilson5eddb702010-09-11 13:48:45 +01004267 &reduced_clock);
Jesse Barnes652c3932009-08-17 13:31:43 -07004268 }
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08004269 /* SDVO TV has fixed PLL values depend on its clock range,
4270 this mirrors vbios setting. */
4271 if (is_sdvo && is_tv) {
4272 if (adjusted_mode->clock >= 100000
Chris Wilson5eddb702010-09-11 13:48:45 +01004273 && adjusted_mode->clock < 140500) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08004274 clock.p1 = 2;
4275 clock.p2 = 10;
4276 clock.n = 3;
4277 clock.m1 = 16;
4278 clock.m2 = 8;
4279 } else if (adjusted_mode->clock >= 140500
Chris Wilson5eddb702010-09-11 13:48:45 +01004280 && adjusted_mode->clock <= 200000) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08004281 clock.p1 = 1;
4282 clock.p2 = 10;
4283 clock.n = 6;
4284 clock.m1 = 12;
4285 clock.m2 = 8;
4286 }
4287 }
4288
Zhenyu Wang2c072452009-06-05 15:38:42 +08004289 /* FDI link */
Eric Anholt8febb292011-03-30 13:01:07 -07004290 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4291 lane = 0;
4292 /* CPU eDP doesn't require FDI link, so just set DP M/N
4293 according to current link config */
Jesse Barnese3aef172012-04-10 11:58:03 -07004294 if (is_cpu_edp) {
Eric Anholt8febb292011-03-30 13:01:07 -07004295 target_clock = mode->clock;
Jesse Barnese3aef172012-04-10 11:58:03 -07004296 intel_edp_link_config(edp_encoder, &lane, &link_bw);
Eric Anholt8febb292011-03-30 13:01:07 -07004297 } else {
4298 /* [e]DP over FDI requires target mode clock
4299 instead of link clock */
Jesse Barnese3aef172012-04-10 11:58:03 -07004300 if (is_dp)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004301 target_clock = mode->clock;
Eric Anholt8febb292011-03-30 13:01:07 -07004302 else
4303 target_clock = adjusted_mode->clock;
Chris Wilson021357a2010-09-07 20:54:59 +01004304
Eric Anholt8febb292011-03-30 13:01:07 -07004305 /* FDI is a binary signal running at ~2.7GHz, encoding
4306 * each output octet as 10 bits. The actual frequency
4307 * is stored as a divider into a 100MHz clock, and the
4308 * mode pixel clock is stored in units of 1KHz.
4309 * Hence the bw of each lane in terms of the mode signal
4310 * is:
4311 */
4312 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08004313 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08004314
Eric Anholt8febb292011-03-30 13:01:07 -07004315 /* determine panel color depth */
4316 temp = I915_READ(PIPECONF(pipe));
4317 temp &= ~PIPE_BPC_MASK;
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004318 dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
Jesse Barnes5a354202011-06-24 12:19:22 -07004319 switch (pipe_bpp) {
4320 case 18:
4321 temp |= PIPE_6BPC;
4322 break;
4323 case 24:
Eric Anholt8febb292011-03-30 13:01:07 -07004324 temp |= PIPE_8BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07004325 break;
Jesse Barnes5a354202011-06-24 12:19:22 -07004326 case 30:
4327 temp |= PIPE_10BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07004328 break;
Jesse Barnes5a354202011-06-24 12:19:22 -07004329 case 36:
4330 temp |= PIPE_12BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07004331 break;
4332 default:
Jesse Barnes62ac41a2011-07-28 12:55:14 -07004333 WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
4334 pipe_bpp);
Jesse Barnes5a354202011-06-24 12:19:22 -07004335 temp |= PIPE_8BPC;
4336 pipe_bpp = 24;
4337 break;
Eric Anholt8febb292011-03-30 13:01:07 -07004338 }
4339
Jesse Barnes5a354202011-06-24 12:19:22 -07004340 intel_crtc->bpp = pipe_bpp;
4341 I915_WRITE(PIPECONF(pipe), temp);
4342
Eric Anholt8febb292011-03-30 13:01:07 -07004343 if (!lane) {
4344 /*
4345 * Account for spread spectrum to avoid
4346 * oversubscribing the link. Max center spread
4347 * is 2.5%; use 5% for safety's sake.
4348 */
Jesse Barnes5a354202011-06-24 12:19:22 -07004349 u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
Eric Anholt8febb292011-03-30 13:01:07 -07004350 lane = bps / (link_bw * 8) + 1;
4351 }
4352
4353 intel_crtc->fdi_lanes = lane;
4354
4355 if (pixel_multiplier > 1)
4356 link_bw *= pixel_multiplier;
Jesse Barnes5a354202011-06-24 12:19:22 -07004357 ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
4358 &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07004359
Eric Anholta07d6782011-03-30 13:01:08 -07004360 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
4361 if (has_reduced_clock)
4362 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
4363 reduced_clock.m2;
Jesse Barnes79e53942008-11-07 14:24:08 -08004364
Chris Wilsonc1858122010-12-03 21:35:48 +00004365 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07004366 factor = 21;
4367 if (is_lvds) {
4368 if ((intel_panel_use_ssc(dev_priv) &&
4369 dev_priv->lvds_ssc_freq == 100) ||
4370 (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
4371 factor = 25;
4372 } else if (is_sdvo && is_tv)
4373 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00004374
Jesse Barnescb0e0932011-07-28 14:50:30 -07004375 if (clock.m < factor * clock.n)
Eric Anholt8febb292011-03-30 13:01:07 -07004376 fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00004377
Chris Wilson5eddb702010-09-11 13:48:45 +01004378 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004379
Eric Anholta07d6782011-03-30 13:01:08 -07004380 if (is_lvds)
4381 dpll |= DPLLB_MODE_LVDS;
4382 else
4383 dpll |= DPLLB_MODE_DAC_SERIAL;
4384 if (is_sdvo) {
4385 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
4386 if (pixel_multiplier > 1) {
4387 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08004388 }
Eric Anholta07d6782011-03-30 13:01:08 -07004389 dpll |= DPLL_DVO_HIGH_SPEED;
4390 }
Jesse Barnese3aef172012-04-10 11:58:03 -07004391 if (is_dp && !is_cpu_edp)
Eric Anholta07d6782011-03-30 13:01:08 -07004392 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08004393
Eric Anholta07d6782011-03-30 13:01:08 -07004394 /* compute bitmask from p1 value */
4395 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
4396 /* also FPA1 */
4397 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
4398
4399 switch (clock.p2) {
4400 case 5:
4401 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
4402 break;
4403 case 7:
4404 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
4405 break;
4406 case 10:
4407 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
4408 break;
4409 case 14:
4410 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
4411 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08004412 }
4413
4414 if (is_sdvo && is_tv)
4415 dpll |= PLL_REF_INPUT_TVCLKINBC;
4416 else if (is_tv)
4417 /* XXX: just matching BIOS for now */
4418 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
4419 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00004420 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Jesse Barnes79e53942008-11-07 14:24:08 -08004421 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
4422 else
4423 dpll |= PLL_REF_INPUT_DREFCLK;
4424
4425 /* setup pipeconf */
Chris Wilson5eddb702010-09-11 13:48:45 +01004426 pipeconf = I915_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004427
4428 /* Set up the display plane register */
4429 dspcntr = DISPPLANE_GAMMA_ENABLE;
4430
Jesse Barnesf7cb34d2011-10-12 10:49:14 -07004431 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004432 drm_mode_debug_printmodeline(mode);
4433
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03004434 /* CPU eDP is the only output that doesn't need a PCH PLL of its own on
4435 * pre-Haswell/LPT generation */
4436 if (HAS_PCH_LPT(dev)) {
4437 DRM_DEBUG_KMS("LPT detected: no PLL for pipe %d necessary\n",
4438 pipe);
4439 } else if (!is_cpu_edp) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004440 struct intel_pch_pll *pll;
Chris Wilson5eddb702010-09-11 13:48:45 +01004441
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004442 pll = intel_get_pch_pll(intel_crtc, dpll, fp);
4443 if (pll == NULL) {
4444 DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
4445 pipe);
Jesse Barnes4b645f12011-10-12 09:51:31 -07004446 return -EINVAL;
4447 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004448 } else
4449 intel_put_pch_pll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08004450
4451 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
4452 * This is an exception to the general rule that mode_set doesn't turn
4453 * things on.
4454 */
4455 if (is_lvds) {
Eric Anholtfae14982011-03-30 13:01:09 -07004456 temp = I915_READ(PCH_LVDS);
Chris Wilson5eddb702010-09-11 13:48:45 +01004457 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
Jesse Barnes7885d202012-01-12 14:51:17 -08004458 if (HAS_PCH_CPT(dev)) {
4459 temp &= ~PORT_TRANS_SEL_MASK;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004460 temp |= PORT_TRANS_SEL_CPT(pipe);
Jesse Barnes7885d202012-01-12 14:51:17 -08004461 } else {
4462 if (pipe == 1)
4463 temp |= LVDS_PIPEB_SELECT;
4464 else
4465 temp &= ~LVDS_PIPEB_SELECT;
4466 }
Jesse Barnes4b645f12011-10-12 09:51:31 -07004467
Zhao Yakuia3e17eb2009-10-10 10:42:37 +08004468 /* set the corresponsding LVDS_BORDER bit */
Chris Wilson5eddb702010-09-11 13:48:45 +01004469 temp |= dev_priv->lvds_border_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -08004470 /* Set the B0-B3 data pairs corresponding to whether we're going to
4471 * set the DPLLs for dual-channel mode or not.
4472 */
4473 if (clock.p2 == 7)
Chris Wilson5eddb702010-09-11 13:48:45 +01004474 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
Jesse Barnes79e53942008-11-07 14:24:08 -08004475 else
Chris Wilson5eddb702010-09-11 13:48:45 +01004476 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
Jesse Barnes79e53942008-11-07 14:24:08 -08004477
4478 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
4479 * appropriately here, but we need to look more thoroughly into how
4480 * panels behave in the two modes.
4481 */
Chris Wilson284d5df2012-04-14 17:41:59 +01004482 temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
Bryan Freedaa9b5002011-01-12 13:43:19 -08004483 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
Chris Wilson284d5df2012-04-14 17:41:59 +01004484 temp |= LVDS_HSYNC_POLARITY;
Bryan Freedaa9b5002011-01-12 13:43:19 -08004485 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
Chris Wilson284d5df2012-04-14 17:41:59 +01004486 temp |= LVDS_VSYNC_POLARITY;
Eric Anholtfae14982011-03-30 13:01:09 -07004487 I915_WRITE(PCH_LVDS, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -08004488 }
Jesse Barnes434ed092010-09-07 14:48:06 -07004489
Eric Anholt8febb292011-03-30 13:01:07 -07004490 pipeconf &= ~PIPECONF_DITHER_EN;
4491 pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
Jesse Barnes5a354202011-06-24 12:19:22 -07004492 if ((is_lvds && dev_priv->lvds_dither) || dither) {
Eric Anholt8febb292011-03-30 13:01:07 -07004493 pipeconf |= PIPECONF_DITHER_EN;
Daniel Vetterf74974c2011-10-11 17:27:51 +02004494 pipeconf |= PIPECONF_DITHER_TYPE_SP;
Jesse Barnes434ed092010-09-07 14:48:06 -07004495 }
Jesse Barnese3aef172012-04-10 11:58:03 -07004496 if (is_dp && !is_cpu_edp) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004497 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Eric Anholt8febb292011-03-30 13:01:07 -07004498 } else {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004499 /* For non-DP output, clear any trans DP clock recovery setting.*/
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004500 I915_WRITE(TRANSDATA_M1(pipe), 0);
4501 I915_WRITE(TRANSDATA_N1(pipe), 0);
4502 I915_WRITE(TRANSDPLINK_M1(pipe), 0);
4503 I915_WRITE(TRANSDPLINK_N1(pipe), 0);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08004504 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004505
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004506 if (intel_crtc->pch_pll) {
4507 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01004508
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004509 /* Wait for the clocks to stabilize. */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004510 POSTING_READ(intel_crtc->pch_pll->pll_reg);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08004511 udelay(150);
4512
Eric Anholt8febb292011-03-30 13:01:07 -07004513 /* The pixel multiplier can only be updated once the
4514 * DPLL is enabled and the clocks are stable.
4515 *
4516 * So write it again.
4517 */
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004518 I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08004519 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004520
Chris Wilson5eddb702010-09-11 13:48:45 +01004521 intel_crtc->lowfreq_avail = false;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004522 if (intel_crtc->pch_pll) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07004523 if (is_lvds && has_reduced_clock && i915_powersave) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004524 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
Jesse Barnes4b645f12011-10-12 09:51:31 -07004525 intel_crtc->lowfreq_avail = true;
4526 if (HAS_PIPE_CXSR(dev)) {
4527 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
4528 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
4529 }
4530 } else {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004531 I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
Jesse Barnes4b645f12011-10-12 09:51:31 -07004532 if (HAS_PIPE_CXSR(dev)) {
4533 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
4534 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
4535 }
Jesse Barnes652c3932009-08-17 13:31:43 -07004536 }
4537 }
4538
Keith Packard617cf882012-02-08 13:53:38 -08004539 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Krzysztof Halasa734b4152010-05-25 18:41:46 +02004540 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Daniel Vetter5def4742012-01-28 14:49:22 +01004541 pipeconf |= PIPECONF_INTERLACED_ILK;
Krzysztof Halasa734b4152010-05-25 18:41:46 +02004542 /* the chip adds 2 halflines automatically */
Krzysztof Halasa734b4152010-05-25 18:41:46 +02004543 adjusted_mode->crtc_vtotal -= 1;
Krzysztof Halasa734b4152010-05-25 18:41:46 +02004544 adjusted_mode->crtc_vblank_end -= 1;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01004545 I915_WRITE(VSYNCSHIFT(pipe),
4546 adjusted_mode->crtc_hsync_start
4547 - adjusted_mode->crtc_htotal/2);
4548 } else {
Keith Packard617cf882012-02-08 13:53:38 -08004549 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01004550 I915_WRITE(VSYNCSHIFT(pipe), 0);
4551 }
Krzysztof Halasa734b4152010-05-25 18:41:46 +02004552
Chris Wilson5eddb702010-09-11 13:48:45 +01004553 I915_WRITE(HTOTAL(pipe),
4554 (adjusted_mode->crtc_hdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004555 ((adjusted_mode->crtc_htotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004556 I915_WRITE(HBLANK(pipe),
4557 (adjusted_mode->crtc_hblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004558 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004559 I915_WRITE(HSYNC(pipe),
4560 (adjusted_mode->crtc_hsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004561 ((adjusted_mode->crtc_hsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004562
4563 I915_WRITE(VTOTAL(pipe),
4564 (adjusted_mode->crtc_vdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004565 ((adjusted_mode->crtc_vtotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004566 I915_WRITE(VBLANK(pipe),
4567 (adjusted_mode->crtc_vblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004568 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004569 I915_WRITE(VSYNC(pipe),
4570 (adjusted_mode->crtc_vsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08004571 ((adjusted_mode->crtc_vsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01004572
Eric Anholt8febb292011-03-30 13:01:07 -07004573 /* pipesrc controls the size that is scaled from, which should
4574 * always be the user's requested size.
Jesse Barnes79e53942008-11-07 14:24:08 -08004575 */
Chris Wilson5eddb702010-09-11 13:48:45 +01004576 I915_WRITE(PIPESRC(pipe),
4577 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
Zhenyu Wang2c072452009-06-05 15:38:42 +08004578
Eric Anholt8febb292011-03-30 13:01:07 -07004579 I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
4580 I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
4581 I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
4582 I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004583
Jesse Barnese3aef172012-04-10 11:58:03 -07004584 if (is_cpu_edp)
Eric Anholt8febb292011-03-30 13:01:07 -07004585 ironlake_set_pll_edp(crtc, adjusted_mode->clock);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004586
Chris Wilson5eddb702010-09-11 13:48:45 +01004587 I915_WRITE(PIPECONF(pipe), pipeconf);
4588 POSTING_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004589
Jesse Barnes9d0498a2010-08-18 13:20:54 -07004590 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004591
Chris Wilson5eddb702010-09-11 13:48:45 +01004592 I915_WRITE(DSPCNTR(plane), dspcntr);
Jesse Barnesb24e7172011-01-04 15:09:30 -08004593 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08004594
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004595 ret = intel_pipe_set_base(crtc, x, y, old_fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004596
4597 intel_update_watermarks(dev);
4598
Chris Wilson1f803ee2009-06-06 09:45:59 +01004599 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004600}
4601
Eric Anholtf564048e2011-03-30 13:01:02 -07004602static int intel_crtc_mode_set(struct drm_crtc *crtc,
4603 struct drm_display_mode *mode,
4604 struct drm_display_mode *adjusted_mode,
4605 int x, int y,
4606 struct drm_framebuffer *old_fb)
4607{
4608 struct drm_device *dev = crtc->dev;
4609 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt0b701d22011-03-30 13:01:03 -07004610 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4611 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07004612 int ret;
4613
Eric Anholt0b701d22011-03-30 13:01:03 -07004614 drm_vblank_pre_modeset(dev, pipe);
4615
Eric Anholtf564048e2011-03-30 13:01:02 -07004616 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
4617 x, y, old_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08004618 drm_vblank_post_modeset(dev, pipe);
4619
Jesse Barnesd8e70a22011-11-15 10:28:54 -08004620 if (ret)
4621 intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
4622 else
4623 intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
Keith Packard120eced2011-07-27 01:21:40 -07004624
Jesse Barnes79e53942008-11-07 14:24:08 -08004625 return ret;
4626}
4627
Wu Fengguang3a9627f2011-12-09 20:42:19 +08004628static bool intel_eld_uptodate(struct drm_connector *connector,
4629 int reg_eldv, uint32_t bits_eldv,
4630 int reg_elda, uint32_t bits_elda,
4631 int reg_edid)
4632{
4633 struct drm_i915_private *dev_priv = connector->dev->dev_private;
4634 uint8_t *eld = connector->eld;
4635 uint32_t i;
4636
4637 i = I915_READ(reg_eldv);
4638 i &= bits_eldv;
4639
4640 if (!eld[0])
4641 return !i;
4642
4643 if (!i)
4644 return false;
4645
4646 i = I915_READ(reg_elda);
4647 i &= ~bits_elda;
4648 I915_WRITE(reg_elda, i);
4649
4650 for (i = 0; i < eld[2]; i++)
4651 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
4652 return false;
4653
4654 return true;
4655}
4656
Wu Fengguange0dac652011-09-05 14:25:34 +08004657static void g4x_write_eld(struct drm_connector *connector,
4658 struct drm_crtc *crtc)
4659{
4660 struct drm_i915_private *dev_priv = connector->dev->dev_private;
4661 uint8_t *eld = connector->eld;
4662 uint32_t eldv;
4663 uint32_t len;
4664 uint32_t i;
4665
4666 i = I915_READ(G4X_AUD_VID_DID);
4667
4668 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
4669 eldv = G4X_ELDV_DEVCL_DEVBLC;
4670 else
4671 eldv = G4X_ELDV_DEVCTG;
4672
Wu Fengguang3a9627f2011-12-09 20:42:19 +08004673 if (intel_eld_uptodate(connector,
4674 G4X_AUD_CNTL_ST, eldv,
4675 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
4676 G4X_HDMIW_HDMIEDID))
4677 return;
4678
Wu Fengguange0dac652011-09-05 14:25:34 +08004679 i = I915_READ(G4X_AUD_CNTL_ST);
4680 i &= ~(eldv | G4X_ELD_ADDR);
4681 len = (i >> 9) & 0x1f; /* ELD buffer size */
4682 I915_WRITE(G4X_AUD_CNTL_ST, i);
4683
4684 if (!eld[0])
4685 return;
4686
4687 len = min_t(uint8_t, eld[2], len);
4688 DRM_DEBUG_DRIVER("ELD size %d\n", len);
4689 for (i = 0; i < len; i++)
4690 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
4691
4692 i = I915_READ(G4X_AUD_CNTL_ST);
4693 i |= eldv;
4694 I915_WRITE(G4X_AUD_CNTL_ST, i);
4695}
4696
4697static void ironlake_write_eld(struct drm_connector *connector,
4698 struct drm_crtc *crtc)
4699{
4700 struct drm_i915_private *dev_priv = connector->dev->dev_private;
4701 uint8_t *eld = connector->eld;
4702 uint32_t eldv;
4703 uint32_t i;
4704 int len;
4705 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06004706 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08004707 int aud_cntl_st;
4708 int aud_cntrl_st2;
4709
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08004710 if (HAS_PCH_IBX(connector->dev)) {
Wu Fengguang1202b4c62011-12-09 20:42:18 +08004711 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
Wu Fengguangb6daa022012-01-06 14:41:31 -06004712 aud_config = IBX_AUD_CONFIG_A;
Wu Fengguang1202b4c62011-12-09 20:42:18 +08004713 aud_cntl_st = IBX_AUD_CNTL_ST_A;
4714 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08004715 } else {
Wu Fengguang1202b4c62011-12-09 20:42:18 +08004716 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
Wu Fengguangb6daa022012-01-06 14:41:31 -06004717 aud_config = CPT_AUD_CONFIG_A;
Wu Fengguang1202b4c62011-12-09 20:42:18 +08004718 aud_cntl_st = CPT_AUD_CNTL_ST_A;
4719 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08004720 }
4721
4722 i = to_intel_crtc(crtc)->pipe;
4723 hdmiw_hdmiedid += i * 0x100;
4724 aud_cntl_st += i * 0x100;
Wu Fengguangb6daa022012-01-06 14:41:31 -06004725 aud_config += i * 0x100;
Wu Fengguange0dac652011-09-05 14:25:34 +08004726
4727 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(i));
4728
4729 i = I915_READ(aud_cntl_st);
4730 i = (i >> 29) & 0x3; /* DIP_Port_Select, 0x1 = PortB */
4731 if (!i) {
4732 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
4733 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08004734 eldv = IBX_ELD_VALIDB;
4735 eldv |= IBX_ELD_VALIDB << 4;
4736 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08004737 } else {
4738 DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08004739 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08004740 }
4741
Wu Fengguang3a9627f2011-12-09 20:42:19 +08004742 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
4743 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
4744 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06004745 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
4746 } else
4747 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08004748
4749 if (intel_eld_uptodate(connector,
4750 aud_cntrl_st2, eldv,
4751 aud_cntl_st, IBX_ELD_ADDRESS,
4752 hdmiw_hdmiedid))
4753 return;
4754
Wu Fengguange0dac652011-09-05 14:25:34 +08004755 i = I915_READ(aud_cntrl_st2);
4756 i &= ~eldv;
4757 I915_WRITE(aud_cntrl_st2, i);
4758
4759 if (!eld[0])
4760 return;
4761
Wu Fengguange0dac652011-09-05 14:25:34 +08004762 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08004763 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08004764 I915_WRITE(aud_cntl_st, i);
4765
4766 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
4767 DRM_DEBUG_DRIVER("ELD size %d\n", len);
4768 for (i = 0; i < len; i++)
4769 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
4770
4771 i = I915_READ(aud_cntrl_st2);
4772 i |= eldv;
4773 I915_WRITE(aud_cntrl_st2, i);
4774}
4775
4776void intel_write_eld(struct drm_encoder *encoder,
4777 struct drm_display_mode *mode)
4778{
4779 struct drm_crtc *crtc = encoder->crtc;
4780 struct drm_connector *connector;
4781 struct drm_device *dev = encoder->dev;
4782 struct drm_i915_private *dev_priv = dev->dev_private;
4783
4784 connector = drm_select_eld(encoder, mode);
4785 if (!connector)
4786 return;
4787
4788 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
4789 connector->base.id,
4790 drm_get_connector_name(connector),
4791 connector->encoder->base.id,
4792 drm_get_encoder_name(connector->encoder));
4793
4794 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
4795
4796 if (dev_priv->display.write_eld)
4797 dev_priv->display.write_eld(connector, crtc);
4798}
4799
Jesse Barnes79e53942008-11-07 14:24:08 -08004800/** Loads the palette/gamma unit for the CRTC with the prepared values */
4801void intel_crtc_load_lut(struct drm_crtc *crtc)
4802{
4803 struct drm_device *dev = crtc->dev;
4804 struct drm_i915_private *dev_priv = dev->dev_private;
4805 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004806 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08004807 int i;
4808
4809 /* The clocks have to be on to load the palette. */
Alban Browaeysaed3f092012-02-24 17:12:45 +00004810 if (!crtc->enabled || !intel_crtc->active)
Jesse Barnes79e53942008-11-07 14:24:08 -08004811 return;
4812
Adam Jacksonf2b115e2009-12-03 17:14:42 -05004813 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07004814 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004815 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08004816
Jesse Barnes79e53942008-11-07 14:24:08 -08004817 for (i = 0; i < 256; i++) {
4818 I915_WRITE(palreg + 4 * i,
4819 (intel_crtc->lut_r[i] << 16) |
4820 (intel_crtc->lut_g[i] << 8) |
4821 intel_crtc->lut_b[i]);
4822 }
4823}
4824
Chris Wilson560b85b2010-08-07 11:01:38 +01004825static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
4826{
4827 struct drm_device *dev = crtc->dev;
4828 struct drm_i915_private *dev_priv = dev->dev_private;
4829 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4830 bool visible = base != 0;
4831 u32 cntl;
4832
4833 if (intel_crtc->cursor_visible == visible)
4834 return;
4835
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004836 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01004837 if (visible) {
4838 /* On these chipsets we can only modify the base whilst
4839 * the cursor is disabled.
4840 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004841 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01004842
4843 cntl &= ~(CURSOR_FORMAT_MASK);
4844 /* XXX width must be 64, stride 256 => 0x00 << 28 */
4845 cntl |= CURSOR_ENABLE |
4846 CURSOR_GAMMA_ENABLE |
4847 CURSOR_FORMAT_ARGB;
4848 } else
4849 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004850 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01004851
4852 intel_crtc->cursor_visible = visible;
4853}
4854
4855static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
4856{
4857 struct drm_device *dev = crtc->dev;
4858 struct drm_i915_private *dev_priv = dev->dev_private;
4859 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4860 int pipe = intel_crtc->pipe;
4861 bool visible = base != 0;
4862
4863 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08004864 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01004865 if (base) {
4866 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
4867 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
4868 cntl |= pipe << 28; /* Connect to correct pipe */
4869 } else {
4870 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
4871 cntl |= CURSOR_MODE_DISABLE;
4872 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004873 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01004874
4875 intel_crtc->cursor_visible = visible;
4876 }
4877 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004878 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01004879}
4880
Jesse Barnes65a21cd2011-10-12 11:10:21 -07004881static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
4882{
4883 struct drm_device *dev = crtc->dev;
4884 struct drm_i915_private *dev_priv = dev->dev_private;
4885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4886 int pipe = intel_crtc->pipe;
4887 bool visible = base != 0;
4888
4889 if (intel_crtc->cursor_visible != visible) {
4890 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
4891 if (base) {
4892 cntl &= ~CURSOR_MODE;
4893 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
4894 } else {
4895 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
4896 cntl |= CURSOR_MODE_DISABLE;
4897 }
4898 I915_WRITE(CURCNTR_IVB(pipe), cntl);
4899
4900 intel_crtc->cursor_visible = visible;
4901 }
4902 /* and commit changes on next vblank */
4903 I915_WRITE(CURBASE_IVB(pipe), base);
4904}
4905
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004906/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01004907static void intel_crtc_update_cursor(struct drm_crtc *crtc,
4908 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004909{
4910 struct drm_device *dev = crtc->dev;
4911 struct drm_i915_private *dev_priv = dev->dev_private;
4912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4913 int pipe = intel_crtc->pipe;
4914 int x = intel_crtc->cursor_x;
4915 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01004916 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004917 bool visible;
4918
4919 pos = 0;
4920
Chris Wilson6b383a72010-09-13 13:54:26 +01004921 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004922 base = intel_crtc->cursor_addr;
4923 if (x > (int) crtc->fb->width)
4924 base = 0;
4925
4926 if (y > (int) crtc->fb->height)
4927 base = 0;
4928 } else
4929 base = 0;
4930
4931 if (x < 0) {
4932 if (x + intel_crtc->cursor_width < 0)
4933 base = 0;
4934
4935 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
4936 x = -x;
4937 }
4938 pos |= x << CURSOR_X_SHIFT;
4939
4940 if (y < 0) {
4941 if (y + intel_crtc->cursor_height < 0)
4942 base = 0;
4943
4944 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
4945 y = -y;
4946 }
4947 pos |= y << CURSOR_Y_SHIFT;
4948
4949 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01004950 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004951 return;
4952
Eugeni Dodonov0cd83aa2012-04-13 17:08:48 -03004953 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Jesse Barnes65a21cd2011-10-12 11:10:21 -07004954 I915_WRITE(CURPOS_IVB(pipe), pos);
4955 ivb_update_cursor(crtc, base);
4956 } else {
4957 I915_WRITE(CURPOS(pipe), pos);
4958 if (IS_845G(dev) || IS_I865G(dev))
4959 i845_update_cursor(crtc, base);
4960 else
4961 i9xx_update_cursor(crtc, base);
4962 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004963}
4964
Jesse Barnes79e53942008-11-07 14:24:08 -08004965static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00004966 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08004967 uint32_t handle,
4968 uint32_t width, uint32_t height)
4969{
4970 struct drm_device *dev = crtc->dev;
4971 struct drm_i915_private *dev_priv = dev->dev_private;
4972 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00004973 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01004974 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004975 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08004976
Zhao Yakui28c97732009-10-09 11:39:41 +08004977 DRM_DEBUG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08004978
4979 /* if we want to turn off the cursor ignore width and height */
4980 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08004981 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004982 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00004983 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10004984 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05004985 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08004986 }
4987
4988 /* Currently we only support 64x64 cursors */
4989 if (width != 64 || height != 64) {
4990 DRM_ERROR("we currently only support 64x64 cursors\n");
4991 return -EINVAL;
4992 }
4993
Chris Wilson05394f32010-11-08 19:18:58 +00004994 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00004995 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08004996 return -ENOENT;
4997
Chris Wilson05394f32010-11-08 19:18:58 +00004998 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004999 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10005000 ret = -ENOMEM;
5001 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08005002 }
5003
Dave Airlie71acb5e2008-12-30 20:31:46 +10005004 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05005005 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05005006 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00005007 if (obj->tiling_mode) {
5008 DRM_ERROR("cursor cannot be tiled\n");
5009 ret = -EINVAL;
5010 goto fail_locked;
5011 }
5012
Chris Wilson2da3b9b2011-04-14 09:41:17 +01005013 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01005014 if (ret) {
5015 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01005016 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01005017 }
5018
Chris Wilsond9e86c02010-11-10 16:40:20 +00005019 ret = i915_gem_object_put_fence(obj);
5020 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01005021 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00005022 goto fail_unpin;
5023 }
5024
Chris Wilson05394f32010-11-08 19:18:58 +00005025 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005026 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01005027 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00005028 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01005029 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
5030 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10005031 if (ret) {
5032 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05005033 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10005034 }
Chris Wilson05394f32010-11-08 19:18:58 +00005035 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005036 }
5037
Chris Wilsona6c45cf2010-09-17 00:32:17 +01005038 if (IS_GEN2(dev))
Jesse Barnes14b603912009-05-20 16:47:08 -04005039 I915_WRITE(CURSIZE, (height << 12) | width);
5040
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005041 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005042 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05005043 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00005044 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10005045 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
5046 } else
5047 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00005048 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005049 }
Jesse Barnes80824002009-09-10 15:28:06 -07005050
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05005051 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005052
5053 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00005054 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005055 intel_crtc->cursor_width = width;
5056 intel_crtc->cursor_height = height;
5057
Chris Wilson6b383a72010-09-13 13:54:26 +01005058 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05005059
Jesse Barnes79e53942008-11-07 14:24:08 -08005060 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01005061fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00005062 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05005063fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10005064 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00005065fail:
Chris Wilson05394f32010-11-08 19:18:58 +00005066 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10005067 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005068}
5069
5070static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
5071{
Jesse Barnes79e53942008-11-07 14:24:08 -08005072 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005073
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005074 intel_crtc->cursor_x = x;
5075 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07005076
Chris Wilson6b383a72010-09-13 13:54:26 +01005077 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08005078
5079 return 0;
5080}
5081
5082/** Sets the color ramps on behalf of RandR */
5083void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
5084 u16 blue, int regno)
5085{
5086 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5087
5088 intel_crtc->lut_r[regno] = red >> 8;
5089 intel_crtc->lut_g[regno] = green >> 8;
5090 intel_crtc->lut_b[regno] = blue >> 8;
5091}
5092
Dave Airlieb8c00ac2009-10-06 13:54:01 +10005093void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
5094 u16 *blue, int regno)
5095{
5096 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5097
5098 *red = intel_crtc->lut_r[regno] << 8;
5099 *green = intel_crtc->lut_g[regno] << 8;
5100 *blue = intel_crtc->lut_b[regno] << 8;
5101}
5102
Jesse Barnes79e53942008-11-07 14:24:08 -08005103static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01005104 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08005105{
James Simmons72034252010-08-03 01:33:19 +01005106 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08005107 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005108
James Simmons72034252010-08-03 01:33:19 +01005109 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005110 intel_crtc->lut_r[i] = red[i] >> 8;
5111 intel_crtc->lut_g[i] = green[i] >> 8;
5112 intel_crtc->lut_b[i] = blue[i] >> 8;
5113 }
5114
5115 intel_crtc_load_lut(crtc);
5116}
5117
5118/**
5119 * Get a pipe with a simple mode set on it for doing load-based monitor
5120 * detection.
5121 *
5122 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07005123 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08005124 *
Eric Anholtc751ce42010-03-25 11:48:48 -07005125 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08005126 * configured for it. In the future, it could choose to temporarily disable
5127 * some outputs to free up a pipe for its use.
5128 *
5129 * \return crtc, or NULL if no pipes are available.
5130 */
5131
5132/* VESA 640x480x72Hz mode to set on the pipe */
5133static struct drm_display_mode load_detect_mode = {
5134 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
5135 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
5136};
5137
Chris Wilsond2dff872011-04-19 08:36:26 +01005138static struct drm_framebuffer *
5139intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08005140 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01005141 struct drm_i915_gem_object *obj)
5142{
5143 struct intel_framebuffer *intel_fb;
5144 int ret;
5145
5146 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
5147 if (!intel_fb) {
5148 drm_gem_object_unreference_unlocked(&obj->base);
5149 return ERR_PTR(-ENOMEM);
5150 }
5151
5152 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
5153 if (ret) {
5154 drm_gem_object_unreference_unlocked(&obj->base);
5155 kfree(intel_fb);
5156 return ERR_PTR(ret);
5157 }
5158
5159 return &intel_fb->base;
5160}
5161
5162static u32
5163intel_framebuffer_pitch_for_width(int width, int bpp)
5164{
5165 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
5166 return ALIGN(pitch, 64);
5167}
5168
5169static u32
5170intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
5171{
5172 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
5173 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
5174}
5175
5176static struct drm_framebuffer *
5177intel_framebuffer_create_for_mode(struct drm_device *dev,
5178 struct drm_display_mode *mode,
5179 int depth, int bpp)
5180{
5181 struct drm_i915_gem_object *obj;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08005182 struct drm_mode_fb_cmd2 mode_cmd;
Chris Wilsond2dff872011-04-19 08:36:26 +01005183
5184 obj = i915_gem_alloc_object(dev,
5185 intel_framebuffer_size_for_mode(mode, bpp));
5186 if (obj == NULL)
5187 return ERR_PTR(-ENOMEM);
5188
5189 mode_cmd.width = mode->hdisplay;
5190 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08005191 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
5192 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00005193 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01005194
5195 return intel_framebuffer_create(dev, &mode_cmd, obj);
5196}
5197
5198static struct drm_framebuffer *
5199mode_fits_in_fbdev(struct drm_device *dev,
5200 struct drm_display_mode *mode)
5201{
5202 struct drm_i915_private *dev_priv = dev->dev_private;
5203 struct drm_i915_gem_object *obj;
5204 struct drm_framebuffer *fb;
5205
5206 if (dev_priv->fbdev == NULL)
5207 return NULL;
5208
5209 obj = dev_priv->fbdev->ifb.obj;
5210 if (obj == NULL)
5211 return NULL;
5212
5213 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02005214 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
5215 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01005216 return NULL;
5217
Ville Syrjälä01f2c772011-12-20 00:06:49 +02005218 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01005219 return NULL;
5220
5221 return fb;
5222}
5223
Chris Wilson71731882011-04-19 23:10:58 +01005224bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
5225 struct drm_connector *connector,
5226 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01005227 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08005228{
5229 struct intel_crtc *intel_crtc;
5230 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01005231 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08005232 struct drm_crtc *crtc = NULL;
5233 struct drm_device *dev = encoder->dev;
Chris Wilsond2dff872011-04-19 08:36:26 +01005234 struct drm_framebuffer *old_fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08005235 int i = -1;
5236
Chris Wilsond2dff872011-04-19 08:36:26 +01005237 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5238 connector->base.id, drm_get_connector_name(connector),
5239 encoder->base.id, drm_get_encoder_name(encoder));
5240
Jesse Barnes79e53942008-11-07 14:24:08 -08005241 /*
5242 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01005243 *
Jesse Barnes79e53942008-11-07 14:24:08 -08005244 * - if the connector already has an assigned crtc, use it (but make
5245 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01005246 *
Jesse Barnes79e53942008-11-07 14:24:08 -08005247 * - try to find the first unused crtc that can drive this connector,
5248 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08005249 */
5250
5251 /* See if we already have a CRTC for this connector */
5252 if (encoder->crtc) {
5253 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01005254
Jesse Barnes79e53942008-11-07 14:24:08 -08005255 intel_crtc = to_intel_crtc(crtc);
Chris Wilson8261b192011-04-19 23:18:09 +01005256 old->dpms_mode = intel_crtc->dpms_mode;
5257 old->load_detect_temp = false;
5258
5259 /* Make sure the crtc and connector are running */
Jesse Barnes79e53942008-11-07 14:24:08 -08005260 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
Chris Wilson64927112011-04-20 07:25:26 +01005261 struct drm_encoder_helper_funcs *encoder_funcs;
5262 struct drm_crtc_helper_funcs *crtc_funcs;
5263
Jesse Barnes79e53942008-11-07 14:24:08 -08005264 crtc_funcs = crtc->helper_private;
5265 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Chris Wilson64927112011-04-20 07:25:26 +01005266
5267 encoder_funcs = encoder->helper_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08005268 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
5269 }
Chris Wilson8261b192011-04-19 23:18:09 +01005270
Chris Wilson71731882011-04-19 23:10:58 +01005271 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005272 }
5273
5274 /* Find an unused one (if possible) */
5275 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
5276 i++;
5277 if (!(encoder->possible_crtcs & (1 << i)))
5278 continue;
5279 if (!possible_crtc->enabled) {
5280 crtc = possible_crtc;
5281 break;
5282 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005283 }
5284
5285 /*
5286 * If we didn't find an unused CRTC, don't use any.
5287 */
5288 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01005289 DRM_DEBUG_KMS("no pipe available for load-detect\n");
5290 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005291 }
5292
5293 encoder->crtc = crtc;
Zhenyu Wangc1c43972010-03-30 14:39:30 +08005294 connector->encoder = encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005295
5296 intel_crtc = to_intel_crtc(crtc);
Chris Wilson8261b192011-04-19 23:18:09 +01005297 old->dpms_mode = intel_crtc->dpms_mode;
5298 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01005299 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08005300
Chris Wilson64927112011-04-20 07:25:26 +01005301 if (!mode)
5302 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08005303
Chris Wilsond2dff872011-04-19 08:36:26 +01005304 old_fb = crtc->fb;
5305
5306 /* We need a framebuffer large enough to accommodate all accesses
5307 * that the plane may generate whilst we perform load detection.
5308 * We can not rely on the fbcon either being present (we get called
5309 * during its initialisation to detect all boot displays, or it may
5310 * not even exist) or that it is large enough to satisfy the
5311 * requested mode.
5312 */
5313 crtc->fb = mode_fits_in_fbdev(dev, mode);
5314 if (crtc->fb == NULL) {
5315 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
5316 crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
5317 old->release_fb = crtc->fb;
5318 } else
5319 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
5320 if (IS_ERR(crtc->fb)) {
5321 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
5322 crtc->fb = old_fb;
5323 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005324 }
Chris Wilsond2dff872011-04-19 08:36:26 +01005325
5326 if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01005327 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01005328 if (old->release_fb)
5329 old->release_fb->funcs->destroy(old->release_fb);
5330 crtc->fb = old_fb;
Chris Wilson64927112011-04-20 07:25:26 +01005331 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005332 }
Chris Wilson71731882011-04-19 23:10:58 +01005333
Jesse Barnes79e53942008-11-07 14:24:08 -08005334 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005335 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005336
Chris Wilson71731882011-04-19 23:10:58 +01005337 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005338}
5339
Zhenyu Wangc1c43972010-03-30 14:39:30 +08005340void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
Chris Wilson8261b192011-04-19 23:18:09 +01005341 struct drm_connector *connector,
5342 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08005343{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005344 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08005345 struct drm_device *dev = encoder->dev;
5346 struct drm_crtc *crtc = encoder->crtc;
5347 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
5348 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
5349
Chris Wilsond2dff872011-04-19 08:36:26 +01005350 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5351 connector->base.id, drm_get_connector_name(connector),
5352 encoder->base.id, drm_get_encoder_name(encoder));
5353
Chris Wilson8261b192011-04-19 23:18:09 +01005354 if (old->load_detect_temp) {
Zhenyu Wangc1c43972010-03-30 14:39:30 +08005355 connector->encoder = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08005356 drm_helper_disable_unused_functions(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +01005357
5358 if (old->release_fb)
5359 old->release_fb->funcs->destroy(old->release_fb);
5360
Chris Wilson0622a532011-04-21 09:32:11 +01005361 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08005362 }
5363
Eric Anholtc751ce42010-03-25 11:48:48 -07005364 /* Switch crtc and encoder back off if necessary */
Chris Wilson0622a532011-04-21 09:32:11 +01005365 if (old->dpms_mode != DRM_MODE_DPMS_ON) {
5366 encoder_funcs->dpms(encoder, old->dpms_mode);
Chris Wilson8261b192011-04-19 23:18:09 +01005367 crtc_funcs->dpms(crtc, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08005368 }
5369}
5370
5371/* Returns the clock of the currently programmed mode of the given pipe. */
5372static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
5373{
5374 struct drm_i915_private *dev_priv = dev->dev_private;
5375 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5376 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08005377 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08005378 u32 fp;
5379 intel_clock_t clock;
5380
5381 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01005382 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08005383 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01005384 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08005385
5386 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05005387 if (IS_PINEVIEW(dev)) {
5388 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
5389 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08005390 } else {
5391 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
5392 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
5393 }
5394
Chris Wilsona6c45cf2010-09-17 00:32:17 +01005395 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05005396 if (IS_PINEVIEW(dev))
5397 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
5398 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08005399 else
5400 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08005401 DPLL_FPA01_P1_POST_DIV_SHIFT);
5402
5403 switch (dpll & DPLL_MODE_MASK) {
5404 case DPLLB_MODE_DAC_SERIAL:
5405 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
5406 5 : 10;
5407 break;
5408 case DPLLB_MODE_LVDS:
5409 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
5410 7 : 14;
5411 break;
5412 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08005413 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08005414 "mode\n", (int)(dpll & DPLL_MODE_MASK));
5415 return 0;
5416 }
5417
5418 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08005419 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005420 } else {
5421 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
5422
5423 if (is_lvds) {
5424 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
5425 DPLL_FPA01_P1_POST_DIV_SHIFT);
5426 clock.p2 = 14;
5427
5428 if ((dpll & PLL_REF_INPUT_MASK) ==
5429 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
5430 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08005431 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005432 } else
Shaohua Li21778322009-02-23 15:19:16 +08005433 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005434 } else {
5435 if (dpll & PLL_P1_DIVIDE_BY_TWO)
5436 clock.p1 = 2;
5437 else {
5438 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
5439 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
5440 }
5441 if (dpll & PLL_P2_DIVIDE_BY_4)
5442 clock.p2 = 4;
5443 else
5444 clock.p2 = 2;
5445
Shaohua Li21778322009-02-23 15:19:16 +08005446 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005447 }
5448 }
5449
5450 /* XXX: It would be nice to validate the clocks, but we can't reuse
5451 * i830PllIsValid() because it relies on the xf86_config connector
5452 * configuration being accurate, which it isn't necessarily.
5453 */
5454
5455 return clock.dot;
5456}
5457
5458/** Returns the currently programmed mode of the given pipe. */
5459struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
5460 struct drm_crtc *crtc)
5461{
Jesse Barnes548f2452011-02-17 10:40:53 -08005462 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08005463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5464 int pipe = intel_crtc->pipe;
5465 struct drm_display_mode *mode;
Jesse Barnes548f2452011-02-17 10:40:53 -08005466 int htot = I915_READ(HTOTAL(pipe));
5467 int hsync = I915_READ(HSYNC(pipe));
5468 int vtot = I915_READ(VTOTAL(pipe));
5469 int vsync = I915_READ(VSYNC(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08005470
5471 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
5472 if (!mode)
5473 return NULL;
5474
5475 mode->clock = intel_crtc_clock_get(dev, crtc);
5476 mode->hdisplay = (htot & 0xffff) + 1;
5477 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
5478 mode->hsync_start = (hsync & 0xffff) + 1;
5479 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
5480 mode->vdisplay = (vtot & 0xffff) + 1;
5481 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
5482 mode->vsync_start = (vsync & 0xffff) + 1;
5483 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
5484
5485 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08005486
5487 return mode;
5488}
5489
Jesse Barnes652c3932009-08-17 13:31:43 -07005490#define GPU_IDLE_TIMEOUT 500 /* ms */
5491
5492/* When this timer fires, we've been idle for awhile */
5493static void intel_gpu_idle_timer(unsigned long arg)
5494{
5495 struct drm_device *dev = (struct drm_device *)arg;
5496 drm_i915_private_t *dev_priv = dev->dev_private;
5497
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005498 if (!list_empty(&dev_priv->mm.active_list)) {
5499 /* Still processing requests, so just re-arm the timer. */
5500 mod_timer(&dev_priv->idle_timer, jiffies +
5501 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
5502 return;
5503 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005504
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005505 dev_priv->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07005506 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07005507}
5508
Jesse Barnes652c3932009-08-17 13:31:43 -07005509#define CRTC_IDLE_TIMEOUT 1000 /* ms */
5510
5511static void intel_crtc_idle_timer(unsigned long arg)
5512{
5513 struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
5514 struct drm_crtc *crtc = &intel_crtc->base;
5515 drm_i915_private_t *dev_priv = crtc->dev->dev_private;
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00005516 struct intel_framebuffer *intel_fb;
5517
5518 intel_fb = to_intel_framebuffer(crtc->fb);
5519 if (intel_fb && intel_fb->obj->active) {
5520 /* The framebuffer is still being accessed by the GPU. */
5521 mod_timer(&intel_crtc->idle_timer, jiffies +
5522 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
5523 return;
5524 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005525
Jesse Barnes652c3932009-08-17 13:31:43 -07005526 intel_crtc->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07005527 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07005528}
5529
Daniel Vetter3dec0092010-08-20 21:40:52 +02005530static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07005531{
5532 struct drm_device *dev = crtc->dev;
5533 drm_i915_private_t *dev_priv = dev->dev_private;
5534 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5535 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005536 int dpll_reg = DPLL(pipe);
5537 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07005538
Eric Anholtbad720f2009-10-22 16:11:14 -07005539 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07005540 return;
5541
5542 if (!dev_priv->lvds_downclock_avail)
5543 return;
5544
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005545 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07005546 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08005547 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005548
Sean Paul8ac5a6d2012-02-13 13:14:51 -05005549 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07005550
5551 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
5552 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005553 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08005554
Jesse Barnes652c3932009-08-17 13:31:43 -07005555 dpll = I915_READ(dpll_reg);
5556 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08005557 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005558 }
5559
5560 /* Schedule downclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02005561 mod_timer(&intel_crtc->idle_timer, jiffies +
5562 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07005563}
5564
5565static void intel_decrease_pllclock(struct drm_crtc *crtc)
5566{
5567 struct drm_device *dev = crtc->dev;
5568 drm_i915_private_t *dev_priv = dev->dev_private;
5569 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07005570
Eric Anholtbad720f2009-10-22 16:11:14 -07005571 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07005572 return;
5573
5574 if (!dev_priv->lvds_downclock_avail)
5575 return;
5576
5577 /*
5578 * Since this is called by a timer, we should never get here in
5579 * the manual case.
5580 */
5581 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01005582 int pipe = intel_crtc->pipe;
5583 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02005584 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01005585
Zhao Yakui44d98a62009-10-09 11:39:40 +08005586 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005587
Sean Paul8ac5a6d2012-02-13 13:14:51 -05005588 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07005589
Chris Wilson074b5e12012-05-02 12:07:06 +01005590 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07005591 dpll |= DISPLAY_RATE_SELECT_FPA1;
5592 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005593 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07005594 dpll = I915_READ(dpll_reg);
5595 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08005596 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07005597 }
5598
5599}
5600
5601/**
5602 * intel_idle_update - adjust clocks for idleness
5603 * @work: work struct
5604 *
5605 * Either the GPU or display (or both) went idle. Check the busy status
5606 * here and adjust the CRTC and GPU clocks as necessary.
5607 */
5608static void intel_idle_update(struct work_struct *work)
5609{
5610 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
5611 idle_work);
5612 struct drm_device *dev = dev_priv->dev;
5613 struct drm_crtc *crtc;
5614 struct intel_crtc *intel_crtc;
5615
5616 if (!i915_powersave)
5617 return;
5618
5619 mutex_lock(&dev->struct_mutex);
5620
Jesse Barnes7648fa92010-05-20 14:28:11 -07005621 i915_update_gfx_val(dev_priv);
5622
Jesse Barnes652c3932009-08-17 13:31:43 -07005623 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5624 /* Skip inactive CRTCs */
5625 if (!crtc->fb)
5626 continue;
5627
5628 intel_crtc = to_intel_crtc(crtc);
5629 if (!intel_crtc->busy)
5630 intel_decrease_pllclock(crtc);
5631 }
5632
Li Peng45ac22c2010-06-12 23:38:35 +08005633
Jesse Barnes652c3932009-08-17 13:31:43 -07005634 mutex_unlock(&dev->struct_mutex);
5635}
5636
5637/**
5638 * intel_mark_busy - mark the GPU and possibly the display busy
5639 * @dev: drm device
5640 * @obj: object we're operating on
5641 *
5642 * Callers can use this function to indicate that the GPU is busy processing
5643 * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
5644 * buffer), we'll also mark the display as busy, so we know to increase its
5645 * clock frequency.
5646 */
Chris Wilson05394f32010-11-08 19:18:58 +00005647void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
Jesse Barnes652c3932009-08-17 13:31:43 -07005648{
5649 drm_i915_private_t *dev_priv = dev->dev_private;
5650 struct drm_crtc *crtc = NULL;
5651 struct intel_framebuffer *intel_fb;
5652 struct intel_crtc *intel_crtc;
5653
Zhenyu Wang5e17ee72009-09-03 09:30:06 +08005654 if (!drm_core_check_feature(dev, DRIVER_MODESET))
5655 return;
5656
Chris Wilson91041832012-04-26 11:28:42 +01005657 if (!dev_priv->busy) {
5658 intel_sanitize_pm(dev);
Chris Wilson28cf7982009-11-30 01:08:56 +00005659 dev_priv->busy = true;
Chris Wilson91041832012-04-26 11:28:42 +01005660 } else
Chris Wilson28cf7982009-11-30 01:08:56 +00005661 mod_timer(&dev_priv->idle_timer, jiffies +
5662 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07005663
Chris Wilsonacb87df2012-05-03 15:47:57 +01005664 if (obj == NULL)
5665 return;
5666
Jesse Barnes652c3932009-08-17 13:31:43 -07005667 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5668 if (!crtc->fb)
5669 continue;
5670
5671 intel_crtc = to_intel_crtc(crtc);
5672 intel_fb = to_intel_framebuffer(crtc->fb);
5673 if (intel_fb->obj == obj) {
5674 if (!intel_crtc->busy) {
5675 /* Non-busy -> busy, upclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02005676 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07005677 intel_crtc->busy = true;
5678 } else {
5679 /* Busy -> busy, put off timer */
5680 mod_timer(&intel_crtc->idle_timer, jiffies +
5681 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
5682 }
5683 }
5684 }
5685}
5686
Jesse Barnes79e53942008-11-07 14:24:08 -08005687static void intel_crtc_destroy(struct drm_crtc *crtc)
5688{
5689 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02005690 struct drm_device *dev = crtc->dev;
5691 struct intel_unpin_work *work;
5692 unsigned long flags;
5693
5694 spin_lock_irqsave(&dev->event_lock, flags);
5695 work = intel_crtc->unpin_work;
5696 intel_crtc->unpin_work = NULL;
5697 spin_unlock_irqrestore(&dev->event_lock, flags);
5698
5699 if (work) {
5700 cancel_work_sync(&work->work);
5701 kfree(work);
5702 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005703
5704 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02005705
Jesse Barnes79e53942008-11-07 14:24:08 -08005706 kfree(intel_crtc);
5707}
5708
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005709static void intel_unpin_work_fn(struct work_struct *__work)
5710{
5711 struct intel_unpin_work *work =
5712 container_of(__work, struct intel_unpin_work, work);
5713
5714 mutex_lock(&work->dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01005715 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00005716 drm_gem_object_unreference(&work->pending_flip_obj->base);
5717 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00005718
Chris Wilson7782de32011-07-08 12:22:41 +01005719 intel_update_fbc(work->dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005720 mutex_unlock(&work->dev->struct_mutex);
5721 kfree(work);
5722}
5723
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005724static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01005725 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005726{
5727 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005728 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5729 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00005730 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005731 struct drm_pending_vblank_event *e;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005732 struct timeval tnow, tvbl;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005733 unsigned long flags;
5734
5735 /* Ignore early vblank irqs */
5736 if (intel_crtc == NULL)
5737 return;
5738
Mario Kleiner49b14a52010-12-09 07:00:07 +01005739 do_gettimeofday(&tnow);
5740
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005741 spin_lock_irqsave(&dev->event_lock, flags);
5742 work = intel_crtc->unpin_work;
5743 if (work == NULL || !work->pending) {
5744 spin_unlock_irqrestore(&dev->event_lock, flags);
5745 return;
5746 }
5747
5748 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005749
5750 if (work->event) {
5751 e = work->event;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005752 e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005753
5754 /* Called before vblank count and timestamps have
5755 * been updated for the vblank interval of flip
5756 * completion? Need to increment vblank count and
5757 * add one videorefresh duration to returned timestamp
Mario Kleiner49b14a52010-12-09 07:00:07 +01005758 * to account for this. We assume this happened if we
5759 * get called over 0.9 frame durations after the last
5760 * timestamped vblank.
5761 *
5762 * This calculation can not be used with vrefresh rates
5763 * below 5Hz (10Hz to be on the safe side) without
5764 * promoting to 64 integers.
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005765 */
Mario Kleiner49b14a52010-12-09 07:00:07 +01005766 if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
5767 9 * crtc->framedur_ns) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005768 e->event.sequence++;
Mario Kleiner49b14a52010-12-09 07:00:07 +01005769 tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
5770 crtc->framedur_ns);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005771 }
5772
Mario Kleiner49b14a52010-12-09 07:00:07 +01005773 e->event.tv_sec = tvbl.tv_sec;
5774 e->event.tv_usec = tvbl.tv_usec;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005775
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005776 list_add_tail(&e->base.link,
5777 &e->base.file_priv->event_list);
5778 wake_up_interruptible(&e->base.file_priv->event_wait);
5779 }
5780
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01005781 drm_vblank_put(dev, intel_crtc->pipe);
5782
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005783 spin_unlock_irqrestore(&dev->event_lock, flags);
5784
Chris Wilson05394f32010-11-08 19:18:58 +00005785 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00005786
Chris Wilsone59f2ba2010-10-07 17:28:15 +01005787 atomic_clear_mask(1 << intel_crtc->plane,
Chris Wilson05394f32010-11-08 19:18:58 +00005788 &obj->pending_flip.counter);
5789 if (atomic_read(&obj->pending_flip) == 0)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01005790 wake_up(&dev_priv->pending_flip_queue);
Chris Wilsond9e86c02010-11-10 16:40:20 +00005791
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005792 schedule_work(&work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07005793
5794 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005795}
5796
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005797void intel_finish_page_flip(struct drm_device *dev, int pipe)
5798{
5799 drm_i915_private_t *dev_priv = dev->dev_private;
5800 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
5801
Mario Kleiner49b14a52010-12-09 07:00:07 +01005802 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005803}
5804
5805void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
5806{
5807 drm_i915_private_t *dev_priv = dev->dev_private;
5808 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
5809
Mario Kleiner49b14a52010-12-09 07:00:07 +01005810 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07005811}
5812
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005813void intel_prepare_page_flip(struct drm_device *dev, int plane)
5814{
5815 drm_i915_private_t *dev_priv = dev->dev_private;
5816 struct intel_crtc *intel_crtc =
5817 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
5818 unsigned long flags;
5819
5820 spin_lock_irqsave(&dev->event_lock, flags);
Jesse Barnesde3f4402010-01-14 13:18:02 -08005821 if (intel_crtc->unpin_work) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01005822 if ((++intel_crtc->unpin_work->pending) > 1)
5823 DRM_ERROR("Prepared flip multiple times\n");
Jesse Barnesde3f4402010-01-14 13:18:02 -08005824 } else {
5825 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
5826 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05005827 spin_unlock_irqrestore(&dev->event_lock, flags);
5828}
5829
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005830static int intel_gen2_queue_flip(struct drm_device *dev,
5831 struct drm_crtc *crtc,
5832 struct drm_framebuffer *fb,
5833 struct drm_i915_gem_object *obj)
5834{
5835 struct drm_i915_private *dev_priv = dev->dev_private;
5836 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5837 unsigned long offset;
5838 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02005839 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005840 int ret;
5841
Daniel Vetter6d90c952012-04-26 23:28:05 +02005842 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005843 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01005844 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005845
5846 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Ville Syrjälä01f2c772011-12-20 00:06:49 +02005847 offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005848
Daniel Vetter6d90c952012-04-26 23:28:05 +02005849 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005850 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01005851 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005852
5853 /* Can't queue multiple flips, so wait for the previous
5854 * one to finish before executing the next.
5855 */
5856 if (intel_crtc->plane)
5857 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
5858 else
5859 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02005860 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
5861 intel_ring_emit(ring, MI_NOOP);
5862 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5863 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5864 intel_ring_emit(ring, fb->pitches[0]);
5865 intel_ring_emit(ring, obj->gtt_offset + offset);
5866 intel_ring_emit(ring, 0); /* aux display base address, unused */
5867 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01005868 return 0;
5869
5870err_unpin:
5871 intel_unpin_fb_obj(obj);
5872err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005873 return ret;
5874}
5875
5876static int intel_gen3_queue_flip(struct drm_device *dev,
5877 struct drm_crtc *crtc,
5878 struct drm_framebuffer *fb,
5879 struct drm_i915_gem_object *obj)
5880{
5881 struct drm_i915_private *dev_priv = dev->dev_private;
5882 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5883 unsigned long offset;
5884 u32 flip_mask;
Daniel Vetter6d90c952012-04-26 23:28:05 +02005885 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005886 int ret;
5887
Daniel Vetter6d90c952012-04-26 23:28:05 +02005888 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005889 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01005890 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005891
5892 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Ville Syrjälä01f2c772011-12-20 00:06:49 +02005893 offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005894
Daniel Vetter6d90c952012-04-26 23:28:05 +02005895 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005896 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01005897 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005898
5899 if (intel_crtc->plane)
5900 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
5901 else
5902 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02005903 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
5904 intel_ring_emit(ring, MI_NOOP);
5905 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
5906 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5907 intel_ring_emit(ring, fb->pitches[0]);
5908 intel_ring_emit(ring, obj->gtt_offset + offset);
5909 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005910
Daniel Vetter6d90c952012-04-26 23:28:05 +02005911 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01005912 return 0;
5913
5914err_unpin:
5915 intel_unpin_fb_obj(obj);
5916err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005917 return ret;
5918}
5919
5920static int intel_gen4_queue_flip(struct drm_device *dev,
5921 struct drm_crtc *crtc,
5922 struct drm_framebuffer *fb,
5923 struct drm_i915_gem_object *obj)
5924{
5925 struct drm_i915_private *dev_priv = dev->dev_private;
5926 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5927 uint32_t pf, pipesrc;
Daniel Vetter6d90c952012-04-26 23:28:05 +02005928 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005929 int ret;
5930
Daniel Vetter6d90c952012-04-26 23:28:05 +02005931 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005932 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01005933 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005934
Daniel Vetter6d90c952012-04-26 23:28:05 +02005935 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005936 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01005937 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005938
5939 /* i965+ uses the linear or tiled offsets from the
5940 * Display Registers (which do not change across a page-flip)
5941 * so we need only reprogram the base address.
5942 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02005943 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5944 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5945 intel_ring_emit(ring, fb->pitches[0]);
5946 intel_ring_emit(ring, obj->gtt_offset | obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005947
5948 /* XXX Enabling the panel-fitter across page-flip is so far
5949 * untested on non-native modes, so ignore it for now.
5950 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
5951 */
5952 pf = 0;
5953 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02005954 intel_ring_emit(ring, pf | pipesrc);
5955 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01005956 return 0;
5957
5958err_unpin:
5959 intel_unpin_fb_obj(obj);
5960err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005961 return ret;
5962}
5963
5964static int intel_gen6_queue_flip(struct drm_device *dev,
5965 struct drm_crtc *crtc,
5966 struct drm_framebuffer *fb,
5967 struct drm_i915_gem_object *obj)
5968{
5969 struct drm_i915_private *dev_priv = dev->dev_private;
5970 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter6d90c952012-04-26 23:28:05 +02005971 struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005972 uint32_t pf, pipesrc;
5973 int ret;
5974
Daniel Vetter6d90c952012-04-26 23:28:05 +02005975 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005976 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01005977 goto err;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005978
Daniel Vetter6d90c952012-04-26 23:28:05 +02005979 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005980 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01005981 goto err_unpin;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005982
Daniel Vetter6d90c952012-04-26 23:28:05 +02005983 intel_ring_emit(ring, MI_DISPLAY_FLIP |
5984 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
5985 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
5986 intel_ring_emit(ring, obj->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005987
Chris Wilson99d9acd2012-04-17 20:37:00 +01005988 /* Contrary to the suggestions in the documentation,
5989 * "Enable Panel Fitter" does not seem to be required when page
5990 * flipping with a non-native mode, and worse causes a normal
5991 * modeset to fail.
5992 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
5993 */
5994 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07005995 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02005996 intel_ring_emit(ring, pf | pipesrc);
5997 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01005998 return 0;
5999
6000err_unpin:
6001 intel_unpin_fb_obj(obj);
6002err:
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006003 return ret;
6004}
6005
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006006/*
6007 * On gen7 we currently use the blit ring because (in early silicon at least)
6008 * the render ring doesn't give us interrpts for page flip completion, which
6009 * means clients will hang after the first flip is queued. Fortunately the
6010 * blit ring generates interrupts properly, so use it instead.
6011 */
6012static int intel_gen7_queue_flip(struct drm_device *dev,
6013 struct drm_crtc *crtc,
6014 struct drm_framebuffer *fb,
6015 struct drm_i915_gem_object *obj)
6016{
6017 struct drm_i915_private *dev_priv = dev->dev_private;
6018 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6019 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
6020 int ret;
6021
6022 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
6023 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006024 goto err;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006025
6026 ret = intel_ring_begin(ring, 4);
6027 if (ret)
Chris Wilson83d40922012-04-17 19:35:53 +01006028 goto err_unpin;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006029
6030 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006031 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006032 intel_ring_emit(ring, (obj->gtt_offset));
6033 intel_ring_emit(ring, (MI_NOOP));
6034 intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01006035 return 0;
6036
6037err_unpin:
6038 intel_unpin_fb_obj(obj);
6039err:
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006040 return ret;
6041}
6042
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006043static int intel_default_queue_flip(struct drm_device *dev,
6044 struct drm_crtc *crtc,
6045 struct drm_framebuffer *fb,
6046 struct drm_i915_gem_object *obj)
6047{
6048 return -ENODEV;
6049}
6050
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006051static int intel_crtc_page_flip(struct drm_crtc *crtc,
6052 struct drm_framebuffer *fb,
6053 struct drm_pending_vblank_event *event)
6054{
6055 struct drm_device *dev = crtc->dev;
6056 struct drm_i915_private *dev_priv = dev->dev_private;
6057 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00006058 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006059 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6060 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006061 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01006062 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006063
6064 work = kzalloc(sizeof *work, GFP_KERNEL);
6065 if (work == NULL)
6066 return -ENOMEM;
6067
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006068 work->event = event;
6069 work->dev = crtc->dev;
6070 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08006071 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006072 INIT_WORK(&work->work, intel_unpin_work_fn);
6073
Jesse Barnes7317c75e62011-08-29 09:45:28 -07006074 ret = drm_vblank_get(dev, intel_crtc->pipe);
6075 if (ret)
6076 goto free_work;
6077
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006078 /* We borrow the event spin lock for protecting unpin_work */
6079 spin_lock_irqsave(&dev->event_lock, flags);
6080 if (intel_crtc->unpin_work) {
6081 spin_unlock_irqrestore(&dev->event_lock, flags);
6082 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07006083 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01006084
6085 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006086 return -EBUSY;
6087 }
6088 intel_crtc->unpin_work = work;
6089 spin_unlock_irqrestore(&dev->event_lock, flags);
6090
6091 intel_fb = to_intel_framebuffer(fb);
6092 obj = intel_fb->obj;
6093
Chris Wilson468f0b42010-05-27 13:18:13 +01006094 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006095
Jesse Barnes75dfca82010-02-10 15:09:44 -08006096 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00006097 drm_gem_object_reference(&work->old_fb_obj->base);
6098 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006099
6100 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01006101
Chris Wilsone1f99ce2010-10-27 12:45:26 +01006102 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01006103
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01006104 work->enable_stall_check = true;
6105
Chris Wilsone1f99ce2010-10-27 12:45:26 +01006106 /* Block clients from rendering to the new back buffer until
6107 * the flip occurs and the object is no longer visible.
6108 */
Chris Wilson05394f32010-11-08 19:18:58 +00006109 atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01006110
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006111 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
6112 if (ret)
6113 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006114
Chris Wilson7782de32011-07-08 12:22:41 +01006115 intel_disable_fbc(dev);
Chris Wilsonacb87df2012-05-03 15:47:57 +01006116 intel_mark_busy(dev, obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006117 mutex_unlock(&dev->struct_mutex);
6118
Jesse Barnese5510fa2010-07-01 16:48:37 -07006119 trace_i915_flip_request(intel_crtc->plane, obj);
6120
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006121 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01006122
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006123cleanup_pending:
6124 atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilson05394f32010-11-08 19:18:58 +00006125 drm_gem_object_unreference(&work->old_fb_obj->base);
6126 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01006127 mutex_unlock(&dev->struct_mutex);
6128
6129 spin_lock_irqsave(&dev->event_lock, flags);
6130 intel_crtc->unpin_work = NULL;
6131 spin_unlock_irqrestore(&dev->event_lock, flags);
6132
Jesse Barnes7317c75e62011-08-29 09:45:28 -07006133 drm_vblank_put(dev, intel_crtc->pipe);
6134free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01006135 kfree(work);
6136
6137 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05006138}
6139
Chris Wilson47f1c6c2010-12-03 15:37:31 +00006140static void intel_sanitize_modesetting(struct drm_device *dev,
6141 int pipe, int plane)
6142{
6143 struct drm_i915_private *dev_priv = dev->dev_private;
6144 u32 reg, val;
6145
Chris Wilsonf47166d2012-03-22 15:00:50 +00006146 /* Clear any frame start delays used for debugging left by the BIOS */
6147 for_each_pipe(pipe) {
6148 reg = PIPECONF(pipe);
6149 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
6150 }
6151
Chris Wilson47f1c6c2010-12-03 15:37:31 +00006152 if (HAS_PCH_SPLIT(dev))
6153 return;
6154
6155 /* Who knows what state these registers were left in by the BIOS or
6156 * grub?
6157 *
6158 * If we leave the registers in a conflicting state (e.g. with the
6159 * display plane reading from the other pipe than the one we intend
6160 * to use) then when we attempt to teardown the active mode, we will
6161 * not disable the pipes and planes in the correct order -- leaving
6162 * a plane reading from a disabled pipe and possibly leading to
6163 * undefined behaviour.
6164 */
6165
6166 reg = DSPCNTR(plane);
6167 val = I915_READ(reg);
6168
6169 if ((val & DISPLAY_PLANE_ENABLE) == 0)
6170 return;
6171 if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
6172 return;
6173
6174 /* This display plane is active and attached to the other CPU pipe. */
6175 pipe = !pipe;
6176
6177 /* Disable the plane and wait for it to stop reading from the pipe. */
Jesse Barnesb24e7172011-01-04 15:09:30 -08006178 intel_disable_plane(dev_priv, plane, pipe);
6179 intel_disable_pipe(dev_priv, pipe);
Chris Wilson47f1c6c2010-12-03 15:37:31 +00006180}
Jesse Barnes79e53942008-11-07 14:24:08 -08006181
Chris Wilsonf6e5b162011-04-12 18:06:51 +01006182static void intel_crtc_reset(struct drm_crtc *crtc)
6183{
6184 struct drm_device *dev = crtc->dev;
6185 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6186
6187 /* Reset flags back to the 'unknown' status so that they
6188 * will be correctly set on the initial modeset.
6189 */
6190 intel_crtc->dpms_mode = -1;
6191
6192 /* We need to fix up any BIOS configuration that conflicts with
6193 * our expectations.
6194 */
6195 intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
6196}
6197
6198static struct drm_crtc_helper_funcs intel_helper_funcs = {
6199 .dpms = intel_crtc_dpms,
6200 .mode_fixup = intel_crtc_mode_fixup,
6201 .mode_set = intel_crtc_mode_set,
6202 .mode_set_base = intel_pipe_set_base,
6203 .mode_set_base_atomic = intel_pipe_set_base_atomic,
6204 .load_lut = intel_crtc_load_lut,
6205 .disable = intel_crtc_disable,
6206};
6207
6208static const struct drm_crtc_funcs intel_crtc_funcs = {
6209 .reset = intel_crtc_reset,
6210 .cursor_set = intel_crtc_cursor_set,
6211 .cursor_move = intel_crtc_cursor_move,
6212 .gamma_set = intel_crtc_gamma_set,
6213 .set_config = drm_crtc_helper_set_config,
6214 .destroy = intel_crtc_destroy,
6215 .page_flip = intel_crtc_page_flip,
6216};
6217
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006218static void intel_pch_pll_init(struct drm_device *dev)
6219{
6220 drm_i915_private_t *dev_priv = dev->dev_private;
6221 int i;
6222
6223 if (dev_priv->num_pch_pll == 0) {
6224 DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
6225 return;
6226 }
6227
6228 for (i = 0; i < dev_priv->num_pch_pll; i++) {
6229 dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
6230 dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
6231 dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
6232 }
6233}
6234
Hannes Ederb358d0a2008-12-18 21:18:47 +01006235static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08006236{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08006237 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006238 struct intel_crtc *intel_crtc;
6239 int i;
6240
6241 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
6242 if (intel_crtc == NULL)
6243 return;
6244
6245 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
6246
6247 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08006248 for (i = 0; i < 256; i++) {
6249 intel_crtc->lut_r[i] = i;
6250 intel_crtc->lut_g[i] = i;
6251 intel_crtc->lut_b[i] = i;
6252 }
6253
Jesse Barnes80824002009-09-10 15:28:06 -07006254 /* Swap pipes & planes for FBC on pre-965 */
6255 intel_crtc->pipe = pipe;
6256 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01006257 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006258 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01006259 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07006260 }
6261
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08006262 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
6263 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
6264 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
6265 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
6266
Chris Wilson5d1d0cc2011-01-24 15:02:15 +00006267 intel_crtc_reset(&intel_crtc->base);
Chris Wilson04dbff52011-02-10 17:38:35 +00006268 intel_crtc->active = true; /* force the pipe off on setup_init_config */
Jesse Barnes5a354202011-06-24 12:19:22 -07006269 intel_crtc->bpp = 24; /* default for pre-Ironlake */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07006270
6271 if (HAS_PCH_SPLIT(dev)) {
6272 intel_helper_funcs.prepare = ironlake_crtc_prepare;
6273 intel_helper_funcs.commit = ironlake_crtc_commit;
6274 } else {
6275 intel_helper_funcs.prepare = i9xx_crtc_prepare;
6276 intel_helper_funcs.commit = i9xx_crtc_commit;
6277 }
6278
Jesse Barnes79e53942008-11-07 14:24:08 -08006279 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
6280
Jesse Barnes652c3932009-08-17 13:31:43 -07006281 intel_crtc->busy = false;
6282
6283 setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
6284 (unsigned long)intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006285}
6286
Carl Worth08d7b3d2009-04-29 14:43:54 -07006287int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00006288 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07006289{
Carl Worth08d7b3d2009-04-29 14:43:54 -07006290 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02006291 struct drm_mode_object *drmmode_obj;
6292 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07006293
Daniel Vetter1cff8f62012-04-24 09:55:08 +02006294 if (!drm_core_check_feature(dev, DRIVER_MODESET))
6295 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -07006296
Daniel Vetterc05422d2009-08-11 16:05:30 +02006297 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
6298 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07006299
Daniel Vetterc05422d2009-08-11 16:05:30 +02006300 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07006301 DRM_ERROR("no such CRTC id\n");
6302 return -EINVAL;
6303 }
6304
Daniel Vetterc05422d2009-08-11 16:05:30 +02006305 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
6306 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07006307
Daniel Vetterc05422d2009-08-11 16:05:30 +02006308 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07006309}
6310
Zhenyu Wangc5e4df32010-03-30 14:39:27 +08006311static int intel_encoder_clones(struct drm_device *dev, int type_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08006312{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006313 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006314 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006315 int entry = 0;
6316
Chris Wilson4ef69c72010-09-09 15:14:28 +01006317 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
6318 if (type_mask & encoder->clone_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08006319 index_mask |= (1 << entry);
6320 entry++;
6321 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01006322
Jesse Barnes79e53942008-11-07 14:24:08 -08006323 return index_mask;
6324}
6325
Chris Wilson4d302442010-12-14 19:21:29 +00006326static bool has_edp_a(struct drm_device *dev)
6327{
6328 struct drm_i915_private *dev_priv = dev->dev_private;
6329
6330 if (!IS_MOBILE(dev))
6331 return false;
6332
6333 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
6334 return false;
6335
6336 if (IS_GEN5(dev) &&
6337 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
6338 return false;
6339
6340 return true;
6341}
6342
Jesse Barnes79e53942008-11-07 14:24:08 -08006343static void intel_setup_outputs(struct drm_device *dev)
6344{
Eric Anholt725e30a2009-01-22 13:01:02 -08006345 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006346 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04006347 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00006348 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08006349
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00006350 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00006351 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
6352 /* disable the panel fitter on everything but LVDS */
6353 I915_WRITE(PFIT_CONTROL, 0);
6354 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006355
Eric Anholtbad720f2009-10-22 16:11:14 -07006356 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04006357 dpd_is_edp = intel_dpd_is_edp(dev);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08006358
Chris Wilson4d302442010-12-14 19:21:29 +00006359 if (has_edp_a(dev))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08006360 intel_dp_init(dev, DP_A);
6361
Adam Jacksoncb0953d2010-07-16 14:46:29 -04006362 if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
6363 intel_dp_init(dev, PCH_DP_D);
6364 }
6365
6366 intel_crt_init(dev);
6367
6368 if (HAS_PCH_SPLIT(dev)) {
6369 int found;
6370
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08006371 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08006372 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +01006373 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08006374 if (!found)
6375 intel_hdmi_init(dev, HDMIB);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08006376 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
6377 intel_dp_init(dev, PCH_DP_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08006378 }
6379
6380 if (I915_READ(HDMIC) & PORT_DETECTED)
6381 intel_hdmi_init(dev, HDMIC);
6382
6383 if (I915_READ(HDMID) & PORT_DETECTED)
6384 intel_hdmi_init(dev, HDMID);
6385
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08006386 if (I915_READ(PCH_DP_C) & DP_DETECTED)
6387 intel_dp_init(dev, PCH_DP_C);
6388
Adam Jacksoncb0953d2010-07-16 14:46:29 -04006389 if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08006390 intel_dp_init(dev, PCH_DP_D);
6391
Zhenyu Wang103a1962009-11-27 11:44:36 +08006392 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08006393 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08006394
Eric Anholt725e30a2009-01-22 13:01:02 -08006395 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08006396 DRM_DEBUG_KMS("probing SDVOB\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01006397 found = intel_sdvo_init(dev, SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08006398 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
6399 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08006400 intel_hdmi_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08006401 }
Ma Ling27185ae2009-08-24 13:50:23 +08006402
Jesse Barnesb01f2c32009-12-11 11:07:17 -08006403 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
6404 DRM_DEBUG_KMS("probing DP_B\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07006405 intel_dp_init(dev, DP_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08006406 }
Eric Anholt725e30a2009-01-22 13:01:02 -08006407 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04006408
6409 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04006410
Jesse Barnesb01f2c32009-12-11 11:07:17 -08006411 if (I915_READ(SDVOB) & SDVO_DETECTED) {
6412 DRM_DEBUG_KMS("probing SDVOC\n");
Daniel Vettereef4eac2012-03-23 23:43:35 +01006413 found = intel_sdvo_init(dev, SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08006414 }
Ma Ling27185ae2009-08-24 13:50:23 +08006415
6416 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
6417
Jesse Barnesb01f2c32009-12-11 11:07:17 -08006418 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
6419 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08006420 intel_hdmi_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08006421 }
6422 if (SUPPORTS_INTEGRATED_DP(dev)) {
6423 DRM_DEBUG_KMS("probing DP_C\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07006424 intel_dp_init(dev, DP_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08006425 }
Eric Anholt725e30a2009-01-22 13:01:02 -08006426 }
Ma Ling27185ae2009-08-24 13:50:23 +08006427
Jesse Barnesb01f2c32009-12-11 11:07:17 -08006428 if (SUPPORTS_INTEGRATED_DP(dev) &&
6429 (I915_READ(DP_D) & DP_DETECTED)) {
6430 DRM_DEBUG_KMS("probing DP_D\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07006431 intel_dp_init(dev, DP_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08006432 }
Eric Anholtbad720f2009-10-22 16:11:14 -07006433 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08006434 intel_dvo_init(dev);
6435
Zhenyu Wang103a1962009-11-27 11:44:36 +08006436 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08006437 intel_tv_init(dev);
6438
Chris Wilson4ef69c72010-09-09 15:14:28 +01006439 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
6440 encoder->base.possible_crtcs = encoder->crtc_mask;
6441 encoder->base.possible_clones =
6442 intel_encoder_clones(dev, encoder->clone_mask);
Jesse Barnes79e53942008-11-07 14:24:08 -08006443 }
Chris Wilson47356eb2011-01-11 17:06:04 +00006444
Chris Wilson2c7111d2011-03-29 10:40:27 +01006445 /* disable all the possible outputs/crtcs before entering KMS mode */
6446 drm_helper_disable_unused_functions(dev);
Keith Packard9fb526d2011-09-26 22:24:57 -07006447
6448 if (HAS_PCH_SPLIT(dev))
6449 ironlake_init_pch_refclk(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006450}
6451
6452static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
6453{
6454 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08006455
6456 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00006457 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08006458
6459 kfree(intel_fb);
6460}
6461
6462static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00006463 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006464 unsigned int *handle)
6465{
6466 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00006467 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08006468
Chris Wilson05394f32010-11-08 19:18:58 +00006469 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08006470}
6471
6472static const struct drm_framebuffer_funcs intel_fb_funcs = {
6473 .destroy = intel_user_framebuffer_destroy,
6474 .create_handle = intel_user_framebuffer_create_handle,
6475};
6476
Dave Airlie38651672010-03-30 05:34:13 +00006477int intel_framebuffer_init(struct drm_device *dev,
6478 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006479 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00006480 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08006481{
Jesse Barnes79e53942008-11-07 14:24:08 -08006482 int ret;
6483
Chris Wilson05394f32010-11-08 19:18:58 +00006484 if (obj->tiling_mode == I915_TILING_Y)
Chris Wilson57cd6502010-08-08 12:34:44 +01006485 return -EINVAL;
6486
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006487 if (mode_cmd->pitches[0] & 63)
Chris Wilson57cd6502010-08-08 12:34:44 +01006488 return -EINVAL;
6489
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006490 switch (mode_cmd->pixel_format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02006491 case DRM_FORMAT_RGB332:
6492 case DRM_FORMAT_RGB565:
6493 case DRM_FORMAT_XRGB8888:
Jesse Barnesb250da72012-03-07 08:49:29 -08006494 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +02006495 case DRM_FORMAT_ARGB8888:
6496 case DRM_FORMAT_XRGB2101010:
6497 case DRM_FORMAT_ARGB2101010:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006498 /* RGB formats are common across chipsets */
Jesse Barnesb5626742011-06-24 12:19:27 -07006499 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02006500 case DRM_FORMAT_YUYV:
6501 case DRM_FORMAT_UYVY:
6502 case DRM_FORMAT_YVYU:
6503 case DRM_FORMAT_VYUY:
Chris Wilson57cd6502010-08-08 12:34:44 +01006504 break;
6505 default:
Eugeni Dodonovaca25842012-01-17 15:25:45 -02006506 DRM_DEBUG_KMS("unsupported pixel format %u\n",
6507 mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01006508 return -EINVAL;
6509 }
6510
Jesse Barnes79e53942008-11-07 14:24:08 -08006511 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
6512 if (ret) {
6513 DRM_ERROR("framebuffer init failed %d\n", ret);
6514 return ret;
6515 }
6516
6517 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -08006518 intel_fb->obj = obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08006519 return 0;
6520}
6521
Jesse Barnes79e53942008-11-07 14:24:08 -08006522static struct drm_framebuffer *
6523intel_user_framebuffer_create(struct drm_device *dev,
6524 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006525 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08006526{
Chris Wilson05394f32010-11-08 19:18:58 +00006527 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08006528
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006529 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
6530 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00006531 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01006532 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08006533
Chris Wilsond2dff872011-04-19 08:36:26 +01006534 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08006535}
6536
Jesse Barnes79e53942008-11-07 14:24:08 -08006537static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08006538 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00006539 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08006540};
6541
Jesse Barnese70236a2009-09-21 10:42:27 -07006542/* Set up chip specific display functions */
6543static void intel_init_display(struct drm_device *dev)
6544{
6545 struct drm_i915_private *dev_priv = dev->dev_private;
6546
6547 /* We always want a DPMS function */
Eric Anholtf564048e2011-03-30 13:01:02 -07006548 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006549 dev_priv->display.dpms = ironlake_crtc_dpms;
Eric Anholtf564048e2011-03-30 13:01:02 -07006550 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006551 dev_priv->display.off = ironlake_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07006552 dev_priv->display.update_plane = ironlake_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07006553 } else {
Jesse Barnese70236a2009-09-21 10:42:27 -07006554 dev_priv->display.dpms = i9xx_crtc_dpms;
Eric Anholtf564048e2011-03-30 13:01:02 -07006555 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006556 dev_priv->display.off = i9xx_crtc_off;
Jesse Barnes17638cd2011-06-24 12:19:23 -07006557 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07006558 }
Jesse Barnese70236a2009-09-21 10:42:27 -07006559
Jesse Barnese70236a2009-09-21 10:42:27 -07006560 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006561 if (IS_VALLEYVIEW(dev))
6562 dev_priv->display.get_display_clock_speed =
6563 valleyview_get_display_clock_speed;
6564 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07006565 dev_priv->display.get_display_clock_speed =
6566 i945_get_display_clock_speed;
6567 else if (IS_I915G(dev))
6568 dev_priv->display.get_display_clock_speed =
6569 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006570 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006571 dev_priv->display.get_display_clock_speed =
6572 i9xx_misc_get_display_clock_speed;
6573 else if (IS_I915GM(dev))
6574 dev_priv->display.get_display_clock_speed =
6575 i915gm_get_display_clock_speed;
6576 else if (IS_I865G(dev))
6577 dev_priv->display.get_display_clock_speed =
6578 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02006579 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07006580 dev_priv->display.get_display_clock_speed =
6581 i855_get_display_clock_speed;
6582 else /* 852, 830 */
6583 dev_priv->display.get_display_clock_speed =
6584 i830_get_display_clock_speed;
6585
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006586 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01006587 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07006588 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08006589 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08006590 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -07006591 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08006592 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07006593 } else if (IS_IVYBRIDGE(dev)) {
6594 /* FIXME: detect B0+ stepping and use auto training */
6595 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +08006596 dev_priv->display.write_eld = ironlake_write_eld;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08006597 } else
6598 dev_priv->display.update_wm = NULL;
Jesse Barnesceb04242012-03-28 13:39:22 -07006599 } else if (IS_VALLEYVIEW(dev)) {
Jesse Barnes575155a2012-03-28 13:39:37 -07006600 dev_priv->display.force_wake_get = vlv_force_wake_get;
6601 dev_priv->display.force_wake_put = vlv_force_wake_put;
Jesse Barnes6067aae2011-04-28 15:04:31 -07006602 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08006603 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07006604 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006605
6606 /* Default just returns -ENODEV to indicate unsupported */
6607 dev_priv->display.queue_flip = intel_default_queue_flip;
6608
6609 switch (INTEL_INFO(dev)->gen) {
6610 case 2:
6611 dev_priv->display.queue_flip = intel_gen2_queue_flip;
6612 break;
6613
6614 case 3:
6615 dev_priv->display.queue_flip = intel_gen3_queue_flip;
6616 break;
6617
6618 case 4:
6619 case 5:
6620 dev_priv->display.queue_flip = intel_gen4_queue_flip;
6621 break;
6622
6623 case 6:
6624 dev_priv->display.queue_flip = intel_gen6_queue_flip;
6625 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07006626 case 7:
6627 dev_priv->display.queue_flip = intel_gen7_queue_flip;
6628 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07006629 }
Jesse Barnese70236a2009-09-21 10:42:27 -07006630}
6631
Jesse Barnesb690e962010-07-19 13:53:12 -07006632/*
6633 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
6634 * resume, or other times. This quirk makes sure that's the case for
6635 * affected systems.
6636 */
Akshay Joshi0206e352011-08-16 15:34:10 -04006637static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07006638{
6639 struct drm_i915_private *dev_priv = dev->dev_private;
6640
6641 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02006642 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07006643}
6644
Keith Packard435793d2011-07-12 14:56:22 -07006645/*
6646 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
6647 */
6648static void quirk_ssc_force_disable(struct drm_device *dev)
6649{
6650 struct drm_i915_private *dev_priv = dev->dev_private;
6651 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02006652 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -07006653}
6654
Carsten Emde4dca20e2012-03-15 15:56:26 +01006655/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01006656 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
6657 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01006658 */
6659static void quirk_invert_brightness(struct drm_device *dev)
6660{
6661 struct drm_i915_private *dev_priv = dev->dev_private;
6662 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +02006663 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -07006664}
6665
6666struct intel_quirk {
6667 int device;
6668 int subsystem_vendor;
6669 int subsystem_device;
6670 void (*hook)(struct drm_device *dev);
6671};
6672
Ben Widawskyc43b5632012-04-16 14:07:40 -07006673static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07006674 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04006675 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07006676
6677 /* Thinkpad R31 needs pipe A force quirk */
6678 { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
6679 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
6680 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
6681
6682 /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
6683 { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
6684 /* ThinkPad X40 needs pipe A force quirk */
6685
6686 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
6687 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
6688
6689 /* 855 & before need to leave pipe A & dpll A up */
6690 { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
6691 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07006692
6693 /* Lenovo U160 cannot use SSC on LVDS */
6694 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02006695
6696 /* Sony Vaio Y cannot use SSC on LVDS */
6697 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01006698
6699 /* Acer Aspire 5734Z must invert backlight brightness */
6700 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07006701};
6702
6703static void intel_init_quirks(struct drm_device *dev)
6704{
6705 struct pci_dev *d = dev->pdev;
6706 int i;
6707
6708 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
6709 struct intel_quirk *q = &intel_quirks[i];
6710
6711 if (d->device == q->device &&
6712 (d->subsystem_vendor == q->subsystem_vendor ||
6713 q->subsystem_vendor == PCI_ANY_ID) &&
6714 (d->subsystem_device == q->subsystem_device ||
6715 q->subsystem_device == PCI_ANY_ID))
6716 q->hook(dev);
6717 }
6718}
6719
Jesse Barnes9cce37f2010-08-13 15:11:26 -07006720/* Disable the VGA plane that we never use */
6721static void i915_disable_vga(struct drm_device *dev)
6722{
6723 struct drm_i915_private *dev_priv = dev->dev_private;
6724 u8 sr1;
6725 u32 vga_reg;
6726
6727 if (HAS_PCH_SPLIT(dev))
6728 vga_reg = CPU_VGACNTRL;
6729 else
6730 vga_reg = VGACNTRL;
6731
6732 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -07006733 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07006734 sr1 = inb(VGA_SR_DATA);
6735 outb(sr1 | 1<<5, VGA_SR_DATA);
6736 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
6737 udelay(300);
6738
6739 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
6740 POSTING_READ(vga_reg);
6741}
6742
Jesse Barnesf82cfb62012-04-11 09:23:35 -07006743static void ivb_pch_pwm_override(struct drm_device *dev)
6744{
6745 struct drm_i915_private *dev_priv = dev->dev_private;
6746
6747 /*
6748 * IVB has CPU eDP backlight regs too, set things up to let the
6749 * PCH regs control the backlight
6750 */
6751 I915_WRITE(BLC_PWM_CPU_CTL2, PWM_ENABLE);
6752 I915_WRITE(BLC_PWM_CPU_CTL, 0);
6753 I915_WRITE(BLC_PWM_PCH_CTL1, PWM_ENABLE | (1<<30));
6754}
6755
Daniel Vetterf8175862012-04-10 15:50:11 +02006756void intel_modeset_init_hw(struct drm_device *dev)
6757{
6758 struct drm_i915_private *dev_priv = dev->dev_private;
6759
6760 intel_init_clock_gating(dev);
6761
6762 if (IS_IRONLAKE_M(dev)) {
6763 ironlake_enable_drps(dev);
Chris Wilson1833b132012-05-09 11:56:28 +01006764 ironlake_enable_rc6(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +02006765 intel_init_emon(dev);
6766 }
6767
Jesse Barnesb6834bd2012-04-11 09:23:33 -07006768 if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev)) {
Daniel Vetterf8175862012-04-10 15:50:11 +02006769 gen6_enable_rps(dev_priv);
6770 gen6_update_ring_freq(dev_priv);
6771 }
Jesse Barnesf82cfb62012-04-11 09:23:35 -07006772
6773 if (IS_IVYBRIDGE(dev))
6774 ivb_pch_pwm_override(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +02006775}
6776
Jesse Barnes79e53942008-11-07 14:24:08 -08006777void intel_modeset_init(struct drm_device *dev)
6778{
Jesse Barnes652c3932009-08-17 13:31:43 -07006779 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08006780 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006781
6782 drm_mode_config_init(dev);
6783
6784 dev->mode_config.min_width = 0;
6785 dev->mode_config.min_height = 0;
6786
Dave Airlie019d96c2011-09-29 16:20:42 +01006787 dev->mode_config.preferred_depth = 24;
6788 dev->mode_config.prefer_shadow = 1;
6789
Jesse Barnes79e53942008-11-07 14:24:08 -08006790 dev->mode_config.funcs = (void *)&intel_mode_funcs;
6791
Jesse Barnesb690e962010-07-19 13:53:12 -07006792 intel_init_quirks(dev);
6793
Eugeni Dodonov1fa61102012-04-18 15:29:26 -03006794 intel_init_pm(dev);
6795
Jesse Barnese70236a2009-09-21 10:42:27 -07006796 intel_init_display(dev);
6797
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006798 if (IS_GEN2(dev)) {
6799 dev->mode_config.max_width = 2048;
6800 dev->mode_config.max_height = 2048;
6801 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07006802 dev->mode_config.max_width = 4096;
6803 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08006804 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006805 dev->mode_config.max_width = 8192;
6806 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08006807 }
Chris Wilson35c30472010-12-22 14:07:12 +00006808 dev->mode_config.fb_base = dev->agp->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006809
Zhao Yakui28c97732009-10-09 11:39:41 +08006810 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10006811 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08006812
Dave Airliea3524f12010-06-06 18:59:41 +10006813 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006814 intel_crtc_init(dev, i);
Jesse Barnes00c2064b2012-01-13 15:48:39 -08006815 ret = intel_plane_init(dev, i);
6816 if (ret)
6817 DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08006818 }
6819
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006820 intel_pch_pll_init(dev);
6821
Jesse Barnes9cce37f2010-08-13 15:11:26 -07006822 /* Just disable it once at startup */
6823 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006824 intel_setup_outputs(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006825
Jesse Barnes652c3932009-08-17 13:31:43 -07006826 INIT_WORK(&dev_priv->idle_work, intel_idle_update);
6827 setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
6828 (unsigned long)dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01006829}
6830
6831void intel_modeset_gem_init(struct drm_device *dev)
6832{
Chris Wilson1833b132012-05-09 11:56:28 +01006833 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02006834
6835 intel_setup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006836}
6837
6838void intel_modeset_cleanup(struct drm_device *dev)
6839{
Jesse Barnes652c3932009-08-17 13:31:43 -07006840 struct drm_i915_private *dev_priv = dev->dev_private;
6841 struct drm_crtc *crtc;
6842 struct intel_crtc *intel_crtc;
6843
Keith Packardf87ea762010-10-03 19:36:26 -07006844 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07006845 mutex_lock(&dev->struct_mutex);
6846
Jesse Barnes723bfd72010-10-07 16:01:13 -07006847 intel_unregister_dsm_handler();
6848
6849
Jesse Barnes652c3932009-08-17 13:31:43 -07006850 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6851 /* Skip inactive CRTCs */
6852 if (!crtc->fb)
6853 continue;
6854
6855 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02006856 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07006857 }
6858
Chris Wilson973d04f2011-07-08 12:22:37 +01006859 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07006860
Jesse Barnesf97108d2010-01-29 11:27:07 -08006861 if (IS_IRONLAKE_M(dev))
6862 ironlake_disable_drps(dev);
Jesse Barnesb6834bd2012-04-11 09:23:33 -07006863 if ((IS_GEN6(dev) || IS_GEN7(dev)) && !IS_VALLEYVIEW(dev))
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08006864 gen6_disable_rps(dev);
Jesse Barnesf97108d2010-01-29 11:27:07 -08006865
Jesse Barnesd5bb0812011-01-05 12:01:26 -08006866 if (IS_IRONLAKE_M(dev))
6867 ironlake_disable_rc6(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00006868
Jesse Barnes57f350b2012-03-28 13:39:25 -07006869 if (IS_VALLEYVIEW(dev))
6870 vlv_init_dpio(dev);
6871
Kristian Høgsberg69341a52009-11-11 12:19:17 -05006872 mutex_unlock(&dev->struct_mutex);
6873
Daniel Vetter6c0d93502010-08-20 18:26:46 +02006874 /* Disable the irq before mode object teardown, for the irq might
6875 * enqueue unpin/hotplug work. */
6876 drm_irq_uninstall(dev);
6877 cancel_work_sync(&dev_priv->hotplug_work);
Daniel Vetter6fdd4d92011-09-08 14:00:22 +02006878 cancel_work_sync(&dev_priv->rps_work);
Daniel Vetter6c0d93502010-08-20 18:26:46 +02006879
Chris Wilson1630fe72011-07-08 12:22:42 +01006880 /* flush any delayed tasks or pending work */
6881 flush_scheduled_work();
6882
Daniel Vetter3dec0092010-08-20 21:40:52 +02006883 /* Shut off idle work before the crtcs get freed. */
6884 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6885 intel_crtc = to_intel_crtc(crtc);
6886 del_timer_sync(&intel_crtc->idle_timer);
6887 }
6888 del_timer_sync(&dev_priv->idle_timer);
6889 cancel_work_sync(&dev_priv->idle_work);
6890
Jesse Barnes79e53942008-11-07 14:24:08 -08006891 drm_mode_config_cleanup(dev);
6892}
6893
Dave Airlie28d52042009-09-21 14:33:58 +10006894/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08006895 * Return which encoder is currently attached for connector.
6896 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01006897struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08006898{
Chris Wilsondf0e9242010-09-09 16:20:55 +01006899 return &intel_attached_encoder(connector)->base;
6900}
Jesse Barnes79e53942008-11-07 14:24:08 -08006901
Chris Wilsondf0e9242010-09-09 16:20:55 +01006902void intel_connector_attach_encoder(struct intel_connector *connector,
6903 struct intel_encoder *encoder)
6904{
6905 connector->encoder = encoder;
6906 drm_mode_connector_attach_encoder(&connector->base,
6907 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08006908}
Dave Airlie28d52042009-09-21 14:33:58 +10006909
6910/*
6911 * set vga decode state - true == enable VGA decode
6912 */
6913int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
6914{
6915 struct drm_i915_private *dev_priv = dev->dev_private;
6916 u16 gmch_ctrl;
6917
6918 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
6919 if (state)
6920 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
6921 else
6922 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
6923 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
6924 return 0;
6925}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00006926
6927#ifdef CONFIG_DEBUG_FS
6928#include <linux/seq_file.h>
6929
6930struct intel_display_error_state {
6931 struct intel_cursor_error_state {
6932 u32 control;
6933 u32 position;
6934 u32 base;
6935 u32 size;
6936 } cursor[2];
6937
6938 struct intel_pipe_error_state {
6939 u32 conf;
6940 u32 source;
6941
6942 u32 htotal;
6943 u32 hblank;
6944 u32 hsync;
6945 u32 vtotal;
6946 u32 vblank;
6947 u32 vsync;
6948 } pipe[2];
6949
6950 struct intel_plane_error_state {
6951 u32 control;
6952 u32 stride;
6953 u32 size;
6954 u32 pos;
6955 u32 addr;
6956 u32 surface;
6957 u32 tile_offset;
6958 } plane[2];
6959};
6960
6961struct intel_display_error_state *
6962intel_display_capture_error_state(struct drm_device *dev)
6963{
Akshay Joshi0206e352011-08-16 15:34:10 -04006964 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00006965 struct intel_display_error_state *error;
6966 int i;
6967
6968 error = kmalloc(sizeof(*error), GFP_ATOMIC);
6969 if (error == NULL)
6970 return NULL;
6971
6972 for (i = 0; i < 2; i++) {
6973 error->cursor[i].control = I915_READ(CURCNTR(i));
6974 error->cursor[i].position = I915_READ(CURPOS(i));
6975 error->cursor[i].base = I915_READ(CURBASE(i));
6976
6977 error->plane[i].control = I915_READ(DSPCNTR(i));
6978 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
6979 error->plane[i].size = I915_READ(DSPSIZE(i));
Akshay Joshi0206e352011-08-16 15:34:10 -04006980 error->plane[i].pos = I915_READ(DSPPOS(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00006981 error->plane[i].addr = I915_READ(DSPADDR(i));
6982 if (INTEL_INFO(dev)->gen >= 4) {
6983 error->plane[i].surface = I915_READ(DSPSURF(i));
6984 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
6985 }
6986
6987 error->pipe[i].conf = I915_READ(PIPECONF(i));
6988 error->pipe[i].source = I915_READ(PIPESRC(i));
6989 error->pipe[i].htotal = I915_READ(HTOTAL(i));
6990 error->pipe[i].hblank = I915_READ(HBLANK(i));
6991 error->pipe[i].hsync = I915_READ(HSYNC(i));
6992 error->pipe[i].vtotal = I915_READ(VTOTAL(i));
6993 error->pipe[i].vblank = I915_READ(VBLANK(i));
6994 error->pipe[i].vsync = I915_READ(VSYNC(i));
6995 }
6996
6997 return error;
6998}
6999
7000void
7001intel_display_print_error_state(struct seq_file *m,
7002 struct drm_device *dev,
7003 struct intel_display_error_state *error)
7004{
7005 int i;
7006
7007 for (i = 0; i < 2; i++) {
7008 seq_printf(m, "Pipe [%d]:\n", i);
7009 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
7010 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
7011 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
7012 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
7013 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
7014 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
7015 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
7016 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
7017
7018 seq_printf(m, "Plane [%d]:\n", i);
7019 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
7020 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
7021 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
7022 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
7023 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
7024 if (INTEL_INFO(dev)->gen >= 4) {
7025 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
7026 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
7027 }
7028
7029 seq_printf(m, "Cursor [%d]:\n", i);
7030 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
7031 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
7032 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
7033 }
7034}
7035#endif