blob: 2e00c8ae361cddf896e0d31c17433242b8393807 [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
Jesse Barnes23b2f8b2011-06-28 13:04:16 -070027#include <linux/cpufreq.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);
Shaohua Li7662c8b2009-06-26 11:23:55 +080047static void intel_update_watermarks(struct drm_device *dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +020048static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010049static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080050
51typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040052 /* given values */
53 int n;
54 int m1, m2;
55 int p1, p2;
56 /* derived values */
57 int dot;
58 int vco;
59 int m;
60 int p;
Jesse Barnes79e53942008-11-07 14:24:08 -080061} intel_clock_t;
62
63typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040064 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080065} intel_range_t;
66
67typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040068 int dot_limit;
69 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080070} intel_p2_t;
71
72#define INTEL_P2_NUM 2
Ma Lingd4906092009-03-18 20:13:27 +080073typedef struct intel_limit intel_limit_t;
74struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040075 intel_range_t dot, vco, n, m, m1, m2, p, p1;
76 intel_p2_t p2;
77 bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
78 int, int, intel_clock_t *);
Ma Lingd4906092009-03-18 20:13:27 +080079};
Jesse Barnes79e53942008-11-07 14:24:08 -080080
Jesse Barnes2377b742010-07-07 14:06:43 -070081/* FDI */
82#define IRONLAKE_FDI_FREQ 2700000 /* in kHz for mode->clock */
83
Ma Lingd4906092009-03-18 20:13:27 +080084static bool
85intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
86 int target, int refclk, intel_clock_t *best_clock);
87static bool
88intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
89 int target, int refclk, intel_clock_t *best_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080090
Keith Packarda4fc5ed2009-04-07 16:16:42 -070091static bool
92intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
93 int target, int refclk, intel_clock_t *best_clock);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080094static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -050095intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
96 int target, int refclk, intel_clock_t *best_clock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -070097
Chris Wilson021357a2010-09-07 20:54:59 +010098static inline u32 /* units of 100MHz */
99intel_fdi_link_freq(struct drm_device *dev)
100{
Chris Wilson8b99e682010-10-13 09:59:17 +0100101 if (IS_GEN5(dev)) {
102 struct drm_i915_private *dev_priv = dev->dev_private;
103 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
104 } else
105 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100106}
107
Keith Packarde4b36692009-06-05 19:22:17 -0700108static const intel_limit_t intel_limits_i8xx_dvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400109 .dot = { .min = 25000, .max = 350000 },
110 .vco = { .min = 930000, .max = 1400000 },
111 .n = { .min = 3, .max = 16 },
112 .m = { .min = 96, .max = 140 },
113 .m1 = { .min = 18, .max = 26 },
114 .m2 = { .min = 6, .max = 16 },
115 .p = { .min = 4, .max = 128 },
116 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700117 .p2 = { .dot_limit = 165000,
118 .p2_slow = 4, .p2_fast = 2 },
Ma Lingd4906092009-03-18 20:13:27 +0800119 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700120};
121
122static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400123 .dot = { .min = 25000, .max = 350000 },
124 .vco = { .min = 930000, .max = 1400000 },
125 .n = { .min = 3, .max = 16 },
126 .m = { .min = 96, .max = 140 },
127 .m1 = { .min = 18, .max = 26 },
128 .m2 = { .min = 6, .max = 16 },
129 .p = { .min = 4, .max = 128 },
130 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700131 .p2 = { .dot_limit = 165000,
132 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800133 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700134};
Eric Anholt273e27c2011-03-30 13:01:10 -0700135
Keith Packarde4b36692009-06-05 19:22:17 -0700136static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400137 .dot = { .min = 20000, .max = 400000 },
138 .vco = { .min = 1400000, .max = 2800000 },
139 .n = { .min = 1, .max = 6 },
140 .m = { .min = 70, .max = 120 },
141 .m1 = { .min = 10, .max = 22 },
142 .m2 = { .min = 5, .max = 9 },
143 .p = { .min = 5, .max = 80 },
144 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700145 .p2 = { .dot_limit = 200000,
146 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800147 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700148};
149
150static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400151 .dot = { .min = 20000, .max = 400000 },
152 .vco = { .min = 1400000, .max = 2800000 },
153 .n = { .min = 1, .max = 6 },
154 .m = { .min = 70, .max = 120 },
155 .m1 = { .min = 10, .max = 22 },
156 .m2 = { .min = 5, .max = 9 },
157 .p = { .min = 7, .max = 98 },
158 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700159 .p2 = { .dot_limit = 112000,
160 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800161 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700162};
163
Eric Anholt273e27c2011-03-30 13:01:10 -0700164
Keith Packarde4b36692009-06-05 19:22:17 -0700165static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700166 .dot = { .min = 25000, .max = 270000 },
167 .vco = { .min = 1750000, .max = 3500000},
168 .n = { .min = 1, .max = 4 },
169 .m = { .min = 104, .max = 138 },
170 .m1 = { .min = 17, .max = 23 },
171 .m2 = { .min = 5, .max = 11 },
172 .p = { .min = 10, .max = 30 },
173 .p1 = { .min = 1, .max = 3},
174 .p2 = { .dot_limit = 270000,
175 .p2_slow = 10,
176 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800177 },
Ma Lingd4906092009-03-18 20:13:27 +0800178 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700179};
180
181static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700182 .dot = { .min = 22000, .max = 400000 },
183 .vco = { .min = 1750000, .max = 3500000},
184 .n = { .min = 1, .max = 4 },
185 .m = { .min = 104, .max = 138 },
186 .m1 = { .min = 16, .max = 23 },
187 .m2 = { .min = 5, .max = 11 },
188 .p = { .min = 5, .max = 80 },
189 .p1 = { .min = 1, .max = 8},
190 .p2 = { .dot_limit = 165000,
191 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800192 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700193};
194
195static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700196 .dot = { .min = 20000, .max = 115000 },
197 .vco = { .min = 1750000, .max = 3500000 },
198 .n = { .min = 1, .max = 3 },
199 .m = { .min = 104, .max = 138 },
200 .m1 = { .min = 17, .max = 23 },
201 .m2 = { .min = 5, .max = 11 },
202 .p = { .min = 28, .max = 112 },
203 .p1 = { .min = 2, .max = 8 },
204 .p2 = { .dot_limit = 0,
205 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800206 },
Ma Lingd4906092009-03-18 20:13:27 +0800207 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700208};
209
210static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700211 .dot = { .min = 80000, .max = 224000 },
212 .vco = { .min = 1750000, .max = 3500000 },
213 .n = { .min = 1, .max = 3 },
214 .m = { .min = 104, .max = 138 },
215 .m1 = { .min = 17, .max = 23 },
216 .m2 = { .min = 5, .max = 11 },
217 .p = { .min = 14, .max = 42 },
218 .p1 = { .min = 2, .max = 6 },
219 .p2 = { .dot_limit = 0,
220 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800221 },
Ma Lingd4906092009-03-18 20:13:27 +0800222 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700223};
224
225static const intel_limit_t intel_limits_g4x_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400226 .dot = { .min = 161670, .max = 227000 },
227 .vco = { .min = 1750000, .max = 3500000},
228 .n = { .min = 1, .max = 2 },
229 .m = { .min = 97, .max = 108 },
230 .m1 = { .min = 0x10, .max = 0x12 },
231 .m2 = { .min = 0x05, .max = 0x06 },
232 .p = { .min = 10, .max = 20 },
233 .p1 = { .min = 1, .max = 2},
234 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700235 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400236 .find_pll = intel_find_pll_g4x_dp,
Keith Packarde4b36692009-06-05 19:22:17 -0700237};
238
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500239static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400240 .dot = { .min = 20000, .max = 400000},
241 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700242 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400243 .n = { .min = 3, .max = 6 },
244 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700245 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400246 .m1 = { .min = 0, .max = 0 },
247 .m2 = { .min = 0, .max = 254 },
248 .p = { .min = 5, .max = 80 },
249 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700250 .p2 = { .dot_limit = 200000,
251 .p2_slow = 10, .p2_fast = 5 },
Shaohua Li61157072009-04-03 15:24:43 +0800252 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700253};
254
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500255static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400256 .dot = { .min = 20000, .max = 400000 },
257 .vco = { .min = 1700000, .max = 3500000 },
258 .n = { .min = 3, .max = 6 },
259 .m = { .min = 2, .max = 256 },
260 .m1 = { .min = 0, .max = 0 },
261 .m2 = { .min = 0, .max = 254 },
262 .p = { .min = 7, .max = 112 },
263 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700264 .p2 = { .dot_limit = 112000,
265 .p2_slow = 14, .p2_fast = 14 },
Shaohua Li61157072009-04-03 15:24:43 +0800266 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700267};
268
Eric Anholt273e27c2011-03-30 13:01:10 -0700269/* Ironlake / Sandybridge
270 *
271 * We calculate clock using (register_value + 2) for N/M1/M2, so here
272 * the range value for them is (actual_value - 2).
273 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800274static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700275 .dot = { .min = 25000, .max = 350000 },
276 .vco = { .min = 1760000, .max = 3510000 },
277 .n = { .min = 1, .max = 5 },
278 .m = { .min = 79, .max = 127 },
279 .m1 = { .min = 12, .max = 22 },
280 .m2 = { .min = 5, .max = 9 },
281 .p = { .min = 5, .max = 80 },
282 .p1 = { .min = 1, .max = 8 },
283 .p2 = { .dot_limit = 225000,
284 .p2_slow = 10, .p2_fast = 5 },
Zhao Yakui45476682009-12-31 16:06:04 +0800285 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700286};
287
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800288static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700289 .dot = { .min = 25000, .max = 350000 },
290 .vco = { .min = 1760000, .max = 3510000 },
291 .n = { .min = 1, .max = 3 },
292 .m = { .min = 79, .max = 118 },
293 .m1 = { .min = 12, .max = 22 },
294 .m2 = { .min = 5, .max = 9 },
295 .p = { .min = 28, .max = 112 },
296 .p1 = { .min = 2, .max = 8 },
297 .p2 = { .dot_limit = 225000,
298 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800299 .find_pll = intel_g4x_find_best_PLL,
300};
301
302static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700303 .dot = { .min = 25000, .max = 350000 },
304 .vco = { .min = 1760000, .max = 3510000 },
305 .n = { .min = 1, .max = 3 },
306 .m = { .min = 79, .max = 127 },
307 .m1 = { .min = 12, .max = 22 },
308 .m2 = { .min = 5, .max = 9 },
309 .p = { .min = 14, .max = 56 },
310 .p1 = { .min = 2, .max = 8 },
311 .p2 = { .dot_limit = 225000,
312 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800313 .find_pll = intel_g4x_find_best_PLL,
314};
315
Eric Anholt273e27c2011-03-30 13:01:10 -0700316/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800317static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700318 .dot = { .min = 25000, .max = 350000 },
319 .vco = { .min = 1760000, .max = 3510000 },
320 .n = { .min = 1, .max = 2 },
321 .m = { .min = 79, .max = 126 },
322 .m1 = { .min = 12, .max = 22 },
323 .m2 = { .min = 5, .max = 9 },
324 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400325 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700326 .p2 = { .dot_limit = 225000,
327 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800328 .find_pll = intel_g4x_find_best_PLL,
329};
330
331static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700332 .dot = { .min = 25000, .max = 350000 },
333 .vco = { .min = 1760000, .max = 3510000 },
334 .n = { .min = 1, .max = 3 },
335 .m = { .min = 79, .max = 126 },
336 .m1 = { .min = 12, .max = 22 },
337 .m2 = { .min = 5, .max = 9 },
338 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400339 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700340 .p2 = { .dot_limit = 225000,
341 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800342 .find_pll = intel_g4x_find_best_PLL,
343};
344
345static const intel_limit_t intel_limits_ironlake_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400346 .dot = { .min = 25000, .max = 350000 },
347 .vco = { .min = 1760000, .max = 3510000},
348 .n = { .min = 1, .max = 2 },
349 .m = { .min = 81, .max = 90 },
350 .m1 = { .min = 12, .max = 22 },
351 .m2 = { .min = 5, .max = 9 },
352 .p = { .min = 10, .max = 20 },
353 .p1 = { .min = 1, .max = 2},
354 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700355 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400356 .find_pll = intel_find_pll_ironlake_dp,
Jesse Barnes79e53942008-11-07 14:24:08 -0800357};
358
Chris Wilson1b894b52010-12-14 20:04:54 +0000359static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
360 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800361{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800362 struct drm_device *dev = crtc->dev;
363 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800364 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800365
366 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800367 if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
368 LVDS_CLKB_POWER_UP) {
369 /* LVDS dual channel */
Chris Wilson1b894b52010-12-14 20:04:54 +0000370 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800371 limit = &intel_limits_ironlake_dual_lvds_100m;
372 else
373 limit = &intel_limits_ironlake_dual_lvds;
374 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000375 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800376 limit = &intel_limits_ironlake_single_lvds_100m;
377 else
378 limit = &intel_limits_ironlake_single_lvds;
379 }
380 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
Zhao Yakui45476682009-12-31 16:06:04 +0800381 HAS_eDP)
382 limit = &intel_limits_ironlake_display_port;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800383 else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800384 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800385
386 return limit;
387}
388
Ma Ling044c7c42009-03-18 20:13:23 +0800389static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
390{
391 struct drm_device *dev = crtc->dev;
392 struct drm_i915_private *dev_priv = dev->dev_private;
393 const intel_limit_t *limit;
394
395 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
396 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
397 LVDS_CLKB_POWER_UP)
398 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700399 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800400 else
401 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700402 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800403 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
404 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700405 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800406 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700407 limit = &intel_limits_g4x_sdvo;
Akshay Joshi0206e352011-08-16 15:34:10 -0400408 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700409 limit = &intel_limits_g4x_display_port;
Ma Ling044c7c42009-03-18 20:13:23 +0800410 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700411 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800412
413 return limit;
414}
415
Chris Wilson1b894b52010-12-14 20:04:54 +0000416static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800417{
418 struct drm_device *dev = crtc->dev;
419 const intel_limit_t *limit;
420
Eric Anholtbad720f2009-10-22 16:11:14 -0700421 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000422 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800423 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800424 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500425 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800426 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500427 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800428 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500429 limit = &intel_limits_pineview_sdvo;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100430 } else if (!IS_GEN2(dev)) {
431 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
432 limit = &intel_limits_i9xx_lvds;
433 else
434 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800435 } else {
436 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700437 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800438 else
Keith Packarde4b36692009-06-05 19:22:17 -0700439 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800440 }
441 return limit;
442}
443
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500444/* m1 is reserved as 0 in Pineview, n is a ring counter */
445static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800446{
Shaohua Li21778322009-02-23 15:19:16 +0800447 clock->m = clock->m2 + 2;
448 clock->p = clock->p1 * clock->p2;
449 clock->vco = refclk * clock->m / clock->n;
450 clock->dot = clock->vco / clock->p;
451}
452
453static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
454{
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500455 if (IS_PINEVIEW(dev)) {
456 pineview_clock(refclk, clock);
Shaohua Li21778322009-02-23 15:19:16 +0800457 return;
458 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800459 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
460 clock->p = clock->p1 * clock->p2;
461 clock->vco = refclk * clock->m / (clock->n + 2);
462 clock->dot = clock->vco / clock->p;
463}
464
Jesse Barnes79e53942008-11-07 14:24:08 -0800465/**
466 * Returns whether any output on the specified pipe is of the specified type
467 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100468bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800469{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100470 struct drm_device *dev = crtc->dev;
471 struct drm_mode_config *mode_config = &dev->mode_config;
472 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800473
Chris Wilson4ef69c72010-09-09 15:14:28 +0100474 list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
475 if (encoder->base.crtc == crtc && encoder->type == type)
476 return true;
477
478 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800479}
480
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800481#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800482/**
483 * Returns whether the given set of divisors are valid for a given refclk with
484 * the given connectors.
485 */
486
Chris Wilson1b894b52010-12-14 20:04:54 +0000487static bool intel_PLL_is_valid(struct drm_device *dev,
488 const intel_limit_t *limit,
489 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800490{
Jesse Barnes79e53942008-11-07 14:24:08 -0800491 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400492 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800493 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400494 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800495 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400496 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800497 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400498 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500499 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400500 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800501 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400502 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800503 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400504 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800505 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400506 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800507 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
508 * connector, etc., rather than just a single range.
509 */
510 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400511 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800512
513 return true;
514}
515
Ma Lingd4906092009-03-18 20:13:27 +0800516static bool
517intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
518 int target, int refclk, intel_clock_t *best_clock)
519
Jesse Barnes79e53942008-11-07 14:24:08 -0800520{
521 struct drm_device *dev = crtc->dev;
522 struct drm_i915_private *dev_priv = dev->dev_private;
523 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800524 int err = target;
525
Bruno Prémontbc5e5712009-08-08 13:01:17 +0200526 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Florian Mickler832cc282009-07-13 18:40:32 +0800527 (I915_READ(LVDS)) != 0) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800528 /*
529 * For LVDS, if the panel is on, just rely on its current
530 * settings for dual-channel. We haven't figured out how to
531 * reliably set up different single/dual channel state, if we
532 * even can.
533 */
534 if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
535 LVDS_CLKB_POWER_UP)
536 clock.p2 = limit->p2.p2_fast;
537 else
538 clock.p2 = limit->p2.p2_slow;
539 } else {
540 if (target < limit->p2.dot_limit)
541 clock.p2 = limit->p2.p2_slow;
542 else
543 clock.p2 = limit->p2.p2_fast;
544 }
545
Akshay Joshi0206e352011-08-16 15:34:10 -0400546 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800547
Zhao Yakui42158662009-11-20 11:24:18 +0800548 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
549 clock.m1++) {
550 for (clock.m2 = limit->m2.min;
551 clock.m2 <= limit->m2.max; clock.m2++) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500552 /* m1 is always 0 in Pineview */
553 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
Zhao Yakui42158662009-11-20 11:24:18 +0800554 break;
555 for (clock.n = limit->n.min;
556 clock.n <= limit->n.max; clock.n++) {
557 for (clock.p1 = limit->p1.min;
558 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800559 int this_err;
560
Shaohua Li21778322009-02-23 15:19:16 +0800561 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000562 if (!intel_PLL_is_valid(dev, limit,
563 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800564 continue;
565
566 this_err = abs(clock.dot - target);
567 if (this_err < err) {
568 *best_clock = clock;
569 err = this_err;
570 }
571 }
572 }
573 }
574 }
575
576 return (err != target);
577}
578
Ma Lingd4906092009-03-18 20:13:27 +0800579static bool
580intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
581 int target, int refclk, intel_clock_t *best_clock)
582{
583 struct drm_device *dev = crtc->dev;
584 struct drm_i915_private *dev_priv = dev->dev_private;
585 intel_clock_t clock;
586 int max_n;
587 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400588 /* approximately equals target * 0.00585 */
589 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800590 found = false;
591
592 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhao Yakui45476682009-12-31 16:06:04 +0800593 int lvds_reg;
594
Eric Anholtc619eed2010-01-28 16:45:52 -0800595 if (HAS_PCH_SPLIT(dev))
Zhao Yakui45476682009-12-31 16:06:04 +0800596 lvds_reg = PCH_LVDS;
597 else
598 lvds_reg = LVDS;
599 if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
Ma Lingd4906092009-03-18 20:13:27 +0800600 LVDS_CLKB_POWER_UP)
601 clock.p2 = limit->p2.p2_fast;
602 else
603 clock.p2 = limit->p2.p2_slow;
604 } else {
605 if (target < limit->p2.dot_limit)
606 clock.p2 = limit->p2.p2_slow;
607 else
608 clock.p2 = limit->p2.p2_fast;
609 }
610
611 memset(best_clock, 0, sizeof(*best_clock));
612 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200613 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800614 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200615 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800616 for (clock.m1 = limit->m1.max;
617 clock.m1 >= limit->m1.min; clock.m1--) {
618 for (clock.m2 = limit->m2.max;
619 clock.m2 >= limit->m2.min; clock.m2--) {
620 for (clock.p1 = limit->p1.max;
621 clock.p1 >= limit->p1.min; clock.p1--) {
622 int this_err;
623
Shaohua Li21778322009-02-23 15:19:16 +0800624 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000625 if (!intel_PLL_is_valid(dev, limit,
626 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800627 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000628
629 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800630 if (this_err < err_most) {
631 *best_clock = clock;
632 err_most = this_err;
633 max_n = clock.n;
634 found = true;
635 }
636 }
637 }
638 }
639 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800640 return found;
641}
Ma Lingd4906092009-03-18 20:13:27 +0800642
Zhenyu Wang2c072452009-06-05 15:38:42 +0800643static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500644intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
645 int target, int refclk, intel_clock_t *best_clock)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800646{
647 struct drm_device *dev = crtc->dev;
648 intel_clock_t clock;
Zhao Yakui45476682009-12-31 16:06:04 +0800649
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800650 if (target < 200000) {
651 clock.n = 1;
652 clock.p1 = 2;
653 clock.p2 = 10;
654 clock.m1 = 12;
655 clock.m2 = 9;
656 } else {
657 clock.n = 2;
658 clock.p1 = 1;
659 clock.p2 = 10;
660 clock.m1 = 14;
661 clock.m2 = 8;
662 }
663 intel_clock(dev, refclk, &clock);
664 memcpy(best_clock, &clock, sizeof(intel_clock_t));
665 return true;
666}
667
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700668/* DisplayPort has only two frequencies, 162MHz and 270MHz */
669static bool
670intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
671 int target, int refclk, intel_clock_t *best_clock)
672{
Chris Wilson5eddb702010-09-11 13:48:45 +0100673 intel_clock_t clock;
674 if (target < 200000) {
675 clock.p1 = 2;
676 clock.p2 = 10;
677 clock.n = 2;
678 clock.m1 = 23;
679 clock.m2 = 8;
680 } else {
681 clock.p1 = 1;
682 clock.p2 = 10;
683 clock.n = 1;
684 clock.m1 = 14;
685 clock.m2 = 2;
686 }
687 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
688 clock.p = (clock.p1 * clock.p2);
689 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
690 clock.vco = 0;
691 memcpy(best_clock, &clock, sizeof(intel_clock_t));
692 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700693}
694
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700695/**
696 * intel_wait_for_vblank - wait for vblank on a given pipe
697 * @dev: drm device
698 * @pipe: pipe to wait for
699 *
700 * Wait for vblank to occur on a given pipe. Needed for various bits of
701 * mode setting code.
702 */
703void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800704{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700705 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800706 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700707
Chris Wilson300387c2010-09-05 20:25:43 +0100708 /* Clear existing vblank status. Note this will clear any other
709 * sticky status fields as well.
710 *
711 * This races with i915_driver_irq_handler() with the result
712 * that either function could miss a vblank event. Here it is not
713 * fatal, as we will either wait upon the next vblank interrupt or
714 * timeout. Generally speaking intel_wait_for_vblank() is only
715 * called during modeset at which time the GPU should be idle and
716 * should *not* be performing page flips and thus not waiting on
717 * vblanks...
718 * Currently, the result of us stealing a vblank from the irq
719 * handler is that a single frame will be skipped during swapbuffers.
720 */
721 I915_WRITE(pipestat_reg,
722 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
723
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700724 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100725 if (wait_for(I915_READ(pipestat_reg) &
726 PIPE_VBLANK_INTERRUPT_STATUS,
727 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700728 DRM_DEBUG_KMS("vblank wait timed out\n");
729}
730
Keith Packardab7ad7f2010-10-03 00:33:06 -0700731/*
732 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700733 * @dev: drm device
734 * @pipe: pipe to wait for
735 *
736 * After disabling a pipe, we can't wait for vblank in the usual way,
737 * spinning on the vblank interrupt status bit, since we won't actually
738 * see an interrupt when the pipe is disabled.
739 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700740 * On Gen4 and above:
741 * wait for the pipe register state bit to turn off
742 *
743 * Otherwise:
744 * wait for the display line value to settle (it usually
745 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100746 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700747 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100748void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700749{
750 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700751
Keith Packardab7ad7f2010-10-03 00:33:06 -0700752 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson58e10eb2010-10-03 10:56:11 +0100753 int reg = PIPECONF(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700754
Keith Packardab7ad7f2010-10-03 00:33:06 -0700755 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100756 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
757 100))
Keith Packardab7ad7f2010-10-03 00:33:06 -0700758 DRM_DEBUG_KMS("pipe_off wait timed out\n");
759 } else {
760 u32 last_line;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100761 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700762 unsigned long timeout = jiffies + msecs_to_jiffies(100);
763
764 /* Wait for the display line to settle */
765 do {
Chris Wilson58e10eb2010-10-03 10:56:11 +0100766 last_line = I915_READ(reg) & DSL_LINEMASK;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700767 mdelay(5);
Chris Wilson58e10eb2010-10-03 10:56:11 +0100768 } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700769 time_after(timeout, jiffies));
770 if (time_after(jiffies, timeout))
771 DRM_DEBUG_KMS("pipe_off wait timed out\n");
772 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800773}
774
Jesse Barnesb24e7172011-01-04 15:09:30 -0800775static const char *state_string(bool enabled)
776{
777 return enabled ? "on" : "off";
778}
779
780/* Only for pre-ILK configs */
781static void assert_pll(struct drm_i915_private *dev_priv,
782 enum pipe pipe, bool state)
783{
784 int reg;
785 u32 val;
786 bool cur_state;
787
788 reg = DPLL(pipe);
789 val = I915_READ(reg);
790 cur_state = !!(val & DPLL_VCO_ENABLE);
791 WARN(cur_state != state,
792 "PLL state assertion failure (expected %s, current %s)\n",
793 state_string(state), state_string(cur_state));
794}
795#define assert_pll_enabled(d, p) assert_pll(d, p, true)
796#define assert_pll_disabled(d, p) assert_pll(d, p, false)
797
Jesse Barnes040484a2011-01-03 12:14:26 -0800798/* For ILK+ */
799static void assert_pch_pll(struct drm_i915_private *dev_priv,
800 enum pipe pipe, bool state)
801{
802 int reg;
803 u32 val;
804 bool cur_state;
805
Jesse Barnesd3ccbe82011-10-12 09:27:42 -0700806 if (HAS_PCH_CPT(dev_priv->dev)) {
807 u32 pch_dpll;
808
809 pch_dpll = I915_READ(PCH_DPLL_SEL);
810
811 /* Make sure the selected PLL is enabled to the transcoder */
812 WARN(!((pch_dpll >> (4 * pipe)) & 8),
813 "transcoder %d PLL not enabled\n", pipe);
814
815 /* Convert the transcoder pipe number to a pll pipe number */
816 pipe = (pch_dpll >> (4 * pipe)) & 1;
817 }
818
Jesse Barnes040484a2011-01-03 12:14:26 -0800819 reg = PCH_DPLL(pipe);
820 val = I915_READ(reg);
821 cur_state = !!(val & DPLL_VCO_ENABLE);
822 WARN(cur_state != state,
823 "PCH PLL state assertion failure (expected %s, current %s)\n",
824 state_string(state), state_string(cur_state));
825}
826#define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
827#define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
828
829static void assert_fdi_tx(struct drm_i915_private *dev_priv,
830 enum pipe pipe, bool state)
831{
832 int reg;
833 u32 val;
834 bool cur_state;
835
836 reg = FDI_TX_CTL(pipe);
837 val = I915_READ(reg);
838 cur_state = !!(val & FDI_TX_ENABLE);
839 WARN(cur_state != state,
840 "FDI TX state assertion failure (expected %s, current %s)\n",
841 state_string(state), state_string(cur_state));
842}
843#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
844#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
845
846static void assert_fdi_rx(struct drm_i915_private *dev_priv,
847 enum pipe pipe, bool state)
848{
849 int reg;
850 u32 val;
851 bool cur_state;
852
853 reg = FDI_RX_CTL(pipe);
854 val = I915_READ(reg);
855 cur_state = !!(val & FDI_RX_ENABLE);
856 WARN(cur_state != state,
857 "FDI RX state assertion failure (expected %s, current %s)\n",
858 state_string(state), state_string(cur_state));
859}
860#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
861#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
862
863static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
864 enum pipe pipe)
865{
866 int reg;
867 u32 val;
868
869 /* ILK FDI PLL is always enabled */
870 if (dev_priv->info->gen == 5)
871 return;
872
873 reg = FDI_TX_CTL(pipe);
874 val = I915_READ(reg);
875 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
876}
877
878static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
879 enum pipe pipe)
880{
881 int reg;
882 u32 val;
883
884 reg = FDI_RX_CTL(pipe);
885 val = I915_READ(reg);
886 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
887}
888
Jesse Barnesea0760c2011-01-04 15:09:32 -0800889static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
890 enum pipe pipe)
891{
892 int pp_reg, lvds_reg;
893 u32 val;
894 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +0200895 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -0800896
897 if (HAS_PCH_SPLIT(dev_priv->dev)) {
898 pp_reg = PCH_PP_CONTROL;
899 lvds_reg = PCH_LVDS;
900 } else {
901 pp_reg = PP_CONTROL;
902 lvds_reg = LVDS;
903 }
904
905 val = I915_READ(pp_reg);
906 if (!(val & PANEL_POWER_ON) ||
907 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
908 locked = false;
909
910 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
911 panel_pipe = PIPE_B;
912
913 WARN(panel_pipe == pipe && locked,
914 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800915 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -0800916}
917
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800918void assert_pipe(struct drm_i915_private *dev_priv,
919 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800920{
921 int reg;
922 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -0800923 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -0800924
925 reg = PIPECONF(pipe);
926 val = I915_READ(reg);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -0800927 cur_state = !!(val & PIPECONF_ENABLE);
928 WARN(cur_state != state,
929 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800930 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -0800931}
932
933static void assert_plane_enabled(struct drm_i915_private *dev_priv,
934 enum plane plane)
935{
936 int reg;
937 u32 val;
938
939 reg = DSPCNTR(plane);
940 val = I915_READ(reg);
941 WARN(!(val & DISPLAY_PLANE_ENABLE),
942 "plane %c assertion failure, should be active but is disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800943 plane_name(plane));
Jesse Barnesb24e7172011-01-04 15:09:30 -0800944}
945
946static void assert_planes_disabled(struct drm_i915_private *dev_priv,
947 enum pipe pipe)
948{
949 int reg, i;
950 u32 val;
951 int cur_pipe;
952
Jesse Barnes19ec1352011-02-02 12:28:02 -0800953 /* Planes are fixed to pipes on ILK+ */
954 if (HAS_PCH_SPLIT(dev_priv->dev))
955 return;
956
Jesse Barnesb24e7172011-01-04 15:09:30 -0800957 /* Need to check both planes against the pipe */
958 for (i = 0; i < 2; i++) {
959 reg = DSPCNTR(i);
960 val = I915_READ(reg);
961 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
962 DISPPLANE_SEL_PIPE_SHIFT;
963 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800964 "plane %c assertion failure, should be off on pipe %c but is still active\n",
965 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -0800966 }
967}
968
Jesse Barnes92f25842011-01-04 15:09:34 -0800969static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
970{
971 u32 val;
972 bool enabled;
973
974 val = I915_READ(PCH_DREF_CONTROL);
975 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
976 DREF_SUPERSPREAD_SOURCE_MASK));
977 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
978}
979
980static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
981 enum pipe pipe)
982{
983 int reg;
984 u32 val;
985 bool enabled;
986
987 reg = TRANSCONF(pipe);
988 val = I915_READ(reg);
989 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800990 WARN(enabled,
991 "transcoder assertion failed, should be off on pipe %c but is still active\n",
992 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -0800993}
994
Keith Packard4e634382011-08-06 10:39:45 -0700995static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
996 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -0700997{
998 if ((val & DP_PORT_EN) == 0)
999 return false;
1000
1001 if (HAS_PCH_CPT(dev_priv->dev)) {
1002 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1003 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1004 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1005 return false;
1006 } else {
1007 if ((val & DP_PIPE_MASK) != (pipe << 30))
1008 return false;
1009 }
1010 return true;
1011}
1012
Keith Packard1519b992011-08-06 10:35:34 -07001013static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1014 enum pipe pipe, u32 val)
1015{
1016 if ((val & PORT_ENABLE) == 0)
1017 return false;
1018
1019 if (HAS_PCH_CPT(dev_priv->dev)) {
1020 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1021 return false;
1022 } else {
1023 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1024 return false;
1025 }
1026 return true;
1027}
1028
1029static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1030 enum pipe pipe, u32 val)
1031{
1032 if ((val & LVDS_PORT_EN) == 0)
1033 return false;
1034
1035 if (HAS_PCH_CPT(dev_priv->dev)) {
1036 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1037 return false;
1038 } else {
1039 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1040 return false;
1041 }
1042 return true;
1043}
1044
1045static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1046 enum pipe pipe, u32 val)
1047{
1048 if ((val & ADPA_DAC_ENABLE) == 0)
1049 return false;
1050 if (HAS_PCH_CPT(dev_priv->dev)) {
1051 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1052 return false;
1053 } else {
1054 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1055 return false;
1056 }
1057 return true;
1058}
1059
Jesse Barnes291906f2011-02-02 12:28:03 -08001060static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001061 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001062{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001063 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001064 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001065 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001066 reg, pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001067}
1068
1069static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1070 enum pipe pipe, int reg)
1071{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001072 u32 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001073 WARN(hdmi_pipe_enabled(dev_priv, val, pipe),
Jesse Barnes291906f2011-02-02 12:28:03 -08001074 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001075 reg, pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001076}
1077
1078static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1079 enum pipe pipe)
1080{
1081 int reg;
1082 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001083
Keith Packardf0575e92011-07-25 22:12:43 -07001084 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1085 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1086 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001087
1088 reg = PCH_ADPA;
1089 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001090 WARN(adpa_pipe_enabled(dev_priv, val, pipe),
Jesse Barnes291906f2011-02-02 12:28:03 -08001091 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001092 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001093
1094 reg = PCH_LVDS;
1095 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001096 WARN(lvds_pipe_enabled(dev_priv, val, pipe),
Jesse Barnes291906f2011-02-02 12:28:03 -08001097 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001098 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001099
1100 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1101 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1102 assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1103}
1104
Jesse Barnesb24e7172011-01-04 15:09:30 -08001105/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001106 * intel_enable_pll - enable a PLL
1107 * @dev_priv: i915 private structure
1108 * @pipe: pipe PLL to enable
1109 *
1110 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1111 * make sure the PLL reg is writable first though, since the panel write
1112 * protect mechanism may be enabled.
1113 *
1114 * Note! This is for pre-ILK only.
1115 */
1116static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1117{
1118 int reg;
1119 u32 val;
1120
1121 /* No really, not for ILK+ */
1122 BUG_ON(dev_priv->info->gen >= 5);
1123
1124 /* PLL is protected by panel, make sure we can write it */
1125 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1126 assert_panel_unlocked(dev_priv, pipe);
1127
1128 reg = DPLL(pipe);
1129 val = I915_READ(reg);
1130 val |= DPLL_VCO_ENABLE;
1131
1132 /* We do this three times for luck */
1133 I915_WRITE(reg, val);
1134 POSTING_READ(reg);
1135 udelay(150); /* wait for warmup */
1136 I915_WRITE(reg, val);
1137 POSTING_READ(reg);
1138 udelay(150); /* wait for warmup */
1139 I915_WRITE(reg, val);
1140 POSTING_READ(reg);
1141 udelay(150); /* wait for warmup */
1142}
1143
1144/**
1145 * intel_disable_pll - disable a PLL
1146 * @dev_priv: i915 private structure
1147 * @pipe: pipe PLL to disable
1148 *
1149 * Disable the PLL for @pipe, making sure the pipe is off first.
1150 *
1151 * Note! This is for pre-ILK only.
1152 */
1153static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1154{
1155 int reg;
1156 u32 val;
1157
1158 /* Don't disable pipe A or pipe A PLLs if needed */
1159 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1160 return;
1161
1162 /* Make sure the pipe isn't still relying on us */
1163 assert_pipe_disabled(dev_priv, pipe);
1164
1165 reg = DPLL(pipe);
1166 val = I915_READ(reg);
1167 val &= ~DPLL_VCO_ENABLE;
1168 I915_WRITE(reg, val);
1169 POSTING_READ(reg);
1170}
1171
1172/**
Jesse Barnes92f25842011-01-04 15:09:34 -08001173 * intel_enable_pch_pll - enable PCH PLL
1174 * @dev_priv: i915 private structure
1175 * @pipe: pipe PLL to enable
1176 *
1177 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1178 * drives the transcoder clock.
1179 */
1180static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
1181 enum pipe pipe)
1182{
1183 int reg;
1184 u32 val;
1185
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001186 if (pipe > 1)
1187 return;
1188
Jesse Barnes92f25842011-01-04 15:09:34 -08001189 /* PCH only available on ILK+ */
1190 BUG_ON(dev_priv->info->gen < 5);
1191
1192 /* PCH refclock must be enabled first */
1193 assert_pch_refclk_enabled(dev_priv);
1194
1195 reg = PCH_DPLL(pipe);
1196 val = I915_READ(reg);
1197 val |= DPLL_VCO_ENABLE;
1198 I915_WRITE(reg, val);
1199 POSTING_READ(reg);
1200 udelay(200);
1201}
1202
1203static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
1204 enum pipe pipe)
1205{
1206 int reg;
1207 u32 val;
1208
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001209 if (pipe > 1)
1210 return;
1211
Jesse Barnes92f25842011-01-04 15:09:34 -08001212 /* PCH only available on ILK+ */
1213 BUG_ON(dev_priv->info->gen < 5);
1214
1215 /* Make sure transcoder isn't still depending on us */
1216 assert_transcoder_disabled(dev_priv, pipe);
1217
1218 reg = PCH_DPLL(pipe);
1219 val = I915_READ(reg);
1220 val &= ~DPLL_VCO_ENABLE;
1221 I915_WRITE(reg, val);
1222 POSTING_READ(reg);
1223 udelay(200);
1224}
1225
Jesse Barnes040484a2011-01-03 12:14:26 -08001226static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
1227 enum pipe pipe)
1228{
1229 int reg;
1230 u32 val;
1231
1232 /* PCH only available on ILK+ */
1233 BUG_ON(dev_priv->info->gen < 5);
1234
1235 /* Make sure PCH DPLL is enabled */
1236 assert_pch_pll_enabled(dev_priv, pipe);
1237
1238 /* FDI must be feeding us bits for PCH ports */
1239 assert_fdi_tx_enabled(dev_priv, pipe);
1240 assert_fdi_rx_enabled(dev_priv, pipe);
1241
1242 reg = TRANSCONF(pipe);
1243 val = I915_READ(reg);
Jesse Barnese9bcff52011-06-24 12:19:20 -07001244
1245 if (HAS_PCH_IBX(dev_priv->dev)) {
1246 /*
1247 * make the BPC in transcoder be consistent with
1248 * that in pipeconf reg.
1249 */
1250 val &= ~PIPE_BPC_MASK;
1251 val |= I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK;
1252 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001253 I915_WRITE(reg, val | TRANS_ENABLE);
1254 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1255 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1256}
1257
1258static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
1259 enum pipe pipe)
1260{
1261 int reg;
1262 u32 val;
1263
1264 /* FDI relies on the transcoder */
1265 assert_fdi_tx_disabled(dev_priv, pipe);
1266 assert_fdi_rx_disabled(dev_priv, pipe);
1267
Jesse Barnes291906f2011-02-02 12:28:03 -08001268 /* Ports must be off as well */
1269 assert_pch_ports_disabled(dev_priv, pipe);
1270
Jesse Barnes040484a2011-01-03 12:14:26 -08001271 reg = TRANSCONF(pipe);
1272 val = I915_READ(reg);
1273 val &= ~TRANS_ENABLE;
1274 I915_WRITE(reg, val);
1275 /* wait for PCH transcoder off, transcoder state */
1276 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes4c9c18c2011-10-13 09:46:32 -07001277 DRM_ERROR("failed to disable transcoder %d\n", pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001278}
1279
Jesse Barnes92f25842011-01-04 15:09:34 -08001280/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001281 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001282 * @dev_priv: i915 private structure
1283 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001284 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001285 *
1286 * Enable @pipe, making sure that various hardware specific requirements
1287 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1288 *
1289 * @pipe should be %PIPE_A or %PIPE_B.
1290 *
1291 * Will wait until the pipe is actually running (i.e. first vblank) before
1292 * returning.
1293 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001294static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1295 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001296{
1297 int reg;
1298 u32 val;
1299
1300 /*
1301 * A pipe without a PLL won't actually be able to drive bits from
1302 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1303 * need the check.
1304 */
1305 if (!HAS_PCH_SPLIT(dev_priv->dev))
1306 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001307 else {
1308 if (pch_port) {
1309 /* if driving the PCH, we need FDI enabled */
1310 assert_fdi_rx_pll_enabled(dev_priv, pipe);
1311 assert_fdi_tx_pll_enabled(dev_priv, pipe);
1312 }
1313 /* FIXME: assert CPU port conditions for SNB+ */
1314 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001315
1316 reg = PIPECONF(pipe);
1317 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001318 if (val & PIPECONF_ENABLE)
1319 return;
1320
1321 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001322 intel_wait_for_vblank(dev_priv->dev, pipe);
1323}
1324
1325/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001326 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001327 * @dev_priv: i915 private structure
1328 * @pipe: pipe to disable
1329 *
1330 * Disable @pipe, making sure that various hardware specific requirements
1331 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1332 *
1333 * @pipe should be %PIPE_A or %PIPE_B.
1334 *
1335 * Will wait until the pipe has shut down before returning.
1336 */
1337static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1338 enum pipe pipe)
1339{
1340 int reg;
1341 u32 val;
1342
1343 /*
1344 * Make sure planes won't keep trying to pump pixels to us,
1345 * or we might hang the display.
1346 */
1347 assert_planes_disabled(dev_priv, pipe);
1348
1349 /* Don't disable pipe A or pipe A PLLs if needed */
1350 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1351 return;
1352
1353 reg = PIPECONF(pipe);
1354 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001355 if ((val & PIPECONF_ENABLE) == 0)
1356 return;
1357
1358 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001359 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1360}
1361
Keith Packardd74362c2011-07-28 14:47:14 -07001362/*
1363 * Plane regs are double buffered, going from enabled->disabled needs a
1364 * trigger in order to latch. The display address reg provides this.
1365 */
1366static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1367 enum plane plane)
1368{
1369 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1370 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1371}
1372
Jesse Barnesb24e7172011-01-04 15:09:30 -08001373/**
1374 * intel_enable_plane - enable a display plane on a given pipe
1375 * @dev_priv: i915 private structure
1376 * @plane: plane to enable
1377 * @pipe: pipe being fed
1378 *
1379 * Enable @plane on @pipe, making sure that @pipe is running first.
1380 */
1381static void intel_enable_plane(struct drm_i915_private *dev_priv,
1382 enum plane plane, enum pipe pipe)
1383{
1384 int reg;
1385 u32 val;
1386
1387 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1388 assert_pipe_enabled(dev_priv, pipe);
1389
1390 reg = DSPCNTR(plane);
1391 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001392 if (val & DISPLAY_PLANE_ENABLE)
1393 return;
1394
1395 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001396 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001397 intel_wait_for_vblank(dev_priv->dev, pipe);
1398}
1399
Jesse Barnesb24e7172011-01-04 15:09:30 -08001400/**
1401 * intel_disable_plane - disable a display plane
1402 * @dev_priv: i915 private structure
1403 * @plane: plane to disable
1404 * @pipe: pipe consuming the data
1405 *
1406 * Disable @plane; should be an independent operation.
1407 */
1408static void intel_disable_plane(struct drm_i915_private *dev_priv,
1409 enum plane plane, enum pipe pipe)
1410{
1411 int reg;
1412 u32 val;
1413
1414 reg = DSPCNTR(plane);
1415 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001416 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1417 return;
1418
1419 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001420 intel_flush_display_plane(dev_priv, plane);
1421 intel_wait_for_vblank(dev_priv->dev, pipe);
1422}
1423
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001424static void disable_pch_dp(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001425 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001426{
1427 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001428 if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
Keith Packardf0575e92011-07-25 22:12:43 -07001429 DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001430 I915_WRITE(reg, val & ~DP_PORT_EN);
Keith Packardf0575e92011-07-25 22:12:43 -07001431 }
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001432}
1433
1434static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
1435 enum pipe pipe, int reg)
1436{
1437 u32 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001438 if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
Keith Packardf0575e92011-07-25 22:12:43 -07001439 DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
1440 reg, pipe);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001441 I915_WRITE(reg, val & ~PORT_ENABLE);
Keith Packardf0575e92011-07-25 22:12:43 -07001442 }
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001443}
1444
1445/* Disable any ports connected to this transcoder */
1446static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
1447 enum pipe pipe)
1448{
1449 u32 reg, val;
1450
1451 val = I915_READ(PCH_PP_CONTROL);
1452 I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
1453
Keith Packardf0575e92011-07-25 22:12:43 -07001454 disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1455 disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1456 disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001457
1458 reg = PCH_ADPA;
1459 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001460 if (adpa_pipe_enabled(dev_priv, val, pipe))
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001461 I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
1462
1463 reg = PCH_LVDS;
1464 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001465 if (lvds_pipe_enabled(dev_priv, val, pipe)) {
1466 DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001467 I915_WRITE(reg, val & ~LVDS_PORT_EN);
1468 POSTING_READ(reg);
1469 udelay(100);
1470 }
1471
1472 disable_pch_hdmi(dev_priv, pipe, HDMIB);
1473 disable_pch_hdmi(dev_priv, pipe, HDMIC);
1474 disable_pch_hdmi(dev_priv, pipe, HDMID);
1475}
1476
Chris Wilson43a95392011-07-08 12:22:36 +01001477static void i8xx_disable_fbc(struct drm_device *dev)
1478{
1479 struct drm_i915_private *dev_priv = dev->dev_private;
1480 u32 fbc_ctl;
1481
1482 /* Disable compression */
1483 fbc_ctl = I915_READ(FBC_CONTROL);
1484 if ((fbc_ctl & FBC_CTL_EN) == 0)
1485 return;
1486
1487 fbc_ctl &= ~FBC_CTL_EN;
1488 I915_WRITE(FBC_CONTROL, fbc_ctl);
1489
1490 /* Wait for compressing bit to clear */
1491 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
1492 DRM_DEBUG_KMS("FBC idle timed out\n");
1493 return;
1494 }
1495
1496 DRM_DEBUG_KMS("disabled FBC\n");
1497}
1498
Jesse Barnes80824002009-09-10 15:28:06 -07001499static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1500{
1501 struct drm_device *dev = crtc->dev;
1502 struct drm_i915_private *dev_priv = dev->dev_private;
1503 struct drm_framebuffer *fb = crtc->fb;
1504 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001505 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes80824002009-09-10 15:28:06 -07001506 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson016b9b62011-07-08 12:22:43 +01001507 int cfb_pitch;
Jesse Barnes80824002009-09-10 15:28:06 -07001508 int plane, i;
1509 u32 fbc_ctl, fbc_ctl2;
1510
Chris Wilson016b9b62011-07-08 12:22:43 +01001511 cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001512 if (fb->pitches[0] < cfb_pitch)
1513 cfb_pitch = fb->pitches[0];
Jesse Barnes80824002009-09-10 15:28:06 -07001514
1515 /* FBC_CTL wants 64B units */
Chris Wilson016b9b62011-07-08 12:22:43 +01001516 cfb_pitch = (cfb_pitch / 64) - 1;
1517 plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
Jesse Barnes80824002009-09-10 15:28:06 -07001518
1519 /* Clear old tags */
1520 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
1521 I915_WRITE(FBC_TAG + (i * 4), 0);
1522
1523 /* Set it up... */
Chris Wilsonde568512011-07-08 12:22:39 +01001524 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
1525 fbc_ctl2 |= plane;
Jesse Barnes80824002009-09-10 15:28:06 -07001526 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
1527 I915_WRITE(FBC_FENCE_OFF, crtc->y);
1528
1529 /* enable it... */
1530 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
Jesse Barnesee25df22010-02-06 10:41:53 -08001531 if (IS_I945GM(dev))
Priit Laes49677902010-03-02 11:37:00 +02001532 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
Chris Wilson016b9b62011-07-08 12:22:43 +01001533 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Jesse Barnes80824002009-09-10 15:28:06 -07001534 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
Chris Wilson016b9b62011-07-08 12:22:43 +01001535 fbc_ctl |= obj->fence_reg;
Jesse Barnes80824002009-09-10 15:28:06 -07001536 I915_WRITE(FBC_CONTROL, fbc_ctl);
1537
Chris Wilson016b9b62011-07-08 12:22:43 +01001538 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
1539 cfb_pitch, crtc->y, intel_crtc->plane);
Jesse Barnes80824002009-09-10 15:28:06 -07001540}
1541
Adam Jacksonee5382a2010-04-23 11:17:39 -04001542static bool i8xx_fbc_enabled(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001543{
Jesse Barnes80824002009-09-10 15:28:06 -07001544 struct drm_i915_private *dev_priv = dev->dev_private;
1545
1546 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
1547}
1548
Jesse Barnes74dff282009-09-14 15:39:40 -07001549static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1550{
1551 struct drm_device *dev = crtc->dev;
1552 struct drm_i915_private *dev_priv = dev->dev_private;
1553 struct drm_framebuffer *fb = crtc->fb;
1554 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001555 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes74dff282009-09-14 15:39:40 -07001556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001557 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Jesse Barnes74dff282009-09-14 15:39:40 -07001558 unsigned long stall_watermark = 200;
1559 u32 dpfc_ctl;
1560
Jesse Barnes74dff282009-09-14 15:39:40 -07001561 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
Chris Wilson016b9b62011-07-08 12:22:43 +01001562 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
Chris Wilsonde568512011-07-08 12:22:39 +01001563 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
Jesse Barnes74dff282009-09-14 15:39:40 -07001564
Jesse Barnes74dff282009-09-14 15:39:40 -07001565 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1566 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1567 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1568 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
1569
1570 /* enable it... */
1571 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
1572
Zhao Yakui28c97732009-10-09 11:39:41 +08001573 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
Jesse Barnes74dff282009-09-14 15:39:40 -07001574}
1575
Chris Wilson43a95392011-07-08 12:22:36 +01001576static void g4x_disable_fbc(struct drm_device *dev)
Jesse Barnes74dff282009-09-14 15:39:40 -07001577{
1578 struct drm_i915_private *dev_priv = dev->dev_private;
1579 u32 dpfc_ctl;
1580
1581 /* Disable compression */
1582 dpfc_ctl = I915_READ(DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001583 if (dpfc_ctl & DPFC_CTL_EN) {
1584 dpfc_ctl &= ~DPFC_CTL_EN;
1585 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
Jesse Barnes74dff282009-09-14 15:39:40 -07001586
Chris Wilsonbed4a672010-09-11 10:47:47 +01001587 DRM_DEBUG_KMS("disabled FBC\n");
1588 }
Jesse Barnes74dff282009-09-14 15:39:40 -07001589}
1590
Adam Jacksonee5382a2010-04-23 11:17:39 -04001591static bool g4x_fbc_enabled(struct drm_device *dev)
Jesse Barnes74dff282009-09-14 15:39:40 -07001592{
Jesse Barnes74dff282009-09-14 15:39:40 -07001593 struct drm_i915_private *dev_priv = dev->dev_private;
1594
1595 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
1596}
1597
Jesse Barnes4efe0702011-01-18 11:25:41 -08001598static void sandybridge_blit_fbc_update(struct drm_device *dev)
1599{
1600 struct drm_i915_private *dev_priv = dev->dev_private;
1601 u32 blt_ecoskpd;
1602
1603 /* Make sure blitter notifies FBC of writes */
Ben Widawskyfcca7922011-04-25 11:23:07 -07001604 gen6_gt_force_wake_get(dev_priv);
Jesse Barnes4efe0702011-01-18 11:25:41 -08001605 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
1606 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
1607 GEN6_BLITTER_LOCK_SHIFT;
1608 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1609 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
1610 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1611 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
1612 GEN6_BLITTER_LOCK_SHIFT);
1613 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1614 POSTING_READ(GEN6_BLITTER_ECOSKPD);
Ben Widawskyfcca7922011-04-25 11:23:07 -07001615 gen6_gt_force_wake_put(dev_priv);
Jesse Barnes4efe0702011-01-18 11:25:41 -08001616}
1617
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001618static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1619{
1620 struct drm_device *dev = crtc->dev;
1621 struct drm_i915_private *dev_priv = dev->dev_private;
1622 struct drm_framebuffer *fb = crtc->fb;
1623 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001624 struct drm_i915_gem_object *obj = intel_fb->obj;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001625 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001626 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001627 unsigned long stall_watermark = 200;
1628 u32 dpfc_ctl;
1629
Chris Wilsonbed4a672010-09-11 10:47:47 +01001630 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001631 dpfc_ctl &= DPFC_RESERVED;
1632 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
Chris Wilson9ce9d062011-07-08 12:22:40 +01001633 /* Set persistent mode for front-buffer rendering, ala X. */
1634 dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
Chris Wilson016b9b62011-07-08 12:22:43 +01001635 dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
Chris Wilsonde568512011-07-08 12:22:39 +01001636 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001637
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001638 I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1639 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1640 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1641 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Chris Wilson05394f32010-11-08 19:18:58 +00001642 I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001643 /* enable it... */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001644 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001645
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001646 if (IS_GEN6(dev)) {
1647 I915_WRITE(SNB_DPFC_CTL_SA,
Chris Wilson016b9b62011-07-08 12:22:43 +01001648 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001649 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
Jesse Barnes4efe0702011-01-18 11:25:41 -08001650 sandybridge_blit_fbc_update(dev);
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001651 }
1652
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001653 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
1654}
1655
Chris Wilson43a95392011-07-08 12:22:36 +01001656static void ironlake_disable_fbc(struct drm_device *dev)
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001657{
1658 struct drm_i915_private *dev_priv = dev->dev_private;
1659 u32 dpfc_ctl;
1660
1661 /* Disable compression */
1662 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001663 if (dpfc_ctl & DPFC_CTL_EN) {
1664 dpfc_ctl &= ~DPFC_CTL_EN;
1665 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001666
Chris Wilsonbed4a672010-09-11 10:47:47 +01001667 DRM_DEBUG_KMS("disabled FBC\n");
1668 }
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001669}
1670
1671static bool ironlake_fbc_enabled(struct drm_device *dev)
1672{
1673 struct drm_i915_private *dev_priv = dev->dev_private;
1674
1675 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
1676}
1677
Adam Jacksonee5382a2010-04-23 11:17:39 -04001678bool intel_fbc_enabled(struct drm_device *dev)
1679{
1680 struct drm_i915_private *dev_priv = dev->dev_private;
1681
1682 if (!dev_priv->display.fbc_enabled)
1683 return false;
1684
1685 return dev_priv->display.fbc_enabled(dev);
1686}
1687
Chris Wilson1630fe72011-07-08 12:22:42 +01001688static void intel_fbc_work_fn(struct work_struct *__work)
1689{
1690 struct intel_fbc_work *work =
1691 container_of(to_delayed_work(__work),
1692 struct intel_fbc_work, work);
1693 struct drm_device *dev = work->crtc->dev;
1694 struct drm_i915_private *dev_priv = dev->dev_private;
1695
1696 mutex_lock(&dev->struct_mutex);
1697 if (work == dev_priv->fbc_work) {
1698 /* Double check that we haven't switched fb without cancelling
1699 * the prior work.
1700 */
Chris Wilson016b9b62011-07-08 12:22:43 +01001701 if (work->crtc->fb == work->fb) {
Chris Wilson1630fe72011-07-08 12:22:42 +01001702 dev_priv->display.enable_fbc(work->crtc,
1703 work->interval);
1704
Chris Wilson016b9b62011-07-08 12:22:43 +01001705 dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
1706 dev_priv->cfb_fb = work->crtc->fb->base.id;
1707 dev_priv->cfb_y = work->crtc->y;
1708 }
1709
Chris Wilson1630fe72011-07-08 12:22:42 +01001710 dev_priv->fbc_work = NULL;
1711 }
1712 mutex_unlock(&dev->struct_mutex);
1713
1714 kfree(work);
1715}
1716
1717static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
1718{
1719 if (dev_priv->fbc_work == NULL)
1720 return;
1721
1722 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
1723
1724 /* Synchronisation is provided by struct_mutex and checking of
1725 * dev_priv->fbc_work, so we can perform the cancellation
1726 * entirely asynchronously.
1727 */
1728 if (cancel_delayed_work(&dev_priv->fbc_work->work))
1729 /* tasklet was killed before being run, clean up */
1730 kfree(dev_priv->fbc_work);
1731
1732 /* Mark the work as no longer wanted so that if it does
1733 * wake-up (because the work was already running and waiting
1734 * for our mutex), it will discover that is no longer
1735 * necessary to run.
1736 */
1737 dev_priv->fbc_work = NULL;
1738}
1739
Chris Wilson43a95392011-07-08 12:22:36 +01001740static void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Adam Jacksonee5382a2010-04-23 11:17:39 -04001741{
Chris Wilson1630fe72011-07-08 12:22:42 +01001742 struct intel_fbc_work *work;
1743 struct drm_device *dev = crtc->dev;
1744 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jacksonee5382a2010-04-23 11:17:39 -04001745
1746 if (!dev_priv->display.enable_fbc)
1747 return;
1748
Chris Wilson1630fe72011-07-08 12:22:42 +01001749 intel_cancel_fbc_work(dev_priv);
1750
1751 work = kzalloc(sizeof *work, GFP_KERNEL);
1752 if (work == NULL) {
1753 dev_priv->display.enable_fbc(crtc, interval);
1754 return;
1755 }
1756
1757 work->crtc = crtc;
1758 work->fb = crtc->fb;
1759 work->interval = interval;
1760 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
1761
1762 dev_priv->fbc_work = work;
1763
1764 DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
1765
1766 /* Delay the actual enabling to let pageflipping cease and the
Chris Wilson016b9b62011-07-08 12:22:43 +01001767 * display to settle before starting the compression. Note that
1768 * this delay also serves a second purpose: it allows for a
1769 * vblank to pass after disabling the FBC before we attempt
1770 * to modify the control registers.
Chris Wilson1630fe72011-07-08 12:22:42 +01001771 *
1772 * A more complicated solution would involve tracking vblanks
1773 * following the termination of the page-flipping sequence
1774 * and indeed performing the enable as a co-routine and not
1775 * waiting synchronously upon the vblank.
1776 */
1777 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
Adam Jacksonee5382a2010-04-23 11:17:39 -04001778}
1779
1780void intel_disable_fbc(struct drm_device *dev)
1781{
1782 struct drm_i915_private *dev_priv = dev->dev_private;
1783
Chris Wilson1630fe72011-07-08 12:22:42 +01001784 intel_cancel_fbc_work(dev_priv);
1785
Adam Jacksonee5382a2010-04-23 11:17:39 -04001786 if (!dev_priv->display.disable_fbc)
1787 return;
1788
1789 dev_priv->display.disable_fbc(dev);
Chris Wilson016b9b62011-07-08 12:22:43 +01001790 dev_priv->cfb_plane = -1;
Adam Jacksonee5382a2010-04-23 11:17:39 -04001791}
1792
Jesse Barnes80824002009-09-10 15:28:06 -07001793/**
1794 * intel_update_fbc - enable/disable FBC as needed
Chris Wilsonbed4a672010-09-11 10:47:47 +01001795 * @dev: the drm_device
Jesse Barnes80824002009-09-10 15:28:06 -07001796 *
1797 * Set up the framebuffer compression hardware at mode set time. We
1798 * enable it if possible:
1799 * - plane A only (on pre-965)
1800 * - no pixel mulitply/line duplication
1801 * - no alpha buffer discard
1802 * - no dual wide
1803 * - framebuffer <= 2048 in width, 1536 in height
1804 *
1805 * We can't assume that any compression will take place (worst case),
1806 * so the compressed buffer has to be the same size as the uncompressed
1807 * one. It also must reside (along with the line length buffer) in
1808 * stolen memory.
1809 *
1810 * We need to enable/disable FBC on a global basis.
1811 */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001812static void intel_update_fbc(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001813{
Jesse Barnes80824002009-09-10 15:28:06 -07001814 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001815 struct drm_crtc *crtc = NULL, *tmp_crtc;
1816 struct intel_crtc *intel_crtc;
1817 struct drm_framebuffer *fb;
Jesse Barnes80824002009-09-10 15:28:06 -07001818 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001819 struct drm_i915_gem_object *obj;
Keith Packardcd0de032011-09-19 21:34:19 -07001820 int enable_fbc;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001821
1822 DRM_DEBUG_KMS("\n");
Jesse Barnes80824002009-09-10 15:28:06 -07001823
1824 if (!i915_powersave)
1825 return;
1826
Adam Jacksonee5382a2010-04-23 11:17:39 -04001827 if (!I915_HAS_FBC(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07001828 return;
1829
Jesse Barnes80824002009-09-10 15:28:06 -07001830 /*
1831 * If FBC is already on, we just have to verify that we can
1832 * keep it that way...
1833 * Need to disable if:
Jesse Barnes9c928d12010-07-23 15:20:00 -07001834 * - more than one pipe is active
Jesse Barnes80824002009-09-10 15:28:06 -07001835 * - changing FBC params (stride, fence, mode)
1836 * - new fb is too large to fit in compressed buffer
1837 * - going to an unsupported config (interlace, pixel multiply, etc.)
1838 */
Jesse Barnes9c928d12010-07-23 15:20:00 -07001839 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilsond2102462011-01-24 17:43:27 +00001840 if (tmp_crtc->enabled && tmp_crtc->fb) {
Chris Wilsonbed4a672010-09-11 10:47:47 +01001841 if (crtc) {
1842 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
1843 dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
1844 goto out_disable;
1845 }
1846 crtc = tmp_crtc;
1847 }
Jesse Barnes9c928d12010-07-23 15:20:00 -07001848 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001849
1850 if (!crtc || crtc->fb == NULL) {
1851 DRM_DEBUG_KMS("no output, disabling\n");
1852 dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001853 goto out_disable;
1854 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001855
1856 intel_crtc = to_intel_crtc(crtc);
1857 fb = crtc->fb;
1858 intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001859 obj = intel_fb->obj;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001860
Keith Packardcd0de032011-09-19 21:34:19 -07001861 enable_fbc = i915_enable_fbc;
1862 if (enable_fbc < 0) {
1863 DRM_DEBUG_KMS("fbc set to per-chip default\n");
1864 enable_fbc = 1;
1865 if (INTEL_INFO(dev)->gen <= 5)
1866 enable_fbc = 0;
1867 }
1868 if (!enable_fbc) {
1869 DRM_DEBUG_KMS("fbc disabled per module param\n");
Jesse Barnesc1a9f042011-05-05 15:24:21 -07001870 dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
1871 goto out_disable;
1872 }
Chris Wilson05394f32010-11-08 19:18:58 +00001873 if (intel_fb->obj->base.size > dev_priv->cfb_size) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001874 DRM_DEBUG_KMS("framebuffer too large, disabling "
Chris Wilson5eddb702010-09-11 13:48:45 +01001875 "compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001876 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001877 goto out_disable;
1878 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001879 if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
1880 (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001881 DRM_DEBUG_KMS("mode incompatible with compression, "
Chris Wilson5eddb702010-09-11 13:48:45 +01001882 "disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001883 dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
Jesse Barnes80824002009-09-10 15:28:06 -07001884 goto out_disable;
1885 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001886 if ((crtc->mode.hdisplay > 2048) ||
1887 (crtc->mode.vdisplay > 1536)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001888 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001889 dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
Jesse Barnes80824002009-09-10 15:28:06 -07001890 goto out_disable;
1891 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001892 if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001893 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001894 dev_priv->no_fbc_reason = FBC_BAD_PLANE;
Jesse Barnes80824002009-09-10 15:28:06 -07001895 goto out_disable;
1896 }
Chris Wilsonde568512011-07-08 12:22:39 +01001897
1898 /* The use of a CPU fence is mandatory in order to detect writes
1899 * by the CPU to the scanout and trigger updates to the FBC.
1900 */
1901 if (obj->tiling_mode != I915_TILING_X ||
1902 obj->fence_reg == I915_FENCE_REG_NONE) {
1903 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001904 dev_priv->no_fbc_reason = FBC_NOT_TILED;
Jesse Barnes80824002009-09-10 15:28:06 -07001905 goto out_disable;
1906 }
1907
Jason Wesselc924b932010-08-05 09:22:32 -05001908 /* If the kernel debugger is active, always disable compression */
1909 if (in_dbg_master())
1910 goto out_disable;
1911
Chris Wilson016b9b62011-07-08 12:22:43 +01001912 /* If the scanout has not changed, don't modify the FBC settings.
1913 * Note that we make the fundamental assumption that the fb->obj
1914 * cannot be unpinned (and have its GTT offset and fence revoked)
1915 * without first being decoupled from the scanout and FBC disabled.
1916 */
1917 if (dev_priv->cfb_plane == intel_crtc->plane &&
1918 dev_priv->cfb_fb == fb->base.id &&
1919 dev_priv->cfb_y == crtc->y)
1920 return;
1921
1922 if (intel_fbc_enabled(dev)) {
1923 /* We update FBC along two paths, after changing fb/crtc
1924 * configuration (modeswitching) and after page-flipping
1925 * finishes. For the latter, we know that not only did
1926 * we disable the FBC at the start of the page-flip
1927 * sequence, but also more than one vblank has passed.
1928 *
1929 * For the former case of modeswitching, it is possible
1930 * to switch between two FBC valid configurations
1931 * instantaneously so we do need to disable the FBC
1932 * before we can modify its control registers. We also
1933 * have to wait for the next vblank for that to take
1934 * effect. However, since we delay enabling FBC we can
1935 * assume that a vblank has passed since disabling and
1936 * that we can safely alter the registers in the deferred
1937 * callback.
1938 *
1939 * In the scenario that we go from a valid to invalid
1940 * and then back to valid FBC configuration we have
1941 * no strict enforcement that a vblank occurred since
1942 * disabling the FBC. However, along all current pipe
1943 * disabling paths we do need to wait for a vblank at
1944 * some point. And we wait before enabling FBC anyway.
1945 */
1946 DRM_DEBUG_KMS("disabling active FBC for update\n");
1947 intel_disable_fbc(dev);
1948 }
1949
Chris Wilsonbed4a672010-09-11 10:47:47 +01001950 intel_enable_fbc(crtc, 500);
Jesse Barnes80824002009-09-10 15:28:06 -07001951 return;
1952
1953out_disable:
Jesse Barnes80824002009-09-10 15:28:06 -07001954 /* Multiple disables should be harmless */
Chris Wilsona9394062010-05-27 13:18:16 +01001955 if (intel_fbc_enabled(dev)) {
1956 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
Adam Jacksonee5382a2010-04-23 11:17:39 -04001957 intel_disable_fbc(dev);
Chris Wilsona9394062010-05-27 13:18:16 +01001958 }
Jesse Barnes80824002009-09-10 15:28:06 -07001959}
1960
Chris Wilson127bd2a2010-07-23 23:32:05 +01001961int
Chris Wilson48b956c2010-09-14 12:50:34 +01001962intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00001963 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00001964 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001965{
Chris Wilsonce453d82011-02-21 14:43:56 +00001966 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001967 u32 alignment;
1968 int ret;
1969
Chris Wilson05394f32010-11-08 19:18:58 +00001970 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001971 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01001972 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
1973 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01001974 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01001975 alignment = 4 * 1024;
1976 else
1977 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001978 break;
1979 case I915_TILING_X:
1980 /* pin() will align the object as required by fence */
1981 alignment = 0;
1982 break;
1983 case I915_TILING_Y:
1984 /* FIXME: Is this true? */
1985 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
1986 return -EINVAL;
1987 default:
1988 BUG();
1989 }
1990
Chris Wilsonce453d82011-02-21 14:43:56 +00001991 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01001992 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01001993 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00001994 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05001995
1996 /* Install a fence for tiled scan-out. Pre-i965 always needs a
1997 * fence, whereas 965+ only requires a fence if using
1998 * framebuffer compression. For simplicity, we always install
1999 * a fence as the cost is not that onerous.
2000 */
Chris Wilson05394f32010-11-08 19:18:58 +00002001 if (obj->tiling_mode != I915_TILING_NONE) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002002 ret = i915_gem_object_get_fence(obj, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002003 if (ret)
2004 goto err_unpin;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002005 }
2006
Chris Wilsonce453d82011-02-21 14:43:56 +00002007 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002008 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002009
2010err_unpin:
2011 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002012err_interruptible:
2013 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01002014 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002015}
2016
Jesse Barnes17638cd2011-06-24 12:19:23 -07002017static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2018 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002019{
2020 struct drm_device *dev = crtc->dev;
2021 struct drm_i915_private *dev_priv = dev->dev_private;
2022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2023 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002024 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002025 int plane = intel_crtc->plane;
2026 unsigned long Start, Offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002027 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002028 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002029
2030 switch (plane) {
2031 case 0:
2032 case 1:
2033 break;
2034 default:
2035 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2036 return -EINVAL;
2037 }
2038
2039 intel_fb = to_intel_framebuffer(fb);
2040 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002041
Chris Wilson5eddb702010-09-11 13:48:45 +01002042 reg = DSPCNTR(plane);
2043 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002044 /* Mask out pixel format bits in case we change it */
2045 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2046 switch (fb->bits_per_pixel) {
2047 case 8:
2048 dspcntr |= DISPPLANE_8BPP;
2049 break;
2050 case 16:
2051 if (fb->depth == 15)
2052 dspcntr |= DISPPLANE_15_16BPP;
2053 else
2054 dspcntr |= DISPPLANE_16BPP;
2055 break;
2056 case 24:
2057 case 32:
2058 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2059 break;
2060 default:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002061 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
Jesse Barnes81255562010-08-02 12:07:50 -07002062 return -EINVAL;
2063 }
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002064 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002065 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002066 dspcntr |= DISPPLANE_TILED;
2067 else
2068 dspcntr &= ~DISPPLANE_TILED;
2069 }
2070
Chris Wilson5eddb702010-09-11 13:48:45 +01002071 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002072
Chris Wilson05394f32010-11-08 19:18:58 +00002073 Start = obj->gtt_offset;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002074 Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002075
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002076 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002077 Start, Offset, x, y, fb->pitches[0]);
2078 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002079 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002080 I915_WRITE(DSPSURF(plane), Start);
2081 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2082 I915_WRITE(DSPADDR(plane), Offset);
2083 } else
2084 I915_WRITE(DSPADDR(plane), Start + Offset);
2085 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002086
Jesse Barnes17638cd2011-06-24 12:19:23 -07002087 return 0;
2088}
2089
2090static int ironlake_update_plane(struct drm_crtc *crtc,
2091 struct drm_framebuffer *fb, int x, int y)
2092{
2093 struct drm_device *dev = crtc->dev;
2094 struct drm_i915_private *dev_priv = dev->dev_private;
2095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2096 struct intel_framebuffer *intel_fb;
2097 struct drm_i915_gem_object *obj;
2098 int plane = intel_crtc->plane;
2099 unsigned long Start, Offset;
2100 u32 dspcntr;
2101 u32 reg;
2102
2103 switch (plane) {
2104 case 0:
2105 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002106 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002107 break;
2108 default:
2109 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2110 return -EINVAL;
2111 }
2112
2113 intel_fb = to_intel_framebuffer(fb);
2114 obj = intel_fb->obj;
2115
2116 reg = DSPCNTR(plane);
2117 dspcntr = I915_READ(reg);
2118 /* Mask out pixel format bits in case we change it */
2119 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2120 switch (fb->bits_per_pixel) {
2121 case 8:
2122 dspcntr |= DISPPLANE_8BPP;
2123 break;
2124 case 16:
2125 if (fb->depth != 16)
2126 return -EINVAL;
2127
2128 dspcntr |= DISPPLANE_16BPP;
2129 break;
2130 case 24:
2131 case 32:
2132 if (fb->depth == 24)
2133 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2134 else if (fb->depth == 30)
2135 dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
2136 else
2137 return -EINVAL;
2138 break;
2139 default:
2140 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
2141 return -EINVAL;
2142 }
2143
2144 if (obj->tiling_mode != I915_TILING_NONE)
2145 dspcntr |= DISPPLANE_TILED;
2146 else
2147 dspcntr &= ~DISPPLANE_TILED;
2148
2149 /* must disable */
2150 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2151
2152 I915_WRITE(reg, dspcntr);
2153
2154 Start = obj->gtt_offset;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002155 Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002156
2157 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002158 Start, Offset, x, y, fb->pitches[0]);
2159 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002160 I915_WRITE(DSPSURF(plane), Start);
2161 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2162 I915_WRITE(DSPADDR(plane), Offset);
2163 POSTING_READ(reg);
2164
2165 return 0;
2166}
2167
2168/* Assume fb object is pinned & idle & fenced and just update base pointers */
2169static int
2170intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2171 int x, int y, enum mode_set_atomic state)
2172{
2173 struct drm_device *dev = crtc->dev;
2174 struct drm_i915_private *dev_priv = dev->dev_private;
2175 int ret;
2176
2177 ret = dev_priv->display.update_plane(crtc, fb, x, y);
2178 if (ret)
2179 return ret;
2180
Chris Wilsonbed4a672010-09-11 10:47:47 +01002181 intel_update_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002182 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002183
2184 return 0;
2185}
2186
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002187static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002188intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2189 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002190{
2191 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002192 struct drm_i915_master_private *master_priv;
2193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002194 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002195
2196 /* no fb bound */
2197 if (!crtc->fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002198 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002199 return 0;
2200 }
2201
Chris Wilson265db952010-09-20 15:41:01 +01002202 switch (intel_crtc->plane) {
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002203 case 0:
2204 case 1:
2205 break;
Jesse Barnes27f82272011-09-02 12:54:37 -07002206 case 2:
2207 if (IS_IVYBRIDGE(dev))
2208 break;
2209 /* fall through otherwise */
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002210 default:
Jesse Barnesa5071c22011-07-19 15:38:56 -07002211 DRM_ERROR("no plane for crtc\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002212 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002213 }
2214
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002215 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002216 ret = intel_pin_and_fence_fb_obj(dev,
2217 to_intel_framebuffer(crtc->fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002218 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002219 if (ret != 0) {
2220 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002221 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002222 return ret;
2223 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002224
Chris Wilson265db952010-09-20 15:41:01 +01002225 if (old_fb) {
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002226 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002227 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
Chris Wilson265db952010-09-20 15:41:01 +01002228
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002229 wait_event(dev_priv->pending_flip_queue,
Chris Wilson01eec722011-02-11 20:47:45 +00002230 atomic_read(&dev_priv->mm.wedged) ||
Chris Wilson05394f32010-11-08 19:18:58 +00002231 atomic_read(&obj->pending_flip) == 0);
Chris Wilson85345512010-11-13 09:49:11 +00002232
2233 /* Big Hammer, we also need to ensure that any pending
2234 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2235 * current scanout is retired before unpinning the old
2236 * framebuffer.
Chris Wilson01eec722011-02-11 20:47:45 +00002237 *
2238 * This should only fail upon a hung GPU, in which case we
2239 * can safely continue.
Chris Wilson85345512010-11-13 09:49:11 +00002240 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002241 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson01eec722011-02-11 20:47:45 +00002242 (void) ret;
Chris Wilson265db952010-09-20 15:41:01 +01002243 }
2244
Jason Wessel21c74a82010-10-13 14:09:44 -05002245 ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
2246 LEAVE_ATOMIC_MODE_SET);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002247 if (ret) {
Chris Wilson265db952010-09-20 15:41:01 +01002248 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002249 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002250 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002251 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002252 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002253
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002254 if (old_fb) {
2255 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson265db952010-09-20 15:41:01 +01002256 i915_gem_object_unpin(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002257 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002258
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002259 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002260
2261 if (!dev->primary->master)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002262 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002263
2264 master_priv = dev->primary->master->driver_priv;
2265 if (!master_priv->sarea_priv)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002266 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002267
Chris Wilson265db952010-09-20 15:41:01 +01002268 if (intel_crtc->pipe) {
Jesse Barnes79e53942008-11-07 14:24:08 -08002269 master_priv->sarea_priv->pipeB_x = x;
2270 master_priv->sarea_priv->pipeB_y = y;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002271 } else {
2272 master_priv->sarea_priv->pipeA_x = x;
2273 master_priv->sarea_priv->pipeA_y = y;
Jesse Barnes79e53942008-11-07 14:24:08 -08002274 }
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002275
2276 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002277}
2278
Chris Wilson5eddb702010-09-11 13:48:45 +01002279static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002280{
2281 struct drm_device *dev = crtc->dev;
2282 struct drm_i915_private *dev_priv = dev->dev_private;
2283 u32 dpa_ctl;
2284
Zhao Yakui28c97732009-10-09 11:39:41 +08002285 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002286 dpa_ctl = I915_READ(DP_A);
2287 dpa_ctl &= ~DP_PLL_FREQ_MASK;
2288
2289 if (clock < 200000) {
2290 u32 temp;
2291 dpa_ctl |= DP_PLL_FREQ_160MHZ;
2292 /* workaround for 160Mhz:
2293 1) program 0x4600c bits 15:0 = 0x8124
2294 2) program 0x46010 bit 0 = 1
2295 3) program 0x46034 bit 24 = 1
2296 4) program 0x64000 bit 14 = 1
2297 */
2298 temp = I915_READ(0x4600c);
2299 temp &= 0xffff0000;
2300 I915_WRITE(0x4600c, temp | 0x8124);
2301
2302 temp = I915_READ(0x46010);
2303 I915_WRITE(0x46010, temp | 1);
2304
2305 temp = I915_READ(0x46034);
2306 I915_WRITE(0x46034, temp | (1 << 24));
2307 } else {
2308 dpa_ctl |= DP_PLL_FREQ_270MHZ;
2309 }
2310 I915_WRITE(DP_A, dpa_ctl);
2311
Chris Wilson5eddb702010-09-11 13:48:45 +01002312 POSTING_READ(DP_A);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002313 udelay(500);
2314}
2315
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002316static void intel_fdi_normal_train(struct drm_crtc *crtc)
2317{
2318 struct drm_device *dev = crtc->dev;
2319 struct drm_i915_private *dev_priv = dev->dev_private;
2320 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2321 int pipe = intel_crtc->pipe;
2322 u32 reg, temp;
2323
2324 /* enable normal train */
2325 reg = FDI_TX_CTL(pipe);
2326 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002327 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002328 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2329 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002330 } else {
2331 temp &= ~FDI_LINK_TRAIN_NONE;
2332 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002333 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002334 I915_WRITE(reg, temp);
2335
2336 reg = FDI_RX_CTL(pipe);
2337 temp = I915_READ(reg);
2338 if (HAS_PCH_CPT(dev)) {
2339 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2340 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2341 } else {
2342 temp &= ~FDI_LINK_TRAIN_NONE;
2343 temp |= FDI_LINK_TRAIN_NONE;
2344 }
2345 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2346
2347 /* wait one idle pattern time */
2348 POSTING_READ(reg);
2349 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002350
2351 /* IVB wants error correction enabled */
2352 if (IS_IVYBRIDGE(dev))
2353 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2354 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002355}
2356
Jesse Barnes291427f2011-07-29 12:42:37 -07002357static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
2358{
2359 struct drm_i915_private *dev_priv = dev->dev_private;
2360 u32 flags = I915_READ(SOUTH_CHICKEN1);
2361
2362 flags |= FDI_PHASE_SYNC_OVR(pipe);
2363 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
2364 flags |= FDI_PHASE_SYNC_EN(pipe);
2365 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2366 POSTING_READ(SOUTH_CHICKEN1);
2367}
2368
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002369/* The FDI link training functions for ILK/Ibexpeak. */
2370static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2371{
2372 struct drm_device *dev = crtc->dev;
2373 struct drm_i915_private *dev_priv = dev->dev_private;
2374 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2375 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002376 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002377 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002378
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002379 /* FDI needs bits from pipe & plane first */
2380 assert_pipe_enabled(dev_priv, pipe);
2381 assert_plane_enabled(dev_priv, plane);
2382
Adam Jacksone1a44742010-06-25 15:32:14 -04002383 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2384 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002385 reg = FDI_RX_IMR(pipe);
2386 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002387 temp &= ~FDI_RX_SYMBOL_LOCK;
2388 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002389 I915_WRITE(reg, temp);
2390 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002391 udelay(150);
2392
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002393 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002394 reg = FDI_TX_CTL(pipe);
2395 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002396 temp &= ~(7 << 19);
2397 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002398 temp &= ~FDI_LINK_TRAIN_NONE;
2399 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002400 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002401
Chris Wilson5eddb702010-09-11 13:48:45 +01002402 reg = FDI_RX_CTL(pipe);
2403 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002404 temp &= ~FDI_LINK_TRAIN_NONE;
2405 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002406 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2407
2408 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002409 udelay(150);
2410
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002411 /* Ironlake workaround, enable clock pointer after FDI enable*/
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002412 if (HAS_PCH_IBX(dev)) {
2413 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2414 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2415 FDI_RX_PHASE_SYNC_POINTER_EN);
2416 }
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002417
Chris Wilson5eddb702010-09-11 13:48:45 +01002418 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002419 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002420 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002421 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2422
2423 if ((temp & FDI_RX_BIT_LOCK)) {
2424 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002425 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002426 break;
2427 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002428 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002429 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002430 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002431
2432 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002433 reg = FDI_TX_CTL(pipe);
2434 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002435 temp &= ~FDI_LINK_TRAIN_NONE;
2436 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002437 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002438
Chris Wilson5eddb702010-09-11 13:48:45 +01002439 reg = FDI_RX_CTL(pipe);
2440 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002441 temp &= ~FDI_LINK_TRAIN_NONE;
2442 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002443 I915_WRITE(reg, temp);
2444
2445 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002446 udelay(150);
2447
Chris Wilson5eddb702010-09-11 13:48:45 +01002448 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002449 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002450 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002451 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2452
2453 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002454 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002455 DRM_DEBUG_KMS("FDI train 2 done.\n");
2456 break;
2457 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002458 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002459 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002460 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002461
2462 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002463
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002464}
2465
Akshay Joshi0206e352011-08-16 15:34:10 -04002466static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002467 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2468 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2469 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2470 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2471};
2472
2473/* The FDI link training functions for SNB/Cougarpoint. */
2474static void gen6_fdi_link_train(struct drm_crtc *crtc)
2475{
2476 struct drm_device *dev = crtc->dev;
2477 struct drm_i915_private *dev_priv = dev->dev_private;
2478 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2479 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002480 u32 reg, temp, i;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002481
Adam Jacksone1a44742010-06-25 15:32:14 -04002482 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2483 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002484 reg = FDI_RX_IMR(pipe);
2485 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002486 temp &= ~FDI_RX_SYMBOL_LOCK;
2487 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002488 I915_WRITE(reg, temp);
2489
2490 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002491 udelay(150);
2492
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002493 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002494 reg = FDI_TX_CTL(pipe);
2495 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002496 temp &= ~(7 << 19);
2497 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002498 temp &= ~FDI_LINK_TRAIN_NONE;
2499 temp |= FDI_LINK_TRAIN_PATTERN_1;
2500 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2501 /* SNB-B */
2502 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002503 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002504
Chris Wilson5eddb702010-09-11 13:48:45 +01002505 reg = FDI_RX_CTL(pipe);
2506 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002507 if (HAS_PCH_CPT(dev)) {
2508 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2509 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2510 } else {
2511 temp &= ~FDI_LINK_TRAIN_NONE;
2512 temp |= FDI_LINK_TRAIN_PATTERN_1;
2513 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002514 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2515
2516 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002517 udelay(150);
2518
Jesse Barnes291427f2011-07-29 12:42:37 -07002519 if (HAS_PCH_CPT(dev))
2520 cpt_phase_pointer_enable(dev, pipe);
2521
Akshay Joshi0206e352011-08-16 15:34:10 -04002522 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002523 reg = FDI_TX_CTL(pipe);
2524 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002525 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2526 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002527 I915_WRITE(reg, temp);
2528
2529 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002530 udelay(500);
2531
Chris Wilson5eddb702010-09-11 13:48:45 +01002532 reg = FDI_RX_IIR(pipe);
2533 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002534 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2535
2536 if (temp & FDI_RX_BIT_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002537 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002538 DRM_DEBUG_KMS("FDI train 1 done.\n");
2539 break;
2540 }
2541 }
2542 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002543 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002544
2545 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002546 reg = FDI_TX_CTL(pipe);
2547 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002548 temp &= ~FDI_LINK_TRAIN_NONE;
2549 temp |= FDI_LINK_TRAIN_PATTERN_2;
2550 if (IS_GEN6(dev)) {
2551 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2552 /* SNB-B */
2553 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2554 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002555 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002556
Chris Wilson5eddb702010-09-11 13:48:45 +01002557 reg = FDI_RX_CTL(pipe);
2558 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002559 if (HAS_PCH_CPT(dev)) {
2560 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2561 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2562 } else {
2563 temp &= ~FDI_LINK_TRAIN_NONE;
2564 temp |= FDI_LINK_TRAIN_PATTERN_2;
2565 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002566 I915_WRITE(reg, temp);
2567
2568 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002569 udelay(150);
2570
Akshay Joshi0206e352011-08-16 15:34:10 -04002571 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002572 reg = FDI_TX_CTL(pipe);
2573 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002574 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2575 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002576 I915_WRITE(reg, temp);
2577
2578 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002579 udelay(500);
2580
Chris Wilson5eddb702010-09-11 13:48:45 +01002581 reg = FDI_RX_IIR(pipe);
2582 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002583 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2584
2585 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002586 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002587 DRM_DEBUG_KMS("FDI train 2 done.\n");
2588 break;
2589 }
2590 }
2591 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002592 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002593
2594 DRM_DEBUG_KMS("FDI train done.\n");
2595}
2596
Jesse Barnes357555c2011-04-28 15:09:55 -07002597/* Manual link training for Ivy Bridge A0 parts */
2598static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2599{
2600 struct drm_device *dev = crtc->dev;
2601 struct drm_i915_private *dev_priv = dev->dev_private;
2602 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2603 int pipe = intel_crtc->pipe;
2604 u32 reg, temp, i;
2605
2606 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2607 for train result */
2608 reg = FDI_RX_IMR(pipe);
2609 temp = I915_READ(reg);
2610 temp &= ~FDI_RX_SYMBOL_LOCK;
2611 temp &= ~FDI_RX_BIT_LOCK;
2612 I915_WRITE(reg, temp);
2613
2614 POSTING_READ(reg);
2615 udelay(150);
2616
2617 /* enable CPU FDI TX and PCH FDI RX */
2618 reg = FDI_TX_CTL(pipe);
2619 temp = I915_READ(reg);
2620 temp &= ~(7 << 19);
2621 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2622 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2623 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2624 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2625 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002626 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002627 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2628
2629 reg = FDI_RX_CTL(pipe);
2630 temp = I915_READ(reg);
2631 temp &= ~FDI_LINK_TRAIN_AUTO;
2632 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2633 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002634 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002635 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2636
2637 POSTING_READ(reg);
2638 udelay(150);
2639
Jesse Barnes291427f2011-07-29 12:42:37 -07002640 if (HAS_PCH_CPT(dev))
2641 cpt_phase_pointer_enable(dev, pipe);
2642
Akshay Joshi0206e352011-08-16 15:34:10 -04002643 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002644 reg = FDI_TX_CTL(pipe);
2645 temp = I915_READ(reg);
2646 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2647 temp |= snb_b_fdi_train_param[i];
2648 I915_WRITE(reg, temp);
2649
2650 POSTING_READ(reg);
2651 udelay(500);
2652
2653 reg = FDI_RX_IIR(pipe);
2654 temp = I915_READ(reg);
2655 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2656
2657 if (temp & FDI_RX_BIT_LOCK ||
2658 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2659 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2660 DRM_DEBUG_KMS("FDI train 1 done.\n");
2661 break;
2662 }
2663 }
2664 if (i == 4)
2665 DRM_ERROR("FDI train 1 fail!\n");
2666
2667 /* Train 2 */
2668 reg = FDI_TX_CTL(pipe);
2669 temp = I915_READ(reg);
2670 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2671 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2672 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2673 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2674 I915_WRITE(reg, temp);
2675
2676 reg = FDI_RX_CTL(pipe);
2677 temp = I915_READ(reg);
2678 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2679 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2680 I915_WRITE(reg, temp);
2681
2682 POSTING_READ(reg);
2683 udelay(150);
2684
Akshay Joshi0206e352011-08-16 15:34:10 -04002685 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002686 reg = FDI_TX_CTL(pipe);
2687 temp = I915_READ(reg);
2688 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2689 temp |= snb_b_fdi_train_param[i];
2690 I915_WRITE(reg, temp);
2691
2692 POSTING_READ(reg);
2693 udelay(500);
2694
2695 reg = FDI_RX_IIR(pipe);
2696 temp = I915_READ(reg);
2697 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2698
2699 if (temp & FDI_RX_SYMBOL_LOCK) {
2700 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2701 DRM_DEBUG_KMS("FDI train 2 done.\n");
2702 break;
2703 }
2704 }
2705 if (i == 4)
2706 DRM_ERROR("FDI train 2 fail!\n");
2707
2708 DRM_DEBUG_KMS("FDI train done.\n");
2709}
2710
2711static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002712{
2713 struct drm_device *dev = crtc->dev;
2714 struct drm_i915_private *dev_priv = dev->dev_private;
2715 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2716 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002717 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002718
Jesse Barnesc64e3112010-09-10 11:27:03 -07002719 /* Write the TU size bits so error detection works */
Chris Wilson5eddb702010-09-11 13:48:45 +01002720 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2721 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
Jesse Barnesc64e3112010-09-10 11:27:03 -07002722
Jesse Barnes0e23b992010-09-10 11:10:00 -07002723 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002724 reg = FDI_RX_CTL(pipe);
2725 temp = I915_READ(reg);
2726 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002727 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Chris Wilson5eddb702010-09-11 13:48:45 +01002728 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2729 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2730
2731 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002732 udelay(200);
2733
2734 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002735 temp = I915_READ(reg);
2736 I915_WRITE(reg, temp | FDI_PCDCLK);
2737
2738 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002739 udelay(200);
2740
2741 /* Enable CPU FDI TX PLL, always on for Ironlake */
Chris Wilson5eddb702010-09-11 13:48:45 +01002742 reg = FDI_TX_CTL(pipe);
2743 temp = I915_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002744 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002745 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2746
2747 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002748 udelay(100);
2749 }
2750}
2751
Jesse Barnes291427f2011-07-29 12:42:37 -07002752static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2753{
2754 struct drm_i915_private *dev_priv = dev->dev_private;
2755 u32 flags = I915_READ(SOUTH_CHICKEN1);
2756
2757 flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2758 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2759 flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2760 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2761 POSTING_READ(SOUTH_CHICKEN1);
2762}
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002763static void ironlake_fdi_disable(struct drm_crtc *crtc)
2764{
2765 struct drm_device *dev = crtc->dev;
2766 struct drm_i915_private *dev_priv = dev->dev_private;
2767 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2768 int pipe = intel_crtc->pipe;
2769 u32 reg, temp;
2770
2771 /* disable CPU FDI tx and PCH FDI rx */
2772 reg = FDI_TX_CTL(pipe);
2773 temp = I915_READ(reg);
2774 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2775 POSTING_READ(reg);
2776
2777 reg = FDI_RX_CTL(pipe);
2778 temp = I915_READ(reg);
2779 temp &= ~(0x7 << 16);
2780 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2781 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2782
2783 POSTING_READ(reg);
2784 udelay(100);
2785
2786 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002787 if (HAS_PCH_IBX(dev)) {
2788 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002789 I915_WRITE(FDI_RX_CHICKEN(pipe),
2790 I915_READ(FDI_RX_CHICKEN(pipe) &
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002791 ~FDI_RX_PHASE_SYNC_POINTER_EN));
Jesse Barnes291427f2011-07-29 12:42:37 -07002792 } else if (HAS_PCH_CPT(dev)) {
2793 cpt_phase_pointer_disable(dev, pipe);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002794 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002795
2796 /* still set train pattern 1 */
2797 reg = FDI_TX_CTL(pipe);
2798 temp = I915_READ(reg);
2799 temp &= ~FDI_LINK_TRAIN_NONE;
2800 temp |= FDI_LINK_TRAIN_PATTERN_1;
2801 I915_WRITE(reg, temp);
2802
2803 reg = FDI_RX_CTL(pipe);
2804 temp = I915_READ(reg);
2805 if (HAS_PCH_CPT(dev)) {
2806 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2807 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2808 } else {
2809 temp &= ~FDI_LINK_TRAIN_NONE;
2810 temp |= FDI_LINK_TRAIN_PATTERN_1;
2811 }
2812 /* BPC in FDI rx is consistent with that in PIPECONF */
2813 temp &= ~(0x07 << 16);
2814 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2815 I915_WRITE(reg, temp);
2816
2817 POSTING_READ(reg);
2818 udelay(100);
2819}
2820
Chris Wilson6b383a72010-09-13 13:54:26 +01002821/*
2822 * When we disable a pipe, we need to clear any pending scanline wait events
2823 * to avoid hanging the ring, which we assume we are waiting on.
2824 */
2825static void intel_clear_scanline_wait(struct drm_device *dev)
2826{
2827 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8168bd42010-11-11 17:54:52 +00002828 struct intel_ring_buffer *ring;
Chris Wilson6b383a72010-09-13 13:54:26 +01002829 u32 tmp;
2830
2831 if (IS_GEN2(dev))
2832 /* Can't break the hang on i8xx */
2833 return;
2834
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002835 ring = LP_RING(dev_priv);
Chris Wilson8168bd42010-11-11 17:54:52 +00002836 tmp = I915_READ_CTL(ring);
2837 if (tmp & RING_WAIT)
2838 I915_WRITE_CTL(ring, tmp);
Chris Wilson6b383a72010-09-13 13:54:26 +01002839}
2840
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002841static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2842{
Chris Wilson05394f32010-11-08 19:18:58 +00002843 struct drm_i915_gem_object *obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002844 struct drm_i915_private *dev_priv;
2845
2846 if (crtc->fb == NULL)
2847 return;
2848
Chris Wilson05394f32010-11-08 19:18:58 +00002849 obj = to_intel_framebuffer(crtc->fb)->obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002850 dev_priv = crtc->dev->dev_private;
2851 wait_event(dev_priv->pending_flip_queue,
Chris Wilson05394f32010-11-08 19:18:58 +00002852 atomic_read(&obj->pending_flip) == 0);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002853}
2854
Jesse Barnes040484a2011-01-03 12:14:26 -08002855static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
2856{
2857 struct drm_device *dev = crtc->dev;
2858 struct drm_mode_config *mode_config = &dev->mode_config;
2859 struct intel_encoder *encoder;
2860
2861 /*
2862 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2863 * must be driven by its own crtc; no sharing is possible.
2864 */
2865 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
2866 if (encoder->base.crtc != crtc)
2867 continue;
2868
2869 switch (encoder->type) {
2870 case INTEL_OUTPUT_EDP:
2871 if (!intel_encoder_is_pch_edp(&encoder->base))
2872 return false;
2873 continue;
2874 }
2875 }
2876
2877 return true;
2878}
2879
Jesse Barnesf67a5592011-01-05 10:31:48 -08002880/*
2881 * Enable PCH resources required for PCH ports:
2882 * - PCH PLLs
2883 * - FDI training & RX/TX
2884 * - update transcoder timings
2885 * - DP transcoding bits
2886 * - transcoder
2887 */
2888static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002889{
2890 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002891 struct drm_i915_private *dev_priv = dev->dev_private;
2892 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2893 int pipe = intel_crtc->pipe;
Jesse Barnes4b645f12011-10-12 09:51:31 -07002894 u32 reg, temp, transc_sel;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002895
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002896 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07002897 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002898
Jesse Barnes92f25842011-01-04 15:09:34 -08002899 intel_enable_pch_pll(dev_priv, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002900
2901 if (HAS_PCH_CPT(dev)) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07002902 transc_sel = intel_crtc->use_pll_a ? TRANSC_DPLLA_SEL :
2903 TRANSC_DPLLB_SEL;
2904
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002905 /* Be sure PCH DPLL SEL is set */
2906 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesd64311a2011-10-12 15:01:33 -07002907 if (pipe == 0) {
2908 temp &= ~(TRANSA_DPLLB_SEL);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002909 temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
Jesse Barnesd64311a2011-10-12 15:01:33 -07002910 } else if (pipe == 1) {
2911 temp &= ~(TRANSB_DPLLB_SEL);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002912 temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnesd64311a2011-10-12 15:01:33 -07002913 } else if (pipe == 2) {
2914 temp &= ~(TRANSC_DPLLB_SEL);
Jesse Barnes4b645f12011-10-12 09:51:31 -07002915 temp |= (TRANSC_DPLL_ENABLE | transc_sel);
Jesse Barnesd64311a2011-10-12 15:01:33 -07002916 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002917 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002918 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002919
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08002920 /* set transcoder timing, panel must allow it */
2921 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01002922 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
2923 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
2924 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
2925
2926 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
2927 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
2928 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002929
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002930 intel_fdi_normal_train(crtc);
2931
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002932 /* For PCH DP, enable TRANS_DP_CTL */
2933 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07002934 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
2935 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnes9325c9f2011-06-24 12:19:21 -07002936 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01002937 reg = TRANS_DP_CTL(pipe);
2938 temp = I915_READ(reg);
2939 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08002940 TRANS_DP_SYNC_MASK |
2941 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01002942 temp |= (TRANS_DP_OUTPUT_ENABLE |
2943 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07002944 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002945
2946 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01002947 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002948 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01002949 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002950
2951 switch (intel_trans_dp_port_sel(crtc)) {
2952 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01002953 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002954 break;
2955 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01002956 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002957 break;
2958 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01002959 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002960 break;
2961 default:
2962 DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002963 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002964 break;
2965 }
2966
Chris Wilson5eddb702010-09-11 13:48:45 +01002967 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002968 }
2969
Jesse Barnes040484a2011-01-03 12:14:26 -08002970 intel_enable_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08002971}
2972
Jesse Barnesd4270e52011-10-11 10:43:02 -07002973void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
2974{
2975 struct drm_i915_private *dev_priv = dev->dev_private;
2976 int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
2977 u32 temp;
2978
2979 temp = I915_READ(dslreg);
2980 udelay(500);
2981 if (wait_for(I915_READ(dslreg) != temp, 5)) {
2982 /* Without this, mode sets may fail silently on FDI */
2983 I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
2984 udelay(250);
2985 I915_WRITE(tc2reg, 0);
2986 if (wait_for(I915_READ(dslreg) != temp, 5))
2987 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
2988 }
2989}
2990
Jesse Barnesf67a5592011-01-05 10:31:48 -08002991static void ironlake_crtc_enable(struct drm_crtc *crtc)
2992{
2993 struct drm_device *dev = crtc->dev;
2994 struct drm_i915_private *dev_priv = dev->dev_private;
2995 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2996 int pipe = intel_crtc->pipe;
2997 int plane = intel_crtc->plane;
2998 u32 temp;
2999 bool is_pch_port;
3000
3001 if (intel_crtc->active)
3002 return;
3003
3004 intel_crtc->active = true;
3005 intel_update_watermarks(dev);
3006
3007 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3008 temp = I915_READ(PCH_LVDS);
3009 if ((temp & LVDS_PORT_EN) == 0)
3010 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3011 }
3012
3013 is_pch_port = intel_crtc_driving_pch(crtc);
3014
3015 if (is_pch_port)
Jesse Barnes357555c2011-04-28 15:09:55 -07003016 ironlake_fdi_pll_enable(crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003017 else
3018 ironlake_fdi_disable(crtc);
3019
3020 /* Enable panel fitting for LVDS */
3021 if (dev_priv->pch_pf_size &&
3022 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
3023 /* Force use of hard-coded filter coefficients
3024 * as some pre-programmed values are broken,
3025 * e.g. x201.
3026 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003027 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3028 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3029 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003030 }
3031
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003032 /*
3033 * On ILK+ LUT must be loaded before the pipe is running but with
3034 * clocks enabled
3035 */
3036 intel_crtc_load_lut(crtc);
3037
Jesse Barnesf67a5592011-01-05 10:31:48 -08003038 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3039 intel_enable_plane(dev_priv, plane, pipe);
3040
3041 if (is_pch_port)
3042 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003043
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003044 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003045 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003046 mutex_unlock(&dev->struct_mutex);
3047
Chris Wilson6b383a72010-09-13 13:54:26 +01003048 intel_crtc_update_cursor(crtc, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003049}
3050
3051static void ironlake_crtc_disable(struct drm_crtc *crtc)
3052{
3053 struct drm_device *dev = crtc->dev;
3054 struct drm_i915_private *dev_priv = dev->dev_private;
3055 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3056 int pipe = intel_crtc->pipe;
3057 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003058 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003059
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003060 if (!intel_crtc->active)
3061 return;
3062
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003063 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003064 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01003065 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01003066
Jesse Barnesb24e7172011-01-04 15:09:30 -08003067 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003068
Chris Wilson973d04f2011-07-08 12:22:37 +01003069 if (dev_priv->cfb_plane == plane)
3070 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003071
Jesse Barnesb24e7172011-01-04 15:09:30 -08003072 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003073
Jesse Barnes6be4a602010-09-10 10:26:01 -07003074 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003075 I915_WRITE(PF_CTL(pipe), 0);
3076 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003077
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003078 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003079
Jesse Barnes47a05ec2011-02-07 13:46:40 -08003080 /* This is a horrible layering violation; we should be doing this in
3081 * the connector/encoder ->prepare instead, but we don't always have
3082 * enough information there about the config to know whether it will
3083 * actually be necessary or just cause undesired flicker.
3084 */
3085 intel_disable_pch_ports(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003086
Jesse Barnes040484a2011-01-03 12:14:26 -08003087 intel_disable_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003088
Jesse Barnes6be4a602010-09-10 10:26:01 -07003089 if (HAS_PCH_CPT(dev)) {
3090 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003091 reg = TRANS_DP_CTL(pipe);
3092 temp = I915_READ(reg);
3093 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08003094 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01003095 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003096
3097 /* disable DPLL_SEL */
3098 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003099 switch (pipe) {
3100 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07003101 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003102 break;
3103 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07003104 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003105 break;
3106 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07003107 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07003108 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003109 break;
3110 default:
3111 BUG(); /* wtf */
3112 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07003113 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003114 }
3115
3116 /* disable PCH DPLL */
Jesse Barnes4b645f12011-10-12 09:51:31 -07003117 if (!intel_crtc->no_pll)
3118 intel_disable_pch_pll(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003119
3120 /* Switch from PCDclk to Rawclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003121 reg = FDI_RX_CTL(pipe);
3122 temp = I915_READ(reg);
3123 I915_WRITE(reg, temp & ~FDI_PCDCLK);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003124
3125 /* Disable CPU FDI TX PLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003126 reg = FDI_TX_CTL(pipe);
3127 temp = I915_READ(reg);
3128 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3129
3130 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003131 udelay(100);
3132
Chris Wilson5eddb702010-09-11 13:48:45 +01003133 reg = FDI_RX_CTL(pipe);
3134 temp = I915_READ(reg);
3135 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003136
3137 /* Wait for the clocks to turn off. */
Chris Wilson5eddb702010-09-11 13:48:45 +01003138 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003139 udelay(100);
Chris Wilson6b383a72010-09-13 13:54:26 +01003140
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003141 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003142 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003143
3144 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003145 intel_update_fbc(dev);
3146 intel_clear_scanline_wait(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003147 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003148}
3149
3150static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
3151{
3152 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3153 int pipe = intel_crtc->pipe;
3154 int plane = intel_crtc->plane;
3155
Zhenyu Wang2c072452009-06-05 15:38:42 +08003156 /* XXX: When our outputs are all unaware of DPMS modes other than off
3157 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
3158 */
3159 switch (mode) {
3160 case DRM_MODE_DPMS_ON:
3161 case DRM_MODE_DPMS_STANDBY:
3162 case DRM_MODE_DPMS_SUSPEND:
Chris Wilson868dc582010-08-07 11:01:31 +01003163 DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003164 ironlake_crtc_enable(crtc);
Chris Wilson868dc582010-08-07 11:01:31 +01003165 break;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08003166
Zhenyu Wang2c072452009-06-05 15:38:42 +08003167 case DRM_MODE_DPMS_OFF:
Chris Wilson868dc582010-08-07 11:01:31 +01003168 DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003169 ironlake_crtc_disable(crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08003170 break;
3171 }
3172}
3173
Daniel Vetter02e792f2009-09-15 22:57:34 +02003174static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3175{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003176 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003177 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003178 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003179
Chris Wilson23f09ce2010-08-12 13:53:37 +01003180 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003181 dev_priv->mm.interruptible = false;
3182 (void) intel_overlay_switch_off(intel_crtc->overlay);
3183 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003184 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003185 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003186
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003187 /* Let userspace switch the overlay on again. In most cases userspace
3188 * has to recompute where to put it anyway.
3189 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003190}
3191
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003192static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003193{
3194 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003195 struct drm_i915_private *dev_priv = dev->dev_private;
3196 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3197 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003198 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003199
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003200 if (intel_crtc->active)
3201 return;
3202
3203 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003204 intel_update_watermarks(dev);
3205
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003206 intel_enable_pll(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08003207 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003208 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003209
3210 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003211 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003212
3213 /* Give the overlay scaler a chance to enable if it's on this pipe */
3214 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003215 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003216}
3217
3218static void i9xx_crtc_disable(struct drm_crtc *crtc)
3219{
3220 struct drm_device *dev = crtc->dev;
3221 struct drm_i915_private *dev_priv = dev->dev_private;
3222 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3223 int pipe = intel_crtc->pipe;
3224 int plane = intel_crtc->plane;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003225
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003226 if (!intel_crtc->active)
3227 return;
3228
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003229 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003230 intel_crtc_wait_for_pending_flips(crtc);
3231 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003232 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003233 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003234
Chris Wilson973d04f2011-07-08 12:22:37 +01003235 if (dev_priv->cfb_plane == plane)
3236 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003237
Jesse Barnesb24e7172011-01-04 15:09:30 -08003238 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003239 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003240 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003241
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003242 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003243 intel_update_fbc(dev);
3244 intel_update_watermarks(dev);
3245 intel_clear_scanline_wait(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003246}
3247
3248static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
3249{
Jesse Barnes79e53942008-11-07 14:24:08 -08003250 /* XXX: When our outputs are all unaware of DPMS modes other than off
3251 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
3252 */
3253 switch (mode) {
3254 case DRM_MODE_DPMS_ON:
3255 case DRM_MODE_DPMS_STANDBY:
3256 case DRM_MODE_DPMS_SUSPEND:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003257 i9xx_crtc_enable(crtc);
3258 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08003259 case DRM_MODE_DPMS_OFF:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003260 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003261 break;
3262 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08003263}
3264
3265/**
3266 * Sets the power management mode of the pipe and plane.
Zhenyu Wang2c072452009-06-05 15:38:42 +08003267 */
3268static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
3269{
3270 struct drm_device *dev = crtc->dev;
Jesse Barnese70236a2009-09-21 10:42:27 -07003271 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003272 struct drm_i915_master_private *master_priv;
3273 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3274 int pipe = intel_crtc->pipe;
3275 bool enabled;
3276
Chris Wilson032d2a02010-09-06 16:17:22 +01003277 if (intel_crtc->dpms_mode == mode)
3278 return;
3279
Chris Wilsondebcadd2010-08-07 11:01:33 +01003280 intel_crtc->dpms_mode = mode;
Chris Wilsondebcadd2010-08-07 11:01:33 +01003281
Jesse Barnese70236a2009-09-21 10:42:27 -07003282 dev_priv->display.dpms(crtc, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08003283
3284 if (!dev->primary->master)
3285 return;
3286
3287 master_priv = dev->primary->master->driver_priv;
3288 if (!master_priv->sarea_priv)
3289 return;
3290
3291 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
3292
3293 switch (pipe) {
3294 case 0:
3295 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3296 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3297 break;
3298 case 1:
3299 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3300 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3301 break;
3302 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003303 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003304 break;
3305 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003306}
3307
Chris Wilsoncdd59982010-09-08 16:30:16 +01003308static void intel_crtc_disable(struct drm_crtc *crtc)
3309{
3310 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
3311 struct drm_device *dev = crtc->dev;
3312
3313 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
3314
3315 if (crtc->fb) {
3316 mutex_lock(&dev->struct_mutex);
3317 i915_gem_object_unpin(to_intel_framebuffer(crtc->fb)->obj);
3318 mutex_unlock(&dev->struct_mutex);
3319 }
3320}
3321
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003322/* Prepare for a mode set.
3323 *
3324 * Note we could be a lot smarter here. We need to figure out which outputs
3325 * will be enabled, which disabled (in short, how the config will changes)
3326 * and perform the minimum necessary steps to accomplish that, e.g. updating
3327 * watermarks, FBC configuration, making sure PLLs are programmed correctly,
3328 * panel fitting is in the proper state, etc.
3329 */
3330static void i9xx_crtc_prepare(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003331{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003332 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003333}
3334
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003335static void i9xx_crtc_commit(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003336{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003337 i9xx_crtc_enable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003338}
3339
3340static void ironlake_crtc_prepare(struct drm_crtc *crtc)
3341{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003342 ironlake_crtc_disable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003343}
3344
3345static void ironlake_crtc_commit(struct drm_crtc *crtc)
3346{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003347 ironlake_crtc_enable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003348}
3349
Akshay Joshi0206e352011-08-16 15:34:10 -04003350void intel_encoder_prepare(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003351{
3352 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3353 /* lvds has its own version of prepare see intel_lvds_prepare */
3354 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
3355}
3356
Akshay Joshi0206e352011-08-16 15:34:10 -04003357void intel_encoder_commit(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003358{
3359 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
Jesse Barnesd4270e52011-10-11 10:43:02 -07003360 struct drm_device *dev = encoder->dev;
3361 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3362 struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
3363
Jesse Barnes79e53942008-11-07 14:24:08 -08003364 /* lvds has its own version of commit see intel_lvds_commit */
3365 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003366
3367 if (HAS_PCH_CPT(dev))
3368 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08003369}
3370
Chris Wilsonea5b2132010-08-04 13:50:23 +01003371void intel_encoder_destroy(struct drm_encoder *encoder)
3372{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003373 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003374
Chris Wilsonea5b2132010-08-04 13:50:23 +01003375 drm_encoder_cleanup(encoder);
3376 kfree(intel_encoder);
3377}
3378
Jesse Barnes79e53942008-11-07 14:24:08 -08003379static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
3380 struct drm_display_mode *mode,
3381 struct drm_display_mode *adjusted_mode)
3382{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003383 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01003384
Eric Anholtbad720f2009-10-22 16:11:14 -07003385 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08003386 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07003387 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3388 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003389 }
Chris Wilson89749352010-09-12 18:25:19 +01003390
3391 /* XXX some encoders set the crtcinfo, others don't.
3392 * Obviously we need some form of conflict resolution here...
3393 */
3394 if (adjusted_mode->crtc_htotal == 0)
3395 drm_mode_set_crtcinfo(adjusted_mode, 0);
3396
Jesse Barnes79e53942008-11-07 14:24:08 -08003397 return true;
3398}
3399
Jesse Barnese70236a2009-09-21 10:42:27 -07003400static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003401{
Jesse Barnese70236a2009-09-21 10:42:27 -07003402 return 400000;
3403}
Jesse Barnes79e53942008-11-07 14:24:08 -08003404
Jesse Barnese70236a2009-09-21 10:42:27 -07003405static int i915_get_display_clock_speed(struct drm_device *dev)
3406{
3407 return 333000;
3408}
Jesse Barnes79e53942008-11-07 14:24:08 -08003409
Jesse Barnese70236a2009-09-21 10:42:27 -07003410static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3411{
3412 return 200000;
3413}
Jesse Barnes79e53942008-11-07 14:24:08 -08003414
Jesse Barnese70236a2009-09-21 10:42:27 -07003415static int i915gm_get_display_clock_speed(struct drm_device *dev)
3416{
3417 u16 gcfgc = 0;
3418
3419 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3420
3421 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08003422 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07003423 else {
3424 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3425 case GC_DISPLAY_CLOCK_333_MHZ:
3426 return 333000;
3427 default:
3428 case GC_DISPLAY_CLOCK_190_200_MHZ:
3429 return 190000;
3430 }
3431 }
3432}
Jesse Barnes79e53942008-11-07 14:24:08 -08003433
Jesse Barnese70236a2009-09-21 10:42:27 -07003434static int i865_get_display_clock_speed(struct drm_device *dev)
3435{
3436 return 266000;
3437}
3438
3439static int i855_get_display_clock_speed(struct drm_device *dev)
3440{
3441 u16 hpllcc = 0;
3442 /* Assume that the hardware is in the high speed state. This
3443 * should be the default.
3444 */
3445 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3446 case GC_CLOCK_133_200:
3447 case GC_CLOCK_100_200:
3448 return 200000;
3449 case GC_CLOCK_166_250:
3450 return 250000;
3451 case GC_CLOCK_100_133:
3452 return 133000;
3453 }
3454
3455 /* Shouldn't happen */
3456 return 0;
3457}
3458
3459static int i830_get_display_clock_speed(struct drm_device *dev)
3460{
3461 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08003462}
3463
Zhenyu Wang2c072452009-06-05 15:38:42 +08003464struct fdi_m_n {
3465 u32 tu;
3466 u32 gmch_m;
3467 u32 gmch_n;
3468 u32 link_m;
3469 u32 link_n;
3470};
3471
3472static void
3473fdi_reduce_ratio(u32 *num, u32 *den)
3474{
3475 while (*num > 0xffffff || *den > 0xffffff) {
3476 *num >>= 1;
3477 *den >>= 1;
3478 }
3479}
3480
Zhenyu Wang2c072452009-06-05 15:38:42 +08003481static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003482ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
3483 int link_clock, struct fdi_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003484{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003485 m_n->tu = 64; /* default size */
3486
Chris Wilson22ed1112010-12-04 01:01:29 +00003487 /* BUG_ON(pixel_clock > INT_MAX / 36); */
3488 m_n->gmch_m = bits_per_pixel * pixel_clock;
3489 m_n->gmch_n = link_clock * nlanes * 8;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003490 fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
3491
Chris Wilson22ed1112010-12-04 01:01:29 +00003492 m_n->link_m = pixel_clock;
3493 m_n->link_n = link_clock;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003494 fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
3495}
3496
3497
Shaohua Li7662c8b2009-06-26 11:23:55 +08003498struct intel_watermark_params {
3499 unsigned long fifo_size;
3500 unsigned long max_wm;
3501 unsigned long default_wm;
3502 unsigned long guard_size;
3503 unsigned long cacheline_size;
3504};
3505
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003506/* Pineview has different values for various configs */
Chris Wilsond2102462011-01-24 17:43:27 +00003507static const struct intel_watermark_params pineview_display_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003508 PINEVIEW_DISPLAY_FIFO,
3509 PINEVIEW_MAX_WM,
3510 PINEVIEW_DFT_WM,
3511 PINEVIEW_GUARD_WM,
3512 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08003513};
Chris Wilsond2102462011-01-24 17:43:27 +00003514static const struct intel_watermark_params pineview_display_hplloff_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003515 PINEVIEW_DISPLAY_FIFO,
3516 PINEVIEW_MAX_WM,
3517 PINEVIEW_DFT_HPLLOFF_WM,
3518 PINEVIEW_GUARD_WM,
3519 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08003520};
Chris Wilsond2102462011-01-24 17:43:27 +00003521static const struct intel_watermark_params pineview_cursor_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003522 PINEVIEW_CURSOR_FIFO,
3523 PINEVIEW_CURSOR_MAX_WM,
3524 PINEVIEW_CURSOR_DFT_WM,
3525 PINEVIEW_CURSOR_GUARD_WM,
3526 PINEVIEW_FIFO_LINE_SIZE,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003527};
Chris Wilsond2102462011-01-24 17:43:27 +00003528static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003529 PINEVIEW_CURSOR_FIFO,
3530 PINEVIEW_CURSOR_MAX_WM,
3531 PINEVIEW_CURSOR_DFT_WM,
3532 PINEVIEW_CURSOR_GUARD_WM,
3533 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08003534};
Chris Wilsond2102462011-01-24 17:43:27 +00003535static const struct intel_watermark_params g4x_wm_info = {
Jesse Barnes0e442c62009-10-19 10:09:33 +09003536 G4X_FIFO_SIZE,
3537 G4X_MAX_WM,
3538 G4X_MAX_WM,
3539 2,
3540 G4X_FIFO_LINE_SIZE,
3541};
Chris Wilsond2102462011-01-24 17:43:27 +00003542static const struct intel_watermark_params g4x_cursor_wm_info = {
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003543 I965_CURSOR_FIFO,
3544 I965_CURSOR_MAX_WM,
3545 I965_CURSOR_DFT_WM,
3546 2,
3547 G4X_FIFO_LINE_SIZE,
3548};
Chris Wilsond2102462011-01-24 17:43:27 +00003549static const struct intel_watermark_params i965_cursor_wm_info = {
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003550 I965_CURSOR_FIFO,
3551 I965_CURSOR_MAX_WM,
3552 I965_CURSOR_DFT_WM,
3553 2,
3554 I915_FIFO_LINE_SIZE,
3555};
Chris Wilsond2102462011-01-24 17:43:27 +00003556static const struct intel_watermark_params i945_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003557 I945_FIFO_SIZE,
3558 I915_MAX_WM,
3559 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003560 2,
3561 I915_FIFO_LINE_SIZE
3562};
Chris Wilsond2102462011-01-24 17:43:27 +00003563static const struct intel_watermark_params i915_wm_info = {
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003564 I915_FIFO_SIZE,
3565 I915_MAX_WM,
3566 1,
3567 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003568 I915_FIFO_LINE_SIZE
3569};
Chris Wilsond2102462011-01-24 17:43:27 +00003570static const struct intel_watermark_params i855_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003571 I855GM_FIFO_SIZE,
3572 I915_MAX_WM,
3573 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003574 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003575 I830_FIFO_LINE_SIZE
3576};
Chris Wilsond2102462011-01-24 17:43:27 +00003577static const struct intel_watermark_params i830_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003578 I830_FIFO_SIZE,
3579 I915_MAX_WM,
3580 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003581 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003582 I830_FIFO_LINE_SIZE
3583};
3584
Chris Wilsond2102462011-01-24 17:43:27 +00003585static const struct intel_watermark_params ironlake_display_wm_info = {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003586 ILK_DISPLAY_FIFO,
3587 ILK_DISPLAY_MAXWM,
3588 ILK_DISPLAY_DFTWM,
3589 2,
3590 ILK_FIFO_LINE_SIZE
3591};
Chris Wilsond2102462011-01-24 17:43:27 +00003592static const struct intel_watermark_params ironlake_cursor_wm_info = {
Zhao Yakuic936f442010-06-12 14:32:26 +08003593 ILK_CURSOR_FIFO,
3594 ILK_CURSOR_MAXWM,
3595 ILK_CURSOR_DFTWM,
3596 2,
3597 ILK_FIFO_LINE_SIZE
3598};
Chris Wilsond2102462011-01-24 17:43:27 +00003599static const struct intel_watermark_params ironlake_display_srwm_info = {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003600 ILK_DISPLAY_SR_FIFO,
3601 ILK_DISPLAY_MAX_SRWM,
3602 ILK_DISPLAY_DFT_SRWM,
3603 2,
3604 ILK_FIFO_LINE_SIZE
3605};
Chris Wilsond2102462011-01-24 17:43:27 +00003606static const struct intel_watermark_params ironlake_cursor_srwm_info = {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003607 ILK_CURSOR_SR_FIFO,
3608 ILK_CURSOR_MAX_SRWM,
3609 ILK_CURSOR_DFT_SRWM,
3610 2,
3611 ILK_FIFO_LINE_SIZE
3612};
3613
Chris Wilsond2102462011-01-24 17:43:27 +00003614static const struct intel_watermark_params sandybridge_display_wm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003615 SNB_DISPLAY_FIFO,
3616 SNB_DISPLAY_MAXWM,
3617 SNB_DISPLAY_DFTWM,
3618 2,
3619 SNB_FIFO_LINE_SIZE
3620};
Chris Wilsond2102462011-01-24 17:43:27 +00003621static const struct intel_watermark_params sandybridge_cursor_wm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003622 SNB_CURSOR_FIFO,
3623 SNB_CURSOR_MAXWM,
3624 SNB_CURSOR_DFTWM,
3625 2,
3626 SNB_FIFO_LINE_SIZE
3627};
Chris Wilsond2102462011-01-24 17:43:27 +00003628static const struct intel_watermark_params sandybridge_display_srwm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003629 SNB_DISPLAY_SR_FIFO,
3630 SNB_DISPLAY_MAX_SRWM,
3631 SNB_DISPLAY_DFT_SRWM,
3632 2,
3633 SNB_FIFO_LINE_SIZE
3634};
Chris Wilsond2102462011-01-24 17:43:27 +00003635static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003636 SNB_CURSOR_SR_FIFO,
3637 SNB_CURSOR_MAX_SRWM,
3638 SNB_CURSOR_DFT_SRWM,
3639 2,
3640 SNB_FIFO_LINE_SIZE
3641};
3642
3643
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003644/**
3645 * intel_calculate_wm - calculate watermark level
3646 * @clock_in_khz: pixel clock
3647 * @wm: chip FIFO params
3648 * @pixel_size: display pixel size
3649 * @latency_ns: memory latency for the platform
3650 *
3651 * Calculate the watermark level (the level at which the display plane will
3652 * start fetching from memory again). Each chip has a different display
3653 * FIFO size and allocation, so the caller needs to figure that out and pass
3654 * in the correct intel_watermark_params structure.
3655 *
3656 * As the pixel clock runs, the FIFO will be drained at a rate that depends
3657 * on the pixel size. When it reaches the watermark level, it'll start
3658 * fetching FIFO line sized based chunks from memory until the FIFO fills
3659 * past the watermark point. If the FIFO drains completely, a FIFO underrun
3660 * will occur, and a display engine hang could result.
3661 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003662static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
Chris Wilsond2102462011-01-24 17:43:27 +00003663 const struct intel_watermark_params *wm,
3664 int fifo_size,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003665 int pixel_size,
3666 unsigned long latency_ns)
3667{
Jesse Barnes390c4dd2009-07-16 13:01:01 -07003668 long entries_required, wm_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003669
Jesse Barnesd6604672009-09-11 12:25:56 -07003670 /*
3671 * Note: we need to make sure we don't overflow for various clock &
3672 * latency values.
3673 * clocks go from a few thousand to several hundred thousand.
3674 * latency is usually a few thousand
3675 */
3676 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
3677 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003678 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003679
Joe Perchesbbb0aef52011-04-17 20:35:52 -07003680 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003681
Chris Wilsond2102462011-01-24 17:43:27 +00003682 wm_size = fifo_size - (entries_required + wm->guard_size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003683
Joe Perchesbbb0aef52011-04-17 20:35:52 -07003684 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003685
Jesse Barnes390c4dd2009-07-16 13:01:01 -07003686 /* Don't promote wm_size to unsigned... */
3687 if (wm_size > (long)wm->max_wm)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003688 wm_size = wm->max_wm;
Chris Wilsonc3add4b2010-09-08 09:14:08 +01003689 if (wm_size <= 0)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003690 wm_size = wm->default_wm;
3691 return wm_size;
3692}
3693
3694struct cxsr_latency {
3695 int is_desktop;
Li Peng95534262010-05-18 18:58:44 +08003696 int is_ddr3;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003697 unsigned long fsb_freq;
3698 unsigned long mem_freq;
3699 unsigned long display_sr;
3700 unsigned long display_hpll_disable;
3701 unsigned long cursor_sr;
3702 unsigned long cursor_hpll_disable;
3703};
3704
Chris Wilson403c89f2010-08-04 15:25:31 +01003705static const struct cxsr_latency cxsr_latency_table[] = {
Li Peng95534262010-05-18 18:58:44 +08003706 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
3707 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
3708 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
3709 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
3710 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003711
Li Peng95534262010-05-18 18:58:44 +08003712 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
3713 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
3714 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
3715 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
3716 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003717
Li Peng95534262010-05-18 18:58:44 +08003718 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
3719 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
3720 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
3721 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
3722 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003723
Li Peng95534262010-05-18 18:58:44 +08003724 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
3725 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
3726 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
3727 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
3728 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003729
Li Peng95534262010-05-18 18:58:44 +08003730 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
3731 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
3732 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
3733 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
3734 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003735
Li Peng95534262010-05-18 18:58:44 +08003736 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
3737 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
3738 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
3739 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
3740 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003741};
3742
Chris Wilson403c89f2010-08-04 15:25:31 +01003743static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
3744 int is_ddr3,
3745 int fsb,
3746 int mem)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003747{
Chris Wilson403c89f2010-08-04 15:25:31 +01003748 const struct cxsr_latency *latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003749 int i;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003750
3751 if (fsb == 0 || mem == 0)
3752 return NULL;
3753
3754 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
3755 latency = &cxsr_latency_table[i];
3756 if (is_desktop == latency->is_desktop &&
Li Peng95534262010-05-18 18:58:44 +08003757 is_ddr3 == latency->is_ddr3 &&
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303758 fsb == latency->fsb_freq && mem == latency->mem_freq)
3759 return latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003760 }
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303761
Zhao Yakui28c97732009-10-09 11:39:41 +08003762 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303763
3764 return NULL;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003765}
3766
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003767static void pineview_disable_cxsr(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003768{
3769 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003770
3771 /* deactivate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01003772 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003773}
3774
Jesse Barnesbcc24fb2009-08-31 10:24:31 -07003775/*
3776 * Latency for FIFO fetches is dependent on several factors:
3777 * - memory configuration (speed, channels)
3778 * - chipset
3779 * - current MCH state
3780 * It can be fairly high in some situations, so here we assume a fairly
3781 * pessimal value. It's a tradeoff between extra memory fetches (if we
3782 * set this value too high, the FIFO will fetch frequently to stay full)
3783 * and power consumption (set it too low to save power and we might see
3784 * FIFO underruns and display "flicker").
3785 *
3786 * A value of 5us seems to be a good balance; safe for very low end
3787 * platforms but not overly aggressive on lower latency configs.
3788 */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003789static const int latency_ns = 5000;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003790
Jesse Barnese70236a2009-09-21 10:42:27 -07003791static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003792{
3793 struct drm_i915_private *dev_priv = dev->dev_private;
3794 uint32_t dsparb = I915_READ(DSPARB);
3795 int size;
3796
Chris Wilson8de9b312010-07-19 19:59:52 +01003797 size = dsparb & 0x7f;
3798 if (plane)
3799 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003800
Zhao Yakui28c97732009-10-09 11:39:41 +08003801 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003802 plane ? "B" : "A", size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003803
3804 return size;
3805}
Shaohua Li7662c8b2009-06-26 11:23:55 +08003806
Jesse Barnese70236a2009-09-21 10:42:27 -07003807static int i85x_get_fifo_size(struct drm_device *dev, int plane)
3808{
3809 struct drm_i915_private *dev_priv = dev->dev_private;
3810 uint32_t dsparb = I915_READ(DSPARB);
3811 int size;
3812
Chris Wilson8de9b312010-07-19 19:59:52 +01003813 size = dsparb & 0x1ff;
3814 if (plane)
3815 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
Jesse Barnese70236a2009-09-21 10:42:27 -07003816 size >>= 1; /* Convert to cachelines */
3817
Zhao Yakui28c97732009-10-09 11:39:41 +08003818 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003819 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003820
3821 return size;
3822}
3823
3824static int i845_get_fifo_size(struct drm_device *dev, int plane)
3825{
3826 struct drm_i915_private *dev_priv = dev->dev_private;
3827 uint32_t dsparb = I915_READ(DSPARB);
3828 int size;
3829
3830 size = dsparb & 0x7f;
3831 size >>= 2; /* Convert to cachelines */
3832
Zhao Yakui28c97732009-10-09 11:39:41 +08003833 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003834 plane ? "B" : "A",
3835 size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003836
3837 return size;
3838}
3839
3840static int i830_get_fifo_size(struct drm_device *dev, int plane)
3841{
3842 struct drm_i915_private *dev_priv = dev->dev_private;
3843 uint32_t dsparb = I915_READ(DSPARB);
3844 int size;
3845
3846 size = dsparb & 0x7f;
3847 size >>= 1; /* Convert to cachelines */
3848
Zhao Yakui28c97732009-10-09 11:39:41 +08003849 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003850 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003851
3852 return size;
3853}
3854
Chris Wilsond2102462011-01-24 17:43:27 +00003855static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
3856{
3857 struct drm_crtc *crtc, *enabled = NULL;
3858
3859 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3860 if (crtc->enabled && crtc->fb) {
3861 if (enabled)
3862 return NULL;
3863 enabled = crtc;
3864 }
3865 }
3866
3867 return enabled;
3868}
3869
3870static void pineview_update_wm(struct drm_device *dev)
Zhao Yakuid4294342010-03-22 22:45:36 +08003871{
3872 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00003873 struct drm_crtc *crtc;
Chris Wilson403c89f2010-08-04 15:25:31 +01003874 const struct cxsr_latency *latency;
Zhao Yakuid4294342010-03-22 22:45:36 +08003875 u32 reg;
3876 unsigned long wm;
Zhao Yakuid4294342010-03-22 22:45:36 +08003877
Chris Wilson403c89f2010-08-04 15:25:31 +01003878 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
Li Peng95534262010-05-18 18:58:44 +08003879 dev_priv->fsb_freq, dev_priv->mem_freq);
Zhao Yakuid4294342010-03-22 22:45:36 +08003880 if (!latency) {
3881 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
3882 pineview_disable_cxsr(dev);
3883 return;
3884 }
3885
Chris Wilsond2102462011-01-24 17:43:27 +00003886 crtc = single_enabled_crtc(dev);
3887 if (crtc) {
3888 int clock = crtc->mode.clock;
3889 int pixel_size = crtc->fb->bits_per_pixel / 8;
Zhao Yakuid4294342010-03-22 22:45:36 +08003890
3891 /* Display SR */
Chris Wilsond2102462011-01-24 17:43:27 +00003892 wm = intel_calculate_wm(clock, &pineview_display_wm,
3893 pineview_display_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08003894 pixel_size, latency->display_sr);
3895 reg = I915_READ(DSPFW1);
3896 reg &= ~DSPFW_SR_MASK;
3897 reg |= wm << DSPFW_SR_SHIFT;
3898 I915_WRITE(DSPFW1, reg);
3899 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
3900
3901 /* cursor SR */
Chris Wilsond2102462011-01-24 17:43:27 +00003902 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
3903 pineview_display_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08003904 pixel_size, latency->cursor_sr);
3905 reg = I915_READ(DSPFW3);
3906 reg &= ~DSPFW_CURSOR_SR_MASK;
3907 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
3908 I915_WRITE(DSPFW3, reg);
3909
3910 /* Display HPLL off SR */
Chris Wilsond2102462011-01-24 17:43:27 +00003911 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
3912 pineview_display_hplloff_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08003913 pixel_size, latency->display_hpll_disable);
3914 reg = I915_READ(DSPFW3);
3915 reg &= ~DSPFW_HPLL_SR_MASK;
3916 reg |= wm & DSPFW_HPLL_SR_MASK;
3917 I915_WRITE(DSPFW3, reg);
3918
3919 /* cursor HPLL off SR */
Chris Wilsond2102462011-01-24 17:43:27 +00003920 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
3921 pineview_display_hplloff_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08003922 pixel_size, latency->cursor_hpll_disable);
3923 reg = I915_READ(DSPFW3);
3924 reg &= ~DSPFW_HPLL_CURSOR_MASK;
3925 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
3926 I915_WRITE(DSPFW3, reg);
3927 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
3928
3929 /* activate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01003930 I915_WRITE(DSPFW3,
3931 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
Zhao Yakuid4294342010-03-22 22:45:36 +08003932 DRM_DEBUG_KMS("Self-refresh is enabled\n");
3933 } else {
3934 pineview_disable_cxsr(dev);
3935 DRM_DEBUG_KMS("Self-refresh is disabled\n");
3936 }
3937}
3938
Chris Wilson417ae142011-01-19 15:04:42 +00003939static bool g4x_compute_wm0(struct drm_device *dev,
3940 int plane,
3941 const struct intel_watermark_params *display,
3942 int display_latency_ns,
3943 const struct intel_watermark_params *cursor,
3944 int cursor_latency_ns,
3945 int *plane_wm,
3946 int *cursor_wm)
Jesse Barnes652c3932009-08-17 13:31:43 -07003947{
Chris Wilson417ae142011-01-19 15:04:42 +00003948 struct drm_crtc *crtc;
3949 int htotal, hdisplay, clock, pixel_size;
3950 int line_time_us, line_count;
3951 int entries, tlb_miss;
Jesse Barnes652c3932009-08-17 13:31:43 -07003952
Chris Wilson417ae142011-01-19 15:04:42 +00003953 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson5c72d062011-04-13 09:28:23 +01003954 if (crtc->fb == NULL || !crtc->enabled) {
3955 *cursor_wm = cursor->guard_size;
3956 *plane_wm = display->guard_size;
Chris Wilson417ae142011-01-19 15:04:42 +00003957 return false;
Chris Wilson5c72d062011-04-13 09:28:23 +01003958 }
Jesse Barnes0e442c62009-10-19 10:09:33 +09003959
Chris Wilson417ae142011-01-19 15:04:42 +00003960 htotal = crtc->mode.htotal;
3961 hdisplay = crtc->mode.hdisplay;
3962 clock = crtc->mode.clock;
3963 pixel_size = crtc->fb->bits_per_pixel / 8;
Jesse Barnes0e442c62009-10-19 10:09:33 +09003964
Chris Wilson417ae142011-01-19 15:04:42 +00003965 /* Use the small buffer method to calculate plane watermark */
3966 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
3967 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
3968 if (tlb_miss > 0)
3969 entries += tlb_miss;
3970 entries = DIV_ROUND_UP(entries, display->cacheline_size);
3971 *plane_wm = entries + display->guard_size;
3972 if (*plane_wm > (int)display->max_wm)
3973 *plane_wm = display->max_wm;
Jesse Barnes0e442c62009-10-19 10:09:33 +09003974
Chris Wilson417ae142011-01-19 15:04:42 +00003975 /* Use the large buffer method to calculate cursor watermark */
3976 line_time_us = ((htotal * 1000) / clock);
3977 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
3978 entries = line_count * 64 * pixel_size;
3979 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
3980 if (tlb_miss > 0)
3981 entries += tlb_miss;
3982 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
3983 *cursor_wm = entries + cursor->guard_size;
3984 if (*cursor_wm > (int)cursor->max_wm)
3985 *cursor_wm = (int)cursor->max_wm;
Jesse Barnes0e442c62009-10-19 10:09:33 +09003986
Chris Wilson417ae142011-01-19 15:04:42 +00003987 return true;
3988}
Jesse Barnes0e442c62009-10-19 10:09:33 +09003989
Chris Wilson417ae142011-01-19 15:04:42 +00003990/*
3991 * Check the wm result.
3992 *
3993 * If any calculated watermark values is larger than the maximum value that
3994 * can be programmed into the associated watermark register, that watermark
3995 * must be disabled.
3996 */
3997static bool g4x_check_srwm(struct drm_device *dev,
3998 int display_wm, int cursor_wm,
3999 const struct intel_watermark_params *display,
4000 const struct intel_watermark_params *cursor)
4001{
4002 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
4003 display_wm, cursor_wm);
Jesse Barnes0e442c62009-10-19 10:09:33 +09004004
Chris Wilson417ae142011-01-19 15:04:42 +00004005 if (display_wm > display->max_wm) {
Joe Perchesbbb0aef52011-04-17 20:35:52 -07004006 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
Chris Wilson417ae142011-01-19 15:04:42 +00004007 display_wm, display->max_wm);
4008 return false;
Jesse Barnes0e442c62009-10-19 10:09:33 +09004009 }
4010
Chris Wilson417ae142011-01-19 15:04:42 +00004011 if (cursor_wm > cursor->max_wm) {
Joe Perchesbbb0aef52011-04-17 20:35:52 -07004012 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
Chris Wilson417ae142011-01-19 15:04:42 +00004013 cursor_wm, cursor->max_wm);
4014 return false;
4015 }
Jesse Barnes0e442c62009-10-19 10:09:33 +09004016
Chris Wilson417ae142011-01-19 15:04:42 +00004017 if (!(display_wm || cursor_wm)) {
4018 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
4019 return false;
4020 }
Jesse Barnes0e442c62009-10-19 10:09:33 +09004021
Chris Wilson417ae142011-01-19 15:04:42 +00004022 return true;
4023}
4024
4025static bool g4x_compute_srwm(struct drm_device *dev,
Chris Wilsond2102462011-01-24 17:43:27 +00004026 int plane,
4027 int latency_ns,
Chris Wilson417ae142011-01-19 15:04:42 +00004028 const struct intel_watermark_params *display,
4029 const struct intel_watermark_params *cursor,
4030 int *display_wm, int *cursor_wm)
4031{
Chris Wilsond2102462011-01-24 17:43:27 +00004032 struct drm_crtc *crtc;
4033 int hdisplay, htotal, pixel_size, clock;
Chris Wilson417ae142011-01-19 15:04:42 +00004034 unsigned long line_time_us;
4035 int line_count, line_size;
4036 int small, large;
4037 int entries;
4038
4039 if (!latency_ns) {
4040 *display_wm = *cursor_wm = 0;
4041 return false;
4042 }
4043
Chris Wilsond2102462011-01-24 17:43:27 +00004044 crtc = intel_get_crtc_for_plane(dev, plane);
4045 hdisplay = crtc->mode.hdisplay;
4046 htotal = crtc->mode.htotal;
4047 clock = crtc->mode.clock;
4048 pixel_size = crtc->fb->bits_per_pixel / 8;
4049
Chris Wilson417ae142011-01-19 15:04:42 +00004050 line_time_us = (htotal * 1000) / clock;
4051 line_count = (latency_ns / line_time_us + 1000) / 1000;
4052 line_size = hdisplay * pixel_size;
4053
4054 /* Use the minimum of the small and large buffer method for primary */
4055 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
4056 large = line_count * line_size;
4057
4058 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
4059 *display_wm = entries + display->guard_size;
4060
4061 /* calculate the self-refresh watermark for display cursor */
4062 entries = line_count * pixel_size * 64;
4063 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4064 *cursor_wm = entries + cursor->guard_size;
4065
4066 return g4x_check_srwm(dev,
4067 *display_wm, *cursor_wm,
4068 display, cursor);
4069}
4070
Yuanhan Liu7ccb4a52011-03-18 07:37:35 +00004071#define single_plane_enabled(mask) is_power_of_2(mask)
Chris Wilsond2102462011-01-24 17:43:27 +00004072
4073static void g4x_update_wm(struct drm_device *dev)
Chris Wilson417ae142011-01-19 15:04:42 +00004074{
4075 static const int sr_latency_ns = 12000;
4076 struct drm_i915_private *dev_priv = dev->dev_private;
4077 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
Chris Wilsond2102462011-01-24 17:43:27 +00004078 int plane_sr, cursor_sr;
4079 unsigned int enabled = 0;
Chris Wilson417ae142011-01-19 15:04:42 +00004080
4081 if (g4x_compute_wm0(dev, 0,
4082 &g4x_wm_info, latency_ns,
4083 &g4x_cursor_wm_info, latency_ns,
4084 &planea_wm, &cursora_wm))
Chris Wilsond2102462011-01-24 17:43:27 +00004085 enabled |= 1;
Chris Wilson417ae142011-01-19 15:04:42 +00004086
4087 if (g4x_compute_wm0(dev, 1,
4088 &g4x_wm_info, latency_ns,
4089 &g4x_cursor_wm_info, latency_ns,
4090 &planeb_wm, &cursorb_wm))
Chris Wilsond2102462011-01-24 17:43:27 +00004091 enabled |= 2;
Chris Wilson417ae142011-01-19 15:04:42 +00004092
4093 plane_sr = cursor_sr = 0;
Chris Wilsond2102462011-01-24 17:43:27 +00004094 if (single_plane_enabled(enabled) &&
4095 g4x_compute_srwm(dev, ffs(enabled) - 1,
4096 sr_latency_ns,
Chris Wilson417ae142011-01-19 15:04:42 +00004097 &g4x_wm_info,
4098 &g4x_cursor_wm_info,
4099 &plane_sr, &cursor_sr))
4100 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
4101 else
4102 I915_WRITE(FW_BLC_SELF,
4103 I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
4104
Chris Wilson308977a2011-02-02 10:41:20 +00004105 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
4106 planea_wm, cursora_wm,
4107 planeb_wm, cursorb_wm,
4108 plane_sr, cursor_sr);
Chris Wilson417ae142011-01-19 15:04:42 +00004109
4110 I915_WRITE(DSPFW1,
4111 (plane_sr << DSPFW_SR_SHIFT) |
Jesse Barnes0e442c62009-10-19 10:09:33 +09004112 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
Chris Wilson417ae142011-01-19 15:04:42 +00004113 (planeb_wm << DSPFW_PLANEB_SHIFT) |
4114 planea_wm);
4115 I915_WRITE(DSPFW2,
4116 (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
Jesse Barnes0e442c62009-10-19 10:09:33 +09004117 (cursora_wm << DSPFW_CURSORA_SHIFT));
4118 /* HPLL off in SR has some issues on G4x... disable it */
Chris Wilson417ae142011-01-19 15:04:42 +00004119 I915_WRITE(DSPFW3,
4120 (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
Jesse Barnes0e442c62009-10-19 10:09:33 +09004121 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Jesse Barnes652c3932009-08-17 13:31:43 -07004122}
4123
Chris Wilsond2102462011-01-24 17:43:27 +00004124static void i965_update_wm(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08004125{
4126 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004127 struct drm_crtc *crtc;
4128 int srwm = 1;
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004129 int cursor_sr = 16;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004130
Jesse Barnes1dc75462009-10-19 10:08:17 +09004131 /* Calc sr entries for one plane configs */
Chris Wilsond2102462011-01-24 17:43:27 +00004132 crtc = single_enabled_crtc(dev);
4133 if (crtc) {
Jesse Barnes1dc75462009-10-19 10:08:17 +09004134 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01004135 static const int sr_latency_ns = 12000;
Chris Wilsond2102462011-01-24 17:43:27 +00004136 int clock = crtc->mode.clock;
4137 int htotal = crtc->mode.htotal;
4138 int hdisplay = crtc->mode.hdisplay;
4139 int pixel_size = crtc->fb->bits_per_pixel / 8;
4140 unsigned long line_time_us;
4141 int entries;
Jesse Barnes1dc75462009-10-19 10:08:17 +09004142
Chris Wilsond2102462011-01-24 17:43:27 +00004143 line_time_us = ((htotal * 1000) / clock);
Jesse Barnes1dc75462009-10-19 10:08:17 +09004144
4145 /* Use ns/us then divide to preserve precision */
Chris Wilsond2102462011-01-24 17:43:27 +00004146 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
4147 pixel_size * hdisplay;
4148 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
Chris Wilsond2102462011-01-24 17:43:27 +00004149 srwm = I965_FIFO_SIZE - entries;
Jesse Barnes1dc75462009-10-19 10:08:17 +09004150 if (srwm < 0)
4151 srwm = 1;
Zhao Yakui1b07e042010-06-12 14:32:24 +08004152 srwm &= 0x1ff;
Chris Wilson308977a2011-02-02 10:41:20 +00004153 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
4154 entries, srwm);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004155
Chris Wilsond2102462011-01-24 17:43:27 +00004156 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01004157 pixel_size * 64;
Chris Wilsond2102462011-01-24 17:43:27 +00004158 entries = DIV_ROUND_UP(entries,
Chris Wilson8de9b312010-07-19 19:59:52 +01004159 i965_cursor_wm_info.cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004160 cursor_sr = i965_cursor_wm_info.fifo_size -
Chris Wilsond2102462011-01-24 17:43:27 +00004161 (entries + i965_cursor_wm_info.guard_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004162
4163 if (cursor_sr > i965_cursor_wm_info.max_wm)
4164 cursor_sr = i965_cursor_wm_info.max_wm;
4165
4166 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
4167 "cursor %d\n", srwm, cursor_sr);
4168
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004169 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07004170 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
David John33c5fd12010-01-27 15:19:08 +05304171 } else {
4172 /* Turn off self refresh if both pipes are enabled */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004173 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07004174 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
4175 & ~FW_BLC_SELF_EN);
Jesse Barnes1dc75462009-10-19 10:08:17 +09004176 }
4177
4178 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
4179 srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004180
4181 /* 965 has limitations... */
Chris Wilson417ae142011-01-19 15:04:42 +00004182 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
4183 (8 << 16) | (8 << 8) | (8 << 0));
Shaohua Li7662c8b2009-06-26 11:23:55 +08004184 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004185 /* update cursor SR watermark */
4186 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Shaohua Li7662c8b2009-06-26 11:23:55 +08004187}
4188
Chris Wilsond2102462011-01-24 17:43:27 +00004189static void i9xx_update_wm(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08004190{
4191 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004192 const struct intel_watermark_params *wm_info;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004193 uint32_t fwater_lo;
4194 uint32_t fwater_hi;
Chris Wilsond2102462011-01-24 17:43:27 +00004195 int cwm, srwm = 1;
4196 int fifo_size;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004197 int planea_wm, planeb_wm;
Chris Wilsond2102462011-01-24 17:43:27 +00004198 struct drm_crtc *crtc, *enabled = NULL;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004199
Chris Wilson72557b42011-01-31 10:29:55 +00004200 if (IS_I945GM(dev))
Chris Wilsond2102462011-01-24 17:43:27 +00004201 wm_info = &i945_wm_info;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004202 else if (!IS_GEN2(dev))
Chris Wilsond2102462011-01-24 17:43:27 +00004203 wm_info = &i915_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004204 else
Chris Wilsond2102462011-01-24 17:43:27 +00004205 wm_info = &i855_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004206
Chris Wilsond2102462011-01-24 17:43:27 +00004207 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
4208 crtc = intel_get_crtc_for_plane(dev, 0);
4209 if (crtc->enabled && crtc->fb) {
4210 planea_wm = intel_calculate_wm(crtc->mode.clock,
4211 wm_info, fifo_size,
4212 crtc->fb->bits_per_pixel / 8,
4213 latency_ns);
4214 enabled = crtc;
4215 } else
4216 planea_wm = fifo_size - wm_info->guard_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004217
Chris Wilsond2102462011-01-24 17:43:27 +00004218 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
4219 crtc = intel_get_crtc_for_plane(dev, 1);
4220 if (crtc->enabled && crtc->fb) {
4221 planeb_wm = intel_calculate_wm(crtc->mode.clock,
4222 wm_info, fifo_size,
4223 crtc->fb->bits_per_pixel / 8,
4224 latency_ns);
4225 if (enabled == NULL)
4226 enabled = crtc;
4227 else
4228 enabled = NULL;
4229 } else
4230 planeb_wm = fifo_size - wm_info->guard_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004231
Zhao Yakui28c97732009-10-09 11:39:41 +08004232 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004233
4234 /*
4235 * Overlay gets an aggressive default since video jitter is bad.
4236 */
4237 cwm = 2;
4238
Alexander Lam18b21902011-01-03 13:28:56 -05004239 /* Play safe and disable self-refresh before adjusting watermarks. */
4240 if (IS_I945G(dev) || IS_I945GM(dev))
4241 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
4242 else if (IS_I915GM(dev))
4243 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
4244
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004245 /* Calc sr entries for one plane configs */
Chris Wilsond2102462011-01-24 17:43:27 +00004246 if (HAS_FW_BLC(dev) && enabled) {
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004247 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01004248 static const int sr_latency_ns = 6000;
Chris Wilsond2102462011-01-24 17:43:27 +00004249 int clock = enabled->mode.clock;
4250 int htotal = enabled->mode.htotal;
4251 int hdisplay = enabled->mode.hdisplay;
4252 int pixel_size = enabled->fb->bits_per_pixel / 8;
4253 unsigned long line_time_us;
4254 int entries;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004255
Chris Wilsond2102462011-01-24 17:43:27 +00004256 line_time_us = (htotal * 1000) / clock;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004257
4258 /* Use ns/us then divide to preserve precision */
Chris Wilsond2102462011-01-24 17:43:27 +00004259 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
4260 pixel_size * hdisplay;
4261 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
4262 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
4263 srwm = wm_info->fifo_size - entries;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004264 if (srwm < 0)
4265 srwm = 1;
Li Pengee980b82010-01-27 19:01:11 +08004266
4267 if (IS_I945G(dev) || IS_I945GM(dev))
Alexander Lam18b21902011-01-03 13:28:56 -05004268 I915_WRITE(FW_BLC_SELF,
4269 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
4270 else if (IS_I915GM(dev))
Li Pengee980b82010-01-27 19:01:11 +08004271 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004272 }
4273
Zhao Yakui28c97732009-10-09 11:39:41 +08004274 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01004275 planea_wm, planeb_wm, cwm, srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004276
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004277 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
4278 fwater_hi = (cwm & 0x1f);
4279
4280 /* Set request length to 8 cachelines per fetch */
4281 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
4282 fwater_hi = fwater_hi | (1 << 8);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004283
4284 I915_WRITE(FW_BLC, fwater_lo);
4285 I915_WRITE(FW_BLC2, fwater_hi);
Alexander Lam18b21902011-01-03 13:28:56 -05004286
Chris Wilsond2102462011-01-24 17:43:27 +00004287 if (HAS_FW_BLC(dev)) {
4288 if (enabled) {
4289 if (IS_I945G(dev) || IS_I945GM(dev))
4290 I915_WRITE(FW_BLC_SELF,
4291 FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
4292 else if (IS_I915GM(dev))
4293 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
4294 DRM_DEBUG_KMS("memory self refresh enabled\n");
4295 } else
4296 DRM_DEBUG_KMS("memory self refresh disabled\n");
4297 }
Shaohua Li7662c8b2009-06-26 11:23:55 +08004298}
4299
Chris Wilsond2102462011-01-24 17:43:27 +00004300static void i830_update_wm(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08004301{
4302 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004303 struct drm_crtc *crtc;
4304 uint32_t fwater_lo;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004305 int planea_wm;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004306
Chris Wilsond2102462011-01-24 17:43:27 +00004307 crtc = single_enabled_crtc(dev);
4308 if (crtc == NULL)
4309 return;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004310
Chris Wilsond2102462011-01-24 17:43:27 +00004311 planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
4312 dev_priv->display.get_fifo_size(dev, 0),
4313 crtc->fb->bits_per_pixel / 8,
4314 latency_ns);
4315 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
Jesse Barnesf3601322009-07-22 12:54:59 -07004316 fwater_lo |= (3<<8) | planea_wm;
4317
Zhao Yakui28c97732009-10-09 11:39:41 +08004318 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004319
4320 I915_WRITE(FW_BLC, fwater_lo);
4321}
4322
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004323#define ILK_LP0_PLANE_LATENCY 700
Zhao Yakuic936f442010-06-12 14:32:26 +08004324#define ILK_LP0_CURSOR_LATENCY 1300
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004325
Jesse Barnesb79d4992010-12-21 13:10:23 -08004326/*
4327 * Check the wm result.
4328 *
4329 * If any calculated watermark values is larger than the maximum value that
4330 * can be programmed into the associated watermark register, that watermark
4331 * must be disabled.
4332 */
4333static bool ironlake_check_srwm(struct drm_device *dev, int level,
4334 int fbc_wm, int display_wm, int cursor_wm,
4335 const struct intel_watermark_params *display,
4336 const struct intel_watermark_params *cursor)
4337{
4338 struct drm_i915_private *dev_priv = dev->dev_private;
4339
4340 DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
4341 " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
4342
4343 if (fbc_wm > SNB_FBC_MAX_SRWM) {
4344 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
4345 fbc_wm, SNB_FBC_MAX_SRWM, level);
4346
4347 /* fbc has it's own way to disable FBC WM */
4348 I915_WRITE(DISP_ARB_CTL,
4349 I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
4350 return false;
4351 }
4352
4353 if (display_wm > display->max_wm) {
4354 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
4355 display_wm, SNB_DISPLAY_MAX_SRWM, level);
4356 return false;
4357 }
4358
4359 if (cursor_wm > cursor->max_wm) {
4360 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
4361 cursor_wm, SNB_CURSOR_MAX_SRWM, level);
4362 return false;
4363 }
4364
4365 if (!(fbc_wm || display_wm || cursor_wm)) {
4366 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
4367 return false;
4368 }
4369
4370 return true;
4371}
4372
4373/*
4374 * Compute watermark values of WM[1-3],
4375 */
Chris Wilsond2102462011-01-24 17:43:27 +00004376static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
4377 int latency_ns,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004378 const struct intel_watermark_params *display,
4379 const struct intel_watermark_params *cursor,
4380 int *fbc_wm, int *display_wm, int *cursor_wm)
4381{
Chris Wilsond2102462011-01-24 17:43:27 +00004382 struct drm_crtc *crtc;
Jesse Barnesb79d4992010-12-21 13:10:23 -08004383 unsigned long line_time_us;
Chris Wilsond2102462011-01-24 17:43:27 +00004384 int hdisplay, htotal, pixel_size, clock;
Jesse Barnesb79d4992010-12-21 13:10:23 -08004385 int line_count, line_size;
4386 int small, large;
4387 int entries;
4388
4389 if (!latency_ns) {
4390 *fbc_wm = *display_wm = *cursor_wm = 0;
4391 return false;
4392 }
4393
Chris Wilsond2102462011-01-24 17:43:27 +00004394 crtc = intel_get_crtc_for_plane(dev, plane);
4395 hdisplay = crtc->mode.hdisplay;
4396 htotal = crtc->mode.htotal;
4397 clock = crtc->mode.clock;
4398 pixel_size = crtc->fb->bits_per_pixel / 8;
4399
Jesse Barnesb79d4992010-12-21 13:10:23 -08004400 line_time_us = (htotal * 1000) / clock;
4401 line_count = (latency_ns / line_time_us + 1000) / 1000;
4402 line_size = hdisplay * pixel_size;
4403
4404 /* Use the minimum of the small and large buffer method for primary */
4405 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
4406 large = line_count * line_size;
4407
4408 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
4409 *display_wm = entries + display->guard_size;
4410
4411 /*
4412 * Spec says:
4413 * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
4414 */
4415 *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
4416
4417 /* calculate the self-refresh watermark for display cursor */
4418 entries = line_count * pixel_size * 64;
4419 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4420 *cursor_wm = entries + cursor->guard_size;
4421
4422 return ironlake_check_srwm(dev, level,
4423 *fbc_wm, *display_wm, *cursor_wm,
4424 display, cursor);
4425}
4426
Chris Wilsond2102462011-01-24 17:43:27 +00004427static void ironlake_update_wm(struct drm_device *dev)
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004428{
4429 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004430 int fbc_wm, plane_wm, cursor_wm;
4431 unsigned int enabled;
Zhao Yakuic936f442010-06-12 14:32:26 +08004432
Chris Wilson4ed765f2010-09-11 10:46:47 +01004433 enabled = 0;
Chris Wilson9f405102011-05-12 22:17:14 +01004434 if (g4x_compute_wm0(dev, 0,
4435 &ironlake_display_wm_info,
4436 ILK_LP0_PLANE_LATENCY,
4437 &ironlake_cursor_wm_info,
4438 ILK_LP0_CURSOR_LATENCY,
4439 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01004440 I915_WRITE(WM0_PIPEA_ILK,
4441 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
4442 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
4443 " plane %d, " "cursor: %d\n",
4444 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004445 enabled |= 1;
Zhao Yakuic936f442010-06-12 14:32:26 +08004446 }
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004447
Chris Wilson9f405102011-05-12 22:17:14 +01004448 if (g4x_compute_wm0(dev, 1,
4449 &ironlake_display_wm_info,
4450 ILK_LP0_PLANE_LATENCY,
4451 &ironlake_cursor_wm_info,
4452 ILK_LP0_CURSOR_LATENCY,
4453 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01004454 I915_WRITE(WM0_PIPEB_ILK,
4455 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
4456 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
4457 " plane %d, cursor: %d\n",
4458 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004459 enabled |= 2;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004460 }
4461
4462 /*
4463 * Calculate and update the self-refresh watermark only when one
4464 * display plane is used.
4465 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08004466 I915_WRITE(WM3_LP_ILK, 0);
4467 I915_WRITE(WM2_LP_ILK, 0);
4468 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004469
Chris Wilsond2102462011-01-24 17:43:27 +00004470 if (!single_plane_enabled(enabled))
Jesse Barnesb79d4992010-12-21 13:10:23 -08004471 return;
Chris Wilsond2102462011-01-24 17:43:27 +00004472 enabled = ffs(enabled) - 1;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004473
Jesse Barnesb79d4992010-12-21 13:10:23 -08004474 /* WM1 */
Chris Wilsond2102462011-01-24 17:43:27 +00004475 if (!ironlake_compute_srwm(dev, 1, enabled,
4476 ILK_READ_WM1_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004477 &ironlake_display_srwm_info,
4478 &ironlake_cursor_srwm_info,
4479 &fbc_wm, &plane_wm, &cursor_wm))
4480 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01004481
Jesse Barnesb79d4992010-12-21 13:10:23 -08004482 I915_WRITE(WM1_LP_ILK,
4483 WM1_LP_SR_EN |
4484 (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4485 (fbc_wm << WM1_LP_FBC_SHIFT) |
4486 (plane_wm << WM1_LP_SR_SHIFT) |
4487 cursor_wm);
Chris Wilson4ed765f2010-09-11 10:46:47 +01004488
Jesse Barnesb79d4992010-12-21 13:10:23 -08004489 /* WM2 */
Chris Wilsond2102462011-01-24 17:43:27 +00004490 if (!ironlake_compute_srwm(dev, 2, enabled,
4491 ILK_READ_WM2_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004492 &ironlake_display_srwm_info,
4493 &ironlake_cursor_srwm_info,
4494 &fbc_wm, &plane_wm, &cursor_wm))
4495 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01004496
Jesse Barnesb79d4992010-12-21 13:10:23 -08004497 I915_WRITE(WM2_LP_ILK,
4498 WM2_LP_EN |
4499 (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4500 (fbc_wm << WM1_LP_FBC_SHIFT) |
4501 (plane_wm << WM1_LP_SR_SHIFT) |
4502 cursor_wm);
Yuanhan Liu13982612010-12-15 15:42:31 +08004503
4504 /*
Jesse Barnesb79d4992010-12-21 13:10:23 -08004505 * WM3 is unsupported on ILK, probably because we don't have latency
4506 * data for that power state
Yuanhan Liu13982612010-12-15 15:42:31 +08004507 */
Yuanhan Liu13982612010-12-15 15:42:31 +08004508}
4509
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004510void sandybridge_update_wm(struct drm_device *dev)
Yuanhan Liu13982612010-12-15 15:42:31 +08004511{
4512 struct drm_i915_private *dev_priv = dev->dev_private;
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08004513 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
Chris Wilsond2102462011-01-24 17:43:27 +00004514 int fbc_wm, plane_wm, cursor_wm;
4515 unsigned int enabled;
Yuanhan Liu13982612010-12-15 15:42:31 +08004516
4517 enabled = 0;
Chris Wilson9f405102011-05-12 22:17:14 +01004518 if (g4x_compute_wm0(dev, 0,
4519 &sandybridge_display_wm_info, latency,
4520 &sandybridge_cursor_wm_info, latency,
4521 &plane_wm, &cursor_wm)) {
Yuanhan Liu13982612010-12-15 15:42:31 +08004522 I915_WRITE(WM0_PIPEA_ILK,
4523 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
4524 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
4525 " plane %d, " "cursor: %d\n",
4526 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004527 enabled |= 1;
Yuanhan Liu13982612010-12-15 15:42:31 +08004528 }
4529
Chris Wilson9f405102011-05-12 22:17:14 +01004530 if (g4x_compute_wm0(dev, 1,
4531 &sandybridge_display_wm_info, latency,
4532 &sandybridge_cursor_wm_info, latency,
4533 &plane_wm, &cursor_wm)) {
Yuanhan Liu13982612010-12-15 15:42:31 +08004534 I915_WRITE(WM0_PIPEB_ILK,
4535 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
4536 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
4537 " plane %d, cursor: %d\n",
4538 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004539 enabled |= 2;
Yuanhan Liu13982612010-12-15 15:42:31 +08004540 }
4541
Jesse Barnesd6c892d2011-10-12 15:36:42 -07004542 /* IVB has 3 pipes */
4543 if (IS_IVYBRIDGE(dev) &&
4544 g4x_compute_wm0(dev, 2,
4545 &sandybridge_display_wm_info, latency,
4546 &sandybridge_cursor_wm_info, latency,
4547 &plane_wm, &cursor_wm)) {
4548 I915_WRITE(WM0_PIPEC_IVB,
4549 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
4550 DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
4551 " plane %d, cursor: %d\n",
4552 plane_wm, cursor_wm);
4553 enabled |= 3;
4554 }
4555
Yuanhan Liu13982612010-12-15 15:42:31 +08004556 /*
4557 * Calculate and update the self-refresh watermark only when one
4558 * display plane is used.
4559 *
4560 * SNB support 3 levels of watermark.
4561 *
4562 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
4563 * and disabled in the descending order
4564 *
4565 */
4566 I915_WRITE(WM3_LP_ILK, 0);
4567 I915_WRITE(WM2_LP_ILK, 0);
4568 I915_WRITE(WM1_LP_ILK, 0);
4569
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004570 if (!single_plane_enabled(enabled) ||
4571 dev_priv->sprite_scaling_enabled)
Yuanhan Liu13982612010-12-15 15:42:31 +08004572 return;
Chris Wilsond2102462011-01-24 17:43:27 +00004573 enabled = ffs(enabled) - 1;
Yuanhan Liu13982612010-12-15 15:42:31 +08004574
4575 /* WM1 */
Chris Wilsond2102462011-01-24 17:43:27 +00004576 if (!ironlake_compute_srwm(dev, 1, enabled,
4577 SNB_READ_WM1_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004578 &sandybridge_display_srwm_info,
4579 &sandybridge_cursor_srwm_info,
4580 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08004581 return;
4582
4583 I915_WRITE(WM1_LP_ILK,
4584 WM1_LP_SR_EN |
4585 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4586 (fbc_wm << WM1_LP_FBC_SHIFT) |
4587 (plane_wm << WM1_LP_SR_SHIFT) |
4588 cursor_wm);
4589
4590 /* WM2 */
Chris Wilsond2102462011-01-24 17:43:27 +00004591 if (!ironlake_compute_srwm(dev, 2, enabled,
4592 SNB_READ_WM2_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004593 &sandybridge_display_srwm_info,
4594 &sandybridge_cursor_srwm_info,
4595 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08004596 return;
4597
4598 I915_WRITE(WM2_LP_ILK,
4599 WM2_LP_EN |
4600 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4601 (fbc_wm << WM1_LP_FBC_SHIFT) |
4602 (plane_wm << WM1_LP_SR_SHIFT) |
4603 cursor_wm);
4604
4605 /* WM3 */
Chris Wilsond2102462011-01-24 17:43:27 +00004606 if (!ironlake_compute_srwm(dev, 3, enabled,
4607 SNB_READ_WM3_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004608 &sandybridge_display_srwm_info,
4609 &sandybridge_cursor_srwm_info,
4610 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08004611 return;
4612
4613 I915_WRITE(WM3_LP_ILK,
4614 WM3_LP_EN |
4615 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4616 (fbc_wm << WM1_LP_FBC_SHIFT) |
4617 (plane_wm << WM1_LP_SR_SHIFT) |
4618 cursor_wm);
4619}
4620
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004621static bool
4622sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
4623 uint32_t sprite_width, int pixel_size,
4624 const struct intel_watermark_params *display,
4625 int display_latency_ns, int *sprite_wm)
4626{
4627 struct drm_crtc *crtc;
4628 int clock;
4629 int entries, tlb_miss;
4630
4631 crtc = intel_get_crtc_for_plane(dev, plane);
4632 if (crtc->fb == NULL || !crtc->enabled) {
4633 *sprite_wm = display->guard_size;
4634 return false;
4635 }
4636
4637 clock = crtc->mode.clock;
4638
4639 /* Use the small buffer method to calculate the sprite watermark */
4640 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
4641 tlb_miss = display->fifo_size*display->cacheline_size -
4642 sprite_width * 8;
4643 if (tlb_miss > 0)
4644 entries += tlb_miss;
4645 entries = DIV_ROUND_UP(entries, display->cacheline_size);
4646 *sprite_wm = entries + display->guard_size;
4647 if (*sprite_wm > (int)display->max_wm)
4648 *sprite_wm = display->max_wm;
4649
4650 return true;
4651}
4652
4653static bool
4654sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
4655 uint32_t sprite_width, int pixel_size,
4656 const struct intel_watermark_params *display,
4657 int latency_ns, int *sprite_wm)
4658{
4659 struct drm_crtc *crtc;
4660 unsigned long line_time_us;
4661 int clock;
4662 int line_count, line_size;
4663 int small, large;
4664 int entries;
4665
4666 if (!latency_ns) {
4667 *sprite_wm = 0;
4668 return false;
4669 }
4670
4671 crtc = intel_get_crtc_for_plane(dev, plane);
4672 clock = crtc->mode.clock;
4673
4674 line_time_us = (sprite_width * 1000) / clock;
4675 line_count = (latency_ns / line_time_us + 1000) / 1000;
4676 line_size = sprite_width * pixel_size;
4677
4678 /* Use the minimum of the small and large buffer method for primary */
4679 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
4680 large = line_count * line_size;
4681
4682 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
4683 *sprite_wm = entries + display->guard_size;
4684
4685 return *sprite_wm > 0x3ff ? false : true;
4686}
4687
4688static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
4689 uint32_t sprite_width, int pixel_size)
4690{
4691 struct drm_i915_private *dev_priv = dev->dev_private;
4692 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
4693 int sprite_wm, reg;
4694 int ret;
4695
4696 switch (pipe) {
4697 case 0:
4698 reg = WM0_PIPEA_ILK;
4699 break;
4700 case 1:
4701 reg = WM0_PIPEB_ILK;
4702 break;
4703 case 2:
4704 reg = WM0_PIPEC_IVB;
4705 break;
4706 default:
4707 return; /* bad pipe */
4708 }
4709
4710 ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
4711 &sandybridge_display_wm_info,
4712 latency, &sprite_wm);
4713 if (!ret) {
4714 DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
4715 pipe);
4716 return;
4717 }
4718
4719 I915_WRITE(reg, I915_READ(reg) | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
4720 DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
4721
4722
4723 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
4724 pixel_size,
4725 &sandybridge_display_srwm_info,
4726 SNB_READ_WM1_LATENCY() * 500,
4727 &sprite_wm);
4728 if (!ret) {
4729 DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
4730 pipe);
4731 return;
4732 }
4733 I915_WRITE(WM1S_LP_ILK, sprite_wm);
4734
4735 /* Only IVB has two more LP watermarks for sprite */
4736 if (!IS_IVYBRIDGE(dev))
4737 return;
4738
4739 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
4740 pixel_size,
4741 &sandybridge_display_srwm_info,
4742 SNB_READ_WM2_LATENCY() * 500,
4743 &sprite_wm);
4744 if (!ret) {
4745 DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
4746 pipe);
4747 return;
4748 }
4749 I915_WRITE(WM2S_LP_IVB, sprite_wm);
4750
4751 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
4752 pixel_size,
4753 &sandybridge_display_srwm_info,
4754 SNB_READ_WM3_LATENCY() * 500,
4755 &sprite_wm);
4756 if (!ret) {
4757 DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
4758 pipe);
4759 return;
4760 }
4761 I915_WRITE(WM3S_LP_IVB, sprite_wm);
4762}
4763
Shaohua Li7662c8b2009-06-26 11:23:55 +08004764/**
4765 * intel_update_watermarks - update FIFO watermark values based on current modes
4766 *
4767 * Calculate watermark values for the various WM regs based on current mode
4768 * and plane configuration.
4769 *
4770 * There are several cases to deal with here:
4771 * - normal (i.e. non-self-refresh)
4772 * - self-refresh (SR) mode
4773 * - lines are large relative to FIFO size (buffer can hold up to 2)
4774 * - lines are small relative to FIFO size (buffer can hold more than 2
4775 * lines), so need to account for TLB latency
4776 *
4777 * The normal calculation is:
4778 * watermark = dotclock * bytes per pixel * latency
4779 * where latency is platform & configuration dependent (we assume pessimal
4780 * values here).
4781 *
4782 * The SR calculation is:
4783 * watermark = (trunc(latency/line time)+1) * surface width *
4784 * bytes per pixel
4785 * where
4786 * line time = htotal / dotclock
Zhao Yakuifa143212010-06-12 14:32:23 +08004787 * surface width = hdisplay for normal plane and 64 for cursor
Shaohua Li7662c8b2009-06-26 11:23:55 +08004788 * and latency is assumed to be high, as above.
4789 *
4790 * The final value programmed to the register should always be rounded up,
4791 * and include an extra 2 entries to account for clock crossings.
4792 *
4793 * We don't use the sprite, so we can ignore that. And on Crestline we have
4794 * to set the non-SR watermarks to 8.
Chris Wilson5eddb702010-09-11 13:48:45 +01004795 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08004796static void intel_update_watermarks(struct drm_device *dev)
4797{
Jesse Barnese70236a2009-09-21 10:42:27 -07004798 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004799
Chris Wilsond2102462011-01-24 17:43:27 +00004800 if (dev_priv->display.update_wm)
4801 dev_priv->display.update_wm(dev);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004802}
4803
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004804void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
4805 uint32_t sprite_width, int pixel_size)
4806{
4807 struct drm_i915_private *dev_priv = dev->dev_private;
4808
4809 if (dev_priv->display.update_sprite_wm)
4810 dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
4811 pixel_size);
4812}
4813
Chris Wilsona7615032011-01-12 17:04:08 +00004814static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4815{
Keith Packard72bbe58c2011-09-26 16:09:45 -07004816 if (i915_panel_use_ssc >= 0)
4817 return i915_panel_use_ssc != 0;
4818 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004819 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004820}
4821
Jesse Barnes5a354202011-06-24 12:19:22 -07004822/**
4823 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
4824 * @crtc: CRTC structure
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004825 * @mode: requested mode
Jesse Barnes5a354202011-06-24 12:19:22 -07004826 *
4827 * A pipe may be connected to one or more outputs. Based on the depth of the
4828 * attached framebuffer, choose a good color depth to use on the pipe.
4829 *
4830 * If possible, match the pipe depth to the fb depth. In some cases, this
4831 * isn't ideal, because the connected output supports a lesser or restricted
4832 * set of depths. Resolve that here:
4833 * LVDS typically supports only 6bpc, so clamp down in that case
4834 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
4835 * Displays may support a restricted set as well, check EDID and clamp as
4836 * appropriate.
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004837 * DP may want to dither down to 6bpc to fit larger modes
Jesse Barnes5a354202011-06-24 12:19:22 -07004838 *
4839 * RETURNS:
4840 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
4841 * true if they don't match).
4842 */
4843static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004844 unsigned int *pipe_bpp,
4845 struct drm_display_mode *mode)
Jesse Barnes5a354202011-06-24 12:19:22 -07004846{
4847 struct drm_device *dev = crtc->dev;
4848 struct drm_i915_private *dev_priv = dev->dev_private;
4849 struct drm_encoder *encoder;
4850 struct drm_connector *connector;
4851 unsigned int display_bpc = UINT_MAX, bpc;
4852
4853 /* Walk the encoders & connectors on this crtc, get min bpc */
4854 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
4855 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
4856
4857 if (encoder->crtc != crtc)
4858 continue;
4859
4860 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
4861 unsigned int lvds_bpc;
4862
4863 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
4864 LVDS_A3_POWER_UP)
4865 lvds_bpc = 8;
4866 else
4867 lvds_bpc = 6;
4868
4869 if (lvds_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004870 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004871 display_bpc = lvds_bpc;
4872 }
4873 continue;
4874 }
4875
4876 if (intel_encoder->type == INTEL_OUTPUT_EDP) {
4877 /* Use VBT settings if we have an eDP panel */
4878 unsigned int edp_bpc = dev_priv->edp.bpp / 3;
4879
4880 if (edp_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004881 DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004882 display_bpc = edp_bpc;
4883 }
4884 continue;
4885 }
4886
4887 /* Not one of the known troublemakers, check the EDID */
4888 list_for_each_entry(connector, &dev->mode_config.connector_list,
4889 head) {
4890 if (connector->encoder != encoder)
4891 continue;
4892
Jesse Barnes62ac41a2011-07-28 12:55:14 -07004893 /* Don't use an invalid EDID bpc value */
4894 if (connector->display_info.bpc &&
4895 connector->display_info.bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004896 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 -07004897 display_bpc = connector->display_info.bpc;
4898 }
4899 }
4900
4901 /*
4902 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
4903 * through, clamp it down. (Note: >12bpc will be caught below.)
4904 */
4905 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
4906 if (display_bpc > 8 && display_bpc < 12) {
Adam Jackson82820492011-10-10 16:33:34 -04004907 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004908 display_bpc = 12;
4909 } else {
Adam Jackson82820492011-10-10 16:33:34 -04004910 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07004911 display_bpc = 8;
4912 }
4913 }
4914 }
4915
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004916 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
4917 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
4918 display_bpc = 6;
4919 }
4920
Jesse Barnes5a354202011-06-24 12:19:22 -07004921 /*
4922 * We could just drive the pipe at the highest bpc all the time and
4923 * enable dithering as needed, but that costs bandwidth. So choose
4924 * the minimum value that expresses the full color range of the fb but
4925 * also stays within the max display bpc discovered above.
4926 */
4927
4928 switch (crtc->fb->depth) {
4929 case 8:
4930 bpc = 8; /* since we go through a colormap */
4931 break;
4932 case 15:
4933 case 16:
4934 bpc = 6; /* min is 18bpp */
4935 break;
4936 case 24:
Keith Packard578393c2011-09-05 11:53:21 -07004937 bpc = 8;
Jesse Barnes5a354202011-06-24 12:19:22 -07004938 break;
4939 case 30:
Keith Packard578393c2011-09-05 11:53:21 -07004940 bpc = 10;
Jesse Barnes5a354202011-06-24 12:19:22 -07004941 break;
4942 case 48:
Keith Packard578393c2011-09-05 11:53:21 -07004943 bpc = 12;
Jesse Barnes5a354202011-06-24 12:19:22 -07004944 break;
4945 default:
4946 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
4947 bpc = min((unsigned int)8, display_bpc);
4948 break;
4949 }
4950
Keith Packard578393c2011-09-05 11:53:21 -07004951 display_bpc = min(display_bpc, bpc);
4952
Adam Jackson82820492011-10-10 16:33:34 -04004953 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
4954 bpc, display_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004955
Keith Packard578393c2011-09-05 11:53:21 -07004956 *pipe_bpp = display_bpc * 3;
Jesse Barnes5a354202011-06-24 12:19:22 -07004957
4958 return display_bpc != bpc;
4959}
4960
Eric Anholtf564048e2011-03-30 13:01:02 -07004961static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
4962 struct drm_display_mode *mode,
4963 struct drm_display_mode *adjusted_mode,
4964 int x, int y,
4965 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08004966{
4967 struct drm_device *dev = crtc->dev;
4968 struct drm_i915_private *dev_priv = dev->dev_private;
4969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4970 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07004971 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07004972 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07004973 intel_clock_t clock, reduced_clock;
Chris Wilson5eddb702010-09-11 13:48:45 +01004974 u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
Jesse Barnes652c3932009-08-17 13:31:43 -07004975 bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07004976 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08004977 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson5eddb702010-09-11 13:48:45 +01004978 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08004979 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00004980 int ret;
Eric Anholtfae14982011-03-30 13:01:09 -07004981 u32 temp;
Bryan Freedaa9b5002011-01-12 13:43:19 -08004982 u32 lvds_sync = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08004983
Chris Wilson5eddb702010-09-11 13:48:45 +01004984 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
4985 if (encoder->base.crtc != crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004986 continue;
4987
Chris Wilson5eddb702010-09-11 13:48:45 +01004988 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08004989 case INTEL_OUTPUT_LVDS:
4990 is_lvds = true;
4991 break;
4992 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08004993 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08004994 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01004995 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08004996 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08004997 break;
4998 case INTEL_OUTPUT_DVO:
4999 is_dvo = true;
5000 break;
5001 case INTEL_OUTPUT_TVOUT:
5002 is_tv = true;
5003 break;
5004 case INTEL_OUTPUT_ANALOG:
5005 is_crt = true;
5006 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005007 case INTEL_OUTPUT_DISPLAYPORT:
5008 is_dp = true;
5009 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005010 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005011
Eric Anholtc751ce42010-03-25 11:48:48 -07005012 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005013 }
5014
Chris Wilsona7615032011-01-12 17:04:08 +00005015 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005016 refclk = dev_priv->lvds_ssc_freq * 1000;
Zhao Yakui28c97732009-10-09 11:39:41 +08005017 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01005018 refclk / 1000);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01005019 } else if (!IS_GEN2(dev)) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005020 refclk = 96000;
5021 } else {
5022 refclk = 48000;
5023 }
5024
Ma Lingd4906092009-03-18 20:13:27 +08005025 /*
5026 * Returns a set of divisors for the desired target clock with the given
5027 * refclk, or FALSE. The returned values represent the clock equation:
5028 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5029 */
Chris Wilson1b894b52010-12-14 20:04:54 +00005030 limit = intel_limit(crtc, refclk);
Ma Lingd4906092009-03-18 20:13:27 +08005031 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005032 if (!ok) {
5033 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07005034 return -EINVAL;
5035 }
5036
5037 /* Ensure that the cursor is valid for the new mode before changing... */
5038 intel_crtc_update_cursor(crtc, true);
5039
5040 if (is_lvds && dev_priv->lvds_downclock_avail) {
5041 has_reduced_clock = limit->find_pll(limit, crtc,
5042 dev_priv->lvds_downclock,
5043 refclk,
5044 &reduced_clock);
5045 if (has_reduced_clock && (clock.p != reduced_clock.p)) {
5046 /*
5047 * If the different P is found, it means that we can't
5048 * switch the display clock by using the FP0/FP1.
5049 * In such case we will disable the LVDS downclock
5050 * feature.
5051 */
5052 DRM_DEBUG_KMS("Different P is found for "
5053 "LVDS clock/downclock\n");
5054 has_reduced_clock = 0;
5055 }
5056 }
5057 /* SDVO TV has fixed PLL values depend on its clock range,
5058 this mirrors vbios setting. */
5059 if (is_sdvo && is_tv) {
5060 if (adjusted_mode->clock >= 100000
5061 && adjusted_mode->clock < 140500) {
5062 clock.p1 = 2;
5063 clock.p2 = 10;
5064 clock.n = 3;
5065 clock.m1 = 16;
5066 clock.m2 = 8;
5067 } else if (adjusted_mode->clock >= 140500
5068 && adjusted_mode->clock <= 200000) {
5069 clock.p1 = 1;
5070 clock.p2 = 10;
5071 clock.n = 6;
5072 clock.m1 = 12;
5073 clock.m2 = 8;
5074 }
5075 }
5076
Eric Anholtf564048e2011-03-30 13:01:02 -07005077 if (IS_PINEVIEW(dev)) {
5078 fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
5079 if (has_reduced_clock)
5080 fp2 = (1 << reduced_clock.n) << 16 |
5081 reduced_clock.m1 << 8 | reduced_clock.m2;
5082 } else {
5083 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5084 if (has_reduced_clock)
5085 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5086 reduced_clock.m2;
5087 }
5088
Eric Anholt929c77f2011-03-30 13:01:04 -07005089 dpll = DPLL_VGA_MODE_DIS;
Eric Anholtf564048e2011-03-30 13:01:02 -07005090
5091 if (!IS_GEN2(dev)) {
5092 if (is_lvds)
5093 dpll |= DPLLB_MODE_LVDS;
5094 else
5095 dpll |= DPLLB_MODE_DAC_SERIAL;
5096 if (is_sdvo) {
5097 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5098 if (pixel_multiplier > 1) {
5099 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
5100 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
Eric Anholtf564048e2011-03-30 13:01:02 -07005101 }
5102 dpll |= DPLL_DVO_HIGH_SPEED;
5103 }
Eric Anholt929c77f2011-03-30 13:01:04 -07005104 if (is_dp)
Eric Anholtf564048e2011-03-30 13:01:02 -07005105 dpll |= DPLL_DVO_HIGH_SPEED;
5106
5107 /* compute bitmask from p1 value */
5108 if (IS_PINEVIEW(dev))
5109 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5110 else {
5111 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholtf564048e2011-03-30 13:01:02 -07005112 if (IS_G4X(dev) && has_reduced_clock)
5113 dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5114 }
5115 switch (clock.p2) {
5116 case 5:
5117 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5118 break;
5119 case 7:
5120 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5121 break;
5122 case 10:
5123 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5124 break;
5125 case 14:
5126 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5127 break;
5128 }
Eric Anholt929c77f2011-03-30 13:01:04 -07005129 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholtf564048e2011-03-30 13:01:02 -07005130 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5131 } else {
5132 if (is_lvds) {
5133 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5134 } else {
5135 if (clock.p1 == 2)
5136 dpll |= PLL_P1_DIVIDE_BY_TWO;
5137 else
5138 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5139 if (clock.p2 == 4)
5140 dpll |= PLL_P2_DIVIDE_BY_4;
5141 }
5142 }
5143
5144 if (is_sdvo && is_tv)
5145 dpll |= PLL_REF_INPUT_TVCLKINBC;
5146 else if (is_tv)
5147 /* XXX: just matching BIOS for now */
5148 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
5149 dpll |= 3;
5150 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5151 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5152 else
5153 dpll |= PLL_REF_INPUT_DREFCLK;
5154
5155 /* setup pipeconf */
5156 pipeconf = I915_READ(PIPECONF(pipe));
5157
5158 /* Set up the display plane register */
5159 dspcntr = DISPPLANE_GAMMA_ENABLE;
5160
5161 /* Ironlake's plane is forced to pipe, bit 24 is to
5162 enable color space conversion */
Eric Anholt929c77f2011-03-30 13:01:04 -07005163 if (pipe == 0)
5164 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5165 else
5166 dspcntr |= DISPPLANE_SEL_PIPE_B;
Eric Anholtf564048e2011-03-30 13:01:02 -07005167
5168 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
5169 /* Enable pixel doubling when the dot clock is > 90% of the (display)
5170 * core speed.
5171 *
5172 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
5173 * pipe == 0 check?
5174 */
5175 if (mode->clock >
5176 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
5177 pipeconf |= PIPECONF_DOUBLE_WIDE;
5178 else
5179 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
5180 }
5181
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005182 /* default to 8bpc */
5183 pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
5184 if (is_dp) {
5185 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
5186 pipeconf |= PIPECONF_BPP_6 |
5187 PIPECONF_DITHER_EN |
5188 PIPECONF_DITHER_TYPE_SP;
5189 }
5190 }
5191
Eric Anholt929c77f2011-03-30 13:01:04 -07005192 dpll |= DPLL_VCO_ENABLE;
Eric Anholtf564048e2011-03-30 13:01:02 -07005193
5194 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
5195 drm_mode_debug_printmodeline(mode);
5196
Eric Anholtfae14982011-03-30 13:01:09 -07005197 I915_WRITE(FP0(pipe), fp);
5198 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
Eric Anholtf564048e2011-03-30 13:01:02 -07005199
Eric Anholtfae14982011-03-30 13:01:09 -07005200 POSTING_READ(DPLL(pipe));
Eric Anholtc713bb02011-03-30 13:01:05 -07005201 udelay(150);
Eric Anholtf564048e2011-03-30 13:01:02 -07005202
Eric Anholtf564048e2011-03-30 13:01:02 -07005203 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
5204 * This is an exception to the general rule that mode_set doesn't turn
5205 * things on.
5206 */
5207 if (is_lvds) {
Eric Anholtfae14982011-03-30 13:01:09 -07005208 temp = I915_READ(LVDS);
Eric Anholtf564048e2011-03-30 13:01:02 -07005209 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
5210 if (pipe == 1) {
Eric Anholt929c77f2011-03-30 13:01:04 -07005211 temp |= LVDS_PIPEB_SELECT;
Eric Anholtf564048e2011-03-30 13:01:02 -07005212 } else {
Eric Anholt929c77f2011-03-30 13:01:04 -07005213 temp &= ~LVDS_PIPEB_SELECT;
Eric Anholtf564048e2011-03-30 13:01:02 -07005214 }
5215 /* set the corresponsding LVDS_BORDER bit */
5216 temp |= dev_priv->lvds_border_bits;
5217 /* Set the B0-B3 data pairs corresponding to whether we're going to
5218 * set the DPLLs for dual-channel mode or not.
5219 */
5220 if (clock.p2 == 7)
5221 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
5222 else
5223 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
5224
5225 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
5226 * appropriately here, but we need to look more thoroughly into how
5227 * panels behave in the two modes.
5228 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005229 /* set the dithering flag on LVDS as needed */
5230 if (INTEL_INFO(dev)->gen >= 4) {
Eric Anholtf564048e2011-03-30 13:01:02 -07005231 if (dev_priv->lvds_dither)
5232 temp |= LVDS_ENABLE_DITHER;
5233 else
5234 temp &= ~LVDS_ENABLE_DITHER;
5235 }
5236 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
5237 lvds_sync |= LVDS_HSYNC_POLARITY;
5238 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
5239 lvds_sync |= LVDS_VSYNC_POLARITY;
5240 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
5241 != lvds_sync) {
5242 char flags[2] = "-+";
5243 DRM_INFO("Changing LVDS panel from "
5244 "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
5245 flags[!(temp & LVDS_HSYNC_POLARITY)],
5246 flags[!(temp & LVDS_VSYNC_POLARITY)],
5247 flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
5248 flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
5249 temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
5250 temp |= lvds_sync;
5251 }
Eric Anholtfae14982011-03-30 13:01:09 -07005252 I915_WRITE(LVDS, temp);
Eric Anholtf564048e2011-03-30 13:01:02 -07005253 }
5254
Eric Anholt929c77f2011-03-30 13:01:04 -07005255 if (is_dp) {
Eric Anholtf564048e2011-03-30 13:01:02 -07005256 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Eric Anholtf564048e2011-03-30 13:01:02 -07005257 }
5258
Eric Anholtfae14982011-03-30 13:01:09 -07005259 I915_WRITE(DPLL(pipe), dpll);
Eric Anholtf564048e2011-03-30 13:01:02 -07005260
Eric Anholtc713bb02011-03-30 13:01:05 -07005261 /* Wait for the clocks to stabilize. */
Eric Anholtfae14982011-03-30 13:01:09 -07005262 POSTING_READ(DPLL(pipe));
Eric Anholtc713bb02011-03-30 13:01:05 -07005263 udelay(150);
Eric Anholtf564048e2011-03-30 13:01:02 -07005264
Eric Anholtc713bb02011-03-30 13:01:05 -07005265 if (INTEL_INFO(dev)->gen >= 4) {
5266 temp = 0;
5267 if (is_sdvo) {
5268 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
5269 if (temp > 1)
5270 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5271 else
5272 temp = 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07005273 }
Eric Anholtc713bb02011-03-30 13:01:05 -07005274 I915_WRITE(DPLL_MD(pipe), temp);
5275 } else {
5276 /* The pixel multiplier can only be updated once the
5277 * DPLL is enabled and the clocks are stable.
5278 *
5279 * So write it again.
5280 */
Eric Anholtfae14982011-03-30 13:01:09 -07005281 I915_WRITE(DPLL(pipe), dpll);
Eric Anholtf564048e2011-03-30 13:01:02 -07005282 }
5283
5284 intel_crtc->lowfreq_avail = false;
5285 if (is_lvds && has_reduced_clock && i915_powersave) {
Eric Anholtfae14982011-03-30 13:01:09 -07005286 I915_WRITE(FP1(pipe), fp2);
Eric Anholtf564048e2011-03-30 13:01:02 -07005287 intel_crtc->lowfreq_avail = true;
5288 if (HAS_PIPE_CXSR(dev)) {
5289 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5290 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5291 }
5292 } else {
Eric Anholtfae14982011-03-30 13:01:09 -07005293 I915_WRITE(FP1(pipe), fp);
Eric Anholtf564048e2011-03-30 13:01:02 -07005294 if (HAS_PIPE_CXSR(dev)) {
5295 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
5296 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
5297 }
5298 }
5299
5300 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5301 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5302 /* the chip adds 2 halflines automatically */
5303 adjusted_mode->crtc_vdisplay -= 1;
5304 adjusted_mode->crtc_vtotal -= 1;
5305 adjusted_mode->crtc_vblank_start -= 1;
5306 adjusted_mode->crtc_vblank_end -= 1;
5307 adjusted_mode->crtc_vsync_end -= 1;
5308 adjusted_mode->crtc_vsync_start -= 1;
5309 } else
Christian Schmidt59df7b12011-12-19 20:03:33 +01005310 pipeconf &= ~PIPECONF_INTERLACE_MASK; /* progressive */
Eric Anholtf564048e2011-03-30 13:01:02 -07005311
5312 I915_WRITE(HTOTAL(pipe),
5313 (adjusted_mode->crtc_hdisplay - 1) |
5314 ((adjusted_mode->crtc_htotal - 1) << 16));
5315 I915_WRITE(HBLANK(pipe),
5316 (adjusted_mode->crtc_hblank_start - 1) |
5317 ((adjusted_mode->crtc_hblank_end - 1) << 16));
5318 I915_WRITE(HSYNC(pipe),
5319 (adjusted_mode->crtc_hsync_start - 1) |
5320 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5321
5322 I915_WRITE(VTOTAL(pipe),
5323 (adjusted_mode->crtc_vdisplay - 1) |
5324 ((adjusted_mode->crtc_vtotal - 1) << 16));
5325 I915_WRITE(VBLANK(pipe),
5326 (adjusted_mode->crtc_vblank_start - 1) |
5327 ((adjusted_mode->crtc_vblank_end - 1) << 16));
5328 I915_WRITE(VSYNC(pipe),
5329 (adjusted_mode->crtc_vsync_start - 1) |
5330 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5331
5332 /* pipesrc and dspsize control the size that is scaled from,
5333 * which should always be the user's requested size.
5334 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005335 I915_WRITE(DSPSIZE(plane),
5336 ((mode->vdisplay - 1) << 16) |
5337 (mode->hdisplay - 1));
5338 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005339 I915_WRITE(PIPESRC(pipe),
5340 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
5341
Eric Anholtf564048e2011-03-30 13:01:02 -07005342 I915_WRITE(PIPECONF(pipe), pipeconf);
5343 POSTING_READ(PIPECONF(pipe));
Eric Anholt929c77f2011-03-30 13:01:04 -07005344 intel_enable_pipe(dev_priv, pipe, false);
Eric Anholtf564048e2011-03-30 13:01:02 -07005345
5346 intel_wait_for_vblank(dev, pipe);
5347
Eric Anholtf564048e2011-03-30 13:01:02 -07005348 I915_WRITE(DSPCNTR(plane), dspcntr);
5349 POSTING_READ(DSPCNTR(plane));
Keith Packard284d9522011-06-06 17:12:49 -07005350 intel_enable_plane(dev_priv, plane, pipe);
Eric Anholtf564048e2011-03-30 13:01:02 -07005351
5352 ret = intel_pipe_set_base(crtc, x, y, old_fb);
5353
5354 intel_update_watermarks(dev);
5355
Eric Anholtf564048e2011-03-30 13:01:02 -07005356 return ret;
5357}
5358
Keith Packard9fb526d2011-09-26 22:24:57 -07005359/*
5360 * Initialize reference clocks when the driver loads
5361 */
5362void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005363{
5364 struct drm_i915_private *dev_priv = dev->dev_private;
5365 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005366 struct intel_encoder *encoder;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005367 u32 temp;
5368 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005369 bool has_cpu_edp = false;
5370 bool has_pch_edp = false;
5371 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005372 bool has_ck505 = false;
5373 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005374
5375 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005376 list_for_each_entry(encoder, &mode_config->encoder_list,
5377 base.head) {
5378 switch (encoder->type) {
5379 case INTEL_OUTPUT_LVDS:
5380 has_panel = true;
5381 has_lvds = true;
5382 break;
5383 case INTEL_OUTPUT_EDP:
5384 has_panel = true;
5385 if (intel_encoder_is_pch_edp(&encoder->base))
5386 has_pch_edp = true;
5387 else
5388 has_cpu_edp = true;
5389 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005390 }
5391 }
5392
Keith Packard99eb6a02011-09-26 14:29:12 -07005393 if (HAS_PCH_IBX(dev)) {
5394 has_ck505 = dev_priv->display_clock_mode;
5395 can_ssc = has_ck505;
5396 } else {
5397 has_ck505 = false;
5398 can_ssc = true;
5399 }
5400
5401 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
5402 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
5403 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005404
5405 /* Ironlake: try to setup display ref clock before DPLL
5406 * enabling. This is only under driver's control after
5407 * PCH B stepping, previous chipset stepping should be
5408 * ignoring this setting.
5409 */
5410 temp = I915_READ(PCH_DREF_CONTROL);
5411 /* Always enable nonspread source */
5412 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005413
Keith Packard99eb6a02011-09-26 14:29:12 -07005414 if (has_ck505)
5415 temp |= DREF_NONSPREAD_CK505_ENABLE;
5416 else
5417 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005418
Keith Packard199e5d72011-09-22 12:01:57 -07005419 if (has_panel) {
5420 temp &= ~DREF_SSC_SOURCE_MASK;
5421 temp |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005422
Keith Packard199e5d72011-09-22 12:01:57 -07005423 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005424 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005425 DRM_DEBUG_KMS("Using SSC on panel\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07005426 temp |= DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005427 }
Keith Packard199e5d72011-09-22 12:01:57 -07005428
5429 /* Get SSC going before enabling the outputs */
5430 I915_WRITE(PCH_DREF_CONTROL, temp);
5431 POSTING_READ(PCH_DREF_CONTROL);
5432 udelay(200);
5433
Jesse Barnes13d83a62011-08-03 12:59:20 -07005434 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5435
5436 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005437 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005438 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005439 DRM_DEBUG_KMS("Using SSC on eDP\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07005440 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005441 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005442 else
5443 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005444 } else
5445 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5446
5447 I915_WRITE(PCH_DREF_CONTROL, temp);
5448 POSTING_READ(PCH_DREF_CONTROL);
5449 udelay(200);
5450 } else {
5451 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5452
5453 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5454
5455 /* Turn off CPU output */
5456 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5457
5458 I915_WRITE(PCH_DREF_CONTROL, temp);
5459 POSTING_READ(PCH_DREF_CONTROL);
5460 udelay(200);
5461
5462 /* Turn off the SSC source */
5463 temp &= ~DREF_SSC_SOURCE_MASK;
5464 temp |= DREF_SSC_SOURCE_DISABLE;
5465
5466 /* Turn off SSC1 */
5467 temp &= ~ DREF_SSC1_ENABLE;
5468
Jesse Barnes13d83a62011-08-03 12:59:20 -07005469 I915_WRITE(PCH_DREF_CONTROL, temp);
5470 POSTING_READ(PCH_DREF_CONTROL);
5471 udelay(200);
5472 }
5473}
5474
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005475static int ironlake_get_refclk(struct drm_crtc *crtc)
5476{
5477 struct drm_device *dev = crtc->dev;
5478 struct drm_i915_private *dev_priv = dev->dev_private;
5479 struct intel_encoder *encoder;
5480 struct drm_mode_config *mode_config = &dev->mode_config;
5481 struct intel_encoder *edp_encoder = NULL;
5482 int num_connectors = 0;
5483 bool is_lvds = false;
5484
5485 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5486 if (encoder->base.crtc != crtc)
5487 continue;
5488
5489 switch (encoder->type) {
5490 case INTEL_OUTPUT_LVDS:
5491 is_lvds = true;
5492 break;
5493 case INTEL_OUTPUT_EDP:
5494 edp_encoder = encoder;
5495 break;
5496 }
5497 num_connectors++;
5498 }
5499
5500 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5501 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5502 dev_priv->lvds_ssc_freq);
5503 return dev_priv->lvds_ssc_freq * 1000;
5504 }
5505
5506 return 120000;
5507}
5508
Eric Anholtf564048e2011-03-30 13:01:02 -07005509static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5510 struct drm_display_mode *mode,
5511 struct drm_display_mode *adjusted_mode,
5512 int x, int y,
5513 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005514{
5515 struct drm_device *dev = crtc->dev;
5516 struct drm_i915_private *dev_priv = dev->dev_private;
5517 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5518 int pipe = intel_crtc->pipe;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005519 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08005520 int refclk, num_connectors = 0;
5521 intel_clock_t clock, reduced_clock;
5522 u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
Eric Anholta07d6782011-03-30 13:01:08 -07005523 bool ok, has_reduced_clock = false, is_sdvo = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005524 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
5525 struct intel_encoder *has_edp_encoder = NULL;
5526 struct drm_mode_config *mode_config = &dev->mode_config;
5527 struct intel_encoder *encoder;
5528 const intel_limit_t *limit;
5529 int ret;
5530 struct fdi_m_n m_n = {0};
Eric Anholtfae14982011-03-30 13:01:09 -07005531 u32 temp;
Jesse Barnes79e53942008-11-07 14:24:08 -08005532 u32 lvds_sync = 0;
Jesse Barnes5a354202011-06-24 12:19:22 -07005533 int target_clock, pixel_multiplier, lane, link_bw, factor;
5534 unsigned int pipe_bpp;
5535 bool dither;
Jesse Barnes79e53942008-11-07 14:24:08 -08005536
Jesse Barnes79e53942008-11-07 14:24:08 -08005537 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5538 if (encoder->base.crtc != crtc)
5539 continue;
5540
5541 switch (encoder->type) {
5542 case INTEL_OUTPUT_LVDS:
5543 is_lvds = true;
5544 break;
5545 case INTEL_OUTPUT_SDVO:
5546 case INTEL_OUTPUT_HDMI:
5547 is_sdvo = true;
5548 if (encoder->needs_tv_clock)
5549 is_tv = true;
5550 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005551 case INTEL_OUTPUT_TVOUT:
5552 is_tv = true;
5553 break;
5554 case INTEL_OUTPUT_ANALOG:
5555 is_crt = true;
5556 break;
5557 case INTEL_OUTPUT_DISPLAYPORT:
5558 is_dp = true;
5559 break;
5560 case INTEL_OUTPUT_EDP:
5561 has_edp_encoder = encoder;
5562 break;
5563 }
5564
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005565 num_connectors++;
5566 }
5567
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005568 refclk = ironlake_get_refclk(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005569
5570 /*
5571 * Returns a set of divisors for the desired target clock with the given
5572 * refclk, or FALSE. The returned values represent the clock equation:
5573 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5574 */
5575 limit = intel_limit(crtc, refclk);
5576 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
5577 if (!ok) {
5578 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08005579 return -EINVAL;
5580 }
5581
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005582 /* Ensure that the cursor is valid for the new mode before changing... */
Chris Wilson6b383a72010-09-13 13:54:26 +01005583 intel_crtc_update_cursor(crtc, true);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005584
Zhao Yakuiddc90032010-01-06 22:05:56 +08005585 if (is_lvds && dev_priv->lvds_downclock_avail) {
5586 has_reduced_clock = limit->find_pll(limit, crtc,
Chris Wilson5eddb702010-09-11 13:48:45 +01005587 dev_priv->lvds_downclock,
5588 refclk,
5589 &reduced_clock);
Zhao Yakui18f9ed12009-11-20 03:24:16 +00005590 if (has_reduced_clock && (clock.p != reduced_clock.p)) {
5591 /*
5592 * If the different P is found, it means that we can't
5593 * switch the display clock by using the FP0/FP1.
5594 * In such case we will disable the LVDS downclock
5595 * feature.
5596 */
5597 DRM_DEBUG_KMS("Different P is found for "
Chris Wilson5eddb702010-09-11 13:48:45 +01005598 "LVDS clock/downclock\n");
Zhao Yakui18f9ed12009-11-20 03:24:16 +00005599 has_reduced_clock = 0;
5600 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005601 }
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08005602 /* SDVO TV has fixed PLL values depend on its clock range,
5603 this mirrors vbios setting. */
5604 if (is_sdvo && is_tv) {
5605 if (adjusted_mode->clock >= 100000
Chris Wilson5eddb702010-09-11 13:48:45 +01005606 && adjusted_mode->clock < 140500) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08005607 clock.p1 = 2;
5608 clock.p2 = 10;
5609 clock.n = 3;
5610 clock.m1 = 16;
5611 clock.m2 = 8;
5612 } else if (adjusted_mode->clock >= 140500
Chris Wilson5eddb702010-09-11 13:48:45 +01005613 && adjusted_mode->clock <= 200000) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08005614 clock.p1 = 1;
5615 clock.p2 = 10;
5616 clock.n = 6;
5617 clock.m1 = 12;
5618 clock.m2 = 8;
5619 }
5620 }
5621
Zhenyu Wang2c072452009-06-05 15:38:42 +08005622 /* FDI link */
Eric Anholt8febb292011-03-30 13:01:07 -07005623 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5624 lane = 0;
5625 /* CPU eDP doesn't require FDI link, so just set DP M/N
5626 according to current link config */
5627 if (has_edp_encoder &&
5628 !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
5629 target_clock = mode->clock;
5630 intel_edp_link_config(has_edp_encoder,
5631 &lane, &link_bw);
5632 } else {
5633 /* [e]DP over FDI requires target mode clock
5634 instead of link clock */
5635 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005636 target_clock = mode->clock;
Eric Anholt8febb292011-03-30 13:01:07 -07005637 else
5638 target_clock = adjusted_mode->clock;
Chris Wilson021357a2010-09-07 20:54:59 +01005639
Eric Anholt8febb292011-03-30 13:01:07 -07005640 /* FDI is a binary signal running at ~2.7GHz, encoding
5641 * each output octet as 10 bits. The actual frequency
5642 * is stored as a divider into a 100MHz clock, and the
5643 * mode pixel clock is stored in units of 1KHz.
5644 * Hence the bw of each lane in terms of the mode signal
5645 * is:
5646 */
5647 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005648 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08005649
Eric Anholt8febb292011-03-30 13:01:07 -07005650 /* determine panel color depth */
5651 temp = I915_READ(PIPECONF(pipe));
5652 temp &= ~PIPE_BPC_MASK;
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005653 dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
Jesse Barnes5a354202011-06-24 12:19:22 -07005654 switch (pipe_bpp) {
5655 case 18:
5656 temp |= PIPE_6BPC;
5657 break;
5658 case 24:
Eric Anholt8febb292011-03-30 13:01:07 -07005659 temp |= PIPE_8BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07005660 break;
Jesse Barnes5a354202011-06-24 12:19:22 -07005661 case 30:
5662 temp |= PIPE_10BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07005663 break;
Jesse Barnes5a354202011-06-24 12:19:22 -07005664 case 36:
5665 temp |= PIPE_12BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07005666 break;
5667 default:
Jesse Barnes62ac41a2011-07-28 12:55:14 -07005668 WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
5669 pipe_bpp);
Jesse Barnes5a354202011-06-24 12:19:22 -07005670 temp |= PIPE_8BPC;
5671 pipe_bpp = 24;
5672 break;
Eric Anholt8febb292011-03-30 13:01:07 -07005673 }
5674
Jesse Barnes5a354202011-06-24 12:19:22 -07005675 intel_crtc->bpp = pipe_bpp;
5676 I915_WRITE(PIPECONF(pipe), temp);
5677
Eric Anholt8febb292011-03-30 13:01:07 -07005678 if (!lane) {
5679 /*
5680 * Account for spread spectrum to avoid
5681 * oversubscribing the link. Max center spread
5682 * is 2.5%; use 5% for safety's sake.
5683 */
Jesse Barnes5a354202011-06-24 12:19:22 -07005684 u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
Eric Anholt8febb292011-03-30 13:01:07 -07005685 lane = bps / (link_bw * 8) + 1;
5686 }
5687
5688 intel_crtc->fdi_lanes = lane;
5689
5690 if (pixel_multiplier > 1)
5691 link_bw *= pixel_multiplier;
Jesse Barnes5a354202011-06-24 12:19:22 -07005692 ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
5693 &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07005694
Eric Anholta07d6782011-03-30 13:01:08 -07005695 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5696 if (has_reduced_clock)
5697 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5698 reduced_clock.m2;
Jesse Barnes79e53942008-11-07 14:24:08 -08005699
Chris Wilsonc1858122010-12-03 21:35:48 +00005700 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005701 factor = 21;
5702 if (is_lvds) {
5703 if ((intel_panel_use_ssc(dev_priv) &&
5704 dev_priv->lvds_ssc_freq == 100) ||
5705 (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
5706 factor = 25;
5707 } else if (is_sdvo && is_tv)
5708 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005709
Jesse Barnescb0e0932011-07-28 14:50:30 -07005710 if (clock.m < factor * clock.n)
Eric Anholt8febb292011-03-30 13:01:07 -07005711 fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005712
Chris Wilson5eddb702010-09-11 13:48:45 +01005713 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005714
Eric Anholta07d6782011-03-30 13:01:08 -07005715 if (is_lvds)
5716 dpll |= DPLLB_MODE_LVDS;
5717 else
5718 dpll |= DPLLB_MODE_DAC_SERIAL;
5719 if (is_sdvo) {
5720 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5721 if (pixel_multiplier > 1) {
5722 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08005723 }
Eric Anholta07d6782011-03-30 13:01:08 -07005724 dpll |= DPLL_DVO_HIGH_SPEED;
5725 }
5726 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
5727 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005728
Eric Anholta07d6782011-03-30 13:01:08 -07005729 /* compute bitmask from p1 value */
5730 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5731 /* also FPA1 */
5732 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5733
5734 switch (clock.p2) {
5735 case 5:
5736 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5737 break;
5738 case 7:
5739 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5740 break;
5741 case 10:
5742 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5743 break;
5744 case 14:
5745 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5746 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005747 }
5748
5749 if (is_sdvo && is_tv)
5750 dpll |= PLL_REF_INPUT_TVCLKINBC;
5751 else if (is_tv)
5752 /* XXX: just matching BIOS for now */
5753 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
5754 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00005755 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Jesse Barnes79e53942008-11-07 14:24:08 -08005756 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5757 else
5758 dpll |= PLL_REF_INPUT_DREFCLK;
5759
5760 /* setup pipeconf */
Chris Wilson5eddb702010-09-11 13:48:45 +01005761 pipeconf = I915_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08005762
5763 /* Set up the display plane register */
5764 dspcntr = DISPPLANE_GAMMA_ENABLE;
5765
Jesse Barnesf7cb34d2011-10-12 10:49:14 -07005766 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005767 drm_mode_debug_printmodeline(mode);
5768
Jesse Barnes5c5313c2010-10-07 16:01:11 -07005769 /* PCH eDP needs FDI, but CPU eDP does not */
Jesse Barnes4b645f12011-10-12 09:51:31 -07005770 if (!intel_crtc->no_pll) {
5771 if (!has_edp_encoder ||
5772 intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
5773 I915_WRITE(PCH_FP0(pipe), fp);
5774 I915_WRITE(PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01005775
Jesse Barnes4b645f12011-10-12 09:51:31 -07005776 POSTING_READ(PCH_DPLL(pipe));
5777 udelay(150);
5778 }
5779 } else {
5780 if (dpll == (I915_READ(PCH_DPLL(0)) & 0x7fffffff) &&
5781 fp == I915_READ(PCH_FP0(0))) {
5782 intel_crtc->use_pll_a = true;
5783 DRM_DEBUG_KMS("using pipe a dpll\n");
5784 } else if (dpll == (I915_READ(PCH_DPLL(1)) & 0x7fffffff) &&
5785 fp == I915_READ(PCH_FP0(1))) {
5786 intel_crtc->use_pll_a = false;
5787 DRM_DEBUG_KMS("using pipe b dpll\n");
5788 } else {
5789 DRM_DEBUG_KMS("no matching PLL configuration for pipe 2\n");
5790 return -EINVAL;
5791 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005792 }
5793
5794 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
5795 * This is an exception to the general rule that mode_set doesn't turn
5796 * things on.
5797 */
5798 if (is_lvds) {
Eric Anholtfae14982011-03-30 13:01:09 -07005799 temp = I915_READ(PCH_LVDS);
Chris Wilson5eddb702010-09-11 13:48:45 +01005800 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
Jesse Barnes4b645f12011-10-12 09:51:31 -07005801 if (HAS_PCH_CPT(dev))
5802 temp |= PORT_TRANS_SEL_CPT(pipe);
5803 else if (pipe == 1)
5804 temp |= LVDS_PIPEB_SELECT;
5805 else
5806 temp &= ~LVDS_PIPEB_SELECT;
5807
Zhao Yakuia3e17eb2009-10-10 10:42:37 +08005808 /* set the corresponsding LVDS_BORDER bit */
Chris Wilson5eddb702010-09-11 13:48:45 +01005809 temp |= dev_priv->lvds_border_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -08005810 /* Set the B0-B3 data pairs corresponding to whether we're going to
5811 * set the DPLLs for dual-channel mode or not.
5812 */
5813 if (clock.p2 == 7)
Chris Wilson5eddb702010-09-11 13:48:45 +01005814 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
Jesse Barnes79e53942008-11-07 14:24:08 -08005815 else
Chris Wilson5eddb702010-09-11 13:48:45 +01005816 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
Jesse Barnes79e53942008-11-07 14:24:08 -08005817
5818 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
5819 * appropriately here, but we need to look more thoroughly into how
5820 * panels behave in the two modes.
5821 */
Bryan Freedaa9b5002011-01-12 13:43:19 -08005822 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
5823 lvds_sync |= LVDS_HSYNC_POLARITY;
5824 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
5825 lvds_sync |= LVDS_VSYNC_POLARITY;
5826 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
5827 != lvds_sync) {
5828 char flags[2] = "-+";
5829 DRM_INFO("Changing LVDS panel from "
5830 "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
5831 flags[!(temp & LVDS_HSYNC_POLARITY)],
5832 flags[!(temp & LVDS_VSYNC_POLARITY)],
5833 flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
5834 flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
5835 temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
5836 temp |= lvds_sync;
5837 }
Eric Anholtfae14982011-03-30 13:01:09 -07005838 I915_WRITE(PCH_LVDS, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -08005839 }
Jesse Barnes434ed092010-09-07 14:48:06 -07005840
Eric Anholt8febb292011-03-30 13:01:07 -07005841 pipeconf &= ~PIPECONF_DITHER_EN;
5842 pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
Jesse Barnes5a354202011-06-24 12:19:22 -07005843 if ((is_lvds && dev_priv->lvds_dither) || dither) {
Eric Anholt8febb292011-03-30 13:01:07 -07005844 pipeconf |= PIPECONF_DITHER_EN;
Daniel Vetterf74974c2011-10-11 17:27:51 +02005845 pipeconf |= PIPECONF_DITHER_TYPE_SP;
Jesse Barnes434ed092010-09-07 14:48:06 -07005846 }
Jesse Barnes5c5313c2010-10-07 16:01:11 -07005847 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005848 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Eric Anholt8febb292011-03-30 13:01:07 -07005849 } else {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08005850 /* For non-DP output, clear any trans DP clock recovery setting.*/
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005851 I915_WRITE(TRANSDATA_M1(pipe), 0);
5852 I915_WRITE(TRANSDATA_N1(pipe), 0);
5853 I915_WRITE(TRANSDPLINK_M1(pipe), 0);
5854 I915_WRITE(TRANSDPLINK_N1(pipe), 0);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08005855 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005856
Jesse Barnes4b645f12011-10-12 09:51:31 -07005857 if (!intel_crtc->no_pll &&
5858 (!has_edp_encoder ||
5859 intel_encoder_is_pch_edp(&has_edp_encoder->base))) {
Eric Anholtfae14982011-03-30 13:01:09 -07005860 I915_WRITE(PCH_DPLL(pipe), dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01005861
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005862 /* Wait for the clocks to stabilize. */
Eric Anholtfae14982011-03-30 13:01:09 -07005863 POSTING_READ(PCH_DPLL(pipe));
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005864 udelay(150);
5865
Eric Anholt8febb292011-03-30 13:01:07 -07005866 /* The pixel multiplier can only be updated once the
5867 * DPLL is enabled and the clocks are stable.
5868 *
5869 * So write it again.
5870 */
Eric Anholtfae14982011-03-30 13:01:09 -07005871 I915_WRITE(PCH_DPLL(pipe), dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08005872 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005873
Chris Wilson5eddb702010-09-11 13:48:45 +01005874 intel_crtc->lowfreq_avail = false;
Jesse Barnes4b645f12011-10-12 09:51:31 -07005875 if (!intel_crtc->no_pll) {
5876 if (is_lvds && has_reduced_clock && i915_powersave) {
5877 I915_WRITE(PCH_FP1(pipe), fp2);
5878 intel_crtc->lowfreq_avail = true;
5879 if (HAS_PIPE_CXSR(dev)) {
5880 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5881 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5882 }
5883 } else {
5884 I915_WRITE(PCH_FP1(pipe), fp);
5885 if (HAS_PIPE_CXSR(dev)) {
5886 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
5887 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
5888 }
Jesse Barnes652c3932009-08-17 13:31:43 -07005889 }
5890 }
5891
Krzysztof Halasa734b4152010-05-25 18:41:46 +02005892 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5893 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5894 /* the chip adds 2 halflines automatically */
5895 adjusted_mode->crtc_vdisplay -= 1;
5896 adjusted_mode->crtc_vtotal -= 1;
5897 adjusted_mode->crtc_vblank_start -= 1;
5898 adjusted_mode->crtc_vblank_end -= 1;
5899 adjusted_mode->crtc_vsync_end -= 1;
5900 adjusted_mode->crtc_vsync_start -= 1;
5901 } else
5902 pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
5903
Chris Wilson5eddb702010-09-11 13:48:45 +01005904 I915_WRITE(HTOTAL(pipe),
5905 (adjusted_mode->crtc_hdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08005906 ((adjusted_mode->crtc_htotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01005907 I915_WRITE(HBLANK(pipe),
5908 (adjusted_mode->crtc_hblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08005909 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01005910 I915_WRITE(HSYNC(pipe),
5911 (adjusted_mode->crtc_hsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08005912 ((adjusted_mode->crtc_hsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01005913
5914 I915_WRITE(VTOTAL(pipe),
5915 (adjusted_mode->crtc_vdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08005916 ((adjusted_mode->crtc_vtotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01005917 I915_WRITE(VBLANK(pipe),
5918 (adjusted_mode->crtc_vblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08005919 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01005920 I915_WRITE(VSYNC(pipe),
5921 (adjusted_mode->crtc_vsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08005922 ((adjusted_mode->crtc_vsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01005923
Eric Anholt8febb292011-03-30 13:01:07 -07005924 /* pipesrc controls the size that is scaled from, which should
5925 * always be the user's requested size.
Jesse Barnes79e53942008-11-07 14:24:08 -08005926 */
Chris Wilson5eddb702010-09-11 13:48:45 +01005927 I915_WRITE(PIPESRC(pipe),
5928 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
Zhenyu Wang2c072452009-06-05 15:38:42 +08005929
Eric Anholt8febb292011-03-30 13:01:07 -07005930 I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
5931 I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
5932 I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
5933 I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005934
Eric Anholt8febb292011-03-30 13:01:07 -07005935 if (has_edp_encoder &&
5936 !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
5937 ironlake_set_pll_edp(crtc, adjusted_mode->clock);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005938 }
5939
Chris Wilson5eddb702010-09-11 13:48:45 +01005940 I915_WRITE(PIPECONF(pipe), pipeconf);
5941 POSTING_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08005942
Jesse Barnes9d0498a2010-08-18 13:20:54 -07005943 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005944
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01005945 if (IS_GEN5(dev)) {
Zhenyu Wang553bd142009-09-02 10:57:52 +08005946 /* enable address swizzle for tiling buffer */
5947 temp = I915_READ(DISP_ARB_CTL);
5948 I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
5949 }
5950
Chris Wilson5eddb702010-09-11 13:48:45 +01005951 I915_WRITE(DSPCNTR(plane), dspcntr);
Jesse Barnesb24e7172011-01-04 15:09:30 -08005952 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08005953
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005954 ret = intel_pipe_set_base(crtc, x, y, old_fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08005955
5956 intel_update_watermarks(dev);
5957
Chris Wilson1f803ee2009-06-06 09:45:59 +01005958 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08005959}
5960
Eric Anholtf564048e2011-03-30 13:01:02 -07005961static int intel_crtc_mode_set(struct drm_crtc *crtc,
5962 struct drm_display_mode *mode,
5963 struct drm_display_mode *adjusted_mode,
5964 int x, int y,
5965 struct drm_framebuffer *old_fb)
5966{
5967 struct drm_device *dev = crtc->dev;
5968 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt0b701d22011-03-30 13:01:03 -07005969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5970 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07005971 int ret;
5972
Eric Anholt0b701d22011-03-30 13:01:03 -07005973 drm_vblank_pre_modeset(dev, pipe);
5974
Eric Anholtf564048e2011-03-30 13:01:02 -07005975 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
5976 x, y, old_fb);
5977
Jesse Barnes79e53942008-11-07 14:24:08 -08005978 drm_vblank_post_modeset(dev, pipe);
5979
Keith Packard120eced2011-07-27 01:21:40 -07005980 intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
5981
Jesse Barnes79e53942008-11-07 14:24:08 -08005982 return ret;
5983}
5984
Wu Fengguang3a9627f2011-12-09 20:42:19 +08005985static bool intel_eld_uptodate(struct drm_connector *connector,
5986 int reg_eldv, uint32_t bits_eldv,
5987 int reg_elda, uint32_t bits_elda,
5988 int reg_edid)
5989{
5990 struct drm_i915_private *dev_priv = connector->dev->dev_private;
5991 uint8_t *eld = connector->eld;
5992 uint32_t i;
5993
5994 i = I915_READ(reg_eldv);
5995 i &= bits_eldv;
5996
5997 if (!eld[0])
5998 return !i;
5999
6000 if (!i)
6001 return false;
6002
6003 i = I915_READ(reg_elda);
6004 i &= ~bits_elda;
6005 I915_WRITE(reg_elda, i);
6006
6007 for (i = 0; i < eld[2]; i++)
6008 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6009 return false;
6010
6011 return true;
6012}
6013
Wu Fengguange0dac652011-09-05 14:25:34 +08006014static void g4x_write_eld(struct drm_connector *connector,
6015 struct drm_crtc *crtc)
6016{
6017 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6018 uint8_t *eld = connector->eld;
6019 uint32_t eldv;
6020 uint32_t len;
6021 uint32_t i;
6022
6023 i = I915_READ(G4X_AUD_VID_DID);
6024
6025 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6026 eldv = G4X_ELDV_DEVCL_DEVBLC;
6027 else
6028 eldv = G4X_ELDV_DEVCTG;
6029
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006030 if (intel_eld_uptodate(connector,
6031 G4X_AUD_CNTL_ST, eldv,
6032 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6033 G4X_HDMIW_HDMIEDID))
6034 return;
6035
Wu Fengguange0dac652011-09-05 14:25:34 +08006036 i = I915_READ(G4X_AUD_CNTL_ST);
6037 i &= ~(eldv | G4X_ELD_ADDR);
6038 len = (i >> 9) & 0x1f; /* ELD buffer size */
6039 I915_WRITE(G4X_AUD_CNTL_ST, i);
6040
6041 if (!eld[0])
6042 return;
6043
6044 len = min_t(uint8_t, eld[2], len);
6045 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6046 for (i = 0; i < len; i++)
6047 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6048
6049 i = I915_READ(G4X_AUD_CNTL_ST);
6050 i |= eldv;
6051 I915_WRITE(G4X_AUD_CNTL_ST, i);
6052}
6053
6054static void ironlake_write_eld(struct drm_connector *connector,
6055 struct drm_crtc *crtc)
6056{
6057 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6058 uint8_t *eld = connector->eld;
6059 uint32_t eldv;
6060 uint32_t i;
6061 int len;
6062 int hdmiw_hdmiedid;
6063 int aud_cntl_st;
6064 int aud_cntrl_st2;
6065
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006066 if (HAS_PCH_IBX(connector->dev)) {
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006067 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
6068 aud_cntl_st = IBX_AUD_CNTL_ST_A;
6069 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006070 } else {
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006071 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
6072 aud_cntl_st = CPT_AUD_CNTL_ST_A;
6073 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006074 }
6075
6076 i = to_intel_crtc(crtc)->pipe;
6077 hdmiw_hdmiedid += i * 0x100;
6078 aud_cntl_st += i * 0x100;
6079
6080 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(i));
6081
6082 i = I915_READ(aud_cntl_st);
6083 i = (i >> 29) & 0x3; /* DIP_Port_Select, 0x1 = PortB */
6084 if (!i) {
6085 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6086 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006087 eldv = IBX_ELD_VALIDB;
6088 eldv |= IBX_ELD_VALIDB << 4;
6089 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006090 } else {
6091 DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006092 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006093 }
6094
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006095 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6096 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6097 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
6098 }
6099
6100 if (intel_eld_uptodate(connector,
6101 aud_cntrl_st2, eldv,
6102 aud_cntl_st, IBX_ELD_ADDRESS,
6103 hdmiw_hdmiedid))
6104 return;
6105
Wu Fengguange0dac652011-09-05 14:25:34 +08006106 i = I915_READ(aud_cntrl_st2);
6107 i &= ~eldv;
6108 I915_WRITE(aud_cntrl_st2, i);
6109
6110 if (!eld[0])
6111 return;
6112
Wu Fengguange0dac652011-09-05 14:25:34 +08006113 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006114 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006115 I915_WRITE(aud_cntl_st, i);
6116
6117 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6118 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6119 for (i = 0; i < len; i++)
6120 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6121
6122 i = I915_READ(aud_cntrl_st2);
6123 i |= eldv;
6124 I915_WRITE(aud_cntrl_st2, i);
6125}
6126
6127void intel_write_eld(struct drm_encoder *encoder,
6128 struct drm_display_mode *mode)
6129{
6130 struct drm_crtc *crtc = encoder->crtc;
6131 struct drm_connector *connector;
6132 struct drm_device *dev = encoder->dev;
6133 struct drm_i915_private *dev_priv = dev->dev_private;
6134
6135 connector = drm_select_eld(encoder, mode);
6136 if (!connector)
6137 return;
6138
6139 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6140 connector->base.id,
6141 drm_get_connector_name(connector),
6142 connector->encoder->base.id,
6143 drm_get_encoder_name(connector->encoder));
6144
6145 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6146
6147 if (dev_priv->display.write_eld)
6148 dev_priv->display.write_eld(connector, crtc);
6149}
6150
Jesse Barnes79e53942008-11-07 14:24:08 -08006151/** Loads the palette/gamma unit for the CRTC with the prepared values */
6152void intel_crtc_load_lut(struct drm_crtc *crtc)
6153{
6154 struct drm_device *dev = crtc->dev;
6155 struct drm_i915_private *dev_priv = dev->dev_private;
6156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006157 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006158 int i;
6159
6160 /* The clocks have to be on to load the palette. */
6161 if (!crtc->enabled)
6162 return;
6163
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006164 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006165 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006166 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006167
Jesse Barnes79e53942008-11-07 14:24:08 -08006168 for (i = 0; i < 256; i++) {
6169 I915_WRITE(palreg + 4 * i,
6170 (intel_crtc->lut_r[i] << 16) |
6171 (intel_crtc->lut_g[i] << 8) |
6172 intel_crtc->lut_b[i]);
6173 }
6174}
6175
Chris Wilson560b85b2010-08-07 11:01:38 +01006176static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6177{
6178 struct drm_device *dev = crtc->dev;
6179 struct drm_i915_private *dev_priv = dev->dev_private;
6180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6181 bool visible = base != 0;
6182 u32 cntl;
6183
6184 if (intel_crtc->cursor_visible == visible)
6185 return;
6186
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006187 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006188 if (visible) {
6189 /* On these chipsets we can only modify the base whilst
6190 * the cursor is disabled.
6191 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006192 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006193
6194 cntl &= ~(CURSOR_FORMAT_MASK);
6195 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6196 cntl |= CURSOR_ENABLE |
6197 CURSOR_GAMMA_ENABLE |
6198 CURSOR_FORMAT_ARGB;
6199 } else
6200 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006201 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006202
6203 intel_crtc->cursor_visible = visible;
6204}
6205
6206static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6207{
6208 struct drm_device *dev = crtc->dev;
6209 struct drm_i915_private *dev_priv = dev->dev_private;
6210 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6211 int pipe = intel_crtc->pipe;
6212 bool visible = base != 0;
6213
6214 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006215 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006216 if (base) {
6217 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6218 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6219 cntl |= pipe << 28; /* Connect to correct pipe */
6220 } else {
6221 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6222 cntl |= CURSOR_MODE_DISABLE;
6223 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006224 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006225
6226 intel_crtc->cursor_visible = visible;
6227 }
6228 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006229 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006230}
6231
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006232static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6233{
6234 struct drm_device *dev = crtc->dev;
6235 struct drm_i915_private *dev_priv = dev->dev_private;
6236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6237 int pipe = intel_crtc->pipe;
6238 bool visible = base != 0;
6239
6240 if (intel_crtc->cursor_visible != visible) {
6241 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6242 if (base) {
6243 cntl &= ~CURSOR_MODE;
6244 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6245 } else {
6246 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6247 cntl |= CURSOR_MODE_DISABLE;
6248 }
6249 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6250
6251 intel_crtc->cursor_visible = visible;
6252 }
6253 /* and commit changes on next vblank */
6254 I915_WRITE(CURBASE_IVB(pipe), base);
6255}
6256
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006257/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006258static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6259 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006260{
6261 struct drm_device *dev = crtc->dev;
6262 struct drm_i915_private *dev_priv = dev->dev_private;
6263 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6264 int pipe = intel_crtc->pipe;
6265 int x = intel_crtc->cursor_x;
6266 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006267 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006268 bool visible;
6269
6270 pos = 0;
6271
Chris Wilson6b383a72010-09-13 13:54:26 +01006272 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006273 base = intel_crtc->cursor_addr;
6274 if (x > (int) crtc->fb->width)
6275 base = 0;
6276
6277 if (y > (int) crtc->fb->height)
6278 base = 0;
6279 } else
6280 base = 0;
6281
6282 if (x < 0) {
6283 if (x + intel_crtc->cursor_width < 0)
6284 base = 0;
6285
6286 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6287 x = -x;
6288 }
6289 pos |= x << CURSOR_X_SHIFT;
6290
6291 if (y < 0) {
6292 if (y + intel_crtc->cursor_height < 0)
6293 base = 0;
6294
6295 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6296 y = -y;
6297 }
6298 pos |= y << CURSOR_Y_SHIFT;
6299
6300 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006301 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006302 return;
6303
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006304 if (IS_IVYBRIDGE(dev)) {
6305 I915_WRITE(CURPOS_IVB(pipe), pos);
6306 ivb_update_cursor(crtc, base);
6307 } else {
6308 I915_WRITE(CURPOS(pipe), pos);
6309 if (IS_845G(dev) || IS_I865G(dev))
6310 i845_update_cursor(crtc, base);
6311 else
6312 i9xx_update_cursor(crtc, base);
6313 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006314
6315 if (visible)
6316 intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
6317}
6318
Jesse Barnes79e53942008-11-07 14:24:08 -08006319static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006320 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006321 uint32_t handle,
6322 uint32_t width, uint32_t height)
6323{
6324 struct drm_device *dev = crtc->dev;
6325 struct drm_i915_private *dev_priv = dev->dev_private;
6326 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006327 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006328 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006329 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006330
Zhao Yakui28c97732009-10-09 11:39:41 +08006331 DRM_DEBUG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08006332
6333 /* if we want to turn off the cursor ignore width and height */
6334 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006335 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006336 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006337 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006338 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006339 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006340 }
6341
6342 /* Currently we only support 64x64 cursors */
6343 if (width != 64 || height != 64) {
6344 DRM_ERROR("we currently only support 64x64 cursors\n");
6345 return -EINVAL;
6346 }
6347
Chris Wilson05394f32010-11-08 19:18:58 +00006348 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006349 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006350 return -ENOENT;
6351
Chris Wilson05394f32010-11-08 19:18:58 +00006352 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006353 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006354 ret = -ENOMEM;
6355 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006356 }
6357
Dave Airlie71acb5e2008-12-30 20:31:46 +10006358 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006359 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006360 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00006361 if (obj->tiling_mode) {
6362 DRM_ERROR("cursor cannot be tiled\n");
6363 ret = -EINVAL;
6364 goto fail_locked;
6365 }
6366
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006367 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006368 if (ret) {
6369 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006370 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006371 }
6372
Chris Wilsond9e86c02010-11-10 16:40:20 +00006373 ret = i915_gem_object_put_fence(obj);
6374 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006375 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006376 goto fail_unpin;
6377 }
6378
Chris Wilson05394f32010-11-08 19:18:58 +00006379 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006380 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006381 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006382 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006383 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6384 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006385 if (ret) {
6386 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006387 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006388 }
Chris Wilson05394f32010-11-08 19:18:58 +00006389 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006390 }
6391
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006392 if (IS_GEN2(dev))
Jesse Barnes14b603912009-05-20 16:47:08 -04006393 I915_WRITE(CURSIZE, (height << 12) | width);
6394
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006395 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006396 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006397 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006398 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006399 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6400 } else
6401 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006402 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006403 }
Jesse Barnes80824002009-09-10 15:28:06 -07006404
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006405 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006406
6407 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006408 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006409 intel_crtc->cursor_width = width;
6410 intel_crtc->cursor_height = height;
6411
Chris Wilson6b383a72010-09-13 13:54:26 +01006412 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006413
Jesse Barnes79e53942008-11-07 14:24:08 -08006414 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006415fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006416 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006417fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006418 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006419fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006420 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006421 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006422}
6423
6424static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6425{
Jesse Barnes79e53942008-11-07 14:24:08 -08006426 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006427
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006428 intel_crtc->cursor_x = x;
6429 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006430
Chris Wilson6b383a72010-09-13 13:54:26 +01006431 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08006432
6433 return 0;
6434}
6435
6436/** Sets the color ramps on behalf of RandR */
6437void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6438 u16 blue, int regno)
6439{
6440 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6441
6442 intel_crtc->lut_r[regno] = red >> 8;
6443 intel_crtc->lut_g[regno] = green >> 8;
6444 intel_crtc->lut_b[regno] = blue >> 8;
6445}
6446
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006447void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6448 u16 *blue, int regno)
6449{
6450 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6451
6452 *red = intel_crtc->lut_r[regno] << 8;
6453 *green = intel_crtc->lut_g[regno] << 8;
6454 *blue = intel_crtc->lut_b[regno] << 8;
6455}
6456
Jesse Barnes79e53942008-11-07 14:24:08 -08006457static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006458 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006459{
James Simmons72034252010-08-03 01:33:19 +01006460 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006461 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006462
James Simmons72034252010-08-03 01:33:19 +01006463 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006464 intel_crtc->lut_r[i] = red[i] >> 8;
6465 intel_crtc->lut_g[i] = green[i] >> 8;
6466 intel_crtc->lut_b[i] = blue[i] >> 8;
6467 }
6468
6469 intel_crtc_load_lut(crtc);
6470}
6471
6472/**
6473 * Get a pipe with a simple mode set on it for doing load-based monitor
6474 * detection.
6475 *
6476 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07006477 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08006478 *
Eric Anholtc751ce42010-03-25 11:48:48 -07006479 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08006480 * configured for it. In the future, it could choose to temporarily disable
6481 * some outputs to free up a pipe for its use.
6482 *
6483 * \return crtc, or NULL if no pipes are available.
6484 */
6485
6486/* VESA 640x480x72Hz mode to set on the pipe */
6487static struct drm_display_mode load_detect_mode = {
6488 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6489 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6490};
6491
Chris Wilsond2dff872011-04-19 08:36:26 +01006492static struct drm_framebuffer *
6493intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006494 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006495 struct drm_i915_gem_object *obj)
6496{
6497 struct intel_framebuffer *intel_fb;
6498 int ret;
6499
6500 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6501 if (!intel_fb) {
6502 drm_gem_object_unreference_unlocked(&obj->base);
6503 return ERR_PTR(-ENOMEM);
6504 }
6505
6506 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6507 if (ret) {
6508 drm_gem_object_unreference_unlocked(&obj->base);
6509 kfree(intel_fb);
6510 return ERR_PTR(ret);
6511 }
6512
6513 return &intel_fb->base;
6514}
6515
6516static u32
6517intel_framebuffer_pitch_for_width(int width, int bpp)
6518{
6519 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6520 return ALIGN(pitch, 64);
6521}
6522
6523static u32
6524intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6525{
6526 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6527 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6528}
6529
6530static struct drm_framebuffer *
6531intel_framebuffer_create_for_mode(struct drm_device *dev,
6532 struct drm_display_mode *mode,
6533 int depth, int bpp)
6534{
6535 struct drm_i915_gem_object *obj;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006536 struct drm_mode_fb_cmd2 mode_cmd;
Chris Wilsond2dff872011-04-19 08:36:26 +01006537
6538 obj = i915_gem_alloc_object(dev,
6539 intel_framebuffer_size_for_mode(mode, bpp));
6540 if (obj == NULL)
6541 return ERR_PTR(-ENOMEM);
6542
6543 mode_cmd.width = mode->hdisplay;
6544 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006545 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6546 bpp);
6547 mode_cmd.pixel_format = 0;
Chris Wilsond2dff872011-04-19 08:36:26 +01006548
6549 return intel_framebuffer_create(dev, &mode_cmd, obj);
6550}
6551
6552static struct drm_framebuffer *
6553mode_fits_in_fbdev(struct drm_device *dev,
6554 struct drm_display_mode *mode)
6555{
6556 struct drm_i915_private *dev_priv = dev->dev_private;
6557 struct drm_i915_gem_object *obj;
6558 struct drm_framebuffer *fb;
6559
6560 if (dev_priv->fbdev == NULL)
6561 return NULL;
6562
6563 obj = dev_priv->fbdev->ifb.obj;
6564 if (obj == NULL)
6565 return NULL;
6566
6567 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006568 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6569 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006570 return NULL;
6571
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006572 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006573 return NULL;
6574
6575 return fb;
6576}
6577
Chris Wilson71731882011-04-19 23:10:58 +01006578bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
6579 struct drm_connector *connector,
6580 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006581 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006582{
6583 struct intel_crtc *intel_crtc;
6584 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006585 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006586 struct drm_crtc *crtc = NULL;
6587 struct drm_device *dev = encoder->dev;
Chris Wilsond2dff872011-04-19 08:36:26 +01006588 struct drm_framebuffer *old_fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006589 int i = -1;
6590
Chris Wilsond2dff872011-04-19 08:36:26 +01006591 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6592 connector->base.id, drm_get_connector_name(connector),
6593 encoder->base.id, drm_get_encoder_name(encoder));
6594
Jesse Barnes79e53942008-11-07 14:24:08 -08006595 /*
6596 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006597 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006598 * - if the connector already has an assigned crtc, use it (but make
6599 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006600 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006601 * - try to find the first unused crtc that can drive this connector,
6602 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006603 */
6604
6605 /* See if we already have a CRTC for this connector */
6606 if (encoder->crtc) {
6607 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006608
Jesse Barnes79e53942008-11-07 14:24:08 -08006609 intel_crtc = to_intel_crtc(crtc);
Chris Wilson8261b192011-04-19 23:18:09 +01006610 old->dpms_mode = intel_crtc->dpms_mode;
6611 old->load_detect_temp = false;
6612
6613 /* Make sure the crtc and connector are running */
Jesse Barnes79e53942008-11-07 14:24:08 -08006614 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
Chris Wilson64927112011-04-20 07:25:26 +01006615 struct drm_encoder_helper_funcs *encoder_funcs;
6616 struct drm_crtc_helper_funcs *crtc_funcs;
6617
Jesse Barnes79e53942008-11-07 14:24:08 -08006618 crtc_funcs = crtc->helper_private;
6619 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Chris Wilson64927112011-04-20 07:25:26 +01006620
6621 encoder_funcs = encoder->helper_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006622 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
6623 }
Chris Wilson8261b192011-04-19 23:18:09 +01006624
Chris Wilson71731882011-04-19 23:10:58 +01006625 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006626 }
6627
6628 /* Find an unused one (if possible) */
6629 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6630 i++;
6631 if (!(encoder->possible_crtcs & (1 << i)))
6632 continue;
6633 if (!possible_crtc->enabled) {
6634 crtc = possible_crtc;
6635 break;
6636 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006637 }
6638
6639 /*
6640 * If we didn't find an unused CRTC, don't use any.
6641 */
6642 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006643 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6644 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006645 }
6646
6647 encoder->crtc = crtc;
Zhenyu Wangc1c43972010-03-30 14:39:30 +08006648 connector->encoder = encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006649
6650 intel_crtc = to_intel_crtc(crtc);
Chris Wilson8261b192011-04-19 23:18:09 +01006651 old->dpms_mode = intel_crtc->dpms_mode;
6652 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006653 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006654
Chris Wilson64927112011-04-20 07:25:26 +01006655 if (!mode)
6656 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006657
Chris Wilsond2dff872011-04-19 08:36:26 +01006658 old_fb = crtc->fb;
6659
6660 /* We need a framebuffer large enough to accommodate all accesses
6661 * that the plane may generate whilst we perform load detection.
6662 * We can not rely on the fbcon either being present (we get called
6663 * during its initialisation to detect all boot displays, or it may
6664 * not even exist) or that it is large enough to satisfy the
6665 * requested mode.
6666 */
6667 crtc->fb = mode_fits_in_fbdev(dev, mode);
6668 if (crtc->fb == NULL) {
6669 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
6670 crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6671 old->release_fb = crtc->fb;
6672 } else
6673 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
6674 if (IS_ERR(crtc->fb)) {
6675 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
6676 crtc->fb = old_fb;
6677 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006678 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006679
6680 if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006681 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006682 if (old->release_fb)
6683 old->release_fb->funcs->destroy(old->release_fb);
6684 crtc->fb = old_fb;
Chris Wilson64927112011-04-20 07:25:26 +01006685 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006686 }
Chris Wilson71731882011-04-19 23:10:58 +01006687
Jesse Barnes79e53942008-11-07 14:24:08 -08006688 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006689 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006690
Chris Wilson71731882011-04-19 23:10:58 +01006691 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006692}
6693
Zhenyu Wangc1c43972010-03-30 14:39:30 +08006694void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
Chris Wilson8261b192011-04-19 23:18:09 +01006695 struct drm_connector *connector,
6696 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006697{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006698 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006699 struct drm_device *dev = encoder->dev;
6700 struct drm_crtc *crtc = encoder->crtc;
6701 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
6702 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
6703
Chris Wilsond2dff872011-04-19 08:36:26 +01006704 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6705 connector->base.id, drm_get_connector_name(connector),
6706 encoder->base.id, drm_get_encoder_name(encoder));
6707
Chris Wilson8261b192011-04-19 23:18:09 +01006708 if (old->load_detect_temp) {
Zhenyu Wangc1c43972010-03-30 14:39:30 +08006709 connector->encoder = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006710 drm_helper_disable_unused_functions(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +01006711
6712 if (old->release_fb)
6713 old->release_fb->funcs->destroy(old->release_fb);
6714
Chris Wilson0622a532011-04-21 09:32:11 +01006715 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006716 }
6717
Eric Anholtc751ce42010-03-25 11:48:48 -07006718 /* Switch crtc and encoder back off if necessary */
Chris Wilson0622a532011-04-21 09:32:11 +01006719 if (old->dpms_mode != DRM_MODE_DPMS_ON) {
6720 encoder_funcs->dpms(encoder, old->dpms_mode);
Chris Wilson8261b192011-04-19 23:18:09 +01006721 crtc_funcs->dpms(crtc, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006722 }
6723}
6724
6725/* Returns the clock of the currently programmed mode of the given pipe. */
6726static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6727{
6728 struct drm_i915_private *dev_priv = dev->dev_private;
6729 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6730 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08006731 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006732 u32 fp;
6733 intel_clock_t clock;
6734
6735 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006736 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006737 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006738 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006739
6740 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006741 if (IS_PINEVIEW(dev)) {
6742 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6743 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006744 } else {
6745 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6746 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6747 }
6748
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006749 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006750 if (IS_PINEVIEW(dev))
6751 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6752 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006753 else
6754 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006755 DPLL_FPA01_P1_POST_DIV_SHIFT);
6756
6757 switch (dpll & DPLL_MODE_MASK) {
6758 case DPLLB_MODE_DAC_SERIAL:
6759 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6760 5 : 10;
6761 break;
6762 case DPLLB_MODE_LVDS:
6763 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6764 7 : 14;
6765 break;
6766 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006767 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006768 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6769 return 0;
6770 }
6771
6772 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08006773 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006774 } else {
6775 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6776
6777 if (is_lvds) {
6778 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6779 DPLL_FPA01_P1_POST_DIV_SHIFT);
6780 clock.p2 = 14;
6781
6782 if ((dpll & PLL_REF_INPUT_MASK) ==
6783 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6784 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08006785 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006786 } else
Shaohua Li21778322009-02-23 15:19:16 +08006787 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006788 } else {
6789 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6790 clock.p1 = 2;
6791 else {
6792 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6793 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6794 }
6795 if (dpll & PLL_P2_DIVIDE_BY_4)
6796 clock.p2 = 4;
6797 else
6798 clock.p2 = 2;
6799
Shaohua Li21778322009-02-23 15:19:16 +08006800 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006801 }
6802 }
6803
6804 /* XXX: It would be nice to validate the clocks, but we can't reuse
6805 * i830PllIsValid() because it relies on the xf86_config connector
6806 * configuration being accurate, which it isn't necessarily.
6807 */
6808
6809 return clock.dot;
6810}
6811
6812/** Returns the currently programmed mode of the given pipe. */
6813struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6814 struct drm_crtc *crtc)
6815{
Jesse Barnes548f2452011-02-17 10:40:53 -08006816 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6818 int pipe = intel_crtc->pipe;
6819 struct drm_display_mode *mode;
Jesse Barnes548f2452011-02-17 10:40:53 -08006820 int htot = I915_READ(HTOTAL(pipe));
6821 int hsync = I915_READ(HSYNC(pipe));
6822 int vtot = I915_READ(VTOTAL(pipe));
6823 int vsync = I915_READ(VSYNC(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006824
6825 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6826 if (!mode)
6827 return NULL;
6828
6829 mode->clock = intel_crtc_clock_get(dev, crtc);
6830 mode->hdisplay = (htot & 0xffff) + 1;
6831 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6832 mode->hsync_start = (hsync & 0xffff) + 1;
6833 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6834 mode->vdisplay = (vtot & 0xffff) + 1;
6835 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6836 mode->vsync_start = (vsync & 0xffff) + 1;
6837 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6838
6839 drm_mode_set_name(mode);
6840 drm_mode_set_crtcinfo(mode, 0);
6841
6842 return mode;
6843}
6844
Jesse Barnes652c3932009-08-17 13:31:43 -07006845#define GPU_IDLE_TIMEOUT 500 /* ms */
6846
6847/* When this timer fires, we've been idle for awhile */
6848static void intel_gpu_idle_timer(unsigned long arg)
6849{
6850 struct drm_device *dev = (struct drm_device *)arg;
6851 drm_i915_private_t *dev_priv = dev->dev_private;
6852
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00006853 if (!list_empty(&dev_priv->mm.active_list)) {
6854 /* Still processing requests, so just re-arm the timer. */
6855 mod_timer(&dev_priv->idle_timer, jiffies +
6856 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
6857 return;
6858 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006859
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00006860 dev_priv->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07006861 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07006862}
6863
Jesse Barnes652c3932009-08-17 13:31:43 -07006864#define CRTC_IDLE_TIMEOUT 1000 /* ms */
6865
6866static void intel_crtc_idle_timer(unsigned long arg)
6867{
6868 struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
6869 struct drm_crtc *crtc = &intel_crtc->base;
6870 drm_i915_private_t *dev_priv = crtc->dev->dev_private;
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00006871 struct intel_framebuffer *intel_fb;
6872
6873 intel_fb = to_intel_framebuffer(crtc->fb);
6874 if (intel_fb && intel_fb->obj->active) {
6875 /* The framebuffer is still being accessed by the GPU. */
6876 mod_timer(&intel_crtc->idle_timer, jiffies +
6877 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
6878 return;
6879 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006880
Jesse Barnes652c3932009-08-17 13:31:43 -07006881 intel_crtc->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07006882 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07006883}
6884
Daniel Vetter3dec0092010-08-20 21:40:52 +02006885static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07006886{
6887 struct drm_device *dev = crtc->dev;
6888 drm_i915_private_t *dev_priv = dev->dev_private;
6889 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6890 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006891 int dpll_reg = DPLL(pipe);
6892 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07006893
Eric Anholtbad720f2009-10-22 16:11:14 -07006894 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006895 return;
6896
6897 if (!dev_priv->lvds_downclock_avail)
6898 return;
6899
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006900 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07006901 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08006902 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006903
6904 /* Unlock panel regs */
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006905 I915_WRITE(PP_CONTROL,
6906 I915_READ(PP_CONTROL) | PANEL_UNLOCK_REGS);
Jesse Barnes652c3932009-08-17 13:31:43 -07006907
6908 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
6909 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006910 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08006911
Jesse Barnes652c3932009-08-17 13:31:43 -07006912 dpll = I915_READ(dpll_reg);
6913 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08006914 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006915
6916 /* ...and lock them again */
6917 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
6918 }
6919
6920 /* Schedule downclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02006921 mod_timer(&intel_crtc->idle_timer, jiffies +
6922 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07006923}
6924
6925static void intel_decrease_pllclock(struct drm_crtc *crtc)
6926{
6927 struct drm_device *dev = crtc->dev;
6928 drm_i915_private_t *dev_priv = dev->dev_private;
6929 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6930 int pipe = intel_crtc->pipe;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006931 int dpll_reg = DPLL(pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006932 int dpll = I915_READ(dpll_reg);
6933
Eric Anholtbad720f2009-10-22 16:11:14 -07006934 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07006935 return;
6936
6937 if (!dev_priv->lvds_downclock_avail)
6938 return;
6939
6940 /*
6941 * Since this is called by a timer, we should never get here in
6942 * the manual case.
6943 */
6944 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08006945 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006946
6947 /* Unlock panel regs */
Jesse Barnes4a655f02010-07-22 13:18:18 -07006948 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
6949 PANEL_UNLOCK_REGS);
Jesse Barnes652c3932009-08-17 13:31:43 -07006950
6951 dpll |= DISPLAY_RATE_SELECT_FPA1;
6952 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006953 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07006954 dpll = I915_READ(dpll_reg);
6955 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08006956 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07006957
6958 /* ...and lock them again */
6959 I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
6960 }
6961
6962}
6963
6964/**
6965 * intel_idle_update - adjust clocks for idleness
6966 * @work: work struct
6967 *
6968 * Either the GPU or display (or both) went idle. Check the busy status
6969 * here and adjust the CRTC and GPU clocks as necessary.
6970 */
6971static void intel_idle_update(struct work_struct *work)
6972{
6973 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
6974 idle_work);
6975 struct drm_device *dev = dev_priv->dev;
6976 struct drm_crtc *crtc;
6977 struct intel_crtc *intel_crtc;
6978
6979 if (!i915_powersave)
6980 return;
6981
6982 mutex_lock(&dev->struct_mutex);
6983
Jesse Barnes7648fa92010-05-20 14:28:11 -07006984 i915_update_gfx_val(dev_priv);
6985
Jesse Barnes652c3932009-08-17 13:31:43 -07006986 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
6987 /* Skip inactive CRTCs */
6988 if (!crtc->fb)
6989 continue;
6990
6991 intel_crtc = to_intel_crtc(crtc);
6992 if (!intel_crtc->busy)
6993 intel_decrease_pllclock(crtc);
6994 }
6995
Li Peng45ac22c2010-06-12 23:38:35 +08006996
Jesse Barnes652c3932009-08-17 13:31:43 -07006997 mutex_unlock(&dev->struct_mutex);
6998}
6999
7000/**
7001 * intel_mark_busy - mark the GPU and possibly the display busy
7002 * @dev: drm device
7003 * @obj: object we're operating on
7004 *
7005 * Callers can use this function to indicate that the GPU is busy processing
7006 * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
7007 * buffer), we'll also mark the display as busy, so we know to increase its
7008 * clock frequency.
7009 */
Chris Wilson05394f32010-11-08 19:18:58 +00007010void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
Jesse Barnes652c3932009-08-17 13:31:43 -07007011{
7012 drm_i915_private_t *dev_priv = dev->dev_private;
7013 struct drm_crtc *crtc = NULL;
7014 struct intel_framebuffer *intel_fb;
7015 struct intel_crtc *intel_crtc;
7016
Zhenyu Wang5e17ee72009-09-03 09:30:06 +08007017 if (!drm_core_check_feature(dev, DRIVER_MODESET))
7018 return;
7019
Alexander Lam18b21902011-01-03 13:28:56 -05007020 if (!dev_priv->busy)
Chris Wilson28cf7982009-11-30 01:08:56 +00007021 dev_priv->busy = true;
Alexander Lam18b21902011-01-03 13:28:56 -05007022 else
Chris Wilson28cf7982009-11-30 01:08:56 +00007023 mod_timer(&dev_priv->idle_timer, jiffies +
7024 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07007025
7026 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7027 if (!crtc->fb)
7028 continue;
7029
7030 intel_crtc = to_intel_crtc(crtc);
7031 intel_fb = to_intel_framebuffer(crtc->fb);
7032 if (intel_fb->obj == obj) {
7033 if (!intel_crtc->busy) {
7034 /* Non-busy -> busy, upclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02007035 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007036 intel_crtc->busy = true;
7037 } else {
7038 /* Busy -> busy, put off timer */
7039 mod_timer(&intel_crtc->idle_timer, jiffies +
7040 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
7041 }
7042 }
7043 }
7044}
7045
Jesse Barnes79e53942008-11-07 14:24:08 -08007046static void intel_crtc_destroy(struct drm_crtc *crtc)
7047{
7048 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007049 struct drm_device *dev = crtc->dev;
7050 struct intel_unpin_work *work;
7051 unsigned long flags;
7052
7053 spin_lock_irqsave(&dev->event_lock, flags);
7054 work = intel_crtc->unpin_work;
7055 intel_crtc->unpin_work = NULL;
7056 spin_unlock_irqrestore(&dev->event_lock, flags);
7057
7058 if (work) {
7059 cancel_work_sync(&work->work);
7060 kfree(work);
7061 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007062
7063 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007064
Jesse Barnes79e53942008-11-07 14:24:08 -08007065 kfree(intel_crtc);
7066}
7067
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007068static void intel_unpin_work_fn(struct work_struct *__work)
7069{
7070 struct intel_unpin_work *work =
7071 container_of(__work, struct intel_unpin_work, work);
7072
7073 mutex_lock(&work->dev->struct_mutex);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08007074 i915_gem_object_unpin(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007075 drm_gem_object_unreference(&work->pending_flip_obj->base);
7076 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007077
Chris Wilson7782de32011-07-08 12:22:41 +01007078 intel_update_fbc(work->dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007079 mutex_unlock(&work->dev->struct_mutex);
7080 kfree(work);
7081}
7082
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007083static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007084 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007085{
7086 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007087 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7088 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00007089 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007090 struct drm_pending_vblank_event *e;
Mario Kleiner49b14a52010-12-09 07:00:07 +01007091 struct timeval tnow, tvbl;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007092 unsigned long flags;
7093
7094 /* Ignore early vblank irqs */
7095 if (intel_crtc == NULL)
7096 return;
7097
Mario Kleiner49b14a52010-12-09 07:00:07 +01007098 do_gettimeofday(&tnow);
7099
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007100 spin_lock_irqsave(&dev->event_lock, flags);
7101 work = intel_crtc->unpin_work;
7102 if (work == NULL || !work->pending) {
7103 spin_unlock_irqrestore(&dev->event_lock, flags);
7104 return;
7105 }
7106
7107 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007108
7109 if (work->event) {
7110 e = work->event;
Mario Kleiner49b14a52010-12-09 07:00:07 +01007111 e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007112
7113 /* Called before vblank count and timestamps have
7114 * been updated for the vblank interval of flip
7115 * completion? Need to increment vblank count and
7116 * add one videorefresh duration to returned timestamp
Mario Kleiner49b14a52010-12-09 07:00:07 +01007117 * to account for this. We assume this happened if we
7118 * get called over 0.9 frame durations after the last
7119 * timestamped vblank.
7120 *
7121 * This calculation can not be used with vrefresh rates
7122 * below 5Hz (10Hz to be on the safe side) without
7123 * promoting to 64 integers.
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007124 */
Mario Kleiner49b14a52010-12-09 07:00:07 +01007125 if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
7126 9 * crtc->framedur_ns) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007127 e->event.sequence++;
Mario Kleiner49b14a52010-12-09 07:00:07 +01007128 tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
7129 crtc->framedur_ns);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007130 }
7131
Mario Kleiner49b14a52010-12-09 07:00:07 +01007132 e->event.tv_sec = tvbl.tv_sec;
7133 e->event.tv_usec = tvbl.tv_usec;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007134
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007135 list_add_tail(&e->base.link,
7136 &e->base.file_priv->event_list);
7137 wake_up_interruptible(&e->base.file_priv->event_wait);
7138 }
7139
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007140 drm_vblank_put(dev, intel_crtc->pipe);
7141
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007142 spin_unlock_irqrestore(&dev->event_lock, flags);
7143
Chris Wilson05394f32010-11-08 19:18:58 +00007144 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00007145
Chris Wilsone59f2ba2010-10-07 17:28:15 +01007146 atomic_clear_mask(1 << intel_crtc->plane,
Chris Wilson05394f32010-11-08 19:18:58 +00007147 &obj->pending_flip.counter);
7148 if (atomic_read(&obj->pending_flip) == 0)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01007149 wake_up(&dev_priv->pending_flip_queue);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007150
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007151 schedule_work(&work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007152
7153 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007154}
7155
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007156void intel_finish_page_flip(struct drm_device *dev, int pipe)
7157{
7158 drm_i915_private_t *dev_priv = dev->dev_private;
7159 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7160
Mario Kleiner49b14a52010-12-09 07:00:07 +01007161 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007162}
7163
7164void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7165{
7166 drm_i915_private_t *dev_priv = dev->dev_private;
7167 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7168
Mario Kleiner49b14a52010-12-09 07:00:07 +01007169 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007170}
7171
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007172void intel_prepare_page_flip(struct drm_device *dev, int plane)
7173{
7174 drm_i915_private_t *dev_priv = dev->dev_private;
7175 struct intel_crtc *intel_crtc =
7176 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7177 unsigned long flags;
7178
7179 spin_lock_irqsave(&dev->event_lock, flags);
Jesse Barnesde3f4402010-01-14 13:18:02 -08007180 if (intel_crtc->unpin_work) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007181 if ((++intel_crtc->unpin_work->pending) > 1)
7182 DRM_ERROR("Prepared flip multiple times\n");
Jesse Barnesde3f4402010-01-14 13:18:02 -08007183 } else {
7184 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
7185 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007186 spin_unlock_irqrestore(&dev->event_lock, flags);
7187}
7188
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007189static int intel_gen2_queue_flip(struct drm_device *dev,
7190 struct drm_crtc *crtc,
7191 struct drm_framebuffer *fb,
7192 struct drm_i915_gem_object *obj)
7193{
7194 struct drm_i915_private *dev_priv = dev->dev_private;
7195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7196 unsigned long offset;
7197 u32 flip_mask;
7198 int ret;
7199
7200 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7201 if (ret)
7202 goto out;
7203
7204 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007205 offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007206
7207 ret = BEGIN_LP_RING(6);
7208 if (ret)
7209 goto out;
7210
7211 /* Can't queue multiple flips, so wait for the previous
7212 * one to finish before executing the next.
7213 */
7214 if (intel_crtc->plane)
7215 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7216 else
7217 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7218 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
7219 OUT_RING(MI_NOOP);
7220 OUT_RING(MI_DISPLAY_FLIP |
7221 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007222 OUT_RING(fb->pitches[0]);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007223 OUT_RING(obj->gtt_offset + offset);
7224 OUT_RING(MI_NOOP);
7225 ADVANCE_LP_RING();
7226out:
7227 return ret;
7228}
7229
7230static int intel_gen3_queue_flip(struct drm_device *dev,
7231 struct drm_crtc *crtc,
7232 struct drm_framebuffer *fb,
7233 struct drm_i915_gem_object *obj)
7234{
7235 struct drm_i915_private *dev_priv = dev->dev_private;
7236 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7237 unsigned long offset;
7238 u32 flip_mask;
7239 int ret;
7240
7241 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7242 if (ret)
7243 goto out;
7244
7245 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007246 offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007247
7248 ret = BEGIN_LP_RING(6);
7249 if (ret)
7250 goto out;
7251
7252 if (intel_crtc->plane)
7253 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7254 else
7255 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7256 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
7257 OUT_RING(MI_NOOP);
7258 OUT_RING(MI_DISPLAY_FLIP_I915 |
7259 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007260 OUT_RING(fb->pitches[0]);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007261 OUT_RING(obj->gtt_offset + offset);
7262 OUT_RING(MI_NOOP);
7263
7264 ADVANCE_LP_RING();
7265out:
7266 return ret;
7267}
7268
7269static int intel_gen4_queue_flip(struct drm_device *dev,
7270 struct drm_crtc *crtc,
7271 struct drm_framebuffer *fb,
7272 struct drm_i915_gem_object *obj)
7273{
7274 struct drm_i915_private *dev_priv = dev->dev_private;
7275 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7276 uint32_t pf, pipesrc;
7277 int ret;
7278
7279 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7280 if (ret)
7281 goto out;
7282
7283 ret = BEGIN_LP_RING(4);
7284 if (ret)
7285 goto out;
7286
7287 /* i965+ uses the linear or tiled offsets from the
7288 * Display Registers (which do not change across a page-flip)
7289 * so we need only reprogram the base address.
7290 */
7291 OUT_RING(MI_DISPLAY_FLIP |
7292 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007293 OUT_RING(fb->pitches[0]);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007294 OUT_RING(obj->gtt_offset | obj->tiling_mode);
7295
7296 /* XXX Enabling the panel-fitter across page-flip is so far
7297 * untested on non-native modes, so ignore it for now.
7298 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7299 */
7300 pf = 0;
7301 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7302 OUT_RING(pf | pipesrc);
7303 ADVANCE_LP_RING();
7304out:
7305 return ret;
7306}
7307
7308static int intel_gen6_queue_flip(struct drm_device *dev,
7309 struct drm_crtc *crtc,
7310 struct drm_framebuffer *fb,
7311 struct drm_i915_gem_object *obj)
7312{
7313 struct drm_i915_private *dev_priv = dev->dev_private;
7314 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7315 uint32_t pf, pipesrc;
7316 int ret;
7317
7318 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7319 if (ret)
7320 goto out;
7321
7322 ret = BEGIN_LP_RING(4);
7323 if (ret)
7324 goto out;
7325
7326 OUT_RING(MI_DISPLAY_FLIP |
7327 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007328 OUT_RING(fb->pitches[0] | obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007329 OUT_RING(obj->gtt_offset);
7330
7331 pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7332 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7333 OUT_RING(pf | pipesrc);
7334 ADVANCE_LP_RING();
7335out:
7336 return ret;
7337}
7338
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007339/*
7340 * On gen7 we currently use the blit ring because (in early silicon at least)
7341 * the render ring doesn't give us interrpts for page flip completion, which
7342 * means clients will hang after the first flip is queued. Fortunately the
7343 * blit ring generates interrupts properly, so use it instead.
7344 */
7345static int intel_gen7_queue_flip(struct drm_device *dev,
7346 struct drm_crtc *crtc,
7347 struct drm_framebuffer *fb,
7348 struct drm_i915_gem_object *obj)
7349{
7350 struct drm_i915_private *dev_priv = dev->dev_private;
7351 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7352 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
7353 int ret;
7354
7355 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7356 if (ret)
7357 goto out;
7358
7359 ret = intel_ring_begin(ring, 4);
7360 if (ret)
7361 goto out;
7362
7363 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007364 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007365 intel_ring_emit(ring, (obj->gtt_offset));
7366 intel_ring_emit(ring, (MI_NOOP));
7367 intel_ring_advance(ring);
7368out:
7369 return ret;
7370}
7371
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007372static int intel_default_queue_flip(struct drm_device *dev,
7373 struct drm_crtc *crtc,
7374 struct drm_framebuffer *fb,
7375 struct drm_i915_gem_object *obj)
7376{
7377 return -ENODEV;
7378}
7379
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007380static int intel_crtc_page_flip(struct drm_crtc *crtc,
7381 struct drm_framebuffer *fb,
7382 struct drm_pending_vblank_event *event)
7383{
7384 struct drm_device *dev = crtc->dev;
7385 struct drm_i915_private *dev_priv = dev->dev_private;
7386 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007387 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007388 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7389 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007390 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007391 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007392
7393 work = kzalloc(sizeof *work, GFP_KERNEL);
7394 if (work == NULL)
7395 return -ENOMEM;
7396
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007397 work->event = event;
7398 work->dev = crtc->dev;
7399 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08007400 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007401 INIT_WORK(&work->work, intel_unpin_work_fn);
7402
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007403 ret = drm_vblank_get(dev, intel_crtc->pipe);
7404 if (ret)
7405 goto free_work;
7406
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007407 /* We borrow the event spin lock for protecting unpin_work */
7408 spin_lock_irqsave(&dev->event_lock, flags);
7409 if (intel_crtc->unpin_work) {
7410 spin_unlock_irqrestore(&dev->event_lock, flags);
7411 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007412 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007413
7414 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007415 return -EBUSY;
7416 }
7417 intel_crtc->unpin_work = work;
7418 spin_unlock_irqrestore(&dev->event_lock, flags);
7419
7420 intel_fb = to_intel_framebuffer(fb);
7421 obj = intel_fb->obj;
7422
Chris Wilson468f0b42010-05-27 13:18:13 +01007423 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007424
Jesse Barnes75dfca82010-02-10 15:09:44 -08007425 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007426 drm_gem_object_reference(&work->old_fb_obj->base);
7427 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007428
7429 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007430
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007431 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007432
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007433 work->enable_stall_check = true;
7434
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007435 /* Block clients from rendering to the new back buffer until
7436 * the flip occurs and the object is no longer visible.
7437 */
Chris Wilson05394f32010-11-08 19:18:58 +00007438 atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007439
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007440 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7441 if (ret)
7442 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007443
Chris Wilson7782de32011-07-08 12:22:41 +01007444 intel_disable_fbc(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007445 mutex_unlock(&dev->struct_mutex);
7446
Jesse Barnese5510fa2010-07-01 16:48:37 -07007447 trace_i915_flip_request(intel_crtc->plane, obj);
7448
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007449 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007450
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007451cleanup_pending:
7452 atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilson05394f32010-11-08 19:18:58 +00007453 drm_gem_object_unreference(&work->old_fb_obj->base);
7454 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007455 mutex_unlock(&dev->struct_mutex);
7456
7457 spin_lock_irqsave(&dev->event_lock, flags);
7458 intel_crtc->unpin_work = NULL;
7459 spin_unlock_irqrestore(&dev->event_lock, flags);
7460
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007461 drm_vblank_put(dev, intel_crtc->pipe);
7462free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007463 kfree(work);
7464
7465 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007466}
7467
Chris Wilson47f1c6c2010-12-03 15:37:31 +00007468static void intel_sanitize_modesetting(struct drm_device *dev,
7469 int pipe, int plane)
7470{
7471 struct drm_i915_private *dev_priv = dev->dev_private;
7472 u32 reg, val;
7473
7474 if (HAS_PCH_SPLIT(dev))
7475 return;
7476
7477 /* Who knows what state these registers were left in by the BIOS or
7478 * grub?
7479 *
7480 * If we leave the registers in a conflicting state (e.g. with the
7481 * display plane reading from the other pipe than the one we intend
7482 * to use) then when we attempt to teardown the active mode, we will
7483 * not disable the pipes and planes in the correct order -- leaving
7484 * a plane reading from a disabled pipe and possibly leading to
7485 * undefined behaviour.
7486 */
7487
7488 reg = DSPCNTR(plane);
7489 val = I915_READ(reg);
7490
7491 if ((val & DISPLAY_PLANE_ENABLE) == 0)
7492 return;
7493 if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
7494 return;
7495
7496 /* This display plane is active and attached to the other CPU pipe. */
7497 pipe = !pipe;
7498
7499 /* Disable the plane and wait for it to stop reading from the pipe. */
Jesse Barnesb24e7172011-01-04 15:09:30 -08007500 intel_disable_plane(dev_priv, plane, pipe);
7501 intel_disable_pipe(dev_priv, pipe);
Chris Wilson47f1c6c2010-12-03 15:37:31 +00007502}
Jesse Barnes79e53942008-11-07 14:24:08 -08007503
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007504static void intel_crtc_reset(struct drm_crtc *crtc)
7505{
7506 struct drm_device *dev = crtc->dev;
7507 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7508
7509 /* Reset flags back to the 'unknown' status so that they
7510 * will be correctly set on the initial modeset.
7511 */
7512 intel_crtc->dpms_mode = -1;
7513
7514 /* We need to fix up any BIOS configuration that conflicts with
7515 * our expectations.
7516 */
7517 intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
7518}
7519
7520static struct drm_crtc_helper_funcs intel_helper_funcs = {
7521 .dpms = intel_crtc_dpms,
7522 .mode_fixup = intel_crtc_mode_fixup,
7523 .mode_set = intel_crtc_mode_set,
7524 .mode_set_base = intel_pipe_set_base,
7525 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7526 .load_lut = intel_crtc_load_lut,
7527 .disable = intel_crtc_disable,
7528};
7529
7530static const struct drm_crtc_funcs intel_crtc_funcs = {
7531 .reset = intel_crtc_reset,
7532 .cursor_set = intel_crtc_cursor_set,
7533 .cursor_move = intel_crtc_cursor_move,
7534 .gamma_set = intel_crtc_gamma_set,
7535 .set_config = drm_crtc_helper_set_config,
7536 .destroy = intel_crtc_destroy,
7537 .page_flip = intel_crtc_page_flip,
7538};
7539
Hannes Ederb358d0a2008-12-18 21:18:47 +01007540static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08007541{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08007542 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007543 struct intel_crtc *intel_crtc;
7544 int i;
7545
7546 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
7547 if (intel_crtc == NULL)
7548 return;
7549
7550 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
7551
7552 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08007553 for (i = 0; i < 256; i++) {
7554 intel_crtc->lut_r[i] = i;
7555 intel_crtc->lut_g[i] = i;
7556 intel_crtc->lut_b[i] = i;
7557 }
7558
Jesse Barnes80824002009-09-10 15:28:06 -07007559 /* Swap pipes & planes for FBC on pre-965 */
7560 intel_crtc->pipe = pipe;
7561 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01007562 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007563 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01007564 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07007565 }
7566
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08007567 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
7568 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
7569 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
7570 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
7571
Chris Wilson5d1d0cc2011-01-24 15:02:15 +00007572 intel_crtc_reset(&intel_crtc->base);
Chris Wilson04dbff52011-02-10 17:38:35 +00007573 intel_crtc->active = true; /* force the pipe off on setup_init_config */
Jesse Barnes5a354202011-06-24 12:19:22 -07007574 intel_crtc->bpp = 24; /* default for pre-Ironlake */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07007575
7576 if (HAS_PCH_SPLIT(dev)) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07007577 if (pipe == 2 && IS_IVYBRIDGE(dev))
7578 intel_crtc->no_pll = true;
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07007579 intel_helper_funcs.prepare = ironlake_crtc_prepare;
7580 intel_helper_funcs.commit = ironlake_crtc_commit;
7581 } else {
7582 intel_helper_funcs.prepare = i9xx_crtc_prepare;
7583 intel_helper_funcs.commit = i9xx_crtc_commit;
7584 }
7585
Jesse Barnes79e53942008-11-07 14:24:08 -08007586 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
7587
Jesse Barnes652c3932009-08-17 13:31:43 -07007588 intel_crtc->busy = false;
7589
7590 setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
7591 (unsigned long)intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007592}
7593
Carl Worth08d7b3d2009-04-29 14:43:54 -07007594int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00007595 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07007596{
7597 drm_i915_private_t *dev_priv = dev->dev_private;
7598 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02007599 struct drm_mode_object *drmmode_obj;
7600 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007601
7602 if (!dev_priv) {
7603 DRM_ERROR("called with no initialization\n");
7604 return -EINVAL;
7605 }
7606
Daniel Vetterc05422d2009-08-11 16:05:30 +02007607 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
7608 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07007609
Daniel Vetterc05422d2009-08-11 16:05:30 +02007610 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07007611 DRM_ERROR("no such CRTC id\n");
7612 return -EINVAL;
7613 }
7614
Daniel Vetterc05422d2009-08-11 16:05:30 +02007615 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
7616 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007617
Daniel Vetterc05422d2009-08-11 16:05:30 +02007618 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007619}
7620
Zhenyu Wangc5e4df32010-03-30 14:39:27 +08007621static int intel_encoder_clones(struct drm_device *dev, int type_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08007622{
Chris Wilson4ef69c72010-09-09 15:14:28 +01007623 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007624 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007625 int entry = 0;
7626
Chris Wilson4ef69c72010-09-09 15:14:28 +01007627 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7628 if (type_mask & encoder->clone_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08007629 index_mask |= (1 << entry);
7630 entry++;
7631 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01007632
Jesse Barnes79e53942008-11-07 14:24:08 -08007633 return index_mask;
7634}
7635
Chris Wilson4d302442010-12-14 19:21:29 +00007636static bool has_edp_a(struct drm_device *dev)
7637{
7638 struct drm_i915_private *dev_priv = dev->dev_private;
7639
7640 if (!IS_MOBILE(dev))
7641 return false;
7642
7643 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
7644 return false;
7645
7646 if (IS_GEN5(dev) &&
7647 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
7648 return false;
7649
7650 return true;
7651}
7652
Jesse Barnes79e53942008-11-07 14:24:08 -08007653static void intel_setup_outputs(struct drm_device *dev)
7654{
Eric Anholt725e30a2009-01-22 13:01:02 -08007655 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007656 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007657 bool dpd_is_edp = false;
Chris Wilsonc5d1b512010-11-29 18:00:23 +00007658 bool has_lvds = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08007659
Zhenyu Wang541998a2009-06-05 15:38:44 +08007660 if (IS_MOBILE(dev) && !IS_I830(dev))
Chris Wilsonc5d1b512010-11-29 18:00:23 +00007661 has_lvds = intel_lvds_init(dev);
7662 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
7663 /* disable the panel fitter on everything but LVDS */
7664 I915_WRITE(PFIT_CONTROL, 0);
7665 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007666
Eric Anholtbad720f2009-10-22 16:11:14 -07007667 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007668 dpd_is_edp = intel_dpd_is_edp(dev);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007669
Chris Wilson4d302442010-12-14 19:21:29 +00007670 if (has_edp_a(dev))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08007671 intel_dp_init(dev, DP_A);
7672
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007673 if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
7674 intel_dp_init(dev, PCH_DP_D);
7675 }
7676
7677 intel_crt_init(dev);
7678
7679 if (HAS_PCH_SPLIT(dev)) {
7680 int found;
7681
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007682 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08007683 /* PCH SDVOB multiplex with HDMIB */
7684 found = intel_sdvo_init(dev, PCH_SDVOB);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007685 if (!found)
7686 intel_hdmi_init(dev, HDMIB);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08007687 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
7688 intel_dp_init(dev, PCH_DP_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007689 }
7690
7691 if (I915_READ(HDMIC) & PORT_DETECTED)
7692 intel_hdmi_init(dev, HDMIC);
7693
7694 if (I915_READ(HDMID) & PORT_DETECTED)
7695 intel_hdmi_init(dev, HDMID);
7696
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08007697 if (I915_READ(PCH_DP_C) & DP_DETECTED)
7698 intel_dp_init(dev, PCH_DP_C);
7699
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007700 if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08007701 intel_dp_init(dev, PCH_DP_D);
7702
Zhenyu Wang103a1962009-11-27 11:44:36 +08007703 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08007704 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08007705
Eric Anholt725e30a2009-01-22 13:01:02 -08007706 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007707 DRM_DEBUG_KMS("probing SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08007708 found = intel_sdvo_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007709 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
7710 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08007711 intel_hdmi_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007712 }
Ma Ling27185ae2009-08-24 13:50:23 +08007713
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007714 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
7715 DRM_DEBUG_KMS("probing DP_B\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07007716 intel_dp_init(dev, DP_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007717 }
Eric Anholt725e30a2009-01-22 13:01:02 -08007718 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04007719
7720 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04007721
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007722 if (I915_READ(SDVOB) & SDVO_DETECTED) {
7723 DRM_DEBUG_KMS("probing SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08007724 found = intel_sdvo_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007725 }
Ma Ling27185ae2009-08-24 13:50:23 +08007726
7727 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
7728
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007729 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
7730 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08007731 intel_hdmi_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007732 }
7733 if (SUPPORTS_INTEGRATED_DP(dev)) {
7734 DRM_DEBUG_KMS("probing DP_C\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07007735 intel_dp_init(dev, DP_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007736 }
Eric Anholt725e30a2009-01-22 13:01:02 -08007737 }
Ma Ling27185ae2009-08-24 13:50:23 +08007738
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007739 if (SUPPORTS_INTEGRATED_DP(dev) &&
7740 (I915_READ(DP_D) & DP_DETECTED)) {
7741 DRM_DEBUG_KMS("probing DP_D\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07007742 intel_dp_init(dev, DP_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007743 }
Eric Anholtbad720f2009-10-22 16:11:14 -07007744 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08007745 intel_dvo_init(dev);
7746
Zhenyu Wang103a1962009-11-27 11:44:36 +08007747 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08007748 intel_tv_init(dev);
7749
Chris Wilson4ef69c72010-09-09 15:14:28 +01007750 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7751 encoder->base.possible_crtcs = encoder->crtc_mask;
7752 encoder->base.possible_clones =
7753 intel_encoder_clones(dev, encoder->clone_mask);
Jesse Barnes79e53942008-11-07 14:24:08 -08007754 }
Chris Wilson47356eb2011-01-11 17:06:04 +00007755
Chris Wilson2c7111d2011-03-29 10:40:27 +01007756 /* disable all the possible outputs/crtcs before entering KMS mode */
7757 drm_helper_disable_unused_functions(dev);
Keith Packard9fb526d2011-09-26 22:24:57 -07007758
7759 if (HAS_PCH_SPLIT(dev))
7760 ironlake_init_pch_refclk(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08007761}
7762
7763static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
7764{
7765 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08007766
7767 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00007768 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007769
7770 kfree(intel_fb);
7771}
7772
7773static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00007774 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08007775 unsigned int *handle)
7776{
7777 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00007778 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08007779
Chris Wilson05394f32010-11-08 19:18:58 +00007780 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08007781}
7782
7783static const struct drm_framebuffer_funcs intel_fb_funcs = {
7784 .destroy = intel_user_framebuffer_destroy,
7785 .create_handle = intel_user_framebuffer_create_handle,
7786};
7787
Dave Airlie38651672010-03-30 05:34:13 +00007788int intel_framebuffer_init(struct drm_device *dev,
7789 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007790 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00007791 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08007792{
Jesse Barnes79e53942008-11-07 14:24:08 -08007793 int ret;
7794
Chris Wilson05394f32010-11-08 19:18:58 +00007795 if (obj->tiling_mode == I915_TILING_Y)
Chris Wilson57cd6502010-08-08 12:34:44 +01007796 return -EINVAL;
7797
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007798 if (mode_cmd->pitches[0] & 63)
Chris Wilson57cd6502010-08-08 12:34:44 +01007799 return -EINVAL;
7800
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007801 switch (mode_cmd->pixel_format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02007802 case DRM_FORMAT_RGB332:
7803 case DRM_FORMAT_RGB565:
7804 case DRM_FORMAT_XRGB8888:
7805 case DRM_FORMAT_ARGB8888:
7806 case DRM_FORMAT_XRGB2101010:
7807 case DRM_FORMAT_ARGB2101010:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007808 /* RGB formats are common across chipsets */
Jesse Barnesb5626742011-06-24 12:19:27 -07007809 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02007810 case DRM_FORMAT_YUYV:
7811 case DRM_FORMAT_UYVY:
7812 case DRM_FORMAT_YVYU:
7813 case DRM_FORMAT_VYUY:
Chris Wilson57cd6502010-08-08 12:34:44 +01007814 break;
7815 default:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007816 DRM_ERROR("unsupported pixel format\n");
Chris Wilson57cd6502010-08-08 12:34:44 +01007817 return -EINVAL;
7818 }
7819
Jesse Barnes79e53942008-11-07 14:24:08 -08007820 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
7821 if (ret) {
7822 DRM_ERROR("framebuffer init failed %d\n", ret);
7823 return ret;
7824 }
7825
7826 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -08007827 intel_fb->obj = obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08007828 return 0;
7829}
7830
Jesse Barnes79e53942008-11-07 14:24:08 -08007831static struct drm_framebuffer *
7832intel_user_framebuffer_create(struct drm_device *dev,
7833 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007834 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08007835{
Chris Wilson05394f32010-11-08 19:18:58 +00007836 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08007837
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007838 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
7839 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00007840 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01007841 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08007842
Chris Wilsond2dff872011-04-19 08:36:26 +01007843 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08007844}
7845
Jesse Barnes79e53942008-11-07 14:24:08 -08007846static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08007847 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00007848 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08007849};
7850
Chris Wilson05394f32010-11-08 19:18:58 +00007851static struct drm_i915_gem_object *
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08007852intel_alloc_context_page(struct drm_device *dev)
Chris Wilson9ea8d052010-01-04 18:57:56 +00007853{
Chris Wilson05394f32010-11-08 19:18:58 +00007854 struct drm_i915_gem_object *ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00007855 int ret;
7856
Ben Widawsky2c34b852011-03-19 18:14:26 -07007857 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
7858
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08007859 ctx = i915_gem_alloc_object(dev, 4096);
7860 if (!ctx) {
Chris Wilson9ea8d052010-01-04 18:57:56 +00007861 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
7862 return NULL;
7863 }
7864
Daniel Vetter75e9e912010-11-04 17:11:09 +01007865 ret = i915_gem_object_pin(ctx, 4096, true);
Chris Wilson9ea8d052010-01-04 18:57:56 +00007866 if (ret) {
7867 DRM_ERROR("failed to pin power context: %d\n", ret);
7868 goto err_unref;
7869 }
7870
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08007871 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
Chris Wilson9ea8d052010-01-04 18:57:56 +00007872 if (ret) {
7873 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
7874 goto err_unpin;
7875 }
Chris Wilson9ea8d052010-01-04 18:57:56 +00007876
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08007877 return ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00007878
7879err_unpin:
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08007880 i915_gem_object_unpin(ctx);
Chris Wilson9ea8d052010-01-04 18:57:56 +00007881err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00007882 drm_gem_object_unreference(&ctx->base);
Chris Wilson9ea8d052010-01-04 18:57:56 +00007883 mutex_unlock(&dev->struct_mutex);
7884 return NULL;
7885}
7886
Jesse Barnes7648fa92010-05-20 14:28:11 -07007887bool ironlake_set_drps(struct drm_device *dev, u8 val)
7888{
7889 struct drm_i915_private *dev_priv = dev->dev_private;
7890 u16 rgvswctl;
7891
7892 rgvswctl = I915_READ16(MEMSWCTL);
7893 if (rgvswctl & MEMCTL_CMD_STS) {
7894 DRM_DEBUG("gpu busy, RCS change rejected\n");
7895 return false; /* still busy with another command */
7896 }
7897
7898 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
7899 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
7900 I915_WRITE16(MEMSWCTL, rgvswctl);
7901 POSTING_READ16(MEMSWCTL);
7902
7903 rgvswctl |= MEMCTL_CMD_STS;
7904 I915_WRITE16(MEMSWCTL, rgvswctl);
7905
7906 return true;
7907}
7908
Jesse Barnesf97108d2010-01-29 11:27:07 -08007909void ironlake_enable_drps(struct drm_device *dev)
7910{
7911 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07007912 u32 rgvmodectl = I915_READ(MEMMODECTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08007913 u8 fmax, fmin, fstart, vstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08007914
Jesse Barnesea056c12010-09-10 10:02:13 -07007915 /* Enable temp reporting */
7916 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
7917 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
7918
Jesse Barnesf97108d2010-01-29 11:27:07 -08007919 /* 100ms RC evaluation intervals */
7920 I915_WRITE(RCUPEI, 100000);
7921 I915_WRITE(RCDNEI, 100000);
7922
7923 /* Set max/min thresholds to 90ms and 80ms respectively */
7924 I915_WRITE(RCBMAXAVG, 90000);
7925 I915_WRITE(RCBMINAVG, 80000);
7926
7927 I915_WRITE(MEMIHYST, 1);
7928
7929 /* Set up min, max, and cur for interrupt handling */
7930 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
7931 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
7932 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
7933 MEMMODE_FSTART_SHIFT;
Jesse Barnes7648fa92010-05-20 14:28:11 -07007934
Jesse Barnesf97108d2010-01-29 11:27:07 -08007935 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
7936 PXVFREQ_PX_SHIFT;
7937
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07007938 dev_priv->fmax = fmax; /* IPS callback will increase this */
Jesse Barnes7648fa92010-05-20 14:28:11 -07007939 dev_priv->fstart = fstart;
7940
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07007941 dev_priv->max_delay = fstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08007942 dev_priv->min_delay = fmin;
7943 dev_priv->cur_delay = fstart;
7944
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07007945 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
7946 fmax, fmin, fstart);
Jesse Barnes7648fa92010-05-20 14:28:11 -07007947
Jesse Barnesf97108d2010-01-29 11:27:07 -08007948 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
7949
7950 /*
7951 * Interrupts will be enabled in ironlake_irq_postinstall
7952 */
7953
7954 I915_WRITE(VIDSTART, vstart);
7955 POSTING_READ(VIDSTART);
7956
7957 rgvmodectl |= MEMMODE_SWMODE_EN;
7958 I915_WRITE(MEMMODECTL, rgvmodectl);
7959
Chris Wilson481b6af2010-08-23 17:43:35 +01007960 if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Chris Wilson913d8d12010-08-07 11:01:35 +01007961 DRM_ERROR("stuck trying to change perf mode\n");
Jesse Barnesf97108d2010-01-29 11:27:07 -08007962 msleep(1);
7963
Jesse Barnes7648fa92010-05-20 14:28:11 -07007964 ironlake_set_drps(dev, fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08007965
Jesse Barnes7648fa92010-05-20 14:28:11 -07007966 dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
7967 I915_READ(0x112e0);
7968 dev_priv->last_time1 = jiffies_to_msecs(jiffies);
7969 dev_priv->last_count2 = I915_READ(0x112f4);
7970 getrawmonotonic(&dev_priv->last_time2);
Jesse Barnesf97108d2010-01-29 11:27:07 -08007971}
7972
7973void ironlake_disable_drps(struct drm_device *dev)
7974{
7975 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07007976 u16 rgvswctl = I915_READ16(MEMSWCTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08007977
7978 /* Ack interrupts, disable EFC interrupt */
7979 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
7980 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
7981 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
7982 I915_WRITE(DEIIR, DE_PCU_EVENT);
7983 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
7984
7985 /* Go back to the starting frequency */
Jesse Barnes7648fa92010-05-20 14:28:11 -07007986 ironlake_set_drps(dev, dev_priv->fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08007987 msleep(1);
7988 rgvswctl |= MEMCTL_CMD_STS;
7989 I915_WRITE(MEMSWCTL, rgvswctl);
7990 msleep(1);
7991
7992}
7993
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08007994void gen6_set_rps(struct drm_device *dev, u8 val)
7995{
7996 struct drm_i915_private *dev_priv = dev->dev_private;
7997 u32 swreq;
7998
7999 swreq = (val & 0x3ff) << 25;
8000 I915_WRITE(GEN6_RPNSWREQ, swreq);
8001}
8002
8003void gen6_disable_rps(struct drm_device *dev)
8004{
8005 struct drm_i915_private *dev_priv = dev->dev_private;
8006
8007 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
8008 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
8009 I915_WRITE(GEN6_PMIER, 0);
Daniel Vetter6fdd4d92011-09-08 14:00:22 +02008010 /* Complete PM interrupt masking here doesn't race with the rps work
8011 * item again unmasking PM interrupts because that is using a different
8012 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
8013 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
Ben Widawsky4912d042011-04-25 11:25:20 -07008014
8015 spin_lock_irq(&dev_priv->rps_lock);
8016 dev_priv->pm_iir = 0;
8017 spin_unlock_irq(&dev_priv->rps_lock);
8018
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008019 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
8020}
8021
Jesse Barnes7648fa92010-05-20 14:28:11 -07008022static unsigned long intel_pxfreq(u32 vidfreq)
8023{
8024 unsigned long freq;
8025 int div = (vidfreq & 0x3f0000) >> 16;
8026 int post = (vidfreq & 0x3000) >> 12;
8027 int pre = (vidfreq & 0x7);
8028
8029 if (!pre)
8030 return 0;
8031
8032 freq = ((div * 133333) / ((1<<post) * pre));
8033
8034 return freq;
8035}
8036
8037void intel_init_emon(struct drm_device *dev)
8038{
8039 struct drm_i915_private *dev_priv = dev->dev_private;
8040 u32 lcfuse;
8041 u8 pxw[16];
8042 int i;
8043
8044 /* Disable to program */
8045 I915_WRITE(ECR, 0);
8046 POSTING_READ(ECR);
8047
8048 /* Program energy weights for various events */
8049 I915_WRITE(SDEW, 0x15040d00);
8050 I915_WRITE(CSIEW0, 0x007f0000);
8051 I915_WRITE(CSIEW1, 0x1e220004);
8052 I915_WRITE(CSIEW2, 0x04000004);
8053
8054 for (i = 0; i < 5; i++)
8055 I915_WRITE(PEW + (i * 4), 0);
8056 for (i = 0; i < 3; i++)
8057 I915_WRITE(DEW + (i * 4), 0);
8058
8059 /* Program P-state weights to account for frequency power adjustment */
8060 for (i = 0; i < 16; i++) {
8061 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
8062 unsigned long freq = intel_pxfreq(pxvidfreq);
8063 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
8064 PXVFREQ_PX_SHIFT;
8065 unsigned long val;
8066
8067 val = vid * vid;
8068 val *= (freq / 1000);
8069 val *= 255;
8070 val /= (127*127*900);
8071 if (val > 0xff)
8072 DRM_ERROR("bad pxval: %ld\n", val);
8073 pxw[i] = val;
8074 }
8075 /* Render standby states get 0 weight */
8076 pxw[14] = 0;
8077 pxw[15] = 0;
8078
8079 for (i = 0; i < 4; i++) {
8080 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
8081 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
8082 I915_WRITE(PXW + (i * 4), val);
8083 }
8084
8085 /* Adjust magic regs to magic values (more experimental results) */
8086 I915_WRITE(OGW0, 0);
8087 I915_WRITE(OGW1, 0);
8088 I915_WRITE(EG0, 0x00007f00);
8089 I915_WRITE(EG1, 0x0000000e);
8090 I915_WRITE(EG2, 0x000e0000);
8091 I915_WRITE(EG3, 0x68000300);
8092 I915_WRITE(EG4, 0x42000000);
8093 I915_WRITE(EG5, 0x00140031);
8094 I915_WRITE(EG6, 0);
8095 I915_WRITE(EG7, 0);
8096
8097 for (i = 0; i < 8; i++)
8098 I915_WRITE(PXWL + (i * 4), 0);
8099
8100 /* Enable PMON + select events */
8101 I915_WRITE(ECR, 0x80000019);
8102
8103 lcfuse = I915_READ(LCFUSE02);
8104
8105 dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
8106}
8107
Keith Packardc0f372b32011-11-16 22:24:52 -08008108static bool intel_enable_rc6(struct drm_device *dev)
8109{
8110 /*
8111 * Respect the kernel parameter if it is set
8112 */
8113 if (i915_enable_rc6 >= 0)
8114 return i915_enable_rc6;
8115
8116 /*
8117 * Disable RC6 on Ironlake
8118 */
8119 if (INTEL_INFO(dev)->gen == 5)
8120 return 0;
8121
8122 /*
8123 * Enable rc6 on Sandybridge if DMA remapping is disabled
8124 */
8125 if (INTEL_INFO(dev)->gen == 6) {
8126 DRM_DEBUG_DRIVER("Sandybridge: intel_iommu_enabled %s -- RC6 %sabled\n",
8127 intel_iommu_enabled ? "true" : "false",
8128 !intel_iommu_enabled ? "en" : "dis");
8129 return !intel_iommu_enabled;
8130 }
8131 DRM_DEBUG_DRIVER("RC6 enabled\n");
8132 return 1;
8133}
8134
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008135void gen6_enable_rps(struct drm_i915_private *dev_priv)
Chris Wilson8fd26852010-12-08 18:40:43 +00008136{
Jesse Barnesa6044e22010-12-20 11:34:20 -08008137 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
8138 u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
Jesse Barnes7df87212011-03-30 14:08:56 -07008139 u32 pcu_mbox, rc6_mask = 0;
Jesse Barnesa6044e22010-12-20 11:34:20 -08008140 int cur_freq, min_freq, max_freq;
Chris Wilson8fd26852010-12-08 18:40:43 +00008141 int i;
8142
8143 /* Here begins a magic sequence of register writes to enable
8144 * auto-downclocking.
8145 *
8146 * Perhaps there might be some value in exposing these to
8147 * userspace...
8148 */
8149 I915_WRITE(GEN6_RC_STATE, 0);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01008150 mutex_lock(&dev_priv->dev->struct_mutex);
Ben Widawskyfcca7922011-04-25 11:23:07 -07008151 gen6_gt_force_wake_get(dev_priv);
Chris Wilson8fd26852010-12-08 18:40:43 +00008152
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008153 /* disable the counters and set deterministic thresholds */
Chris Wilson8fd26852010-12-08 18:40:43 +00008154 I915_WRITE(GEN6_RC_CONTROL, 0);
8155
8156 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
8157 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
8158 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
8159 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
8160 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
8161
8162 for (i = 0; i < I915_NUM_RINGS; i++)
8163 I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
8164
8165 I915_WRITE(GEN6_RC_SLEEP, 0);
8166 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
8167 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
8168 I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
8169 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
8170
Keith Packardc0f372b32011-11-16 22:24:52 -08008171 if (intel_enable_rc6(dev_priv->dev))
Jesse Barnes7df87212011-03-30 14:08:56 -07008172 rc6_mask = GEN6_RC_CTL_RC6p_ENABLE |
8173 GEN6_RC_CTL_RC6_ENABLE;
8174
Chris Wilson8fd26852010-12-08 18:40:43 +00008175 I915_WRITE(GEN6_RC_CONTROL,
Jesse Barnes7df87212011-03-30 14:08:56 -07008176 rc6_mask |
Chris Wilson9c3d2f72010-12-17 10:54:26 +00008177 GEN6_RC_CTL_EI_MODE(1) |
Chris Wilson8fd26852010-12-08 18:40:43 +00008178 GEN6_RC_CTL_HW_ENABLE);
8179
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008180 I915_WRITE(GEN6_RPNSWREQ,
Chris Wilson8fd26852010-12-08 18:40:43 +00008181 GEN6_FREQUENCY(10) |
8182 GEN6_OFFSET(0) |
8183 GEN6_AGGRESSIVE_TURBO);
8184 I915_WRITE(GEN6_RC_VIDEO_FREQ,
8185 GEN6_FREQUENCY(12));
8186
8187 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
8188 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
8189 18 << 24 |
8190 6 << 16);
Jesse Barnesccab5c82011-01-18 15:49:25 -08008191 I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
8192 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
Chris Wilson8fd26852010-12-08 18:40:43 +00008193 I915_WRITE(GEN6_RP_UP_EI, 100000);
Jesse Barnesccab5c82011-01-18 15:49:25 -08008194 I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
Chris Wilson8fd26852010-12-08 18:40:43 +00008195 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
8196 I915_WRITE(GEN6_RP_CONTROL,
8197 GEN6_RP_MEDIA_TURBO |
Ben Widawsky6ed55ee2011-12-12 19:21:59 -08008198 GEN6_RP_MEDIA_HW_MODE |
Chris Wilson8fd26852010-12-08 18:40:43 +00008199 GEN6_RP_MEDIA_IS_GFX |
8200 GEN6_RP_ENABLE |
Jesse Barnesccab5c82011-01-18 15:49:25 -08008201 GEN6_RP_UP_BUSY_AVG |
8202 GEN6_RP_DOWN_IDLE_CONT);
Chris Wilson8fd26852010-12-08 18:40:43 +00008203
8204 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8205 500))
8206 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
8207
8208 I915_WRITE(GEN6_PCODE_DATA, 0);
8209 I915_WRITE(GEN6_PCODE_MAILBOX,
8210 GEN6_PCODE_READY |
8211 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
8212 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8213 500))
8214 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
8215
Jesse Barnesa6044e22010-12-20 11:34:20 -08008216 min_freq = (rp_state_cap & 0xff0000) >> 16;
8217 max_freq = rp_state_cap & 0xff;
8218 cur_freq = (gt_perf_status & 0xff00) >> 8;
8219
8220 /* Check for overclock support */
8221 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8222 500))
8223 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
8224 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
8225 pcu_mbox = I915_READ(GEN6_PCODE_DATA);
8226 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8227 500))
8228 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
8229 if (pcu_mbox & (1<<31)) { /* OC supported */
8230 max_freq = pcu_mbox & 0xff;
Jesse Barnese281fca2011-03-18 10:32:07 -07008231 DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
Jesse Barnesa6044e22010-12-20 11:34:20 -08008232 }
8233
8234 /* In units of 100MHz */
8235 dev_priv->max_delay = max_freq;
8236 dev_priv->min_delay = min_freq;
8237 dev_priv->cur_delay = cur_freq;
8238
Chris Wilson8fd26852010-12-08 18:40:43 +00008239 /* requires MSI enabled */
8240 I915_WRITE(GEN6_PMIER,
8241 GEN6_PM_MBOX_EVENT |
8242 GEN6_PM_THERMAL_EVENT |
8243 GEN6_PM_RP_DOWN_TIMEOUT |
8244 GEN6_PM_RP_UP_THRESHOLD |
8245 GEN6_PM_RP_DOWN_THRESHOLD |
8246 GEN6_PM_RP_UP_EI_EXPIRED |
8247 GEN6_PM_RP_DOWN_EI_EXPIRED);
Ben Widawsky4912d042011-04-25 11:25:20 -07008248 spin_lock_irq(&dev_priv->rps_lock);
8249 WARN_ON(dev_priv->pm_iir != 0);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008250 I915_WRITE(GEN6_PMIMR, 0);
Ben Widawsky4912d042011-04-25 11:25:20 -07008251 spin_unlock_irq(&dev_priv->rps_lock);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008252 /* enable all PM interrupts */
8253 I915_WRITE(GEN6_PMINTRMSK, 0);
Chris Wilson8fd26852010-12-08 18:40:43 +00008254
Ben Widawskyfcca7922011-04-25 11:23:07 -07008255 gen6_gt_force_wake_put(dev_priv);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01008256 mutex_unlock(&dev_priv->dev->struct_mutex);
Chris Wilson8fd26852010-12-08 18:40:43 +00008257}
8258
Jesse Barnes23b2f8b2011-06-28 13:04:16 -07008259void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
8260{
8261 int min_freq = 15;
8262 int gpu_freq, ia_freq, max_ia_freq;
8263 int scaling_factor = 180;
8264
8265 max_ia_freq = cpufreq_quick_get_max(0);
8266 /*
8267 * Default to measured freq if none found, PCU will ensure we don't go
8268 * over
8269 */
8270 if (!max_ia_freq)
8271 max_ia_freq = tsc_khz;
8272
8273 /* Convert from kHz to MHz */
8274 max_ia_freq /= 1000;
8275
8276 mutex_lock(&dev_priv->dev->struct_mutex);
8277
8278 /*
8279 * For each potential GPU frequency, load a ring frequency we'd like
8280 * to use for memory access. We do this by specifying the IA frequency
8281 * the PCU should use as a reference to determine the ring frequency.
8282 */
8283 for (gpu_freq = dev_priv->max_delay; gpu_freq >= dev_priv->min_delay;
8284 gpu_freq--) {
8285 int diff = dev_priv->max_delay - gpu_freq;
8286
8287 /*
8288 * For GPU frequencies less than 750MHz, just use the lowest
8289 * ring freq.
8290 */
8291 if (gpu_freq < min_freq)
8292 ia_freq = 800;
8293 else
8294 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
8295 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
8296
8297 I915_WRITE(GEN6_PCODE_DATA,
8298 (ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT) |
8299 gpu_freq);
8300 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY |
8301 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
8302 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) &
8303 GEN6_PCODE_READY) == 0, 10)) {
8304 DRM_ERROR("pcode write of freq table timed out\n");
8305 continue;
8306 }
8307 }
8308
8309 mutex_unlock(&dev_priv->dev->struct_mutex);
8310}
8311
Jesse Barnes6067aae2011-04-28 15:04:31 -07008312static void ironlake_init_clock_gating(struct drm_device *dev)
8313{
8314 struct drm_i915_private *dev_priv = dev->dev_private;
8315 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
8316
8317 /* Required for FBC */
8318 dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
8319 DPFCRUNIT_CLOCK_GATE_DISABLE |
8320 DPFDUNIT_CLOCK_GATE_DISABLE;
8321 /* Required for CxSR */
8322 dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
8323
8324 I915_WRITE(PCH_3DCGDIS0,
8325 MARIUNIT_CLOCK_GATE_DISABLE |
8326 SVSMUNIT_CLOCK_GATE_DISABLE);
8327 I915_WRITE(PCH_3DCGDIS1,
8328 VFMUNIT_CLOCK_GATE_DISABLE);
8329
8330 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
8331
8332 /*
Jesse Barnes6067aae2011-04-28 15:04:31 -07008333 * According to the spec the following bits should be set in
8334 * order to enable memory self-refresh
8335 * The bit 22/21 of 0x42004
8336 * The bit 5 of 0x42020
8337 * The bit 15 of 0x45000
8338 */
8339 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8340 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8341 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
8342 I915_WRITE(ILK_DSPCLK_GATE,
8343 (I915_READ(ILK_DSPCLK_GATE) |
8344 ILK_DPARB_CLK_GATE));
8345 I915_WRITE(DISP_ARB_CTL,
8346 (I915_READ(DISP_ARB_CTL) |
8347 DISP_FBC_WM_DIS));
8348 I915_WRITE(WM3_LP_ILK, 0);
8349 I915_WRITE(WM2_LP_ILK, 0);
8350 I915_WRITE(WM1_LP_ILK, 0);
8351
8352 /*
8353 * Based on the document from hardware guys the following bits
8354 * should be set unconditionally in order to enable FBC.
8355 * The bit 22 of 0x42000
8356 * The bit 22 of 0x42004
8357 * The bit 7,8,9 of 0x42020.
8358 */
8359 if (IS_IRONLAKE_M(dev)) {
8360 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8361 I915_READ(ILK_DISPLAY_CHICKEN1) |
8362 ILK_FBCQ_DIS);
8363 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8364 I915_READ(ILK_DISPLAY_CHICKEN2) |
8365 ILK_DPARB_GATE);
8366 I915_WRITE(ILK_DSPCLK_GATE,
8367 I915_READ(ILK_DSPCLK_GATE) |
8368 ILK_DPFC_DIS1 |
8369 ILK_DPFC_DIS2 |
8370 ILK_CLK_FBC);
8371 }
8372
8373 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8374 I915_READ(ILK_DISPLAY_CHICKEN2) |
8375 ILK_ELPIN_409_SELECT);
8376 I915_WRITE(_3D_CHICKEN2,
8377 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8378 _3D_CHICKEN2_WM_READ_PIPELINED);
8379}
8380
8381static void gen6_init_clock_gating(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008382{
8383 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08008384 int pipe;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008385 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
8386
8387 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
Jesse Barnes652c3932009-08-17 13:31:43 -07008388
Jesse Barnes6067aae2011-04-28 15:04:31 -07008389 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8390 I915_READ(ILK_DISPLAY_CHICKEN2) |
8391 ILK_ELPIN_409_SELECT);
Eric Anholt8956c8b2010-03-18 13:21:14 -07008392
Jesse Barnes6067aae2011-04-28 15:04:31 -07008393 I915_WRITE(WM3_LP_ILK, 0);
8394 I915_WRITE(WM2_LP_ILK, 0);
8395 I915_WRITE(WM1_LP_ILK, 0);
Eric Anholt8956c8b2010-03-18 13:21:14 -07008396
Eric Anholt406478d2011-11-07 16:07:04 -08008397 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8398 * gating disable must be set. Failure to set it results in
8399 * flickering pixels due to Z write ordering failures after
8400 * some amount of runtime in the Mesa "fire" demo, and Unigine
8401 * Sanctuary and Tropics, and apparently anything else with
8402 * alpha test or pixel discard.
Eric Anholt9ca1d102011-11-07 16:07:05 -08008403 *
8404 * According to the spec, bit 11 (RCCUNIT) must also be set,
8405 * but we didn't debug actual testcases to find it out.
Eric Anholt406478d2011-11-07 16:07:04 -08008406 */
Eric Anholt9ca1d102011-11-07 16:07:05 -08008407 I915_WRITE(GEN6_UCGCTL2,
8408 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8409 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
Eric Anholt406478d2011-11-07 16:07:04 -08008410
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008411 /*
Jesse Barnes6067aae2011-04-28 15:04:31 -07008412 * According to the spec the following bits should be
8413 * set in order to enable memory self-refresh and fbc:
8414 * The bit21 and bit22 of 0x42000
8415 * The bit21 and bit22 of 0x42004
8416 * The bit5 and bit7 of 0x42020
8417 * The bit14 of 0x70180
8418 * The bit14 of 0x71180
Jesse Barnes382b0932010-10-07 16:01:25 -07008419 */
Jesse Barnes6067aae2011-04-28 15:04:31 -07008420 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8421 I915_READ(ILK_DISPLAY_CHICKEN1) |
8422 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8423 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8424 I915_READ(ILK_DISPLAY_CHICKEN2) |
8425 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
8426 I915_WRITE(ILK_DSPCLK_GATE,
8427 I915_READ(ILK_DSPCLK_GATE) |
8428 ILK_DPARB_CLK_GATE |
8429 ILK_DPFD_CLK_GATE);
Jesse Barnes382b0932010-10-07 16:01:25 -07008430
Keith Packardd74362c2011-07-28 14:47:14 -07008431 for_each_pipe(pipe) {
Jesse Barnes6067aae2011-04-28 15:04:31 -07008432 I915_WRITE(DSPCNTR(pipe),
8433 I915_READ(DSPCNTR(pipe)) |
8434 DISPPLANE_TRICKLE_FEED_DISABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07008435 intel_flush_display_plane(dev_priv, pipe);
8436 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07008437}
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008438
Jesse Barnes28963a32011-05-11 09:42:30 -07008439static void ivybridge_init_clock_gating(struct drm_device *dev)
8440{
8441 struct drm_i915_private *dev_priv = dev->dev_private;
8442 int pipe;
8443 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
Jesse Barnes652c3932009-08-17 13:31:43 -07008444
Jesse Barnes28963a32011-05-11 09:42:30 -07008445 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008446
Jesse Barnes28963a32011-05-11 09:42:30 -07008447 I915_WRITE(WM3_LP_ILK, 0);
8448 I915_WRITE(WM2_LP_ILK, 0);
8449 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008450
Jesse Barnes28963a32011-05-11 09:42:30 -07008451 I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
Eric Anholtde6e2ea2010-11-06 14:53:32 -07008452
Eric Anholt116ac8d2011-12-21 10:31:09 -08008453 I915_WRITE(IVB_CHICKEN3,
8454 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8455 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8456
Keith Packardd74362c2011-07-28 14:47:14 -07008457 for_each_pipe(pipe) {
Jesse Barnes28963a32011-05-11 09:42:30 -07008458 I915_WRITE(DSPCNTR(pipe),
8459 I915_READ(DSPCNTR(pipe)) |
8460 DISPPLANE_TRICKLE_FEED_DISABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07008461 intel_flush_display_plane(dev_priv, pipe);
8462 }
Jesse Barnes28963a32011-05-11 09:42:30 -07008463}
Eric Anholt67e92af2010-11-06 14:53:33 -07008464
Jesse Barnes6067aae2011-04-28 15:04:31 -07008465static void g4x_init_clock_gating(struct drm_device *dev)
8466{
8467 struct drm_i915_private *dev_priv = dev->dev_private;
8468 uint32_t dspclk_gate;
Chris Wilson8fd26852010-12-08 18:40:43 +00008469
Jesse Barnes6067aae2011-04-28 15:04:31 -07008470 I915_WRITE(RENCLK_GATE_D1, 0);
8471 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8472 GS_UNIT_CLOCK_GATE_DISABLE |
8473 CL_UNIT_CLOCK_GATE_DISABLE);
8474 I915_WRITE(RAMCLK_GATE_D, 0);
8475 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8476 OVRUNIT_CLOCK_GATE_DISABLE |
8477 OVCUNIT_CLOCK_GATE_DISABLE;
8478 if (IS_GM45(dev))
8479 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8480 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
8481}
Yuanhan Liu13982612010-12-15 15:42:31 +08008482
Jesse Barnes6067aae2011-04-28 15:04:31 -07008483static void crestline_init_clock_gating(struct drm_device *dev)
8484{
8485 struct drm_i915_private *dev_priv = dev->dev_private;
Yuanhan Liu13982612010-12-15 15:42:31 +08008486
Jesse Barnes6067aae2011-04-28 15:04:31 -07008487 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8488 I915_WRITE(RENCLK_GATE_D2, 0);
8489 I915_WRITE(DSPCLK_GATE_D, 0);
8490 I915_WRITE(RAMCLK_GATE_D, 0);
8491 I915_WRITE16(DEUC, 0);
8492}
Jesse Barnes652c3932009-08-17 13:31:43 -07008493
Jesse Barnes6067aae2011-04-28 15:04:31 -07008494static void broadwater_init_clock_gating(struct drm_device *dev)
8495{
8496 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008497
Jesse Barnes6067aae2011-04-28 15:04:31 -07008498 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8499 I965_RCC_CLOCK_GATE_DISABLE |
8500 I965_RCPB_CLOCK_GATE_DISABLE |
8501 I965_ISC_CLOCK_GATE_DISABLE |
8502 I965_FBC_CLOCK_GATE_DISABLE);
8503 I915_WRITE(RENCLK_GATE_D2, 0);
8504}
Jesse Barnes652c3932009-08-17 13:31:43 -07008505
Jesse Barnes6067aae2011-04-28 15:04:31 -07008506static void gen3_init_clock_gating(struct drm_device *dev)
8507{
8508 struct drm_i915_private *dev_priv = dev->dev_private;
8509 u32 dstate = I915_READ(D_STATE);
8510
8511 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8512 DSTATE_DOT_CLOCK_GATING;
8513 I915_WRITE(D_STATE, dstate);
8514}
8515
8516static void i85x_init_clock_gating(struct drm_device *dev)
8517{
8518 struct drm_i915_private *dev_priv = dev->dev_private;
8519
8520 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
8521}
8522
8523static void i830_init_clock_gating(struct drm_device *dev)
8524{
8525 struct drm_i915_private *dev_priv = dev->dev_private;
8526
8527 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes652c3932009-08-17 13:31:43 -07008528}
8529
Jesse Barnes645c62a2011-05-11 09:49:31 -07008530static void ibx_init_clock_gating(struct drm_device *dev)
8531{
8532 struct drm_i915_private *dev_priv = dev->dev_private;
8533
8534 /*
8535 * On Ibex Peak and Cougar Point, we need to disable clock
8536 * gating for the panel power sequencer or it will fail to
8537 * start up when no ports are active.
8538 */
8539 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
8540}
8541
8542static void cpt_init_clock_gating(struct drm_device *dev)
8543{
8544 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes3bcf6032011-07-27 11:51:40 -07008545 int pipe;
Jesse Barnes645c62a2011-05-11 09:49:31 -07008546
8547 /*
8548 * On Ibex Peak and Cougar Point, we need to disable clock
8549 * gating for the panel power sequencer or it will fail to
8550 * start up when no ports are active.
8551 */
8552 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
8553 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8554 DPLS_EDP_PPS_FIX_DIS);
Jesse Barnes3bcf6032011-07-27 11:51:40 -07008555 /* Without this, mode sets may fail silently on FDI */
8556 for_each_pipe(pipe)
8557 I915_WRITE(TRANS_CHICKEN2(pipe), TRANS_AUTOTRAIN_GEN_STALL_DIS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008558}
8559
Chris Wilsonac668082011-02-09 16:15:32 +00008560static void ironlake_teardown_rc6(struct drm_device *dev)
Chris Wilson0cdab212010-12-05 17:27:06 +00008561{
8562 struct drm_i915_private *dev_priv = dev->dev_private;
8563
8564 if (dev_priv->renderctx) {
Chris Wilsonac668082011-02-09 16:15:32 +00008565 i915_gem_object_unpin(dev_priv->renderctx);
8566 drm_gem_object_unreference(&dev_priv->renderctx->base);
Chris Wilson0cdab212010-12-05 17:27:06 +00008567 dev_priv->renderctx = NULL;
8568 }
8569
8570 if (dev_priv->pwrctx) {
Chris Wilsonac668082011-02-09 16:15:32 +00008571 i915_gem_object_unpin(dev_priv->pwrctx);
8572 drm_gem_object_unreference(&dev_priv->pwrctx->base);
Chris Wilson0cdab212010-12-05 17:27:06 +00008573 dev_priv->pwrctx = NULL;
8574 }
8575}
8576
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008577static void ironlake_disable_rc6(struct drm_device *dev)
8578{
8579 struct drm_i915_private *dev_priv = dev->dev_private;
8580
Chris Wilsonac668082011-02-09 16:15:32 +00008581 if (I915_READ(PWRCTXA)) {
8582 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
8583 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
8584 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
8585 50);
8586
8587 I915_WRITE(PWRCTXA, 0);
8588 POSTING_READ(PWRCTXA);
8589
8590 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
8591 POSTING_READ(RSTDBYCTL);
8592 }
8593
Chris Wilson99507302011-02-24 09:42:52 +00008594 ironlake_teardown_rc6(dev);
Chris Wilsonac668082011-02-09 16:15:32 +00008595}
8596
8597static int ironlake_setup_rc6(struct drm_device *dev)
8598{
8599 struct drm_i915_private *dev_priv = dev->dev_private;
8600
8601 if (dev_priv->renderctx == NULL)
8602 dev_priv->renderctx = intel_alloc_context_page(dev);
8603 if (!dev_priv->renderctx)
8604 return -ENOMEM;
8605
8606 if (dev_priv->pwrctx == NULL)
8607 dev_priv->pwrctx = intel_alloc_context_page(dev);
8608 if (!dev_priv->pwrctx) {
8609 ironlake_teardown_rc6(dev);
8610 return -ENOMEM;
8611 }
8612
8613 return 0;
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008614}
8615
8616void ironlake_enable_rc6(struct drm_device *dev)
8617{
8618 struct drm_i915_private *dev_priv = dev->dev_private;
8619 int ret;
8620
Chris Wilsonac668082011-02-09 16:15:32 +00008621 /* rc6 disabled by default due to repeated reports of hanging during
8622 * boot and resume.
8623 */
Keith Packardc0f372b32011-11-16 22:24:52 -08008624 if (!intel_enable_rc6(dev))
Chris Wilsonac668082011-02-09 16:15:32 +00008625 return;
8626
Ben Widawsky2c34b852011-03-19 18:14:26 -07008627 mutex_lock(&dev->struct_mutex);
Chris Wilsonac668082011-02-09 16:15:32 +00008628 ret = ironlake_setup_rc6(dev);
Ben Widawsky2c34b852011-03-19 18:14:26 -07008629 if (ret) {
8630 mutex_unlock(&dev->struct_mutex);
Chris Wilsonac668082011-02-09 16:15:32 +00008631 return;
Ben Widawsky2c34b852011-03-19 18:14:26 -07008632 }
Chris Wilsonac668082011-02-09 16:15:32 +00008633
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008634 /*
8635 * GPU can automatically power down the render unit if given a page
8636 * to save state.
8637 */
8638 ret = BEGIN_LP_RING(6);
8639 if (ret) {
Chris Wilsonac668082011-02-09 16:15:32 +00008640 ironlake_teardown_rc6(dev);
Ben Widawsky2c34b852011-03-19 18:14:26 -07008641 mutex_unlock(&dev->struct_mutex);
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008642 return;
8643 }
Chris Wilsonac668082011-02-09 16:15:32 +00008644
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008645 OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
8646 OUT_RING(MI_SET_CONTEXT);
8647 OUT_RING(dev_priv->renderctx->gtt_offset |
8648 MI_MM_SPACE_GTT |
8649 MI_SAVE_EXT_STATE_EN |
8650 MI_RESTORE_EXT_STATE_EN |
8651 MI_RESTORE_INHIBIT);
8652 OUT_RING(MI_SUSPEND_FLUSH);
8653 OUT_RING(MI_NOOP);
8654 OUT_RING(MI_FLUSH);
8655 ADVANCE_LP_RING();
8656
Ben Widawsky4a246cf2011-03-19 18:14:28 -07008657 /*
8658 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
8659 * does an implicit flush, combined with MI_FLUSH above, it should be
8660 * safe to assume that renderctx is valid
8661 */
8662 ret = intel_wait_ring_idle(LP_RING(dev_priv));
8663 if (ret) {
8664 DRM_ERROR("failed to enable ironlake power power savings\n");
8665 ironlake_teardown_rc6(dev);
8666 mutex_unlock(&dev->struct_mutex);
8667 return;
8668 }
8669
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008670 I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
8671 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawsky2c34b852011-03-19 18:14:26 -07008672 mutex_unlock(&dev->struct_mutex);
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008673}
8674
Jesse Barnes645c62a2011-05-11 09:49:31 -07008675void intel_init_clock_gating(struct drm_device *dev)
8676{
8677 struct drm_i915_private *dev_priv = dev->dev_private;
8678
8679 dev_priv->display.init_clock_gating(dev);
8680
8681 if (dev_priv->display.init_pch_clock_gating)
8682 dev_priv->display.init_pch_clock_gating(dev);
8683}
Chris Wilsonac668082011-02-09 16:15:32 +00008684
Jesse Barnese70236a2009-09-21 10:42:27 -07008685/* Set up chip specific display functions */
8686static void intel_init_display(struct drm_device *dev)
8687{
8688 struct drm_i915_private *dev_priv = dev->dev_private;
8689
8690 /* We always want a DPMS function */
Eric Anholtf564048e2011-03-30 13:01:02 -07008691 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008692 dev_priv->display.dpms = ironlake_crtc_dpms;
Eric Anholtf564048e2011-03-30 13:01:02 -07008693 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008694 dev_priv->display.update_plane = ironlake_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008695 } else {
Jesse Barnese70236a2009-09-21 10:42:27 -07008696 dev_priv->display.dpms = i9xx_crtc_dpms;
Eric Anholtf564048e2011-03-30 13:01:02 -07008697 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008698 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008699 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008700
Adam Jacksonee5382a2010-04-23 11:17:39 -04008701 if (I915_HAS_FBC(dev)) {
Yuanhan Liu9c04f012010-12-15 15:42:32 +08008702 if (HAS_PCH_SPLIT(dev)) {
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08008703 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
8704 dev_priv->display.enable_fbc = ironlake_enable_fbc;
8705 dev_priv->display.disable_fbc = ironlake_disable_fbc;
8706 } else if (IS_GM45(dev)) {
Jesse Barnes74dff282009-09-14 15:39:40 -07008707 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
8708 dev_priv->display.enable_fbc = g4x_enable_fbc;
8709 dev_priv->display.disable_fbc = g4x_disable_fbc;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008710 } else if (IS_CRESTLINE(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07008711 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
8712 dev_priv->display.enable_fbc = i8xx_enable_fbc;
8713 dev_priv->display.disable_fbc = i8xx_disable_fbc;
8714 }
Jesse Barnes74dff282009-09-14 15:39:40 -07008715 /* 855GM needs testing */
Jesse Barnese70236a2009-09-21 10:42:27 -07008716 }
8717
8718 /* Returns the core display clock speed */
Akshay Joshi0206e352011-08-16 15:34:10 -04008719 if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07008720 dev_priv->display.get_display_clock_speed =
8721 i945_get_display_clock_speed;
8722 else if (IS_I915G(dev))
8723 dev_priv->display.get_display_clock_speed =
8724 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008725 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008726 dev_priv->display.get_display_clock_speed =
8727 i9xx_misc_get_display_clock_speed;
8728 else if (IS_I915GM(dev))
8729 dev_priv->display.get_display_clock_speed =
8730 i915gm_get_display_clock_speed;
8731 else if (IS_I865G(dev))
8732 dev_priv->display.get_display_clock_speed =
8733 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02008734 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008735 dev_priv->display.get_display_clock_speed =
8736 i855_get_display_clock_speed;
8737 else /* 852, 830 */
8738 dev_priv->display.get_display_clock_speed =
8739 i830_get_display_clock_speed;
8740
8741 /* For FIFO watermark updates */
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008742 if (HAS_PCH_SPLIT(dev)) {
Keith Packard8d715f02011-11-18 20:39:01 -08008743 dev_priv->display.force_wake_get = __gen6_gt_force_wake_get;
8744 dev_priv->display.force_wake_put = __gen6_gt_force_wake_put;
8745
8746 /* IVB configs may use multi-threaded forcewake */
8747 if (IS_IVYBRIDGE(dev)) {
8748 u32 ecobus;
8749
Keith Packardc7dffff2011-12-09 11:33:00 -08008750 /* A small trick here - if the bios hasn't configured MT forcewake,
8751 * and if the device is in RC6, then force_wake_mt_get will not wake
8752 * the device and the ECOBUS read will return zero. Which will be
8753 * (correctly) interpreted by the test below as MT forcewake being
8754 * disabled.
8755 */
Keith Packard8d715f02011-11-18 20:39:01 -08008756 mutex_lock(&dev->struct_mutex);
8757 __gen6_gt_force_wake_mt_get(dev_priv);
Keith Packardc7dffff2011-12-09 11:33:00 -08008758 ecobus = I915_READ_NOTRACE(ECOBUS);
Keith Packard8d715f02011-11-18 20:39:01 -08008759 __gen6_gt_force_wake_mt_put(dev_priv);
8760 mutex_unlock(&dev->struct_mutex);
8761
8762 if (ecobus & FORCEWAKE_MT_ENABLE) {
8763 DRM_DEBUG_KMS("Using MT version of forcewake\n");
8764 dev_priv->display.force_wake_get =
8765 __gen6_gt_force_wake_mt_get;
8766 dev_priv->display.force_wake_put =
8767 __gen6_gt_force_wake_mt_put;
8768 }
8769 }
8770
Jesse Barnes645c62a2011-05-11 09:49:31 -07008771 if (HAS_PCH_IBX(dev))
8772 dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
8773 else if (HAS_PCH_CPT(dev))
8774 dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
8775
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01008776 if (IS_GEN5(dev)) {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008777 if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
8778 dev_priv->display.update_wm = ironlake_update_wm;
8779 else {
8780 DRM_DEBUG_KMS("Failed to get proper latency. "
8781 "Disable CxSR\n");
8782 dev_priv->display.update_wm = NULL;
8783 }
Jesse Barnes674cf962011-04-28 14:27:04 -07008784 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008785 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Wu Fengguange0dac652011-09-05 14:25:34 +08008786 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08008787 } else if (IS_GEN6(dev)) {
8788 if (SNB_READ_WM0_LATENCY()) {
8789 dev_priv->display.update_wm = sandybridge_update_wm;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08008790 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
Yuanhan Liu13982612010-12-15 15:42:31 +08008791 } else {
8792 DRM_DEBUG_KMS("Failed to read display plane latency. "
8793 "Disable CxSR\n");
8794 dev_priv->display.update_wm = NULL;
8795 }
Jesse Barnes674cf962011-04-28 14:27:04 -07008796 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008797 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Wu Fengguange0dac652011-09-05 14:25:34 +08008798 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07008799 } else if (IS_IVYBRIDGE(dev)) {
8800 /* FIXME: detect B0+ stepping and use auto training */
8801 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Jesse Barnesfe100d42011-04-28 14:29:45 -07008802 if (SNB_READ_WM0_LATENCY()) {
8803 dev_priv->display.update_wm = sandybridge_update_wm;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08008804 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
Jesse Barnesfe100d42011-04-28 14:29:45 -07008805 } else {
8806 DRM_DEBUG_KMS("Failed to read display plane latency. "
8807 "Disable CxSR\n");
8808 dev_priv->display.update_wm = NULL;
8809 }
Jesse Barnes28963a32011-05-11 09:42:30 -07008810 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Wu Fengguange0dac652011-09-05 14:25:34 +08008811 dev_priv->display.write_eld = ironlake_write_eld;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008812 } else
8813 dev_priv->display.update_wm = NULL;
8814 } else if (IS_PINEVIEW(dev)) {
Zhao Yakuid4294342010-03-22 22:45:36 +08008815 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
Li Peng95534262010-05-18 18:58:44 +08008816 dev_priv->is_ddr3,
Zhao Yakuid4294342010-03-22 22:45:36 +08008817 dev_priv->fsb_freq,
8818 dev_priv->mem_freq)) {
8819 DRM_INFO("failed to find known CxSR latency "
Li Peng95534262010-05-18 18:58:44 +08008820 "(found ddr%s fsb freq %d, mem freq %d), "
Zhao Yakuid4294342010-03-22 22:45:36 +08008821 "disabling CxSR\n",
Akshay Joshi0206e352011-08-16 15:34:10 -04008822 (dev_priv->is_ddr3 == 1) ? "3" : "2",
Zhao Yakuid4294342010-03-22 22:45:36 +08008823 dev_priv->fsb_freq, dev_priv->mem_freq);
8824 /* Disable CxSR and never update its watermark again */
8825 pineview_disable_cxsr(dev);
8826 dev_priv->display.update_wm = NULL;
8827 } else
8828 dev_priv->display.update_wm = pineview_update_wm;
Jason Stubbs95e0ee92011-05-28 14:26:48 +10008829 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008830 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08008831 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07008832 dev_priv->display.update_wm = g4x_update_wm;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008833 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
8834 } else if (IS_GEN4(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07008835 dev_priv->display.update_wm = i965_update_wm;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008836 if (IS_CRESTLINE(dev))
8837 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
8838 else if (IS_BROADWATER(dev))
8839 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
8840 } else if (IS_GEN3(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07008841 dev_priv->display.update_wm = i9xx_update_wm;
8842 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008843 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8844 } else if (IS_I865G(dev)) {
8845 dev_priv->display.update_wm = i830_update_wm;
8846 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
8847 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Adam Jackson8f4695e2010-04-16 18:20:57 -04008848 } else if (IS_I85X(dev)) {
8849 dev_priv->display.update_wm = i9xx_update_wm;
8850 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008851 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Jesse Barnese70236a2009-09-21 10:42:27 -07008852 } else {
Adam Jackson8f4695e2010-04-16 18:20:57 -04008853 dev_priv->display.update_wm = i830_update_wm;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008854 dev_priv->display.init_clock_gating = i830_init_clock_gating;
Adam Jackson8f4695e2010-04-16 18:20:57 -04008855 if (IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008856 dev_priv->display.get_fifo_size = i845_get_fifo_size;
8857 else
8858 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Jesse Barnese70236a2009-09-21 10:42:27 -07008859 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008860
8861 /* Default just returns -ENODEV to indicate unsupported */
8862 dev_priv->display.queue_flip = intel_default_queue_flip;
8863
8864 switch (INTEL_INFO(dev)->gen) {
8865 case 2:
8866 dev_priv->display.queue_flip = intel_gen2_queue_flip;
8867 break;
8868
8869 case 3:
8870 dev_priv->display.queue_flip = intel_gen3_queue_flip;
8871 break;
8872
8873 case 4:
8874 case 5:
8875 dev_priv->display.queue_flip = intel_gen4_queue_flip;
8876 break;
8877
8878 case 6:
8879 dev_priv->display.queue_flip = intel_gen6_queue_flip;
8880 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07008881 case 7:
8882 dev_priv->display.queue_flip = intel_gen7_queue_flip;
8883 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008884 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008885}
8886
Jesse Barnesb690e962010-07-19 13:53:12 -07008887/*
8888 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
8889 * resume, or other times. This quirk makes sure that's the case for
8890 * affected systems.
8891 */
Akshay Joshi0206e352011-08-16 15:34:10 -04008892static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07008893{
8894 struct drm_i915_private *dev_priv = dev->dev_private;
8895
8896 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
8897 DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
8898}
8899
Keith Packard435793d2011-07-12 14:56:22 -07008900/*
8901 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
8902 */
8903static void quirk_ssc_force_disable(struct drm_device *dev)
8904{
8905 struct drm_i915_private *dev_priv = dev->dev_private;
8906 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
8907}
8908
Jesse Barnesb690e962010-07-19 13:53:12 -07008909struct intel_quirk {
8910 int device;
8911 int subsystem_vendor;
8912 int subsystem_device;
8913 void (*hook)(struct drm_device *dev);
8914};
8915
8916struct intel_quirk intel_quirks[] = {
8917 /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
8918 { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
8919 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04008920 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07008921
8922 /* Thinkpad R31 needs pipe A force quirk */
8923 { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
8924 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
8925 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
8926
8927 /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
8928 { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
8929 /* ThinkPad X40 needs pipe A force quirk */
8930
8931 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
8932 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
8933
8934 /* 855 & before need to leave pipe A & dpll A up */
8935 { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
8936 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07008937
8938 /* Lenovo U160 cannot use SSC on LVDS */
8939 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02008940
8941 /* Sony Vaio Y cannot use SSC on LVDS */
8942 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Jesse Barnesb690e962010-07-19 13:53:12 -07008943};
8944
8945static void intel_init_quirks(struct drm_device *dev)
8946{
8947 struct pci_dev *d = dev->pdev;
8948 int i;
8949
8950 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
8951 struct intel_quirk *q = &intel_quirks[i];
8952
8953 if (d->device == q->device &&
8954 (d->subsystem_vendor == q->subsystem_vendor ||
8955 q->subsystem_vendor == PCI_ANY_ID) &&
8956 (d->subsystem_device == q->subsystem_device ||
8957 q->subsystem_device == PCI_ANY_ID))
8958 q->hook(dev);
8959 }
8960}
8961
Jesse Barnes9cce37f2010-08-13 15:11:26 -07008962/* Disable the VGA plane that we never use */
8963static void i915_disable_vga(struct drm_device *dev)
8964{
8965 struct drm_i915_private *dev_priv = dev->dev_private;
8966 u8 sr1;
8967 u32 vga_reg;
8968
8969 if (HAS_PCH_SPLIT(dev))
8970 vga_reg = CPU_VGACNTRL;
8971 else
8972 vga_reg = VGACNTRL;
8973
8974 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
8975 outb(1, VGA_SR_INDEX);
8976 sr1 = inb(VGA_SR_DATA);
8977 outb(sr1 | 1<<5, VGA_SR_DATA);
8978 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
8979 udelay(300);
8980
8981 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
8982 POSTING_READ(vga_reg);
8983}
8984
Jesse Barnes79e53942008-11-07 14:24:08 -08008985void intel_modeset_init(struct drm_device *dev)
8986{
Jesse Barnes652c3932009-08-17 13:31:43 -07008987 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08008988 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008989
8990 drm_mode_config_init(dev);
8991
8992 dev->mode_config.min_width = 0;
8993 dev->mode_config.min_height = 0;
8994
8995 dev->mode_config.funcs = (void *)&intel_mode_funcs;
8996
Jesse Barnesb690e962010-07-19 13:53:12 -07008997 intel_init_quirks(dev);
8998
Jesse Barnese70236a2009-09-21 10:42:27 -07008999 intel_init_display(dev);
9000
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009001 if (IS_GEN2(dev)) {
9002 dev->mode_config.max_width = 2048;
9003 dev->mode_config.max_height = 2048;
9004 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07009005 dev->mode_config.max_width = 4096;
9006 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08009007 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009008 dev->mode_config.max_width = 8192;
9009 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08009010 }
Chris Wilson35c30472010-12-22 14:07:12 +00009011 dev->mode_config.fb_base = dev->agp->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009012
Zhao Yakui28c97732009-10-09 11:39:41 +08009013 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10009014 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08009015
Dave Airliea3524f12010-06-06 18:59:41 +10009016 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009017 intel_crtc_init(dev, i);
Jesse Barnesb840d907f2011-12-13 13:19:38 -08009018 if (HAS_PCH_SPLIT(dev)) {
9019 ret = intel_plane_init(dev, i);
9020 if (ret)
9021 DRM_ERROR("plane %d init failed: %d\n",
9022 i, ret);
9023 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009024 }
9025
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009026 /* Just disable it once at startup */
9027 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009028 intel_setup_outputs(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07009029
Jesse Barnes645c62a2011-05-11 09:49:31 -07009030 intel_init_clock_gating(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009031
Jesse Barnes7648fa92010-05-20 14:28:11 -07009032 if (IS_IRONLAKE_M(dev)) {
Jesse Barnesf97108d2010-01-29 11:27:07 -08009033 ironlake_enable_drps(dev);
Jesse Barnes7648fa92010-05-20 14:28:11 -07009034 intel_init_emon(dev);
9035 }
Jesse Barnesf97108d2010-01-29 11:27:07 -08009036
Jesse Barnes1c70c0c2011-06-29 13:34:36 -07009037 if (IS_GEN6(dev) || IS_GEN7(dev)) {
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08009038 gen6_enable_rps(dev_priv);
Jesse Barnes23b2f8b2011-06-28 13:04:16 -07009039 gen6_update_ring_freq(dev_priv);
9040 }
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08009041
Jesse Barnes652c3932009-08-17 13:31:43 -07009042 INIT_WORK(&dev_priv->idle_work, intel_idle_update);
9043 setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
9044 (unsigned long)dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009045}
9046
9047void intel_modeset_gem_init(struct drm_device *dev)
9048{
9049 if (IS_IRONLAKE_M(dev))
9050 ironlake_enable_rc6(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02009051
9052 intel_setup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009053}
9054
9055void intel_modeset_cleanup(struct drm_device *dev)
9056{
Jesse Barnes652c3932009-08-17 13:31:43 -07009057 struct drm_i915_private *dev_priv = dev->dev_private;
9058 struct drm_crtc *crtc;
9059 struct intel_crtc *intel_crtc;
9060
Keith Packardf87ea762010-10-03 19:36:26 -07009061 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07009062 mutex_lock(&dev->struct_mutex);
9063
Jesse Barnes723bfd72010-10-07 16:01:13 -07009064 intel_unregister_dsm_handler();
9065
9066
Jesse Barnes652c3932009-08-17 13:31:43 -07009067 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9068 /* Skip inactive CRTCs */
9069 if (!crtc->fb)
9070 continue;
9071
9072 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02009073 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009074 }
9075
Chris Wilson973d04f2011-07-08 12:22:37 +01009076 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07009077
Jesse Barnesf97108d2010-01-29 11:27:07 -08009078 if (IS_IRONLAKE_M(dev))
9079 ironlake_disable_drps(dev);
Jesse Barnes1c70c0c2011-06-29 13:34:36 -07009080 if (IS_GEN6(dev) || IS_GEN7(dev))
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08009081 gen6_disable_rps(dev);
Jesse Barnesf97108d2010-01-29 11:27:07 -08009082
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009083 if (IS_IRONLAKE_M(dev))
9084 ironlake_disable_rc6(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00009085
Kristian Høgsberg69341a52009-11-11 12:19:17 -05009086 mutex_unlock(&dev->struct_mutex);
9087
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009088 /* Disable the irq before mode object teardown, for the irq might
9089 * enqueue unpin/hotplug work. */
9090 drm_irq_uninstall(dev);
9091 cancel_work_sync(&dev_priv->hotplug_work);
Daniel Vetter6fdd4d92011-09-08 14:00:22 +02009092 cancel_work_sync(&dev_priv->rps_work);
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009093
Chris Wilson1630fe72011-07-08 12:22:42 +01009094 /* flush any delayed tasks or pending work */
9095 flush_scheduled_work();
9096
Daniel Vetter3dec0092010-08-20 21:40:52 +02009097 /* Shut off idle work before the crtcs get freed. */
9098 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9099 intel_crtc = to_intel_crtc(crtc);
9100 del_timer_sync(&intel_crtc->idle_timer);
9101 }
9102 del_timer_sync(&dev_priv->idle_timer);
9103 cancel_work_sync(&dev_priv->idle_work);
9104
Jesse Barnes79e53942008-11-07 14:24:08 -08009105 drm_mode_config_cleanup(dev);
9106}
9107
Dave Airlie28d52042009-09-21 14:33:58 +10009108/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08009109 * Return which encoder is currently attached for connector.
9110 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01009111struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08009112{
Chris Wilsondf0e9242010-09-09 16:20:55 +01009113 return &intel_attached_encoder(connector)->base;
9114}
Jesse Barnes79e53942008-11-07 14:24:08 -08009115
Chris Wilsondf0e9242010-09-09 16:20:55 +01009116void intel_connector_attach_encoder(struct intel_connector *connector,
9117 struct intel_encoder *encoder)
9118{
9119 connector->encoder = encoder;
9120 drm_mode_connector_attach_encoder(&connector->base,
9121 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009122}
Dave Airlie28d52042009-09-21 14:33:58 +10009123
9124/*
9125 * set vga decode state - true == enable VGA decode
9126 */
9127int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9128{
9129 struct drm_i915_private *dev_priv = dev->dev_private;
9130 u16 gmch_ctrl;
9131
9132 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9133 if (state)
9134 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9135 else
9136 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9137 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9138 return 0;
9139}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009140
9141#ifdef CONFIG_DEBUG_FS
9142#include <linux/seq_file.h>
9143
9144struct intel_display_error_state {
9145 struct intel_cursor_error_state {
9146 u32 control;
9147 u32 position;
9148 u32 base;
9149 u32 size;
9150 } cursor[2];
9151
9152 struct intel_pipe_error_state {
9153 u32 conf;
9154 u32 source;
9155
9156 u32 htotal;
9157 u32 hblank;
9158 u32 hsync;
9159 u32 vtotal;
9160 u32 vblank;
9161 u32 vsync;
9162 } pipe[2];
9163
9164 struct intel_plane_error_state {
9165 u32 control;
9166 u32 stride;
9167 u32 size;
9168 u32 pos;
9169 u32 addr;
9170 u32 surface;
9171 u32 tile_offset;
9172 } plane[2];
9173};
9174
9175struct intel_display_error_state *
9176intel_display_capture_error_state(struct drm_device *dev)
9177{
Akshay Joshi0206e352011-08-16 15:34:10 -04009178 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009179 struct intel_display_error_state *error;
9180 int i;
9181
9182 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9183 if (error == NULL)
9184 return NULL;
9185
9186 for (i = 0; i < 2; i++) {
9187 error->cursor[i].control = I915_READ(CURCNTR(i));
9188 error->cursor[i].position = I915_READ(CURPOS(i));
9189 error->cursor[i].base = I915_READ(CURBASE(i));
9190
9191 error->plane[i].control = I915_READ(DSPCNTR(i));
9192 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
9193 error->plane[i].size = I915_READ(DSPSIZE(i));
Akshay Joshi0206e352011-08-16 15:34:10 -04009194 error->plane[i].pos = I915_READ(DSPPOS(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009195 error->plane[i].addr = I915_READ(DSPADDR(i));
9196 if (INTEL_INFO(dev)->gen >= 4) {
9197 error->plane[i].surface = I915_READ(DSPSURF(i));
9198 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9199 }
9200
9201 error->pipe[i].conf = I915_READ(PIPECONF(i));
9202 error->pipe[i].source = I915_READ(PIPESRC(i));
9203 error->pipe[i].htotal = I915_READ(HTOTAL(i));
9204 error->pipe[i].hblank = I915_READ(HBLANK(i));
9205 error->pipe[i].hsync = I915_READ(HSYNC(i));
9206 error->pipe[i].vtotal = I915_READ(VTOTAL(i));
9207 error->pipe[i].vblank = I915_READ(VBLANK(i));
9208 error->pipe[i].vsync = I915_READ(VSYNC(i));
9209 }
9210
9211 return error;
9212}
9213
9214void
9215intel_display_print_error_state(struct seq_file *m,
9216 struct drm_device *dev,
9217 struct intel_display_error_state *error)
9218{
9219 int i;
9220
9221 for (i = 0; i < 2; i++) {
9222 seq_printf(m, "Pipe [%d]:\n", i);
9223 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9224 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
9225 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9226 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9227 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9228 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9229 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9230 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9231
9232 seq_printf(m, "Plane [%d]:\n", i);
9233 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
9234 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
9235 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
9236 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
9237 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
9238 if (INTEL_INFO(dev)->gen >= 4) {
9239 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9240 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9241 }
9242
9243 seq_printf(m, "Cursor [%d]:\n", i);
9244 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9245 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9246 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
9247 }
9248}
9249#endif