blob: a76ac2eb99384977dfff324052236987fba85dae [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 *,
Sean Paulcec2f352012-01-10 15:09:36 -080078 int, int, intel_clock_t *, 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,
Sean Paulcec2f352012-01-10 15:09:36 -080086 int target, int refclk, intel_clock_t *match_clock,
87 intel_clock_t *best_clock);
Ma Lingd4906092009-03-18 20:13:27 +080088static bool
89intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080090 int target, int refclk, intel_clock_t *match_clock,
91 intel_clock_t *best_clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080092
Keith Packarda4fc5ed2009-04-07 16:16:42 -070093static bool
94intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080095 int target, int refclk, intel_clock_t *match_clock,
96 intel_clock_t *best_clock);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080097static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -050098intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -080099 int target, int refclk, intel_clock_t *match_clock,
100 intel_clock_t *best_clock);
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700101
Chris Wilson021357a2010-09-07 20:54:59 +0100102static inline u32 /* units of 100MHz */
103intel_fdi_link_freq(struct drm_device *dev)
104{
Chris Wilson8b99e682010-10-13 09:59:17 +0100105 if (IS_GEN5(dev)) {
106 struct drm_i915_private *dev_priv = dev->dev_private;
107 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
108 } else
109 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100110}
111
Keith Packarde4b36692009-06-05 19:22:17 -0700112static const intel_limit_t intel_limits_i8xx_dvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400113 .dot = { .min = 25000, .max = 350000 },
114 .vco = { .min = 930000, .max = 1400000 },
115 .n = { .min = 3, .max = 16 },
116 .m = { .min = 96, .max = 140 },
117 .m1 = { .min = 18, .max = 26 },
118 .m2 = { .min = 6, .max = 16 },
119 .p = { .min = 4, .max = 128 },
120 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700121 .p2 = { .dot_limit = 165000,
122 .p2_slow = 4, .p2_fast = 2 },
Ma Lingd4906092009-03-18 20:13:27 +0800123 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700124};
125
126static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400127 .dot = { .min = 25000, .max = 350000 },
128 .vco = { .min = 930000, .max = 1400000 },
129 .n = { .min = 3, .max = 16 },
130 .m = { .min = 96, .max = 140 },
131 .m1 = { .min = 18, .max = 26 },
132 .m2 = { .min = 6, .max = 16 },
133 .p = { .min = 4, .max = 128 },
134 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700135 .p2 = { .dot_limit = 165000,
136 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800137 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700138};
Eric Anholt273e27c2011-03-30 13:01:10 -0700139
Keith Packarde4b36692009-06-05 19:22:17 -0700140static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400141 .dot = { .min = 20000, .max = 400000 },
142 .vco = { .min = 1400000, .max = 2800000 },
143 .n = { .min = 1, .max = 6 },
144 .m = { .min = 70, .max = 120 },
145 .m1 = { .min = 10, .max = 22 },
146 .m2 = { .min = 5, .max = 9 },
147 .p = { .min = 5, .max = 80 },
148 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700149 .p2 = { .dot_limit = 200000,
150 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800151 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700152};
153
154static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400155 .dot = { .min = 20000, .max = 400000 },
156 .vco = { .min = 1400000, .max = 2800000 },
157 .n = { .min = 1, .max = 6 },
158 .m = { .min = 70, .max = 120 },
159 .m1 = { .min = 10, .max = 22 },
160 .m2 = { .min = 5, .max = 9 },
161 .p = { .min = 7, .max = 98 },
162 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700163 .p2 = { .dot_limit = 112000,
164 .p2_slow = 14, .p2_fast = 7 },
Ma Lingd4906092009-03-18 20:13:27 +0800165 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700166};
167
Eric Anholt273e27c2011-03-30 13:01:10 -0700168
Keith Packarde4b36692009-06-05 19:22:17 -0700169static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700170 .dot = { .min = 25000, .max = 270000 },
171 .vco = { .min = 1750000, .max = 3500000},
172 .n = { .min = 1, .max = 4 },
173 .m = { .min = 104, .max = 138 },
174 .m1 = { .min = 17, .max = 23 },
175 .m2 = { .min = 5, .max = 11 },
176 .p = { .min = 10, .max = 30 },
177 .p1 = { .min = 1, .max = 3},
178 .p2 = { .dot_limit = 270000,
179 .p2_slow = 10,
180 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800181 },
Ma Lingd4906092009-03-18 20:13:27 +0800182 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700183};
184
185static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700186 .dot = { .min = 22000, .max = 400000 },
187 .vco = { .min = 1750000, .max = 3500000},
188 .n = { .min = 1, .max = 4 },
189 .m = { .min = 104, .max = 138 },
190 .m1 = { .min = 16, .max = 23 },
191 .m2 = { .min = 5, .max = 11 },
192 .p = { .min = 5, .max = 80 },
193 .p1 = { .min = 1, .max = 8},
194 .p2 = { .dot_limit = 165000,
195 .p2_slow = 10, .p2_fast = 5 },
Ma Lingd4906092009-03-18 20:13:27 +0800196 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700197};
198
199static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700200 .dot = { .min = 20000, .max = 115000 },
201 .vco = { .min = 1750000, .max = 3500000 },
202 .n = { .min = 1, .max = 3 },
203 .m = { .min = 104, .max = 138 },
204 .m1 = { .min = 17, .max = 23 },
205 .m2 = { .min = 5, .max = 11 },
206 .p = { .min = 28, .max = 112 },
207 .p1 = { .min = 2, .max = 8 },
208 .p2 = { .dot_limit = 0,
209 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800210 },
Ma Lingd4906092009-03-18 20:13:27 +0800211 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700212};
213
214static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700215 .dot = { .min = 80000, .max = 224000 },
216 .vco = { .min = 1750000, .max = 3500000 },
217 .n = { .min = 1, .max = 3 },
218 .m = { .min = 104, .max = 138 },
219 .m1 = { .min = 17, .max = 23 },
220 .m2 = { .min = 5, .max = 11 },
221 .p = { .min = 14, .max = 42 },
222 .p1 = { .min = 2, .max = 6 },
223 .p2 = { .dot_limit = 0,
224 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800225 },
Ma Lingd4906092009-03-18 20:13:27 +0800226 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700227};
228
229static const intel_limit_t intel_limits_g4x_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400230 .dot = { .min = 161670, .max = 227000 },
231 .vco = { .min = 1750000, .max = 3500000},
232 .n = { .min = 1, .max = 2 },
233 .m = { .min = 97, .max = 108 },
234 .m1 = { .min = 0x10, .max = 0x12 },
235 .m2 = { .min = 0x05, .max = 0x06 },
236 .p = { .min = 10, .max = 20 },
237 .p1 = { .min = 1, .max = 2},
238 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700239 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400240 .find_pll = intel_find_pll_g4x_dp,
Keith Packarde4b36692009-06-05 19:22:17 -0700241};
242
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500243static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400244 .dot = { .min = 20000, .max = 400000},
245 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700246 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400247 .n = { .min = 3, .max = 6 },
248 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700249 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400250 .m1 = { .min = 0, .max = 0 },
251 .m2 = { .min = 0, .max = 254 },
252 .p = { .min = 5, .max = 80 },
253 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700254 .p2 = { .dot_limit = 200000,
255 .p2_slow = 10, .p2_fast = 5 },
Shaohua Li61157072009-04-03 15:24:43 +0800256 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700257};
258
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500259static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400260 .dot = { .min = 20000, .max = 400000 },
261 .vco = { .min = 1700000, .max = 3500000 },
262 .n = { .min = 3, .max = 6 },
263 .m = { .min = 2, .max = 256 },
264 .m1 = { .min = 0, .max = 0 },
265 .m2 = { .min = 0, .max = 254 },
266 .p = { .min = 7, .max = 112 },
267 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700268 .p2 = { .dot_limit = 112000,
269 .p2_slow = 14, .p2_fast = 14 },
Shaohua Li61157072009-04-03 15:24:43 +0800270 .find_pll = intel_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700271};
272
Eric Anholt273e27c2011-03-30 13:01:10 -0700273/* Ironlake / Sandybridge
274 *
275 * We calculate clock using (register_value + 2) for N/M1/M2, so here
276 * the range value for them is (actual_value - 2).
277 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800278static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700279 .dot = { .min = 25000, .max = 350000 },
280 .vco = { .min = 1760000, .max = 3510000 },
281 .n = { .min = 1, .max = 5 },
282 .m = { .min = 79, .max = 127 },
283 .m1 = { .min = 12, .max = 22 },
284 .m2 = { .min = 5, .max = 9 },
285 .p = { .min = 5, .max = 80 },
286 .p1 = { .min = 1, .max = 8 },
287 .p2 = { .dot_limit = 225000,
288 .p2_slow = 10, .p2_fast = 5 },
Zhao Yakui45476682009-12-31 16:06:04 +0800289 .find_pll = intel_g4x_find_best_PLL,
Keith Packarde4b36692009-06-05 19:22:17 -0700290};
291
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800292static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700293 .dot = { .min = 25000, .max = 350000 },
294 .vco = { .min = 1760000, .max = 3510000 },
295 .n = { .min = 1, .max = 3 },
296 .m = { .min = 79, .max = 118 },
297 .m1 = { .min = 12, .max = 22 },
298 .m2 = { .min = 5, .max = 9 },
299 .p = { .min = 28, .max = 112 },
300 .p1 = { .min = 2, .max = 8 },
301 .p2 = { .dot_limit = 225000,
302 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800303 .find_pll = intel_g4x_find_best_PLL,
304};
305
306static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700307 .dot = { .min = 25000, .max = 350000 },
308 .vco = { .min = 1760000, .max = 3510000 },
309 .n = { .min = 1, .max = 3 },
310 .m = { .min = 79, .max = 127 },
311 .m1 = { .min = 12, .max = 22 },
312 .m2 = { .min = 5, .max = 9 },
313 .p = { .min = 14, .max = 56 },
314 .p1 = { .min = 2, .max = 8 },
315 .p2 = { .dot_limit = 225000,
316 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800317 .find_pll = intel_g4x_find_best_PLL,
318};
319
Eric Anholt273e27c2011-03-30 13:01:10 -0700320/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800321static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700322 .dot = { .min = 25000, .max = 350000 },
323 .vco = { .min = 1760000, .max = 3510000 },
324 .n = { .min = 1, .max = 2 },
325 .m = { .min = 79, .max = 126 },
326 .m1 = { .min = 12, .max = 22 },
327 .m2 = { .min = 5, .max = 9 },
328 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400329 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700330 .p2 = { .dot_limit = 225000,
331 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800332 .find_pll = intel_g4x_find_best_PLL,
333};
334
335static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700336 .dot = { .min = 25000, .max = 350000 },
337 .vco = { .min = 1760000, .max = 3510000 },
338 .n = { .min = 1, .max = 3 },
339 .m = { .min = 79, .max = 126 },
340 .m1 = { .min = 12, .max = 22 },
341 .m2 = { .min = 5, .max = 9 },
342 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400343 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700344 .p2 = { .dot_limit = 225000,
345 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800346 .find_pll = intel_g4x_find_best_PLL,
347};
348
349static const intel_limit_t intel_limits_ironlake_display_port = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400350 .dot = { .min = 25000, .max = 350000 },
351 .vco = { .min = 1760000, .max = 3510000},
352 .n = { .min = 1, .max = 2 },
353 .m = { .min = 81, .max = 90 },
354 .m1 = { .min = 12, .max = 22 },
355 .m2 = { .min = 5, .max = 9 },
356 .p = { .min = 10, .max = 20 },
357 .p1 = { .min = 1, .max = 2},
358 .p2 = { .dot_limit = 0,
Eric Anholt273e27c2011-03-30 13:01:10 -0700359 .p2_slow = 10, .p2_fast = 10 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400360 .find_pll = intel_find_pll_ironlake_dp,
Jesse Barnes79e53942008-11-07 14:24:08 -0800361};
362
Takashi Iwaib0354382012-03-20 13:07:05 +0100363static bool is_dual_link_lvds(struct drm_i915_private *dev_priv,
364 unsigned int reg)
365{
366 unsigned int val;
367
368 if (dev_priv->lvds_val)
369 val = dev_priv->lvds_val;
370 else {
371 /* BIOS should set the proper LVDS register value at boot, but
372 * in reality, it doesn't set the value when the lid is closed;
373 * we need to check "the value to be set" in VBT when LVDS
374 * register is uninitialized.
375 */
376 val = I915_READ(reg);
377 if (!(val & ~LVDS_DETECTED))
378 val = dev_priv->bios_lvds_val;
379 dev_priv->lvds_val = val;
380 }
381 return (val & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP;
382}
383
Chris Wilson1b894b52010-12-14 20:04:54 +0000384static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
385 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800386{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800387 struct drm_device *dev = crtc->dev;
388 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800389 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800390
391 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Takashi Iwaib0354382012-03-20 13:07:05 +0100392 if (is_dual_link_lvds(dev_priv, PCH_LVDS)) {
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800393 /* LVDS dual channel */
Chris Wilson1b894b52010-12-14 20:04:54 +0000394 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800395 limit = &intel_limits_ironlake_dual_lvds_100m;
396 else
397 limit = &intel_limits_ironlake_dual_lvds;
398 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000399 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800400 limit = &intel_limits_ironlake_single_lvds_100m;
401 else
402 limit = &intel_limits_ironlake_single_lvds;
403 }
404 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
Zhao Yakui45476682009-12-31 16:06:04 +0800405 HAS_eDP)
406 limit = &intel_limits_ironlake_display_port;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800407 else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800408 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800409
410 return limit;
411}
412
Ma Ling044c7c42009-03-18 20:13:23 +0800413static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
414{
415 struct drm_device *dev = crtc->dev;
416 struct drm_i915_private *dev_priv = dev->dev_private;
417 const intel_limit_t *limit;
418
419 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Takashi Iwaib0354382012-03-20 13:07:05 +0100420 if (is_dual_link_lvds(dev_priv, LVDS))
Ma Ling044c7c42009-03-18 20:13:23 +0800421 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700422 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800423 else
424 /* LVDS with dual channel */
Keith Packarde4b36692009-06-05 19:22:17 -0700425 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800426 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
427 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700428 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800429 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700430 limit = &intel_limits_g4x_sdvo;
Akshay Joshi0206e352011-08-16 15:34:10 -0400431 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700432 limit = &intel_limits_g4x_display_port;
Ma Ling044c7c42009-03-18 20:13:23 +0800433 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700434 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800435
436 return limit;
437}
438
Chris Wilson1b894b52010-12-14 20:04:54 +0000439static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800440{
441 struct drm_device *dev = crtc->dev;
442 const intel_limit_t *limit;
443
Eric Anholtbad720f2009-10-22 16:11:14 -0700444 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000445 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800446 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800447 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500448 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800449 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500450 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800451 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500452 limit = &intel_limits_pineview_sdvo;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100453 } else if (!IS_GEN2(dev)) {
454 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
455 limit = &intel_limits_i9xx_lvds;
456 else
457 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800458 } else {
459 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700460 limit = &intel_limits_i8xx_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -0800461 else
Keith Packarde4b36692009-06-05 19:22:17 -0700462 limit = &intel_limits_i8xx_dvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800463 }
464 return limit;
465}
466
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500467/* m1 is reserved as 0 in Pineview, n is a ring counter */
468static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800469{
Shaohua Li21778322009-02-23 15:19:16 +0800470 clock->m = clock->m2 + 2;
471 clock->p = clock->p1 * clock->p2;
472 clock->vco = refclk * clock->m / clock->n;
473 clock->dot = clock->vco / clock->p;
474}
475
476static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
477{
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500478 if (IS_PINEVIEW(dev)) {
479 pineview_clock(refclk, clock);
Shaohua Li21778322009-02-23 15:19:16 +0800480 return;
481 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800482 clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
483 clock->p = clock->p1 * clock->p2;
484 clock->vco = refclk * clock->m / (clock->n + 2);
485 clock->dot = clock->vco / clock->p;
486}
487
Jesse Barnes79e53942008-11-07 14:24:08 -0800488/**
489 * Returns whether any output on the specified pipe is of the specified type
490 */
Chris Wilson4ef69c72010-09-09 15:14:28 +0100491bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
Jesse Barnes79e53942008-11-07 14:24:08 -0800492{
Chris Wilson4ef69c72010-09-09 15:14:28 +0100493 struct drm_device *dev = crtc->dev;
494 struct drm_mode_config *mode_config = &dev->mode_config;
495 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -0800496
Chris Wilson4ef69c72010-09-09 15:14:28 +0100497 list_for_each_entry(encoder, &mode_config->encoder_list, base.head)
498 if (encoder->base.crtc == crtc && encoder->type == type)
499 return true;
500
501 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -0800502}
503
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800504#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800505/**
506 * Returns whether the given set of divisors are valid for a given refclk with
507 * the given connectors.
508 */
509
Chris Wilson1b894b52010-12-14 20:04:54 +0000510static bool intel_PLL_is_valid(struct drm_device *dev,
511 const intel_limit_t *limit,
512 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800513{
Jesse Barnes79e53942008-11-07 14:24:08 -0800514 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400515 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800516 if (clock->p < limit->p.min || limit->p.max < clock->p)
Akshay Joshi0206e352011-08-16 15:34:10 -0400517 INTELPllInvalid("p out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800518 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400519 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800520 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400521 INTELPllInvalid("m1 out of range\n");
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500522 if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
Akshay Joshi0206e352011-08-16 15:34:10 -0400523 INTELPllInvalid("m1 <= m2\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800524 if (clock->m < limit->m.min || limit->m.max < clock->m)
Akshay Joshi0206e352011-08-16 15:34:10 -0400525 INTELPllInvalid("m out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800526 if (clock->n < limit->n.min || limit->n.max < clock->n)
Akshay Joshi0206e352011-08-16 15:34:10 -0400527 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800528 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400529 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800530 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
531 * connector, etc., rather than just a single range.
532 */
533 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400534 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800535
536 return true;
537}
538
Ma Lingd4906092009-03-18 20:13:27 +0800539static bool
540intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800541 int target, int refclk, intel_clock_t *match_clock,
542 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800543
Jesse Barnes79e53942008-11-07 14:24:08 -0800544{
545 struct drm_device *dev = crtc->dev;
546 struct drm_i915_private *dev_priv = dev->dev_private;
547 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800548 int err = target;
549
Bruno Prémontbc5e5712009-08-08 13:01:17 +0200550 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Florian Mickler832cc282009-07-13 18:40:32 +0800551 (I915_READ(LVDS)) != 0) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800552 /*
553 * For LVDS, if the panel is on, just rely on its current
554 * settings for dual-channel. We haven't figured out how to
555 * reliably set up different single/dual channel state, if we
556 * even can.
557 */
Takashi Iwaib0354382012-03-20 13:07:05 +0100558 if (is_dual_link_lvds(dev_priv, LVDS))
Jesse Barnes79e53942008-11-07 14:24:08 -0800559 clock.p2 = limit->p2.p2_fast;
560 else
561 clock.p2 = limit->p2.p2_slow;
562 } else {
563 if (target < limit->p2.dot_limit)
564 clock.p2 = limit->p2.p2_slow;
565 else
566 clock.p2 = limit->p2.p2_fast;
567 }
568
Akshay Joshi0206e352011-08-16 15:34:10 -0400569 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800570
Zhao Yakui42158662009-11-20 11:24:18 +0800571 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
572 clock.m1++) {
573 for (clock.m2 = limit->m2.min;
574 clock.m2 <= limit->m2.max; clock.m2++) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500575 /* m1 is always 0 in Pineview */
576 if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
Zhao Yakui42158662009-11-20 11:24:18 +0800577 break;
578 for (clock.n = limit->n.min;
579 clock.n <= limit->n.max; clock.n++) {
580 for (clock.p1 = limit->p1.min;
581 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800582 int this_err;
583
Shaohua Li21778322009-02-23 15:19:16 +0800584 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000585 if (!intel_PLL_is_valid(dev, limit,
586 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800587 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800588 if (match_clock &&
589 clock.p != match_clock->p)
590 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800591
592 this_err = abs(clock.dot - target);
593 if (this_err < err) {
594 *best_clock = clock;
595 err = this_err;
596 }
597 }
598 }
599 }
600 }
601
602 return (err != target);
603}
604
Ma Lingd4906092009-03-18 20:13:27 +0800605static bool
606intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800607 int target, int refclk, intel_clock_t *match_clock,
608 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800609{
610 struct drm_device *dev = crtc->dev;
611 struct drm_i915_private *dev_priv = dev->dev_private;
612 intel_clock_t clock;
613 int max_n;
614 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400615 /* approximately equals target * 0.00585 */
616 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800617 found = false;
618
619 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Zhao Yakui45476682009-12-31 16:06:04 +0800620 int lvds_reg;
621
Eric Anholtc619eed2010-01-28 16:45:52 -0800622 if (HAS_PCH_SPLIT(dev))
Zhao Yakui45476682009-12-31 16:06:04 +0800623 lvds_reg = PCH_LVDS;
624 else
625 lvds_reg = LVDS;
626 if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
Ma Lingd4906092009-03-18 20:13:27 +0800627 LVDS_CLKB_POWER_UP)
628 clock.p2 = limit->p2.p2_fast;
629 else
630 clock.p2 = limit->p2.p2_slow;
631 } else {
632 if (target < limit->p2.dot_limit)
633 clock.p2 = limit->p2.p2_slow;
634 else
635 clock.p2 = limit->p2.p2_fast;
636 }
637
638 memset(best_clock, 0, sizeof(*best_clock));
639 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200640 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800641 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200642 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800643 for (clock.m1 = limit->m1.max;
644 clock.m1 >= limit->m1.min; clock.m1--) {
645 for (clock.m2 = limit->m2.max;
646 clock.m2 >= limit->m2.min; clock.m2--) {
647 for (clock.p1 = limit->p1.max;
648 clock.p1 >= limit->p1.min; clock.p1--) {
649 int this_err;
650
Shaohua Li21778322009-02-23 15:19:16 +0800651 intel_clock(dev, refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000652 if (!intel_PLL_is_valid(dev, limit,
653 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800654 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800655 if (match_clock &&
656 clock.p != match_clock->p)
657 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000658
659 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800660 if (this_err < err_most) {
661 *best_clock = clock;
662 err_most = this_err;
663 max_n = clock.n;
664 found = true;
665 }
666 }
667 }
668 }
669 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800670 return found;
671}
Ma Lingd4906092009-03-18 20:13:27 +0800672
Zhenyu Wang2c072452009-06-05 15:38:42 +0800673static bool
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500674intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800675 int target, int refclk, intel_clock_t *match_clock,
676 intel_clock_t *best_clock)
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800677{
678 struct drm_device *dev = crtc->dev;
679 intel_clock_t clock;
Zhao Yakui45476682009-12-31 16:06:04 +0800680
Zhenyu Wang5eb08b62009-07-24 01:00:31 +0800681 if (target < 200000) {
682 clock.n = 1;
683 clock.p1 = 2;
684 clock.p2 = 10;
685 clock.m1 = 12;
686 clock.m2 = 9;
687 } else {
688 clock.n = 2;
689 clock.p1 = 1;
690 clock.p2 = 10;
691 clock.m1 = 14;
692 clock.m2 = 8;
693 }
694 intel_clock(dev, refclk, &clock);
695 memcpy(best_clock, &clock, sizeof(intel_clock_t));
696 return true;
697}
698
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700699/* DisplayPort has only two frequencies, 162MHz and 270MHz */
700static bool
701intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800702 int target, int refclk, intel_clock_t *match_clock,
703 intel_clock_t *best_clock)
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700704{
Chris Wilson5eddb702010-09-11 13:48:45 +0100705 intel_clock_t clock;
706 if (target < 200000) {
707 clock.p1 = 2;
708 clock.p2 = 10;
709 clock.n = 2;
710 clock.m1 = 23;
711 clock.m2 = 8;
712 } else {
713 clock.p1 = 1;
714 clock.p2 = 10;
715 clock.n = 1;
716 clock.m1 = 14;
717 clock.m2 = 2;
718 }
719 clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
720 clock.p = (clock.p1 * clock.p2);
721 clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
722 clock.vco = 0;
723 memcpy(best_clock, &clock, sizeof(intel_clock_t));
724 return true;
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700725}
726
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700727/**
728 * intel_wait_for_vblank - wait for vblank on a given pipe
729 * @dev: drm device
730 * @pipe: pipe to wait for
731 *
732 * Wait for vblank to occur on a given pipe. Needed for various bits of
733 * mode setting code.
734 */
735void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800736{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700737 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800738 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700739
Chris Wilson300387c2010-09-05 20:25:43 +0100740 /* Clear existing vblank status. Note this will clear any other
741 * sticky status fields as well.
742 *
743 * This races with i915_driver_irq_handler() with the result
744 * that either function could miss a vblank event. Here it is not
745 * fatal, as we will either wait upon the next vblank interrupt or
746 * timeout. Generally speaking intel_wait_for_vblank() is only
747 * called during modeset at which time the GPU should be idle and
748 * should *not* be performing page flips and thus not waiting on
749 * vblanks...
750 * Currently, the result of us stealing a vblank from the irq
751 * handler is that a single frame will be skipped during swapbuffers.
752 */
753 I915_WRITE(pipestat_reg,
754 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
755
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700756 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100757 if (wait_for(I915_READ(pipestat_reg) &
758 PIPE_VBLANK_INTERRUPT_STATUS,
759 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700760 DRM_DEBUG_KMS("vblank wait timed out\n");
761}
762
Keith Packardab7ad7f2010-10-03 00:33:06 -0700763/*
764 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700765 * @dev: drm device
766 * @pipe: pipe to wait for
767 *
768 * After disabling a pipe, we can't wait for vblank in the usual way,
769 * spinning on the vblank interrupt status bit, since we won't actually
770 * see an interrupt when the pipe is disabled.
771 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700772 * On Gen4 and above:
773 * wait for the pipe register state bit to turn off
774 *
775 * Otherwise:
776 * wait for the display line value to settle (it usually
777 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100778 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700779 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100780void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700781{
782 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700783
Keith Packardab7ad7f2010-10-03 00:33:06 -0700784 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson58e10eb2010-10-03 10:56:11 +0100785 int reg = PIPECONF(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700786
Keith Packardab7ad7f2010-10-03 00:33:06 -0700787 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100788 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
789 100))
Keith Packardab7ad7f2010-10-03 00:33:06 -0700790 DRM_DEBUG_KMS("pipe_off wait timed out\n");
791 } else {
792 u32 last_line;
Chris Wilson58e10eb2010-10-03 10:56:11 +0100793 int reg = PIPEDSL(pipe);
Keith Packardab7ad7f2010-10-03 00:33:06 -0700794 unsigned long timeout = jiffies + msecs_to_jiffies(100);
795
796 /* Wait for the display line to settle */
797 do {
Chris Wilson58e10eb2010-10-03 10:56:11 +0100798 last_line = I915_READ(reg) & DSL_LINEMASK;
Keith Packardab7ad7f2010-10-03 00:33:06 -0700799 mdelay(5);
Chris Wilson58e10eb2010-10-03 10:56:11 +0100800 } while (((I915_READ(reg) & DSL_LINEMASK) != last_line) &&
Keith Packardab7ad7f2010-10-03 00:33:06 -0700801 time_after(timeout, jiffies));
802 if (time_after(jiffies, timeout))
803 DRM_DEBUG_KMS("pipe_off wait timed out\n");
804 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800805}
806
Jesse Barnesb24e7172011-01-04 15:09:30 -0800807static const char *state_string(bool enabled)
808{
809 return enabled ? "on" : "off";
810}
811
812/* Only for pre-ILK configs */
813static void assert_pll(struct drm_i915_private *dev_priv,
814 enum pipe pipe, bool state)
815{
816 int reg;
817 u32 val;
818 bool cur_state;
819
820 reg = DPLL(pipe);
821 val = I915_READ(reg);
822 cur_state = !!(val & DPLL_VCO_ENABLE);
823 WARN(cur_state != state,
824 "PLL state assertion failure (expected %s, current %s)\n",
825 state_string(state), state_string(cur_state));
826}
827#define assert_pll_enabled(d, p) assert_pll(d, p, true)
828#define assert_pll_disabled(d, p) assert_pll(d, p, false)
829
Jesse Barnes040484a2011-01-03 12:14:26 -0800830/* For ILK+ */
831static void assert_pch_pll(struct drm_i915_private *dev_priv,
832 enum pipe pipe, bool state)
833{
834 int reg;
835 u32 val;
836 bool cur_state;
837
Jesse Barnesd3ccbe82011-10-12 09:27:42 -0700838 if (HAS_PCH_CPT(dev_priv->dev)) {
839 u32 pch_dpll;
840
841 pch_dpll = I915_READ(PCH_DPLL_SEL);
842
843 /* Make sure the selected PLL is enabled to the transcoder */
844 WARN(!((pch_dpll >> (4 * pipe)) & 8),
845 "transcoder %d PLL not enabled\n", pipe);
846
847 /* Convert the transcoder pipe number to a pll pipe number */
848 pipe = (pch_dpll >> (4 * pipe)) & 1;
849 }
850
Jesse Barnes040484a2011-01-03 12:14:26 -0800851 reg = PCH_DPLL(pipe);
852 val = I915_READ(reg);
853 cur_state = !!(val & DPLL_VCO_ENABLE);
854 WARN(cur_state != state,
855 "PCH PLL state assertion failure (expected %s, current %s)\n",
856 state_string(state), state_string(cur_state));
857}
858#define assert_pch_pll_enabled(d, p) assert_pch_pll(d, p, true)
859#define assert_pch_pll_disabled(d, p) assert_pch_pll(d, p, false)
860
861static void assert_fdi_tx(struct drm_i915_private *dev_priv,
862 enum pipe pipe, bool state)
863{
864 int reg;
865 u32 val;
866 bool cur_state;
867
868 reg = FDI_TX_CTL(pipe);
869 val = I915_READ(reg);
870 cur_state = !!(val & FDI_TX_ENABLE);
871 WARN(cur_state != state,
872 "FDI TX state assertion failure (expected %s, current %s)\n",
873 state_string(state), state_string(cur_state));
874}
875#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
876#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
877
878static void assert_fdi_rx(struct drm_i915_private *dev_priv,
879 enum pipe pipe, bool state)
880{
881 int reg;
882 u32 val;
883 bool cur_state;
884
885 reg = FDI_RX_CTL(pipe);
886 val = I915_READ(reg);
887 cur_state = !!(val & FDI_RX_ENABLE);
888 WARN(cur_state != state,
889 "FDI RX state assertion failure (expected %s, current %s)\n",
890 state_string(state), state_string(cur_state));
891}
892#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
893#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
894
895static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
896 enum pipe pipe)
897{
898 int reg;
899 u32 val;
900
901 /* ILK FDI PLL is always enabled */
902 if (dev_priv->info->gen == 5)
903 return;
904
905 reg = FDI_TX_CTL(pipe);
906 val = I915_READ(reg);
907 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
908}
909
910static void assert_fdi_rx_pll_enabled(struct drm_i915_private *dev_priv,
911 enum pipe pipe)
912{
913 int reg;
914 u32 val;
915
916 reg = FDI_RX_CTL(pipe);
917 val = I915_READ(reg);
918 WARN(!(val & FDI_RX_PLL_ENABLE), "FDI RX PLL assertion failure, should be active but is disabled\n");
919}
920
Jesse Barnesea0760c2011-01-04 15:09:32 -0800921static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
922 enum pipe pipe)
923{
924 int pp_reg, lvds_reg;
925 u32 val;
926 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +0200927 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -0800928
929 if (HAS_PCH_SPLIT(dev_priv->dev)) {
930 pp_reg = PCH_PP_CONTROL;
931 lvds_reg = PCH_LVDS;
932 } else {
933 pp_reg = PP_CONTROL;
934 lvds_reg = LVDS;
935 }
936
937 val = I915_READ(pp_reg);
938 if (!(val & PANEL_POWER_ON) ||
939 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
940 locked = false;
941
942 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
943 panel_pipe = PIPE_B;
944
945 WARN(panel_pipe == pipe && locked,
946 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800947 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -0800948}
949
Jesse Barnesb840d907f2011-12-13 13:19:38 -0800950void assert_pipe(struct drm_i915_private *dev_priv,
951 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800952{
953 int reg;
954 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -0800955 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -0800956
Daniel Vetter8e636782012-01-22 01:36:48 +0100957 /* if we need the pipe A quirk it must be always on */
958 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
959 state = true;
960
Jesse Barnesb24e7172011-01-04 15:09:30 -0800961 reg = PIPECONF(pipe);
962 val = I915_READ(reg);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -0800963 cur_state = !!(val & PIPECONF_ENABLE);
964 WARN(cur_state != state,
965 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800966 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -0800967}
968
Chris Wilson931872f2012-01-16 23:01:13 +0000969static void assert_plane(struct drm_i915_private *dev_priv,
970 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -0800971{
972 int reg;
973 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +0000974 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -0800975
976 reg = DSPCNTR(plane);
977 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +0000978 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
979 WARN(cur_state != state,
980 "plane %c assertion failure (expected %s, current %s)\n",
981 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -0800982}
983
Chris Wilson931872f2012-01-16 23:01:13 +0000984#define assert_plane_enabled(d, p) assert_plane(d, p, true)
985#define assert_plane_disabled(d, p) assert_plane(d, p, false)
986
Jesse Barnesb24e7172011-01-04 15:09:30 -0800987static void assert_planes_disabled(struct drm_i915_private *dev_priv,
988 enum pipe pipe)
989{
990 int reg, i;
991 u32 val;
992 int cur_pipe;
993
Jesse Barnes19ec1352011-02-02 12:28:02 -0800994 /* Planes are fixed to pipes on ILK+ */
Adam Jackson28c057942011-10-07 14:38:42 -0400995 if (HAS_PCH_SPLIT(dev_priv->dev)) {
996 reg = DSPCNTR(pipe);
997 val = I915_READ(reg);
998 WARN((val & DISPLAY_PLANE_ENABLE),
999 "plane %c assertion failure, should be disabled but not\n",
1000 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001001 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001002 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001003
Jesse Barnesb24e7172011-01-04 15:09:30 -08001004 /* Need to check both planes against the pipe */
1005 for (i = 0; i < 2; i++) {
1006 reg = DSPCNTR(i);
1007 val = I915_READ(reg);
1008 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1009 DISPPLANE_SEL_PIPE_SHIFT;
1010 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001011 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1012 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001013 }
1014}
1015
Jesse Barnes92f25842011-01-04 15:09:34 -08001016static void assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
1017{
1018 u32 val;
1019 bool enabled;
1020
1021 val = I915_READ(PCH_DREF_CONTROL);
1022 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1023 DREF_SUPERSPREAD_SOURCE_MASK));
1024 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1025}
1026
1027static void assert_transcoder_disabled(struct drm_i915_private *dev_priv,
1028 enum pipe pipe)
1029{
1030 int reg;
1031 u32 val;
1032 bool enabled;
1033
1034 reg = TRANSCONF(pipe);
1035 val = I915_READ(reg);
1036 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001037 WARN(enabled,
1038 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1039 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001040}
1041
Keith Packard4e634382011-08-06 10:39:45 -07001042static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1043 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001044{
1045 if ((val & DP_PORT_EN) == 0)
1046 return false;
1047
1048 if (HAS_PCH_CPT(dev_priv->dev)) {
1049 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1050 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1051 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1052 return false;
1053 } else {
1054 if ((val & DP_PIPE_MASK) != (pipe << 30))
1055 return false;
1056 }
1057 return true;
1058}
1059
Keith Packard1519b992011-08-06 10:35:34 -07001060static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1061 enum pipe pipe, u32 val)
1062{
1063 if ((val & PORT_ENABLE) == 0)
1064 return false;
1065
1066 if (HAS_PCH_CPT(dev_priv->dev)) {
1067 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1068 return false;
1069 } else {
1070 if ((val & TRANSCODER_MASK) != TRANSCODER(pipe))
1071 return false;
1072 }
1073 return true;
1074}
1075
1076static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1077 enum pipe pipe, u32 val)
1078{
1079 if ((val & LVDS_PORT_EN) == 0)
1080 return false;
1081
1082 if (HAS_PCH_CPT(dev_priv->dev)) {
1083 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1084 return false;
1085 } else {
1086 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1087 return false;
1088 }
1089 return true;
1090}
1091
1092static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1093 enum pipe pipe, u32 val)
1094{
1095 if ((val & ADPA_DAC_ENABLE) == 0)
1096 return false;
1097 if (HAS_PCH_CPT(dev_priv->dev)) {
1098 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1099 return false;
1100 } else {
1101 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1102 return false;
1103 }
1104 return true;
1105}
1106
Jesse Barnes291906f2011-02-02 12:28:03 -08001107static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001108 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001109{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001110 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001111 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001112 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001113 reg, pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001114}
1115
1116static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1117 enum pipe pipe, int reg)
1118{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001119 u32 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001120 WARN(hdmi_pipe_enabled(dev_priv, val, pipe),
Adam Jackson23c99e72011-10-07 14:38:43 -04001121 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001122 reg, pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001123}
1124
1125static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1126 enum pipe pipe)
1127{
1128 int reg;
1129 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001130
Keith Packardf0575e92011-07-25 22:12:43 -07001131 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1132 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1133 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001134
1135 reg = PCH_ADPA;
1136 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001137 WARN(adpa_pipe_enabled(dev_priv, val, pipe),
Jesse Barnes291906f2011-02-02 12:28:03 -08001138 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001139 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001140
1141 reg = PCH_LVDS;
1142 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001143 WARN(lvds_pipe_enabled(dev_priv, val, pipe),
Jesse Barnes291906f2011-02-02 12:28:03 -08001144 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001145 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001146
1147 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIB);
1148 assert_pch_hdmi_disabled(dev_priv, pipe, HDMIC);
1149 assert_pch_hdmi_disabled(dev_priv, pipe, HDMID);
1150}
1151
Jesse Barnesb24e7172011-01-04 15:09:30 -08001152/**
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001153 * intel_enable_pll - enable a PLL
1154 * @dev_priv: i915 private structure
1155 * @pipe: pipe PLL to enable
1156 *
1157 * Enable @pipe's PLL so we can start pumping pixels from a plane. Check to
1158 * make sure the PLL reg is writable first though, since the panel write
1159 * protect mechanism may be enabled.
1160 *
1161 * Note! This is for pre-ILK only.
1162 */
1163static void intel_enable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1164{
1165 int reg;
1166 u32 val;
1167
1168 /* No really, not for ILK+ */
1169 BUG_ON(dev_priv->info->gen >= 5);
1170
1171 /* PLL is protected by panel, make sure we can write it */
1172 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
1173 assert_panel_unlocked(dev_priv, pipe);
1174
1175 reg = DPLL(pipe);
1176 val = I915_READ(reg);
1177 val |= DPLL_VCO_ENABLE;
1178
1179 /* We do this three times for luck */
1180 I915_WRITE(reg, val);
1181 POSTING_READ(reg);
1182 udelay(150); /* wait for warmup */
1183 I915_WRITE(reg, val);
1184 POSTING_READ(reg);
1185 udelay(150); /* wait for warmup */
1186 I915_WRITE(reg, val);
1187 POSTING_READ(reg);
1188 udelay(150); /* wait for warmup */
1189}
1190
1191/**
1192 * intel_disable_pll - disable a PLL
1193 * @dev_priv: i915 private structure
1194 * @pipe: pipe PLL to disable
1195 *
1196 * Disable the PLL for @pipe, making sure the pipe is off first.
1197 *
1198 * Note! This is for pre-ILK only.
1199 */
1200static void intel_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1201{
1202 int reg;
1203 u32 val;
1204
1205 /* Don't disable pipe A or pipe A PLLs if needed */
1206 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1207 return;
1208
1209 /* Make sure the pipe isn't still relying on us */
1210 assert_pipe_disabled(dev_priv, pipe);
1211
1212 reg = DPLL(pipe);
1213 val = I915_READ(reg);
1214 val &= ~DPLL_VCO_ENABLE;
1215 I915_WRITE(reg, val);
1216 POSTING_READ(reg);
1217}
1218
1219/**
Jesse Barnes92f25842011-01-04 15:09:34 -08001220 * intel_enable_pch_pll - enable PCH PLL
1221 * @dev_priv: i915 private structure
1222 * @pipe: pipe PLL to enable
1223 *
1224 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1225 * drives the transcoder clock.
1226 */
1227static void intel_enable_pch_pll(struct drm_i915_private *dev_priv,
1228 enum pipe pipe)
1229{
1230 int reg;
1231 u32 val;
1232
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001233 if (pipe > 1)
1234 return;
1235
Jesse Barnes92f25842011-01-04 15:09:34 -08001236 /* PCH only available on ILK+ */
1237 BUG_ON(dev_priv->info->gen < 5);
1238
1239 /* PCH refclock must be enabled first */
1240 assert_pch_refclk_enabled(dev_priv);
1241
1242 reg = PCH_DPLL(pipe);
1243 val = I915_READ(reg);
1244 val |= DPLL_VCO_ENABLE;
1245 I915_WRITE(reg, val);
1246 POSTING_READ(reg);
1247 udelay(200);
1248}
1249
1250static void intel_disable_pch_pll(struct drm_i915_private *dev_priv,
1251 enum pipe pipe)
1252{
1253 int reg;
Jesse Barnes7a419862011-11-15 10:28:53 -08001254 u32 val, pll_mask = TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL,
1255 pll_sel = TRANSC_DPLL_ENABLE;
Jesse Barnes92f25842011-01-04 15:09:34 -08001256
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001257 if (pipe > 1)
1258 return;
1259
Jesse Barnes92f25842011-01-04 15:09:34 -08001260 /* PCH only available on ILK+ */
1261 BUG_ON(dev_priv->info->gen < 5);
1262
1263 /* Make sure transcoder isn't still depending on us */
1264 assert_transcoder_disabled(dev_priv, pipe);
1265
Jesse Barnes7a419862011-11-15 10:28:53 -08001266 if (pipe == 0)
1267 pll_sel |= TRANSC_DPLLA_SEL;
1268 else if (pipe == 1)
1269 pll_sel |= TRANSC_DPLLB_SEL;
1270
1271
1272 if ((I915_READ(PCH_DPLL_SEL) & pll_mask) == pll_sel)
1273 return;
1274
Jesse Barnes92f25842011-01-04 15:09:34 -08001275 reg = PCH_DPLL(pipe);
1276 val = I915_READ(reg);
1277 val &= ~DPLL_VCO_ENABLE;
1278 I915_WRITE(reg, val);
1279 POSTING_READ(reg);
1280 udelay(200);
1281}
1282
Jesse Barnes040484a2011-01-03 12:14:26 -08001283static void intel_enable_transcoder(struct drm_i915_private *dev_priv,
1284 enum pipe pipe)
1285{
1286 int reg;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001287 u32 val, pipeconf_val;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001288 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Jesse Barnes040484a2011-01-03 12:14:26 -08001289
1290 /* PCH only available on ILK+ */
1291 BUG_ON(dev_priv->info->gen < 5);
1292
1293 /* Make sure PCH DPLL is enabled */
1294 assert_pch_pll_enabled(dev_priv, pipe);
1295
1296 /* FDI must be feeding us bits for PCH ports */
1297 assert_fdi_tx_enabled(dev_priv, pipe);
1298 assert_fdi_rx_enabled(dev_priv, pipe);
1299
1300 reg = TRANSCONF(pipe);
1301 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001302 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001303
1304 if (HAS_PCH_IBX(dev_priv->dev)) {
1305 /*
1306 * make the BPC in transcoder be consistent with
1307 * that in pipeconf reg.
1308 */
1309 val &= ~PIPE_BPC_MASK;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001310 val |= pipeconf_val & PIPE_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001311 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001312
1313 val &= ~TRANS_INTERLACE_MASK;
1314 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001315 if (HAS_PCH_IBX(dev_priv->dev) &&
1316 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1317 val |= TRANS_LEGACY_INTERLACED_ILK;
1318 else
1319 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001320 else
1321 val |= TRANS_PROGRESSIVE;
1322
Jesse Barnes040484a2011-01-03 12:14:26 -08001323 I915_WRITE(reg, val | TRANS_ENABLE);
1324 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
1325 DRM_ERROR("failed to enable transcoder %d\n", pipe);
1326}
1327
1328static void intel_disable_transcoder(struct drm_i915_private *dev_priv,
1329 enum pipe pipe)
1330{
1331 int reg;
1332 u32 val;
1333
1334 /* FDI relies on the transcoder */
1335 assert_fdi_tx_disabled(dev_priv, pipe);
1336 assert_fdi_rx_disabled(dev_priv, pipe);
1337
Jesse Barnes291906f2011-02-02 12:28:03 -08001338 /* Ports must be off as well */
1339 assert_pch_ports_disabled(dev_priv, pipe);
1340
Jesse Barnes040484a2011-01-03 12:14:26 -08001341 reg = TRANSCONF(pipe);
1342 val = I915_READ(reg);
1343 val &= ~TRANS_ENABLE;
1344 I915_WRITE(reg, val);
1345 /* wait for PCH transcoder off, transcoder state */
1346 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Jesse Barnes4c9c18c2011-10-13 09:46:32 -07001347 DRM_ERROR("failed to disable transcoder %d\n", pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001348}
1349
Jesse Barnes92f25842011-01-04 15:09:34 -08001350/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001351 * intel_enable_pipe - enable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001352 * @dev_priv: i915 private structure
1353 * @pipe: pipe to enable
Jesse Barnes040484a2011-01-03 12:14:26 -08001354 * @pch_port: on ILK+, is this pipe driving a PCH port or not
Jesse Barnesb24e7172011-01-04 15:09:30 -08001355 *
1356 * Enable @pipe, making sure that various hardware specific requirements
1357 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
1358 *
1359 * @pipe should be %PIPE_A or %PIPE_B.
1360 *
1361 * Will wait until the pipe is actually running (i.e. first vblank) before
1362 * returning.
1363 */
Jesse Barnes040484a2011-01-03 12:14:26 -08001364static void intel_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe,
1365 bool pch_port)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001366{
1367 int reg;
1368 u32 val;
1369
1370 /*
1371 * A pipe without a PLL won't actually be able to drive bits from
1372 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
1373 * need the check.
1374 */
1375 if (!HAS_PCH_SPLIT(dev_priv->dev))
1376 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001377 else {
1378 if (pch_port) {
1379 /* if driving the PCH, we need FDI enabled */
1380 assert_fdi_rx_pll_enabled(dev_priv, pipe);
1381 assert_fdi_tx_pll_enabled(dev_priv, pipe);
1382 }
1383 /* FIXME: assert CPU port conditions for SNB+ */
1384 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08001385
1386 reg = PIPECONF(pipe);
1387 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001388 if (val & PIPECONF_ENABLE)
1389 return;
1390
1391 I915_WRITE(reg, val | PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001392 intel_wait_for_vblank(dev_priv->dev, pipe);
1393}
1394
1395/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001396 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001397 * @dev_priv: i915 private structure
1398 * @pipe: pipe to disable
1399 *
1400 * Disable @pipe, making sure that various hardware specific requirements
1401 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
1402 *
1403 * @pipe should be %PIPE_A or %PIPE_B.
1404 *
1405 * Will wait until the pipe has shut down before returning.
1406 */
1407static void intel_disable_pipe(struct drm_i915_private *dev_priv,
1408 enum pipe pipe)
1409{
1410 int reg;
1411 u32 val;
1412
1413 /*
1414 * Make sure planes won't keep trying to pump pixels to us,
1415 * or we might hang the display.
1416 */
1417 assert_planes_disabled(dev_priv, pipe);
1418
1419 /* Don't disable pipe A or pipe A PLLs if needed */
1420 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1421 return;
1422
1423 reg = PIPECONF(pipe);
1424 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001425 if ((val & PIPECONF_ENABLE) == 0)
1426 return;
1427
1428 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001429 intel_wait_for_pipe_off(dev_priv->dev, pipe);
1430}
1431
Keith Packardd74362c2011-07-28 14:47:14 -07001432/*
1433 * Plane regs are double buffered, going from enabled->disabled needs a
1434 * trigger in order to latch. The display address reg provides this.
1435 */
1436static void intel_flush_display_plane(struct drm_i915_private *dev_priv,
1437 enum plane plane)
1438{
1439 I915_WRITE(DSPADDR(plane), I915_READ(DSPADDR(plane)));
1440 I915_WRITE(DSPSURF(plane), I915_READ(DSPSURF(plane)));
1441}
1442
Jesse Barnesb24e7172011-01-04 15:09:30 -08001443/**
1444 * intel_enable_plane - enable a display plane on a given pipe
1445 * @dev_priv: i915 private structure
1446 * @plane: plane to enable
1447 * @pipe: pipe being fed
1448 *
1449 * Enable @plane on @pipe, making sure that @pipe is running first.
1450 */
1451static void intel_enable_plane(struct drm_i915_private *dev_priv,
1452 enum plane plane, enum pipe pipe)
1453{
1454 int reg;
1455 u32 val;
1456
1457 /* If the pipe isn't enabled, we can't pump pixels and may hang */
1458 assert_pipe_enabled(dev_priv, pipe);
1459
1460 reg = DSPCNTR(plane);
1461 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001462 if (val & DISPLAY_PLANE_ENABLE)
1463 return;
1464
1465 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07001466 intel_flush_display_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001467 intel_wait_for_vblank(dev_priv->dev, pipe);
1468}
1469
Jesse Barnesb24e7172011-01-04 15:09:30 -08001470/**
1471 * intel_disable_plane - disable a display plane
1472 * @dev_priv: i915 private structure
1473 * @plane: plane to disable
1474 * @pipe: pipe consuming the data
1475 *
1476 * Disable @plane; should be an independent operation.
1477 */
1478static void intel_disable_plane(struct drm_i915_private *dev_priv,
1479 enum plane plane, enum pipe pipe)
1480{
1481 int reg;
1482 u32 val;
1483
1484 reg = DSPCNTR(plane);
1485 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00001486 if ((val & DISPLAY_PLANE_ENABLE) == 0)
1487 return;
1488
1489 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001490 intel_flush_display_plane(dev_priv, plane);
1491 intel_wait_for_vblank(dev_priv->dev, pipe);
1492}
1493
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001494static void disable_pch_dp(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001495 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001496{
1497 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001498 if (dp_pipe_enabled(dev_priv, pipe, port_sel, val)) {
Keith Packardf0575e92011-07-25 22:12:43 -07001499 DRM_DEBUG_KMS("Disabling pch dp %x on pipe %d\n", reg, pipe);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001500 I915_WRITE(reg, val & ~DP_PORT_EN);
Keith Packardf0575e92011-07-25 22:12:43 -07001501 }
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001502}
1503
1504static void disable_pch_hdmi(struct drm_i915_private *dev_priv,
1505 enum pipe pipe, int reg)
1506{
1507 u32 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001508 if (hdmi_pipe_enabled(dev_priv, val, pipe)) {
Keith Packardf0575e92011-07-25 22:12:43 -07001509 DRM_DEBUG_KMS("Disabling pch HDMI %x on pipe %d\n",
1510 reg, pipe);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001511 I915_WRITE(reg, val & ~PORT_ENABLE);
Keith Packardf0575e92011-07-25 22:12:43 -07001512 }
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001513}
1514
1515/* Disable any ports connected to this transcoder */
1516static void intel_disable_pch_ports(struct drm_i915_private *dev_priv,
1517 enum pipe pipe)
1518{
1519 u32 reg, val;
1520
1521 val = I915_READ(PCH_PP_CONTROL);
1522 I915_WRITE(PCH_PP_CONTROL, val | PANEL_UNLOCK_REGS);
1523
Keith Packardf0575e92011-07-25 22:12:43 -07001524 disable_pch_dp(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1525 disable_pch_dp(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1526 disable_pch_dp(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001527
1528 reg = PCH_ADPA;
1529 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001530 if (adpa_pipe_enabled(dev_priv, val, pipe))
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001531 I915_WRITE(reg, val & ~ADPA_DAC_ENABLE);
1532
1533 reg = PCH_LVDS;
1534 val = I915_READ(reg);
Keith Packard1519b992011-08-06 10:35:34 -07001535 if (lvds_pipe_enabled(dev_priv, val, pipe)) {
1536 DRM_DEBUG_KMS("disable lvds on pipe %d val 0x%08x\n", pipe, val);
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001537 I915_WRITE(reg, val & ~LVDS_PORT_EN);
1538 POSTING_READ(reg);
1539 udelay(100);
1540 }
1541
1542 disable_pch_hdmi(dev_priv, pipe, HDMIB);
1543 disable_pch_hdmi(dev_priv, pipe, HDMIC);
1544 disable_pch_hdmi(dev_priv, pipe, HDMID);
1545}
1546
Chris Wilson43a95392011-07-08 12:22:36 +01001547static void i8xx_disable_fbc(struct drm_device *dev)
1548{
1549 struct drm_i915_private *dev_priv = dev->dev_private;
1550 u32 fbc_ctl;
1551
1552 /* Disable compression */
1553 fbc_ctl = I915_READ(FBC_CONTROL);
1554 if ((fbc_ctl & FBC_CTL_EN) == 0)
1555 return;
1556
1557 fbc_ctl &= ~FBC_CTL_EN;
1558 I915_WRITE(FBC_CONTROL, fbc_ctl);
1559
1560 /* Wait for compressing bit to clear */
1561 if (wait_for((I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) == 0, 10)) {
1562 DRM_DEBUG_KMS("FBC idle timed out\n");
1563 return;
1564 }
1565
1566 DRM_DEBUG_KMS("disabled FBC\n");
1567}
1568
Jesse Barnes80824002009-09-10 15:28:06 -07001569static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1570{
1571 struct drm_device *dev = crtc->dev;
1572 struct drm_i915_private *dev_priv = dev->dev_private;
1573 struct drm_framebuffer *fb = crtc->fb;
1574 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001575 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes80824002009-09-10 15:28:06 -07001576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson016b9b62011-07-08 12:22:43 +01001577 int cfb_pitch;
Jesse Barnes80824002009-09-10 15:28:06 -07001578 int plane, i;
1579 u32 fbc_ctl, fbc_ctl2;
1580
Chris Wilson016b9b62011-07-08 12:22:43 +01001581 cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001582 if (fb->pitches[0] < cfb_pitch)
1583 cfb_pitch = fb->pitches[0];
Jesse Barnes80824002009-09-10 15:28:06 -07001584
1585 /* FBC_CTL wants 64B units */
Chris Wilson016b9b62011-07-08 12:22:43 +01001586 cfb_pitch = (cfb_pitch / 64) - 1;
1587 plane = intel_crtc->plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
Jesse Barnes80824002009-09-10 15:28:06 -07001588
1589 /* Clear old tags */
1590 for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
1591 I915_WRITE(FBC_TAG + (i * 4), 0);
1592
1593 /* Set it up... */
Chris Wilsonde568512011-07-08 12:22:39 +01001594 fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | FBC_CTL_CPU_FENCE;
1595 fbc_ctl2 |= plane;
Jesse Barnes80824002009-09-10 15:28:06 -07001596 I915_WRITE(FBC_CONTROL2, fbc_ctl2);
1597 I915_WRITE(FBC_FENCE_OFF, crtc->y);
1598
1599 /* enable it... */
1600 fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
Jesse Barnesee25df22010-02-06 10:41:53 -08001601 if (IS_I945GM(dev))
Priit Laes49677902010-03-02 11:37:00 +02001602 fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
Chris Wilson016b9b62011-07-08 12:22:43 +01001603 fbc_ctl |= (cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
Jesse Barnes80824002009-09-10 15:28:06 -07001604 fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
Chris Wilson016b9b62011-07-08 12:22:43 +01001605 fbc_ctl |= obj->fence_reg;
Jesse Barnes80824002009-09-10 15:28:06 -07001606 I915_WRITE(FBC_CONTROL, fbc_ctl);
1607
Chris Wilson016b9b62011-07-08 12:22:43 +01001608 DRM_DEBUG_KMS("enabled FBC, pitch %d, yoff %d, plane %d, ",
1609 cfb_pitch, crtc->y, intel_crtc->plane);
Jesse Barnes80824002009-09-10 15:28:06 -07001610}
1611
Adam Jacksonee5382a2010-04-23 11:17:39 -04001612static bool i8xx_fbc_enabled(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001613{
Jesse Barnes80824002009-09-10 15:28:06 -07001614 struct drm_i915_private *dev_priv = dev->dev_private;
1615
1616 return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
1617}
1618
Jesse Barnes74dff282009-09-14 15:39:40 -07001619static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1620{
1621 struct drm_device *dev = crtc->dev;
1622 struct drm_i915_private *dev_priv = dev->dev_private;
1623 struct drm_framebuffer *fb = crtc->fb;
1624 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001625 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes74dff282009-09-14 15:39:40 -07001626 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001627 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Jesse Barnes74dff282009-09-14 15:39:40 -07001628 unsigned long stall_watermark = 200;
1629 u32 dpfc_ctl;
1630
Jesse Barnes74dff282009-09-14 15:39:40 -07001631 dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
Chris Wilson016b9b62011-07-08 12:22:43 +01001632 dpfc_ctl |= DPFC_CTL_FENCE_EN | obj->fence_reg;
Chris Wilsonde568512011-07-08 12:22:39 +01001633 I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
Jesse Barnes74dff282009-09-14 15:39:40 -07001634
Jesse Barnes74dff282009-09-14 15:39:40 -07001635 I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1636 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1637 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1638 I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
1639
1640 /* enable it... */
1641 I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
1642
Zhao Yakui28c97732009-10-09 11:39:41 +08001643 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
Jesse Barnes74dff282009-09-14 15:39:40 -07001644}
1645
Chris Wilson43a95392011-07-08 12:22:36 +01001646static void g4x_disable_fbc(struct drm_device *dev)
Jesse Barnes74dff282009-09-14 15:39:40 -07001647{
1648 struct drm_i915_private *dev_priv = dev->dev_private;
1649 u32 dpfc_ctl;
1650
1651 /* Disable compression */
1652 dpfc_ctl = I915_READ(DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001653 if (dpfc_ctl & DPFC_CTL_EN) {
1654 dpfc_ctl &= ~DPFC_CTL_EN;
1655 I915_WRITE(DPFC_CONTROL, dpfc_ctl);
Jesse Barnes74dff282009-09-14 15:39:40 -07001656
Chris Wilsonbed4a672010-09-11 10:47:47 +01001657 DRM_DEBUG_KMS("disabled FBC\n");
1658 }
Jesse Barnes74dff282009-09-14 15:39:40 -07001659}
1660
Adam Jacksonee5382a2010-04-23 11:17:39 -04001661static bool g4x_fbc_enabled(struct drm_device *dev)
Jesse Barnes74dff282009-09-14 15:39:40 -07001662{
Jesse Barnes74dff282009-09-14 15:39:40 -07001663 struct drm_i915_private *dev_priv = dev->dev_private;
1664
1665 return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
1666}
1667
Jesse Barnes4efe0702011-01-18 11:25:41 -08001668static void sandybridge_blit_fbc_update(struct drm_device *dev)
1669{
1670 struct drm_i915_private *dev_priv = dev->dev_private;
1671 u32 blt_ecoskpd;
1672
1673 /* Make sure blitter notifies FBC of writes */
Ben Widawskyfcca7922011-04-25 11:23:07 -07001674 gen6_gt_force_wake_get(dev_priv);
Jesse Barnes4efe0702011-01-18 11:25:41 -08001675 blt_ecoskpd = I915_READ(GEN6_BLITTER_ECOSKPD);
1676 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY <<
1677 GEN6_BLITTER_LOCK_SHIFT;
1678 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1679 blt_ecoskpd |= GEN6_BLITTER_FBC_NOTIFY;
1680 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1681 blt_ecoskpd &= ~(GEN6_BLITTER_FBC_NOTIFY <<
1682 GEN6_BLITTER_LOCK_SHIFT);
1683 I915_WRITE(GEN6_BLITTER_ECOSKPD, blt_ecoskpd);
1684 POSTING_READ(GEN6_BLITTER_ECOSKPD);
Ben Widawskyfcca7922011-04-25 11:23:07 -07001685 gen6_gt_force_wake_put(dev_priv);
Jesse Barnes4efe0702011-01-18 11:25:41 -08001686}
1687
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001688static void ironlake_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
1689{
1690 struct drm_device *dev = crtc->dev;
1691 struct drm_i915_private *dev_priv = dev->dev_private;
1692 struct drm_framebuffer *fb = crtc->fb;
1693 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001694 struct drm_i915_gem_object *obj = intel_fb->obj;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001695 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5eddb702010-09-11 13:48:45 +01001696 int plane = intel_crtc->plane == 0 ? DPFC_CTL_PLANEA : DPFC_CTL_PLANEB;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001697 unsigned long stall_watermark = 200;
1698 u32 dpfc_ctl;
1699
Chris Wilsonbed4a672010-09-11 10:47:47 +01001700 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001701 dpfc_ctl &= DPFC_RESERVED;
1702 dpfc_ctl |= (plane | DPFC_CTL_LIMIT_1X);
Chris Wilson9ce9d062011-07-08 12:22:40 +01001703 /* Set persistent mode for front-buffer rendering, ala X. */
1704 dpfc_ctl |= DPFC_CTL_PERSISTENT_MODE;
Chris Wilson016b9b62011-07-08 12:22:43 +01001705 dpfc_ctl |= (DPFC_CTL_FENCE_EN | obj->fence_reg);
Chris Wilsonde568512011-07-08 12:22:39 +01001706 I915_WRITE(ILK_DPFC_CHICKEN, DPFC_HT_MODIFY);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001707
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001708 I915_WRITE(ILK_DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
1709 (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
1710 (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
1711 I915_WRITE(ILK_DPFC_FENCE_YOFF, crtc->y);
Chris Wilson05394f32010-11-08 19:18:58 +00001712 I915_WRITE(ILK_FBC_RT_BASE, obj->gtt_offset | ILK_FBC_RT_VALID);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001713 /* enable it... */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001714 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl | DPFC_CTL_EN);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001715
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001716 if (IS_GEN6(dev)) {
1717 I915_WRITE(SNB_DPFC_CTL_SA,
Chris Wilson016b9b62011-07-08 12:22:43 +01001718 SNB_CPU_FENCE_ENABLE | obj->fence_reg);
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001719 I915_WRITE(DPFC_CPU_FENCE_OFFSET, crtc->y);
Jesse Barnes4efe0702011-01-18 11:25:41 -08001720 sandybridge_blit_fbc_update(dev);
Yuanhan Liu9c04f012010-12-15 15:42:32 +08001721 }
1722
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001723 DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
1724}
1725
Chris Wilson43a95392011-07-08 12:22:36 +01001726static void ironlake_disable_fbc(struct drm_device *dev)
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001727{
1728 struct drm_i915_private *dev_priv = dev->dev_private;
1729 u32 dpfc_ctl;
1730
1731 /* Disable compression */
1732 dpfc_ctl = I915_READ(ILK_DPFC_CONTROL);
Chris Wilsonbed4a672010-09-11 10:47:47 +01001733 if (dpfc_ctl & DPFC_CTL_EN) {
1734 dpfc_ctl &= ~DPFC_CTL_EN;
1735 I915_WRITE(ILK_DPFC_CONTROL, dpfc_ctl);
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001736
Chris Wilsonbed4a672010-09-11 10:47:47 +01001737 DRM_DEBUG_KMS("disabled FBC\n");
1738 }
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08001739}
1740
1741static bool ironlake_fbc_enabled(struct drm_device *dev)
1742{
1743 struct drm_i915_private *dev_priv = dev->dev_private;
1744
1745 return I915_READ(ILK_DPFC_CONTROL) & DPFC_CTL_EN;
1746}
1747
Adam Jacksonee5382a2010-04-23 11:17:39 -04001748bool intel_fbc_enabled(struct drm_device *dev)
1749{
1750 struct drm_i915_private *dev_priv = dev->dev_private;
1751
1752 if (!dev_priv->display.fbc_enabled)
1753 return false;
1754
1755 return dev_priv->display.fbc_enabled(dev);
1756}
1757
Chris Wilson1630fe72011-07-08 12:22:42 +01001758static void intel_fbc_work_fn(struct work_struct *__work)
1759{
1760 struct intel_fbc_work *work =
1761 container_of(to_delayed_work(__work),
1762 struct intel_fbc_work, work);
1763 struct drm_device *dev = work->crtc->dev;
1764 struct drm_i915_private *dev_priv = dev->dev_private;
1765
1766 mutex_lock(&dev->struct_mutex);
1767 if (work == dev_priv->fbc_work) {
1768 /* Double check that we haven't switched fb without cancelling
1769 * the prior work.
1770 */
Chris Wilson016b9b62011-07-08 12:22:43 +01001771 if (work->crtc->fb == work->fb) {
Chris Wilson1630fe72011-07-08 12:22:42 +01001772 dev_priv->display.enable_fbc(work->crtc,
1773 work->interval);
1774
Chris Wilson016b9b62011-07-08 12:22:43 +01001775 dev_priv->cfb_plane = to_intel_crtc(work->crtc)->plane;
1776 dev_priv->cfb_fb = work->crtc->fb->base.id;
1777 dev_priv->cfb_y = work->crtc->y;
1778 }
1779
Chris Wilson1630fe72011-07-08 12:22:42 +01001780 dev_priv->fbc_work = NULL;
1781 }
1782 mutex_unlock(&dev->struct_mutex);
1783
1784 kfree(work);
1785}
1786
1787static void intel_cancel_fbc_work(struct drm_i915_private *dev_priv)
1788{
1789 if (dev_priv->fbc_work == NULL)
1790 return;
1791
1792 DRM_DEBUG_KMS("cancelling pending FBC enable\n");
1793
1794 /* Synchronisation is provided by struct_mutex and checking of
1795 * dev_priv->fbc_work, so we can perform the cancellation
1796 * entirely asynchronously.
1797 */
1798 if (cancel_delayed_work(&dev_priv->fbc_work->work))
1799 /* tasklet was killed before being run, clean up */
1800 kfree(dev_priv->fbc_work);
1801
1802 /* Mark the work as no longer wanted so that if it does
1803 * wake-up (because the work was already running and waiting
1804 * for our mutex), it will discover that is no longer
1805 * necessary to run.
1806 */
1807 dev_priv->fbc_work = NULL;
1808}
1809
Chris Wilson43a95392011-07-08 12:22:36 +01001810static void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
Adam Jacksonee5382a2010-04-23 11:17:39 -04001811{
Chris Wilson1630fe72011-07-08 12:22:42 +01001812 struct intel_fbc_work *work;
1813 struct drm_device *dev = crtc->dev;
1814 struct drm_i915_private *dev_priv = dev->dev_private;
Adam Jacksonee5382a2010-04-23 11:17:39 -04001815
1816 if (!dev_priv->display.enable_fbc)
1817 return;
1818
Chris Wilson1630fe72011-07-08 12:22:42 +01001819 intel_cancel_fbc_work(dev_priv);
1820
1821 work = kzalloc(sizeof *work, GFP_KERNEL);
1822 if (work == NULL) {
1823 dev_priv->display.enable_fbc(crtc, interval);
1824 return;
1825 }
1826
1827 work->crtc = crtc;
1828 work->fb = crtc->fb;
1829 work->interval = interval;
1830 INIT_DELAYED_WORK(&work->work, intel_fbc_work_fn);
1831
1832 dev_priv->fbc_work = work;
1833
1834 DRM_DEBUG_KMS("scheduling delayed FBC enable\n");
1835
1836 /* Delay the actual enabling to let pageflipping cease and the
Chris Wilson016b9b62011-07-08 12:22:43 +01001837 * display to settle before starting the compression. Note that
1838 * this delay also serves a second purpose: it allows for a
1839 * vblank to pass after disabling the FBC before we attempt
1840 * to modify the control registers.
Chris Wilson1630fe72011-07-08 12:22:42 +01001841 *
1842 * A more complicated solution would involve tracking vblanks
1843 * following the termination of the page-flipping sequence
1844 * and indeed performing the enable as a co-routine and not
1845 * waiting synchronously upon the vblank.
1846 */
1847 schedule_delayed_work(&work->work, msecs_to_jiffies(50));
Adam Jacksonee5382a2010-04-23 11:17:39 -04001848}
1849
1850void intel_disable_fbc(struct drm_device *dev)
1851{
1852 struct drm_i915_private *dev_priv = dev->dev_private;
1853
Chris Wilson1630fe72011-07-08 12:22:42 +01001854 intel_cancel_fbc_work(dev_priv);
1855
Adam Jacksonee5382a2010-04-23 11:17:39 -04001856 if (!dev_priv->display.disable_fbc)
1857 return;
1858
1859 dev_priv->display.disable_fbc(dev);
Chris Wilson016b9b62011-07-08 12:22:43 +01001860 dev_priv->cfb_plane = -1;
Adam Jacksonee5382a2010-04-23 11:17:39 -04001861}
1862
Jesse Barnes80824002009-09-10 15:28:06 -07001863/**
1864 * intel_update_fbc - enable/disable FBC as needed
Chris Wilsonbed4a672010-09-11 10:47:47 +01001865 * @dev: the drm_device
Jesse Barnes80824002009-09-10 15:28:06 -07001866 *
1867 * Set up the framebuffer compression hardware at mode set time. We
1868 * enable it if possible:
1869 * - plane A only (on pre-965)
1870 * - no pixel mulitply/line duplication
1871 * - no alpha buffer discard
1872 * - no dual wide
1873 * - framebuffer <= 2048 in width, 1536 in height
1874 *
1875 * We can't assume that any compression will take place (worst case),
1876 * so the compressed buffer has to be the same size as the uncompressed
1877 * one. It also must reside (along with the line length buffer) in
1878 * stolen memory.
1879 *
1880 * We need to enable/disable FBC on a global basis.
1881 */
Chris Wilsonbed4a672010-09-11 10:47:47 +01001882static void intel_update_fbc(struct drm_device *dev)
Jesse Barnes80824002009-09-10 15:28:06 -07001883{
Jesse Barnes80824002009-09-10 15:28:06 -07001884 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001885 struct drm_crtc *crtc = NULL, *tmp_crtc;
1886 struct intel_crtc *intel_crtc;
1887 struct drm_framebuffer *fb;
Jesse Barnes80824002009-09-10 15:28:06 -07001888 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00001889 struct drm_i915_gem_object *obj;
Keith Packardcd0de032011-09-19 21:34:19 -07001890 int enable_fbc;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001891
1892 DRM_DEBUG_KMS("\n");
Jesse Barnes80824002009-09-10 15:28:06 -07001893
1894 if (!i915_powersave)
1895 return;
1896
Adam Jacksonee5382a2010-04-23 11:17:39 -04001897 if (!I915_HAS_FBC(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07001898 return;
1899
Jesse Barnes80824002009-09-10 15:28:06 -07001900 /*
1901 * If FBC is already on, we just have to verify that we can
1902 * keep it that way...
1903 * Need to disable if:
Jesse Barnes9c928d12010-07-23 15:20:00 -07001904 * - more than one pipe is active
Jesse Barnes80824002009-09-10 15:28:06 -07001905 * - changing FBC params (stride, fence, mode)
1906 * - new fb is too large to fit in compressed buffer
1907 * - going to an unsupported config (interlace, pixel multiply, etc.)
1908 */
Jesse Barnes9c928d12010-07-23 15:20:00 -07001909 list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
Chris Wilsond2102462011-01-24 17:43:27 +00001910 if (tmp_crtc->enabled && tmp_crtc->fb) {
Chris Wilsonbed4a672010-09-11 10:47:47 +01001911 if (crtc) {
1912 DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
1913 dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
1914 goto out_disable;
1915 }
1916 crtc = tmp_crtc;
1917 }
Jesse Barnes9c928d12010-07-23 15:20:00 -07001918 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001919
1920 if (!crtc || crtc->fb == NULL) {
1921 DRM_DEBUG_KMS("no output, disabling\n");
1922 dev_priv->no_fbc_reason = FBC_NO_OUTPUT;
Jesse Barnes9c928d12010-07-23 15:20:00 -07001923 goto out_disable;
1924 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001925
1926 intel_crtc = to_intel_crtc(crtc);
1927 fb = crtc->fb;
1928 intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00001929 obj = intel_fb->obj;
Chris Wilsonbed4a672010-09-11 10:47:47 +01001930
Keith Packardcd0de032011-09-19 21:34:19 -07001931 enable_fbc = i915_enable_fbc;
1932 if (enable_fbc < 0) {
1933 DRM_DEBUG_KMS("fbc set to per-chip default\n");
1934 enable_fbc = 1;
Chris Wilsond56d8b22011-11-08 23:17:34 +00001935 if (INTEL_INFO(dev)->gen <= 6)
Keith Packardcd0de032011-09-19 21:34:19 -07001936 enable_fbc = 0;
1937 }
1938 if (!enable_fbc) {
1939 DRM_DEBUG_KMS("fbc disabled per module param\n");
Jesse Barnesc1a9f042011-05-05 15:24:21 -07001940 dev_priv->no_fbc_reason = FBC_MODULE_PARAM;
1941 goto out_disable;
1942 }
Chris Wilson05394f32010-11-08 19:18:58 +00001943 if (intel_fb->obj->base.size > dev_priv->cfb_size) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001944 DRM_DEBUG_KMS("framebuffer too large, disabling "
Chris Wilson5eddb702010-09-11 13:48:45 +01001945 "compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001946 dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
Jesse Barnes80824002009-09-10 15:28:06 -07001947 goto out_disable;
1948 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001949 if ((crtc->mode.flags & DRM_MODE_FLAG_INTERLACE) ||
1950 (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001951 DRM_DEBUG_KMS("mode incompatible with compression, "
Chris Wilson5eddb702010-09-11 13:48:45 +01001952 "disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001953 dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
Jesse Barnes80824002009-09-10 15:28:06 -07001954 goto out_disable;
1955 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001956 if ((crtc->mode.hdisplay > 2048) ||
1957 (crtc->mode.vdisplay > 1536)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001958 DRM_DEBUG_KMS("mode too large for compression, disabling\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001959 dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
Jesse Barnes80824002009-09-10 15:28:06 -07001960 goto out_disable;
1961 }
Chris Wilsonbed4a672010-09-11 10:47:47 +01001962 if ((IS_I915GM(dev) || IS_I945GM(dev)) && intel_crtc->plane != 0) {
Zhao Yakui28c97732009-10-09 11:39:41 +08001963 DRM_DEBUG_KMS("plane not 0, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001964 dev_priv->no_fbc_reason = FBC_BAD_PLANE;
Jesse Barnes80824002009-09-10 15:28:06 -07001965 goto out_disable;
1966 }
Chris Wilsonde568512011-07-08 12:22:39 +01001967
1968 /* The use of a CPU fence is mandatory in order to detect writes
1969 * by the CPU to the scanout and trigger updates to the FBC.
1970 */
1971 if (obj->tiling_mode != I915_TILING_X ||
1972 obj->fence_reg == I915_FENCE_REG_NONE) {
1973 DRM_DEBUG_KMS("framebuffer not tiled or fenced, disabling compression\n");
Jesse Barnesb5e50c32010-02-05 12:42:41 -08001974 dev_priv->no_fbc_reason = FBC_NOT_TILED;
Jesse Barnes80824002009-09-10 15:28:06 -07001975 goto out_disable;
1976 }
1977
Jason Wesselc924b932010-08-05 09:22:32 -05001978 /* If the kernel debugger is active, always disable compression */
1979 if (in_dbg_master())
1980 goto out_disable;
1981
Chris Wilson016b9b62011-07-08 12:22:43 +01001982 /* If the scanout has not changed, don't modify the FBC settings.
1983 * Note that we make the fundamental assumption that the fb->obj
1984 * cannot be unpinned (and have its GTT offset and fence revoked)
1985 * without first being decoupled from the scanout and FBC disabled.
1986 */
1987 if (dev_priv->cfb_plane == intel_crtc->plane &&
1988 dev_priv->cfb_fb == fb->base.id &&
1989 dev_priv->cfb_y == crtc->y)
1990 return;
1991
1992 if (intel_fbc_enabled(dev)) {
1993 /* We update FBC along two paths, after changing fb/crtc
1994 * configuration (modeswitching) and after page-flipping
1995 * finishes. For the latter, we know that not only did
1996 * we disable the FBC at the start of the page-flip
1997 * sequence, but also more than one vblank has passed.
1998 *
1999 * For the former case of modeswitching, it is possible
2000 * to switch between two FBC valid configurations
2001 * instantaneously so we do need to disable the FBC
2002 * before we can modify its control registers. We also
2003 * have to wait for the next vblank for that to take
2004 * effect. However, since we delay enabling FBC we can
2005 * assume that a vblank has passed since disabling and
2006 * that we can safely alter the registers in the deferred
2007 * callback.
2008 *
2009 * In the scenario that we go from a valid to invalid
2010 * and then back to valid FBC configuration we have
2011 * no strict enforcement that a vblank occurred since
2012 * disabling the FBC. However, along all current pipe
2013 * disabling paths we do need to wait for a vblank at
2014 * some point. And we wait before enabling FBC anyway.
2015 */
2016 DRM_DEBUG_KMS("disabling active FBC for update\n");
2017 intel_disable_fbc(dev);
2018 }
2019
Chris Wilsonbed4a672010-09-11 10:47:47 +01002020 intel_enable_fbc(crtc, 500);
Jesse Barnes80824002009-09-10 15:28:06 -07002021 return;
2022
2023out_disable:
Jesse Barnes80824002009-09-10 15:28:06 -07002024 /* Multiple disables should be harmless */
Chris Wilsona9394062010-05-27 13:18:16 +01002025 if (intel_fbc_enabled(dev)) {
2026 DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
Adam Jacksonee5382a2010-04-23 11:17:39 -04002027 intel_disable_fbc(dev);
Chris Wilsona9394062010-05-27 13:18:16 +01002028 }
Jesse Barnes80824002009-09-10 15:28:06 -07002029}
2030
Chris Wilson127bd2a2010-07-23 23:32:05 +01002031int
Chris Wilson48b956c2010-09-14 12:50:34 +01002032intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002033 struct drm_i915_gem_object *obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002034 struct intel_ring_buffer *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002035{
Chris Wilsonce453d82011-02-21 14:43:56 +00002036 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002037 u32 alignment;
2038 int ret;
2039
Chris Wilson05394f32010-11-08 19:18:58 +00002040 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002041 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01002042 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2043 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002044 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002045 alignment = 4 * 1024;
2046 else
2047 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002048 break;
2049 case I915_TILING_X:
2050 /* pin() will align the object as required by fence */
2051 alignment = 0;
2052 break;
2053 case I915_TILING_Y:
2054 /* FIXME: Is this true? */
2055 DRM_ERROR("Y tiled not allowed for scan out buffers\n");
2056 return -EINVAL;
2057 default:
2058 BUG();
2059 }
2060
Chris Wilsonce453d82011-02-21 14:43:56 +00002061 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002062 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002063 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002064 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002065
2066 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2067 * fence, whereas 965+ only requires a fence if using
2068 * framebuffer compression. For simplicity, we always install
2069 * a fence as the cost is not that onerous.
2070 */
Chris Wilson05394f32010-11-08 19:18:58 +00002071 if (obj->tiling_mode != I915_TILING_NONE) {
Chris Wilsonce453d82011-02-21 14:43:56 +00002072 ret = i915_gem_object_get_fence(obj, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002073 if (ret)
2074 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002075
2076 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002077 }
2078
Chris Wilsonce453d82011-02-21 14:43:56 +00002079 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002080 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002081
2082err_unpin:
2083 i915_gem_object_unpin(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002084err_interruptible:
2085 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01002086 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002087}
2088
Chris Wilson1690e1e2011-12-14 13:57:08 +01002089void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2090{
2091 i915_gem_object_unpin_fence(obj);
2092 i915_gem_object_unpin(obj);
2093}
2094
Jesse Barnes17638cd2011-06-24 12:19:23 -07002095static int i9xx_update_plane(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2096 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002097{
2098 struct drm_device *dev = crtc->dev;
2099 struct drm_i915_private *dev_priv = dev->dev_private;
2100 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2101 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002102 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002103 int plane = intel_crtc->plane;
2104 unsigned long Start, Offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002105 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002106 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002107
2108 switch (plane) {
2109 case 0:
2110 case 1:
2111 break;
2112 default:
2113 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2114 return -EINVAL;
2115 }
2116
2117 intel_fb = to_intel_framebuffer(fb);
2118 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002119
Chris Wilson5eddb702010-09-11 13:48:45 +01002120 reg = DSPCNTR(plane);
2121 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002122 /* Mask out pixel format bits in case we change it */
2123 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2124 switch (fb->bits_per_pixel) {
2125 case 8:
2126 dspcntr |= DISPPLANE_8BPP;
2127 break;
2128 case 16:
2129 if (fb->depth == 15)
2130 dspcntr |= DISPPLANE_15_16BPP;
2131 else
2132 dspcntr |= DISPPLANE_16BPP;
2133 break;
2134 case 24:
2135 case 32:
2136 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2137 break;
2138 default:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002139 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
Jesse Barnes81255562010-08-02 12:07:50 -07002140 return -EINVAL;
2141 }
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002142 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002143 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002144 dspcntr |= DISPPLANE_TILED;
2145 else
2146 dspcntr &= ~DISPPLANE_TILED;
2147 }
2148
Chris Wilson5eddb702010-09-11 13:48:45 +01002149 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002150
Chris Wilson05394f32010-11-08 19:18:58 +00002151 Start = obj->gtt_offset;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002152 Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002153
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002154 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002155 Start, Offset, x, y, fb->pitches[0]);
2156 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002157 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002158 I915_WRITE(DSPSURF(plane), Start);
2159 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2160 I915_WRITE(DSPADDR(plane), Offset);
2161 } else
2162 I915_WRITE(DSPADDR(plane), Start + Offset);
2163 POSTING_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002164
Jesse Barnes17638cd2011-06-24 12:19:23 -07002165 return 0;
2166}
2167
2168static int ironlake_update_plane(struct drm_crtc *crtc,
2169 struct drm_framebuffer *fb, int x, int y)
2170{
2171 struct drm_device *dev = crtc->dev;
2172 struct drm_i915_private *dev_priv = dev->dev_private;
2173 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2174 struct intel_framebuffer *intel_fb;
2175 struct drm_i915_gem_object *obj;
2176 int plane = intel_crtc->plane;
2177 unsigned long Start, Offset;
2178 u32 dspcntr;
2179 u32 reg;
2180
2181 switch (plane) {
2182 case 0:
2183 case 1:
Jesse Barnes27f82272011-09-02 12:54:37 -07002184 case 2:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002185 break;
2186 default:
2187 DRM_ERROR("Can't update plane %d in SAREA\n", plane);
2188 return -EINVAL;
2189 }
2190
2191 intel_fb = to_intel_framebuffer(fb);
2192 obj = intel_fb->obj;
2193
2194 reg = DSPCNTR(plane);
2195 dspcntr = I915_READ(reg);
2196 /* Mask out pixel format bits in case we change it */
2197 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
2198 switch (fb->bits_per_pixel) {
2199 case 8:
2200 dspcntr |= DISPPLANE_8BPP;
2201 break;
2202 case 16:
2203 if (fb->depth != 16)
2204 return -EINVAL;
2205
2206 dspcntr |= DISPPLANE_16BPP;
2207 break;
2208 case 24:
2209 case 32:
2210 if (fb->depth == 24)
2211 dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
2212 else if (fb->depth == 30)
2213 dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
2214 else
2215 return -EINVAL;
2216 break;
2217 default:
2218 DRM_ERROR("Unknown color depth %d\n", fb->bits_per_pixel);
2219 return -EINVAL;
2220 }
2221
2222 if (obj->tiling_mode != I915_TILING_NONE)
2223 dspcntr |= DISPPLANE_TILED;
2224 else
2225 dspcntr &= ~DISPPLANE_TILED;
2226
2227 /* must disable */
2228 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2229
2230 I915_WRITE(reg, dspcntr);
2231
2232 Start = obj->gtt_offset;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002233 Offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002234
2235 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002236 Start, Offset, x, y, fb->pitches[0]);
2237 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002238 I915_WRITE(DSPSURF(plane), Start);
2239 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2240 I915_WRITE(DSPADDR(plane), Offset);
2241 POSTING_READ(reg);
2242
2243 return 0;
2244}
2245
2246/* Assume fb object is pinned & idle & fenced and just update base pointers */
2247static int
2248intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2249 int x, int y, enum mode_set_atomic state)
2250{
2251 struct drm_device *dev = crtc->dev;
2252 struct drm_i915_private *dev_priv = dev->dev_private;
2253 int ret;
2254
2255 ret = dev_priv->display.update_plane(crtc, fb, x, y);
2256 if (ret)
2257 return ret;
2258
Chris Wilsonbed4a672010-09-11 10:47:47 +01002259 intel_update_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002260 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002261
2262 return 0;
2263}
2264
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002265static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002266intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
2267 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002268{
2269 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08002270 struct drm_i915_master_private *master_priv;
2271 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002272 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002273
2274 /* no fb bound */
2275 if (!crtc->fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002276 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002277 return 0;
2278 }
2279
Chris Wilson265db952010-09-20 15:41:01 +01002280 switch (intel_crtc->plane) {
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002281 case 0:
2282 case 1:
2283 break;
Jesse Barnes27f82272011-09-02 12:54:37 -07002284 case 2:
2285 if (IS_IVYBRIDGE(dev))
2286 break;
2287 /* fall through otherwise */
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002288 default:
Jesse Barnesa5071c22011-07-19 15:38:56 -07002289 DRM_ERROR("no plane for crtc\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002290 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002291 }
2292
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002293 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002294 ret = intel_pin_and_fence_fb_obj(dev,
2295 to_intel_framebuffer(crtc->fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002296 NULL);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002297 if (ret != 0) {
2298 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002299 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002300 return ret;
2301 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002302
Chris Wilson265db952010-09-20 15:41:01 +01002303 if (old_fb) {
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002304 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +00002305 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
Chris Wilson265db952010-09-20 15:41:01 +01002306
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002307 wait_event(dev_priv->pending_flip_queue,
Chris Wilson01eec722011-02-11 20:47:45 +00002308 atomic_read(&dev_priv->mm.wedged) ||
Chris Wilson05394f32010-11-08 19:18:58 +00002309 atomic_read(&obj->pending_flip) == 0);
Chris Wilson85345512010-11-13 09:49:11 +00002310
2311 /* Big Hammer, we also need to ensure that any pending
2312 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2313 * current scanout is retired before unpinning the old
2314 * framebuffer.
Chris Wilson01eec722011-02-11 20:47:45 +00002315 *
2316 * This should only fail upon a hung GPU, in which case we
2317 * can safely continue.
Chris Wilson85345512010-11-13 09:49:11 +00002318 */
Chris Wilsona8198ee2011-04-13 22:04:09 +01002319 ret = i915_gem_object_finish_gpu(obj);
Chris Wilson01eec722011-02-11 20:47:45 +00002320 (void) ret;
Chris Wilson265db952010-09-20 15:41:01 +01002321 }
2322
Jason Wessel21c74a82010-10-13 14:09:44 -05002323 ret = intel_pipe_set_base_atomic(crtc, crtc->fb, x, y,
2324 LEAVE_ATOMIC_MODE_SET);
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002325 if (ret) {
Chris Wilson1690e1e2011-12-14 13:57:08 +01002326 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002327 mutex_unlock(&dev->struct_mutex);
Jesse Barnesa5071c22011-07-19 15:38:56 -07002328 DRM_ERROR("failed to update base address\n");
Chris Wilson4e6cfef2010-08-08 13:20:19 +01002329 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002330 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002331
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002332 if (old_fb) {
2333 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002334 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002335 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002336
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002337 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002338
2339 if (!dev->primary->master)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002340 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002341
2342 master_priv = dev->primary->master->driver_priv;
2343 if (!master_priv->sarea_priv)
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002344 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002345
Chris Wilson265db952010-09-20 15:41:01 +01002346 if (intel_crtc->pipe) {
Jesse Barnes79e53942008-11-07 14:24:08 -08002347 master_priv->sarea_priv->pipeB_x = x;
2348 master_priv->sarea_priv->pipeB_y = y;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002349 } else {
2350 master_priv->sarea_priv->pipeA_x = x;
2351 master_priv->sarea_priv->pipeA_y = y;
Jesse Barnes79e53942008-11-07 14:24:08 -08002352 }
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002353
2354 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002355}
2356
Chris Wilson5eddb702010-09-11 13:48:45 +01002357static void ironlake_set_pll_edp(struct drm_crtc *crtc, int clock)
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002358{
2359 struct drm_device *dev = crtc->dev;
2360 struct drm_i915_private *dev_priv = dev->dev_private;
2361 u32 dpa_ctl;
2362
Zhao Yakui28c97732009-10-09 11:39:41 +08002363 DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002364 dpa_ctl = I915_READ(DP_A);
2365 dpa_ctl &= ~DP_PLL_FREQ_MASK;
2366
2367 if (clock < 200000) {
2368 u32 temp;
2369 dpa_ctl |= DP_PLL_FREQ_160MHZ;
2370 /* workaround for 160Mhz:
2371 1) program 0x4600c bits 15:0 = 0x8124
2372 2) program 0x46010 bit 0 = 1
2373 3) program 0x46034 bit 24 = 1
2374 4) program 0x64000 bit 14 = 1
2375 */
2376 temp = I915_READ(0x4600c);
2377 temp &= 0xffff0000;
2378 I915_WRITE(0x4600c, temp | 0x8124);
2379
2380 temp = I915_READ(0x46010);
2381 I915_WRITE(0x46010, temp | 1);
2382
2383 temp = I915_READ(0x46034);
2384 I915_WRITE(0x46034, temp | (1 << 24));
2385 } else {
2386 dpa_ctl |= DP_PLL_FREQ_270MHZ;
2387 }
2388 I915_WRITE(DP_A, dpa_ctl);
2389
Chris Wilson5eddb702010-09-11 13:48:45 +01002390 POSTING_READ(DP_A);
Zhenyu Wang32f9d652009-07-24 01:00:32 +08002391 udelay(500);
2392}
2393
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002394static void intel_fdi_normal_train(struct drm_crtc *crtc)
2395{
2396 struct drm_device *dev = crtc->dev;
2397 struct drm_i915_private *dev_priv = dev->dev_private;
2398 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2399 int pipe = intel_crtc->pipe;
2400 u32 reg, temp;
2401
2402 /* enable normal train */
2403 reg = FDI_TX_CTL(pipe);
2404 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002405 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002406 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2407 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002408 } else {
2409 temp &= ~FDI_LINK_TRAIN_NONE;
2410 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002411 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002412 I915_WRITE(reg, temp);
2413
2414 reg = FDI_RX_CTL(pipe);
2415 temp = I915_READ(reg);
2416 if (HAS_PCH_CPT(dev)) {
2417 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2418 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2419 } else {
2420 temp &= ~FDI_LINK_TRAIN_NONE;
2421 temp |= FDI_LINK_TRAIN_NONE;
2422 }
2423 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2424
2425 /* wait one idle pattern time */
2426 POSTING_READ(reg);
2427 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002428
2429 /* IVB wants error correction enabled */
2430 if (IS_IVYBRIDGE(dev))
2431 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2432 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002433}
2434
Jesse Barnes291427f2011-07-29 12:42:37 -07002435static void cpt_phase_pointer_enable(struct drm_device *dev, int pipe)
2436{
2437 struct drm_i915_private *dev_priv = dev->dev_private;
2438 u32 flags = I915_READ(SOUTH_CHICKEN1);
2439
2440 flags |= FDI_PHASE_SYNC_OVR(pipe);
2441 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to unlock... */
2442 flags |= FDI_PHASE_SYNC_EN(pipe);
2443 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to enable */
2444 POSTING_READ(SOUTH_CHICKEN1);
2445}
2446
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002447/* The FDI link training functions for ILK/Ibexpeak. */
2448static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2449{
2450 struct drm_device *dev = crtc->dev;
2451 struct drm_i915_private *dev_priv = dev->dev_private;
2452 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2453 int pipe = intel_crtc->pipe;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002454 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01002455 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002456
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002457 /* FDI needs bits from pipe & plane first */
2458 assert_pipe_enabled(dev_priv, pipe);
2459 assert_plane_enabled(dev_priv, plane);
2460
Adam Jacksone1a44742010-06-25 15:32:14 -04002461 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2462 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002463 reg = FDI_RX_IMR(pipe);
2464 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002465 temp &= ~FDI_RX_SYMBOL_LOCK;
2466 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002467 I915_WRITE(reg, temp);
2468 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002469 udelay(150);
2470
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002471 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002472 reg = FDI_TX_CTL(pipe);
2473 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002474 temp &= ~(7 << 19);
2475 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002476 temp &= ~FDI_LINK_TRAIN_NONE;
2477 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002478 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002479
Chris Wilson5eddb702010-09-11 13:48:45 +01002480 reg = FDI_RX_CTL(pipe);
2481 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002482 temp &= ~FDI_LINK_TRAIN_NONE;
2483 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002484 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2485
2486 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002487 udelay(150);
2488
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002489 /* Ironlake workaround, enable clock pointer after FDI enable*/
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002490 if (HAS_PCH_IBX(dev)) {
2491 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2492 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2493 FDI_RX_PHASE_SYNC_POINTER_EN);
2494 }
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002495
Chris Wilson5eddb702010-09-11 13:48:45 +01002496 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002497 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002498 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002499 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2500
2501 if ((temp & FDI_RX_BIT_LOCK)) {
2502 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002503 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002504 break;
2505 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002506 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002507 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002508 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002509
2510 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002511 reg = FDI_TX_CTL(pipe);
2512 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002513 temp &= ~FDI_LINK_TRAIN_NONE;
2514 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002515 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002516
Chris Wilson5eddb702010-09-11 13:48:45 +01002517 reg = FDI_RX_CTL(pipe);
2518 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002519 temp &= ~FDI_LINK_TRAIN_NONE;
2520 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002521 I915_WRITE(reg, temp);
2522
2523 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002524 udelay(150);
2525
Chris Wilson5eddb702010-09-11 13:48:45 +01002526 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002527 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002528 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002529 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2530
2531 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002532 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002533 DRM_DEBUG_KMS("FDI train 2 done.\n");
2534 break;
2535 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002536 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002537 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002538 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002539
2540 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002541
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002542}
2543
Akshay Joshi0206e352011-08-16 15:34:10 -04002544static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002545 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2546 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2547 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2548 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2549};
2550
2551/* The FDI link training functions for SNB/Cougarpoint. */
2552static void gen6_fdi_link_train(struct drm_crtc *crtc)
2553{
2554 struct drm_device *dev = crtc->dev;
2555 struct drm_i915_private *dev_priv = dev->dev_private;
2556 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2557 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002558 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002559
Adam Jacksone1a44742010-06-25 15:32:14 -04002560 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2561 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002562 reg = FDI_RX_IMR(pipe);
2563 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002564 temp &= ~FDI_RX_SYMBOL_LOCK;
2565 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002566 I915_WRITE(reg, temp);
2567
2568 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002569 udelay(150);
2570
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002571 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002572 reg = FDI_TX_CTL(pipe);
2573 temp = I915_READ(reg);
Adam Jackson77ffb592010-04-12 11:38:44 -04002574 temp &= ~(7 << 19);
2575 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002576 temp &= ~FDI_LINK_TRAIN_NONE;
2577 temp |= FDI_LINK_TRAIN_PATTERN_1;
2578 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2579 /* SNB-B */
2580 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002581 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002582
Chris Wilson5eddb702010-09-11 13:48:45 +01002583 reg = FDI_RX_CTL(pipe);
2584 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002585 if (HAS_PCH_CPT(dev)) {
2586 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2587 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2588 } else {
2589 temp &= ~FDI_LINK_TRAIN_NONE;
2590 temp |= FDI_LINK_TRAIN_PATTERN_1;
2591 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002592 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2593
2594 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002595 udelay(150);
2596
Jesse Barnes291427f2011-07-29 12:42:37 -07002597 if (HAS_PCH_CPT(dev))
2598 cpt_phase_pointer_enable(dev, pipe);
2599
Akshay Joshi0206e352011-08-16 15:34:10 -04002600 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002601 reg = FDI_TX_CTL(pipe);
2602 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002603 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2604 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002605 I915_WRITE(reg, temp);
2606
2607 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002608 udelay(500);
2609
Sean Paulfa37d392012-03-02 12:53:39 -05002610 for (retry = 0; retry < 5; retry++) {
2611 reg = FDI_RX_IIR(pipe);
2612 temp = I915_READ(reg);
2613 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2614 if (temp & FDI_RX_BIT_LOCK) {
2615 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2616 DRM_DEBUG_KMS("FDI train 1 done.\n");
2617 break;
2618 }
2619 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002620 }
Sean Paulfa37d392012-03-02 12:53:39 -05002621 if (retry < 5)
2622 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002623 }
2624 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002625 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002626
2627 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002628 reg = FDI_TX_CTL(pipe);
2629 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002630 temp &= ~FDI_LINK_TRAIN_NONE;
2631 temp |= FDI_LINK_TRAIN_PATTERN_2;
2632 if (IS_GEN6(dev)) {
2633 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2634 /* SNB-B */
2635 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2636 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002637 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002638
Chris Wilson5eddb702010-09-11 13:48:45 +01002639 reg = FDI_RX_CTL(pipe);
2640 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002641 if (HAS_PCH_CPT(dev)) {
2642 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2643 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2644 } else {
2645 temp &= ~FDI_LINK_TRAIN_NONE;
2646 temp |= FDI_LINK_TRAIN_PATTERN_2;
2647 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002648 I915_WRITE(reg, temp);
2649
2650 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002651 udelay(150);
2652
Akshay Joshi0206e352011-08-16 15:34:10 -04002653 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002654 reg = FDI_TX_CTL(pipe);
2655 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002656 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2657 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002658 I915_WRITE(reg, temp);
2659
2660 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002661 udelay(500);
2662
Sean Paulfa37d392012-03-02 12:53:39 -05002663 for (retry = 0; retry < 5; retry++) {
2664 reg = FDI_RX_IIR(pipe);
2665 temp = I915_READ(reg);
2666 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2667 if (temp & FDI_RX_SYMBOL_LOCK) {
2668 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2669 DRM_DEBUG_KMS("FDI train 2 done.\n");
2670 break;
2671 }
2672 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002673 }
Sean Paulfa37d392012-03-02 12:53:39 -05002674 if (retry < 5)
2675 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002676 }
2677 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002678 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002679
2680 DRM_DEBUG_KMS("FDI train done.\n");
2681}
2682
Jesse Barnes357555c2011-04-28 15:09:55 -07002683/* Manual link training for Ivy Bridge A0 parts */
2684static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
2685{
2686 struct drm_device *dev = crtc->dev;
2687 struct drm_i915_private *dev_priv = dev->dev_private;
2688 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2689 int pipe = intel_crtc->pipe;
2690 u32 reg, temp, i;
2691
2692 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2693 for train result */
2694 reg = FDI_RX_IMR(pipe);
2695 temp = I915_READ(reg);
2696 temp &= ~FDI_RX_SYMBOL_LOCK;
2697 temp &= ~FDI_RX_BIT_LOCK;
2698 I915_WRITE(reg, temp);
2699
2700 POSTING_READ(reg);
2701 udelay(150);
2702
2703 /* enable CPU FDI TX and PCH FDI RX */
2704 reg = FDI_TX_CTL(pipe);
2705 temp = I915_READ(reg);
2706 temp &= ~(7 << 19);
2707 temp |= (intel_crtc->fdi_lanes - 1) << 19;
2708 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
2709 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
2710 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2711 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002712 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002713 I915_WRITE(reg, temp | FDI_TX_ENABLE);
2714
2715 reg = FDI_RX_CTL(pipe);
2716 temp = I915_READ(reg);
2717 temp &= ~FDI_LINK_TRAIN_AUTO;
2718 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2719 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
Jesse Barnesc4f9c4c2011-10-10 14:28:52 -07002720 temp |= FDI_COMPOSITE_SYNC;
Jesse Barnes357555c2011-04-28 15:09:55 -07002721 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2722
2723 POSTING_READ(reg);
2724 udelay(150);
2725
Jesse Barnes291427f2011-07-29 12:42:37 -07002726 if (HAS_PCH_CPT(dev))
2727 cpt_phase_pointer_enable(dev, pipe);
2728
Akshay Joshi0206e352011-08-16 15:34:10 -04002729 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002730 reg = FDI_TX_CTL(pipe);
2731 temp = I915_READ(reg);
2732 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2733 temp |= snb_b_fdi_train_param[i];
2734 I915_WRITE(reg, temp);
2735
2736 POSTING_READ(reg);
2737 udelay(500);
2738
2739 reg = FDI_RX_IIR(pipe);
2740 temp = I915_READ(reg);
2741 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2742
2743 if (temp & FDI_RX_BIT_LOCK ||
2744 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
2745 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2746 DRM_DEBUG_KMS("FDI train 1 done.\n");
2747 break;
2748 }
2749 }
2750 if (i == 4)
2751 DRM_ERROR("FDI train 1 fail!\n");
2752
2753 /* Train 2 */
2754 reg = FDI_TX_CTL(pipe);
2755 temp = I915_READ(reg);
2756 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2757 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
2758 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2759 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
2760 I915_WRITE(reg, temp);
2761
2762 reg = FDI_RX_CTL(pipe);
2763 temp = I915_READ(reg);
2764 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2765 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
2766 I915_WRITE(reg, temp);
2767
2768 POSTING_READ(reg);
2769 udelay(150);
2770
Akshay Joshi0206e352011-08-16 15:34:10 -04002771 for (i = 0; i < 4; i++) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002772 reg = FDI_TX_CTL(pipe);
2773 temp = I915_READ(reg);
2774 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2775 temp |= snb_b_fdi_train_param[i];
2776 I915_WRITE(reg, temp);
2777
2778 POSTING_READ(reg);
2779 udelay(500);
2780
2781 reg = FDI_RX_IIR(pipe);
2782 temp = I915_READ(reg);
2783 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2784
2785 if (temp & FDI_RX_SYMBOL_LOCK) {
2786 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
2787 DRM_DEBUG_KMS("FDI train 2 done.\n");
2788 break;
2789 }
2790 }
2791 if (i == 4)
2792 DRM_ERROR("FDI train 2 fail!\n");
2793
2794 DRM_DEBUG_KMS("FDI train done.\n");
2795}
2796
2797static void ironlake_fdi_pll_enable(struct drm_crtc *crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07002798{
2799 struct drm_device *dev = crtc->dev;
2800 struct drm_i915_private *dev_priv = dev->dev_private;
2801 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2802 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002803 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07002804
Jesse Barnesc64e3112010-09-10 11:27:03 -07002805 /* Write the TU size bits so error detection works */
Chris Wilson5eddb702010-09-11 13:48:45 +01002806 I915_WRITE(FDI_RX_TUSIZE1(pipe),
2807 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
Jesse Barnesc64e3112010-09-10 11:27:03 -07002808
Jesse Barnes0e23b992010-09-10 11:10:00 -07002809 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01002810 reg = FDI_RX_CTL(pipe);
2811 temp = I915_READ(reg);
2812 temp &= ~((0x7 << 19) | (0x7 << 16));
Jesse Barnes0e23b992010-09-10 11:10:00 -07002813 temp |= (intel_crtc->fdi_lanes - 1) << 19;
Chris Wilson5eddb702010-09-11 13:48:45 +01002814 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2815 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
2816
2817 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002818 udelay(200);
2819
2820 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01002821 temp = I915_READ(reg);
2822 I915_WRITE(reg, temp | FDI_PCDCLK);
2823
2824 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002825 udelay(200);
2826
2827 /* Enable CPU FDI TX PLL, always on for Ironlake */
Chris Wilson5eddb702010-09-11 13:48:45 +01002828 reg = FDI_TX_CTL(pipe);
2829 temp = I915_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002830 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002831 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
2832
2833 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07002834 udelay(100);
2835 }
2836}
2837
Jesse Barnes291427f2011-07-29 12:42:37 -07002838static void cpt_phase_pointer_disable(struct drm_device *dev, int pipe)
2839{
2840 struct drm_i915_private *dev_priv = dev->dev_private;
2841 u32 flags = I915_READ(SOUTH_CHICKEN1);
2842
2843 flags &= ~(FDI_PHASE_SYNC_EN(pipe));
2844 I915_WRITE(SOUTH_CHICKEN1, flags); /* once to disable... */
2845 flags &= ~(FDI_PHASE_SYNC_OVR(pipe));
2846 I915_WRITE(SOUTH_CHICKEN1, flags); /* then again to lock */
2847 POSTING_READ(SOUTH_CHICKEN1);
2848}
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002849static void ironlake_fdi_disable(struct drm_crtc *crtc)
2850{
2851 struct drm_device *dev = crtc->dev;
2852 struct drm_i915_private *dev_priv = dev->dev_private;
2853 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2854 int pipe = intel_crtc->pipe;
2855 u32 reg, temp;
2856
2857 /* disable CPU FDI tx and PCH FDI rx */
2858 reg = FDI_TX_CTL(pipe);
2859 temp = I915_READ(reg);
2860 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
2861 POSTING_READ(reg);
2862
2863 reg = FDI_RX_CTL(pipe);
2864 temp = I915_READ(reg);
2865 temp &= ~(0x7 << 16);
2866 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2867 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
2868
2869 POSTING_READ(reg);
2870 udelay(100);
2871
2872 /* Ironlake workaround, disable clock pointer after downing FDI */
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002873 if (HAS_PCH_IBX(dev)) {
2874 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002875 I915_WRITE(FDI_RX_CHICKEN(pipe),
2876 I915_READ(FDI_RX_CHICKEN(pipe) &
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002877 ~FDI_RX_PHASE_SYNC_POINTER_EN));
Jesse Barnes291427f2011-07-29 12:42:37 -07002878 } else if (HAS_PCH_CPT(dev)) {
2879 cpt_phase_pointer_disable(dev, pipe);
Jesse Barnes6f06ce12011-01-04 15:09:38 -08002880 }
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002881
2882 /* still set train pattern 1 */
2883 reg = FDI_TX_CTL(pipe);
2884 temp = I915_READ(reg);
2885 temp &= ~FDI_LINK_TRAIN_NONE;
2886 temp |= FDI_LINK_TRAIN_PATTERN_1;
2887 I915_WRITE(reg, temp);
2888
2889 reg = FDI_RX_CTL(pipe);
2890 temp = I915_READ(reg);
2891 if (HAS_PCH_CPT(dev)) {
2892 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2893 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2894 } else {
2895 temp &= ~FDI_LINK_TRAIN_NONE;
2896 temp |= FDI_LINK_TRAIN_PATTERN_1;
2897 }
2898 /* BPC in FDI rx is consistent with that in PIPECONF */
2899 temp &= ~(0x07 << 16);
2900 temp |= (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) << 11;
2901 I915_WRITE(reg, temp);
2902
2903 POSTING_READ(reg);
2904 udelay(100);
2905}
2906
Chris Wilson6b383a72010-09-13 13:54:26 +01002907/*
2908 * When we disable a pipe, we need to clear any pending scanline wait events
2909 * to avoid hanging the ring, which we assume we are waiting on.
2910 */
2911static void intel_clear_scanline_wait(struct drm_device *dev)
2912{
2913 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson8168bd42010-11-11 17:54:52 +00002914 struct intel_ring_buffer *ring;
Chris Wilson6b383a72010-09-13 13:54:26 +01002915 u32 tmp;
2916
2917 if (IS_GEN2(dev))
2918 /* Can't break the hang on i8xx */
2919 return;
2920
Chris Wilson1ec14ad2010-12-04 11:30:53 +00002921 ring = LP_RING(dev_priv);
Chris Wilson8168bd42010-11-11 17:54:52 +00002922 tmp = I915_READ_CTL(ring);
2923 if (tmp & RING_WAIT)
2924 I915_WRITE_CTL(ring, tmp);
Chris Wilson6b383a72010-09-13 13:54:26 +01002925}
2926
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002927static void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
2928{
Chris Wilson05394f32010-11-08 19:18:58 +00002929 struct drm_i915_gem_object *obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002930 struct drm_i915_private *dev_priv;
2931
2932 if (crtc->fb == NULL)
2933 return;
2934
Chris Wilson05394f32010-11-08 19:18:58 +00002935 obj = to_intel_framebuffer(crtc->fb)->obj;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002936 dev_priv = crtc->dev->dev_private;
2937 wait_event(dev_priv->pending_flip_queue,
Chris Wilson05394f32010-11-08 19:18:58 +00002938 atomic_read(&obj->pending_flip) == 0);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01002939}
2940
Jesse Barnes040484a2011-01-03 12:14:26 -08002941static bool intel_crtc_driving_pch(struct drm_crtc *crtc)
2942{
2943 struct drm_device *dev = crtc->dev;
2944 struct drm_mode_config *mode_config = &dev->mode_config;
2945 struct intel_encoder *encoder;
2946
2947 /*
2948 * If there's a non-PCH eDP on this crtc, it must be DP_A, and that
2949 * must be driven by its own crtc; no sharing is possible.
2950 */
2951 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
2952 if (encoder->base.crtc != crtc)
2953 continue;
2954
2955 switch (encoder->type) {
2956 case INTEL_OUTPUT_EDP:
2957 if (!intel_encoder_is_pch_edp(&encoder->base))
2958 return false;
2959 continue;
2960 }
2961 }
2962
2963 return true;
2964}
2965
Jesse Barnesf67a5592011-01-05 10:31:48 -08002966/*
2967 * Enable PCH resources required for PCH ports:
2968 * - PCH PLLs
2969 * - FDI training & RX/TX
2970 * - update transcoder timings
2971 * - DP transcoding bits
2972 * - transcoder
2973 */
2974static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08002975{
2976 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08002977 struct drm_i915_private *dev_priv = dev->dev_private;
2978 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2979 int pipe = intel_crtc->pipe;
Jesse Barnes4b645f12011-10-12 09:51:31 -07002980 u32 reg, temp, transc_sel;
Jesse Barnes6be4a602010-09-10 10:26:01 -07002981
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002982 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07002983 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07002984
Jesse Barnes92f25842011-01-04 15:09:34 -08002985 intel_enable_pch_pll(dev_priv, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002986
2987 if (HAS_PCH_CPT(dev)) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07002988 transc_sel = intel_crtc->use_pll_a ? TRANSC_DPLLA_SEL :
2989 TRANSC_DPLLB_SEL;
2990
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002991 /* Be sure PCH DPLL SEL is set */
2992 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnesd64311a2011-10-12 15:01:33 -07002993 if (pipe == 0) {
2994 temp &= ~(TRANSA_DPLLB_SEL);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002995 temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
Jesse Barnesd64311a2011-10-12 15:01:33 -07002996 } else if (pipe == 1) {
2997 temp &= ~(TRANSB_DPLLB_SEL);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07002998 temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnesd64311a2011-10-12 15:01:33 -07002999 } else if (pipe == 2) {
3000 temp &= ~(TRANSC_DPLLB_SEL);
Jesse Barnes4b645f12011-10-12 09:51:31 -07003001 temp |= (TRANSC_DPLL_ENABLE | transc_sel);
Jesse Barnesd64311a2011-10-12 15:01:33 -07003002 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003003 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003004 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003005
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003006 /* set transcoder timing, panel must allow it */
3007 assert_panel_unlocked(dev_priv, pipe);
Chris Wilson5eddb702010-09-11 13:48:45 +01003008 I915_WRITE(TRANS_HTOTAL(pipe), I915_READ(HTOTAL(pipe)));
3009 I915_WRITE(TRANS_HBLANK(pipe), I915_READ(HBLANK(pipe)));
3010 I915_WRITE(TRANS_HSYNC(pipe), I915_READ(HSYNC(pipe)));
3011
3012 I915_WRITE(TRANS_VTOTAL(pipe), I915_READ(VTOTAL(pipe)));
3013 I915_WRITE(TRANS_VBLANK(pipe), I915_READ(VBLANK(pipe)));
3014 I915_WRITE(TRANS_VSYNC(pipe), I915_READ(VSYNC(pipe)));
Daniel Vetter0529a0d2012-01-28 14:49:24 +01003015 I915_WRITE(TRANS_VSYNCSHIFT(pipe), I915_READ(VSYNCSHIFT(pipe)));
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003016
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003017 intel_fdi_normal_train(crtc);
3018
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003019 /* For PCH DP, enable TRANS_DP_CTL */
3020 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003021 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3022 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003023 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPE_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003024 reg = TRANS_DP_CTL(pipe);
3025 temp = I915_READ(reg);
3026 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003027 TRANS_DP_SYNC_MASK |
3028 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003029 temp |= (TRANS_DP_OUTPUT_ENABLE |
3030 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003031 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003032
3033 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003034 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003035 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003036 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003037
3038 switch (intel_trans_dp_port_sel(crtc)) {
3039 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003040 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003041 break;
3042 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003043 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003044 break;
3045 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003046 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003047 break;
3048 default:
3049 DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003050 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003051 break;
3052 }
3053
Chris Wilson5eddb702010-09-11 13:48:45 +01003054 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003055 }
3056
Jesse Barnes040484a2011-01-03 12:14:26 -08003057 intel_enable_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003058}
3059
Jesse Barnesd4270e52011-10-11 10:43:02 -07003060void intel_cpt_verify_modeset(struct drm_device *dev, int pipe)
3061{
3062 struct drm_i915_private *dev_priv = dev->dev_private;
3063 int dslreg = PIPEDSL(pipe), tc2reg = TRANS_CHICKEN2(pipe);
3064 u32 temp;
3065
3066 temp = I915_READ(dslreg);
3067 udelay(500);
3068 if (wait_for(I915_READ(dslreg) != temp, 5)) {
3069 /* Without this, mode sets may fail silently on FDI */
3070 I915_WRITE(tc2reg, TRANS_AUTOTRAIN_GEN_STALL_DIS);
3071 udelay(250);
3072 I915_WRITE(tc2reg, 0);
3073 if (wait_for(I915_READ(dslreg) != temp, 5))
3074 DRM_ERROR("mode set failed: pipe %d stuck\n", pipe);
3075 }
3076}
3077
Jesse Barnesf67a5592011-01-05 10:31:48 -08003078static void ironlake_crtc_enable(struct drm_crtc *crtc)
3079{
3080 struct drm_device *dev = crtc->dev;
3081 struct drm_i915_private *dev_priv = dev->dev_private;
3082 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3083 int pipe = intel_crtc->pipe;
3084 int plane = intel_crtc->plane;
3085 u32 temp;
3086 bool is_pch_port;
3087
3088 if (intel_crtc->active)
3089 return;
3090
3091 intel_crtc->active = true;
3092 intel_update_watermarks(dev);
3093
3094 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
3095 temp = I915_READ(PCH_LVDS);
3096 if ((temp & LVDS_PORT_EN) == 0)
3097 I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
3098 }
3099
3100 is_pch_port = intel_crtc_driving_pch(crtc);
3101
3102 if (is_pch_port)
Jesse Barnes357555c2011-04-28 15:09:55 -07003103 ironlake_fdi_pll_enable(crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003104 else
3105 ironlake_fdi_disable(crtc);
3106
3107 /* Enable panel fitting for LVDS */
3108 if (dev_priv->pch_pf_size &&
3109 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) || HAS_eDP)) {
3110 /* Force use of hard-coded filter coefficients
3111 * as some pre-programmed values are broken,
3112 * e.g. x201.
3113 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003114 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3115 I915_WRITE(PF_WIN_POS(pipe), dev_priv->pch_pf_pos);
3116 I915_WRITE(PF_WIN_SZ(pipe), dev_priv->pch_pf_size);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003117 }
3118
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02003119 /*
3120 * On ILK+ LUT must be loaded before the pipe is running but with
3121 * clocks enabled
3122 */
3123 intel_crtc_load_lut(crtc);
3124
Jesse Barnesf67a5592011-01-05 10:31:48 -08003125 intel_enable_pipe(dev_priv, pipe, is_pch_port);
3126 intel_enable_plane(dev_priv, plane, pipe);
3127
3128 if (is_pch_port)
3129 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003130
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003131 mutex_lock(&dev->struct_mutex);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003132 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003133 mutex_unlock(&dev->struct_mutex);
3134
Chris Wilson6b383a72010-09-13 13:54:26 +01003135 intel_crtc_update_cursor(crtc, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003136}
3137
3138static void ironlake_crtc_disable(struct drm_crtc *crtc)
3139{
3140 struct drm_device *dev = crtc->dev;
3141 struct drm_i915_private *dev_priv = dev->dev_private;
3142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3143 int pipe = intel_crtc->pipe;
3144 int plane = intel_crtc->plane;
Chris Wilson5eddb702010-09-11 13:48:45 +01003145 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003146
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003147 if (!intel_crtc->active)
3148 return;
3149
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003150 intel_crtc_wait_for_pending_flips(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003151 drm_vblank_off(dev, pipe);
Chris Wilson6b383a72010-09-13 13:54:26 +01003152 intel_crtc_update_cursor(crtc, false);
Chris Wilson5eddb702010-09-11 13:48:45 +01003153
Jesse Barnesb24e7172011-01-04 15:09:30 -08003154 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003155
Chris Wilson973d04f2011-07-08 12:22:37 +01003156 if (dev_priv->cfb_plane == plane)
3157 intel_disable_fbc(dev);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003158
Jesse Barnesb24e7172011-01-04 15:09:30 -08003159 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003160
Jesse Barnes6be4a602010-09-10 10:26:01 -07003161 /* Disable PF */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003162 I915_WRITE(PF_CTL(pipe), 0);
3163 I915_WRITE(PF_WIN_SZ(pipe), 0);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003164
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003165 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003166
Jesse Barnes47a05ec2011-02-07 13:46:40 -08003167 /* This is a horrible layering violation; we should be doing this in
3168 * the connector/encoder ->prepare instead, but we don't always have
3169 * enough information there about the config to know whether it will
3170 * actually be necessary or just cause undesired flicker.
3171 */
3172 intel_disable_pch_ports(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003173
Jesse Barnes040484a2011-01-03 12:14:26 -08003174 intel_disable_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003175
Jesse Barnes6be4a602010-09-10 10:26:01 -07003176 if (HAS_PCH_CPT(dev)) {
3177 /* disable TRANS_DP_CTL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003178 reg = TRANS_DP_CTL(pipe);
3179 temp = I915_READ(reg);
3180 temp &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
Eric Anholtcb3543c2011-02-02 12:08:07 -08003181 temp |= TRANS_DP_PORT_SEL_NONE;
Chris Wilson5eddb702010-09-11 13:48:45 +01003182 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003183
3184 /* disable DPLL_SEL */
3185 temp = I915_READ(PCH_DPLL_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003186 switch (pipe) {
3187 case 0:
Jesse Barnesd64311a2011-10-12 15:01:33 -07003188 temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003189 break;
3190 case 1:
Jesse Barnes6be4a602010-09-10 10:26:01 -07003191 temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003192 break;
3193 case 2:
Jesse Barnes4b645f12011-10-12 09:51:31 -07003194 /* C shares PLL A or B */
Jesse Barnesd64311a2011-10-12 15:01:33 -07003195 temp &= ~(TRANSC_DPLL_ENABLE | TRANSC_DPLLB_SEL);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003196 break;
3197 default:
3198 BUG(); /* wtf */
3199 }
Jesse Barnes6be4a602010-09-10 10:26:01 -07003200 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003201 }
3202
3203 /* disable PCH DPLL */
Jesse Barnes4b645f12011-10-12 09:51:31 -07003204 if (!intel_crtc->no_pll)
3205 intel_disable_pch_pll(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003206
3207 /* Switch from PCDclk to Rawclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003208 reg = FDI_RX_CTL(pipe);
3209 temp = I915_READ(reg);
3210 I915_WRITE(reg, temp & ~FDI_PCDCLK);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003211
3212 /* Disable CPU FDI TX PLL */
Chris Wilson5eddb702010-09-11 13:48:45 +01003213 reg = FDI_TX_CTL(pipe);
3214 temp = I915_READ(reg);
3215 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3216
3217 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003218 udelay(100);
3219
Chris Wilson5eddb702010-09-11 13:48:45 +01003220 reg = FDI_RX_CTL(pipe);
3221 temp = I915_READ(reg);
3222 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003223
3224 /* Wait for the clocks to turn off. */
Chris Wilson5eddb702010-09-11 13:48:45 +01003225 POSTING_READ(reg);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003226 udelay(100);
Chris Wilson6b383a72010-09-13 13:54:26 +01003227
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003228 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003229 intel_update_watermarks(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003230
3231 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01003232 intel_update_fbc(dev);
3233 intel_clear_scanline_wait(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01003234 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003235}
3236
3237static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
3238{
3239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3240 int pipe = intel_crtc->pipe;
3241 int plane = intel_crtc->plane;
3242
Zhenyu Wang2c072452009-06-05 15:38:42 +08003243 /* XXX: When our outputs are all unaware of DPMS modes other than off
3244 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
3245 */
3246 switch (mode) {
3247 case DRM_MODE_DPMS_ON:
3248 case DRM_MODE_DPMS_STANDBY:
3249 case DRM_MODE_DPMS_SUSPEND:
Chris Wilson868dc582010-08-07 11:01:31 +01003250 DRM_DEBUG_KMS("crtc %d/%d dpms on\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003251 ironlake_crtc_enable(crtc);
Chris Wilson868dc582010-08-07 11:01:31 +01003252 break;
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08003253
Zhenyu Wang2c072452009-06-05 15:38:42 +08003254 case DRM_MODE_DPMS_OFF:
Chris Wilson868dc582010-08-07 11:01:31 +01003255 DRM_DEBUG_KMS("crtc %d/%d dpms off\n", pipe, plane);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003256 ironlake_crtc_disable(crtc);
Zhenyu Wang2c072452009-06-05 15:38:42 +08003257 break;
3258 }
3259}
3260
Daniel Vetter02e792f2009-09-15 22:57:34 +02003261static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3262{
Daniel Vetter02e792f2009-09-15 22:57:34 +02003263 if (!enable && intel_crtc->overlay) {
Chris Wilson23f09ce2010-08-12 13:53:37 +01003264 struct drm_device *dev = intel_crtc->base.dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00003265 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter03f77ea2009-09-15 22:57:37 +02003266
Chris Wilson23f09ce2010-08-12 13:53:37 +01003267 mutex_lock(&dev->struct_mutex);
Chris Wilsonce453d82011-02-21 14:43:56 +00003268 dev_priv->mm.interruptible = false;
3269 (void) intel_overlay_switch_off(intel_crtc->overlay);
3270 dev_priv->mm.interruptible = true;
Chris Wilson23f09ce2010-08-12 13:53:37 +01003271 mutex_unlock(&dev->struct_mutex);
Daniel Vetter02e792f2009-09-15 22:57:34 +02003272 }
Daniel Vetter02e792f2009-09-15 22:57:34 +02003273
Chris Wilson5dcdbcb2010-08-12 13:50:28 +01003274 /* Let userspace switch the overlay on again. In most cases userspace
3275 * has to recompute where to put it anyway.
3276 */
Daniel Vetter02e792f2009-09-15 22:57:34 +02003277}
3278
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003279static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003280{
3281 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08003282 struct drm_i915_private *dev_priv = dev->dev_private;
3283 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3284 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07003285 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08003286
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003287 if (intel_crtc->active)
3288 return;
3289
3290 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01003291 intel_update_watermarks(dev);
3292
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003293 intel_enable_pll(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08003294 intel_enable_pipe(dev_priv, pipe, false);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003295 intel_enable_plane(dev_priv, plane, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003296
3297 intel_crtc_load_lut(crtc);
Chris Wilsonbed4a672010-09-11 10:47:47 +01003298 intel_update_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003299
3300 /* Give the overlay scaler a chance to enable if it's on this pipe */
3301 intel_crtc_dpms_overlay(intel_crtc, true);
Chris Wilson6b383a72010-09-13 13:54:26 +01003302 intel_crtc_update_cursor(crtc, true);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003303}
3304
3305static void i9xx_crtc_disable(struct drm_crtc *crtc)
3306{
3307 struct drm_device *dev = crtc->dev;
3308 struct drm_i915_private *dev_priv = dev->dev_private;
3309 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3310 int pipe = intel_crtc->pipe;
3311 int plane = intel_crtc->plane;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003312
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003313 if (!intel_crtc->active)
3314 return;
3315
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003316 /* Give the overlay scaler a chance to disable if it's on this pipe */
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003317 intel_crtc_wait_for_pending_flips(crtc);
3318 drm_vblank_off(dev, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003319 intel_crtc_dpms_overlay(intel_crtc, false);
Chris Wilson6b383a72010-09-13 13:54:26 +01003320 intel_crtc_update_cursor(crtc, false);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003321
Chris Wilson973d04f2011-07-08 12:22:37 +01003322 if (dev_priv->cfb_plane == plane)
3323 intel_disable_fbc(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003324
Jesse Barnesb24e7172011-01-04 15:09:30 -08003325 intel_disable_plane(dev_priv, plane, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08003326 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08003327 intel_disable_pll(dev_priv, pipe);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003328
Chris Wilsonf7abfe82010-09-13 14:19:16 +01003329 intel_crtc->active = false;
Chris Wilson6b383a72010-09-13 13:54:26 +01003330 intel_update_fbc(dev);
3331 intel_update_watermarks(dev);
3332 intel_clear_scanline_wait(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003333}
3334
3335static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
3336{
Jesse Barnes79e53942008-11-07 14:24:08 -08003337 /* XXX: When our outputs are all unaware of DPMS modes other than off
3338 * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
3339 */
3340 switch (mode) {
3341 case DRM_MODE_DPMS_ON:
3342 case DRM_MODE_DPMS_STANDBY:
3343 case DRM_MODE_DPMS_SUSPEND:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003344 i9xx_crtc_enable(crtc);
3345 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08003346 case DRM_MODE_DPMS_OFF:
Jesse Barnes0b8765c62010-09-10 10:31:34 -07003347 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003348 break;
3349 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08003350}
3351
3352/**
3353 * Sets the power management mode of the pipe and plane.
Zhenyu Wang2c072452009-06-05 15:38:42 +08003354 */
3355static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
3356{
3357 struct drm_device *dev = crtc->dev;
Jesse Barnese70236a2009-09-21 10:42:27 -07003358 struct drm_i915_private *dev_priv = dev->dev_private;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003359 struct drm_i915_master_private *master_priv;
3360 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3361 int pipe = intel_crtc->pipe;
3362 bool enabled;
3363
Chris Wilson032d2a02010-09-06 16:17:22 +01003364 if (intel_crtc->dpms_mode == mode)
3365 return;
3366
Chris Wilsondebcadd2010-08-07 11:01:33 +01003367 intel_crtc->dpms_mode = mode;
Chris Wilsondebcadd2010-08-07 11:01:33 +01003368
Jesse Barnese70236a2009-09-21 10:42:27 -07003369 dev_priv->display.dpms(crtc, mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08003370
3371 if (!dev->primary->master)
3372 return;
3373
3374 master_priv = dev->primary->master->driver_priv;
3375 if (!master_priv->sarea_priv)
3376 return;
3377
3378 enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
3379
3380 switch (pipe) {
3381 case 0:
3382 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
3383 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
3384 break;
3385 case 1:
3386 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
3387 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
3388 break;
3389 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08003390 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08003391 break;
3392 }
Jesse Barnes79e53942008-11-07 14:24:08 -08003393}
3394
Chris Wilsoncdd59982010-09-08 16:30:16 +01003395static void intel_crtc_disable(struct drm_crtc *crtc)
3396{
3397 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
3398 struct drm_device *dev = crtc->dev;
3399
3400 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
Chris Wilson931872f2012-01-16 23:01:13 +00003401 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
3402 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003403
3404 if (crtc->fb) {
3405 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01003406 intel_unpin_fb_obj(to_intel_framebuffer(crtc->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01003407 mutex_unlock(&dev->struct_mutex);
3408 }
3409}
3410
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003411/* Prepare for a mode set.
3412 *
3413 * Note we could be a lot smarter here. We need to figure out which outputs
3414 * will be enabled, which disabled (in short, how the config will changes)
3415 * and perform the minimum necessary steps to accomplish that, e.g. updating
3416 * watermarks, FBC configuration, making sure PLLs are programmed correctly,
3417 * panel fitting is in the proper state, etc.
3418 */
3419static void i9xx_crtc_prepare(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003420{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003421 i9xx_crtc_disable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003422}
3423
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003424static void i9xx_crtc_commit(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003425{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003426 i9xx_crtc_enable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003427}
3428
3429static void ironlake_crtc_prepare(struct drm_crtc *crtc)
3430{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003431 ironlake_crtc_disable(crtc);
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003432}
3433
3434static void ironlake_crtc_commit(struct drm_crtc *crtc)
3435{
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07003436 ironlake_crtc_enable(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08003437}
3438
Akshay Joshi0206e352011-08-16 15:34:10 -04003439void intel_encoder_prepare(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003440{
3441 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
3442 /* lvds has its own version of prepare see intel_lvds_prepare */
3443 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
3444}
3445
Akshay Joshi0206e352011-08-16 15:34:10 -04003446void intel_encoder_commit(struct drm_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -08003447{
3448 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
Jesse Barnesd4270e52011-10-11 10:43:02 -07003449 struct drm_device *dev = encoder->dev;
3450 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3451 struct intel_crtc *intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
3452
Jesse Barnes79e53942008-11-07 14:24:08 -08003453 /* lvds has its own version of commit see intel_lvds_commit */
3454 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003455
3456 if (HAS_PCH_CPT(dev))
3457 intel_cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08003458}
3459
Chris Wilsonea5b2132010-08-04 13:50:23 +01003460void intel_encoder_destroy(struct drm_encoder *encoder)
3461{
Chris Wilson4ef69c72010-09-09 15:14:28 +01003462 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01003463
Chris Wilsonea5b2132010-08-04 13:50:23 +01003464 drm_encoder_cleanup(encoder);
3465 kfree(intel_encoder);
3466}
3467
Jesse Barnes79e53942008-11-07 14:24:08 -08003468static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
3469 struct drm_display_mode *mode,
3470 struct drm_display_mode *adjusted_mode)
3471{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003472 struct drm_device *dev = crtc->dev;
Chris Wilson89749352010-09-12 18:25:19 +01003473
Eric Anholtbad720f2009-10-22 16:11:14 -07003474 if (HAS_PCH_SPLIT(dev)) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08003475 /* FDI link clock is fixed at 2.7G */
Jesse Barnes2377b742010-07-07 14:06:43 -07003476 if (mode->clock * 3 > IRONLAKE_FDI_FREQ * 4)
3477 return false;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003478 }
Chris Wilson89749352010-09-12 18:25:19 +01003479
Daniel Vetterca9bfa72012-01-28 14:49:20 +01003480 /* All interlaced capable intel hw wants timings in frames. */
3481 drm_mode_set_crtcinfo(adjusted_mode, 0);
Chris Wilson89749352010-09-12 18:25:19 +01003482
Jesse Barnes79e53942008-11-07 14:24:08 -08003483 return true;
3484}
3485
Jesse Barnese70236a2009-09-21 10:42:27 -07003486static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08003487{
Jesse Barnese70236a2009-09-21 10:42:27 -07003488 return 400000;
3489}
Jesse Barnes79e53942008-11-07 14:24:08 -08003490
Jesse Barnese70236a2009-09-21 10:42:27 -07003491static int i915_get_display_clock_speed(struct drm_device *dev)
3492{
3493 return 333000;
3494}
Jesse Barnes79e53942008-11-07 14:24:08 -08003495
Jesse Barnese70236a2009-09-21 10:42:27 -07003496static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
3497{
3498 return 200000;
3499}
Jesse Barnes79e53942008-11-07 14:24:08 -08003500
Jesse Barnese70236a2009-09-21 10:42:27 -07003501static int i915gm_get_display_clock_speed(struct drm_device *dev)
3502{
3503 u16 gcfgc = 0;
3504
3505 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
3506
3507 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08003508 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07003509 else {
3510 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
3511 case GC_DISPLAY_CLOCK_333_MHZ:
3512 return 333000;
3513 default:
3514 case GC_DISPLAY_CLOCK_190_200_MHZ:
3515 return 190000;
3516 }
3517 }
3518}
Jesse Barnes79e53942008-11-07 14:24:08 -08003519
Jesse Barnese70236a2009-09-21 10:42:27 -07003520static int i865_get_display_clock_speed(struct drm_device *dev)
3521{
3522 return 266000;
3523}
3524
3525static int i855_get_display_clock_speed(struct drm_device *dev)
3526{
3527 u16 hpllcc = 0;
3528 /* Assume that the hardware is in the high speed state. This
3529 * should be the default.
3530 */
3531 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
3532 case GC_CLOCK_133_200:
3533 case GC_CLOCK_100_200:
3534 return 200000;
3535 case GC_CLOCK_166_250:
3536 return 250000;
3537 case GC_CLOCK_100_133:
3538 return 133000;
3539 }
3540
3541 /* Shouldn't happen */
3542 return 0;
3543}
3544
3545static int i830_get_display_clock_speed(struct drm_device *dev)
3546{
3547 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08003548}
3549
Zhenyu Wang2c072452009-06-05 15:38:42 +08003550struct fdi_m_n {
3551 u32 tu;
3552 u32 gmch_m;
3553 u32 gmch_n;
3554 u32 link_m;
3555 u32 link_n;
3556};
3557
3558static void
3559fdi_reduce_ratio(u32 *num, u32 *den)
3560{
3561 while (*num > 0xffffff || *den > 0xffffff) {
3562 *num >>= 1;
3563 *den >>= 1;
3564 }
3565}
3566
Zhenyu Wang2c072452009-06-05 15:38:42 +08003567static void
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003568ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
3569 int link_clock, struct fdi_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08003570{
Zhenyu Wang2c072452009-06-05 15:38:42 +08003571 m_n->tu = 64; /* default size */
3572
Chris Wilson22ed1112010-12-04 01:01:29 +00003573 /* BUG_ON(pixel_clock > INT_MAX / 36); */
3574 m_n->gmch_m = bits_per_pixel * pixel_clock;
3575 m_n->gmch_n = link_clock * nlanes * 8;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003576 fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
3577
Chris Wilson22ed1112010-12-04 01:01:29 +00003578 m_n->link_m = pixel_clock;
3579 m_n->link_n = link_clock;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003580 fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
3581}
3582
3583
Shaohua Li7662c8b2009-06-26 11:23:55 +08003584struct intel_watermark_params {
3585 unsigned long fifo_size;
3586 unsigned long max_wm;
3587 unsigned long default_wm;
3588 unsigned long guard_size;
3589 unsigned long cacheline_size;
3590};
3591
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003592/* Pineview has different values for various configs */
Chris Wilsond2102462011-01-24 17:43:27 +00003593static const struct intel_watermark_params pineview_display_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003594 PINEVIEW_DISPLAY_FIFO,
3595 PINEVIEW_MAX_WM,
3596 PINEVIEW_DFT_WM,
3597 PINEVIEW_GUARD_WM,
3598 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08003599};
Chris Wilsond2102462011-01-24 17:43:27 +00003600static const struct intel_watermark_params pineview_display_hplloff_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003601 PINEVIEW_DISPLAY_FIFO,
3602 PINEVIEW_MAX_WM,
3603 PINEVIEW_DFT_HPLLOFF_WM,
3604 PINEVIEW_GUARD_WM,
3605 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08003606};
Chris Wilsond2102462011-01-24 17:43:27 +00003607static const struct intel_watermark_params pineview_cursor_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003608 PINEVIEW_CURSOR_FIFO,
3609 PINEVIEW_CURSOR_MAX_WM,
3610 PINEVIEW_CURSOR_DFT_WM,
3611 PINEVIEW_CURSOR_GUARD_WM,
3612 PINEVIEW_FIFO_LINE_SIZE,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003613};
Chris Wilsond2102462011-01-24 17:43:27 +00003614static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003615 PINEVIEW_CURSOR_FIFO,
3616 PINEVIEW_CURSOR_MAX_WM,
3617 PINEVIEW_CURSOR_DFT_WM,
3618 PINEVIEW_CURSOR_GUARD_WM,
3619 PINEVIEW_FIFO_LINE_SIZE
Shaohua Li7662c8b2009-06-26 11:23:55 +08003620};
Chris Wilsond2102462011-01-24 17:43:27 +00003621static const struct intel_watermark_params g4x_wm_info = {
Jesse Barnes0e442c62009-10-19 10:09:33 +09003622 G4X_FIFO_SIZE,
3623 G4X_MAX_WM,
3624 G4X_MAX_WM,
3625 2,
3626 G4X_FIFO_LINE_SIZE,
3627};
Chris Wilsond2102462011-01-24 17:43:27 +00003628static const struct intel_watermark_params g4x_cursor_wm_info = {
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003629 I965_CURSOR_FIFO,
3630 I965_CURSOR_MAX_WM,
3631 I965_CURSOR_DFT_WM,
3632 2,
3633 G4X_FIFO_LINE_SIZE,
3634};
Chris Wilsond2102462011-01-24 17:43:27 +00003635static const struct intel_watermark_params i965_cursor_wm_info = {
Zhao Yakui4fe5e612010-06-12 14:32:25 +08003636 I965_CURSOR_FIFO,
3637 I965_CURSOR_MAX_WM,
3638 I965_CURSOR_DFT_WM,
3639 2,
3640 I915_FIFO_LINE_SIZE,
3641};
Chris Wilsond2102462011-01-24 17:43:27 +00003642static const struct intel_watermark_params i945_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003643 I945_FIFO_SIZE,
3644 I915_MAX_WM,
3645 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003646 2,
3647 I915_FIFO_LINE_SIZE
3648};
Chris Wilsond2102462011-01-24 17:43:27 +00003649static const struct intel_watermark_params i915_wm_info = {
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003650 I915_FIFO_SIZE,
3651 I915_MAX_WM,
3652 1,
3653 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003654 I915_FIFO_LINE_SIZE
3655};
Chris Wilsond2102462011-01-24 17:43:27 +00003656static const struct intel_watermark_params i855_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003657 I855GM_FIFO_SIZE,
3658 I915_MAX_WM,
3659 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003660 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003661 I830_FIFO_LINE_SIZE
3662};
Chris Wilsond2102462011-01-24 17:43:27 +00003663static const struct intel_watermark_params i830_wm_info = {
Shaohua Li7662c8b2009-06-26 11:23:55 +08003664 I830_FIFO_SIZE,
3665 I915_MAX_WM,
3666 1,
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003667 2,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003668 I830_FIFO_LINE_SIZE
3669};
3670
Chris Wilsond2102462011-01-24 17:43:27 +00003671static const struct intel_watermark_params ironlake_display_wm_info = {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003672 ILK_DISPLAY_FIFO,
3673 ILK_DISPLAY_MAXWM,
3674 ILK_DISPLAY_DFTWM,
3675 2,
3676 ILK_FIFO_LINE_SIZE
3677};
Chris Wilsond2102462011-01-24 17:43:27 +00003678static const struct intel_watermark_params ironlake_cursor_wm_info = {
Zhao Yakuic936f442010-06-12 14:32:26 +08003679 ILK_CURSOR_FIFO,
3680 ILK_CURSOR_MAXWM,
3681 ILK_CURSOR_DFTWM,
3682 2,
3683 ILK_FIFO_LINE_SIZE
3684};
Chris Wilsond2102462011-01-24 17:43:27 +00003685static const struct intel_watermark_params ironlake_display_srwm_info = {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003686 ILK_DISPLAY_SR_FIFO,
3687 ILK_DISPLAY_MAX_SRWM,
3688 ILK_DISPLAY_DFT_SRWM,
3689 2,
3690 ILK_FIFO_LINE_SIZE
3691};
Chris Wilsond2102462011-01-24 17:43:27 +00003692static const struct intel_watermark_params ironlake_cursor_srwm_info = {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08003693 ILK_CURSOR_SR_FIFO,
3694 ILK_CURSOR_MAX_SRWM,
3695 ILK_CURSOR_DFT_SRWM,
3696 2,
3697 ILK_FIFO_LINE_SIZE
3698};
3699
Chris Wilsond2102462011-01-24 17:43:27 +00003700static const struct intel_watermark_params sandybridge_display_wm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003701 SNB_DISPLAY_FIFO,
3702 SNB_DISPLAY_MAXWM,
3703 SNB_DISPLAY_DFTWM,
3704 2,
3705 SNB_FIFO_LINE_SIZE
3706};
Chris Wilsond2102462011-01-24 17:43:27 +00003707static const struct intel_watermark_params sandybridge_cursor_wm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003708 SNB_CURSOR_FIFO,
3709 SNB_CURSOR_MAXWM,
3710 SNB_CURSOR_DFTWM,
3711 2,
3712 SNB_FIFO_LINE_SIZE
3713};
Chris Wilsond2102462011-01-24 17:43:27 +00003714static const struct intel_watermark_params sandybridge_display_srwm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003715 SNB_DISPLAY_SR_FIFO,
3716 SNB_DISPLAY_MAX_SRWM,
3717 SNB_DISPLAY_DFT_SRWM,
3718 2,
3719 SNB_FIFO_LINE_SIZE
3720};
Chris Wilsond2102462011-01-24 17:43:27 +00003721static const struct intel_watermark_params sandybridge_cursor_srwm_info = {
Yuanhan Liu13982612010-12-15 15:42:31 +08003722 SNB_CURSOR_SR_FIFO,
3723 SNB_CURSOR_MAX_SRWM,
3724 SNB_CURSOR_DFT_SRWM,
3725 2,
3726 SNB_FIFO_LINE_SIZE
3727};
3728
3729
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003730/**
3731 * intel_calculate_wm - calculate watermark level
3732 * @clock_in_khz: pixel clock
3733 * @wm: chip FIFO params
3734 * @pixel_size: display pixel size
3735 * @latency_ns: memory latency for the platform
3736 *
3737 * Calculate the watermark level (the level at which the display plane will
3738 * start fetching from memory again). Each chip has a different display
3739 * FIFO size and allocation, so the caller needs to figure that out and pass
3740 * in the correct intel_watermark_params structure.
3741 *
3742 * As the pixel clock runs, the FIFO will be drained at a rate that depends
3743 * on the pixel size. When it reaches the watermark level, it'll start
3744 * fetching FIFO line sized based chunks from memory until the FIFO fills
3745 * past the watermark point. If the FIFO drains completely, a FIFO underrun
3746 * will occur, and a display engine hang could result.
3747 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003748static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
Chris Wilsond2102462011-01-24 17:43:27 +00003749 const struct intel_watermark_params *wm,
3750 int fifo_size,
Shaohua Li7662c8b2009-06-26 11:23:55 +08003751 int pixel_size,
3752 unsigned long latency_ns)
3753{
Jesse Barnes390c4dd2009-07-16 13:01:01 -07003754 long entries_required, wm_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003755
Jesse Barnesd6604672009-09-11 12:25:56 -07003756 /*
3757 * Note: we need to make sure we don't overflow for various clock &
3758 * latency values.
3759 * clocks go from a few thousand to several hundred thousand.
3760 * latency is usually a few thousand
3761 */
3762 entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
3763 1000;
Chris Wilson8de9b312010-07-19 19:59:52 +01003764 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003765
Joe Perchesbbb0aef52011-04-17 20:35:52 -07003766 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003767
Chris Wilsond2102462011-01-24 17:43:27 +00003768 wm_size = fifo_size - (entries_required + wm->guard_size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003769
Joe Perchesbbb0aef52011-04-17 20:35:52 -07003770 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003771
Jesse Barnes390c4dd2009-07-16 13:01:01 -07003772 /* Don't promote wm_size to unsigned... */
3773 if (wm_size > (long)wm->max_wm)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003774 wm_size = wm->max_wm;
Chris Wilsonc3add4b2010-09-08 09:14:08 +01003775 if (wm_size <= 0)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003776 wm_size = wm->default_wm;
3777 return wm_size;
3778}
3779
3780struct cxsr_latency {
3781 int is_desktop;
Li Peng95534262010-05-18 18:58:44 +08003782 int is_ddr3;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003783 unsigned long fsb_freq;
3784 unsigned long mem_freq;
3785 unsigned long display_sr;
3786 unsigned long display_hpll_disable;
3787 unsigned long cursor_sr;
3788 unsigned long cursor_hpll_disable;
3789};
3790
Chris Wilson403c89f2010-08-04 15:25:31 +01003791static const struct cxsr_latency cxsr_latency_table[] = {
Li Peng95534262010-05-18 18:58:44 +08003792 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
3793 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
3794 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
3795 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
3796 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003797
Li Peng95534262010-05-18 18:58:44 +08003798 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
3799 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
3800 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
3801 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
3802 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003803
Li Peng95534262010-05-18 18:58:44 +08003804 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
3805 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
3806 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
3807 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
3808 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003809
Li Peng95534262010-05-18 18:58:44 +08003810 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
3811 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
3812 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
3813 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
3814 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003815
Li Peng95534262010-05-18 18:58:44 +08003816 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
3817 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
3818 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
3819 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
3820 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003821
Li Peng95534262010-05-18 18:58:44 +08003822 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
3823 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
3824 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
3825 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
3826 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
Shaohua Li7662c8b2009-06-26 11:23:55 +08003827};
3828
Chris Wilson403c89f2010-08-04 15:25:31 +01003829static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
3830 int is_ddr3,
3831 int fsb,
3832 int mem)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003833{
Chris Wilson403c89f2010-08-04 15:25:31 +01003834 const struct cxsr_latency *latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003835 int i;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003836
3837 if (fsb == 0 || mem == 0)
3838 return NULL;
3839
3840 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
3841 latency = &cxsr_latency_table[i];
3842 if (is_desktop == latency->is_desktop &&
Li Peng95534262010-05-18 18:58:44 +08003843 is_ddr3 == latency->is_ddr3 &&
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303844 fsb == latency->fsb_freq && mem == latency->mem_freq)
3845 return latency;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003846 }
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303847
Zhao Yakui28c97732009-10-09 11:39:41 +08003848 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
Jaswinder Singh Rajputdecbbcd2009-09-12 23:15:07 +05303849
3850 return NULL;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003851}
3852
Adam Jacksonf2b115e2009-12-03 17:14:42 -05003853static void pineview_disable_cxsr(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08003854{
3855 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003856
3857 /* deactivate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01003858 I915_WRITE(DSPFW3, I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN);
Shaohua Li7662c8b2009-06-26 11:23:55 +08003859}
3860
Jesse Barnesbcc24fb2009-08-31 10:24:31 -07003861/*
3862 * Latency for FIFO fetches is dependent on several factors:
3863 * - memory configuration (speed, channels)
3864 * - chipset
3865 * - current MCH state
3866 * It can be fairly high in some situations, so here we assume a fairly
3867 * pessimal value. It's a tradeoff between extra memory fetches (if we
3868 * set this value too high, the FIFO will fetch frequently to stay full)
3869 * and power consumption (set it too low to save power and we might see
3870 * FIFO underruns and display "flicker").
3871 *
3872 * A value of 5us seems to be a good balance; safe for very low end
3873 * platforms but not overly aggressive on lower latency configs.
3874 */
Tobias Klauser69e302a2009-12-23 14:14:34 +01003875static const int latency_ns = 5000;
Shaohua Li7662c8b2009-06-26 11:23:55 +08003876
Jesse Barnese70236a2009-09-21 10:42:27 -07003877static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003878{
3879 struct drm_i915_private *dev_priv = dev->dev_private;
3880 uint32_t dsparb = I915_READ(DSPARB);
3881 int size;
3882
Chris Wilson8de9b312010-07-19 19:59:52 +01003883 size = dsparb & 0x7f;
3884 if (plane)
3885 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003886
Zhao Yakui28c97732009-10-09 11:39:41 +08003887 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003888 plane ? "B" : "A", size);
Jesse Barnesdff33cf2009-07-14 10:15:56 -07003889
3890 return size;
3891}
Shaohua Li7662c8b2009-06-26 11:23:55 +08003892
Jesse Barnese70236a2009-09-21 10:42:27 -07003893static int i85x_get_fifo_size(struct drm_device *dev, int plane)
3894{
3895 struct drm_i915_private *dev_priv = dev->dev_private;
3896 uint32_t dsparb = I915_READ(DSPARB);
3897 int size;
3898
Chris Wilson8de9b312010-07-19 19:59:52 +01003899 size = dsparb & 0x1ff;
3900 if (plane)
3901 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
Jesse Barnese70236a2009-09-21 10:42:27 -07003902 size >>= 1; /* Convert to cachelines */
3903
Zhao Yakui28c97732009-10-09 11:39:41 +08003904 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003905 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003906
3907 return size;
3908}
3909
3910static int i845_get_fifo_size(struct drm_device *dev, int plane)
3911{
3912 struct drm_i915_private *dev_priv = dev->dev_private;
3913 uint32_t dsparb = I915_READ(DSPARB);
3914 int size;
3915
3916 size = dsparb & 0x7f;
3917 size >>= 2; /* Convert to cachelines */
3918
Zhao Yakui28c97732009-10-09 11:39:41 +08003919 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003920 plane ? "B" : "A",
3921 size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003922
3923 return size;
3924}
3925
3926static int i830_get_fifo_size(struct drm_device *dev, int plane)
3927{
3928 struct drm_i915_private *dev_priv = dev->dev_private;
3929 uint32_t dsparb = I915_READ(DSPARB);
3930 int size;
3931
3932 size = dsparb & 0x7f;
3933 size >>= 1; /* Convert to cachelines */
3934
Zhao Yakui28c97732009-10-09 11:39:41 +08003935 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
Chris Wilson5eddb702010-09-11 13:48:45 +01003936 plane ? "B" : "A", size);
Jesse Barnese70236a2009-09-21 10:42:27 -07003937
3938 return size;
3939}
3940
Chris Wilsond2102462011-01-24 17:43:27 +00003941static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
3942{
3943 struct drm_crtc *crtc, *enabled = NULL;
3944
3945 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3946 if (crtc->enabled && crtc->fb) {
3947 if (enabled)
3948 return NULL;
3949 enabled = crtc;
3950 }
3951 }
3952
3953 return enabled;
3954}
3955
3956static void pineview_update_wm(struct drm_device *dev)
Zhao Yakuid4294342010-03-22 22:45:36 +08003957{
3958 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00003959 struct drm_crtc *crtc;
Chris Wilson403c89f2010-08-04 15:25:31 +01003960 const struct cxsr_latency *latency;
Zhao Yakuid4294342010-03-22 22:45:36 +08003961 u32 reg;
3962 unsigned long wm;
Zhao Yakuid4294342010-03-22 22:45:36 +08003963
Chris Wilson403c89f2010-08-04 15:25:31 +01003964 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
Li Peng95534262010-05-18 18:58:44 +08003965 dev_priv->fsb_freq, dev_priv->mem_freq);
Zhao Yakuid4294342010-03-22 22:45:36 +08003966 if (!latency) {
3967 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
3968 pineview_disable_cxsr(dev);
3969 return;
3970 }
3971
Chris Wilsond2102462011-01-24 17:43:27 +00003972 crtc = single_enabled_crtc(dev);
3973 if (crtc) {
3974 int clock = crtc->mode.clock;
3975 int pixel_size = crtc->fb->bits_per_pixel / 8;
Zhao Yakuid4294342010-03-22 22:45:36 +08003976
3977 /* Display SR */
Chris Wilsond2102462011-01-24 17:43:27 +00003978 wm = intel_calculate_wm(clock, &pineview_display_wm,
3979 pineview_display_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08003980 pixel_size, latency->display_sr);
3981 reg = I915_READ(DSPFW1);
3982 reg &= ~DSPFW_SR_MASK;
3983 reg |= wm << DSPFW_SR_SHIFT;
3984 I915_WRITE(DSPFW1, reg);
3985 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
3986
3987 /* cursor SR */
Chris Wilsond2102462011-01-24 17:43:27 +00003988 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
3989 pineview_display_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08003990 pixel_size, latency->cursor_sr);
3991 reg = I915_READ(DSPFW3);
3992 reg &= ~DSPFW_CURSOR_SR_MASK;
3993 reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
3994 I915_WRITE(DSPFW3, reg);
3995
3996 /* Display HPLL off SR */
Chris Wilsond2102462011-01-24 17:43:27 +00003997 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
3998 pineview_display_hplloff_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08003999 pixel_size, latency->display_hpll_disable);
4000 reg = I915_READ(DSPFW3);
4001 reg &= ~DSPFW_HPLL_SR_MASK;
4002 reg |= wm & DSPFW_HPLL_SR_MASK;
4003 I915_WRITE(DSPFW3, reg);
4004
4005 /* cursor HPLL off SR */
Chris Wilsond2102462011-01-24 17:43:27 +00004006 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
4007 pineview_display_hplloff_wm.fifo_size,
Zhao Yakuid4294342010-03-22 22:45:36 +08004008 pixel_size, latency->cursor_hpll_disable);
4009 reg = I915_READ(DSPFW3);
4010 reg &= ~DSPFW_HPLL_CURSOR_MASK;
4011 reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
4012 I915_WRITE(DSPFW3, reg);
4013 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
4014
4015 /* activate cxsr */
Chris Wilson3e33d942010-08-04 11:17:25 +01004016 I915_WRITE(DSPFW3,
4017 I915_READ(DSPFW3) | PINEVIEW_SELF_REFRESH_EN);
Zhao Yakuid4294342010-03-22 22:45:36 +08004018 DRM_DEBUG_KMS("Self-refresh is enabled\n");
4019 } else {
4020 pineview_disable_cxsr(dev);
4021 DRM_DEBUG_KMS("Self-refresh is disabled\n");
4022 }
4023}
4024
Chris Wilson417ae142011-01-19 15:04:42 +00004025static bool g4x_compute_wm0(struct drm_device *dev,
4026 int plane,
4027 const struct intel_watermark_params *display,
4028 int display_latency_ns,
4029 const struct intel_watermark_params *cursor,
4030 int cursor_latency_ns,
4031 int *plane_wm,
4032 int *cursor_wm)
Jesse Barnes652c3932009-08-17 13:31:43 -07004033{
Chris Wilson417ae142011-01-19 15:04:42 +00004034 struct drm_crtc *crtc;
4035 int htotal, hdisplay, clock, pixel_size;
4036 int line_time_us, line_count;
4037 int entries, tlb_miss;
Jesse Barnes652c3932009-08-17 13:31:43 -07004038
Chris Wilson417ae142011-01-19 15:04:42 +00004039 crtc = intel_get_crtc_for_plane(dev, plane);
Chris Wilson5c72d062011-04-13 09:28:23 +01004040 if (crtc->fb == NULL || !crtc->enabled) {
4041 *cursor_wm = cursor->guard_size;
4042 *plane_wm = display->guard_size;
Chris Wilson417ae142011-01-19 15:04:42 +00004043 return false;
Chris Wilson5c72d062011-04-13 09:28:23 +01004044 }
Jesse Barnes0e442c62009-10-19 10:09:33 +09004045
Chris Wilson417ae142011-01-19 15:04:42 +00004046 htotal = crtc->mode.htotal;
4047 hdisplay = crtc->mode.hdisplay;
4048 clock = crtc->mode.clock;
4049 pixel_size = crtc->fb->bits_per_pixel / 8;
Jesse Barnes0e442c62009-10-19 10:09:33 +09004050
Chris Wilson417ae142011-01-19 15:04:42 +00004051 /* Use the small buffer method to calculate plane watermark */
4052 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
4053 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
4054 if (tlb_miss > 0)
4055 entries += tlb_miss;
4056 entries = DIV_ROUND_UP(entries, display->cacheline_size);
4057 *plane_wm = entries + display->guard_size;
4058 if (*plane_wm > (int)display->max_wm)
4059 *plane_wm = display->max_wm;
Jesse Barnes0e442c62009-10-19 10:09:33 +09004060
Chris Wilson417ae142011-01-19 15:04:42 +00004061 /* Use the large buffer method to calculate cursor watermark */
4062 line_time_us = ((htotal * 1000) / clock);
4063 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
4064 entries = line_count * 64 * pixel_size;
4065 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
4066 if (tlb_miss > 0)
4067 entries += tlb_miss;
4068 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4069 *cursor_wm = entries + cursor->guard_size;
4070 if (*cursor_wm > (int)cursor->max_wm)
4071 *cursor_wm = (int)cursor->max_wm;
Jesse Barnes0e442c62009-10-19 10:09:33 +09004072
Chris Wilson417ae142011-01-19 15:04:42 +00004073 return true;
4074}
Jesse Barnes0e442c62009-10-19 10:09:33 +09004075
Chris Wilson417ae142011-01-19 15:04:42 +00004076/*
4077 * Check the wm result.
4078 *
4079 * If any calculated watermark values is larger than the maximum value that
4080 * can be programmed into the associated watermark register, that watermark
4081 * must be disabled.
4082 */
4083static bool g4x_check_srwm(struct drm_device *dev,
4084 int display_wm, int cursor_wm,
4085 const struct intel_watermark_params *display,
4086 const struct intel_watermark_params *cursor)
4087{
4088 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
4089 display_wm, cursor_wm);
Jesse Barnes0e442c62009-10-19 10:09:33 +09004090
Chris Wilson417ae142011-01-19 15:04:42 +00004091 if (display_wm > display->max_wm) {
Joe Perchesbbb0aef52011-04-17 20:35:52 -07004092 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
Chris Wilson417ae142011-01-19 15:04:42 +00004093 display_wm, display->max_wm);
4094 return false;
Jesse Barnes0e442c62009-10-19 10:09:33 +09004095 }
4096
Chris Wilson417ae142011-01-19 15:04:42 +00004097 if (cursor_wm > cursor->max_wm) {
Joe Perchesbbb0aef52011-04-17 20:35:52 -07004098 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
Chris Wilson417ae142011-01-19 15:04:42 +00004099 cursor_wm, cursor->max_wm);
4100 return false;
4101 }
Jesse Barnes0e442c62009-10-19 10:09:33 +09004102
Chris Wilson417ae142011-01-19 15:04:42 +00004103 if (!(display_wm || cursor_wm)) {
4104 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
4105 return false;
4106 }
Jesse Barnes0e442c62009-10-19 10:09:33 +09004107
Chris Wilson417ae142011-01-19 15:04:42 +00004108 return true;
4109}
4110
4111static bool g4x_compute_srwm(struct drm_device *dev,
Chris Wilsond2102462011-01-24 17:43:27 +00004112 int plane,
4113 int latency_ns,
Chris Wilson417ae142011-01-19 15:04:42 +00004114 const struct intel_watermark_params *display,
4115 const struct intel_watermark_params *cursor,
4116 int *display_wm, int *cursor_wm)
4117{
Chris Wilsond2102462011-01-24 17:43:27 +00004118 struct drm_crtc *crtc;
4119 int hdisplay, htotal, pixel_size, clock;
Chris Wilson417ae142011-01-19 15:04:42 +00004120 unsigned long line_time_us;
4121 int line_count, line_size;
4122 int small, large;
4123 int entries;
4124
4125 if (!latency_ns) {
4126 *display_wm = *cursor_wm = 0;
4127 return false;
4128 }
4129
Chris Wilsond2102462011-01-24 17:43:27 +00004130 crtc = intel_get_crtc_for_plane(dev, plane);
4131 hdisplay = crtc->mode.hdisplay;
4132 htotal = crtc->mode.htotal;
4133 clock = crtc->mode.clock;
4134 pixel_size = crtc->fb->bits_per_pixel / 8;
4135
Chris Wilson417ae142011-01-19 15:04:42 +00004136 line_time_us = (htotal * 1000) / clock;
4137 line_count = (latency_ns / line_time_us + 1000) / 1000;
4138 line_size = hdisplay * pixel_size;
4139
4140 /* Use the minimum of the small and large buffer method for primary */
4141 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
4142 large = line_count * line_size;
4143
4144 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
4145 *display_wm = entries + display->guard_size;
4146
4147 /* calculate the self-refresh watermark for display cursor */
4148 entries = line_count * pixel_size * 64;
4149 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4150 *cursor_wm = entries + cursor->guard_size;
4151
4152 return g4x_check_srwm(dev,
4153 *display_wm, *cursor_wm,
4154 display, cursor);
4155}
4156
Yuanhan Liu7ccb4a52011-03-18 07:37:35 +00004157#define single_plane_enabled(mask) is_power_of_2(mask)
Chris Wilsond2102462011-01-24 17:43:27 +00004158
4159static void g4x_update_wm(struct drm_device *dev)
Chris Wilson417ae142011-01-19 15:04:42 +00004160{
4161 static const int sr_latency_ns = 12000;
4162 struct drm_i915_private *dev_priv = dev->dev_private;
4163 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
Chris Wilsond2102462011-01-24 17:43:27 +00004164 int plane_sr, cursor_sr;
4165 unsigned int enabled = 0;
Chris Wilson417ae142011-01-19 15:04:42 +00004166
4167 if (g4x_compute_wm0(dev, 0,
4168 &g4x_wm_info, latency_ns,
4169 &g4x_cursor_wm_info, latency_ns,
4170 &planea_wm, &cursora_wm))
Chris Wilsond2102462011-01-24 17:43:27 +00004171 enabled |= 1;
Chris Wilson417ae142011-01-19 15:04:42 +00004172
4173 if (g4x_compute_wm0(dev, 1,
4174 &g4x_wm_info, latency_ns,
4175 &g4x_cursor_wm_info, latency_ns,
4176 &planeb_wm, &cursorb_wm))
Chris Wilsond2102462011-01-24 17:43:27 +00004177 enabled |= 2;
Chris Wilson417ae142011-01-19 15:04:42 +00004178
4179 plane_sr = cursor_sr = 0;
Chris Wilsond2102462011-01-24 17:43:27 +00004180 if (single_plane_enabled(enabled) &&
4181 g4x_compute_srwm(dev, ffs(enabled) - 1,
4182 sr_latency_ns,
Chris Wilson417ae142011-01-19 15:04:42 +00004183 &g4x_wm_info,
4184 &g4x_cursor_wm_info,
4185 &plane_sr, &cursor_sr))
4186 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
4187 else
4188 I915_WRITE(FW_BLC_SELF,
4189 I915_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN);
4190
Chris Wilson308977a2011-02-02 10:41:20 +00004191 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
4192 planea_wm, cursora_wm,
4193 planeb_wm, cursorb_wm,
4194 plane_sr, cursor_sr);
Chris Wilson417ae142011-01-19 15:04:42 +00004195
4196 I915_WRITE(DSPFW1,
4197 (plane_sr << DSPFW_SR_SHIFT) |
Jesse Barnes0e442c62009-10-19 10:09:33 +09004198 (cursorb_wm << DSPFW_CURSORB_SHIFT) |
Chris Wilson417ae142011-01-19 15:04:42 +00004199 (planeb_wm << DSPFW_PLANEB_SHIFT) |
4200 planea_wm);
4201 I915_WRITE(DSPFW2,
4202 (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
Jesse Barnes0e442c62009-10-19 10:09:33 +09004203 (cursora_wm << DSPFW_CURSORA_SHIFT));
4204 /* HPLL off in SR has some issues on G4x... disable it */
Chris Wilson417ae142011-01-19 15:04:42 +00004205 I915_WRITE(DSPFW3,
4206 (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
Jesse Barnes0e442c62009-10-19 10:09:33 +09004207 (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Jesse Barnes652c3932009-08-17 13:31:43 -07004208}
4209
Chris Wilsond2102462011-01-24 17:43:27 +00004210static void i965_update_wm(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08004211{
4212 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004213 struct drm_crtc *crtc;
4214 int srwm = 1;
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004215 int cursor_sr = 16;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004216
Jesse Barnes1dc75462009-10-19 10:08:17 +09004217 /* Calc sr entries for one plane configs */
Chris Wilsond2102462011-01-24 17:43:27 +00004218 crtc = single_enabled_crtc(dev);
4219 if (crtc) {
Jesse Barnes1dc75462009-10-19 10:08:17 +09004220 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01004221 static const int sr_latency_ns = 12000;
Chris Wilsond2102462011-01-24 17:43:27 +00004222 int clock = crtc->mode.clock;
4223 int htotal = crtc->mode.htotal;
4224 int hdisplay = crtc->mode.hdisplay;
4225 int pixel_size = crtc->fb->bits_per_pixel / 8;
4226 unsigned long line_time_us;
4227 int entries;
Jesse Barnes1dc75462009-10-19 10:08:17 +09004228
Chris Wilsond2102462011-01-24 17:43:27 +00004229 line_time_us = ((htotal * 1000) / clock);
Jesse Barnes1dc75462009-10-19 10:08:17 +09004230
4231 /* Use ns/us then divide to preserve precision */
Chris Wilsond2102462011-01-24 17:43:27 +00004232 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
4233 pixel_size * hdisplay;
4234 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
Chris Wilsond2102462011-01-24 17:43:27 +00004235 srwm = I965_FIFO_SIZE - entries;
Jesse Barnes1dc75462009-10-19 10:08:17 +09004236 if (srwm < 0)
4237 srwm = 1;
Zhao Yakui1b07e042010-06-12 14:32:24 +08004238 srwm &= 0x1ff;
Chris Wilson308977a2011-02-02 10:41:20 +00004239 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
4240 entries, srwm);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004241
Chris Wilsond2102462011-01-24 17:43:27 +00004242 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
Chris Wilson5eddb702010-09-11 13:48:45 +01004243 pixel_size * 64;
Chris Wilsond2102462011-01-24 17:43:27 +00004244 entries = DIV_ROUND_UP(entries,
Chris Wilson8de9b312010-07-19 19:59:52 +01004245 i965_cursor_wm_info.cacheline_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004246 cursor_sr = i965_cursor_wm_info.fifo_size -
Chris Wilsond2102462011-01-24 17:43:27 +00004247 (entries + i965_cursor_wm_info.guard_size);
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004248
4249 if (cursor_sr > i965_cursor_wm_info.max_wm)
4250 cursor_sr = i965_cursor_wm_info.max_wm;
4251
4252 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
4253 "cursor %d\n", srwm, cursor_sr);
4254
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004255 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07004256 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
David John33c5fd12010-01-27 15:19:08 +05304257 } else {
4258 /* Turn off self refresh if both pipes are enabled */
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004259 if (IS_CRESTLINE(dev))
Jesse Barnesadcdbc62010-06-30 13:49:37 -07004260 I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
4261 & ~FW_BLC_SELF_EN);
Jesse Barnes1dc75462009-10-19 10:08:17 +09004262 }
4263
4264 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
4265 srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004266
4267 /* 965 has limitations... */
Chris Wilson417ae142011-01-19 15:04:42 +00004268 I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) |
4269 (8 << 16) | (8 << 8) | (8 << 0));
Shaohua Li7662c8b2009-06-26 11:23:55 +08004270 I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
Zhao Yakui4fe5e612010-06-12 14:32:25 +08004271 /* update cursor SR watermark */
4272 I915_WRITE(DSPFW3, (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
Shaohua Li7662c8b2009-06-26 11:23:55 +08004273}
4274
Chris Wilsond2102462011-01-24 17:43:27 +00004275static void i9xx_update_wm(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08004276{
4277 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004278 const struct intel_watermark_params *wm_info;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004279 uint32_t fwater_lo;
4280 uint32_t fwater_hi;
Chris Wilsond2102462011-01-24 17:43:27 +00004281 int cwm, srwm = 1;
4282 int fifo_size;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004283 int planea_wm, planeb_wm;
Chris Wilsond2102462011-01-24 17:43:27 +00004284 struct drm_crtc *crtc, *enabled = NULL;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004285
Chris Wilson72557b42011-01-31 10:29:55 +00004286 if (IS_I945GM(dev))
Chris Wilsond2102462011-01-24 17:43:27 +00004287 wm_info = &i945_wm_info;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01004288 else if (!IS_GEN2(dev))
Chris Wilsond2102462011-01-24 17:43:27 +00004289 wm_info = &i915_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004290 else
Chris Wilsond2102462011-01-24 17:43:27 +00004291 wm_info = &i855_wm_info;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004292
Chris Wilsond2102462011-01-24 17:43:27 +00004293 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
4294 crtc = intel_get_crtc_for_plane(dev, 0);
4295 if (crtc->enabled && crtc->fb) {
4296 planea_wm = intel_calculate_wm(crtc->mode.clock,
4297 wm_info, fifo_size,
4298 crtc->fb->bits_per_pixel / 8,
4299 latency_ns);
4300 enabled = crtc;
4301 } else
4302 planea_wm = fifo_size - wm_info->guard_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004303
Chris Wilsond2102462011-01-24 17:43:27 +00004304 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
4305 crtc = intel_get_crtc_for_plane(dev, 1);
4306 if (crtc->enabled && crtc->fb) {
4307 planeb_wm = intel_calculate_wm(crtc->mode.clock,
4308 wm_info, fifo_size,
4309 crtc->fb->bits_per_pixel / 8,
4310 latency_ns);
4311 if (enabled == NULL)
4312 enabled = crtc;
4313 else
4314 enabled = NULL;
4315 } else
4316 planeb_wm = fifo_size - wm_info->guard_size;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004317
Zhao Yakui28c97732009-10-09 11:39:41 +08004318 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004319
4320 /*
4321 * Overlay gets an aggressive default since video jitter is bad.
4322 */
4323 cwm = 2;
4324
Alexander Lam18b21902011-01-03 13:28:56 -05004325 /* Play safe and disable self-refresh before adjusting watermarks. */
4326 if (IS_I945G(dev) || IS_I945GM(dev))
4327 I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | 0);
4328 else if (IS_I915GM(dev))
4329 I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
4330
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004331 /* Calc sr entries for one plane configs */
Chris Wilsond2102462011-01-24 17:43:27 +00004332 if (HAS_FW_BLC(dev) && enabled) {
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004333 /* self-refresh has much higher latency */
Tobias Klauser69e302a2009-12-23 14:14:34 +01004334 static const int sr_latency_ns = 6000;
Chris Wilsond2102462011-01-24 17:43:27 +00004335 int clock = enabled->mode.clock;
4336 int htotal = enabled->mode.htotal;
4337 int hdisplay = enabled->mode.hdisplay;
4338 int pixel_size = enabled->fb->bits_per_pixel / 8;
4339 unsigned long line_time_us;
4340 int entries;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004341
Chris Wilsond2102462011-01-24 17:43:27 +00004342 line_time_us = (htotal * 1000) / clock;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004343
4344 /* Use ns/us then divide to preserve precision */
Chris Wilsond2102462011-01-24 17:43:27 +00004345 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
4346 pixel_size * hdisplay;
4347 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
4348 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
4349 srwm = wm_info->fifo_size - entries;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004350 if (srwm < 0)
4351 srwm = 1;
Li Pengee980b82010-01-27 19:01:11 +08004352
4353 if (IS_I945G(dev) || IS_I945GM(dev))
Alexander Lam18b21902011-01-03 13:28:56 -05004354 I915_WRITE(FW_BLC_SELF,
4355 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
4356 else if (IS_I915GM(dev))
Li Pengee980b82010-01-27 19:01:11 +08004357 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004358 }
4359
Zhao Yakui28c97732009-10-09 11:39:41 +08004360 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
Chris Wilson5eddb702010-09-11 13:48:45 +01004361 planea_wm, planeb_wm, cwm, srwm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004362
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004363 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
4364 fwater_hi = (cwm & 0x1f);
4365
4366 /* Set request length to 8 cachelines per fetch */
4367 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
4368 fwater_hi = fwater_hi | (1 << 8);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004369
4370 I915_WRITE(FW_BLC, fwater_lo);
4371 I915_WRITE(FW_BLC2, fwater_hi);
Alexander Lam18b21902011-01-03 13:28:56 -05004372
Chris Wilsond2102462011-01-24 17:43:27 +00004373 if (HAS_FW_BLC(dev)) {
4374 if (enabled) {
4375 if (IS_I945G(dev) || IS_I945GM(dev))
4376 I915_WRITE(FW_BLC_SELF,
4377 FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
4378 else if (IS_I915GM(dev))
4379 I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
4380 DRM_DEBUG_KMS("memory self refresh enabled\n");
4381 } else
4382 DRM_DEBUG_KMS("memory self refresh disabled\n");
4383 }
Shaohua Li7662c8b2009-06-26 11:23:55 +08004384}
4385
Chris Wilsond2102462011-01-24 17:43:27 +00004386static void i830_update_wm(struct drm_device *dev)
Shaohua Li7662c8b2009-06-26 11:23:55 +08004387{
4388 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004389 struct drm_crtc *crtc;
4390 uint32_t fwater_lo;
Jesse Barnesdff33cf2009-07-14 10:15:56 -07004391 int planea_wm;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004392
Chris Wilsond2102462011-01-24 17:43:27 +00004393 crtc = single_enabled_crtc(dev);
4394 if (crtc == NULL)
4395 return;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004396
Chris Wilsond2102462011-01-24 17:43:27 +00004397 planea_wm = intel_calculate_wm(crtc->mode.clock, &i830_wm_info,
4398 dev_priv->display.get_fifo_size(dev, 0),
4399 crtc->fb->bits_per_pixel / 8,
4400 latency_ns);
4401 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
Jesse Barnesf3601322009-07-22 12:54:59 -07004402 fwater_lo |= (3<<8) | planea_wm;
4403
Zhao Yakui28c97732009-10-09 11:39:41 +08004404 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004405
4406 I915_WRITE(FW_BLC, fwater_lo);
4407}
4408
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004409#define ILK_LP0_PLANE_LATENCY 700
Zhao Yakuic936f442010-06-12 14:32:26 +08004410#define ILK_LP0_CURSOR_LATENCY 1300
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004411
Jesse Barnesb79d4992010-12-21 13:10:23 -08004412/*
4413 * Check the wm result.
4414 *
4415 * If any calculated watermark values is larger than the maximum value that
4416 * can be programmed into the associated watermark register, that watermark
4417 * must be disabled.
4418 */
4419static bool ironlake_check_srwm(struct drm_device *dev, int level,
4420 int fbc_wm, int display_wm, int cursor_wm,
4421 const struct intel_watermark_params *display,
4422 const struct intel_watermark_params *cursor)
4423{
4424 struct drm_i915_private *dev_priv = dev->dev_private;
4425
4426 DRM_DEBUG_KMS("watermark %d: display plane %d, fbc lines %d,"
4427 " cursor %d\n", level, display_wm, fbc_wm, cursor_wm);
4428
4429 if (fbc_wm > SNB_FBC_MAX_SRWM) {
4430 DRM_DEBUG_KMS("fbc watermark(%d) is too large(%d), disabling wm%d+\n",
4431 fbc_wm, SNB_FBC_MAX_SRWM, level);
4432
4433 /* fbc has it's own way to disable FBC WM */
4434 I915_WRITE(DISP_ARB_CTL,
4435 I915_READ(DISP_ARB_CTL) | DISP_FBC_WM_DIS);
4436 return false;
4437 }
4438
4439 if (display_wm > display->max_wm) {
4440 DRM_DEBUG_KMS("display watermark(%d) is too large(%d), disabling wm%d+\n",
4441 display_wm, SNB_DISPLAY_MAX_SRWM, level);
4442 return false;
4443 }
4444
4445 if (cursor_wm > cursor->max_wm) {
4446 DRM_DEBUG_KMS("cursor watermark(%d) is too large(%d), disabling wm%d+\n",
4447 cursor_wm, SNB_CURSOR_MAX_SRWM, level);
4448 return false;
4449 }
4450
4451 if (!(fbc_wm || display_wm || cursor_wm)) {
4452 DRM_DEBUG_KMS("latency %d is 0, disabling wm%d+\n", level, level);
4453 return false;
4454 }
4455
4456 return true;
4457}
4458
4459/*
4460 * Compute watermark values of WM[1-3],
4461 */
Chris Wilsond2102462011-01-24 17:43:27 +00004462static bool ironlake_compute_srwm(struct drm_device *dev, int level, int plane,
4463 int latency_ns,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004464 const struct intel_watermark_params *display,
4465 const struct intel_watermark_params *cursor,
4466 int *fbc_wm, int *display_wm, int *cursor_wm)
4467{
Chris Wilsond2102462011-01-24 17:43:27 +00004468 struct drm_crtc *crtc;
Jesse Barnesb79d4992010-12-21 13:10:23 -08004469 unsigned long line_time_us;
Chris Wilsond2102462011-01-24 17:43:27 +00004470 int hdisplay, htotal, pixel_size, clock;
Jesse Barnesb79d4992010-12-21 13:10:23 -08004471 int line_count, line_size;
4472 int small, large;
4473 int entries;
4474
4475 if (!latency_ns) {
4476 *fbc_wm = *display_wm = *cursor_wm = 0;
4477 return false;
4478 }
4479
Chris Wilsond2102462011-01-24 17:43:27 +00004480 crtc = intel_get_crtc_for_plane(dev, plane);
4481 hdisplay = crtc->mode.hdisplay;
4482 htotal = crtc->mode.htotal;
4483 clock = crtc->mode.clock;
4484 pixel_size = crtc->fb->bits_per_pixel / 8;
4485
Jesse Barnesb79d4992010-12-21 13:10:23 -08004486 line_time_us = (htotal * 1000) / clock;
4487 line_count = (latency_ns / line_time_us + 1000) / 1000;
4488 line_size = hdisplay * pixel_size;
4489
4490 /* Use the minimum of the small and large buffer method for primary */
4491 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
4492 large = line_count * line_size;
4493
4494 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
4495 *display_wm = entries + display->guard_size;
4496
4497 /*
4498 * Spec says:
4499 * FBC WM = ((Final Primary WM * 64) / number of bytes per line) + 2
4500 */
4501 *fbc_wm = DIV_ROUND_UP(*display_wm * 64, line_size) + 2;
4502
4503 /* calculate the self-refresh watermark for display cursor */
4504 entries = line_count * pixel_size * 64;
4505 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
4506 *cursor_wm = entries + cursor->guard_size;
4507
4508 return ironlake_check_srwm(dev, level,
4509 *fbc_wm, *display_wm, *cursor_wm,
4510 display, cursor);
4511}
4512
Chris Wilsond2102462011-01-24 17:43:27 +00004513static void ironlake_update_wm(struct drm_device *dev)
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004514{
4515 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsond2102462011-01-24 17:43:27 +00004516 int fbc_wm, plane_wm, cursor_wm;
4517 unsigned int enabled;
Zhao Yakuic936f442010-06-12 14:32:26 +08004518
Chris Wilson4ed765f2010-09-11 10:46:47 +01004519 enabled = 0;
Chris Wilson9f405102011-05-12 22:17:14 +01004520 if (g4x_compute_wm0(dev, 0,
4521 &ironlake_display_wm_info,
4522 ILK_LP0_PLANE_LATENCY,
4523 &ironlake_cursor_wm_info,
4524 ILK_LP0_CURSOR_LATENCY,
4525 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01004526 I915_WRITE(WM0_PIPEA_ILK,
4527 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
4528 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
4529 " plane %d, " "cursor: %d\n",
4530 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004531 enabled |= 1;
Zhao Yakuic936f442010-06-12 14:32:26 +08004532 }
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004533
Chris Wilson9f405102011-05-12 22:17:14 +01004534 if (g4x_compute_wm0(dev, 1,
4535 &ironlake_display_wm_info,
4536 ILK_LP0_PLANE_LATENCY,
4537 &ironlake_cursor_wm_info,
4538 ILK_LP0_CURSOR_LATENCY,
4539 &plane_wm, &cursor_wm)) {
Chris Wilson4ed765f2010-09-11 10:46:47 +01004540 I915_WRITE(WM0_PIPEB_ILK,
4541 (plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm);
4542 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
4543 " plane %d, cursor: %d\n",
4544 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004545 enabled |= 2;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004546 }
4547
4548 /*
4549 * Calculate and update the self-refresh watermark only when one
4550 * display plane is used.
4551 */
Jesse Barnesb79d4992010-12-21 13:10:23 -08004552 I915_WRITE(WM3_LP_ILK, 0);
4553 I915_WRITE(WM2_LP_ILK, 0);
4554 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004555
Chris Wilsond2102462011-01-24 17:43:27 +00004556 if (!single_plane_enabled(enabled))
Jesse Barnesb79d4992010-12-21 13:10:23 -08004557 return;
Chris Wilsond2102462011-01-24 17:43:27 +00004558 enabled = ffs(enabled) - 1;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08004559
Jesse Barnesb79d4992010-12-21 13:10:23 -08004560 /* WM1 */
Chris Wilsond2102462011-01-24 17:43:27 +00004561 if (!ironlake_compute_srwm(dev, 1, enabled,
4562 ILK_READ_WM1_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004563 &ironlake_display_srwm_info,
4564 &ironlake_cursor_srwm_info,
4565 &fbc_wm, &plane_wm, &cursor_wm))
4566 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01004567
Jesse Barnesb79d4992010-12-21 13:10:23 -08004568 I915_WRITE(WM1_LP_ILK,
4569 WM1_LP_SR_EN |
4570 (ILK_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4571 (fbc_wm << WM1_LP_FBC_SHIFT) |
4572 (plane_wm << WM1_LP_SR_SHIFT) |
4573 cursor_wm);
Chris Wilson4ed765f2010-09-11 10:46:47 +01004574
Jesse Barnesb79d4992010-12-21 13:10:23 -08004575 /* WM2 */
Chris Wilsond2102462011-01-24 17:43:27 +00004576 if (!ironlake_compute_srwm(dev, 2, enabled,
4577 ILK_READ_WM2_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004578 &ironlake_display_srwm_info,
4579 &ironlake_cursor_srwm_info,
4580 &fbc_wm, &plane_wm, &cursor_wm))
4581 return;
Chris Wilson4ed765f2010-09-11 10:46:47 +01004582
Jesse Barnesb79d4992010-12-21 13:10:23 -08004583 I915_WRITE(WM2_LP_ILK,
4584 WM2_LP_EN |
4585 (ILK_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4586 (fbc_wm << WM1_LP_FBC_SHIFT) |
4587 (plane_wm << WM1_LP_SR_SHIFT) |
4588 cursor_wm);
Yuanhan Liu13982612010-12-15 15:42:31 +08004589
4590 /*
Jesse Barnesb79d4992010-12-21 13:10:23 -08004591 * WM3 is unsupported on ILK, probably because we don't have latency
4592 * data for that power state
Yuanhan Liu13982612010-12-15 15:42:31 +08004593 */
Yuanhan Liu13982612010-12-15 15:42:31 +08004594}
4595
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004596void sandybridge_update_wm(struct drm_device *dev)
Yuanhan Liu13982612010-12-15 15:42:31 +08004597{
4598 struct drm_i915_private *dev_priv = dev->dev_private;
Yuanhan Liua0fa62d2010-12-23 16:35:40 +08004599 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
Jesse Barnes47842642012-01-16 11:57:54 -08004600 u32 val;
Chris Wilsond2102462011-01-24 17:43:27 +00004601 int fbc_wm, plane_wm, cursor_wm;
4602 unsigned int enabled;
Yuanhan Liu13982612010-12-15 15:42:31 +08004603
4604 enabled = 0;
Chris Wilson9f405102011-05-12 22:17:14 +01004605 if (g4x_compute_wm0(dev, 0,
4606 &sandybridge_display_wm_info, latency,
4607 &sandybridge_cursor_wm_info, latency,
4608 &plane_wm, &cursor_wm)) {
Jesse Barnes47842642012-01-16 11:57:54 -08004609 val = I915_READ(WM0_PIPEA_ILK);
4610 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
4611 I915_WRITE(WM0_PIPEA_ILK, val |
4612 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
Yuanhan Liu13982612010-12-15 15:42:31 +08004613 DRM_DEBUG_KMS("FIFO watermarks For pipe A -"
4614 " plane %d, " "cursor: %d\n",
4615 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004616 enabled |= 1;
Yuanhan Liu13982612010-12-15 15:42:31 +08004617 }
4618
Chris Wilson9f405102011-05-12 22:17:14 +01004619 if (g4x_compute_wm0(dev, 1,
4620 &sandybridge_display_wm_info, latency,
4621 &sandybridge_cursor_wm_info, latency,
4622 &plane_wm, &cursor_wm)) {
Jesse Barnes47842642012-01-16 11:57:54 -08004623 val = I915_READ(WM0_PIPEB_ILK);
4624 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
4625 I915_WRITE(WM0_PIPEB_ILK, val |
4626 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
Yuanhan Liu13982612010-12-15 15:42:31 +08004627 DRM_DEBUG_KMS("FIFO watermarks For pipe B -"
4628 " plane %d, cursor: %d\n",
4629 plane_wm, cursor_wm);
Chris Wilsond2102462011-01-24 17:43:27 +00004630 enabled |= 2;
Yuanhan Liu13982612010-12-15 15:42:31 +08004631 }
4632
Jesse Barnesd6c892d2011-10-12 15:36:42 -07004633 /* IVB has 3 pipes */
4634 if (IS_IVYBRIDGE(dev) &&
4635 g4x_compute_wm0(dev, 2,
4636 &sandybridge_display_wm_info, latency,
4637 &sandybridge_cursor_wm_info, latency,
4638 &plane_wm, &cursor_wm)) {
Jesse Barnes47842642012-01-16 11:57:54 -08004639 val = I915_READ(WM0_PIPEC_IVB);
4640 val &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
4641 I915_WRITE(WM0_PIPEC_IVB, val |
4642 ((plane_wm << WM0_PIPE_PLANE_SHIFT) | cursor_wm));
Jesse Barnesd6c892d2011-10-12 15:36:42 -07004643 DRM_DEBUG_KMS("FIFO watermarks For pipe C -"
4644 " plane %d, cursor: %d\n",
4645 plane_wm, cursor_wm);
4646 enabled |= 3;
4647 }
4648
Yuanhan Liu13982612010-12-15 15:42:31 +08004649 /*
4650 * Calculate and update the self-refresh watermark only when one
4651 * display plane is used.
4652 *
4653 * SNB support 3 levels of watermark.
4654 *
4655 * WM1/WM2/WM2 watermarks have to be enabled in the ascending order,
4656 * and disabled in the descending order
4657 *
4658 */
4659 I915_WRITE(WM3_LP_ILK, 0);
4660 I915_WRITE(WM2_LP_ILK, 0);
4661 I915_WRITE(WM1_LP_ILK, 0);
4662
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004663 if (!single_plane_enabled(enabled) ||
4664 dev_priv->sprite_scaling_enabled)
Yuanhan Liu13982612010-12-15 15:42:31 +08004665 return;
Chris Wilsond2102462011-01-24 17:43:27 +00004666 enabled = ffs(enabled) - 1;
Yuanhan Liu13982612010-12-15 15:42:31 +08004667
4668 /* WM1 */
Chris Wilsond2102462011-01-24 17:43:27 +00004669 if (!ironlake_compute_srwm(dev, 1, enabled,
4670 SNB_READ_WM1_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004671 &sandybridge_display_srwm_info,
4672 &sandybridge_cursor_srwm_info,
4673 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08004674 return;
4675
4676 I915_WRITE(WM1_LP_ILK,
4677 WM1_LP_SR_EN |
4678 (SNB_READ_WM1_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4679 (fbc_wm << WM1_LP_FBC_SHIFT) |
4680 (plane_wm << WM1_LP_SR_SHIFT) |
4681 cursor_wm);
4682
4683 /* WM2 */
Chris Wilsond2102462011-01-24 17:43:27 +00004684 if (!ironlake_compute_srwm(dev, 2, enabled,
4685 SNB_READ_WM2_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004686 &sandybridge_display_srwm_info,
4687 &sandybridge_cursor_srwm_info,
4688 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08004689 return;
4690
4691 I915_WRITE(WM2_LP_ILK,
4692 WM2_LP_EN |
4693 (SNB_READ_WM2_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4694 (fbc_wm << WM1_LP_FBC_SHIFT) |
4695 (plane_wm << WM1_LP_SR_SHIFT) |
4696 cursor_wm);
4697
4698 /* WM3 */
Chris Wilsond2102462011-01-24 17:43:27 +00004699 if (!ironlake_compute_srwm(dev, 3, enabled,
4700 SNB_READ_WM3_LATENCY() * 500,
Jesse Barnesb79d4992010-12-21 13:10:23 -08004701 &sandybridge_display_srwm_info,
4702 &sandybridge_cursor_srwm_info,
4703 &fbc_wm, &plane_wm, &cursor_wm))
Yuanhan Liu13982612010-12-15 15:42:31 +08004704 return;
4705
4706 I915_WRITE(WM3_LP_ILK,
4707 WM3_LP_EN |
4708 (SNB_READ_WM3_LATENCY() << WM1_LP_LATENCY_SHIFT) |
4709 (fbc_wm << WM1_LP_FBC_SHIFT) |
4710 (plane_wm << WM1_LP_SR_SHIFT) |
4711 cursor_wm);
4712}
4713
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004714static bool
4715sandybridge_compute_sprite_wm(struct drm_device *dev, int plane,
4716 uint32_t sprite_width, int pixel_size,
4717 const struct intel_watermark_params *display,
4718 int display_latency_ns, int *sprite_wm)
4719{
4720 struct drm_crtc *crtc;
4721 int clock;
4722 int entries, tlb_miss;
4723
4724 crtc = intel_get_crtc_for_plane(dev, plane);
4725 if (crtc->fb == NULL || !crtc->enabled) {
4726 *sprite_wm = display->guard_size;
4727 return false;
4728 }
4729
4730 clock = crtc->mode.clock;
4731
4732 /* Use the small buffer method to calculate the sprite watermark */
4733 entries = ((clock * pixel_size / 1000) * display_latency_ns) / 1000;
4734 tlb_miss = display->fifo_size*display->cacheline_size -
4735 sprite_width * 8;
4736 if (tlb_miss > 0)
4737 entries += tlb_miss;
4738 entries = DIV_ROUND_UP(entries, display->cacheline_size);
4739 *sprite_wm = entries + display->guard_size;
4740 if (*sprite_wm > (int)display->max_wm)
4741 *sprite_wm = display->max_wm;
4742
4743 return true;
4744}
4745
4746static bool
4747sandybridge_compute_sprite_srwm(struct drm_device *dev, int plane,
4748 uint32_t sprite_width, int pixel_size,
4749 const struct intel_watermark_params *display,
4750 int latency_ns, int *sprite_wm)
4751{
4752 struct drm_crtc *crtc;
4753 unsigned long line_time_us;
4754 int clock;
4755 int line_count, line_size;
4756 int small, large;
4757 int entries;
4758
4759 if (!latency_ns) {
4760 *sprite_wm = 0;
4761 return false;
4762 }
4763
4764 crtc = intel_get_crtc_for_plane(dev, plane);
4765 clock = crtc->mode.clock;
4766
4767 line_time_us = (sprite_width * 1000) / clock;
4768 line_count = (latency_ns / line_time_us + 1000) / 1000;
4769 line_size = sprite_width * pixel_size;
4770
4771 /* Use the minimum of the small and large buffer method for primary */
4772 small = ((clock * pixel_size / 1000) * latency_ns) / 1000;
4773 large = line_count * line_size;
4774
4775 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
4776 *sprite_wm = entries + display->guard_size;
4777
4778 return *sprite_wm > 0x3ff ? false : true;
4779}
4780
4781static void sandybridge_update_sprite_wm(struct drm_device *dev, int pipe,
4782 uint32_t sprite_width, int pixel_size)
4783{
4784 struct drm_i915_private *dev_priv = dev->dev_private;
4785 int latency = SNB_READ_WM0_LATENCY() * 100; /* In unit 0.1us */
Jesse Barnes47842642012-01-16 11:57:54 -08004786 u32 val;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004787 int sprite_wm, reg;
4788 int ret;
4789
4790 switch (pipe) {
4791 case 0:
4792 reg = WM0_PIPEA_ILK;
4793 break;
4794 case 1:
4795 reg = WM0_PIPEB_ILK;
4796 break;
4797 case 2:
4798 reg = WM0_PIPEC_IVB;
4799 break;
4800 default:
4801 return; /* bad pipe */
4802 }
4803
4804 ret = sandybridge_compute_sprite_wm(dev, pipe, sprite_width, pixel_size,
4805 &sandybridge_display_wm_info,
4806 latency, &sprite_wm);
4807 if (!ret) {
4808 DRM_DEBUG_KMS("failed to compute sprite wm for pipe %d\n",
4809 pipe);
4810 return;
4811 }
4812
Jesse Barnes47842642012-01-16 11:57:54 -08004813 val = I915_READ(reg);
4814 val &= ~WM0_PIPE_SPRITE_MASK;
4815 I915_WRITE(reg, val | (sprite_wm << WM0_PIPE_SPRITE_SHIFT));
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004816 DRM_DEBUG_KMS("sprite watermarks For pipe %d - %d\n", pipe, sprite_wm);
4817
4818
4819 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
4820 pixel_size,
4821 &sandybridge_display_srwm_info,
4822 SNB_READ_WM1_LATENCY() * 500,
4823 &sprite_wm);
4824 if (!ret) {
4825 DRM_DEBUG_KMS("failed to compute sprite lp1 wm on pipe %d\n",
4826 pipe);
4827 return;
4828 }
4829 I915_WRITE(WM1S_LP_ILK, sprite_wm);
4830
4831 /* Only IVB has two more LP watermarks for sprite */
4832 if (!IS_IVYBRIDGE(dev))
4833 return;
4834
4835 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
4836 pixel_size,
4837 &sandybridge_display_srwm_info,
4838 SNB_READ_WM2_LATENCY() * 500,
4839 &sprite_wm);
4840 if (!ret) {
4841 DRM_DEBUG_KMS("failed to compute sprite lp2 wm on pipe %d\n",
4842 pipe);
4843 return;
4844 }
4845 I915_WRITE(WM2S_LP_IVB, sprite_wm);
4846
4847 ret = sandybridge_compute_sprite_srwm(dev, pipe, sprite_width,
4848 pixel_size,
4849 &sandybridge_display_srwm_info,
4850 SNB_READ_WM3_LATENCY() * 500,
4851 &sprite_wm);
4852 if (!ret) {
4853 DRM_DEBUG_KMS("failed to compute sprite lp3 wm on pipe %d\n",
4854 pipe);
4855 return;
4856 }
4857 I915_WRITE(WM3S_LP_IVB, sprite_wm);
4858}
4859
Shaohua Li7662c8b2009-06-26 11:23:55 +08004860/**
4861 * intel_update_watermarks - update FIFO watermark values based on current modes
4862 *
4863 * Calculate watermark values for the various WM regs based on current mode
4864 * and plane configuration.
4865 *
4866 * There are several cases to deal with here:
4867 * - normal (i.e. non-self-refresh)
4868 * - self-refresh (SR) mode
4869 * - lines are large relative to FIFO size (buffer can hold up to 2)
4870 * - lines are small relative to FIFO size (buffer can hold more than 2
4871 * lines), so need to account for TLB latency
4872 *
4873 * The normal calculation is:
4874 * watermark = dotclock * bytes per pixel * latency
4875 * where latency is platform & configuration dependent (we assume pessimal
4876 * values here).
4877 *
4878 * The SR calculation is:
4879 * watermark = (trunc(latency/line time)+1) * surface width *
4880 * bytes per pixel
4881 * where
4882 * line time = htotal / dotclock
Zhao Yakuifa143212010-06-12 14:32:23 +08004883 * surface width = hdisplay for normal plane and 64 for cursor
Shaohua Li7662c8b2009-06-26 11:23:55 +08004884 * and latency is assumed to be high, as above.
4885 *
4886 * The final value programmed to the register should always be rounded up,
4887 * and include an extra 2 entries to account for clock crossings.
4888 *
4889 * We don't use the sprite, so we can ignore that. And on Crestline we have
4890 * to set the non-SR watermarks to 8.
Chris Wilson5eddb702010-09-11 13:48:45 +01004891 */
Shaohua Li7662c8b2009-06-26 11:23:55 +08004892static void intel_update_watermarks(struct drm_device *dev)
4893{
Jesse Barnese70236a2009-09-21 10:42:27 -07004894 struct drm_i915_private *dev_priv = dev->dev_private;
Shaohua Li7662c8b2009-06-26 11:23:55 +08004895
Chris Wilsond2102462011-01-24 17:43:27 +00004896 if (dev_priv->display.update_wm)
4897 dev_priv->display.update_wm(dev);
Shaohua Li7662c8b2009-06-26 11:23:55 +08004898}
4899
Jesse Barnesb840d907f2011-12-13 13:19:38 -08004900void intel_update_sprite_watermarks(struct drm_device *dev, int pipe,
4901 uint32_t sprite_width, int pixel_size)
4902{
4903 struct drm_i915_private *dev_priv = dev->dev_private;
4904
4905 if (dev_priv->display.update_sprite_wm)
4906 dev_priv->display.update_sprite_wm(dev, pipe, sprite_width,
4907 pixel_size);
4908}
4909
Chris Wilsona7615032011-01-12 17:04:08 +00004910static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
4911{
Keith Packard72bbe582011-09-26 16:09:45 -07004912 if (i915_panel_use_ssc >= 0)
4913 return i915_panel_use_ssc != 0;
4914 return dev_priv->lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07004915 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00004916}
4917
Jesse Barnes5a354202011-06-24 12:19:22 -07004918/**
4919 * intel_choose_pipe_bpp_dither - figure out what color depth the pipe should send
4920 * @crtc: CRTC structure
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004921 * @mode: requested mode
Jesse Barnes5a354202011-06-24 12:19:22 -07004922 *
4923 * A pipe may be connected to one or more outputs. Based on the depth of the
4924 * attached framebuffer, choose a good color depth to use on the pipe.
4925 *
4926 * If possible, match the pipe depth to the fb depth. In some cases, this
4927 * isn't ideal, because the connected output supports a lesser or restricted
4928 * set of depths. Resolve that here:
4929 * LVDS typically supports only 6bpc, so clamp down in that case
4930 * HDMI supports only 8bpc or 12bpc, so clamp to 8bpc with dither for 10bpc
4931 * Displays may support a restricted set as well, check EDID and clamp as
4932 * appropriate.
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004933 * DP may want to dither down to 6bpc to fit larger modes
Jesse Barnes5a354202011-06-24 12:19:22 -07004934 *
4935 * RETURNS:
4936 * Dithering requirement (i.e. false if display bpc and pipe bpc match,
4937 * true if they don't match).
4938 */
4939static bool intel_choose_pipe_bpp_dither(struct drm_crtc *crtc,
Adam Jackson3b5c78a2011-12-13 15:41:00 -08004940 unsigned int *pipe_bpp,
4941 struct drm_display_mode *mode)
Jesse Barnes5a354202011-06-24 12:19:22 -07004942{
4943 struct drm_device *dev = crtc->dev;
4944 struct drm_i915_private *dev_priv = dev->dev_private;
4945 struct drm_encoder *encoder;
4946 struct drm_connector *connector;
4947 unsigned int display_bpc = UINT_MAX, bpc;
4948
4949 /* Walk the encoders & connectors on this crtc, get min bpc */
4950 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
4951 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
4952
4953 if (encoder->crtc != crtc)
4954 continue;
4955
4956 if (intel_encoder->type == INTEL_OUTPUT_LVDS) {
4957 unsigned int lvds_bpc;
4958
4959 if ((I915_READ(PCH_LVDS) & LVDS_A3_POWER_MASK) ==
4960 LVDS_A3_POWER_UP)
4961 lvds_bpc = 8;
4962 else
4963 lvds_bpc = 6;
4964
4965 if (lvds_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004966 DRM_DEBUG_KMS("clamping display bpc (was %d) to LVDS (%d)\n", display_bpc, lvds_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004967 display_bpc = lvds_bpc;
4968 }
4969 continue;
4970 }
4971
4972 if (intel_encoder->type == INTEL_OUTPUT_EDP) {
4973 /* Use VBT settings if we have an eDP panel */
4974 unsigned int edp_bpc = dev_priv->edp.bpp / 3;
4975
4976 if (edp_bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004977 DRM_DEBUG_KMS("clamping display bpc (was %d) to eDP (%d)\n", display_bpc, edp_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07004978 display_bpc = edp_bpc;
4979 }
4980 continue;
4981 }
4982
4983 /* Not one of the known troublemakers, check the EDID */
4984 list_for_each_entry(connector, &dev->mode_config.connector_list,
4985 head) {
4986 if (connector->encoder != encoder)
4987 continue;
4988
Jesse Barnes62ac41a2011-07-28 12:55:14 -07004989 /* Don't use an invalid EDID bpc value */
4990 if (connector->display_info.bpc &&
4991 connector->display_info.bpc < display_bpc) {
Adam Jackson82820492011-10-10 16:33:34 -04004992 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 -07004993 display_bpc = connector->display_info.bpc;
4994 }
4995 }
4996
4997 /*
4998 * HDMI is either 12 or 8, so if the display lets 10bpc sneak
4999 * through, clamp it down. (Note: >12bpc will be caught below.)
5000 */
5001 if (intel_encoder->type == INTEL_OUTPUT_HDMI) {
5002 if (display_bpc > 8 && display_bpc < 12) {
Adam Jackson82820492011-10-10 16:33:34 -04005003 DRM_DEBUG_KMS("forcing bpc to 12 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07005004 display_bpc = 12;
5005 } else {
Adam Jackson82820492011-10-10 16:33:34 -04005006 DRM_DEBUG_KMS("forcing bpc to 8 for HDMI\n");
Jesse Barnes5a354202011-06-24 12:19:22 -07005007 display_bpc = 8;
5008 }
5009 }
5010 }
5011
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005012 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
5013 DRM_DEBUG_KMS("Dithering DP to 6bpc\n");
5014 display_bpc = 6;
5015 }
5016
Jesse Barnes5a354202011-06-24 12:19:22 -07005017 /*
5018 * We could just drive the pipe at the highest bpc all the time and
5019 * enable dithering as needed, but that costs bandwidth. So choose
5020 * the minimum value that expresses the full color range of the fb but
5021 * also stays within the max display bpc discovered above.
5022 */
5023
5024 switch (crtc->fb->depth) {
5025 case 8:
5026 bpc = 8; /* since we go through a colormap */
5027 break;
5028 case 15:
5029 case 16:
5030 bpc = 6; /* min is 18bpp */
5031 break;
5032 case 24:
Keith Packard578393c2011-09-05 11:53:21 -07005033 bpc = 8;
Jesse Barnes5a354202011-06-24 12:19:22 -07005034 break;
5035 case 30:
Keith Packard578393c2011-09-05 11:53:21 -07005036 bpc = 10;
Jesse Barnes5a354202011-06-24 12:19:22 -07005037 break;
5038 case 48:
Keith Packard578393c2011-09-05 11:53:21 -07005039 bpc = 12;
Jesse Barnes5a354202011-06-24 12:19:22 -07005040 break;
5041 default:
5042 DRM_DEBUG("unsupported depth, assuming 24 bits\n");
5043 bpc = min((unsigned int)8, display_bpc);
5044 break;
5045 }
5046
Keith Packard578393c2011-09-05 11:53:21 -07005047 display_bpc = min(display_bpc, bpc);
5048
Adam Jackson82820492011-10-10 16:33:34 -04005049 DRM_DEBUG_KMS("setting pipe bpc to %d (max display bpc %d)\n",
5050 bpc, display_bpc);
Jesse Barnes5a354202011-06-24 12:19:22 -07005051
Keith Packard578393c2011-09-05 11:53:21 -07005052 *pipe_bpp = display_bpc * 3;
Jesse Barnes5a354202011-06-24 12:19:22 -07005053
5054 return display_bpc != bpc;
5055}
5056
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005057static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5058{
5059 struct drm_device *dev = crtc->dev;
5060 struct drm_i915_private *dev_priv = dev->dev_private;
5061 int refclk;
5062
5063 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5064 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5065 refclk = dev_priv->lvds_ssc_freq * 1000;
5066 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5067 refclk / 1000);
5068 } else if (!IS_GEN2(dev)) {
5069 refclk = 96000;
5070 } else {
5071 refclk = 48000;
5072 }
5073
5074 return refclk;
5075}
5076
5077static void i9xx_adjust_sdvo_tv_clock(struct drm_display_mode *adjusted_mode,
5078 intel_clock_t *clock)
5079{
5080 /* SDVO TV has fixed PLL values depend on its clock range,
5081 this mirrors vbios setting. */
5082 if (adjusted_mode->clock >= 100000
5083 && adjusted_mode->clock < 140500) {
5084 clock->p1 = 2;
5085 clock->p2 = 10;
5086 clock->n = 3;
5087 clock->m1 = 16;
5088 clock->m2 = 8;
5089 } else if (adjusted_mode->clock >= 140500
5090 && adjusted_mode->clock <= 200000) {
5091 clock->p1 = 1;
5092 clock->p2 = 10;
5093 clock->n = 6;
5094 clock->m1 = 12;
5095 clock->m2 = 8;
5096 }
5097}
5098
Jesse Barnesa7516a02011-12-15 12:30:37 -08005099static void i9xx_update_pll_dividers(struct drm_crtc *crtc,
5100 intel_clock_t *clock,
5101 intel_clock_t *reduced_clock)
5102{
5103 struct drm_device *dev = crtc->dev;
5104 struct drm_i915_private *dev_priv = dev->dev_private;
5105 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5106 int pipe = intel_crtc->pipe;
5107 u32 fp, fp2 = 0;
5108
5109 if (IS_PINEVIEW(dev)) {
5110 fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
5111 if (reduced_clock)
5112 fp2 = (1 << reduced_clock->n) << 16 |
5113 reduced_clock->m1 << 8 | reduced_clock->m2;
5114 } else {
5115 fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
5116 if (reduced_clock)
5117 fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
5118 reduced_clock->m2;
5119 }
5120
5121 I915_WRITE(FP0(pipe), fp);
5122
5123 intel_crtc->lowfreq_avail = false;
5124 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
5125 reduced_clock && i915_powersave) {
5126 I915_WRITE(FP1(pipe), fp2);
5127 intel_crtc->lowfreq_avail = true;
5128 } else {
5129 I915_WRITE(FP1(pipe), fp);
5130 }
5131}
5132
Eric Anholtf564048e2011-03-30 13:01:02 -07005133static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
5134 struct drm_display_mode *mode,
5135 struct drm_display_mode *adjusted_mode,
5136 int x, int y,
5137 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005138{
5139 struct drm_device *dev = crtc->dev;
5140 struct drm_i915_private *dev_priv = dev->dev_private;
5141 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5142 int pipe = intel_crtc->pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07005143 int plane = intel_crtc->plane;
Eric Anholtc751ce42010-03-25 11:48:48 -07005144 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005145 intel_clock_t clock, reduced_clock;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01005146 u32 dpll, dspcntr, pipeconf, vsyncshift;
Jesse Barnes652c3932009-08-17 13:31:43 -07005147 bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005148 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005149 struct drm_mode_config *mode_config = &dev->mode_config;
Chris Wilson5eddb702010-09-11 13:48:45 +01005150 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005151 const intel_limit_t *limit;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005152 int ret;
Eric Anholtfae14982011-03-30 13:01:09 -07005153 u32 temp;
Bryan Freedaa9b5002011-01-12 13:43:19 -08005154 u32 lvds_sync = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005155
Chris Wilson5eddb702010-09-11 13:48:45 +01005156 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5157 if (encoder->base.crtc != crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08005158 continue;
5159
Chris Wilson5eddb702010-09-11 13:48:45 +01005160 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005161 case INTEL_OUTPUT_LVDS:
5162 is_lvds = true;
5163 break;
5164 case INTEL_OUTPUT_SDVO:
Eric Anholt7d573822009-01-02 13:33:00 -08005165 case INTEL_OUTPUT_HDMI:
Jesse Barnes79e53942008-11-07 14:24:08 -08005166 is_sdvo = true;
Chris Wilson5eddb702010-09-11 13:48:45 +01005167 if (encoder->needs_tv_clock)
Jesse Barnese2f0ba92009-02-02 15:11:52 -08005168 is_tv = true;
Jesse Barnes79e53942008-11-07 14:24:08 -08005169 break;
5170 case INTEL_OUTPUT_DVO:
5171 is_dvo = true;
5172 break;
5173 case INTEL_OUTPUT_TVOUT:
5174 is_tv = true;
5175 break;
5176 case INTEL_OUTPUT_ANALOG:
5177 is_crt = true;
5178 break;
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005179 case INTEL_OUTPUT_DISPLAYPORT:
5180 is_dp = true;
5181 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005182 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005183
Eric Anholtc751ce42010-03-25 11:48:48 -07005184 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005185 }
5186
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005187 refclk = i9xx_get_refclk(crtc, num_connectors);
Jesse Barnes79e53942008-11-07 14:24:08 -08005188
Ma Lingd4906092009-03-18 20:13:27 +08005189 /*
5190 * Returns a set of divisors for the desired target clock with the given
5191 * refclk, or FALSE. The returned values represent the clock equation:
5192 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5193 */
Chris Wilson1b894b52010-12-14 20:04:54 +00005194 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08005195 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5196 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005197 if (!ok) {
5198 DRM_ERROR("Couldn't find PLL settings for mode!\n");
Eric Anholtf564048e2011-03-30 13:01:02 -07005199 return -EINVAL;
5200 }
5201
5202 /* Ensure that the cursor is valid for the new mode before changing... */
5203 intel_crtc_update_cursor(crtc, true);
5204
5205 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08005206 /*
5207 * Ensure we match the reduced clock's P to the target clock.
5208 * If the clocks don't match, we can't switch the display clock
5209 * by using the FP0/FP1. In such case we will disable the LVDS
5210 * downclock feature.
5211 */
Eric Anholtf564048e2011-03-30 13:01:02 -07005212 has_reduced_clock = limit->find_pll(limit, crtc,
5213 dev_priv->lvds_downclock,
5214 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08005215 &clock,
Eric Anholtf564048e2011-03-30 13:01:02 -07005216 &reduced_clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07005217 }
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005218
5219 if (is_sdvo && is_tv)
5220 i9xx_adjust_sdvo_tv_clock(adjusted_mode, &clock);
Eric Anholtf564048e2011-03-30 13:01:02 -07005221
Jesse Barnesa7516a02011-12-15 12:30:37 -08005222 i9xx_update_pll_dividers(crtc, &clock, has_reduced_clock ?
5223 &reduced_clock : NULL);
Eric Anholtf564048e2011-03-30 13:01:02 -07005224
Eric Anholt929c77f2011-03-30 13:01:04 -07005225 dpll = DPLL_VGA_MODE_DIS;
Eric Anholtf564048e2011-03-30 13:01:02 -07005226
5227 if (!IS_GEN2(dev)) {
5228 if (is_lvds)
5229 dpll |= DPLLB_MODE_LVDS;
5230 else
5231 dpll |= DPLLB_MODE_DAC_SERIAL;
5232 if (is_sdvo) {
5233 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5234 if (pixel_multiplier > 1) {
5235 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
5236 dpll |= (pixel_multiplier - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
Eric Anholtf564048e2011-03-30 13:01:02 -07005237 }
5238 dpll |= DPLL_DVO_HIGH_SPEED;
5239 }
Eric Anholt929c77f2011-03-30 13:01:04 -07005240 if (is_dp)
Eric Anholtf564048e2011-03-30 13:01:02 -07005241 dpll |= DPLL_DVO_HIGH_SPEED;
5242
5243 /* compute bitmask from p1 value */
5244 if (IS_PINEVIEW(dev))
5245 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5246 else {
5247 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholtf564048e2011-03-30 13:01:02 -07005248 if (IS_G4X(dev) && has_reduced_clock)
5249 dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5250 }
5251 switch (clock.p2) {
5252 case 5:
5253 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5254 break;
5255 case 7:
5256 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5257 break;
5258 case 10:
5259 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5260 break;
5261 case 14:
5262 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5263 break;
5264 }
Eric Anholt929c77f2011-03-30 13:01:04 -07005265 if (INTEL_INFO(dev)->gen >= 4)
Eric Anholtf564048e2011-03-30 13:01:02 -07005266 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5267 } else {
5268 if (is_lvds) {
5269 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5270 } else {
5271 if (clock.p1 == 2)
5272 dpll |= PLL_P1_DIVIDE_BY_TWO;
5273 else
5274 dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5275 if (clock.p2 == 4)
5276 dpll |= PLL_P2_DIVIDE_BY_4;
5277 }
5278 }
5279
5280 if (is_sdvo && is_tv)
5281 dpll |= PLL_REF_INPUT_TVCLKINBC;
5282 else if (is_tv)
5283 /* XXX: just matching BIOS for now */
5284 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
5285 dpll |= 3;
5286 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5287 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5288 else
5289 dpll |= PLL_REF_INPUT_DREFCLK;
5290
5291 /* setup pipeconf */
5292 pipeconf = I915_READ(PIPECONF(pipe));
5293
5294 /* Set up the display plane register */
5295 dspcntr = DISPPLANE_GAMMA_ENABLE;
5296
Eric Anholt929c77f2011-03-30 13:01:04 -07005297 if (pipe == 0)
5298 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
5299 else
5300 dspcntr |= DISPPLANE_SEL_PIPE_B;
Eric Anholtf564048e2011-03-30 13:01:02 -07005301
5302 if (pipe == 0 && INTEL_INFO(dev)->gen < 4) {
5303 /* Enable pixel doubling when the dot clock is > 90% of the (display)
5304 * core speed.
5305 *
5306 * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
5307 * pipe == 0 check?
5308 */
5309 if (mode->clock >
5310 dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
5311 pipeconf |= PIPECONF_DOUBLE_WIDE;
5312 else
5313 pipeconf &= ~PIPECONF_DOUBLE_WIDE;
5314 }
5315
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005316 /* default to 8bpc */
5317 pipeconf &= ~(PIPECONF_BPP_MASK | PIPECONF_DITHER_EN);
5318 if (is_dp) {
5319 if (mode->private_flags & INTEL_MODE_DP_FORCE_6BPC) {
5320 pipeconf |= PIPECONF_BPP_6 |
5321 PIPECONF_DITHER_EN |
5322 PIPECONF_DITHER_TYPE_SP;
5323 }
5324 }
5325
Eric Anholt929c77f2011-03-30 13:01:04 -07005326 dpll |= DPLL_VCO_ENABLE;
Eric Anholtf564048e2011-03-30 13:01:02 -07005327
5328 DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
5329 drm_mode_debug_printmodeline(mode);
5330
Eric Anholtfae14982011-03-30 13:01:09 -07005331 I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
Eric Anholtf564048e2011-03-30 13:01:02 -07005332
Eric Anholtfae14982011-03-30 13:01:09 -07005333 POSTING_READ(DPLL(pipe));
Eric Anholtc713bb02011-03-30 13:01:05 -07005334 udelay(150);
Eric Anholtf564048e2011-03-30 13:01:02 -07005335
Eric Anholtf564048e2011-03-30 13:01:02 -07005336 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
5337 * This is an exception to the general rule that mode_set doesn't turn
5338 * things on.
5339 */
5340 if (is_lvds) {
Eric Anholtfae14982011-03-30 13:01:09 -07005341 temp = I915_READ(LVDS);
Eric Anholtf564048e2011-03-30 13:01:02 -07005342 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
5343 if (pipe == 1) {
Eric Anholt929c77f2011-03-30 13:01:04 -07005344 temp |= LVDS_PIPEB_SELECT;
Eric Anholtf564048e2011-03-30 13:01:02 -07005345 } else {
Eric Anholt929c77f2011-03-30 13:01:04 -07005346 temp &= ~LVDS_PIPEB_SELECT;
Eric Anholtf564048e2011-03-30 13:01:02 -07005347 }
5348 /* set the corresponsding LVDS_BORDER bit */
5349 temp |= dev_priv->lvds_border_bits;
5350 /* Set the B0-B3 data pairs corresponding to whether we're going to
5351 * set the DPLLs for dual-channel mode or not.
5352 */
5353 if (clock.p2 == 7)
5354 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
5355 else
5356 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
5357
5358 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
5359 * appropriately here, but we need to look more thoroughly into how
5360 * panels behave in the two modes.
5361 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005362 /* set the dithering flag on LVDS as needed */
5363 if (INTEL_INFO(dev)->gen >= 4) {
Eric Anholtf564048e2011-03-30 13:01:02 -07005364 if (dev_priv->lvds_dither)
5365 temp |= LVDS_ENABLE_DITHER;
5366 else
5367 temp &= ~LVDS_ENABLE_DITHER;
5368 }
5369 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
5370 lvds_sync |= LVDS_HSYNC_POLARITY;
5371 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
5372 lvds_sync |= LVDS_VSYNC_POLARITY;
5373 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
5374 != lvds_sync) {
5375 char flags[2] = "-+";
5376 DRM_INFO("Changing LVDS panel from "
5377 "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
5378 flags[!(temp & LVDS_HSYNC_POLARITY)],
5379 flags[!(temp & LVDS_VSYNC_POLARITY)],
5380 flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
5381 flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
5382 temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
5383 temp |= lvds_sync;
5384 }
Eric Anholtfae14982011-03-30 13:01:09 -07005385 I915_WRITE(LVDS, temp);
Eric Anholtf564048e2011-03-30 13:01:02 -07005386 }
5387
Eric Anholt929c77f2011-03-30 13:01:04 -07005388 if (is_dp) {
Eric Anholtf564048e2011-03-30 13:01:02 -07005389 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Eric Anholtf564048e2011-03-30 13:01:02 -07005390 }
5391
Eric Anholtfae14982011-03-30 13:01:09 -07005392 I915_WRITE(DPLL(pipe), dpll);
Eric Anholtf564048e2011-03-30 13:01:02 -07005393
Eric Anholtc713bb02011-03-30 13:01:05 -07005394 /* Wait for the clocks to stabilize. */
Eric Anholtfae14982011-03-30 13:01:09 -07005395 POSTING_READ(DPLL(pipe));
Eric Anholtc713bb02011-03-30 13:01:05 -07005396 udelay(150);
Eric Anholtf564048e2011-03-30 13:01:02 -07005397
Eric Anholtc713bb02011-03-30 13:01:05 -07005398 if (INTEL_INFO(dev)->gen >= 4) {
5399 temp = 0;
5400 if (is_sdvo) {
5401 temp = intel_mode_get_pixel_multiplier(adjusted_mode);
5402 if (temp > 1)
5403 temp = (temp - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5404 else
5405 temp = 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07005406 }
Eric Anholtc713bb02011-03-30 13:01:05 -07005407 I915_WRITE(DPLL_MD(pipe), temp);
5408 } else {
5409 /* The pixel multiplier can only be updated once the
5410 * DPLL is enabled and the clocks are stable.
5411 *
5412 * So write it again.
5413 */
Eric Anholtfae14982011-03-30 13:01:09 -07005414 I915_WRITE(DPLL(pipe), dpll);
Eric Anholtf564048e2011-03-30 13:01:02 -07005415 }
5416
Jesse Barnesa7516a02011-12-15 12:30:37 -08005417 if (HAS_PIPE_CXSR(dev)) {
5418 if (intel_crtc->lowfreq_avail) {
Eric Anholtf564048e2011-03-30 13:01:02 -07005419 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5420 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005421 } else {
Eric Anholtf564048e2011-03-30 13:01:02 -07005422 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
5423 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
5424 }
5425 }
5426
Keith Packard617cf882012-02-08 13:53:38 -08005427 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Daniel Vetterdbb02572012-01-28 14:49:23 +01005428 if (!IS_GEN2(dev) &&
5429 adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Eric Anholtf564048e2011-03-30 13:01:02 -07005430 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5431 /* the chip adds 2 halflines automatically */
Eric Anholtf564048e2011-03-30 13:01:02 -07005432 adjusted_mode->crtc_vtotal -= 1;
Eric Anholtf564048e2011-03-30 13:01:02 -07005433 adjusted_mode->crtc_vblank_end -= 1;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01005434 vsyncshift = adjusted_mode->crtc_hsync_start
5435 - adjusted_mode->crtc_htotal/2;
5436 } else {
Keith Packard617cf882012-02-08 13:53:38 -08005437 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01005438 vsyncshift = 0;
5439 }
5440
5441 if (!IS_GEN3(dev))
5442 I915_WRITE(VSYNCSHIFT(pipe), vsyncshift);
Eric Anholtf564048e2011-03-30 13:01:02 -07005443
5444 I915_WRITE(HTOTAL(pipe),
5445 (adjusted_mode->crtc_hdisplay - 1) |
5446 ((adjusted_mode->crtc_htotal - 1) << 16));
5447 I915_WRITE(HBLANK(pipe),
5448 (adjusted_mode->crtc_hblank_start - 1) |
5449 ((adjusted_mode->crtc_hblank_end - 1) << 16));
5450 I915_WRITE(HSYNC(pipe),
5451 (adjusted_mode->crtc_hsync_start - 1) |
5452 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5453
5454 I915_WRITE(VTOTAL(pipe),
5455 (adjusted_mode->crtc_vdisplay - 1) |
5456 ((adjusted_mode->crtc_vtotal - 1) << 16));
5457 I915_WRITE(VBLANK(pipe),
5458 (adjusted_mode->crtc_vblank_start - 1) |
5459 ((adjusted_mode->crtc_vblank_end - 1) << 16));
5460 I915_WRITE(VSYNC(pipe),
5461 (adjusted_mode->crtc_vsync_start - 1) |
5462 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5463
5464 /* pipesrc and dspsize control the size that is scaled from,
5465 * which should always be the user's requested size.
5466 */
Eric Anholt929c77f2011-03-30 13:01:04 -07005467 I915_WRITE(DSPSIZE(plane),
5468 ((mode->vdisplay - 1) << 16) |
5469 (mode->hdisplay - 1));
5470 I915_WRITE(DSPPOS(plane), 0);
Eric Anholtf564048e2011-03-30 13:01:02 -07005471 I915_WRITE(PIPESRC(pipe),
5472 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
5473
Eric Anholtf564048e2011-03-30 13:01:02 -07005474 I915_WRITE(PIPECONF(pipe), pipeconf);
5475 POSTING_READ(PIPECONF(pipe));
Eric Anholt929c77f2011-03-30 13:01:04 -07005476 intel_enable_pipe(dev_priv, pipe, false);
Eric Anholtf564048e2011-03-30 13:01:02 -07005477
5478 intel_wait_for_vblank(dev, pipe);
5479
Eric Anholtf564048e2011-03-30 13:01:02 -07005480 I915_WRITE(DSPCNTR(plane), dspcntr);
5481 POSTING_READ(DSPCNTR(plane));
Keith Packard284d9522011-06-06 17:12:49 -07005482 intel_enable_plane(dev_priv, plane, pipe);
Eric Anholtf564048e2011-03-30 13:01:02 -07005483
5484 ret = intel_pipe_set_base(crtc, x, y, old_fb);
5485
5486 intel_update_watermarks(dev);
5487
Eric Anholtf564048e2011-03-30 13:01:02 -07005488 return ret;
5489}
5490
Keith Packard9fb526d2011-09-26 22:24:57 -07005491/*
5492 * Initialize reference clocks when the driver loads
5493 */
5494void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07005495{
5496 struct drm_i915_private *dev_priv = dev->dev_private;
5497 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005498 struct intel_encoder *encoder;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005499 u32 temp;
5500 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07005501 bool has_cpu_edp = false;
5502 bool has_pch_edp = false;
5503 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07005504 bool has_ck505 = false;
5505 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005506
5507 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07005508 list_for_each_entry(encoder, &mode_config->encoder_list,
5509 base.head) {
5510 switch (encoder->type) {
5511 case INTEL_OUTPUT_LVDS:
5512 has_panel = true;
5513 has_lvds = true;
5514 break;
5515 case INTEL_OUTPUT_EDP:
5516 has_panel = true;
5517 if (intel_encoder_is_pch_edp(&encoder->base))
5518 has_pch_edp = true;
5519 else
5520 has_cpu_edp = true;
5521 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005522 }
5523 }
5524
Keith Packard99eb6a02011-09-26 14:29:12 -07005525 if (HAS_PCH_IBX(dev)) {
5526 has_ck505 = dev_priv->display_clock_mode;
5527 can_ssc = has_ck505;
5528 } else {
5529 has_ck505 = false;
5530 can_ssc = true;
5531 }
5532
5533 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
5534 has_panel, has_lvds, has_pch_edp, has_cpu_edp,
5535 has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07005536
5537 /* Ironlake: try to setup display ref clock before DPLL
5538 * enabling. This is only under driver's control after
5539 * PCH B stepping, previous chipset stepping should be
5540 * ignoring this setting.
5541 */
5542 temp = I915_READ(PCH_DREF_CONTROL);
5543 /* Always enable nonspread source */
5544 temp &= ~DREF_NONSPREAD_SOURCE_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005545
Keith Packard99eb6a02011-09-26 14:29:12 -07005546 if (has_ck505)
5547 temp |= DREF_NONSPREAD_CK505_ENABLE;
5548 else
5549 temp |= DREF_NONSPREAD_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005550
Keith Packard199e5d72011-09-22 12:01:57 -07005551 if (has_panel) {
5552 temp &= ~DREF_SSC_SOURCE_MASK;
5553 temp |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005554
Keith Packard199e5d72011-09-22 12:01:57 -07005555 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07005556 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005557 DRM_DEBUG_KMS("Using SSC on panel\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07005558 temp |= DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07005559 }
Keith Packard199e5d72011-09-22 12:01:57 -07005560
5561 /* Get SSC going before enabling the outputs */
5562 I915_WRITE(PCH_DREF_CONTROL, temp);
5563 POSTING_READ(PCH_DREF_CONTROL);
5564 udelay(200);
5565
Jesse Barnes13d83a62011-08-03 12:59:20 -07005566 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5567
5568 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07005569 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07005570 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07005571 DRM_DEBUG_KMS("Using SSC on eDP\n");
Jesse Barnes13d83a62011-08-03 12:59:20 -07005572 temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005573 }
Jesse Barnes13d83a62011-08-03 12:59:20 -07005574 else
5575 temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07005576 } else
5577 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5578
5579 I915_WRITE(PCH_DREF_CONTROL, temp);
5580 POSTING_READ(PCH_DREF_CONTROL);
5581 udelay(200);
5582 } else {
5583 DRM_DEBUG_KMS("Disabling SSC entirely\n");
5584
5585 temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5586
5587 /* Turn off CPU output */
5588 temp |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5589
5590 I915_WRITE(PCH_DREF_CONTROL, temp);
5591 POSTING_READ(PCH_DREF_CONTROL);
5592 udelay(200);
5593
5594 /* Turn off the SSC source */
5595 temp &= ~DREF_SSC_SOURCE_MASK;
5596 temp |= DREF_SSC_SOURCE_DISABLE;
5597
5598 /* Turn off SSC1 */
5599 temp &= ~ DREF_SSC1_ENABLE;
5600
Jesse Barnes13d83a62011-08-03 12:59:20 -07005601 I915_WRITE(PCH_DREF_CONTROL, temp);
5602 POSTING_READ(PCH_DREF_CONTROL);
5603 udelay(200);
5604 }
5605}
5606
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005607static int ironlake_get_refclk(struct drm_crtc *crtc)
5608{
5609 struct drm_device *dev = crtc->dev;
5610 struct drm_i915_private *dev_priv = dev->dev_private;
5611 struct intel_encoder *encoder;
5612 struct drm_mode_config *mode_config = &dev->mode_config;
5613 struct intel_encoder *edp_encoder = NULL;
5614 int num_connectors = 0;
5615 bool is_lvds = false;
5616
5617 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5618 if (encoder->base.crtc != crtc)
5619 continue;
5620
5621 switch (encoder->type) {
5622 case INTEL_OUTPUT_LVDS:
5623 is_lvds = true;
5624 break;
5625 case INTEL_OUTPUT_EDP:
5626 edp_encoder = encoder;
5627 break;
5628 }
5629 num_connectors++;
5630 }
5631
5632 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
5633 DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
5634 dev_priv->lvds_ssc_freq);
5635 return dev_priv->lvds_ssc_freq * 1000;
5636 }
5637
5638 return 120000;
5639}
5640
Eric Anholtf564048e2011-03-30 13:01:02 -07005641static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
5642 struct drm_display_mode *mode,
5643 struct drm_display_mode *adjusted_mode,
5644 int x, int y,
5645 struct drm_framebuffer *old_fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005646{
5647 struct drm_device *dev = crtc->dev;
5648 struct drm_i915_private *dev_priv = dev->dev_private;
5649 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5650 int pipe = intel_crtc->pipe;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00005651 int plane = intel_crtc->plane;
Jesse Barnes79e53942008-11-07 14:24:08 -08005652 int refclk, num_connectors = 0;
5653 intel_clock_t clock, reduced_clock;
5654 u32 dpll, fp = 0, fp2 = 0, dspcntr, pipeconf;
Eric Anholta07d6782011-03-30 13:01:08 -07005655 bool ok, has_reduced_clock = false, is_sdvo = false;
Jesse Barnes79e53942008-11-07 14:24:08 -08005656 bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
5657 struct intel_encoder *has_edp_encoder = NULL;
5658 struct drm_mode_config *mode_config = &dev->mode_config;
5659 struct intel_encoder *encoder;
5660 const intel_limit_t *limit;
5661 int ret;
5662 struct fdi_m_n m_n = {0};
Eric Anholtfae14982011-03-30 13:01:09 -07005663 u32 temp;
Jesse Barnes79e53942008-11-07 14:24:08 -08005664 u32 lvds_sync = 0;
Jesse Barnes5a354202011-06-24 12:19:22 -07005665 int target_clock, pixel_multiplier, lane, link_bw, factor;
5666 unsigned int pipe_bpp;
5667 bool dither;
Jesse Barnes79e53942008-11-07 14:24:08 -08005668
Jesse Barnes79e53942008-11-07 14:24:08 -08005669 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
5670 if (encoder->base.crtc != crtc)
5671 continue;
5672
5673 switch (encoder->type) {
5674 case INTEL_OUTPUT_LVDS:
5675 is_lvds = true;
5676 break;
5677 case INTEL_OUTPUT_SDVO:
5678 case INTEL_OUTPUT_HDMI:
5679 is_sdvo = true;
5680 if (encoder->needs_tv_clock)
5681 is_tv = true;
5682 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005683 case INTEL_OUTPUT_TVOUT:
5684 is_tv = true;
5685 break;
5686 case INTEL_OUTPUT_ANALOG:
5687 is_crt = true;
5688 break;
5689 case INTEL_OUTPUT_DISPLAYPORT:
5690 is_dp = true;
5691 break;
5692 case INTEL_OUTPUT_EDP:
5693 has_edp_encoder = encoder;
5694 break;
5695 }
5696
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005697 num_connectors++;
5698 }
5699
Jesse Barnesd9d444c2011-09-02 13:03:05 -07005700 refclk = ironlake_get_refclk(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08005701
5702 /*
5703 * Returns a set of divisors for the desired target clock with the given
5704 * refclk, or FALSE. The returned values represent the clock equation:
5705 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
5706 */
5707 limit = intel_limit(crtc, refclk);
Sean Paulcec2f352012-01-10 15:09:36 -08005708 ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
5709 &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08005710 if (!ok) {
5711 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5712 return -EINVAL;
5713 }
5714
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005715 /* Ensure that the cursor is valid for the new mode before changing... */
Chris Wilson6b383a72010-09-13 13:54:26 +01005716 intel_crtc_update_cursor(crtc, true);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01005717
Zhao Yakuiddc90032010-01-06 22:05:56 +08005718 if (is_lvds && dev_priv->lvds_downclock_avail) {
Sean Paulcec2f352012-01-10 15:09:36 -08005719 /*
5720 * Ensure we match the reduced clock's P to the target clock.
5721 * If the clocks don't match, we can't switch the display clock
5722 * by using the FP0/FP1. In such case we will disable the LVDS
5723 * downclock feature.
5724 */
Zhao Yakuiddc90032010-01-06 22:05:56 +08005725 has_reduced_clock = limit->find_pll(limit, crtc,
Chris Wilson5eddb702010-09-11 13:48:45 +01005726 dev_priv->lvds_downclock,
5727 refclk,
Sean Paulcec2f352012-01-10 15:09:36 -08005728 &clock,
Chris Wilson5eddb702010-09-11 13:48:45 +01005729 &reduced_clock);
Jesse Barnes652c3932009-08-17 13:31:43 -07005730 }
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08005731 /* SDVO TV has fixed PLL values depend on its clock range,
5732 this mirrors vbios setting. */
5733 if (is_sdvo && is_tv) {
5734 if (adjusted_mode->clock >= 100000
Chris Wilson5eddb702010-09-11 13:48:45 +01005735 && adjusted_mode->clock < 140500) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08005736 clock.p1 = 2;
5737 clock.p2 = 10;
5738 clock.n = 3;
5739 clock.m1 = 16;
5740 clock.m2 = 8;
5741 } else if (adjusted_mode->clock >= 140500
Chris Wilson5eddb702010-09-11 13:48:45 +01005742 && adjusted_mode->clock <= 200000) {
Zhenyu Wang7026d4a2009-03-24 14:02:43 +08005743 clock.p1 = 1;
5744 clock.p2 = 10;
5745 clock.n = 6;
5746 clock.m1 = 12;
5747 clock.m2 = 8;
5748 }
5749 }
5750
Zhenyu Wang2c072452009-06-05 15:38:42 +08005751 /* FDI link */
Eric Anholt8febb292011-03-30 13:01:07 -07005752 pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5753 lane = 0;
5754 /* CPU eDP doesn't require FDI link, so just set DP M/N
5755 according to current link config */
5756 if (has_edp_encoder &&
5757 !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
5758 target_clock = mode->clock;
5759 intel_edp_link_config(has_edp_encoder,
5760 &lane, &link_bw);
5761 } else {
5762 /* [e]DP over FDI requires target mode clock
5763 instead of link clock */
5764 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005765 target_clock = mode->clock;
Eric Anholt8febb292011-03-30 13:01:07 -07005766 else
5767 target_clock = adjusted_mode->clock;
Chris Wilson021357a2010-09-07 20:54:59 +01005768
Eric Anholt8febb292011-03-30 13:01:07 -07005769 /* FDI is a binary signal running at ~2.7GHz, encoding
5770 * each output octet as 10 bits. The actual frequency
5771 * is stored as a divider into a 100MHz clock, and the
5772 * mode pixel clock is stored in units of 1KHz.
5773 * Hence the bw of each lane in terms of the mode signal
5774 * is:
5775 */
5776 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08005777 }
Zhenyu Wang2c072452009-06-05 15:38:42 +08005778
Eric Anholt8febb292011-03-30 13:01:07 -07005779 /* determine panel color depth */
5780 temp = I915_READ(PIPECONF(pipe));
5781 temp &= ~PIPE_BPC_MASK;
Adam Jackson3b5c78a2011-12-13 15:41:00 -08005782 dither = intel_choose_pipe_bpp_dither(crtc, &pipe_bpp, mode);
Jesse Barnes5a354202011-06-24 12:19:22 -07005783 switch (pipe_bpp) {
5784 case 18:
5785 temp |= PIPE_6BPC;
5786 break;
5787 case 24:
Eric Anholt8febb292011-03-30 13:01:07 -07005788 temp |= PIPE_8BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07005789 break;
Jesse Barnes5a354202011-06-24 12:19:22 -07005790 case 30:
5791 temp |= PIPE_10BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07005792 break;
Jesse Barnes5a354202011-06-24 12:19:22 -07005793 case 36:
5794 temp |= PIPE_12BPC;
Eric Anholt8febb292011-03-30 13:01:07 -07005795 break;
5796 default:
Jesse Barnes62ac41a2011-07-28 12:55:14 -07005797 WARN(1, "intel_choose_pipe_bpp returned invalid value %d\n",
5798 pipe_bpp);
Jesse Barnes5a354202011-06-24 12:19:22 -07005799 temp |= PIPE_8BPC;
5800 pipe_bpp = 24;
5801 break;
Eric Anholt8febb292011-03-30 13:01:07 -07005802 }
5803
Jesse Barnes5a354202011-06-24 12:19:22 -07005804 intel_crtc->bpp = pipe_bpp;
5805 I915_WRITE(PIPECONF(pipe), temp);
5806
Eric Anholt8febb292011-03-30 13:01:07 -07005807 if (!lane) {
5808 /*
5809 * Account for spread spectrum to avoid
5810 * oversubscribing the link. Max center spread
5811 * is 2.5%; use 5% for safety's sake.
5812 */
Jesse Barnes5a354202011-06-24 12:19:22 -07005813 u32 bps = target_clock * intel_crtc->bpp * 21 / 20;
Eric Anholt8febb292011-03-30 13:01:07 -07005814 lane = bps / (link_bw * 8) + 1;
5815 }
5816
5817 intel_crtc->fdi_lanes = lane;
5818
5819 if (pixel_multiplier > 1)
5820 link_bw *= pixel_multiplier;
Jesse Barnes5a354202011-06-24 12:19:22 -07005821 ironlake_compute_m_n(intel_crtc->bpp, lane, target_clock, link_bw,
5822 &m_n);
Eric Anholt8febb292011-03-30 13:01:07 -07005823
Eric Anholta07d6782011-03-30 13:01:08 -07005824 fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
5825 if (has_reduced_clock)
5826 fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
5827 reduced_clock.m2;
Jesse Barnes79e53942008-11-07 14:24:08 -08005828
Chris Wilsonc1858122010-12-03 21:35:48 +00005829 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07005830 factor = 21;
5831 if (is_lvds) {
5832 if ((intel_panel_use_ssc(dev_priv) &&
5833 dev_priv->lvds_ssc_freq == 100) ||
5834 (I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) == LVDS_CLKB_POWER_UP)
5835 factor = 25;
5836 } else if (is_sdvo && is_tv)
5837 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00005838
Jesse Barnescb0e0932011-07-28 14:50:30 -07005839 if (clock.m < factor * clock.n)
Eric Anholt8febb292011-03-30 13:01:07 -07005840 fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00005841
Chris Wilson5eddb702010-09-11 13:48:45 +01005842 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005843
Eric Anholta07d6782011-03-30 13:01:08 -07005844 if (is_lvds)
5845 dpll |= DPLLB_MODE_LVDS;
5846 else
5847 dpll |= DPLLB_MODE_DAC_SERIAL;
5848 if (is_sdvo) {
5849 int pixel_multiplier = intel_mode_get_pixel_multiplier(adjusted_mode);
5850 if (pixel_multiplier > 1) {
5851 dpll |= (pixel_multiplier - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Jesse Barnes79e53942008-11-07 14:24:08 -08005852 }
Eric Anholta07d6782011-03-30 13:01:08 -07005853 dpll |= DPLL_DVO_HIGH_SPEED;
5854 }
5855 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base))
5856 dpll |= DPLL_DVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08005857
Eric Anholta07d6782011-03-30 13:01:08 -07005858 /* compute bitmask from p1 value */
5859 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5860 /* also FPA1 */
5861 dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5862
5863 switch (clock.p2) {
5864 case 5:
5865 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5866 break;
5867 case 7:
5868 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5869 break;
5870 case 10:
5871 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5872 break;
5873 case 14:
5874 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5875 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005876 }
5877
5878 if (is_sdvo && is_tv)
5879 dpll |= PLL_REF_INPUT_TVCLKINBC;
5880 else if (is_tv)
5881 /* XXX: just matching BIOS for now */
5882 /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
5883 dpll |= 3;
Chris Wilsona7615032011-01-12 17:04:08 +00005884 else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Jesse Barnes79e53942008-11-07 14:24:08 -08005885 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5886 else
5887 dpll |= PLL_REF_INPUT_DREFCLK;
5888
5889 /* setup pipeconf */
Chris Wilson5eddb702010-09-11 13:48:45 +01005890 pipeconf = I915_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08005891
5892 /* Set up the display plane register */
5893 dspcntr = DISPPLANE_GAMMA_ENABLE;
5894
Jesse Barnesf7cb34d2011-10-12 10:49:14 -07005895 DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08005896 drm_mode_debug_printmodeline(mode);
5897
Jesse Barnes5c5313c2010-10-07 16:01:11 -07005898 /* PCH eDP needs FDI, but CPU eDP does not */
Jesse Barnes4b645f12011-10-12 09:51:31 -07005899 if (!intel_crtc->no_pll) {
5900 if (!has_edp_encoder ||
5901 intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
5902 I915_WRITE(PCH_FP0(pipe), fp);
5903 I915_WRITE(PCH_DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01005904
Jesse Barnes4b645f12011-10-12 09:51:31 -07005905 POSTING_READ(PCH_DPLL(pipe));
5906 udelay(150);
5907 }
5908 } else {
5909 if (dpll == (I915_READ(PCH_DPLL(0)) & 0x7fffffff) &&
5910 fp == I915_READ(PCH_FP0(0))) {
5911 intel_crtc->use_pll_a = true;
5912 DRM_DEBUG_KMS("using pipe a dpll\n");
5913 } else if (dpll == (I915_READ(PCH_DPLL(1)) & 0x7fffffff) &&
5914 fp == I915_READ(PCH_FP0(1))) {
5915 intel_crtc->use_pll_a = false;
5916 DRM_DEBUG_KMS("using pipe b dpll\n");
5917 } else {
5918 DRM_DEBUG_KMS("no matching PLL configuration for pipe 2\n");
5919 return -EINVAL;
5920 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005921 }
5922
5923 /* The LVDS pin pair needs to be on before the DPLLs are enabled.
5924 * This is an exception to the general rule that mode_set doesn't turn
5925 * things on.
5926 */
5927 if (is_lvds) {
Eric Anholtfae14982011-03-30 13:01:09 -07005928 temp = I915_READ(PCH_LVDS);
Chris Wilson5eddb702010-09-11 13:48:45 +01005929 temp |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
Jesse Barnes7885d202012-01-12 14:51:17 -08005930 if (HAS_PCH_CPT(dev)) {
5931 temp &= ~PORT_TRANS_SEL_MASK;
Jesse Barnes4b645f12011-10-12 09:51:31 -07005932 temp |= PORT_TRANS_SEL_CPT(pipe);
Jesse Barnes7885d202012-01-12 14:51:17 -08005933 } else {
5934 if (pipe == 1)
5935 temp |= LVDS_PIPEB_SELECT;
5936 else
5937 temp &= ~LVDS_PIPEB_SELECT;
5938 }
Jesse Barnes4b645f12011-10-12 09:51:31 -07005939
Zhao Yakuia3e17eb2009-10-10 10:42:37 +08005940 /* set the corresponsding LVDS_BORDER bit */
Chris Wilson5eddb702010-09-11 13:48:45 +01005941 temp |= dev_priv->lvds_border_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -08005942 /* Set the B0-B3 data pairs corresponding to whether we're going to
5943 * set the DPLLs for dual-channel mode or not.
5944 */
5945 if (clock.p2 == 7)
Chris Wilson5eddb702010-09-11 13:48:45 +01005946 temp |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
Jesse Barnes79e53942008-11-07 14:24:08 -08005947 else
Chris Wilson5eddb702010-09-11 13:48:45 +01005948 temp &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
Jesse Barnes79e53942008-11-07 14:24:08 -08005949
5950 /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
5951 * appropriately here, but we need to look more thoroughly into how
5952 * panels behave in the two modes.
5953 */
Bryan Freedaa9b5002011-01-12 13:43:19 -08005954 if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
5955 lvds_sync |= LVDS_HSYNC_POLARITY;
5956 if (adjusted_mode->flags & DRM_MODE_FLAG_NVSYNC)
5957 lvds_sync |= LVDS_VSYNC_POLARITY;
5958 if ((temp & (LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY))
5959 != lvds_sync) {
5960 char flags[2] = "-+";
5961 DRM_INFO("Changing LVDS panel from "
5962 "(%chsync, %cvsync) to (%chsync, %cvsync)\n",
5963 flags[!(temp & LVDS_HSYNC_POLARITY)],
5964 flags[!(temp & LVDS_VSYNC_POLARITY)],
5965 flags[!(lvds_sync & LVDS_HSYNC_POLARITY)],
5966 flags[!(lvds_sync & LVDS_VSYNC_POLARITY)]);
5967 temp &= ~(LVDS_HSYNC_POLARITY | LVDS_VSYNC_POLARITY);
5968 temp |= lvds_sync;
5969 }
Eric Anholtfae14982011-03-30 13:01:09 -07005970 I915_WRITE(PCH_LVDS, temp);
Jesse Barnes79e53942008-11-07 14:24:08 -08005971 }
Jesse Barnes434ed092010-09-07 14:48:06 -07005972
Eric Anholt8febb292011-03-30 13:01:07 -07005973 pipeconf &= ~PIPECONF_DITHER_EN;
5974 pipeconf &= ~PIPECONF_DITHER_TYPE_MASK;
Jesse Barnes5a354202011-06-24 12:19:22 -07005975 if ((is_lvds && dev_priv->lvds_dither) || dither) {
Eric Anholt8febb292011-03-30 13:01:07 -07005976 pipeconf |= PIPECONF_DITHER_EN;
Daniel Vetterf74974c2011-10-11 17:27:51 +02005977 pipeconf |= PIPECONF_DITHER_TYPE_SP;
Jesse Barnes434ed092010-09-07 14:48:06 -07005978 }
Jesse Barnes5c5313c2010-10-07 16:01:11 -07005979 if (is_dp || intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
Keith Packarda4fc5ed2009-04-07 16:16:42 -07005980 intel_dp_set_m_n(crtc, mode, adjusted_mode);
Eric Anholt8febb292011-03-30 13:01:07 -07005981 } else {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08005982 /* For non-DP output, clear any trans DP clock recovery setting.*/
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005983 I915_WRITE(TRANSDATA_M1(pipe), 0);
5984 I915_WRITE(TRANSDATA_N1(pipe), 0);
5985 I915_WRITE(TRANSDPLINK_M1(pipe), 0);
5986 I915_WRITE(TRANSDPLINK_N1(pipe), 0);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08005987 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005988
Jesse Barnes4b645f12011-10-12 09:51:31 -07005989 if (!intel_crtc->no_pll &&
5990 (!has_edp_encoder ||
5991 intel_encoder_is_pch_edp(&has_edp_encoder->base))) {
Eric Anholtfae14982011-03-30 13:01:09 -07005992 I915_WRITE(PCH_DPLL(pipe), dpll);
Chris Wilson5eddb702010-09-11 13:48:45 +01005993
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005994 /* Wait for the clocks to stabilize. */
Eric Anholtfae14982011-03-30 13:01:09 -07005995 POSTING_READ(PCH_DPLL(pipe));
Zhenyu Wang32f9d652009-07-24 01:00:32 +08005996 udelay(150);
5997
Eric Anholt8febb292011-03-30 13:01:07 -07005998 /* The pixel multiplier can only be updated once the
5999 * DPLL is enabled and the clocks are stable.
6000 *
6001 * So write it again.
6002 */
Eric Anholtfae14982011-03-30 13:01:09 -07006003 I915_WRITE(PCH_DPLL(pipe), dpll);
Jesse Barnes79e53942008-11-07 14:24:08 -08006004 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006005
Chris Wilson5eddb702010-09-11 13:48:45 +01006006 intel_crtc->lowfreq_avail = false;
Jesse Barnes4b645f12011-10-12 09:51:31 -07006007 if (!intel_crtc->no_pll) {
6008 if (is_lvds && has_reduced_clock && i915_powersave) {
6009 I915_WRITE(PCH_FP1(pipe), fp2);
6010 intel_crtc->lowfreq_avail = true;
6011 if (HAS_PIPE_CXSR(dev)) {
6012 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6013 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6014 }
6015 } else {
6016 I915_WRITE(PCH_FP1(pipe), fp);
6017 if (HAS_PIPE_CXSR(dev)) {
6018 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
6019 pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
6020 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006021 }
6022 }
6023
Keith Packard617cf882012-02-08 13:53:38 -08006024 pipeconf &= ~PIPECONF_INTERLACE_MASK;
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006025 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Daniel Vetter5def4742012-01-28 14:49:22 +01006026 pipeconf |= PIPECONF_INTERLACED_ILK;
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006027 /* the chip adds 2 halflines automatically */
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006028 adjusted_mode->crtc_vtotal -= 1;
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006029 adjusted_mode->crtc_vblank_end -= 1;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01006030 I915_WRITE(VSYNCSHIFT(pipe),
6031 adjusted_mode->crtc_hsync_start
6032 - adjusted_mode->crtc_htotal/2);
6033 } else {
Keith Packard617cf882012-02-08 13:53:38 -08006034 pipeconf |= PIPECONF_PROGRESSIVE;
Daniel Vetter0529a0d2012-01-28 14:49:24 +01006035 I915_WRITE(VSYNCSHIFT(pipe), 0);
6036 }
Krzysztof Halasa734b4152010-05-25 18:41:46 +02006037
Chris Wilson5eddb702010-09-11 13:48:45 +01006038 I915_WRITE(HTOTAL(pipe),
6039 (adjusted_mode->crtc_hdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006040 ((adjusted_mode->crtc_htotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006041 I915_WRITE(HBLANK(pipe),
6042 (adjusted_mode->crtc_hblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006043 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006044 I915_WRITE(HSYNC(pipe),
6045 (adjusted_mode->crtc_hsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006046 ((adjusted_mode->crtc_hsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006047
6048 I915_WRITE(VTOTAL(pipe),
6049 (adjusted_mode->crtc_vdisplay - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006050 ((adjusted_mode->crtc_vtotal - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006051 I915_WRITE(VBLANK(pipe),
6052 (adjusted_mode->crtc_vblank_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006053 ((adjusted_mode->crtc_vblank_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006054 I915_WRITE(VSYNC(pipe),
6055 (adjusted_mode->crtc_vsync_start - 1) |
Jesse Barnes79e53942008-11-07 14:24:08 -08006056 ((adjusted_mode->crtc_vsync_end - 1) << 16));
Chris Wilson5eddb702010-09-11 13:48:45 +01006057
Eric Anholt8febb292011-03-30 13:01:07 -07006058 /* pipesrc controls the size that is scaled from, which should
6059 * always be the user's requested size.
Jesse Barnes79e53942008-11-07 14:24:08 -08006060 */
Chris Wilson5eddb702010-09-11 13:48:45 +01006061 I915_WRITE(PIPESRC(pipe),
6062 ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
Zhenyu Wang2c072452009-06-05 15:38:42 +08006063
Eric Anholt8febb292011-03-30 13:01:07 -07006064 I915_WRITE(PIPE_DATA_M1(pipe), TU_SIZE(m_n.tu) | m_n.gmch_m);
6065 I915_WRITE(PIPE_DATA_N1(pipe), m_n.gmch_n);
6066 I915_WRITE(PIPE_LINK_M1(pipe), m_n.link_m);
6067 I915_WRITE(PIPE_LINK_N1(pipe), m_n.link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006068
Eric Anholt8febb292011-03-30 13:01:07 -07006069 if (has_edp_encoder &&
6070 !intel_encoder_is_pch_edp(&has_edp_encoder->base)) {
6071 ironlake_set_pll_edp(crtc, adjusted_mode->clock);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006072 }
6073
Chris Wilson5eddb702010-09-11 13:48:45 +01006074 I915_WRITE(PIPECONF(pipe), pipeconf);
6075 POSTING_READ(PIPECONF(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006076
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006077 intel_wait_for_vblank(dev, pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006078
Chris Wilson5eddb702010-09-11 13:48:45 +01006079 I915_WRITE(DSPCNTR(plane), dspcntr);
Jesse Barnesb24e7172011-01-04 15:09:30 -08006080 POSTING_READ(DSPCNTR(plane));
Jesse Barnes79e53942008-11-07 14:24:08 -08006081
Chris Wilson5c3b82e2009-02-11 13:25:09 +00006082 ret = intel_pipe_set_base(crtc, x, y, old_fb);
Shaohua Li7662c8b2009-06-26 11:23:55 +08006083
6084 intel_update_watermarks(dev);
6085
Chris Wilson1f803ee2009-06-06 09:45:59 +01006086 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006087}
6088
Eric Anholtf564048e2011-03-30 13:01:02 -07006089static int intel_crtc_mode_set(struct drm_crtc *crtc,
6090 struct drm_display_mode *mode,
6091 struct drm_display_mode *adjusted_mode,
6092 int x, int y,
6093 struct drm_framebuffer *old_fb)
6094{
6095 struct drm_device *dev = crtc->dev;
6096 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholt0b701d22011-03-30 13:01:03 -07006097 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6098 int pipe = intel_crtc->pipe;
Eric Anholtf564048e2011-03-30 13:01:02 -07006099 int ret;
6100
Eric Anholt0b701d22011-03-30 13:01:03 -07006101 drm_vblank_pre_modeset(dev, pipe);
6102
Eric Anholtf564048e2011-03-30 13:01:02 -07006103 ret = dev_priv->display.crtc_mode_set(crtc, mode, adjusted_mode,
6104 x, y, old_fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08006105 drm_vblank_post_modeset(dev, pipe);
6106
Jesse Barnesd8e70a22011-11-15 10:28:54 -08006107 if (ret)
6108 intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
6109 else
6110 intel_crtc->dpms_mode = DRM_MODE_DPMS_ON;
Keith Packard120eced2011-07-27 01:21:40 -07006111
Jesse Barnes79e53942008-11-07 14:24:08 -08006112 return ret;
6113}
6114
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006115static bool intel_eld_uptodate(struct drm_connector *connector,
6116 int reg_eldv, uint32_t bits_eldv,
6117 int reg_elda, uint32_t bits_elda,
6118 int reg_edid)
6119{
6120 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6121 uint8_t *eld = connector->eld;
6122 uint32_t i;
6123
6124 i = I915_READ(reg_eldv);
6125 i &= bits_eldv;
6126
6127 if (!eld[0])
6128 return !i;
6129
6130 if (!i)
6131 return false;
6132
6133 i = I915_READ(reg_elda);
6134 i &= ~bits_elda;
6135 I915_WRITE(reg_elda, i);
6136
6137 for (i = 0; i < eld[2]; i++)
6138 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
6139 return false;
6140
6141 return true;
6142}
6143
Wu Fengguange0dac652011-09-05 14:25:34 +08006144static void g4x_write_eld(struct drm_connector *connector,
6145 struct drm_crtc *crtc)
6146{
6147 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6148 uint8_t *eld = connector->eld;
6149 uint32_t eldv;
6150 uint32_t len;
6151 uint32_t i;
6152
6153 i = I915_READ(G4X_AUD_VID_DID);
6154
6155 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
6156 eldv = G4X_ELDV_DEVCL_DEVBLC;
6157 else
6158 eldv = G4X_ELDV_DEVCTG;
6159
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006160 if (intel_eld_uptodate(connector,
6161 G4X_AUD_CNTL_ST, eldv,
6162 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
6163 G4X_HDMIW_HDMIEDID))
6164 return;
6165
Wu Fengguange0dac652011-09-05 14:25:34 +08006166 i = I915_READ(G4X_AUD_CNTL_ST);
6167 i &= ~(eldv | G4X_ELD_ADDR);
6168 len = (i >> 9) & 0x1f; /* ELD buffer size */
6169 I915_WRITE(G4X_AUD_CNTL_ST, i);
6170
6171 if (!eld[0])
6172 return;
6173
6174 len = min_t(uint8_t, eld[2], len);
6175 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6176 for (i = 0; i < len; i++)
6177 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
6178
6179 i = I915_READ(G4X_AUD_CNTL_ST);
6180 i |= eldv;
6181 I915_WRITE(G4X_AUD_CNTL_ST, i);
6182}
6183
6184static void ironlake_write_eld(struct drm_connector *connector,
6185 struct drm_crtc *crtc)
6186{
6187 struct drm_i915_private *dev_priv = connector->dev->dev_private;
6188 uint8_t *eld = connector->eld;
6189 uint32_t eldv;
6190 uint32_t i;
6191 int len;
6192 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006193 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08006194 int aud_cntl_st;
6195 int aud_cntrl_st2;
6196
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08006197 if (HAS_PCH_IBX(connector->dev)) {
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006198 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID_A;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006199 aud_config = IBX_AUD_CONFIG_A;
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006200 aud_cntl_st = IBX_AUD_CNTL_ST_A;
6201 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006202 } else {
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006203 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID_A;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006204 aud_config = CPT_AUD_CONFIG_A;
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006205 aud_cntl_st = CPT_AUD_CNTL_ST_A;
6206 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08006207 }
6208
6209 i = to_intel_crtc(crtc)->pipe;
6210 hdmiw_hdmiedid += i * 0x100;
6211 aud_cntl_st += i * 0x100;
Wu Fengguangb6daa022012-01-06 14:41:31 -06006212 aud_config += i * 0x100;
Wu Fengguange0dac652011-09-05 14:25:34 +08006213
6214 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(i));
6215
6216 i = I915_READ(aud_cntl_st);
6217 i = (i >> 29) & 0x3; /* DIP_Port_Select, 0x1 = PortB */
6218 if (!i) {
6219 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
6220 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006221 eldv = IBX_ELD_VALIDB;
6222 eldv |= IBX_ELD_VALIDB << 4;
6223 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08006224 } else {
6225 DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006226 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08006227 }
6228
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006229 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
6230 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
6231 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06006232 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
6233 } else
6234 I915_WRITE(aud_config, 0);
Wu Fengguang3a9627f2011-12-09 20:42:19 +08006235
6236 if (intel_eld_uptodate(connector,
6237 aud_cntrl_st2, eldv,
6238 aud_cntl_st, IBX_ELD_ADDRESS,
6239 hdmiw_hdmiedid))
6240 return;
6241
Wu Fengguange0dac652011-09-05 14:25:34 +08006242 i = I915_READ(aud_cntrl_st2);
6243 i &= ~eldv;
6244 I915_WRITE(aud_cntrl_st2, i);
6245
6246 if (!eld[0])
6247 return;
6248
Wu Fengguange0dac652011-09-05 14:25:34 +08006249 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08006250 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08006251 I915_WRITE(aud_cntl_st, i);
6252
6253 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
6254 DRM_DEBUG_DRIVER("ELD size %d\n", len);
6255 for (i = 0; i < len; i++)
6256 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
6257
6258 i = I915_READ(aud_cntrl_st2);
6259 i |= eldv;
6260 I915_WRITE(aud_cntrl_st2, i);
6261}
6262
6263void intel_write_eld(struct drm_encoder *encoder,
6264 struct drm_display_mode *mode)
6265{
6266 struct drm_crtc *crtc = encoder->crtc;
6267 struct drm_connector *connector;
6268 struct drm_device *dev = encoder->dev;
6269 struct drm_i915_private *dev_priv = dev->dev_private;
6270
6271 connector = drm_select_eld(encoder, mode);
6272 if (!connector)
6273 return;
6274
6275 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6276 connector->base.id,
6277 drm_get_connector_name(connector),
6278 connector->encoder->base.id,
6279 drm_get_encoder_name(connector->encoder));
6280
6281 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
6282
6283 if (dev_priv->display.write_eld)
6284 dev_priv->display.write_eld(connector, crtc);
6285}
6286
Jesse Barnes79e53942008-11-07 14:24:08 -08006287/** Loads the palette/gamma unit for the CRTC with the prepared values */
6288void intel_crtc_load_lut(struct drm_crtc *crtc)
6289{
6290 struct drm_device *dev = crtc->dev;
6291 struct drm_i915_private *dev_priv = dev->dev_private;
6292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006293 int palreg = PALETTE(intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006294 int i;
6295
6296 /* The clocks have to be on to load the palette. */
6297 if (!crtc->enabled)
6298 return;
6299
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006300 /* use legacy palette for Ironlake */
Eric Anholtbad720f2009-10-22 16:11:14 -07006301 if (HAS_PCH_SPLIT(dev))
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006302 palreg = LGC_PALETTE(intel_crtc->pipe);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006303
Jesse Barnes79e53942008-11-07 14:24:08 -08006304 for (i = 0; i < 256; i++) {
6305 I915_WRITE(palreg + 4 * i,
6306 (intel_crtc->lut_r[i] << 16) |
6307 (intel_crtc->lut_g[i] << 8) |
6308 intel_crtc->lut_b[i]);
6309 }
6310}
6311
Chris Wilson560b85b2010-08-07 11:01:38 +01006312static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
6313{
6314 struct drm_device *dev = crtc->dev;
6315 struct drm_i915_private *dev_priv = dev->dev_private;
6316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6317 bool visible = base != 0;
6318 u32 cntl;
6319
6320 if (intel_crtc->cursor_visible == visible)
6321 return;
6322
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006323 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01006324 if (visible) {
6325 /* On these chipsets we can only modify the base whilst
6326 * the cursor is disabled.
6327 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006328 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006329
6330 cntl &= ~(CURSOR_FORMAT_MASK);
6331 /* XXX width must be 64, stride 256 => 0x00 << 28 */
6332 cntl |= CURSOR_ENABLE |
6333 CURSOR_GAMMA_ENABLE |
6334 CURSOR_FORMAT_ARGB;
6335 } else
6336 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006337 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006338
6339 intel_crtc->cursor_visible = visible;
6340}
6341
6342static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
6343{
6344 struct drm_device *dev = crtc->dev;
6345 struct drm_i915_private *dev_priv = dev->dev_private;
6346 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6347 int pipe = intel_crtc->pipe;
6348 bool visible = base != 0;
6349
6350 if (intel_crtc->cursor_visible != visible) {
Jesse Barnes548f2452011-02-17 10:40:53 -08006351 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01006352 if (base) {
6353 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
6354 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6355 cntl |= pipe << 28; /* Connect to correct pipe */
6356 } else {
6357 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6358 cntl |= CURSOR_MODE_DISABLE;
6359 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006360 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01006361
6362 intel_crtc->cursor_visible = visible;
6363 }
6364 /* and commit changes on next vblank */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08006365 I915_WRITE(CURBASE(pipe), base);
Chris Wilson560b85b2010-08-07 11:01:38 +01006366}
6367
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006368static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
6369{
6370 struct drm_device *dev = crtc->dev;
6371 struct drm_i915_private *dev_priv = dev->dev_private;
6372 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6373 int pipe = intel_crtc->pipe;
6374 bool visible = base != 0;
6375
6376 if (intel_crtc->cursor_visible != visible) {
6377 uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
6378 if (base) {
6379 cntl &= ~CURSOR_MODE;
6380 cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
6381 } else {
6382 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
6383 cntl |= CURSOR_MODE_DISABLE;
6384 }
6385 I915_WRITE(CURCNTR_IVB(pipe), cntl);
6386
6387 intel_crtc->cursor_visible = visible;
6388 }
6389 /* and commit changes on next vblank */
6390 I915_WRITE(CURBASE_IVB(pipe), base);
6391}
6392
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006393/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01006394static void intel_crtc_update_cursor(struct drm_crtc *crtc,
6395 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006396{
6397 struct drm_device *dev = crtc->dev;
6398 struct drm_i915_private *dev_priv = dev->dev_private;
6399 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6400 int pipe = intel_crtc->pipe;
6401 int x = intel_crtc->cursor_x;
6402 int y = intel_crtc->cursor_y;
Chris Wilson560b85b2010-08-07 11:01:38 +01006403 u32 base, pos;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006404 bool visible;
6405
6406 pos = 0;
6407
Chris Wilson6b383a72010-09-13 13:54:26 +01006408 if (on && crtc->enabled && crtc->fb) {
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006409 base = intel_crtc->cursor_addr;
6410 if (x > (int) crtc->fb->width)
6411 base = 0;
6412
6413 if (y > (int) crtc->fb->height)
6414 base = 0;
6415 } else
6416 base = 0;
6417
6418 if (x < 0) {
6419 if (x + intel_crtc->cursor_width < 0)
6420 base = 0;
6421
6422 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
6423 x = -x;
6424 }
6425 pos |= x << CURSOR_X_SHIFT;
6426
6427 if (y < 0) {
6428 if (y + intel_crtc->cursor_height < 0)
6429 base = 0;
6430
6431 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
6432 y = -y;
6433 }
6434 pos |= y << CURSOR_Y_SHIFT;
6435
6436 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01006437 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006438 return;
6439
Jesse Barnes65a21cd2011-10-12 11:10:21 -07006440 if (IS_IVYBRIDGE(dev)) {
6441 I915_WRITE(CURPOS_IVB(pipe), pos);
6442 ivb_update_cursor(crtc, base);
6443 } else {
6444 I915_WRITE(CURPOS(pipe), pos);
6445 if (IS_845G(dev) || IS_I865G(dev))
6446 i845_update_cursor(crtc, base);
6447 else
6448 i9xx_update_cursor(crtc, base);
6449 }
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006450
6451 if (visible)
6452 intel_mark_busy(dev, to_intel_framebuffer(crtc->fb)->obj);
6453}
6454
Jesse Barnes79e53942008-11-07 14:24:08 -08006455static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00006456 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08006457 uint32_t handle,
6458 uint32_t width, uint32_t height)
6459{
6460 struct drm_device *dev = crtc->dev;
6461 struct drm_i915_private *dev_priv = dev->dev_private;
6462 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00006463 struct drm_i915_gem_object *obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006464 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006465 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006466
Zhao Yakui28c97732009-10-09 11:39:41 +08006467 DRM_DEBUG_KMS("\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08006468
6469 /* if we want to turn off the cursor ignore width and height */
6470 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08006471 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006472 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00006473 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10006474 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006475 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08006476 }
6477
6478 /* Currently we only support 64x64 cursors */
6479 if (width != 64 || height != 64) {
6480 DRM_ERROR("we currently only support 64x64 cursors\n");
6481 return -EINVAL;
6482 }
6483
Chris Wilson05394f32010-11-08 19:18:58 +00006484 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00006485 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08006486 return -ENOENT;
6487
Chris Wilson05394f32010-11-08 19:18:58 +00006488 if (obj->base.size < width * height * 4) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006489 DRM_ERROR("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10006490 ret = -ENOMEM;
6491 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08006492 }
6493
Dave Airlie71acb5e2008-12-30 20:31:46 +10006494 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006495 mutex_lock(&dev->struct_mutex);
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006496 if (!dev_priv->info->cursor_needs_physical) {
Chris Wilsond9e86c02010-11-10 16:40:20 +00006497 if (obj->tiling_mode) {
6498 DRM_ERROR("cursor cannot be tiled\n");
6499 ret = -EINVAL;
6500 goto fail_locked;
6501 }
6502
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006503 ret = i915_gem_object_pin_to_display_plane(obj, 0, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01006504 if (ret) {
6505 DRM_ERROR("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006506 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006507 }
6508
Chris Wilsond9e86c02010-11-10 16:40:20 +00006509 ret = i915_gem_object_put_fence(obj);
6510 if (ret) {
Chris Wilson2da3b9b2011-04-14 09:41:17 +01006511 DRM_ERROR("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00006512 goto fail_unpin;
6513 }
6514
Chris Wilson05394f32010-11-08 19:18:58 +00006515 addr = obj->gtt_offset;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006516 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006517 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00006518 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01006519 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
6520 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10006521 if (ret) {
6522 DRM_ERROR("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006523 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10006524 }
Chris Wilson05394f32010-11-08 19:18:58 +00006525 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006526 }
6527
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006528 if (IS_GEN2(dev))
Jesse Barnes14b60392009-05-20 16:47:08 -04006529 I915_WRITE(CURSIZE, (height << 12) | width);
6530
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006531 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006532 if (intel_crtc->cursor_bo) {
Kristian Høgsbergb295d1b2009-12-16 15:16:17 -05006533 if (dev_priv->info->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00006534 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10006535 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
6536 } else
6537 i915_gem_object_unpin(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00006538 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006539 }
Jesse Barnes80824002009-09-10 15:28:06 -07006540
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006541 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006542
6543 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00006544 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006545 intel_crtc->cursor_width = width;
6546 intel_crtc->cursor_height = height;
6547
Chris Wilson6b383a72010-09-13 13:54:26 +01006548 intel_crtc_update_cursor(crtc, true);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05006549
Jesse Barnes79e53942008-11-07 14:24:08 -08006550 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01006551fail_unpin:
Chris Wilson05394f32010-11-08 19:18:58 +00006552 i915_gem_object_unpin(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05006553fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10006554 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00006555fail:
Chris Wilson05394f32010-11-08 19:18:58 +00006556 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10006557 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006558}
6559
6560static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
6561{
Jesse Barnes79e53942008-11-07 14:24:08 -08006562 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006563
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01006564 intel_crtc->cursor_x = x;
6565 intel_crtc->cursor_y = y;
Jesse Barnes652c3932009-08-17 13:31:43 -07006566
Chris Wilson6b383a72010-09-13 13:54:26 +01006567 intel_crtc_update_cursor(crtc, true);
Jesse Barnes79e53942008-11-07 14:24:08 -08006568
6569 return 0;
6570}
6571
6572/** Sets the color ramps on behalf of RandR */
6573void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
6574 u16 blue, int regno)
6575{
6576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6577
6578 intel_crtc->lut_r[regno] = red >> 8;
6579 intel_crtc->lut_g[regno] = green >> 8;
6580 intel_crtc->lut_b[regno] = blue >> 8;
6581}
6582
Dave Airlieb8c00ac2009-10-06 13:54:01 +10006583void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
6584 u16 *blue, int regno)
6585{
6586 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6587
6588 *red = intel_crtc->lut_r[regno] << 8;
6589 *green = intel_crtc->lut_g[regno] << 8;
6590 *blue = intel_crtc->lut_b[regno] << 8;
6591}
6592
Jesse Barnes79e53942008-11-07 14:24:08 -08006593static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01006594 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08006595{
James Simmons72034252010-08-03 01:33:19 +01006596 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08006597 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08006598
James Simmons72034252010-08-03 01:33:19 +01006599 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006600 intel_crtc->lut_r[i] = red[i] >> 8;
6601 intel_crtc->lut_g[i] = green[i] >> 8;
6602 intel_crtc->lut_b[i] = blue[i] >> 8;
6603 }
6604
6605 intel_crtc_load_lut(crtc);
6606}
6607
6608/**
6609 * Get a pipe with a simple mode set on it for doing load-based monitor
6610 * detection.
6611 *
6612 * It will be up to the load-detect code to adjust the pipe as appropriate for
Eric Anholtc751ce42010-03-25 11:48:48 -07006613 * its requirements. The pipe will be connected to no other encoders.
Jesse Barnes79e53942008-11-07 14:24:08 -08006614 *
Eric Anholtc751ce42010-03-25 11:48:48 -07006615 * Currently this code will only succeed if there is a pipe with no encoders
Jesse Barnes79e53942008-11-07 14:24:08 -08006616 * configured for it. In the future, it could choose to temporarily disable
6617 * some outputs to free up a pipe for its use.
6618 *
6619 * \return crtc, or NULL if no pipes are available.
6620 */
6621
6622/* VESA 640x480x72Hz mode to set on the pipe */
6623static struct drm_display_mode load_detect_mode = {
6624 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6625 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6626};
6627
Chris Wilsond2dff872011-04-19 08:36:26 +01006628static struct drm_framebuffer *
6629intel_framebuffer_create(struct drm_device *dev,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006630 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilsond2dff872011-04-19 08:36:26 +01006631 struct drm_i915_gem_object *obj)
6632{
6633 struct intel_framebuffer *intel_fb;
6634 int ret;
6635
6636 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6637 if (!intel_fb) {
6638 drm_gem_object_unreference_unlocked(&obj->base);
6639 return ERR_PTR(-ENOMEM);
6640 }
6641
6642 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
6643 if (ret) {
6644 drm_gem_object_unreference_unlocked(&obj->base);
6645 kfree(intel_fb);
6646 return ERR_PTR(ret);
6647 }
6648
6649 return &intel_fb->base;
6650}
6651
6652static u32
6653intel_framebuffer_pitch_for_width(int width, int bpp)
6654{
6655 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
6656 return ALIGN(pitch, 64);
6657}
6658
6659static u32
6660intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
6661{
6662 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
6663 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
6664}
6665
6666static struct drm_framebuffer *
6667intel_framebuffer_create_for_mode(struct drm_device *dev,
6668 struct drm_display_mode *mode,
6669 int depth, int bpp)
6670{
6671 struct drm_i915_gem_object *obj;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006672 struct drm_mode_fb_cmd2 mode_cmd;
Chris Wilsond2dff872011-04-19 08:36:26 +01006673
6674 obj = i915_gem_alloc_object(dev,
6675 intel_framebuffer_size_for_mode(mode, bpp));
6676 if (obj == NULL)
6677 return ERR_PTR(-ENOMEM);
6678
6679 mode_cmd.width = mode->hdisplay;
6680 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08006681 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
6682 bpp);
6683 mode_cmd.pixel_format = 0;
Chris Wilsond2dff872011-04-19 08:36:26 +01006684
6685 return intel_framebuffer_create(dev, &mode_cmd, obj);
6686}
6687
6688static struct drm_framebuffer *
6689mode_fits_in_fbdev(struct drm_device *dev,
6690 struct drm_display_mode *mode)
6691{
6692 struct drm_i915_private *dev_priv = dev->dev_private;
6693 struct drm_i915_gem_object *obj;
6694 struct drm_framebuffer *fb;
6695
6696 if (dev_priv->fbdev == NULL)
6697 return NULL;
6698
6699 obj = dev_priv->fbdev->ifb.obj;
6700 if (obj == NULL)
6701 return NULL;
6702
6703 fb = &dev_priv->fbdev->ifb.base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006704 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
6705 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01006706 return NULL;
6707
Ville Syrjälä01f2c772011-12-20 00:06:49 +02006708 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01006709 return NULL;
6710
6711 return fb;
6712}
6713
Chris Wilson71731882011-04-19 23:10:58 +01006714bool intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
6715 struct drm_connector *connector,
6716 struct drm_display_mode *mode,
Chris Wilson8261b192011-04-19 23:18:09 +01006717 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006718{
6719 struct intel_crtc *intel_crtc;
6720 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01006721 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006722 struct drm_crtc *crtc = NULL;
6723 struct drm_device *dev = encoder->dev;
Chris Wilsond2dff872011-04-19 08:36:26 +01006724 struct drm_framebuffer *old_fb;
Jesse Barnes79e53942008-11-07 14:24:08 -08006725 int i = -1;
6726
Chris Wilsond2dff872011-04-19 08:36:26 +01006727 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6728 connector->base.id, drm_get_connector_name(connector),
6729 encoder->base.id, drm_get_encoder_name(encoder));
6730
Jesse Barnes79e53942008-11-07 14:24:08 -08006731 /*
6732 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01006733 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006734 * - if the connector already has an assigned crtc, use it (but make
6735 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01006736 *
Jesse Barnes79e53942008-11-07 14:24:08 -08006737 * - try to find the first unused crtc that can drive this connector,
6738 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08006739 */
6740
6741 /* See if we already have a CRTC for this connector */
6742 if (encoder->crtc) {
6743 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01006744
Jesse Barnes79e53942008-11-07 14:24:08 -08006745 intel_crtc = to_intel_crtc(crtc);
Chris Wilson8261b192011-04-19 23:18:09 +01006746 old->dpms_mode = intel_crtc->dpms_mode;
6747 old->load_detect_temp = false;
6748
6749 /* Make sure the crtc and connector are running */
Jesse Barnes79e53942008-11-07 14:24:08 -08006750 if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
Chris Wilson64927112011-04-20 07:25:26 +01006751 struct drm_encoder_helper_funcs *encoder_funcs;
6752 struct drm_crtc_helper_funcs *crtc_funcs;
6753
Jesse Barnes79e53942008-11-07 14:24:08 -08006754 crtc_funcs = crtc->helper_private;
6755 crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
Chris Wilson64927112011-04-20 07:25:26 +01006756
6757 encoder_funcs = encoder->helper_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006758 encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
6759 }
Chris Wilson8261b192011-04-19 23:18:09 +01006760
Chris Wilson71731882011-04-19 23:10:58 +01006761 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006762 }
6763
6764 /* Find an unused one (if possible) */
6765 list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
6766 i++;
6767 if (!(encoder->possible_crtcs & (1 << i)))
6768 continue;
6769 if (!possible_crtc->enabled) {
6770 crtc = possible_crtc;
6771 break;
6772 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006773 }
6774
6775 /*
6776 * If we didn't find an unused CRTC, don't use any.
6777 */
6778 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01006779 DRM_DEBUG_KMS("no pipe available for load-detect\n");
6780 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006781 }
6782
6783 encoder->crtc = crtc;
Zhenyu Wangc1c43972010-03-30 14:39:30 +08006784 connector->encoder = encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006785
6786 intel_crtc = to_intel_crtc(crtc);
Chris Wilson8261b192011-04-19 23:18:09 +01006787 old->dpms_mode = intel_crtc->dpms_mode;
6788 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01006789 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006790
Chris Wilson64927112011-04-20 07:25:26 +01006791 if (!mode)
6792 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08006793
Chris Wilsond2dff872011-04-19 08:36:26 +01006794 old_fb = crtc->fb;
6795
6796 /* We need a framebuffer large enough to accommodate all accesses
6797 * that the plane may generate whilst we perform load detection.
6798 * We can not rely on the fbcon either being present (we get called
6799 * during its initialisation to detect all boot displays, or it may
6800 * not even exist) or that it is large enough to satisfy the
6801 * requested mode.
6802 */
6803 crtc->fb = mode_fits_in_fbdev(dev, mode);
6804 if (crtc->fb == NULL) {
6805 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
6806 crtc->fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
6807 old->release_fb = crtc->fb;
6808 } else
6809 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
6810 if (IS_ERR(crtc->fb)) {
6811 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
6812 crtc->fb = old_fb;
6813 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006814 }
Chris Wilsond2dff872011-04-19 08:36:26 +01006815
6816 if (!drm_crtc_helper_set_mode(crtc, mode, 0, 0, old_fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01006817 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01006818 if (old->release_fb)
6819 old->release_fb->funcs->destroy(old->release_fb);
6820 crtc->fb = old_fb;
Chris Wilson64927112011-04-20 07:25:26 +01006821 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08006822 }
Chris Wilson71731882011-04-19 23:10:58 +01006823
Jesse Barnes79e53942008-11-07 14:24:08 -08006824 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07006825 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -08006826
Chris Wilson71731882011-04-19 23:10:58 +01006827 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08006828}
6829
Zhenyu Wangc1c43972010-03-30 14:39:30 +08006830void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
Chris Wilson8261b192011-04-19 23:18:09 +01006831 struct drm_connector *connector,
6832 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08006833{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006834 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08006835 struct drm_device *dev = encoder->dev;
6836 struct drm_crtc *crtc = encoder->crtc;
6837 struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
6838 struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
6839
Chris Wilsond2dff872011-04-19 08:36:26 +01006840 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6841 connector->base.id, drm_get_connector_name(connector),
6842 encoder->base.id, drm_get_encoder_name(encoder));
6843
Chris Wilson8261b192011-04-19 23:18:09 +01006844 if (old->load_detect_temp) {
Zhenyu Wangc1c43972010-03-30 14:39:30 +08006845 connector->encoder = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08006846 drm_helper_disable_unused_functions(dev);
Chris Wilsond2dff872011-04-19 08:36:26 +01006847
6848 if (old->release_fb)
6849 old->release_fb->funcs->destroy(old->release_fb);
6850
Chris Wilson0622a532011-04-21 09:32:11 +01006851 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08006852 }
6853
Eric Anholtc751ce42010-03-25 11:48:48 -07006854 /* Switch crtc and encoder back off if necessary */
Chris Wilson0622a532011-04-21 09:32:11 +01006855 if (old->dpms_mode != DRM_MODE_DPMS_ON) {
6856 encoder_funcs->dpms(encoder, old->dpms_mode);
Chris Wilson8261b192011-04-19 23:18:09 +01006857 crtc_funcs->dpms(crtc, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08006858 }
6859}
6860
6861/* Returns the clock of the currently programmed mode of the given pipe. */
6862static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
6863{
6864 struct drm_i915_private *dev_priv = dev->dev_private;
6865 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6866 int pipe = intel_crtc->pipe;
Jesse Barnes548f2452011-02-17 10:40:53 -08006867 u32 dpll = I915_READ(DPLL(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006868 u32 fp;
6869 intel_clock_t clock;
6870
6871 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Chris Wilson39adb7a2011-04-22 22:17:21 +01006872 fp = I915_READ(FP0(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006873 else
Chris Wilson39adb7a2011-04-22 22:17:21 +01006874 fp = I915_READ(FP1(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006875
6876 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006877 if (IS_PINEVIEW(dev)) {
6878 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
6879 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08006880 } else {
6881 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
6882 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
6883 }
6884
Chris Wilsona6c45cf2010-09-17 00:32:17 +01006885 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05006886 if (IS_PINEVIEW(dev))
6887 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
6888 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08006889 else
6890 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08006891 DPLL_FPA01_P1_POST_DIV_SHIFT);
6892
6893 switch (dpll & DPLL_MODE_MASK) {
6894 case DPLLB_MODE_DAC_SERIAL:
6895 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
6896 5 : 10;
6897 break;
6898 case DPLLB_MODE_LVDS:
6899 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
6900 7 : 14;
6901 break;
6902 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08006903 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08006904 "mode\n", (int)(dpll & DPLL_MODE_MASK));
6905 return 0;
6906 }
6907
6908 /* XXX: Handle the 100Mhz refclk */
Shaohua Li21778322009-02-23 15:19:16 +08006909 intel_clock(dev, 96000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006910 } else {
6911 bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
6912
6913 if (is_lvds) {
6914 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
6915 DPLL_FPA01_P1_POST_DIV_SHIFT);
6916 clock.p2 = 14;
6917
6918 if ((dpll & PLL_REF_INPUT_MASK) ==
6919 PLLB_REF_INPUT_SPREADSPECTRUMIN) {
6920 /* XXX: might not be 66MHz */
Shaohua Li21778322009-02-23 15:19:16 +08006921 intel_clock(dev, 66000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006922 } else
Shaohua Li21778322009-02-23 15:19:16 +08006923 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006924 } else {
6925 if (dpll & PLL_P1_DIVIDE_BY_TWO)
6926 clock.p1 = 2;
6927 else {
6928 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
6929 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
6930 }
6931 if (dpll & PLL_P2_DIVIDE_BY_4)
6932 clock.p2 = 4;
6933 else
6934 clock.p2 = 2;
6935
Shaohua Li21778322009-02-23 15:19:16 +08006936 intel_clock(dev, 48000, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08006937 }
6938 }
6939
6940 /* XXX: It would be nice to validate the clocks, but we can't reuse
6941 * i830PllIsValid() because it relies on the xf86_config connector
6942 * configuration being accurate, which it isn't necessarily.
6943 */
6944
6945 return clock.dot;
6946}
6947
6948/** Returns the currently programmed mode of the given pipe. */
6949struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
6950 struct drm_crtc *crtc)
6951{
Jesse Barnes548f2452011-02-17 10:40:53 -08006952 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08006953 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6954 int pipe = intel_crtc->pipe;
6955 struct drm_display_mode *mode;
Jesse Barnes548f2452011-02-17 10:40:53 -08006956 int htot = I915_READ(HTOTAL(pipe));
6957 int hsync = I915_READ(HSYNC(pipe));
6958 int vtot = I915_READ(VTOTAL(pipe));
6959 int vsync = I915_READ(VSYNC(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08006960
6961 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
6962 if (!mode)
6963 return NULL;
6964
6965 mode->clock = intel_crtc_clock_get(dev, crtc);
6966 mode->hdisplay = (htot & 0xffff) + 1;
6967 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
6968 mode->hsync_start = (hsync & 0xffff) + 1;
6969 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
6970 mode->vdisplay = (vtot & 0xffff) + 1;
6971 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
6972 mode->vsync_start = (vsync & 0xffff) + 1;
6973 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
6974
6975 drm_mode_set_name(mode);
6976 drm_mode_set_crtcinfo(mode, 0);
6977
6978 return mode;
6979}
6980
Jesse Barnes652c3932009-08-17 13:31:43 -07006981#define GPU_IDLE_TIMEOUT 500 /* ms */
6982
6983/* When this timer fires, we've been idle for awhile */
6984static void intel_gpu_idle_timer(unsigned long arg)
6985{
6986 struct drm_device *dev = (struct drm_device *)arg;
6987 drm_i915_private_t *dev_priv = dev->dev_private;
6988
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00006989 if (!list_empty(&dev_priv->mm.active_list)) {
6990 /* Still processing requests, so just re-arm the timer. */
6991 mod_timer(&dev_priv->idle_timer, jiffies +
6992 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
6993 return;
6994 }
Jesse Barnes652c3932009-08-17 13:31:43 -07006995
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00006996 dev_priv->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07006997 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07006998}
6999
Jesse Barnes652c3932009-08-17 13:31:43 -07007000#define CRTC_IDLE_TIMEOUT 1000 /* ms */
7001
7002static void intel_crtc_idle_timer(unsigned long arg)
7003{
7004 struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
7005 struct drm_crtc *crtc = &intel_crtc->base;
7006 drm_i915_private_t *dev_priv = crtc->dev->dev_private;
Chris Wilsonff7ea4c2010-12-08 09:43:41 +00007007 struct intel_framebuffer *intel_fb;
7008
7009 intel_fb = to_intel_framebuffer(crtc->fb);
7010 if (intel_fb && intel_fb->obj->active) {
7011 /* The framebuffer is still being accessed by the GPU. */
7012 mod_timer(&intel_crtc->idle_timer, jiffies +
7013 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
7014 return;
7015 }
Jesse Barnes652c3932009-08-17 13:31:43 -07007016
Jesse Barnes652c3932009-08-17 13:31:43 -07007017 intel_crtc->busy = false;
Eric Anholt01dfba92009-09-06 15:18:53 -07007018 queue_work(dev_priv->wq, &dev_priv->idle_work);
Jesse Barnes652c3932009-08-17 13:31:43 -07007019}
7020
Daniel Vetter3dec0092010-08-20 21:40:52 +02007021static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07007022{
7023 struct drm_device *dev = crtc->dev;
7024 drm_i915_private_t *dev_priv = dev->dev_private;
7025 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7026 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007027 int dpll_reg = DPLL(pipe);
7028 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07007029
Eric Anholtbad720f2009-10-22 16:11:14 -07007030 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007031 return;
7032
7033 if (!dev_priv->lvds_downclock_avail)
7034 return;
7035
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007036 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07007037 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007038 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007039
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007040 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007041
7042 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
7043 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007044 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08007045
Jesse Barnes652c3932009-08-17 13:31:43 -07007046 dpll = I915_READ(dpll_reg);
7047 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08007048 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007049 }
7050
7051 /* Schedule downclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02007052 mod_timer(&intel_crtc->idle_timer, jiffies +
7053 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07007054}
7055
7056static void intel_decrease_pllclock(struct drm_crtc *crtc)
7057{
7058 struct drm_device *dev = crtc->dev;
7059 drm_i915_private_t *dev_priv = dev->dev_private;
7060 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7061 int pipe = intel_crtc->pipe;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007062 int dpll_reg = DPLL(pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007063 int dpll = I915_READ(dpll_reg);
7064
Eric Anholtbad720f2009-10-22 16:11:14 -07007065 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07007066 return;
7067
7068 if (!dev_priv->lvds_downclock_avail)
7069 return;
7070
7071 /*
7072 * Since this is called by a timer, we should never get here in
7073 * the manual case.
7074 */
7075 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08007076 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007077
Sean Paul8ac5a6d2012-02-13 13:14:51 -05007078 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007079
7080 dpll |= DISPLAY_RATE_SELECT_FPA1;
7081 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07007082 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07007083 dpll = I915_READ(dpll_reg);
7084 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08007085 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07007086 }
7087
7088}
7089
7090/**
7091 * intel_idle_update - adjust clocks for idleness
7092 * @work: work struct
7093 *
7094 * Either the GPU or display (or both) went idle. Check the busy status
7095 * here and adjust the CRTC and GPU clocks as necessary.
7096 */
7097static void intel_idle_update(struct work_struct *work)
7098{
7099 drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
7100 idle_work);
7101 struct drm_device *dev = dev_priv->dev;
7102 struct drm_crtc *crtc;
7103 struct intel_crtc *intel_crtc;
7104
7105 if (!i915_powersave)
7106 return;
7107
7108 mutex_lock(&dev->struct_mutex);
7109
Jesse Barnes7648fa92010-05-20 14:28:11 -07007110 i915_update_gfx_val(dev_priv);
7111
Jesse Barnes652c3932009-08-17 13:31:43 -07007112 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7113 /* Skip inactive CRTCs */
7114 if (!crtc->fb)
7115 continue;
7116
7117 intel_crtc = to_intel_crtc(crtc);
7118 if (!intel_crtc->busy)
7119 intel_decrease_pllclock(crtc);
7120 }
7121
Li Peng45ac22c2010-06-12 23:38:35 +08007122
Jesse Barnes652c3932009-08-17 13:31:43 -07007123 mutex_unlock(&dev->struct_mutex);
7124}
7125
7126/**
7127 * intel_mark_busy - mark the GPU and possibly the display busy
7128 * @dev: drm device
7129 * @obj: object we're operating on
7130 *
7131 * Callers can use this function to indicate that the GPU is busy processing
7132 * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
7133 * buffer), we'll also mark the display as busy, so we know to increase its
7134 * clock frequency.
7135 */
Chris Wilson05394f32010-11-08 19:18:58 +00007136void intel_mark_busy(struct drm_device *dev, struct drm_i915_gem_object *obj)
Jesse Barnes652c3932009-08-17 13:31:43 -07007137{
7138 drm_i915_private_t *dev_priv = dev->dev_private;
7139 struct drm_crtc *crtc = NULL;
7140 struct intel_framebuffer *intel_fb;
7141 struct intel_crtc *intel_crtc;
7142
Zhenyu Wang5e17ee72009-09-03 09:30:06 +08007143 if (!drm_core_check_feature(dev, DRIVER_MODESET))
7144 return;
7145
Alexander Lam18b21902011-01-03 13:28:56 -05007146 if (!dev_priv->busy)
Chris Wilson28cf7982009-11-30 01:08:56 +00007147 dev_priv->busy = true;
Alexander Lam18b21902011-01-03 13:28:56 -05007148 else
Chris Wilson28cf7982009-11-30 01:08:56 +00007149 mod_timer(&dev_priv->idle_timer, jiffies +
7150 msecs_to_jiffies(GPU_IDLE_TIMEOUT));
Jesse Barnes652c3932009-08-17 13:31:43 -07007151
7152 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
7153 if (!crtc->fb)
7154 continue;
7155
7156 intel_crtc = to_intel_crtc(crtc);
7157 intel_fb = to_intel_framebuffer(crtc->fb);
7158 if (intel_fb->obj == obj) {
7159 if (!intel_crtc->busy) {
7160 /* Non-busy -> busy, upclock */
Daniel Vetter3dec0092010-08-20 21:40:52 +02007161 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07007162 intel_crtc->busy = true;
7163 } else {
7164 /* Busy -> busy, put off timer */
7165 mod_timer(&intel_crtc->idle_timer, jiffies +
7166 msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
7167 }
7168 }
7169 }
7170}
7171
Jesse Barnes79e53942008-11-07 14:24:08 -08007172static void intel_crtc_destroy(struct drm_crtc *crtc)
7173{
7174 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007175 struct drm_device *dev = crtc->dev;
7176 struct intel_unpin_work *work;
7177 unsigned long flags;
7178
7179 spin_lock_irqsave(&dev->event_lock, flags);
7180 work = intel_crtc->unpin_work;
7181 intel_crtc->unpin_work = NULL;
7182 spin_unlock_irqrestore(&dev->event_lock, flags);
7183
7184 if (work) {
7185 cancel_work_sync(&work->work);
7186 kfree(work);
7187 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007188
7189 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02007190
Jesse Barnes79e53942008-11-07 14:24:08 -08007191 kfree(intel_crtc);
7192}
7193
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007194static void intel_unpin_work_fn(struct work_struct *__work)
7195{
7196 struct intel_unpin_work *work =
7197 container_of(__work, struct intel_unpin_work, work);
7198
7199 mutex_lock(&work->dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01007200 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00007201 drm_gem_object_unreference(&work->pending_flip_obj->base);
7202 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007203
Chris Wilson7782de32011-07-08 12:22:41 +01007204 intel_update_fbc(work->dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007205 mutex_unlock(&work->dev->struct_mutex);
7206 kfree(work);
7207}
7208
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007209static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01007210 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007211{
7212 drm_i915_private_t *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007213 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7214 struct intel_unpin_work *work;
Chris Wilson05394f32010-11-08 19:18:58 +00007215 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007216 struct drm_pending_vblank_event *e;
Mario Kleiner49b14a52010-12-09 07:00:07 +01007217 struct timeval tnow, tvbl;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007218 unsigned long flags;
7219
7220 /* Ignore early vblank irqs */
7221 if (intel_crtc == NULL)
7222 return;
7223
Mario Kleiner49b14a52010-12-09 07:00:07 +01007224 do_gettimeofday(&tnow);
7225
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007226 spin_lock_irqsave(&dev->event_lock, flags);
7227 work = intel_crtc->unpin_work;
7228 if (work == NULL || !work->pending) {
7229 spin_unlock_irqrestore(&dev->event_lock, flags);
7230 return;
7231 }
7232
7233 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007234
7235 if (work->event) {
7236 e = work->event;
Mario Kleiner49b14a52010-12-09 07:00:07 +01007237 e->event.sequence = drm_vblank_count_and_time(dev, intel_crtc->pipe, &tvbl);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007238
7239 /* Called before vblank count and timestamps have
7240 * been updated for the vblank interval of flip
7241 * completion? Need to increment vblank count and
7242 * add one videorefresh duration to returned timestamp
Mario Kleiner49b14a52010-12-09 07:00:07 +01007243 * to account for this. We assume this happened if we
7244 * get called over 0.9 frame durations after the last
7245 * timestamped vblank.
7246 *
7247 * This calculation can not be used with vrefresh rates
7248 * below 5Hz (10Hz to be on the safe side) without
7249 * promoting to 64 integers.
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007250 */
Mario Kleiner49b14a52010-12-09 07:00:07 +01007251 if (10 * (timeval_to_ns(&tnow) - timeval_to_ns(&tvbl)) >
7252 9 * crtc->framedur_ns) {
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007253 e->event.sequence++;
Mario Kleiner49b14a52010-12-09 07:00:07 +01007254 tvbl = ns_to_timeval(timeval_to_ns(&tvbl) +
7255 crtc->framedur_ns);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007256 }
7257
Mario Kleiner49b14a52010-12-09 07:00:07 +01007258 e->event.tv_sec = tvbl.tv_sec;
7259 e->event.tv_usec = tvbl.tv_usec;
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007260
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007261 list_add_tail(&e->base.link,
7262 &e->base.file_priv->event_list);
7263 wake_up_interruptible(&e->base.file_priv->event_wait);
7264 }
7265
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01007266 drm_vblank_put(dev, intel_crtc->pipe);
7267
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007268 spin_unlock_irqrestore(&dev->event_lock, flags);
7269
Chris Wilson05394f32010-11-08 19:18:58 +00007270 obj = work->old_fb_obj;
Chris Wilsond9e86c02010-11-10 16:40:20 +00007271
Chris Wilsone59f2ba2010-10-07 17:28:15 +01007272 atomic_clear_mask(1 << intel_crtc->plane,
Chris Wilson05394f32010-11-08 19:18:58 +00007273 &obj->pending_flip.counter);
7274 if (atomic_read(&obj->pending_flip) == 0)
Chris Wilsonf787a5f2010-09-24 16:02:42 +01007275 wake_up(&dev_priv->pending_flip_queue);
Chris Wilsond9e86c02010-11-10 16:40:20 +00007276
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007277 schedule_work(&work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07007278
7279 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007280}
7281
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007282void intel_finish_page_flip(struct drm_device *dev, int pipe)
7283{
7284 drm_i915_private_t *dev_priv = dev->dev_private;
7285 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
7286
Mario Kleiner49b14a52010-12-09 07:00:07 +01007287 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007288}
7289
7290void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
7291{
7292 drm_i915_private_t *dev_priv = dev->dev_private;
7293 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
7294
Mario Kleiner49b14a52010-12-09 07:00:07 +01007295 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07007296}
7297
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007298void intel_prepare_page_flip(struct drm_device *dev, int plane)
7299{
7300 drm_i915_private_t *dev_priv = dev->dev_private;
7301 struct intel_crtc *intel_crtc =
7302 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
7303 unsigned long flags;
7304
7305 spin_lock_irqsave(&dev->event_lock, flags);
Jesse Barnesde3f4402010-01-14 13:18:02 -08007306 if (intel_crtc->unpin_work) {
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007307 if ((++intel_crtc->unpin_work->pending) > 1)
7308 DRM_ERROR("Prepared flip multiple times\n");
Jesse Barnesde3f4402010-01-14 13:18:02 -08007309 } else {
7310 DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
7311 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007312 spin_unlock_irqrestore(&dev->event_lock, flags);
7313}
7314
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007315static int intel_gen2_queue_flip(struct drm_device *dev,
7316 struct drm_crtc *crtc,
7317 struct drm_framebuffer *fb,
7318 struct drm_i915_gem_object *obj)
7319{
7320 struct drm_i915_private *dev_priv = dev->dev_private;
7321 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7322 unsigned long offset;
7323 u32 flip_mask;
7324 int ret;
7325
7326 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7327 if (ret)
7328 goto out;
7329
7330 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007331 offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007332
7333 ret = BEGIN_LP_RING(6);
7334 if (ret)
7335 goto out;
7336
7337 /* Can't queue multiple flips, so wait for the previous
7338 * one to finish before executing the next.
7339 */
7340 if (intel_crtc->plane)
7341 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7342 else
7343 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7344 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
7345 OUT_RING(MI_NOOP);
7346 OUT_RING(MI_DISPLAY_FLIP |
7347 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007348 OUT_RING(fb->pitches[0]);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007349 OUT_RING(obj->gtt_offset + offset);
Daniel Vetterc6a32fc2012-01-20 10:43:44 +01007350 OUT_RING(0); /* aux display base address, unused */
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007351 ADVANCE_LP_RING();
7352out:
7353 return ret;
7354}
7355
7356static int intel_gen3_queue_flip(struct drm_device *dev,
7357 struct drm_crtc *crtc,
7358 struct drm_framebuffer *fb,
7359 struct drm_i915_gem_object *obj)
7360{
7361 struct drm_i915_private *dev_priv = dev->dev_private;
7362 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7363 unsigned long offset;
7364 u32 flip_mask;
7365 int ret;
7366
7367 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7368 if (ret)
7369 goto out;
7370
7371 /* Offset into the new buffer for cases of shared fbs between CRTCs */
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007372 offset = crtc->y * fb->pitches[0] + crtc->x * fb->bits_per_pixel/8;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007373
7374 ret = BEGIN_LP_RING(6);
7375 if (ret)
7376 goto out;
7377
7378 if (intel_crtc->plane)
7379 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
7380 else
7381 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
7382 OUT_RING(MI_WAIT_FOR_EVENT | flip_mask);
7383 OUT_RING(MI_NOOP);
7384 OUT_RING(MI_DISPLAY_FLIP_I915 |
7385 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007386 OUT_RING(fb->pitches[0]);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007387 OUT_RING(obj->gtt_offset + offset);
7388 OUT_RING(MI_NOOP);
7389
7390 ADVANCE_LP_RING();
7391out:
7392 return ret;
7393}
7394
7395static int intel_gen4_queue_flip(struct drm_device *dev,
7396 struct drm_crtc *crtc,
7397 struct drm_framebuffer *fb,
7398 struct drm_i915_gem_object *obj)
7399{
7400 struct drm_i915_private *dev_priv = dev->dev_private;
7401 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7402 uint32_t pf, pipesrc;
7403 int ret;
7404
7405 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7406 if (ret)
7407 goto out;
7408
7409 ret = BEGIN_LP_RING(4);
7410 if (ret)
7411 goto out;
7412
7413 /* i965+ uses the linear or tiled offsets from the
7414 * Display Registers (which do not change across a page-flip)
7415 * so we need only reprogram the base address.
7416 */
7417 OUT_RING(MI_DISPLAY_FLIP |
7418 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007419 OUT_RING(fb->pitches[0]);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007420 OUT_RING(obj->gtt_offset | obj->tiling_mode);
7421
7422 /* XXX Enabling the panel-fitter across page-flip is so far
7423 * untested on non-native modes, so ignore it for now.
7424 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
7425 */
7426 pf = 0;
7427 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7428 OUT_RING(pf | pipesrc);
7429 ADVANCE_LP_RING();
7430out:
7431 return ret;
7432}
7433
7434static int intel_gen6_queue_flip(struct drm_device *dev,
7435 struct drm_crtc *crtc,
7436 struct drm_framebuffer *fb,
7437 struct drm_i915_gem_object *obj)
7438{
7439 struct drm_i915_private *dev_priv = dev->dev_private;
7440 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7441 uint32_t pf, pipesrc;
7442 int ret;
7443
7444 ret = intel_pin_and_fence_fb_obj(dev, obj, LP_RING(dev_priv));
7445 if (ret)
7446 goto out;
7447
7448 ret = BEGIN_LP_RING(4);
7449 if (ret)
7450 goto out;
7451
7452 OUT_RING(MI_DISPLAY_FLIP |
7453 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007454 OUT_RING(fb->pitches[0] | obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007455 OUT_RING(obj->gtt_offset);
7456
7457 pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
7458 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
7459 OUT_RING(pf | pipesrc);
7460 ADVANCE_LP_RING();
7461out:
7462 return ret;
7463}
7464
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007465/*
7466 * On gen7 we currently use the blit ring because (in early silicon at least)
7467 * the render ring doesn't give us interrpts for page flip completion, which
7468 * means clients will hang after the first flip is queued. Fortunately the
7469 * blit ring generates interrupts properly, so use it instead.
7470 */
7471static int intel_gen7_queue_flip(struct drm_device *dev,
7472 struct drm_crtc *crtc,
7473 struct drm_framebuffer *fb,
7474 struct drm_i915_gem_object *obj)
7475{
7476 struct drm_i915_private *dev_priv = dev->dev_private;
7477 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7478 struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
7479 int ret;
7480
7481 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
7482 if (ret)
7483 goto out;
7484
7485 ret = intel_ring_begin(ring, 4);
7486 if (ret)
7487 goto out;
7488
7489 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | (intel_crtc->plane << 19));
Ville Syrjälä01f2c772011-12-20 00:06:49 +02007490 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Jesse Barnes7c9017e2011-06-16 12:18:54 -07007491 intel_ring_emit(ring, (obj->gtt_offset));
7492 intel_ring_emit(ring, (MI_NOOP));
7493 intel_ring_advance(ring);
7494out:
7495 return ret;
7496}
7497
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007498static int intel_default_queue_flip(struct drm_device *dev,
7499 struct drm_crtc *crtc,
7500 struct drm_framebuffer *fb,
7501 struct drm_i915_gem_object *obj)
7502{
7503 return -ENODEV;
7504}
7505
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007506static int intel_crtc_page_flip(struct drm_crtc *crtc,
7507 struct drm_framebuffer *fb,
7508 struct drm_pending_vblank_event *event)
7509{
7510 struct drm_device *dev = crtc->dev;
7511 struct drm_i915_private *dev_priv = dev->dev_private;
7512 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00007513 struct drm_i915_gem_object *obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007514 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7515 struct intel_unpin_work *work;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007516 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01007517 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007518
7519 work = kzalloc(sizeof *work, GFP_KERNEL);
7520 if (work == NULL)
7521 return -ENOMEM;
7522
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007523 work->event = event;
7524 work->dev = crtc->dev;
7525 intel_fb = to_intel_framebuffer(crtc->fb);
Jesse Barnesb1b87f62010-01-26 14:40:05 -08007526 work->old_fb_obj = intel_fb->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007527 INIT_WORK(&work->work, intel_unpin_work_fn);
7528
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007529 ret = drm_vblank_get(dev, intel_crtc->pipe);
7530 if (ret)
7531 goto free_work;
7532
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007533 /* We borrow the event spin lock for protecting unpin_work */
7534 spin_lock_irqsave(&dev->event_lock, flags);
7535 if (intel_crtc->unpin_work) {
7536 spin_unlock_irqrestore(&dev->event_lock, flags);
7537 kfree(work);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007538 drm_vblank_put(dev, intel_crtc->pipe);
Chris Wilson468f0b42010-05-27 13:18:13 +01007539
7540 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007541 return -EBUSY;
7542 }
7543 intel_crtc->unpin_work = work;
7544 spin_unlock_irqrestore(&dev->event_lock, flags);
7545
7546 intel_fb = to_intel_framebuffer(fb);
7547 obj = intel_fb->obj;
7548
Chris Wilson468f0b42010-05-27 13:18:13 +01007549 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007550
Jesse Barnes75dfca82010-02-10 15:09:44 -08007551 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00007552 drm_gem_object_reference(&work->old_fb_obj->base);
7553 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007554
7555 crtc->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01007556
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007557 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007558
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01007559 work->enable_stall_check = true;
7560
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007561 /* Block clients from rendering to the new back buffer until
7562 * the flip occurs and the object is no longer visible.
7563 */
Chris Wilson05394f32010-11-08 19:18:58 +00007564 atomic_add(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01007565
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007566 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
7567 if (ret)
7568 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007569
Chris Wilson7782de32011-07-08 12:22:41 +01007570 intel_disable_fbc(dev);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007571 mutex_unlock(&dev->struct_mutex);
7572
Jesse Barnese5510fa2010-07-01 16:48:37 -07007573 trace_i915_flip_request(intel_crtc->plane, obj);
7574
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007575 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01007576
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07007577cleanup_pending:
7578 atomic_sub(1 << intel_crtc->plane, &work->old_fb_obj->pending_flip);
Chris Wilson05394f32010-11-08 19:18:58 +00007579 drm_gem_object_unreference(&work->old_fb_obj->base);
7580 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01007581 mutex_unlock(&dev->struct_mutex);
7582
7583 spin_lock_irqsave(&dev->event_lock, flags);
7584 intel_crtc->unpin_work = NULL;
7585 spin_unlock_irqrestore(&dev->event_lock, flags);
7586
Jesse Barnes7317c75e62011-08-29 09:45:28 -07007587 drm_vblank_put(dev, intel_crtc->pipe);
7588free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01007589 kfree(work);
7590
7591 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05007592}
7593
Chris Wilson47f1c6c2010-12-03 15:37:31 +00007594static void intel_sanitize_modesetting(struct drm_device *dev,
7595 int pipe, int plane)
7596{
7597 struct drm_i915_private *dev_priv = dev->dev_private;
7598 u32 reg, val;
7599
7600 if (HAS_PCH_SPLIT(dev))
7601 return;
7602
7603 /* Who knows what state these registers were left in by the BIOS or
7604 * grub?
7605 *
7606 * If we leave the registers in a conflicting state (e.g. with the
7607 * display plane reading from the other pipe than the one we intend
7608 * to use) then when we attempt to teardown the active mode, we will
7609 * not disable the pipes and planes in the correct order -- leaving
7610 * a plane reading from a disabled pipe and possibly leading to
7611 * undefined behaviour.
7612 */
7613
7614 reg = DSPCNTR(plane);
7615 val = I915_READ(reg);
7616
7617 if ((val & DISPLAY_PLANE_ENABLE) == 0)
7618 return;
7619 if (!!(val & DISPPLANE_SEL_PIPE_MASK) == pipe)
7620 return;
7621
7622 /* This display plane is active and attached to the other CPU pipe. */
7623 pipe = !pipe;
7624
7625 /* Disable the plane and wait for it to stop reading from the pipe. */
Jesse Barnesb24e7172011-01-04 15:09:30 -08007626 intel_disable_plane(dev_priv, plane, pipe);
7627 intel_disable_pipe(dev_priv, pipe);
Chris Wilson47f1c6c2010-12-03 15:37:31 +00007628}
Jesse Barnes79e53942008-11-07 14:24:08 -08007629
Chris Wilsonf6e5b162011-04-12 18:06:51 +01007630static void intel_crtc_reset(struct drm_crtc *crtc)
7631{
7632 struct drm_device *dev = crtc->dev;
7633 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7634
7635 /* Reset flags back to the 'unknown' status so that they
7636 * will be correctly set on the initial modeset.
7637 */
7638 intel_crtc->dpms_mode = -1;
7639
7640 /* We need to fix up any BIOS configuration that conflicts with
7641 * our expectations.
7642 */
7643 intel_sanitize_modesetting(dev, intel_crtc->pipe, intel_crtc->plane);
7644}
7645
7646static struct drm_crtc_helper_funcs intel_helper_funcs = {
7647 .dpms = intel_crtc_dpms,
7648 .mode_fixup = intel_crtc_mode_fixup,
7649 .mode_set = intel_crtc_mode_set,
7650 .mode_set_base = intel_pipe_set_base,
7651 .mode_set_base_atomic = intel_pipe_set_base_atomic,
7652 .load_lut = intel_crtc_load_lut,
7653 .disable = intel_crtc_disable,
7654};
7655
7656static const struct drm_crtc_funcs intel_crtc_funcs = {
7657 .reset = intel_crtc_reset,
7658 .cursor_set = intel_crtc_cursor_set,
7659 .cursor_move = intel_crtc_cursor_move,
7660 .gamma_set = intel_crtc_gamma_set,
7661 .set_config = drm_crtc_helper_set_config,
7662 .destroy = intel_crtc_destroy,
7663 .page_flip = intel_crtc_page_flip,
7664};
7665
Hannes Ederb358d0a2008-12-18 21:18:47 +01007666static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -08007667{
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08007668 drm_i915_private_t *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08007669 struct intel_crtc *intel_crtc;
7670 int i;
7671
7672 intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
7673 if (intel_crtc == NULL)
7674 return;
7675
7676 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
7677
7678 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -08007679 for (i = 0; i < 256; i++) {
7680 intel_crtc->lut_r[i] = i;
7681 intel_crtc->lut_g[i] = i;
7682 intel_crtc->lut_b[i] = i;
7683 }
7684
Jesse Barnes80824002009-09-10 15:28:06 -07007685 /* Swap pipes & planes for FBC on pre-965 */
7686 intel_crtc->pipe = pipe;
7687 intel_crtc->plane = pipe;
Chris Wilsone2e767a2010-09-13 16:53:12 +01007688 if (IS_MOBILE(dev) && IS_GEN3(dev)) {
Zhao Yakui28c97732009-10-09 11:39:41 +08007689 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +01007690 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -07007691 }
7692
Jesse Barnes22fd0fa2009-12-02 13:42:53 -08007693 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
7694 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
7695 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
7696 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
7697
Chris Wilson5d1d0cc2011-01-24 15:02:15 +00007698 intel_crtc_reset(&intel_crtc->base);
Chris Wilson04dbff52011-02-10 17:38:35 +00007699 intel_crtc->active = true; /* force the pipe off on setup_init_config */
Jesse Barnes5a354202011-06-24 12:19:22 -07007700 intel_crtc->bpp = 24; /* default for pre-Ironlake */
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07007701
7702 if (HAS_PCH_SPLIT(dev)) {
Jesse Barnes4b645f12011-10-12 09:51:31 -07007703 if (pipe == 2 && IS_IVYBRIDGE(dev))
7704 intel_crtc->no_pll = true;
Jesse Barnes7e7d76c2010-09-10 10:47:20 -07007705 intel_helper_funcs.prepare = ironlake_crtc_prepare;
7706 intel_helper_funcs.commit = ironlake_crtc_commit;
7707 } else {
7708 intel_helper_funcs.prepare = i9xx_crtc_prepare;
7709 intel_helper_funcs.commit = i9xx_crtc_commit;
7710 }
7711
Jesse Barnes79e53942008-11-07 14:24:08 -08007712 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
7713
Jesse Barnes652c3932009-08-17 13:31:43 -07007714 intel_crtc->busy = false;
7715
7716 setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
7717 (unsigned long)intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007718}
7719
Carl Worth08d7b3d2009-04-29 14:43:54 -07007720int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +00007721 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -07007722{
7723 drm_i915_private_t *dev_priv = dev->dev_private;
7724 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +02007725 struct drm_mode_object *drmmode_obj;
7726 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007727
7728 if (!dev_priv) {
7729 DRM_ERROR("called with no initialization\n");
7730 return -EINVAL;
7731 }
7732
Daniel Vetterc05422d2009-08-11 16:05:30 +02007733 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
7734 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -07007735
Daniel Vetterc05422d2009-08-11 16:05:30 +02007736 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -07007737 DRM_ERROR("no such CRTC id\n");
7738 return -EINVAL;
7739 }
7740
Daniel Vetterc05422d2009-08-11 16:05:30 +02007741 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
7742 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007743
Daniel Vetterc05422d2009-08-11 16:05:30 +02007744 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -07007745}
7746
Zhenyu Wangc5e4df32010-03-30 14:39:27 +08007747static int intel_encoder_clones(struct drm_device *dev, int type_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08007748{
Chris Wilson4ef69c72010-09-09 15:14:28 +01007749 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08007750 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007751 int entry = 0;
7752
Chris Wilson4ef69c72010-09-09 15:14:28 +01007753 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7754 if (type_mask & encoder->clone_mask)
Jesse Barnes79e53942008-11-07 14:24:08 -08007755 index_mask |= (1 << entry);
7756 entry++;
7757 }
Chris Wilson4ef69c72010-09-09 15:14:28 +01007758
Jesse Barnes79e53942008-11-07 14:24:08 -08007759 return index_mask;
7760}
7761
Chris Wilson4d302442010-12-14 19:21:29 +00007762static bool has_edp_a(struct drm_device *dev)
7763{
7764 struct drm_i915_private *dev_priv = dev->dev_private;
7765
7766 if (!IS_MOBILE(dev))
7767 return false;
7768
7769 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
7770 return false;
7771
7772 if (IS_GEN5(dev) &&
7773 (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
7774 return false;
7775
7776 return true;
7777}
7778
Jesse Barnes79e53942008-11-07 14:24:08 -08007779static void intel_setup_outputs(struct drm_device *dev)
7780{
Eric Anholt725e30a2009-01-22 13:01:02 -08007781 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +01007782 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007783 bool dpd_is_edp = false;
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00007784 bool has_lvds;
Jesse Barnes79e53942008-11-07 14:24:08 -08007785
Chris Wilsonf3cfcba2012-02-09 09:35:53 +00007786 has_lvds = intel_lvds_init(dev);
Chris Wilsonc5d1b512010-11-29 18:00:23 +00007787 if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
7788 /* disable the panel fitter on everything but LVDS */
7789 I915_WRITE(PFIT_CONTROL, 0);
7790 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007791
Eric Anholtbad720f2009-10-22 16:11:14 -07007792 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007793 dpd_is_edp = intel_dpd_is_edp(dev);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007794
Chris Wilson4d302442010-12-14 19:21:29 +00007795 if (has_edp_a(dev))
Zhenyu Wang32f9d652009-07-24 01:00:32 +08007796 intel_dp_init(dev, DP_A);
7797
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007798 if (dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
7799 intel_dp_init(dev, PCH_DP_D);
7800 }
7801
7802 intel_crt_init(dev);
7803
7804 if (HAS_PCH_SPLIT(dev)) {
7805 int found;
7806
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007807 if (I915_READ(HDMIB) & PORT_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +08007808 /* PCH SDVOB multiplex with HDMIB */
7809 found = intel_sdvo_init(dev, PCH_SDVOB);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007810 if (!found)
7811 intel_hdmi_init(dev, HDMIB);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08007812 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
7813 intel_dp_init(dev, PCH_DP_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +08007814 }
7815
7816 if (I915_READ(HDMIC) & PORT_DETECTED)
7817 intel_hdmi_init(dev, HDMIC);
7818
7819 if (I915_READ(HDMID) & PORT_DETECTED)
7820 intel_hdmi_init(dev, HDMID);
7821
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08007822 if (I915_READ(PCH_DP_C) & DP_DETECTED)
7823 intel_dp_init(dev, PCH_DP_C);
7824
Adam Jacksoncb0953d2010-07-16 14:46:29 -04007825 if (!dpd_is_edp && (I915_READ(PCH_DP_D) & DP_DETECTED))
Zhenyu Wang5eb08b62009-07-24 01:00:31 +08007826 intel_dp_init(dev, PCH_DP_D);
7827
Zhenyu Wang103a1962009-11-27 11:44:36 +08007828 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +08007829 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -08007830
Eric Anholt725e30a2009-01-22 13:01:02 -08007831 if (I915_READ(SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007832 DRM_DEBUG_KMS("probing SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08007833 found = intel_sdvo_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007834 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
7835 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08007836 intel_hdmi_init(dev, SDVOB);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007837 }
Ma Ling27185ae2009-08-24 13:50:23 +08007838
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007839 if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
7840 DRM_DEBUG_KMS("probing DP_B\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07007841 intel_dp_init(dev, DP_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007842 }
Eric Anholt725e30a2009-01-22 13:01:02 -08007843 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -04007844
7845 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -04007846
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007847 if (I915_READ(SDVOB) & SDVO_DETECTED) {
7848 DRM_DEBUG_KMS("probing SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08007849 found = intel_sdvo_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007850 }
Ma Ling27185ae2009-08-24 13:50:23 +08007851
7852 if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
7853
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007854 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
7855 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Eric Anholt725e30a2009-01-22 13:01:02 -08007856 intel_hdmi_init(dev, SDVOC);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007857 }
7858 if (SUPPORTS_INTEGRATED_DP(dev)) {
7859 DRM_DEBUG_KMS("probing DP_C\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07007860 intel_dp_init(dev, DP_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007861 }
Eric Anholt725e30a2009-01-22 13:01:02 -08007862 }
Ma Ling27185ae2009-08-24 13:50:23 +08007863
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007864 if (SUPPORTS_INTEGRATED_DP(dev) &&
7865 (I915_READ(DP_D) & DP_DETECTED)) {
7866 DRM_DEBUG_KMS("probing DP_D\n");
Keith Packarda4fc5ed2009-04-07 16:16:42 -07007867 intel_dp_init(dev, DP_D);
Jesse Barnesb01f2c32009-12-11 11:07:17 -08007868 }
Eric Anholtbad720f2009-10-22 16:11:14 -07007869 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08007870 intel_dvo_init(dev);
7871
Zhenyu Wang103a1962009-11-27 11:44:36 +08007872 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -08007873 intel_tv_init(dev);
7874
Chris Wilson4ef69c72010-09-09 15:14:28 +01007875 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
7876 encoder->base.possible_crtcs = encoder->crtc_mask;
7877 encoder->base.possible_clones =
7878 intel_encoder_clones(dev, encoder->clone_mask);
Jesse Barnes79e53942008-11-07 14:24:08 -08007879 }
Chris Wilson47356eb2011-01-11 17:06:04 +00007880
Chris Wilson2c7111d2011-03-29 10:40:27 +01007881 /* disable all the possible outputs/crtcs before entering KMS mode */
7882 drm_helper_disable_unused_functions(dev);
Keith Packard9fb526d2011-09-26 22:24:57 -07007883
7884 if (HAS_PCH_SPLIT(dev))
7885 ironlake_init_pch_refclk(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08007886}
7887
7888static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
7889{
7890 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -08007891
7892 drm_framebuffer_cleanup(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00007893 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08007894
7895 kfree(intel_fb);
7896}
7897
7898static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +00007899 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08007900 unsigned int *handle)
7901{
7902 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +00007903 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08007904
Chris Wilson05394f32010-11-08 19:18:58 +00007905 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -08007906}
7907
7908static const struct drm_framebuffer_funcs intel_fb_funcs = {
7909 .destroy = intel_user_framebuffer_destroy,
7910 .create_handle = intel_user_framebuffer_create_handle,
7911};
7912
Dave Airlie38651672010-03-30 05:34:13 +00007913int intel_framebuffer_init(struct drm_device *dev,
7914 struct intel_framebuffer *intel_fb,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007915 struct drm_mode_fb_cmd2 *mode_cmd,
Chris Wilson05394f32010-11-08 19:18:58 +00007916 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -08007917{
Jesse Barnes79e53942008-11-07 14:24:08 -08007918 int ret;
7919
Chris Wilson05394f32010-11-08 19:18:58 +00007920 if (obj->tiling_mode == I915_TILING_Y)
Chris Wilson57cd6502010-08-08 12:34:44 +01007921 return -EINVAL;
7922
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007923 if (mode_cmd->pitches[0] & 63)
Chris Wilson57cd6502010-08-08 12:34:44 +01007924 return -EINVAL;
7925
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007926 switch (mode_cmd->pixel_format) {
Ville Syrjälä04b39242011-11-17 18:05:13 +02007927 case DRM_FORMAT_RGB332:
7928 case DRM_FORMAT_RGB565:
7929 case DRM_FORMAT_XRGB8888:
7930 case DRM_FORMAT_ARGB8888:
7931 case DRM_FORMAT_XRGB2101010:
7932 case DRM_FORMAT_ARGB2101010:
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007933 /* RGB formats are common across chipsets */
Jesse Barnesb5626742011-06-24 12:19:27 -07007934 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +02007935 case DRM_FORMAT_YUYV:
7936 case DRM_FORMAT_UYVY:
7937 case DRM_FORMAT_YVYU:
7938 case DRM_FORMAT_VYUY:
Chris Wilson57cd6502010-08-08 12:34:44 +01007939 break;
7940 default:
Eugeni Dodonovaca25842012-01-17 15:25:45 -02007941 DRM_DEBUG_KMS("unsupported pixel format %u\n",
7942 mode_cmd->pixel_format);
Chris Wilson57cd6502010-08-08 12:34:44 +01007943 return -EINVAL;
7944 }
7945
Jesse Barnes79e53942008-11-07 14:24:08 -08007946 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
7947 if (ret) {
7948 DRM_ERROR("framebuffer init failed %d\n", ret);
7949 return ret;
7950 }
7951
7952 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
Jesse Barnes79e53942008-11-07 14:24:08 -08007953 intel_fb->obj = obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08007954 return 0;
7955}
7956
Jesse Barnes79e53942008-11-07 14:24:08 -08007957static struct drm_framebuffer *
7958intel_user_framebuffer_create(struct drm_device *dev,
7959 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007960 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -08007961{
Chris Wilson05394f32010-11-08 19:18:58 +00007962 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -08007963
Jesse Barnes308e5bc2011-11-14 14:51:28 -08007964 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
7965 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +00007966 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +01007967 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -08007968
Chris Wilsond2dff872011-04-19 08:36:26 +01007969 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -08007970}
7971
Jesse Barnes79e53942008-11-07 14:24:08 -08007972static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -08007973 .fb_create = intel_user_framebuffer_create,
Dave Airlieeb1f8e42010-05-07 06:42:51 +00007974 .output_poll_changed = intel_fb_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -08007975};
7976
Chris Wilson05394f32010-11-08 19:18:58 +00007977static struct drm_i915_gem_object *
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08007978intel_alloc_context_page(struct drm_device *dev)
Chris Wilson9ea8d052010-01-04 18:57:56 +00007979{
Chris Wilson05394f32010-11-08 19:18:58 +00007980 struct drm_i915_gem_object *ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00007981 int ret;
7982
Ben Widawsky2c34b852011-03-19 18:14:26 -07007983 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
7984
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08007985 ctx = i915_gem_alloc_object(dev, 4096);
7986 if (!ctx) {
Chris Wilson9ea8d052010-01-04 18:57:56 +00007987 DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
7988 return NULL;
7989 }
7990
Daniel Vetter75e9e912010-11-04 17:11:09 +01007991 ret = i915_gem_object_pin(ctx, 4096, true);
Chris Wilson9ea8d052010-01-04 18:57:56 +00007992 if (ret) {
7993 DRM_ERROR("failed to pin power context: %d\n", ret);
7994 goto err_unref;
7995 }
7996
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08007997 ret = i915_gem_object_set_to_gtt_domain(ctx, 1);
Chris Wilson9ea8d052010-01-04 18:57:56 +00007998 if (ret) {
7999 DRM_ERROR("failed to set-domain on power context: %d\n", ret);
8000 goto err_unpin;
8001 }
Chris Wilson9ea8d052010-01-04 18:57:56 +00008002
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08008003 return ctx;
Chris Wilson9ea8d052010-01-04 18:57:56 +00008004
8005err_unpin:
Zou Nan haiaa40d6b2010-06-25 13:40:23 +08008006 i915_gem_object_unpin(ctx);
Chris Wilson9ea8d052010-01-04 18:57:56 +00008007err_unref:
Chris Wilson05394f32010-11-08 19:18:58 +00008008 drm_gem_object_unreference(&ctx->base);
Chris Wilson9ea8d052010-01-04 18:57:56 +00008009 mutex_unlock(&dev->struct_mutex);
8010 return NULL;
8011}
8012
Jesse Barnes7648fa92010-05-20 14:28:11 -07008013bool ironlake_set_drps(struct drm_device *dev, u8 val)
8014{
8015 struct drm_i915_private *dev_priv = dev->dev_private;
8016 u16 rgvswctl;
8017
8018 rgvswctl = I915_READ16(MEMSWCTL);
8019 if (rgvswctl & MEMCTL_CMD_STS) {
8020 DRM_DEBUG("gpu busy, RCS change rejected\n");
8021 return false; /* still busy with another command */
8022 }
8023
8024 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
8025 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
8026 I915_WRITE16(MEMSWCTL, rgvswctl);
8027 POSTING_READ16(MEMSWCTL);
8028
8029 rgvswctl |= MEMCTL_CMD_STS;
8030 I915_WRITE16(MEMSWCTL, rgvswctl);
8031
8032 return true;
8033}
8034
Jesse Barnesf97108d2010-01-29 11:27:07 -08008035void ironlake_enable_drps(struct drm_device *dev)
8036{
8037 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07008038 u32 rgvmodectl = I915_READ(MEMMODECTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008039 u8 fmax, fmin, fstart, vstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08008040
Jesse Barnesea056c12010-09-10 10:02:13 -07008041 /* Enable temp reporting */
8042 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
8043 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
8044
Jesse Barnesf97108d2010-01-29 11:27:07 -08008045 /* 100ms RC evaluation intervals */
8046 I915_WRITE(RCUPEI, 100000);
8047 I915_WRITE(RCDNEI, 100000);
8048
8049 /* Set max/min thresholds to 90ms and 80ms respectively */
8050 I915_WRITE(RCBMAXAVG, 90000);
8051 I915_WRITE(RCBMINAVG, 80000);
8052
8053 I915_WRITE(MEMIHYST, 1);
8054
8055 /* Set up min, max, and cur for interrupt handling */
8056 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
8057 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
8058 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
8059 MEMMODE_FSTART_SHIFT;
Jesse Barnes7648fa92010-05-20 14:28:11 -07008060
Jesse Barnesf97108d2010-01-29 11:27:07 -08008061 vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
8062 PXVFREQ_PX_SHIFT;
8063
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07008064 dev_priv->fmax = fmax; /* IPS callback will increase this */
Jesse Barnes7648fa92010-05-20 14:28:11 -07008065 dev_priv->fstart = fstart;
8066
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07008067 dev_priv->max_delay = fstart;
Jesse Barnesf97108d2010-01-29 11:27:07 -08008068 dev_priv->min_delay = fmin;
8069 dev_priv->cur_delay = fstart;
8070
Jesse Barnes80dbf4b2010-11-01 14:12:01 -07008071 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
8072 fmax, fmin, fstart);
Jesse Barnes7648fa92010-05-20 14:28:11 -07008073
Jesse Barnesf97108d2010-01-29 11:27:07 -08008074 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
8075
8076 /*
8077 * Interrupts will be enabled in ironlake_irq_postinstall
8078 */
8079
8080 I915_WRITE(VIDSTART, vstart);
8081 POSTING_READ(VIDSTART);
8082
8083 rgvmodectl |= MEMMODE_SWMODE_EN;
8084 I915_WRITE(MEMMODECTL, rgvmodectl);
8085
Chris Wilson481b6af2010-08-23 17:43:35 +01008086 if (wait_for((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
Chris Wilson913d8d12010-08-07 11:01:35 +01008087 DRM_ERROR("stuck trying to change perf mode\n");
Jesse Barnesf97108d2010-01-29 11:27:07 -08008088 msleep(1);
8089
Jesse Barnes7648fa92010-05-20 14:28:11 -07008090 ironlake_set_drps(dev, fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008091
Jesse Barnes7648fa92010-05-20 14:28:11 -07008092 dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
8093 I915_READ(0x112e0);
8094 dev_priv->last_time1 = jiffies_to_msecs(jiffies);
8095 dev_priv->last_count2 = I915_READ(0x112f4);
8096 getrawmonotonic(&dev_priv->last_time2);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008097}
8098
8099void ironlake_disable_drps(struct drm_device *dev)
8100{
8101 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes7648fa92010-05-20 14:28:11 -07008102 u16 rgvswctl = I915_READ16(MEMSWCTL);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008103
8104 /* Ack interrupts, disable EFC interrupt */
8105 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
8106 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
8107 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
8108 I915_WRITE(DEIIR, DE_PCU_EVENT);
8109 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
8110
8111 /* Go back to the starting frequency */
Jesse Barnes7648fa92010-05-20 14:28:11 -07008112 ironlake_set_drps(dev, dev_priv->fstart);
Jesse Barnesf97108d2010-01-29 11:27:07 -08008113 msleep(1);
8114 rgvswctl |= MEMCTL_CMD_STS;
8115 I915_WRITE(MEMSWCTL, rgvswctl);
8116 msleep(1);
8117
8118}
8119
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008120void gen6_set_rps(struct drm_device *dev, u8 val)
8121{
8122 struct drm_i915_private *dev_priv = dev->dev_private;
8123 u32 swreq;
8124
8125 swreq = (val & 0x3ff) << 25;
8126 I915_WRITE(GEN6_RPNSWREQ, swreq);
8127}
8128
8129void gen6_disable_rps(struct drm_device *dev)
8130{
8131 struct drm_i915_private *dev_priv = dev->dev_private;
8132
8133 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
8134 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
8135 I915_WRITE(GEN6_PMIER, 0);
Daniel Vetter6fdd4d92011-09-08 14:00:22 +02008136 /* Complete PM interrupt masking here doesn't race with the rps work
8137 * item again unmasking PM interrupts because that is using a different
8138 * register (PMIMR) to mask PM interrupts. The only risk is in leaving
8139 * stale bits in PMIIR and PMIMR which gen6_enable_rps will clean up. */
Ben Widawsky4912d042011-04-25 11:25:20 -07008140
8141 spin_lock_irq(&dev_priv->rps_lock);
8142 dev_priv->pm_iir = 0;
8143 spin_unlock_irq(&dev_priv->rps_lock);
8144
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008145 I915_WRITE(GEN6_PMIIR, I915_READ(GEN6_PMIIR));
8146}
8147
Jesse Barnes7648fa92010-05-20 14:28:11 -07008148static unsigned long intel_pxfreq(u32 vidfreq)
8149{
8150 unsigned long freq;
8151 int div = (vidfreq & 0x3f0000) >> 16;
8152 int post = (vidfreq & 0x3000) >> 12;
8153 int pre = (vidfreq & 0x7);
8154
8155 if (!pre)
8156 return 0;
8157
8158 freq = ((div * 133333) / ((1<<post) * pre));
8159
8160 return freq;
8161}
8162
8163void intel_init_emon(struct drm_device *dev)
8164{
8165 struct drm_i915_private *dev_priv = dev->dev_private;
8166 u32 lcfuse;
8167 u8 pxw[16];
8168 int i;
8169
8170 /* Disable to program */
8171 I915_WRITE(ECR, 0);
8172 POSTING_READ(ECR);
8173
8174 /* Program energy weights for various events */
8175 I915_WRITE(SDEW, 0x15040d00);
8176 I915_WRITE(CSIEW0, 0x007f0000);
8177 I915_WRITE(CSIEW1, 0x1e220004);
8178 I915_WRITE(CSIEW2, 0x04000004);
8179
8180 for (i = 0; i < 5; i++)
8181 I915_WRITE(PEW + (i * 4), 0);
8182 for (i = 0; i < 3; i++)
8183 I915_WRITE(DEW + (i * 4), 0);
8184
8185 /* Program P-state weights to account for frequency power adjustment */
8186 for (i = 0; i < 16; i++) {
8187 u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
8188 unsigned long freq = intel_pxfreq(pxvidfreq);
8189 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
8190 PXVFREQ_PX_SHIFT;
8191 unsigned long val;
8192
8193 val = vid * vid;
8194 val *= (freq / 1000);
8195 val *= 255;
8196 val /= (127*127*900);
8197 if (val > 0xff)
8198 DRM_ERROR("bad pxval: %ld\n", val);
8199 pxw[i] = val;
8200 }
8201 /* Render standby states get 0 weight */
8202 pxw[14] = 0;
8203 pxw[15] = 0;
8204
8205 for (i = 0; i < 4; i++) {
8206 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
8207 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
8208 I915_WRITE(PXW + (i * 4), val);
8209 }
8210
8211 /* Adjust magic regs to magic values (more experimental results) */
8212 I915_WRITE(OGW0, 0);
8213 I915_WRITE(OGW1, 0);
8214 I915_WRITE(EG0, 0x00007f00);
8215 I915_WRITE(EG1, 0x0000000e);
8216 I915_WRITE(EG2, 0x000e0000);
8217 I915_WRITE(EG3, 0x68000300);
8218 I915_WRITE(EG4, 0x42000000);
8219 I915_WRITE(EG5, 0x00140031);
8220 I915_WRITE(EG6, 0);
8221 I915_WRITE(EG7, 0);
8222
8223 for (i = 0; i < 8; i++)
8224 I915_WRITE(PXWL + (i * 4), 0);
8225
8226 /* Enable PMON + select events */
8227 I915_WRITE(ECR, 0x80000019);
8228
8229 lcfuse = I915_READ(LCFUSE02);
8230
8231 dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
8232}
8233
Keith Packardc0f372b32011-11-16 22:24:52 -08008234static bool intel_enable_rc6(struct drm_device *dev)
8235{
8236 /*
8237 * Respect the kernel parameter if it is set
8238 */
8239 if (i915_enable_rc6 >= 0)
8240 return i915_enable_rc6;
8241
8242 /*
8243 * Disable RC6 on Ironlake
8244 */
8245 if (INTEL_INFO(dev)->gen == 5)
8246 return 0;
8247
8248 /*
Keith Packard371de6e2011-12-26 17:02:11 -08008249 * Disable rc6 on Sandybridge
Keith Packardc0f372b32011-11-16 22:24:52 -08008250 */
8251 if (INTEL_INFO(dev)->gen == 6) {
Keith Packard371de6e2011-12-26 17:02:11 -08008252 DRM_DEBUG_DRIVER("Sandybridge: RC6 disabled\n");
8253 return 0;
Keith Packardc0f372b32011-11-16 22:24:52 -08008254 }
8255 DRM_DEBUG_DRIVER("RC6 enabled\n");
8256 return 1;
8257}
8258
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008259void gen6_enable_rps(struct drm_i915_private *dev_priv)
Chris Wilson8fd26852010-12-08 18:40:43 +00008260{
Jesse Barnesa6044e22010-12-20 11:34:20 -08008261 u32 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
8262 u32 gt_perf_status = I915_READ(GEN6_GT_PERF_STATUS);
Jesse Barnes7df87212011-03-30 14:08:56 -07008263 u32 pcu_mbox, rc6_mask = 0;
Ben Widawskydd202c62012-02-09 10:15:18 +01008264 u32 gtfifodbg;
Jesse Barnesa6044e22010-12-20 11:34:20 -08008265 int cur_freq, min_freq, max_freq;
Chris Wilson8fd26852010-12-08 18:40:43 +00008266 int i;
8267
8268 /* Here begins a magic sequence of register writes to enable
8269 * auto-downclocking.
8270 *
8271 * Perhaps there might be some value in exposing these to
8272 * userspace...
8273 */
8274 I915_WRITE(GEN6_RC_STATE, 0);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01008275 mutex_lock(&dev_priv->dev->struct_mutex);
Ben Widawskydd202c62012-02-09 10:15:18 +01008276
8277 /* Clear the DBG now so we don't confuse earlier errors */
8278 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
8279 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
8280 I915_WRITE(GTFIFODBG, gtfifodbg);
8281 }
8282
Ben Widawskyfcca7922011-04-25 11:23:07 -07008283 gen6_gt_force_wake_get(dev_priv);
Chris Wilson8fd26852010-12-08 18:40:43 +00008284
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008285 /* disable the counters and set deterministic thresholds */
Chris Wilson8fd26852010-12-08 18:40:43 +00008286 I915_WRITE(GEN6_RC_CONTROL, 0);
8287
8288 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
8289 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
8290 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
8291 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
8292 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
8293
8294 for (i = 0; i < I915_NUM_RINGS; i++)
8295 I915_WRITE(RING_MAX_IDLE(dev_priv->ring[i].mmio_base), 10);
8296
8297 I915_WRITE(GEN6_RC_SLEEP, 0);
8298 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
8299 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
8300 I915_WRITE(GEN6_RC6p_THRESHOLD, 100000);
8301 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
8302
Keith Packardc0f372b32011-11-16 22:24:52 -08008303 if (intel_enable_rc6(dev_priv->dev))
Jesse Barnes7df87212011-03-30 14:08:56 -07008304 rc6_mask = GEN6_RC_CTL_RC6p_ENABLE |
8305 GEN6_RC_CTL_RC6_ENABLE;
8306
Chris Wilson8fd26852010-12-08 18:40:43 +00008307 I915_WRITE(GEN6_RC_CONTROL,
Jesse Barnes7df87212011-03-30 14:08:56 -07008308 rc6_mask |
Chris Wilson9c3d2f72010-12-17 10:54:26 +00008309 GEN6_RC_CTL_EI_MODE(1) |
Chris Wilson8fd26852010-12-08 18:40:43 +00008310 GEN6_RC_CTL_HW_ENABLE);
8311
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008312 I915_WRITE(GEN6_RPNSWREQ,
Chris Wilson8fd26852010-12-08 18:40:43 +00008313 GEN6_FREQUENCY(10) |
8314 GEN6_OFFSET(0) |
8315 GEN6_AGGRESSIVE_TURBO);
8316 I915_WRITE(GEN6_RC_VIDEO_FREQ,
8317 GEN6_FREQUENCY(12));
8318
8319 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
8320 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
8321 18 << 24 |
8322 6 << 16);
Jesse Barnesccab5c82011-01-18 15:49:25 -08008323 I915_WRITE(GEN6_RP_UP_THRESHOLD, 10000);
8324 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 1000000);
Chris Wilson8fd26852010-12-08 18:40:43 +00008325 I915_WRITE(GEN6_RP_UP_EI, 100000);
Jesse Barnesccab5c82011-01-18 15:49:25 -08008326 I915_WRITE(GEN6_RP_DOWN_EI, 5000000);
Chris Wilson8fd26852010-12-08 18:40:43 +00008327 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
8328 I915_WRITE(GEN6_RP_CONTROL,
8329 GEN6_RP_MEDIA_TURBO |
Ben Widawsky6ed55ee2011-12-12 19:21:59 -08008330 GEN6_RP_MEDIA_HW_MODE |
Chris Wilson8fd26852010-12-08 18:40:43 +00008331 GEN6_RP_MEDIA_IS_GFX |
8332 GEN6_RP_ENABLE |
Jesse Barnesccab5c82011-01-18 15:49:25 -08008333 GEN6_RP_UP_BUSY_AVG |
8334 GEN6_RP_DOWN_IDLE_CONT);
Chris Wilson8fd26852010-12-08 18:40:43 +00008335
8336 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8337 500))
8338 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
8339
8340 I915_WRITE(GEN6_PCODE_DATA, 0);
8341 I915_WRITE(GEN6_PCODE_MAILBOX,
8342 GEN6_PCODE_READY |
8343 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
8344 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8345 500))
8346 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
8347
Jesse Barnesa6044e22010-12-20 11:34:20 -08008348 min_freq = (rp_state_cap & 0xff0000) >> 16;
8349 max_freq = rp_state_cap & 0xff;
8350 cur_freq = (gt_perf_status & 0xff00) >> 8;
8351
8352 /* Check for overclock support */
8353 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8354 500))
8355 DRM_ERROR("timeout waiting for pcode mailbox to become idle\n");
8356 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_READ_OC_PARAMS);
8357 pcu_mbox = I915_READ(GEN6_PCODE_DATA);
8358 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
8359 500))
8360 DRM_ERROR("timeout waiting for pcode mailbox to finish\n");
8361 if (pcu_mbox & (1<<31)) { /* OC supported */
8362 max_freq = pcu_mbox & 0xff;
Jesse Barnese281fca2011-03-18 10:32:07 -07008363 DRM_DEBUG_DRIVER("overclocking supported, adjusting frequency max to %dMHz\n", pcu_mbox * 50);
Jesse Barnesa6044e22010-12-20 11:34:20 -08008364 }
8365
8366 /* In units of 100MHz */
8367 dev_priv->max_delay = max_freq;
8368 dev_priv->min_delay = min_freq;
8369 dev_priv->cur_delay = cur_freq;
8370
Chris Wilson8fd26852010-12-08 18:40:43 +00008371 /* requires MSI enabled */
8372 I915_WRITE(GEN6_PMIER,
8373 GEN6_PM_MBOX_EVENT |
8374 GEN6_PM_THERMAL_EVENT |
8375 GEN6_PM_RP_DOWN_TIMEOUT |
8376 GEN6_PM_RP_UP_THRESHOLD |
8377 GEN6_PM_RP_DOWN_THRESHOLD |
8378 GEN6_PM_RP_UP_EI_EXPIRED |
8379 GEN6_PM_RP_DOWN_EI_EXPIRED);
Ben Widawsky4912d042011-04-25 11:25:20 -07008380 spin_lock_irq(&dev_priv->rps_lock);
8381 WARN_ON(dev_priv->pm_iir != 0);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008382 I915_WRITE(GEN6_PMIMR, 0);
Ben Widawsky4912d042011-04-25 11:25:20 -07008383 spin_unlock_irq(&dev_priv->rps_lock);
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08008384 /* enable all PM interrupts */
8385 I915_WRITE(GEN6_PMINTRMSK, 0);
Chris Wilson8fd26852010-12-08 18:40:43 +00008386
Ben Widawskyfcca7922011-04-25 11:23:07 -07008387 gen6_gt_force_wake_put(dev_priv);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01008388 mutex_unlock(&dev_priv->dev->struct_mutex);
Chris Wilson8fd26852010-12-08 18:40:43 +00008389}
8390
Jesse Barnes23b2f8b2011-06-28 13:04:16 -07008391void gen6_update_ring_freq(struct drm_i915_private *dev_priv)
8392{
8393 int min_freq = 15;
8394 int gpu_freq, ia_freq, max_ia_freq;
8395 int scaling_factor = 180;
8396
8397 max_ia_freq = cpufreq_quick_get_max(0);
8398 /*
8399 * Default to measured freq if none found, PCU will ensure we don't go
8400 * over
8401 */
8402 if (!max_ia_freq)
8403 max_ia_freq = tsc_khz;
8404
8405 /* Convert from kHz to MHz */
8406 max_ia_freq /= 1000;
8407
8408 mutex_lock(&dev_priv->dev->struct_mutex);
8409
8410 /*
8411 * For each potential GPU frequency, load a ring frequency we'd like
8412 * to use for memory access. We do this by specifying the IA frequency
8413 * the PCU should use as a reference to determine the ring frequency.
8414 */
8415 for (gpu_freq = dev_priv->max_delay; gpu_freq >= dev_priv->min_delay;
8416 gpu_freq--) {
8417 int diff = dev_priv->max_delay - gpu_freq;
8418
8419 /*
8420 * For GPU frequencies less than 750MHz, just use the lowest
8421 * ring freq.
8422 */
8423 if (gpu_freq < min_freq)
8424 ia_freq = 800;
8425 else
8426 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
8427 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
8428
8429 I915_WRITE(GEN6_PCODE_DATA,
8430 (ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT) |
8431 gpu_freq);
8432 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY |
8433 GEN6_PCODE_WRITE_MIN_FREQ_TABLE);
8434 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) &
8435 GEN6_PCODE_READY) == 0, 10)) {
8436 DRM_ERROR("pcode write of freq table timed out\n");
8437 continue;
8438 }
8439 }
8440
8441 mutex_unlock(&dev_priv->dev->struct_mutex);
8442}
8443
Jesse Barnes6067aae2011-04-28 15:04:31 -07008444static void ironlake_init_clock_gating(struct drm_device *dev)
8445{
8446 struct drm_i915_private *dev_priv = dev->dev_private;
8447 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
8448
8449 /* Required for FBC */
8450 dspclk_gate |= DPFCUNIT_CLOCK_GATE_DISABLE |
8451 DPFCRUNIT_CLOCK_GATE_DISABLE |
8452 DPFDUNIT_CLOCK_GATE_DISABLE;
8453 /* Required for CxSR */
8454 dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
8455
8456 I915_WRITE(PCH_3DCGDIS0,
8457 MARIUNIT_CLOCK_GATE_DISABLE |
8458 SVSMUNIT_CLOCK_GATE_DISABLE);
8459 I915_WRITE(PCH_3DCGDIS1,
8460 VFMUNIT_CLOCK_GATE_DISABLE);
8461
8462 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
8463
8464 /*
Jesse Barnes6067aae2011-04-28 15:04:31 -07008465 * According to the spec the following bits should be set in
8466 * order to enable memory self-refresh
8467 * The bit 22/21 of 0x42004
8468 * The bit 5 of 0x42020
8469 * The bit 15 of 0x45000
8470 */
8471 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8472 (I915_READ(ILK_DISPLAY_CHICKEN2) |
8473 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
8474 I915_WRITE(ILK_DSPCLK_GATE,
8475 (I915_READ(ILK_DSPCLK_GATE) |
8476 ILK_DPARB_CLK_GATE));
8477 I915_WRITE(DISP_ARB_CTL,
8478 (I915_READ(DISP_ARB_CTL) |
8479 DISP_FBC_WM_DIS));
8480 I915_WRITE(WM3_LP_ILK, 0);
8481 I915_WRITE(WM2_LP_ILK, 0);
8482 I915_WRITE(WM1_LP_ILK, 0);
8483
8484 /*
8485 * Based on the document from hardware guys the following bits
8486 * should be set unconditionally in order to enable FBC.
8487 * The bit 22 of 0x42000
8488 * The bit 22 of 0x42004
8489 * The bit 7,8,9 of 0x42020.
8490 */
8491 if (IS_IRONLAKE_M(dev)) {
8492 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8493 I915_READ(ILK_DISPLAY_CHICKEN1) |
8494 ILK_FBCQ_DIS);
8495 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8496 I915_READ(ILK_DISPLAY_CHICKEN2) |
8497 ILK_DPARB_GATE);
8498 I915_WRITE(ILK_DSPCLK_GATE,
8499 I915_READ(ILK_DSPCLK_GATE) |
8500 ILK_DPFC_DIS1 |
8501 ILK_DPFC_DIS2 |
8502 ILK_CLK_FBC);
8503 }
8504
8505 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8506 I915_READ(ILK_DISPLAY_CHICKEN2) |
8507 ILK_ELPIN_409_SELECT);
8508 I915_WRITE(_3D_CHICKEN2,
8509 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
8510 _3D_CHICKEN2_WM_READ_PIPELINED);
8511}
8512
8513static void gen6_init_clock_gating(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008514{
8515 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08008516 int pipe;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008517 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
8518
8519 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
Jesse Barnes652c3932009-08-17 13:31:43 -07008520
Jesse Barnes6067aae2011-04-28 15:04:31 -07008521 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8522 I915_READ(ILK_DISPLAY_CHICKEN2) |
8523 ILK_ELPIN_409_SELECT);
Eric Anholt8956c8b2010-03-18 13:21:14 -07008524
Jesse Barnes6067aae2011-04-28 15:04:31 -07008525 I915_WRITE(WM3_LP_ILK, 0);
8526 I915_WRITE(WM2_LP_ILK, 0);
8527 I915_WRITE(WM1_LP_ILK, 0);
Eric Anholt8956c8b2010-03-18 13:21:14 -07008528
Eric Anholt406478d2011-11-07 16:07:04 -08008529 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
8530 * gating disable must be set. Failure to set it results in
8531 * flickering pixels due to Z write ordering failures after
8532 * some amount of runtime in the Mesa "fire" demo, and Unigine
8533 * Sanctuary and Tropics, and apparently anything else with
8534 * alpha test or pixel discard.
Eric Anholt9ca1d102011-11-07 16:07:05 -08008535 *
8536 * According to the spec, bit 11 (RCCUNIT) must also be set,
8537 * but we didn't debug actual testcases to find it out.
Eric Anholt406478d2011-11-07 16:07:04 -08008538 */
Eric Anholt9ca1d102011-11-07 16:07:05 -08008539 I915_WRITE(GEN6_UCGCTL2,
8540 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
8541 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
Eric Anholt406478d2011-11-07 16:07:04 -08008542
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008543 /*
Jesse Barnes6067aae2011-04-28 15:04:31 -07008544 * According to the spec the following bits should be
8545 * set in order to enable memory self-refresh and fbc:
8546 * The bit21 and bit22 of 0x42000
8547 * The bit21 and bit22 of 0x42004
8548 * The bit5 and bit7 of 0x42020
8549 * The bit14 of 0x70180
8550 * The bit14 of 0x71180
Jesse Barnes382b0932010-10-07 16:01:25 -07008551 */
Jesse Barnes6067aae2011-04-28 15:04:31 -07008552 I915_WRITE(ILK_DISPLAY_CHICKEN1,
8553 I915_READ(ILK_DISPLAY_CHICKEN1) |
8554 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
8555 I915_WRITE(ILK_DISPLAY_CHICKEN2,
8556 I915_READ(ILK_DISPLAY_CHICKEN2) |
8557 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
8558 I915_WRITE(ILK_DSPCLK_GATE,
8559 I915_READ(ILK_DSPCLK_GATE) |
8560 ILK_DPARB_CLK_GATE |
8561 ILK_DPFD_CLK_GATE);
Jesse Barnes382b0932010-10-07 16:01:25 -07008562
Keith Packardd74362c2011-07-28 14:47:14 -07008563 for_each_pipe(pipe) {
Jesse Barnes6067aae2011-04-28 15:04:31 -07008564 I915_WRITE(DSPCNTR(pipe),
8565 I915_READ(DSPCNTR(pipe)) |
8566 DISPPLANE_TRICKLE_FEED_DISABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07008567 intel_flush_display_plane(dev_priv, pipe);
8568 }
Jesse Barnes6067aae2011-04-28 15:04:31 -07008569}
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008570
Jesse Barnes28963a32011-05-11 09:42:30 -07008571static void ivybridge_init_clock_gating(struct drm_device *dev)
8572{
8573 struct drm_i915_private *dev_priv = dev->dev_private;
8574 int pipe;
8575 uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
Jesse Barnes652c3932009-08-17 13:31:43 -07008576
Jesse Barnes28963a32011-05-11 09:42:30 -07008577 I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008578
Jesse Barnes28963a32011-05-11 09:42:30 -07008579 I915_WRITE(WM3_LP_ILK, 0);
8580 I915_WRITE(WM2_LP_ILK, 0);
8581 I915_WRITE(WM1_LP_ILK, 0);
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008582
Jesse Barnes28963a32011-05-11 09:42:30 -07008583 I915_WRITE(ILK_DSPCLK_GATE, IVB_VRHUNIT_CLK_GATE);
Eric Anholtde6e2ea2010-11-06 14:53:32 -07008584
Eric Anholt116ac8d2011-12-21 10:31:09 -08008585 I915_WRITE(IVB_CHICKEN3,
8586 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
8587 CHICKEN3_DGMG_DONE_FIX_DISABLE);
8588
Keith Packardd74362c2011-07-28 14:47:14 -07008589 for_each_pipe(pipe) {
Jesse Barnes28963a32011-05-11 09:42:30 -07008590 I915_WRITE(DSPCNTR(pipe),
8591 I915_READ(DSPCNTR(pipe)) |
8592 DISPPLANE_TRICKLE_FEED_DISABLE);
Keith Packardd74362c2011-07-28 14:47:14 -07008593 intel_flush_display_plane(dev_priv, pipe);
8594 }
Jesse Barnes28963a32011-05-11 09:42:30 -07008595}
Eric Anholt67e92af2010-11-06 14:53:33 -07008596
Jesse Barnes6067aae2011-04-28 15:04:31 -07008597static void g4x_init_clock_gating(struct drm_device *dev)
8598{
8599 struct drm_i915_private *dev_priv = dev->dev_private;
8600 uint32_t dspclk_gate;
Chris Wilson8fd26852010-12-08 18:40:43 +00008601
Jesse Barnes6067aae2011-04-28 15:04:31 -07008602 I915_WRITE(RENCLK_GATE_D1, 0);
8603 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
8604 GS_UNIT_CLOCK_GATE_DISABLE |
8605 CL_UNIT_CLOCK_GATE_DISABLE);
8606 I915_WRITE(RAMCLK_GATE_D, 0);
8607 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
8608 OVRUNIT_CLOCK_GATE_DISABLE |
8609 OVCUNIT_CLOCK_GATE_DISABLE;
8610 if (IS_GM45(dev))
8611 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
8612 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
8613}
Yuanhan Liu13982612010-12-15 15:42:31 +08008614
Jesse Barnes6067aae2011-04-28 15:04:31 -07008615static void crestline_init_clock_gating(struct drm_device *dev)
8616{
8617 struct drm_i915_private *dev_priv = dev->dev_private;
Yuanhan Liu13982612010-12-15 15:42:31 +08008618
Jesse Barnes6067aae2011-04-28 15:04:31 -07008619 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
8620 I915_WRITE(RENCLK_GATE_D2, 0);
8621 I915_WRITE(DSPCLK_GATE_D, 0);
8622 I915_WRITE(RAMCLK_GATE_D, 0);
8623 I915_WRITE16(DEUC, 0);
8624}
Jesse Barnes652c3932009-08-17 13:31:43 -07008625
Jesse Barnes6067aae2011-04-28 15:04:31 -07008626static void broadwater_init_clock_gating(struct drm_device *dev)
8627{
8628 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008629
Jesse Barnes6067aae2011-04-28 15:04:31 -07008630 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
8631 I965_RCC_CLOCK_GATE_DISABLE |
8632 I965_RCPB_CLOCK_GATE_DISABLE |
8633 I965_ISC_CLOCK_GATE_DISABLE |
8634 I965_FBC_CLOCK_GATE_DISABLE);
8635 I915_WRITE(RENCLK_GATE_D2, 0);
8636}
Jesse Barnes652c3932009-08-17 13:31:43 -07008637
Jesse Barnes6067aae2011-04-28 15:04:31 -07008638static void gen3_init_clock_gating(struct drm_device *dev)
8639{
8640 struct drm_i915_private *dev_priv = dev->dev_private;
8641 u32 dstate = I915_READ(D_STATE);
8642
8643 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
8644 DSTATE_DOT_CLOCK_GATING;
8645 I915_WRITE(D_STATE, dstate);
8646}
8647
8648static void i85x_init_clock_gating(struct drm_device *dev)
8649{
8650 struct drm_i915_private *dev_priv = dev->dev_private;
8651
8652 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
8653}
8654
8655static void i830_init_clock_gating(struct drm_device *dev)
8656{
8657 struct drm_i915_private *dev_priv = dev->dev_private;
8658
8659 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
Jesse Barnes652c3932009-08-17 13:31:43 -07008660}
8661
Jesse Barnes645c62a2011-05-11 09:49:31 -07008662static void ibx_init_clock_gating(struct drm_device *dev)
8663{
8664 struct drm_i915_private *dev_priv = dev->dev_private;
8665
8666 /*
8667 * On Ibex Peak and Cougar Point, we need to disable clock
8668 * gating for the panel power sequencer or it will fail to
8669 * start up when no ports are active.
8670 */
8671 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
8672}
8673
8674static void cpt_init_clock_gating(struct drm_device *dev)
8675{
8676 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes3bcf6032011-07-27 11:51:40 -07008677 int pipe;
Jesse Barnes645c62a2011-05-11 09:49:31 -07008678
8679 /*
8680 * On Ibex Peak and Cougar Point, we need to disable clock
8681 * gating for the panel power sequencer or it will fail to
8682 * start up when no ports are active.
8683 */
8684 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
8685 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
8686 DPLS_EDP_PPS_FIX_DIS);
Jesse Barnes3bcf6032011-07-27 11:51:40 -07008687 /* Without this, mode sets may fail silently on FDI */
8688 for_each_pipe(pipe)
8689 I915_WRITE(TRANS_CHICKEN2(pipe), TRANS_AUTOTRAIN_GEN_STALL_DIS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008690}
8691
Chris Wilsonac668082011-02-09 16:15:32 +00008692static void ironlake_teardown_rc6(struct drm_device *dev)
Chris Wilson0cdab212010-12-05 17:27:06 +00008693{
8694 struct drm_i915_private *dev_priv = dev->dev_private;
8695
8696 if (dev_priv->renderctx) {
Chris Wilsonac668082011-02-09 16:15:32 +00008697 i915_gem_object_unpin(dev_priv->renderctx);
8698 drm_gem_object_unreference(&dev_priv->renderctx->base);
Chris Wilson0cdab212010-12-05 17:27:06 +00008699 dev_priv->renderctx = NULL;
8700 }
8701
8702 if (dev_priv->pwrctx) {
Chris Wilsonac668082011-02-09 16:15:32 +00008703 i915_gem_object_unpin(dev_priv->pwrctx);
8704 drm_gem_object_unreference(&dev_priv->pwrctx->base);
Chris Wilson0cdab212010-12-05 17:27:06 +00008705 dev_priv->pwrctx = NULL;
8706 }
8707}
8708
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008709static void ironlake_disable_rc6(struct drm_device *dev)
8710{
8711 struct drm_i915_private *dev_priv = dev->dev_private;
8712
Chris Wilsonac668082011-02-09 16:15:32 +00008713 if (I915_READ(PWRCTXA)) {
8714 /* Wake the GPU, prevent RC6, then restore RSTDBYCTL */
8715 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) | RCX_SW_EXIT);
8716 wait_for(((I915_READ(RSTDBYCTL) & RSX_STATUS_MASK) == RSX_STATUS_ON),
8717 50);
8718
8719 I915_WRITE(PWRCTXA, 0);
8720 POSTING_READ(PWRCTXA);
8721
8722 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
8723 POSTING_READ(RSTDBYCTL);
8724 }
8725
Chris Wilson99507302011-02-24 09:42:52 +00008726 ironlake_teardown_rc6(dev);
Chris Wilsonac668082011-02-09 16:15:32 +00008727}
8728
8729static int ironlake_setup_rc6(struct drm_device *dev)
8730{
8731 struct drm_i915_private *dev_priv = dev->dev_private;
8732
8733 if (dev_priv->renderctx == NULL)
8734 dev_priv->renderctx = intel_alloc_context_page(dev);
8735 if (!dev_priv->renderctx)
8736 return -ENOMEM;
8737
8738 if (dev_priv->pwrctx == NULL)
8739 dev_priv->pwrctx = intel_alloc_context_page(dev);
8740 if (!dev_priv->pwrctx) {
8741 ironlake_teardown_rc6(dev);
8742 return -ENOMEM;
8743 }
8744
8745 return 0;
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008746}
8747
8748void ironlake_enable_rc6(struct drm_device *dev)
8749{
8750 struct drm_i915_private *dev_priv = dev->dev_private;
8751 int ret;
8752
Chris Wilsonac668082011-02-09 16:15:32 +00008753 /* rc6 disabled by default due to repeated reports of hanging during
8754 * boot and resume.
8755 */
Keith Packardc0f372b32011-11-16 22:24:52 -08008756 if (!intel_enable_rc6(dev))
Chris Wilsonac668082011-02-09 16:15:32 +00008757 return;
8758
Ben Widawsky2c34b852011-03-19 18:14:26 -07008759 mutex_lock(&dev->struct_mutex);
Chris Wilsonac668082011-02-09 16:15:32 +00008760 ret = ironlake_setup_rc6(dev);
Ben Widawsky2c34b852011-03-19 18:14:26 -07008761 if (ret) {
8762 mutex_unlock(&dev->struct_mutex);
Chris Wilsonac668082011-02-09 16:15:32 +00008763 return;
Ben Widawsky2c34b852011-03-19 18:14:26 -07008764 }
Chris Wilsonac668082011-02-09 16:15:32 +00008765
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008766 /*
8767 * GPU can automatically power down the render unit if given a page
8768 * to save state.
8769 */
8770 ret = BEGIN_LP_RING(6);
8771 if (ret) {
Chris Wilsonac668082011-02-09 16:15:32 +00008772 ironlake_teardown_rc6(dev);
Ben Widawsky2c34b852011-03-19 18:14:26 -07008773 mutex_unlock(&dev->struct_mutex);
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008774 return;
8775 }
Chris Wilsonac668082011-02-09 16:15:32 +00008776
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008777 OUT_RING(MI_SUSPEND_FLUSH | MI_SUSPEND_FLUSH_EN);
8778 OUT_RING(MI_SET_CONTEXT);
8779 OUT_RING(dev_priv->renderctx->gtt_offset |
8780 MI_MM_SPACE_GTT |
8781 MI_SAVE_EXT_STATE_EN |
8782 MI_RESTORE_EXT_STATE_EN |
8783 MI_RESTORE_INHIBIT);
8784 OUT_RING(MI_SUSPEND_FLUSH);
8785 OUT_RING(MI_NOOP);
8786 OUT_RING(MI_FLUSH);
8787 ADVANCE_LP_RING();
8788
Ben Widawsky4a246cf2011-03-19 18:14:28 -07008789 /*
8790 * Wait for the command parser to advance past MI_SET_CONTEXT. The HW
8791 * does an implicit flush, combined with MI_FLUSH above, it should be
8792 * safe to assume that renderctx is valid
8793 */
8794 ret = intel_wait_ring_idle(LP_RING(dev_priv));
8795 if (ret) {
8796 DRM_ERROR("failed to enable ironlake power power savings\n");
8797 ironlake_teardown_rc6(dev);
8798 mutex_unlock(&dev->struct_mutex);
8799 return;
8800 }
8801
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008802 I915_WRITE(PWRCTXA, dev_priv->pwrctx->gtt_offset | PWRCTX_EN);
8803 I915_WRITE(RSTDBYCTL, I915_READ(RSTDBYCTL) & ~RCX_SW_EXIT);
Ben Widawsky2c34b852011-03-19 18:14:26 -07008804 mutex_unlock(&dev->struct_mutex);
Jesse Barnesd5bb0812011-01-05 12:01:26 -08008805}
8806
Jesse Barnes645c62a2011-05-11 09:49:31 -07008807void intel_init_clock_gating(struct drm_device *dev)
8808{
8809 struct drm_i915_private *dev_priv = dev->dev_private;
8810
8811 dev_priv->display.init_clock_gating(dev);
8812
8813 if (dev_priv->display.init_pch_clock_gating)
8814 dev_priv->display.init_pch_clock_gating(dev);
8815}
Chris Wilsonac668082011-02-09 16:15:32 +00008816
Jesse Barnese70236a2009-09-21 10:42:27 -07008817/* Set up chip specific display functions */
8818static void intel_init_display(struct drm_device *dev)
8819{
8820 struct drm_i915_private *dev_priv = dev->dev_private;
8821
8822 /* We always want a DPMS function */
Eric Anholtf564048e2011-03-30 13:01:02 -07008823 if (HAS_PCH_SPLIT(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008824 dev_priv->display.dpms = ironlake_crtc_dpms;
Eric Anholtf564048e2011-03-30 13:01:02 -07008825 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008826 dev_priv->display.update_plane = ironlake_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008827 } else {
Jesse Barnese70236a2009-09-21 10:42:27 -07008828 dev_priv->display.dpms = i9xx_crtc_dpms;
Eric Anholtf564048e2011-03-30 13:01:02 -07008829 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Jesse Barnes17638cd2011-06-24 12:19:23 -07008830 dev_priv->display.update_plane = i9xx_update_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -07008831 }
Jesse Barnese70236a2009-09-21 10:42:27 -07008832
Adam Jacksonee5382a2010-04-23 11:17:39 -04008833 if (I915_HAS_FBC(dev)) {
Yuanhan Liu9c04f012010-12-15 15:42:32 +08008834 if (HAS_PCH_SPLIT(dev)) {
Zhao Yakuib52eb4d2010-06-12 14:32:27 +08008835 dev_priv->display.fbc_enabled = ironlake_fbc_enabled;
8836 dev_priv->display.enable_fbc = ironlake_enable_fbc;
8837 dev_priv->display.disable_fbc = ironlake_disable_fbc;
8838 } else if (IS_GM45(dev)) {
Jesse Barnes74dff282009-09-14 15:39:40 -07008839 dev_priv->display.fbc_enabled = g4x_fbc_enabled;
8840 dev_priv->display.enable_fbc = g4x_enable_fbc;
8841 dev_priv->display.disable_fbc = g4x_disable_fbc;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008842 } else if (IS_CRESTLINE(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07008843 dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
8844 dev_priv->display.enable_fbc = i8xx_enable_fbc;
8845 dev_priv->display.disable_fbc = i8xx_disable_fbc;
8846 }
Jesse Barnes74dff282009-09-14 15:39:40 -07008847 /* 855GM needs testing */
Jesse Barnese70236a2009-09-21 10:42:27 -07008848 }
8849
8850 /* Returns the core display clock speed */
Akshay Joshi0206e352011-08-16 15:34:10 -04008851 if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -07008852 dev_priv->display.get_display_clock_speed =
8853 i945_get_display_clock_speed;
8854 else if (IS_I915G(dev))
8855 dev_priv->display.get_display_clock_speed =
8856 i915_get_display_clock_speed;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008857 else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008858 dev_priv->display.get_display_clock_speed =
8859 i9xx_misc_get_display_clock_speed;
8860 else if (IS_I915GM(dev))
8861 dev_priv->display.get_display_clock_speed =
8862 i915gm_get_display_clock_speed;
8863 else if (IS_I865G(dev))
8864 dev_priv->display.get_display_clock_speed =
8865 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +02008866 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008867 dev_priv->display.get_display_clock_speed =
8868 i855_get_display_clock_speed;
8869 else /* 852, 830 */
8870 dev_priv->display.get_display_clock_speed =
8871 i830_get_display_clock_speed;
8872
8873 /* For FIFO watermark updates */
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008874 if (HAS_PCH_SPLIT(dev)) {
Keith Packard8d715f02011-11-18 20:39:01 -08008875 dev_priv->display.force_wake_get = __gen6_gt_force_wake_get;
8876 dev_priv->display.force_wake_put = __gen6_gt_force_wake_put;
8877
8878 /* IVB configs may use multi-threaded forcewake */
8879 if (IS_IVYBRIDGE(dev)) {
8880 u32 ecobus;
8881
Keith Packardc7dffff2011-12-09 11:33:00 -08008882 /* A small trick here - if the bios hasn't configured MT forcewake,
8883 * and if the device is in RC6, then force_wake_mt_get will not wake
8884 * the device and the ECOBUS read will return zero. Which will be
8885 * (correctly) interpreted by the test below as MT forcewake being
8886 * disabled.
8887 */
Keith Packard8d715f02011-11-18 20:39:01 -08008888 mutex_lock(&dev->struct_mutex);
8889 __gen6_gt_force_wake_mt_get(dev_priv);
Keith Packardc7dffff2011-12-09 11:33:00 -08008890 ecobus = I915_READ_NOTRACE(ECOBUS);
Keith Packard8d715f02011-11-18 20:39:01 -08008891 __gen6_gt_force_wake_mt_put(dev_priv);
8892 mutex_unlock(&dev->struct_mutex);
8893
8894 if (ecobus & FORCEWAKE_MT_ENABLE) {
8895 DRM_DEBUG_KMS("Using MT version of forcewake\n");
8896 dev_priv->display.force_wake_get =
8897 __gen6_gt_force_wake_mt_get;
8898 dev_priv->display.force_wake_put =
8899 __gen6_gt_force_wake_mt_put;
8900 }
8901 }
8902
Jesse Barnes645c62a2011-05-11 09:49:31 -07008903 if (HAS_PCH_IBX(dev))
8904 dev_priv->display.init_pch_clock_gating = ibx_init_clock_gating;
8905 else if (HAS_PCH_CPT(dev))
8906 dev_priv->display.init_pch_clock_gating = cpt_init_clock_gating;
8907
Chris Wilsonf00a3dd2010-10-21 14:57:17 +01008908 if (IS_GEN5(dev)) {
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008909 if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
8910 dev_priv->display.update_wm = ironlake_update_wm;
8911 else {
8912 DRM_DEBUG_KMS("Failed to get proper latency. "
8913 "Disable CxSR\n");
8914 dev_priv->display.update_wm = NULL;
8915 }
Jesse Barnes674cf962011-04-28 14:27:04 -07008916 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008917 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
Wu Fengguange0dac652011-09-05 14:25:34 +08008918 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +08008919 } else if (IS_GEN6(dev)) {
8920 if (SNB_READ_WM0_LATENCY()) {
8921 dev_priv->display.update_wm = sandybridge_update_wm;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08008922 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
Yuanhan Liu13982612010-12-15 15:42:31 +08008923 } else {
8924 DRM_DEBUG_KMS("Failed to read display plane latency. "
8925 "Disable CxSR\n");
8926 dev_priv->display.update_wm = NULL;
8927 }
Jesse Barnes674cf962011-04-28 14:27:04 -07008928 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008929 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
Wu Fengguange0dac652011-09-05 14:25:34 +08008930 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnes357555c2011-04-28 15:09:55 -07008931 } else if (IS_IVYBRIDGE(dev)) {
8932 /* FIXME: detect B0+ stepping and use auto training */
8933 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Jesse Barnesfe100d42011-04-28 14:29:45 -07008934 if (SNB_READ_WM0_LATENCY()) {
8935 dev_priv->display.update_wm = sandybridge_update_wm;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08008936 dev_priv->display.update_sprite_wm = sandybridge_update_sprite_wm;
Jesse Barnesfe100d42011-04-28 14:29:45 -07008937 } else {
8938 DRM_DEBUG_KMS("Failed to read display plane latency. "
8939 "Disable CxSR\n");
8940 dev_priv->display.update_wm = NULL;
8941 }
Jesse Barnes28963a32011-05-11 09:42:30 -07008942 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
Wu Fengguange0dac652011-09-05 14:25:34 +08008943 dev_priv->display.write_eld = ironlake_write_eld;
Zhenyu Wang7f8a8562010-04-01 13:07:53 +08008944 } else
8945 dev_priv->display.update_wm = NULL;
8946 } else if (IS_PINEVIEW(dev)) {
Zhao Yakuid4294342010-03-22 22:45:36 +08008947 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
Li Peng95534262010-05-18 18:58:44 +08008948 dev_priv->is_ddr3,
Zhao Yakuid4294342010-03-22 22:45:36 +08008949 dev_priv->fsb_freq,
8950 dev_priv->mem_freq)) {
8951 DRM_INFO("failed to find known CxSR latency "
Li Peng95534262010-05-18 18:58:44 +08008952 "(found ddr%s fsb freq %d, mem freq %d), "
Zhao Yakuid4294342010-03-22 22:45:36 +08008953 "disabling CxSR\n",
Akshay Joshi0206e352011-08-16 15:34:10 -04008954 (dev_priv->is_ddr3 == 1) ? "3" : "2",
Zhao Yakuid4294342010-03-22 22:45:36 +08008955 dev_priv->fsb_freq, dev_priv->mem_freq);
8956 /* Disable CxSR and never update its watermark again */
8957 pineview_disable_cxsr(dev);
8958 dev_priv->display.update_wm = NULL;
8959 } else
8960 dev_priv->display.update_wm = pineview_update_wm;
Jason Stubbs95e0ee92011-05-28 14:26:48 +10008961 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008962 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +08008963 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -07008964 dev_priv->display.update_wm = g4x_update_wm;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008965 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
8966 } else if (IS_GEN4(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07008967 dev_priv->display.update_wm = i965_update_wm;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008968 if (IS_CRESTLINE(dev))
8969 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
8970 else if (IS_BROADWATER(dev))
8971 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
8972 } else if (IS_GEN3(dev)) {
Jesse Barnese70236a2009-09-21 10:42:27 -07008973 dev_priv->display.update_wm = i9xx_update_wm;
8974 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008975 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
8976 } else if (IS_I865G(dev)) {
8977 dev_priv->display.update_wm = i830_update_wm;
8978 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
8979 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Adam Jackson8f4695e2010-04-16 18:20:57 -04008980 } else if (IS_I85X(dev)) {
8981 dev_priv->display.update_wm = i9xx_update_wm;
8982 dev_priv->display.get_fifo_size = i85x_get_fifo_size;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008983 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
Jesse Barnese70236a2009-09-21 10:42:27 -07008984 } else {
Adam Jackson8f4695e2010-04-16 18:20:57 -04008985 dev_priv->display.update_wm = i830_update_wm;
Jesse Barnes6067aae2011-04-28 15:04:31 -07008986 dev_priv->display.init_clock_gating = i830_init_clock_gating;
Adam Jackson8f4695e2010-04-16 18:20:57 -04008987 if (IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -07008988 dev_priv->display.get_fifo_size = i845_get_fifo_size;
8989 else
8990 dev_priv->display.get_fifo_size = i830_get_fifo_size;
Jesse Barnese70236a2009-09-21 10:42:27 -07008991 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008992
8993 /* Default just returns -ENODEV to indicate unsupported */
8994 dev_priv->display.queue_flip = intel_default_queue_flip;
8995
8996 switch (INTEL_INFO(dev)->gen) {
8997 case 2:
8998 dev_priv->display.queue_flip = intel_gen2_queue_flip;
8999 break;
9000
9001 case 3:
9002 dev_priv->display.queue_flip = intel_gen3_queue_flip;
9003 break;
9004
9005 case 4:
9006 case 5:
9007 dev_priv->display.queue_flip = intel_gen4_queue_flip;
9008 break;
9009
9010 case 6:
9011 dev_priv->display.queue_flip = intel_gen6_queue_flip;
9012 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009013 case 7:
9014 dev_priv->display.queue_flip = intel_gen7_queue_flip;
9015 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009016 }
Jesse Barnese70236a2009-09-21 10:42:27 -07009017}
9018
Jesse Barnesb690e962010-07-19 13:53:12 -07009019/*
9020 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
9021 * resume, or other times. This quirk makes sure that's the case for
9022 * affected systems.
9023 */
Akshay Joshi0206e352011-08-16 15:34:10 -04009024static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -07009025{
9026 struct drm_i915_private *dev_priv = dev->dev_private;
9027
9028 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
9029 DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
9030}
9031
Keith Packard435793d2011-07-12 14:56:22 -07009032/*
9033 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
9034 */
9035static void quirk_ssc_force_disable(struct drm_device *dev)
9036{
9037 struct drm_i915_private *dev_priv = dev->dev_private;
9038 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
9039}
9040
Carsten Emde4dca20e2012-03-15 15:56:26 +01009041/*
Carsten Emde5a15ab52012-03-15 15:56:27 +01009042 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
9043 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +01009044 */
9045static void quirk_invert_brightness(struct drm_device *dev)
9046{
9047 struct drm_i915_private *dev_priv = dev->dev_private;
9048 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
9049}
9050
Jesse Barnesb690e962010-07-19 13:53:12 -07009051struct intel_quirk {
9052 int device;
9053 int subsystem_vendor;
9054 int subsystem_device;
9055 void (*hook)(struct drm_device *dev);
9056};
9057
9058struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -07009059 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -04009060 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -07009061
9062 /* Thinkpad R31 needs pipe A force quirk */
9063 { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
9064 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
9065 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
9066
9067 /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
9068 { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
9069 /* ThinkPad X40 needs pipe A force quirk */
9070
9071 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
9072 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
9073
9074 /* 855 & before need to leave pipe A & dpll A up */
9075 { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
9076 { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -07009077
9078 /* Lenovo U160 cannot use SSC on LVDS */
9079 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +02009080
9081 /* Sony Vaio Y cannot use SSC on LVDS */
9082 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +01009083
9084 /* Acer Aspire 5734Z must invert backlight brightness */
9085 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -07009086};
9087
9088static void intel_init_quirks(struct drm_device *dev)
9089{
9090 struct pci_dev *d = dev->pdev;
9091 int i;
9092
9093 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
9094 struct intel_quirk *q = &intel_quirks[i];
9095
9096 if (d->device == q->device &&
9097 (d->subsystem_vendor == q->subsystem_vendor ||
9098 q->subsystem_vendor == PCI_ANY_ID) &&
9099 (d->subsystem_device == q->subsystem_device ||
9100 q->subsystem_device == PCI_ANY_ID))
9101 q->hook(dev);
9102 }
9103}
9104
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009105/* Disable the VGA plane that we never use */
9106static void i915_disable_vga(struct drm_device *dev)
9107{
9108 struct drm_i915_private *dev_priv = dev->dev_private;
9109 u8 sr1;
9110 u32 vga_reg;
9111
9112 if (HAS_PCH_SPLIT(dev))
9113 vga_reg = CPU_VGACNTRL;
9114 else
9115 vga_reg = VGACNTRL;
9116
9117 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
9118 outb(1, VGA_SR_INDEX);
9119 sr1 = inb(VGA_SR_DATA);
9120 outb(sr1 | 1<<5, VGA_SR_DATA);
9121 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
9122 udelay(300);
9123
9124 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
9125 POSTING_READ(vga_reg);
9126}
9127
Jesse Barnes79e53942008-11-07 14:24:08 -08009128void intel_modeset_init(struct drm_device *dev)
9129{
Jesse Barnes652c3932009-08-17 13:31:43 -07009130 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesb840d907f2011-12-13 13:19:38 -08009131 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08009132
9133 drm_mode_config_init(dev);
9134
9135 dev->mode_config.min_width = 0;
9136 dev->mode_config.min_height = 0;
9137
Dave Airlie019d96c2011-09-29 16:20:42 +01009138 dev->mode_config.preferred_depth = 24;
9139 dev->mode_config.prefer_shadow = 1;
9140
Jesse Barnes79e53942008-11-07 14:24:08 -08009141 dev->mode_config.funcs = (void *)&intel_mode_funcs;
9142
Jesse Barnesb690e962010-07-19 13:53:12 -07009143 intel_init_quirks(dev);
9144
Jesse Barnese70236a2009-09-21 10:42:27 -07009145 intel_init_display(dev);
9146
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009147 if (IS_GEN2(dev)) {
9148 dev->mode_config.max_width = 2048;
9149 dev->mode_config.max_height = 2048;
9150 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -07009151 dev->mode_config.max_width = 4096;
9152 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -08009153 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009154 dev->mode_config.max_width = 8192;
9155 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -08009156 }
Chris Wilson35c30472010-12-22 14:07:12 +00009157 dev->mode_config.fb_base = dev->agp->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009158
Zhao Yakui28c97732009-10-09 11:39:41 +08009159 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Dave Airliea3524f12010-06-06 18:59:41 +10009160 dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -08009161
Dave Airliea3524f12010-06-06 18:59:41 +10009162 for (i = 0; i < dev_priv->num_pipe; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009163 intel_crtc_init(dev, i);
Jesse Barnes00c2064b2012-01-13 15:48:39 -08009164 ret = intel_plane_init(dev, i);
9165 if (ret)
9166 DRM_DEBUG_KMS("plane %d init failed: %d\n", i, ret);
Jesse Barnes79e53942008-11-07 14:24:08 -08009167 }
9168
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009169 /* Just disable it once at startup */
9170 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009171 intel_setup_outputs(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07009172
Jesse Barnes645c62a2011-05-11 09:49:31 -07009173 intel_init_clock_gating(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -07009174
Jesse Barnes7648fa92010-05-20 14:28:11 -07009175 if (IS_IRONLAKE_M(dev)) {
Jesse Barnesf97108d2010-01-29 11:27:07 -08009176 ironlake_enable_drps(dev);
Jesse Barnes7648fa92010-05-20 14:28:11 -07009177 intel_init_emon(dev);
9178 }
Jesse Barnesf97108d2010-01-29 11:27:07 -08009179
Jesse Barnes1c70c0c2011-06-29 13:34:36 -07009180 if (IS_GEN6(dev) || IS_GEN7(dev)) {
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08009181 gen6_enable_rps(dev_priv);
Jesse Barnes23b2f8b2011-06-28 13:04:16 -07009182 gen6_update_ring_freq(dev_priv);
9183 }
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08009184
Jesse Barnes652c3932009-08-17 13:31:43 -07009185 INIT_WORK(&dev_priv->idle_work, intel_idle_update);
9186 setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
9187 (unsigned long)dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +01009188}
9189
9190void intel_modeset_gem_init(struct drm_device *dev)
9191{
9192 if (IS_IRONLAKE_M(dev))
9193 ironlake_enable_rc6(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +02009194
9195 intel_setup_overlay(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08009196}
9197
9198void intel_modeset_cleanup(struct drm_device *dev)
9199{
Jesse Barnes652c3932009-08-17 13:31:43 -07009200 struct drm_i915_private *dev_priv = dev->dev_private;
9201 struct drm_crtc *crtc;
9202 struct intel_crtc *intel_crtc;
9203
Keith Packardf87ea762010-10-03 19:36:26 -07009204 drm_kms_helper_poll_fini(dev);
Jesse Barnes652c3932009-08-17 13:31:43 -07009205 mutex_lock(&dev->struct_mutex);
9206
Jesse Barnes723bfd72010-10-07 16:01:13 -07009207 intel_unregister_dsm_handler();
9208
9209
Jesse Barnes652c3932009-08-17 13:31:43 -07009210 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9211 /* Skip inactive CRTCs */
9212 if (!crtc->fb)
9213 continue;
9214
9215 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3dec0092010-08-20 21:40:52 +02009216 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009217 }
9218
Chris Wilson973d04f2011-07-08 12:22:37 +01009219 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -07009220
Jesse Barnesf97108d2010-01-29 11:27:07 -08009221 if (IS_IRONLAKE_M(dev))
9222 ironlake_disable_drps(dev);
Jesse Barnes1c70c0c2011-06-29 13:34:36 -07009223 if (IS_GEN6(dev) || IS_GEN7(dev))
Jesse Barnes3b8d8d92010-12-17 14:19:02 -08009224 gen6_disable_rps(dev);
Jesse Barnesf97108d2010-01-29 11:27:07 -08009225
Jesse Barnesd5bb0812011-01-05 12:01:26 -08009226 if (IS_IRONLAKE_M(dev))
9227 ironlake_disable_rc6(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +00009228
Kristian Høgsberg69341a52009-11-11 12:19:17 -05009229 mutex_unlock(&dev->struct_mutex);
9230
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009231 /* Disable the irq before mode object teardown, for the irq might
9232 * enqueue unpin/hotplug work. */
9233 drm_irq_uninstall(dev);
9234 cancel_work_sync(&dev_priv->hotplug_work);
Daniel Vetter6fdd4d92011-09-08 14:00:22 +02009235 cancel_work_sync(&dev_priv->rps_work);
Daniel Vetter6c0d93502010-08-20 18:26:46 +02009236
Chris Wilson1630fe72011-07-08 12:22:42 +01009237 /* flush any delayed tasks or pending work */
9238 flush_scheduled_work();
9239
Daniel Vetter3dec0092010-08-20 21:40:52 +02009240 /* Shut off idle work before the crtcs get freed. */
9241 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
9242 intel_crtc = to_intel_crtc(crtc);
9243 del_timer_sync(&intel_crtc->idle_timer);
9244 }
9245 del_timer_sync(&dev_priv->idle_timer);
9246 cancel_work_sync(&dev_priv->idle_work);
9247
Jesse Barnes79e53942008-11-07 14:24:08 -08009248 drm_mode_config_cleanup(dev);
9249}
9250
Dave Airlie28d52042009-09-21 14:33:58 +10009251/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +08009252 * Return which encoder is currently attached for connector.
9253 */
Chris Wilsondf0e9242010-09-09 16:20:55 +01009254struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -08009255{
Chris Wilsondf0e9242010-09-09 16:20:55 +01009256 return &intel_attached_encoder(connector)->base;
9257}
Jesse Barnes79e53942008-11-07 14:24:08 -08009258
Chris Wilsondf0e9242010-09-09 16:20:55 +01009259void intel_connector_attach_encoder(struct intel_connector *connector,
9260 struct intel_encoder *encoder)
9261{
9262 connector->encoder = encoder;
9263 drm_mode_connector_attach_encoder(&connector->base,
9264 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -08009265}
Dave Airlie28d52042009-09-21 14:33:58 +10009266
9267/*
9268 * set vga decode state - true == enable VGA decode
9269 */
9270int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
9271{
9272 struct drm_i915_private *dev_priv = dev->dev_private;
9273 u16 gmch_ctrl;
9274
9275 pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
9276 if (state)
9277 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
9278 else
9279 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
9280 pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
9281 return 0;
9282}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009283
9284#ifdef CONFIG_DEBUG_FS
9285#include <linux/seq_file.h>
9286
9287struct intel_display_error_state {
9288 struct intel_cursor_error_state {
9289 u32 control;
9290 u32 position;
9291 u32 base;
9292 u32 size;
9293 } cursor[2];
9294
9295 struct intel_pipe_error_state {
9296 u32 conf;
9297 u32 source;
9298
9299 u32 htotal;
9300 u32 hblank;
9301 u32 hsync;
9302 u32 vtotal;
9303 u32 vblank;
9304 u32 vsync;
9305 } pipe[2];
9306
9307 struct intel_plane_error_state {
9308 u32 control;
9309 u32 stride;
9310 u32 size;
9311 u32 pos;
9312 u32 addr;
9313 u32 surface;
9314 u32 tile_offset;
9315 } plane[2];
9316};
9317
9318struct intel_display_error_state *
9319intel_display_capture_error_state(struct drm_device *dev)
9320{
Akshay Joshi0206e352011-08-16 15:34:10 -04009321 drm_i915_private_t *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009322 struct intel_display_error_state *error;
9323 int i;
9324
9325 error = kmalloc(sizeof(*error), GFP_ATOMIC);
9326 if (error == NULL)
9327 return NULL;
9328
9329 for (i = 0; i < 2; i++) {
9330 error->cursor[i].control = I915_READ(CURCNTR(i));
9331 error->cursor[i].position = I915_READ(CURPOS(i));
9332 error->cursor[i].base = I915_READ(CURBASE(i));
9333
9334 error->plane[i].control = I915_READ(DSPCNTR(i));
9335 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
9336 error->plane[i].size = I915_READ(DSPSIZE(i));
Akshay Joshi0206e352011-08-16 15:34:10 -04009337 error->plane[i].pos = I915_READ(DSPPOS(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +00009338 error->plane[i].addr = I915_READ(DSPADDR(i));
9339 if (INTEL_INFO(dev)->gen >= 4) {
9340 error->plane[i].surface = I915_READ(DSPSURF(i));
9341 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
9342 }
9343
9344 error->pipe[i].conf = I915_READ(PIPECONF(i));
9345 error->pipe[i].source = I915_READ(PIPESRC(i));
9346 error->pipe[i].htotal = I915_READ(HTOTAL(i));
9347 error->pipe[i].hblank = I915_READ(HBLANK(i));
9348 error->pipe[i].hsync = I915_READ(HSYNC(i));
9349 error->pipe[i].vtotal = I915_READ(VTOTAL(i));
9350 error->pipe[i].vblank = I915_READ(VBLANK(i));
9351 error->pipe[i].vsync = I915_READ(VSYNC(i));
9352 }
9353
9354 return error;
9355}
9356
9357void
9358intel_display_print_error_state(struct seq_file *m,
9359 struct drm_device *dev,
9360 struct intel_display_error_state *error)
9361{
9362 int i;
9363
9364 for (i = 0; i < 2; i++) {
9365 seq_printf(m, "Pipe [%d]:\n", i);
9366 seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
9367 seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
9368 seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
9369 seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
9370 seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
9371 seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
9372 seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
9373 seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
9374
9375 seq_printf(m, "Plane [%d]:\n", i);
9376 seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
9377 seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
9378 seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
9379 seq_printf(m, " POS: %08x\n", error->plane[i].pos);
9380 seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
9381 if (INTEL_INFO(dev)->gen >= 4) {
9382 seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
9383 seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
9384 }
9385
9386 seq_printf(m, "Cursor [%d]:\n", i);
9387 seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
9388 seq_printf(m, " POS: %08x\n", error->cursor[i].position);
9389 seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
9390 }
9391}
9392#endif